[multiple changes]
[gcc.git] / gcc / ada / sem_util.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ U T I L --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2015, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Casing; use Casing;
29 with Checks; use Checks;
30 with Debug; use Debug;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Exp_Ch11; use Exp_Ch11;
34 with Exp_Disp; use Exp_Disp;
35 with Exp_Util; use Exp_Util;
36 with Fname; use Fname;
37 with Freeze; use Freeze;
38 with Lib; use Lib;
39 with Lib.Xref; use Lib.Xref;
40 with Namet.Sp; use Namet.Sp;
41 with Nlists; use Nlists;
42 with Nmake; use Nmake;
43 with Output; use Output;
44 with Restrict; use Restrict;
45 with Rident; use Rident;
46 with Rtsfind; use Rtsfind;
47 with Sem; use Sem;
48 with Sem_Aux; use Sem_Aux;
49 with Sem_Attr; use Sem_Attr;
50 with Sem_Ch6; use Sem_Ch6;
51 with Sem_Ch8; use Sem_Ch8;
52 with Sem_Ch12; use Sem_Ch12;
53 with Sem_Ch13; use Sem_Ch13;
54 with Sem_Disp; use Sem_Disp;
55 with Sem_Eval; use Sem_Eval;
56 with Sem_Prag; use Sem_Prag;
57 with Sem_Res; use Sem_Res;
58 with Sem_Warn; use Sem_Warn;
59 with Sem_Type; use Sem_Type;
60 with Sinfo; use Sinfo;
61 with Sinput; use Sinput;
62 with Stand; use Stand;
63 with Style;
64 with Stringt; use Stringt;
65 with Targparm; use Targparm;
66 with Tbuild; use Tbuild;
67 with Ttypes; use Ttypes;
68 with Uname; use Uname;
69
70 with GNAT.HTable; use GNAT.HTable;
71
72 package body Sem_Util is
73
74 ----------------------------------------
75 -- Global Variables for New_Copy_Tree --
76 ----------------------------------------
77
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.
82
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.
87
88 NCT_Hash_Tables_Used : Boolean := False;
89 -- Set to True if hash tables are in use
90
91 NCT_Table_Entries : Nat := 0;
92 -- Count entries in table to see if threshold is reached
93
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.
99
100 subtype NCT_Header_Num is Int range 0 .. 511;
101 -- Defines range of headers in hash tables (512 headers)
102
103 -----------------------
104 -- Local Subprograms --
105 -----------------------
106
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.
114
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.
121
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.
125
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.
131
132 ------------------------------
133 -- Abstract_Interface_List --
134 ------------------------------
135
136 function Abstract_Interface_List (Typ : Entity_Id) return List_Id is
137 Nod : Node_Id;
138
139 begin
140 if Is_Concurrent_Type (Typ) then
141
142 -- If we are dealing with a synchronized subtype, go to the base
143 -- type, whose declaration has the interface list.
144
145 -- Shouldn't this be Declaration_Node???
146
147 Nod := Parent (Base_Type (Typ));
148
149 if Nkind (Nod) = N_Full_Type_Declaration then
150 return Empty_List;
151 end if;
152
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));
156
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)));
163
164 -- If the full-view is not available we cannot do anything else
165 -- here (the source has errors).
166
167 else
168 return Empty_List;
169 end if;
170
171 -- Support for generic formals with interfaces is still missing ???
172
173 elsif Nkind (Parent (Typ)) = N_Formal_Type_Declaration then
174 return Empty_List;
175
176 else
177 pragma Assert
178 (Nkind (Parent (Typ)) = N_Private_Extension_Declaration);
179 Nod := Parent (Typ);
180 end if;
181
182 elsif Ekind (Typ) = E_Record_Subtype then
183 Nod := Type_Definition (Parent (Etype (Typ)));
184
185 elsif Ekind (Typ) = E_Record_Subtype_With_Private then
186
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.
190
191 return Abstract_Interface_List (Etype (Typ));
192
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;
200
201 return Interface_List (Nod);
202 end Abstract_Interface_List;
203
204 --------------------------------
205 -- Add_Access_Type_To_Process --
206 --------------------------------
207
208 procedure Add_Access_Type_To_Process (E : Entity_Id; A : Entity_Id) is
209 L : Elist_Id;
210
211 begin
212 Ensure_Freeze_Node (E);
213 L := Access_Types_To_Process (Freeze_Node (E));
214
215 if No (L) then
216 L := New_Elmt_List;
217 Set_Access_Types_To_Process (Freeze_Node (E), L);
218 end if;
219
220 Append_Elmt (A, L);
221 end Add_Access_Type_To_Process;
222
223 --------------------------
224 -- Add_Block_Identifier --
225 --------------------------
226
227 procedure Add_Block_Identifier (N : Node_Id; Id : out Entity_Id) is
228 Loc : constant Source_Ptr := Sloc (N);
229
230 begin
231 pragma Assert (Nkind (N) = N_Block_Statement);
232
233 -- The block already has a label, return its entity
234
235 if Present (Identifier (N)) then
236 Id := Entity (Identifier (N));
237
238 -- Create a new block label and set its attributes
239
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);
244
245 Set_Identifier (N, New_Occurrence_Of (Id, Loc));
246 Set_Block_Node (Id, Identifier (N));
247 end if;
248 end Add_Block_Identifier;
249
250 -----------------------
251 -- Add_Contract_Item --
252 -----------------------
253
254 procedure Add_Contract_Item (Prag : Node_Id; Id : Entity_Id) is
255 Items : Node_Id := Contract (Id);
256
257 procedure Add_Classification;
258 -- Prepend Prag to the list of classifications
259
260 procedure Add_Contract_Test_Case;
261 -- Prepend Prag to the list of contract and test cases
262
263 procedure Add_Pre_Post_Condition;
264 -- Prepend Prag to the list of pre- and postconditions
265
266 ------------------------
267 -- Add_Classification --
268 ------------------------
269
270 procedure Add_Classification is
271 begin
272 Set_Next_Pragma (Prag, Classifications (Items));
273 Set_Classifications (Items, Prag);
274 end Add_Classification;
275
276 ----------------------------
277 -- Add_Contract_Test_Case --
278 ----------------------------
279
280 procedure Add_Contract_Test_Case is
281 begin
282 Set_Next_Pragma (Prag, Contract_Test_Cases (Items));
283 Set_Contract_Test_Cases (Items, Prag);
284 end Add_Contract_Test_Case;
285
286 ----------------------------
287 -- Add_Pre_Post_Condition --
288 ----------------------------
289
290 procedure Add_Pre_Post_Condition is
291 begin
292 Set_Next_Pragma (Prag, Pre_Post_Conditions (Items));
293 Set_Pre_Post_Conditions (Items, Prag);
294 end Add_Pre_Post_Condition;
295
296 -- Local variables
297
298 Prag_Nam : Name_Id;
299
300 -- Start of processing for Add_Contract_Item
301
302 begin
303 -- A contract must contain only pragmas
304
305 pragma Assert (Nkind (Prag) = N_Pragma);
306 Prag_Nam := Pragma_Name (Prag);
307
308 -- Create a new contract when adding the first item
309
310 if No (Items) then
311 Items := Make_Contract (Sloc (Id));
312 Set_Contract (Id, Items);
313 end if;
314
315 -- Contract items related to [generic] packages or instantiations. The
316 -- applicable pragmas are:
317 -- Abstract_States
318 -- Initial_Condition
319 -- Initializes
320 -- Part_Of (instantiation only)
321
322 if Ekind_In (Id, E_Generic_Package, E_Package) then
323 if Nam_In (Prag_Nam, Name_Abstract_State,
324 Name_Initial_Condition,
325 Name_Initializes)
326 then
327 Add_Classification;
328
329 -- Indicator Part_Of must be associated with a package instantiation
330
331 elsif Prag_Nam = Name_Part_Of and then Is_Generic_Instance (Id) then
332 Add_Classification;
333
334 -- The pragma is not a proper contract item
335
336 else
337 raise Program_Error;
338 end if;
339
340 -- Contract items related to package bodies. The applicable pragmas are:
341 -- Refined_States
342
343 elsif Ekind (Id) = E_Package_Body then
344 if Prag_Nam = Name_Refined_State then
345 Add_Classification;
346
347 -- The pragma is not a proper contract item
348
349 else
350 raise Program_Error;
351 end if;
352
353 -- Contract items related to subprogram or entry declarations. The
354 -- applicable pragmas are:
355 -- Contract_Cases
356 -- Depends
357 -- Extensions_Visible
358 -- Global
359 -- Postcondition
360 -- Precondition
361 -- Test_Case
362
363 elsif Ekind_In (Id, E_Entry, E_Entry_Family)
364 or else Is_Generic_Subprogram (Id)
365 or else Is_Subprogram (Id)
366 then
367 if Nam_In (Prag_Nam, Name_Postcondition, Name_Precondition) then
368 Add_Pre_Post_Condition;
369
370 elsif Nam_In (Prag_Nam, Name_Contract_Cases, Name_Test_Case) then
371 Add_Contract_Test_Case;
372
373 elsif Nam_In (Prag_Nam, Name_Depends,
374 Name_Extensions_Visible,
375 Name_Global)
376 then
377 Add_Classification;
378
379 -- The pragma is not a proper contract item
380
381 else
382 raise Program_Error;
383 end if;
384
385 -- Contract items related to subprogram bodies. Applicable pragmas are:
386 -- Postcondition
387 -- Precondition
388 -- Refined_Depends
389 -- Refined_Global
390 -- Refined_Post
391
392 elsif Ekind (Id) = E_Subprogram_Body then
393 if Nam_In (Prag_Nam, Name_Refined_Depends, Name_Refined_Global) then
394 Add_Classification;
395
396 elsif Nam_In (Prag_Nam, Name_Postcondition,
397 Name_Precondition,
398 Name_Refined_Post)
399 then
400 Add_Pre_Post_Condition;
401
402 -- The pragma is not a proper contract item
403
404 else
405 raise Program_Error;
406 end if;
407
408 -- Contract items related to variables. Applicable pragmas are:
409 -- Async_Readers
410 -- Async_Writers
411 -- Effective_Reads
412 -- Effective_Writes
413 -- Part_Of
414
415 elsif Ekind (Id) = E_Variable then
416 if Nam_In (Prag_Nam, Name_Async_Readers,
417 Name_Async_Writers,
418 Name_Effective_Reads,
419 Name_Effective_Writes,
420 Name_Part_Of)
421 then
422 Add_Classification;
423
424 -- The pragma is not a proper contract item
425
426 else
427 raise Program_Error;
428 end if;
429 end if;
430 end Add_Contract_Item;
431
432 ----------------------------
433 -- Add_Global_Declaration --
434 ----------------------------
435
436 procedure Add_Global_Declaration (N : Node_Id) is
437 Aux_Node : constant Node_Id := Aux_Decls_Node (Cunit (Current_Sem_Unit));
438
439 begin
440 if No (Declarations (Aux_Node)) then
441 Set_Declarations (Aux_Node, New_List);
442 end if;
443
444 Append_To (Declarations (Aux_Node), N);
445 Analyze (N);
446 end Add_Global_Declaration;
447
448 --------------------------------
449 -- Address_Integer_Convert_OK --
450 --------------------------------
451
452 function Address_Integer_Convert_OK (T1, T2 : Entity_Id) return Boolean is
453 begin
454 if Allow_Integer_Address
455 and then ((Is_Descendent_Of_Address (T1)
456 and then Is_Private_Type (T1)
457 and then Is_Integer_Type (T2))
458 or else
459 (Is_Descendent_Of_Address (T2)
460 and then Is_Private_Type (T2)
461 and then Is_Integer_Type (T1)))
462 then
463 return True;
464 else
465 return False;
466 end if;
467 end Address_Integer_Convert_OK;
468
469 -----------------
470 -- Addressable --
471 -----------------
472
473 -- For now, just 8/16/32/64. but analyze later if AAMP is special???
474
475 function Addressable (V : Uint) return Boolean is
476 begin
477 return V = Uint_8 or else
478 V = Uint_16 or else
479 V = Uint_32 or else
480 V = Uint_64;
481 end Addressable;
482
483 function Addressable (V : Int) return Boolean is
484 begin
485 return V = 8 or else
486 V = 16 or else
487 V = 32 or else
488 V = 64;
489 end Addressable;
490
491 ---------------------------------
492 -- Aggregate_Constraint_Checks --
493 ---------------------------------
494
495 procedure Aggregate_Constraint_Checks
496 (Exp : Node_Id;
497 Check_Typ : Entity_Id)
498 is
499 Exp_Typ : constant Entity_Id := Etype (Exp);
500
501 begin
502 if Raises_Constraint_Error (Exp) then
503 return;
504 end if;
505
506 -- Ada 2005 (AI-230): Generate a conversion to an anonymous access
507 -- component's type to force the appropriate accessibility checks.
508
509 -- Ada 2005 (AI-231): Generate conversion to the null-excluding
510 -- type to force the corresponding run-time check
511
512 if Is_Access_Type (Check_Typ)
513 and then ((Is_Local_Anonymous_Access (Check_Typ))
514 or else (Can_Never_Be_Null (Check_Typ)
515 and then not Can_Never_Be_Null (Exp_Typ)))
516 then
517 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
518 Analyze_And_Resolve (Exp, Check_Typ);
519 Check_Unset_Reference (Exp);
520 end if;
521
522 -- This is really expansion activity, so make sure that expansion is
523 -- on and is allowed. In GNATprove mode, we also want check flags to
524 -- be added in the tree, so that the formal verification can rely on
525 -- those to be present. In GNATprove mode for formal verification, some
526 -- treatment typically only done during expansion needs to be performed
527 -- on the tree, but it should not be applied inside generics. Otherwise,
528 -- this breaks the name resolution mechanism for generic instances.
529
530 if not Expander_Active
531 and (Inside_A_Generic or not Full_Analysis or not GNATprove_Mode)
532 then
533 return;
534 end if;
535
536 -- First check if we have to insert discriminant checks
537
538 if Has_Discriminants (Exp_Typ) then
539 Apply_Discriminant_Check (Exp, Check_Typ);
540
541 -- Next emit length checks for array aggregates
542
543 elsif Is_Array_Type (Exp_Typ) then
544 Apply_Length_Check (Exp, Check_Typ);
545
546 -- Finally emit scalar and string checks. If we are dealing with a
547 -- scalar literal we need to check by hand because the Etype of
548 -- literals is not necessarily correct.
549
550 elsif Is_Scalar_Type (Exp_Typ)
551 and then Compile_Time_Known_Value (Exp)
552 then
553 if Is_Out_Of_Range (Exp, Base_Type (Check_Typ)) then
554 Apply_Compile_Time_Constraint_Error
555 (Exp, "value not in range of}??", CE_Range_Check_Failed,
556 Ent => Base_Type (Check_Typ),
557 Typ => Base_Type (Check_Typ));
558
559 elsif Is_Out_Of_Range (Exp, Check_Typ) then
560 Apply_Compile_Time_Constraint_Error
561 (Exp, "value not in range of}??", CE_Range_Check_Failed,
562 Ent => Check_Typ,
563 Typ => Check_Typ);
564
565 elsif not Range_Checks_Suppressed (Check_Typ) then
566 Apply_Scalar_Range_Check (Exp, Check_Typ);
567 end if;
568
569 -- Verify that target type is also scalar, to prevent view anomalies
570 -- in instantiations.
571
572 elsif (Is_Scalar_Type (Exp_Typ)
573 or else Nkind (Exp) = N_String_Literal)
574 and then Is_Scalar_Type (Check_Typ)
575 and then Exp_Typ /= Check_Typ
576 then
577 if Is_Entity_Name (Exp)
578 and then Ekind (Entity (Exp)) = E_Constant
579 then
580 -- If expression is a constant, it is worthwhile checking whether
581 -- it is a bound of the type.
582
583 if (Is_Entity_Name (Type_Low_Bound (Check_Typ))
584 and then Entity (Exp) = Entity (Type_Low_Bound (Check_Typ)))
585 or else
586 (Is_Entity_Name (Type_High_Bound (Check_Typ))
587 and then Entity (Exp) = Entity (Type_High_Bound (Check_Typ)))
588 then
589 return;
590
591 else
592 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
593 Analyze_And_Resolve (Exp, Check_Typ);
594 Check_Unset_Reference (Exp);
595 end if;
596
597 -- Could use a comment on this case ???
598
599 else
600 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
601 Analyze_And_Resolve (Exp, Check_Typ);
602 Check_Unset_Reference (Exp);
603 end if;
604
605 end if;
606 end Aggregate_Constraint_Checks;
607
608 -----------------------
609 -- Alignment_In_Bits --
610 -----------------------
611
612 function Alignment_In_Bits (E : Entity_Id) return Uint is
613 begin
614 return Alignment (E) * System_Storage_Unit;
615 end Alignment_In_Bits;
616
617 ---------------------------------
618 -- Append_Inherited_Subprogram --
619 ---------------------------------
620
621 procedure Append_Inherited_Subprogram (S : Entity_Id) is
622 Par : constant Entity_Id := Alias (S);
623 -- The parent subprogram
624
625 Scop : constant Entity_Id := Scope (Par);
626 -- The scope of definition of the parent subprogram
627
628 Typ : constant Entity_Id := Defining_Entity (Parent (S));
629 -- The derived type of which S is a primitive operation
630
631 Decl : Node_Id;
632 Next_E : Entity_Id;
633
634 begin
635 if Ekind (Current_Scope) = E_Package
636 and then In_Private_Part (Current_Scope)
637 and then Has_Private_Declaration (Typ)
638 and then Is_Tagged_Type (Typ)
639 and then Scop = Current_Scope
640 then
641 -- The inherited operation is available at the earliest place after
642 -- the derived type declaration ( RM 7.3.1 (6/1)). This is only
643 -- relevant for type extensions. If the parent operation appears
644 -- after the type extension, the operation is not visible.
645
646 Decl := First
647 (Visible_Declarations
648 (Package_Specification (Current_Scope)));
649 while Present (Decl) loop
650 if Nkind (Decl) = N_Private_Extension_Declaration
651 and then Defining_Entity (Decl) = Typ
652 then
653 if Sloc (Decl) > Sloc (Par) then
654 Next_E := Next_Entity (Par);
655 Set_Next_Entity (Par, S);
656 Set_Next_Entity (S, Next_E);
657 return;
658
659 else
660 exit;
661 end if;
662 end if;
663
664 Next (Decl);
665 end loop;
666 end if;
667
668 -- If partial view is not a type extension, or it appears before the
669 -- subprogram declaration, insert normally at end of entity list.
670
671 Append_Entity (S, Current_Scope);
672 end Append_Inherited_Subprogram;
673
674 -----------------------------------------
675 -- Apply_Compile_Time_Constraint_Error --
676 -----------------------------------------
677
678 procedure Apply_Compile_Time_Constraint_Error
679 (N : Node_Id;
680 Msg : String;
681 Reason : RT_Exception_Code;
682 Ent : Entity_Id := Empty;
683 Typ : Entity_Id := Empty;
684 Loc : Source_Ptr := No_Location;
685 Rep : Boolean := True;
686 Warn : Boolean := False)
687 is
688 Stat : constant Boolean := Is_Static_Expression (N);
689 R_Stat : constant Node_Id :=
690 Make_Raise_Constraint_Error (Sloc (N), Reason => Reason);
691 Rtyp : Entity_Id;
692
693 begin
694 if No (Typ) then
695 Rtyp := Etype (N);
696 else
697 Rtyp := Typ;
698 end if;
699
700 Discard_Node
701 (Compile_Time_Constraint_Error (N, Msg, Ent, Loc, Warn => Warn));
702
703 if not Rep then
704 return;
705 end if;
706
707 -- Now we replace the node by an N_Raise_Constraint_Error node
708 -- This does not need reanalyzing, so set it as analyzed now.
709
710 Rewrite (N, R_Stat);
711 Set_Analyzed (N, True);
712
713 Set_Etype (N, Rtyp);
714 Set_Raises_Constraint_Error (N);
715
716 -- Now deal with possible local raise handling
717
718 Possible_Local_Raise (N, Standard_Constraint_Error);
719
720 -- If the original expression was marked as static, the result is
721 -- still marked as static, but the Raises_Constraint_Error flag is
722 -- always set so that further static evaluation is not attempted.
723
724 if Stat then
725 Set_Is_Static_Expression (N);
726 end if;
727 end Apply_Compile_Time_Constraint_Error;
728
729 ---------------------------
730 -- Async_Readers_Enabled --
731 ---------------------------
732
733 function Async_Readers_Enabled (Id : Entity_Id) return Boolean is
734 begin
735 return Has_Enabled_Property (Id, Name_Async_Readers);
736 end Async_Readers_Enabled;
737
738 ---------------------------
739 -- Async_Writers_Enabled --
740 ---------------------------
741
742 function Async_Writers_Enabled (Id : Entity_Id) return Boolean is
743 begin
744 return Has_Enabled_Property (Id, Name_Async_Writers);
745 end Async_Writers_Enabled;
746
747 --------------------------------------
748 -- Available_Full_View_Of_Component --
749 --------------------------------------
750
751 function Available_Full_View_Of_Component (T : Entity_Id) return Boolean is
752 ST : constant Entity_Id := Scope (T);
753 SCT : constant Entity_Id := Scope (Component_Type (T));
754 begin
755 return In_Open_Scopes (ST)
756 and then In_Open_Scopes (SCT)
757 and then Scope_Depth (ST) >= Scope_Depth (SCT);
758 end Available_Full_View_Of_Component;
759
760 -------------------
761 -- Bad_Attribute --
762 -------------------
763
764 procedure Bad_Attribute
765 (N : Node_Id;
766 Nam : Name_Id;
767 Warn : Boolean := False)
768 is
769 begin
770 Error_Msg_Warn := Warn;
771 Error_Msg_N ("unrecognized attribute&<<", N);
772
773 -- Check for possible misspelling
774
775 Error_Msg_Name_1 := First_Attribute_Name;
776 while Error_Msg_Name_1 <= Last_Attribute_Name loop
777 if Is_Bad_Spelling_Of (Nam, Error_Msg_Name_1) then
778 Error_Msg_N -- CODEFIX
779 ("\possible misspelling of %<<", N);
780 exit;
781 end if;
782
783 Error_Msg_Name_1 := Error_Msg_Name_1 + 1;
784 end loop;
785 end Bad_Attribute;
786
787 --------------------------------
788 -- Bad_Predicated_Subtype_Use --
789 --------------------------------
790
791 procedure Bad_Predicated_Subtype_Use
792 (Msg : String;
793 N : Node_Id;
794 Typ : Entity_Id;
795 Suggest_Static : Boolean := False)
796 is
797 Gen : Entity_Id;
798
799 begin
800 -- Avoid cascaded errors
801
802 if Error_Posted (N) then
803 return;
804 end if;
805
806 if Inside_A_Generic then
807 Gen := Current_Scope;
808 while Present (Gen) and then Ekind (Gen) /= E_Generic_Package loop
809 Gen := Scope (Gen);
810 end loop;
811
812 if No (Gen) then
813 return;
814 end if;
815
816 if Is_Generic_Formal (Typ) and then Is_Discrete_Type (Typ) then
817 Set_No_Predicate_On_Actual (Typ);
818 end if;
819
820 elsif Has_Predicates (Typ) then
821 if Is_Generic_Actual_Type (Typ) then
822
823 -- The restriction on loop parameters is only that the type
824 -- should have no dynamic predicates.
825
826 if Nkind (Parent (N)) = N_Loop_Parameter_Specification
827 and then not Has_Dynamic_Predicate_Aspect (Typ)
828 and then Is_OK_Static_Subtype (Typ)
829 then
830 return;
831 end if;
832
833 Gen := Current_Scope;
834 while not Is_Generic_Instance (Gen) loop
835 Gen := Scope (Gen);
836 end loop;
837
838 pragma Assert (Present (Gen));
839
840 if Ekind (Gen) = E_Package and then In_Package_Body (Gen) then
841 Error_Msg_Warn := SPARK_Mode /= On;
842 Error_Msg_FE (Msg & "<<", N, Typ);
843 Error_Msg_F ("\Program_Error [<<", N);
844
845 Insert_Action (N,
846 Make_Raise_Program_Error (Sloc (N),
847 Reason => PE_Bad_Predicated_Generic_Type));
848
849 else
850 Error_Msg_FE (Msg & "<<", N, Typ);
851 end if;
852
853 else
854 Error_Msg_FE (Msg, N, Typ);
855 end if;
856
857 -- Emit an optional suggestion on how to remedy the error if the
858 -- context warrants it.
859
860 if Suggest_Static and then Has_Static_Predicate (Typ) then
861 Error_Msg_FE ("\predicate of & should be marked static", N, Typ);
862 end if;
863 end if;
864 end Bad_Predicated_Subtype_Use;
865
866 -----------------------------------------
867 -- Bad_Unordered_Enumeration_Reference --
868 -----------------------------------------
869
870 function Bad_Unordered_Enumeration_Reference
871 (N : Node_Id;
872 T : Entity_Id) return Boolean
873 is
874 begin
875 return Is_Enumeration_Type (T)
876 and then Warn_On_Unordered_Enumeration_Type
877 and then not Is_Generic_Type (T)
878 and then Comes_From_Source (N)
879 and then not Has_Pragma_Ordered (T)
880 and then not In_Same_Extended_Unit (N, T);
881 end Bad_Unordered_Enumeration_Reference;
882
883 --------------------------
884 -- Build_Actual_Subtype --
885 --------------------------
886
887 function Build_Actual_Subtype
888 (T : Entity_Id;
889 N : Node_Or_Entity_Id) return Node_Id
890 is
891 Loc : Source_Ptr;
892 -- Normally Sloc (N), but may point to corresponding body in some cases
893
894 Constraints : List_Id;
895 Decl : Node_Id;
896 Discr : Entity_Id;
897 Hi : Node_Id;
898 Lo : Node_Id;
899 Subt : Entity_Id;
900 Disc_Type : Entity_Id;
901 Obj : Node_Id;
902
903 begin
904 Loc := Sloc (N);
905
906 if Nkind (N) = N_Defining_Identifier then
907 Obj := New_Occurrence_Of (N, Loc);
908
909 -- If this is a formal parameter of a subprogram declaration, and
910 -- we are compiling the body, we want the declaration for the
911 -- actual subtype to carry the source position of the body, to
912 -- prevent anomalies in gdb when stepping through the code.
913
914 if Is_Formal (N) then
915 declare
916 Decl : constant Node_Id := Unit_Declaration_Node (Scope (N));
917 begin
918 if Nkind (Decl) = N_Subprogram_Declaration
919 and then Present (Corresponding_Body (Decl))
920 then
921 Loc := Sloc (Corresponding_Body (Decl));
922 end if;
923 end;
924 end if;
925
926 else
927 Obj := N;
928 end if;
929
930 if Is_Array_Type (T) then
931 Constraints := New_List;
932 for J in 1 .. Number_Dimensions (T) loop
933
934 -- Build an array subtype declaration with the nominal subtype and
935 -- the bounds of the actual. Add the declaration in front of the
936 -- local declarations for the subprogram, for analysis before any
937 -- reference to the formal in the body.
938
939 Lo :=
940 Make_Attribute_Reference (Loc,
941 Prefix =>
942 Duplicate_Subexpr_No_Checks (Obj, Name_Req => True),
943 Attribute_Name => Name_First,
944 Expressions => New_List (
945 Make_Integer_Literal (Loc, J)));
946
947 Hi :=
948 Make_Attribute_Reference (Loc,
949 Prefix =>
950 Duplicate_Subexpr_No_Checks (Obj, Name_Req => True),
951 Attribute_Name => Name_Last,
952 Expressions => New_List (
953 Make_Integer_Literal (Loc, J)));
954
955 Append (Make_Range (Loc, Lo, Hi), Constraints);
956 end loop;
957
958 -- If the type has unknown discriminants there is no constrained
959 -- subtype to build. This is never called for a formal or for a
960 -- lhs, so returning the type is ok ???
961
962 elsif Has_Unknown_Discriminants (T) then
963 return T;
964
965 else
966 Constraints := New_List;
967
968 -- Type T is a generic derived type, inherit the discriminants from
969 -- the parent type.
970
971 if Is_Private_Type (T)
972 and then No (Full_View (T))
973
974 -- T was flagged as an error if it was declared as a formal
975 -- derived type with known discriminants. In this case there
976 -- is no need to look at the parent type since T already carries
977 -- its own discriminants.
978
979 and then not Error_Posted (T)
980 then
981 Disc_Type := Etype (Base_Type (T));
982 else
983 Disc_Type := T;
984 end if;
985
986 Discr := First_Discriminant (Disc_Type);
987 while Present (Discr) loop
988 Append_To (Constraints,
989 Make_Selected_Component (Loc,
990 Prefix =>
991 Duplicate_Subexpr_No_Checks (Obj),
992 Selector_Name => New_Occurrence_Of (Discr, Loc)));
993 Next_Discriminant (Discr);
994 end loop;
995 end if;
996
997 Subt := Make_Temporary (Loc, 'S', Related_Node => N);
998 Set_Is_Internal (Subt);
999
1000 Decl :=
1001 Make_Subtype_Declaration (Loc,
1002 Defining_Identifier => Subt,
1003 Subtype_Indication =>
1004 Make_Subtype_Indication (Loc,
1005 Subtype_Mark => New_Occurrence_Of (T, Loc),
1006 Constraint =>
1007 Make_Index_Or_Discriminant_Constraint (Loc,
1008 Constraints => Constraints)));
1009
1010 Mark_Rewrite_Insertion (Decl);
1011 return Decl;
1012 end Build_Actual_Subtype;
1013
1014 ---------------------------------------
1015 -- Build_Actual_Subtype_Of_Component --
1016 ---------------------------------------
1017
1018 function Build_Actual_Subtype_Of_Component
1019 (T : Entity_Id;
1020 N : Node_Id) return Node_Id
1021 is
1022 Loc : constant Source_Ptr := Sloc (N);
1023 P : constant Node_Id := Prefix (N);
1024 D : Elmt_Id;
1025 Id : Node_Id;
1026 Index_Typ : Entity_Id;
1027
1028 Desig_Typ : Entity_Id;
1029 -- This is either a copy of T, or if T is an access type, then it is
1030 -- the directly designated type of this access type.
1031
1032 function Build_Actual_Array_Constraint return List_Id;
1033 -- If one or more of the bounds of the component depends on
1034 -- discriminants, build actual constraint using the discriminants
1035 -- of the prefix.
1036
1037 function Build_Actual_Record_Constraint return List_Id;
1038 -- Similar to previous one, for discriminated components constrained
1039 -- by the discriminant of the enclosing object.
1040
1041 -----------------------------------
1042 -- Build_Actual_Array_Constraint --
1043 -----------------------------------
1044
1045 function Build_Actual_Array_Constraint return List_Id is
1046 Constraints : constant List_Id := New_List;
1047 Indx : Node_Id;
1048 Hi : Node_Id;
1049 Lo : Node_Id;
1050 Old_Hi : Node_Id;
1051 Old_Lo : Node_Id;
1052
1053 begin
1054 Indx := First_Index (Desig_Typ);
1055 while Present (Indx) loop
1056 Old_Lo := Type_Low_Bound (Etype (Indx));
1057 Old_Hi := Type_High_Bound (Etype (Indx));
1058
1059 if Denotes_Discriminant (Old_Lo) then
1060 Lo :=
1061 Make_Selected_Component (Loc,
1062 Prefix => New_Copy_Tree (P),
1063 Selector_Name => New_Occurrence_Of (Entity (Old_Lo), Loc));
1064
1065 else
1066 Lo := New_Copy_Tree (Old_Lo);
1067
1068 -- The new bound will be reanalyzed in the enclosing
1069 -- declaration. For literal bounds that come from a type
1070 -- declaration, the type of the context must be imposed, so
1071 -- insure that analysis will take place. For non-universal
1072 -- types this is not strictly necessary.
1073
1074 Set_Analyzed (Lo, False);
1075 end if;
1076
1077 if Denotes_Discriminant (Old_Hi) then
1078 Hi :=
1079 Make_Selected_Component (Loc,
1080 Prefix => New_Copy_Tree (P),
1081 Selector_Name => New_Occurrence_Of (Entity (Old_Hi), Loc));
1082
1083 else
1084 Hi := New_Copy_Tree (Old_Hi);
1085 Set_Analyzed (Hi, False);
1086 end if;
1087
1088 Append (Make_Range (Loc, Lo, Hi), Constraints);
1089 Next_Index (Indx);
1090 end loop;
1091
1092 return Constraints;
1093 end Build_Actual_Array_Constraint;
1094
1095 ------------------------------------
1096 -- Build_Actual_Record_Constraint --
1097 ------------------------------------
1098
1099 function Build_Actual_Record_Constraint return List_Id is
1100 Constraints : constant List_Id := New_List;
1101 D : Elmt_Id;
1102 D_Val : Node_Id;
1103
1104 begin
1105 D := First_Elmt (Discriminant_Constraint (Desig_Typ));
1106 while Present (D) loop
1107 if Denotes_Discriminant (Node (D)) then
1108 D_Val := Make_Selected_Component (Loc,
1109 Prefix => New_Copy_Tree (P),
1110 Selector_Name => New_Occurrence_Of (Entity (Node (D)), Loc));
1111
1112 else
1113 D_Val := New_Copy_Tree (Node (D));
1114 end if;
1115
1116 Append (D_Val, Constraints);
1117 Next_Elmt (D);
1118 end loop;
1119
1120 return Constraints;
1121 end Build_Actual_Record_Constraint;
1122
1123 -- Start of processing for Build_Actual_Subtype_Of_Component
1124
1125 begin
1126 -- Why the test for Spec_Expression mode here???
1127
1128 if In_Spec_Expression then
1129 return Empty;
1130
1131 -- More comments for the rest of this body would be good ???
1132
1133 elsif Nkind (N) = N_Explicit_Dereference then
1134 if Is_Composite_Type (T)
1135 and then not Is_Constrained (T)
1136 and then not (Is_Class_Wide_Type (T)
1137 and then Is_Constrained (Root_Type (T)))
1138 and then not Has_Unknown_Discriminants (T)
1139 then
1140 -- If the type of the dereference is already constrained, it is an
1141 -- actual subtype.
1142
1143 if Is_Array_Type (Etype (N))
1144 and then Is_Constrained (Etype (N))
1145 then
1146 return Empty;
1147 else
1148 Remove_Side_Effects (P);
1149 return Build_Actual_Subtype (T, N);
1150 end if;
1151 else
1152 return Empty;
1153 end if;
1154 end if;
1155
1156 if Ekind (T) = E_Access_Subtype then
1157 Desig_Typ := Designated_Type (T);
1158 else
1159 Desig_Typ := T;
1160 end if;
1161
1162 if Ekind (Desig_Typ) = E_Array_Subtype then
1163 Id := First_Index (Desig_Typ);
1164 while Present (Id) loop
1165 Index_Typ := Underlying_Type (Etype (Id));
1166
1167 if Denotes_Discriminant (Type_Low_Bound (Index_Typ))
1168 or else
1169 Denotes_Discriminant (Type_High_Bound (Index_Typ))
1170 then
1171 Remove_Side_Effects (P);
1172 return
1173 Build_Component_Subtype
1174 (Build_Actual_Array_Constraint, Loc, Base_Type (T));
1175 end if;
1176
1177 Next_Index (Id);
1178 end loop;
1179
1180 elsif Is_Composite_Type (Desig_Typ)
1181 and then Has_Discriminants (Desig_Typ)
1182 and then not Has_Unknown_Discriminants (Desig_Typ)
1183 then
1184 if Is_Private_Type (Desig_Typ)
1185 and then No (Discriminant_Constraint (Desig_Typ))
1186 then
1187 Desig_Typ := Full_View (Desig_Typ);
1188 end if;
1189
1190 D := First_Elmt (Discriminant_Constraint (Desig_Typ));
1191 while Present (D) loop
1192 if Denotes_Discriminant (Node (D)) then
1193 Remove_Side_Effects (P);
1194 return
1195 Build_Component_Subtype (
1196 Build_Actual_Record_Constraint, Loc, Base_Type (T));
1197 end if;
1198
1199 Next_Elmt (D);
1200 end loop;
1201 end if;
1202
1203 -- If none of the above, the actual and nominal subtypes are the same
1204
1205 return Empty;
1206 end Build_Actual_Subtype_Of_Component;
1207
1208 -----------------------------
1209 -- Build_Component_Subtype --
1210 -----------------------------
1211
1212 function Build_Component_Subtype
1213 (C : List_Id;
1214 Loc : Source_Ptr;
1215 T : Entity_Id) return Node_Id
1216 is
1217 Subt : Entity_Id;
1218 Decl : Node_Id;
1219
1220 begin
1221 -- Unchecked_Union components do not require component subtypes
1222
1223 if Is_Unchecked_Union (T) then
1224 return Empty;
1225 end if;
1226
1227 Subt := Make_Temporary (Loc, 'S');
1228 Set_Is_Internal (Subt);
1229
1230 Decl :=
1231 Make_Subtype_Declaration (Loc,
1232 Defining_Identifier => Subt,
1233 Subtype_Indication =>
1234 Make_Subtype_Indication (Loc,
1235 Subtype_Mark => New_Occurrence_Of (Base_Type (T), Loc),
1236 Constraint =>
1237 Make_Index_Or_Discriminant_Constraint (Loc,
1238 Constraints => C)));
1239
1240 Mark_Rewrite_Insertion (Decl);
1241 return Decl;
1242 end Build_Component_Subtype;
1243
1244 ----------------------------------
1245 -- Build_Default_Init_Cond_Call --
1246 ----------------------------------
1247
1248 function Build_Default_Init_Cond_Call
1249 (Loc : Source_Ptr;
1250 Obj_Id : Entity_Id;
1251 Typ : Entity_Id) return Node_Id
1252 is
1253 Proc_Id : constant Entity_Id := Default_Init_Cond_Procedure (Typ);
1254 Formal_Typ : constant Entity_Id := Etype (First_Formal (Proc_Id));
1255
1256 begin
1257 return
1258 Make_Procedure_Call_Statement (Loc,
1259 Name => New_Occurrence_Of (Proc_Id, Loc),
1260 Parameter_Associations => New_List (
1261 Make_Unchecked_Type_Conversion (Loc,
1262 Subtype_Mark => New_Occurrence_Of (Formal_Typ, Loc),
1263 Expression => New_Occurrence_Of (Obj_Id, Loc))));
1264 end Build_Default_Init_Cond_Call;
1265
1266 ----------------------------------------------
1267 -- Build_Default_Init_Cond_Procedure_Bodies --
1268 ----------------------------------------------
1269
1270 procedure Build_Default_Init_Cond_Procedure_Bodies (Priv_Decls : List_Id) is
1271 procedure Build_Default_Init_Cond_Procedure_Body (Typ : Entity_Id);
1272 -- If type Typ is subject to pragma Default_Initial_Condition, build the
1273 -- body of the procedure which verifies the assumption of the pragma at
1274 -- run time. The generated body is added after the type declaration.
1275
1276 --------------------------------------------
1277 -- Build_Default_Init_Cond_Procedure_Body --
1278 --------------------------------------------
1279
1280 procedure Build_Default_Init_Cond_Procedure_Body (Typ : Entity_Id) is
1281 Param_Id : Entity_Id;
1282 -- The entity of the sole formal parameter of the default initial
1283 -- condition procedure.
1284
1285 procedure Replace_Type_Reference (N : Node_Id);
1286 -- Replace a single reference to type Typ with a reference to formal
1287 -- parameter Param_Id.
1288
1289 ----------------------------
1290 -- Replace_Type_Reference --
1291 ----------------------------
1292
1293 procedure Replace_Type_Reference (N : Node_Id) is
1294 begin
1295 Rewrite (N, New_Occurrence_Of (Param_Id, Sloc (N)));
1296 end Replace_Type_Reference;
1297
1298 procedure Replace_Type_References is
1299 new Replace_Type_References_Generic (Replace_Type_Reference);
1300
1301 -- Local variables
1302
1303 Loc : constant Source_Ptr := Sloc (Typ);
1304 Prag : constant Node_Id :=
1305 Get_Pragma (Typ, Pragma_Default_Initial_Condition);
1306 Proc_Id : constant Entity_Id := Default_Init_Cond_Procedure (Typ);
1307 Spec_Decl : constant Node_Id := Unit_Declaration_Node (Proc_Id);
1308 Body_Decl : Node_Id;
1309 Expr : Node_Id;
1310 Stmt : Node_Id;
1311
1312 -- Start of processing for Build_Default_Init_Cond_Procedure_Body
1313
1314 begin
1315 -- The procedure should be generated only for [sub]types subject to
1316 -- pragma Default_Initial_Condition. Types that inherit the pragma do
1317 -- not get this specialized procedure.
1318
1319 pragma Assert (Has_Default_Init_Cond (Typ));
1320 pragma Assert (Present (Prag));
1321 pragma Assert (Present (Proc_Id));
1322
1323 -- Nothing to do if the body was already built
1324
1325 if Present (Corresponding_Body (Spec_Decl)) then
1326 return;
1327 end if;
1328
1329 Param_Id := First_Formal (Proc_Id);
1330
1331 -- The pragma has an argument. Note that the argument is analyzed
1332 -- after all references to the current instance of the type are
1333 -- replaced.
1334
1335 if Present (Pragma_Argument_Associations (Prag)) then
1336 Expr :=
1337 Get_Pragma_Arg (First (Pragma_Argument_Associations (Prag)));
1338
1339 if Nkind (Expr) = N_Null then
1340 Stmt := Make_Null_Statement (Loc);
1341
1342 -- Preserve the original argument of the pragma by replicating it.
1343 -- Replace all references to the current instance of the type with
1344 -- references to the formal parameter.
1345
1346 else
1347 Expr := New_Copy_Tree (Expr);
1348 Replace_Type_References (Expr, Typ);
1349
1350 -- Generate:
1351 -- pragma Check (Default_Initial_Condition, <Expr>);
1352
1353 Stmt :=
1354 Make_Pragma (Loc,
1355 Pragma_Identifier =>
1356 Make_Identifier (Loc, Name_Check),
1357
1358 Pragma_Argument_Associations => New_List (
1359 Make_Pragma_Argument_Association (Loc,
1360 Expression =>
1361 Make_Identifier (Loc,
1362 Chars => Name_Default_Initial_Condition)),
1363 Make_Pragma_Argument_Association (Loc,
1364 Expression => Expr)));
1365 end if;
1366
1367 -- Otherwise the pragma appears without an argument
1368
1369 else
1370 Stmt := Make_Null_Statement (Loc);
1371 end if;
1372
1373 -- Generate:
1374 -- procedure <Typ>Default_Init_Cond (I : <Typ>) is
1375 -- begin
1376 -- <Stmt>;
1377 -- end <Typ>Default_Init_Cond;
1378
1379 Body_Decl :=
1380 Make_Subprogram_Body (Loc,
1381 Specification =>
1382 Copy_Separate_Tree (Specification (Spec_Decl)),
1383 Declarations => Empty_List,
1384 Handled_Statement_Sequence =>
1385 Make_Handled_Sequence_Of_Statements (Loc,
1386 Statements => New_List (Stmt)));
1387
1388 -- Link the spec and body of the default initial condition procedure
1389 -- to prevent the generation of a duplicate body.
1390
1391 Set_Corresponding_Body (Spec_Decl, Defining_Entity (Body_Decl));
1392 Set_Corresponding_Spec (Body_Decl, Proc_Id);
1393
1394 Insert_After_And_Analyze (Declaration_Node (Typ), Body_Decl);
1395 end Build_Default_Init_Cond_Procedure_Body;
1396
1397 -- Local variables
1398
1399 Decl : Node_Id;
1400 Typ : Entity_Id;
1401
1402 -- Start of processing for Build_Default_Init_Cond_Procedure_Bodies
1403
1404 begin
1405 -- Inspect the private declarations looking for [sub]type declarations
1406
1407 Decl := First (Priv_Decls);
1408 while Present (Decl) loop
1409 if Nkind_In (Decl, N_Full_Type_Declaration,
1410 N_Subtype_Declaration)
1411 then
1412 Typ := Defining_Entity (Decl);
1413
1414 -- Guard against partially decorate types due to previous errors
1415
1416 if Is_Type (Typ) then
1417
1418 -- If the type is subject to pragma Default_Initial_Condition,
1419 -- generate the body of the internal procedure which verifies
1420 -- the assertion of the pragma at run time.
1421
1422 if Has_Default_Init_Cond (Typ) then
1423 Build_Default_Init_Cond_Procedure_Body (Typ);
1424
1425 -- A derived type inherits the default initial condition
1426 -- procedure from its parent type.
1427
1428 elsif Has_Inherited_Default_Init_Cond (Typ) then
1429 Inherit_Default_Init_Cond_Procedure (Typ);
1430 end if;
1431 end if;
1432 end if;
1433
1434 Next (Decl);
1435 end loop;
1436 end Build_Default_Init_Cond_Procedure_Bodies;
1437
1438 ---------------------------------------------------
1439 -- Build_Default_Init_Cond_Procedure_Declaration --
1440 ---------------------------------------------------
1441
1442 procedure Build_Default_Init_Cond_Procedure_Declaration (Typ : Entity_Id) is
1443 Loc : constant Source_Ptr := Sloc (Typ);
1444 Prag : constant Node_Id :=
1445 Get_Pragma (Typ, Pragma_Default_Initial_Condition);
1446 Proc_Id : Entity_Id;
1447
1448 begin
1449 -- The procedure should be generated only for types subject to pragma
1450 -- Default_Initial_Condition. Types that inherit the pragma do not get
1451 -- this specialized procedure.
1452
1453 pragma Assert (Has_Default_Init_Cond (Typ));
1454 pragma Assert (Present (Prag));
1455
1456 -- Nothing to do if default initial condition procedure already built
1457
1458 if Present (Default_Init_Cond_Procedure (Typ)) then
1459 return;
1460 end if;
1461
1462 Proc_Id :=
1463 Make_Defining_Identifier (Loc,
1464 Chars => New_External_Name (Chars (Typ), "Default_Init_Cond"));
1465
1466 -- Associate default initial condition procedure with the private type
1467
1468 Set_Ekind (Proc_Id, E_Procedure);
1469 Set_Is_Default_Init_Cond_Procedure (Proc_Id);
1470 Set_Default_Init_Cond_Procedure (Typ, Proc_Id);
1471
1472 -- Generate:
1473 -- procedure <Typ>Default_Init_Cond (Inn : <Typ>);
1474
1475 Insert_After_And_Analyze (Prag,
1476 Make_Subprogram_Declaration (Loc,
1477 Specification =>
1478 Make_Procedure_Specification (Loc,
1479 Defining_Unit_Name => Proc_Id,
1480 Parameter_Specifications => New_List (
1481 Make_Parameter_Specification (Loc,
1482 Defining_Identifier => Make_Temporary (Loc, 'I'),
1483 Parameter_Type => New_Occurrence_Of (Typ, Loc))))));
1484 end Build_Default_Init_Cond_Procedure_Declaration;
1485
1486 ---------------------------
1487 -- Build_Default_Subtype --
1488 ---------------------------
1489
1490 function Build_Default_Subtype
1491 (T : Entity_Id;
1492 N : Node_Id) return Entity_Id
1493 is
1494 Loc : constant Source_Ptr := Sloc (N);
1495 Disc : Entity_Id;
1496
1497 Bas : Entity_Id;
1498 -- The base type that is to be constrained by the defaults
1499
1500 begin
1501 if not Has_Discriminants (T) or else Is_Constrained (T) then
1502 return T;
1503 end if;
1504
1505 Bas := Base_Type (T);
1506
1507 -- If T is non-private but its base type is private, this is the
1508 -- completion of a subtype declaration whose parent type is private
1509 -- (see Complete_Private_Subtype in Sem_Ch3). The proper discriminants
1510 -- are to be found in the full view of the base. Check that the private
1511 -- status of T and its base differ.
1512
1513 if Is_Private_Type (Bas)
1514 and then not Is_Private_Type (T)
1515 and then Present (Full_View (Bas))
1516 then
1517 Bas := Full_View (Bas);
1518 end if;
1519
1520 Disc := First_Discriminant (T);
1521
1522 if No (Discriminant_Default_Value (Disc)) then
1523 return T;
1524 end if;
1525
1526 declare
1527 Act : constant Entity_Id := Make_Temporary (Loc, 'S');
1528 Constraints : constant List_Id := New_List;
1529 Decl : Node_Id;
1530
1531 begin
1532 while Present (Disc) loop
1533 Append_To (Constraints,
1534 New_Copy_Tree (Discriminant_Default_Value (Disc)));
1535 Next_Discriminant (Disc);
1536 end loop;
1537
1538 Decl :=
1539 Make_Subtype_Declaration (Loc,
1540 Defining_Identifier => Act,
1541 Subtype_Indication =>
1542 Make_Subtype_Indication (Loc,
1543 Subtype_Mark => New_Occurrence_Of (Bas, Loc),
1544 Constraint =>
1545 Make_Index_Or_Discriminant_Constraint (Loc,
1546 Constraints => Constraints)));
1547
1548 Insert_Action (N, Decl);
1549
1550 -- If the context is a component declaration the subtype declaration
1551 -- will be analyzed when the enclosing type is frozen, otherwise do
1552 -- it now.
1553
1554 if Ekind (Current_Scope) /= E_Record_Type then
1555 Analyze (Decl);
1556 end if;
1557
1558 return Act;
1559 end;
1560 end Build_Default_Subtype;
1561
1562 --------------------------------------------
1563 -- Build_Discriminal_Subtype_Of_Component --
1564 --------------------------------------------
1565
1566 function Build_Discriminal_Subtype_Of_Component
1567 (T : Entity_Id) return Node_Id
1568 is
1569 Loc : constant Source_Ptr := Sloc (T);
1570 D : Elmt_Id;
1571 Id : Node_Id;
1572
1573 function Build_Discriminal_Array_Constraint return List_Id;
1574 -- If one or more of the bounds of the component depends on
1575 -- discriminants, build actual constraint using the discriminants
1576 -- of the prefix.
1577
1578 function Build_Discriminal_Record_Constraint return List_Id;
1579 -- Similar to previous one, for discriminated components constrained by
1580 -- the discriminant of the enclosing object.
1581
1582 ----------------------------------------
1583 -- Build_Discriminal_Array_Constraint --
1584 ----------------------------------------
1585
1586 function Build_Discriminal_Array_Constraint return List_Id is
1587 Constraints : constant List_Id := New_List;
1588 Indx : Node_Id;
1589 Hi : Node_Id;
1590 Lo : Node_Id;
1591 Old_Hi : Node_Id;
1592 Old_Lo : Node_Id;
1593
1594 begin
1595 Indx := First_Index (T);
1596 while Present (Indx) loop
1597 Old_Lo := Type_Low_Bound (Etype (Indx));
1598 Old_Hi := Type_High_Bound (Etype (Indx));
1599
1600 if Denotes_Discriminant (Old_Lo) then
1601 Lo := New_Occurrence_Of (Discriminal (Entity (Old_Lo)), Loc);
1602
1603 else
1604 Lo := New_Copy_Tree (Old_Lo);
1605 end if;
1606
1607 if Denotes_Discriminant (Old_Hi) then
1608 Hi := New_Occurrence_Of (Discriminal (Entity (Old_Hi)), Loc);
1609
1610 else
1611 Hi := New_Copy_Tree (Old_Hi);
1612 end if;
1613
1614 Append (Make_Range (Loc, Lo, Hi), Constraints);
1615 Next_Index (Indx);
1616 end loop;
1617
1618 return Constraints;
1619 end Build_Discriminal_Array_Constraint;
1620
1621 -----------------------------------------
1622 -- Build_Discriminal_Record_Constraint --
1623 -----------------------------------------
1624
1625 function Build_Discriminal_Record_Constraint return List_Id is
1626 Constraints : constant List_Id := New_List;
1627 D : Elmt_Id;
1628 D_Val : Node_Id;
1629
1630 begin
1631 D := First_Elmt (Discriminant_Constraint (T));
1632 while Present (D) loop
1633 if Denotes_Discriminant (Node (D)) then
1634 D_Val :=
1635 New_Occurrence_Of (Discriminal (Entity (Node (D))), Loc);
1636 else
1637 D_Val := New_Copy_Tree (Node (D));
1638 end if;
1639
1640 Append (D_Val, Constraints);
1641 Next_Elmt (D);
1642 end loop;
1643
1644 return Constraints;
1645 end Build_Discriminal_Record_Constraint;
1646
1647 -- Start of processing for Build_Discriminal_Subtype_Of_Component
1648
1649 begin
1650 if Ekind (T) = E_Array_Subtype then
1651 Id := First_Index (T);
1652 while Present (Id) loop
1653 if Denotes_Discriminant (Type_Low_Bound (Etype (Id)))
1654 or else
1655 Denotes_Discriminant (Type_High_Bound (Etype (Id)))
1656 then
1657 return Build_Component_Subtype
1658 (Build_Discriminal_Array_Constraint, Loc, T);
1659 end if;
1660
1661 Next_Index (Id);
1662 end loop;
1663
1664 elsif Ekind (T) = E_Record_Subtype
1665 and then Has_Discriminants (T)
1666 and then not Has_Unknown_Discriminants (T)
1667 then
1668 D := First_Elmt (Discriminant_Constraint (T));
1669 while Present (D) loop
1670 if Denotes_Discriminant (Node (D)) then
1671 return Build_Component_Subtype
1672 (Build_Discriminal_Record_Constraint, Loc, T);
1673 end if;
1674
1675 Next_Elmt (D);
1676 end loop;
1677 end if;
1678
1679 -- If none of the above, the actual and nominal subtypes are the same
1680
1681 return Empty;
1682 end Build_Discriminal_Subtype_Of_Component;
1683
1684 ------------------------------
1685 -- Build_Elaboration_Entity --
1686 ------------------------------
1687
1688 procedure Build_Elaboration_Entity (N : Node_Id; Spec_Id : Entity_Id) is
1689 Loc : constant Source_Ptr := Sloc (N);
1690 Decl : Node_Id;
1691 Elab_Ent : Entity_Id;
1692
1693 procedure Set_Package_Name (Ent : Entity_Id);
1694 -- Given an entity, sets the fully qualified name of the entity in
1695 -- Name_Buffer, with components separated by double underscores. This
1696 -- is a recursive routine that climbs the scope chain to Standard.
1697
1698 ----------------------
1699 -- Set_Package_Name --
1700 ----------------------
1701
1702 procedure Set_Package_Name (Ent : Entity_Id) is
1703 begin
1704 if Scope (Ent) /= Standard_Standard then
1705 Set_Package_Name (Scope (Ent));
1706
1707 declare
1708 Nam : constant String := Get_Name_String (Chars (Ent));
1709 begin
1710 Name_Buffer (Name_Len + 1) := '_';
1711 Name_Buffer (Name_Len + 2) := '_';
1712 Name_Buffer (Name_Len + 3 .. Name_Len + Nam'Length + 2) := Nam;
1713 Name_Len := Name_Len + Nam'Length + 2;
1714 end;
1715
1716 else
1717 Get_Name_String (Chars (Ent));
1718 end if;
1719 end Set_Package_Name;
1720
1721 -- Start of processing for Build_Elaboration_Entity
1722
1723 begin
1724 -- Ignore call if already constructed
1725
1726 if Present (Elaboration_Entity (Spec_Id)) then
1727 return;
1728
1729 -- Ignore in ASIS mode, elaboration entity is not in source and plays
1730 -- no role in analysis.
1731
1732 elsif ASIS_Mode then
1733 return;
1734
1735 -- See if we need elaboration entity. We always need it for the dynamic
1736 -- elaboration model, since it is needed to properly generate the PE
1737 -- exception for access before elaboration.
1738
1739 elsif Dynamic_Elaboration_Checks then
1740 null;
1741
1742 -- For the static model, we don't need the elaboration counter if this
1743 -- unit is sure to have no elaboration code, since that means there
1744 -- is no elaboration unit to be called. Note that we can't just decide
1745 -- after the fact by looking to see whether there was elaboration code,
1746 -- because that's too late to make this decision.
1747
1748 elsif Restriction_Active (No_Elaboration_Code) then
1749 return;
1750
1751 -- Similarly, for the static model, we can skip the elaboration counter
1752 -- if we have the No_Multiple_Elaboration restriction, since for the
1753 -- static model, that's the only purpose of the counter (to avoid
1754 -- multiple elaboration).
1755
1756 elsif Restriction_Active (No_Multiple_Elaboration) then
1757 return;
1758 end if;
1759
1760 -- Here we need the elaboration entity
1761
1762 -- Construct name of elaboration entity as xxx_E, where xxx is the unit
1763 -- name with dots replaced by double underscore. We have to manually
1764 -- construct this name, since it will be elaborated in the outer scope,
1765 -- and thus will not have the unit name automatically prepended.
1766
1767 Set_Package_Name (Spec_Id);
1768 Add_Str_To_Name_Buffer ("_E");
1769
1770 -- Create elaboration counter
1771
1772 Elab_Ent := Make_Defining_Identifier (Loc, Chars => Name_Find);
1773 Set_Elaboration_Entity (Spec_Id, Elab_Ent);
1774
1775 Decl :=
1776 Make_Object_Declaration (Loc,
1777 Defining_Identifier => Elab_Ent,
1778 Object_Definition =>
1779 New_Occurrence_Of (Standard_Short_Integer, Loc),
1780 Expression => Make_Integer_Literal (Loc, Uint_0));
1781
1782 Push_Scope (Standard_Standard);
1783 Add_Global_Declaration (Decl);
1784 Pop_Scope;
1785
1786 -- Reset True_Constant indication, since we will indeed assign a value
1787 -- to the variable in the binder main. We also kill the Current_Value
1788 -- and Last_Assignment fields for the same reason.
1789
1790 Set_Is_True_Constant (Elab_Ent, False);
1791 Set_Current_Value (Elab_Ent, Empty);
1792 Set_Last_Assignment (Elab_Ent, Empty);
1793
1794 -- We do not want any further qualification of the name (if we did not
1795 -- do this, we would pick up the name of the generic package in the case
1796 -- of a library level generic instantiation).
1797
1798 Set_Has_Qualified_Name (Elab_Ent);
1799 Set_Has_Fully_Qualified_Name (Elab_Ent);
1800 end Build_Elaboration_Entity;
1801
1802 --------------------------------
1803 -- Build_Explicit_Dereference --
1804 --------------------------------
1805
1806 procedure Build_Explicit_Dereference
1807 (Expr : Node_Id;
1808 Disc : Entity_Id)
1809 is
1810 Loc : constant Source_Ptr := Sloc (Expr);
1811
1812 begin
1813 -- An entity of a type with a reference aspect is overloaded with
1814 -- both interpretations: with and without the dereference. Now that
1815 -- the dereference is made explicit, set the type of the node properly,
1816 -- to prevent anomalies in the backend. Same if the expression is an
1817 -- overloaded function call whose return type has a reference aspect.
1818
1819 if Is_Entity_Name (Expr) then
1820 Set_Etype (Expr, Etype (Entity (Expr)));
1821
1822 elsif Nkind (Expr) = N_Function_Call then
1823 Set_Etype (Expr, Etype (Name (Expr)));
1824 end if;
1825
1826 Set_Is_Overloaded (Expr, False);
1827
1828 -- The expression will often be a generalized indexing that yields a
1829 -- container element that is then dereferenced, in which case the
1830 -- generalized indexing call is also non-overloaded.
1831
1832 if Nkind (Expr) = N_Indexed_Component
1833 and then Present (Generalized_Indexing (Expr))
1834 then
1835 Set_Is_Overloaded (Generalized_Indexing (Expr), False);
1836 end if;
1837
1838 Rewrite (Expr,
1839 Make_Explicit_Dereference (Loc,
1840 Prefix =>
1841 Make_Selected_Component (Loc,
1842 Prefix => Relocate_Node (Expr),
1843 Selector_Name => New_Occurrence_Of (Disc, Loc))));
1844 Set_Etype (Prefix (Expr), Etype (Disc));
1845 Set_Etype (Expr, Designated_Type (Etype (Disc)));
1846 end Build_Explicit_Dereference;
1847
1848 -----------------------------------
1849 -- Cannot_Raise_Constraint_Error --
1850 -----------------------------------
1851
1852 function Cannot_Raise_Constraint_Error (Expr : Node_Id) return Boolean is
1853 begin
1854 if Compile_Time_Known_Value (Expr) then
1855 return True;
1856
1857 elsif Do_Range_Check (Expr) then
1858 return False;
1859
1860 elsif Raises_Constraint_Error (Expr) then
1861 return False;
1862
1863 else
1864 case Nkind (Expr) is
1865 when N_Identifier =>
1866 return True;
1867
1868 when N_Expanded_Name =>
1869 return True;
1870
1871 when N_Selected_Component =>
1872 return not Do_Discriminant_Check (Expr);
1873
1874 when N_Attribute_Reference =>
1875 if Do_Overflow_Check (Expr) then
1876 return False;
1877
1878 elsif No (Expressions (Expr)) then
1879 return True;
1880
1881 else
1882 declare
1883 N : Node_Id;
1884
1885 begin
1886 N := First (Expressions (Expr));
1887 while Present (N) loop
1888 if Cannot_Raise_Constraint_Error (N) then
1889 Next (N);
1890 else
1891 return False;
1892 end if;
1893 end loop;
1894
1895 return True;
1896 end;
1897 end if;
1898
1899 when N_Type_Conversion =>
1900 if Do_Overflow_Check (Expr)
1901 or else Do_Length_Check (Expr)
1902 or else Do_Tag_Check (Expr)
1903 then
1904 return False;
1905 else
1906 return Cannot_Raise_Constraint_Error (Expression (Expr));
1907 end if;
1908
1909 when N_Unchecked_Type_Conversion =>
1910 return Cannot_Raise_Constraint_Error (Expression (Expr));
1911
1912 when N_Unary_Op =>
1913 if Do_Overflow_Check (Expr) then
1914 return False;
1915 else
1916 return Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
1917 end if;
1918
1919 when N_Op_Divide |
1920 N_Op_Mod |
1921 N_Op_Rem
1922 =>
1923 if Do_Division_Check (Expr)
1924 or else
1925 Do_Overflow_Check (Expr)
1926 then
1927 return False;
1928 else
1929 return
1930 Cannot_Raise_Constraint_Error (Left_Opnd (Expr))
1931 and then
1932 Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
1933 end if;
1934
1935 when N_Op_Add |
1936 N_Op_And |
1937 N_Op_Concat |
1938 N_Op_Eq |
1939 N_Op_Expon |
1940 N_Op_Ge |
1941 N_Op_Gt |
1942 N_Op_Le |
1943 N_Op_Lt |
1944 N_Op_Multiply |
1945 N_Op_Ne |
1946 N_Op_Or |
1947 N_Op_Rotate_Left |
1948 N_Op_Rotate_Right |
1949 N_Op_Shift_Left |
1950 N_Op_Shift_Right |
1951 N_Op_Shift_Right_Arithmetic |
1952 N_Op_Subtract |
1953 N_Op_Xor
1954 =>
1955 if Do_Overflow_Check (Expr) then
1956 return False;
1957 else
1958 return
1959 Cannot_Raise_Constraint_Error (Left_Opnd (Expr))
1960 and then
1961 Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
1962 end if;
1963
1964 when others =>
1965 return False;
1966 end case;
1967 end if;
1968 end Cannot_Raise_Constraint_Error;
1969
1970 -----------------------------------------
1971 -- Check_Dynamically_Tagged_Expression --
1972 -----------------------------------------
1973
1974 procedure Check_Dynamically_Tagged_Expression
1975 (Expr : Node_Id;
1976 Typ : Entity_Id;
1977 Related_Nod : Node_Id)
1978 is
1979 begin
1980 pragma Assert (Is_Tagged_Type (Typ));
1981
1982 -- In order to avoid spurious errors when analyzing the expanded code,
1983 -- this check is done only for nodes that come from source and for
1984 -- actuals of generic instantiations.
1985
1986 if (Comes_From_Source (Related_Nod)
1987 or else In_Generic_Actual (Expr))
1988 and then (Is_Class_Wide_Type (Etype (Expr))
1989 or else Is_Dynamically_Tagged (Expr))
1990 and then Is_Tagged_Type (Typ)
1991 and then not Is_Class_Wide_Type (Typ)
1992 then
1993 Error_Msg_N ("dynamically tagged expression not allowed!", Expr);
1994 end if;
1995 end Check_Dynamically_Tagged_Expression;
1996
1997 --------------------------
1998 -- Check_Fully_Declared --
1999 --------------------------
2000
2001 procedure Check_Fully_Declared (T : Entity_Id; N : Node_Id) is
2002 begin
2003 if Ekind (T) = E_Incomplete_Type then
2004
2005 -- Ada 2005 (AI-50217): If the type is available through a limited
2006 -- with_clause, verify that its full view has been analyzed.
2007
2008 if From_Limited_With (T)
2009 and then Present (Non_Limited_View (T))
2010 and then Ekind (Non_Limited_View (T)) /= E_Incomplete_Type
2011 then
2012 -- The non-limited view is fully declared
2013
2014 null;
2015
2016 else
2017 Error_Msg_NE
2018 ("premature usage of incomplete}", N, First_Subtype (T));
2019 end if;
2020
2021 -- Need comments for these tests ???
2022
2023 elsif Has_Private_Component (T)
2024 and then not Is_Generic_Type (Root_Type (T))
2025 and then not In_Spec_Expression
2026 then
2027 -- Special case: if T is the anonymous type created for a single
2028 -- task or protected object, use the name of the source object.
2029
2030 if Is_Concurrent_Type (T)
2031 and then not Comes_From_Source (T)
2032 and then Nkind (N) = N_Object_Declaration
2033 then
2034 Error_Msg_NE
2035 ("type of& has incomplete component",
2036 N, Defining_Identifier (N));
2037 else
2038 Error_Msg_NE
2039 ("premature usage of incomplete}",
2040 N, First_Subtype (T));
2041 end if;
2042 end if;
2043 end Check_Fully_Declared;
2044
2045 -------------------------------------
2046 -- Check_Function_Writable_Actuals --
2047 -------------------------------------
2048
2049 procedure Check_Function_Writable_Actuals (N : Node_Id) is
2050 Writable_Actuals_List : Elist_Id := No_Elist;
2051 Identifiers_List : Elist_Id := No_Elist;
2052 Error_Node : Node_Id := Empty;
2053
2054 procedure Collect_Identifiers (N : Node_Id);
2055 -- In a single traversal of subtree N collect in Writable_Actuals_List
2056 -- all the actuals of functions with writable actuals, and in the list
2057 -- Identifiers_List collect all the identifiers that are not actuals of
2058 -- functions with writable actuals. If a writable actual is referenced
2059 -- twice as writable actual then Error_Node is set to reference its
2060 -- second occurrence, the error is reported, and the tree traversal
2061 -- is abandoned.
2062
2063 function Get_Function_Id (Call : Node_Id) return Entity_Id;
2064 -- Return the entity associated with the function call
2065
2066 procedure Preanalyze_Without_Errors (N : Node_Id);
2067 -- Preanalyze N without reporting errors. Very dubious, you can't just
2068 -- go analyzing things more than once???
2069
2070 -------------------------
2071 -- Collect_Identifiers --
2072 -------------------------
2073
2074 procedure Collect_Identifiers (N : Node_Id) is
2075
2076 function Check_Node (N : Node_Id) return Traverse_Result;
2077 -- Process a single node during the tree traversal to collect the
2078 -- writable actuals of functions and all the identifiers which are
2079 -- not writable actuals of functions.
2080
2081 function Contains (List : Elist_Id; N : Node_Id) return Boolean;
2082 -- Returns True if List has a node whose Entity is Entity (N)
2083
2084 -------------------------
2085 -- Check_Function_Call --
2086 -------------------------
2087
2088 function Check_Node (N : Node_Id) return Traverse_Result is
2089 Is_Writable_Actual : Boolean := False;
2090 Id : Entity_Id;
2091
2092 begin
2093 if Nkind (N) = N_Identifier then
2094
2095 -- No analysis possible if the entity is not decorated
2096
2097 if No (Entity (N)) then
2098 return Skip;
2099
2100 -- Don't collect identifiers of packages, called functions, etc
2101
2102 elsif Ekind_In (Entity (N), E_Package,
2103 E_Function,
2104 E_Procedure,
2105 E_Entry)
2106 then
2107 return Skip;
2108
2109 -- Analyze if N is a writable actual of a function
2110
2111 elsif Nkind (Parent (N)) = N_Function_Call then
2112 declare
2113 Call : constant Node_Id := Parent (N);
2114 Actual : Node_Id;
2115 Formal : Node_Id;
2116
2117 begin
2118 Id := Get_Function_Id (Call);
2119
2120 -- In case of previous error, no check is possible
2121
2122 if No (Id) then
2123 return Abandon;
2124 end if;
2125
2126 Formal := First_Formal (Id);
2127 Actual := First_Actual (Call);
2128 while Present (Actual) and then Present (Formal) loop
2129 if Actual = N then
2130 if Ekind_In (Formal, E_Out_Parameter,
2131 E_In_Out_Parameter)
2132 then
2133 Is_Writable_Actual := True;
2134 end if;
2135
2136 exit;
2137 end if;
2138
2139 Next_Formal (Formal);
2140 Next_Actual (Actual);
2141 end loop;
2142 end;
2143 end if;
2144
2145 if Is_Writable_Actual then
2146 if Contains (Writable_Actuals_List, N) then
2147 Error_Msg_NE
2148 ("value may be affected by call to& "
2149 & "because order of evaluation is arbitrary", N, Id);
2150 Error_Node := N;
2151 return Abandon;
2152 end if;
2153
2154 Append_New_Elmt (N, To => Writable_Actuals_List);
2155
2156 else
2157 if Identifiers_List = No_Elist then
2158 Identifiers_List := New_Elmt_List;
2159 end if;
2160
2161 Append_Unique_Elmt (N, Identifiers_List);
2162 end if;
2163 end if;
2164
2165 return OK;
2166 end Check_Node;
2167
2168 --------------
2169 -- Contains --
2170 --------------
2171
2172 function Contains
2173 (List : Elist_Id;
2174 N : Node_Id) return Boolean
2175 is
2176 pragma Assert (Nkind (N) in N_Has_Entity);
2177
2178 Elmt : Elmt_Id;
2179
2180 begin
2181 if List = No_Elist then
2182 return False;
2183 end if;
2184
2185 Elmt := First_Elmt (List);
2186 while Present (Elmt) loop
2187 if Entity (Node (Elmt)) = Entity (N) then
2188 return True;
2189 else
2190 Next_Elmt (Elmt);
2191 end if;
2192 end loop;
2193
2194 return False;
2195 end Contains;
2196
2197 ------------------
2198 -- Do_Traversal --
2199 ------------------
2200
2201 procedure Do_Traversal is new Traverse_Proc (Check_Node);
2202 -- The traversal procedure
2203
2204 -- Start of processing for Collect_Identifiers
2205
2206 begin
2207 if Present (Error_Node) then
2208 return;
2209 end if;
2210
2211 if Nkind (N) in N_Subexpr and then Is_OK_Static_Expression (N) then
2212 return;
2213 end if;
2214
2215 Do_Traversal (N);
2216 end Collect_Identifiers;
2217
2218 ---------------------
2219 -- Get_Function_Id --
2220 ---------------------
2221
2222 function Get_Function_Id (Call : Node_Id) return Entity_Id is
2223 Nam : constant Node_Id := Name (Call);
2224 Id : Entity_Id;
2225
2226 begin
2227 if Nkind (Nam) = N_Explicit_Dereference then
2228 Id := Etype (Nam);
2229 pragma Assert (Ekind (Id) = E_Subprogram_Type);
2230
2231 elsif Nkind (Nam) = N_Selected_Component then
2232 Id := Entity (Selector_Name (Nam));
2233
2234 elsif Nkind (Nam) = N_Indexed_Component then
2235 Id := Entity (Selector_Name (Prefix (Nam)));
2236
2237 else
2238 Id := Entity (Nam);
2239 end if;
2240
2241 return Id;
2242 end Get_Function_Id;
2243
2244 ---------------------------
2245 -- Preanalyze_Expression --
2246 ---------------------------
2247
2248 procedure Preanalyze_Without_Errors (N : Node_Id) is
2249 Status : constant Boolean := Get_Ignore_Errors;
2250 begin
2251 Set_Ignore_Errors (True);
2252 Preanalyze (N);
2253 Set_Ignore_Errors (Status);
2254 end Preanalyze_Without_Errors;
2255
2256 -- Start of processing for Check_Function_Writable_Actuals
2257
2258 begin
2259 -- The check only applies to Ada 2012 code, and only to constructs that
2260 -- have multiple constituents whose order of evaluation is not specified
2261 -- by the language.
2262
2263 if Ada_Version < Ada_2012
2264 or else (not (Nkind (N) in N_Op)
2265 and then not (Nkind (N) in N_Membership_Test)
2266 and then not Nkind_In (N, N_Range,
2267 N_Aggregate,
2268 N_Extension_Aggregate,
2269 N_Full_Type_Declaration,
2270 N_Function_Call,
2271 N_Procedure_Call_Statement,
2272 N_Entry_Call_Statement))
2273 or else (Nkind (N) = N_Full_Type_Declaration
2274 and then not Is_Record_Type (Defining_Identifier (N)))
2275
2276 -- In addition, this check only applies to source code, not to code
2277 -- generated by constraint checks.
2278
2279 or else not Comes_From_Source (N)
2280 then
2281 return;
2282 end if;
2283
2284 -- If a construct C has two or more direct constituents that are names
2285 -- or expressions whose evaluation may occur in an arbitrary order, at
2286 -- least one of which contains a function call with an in out or out
2287 -- parameter, then the construct is legal only if: for each name N that
2288 -- is passed as a parameter of mode in out or out to some inner function
2289 -- call C2 (not including the construct C itself), there is no other
2290 -- name anywhere within a direct constituent of the construct C other
2291 -- than the one containing C2, that is known to refer to the same
2292 -- object (RM 6.4.1(6.17/3)).
2293
2294 case Nkind (N) is
2295 when N_Range =>
2296 Collect_Identifiers (Low_Bound (N));
2297 Collect_Identifiers (High_Bound (N));
2298
2299 when N_Op | N_Membership_Test =>
2300 declare
2301 Expr : Node_Id;
2302
2303 begin
2304 Collect_Identifiers (Left_Opnd (N));
2305
2306 if Present (Right_Opnd (N)) then
2307 Collect_Identifiers (Right_Opnd (N));
2308 end if;
2309
2310 if Nkind_In (N, N_In, N_Not_In)
2311 and then Present (Alternatives (N))
2312 then
2313 Expr := First (Alternatives (N));
2314 while Present (Expr) loop
2315 Collect_Identifiers (Expr);
2316
2317 Next (Expr);
2318 end loop;
2319 end if;
2320 end;
2321
2322 when N_Full_Type_Declaration =>
2323 declare
2324 function Get_Record_Part (N : Node_Id) return Node_Id;
2325 -- Return the record part of this record type definition
2326
2327 function Get_Record_Part (N : Node_Id) return Node_Id is
2328 Type_Def : constant Node_Id := Type_Definition (N);
2329 begin
2330 if Nkind (Type_Def) = N_Derived_Type_Definition then
2331 return Record_Extension_Part (Type_Def);
2332 else
2333 return Type_Def;
2334 end if;
2335 end Get_Record_Part;
2336
2337 Comp : Node_Id;
2338 Def_Id : Entity_Id := Defining_Identifier (N);
2339 Rec : Node_Id := Get_Record_Part (N);
2340
2341 begin
2342 -- No need to perform any analysis if the record has no
2343 -- components
2344
2345 if No (Rec) or else No (Component_List (Rec)) then
2346 return;
2347 end if;
2348
2349 -- Collect the identifiers starting from the deepest
2350 -- derivation. Done to report the error in the deepest
2351 -- derivation.
2352
2353 loop
2354 if Present (Component_List (Rec)) then
2355 Comp := First (Component_Items (Component_List (Rec)));
2356 while Present (Comp) loop
2357 if Nkind (Comp) = N_Component_Declaration
2358 and then Present (Expression (Comp))
2359 then
2360 Collect_Identifiers (Expression (Comp));
2361 end if;
2362
2363 Next (Comp);
2364 end loop;
2365 end if;
2366
2367 exit when No (Underlying_Type (Etype (Def_Id)))
2368 or else Base_Type (Underlying_Type (Etype (Def_Id)))
2369 = Def_Id;
2370
2371 Def_Id := Base_Type (Underlying_Type (Etype (Def_Id)));
2372 Rec := Get_Record_Part (Parent (Def_Id));
2373 end loop;
2374 end;
2375
2376 when N_Subprogram_Call |
2377 N_Entry_Call_Statement =>
2378 declare
2379 Id : constant Entity_Id := Get_Function_Id (N);
2380 Formal : Node_Id;
2381 Actual : Node_Id;
2382
2383 begin
2384 Formal := First_Formal (Id);
2385 Actual := First_Actual (N);
2386 while Present (Actual) and then Present (Formal) loop
2387 if Ekind_In (Formal, E_Out_Parameter,
2388 E_In_Out_Parameter)
2389 then
2390 Collect_Identifiers (Actual);
2391 end if;
2392
2393 Next_Formal (Formal);
2394 Next_Actual (Actual);
2395 end loop;
2396 end;
2397
2398 when N_Aggregate |
2399 N_Extension_Aggregate =>
2400 declare
2401 Assoc : Node_Id;
2402 Choice : Node_Id;
2403 Comp_Expr : Node_Id;
2404
2405 begin
2406 -- Handle the N_Others_Choice of array aggregates with static
2407 -- bounds. There is no need to perform this analysis in
2408 -- aggregates without static bounds since we cannot evaluate
2409 -- if the N_Others_Choice covers several elements. There is
2410 -- no need to handle the N_Others choice of record aggregates
2411 -- since at this stage it has been already expanded by
2412 -- Resolve_Record_Aggregate.
2413
2414 if Is_Array_Type (Etype (N))
2415 and then Nkind (N) = N_Aggregate
2416 and then Present (Aggregate_Bounds (N))
2417 and then Compile_Time_Known_Bounds (Etype (N))
2418 and then Expr_Value (High_Bound (Aggregate_Bounds (N)))
2419 >
2420 Expr_Value (Low_Bound (Aggregate_Bounds (N)))
2421 then
2422 declare
2423 Count_Components : Uint := Uint_0;
2424 Num_Components : Uint;
2425 Others_Assoc : Node_Id;
2426 Others_Choice : Node_Id := Empty;
2427 Others_Box_Present : Boolean := False;
2428
2429 begin
2430 -- Count positional associations
2431
2432 if Present (Expressions (N)) then
2433 Comp_Expr := First (Expressions (N));
2434 while Present (Comp_Expr) loop
2435 Count_Components := Count_Components + 1;
2436 Next (Comp_Expr);
2437 end loop;
2438 end if;
2439
2440 -- Count the rest of elements and locate the N_Others
2441 -- choice (if any)
2442
2443 Assoc := First (Component_Associations (N));
2444 while Present (Assoc) loop
2445 Choice := First (Choices (Assoc));
2446 while Present (Choice) loop
2447 if Nkind (Choice) = N_Others_Choice then
2448 Others_Assoc := Assoc;
2449 Others_Choice := Choice;
2450 Others_Box_Present := Box_Present (Assoc);
2451
2452 -- Count several components
2453
2454 elsif Nkind_In (Choice, N_Range,
2455 N_Subtype_Indication)
2456 or else (Is_Entity_Name (Choice)
2457 and then Is_Type (Entity (Choice)))
2458 then
2459 declare
2460 L, H : Node_Id;
2461 begin
2462 Get_Index_Bounds (Choice, L, H);
2463 pragma Assert
2464 (Compile_Time_Known_Value (L)
2465 and then Compile_Time_Known_Value (H));
2466 Count_Components :=
2467 Count_Components
2468 + Expr_Value (H) - Expr_Value (L) + 1;
2469 end;
2470
2471 -- Count single component. No other case available
2472 -- since we are handling an aggregate with static
2473 -- bounds.
2474
2475 else
2476 pragma Assert (Is_OK_Static_Expression (Choice)
2477 or else Nkind (Choice) = N_Identifier
2478 or else Nkind (Choice) = N_Integer_Literal);
2479
2480 Count_Components := Count_Components + 1;
2481 end if;
2482
2483 Next (Choice);
2484 end loop;
2485
2486 Next (Assoc);
2487 end loop;
2488
2489 Num_Components :=
2490 Expr_Value (High_Bound (Aggregate_Bounds (N))) -
2491 Expr_Value (Low_Bound (Aggregate_Bounds (N))) + 1;
2492
2493 pragma Assert (Count_Components <= Num_Components);
2494
2495 -- Handle the N_Others choice if it covers several
2496 -- components
2497
2498 if Present (Others_Choice)
2499 and then (Num_Components - Count_Components) > 1
2500 then
2501 if not Others_Box_Present then
2502
2503 -- At this stage, if expansion is active, the
2504 -- expression of the others choice has not been
2505 -- analyzed. Hence we generate a duplicate and
2506 -- we analyze it silently to have available the
2507 -- minimum decoration required to collect the
2508 -- identifiers.
2509
2510 if not Expander_Active then
2511 Comp_Expr := Expression (Others_Assoc);
2512 else
2513 Comp_Expr :=
2514 New_Copy_Tree (Expression (Others_Assoc));
2515 Preanalyze_Without_Errors (Comp_Expr);
2516 end if;
2517
2518 Collect_Identifiers (Comp_Expr);
2519
2520 if Writable_Actuals_List /= No_Elist then
2521
2522 -- As suggested by Robert, at current stage we
2523 -- report occurrences of this case as warnings.
2524
2525 Error_Msg_N
2526 ("writable function parameter may affect "
2527 & "value in other component because order "
2528 & "of evaluation is unspecified??",
2529 Node (First_Elmt (Writable_Actuals_List)));
2530 end if;
2531 end if;
2532 end if;
2533 end;
2534 end if;
2535
2536 -- Handle ancestor part of extension aggregates
2537
2538 if Nkind (N) = N_Extension_Aggregate then
2539 Collect_Identifiers (Ancestor_Part (N));
2540 end if;
2541
2542 -- Handle positional associations
2543
2544 if Present (Expressions (N)) then
2545 Comp_Expr := First (Expressions (N));
2546 while Present (Comp_Expr) loop
2547 if not Is_OK_Static_Expression (Comp_Expr) then
2548 Collect_Identifiers (Comp_Expr);
2549 end if;
2550
2551 Next (Comp_Expr);
2552 end loop;
2553 end if;
2554
2555 -- Handle discrete associations
2556
2557 if Present (Component_Associations (N)) then
2558 Assoc := First (Component_Associations (N));
2559 while Present (Assoc) loop
2560
2561 if not Box_Present (Assoc) then
2562 Choice := First (Choices (Assoc));
2563 while Present (Choice) loop
2564
2565 -- For now we skip discriminants since it requires
2566 -- performing the analysis in two phases: first one
2567 -- analyzing discriminants and second one analyzing
2568 -- the rest of components since discriminants are
2569 -- evaluated prior to components: too much extra
2570 -- work to detect a corner case???
2571
2572 if Nkind (Choice) in N_Has_Entity
2573 and then Present (Entity (Choice))
2574 and then Ekind (Entity (Choice)) = E_Discriminant
2575 then
2576 null;
2577
2578 elsif Box_Present (Assoc) then
2579 null;
2580
2581 else
2582 if not Analyzed (Expression (Assoc)) then
2583 Comp_Expr :=
2584 New_Copy_Tree (Expression (Assoc));
2585 Set_Parent (Comp_Expr, Parent (N));
2586 Preanalyze_Without_Errors (Comp_Expr);
2587 else
2588 Comp_Expr := Expression (Assoc);
2589 end if;
2590
2591 Collect_Identifiers (Comp_Expr);
2592 end if;
2593
2594 Next (Choice);
2595 end loop;
2596 end if;
2597
2598 Next (Assoc);
2599 end loop;
2600 end if;
2601 end;
2602
2603 when others =>
2604 return;
2605 end case;
2606
2607 -- No further action needed if we already reported an error
2608
2609 if Present (Error_Node) then
2610 return;
2611 end if;
2612
2613 -- Check if some writable argument of a function is referenced
2614
2615 if Writable_Actuals_List /= No_Elist
2616 and then Identifiers_List /= No_Elist
2617 then
2618 declare
2619 Elmt_1 : Elmt_Id;
2620 Elmt_2 : Elmt_Id;
2621
2622 begin
2623 Elmt_1 := First_Elmt (Writable_Actuals_List);
2624 while Present (Elmt_1) loop
2625 Elmt_2 := First_Elmt (Identifiers_List);
2626 while Present (Elmt_2) loop
2627 if Entity (Node (Elmt_1)) = Entity (Node (Elmt_2)) then
2628 case Nkind (Parent (Node (Elmt_2))) is
2629 when N_Aggregate |
2630 N_Component_Association |
2631 N_Component_Declaration =>
2632 Error_Msg_N
2633 ("value may be affected by call in other "
2634 & "component because they are evaluated "
2635 & "in unspecified order",
2636 Node (Elmt_2));
2637
2638 when N_In | N_Not_In =>
2639 Error_Msg_N
2640 ("value may be affected by call in other "
2641 & "alternative because they are evaluated "
2642 & "in unspecified order",
2643 Node (Elmt_2));
2644
2645 when others =>
2646 Error_Msg_N
2647 ("value of actual may be affected by call in "
2648 & "other actual because they are evaluated "
2649 & "in unspecified order",
2650 Node (Elmt_2));
2651 end case;
2652 end if;
2653
2654 Next_Elmt (Elmt_2);
2655 end loop;
2656
2657 Next_Elmt (Elmt_1);
2658 end loop;
2659 end;
2660 end if;
2661 end Check_Function_Writable_Actuals;
2662
2663 --------------------------------
2664 -- Check_Implicit_Dereference --
2665 --------------------------------
2666
2667 procedure Check_Implicit_Dereference (N : Node_Id; Typ : Entity_Id) is
2668 Disc : Entity_Id;
2669 Desig : Entity_Id;
2670 Nam : Node_Id;
2671
2672 begin
2673 if Nkind (N) = N_Indexed_Component
2674 and then Present (Generalized_Indexing (N))
2675 then
2676 Nam := Generalized_Indexing (N);
2677 else
2678 Nam := N;
2679 end if;
2680
2681 if Ada_Version < Ada_2012
2682 or else not Has_Implicit_Dereference (Base_Type (Typ))
2683 then
2684 return;
2685
2686 elsif not Comes_From_Source (N)
2687 and then Nkind (N) /= N_Indexed_Component
2688 then
2689 return;
2690
2691 elsif Is_Entity_Name (Nam) and then Is_Type (Entity (Nam)) then
2692 null;
2693
2694 else
2695 Disc := First_Discriminant (Typ);
2696 while Present (Disc) loop
2697 if Has_Implicit_Dereference (Disc) then
2698 Desig := Designated_Type (Etype (Disc));
2699 Add_One_Interp (Nam, Disc, Desig);
2700
2701 -- If the node is a generalized indexing, add interpretation
2702 -- to that node as well, for subsequent resolution.
2703
2704 if Nkind (N) = N_Indexed_Component then
2705 Add_One_Interp (N, Disc, Desig);
2706 end if;
2707
2708 -- If the operation comes from a generic unit and the context
2709 -- is a selected component, the selector name may be global
2710 -- and set in the instance already. Remove the entity to
2711 -- force resolution of the selected component, and the
2712 -- generation of an explicit dereference if needed.
2713
2714 if In_Instance
2715 and then Nkind (Parent (Nam)) = N_Selected_Component
2716 then
2717 Set_Entity (Selector_Name (Parent (Nam)), Empty);
2718 end if;
2719
2720 exit;
2721 end if;
2722
2723 Next_Discriminant (Disc);
2724 end loop;
2725 end if;
2726 end Check_Implicit_Dereference;
2727
2728 ----------------------------------
2729 -- Check_Internal_Protected_Use --
2730 ----------------------------------
2731
2732 procedure Check_Internal_Protected_Use (N : Node_Id; Nam : Entity_Id) is
2733 S : Entity_Id;
2734 Prot : Entity_Id;
2735
2736 begin
2737 S := Current_Scope;
2738 while Present (S) loop
2739 if S = Standard_Standard then
2740 return;
2741
2742 elsif Ekind (S) = E_Function
2743 and then Ekind (Scope (S)) = E_Protected_Type
2744 then
2745 Prot := Scope (S);
2746 exit;
2747 end if;
2748
2749 S := Scope (S);
2750 end loop;
2751
2752 if Scope (Nam) = Prot and then Ekind (Nam) /= E_Function then
2753
2754 -- An indirect function call (e.g. a callback within a protected
2755 -- function body) is not statically illegal. If the access type is
2756 -- anonymous and is the type of an access parameter, the scope of Nam
2757 -- will be the protected type, but it is not a protected operation.
2758
2759 if Ekind (Nam) = E_Subprogram_Type
2760 and then
2761 Nkind (Associated_Node_For_Itype (Nam)) = N_Function_Specification
2762 then
2763 null;
2764
2765 elsif Nkind (N) = N_Subprogram_Renaming_Declaration then
2766 Error_Msg_N
2767 ("within protected function cannot use protected "
2768 & "procedure in renaming or as generic actual", N);
2769
2770 elsif Nkind (N) = N_Attribute_Reference then
2771 Error_Msg_N
2772 ("within protected function cannot take access of "
2773 & " protected procedure", N);
2774
2775 else
2776 Error_Msg_N
2777 ("within protected function, protected object is constant", N);
2778 Error_Msg_N
2779 ("\cannot call operation that may modify it", N);
2780 end if;
2781 end if;
2782 end Check_Internal_Protected_Use;
2783
2784 ---------------------------------------
2785 -- Check_Later_Vs_Basic_Declarations --
2786 ---------------------------------------
2787
2788 procedure Check_Later_Vs_Basic_Declarations
2789 (Decls : List_Id;
2790 During_Parsing : Boolean)
2791 is
2792 Body_Sloc : Source_Ptr;
2793 Decl : Node_Id;
2794
2795 function Is_Later_Declarative_Item (Decl : Node_Id) return Boolean;
2796 -- Return whether Decl is considered as a declarative item.
2797 -- When During_Parsing is True, the semantics of Ada 83 is followed.
2798 -- When During_Parsing is False, the semantics of SPARK is followed.
2799
2800 -------------------------------
2801 -- Is_Later_Declarative_Item --
2802 -------------------------------
2803
2804 function Is_Later_Declarative_Item (Decl : Node_Id) return Boolean is
2805 begin
2806 if Nkind (Decl) in N_Later_Decl_Item then
2807 return True;
2808
2809 elsif Nkind (Decl) = N_Pragma then
2810 return True;
2811
2812 elsif During_Parsing then
2813 return False;
2814
2815 -- In SPARK, a package declaration is not considered as a later
2816 -- declarative item.
2817
2818 elsif Nkind (Decl) = N_Package_Declaration then
2819 return False;
2820
2821 -- In SPARK, a renaming is considered as a later declarative item
2822
2823 elsif Nkind (Decl) in N_Renaming_Declaration then
2824 return True;
2825
2826 else
2827 return False;
2828 end if;
2829 end Is_Later_Declarative_Item;
2830
2831 -- Start of Check_Later_Vs_Basic_Declarations
2832
2833 begin
2834 Decl := First (Decls);
2835
2836 -- Loop through sequence of basic declarative items
2837
2838 Outer : while Present (Decl) loop
2839 if not Nkind_In (Decl, N_Subprogram_Body, N_Package_Body, N_Task_Body)
2840 and then Nkind (Decl) not in N_Body_Stub
2841 then
2842 Next (Decl);
2843
2844 -- Once a body is encountered, we only allow later declarative
2845 -- items. The inner loop checks the rest of the list.
2846
2847 else
2848 Body_Sloc := Sloc (Decl);
2849
2850 Inner : while Present (Decl) loop
2851 if not Is_Later_Declarative_Item (Decl) then
2852 if During_Parsing then
2853 if Ada_Version = Ada_83 then
2854 Error_Msg_Sloc := Body_Sloc;
2855 Error_Msg_N
2856 ("(Ada 83) decl cannot appear after body#", Decl);
2857 end if;
2858 else
2859 Error_Msg_Sloc := Body_Sloc;
2860 Check_SPARK_05_Restriction
2861 ("decl cannot appear after body#", Decl);
2862 end if;
2863 end if;
2864
2865 Next (Decl);
2866 end loop Inner;
2867 end if;
2868 end loop Outer;
2869 end Check_Later_Vs_Basic_Declarations;
2870
2871 -------------------------
2872 -- Check_Nested_Access --
2873 -------------------------
2874
2875 procedure Check_Nested_Access (Ent : Entity_Id) is
2876 Scop : constant Entity_Id := Current_Scope;
2877 Current_Subp : Entity_Id;
2878 Enclosing : Entity_Id;
2879
2880 begin
2881 -- Currently only enabled for VM back-ends for efficiency
2882
2883 if VM_Target /= No_VM
2884 and then Ekind_In (Ent, E_Variable, E_Constant, E_Loop_Parameter)
2885 and then Scope (Ent) /= Empty
2886 and then not Is_Library_Level_Entity (Ent)
2887
2888 -- Comment the exclusion of imported entities ???
2889
2890 and then not Is_Imported (Ent)
2891 then
2892 -- Get current subprogram that is relevant
2893
2894 if Is_Subprogram (Scop)
2895 or else Is_Generic_Subprogram (Scop)
2896 or else Is_Entry (Scop)
2897 then
2898 Current_Subp := Scop;
2899 else
2900 Current_Subp := Current_Subprogram;
2901 end if;
2902
2903 Enclosing := Enclosing_Subprogram (Ent);
2904
2905 -- Set flag if uplevel reference
2906
2907 if Enclosing /= Empty and then Enclosing /= Current_Subp then
2908 Set_Has_Uplevel_Reference (Ent, True);
2909 end if;
2910 end if;
2911 end Check_Nested_Access;
2912
2913 ---------------------------
2914 -- Check_No_Hidden_State --
2915 ---------------------------
2916
2917 procedure Check_No_Hidden_State (Id : Entity_Id) is
2918 function Has_Null_Abstract_State (Pkg : Entity_Id) return Boolean;
2919 -- Determine whether the entity of a package denoted by Pkg has a null
2920 -- abstract state.
2921
2922 -----------------------------
2923 -- Has_Null_Abstract_State --
2924 -----------------------------
2925
2926 function Has_Null_Abstract_State (Pkg : Entity_Id) return Boolean is
2927 States : constant Elist_Id := Abstract_States (Pkg);
2928
2929 begin
2930 -- Check first available state of related package. A null abstract
2931 -- state always appears as the sole element of the state list.
2932
2933 return
2934 Present (States)
2935 and then Is_Null_State (Node (First_Elmt (States)));
2936 end Has_Null_Abstract_State;
2937
2938 -- Local variables
2939
2940 Context : Entity_Id := Empty;
2941 Not_Visible : Boolean := False;
2942 Scop : Entity_Id;
2943
2944 -- Start of processing for Check_No_Hidden_State
2945
2946 begin
2947 pragma Assert (Ekind_In (Id, E_Abstract_State, E_Variable));
2948
2949 -- Find the proper context where the object or state appears
2950
2951 Scop := Scope (Id);
2952 while Present (Scop) loop
2953 Context := Scop;
2954
2955 -- Keep track of the context's visibility
2956
2957 Not_Visible := Not_Visible or else In_Private_Part (Context);
2958
2959 -- Prevent the search from going too far
2960
2961 if Context = Standard_Standard then
2962 return;
2963
2964 -- Objects and states that appear immediately within a subprogram or
2965 -- inside a construct nested within a subprogram do not introduce a
2966 -- hidden state. They behave as local variable declarations.
2967
2968 elsif Is_Subprogram (Context) then
2969 return;
2970
2971 -- When examining a package body, use the entity of the spec as it
2972 -- carries the abstract state declarations.
2973
2974 elsif Ekind (Context) = E_Package_Body then
2975 Context := Spec_Entity (Context);
2976 end if;
2977
2978 -- Stop the traversal when a package subject to a null abstract state
2979 -- has been found.
2980
2981 if Ekind_In (Context, E_Generic_Package, E_Package)
2982 and then Has_Null_Abstract_State (Context)
2983 then
2984 exit;
2985 end if;
2986
2987 Scop := Scope (Scop);
2988 end loop;
2989
2990 -- At this point we know that there is at least one package with a null
2991 -- abstract state in visibility. Emit an error message unconditionally
2992 -- if the entity being processed is a state because the placement of the
2993 -- related package is irrelevant. This is not the case for objects as
2994 -- the intermediate context matters.
2995
2996 if Present (Context)
2997 and then (Ekind (Id) = E_Abstract_State or else Not_Visible)
2998 then
2999 Error_Msg_N ("cannot introduce hidden state &", Id);
3000 Error_Msg_NE ("\package & has null abstract state", Id, Context);
3001 end if;
3002 end Check_No_Hidden_State;
3003
3004 ------------------------------------------
3005 -- Check_Potentially_Blocking_Operation --
3006 ------------------------------------------
3007
3008 procedure Check_Potentially_Blocking_Operation (N : Node_Id) is
3009 S : Entity_Id;
3010
3011 begin
3012 -- N is one of the potentially blocking operations listed in 9.5.1(8).
3013 -- When pragma Detect_Blocking is active, the run time will raise
3014 -- Program_Error. Here we only issue a warning, since we generally
3015 -- support the use of potentially blocking operations in the absence
3016 -- of the pragma.
3017
3018 -- Indirect blocking through a subprogram call cannot be diagnosed
3019 -- statically without interprocedural analysis, so we do not attempt
3020 -- to do it here.
3021
3022 S := Scope (Current_Scope);
3023 while Present (S) and then S /= Standard_Standard loop
3024 if Is_Protected_Type (S) then
3025 Error_Msg_N
3026 ("potentially blocking operation in protected operation??", N);
3027 return;
3028 end if;
3029
3030 S := Scope (S);
3031 end loop;
3032 end Check_Potentially_Blocking_Operation;
3033
3034 ---------------------------------
3035 -- Check_Result_And_Post_State --
3036 ---------------------------------
3037
3038 procedure Check_Result_And_Post_State (Subp_Id : Entity_Id) is
3039 procedure Check_Result_And_Post_State_In_Pragma
3040 (Prag : Node_Id;
3041 Result_Seen : in out Boolean);
3042 -- Determine whether pragma Prag mentions attribute 'Result and whether
3043 -- the pragma contains an expression that evaluates differently in pre-
3044 -- and post-state. Prag is a [refined] postcondition or a contract-cases
3045 -- pragma. Result_Seen is set when the pragma mentions attribute 'Result
3046
3047 function Has_In_Out_Parameter (Subp_Id : Entity_Id) return Boolean;
3048 -- Determine whether subprogram Subp_Id contains at least one IN OUT
3049 -- formal parameter.
3050
3051 -------------------------------------------
3052 -- Check_Result_And_Post_State_In_Pragma --
3053 -------------------------------------------
3054
3055 procedure Check_Result_And_Post_State_In_Pragma
3056 (Prag : Node_Id;
3057 Result_Seen : in out Boolean)
3058 is
3059 procedure Check_Expression (Expr : Node_Id);
3060 -- Perform the 'Result and post-state checks on a given expression
3061
3062 function Is_Function_Result (N : Node_Id) return Traverse_Result;
3063 -- Attempt to find attribute 'Result in a subtree denoted by N
3064
3065 function Is_Trivial_Boolean (N : Node_Id) return Boolean;
3066 -- Determine whether source node N denotes "True" or "False"
3067
3068 function Mentions_Post_State (N : Node_Id) return Boolean;
3069 -- Determine whether a subtree denoted by N mentions any construct
3070 -- that denotes a post-state.
3071
3072 procedure Check_Function_Result is
3073 new Traverse_Proc (Is_Function_Result);
3074
3075 ----------------------
3076 -- Check_Expression --
3077 ----------------------
3078
3079 procedure Check_Expression (Expr : Node_Id) is
3080 begin
3081 if not Is_Trivial_Boolean (Expr) then
3082 Check_Function_Result (Expr);
3083
3084 if not Mentions_Post_State (Expr) then
3085 if Pragma_Name (Prag) = Name_Contract_Cases then
3086 Error_Msg_NE
3087 ("contract case does not check the outcome of calling "
3088 & "&?T?", Expr, Subp_Id);
3089
3090 elsif Pragma_Name (Prag) = Name_Refined_Post then
3091 Error_Msg_NE
3092 ("refined postcondition does not check the outcome of "
3093 & "calling &?T?", Prag, Subp_Id);
3094
3095 else
3096 Error_Msg_NE
3097 ("postcondition does not check the outcome of calling "
3098 & "&?T?", Prag, Subp_Id);
3099 end if;
3100 end if;
3101 end if;
3102 end Check_Expression;
3103
3104 ------------------------
3105 -- Is_Function_Result --
3106 ------------------------
3107
3108 function Is_Function_Result (N : Node_Id) return Traverse_Result is
3109 begin
3110 if Is_Attribute_Result (N) then
3111 Result_Seen := True;
3112 return Abandon;
3113
3114 -- Continue the traversal
3115
3116 else
3117 return OK;
3118 end if;
3119 end Is_Function_Result;
3120
3121 ------------------------
3122 -- Is_Trivial_Boolean --
3123 ------------------------
3124
3125 function Is_Trivial_Boolean (N : Node_Id) return Boolean is
3126 begin
3127 return
3128 Comes_From_Source (N)
3129 and then Is_Entity_Name (N)
3130 and then (Entity (N) = Standard_True
3131 or else
3132 Entity (N) = Standard_False);
3133 end Is_Trivial_Boolean;
3134
3135 -------------------------
3136 -- Mentions_Post_State --
3137 -------------------------
3138
3139 function Mentions_Post_State (N : Node_Id) return Boolean is
3140 Post_State_Seen : Boolean := False;
3141
3142 function Is_Post_State (N : Node_Id) return Traverse_Result;
3143 -- Attempt to find a construct that denotes a post-state. If this
3144 -- is the case, set flag Post_State_Seen.
3145
3146 -------------------
3147 -- Is_Post_State --
3148 -------------------
3149
3150 function Is_Post_State (N : Node_Id) return Traverse_Result is
3151 Ent : Entity_Id;
3152
3153 begin
3154 if Nkind_In (N, N_Explicit_Dereference, N_Function_Call) then
3155 Post_State_Seen := True;
3156 return Abandon;
3157
3158 elsif Nkind_In (N, N_Expanded_Name, N_Identifier) then
3159 Ent := Entity (N);
3160
3161 -- The entity may be modifiable through an implicit
3162 -- dereference.
3163
3164 if No (Ent)
3165 or else Ekind (Ent) in Assignable_Kind
3166 or else (Is_Access_Type (Etype (Ent))
3167 and then Nkind (Parent (N)) =
3168 N_Selected_Component)
3169 then
3170 Post_State_Seen := True;
3171 return Abandon;
3172 end if;
3173
3174 elsif Nkind (N) = N_Attribute_Reference then
3175 if Attribute_Name (N) = Name_Old then
3176 return Skip;
3177
3178 elsif Attribute_Name (N) = Name_Result then
3179 Post_State_Seen := True;
3180 return Abandon;
3181 end if;
3182 end if;
3183
3184 return OK;
3185 end Is_Post_State;
3186
3187 procedure Find_Post_State is new Traverse_Proc (Is_Post_State);
3188
3189 -- Start of processing for Mentions_Post_State
3190
3191 begin
3192 Find_Post_State (N);
3193
3194 return Post_State_Seen;
3195 end Mentions_Post_State;
3196
3197 -- Local variables
3198
3199 Expr : constant Node_Id :=
3200 Get_Pragma_Arg
3201 (First (Pragma_Argument_Associations (Prag)));
3202 Nam : constant Name_Id := Pragma_Name (Prag);
3203 CCase : Node_Id;
3204
3205 -- Start of processing for Check_Result_And_Post_State_In_Pragma
3206
3207 begin
3208 -- Examine all consequences
3209
3210 if Nam = Name_Contract_Cases then
3211 CCase := First (Component_Associations (Expr));
3212 while Present (CCase) loop
3213 Check_Expression (Expression (CCase));
3214
3215 Next (CCase);
3216 end loop;
3217
3218 -- Examine the expression of a postcondition
3219
3220 else pragma Assert (Nam_In (Nam, Name_Postcondition,
3221 Name_Refined_Post));
3222 Check_Expression (Expr);
3223 end if;
3224 end Check_Result_And_Post_State_In_Pragma;
3225
3226 --------------------------
3227 -- Has_In_Out_Parameter --
3228 --------------------------
3229
3230 function Has_In_Out_Parameter (Subp_Id : Entity_Id) return Boolean is
3231 Formal : Entity_Id;
3232
3233 begin
3234 -- Traverse the formals looking for an IN OUT parameter
3235
3236 Formal := First_Formal (Subp_Id);
3237 while Present (Formal) loop
3238 if Ekind (Formal) = E_In_Out_Parameter then
3239 return True;
3240 end if;
3241
3242 Next_Formal (Formal);
3243 end loop;
3244
3245 return False;
3246 end Has_In_Out_Parameter;
3247
3248 -- Local variables
3249
3250 Items : constant Node_Id := Contract (Subp_Id);
3251 Subp_Decl : constant Node_Id := Unit_Declaration_Node (Subp_Id);
3252 Case_Prag : Node_Id := Empty;
3253 Post_Prag : Node_Id := Empty;
3254 Prag : Node_Id;
3255 Seen_In_Case : Boolean := False;
3256 Seen_In_Post : Boolean := False;
3257 Spec_Id : Entity_Id;
3258
3259 -- Start of processing for Check_Result_And_Post_State
3260
3261 begin
3262 -- The lack of attribute 'Result or a post-state is classified as a
3263 -- suspicious contract. Do not perform the check if the corresponding
3264 -- swich is not set.
3265
3266 if not Warn_On_Suspicious_Contract then
3267 return;
3268
3269 -- Nothing to do if there is no contract
3270
3271 elsif No (Items) then
3272 return;
3273 end if;
3274
3275 -- Retrieve the entity of the subprogram spec (if any)
3276
3277 if Nkind (Subp_Decl) = N_Subprogram_Body
3278 and then Present (Corresponding_Spec (Subp_Decl))
3279 then
3280 Spec_Id := Corresponding_Spec (Subp_Decl);
3281
3282 elsif Nkind (Subp_Decl) = N_Subprogram_Body_Stub
3283 and then Present (Corresponding_Spec_Of_Stub (Subp_Decl))
3284 then
3285 Spec_Id := Corresponding_Spec_Of_Stub (Subp_Decl);
3286
3287 else
3288 Spec_Id := Subp_Id;
3289 end if;
3290
3291 -- Examine all postconditions for attribute 'Result and a post-state
3292
3293 Prag := Pre_Post_Conditions (Items);
3294 while Present (Prag) loop
3295 if Nam_In (Pragma_Name (Prag), Name_Postcondition,
3296 Name_Refined_Post)
3297 and then not Error_Posted (Prag)
3298 then
3299 Post_Prag := Prag;
3300 Check_Result_And_Post_State_In_Pragma (Prag, Seen_In_Post);
3301 end if;
3302
3303 Prag := Next_Pragma (Prag);
3304 end loop;
3305
3306 -- Examine the contract cases of the subprogram for attribute 'Result
3307 -- and a post-state.
3308
3309 Prag := Contract_Test_Cases (Items);
3310 while Present (Prag) loop
3311 if Pragma_Name (Prag) = Name_Contract_Cases
3312 and then not Error_Posted (Prag)
3313 then
3314 Case_Prag := Prag;
3315 Check_Result_And_Post_State_In_Pragma (Prag, Seen_In_Case);
3316 end if;
3317
3318 Prag := Next_Pragma (Prag);
3319 end loop;
3320
3321 -- Do not emit any errors if the subprogram is not a function
3322
3323 if not Ekind_In (Spec_Id, E_Function, E_Generic_Function) then
3324 null;
3325
3326 -- Regardless of whether the function has postconditions or contract
3327 -- cases, or whether they mention attribute 'Result, an IN OUT formal
3328 -- parameter is always treated as a result.
3329
3330 elsif Has_In_Out_Parameter (Spec_Id) then
3331 null;
3332
3333 -- The function has both a postcondition and contract cases and they do
3334 -- not mention attribute 'Result.
3335
3336 elsif Present (Case_Prag)
3337 and then not Seen_In_Case
3338 and then Present (Post_Prag)
3339 and then not Seen_In_Post
3340 then
3341 Error_Msg_N
3342 ("neither postcondition nor contract cases mention function "
3343 & "result?T?", Post_Prag);
3344
3345 -- The function has contract cases only and they do not mention
3346 -- attribute 'Result.
3347
3348 elsif Present (Case_Prag) and then not Seen_In_Case then
3349 Error_Msg_N ("contract cases do not mention result?T?", Case_Prag);
3350
3351 -- The function has postconditions only and they do not mention
3352 -- attribute 'Result.
3353
3354 elsif Present (Post_Prag) and then not Seen_In_Post then
3355 Error_Msg_N
3356 ("postcondition does not mention function result?T?", Post_Prag);
3357 end if;
3358 end Check_Result_And_Post_State;
3359
3360 ------------------------------
3361 -- Check_Unprotected_Access --
3362 ------------------------------
3363
3364 procedure Check_Unprotected_Access
3365 (Context : Node_Id;
3366 Expr : Node_Id)
3367 is
3368 Cont_Encl_Typ : Entity_Id;
3369 Pref_Encl_Typ : Entity_Id;
3370
3371 function Enclosing_Protected_Type (Obj : Node_Id) return Entity_Id;
3372 -- Check whether Obj is a private component of a protected object.
3373 -- Return the protected type where the component resides, Empty
3374 -- otherwise.
3375
3376 function Is_Public_Operation return Boolean;
3377 -- Verify that the enclosing operation is callable from outside the
3378 -- protected object, to minimize false positives.
3379
3380 ------------------------------
3381 -- Enclosing_Protected_Type --
3382 ------------------------------
3383
3384 function Enclosing_Protected_Type (Obj : Node_Id) return Entity_Id is
3385 begin
3386 if Is_Entity_Name (Obj) then
3387 declare
3388 Ent : Entity_Id := Entity (Obj);
3389
3390 begin
3391 -- The object can be a renaming of a private component, use
3392 -- the original record component.
3393
3394 if Is_Prival (Ent) then
3395 Ent := Prival_Link (Ent);
3396 end if;
3397
3398 if Is_Protected_Type (Scope (Ent)) then
3399 return Scope (Ent);
3400 end if;
3401 end;
3402 end if;
3403
3404 -- For indexed and selected components, recursively check the prefix
3405
3406 if Nkind_In (Obj, N_Indexed_Component, N_Selected_Component) then
3407 return Enclosing_Protected_Type (Prefix (Obj));
3408
3409 -- The object does not denote a protected component
3410
3411 else
3412 return Empty;
3413 end if;
3414 end Enclosing_Protected_Type;
3415
3416 -------------------------
3417 -- Is_Public_Operation --
3418 -------------------------
3419
3420 function Is_Public_Operation return Boolean is
3421 S : Entity_Id;
3422 E : Entity_Id;
3423
3424 begin
3425 S := Current_Scope;
3426 while Present (S) and then S /= Pref_Encl_Typ loop
3427 if Scope (S) = Pref_Encl_Typ then
3428 E := First_Entity (Pref_Encl_Typ);
3429 while Present (E)
3430 and then E /= First_Private_Entity (Pref_Encl_Typ)
3431 loop
3432 if E = S then
3433 return True;
3434 end if;
3435
3436 Next_Entity (E);
3437 end loop;
3438 end if;
3439
3440 S := Scope (S);
3441 end loop;
3442
3443 return False;
3444 end Is_Public_Operation;
3445
3446 -- Start of processing for Check_Unprotected_Access
3447
3448 begin
3449 if Nkind (Expr) = N_Attribute_Reference
3450 and then Attribute_Name (Expr) = Name_Unchecked_Access
3451 then
3452 Cont_Encl_Typ := Enclosing_Protected_Type (Context);
3453 Pref_Encl_Typ := Enclosing_Protected_Type (Prefix (Expr));
3454
3455 -- Check whether we are trying to export a protected component to a
3456 -- context with an equal or lower access level.
3457
3458 if Present (Pref_Encl_Typ)
3459 and then No (Cont_Encl_Typ)
3460 and then Is_Public_Operation
3461 and then Scope_Depth (Pref_Encl_Typ) >=
3462 Object_Access_Level (Context)
3463 then
3464 Error_Msg_N
3465 ("??possible unprotected access to protected data", Expr);
3466 end if;
3467 end if;
3468 end Check_Unprotected_Access;
3469
3470 ------------------------
3471 -- Collect_Interfaces --
3472 ------------------------
3473
3474 procedure Collect_Interfaces
3475 (T : Entity_Id;
3476 Ifaces_List : out Elist_Id;
3477 Exclude_Parents : Boolean := False;
3478 Use_Full_View : Boolean := True)
3479 is
3480 procedure Collect (Typ : Entity_Id);
3481 -- Subsidiary subprogram used to traverse the whole list
3482 -- of directly and indirectly implemented interfaces
3483
3484 -------------
3485 -- Collect --
3486 -------------
3487
3488 procedure Collect (Typ : Entity_Id) is
3489 Ancestor : Entity_Id;
3490 Full_T : Entity_Id;
3491 Id : Node_Id;
3492 Iface : Entity_Id;
3493
3494 begin
3495 Full_T := Typ;
3496
3497 -- Handle private types and subtypes
3498
3499 if Use_Full_View
3500 and then Is_Private_Type (Typ)
3501 and then Present (Full_View (Typ))
3502 then
3503 Full_T := Full_View (Typ);
3504
3505 if Ekind (Full_T) = E_Record_Subtype then
3506 Full_T := Full_View (Etype (Typ));
3507 end if;
3508 end if;
3509
3510 -- Include the ancestor if we are generating the whole list of
3511 -- abstract interfaces.
3512
3513 if Etype (Full_T) /= Typ
3514
3515 -- Protect the frontend against wrong sources. For example:
3516
3517 -- package P is
3518 -- type A is tagged null record;
3519 -- type B is new A with private;
3520 -- type C is new A with private;
3521 -- private
3522 -- type B is new C with null record;
3523 -- type C is new B with null record;
3524 -- end P;
3525
3526 and then Etype (Full_T) /= T
3527 then
3528 Ancestor := Etype (Full_T);
3529 Collect (Ancestor);
3530
3531 if Is_Interface (Ancestor) and then not Exclude_Parents then
3532 Append_Unique_Elmt (Ancestor, Ifaces_List);
3533 end if;
3534 end if;
3535
3536 -- Traverse the graph of ancestor interfaces
3537
3538 if Is_Non_Empty_List (Abstract_Interface_List (Full_T)) then
3539 Id := First (Abstract_Interface_List (Full_T));
3540 while Present (Id) loop
3541 Iface := Etype (Id);
3542
3543 -- Protect against wrong uses. For example:
3544 -- type I is interface;
3545 -- type O is tagged null record;
3546 -- type Wrong is new I and O with null record; -- ERROR
3547
3548 if Is_Interface (Iface) then
3549 if Exclude_Parents
3550 and then Etype (T) /= T
3551 and then Interface_Present_In_Ancestor (Etype (T), Iface)
3552 then
3553 null;
3554 else
3555 Collect (Iface);
3556 Append_Unique_Elmt (Iface, Ifaces_List);
3557 end if;
3558 end if;
3559
3560 Next (Id);
3561 end loop;
3562 end if;
3563 end Collect;
3564
3565 -- Start of processing for Collect_Interfaces
3566
3567 begin
3568 pragma Assert (Is_Tagged_Type (T) or else Is_Concurrent_Type (T));
3569 Ifaces_List := New_Elmt_List;
3570 Collect (T);
3571 end Collect_Interfaces;
3572
3573 ----------------------------------
3574 -- Collect_Interface_Components --
3575 ----------------------------------
3576
3577 procedure Collect_Interface_Components
3578 (Tagged_Type : Entity_Id;
3579 Components_List : out Elist_Id)
3580 is
3581 procedure Collect (Typ : Entity_Id);
3582 -- Subsidiary subprogram used to climb to the parents
3583
3584 -------------
3585 -- Collect --
3586 -------------
3587
3588 procedure Collect (Typ : Entity_Id) is
3589 Tag_Comp : Entity_Id;
3590 Parent_Typ : Entity_Id;
3591
3592 begin
3593 -- Handle private types
3594
3595 if Present (Full_View (Etype (Typ))) then
3596 Parent_Typ := Full_View (Etype (Typ));
3597 else
3598 Parent_Typ := Etype (Typ);
3599 end if;
3600
3601 if Parent_Typ /= Typ
3602
3603 -- Protect the frontend against wrong sources. For example:
3604
3605 -- package P is
3606 -- type A is tagged null record;
3607 -- type B is new A with private;
3608 -- type C is new A with private;
3609 -- private
3610 -- type B is new C with null record;
3611 -- type C is new B with null record;
3612 -- end P;
3613
3614 and then Parent_Typ /= Tagged_Type
3615 then
3616 Collect (Parent_Typ);
3617 end if;
3618
3619 -- Collect the components containing tags of secondary dispatch
3620 -- tables.
3621
3622 Tag_Comp := Next_Tag_Component (First_Tag_Component (Typ));
3623 while Present (Tag_Comp) loop
3624 pragma Assert (Present (Related_Type (Tag_Comp)));
3625 Append_Elmt (Tag_Comp, Components_List);
3626
3627 Tag_Comp := Next_Tag_Component (Tag_Comp);
3628 end loop;
3629 end Collect;
3630
3631 -- Start of processing for Collect_Interface_Components
3632
3633 begin
3634 pragma Assert (Ekind (Tagged_Type) = E_Record_Type
3635 and then Is_Tagged_Type (Tagged_Type));
3636
3637 Components_List := New_Elmt_List;
3638 Collect (Tagged_Type);
3639 end Collect_Interface_Components;
3640
3641 -----------------------------
3642 -- Collect_Interfaces_Info --
3643 -----------------------------
3644
3645 procedure Collect_Interfaces_Info
3646 (T : Entity_Id;
3647 Ifaces_List : out Elist_Id;
3648 Components_List : out Elist_Id;
3649 Tags_List : out Elist_Id)
3650 is
3651 Comps_List : Elist_Id;
3652 Comp_Elmt : Elmt_Id;
3653 Comp_Iface : Entity_Id;
3654 Iface_Elmt : Elmt_Id;
3655 Iface : Entity_Id;
3656
3657 function Search_Tag (Iface : Entity_Id) return Entity_Id;
3658 -- Search for the secondary tag associated with the interface type
3659 -- Iface that is implemented by T.
3660
3661 ----------------
3662 -- Search_Tag --
3663 ----------------
3664
3665 function Search_Tag (Iface : Entity_Id) return Entity_Id is
3666 ADT : Elmt_Id;
3667 begin
3668 if not Is_CPP_Class (T) then
3669 ADT := Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (T))));
3670 else
3671 ADT := Next_Elmt (First_Elmt (Access_Disp_Table (T)));
3672 end if;
3673
3674 while Present (ADT)
3675 and then Is_Tag (Node (ADT))
3676 and then Related_Type (Node (ADT)) /= Iface
3677 loop
3678 -- Skip secondary dispatch table referencing thunks to user
3679 -- defined primitives covered by this interface.
3680
3681 pragma Assert (Has_Suffix (Node (ADT), 'P'));
3682 Next_Elmt (ADT);
3683
3684 -- Skip secondary dispatch tables of Ada types
3685
3686 if not Is_CPP_Class (T) then
3687
3688 -- Skip secondary dispatch table referencing thunks to
3689 -- predefined primitives.
3690
3691 pragma Assert (Has_Suffix (Node (ADT), 'Y'));
3692 Next_Elmt (ADT);
3693
3694 -- Skip secondary dispatch table referencing user-defined
3695 -- primitives covered by this interface.
3696
3697 pragma Assert (Has_Suffix (Node (ADT), 'D'));
3698 Next_Elmt (ADT);
3699
3700 -- Skip secondary dispatch table referencing predefined
3701 -- primitives.
3702
3703 pragma Assert (Has_Suffix (Node (ADT), 'Z'));
3704 Next_Elmt (ADT);
3705 end if;
3706 end loop;
3707
3708 pragma Assert (Is_Tag (Node (ADT)));
3709 return Node (ADT);
3710 end Search_Tag;
3711
3712 -- Start of processing for Collect_Interfaces_Info
3713
3714 begin
3715 Collect_Interfaces (T, Ifaces_List);
3716 Collect_Interface_Components (T, Comps_List);
3717
3718 -- Search for the record component and tag associated with each
3719 -- interface type of T.
3720
3721 Components_List := New_Elmt_List;
3722 Tags_List := New_Elmt_List;
3723
3724 Iface_Elmt := First_Elmt (Ifaces_List);
3725 while Present (Iface_Elmt) loop
3726 Iface := Node (Iface_Elmt);
3727
3728 -- Associate the primary tag component and the primary dispatch table
3729 -- with all the interfaces that are parents of T
3730
3731 if Is_Ancestor (Iface, T, Use_Full_View => True) then
3732 Append_Elmt (First_Tag_Component (T), Components_List);
3733 Append_Elmt (Node (First_Elmt (Access_Disp_Table (T))), Tags_List);
3734
3735 -- Otherwise search for the tag component and secondary dispatch
3736 -- table of Iface
3737
3738 else
3739 Comp_Elmt := First_Elmt (Comps_List);
3740 while Present (Comp_Elmt) loop
3741 Comp_Iface := Related_Type (Node (Comp_Elmt));
3742
3743 if Comp_Iface = Iface
3744 or else Is_Ancestor (Iface, Comp_Iface, Use_Full_View => True)
3745 then
3746 Append_Elmt (Node (Comp_Elmt), Components_List);
3747 Append_Elmt (Search_Tag (Comp_Iface), Tags_List);
3748 exit;
3749 end if;
3750
3751 Next_Elmt (Comp_Elmt);
3752 end loop;
3753 pragma Assert (Present (Comp_Elmt));
3754 end if;
3755
3756 Next_Elmt (Iface_Elmt);
3757 end loop;
3758 end Collect_Interfaces_Info;
3759
3760 ---------------------
3761 -- Collect_Parents --
3762 ---------------------
3763
3764 procedure Collect_Parents
3765 (T : Entity_Id;
3766 List : out Elist_Id;
3767 Use_Full_View : Boolean := True)
3768 is
3769 Current_Typ : Entity_Id := T;
3770 Parent_Typ : Entity_Id;
3771
3772 begin
3773 List := New_Elmt_List;
3774
3775 -- No action if the if the type has no parents
3776
3777 if T = Etype (T) then
3778 return;
3779 end if;
3780
3781 loop
3782 Parent_Typ := Etype (Current_Typ);
3783
3784 if Is_Private_Type (Parent_Typ)
3785 and then Present (Full_View (Parent_Typ))
3786 and then Use_Full_View
3787 then
3788 Parent_Typ := Full_View (Base_Type (Parent_Typ));
3789 end if;
3790
3791 Append_Elmt (Parent_Typ, List);
3792
3793 exit when Parent_Typ = Current_Typ;
3794 Current_Typ := Parent_Typ;
3795 end loop;
3796 end Collect_Parents;
3797
3798 ----------------------------------
3799 -- Collect_Primitive_Operations --
3800 ----------------------------------
3801
3802 function Collect_Primitive_Operations (T : Entity_Id) return Elist_Id is
3803 B_Type : constant Entity_Id := Base_Type (T);
3804 B_Decl : constant Node_Id := Original_Node (Parent (B_Type));
3805 B_Scope : Entity_Id := Scope (B_Type);
3806 Op_List : Elist_Id;
3807 Formal : Entity_Id;
3808 Is_Prim : Boolean;
3809 Is_Type_In_Pkg : Boolean;
3810 Formal_Derived : Boolean := False;
3811 Id : Entity_Id;
3812
3813 function Match (E : Entity_Id) return Boolean;
3814 -- True if E's base type is B_Type, or E is of an anonymous access type
3815 -- and the base type of its designated type is B_Type.
3816
3817 -----------
3818 -- Match --
3819 -----------
3820
3821 function Match (E : Entity_Id) return Boolean is
3822 Etyp : Entity_Id := Etype (E);
3823
3824 begin
3825 if Ekind (Etyp) = E_Anonymous_Access_Type then
3826 Etyp := Designated_Type (Etyp);
3827 end if;
3828
3829 -- In Ada 2012 a primitive operation may have a formal of an
3830 -- incomplete view of the parent type.
3831
3832 return Base_Type (Etyp) = B_Type
3833 or else
3834 (Ada_Version >= Ada_2012
3835 and then Ekind (Etyp) = E_Incomplete_Type
3836 and then Full_View (Etyp) = B_Type);
3837 end Match;
3838
3839 -- Start of processing for Collect_Primitive_Operations
3840
3841 begin
3842 -- For tagged types, the primitive operations are collected as they
3843 -- are declared, and held in an explicit list which is simply returned.
3844
3845 if Is_Tagged_Type (B_Type) then
3846 return Primitive_Operations (B_Type);
3847
3848 -- An untagged generic type that is a derived type inherits the
3849 -- primitive operations of its parent type. Other formal types only
3850 -- have predefined operators, which are not explicitly represented.
3851
3852 elsif Is_Generic_Type (B_Type) then
3853 if Nkind (B_Decl) = N_Formal_Type_Declaration
3854 and then Nkind (Formal_Type_Definition (B_Decl)) =
3855 N_Formal_Derived_Type_Definition
3856 then
3857 Formal_Derived := True;
3858 else
3859 return New_Elmt_List;
3860 end if;
3861 end if;
3862
3863 Op_List := New_Elmt_List;
3864
3865 if B_Scope = Standard_Standard then
3866 if B_Type = Standard_String then
3867 Append_Elmt (Standard_Op_Concat, Op_List);
3868
3869 elsif B_Type = Standard_Wide_String then
3870 Append_Elmt (Standard_Op_Concatw, Op_List);
3871
3872 else
3873 null;
3874 end if;
3875
3876 -- Locate the primitive subprograms of the type
3877
3878 else
3879 -- The primitive operations appear after the base type, except
3880 -- if the derivation happens within the private part of B_Scope
3881 -- and the type is a private type, in which case both the type
3882 -- and some primitive operations may appear before the base
3883 -- type, and the list of candidates starts after the type.
3884
3885 if In_Open_Scopes (B_Scope)
3886 and then Scope (T) = B_Scope
3887 and then In_Private_Part (B_Scope)
3888 then
3889 Id := Next_Entity (T);
3890
3891 -- In Ada 2012, If the type has an incomplete partial view, there
3892 -- may be primitive operations declared before the full view, so
3893 -- we need to start scanning from the incomplete view, which is
3894 -- earlier on the entity chain.
3895
3896 elsif Nkind (Parent (B_Type)) = N_Full_Type_Declaration
3897 and then Present (Incomplete_View (Parent (B_Type)))
3898 then
3899 Id := Defining_Entity (Incomplete_View (Parent (B_Type)));
3900
3901 else
3902 Id := Next_Entity (B_Type);
3903 end if;
3904
3905 -- Set flag if this is a type in a package spec
3906
3907 Is_Type_In_Pkg :=
3908 Is_Package_Or_Generic_Package (B_Scope)
3909 and then
3910 Nkind (Parent (Declaration_Node (First_Subtype (T)))) /=
3911 N_Package_Body;
3912
3913 while Present (Id) loop
3914
3915 -- Test whether the result type or any of the parameter types of
3916 -- each subprogram following the type match that type when the
3917 -- type is declared in a package spec, is a derived type, or the
3918 -- subprogram is marked as primitive. (The Is_Primitive test is
3919 -- needed to find primitives of nonderived types in declarative
3920 -- parts that happen to override the predefined "=" operator.)
3921
3922 -- Note that generic formal subprograms are not considered to be
3923 -- primitive operations and thus are never inherited.
3924
3925 if Is_Overloadable (Id)
3926 and then (Is_Type_In_Pkg
3927 or else Is_Derived_Type (B_Type)
3928 or else Is_Primitive (Id))
3929 and then Nkind (Parent (Parent (Id)))
3930 not in N_Formal_Subprogram_Declaration
3931 then
3932 Is_Prim := False;
3933
3934 if Match (Id) then
3935 Is_Prim := True;
3936
3937 else
3938 Formal := First_Formal (Id);
3939 while Present (Formal) loop
3940 if Match (Formal) then
3941 Is_Prim := True;
3942 exit;
3943 end if;
3944
3945 Next_Formal (Formal);
3946 end loop;
3947 end if;
3948
3949 -- For a formal derived type, the only primitives are the ones
3950 -- inherited from the parent type. Operations appearing in the
3951 -- package declaration are not primitive for it.
3952
3953 if Is_Prim
3954 and then (not Formal_Derived or else Present (Alias (Id)))
3955 then
3956 -- In the special case of an equality operator aliased to
3957 -- an overriding dispatching equality belonging to the same
3958 -- type, we don't include it in the list of primitives.
3959 -- This avoids inheriting multiple equality operators when
3960 -- deriving from untagged private types whose full type is
3961 -- tagged, which can otherwise cause ambiguities. Note that
3962 -- this should only happen for this kind of untagged parent
3963 -- type, since normally dispatching operations are inherited
3964 -- using the type's Primitive_Operations list.
3965
3966 if Chars (Id) = Name_Op_Eq
3967 and then Is_Dispatching_Operation (Id)
3968 and then Present (Alias (Id))
3969 and then Present (Overridden_Operation (Alias (Id)))
3970 and then Base_Type (Etype (First_Entity (Id))) =
3971 Base_Type (Etype (First_Entity (Alias (Id))))
3972 then
3973 null;
3974
3975 -- Include the subprogram in the list of primitives
3976
3977 else
3978 Append_Elmt (Id, Op_List);
3979 end if;
3980 end if;
3981 end if;
3982
3983 Next_Entity (Id);
3984
3985 -- For a type declared in System, some of its operations may
3986 -- appear in the target-specific extension to System.
3987
3988 if No (Id)
3989 and then B_Scope = RTU_Entity (System)
3990 and then Present_System_Aux
3991 then
3992 B_Scope := System_Aux_Id;
3993 Id := First_Entity (System_Aux_Id);
3994 end if;
3995 end loop;
3996 end if;
3997
3998 return Op_List;
3999 end Collect_Primitive_Operations;
4000
4001 -----------------------------------
4002 -- Compile_Time_Constraint_Error --
4003 -----------------------------------
4004
4005 function Compile_Time_Constraint_Error
4006 (N : Node_Id;
4007 Msg : String;
4008 Ent : Entity_Id := Empty;
4009 Loc : Source_Ptr := No_Location;
4010 Warn : Boolean := False) return Node_Id
4011 is
4012 Msgc : String (1 .. Msg'Length + 3);
4013 -- Copy of message, with room for possible ?? or << and ! at end
4014
4015 Msgl : Natural;
4016 Wmsg : Boolean;
4017 Eloc : Source_Ptr;
4018
4019 -- Start of processing for Compile_Time_Constraint_Error
4020
4021 begin
4022 -- If this is a warning, convert it into an error if we are in code
4023 -- subject to SPARK_Mode being set ON.
4024
4025 Error_Msg_Warn := SPARK_Mode /= On;
4026
4027 -- A static constraint error in an instance body is not a fatal error.
4028 -- we choose to inhibit the message altogether, because there is no
4029 -- obvious node (for now) on which to post it. On the other hand the
4030 -- offending node must be replaced with a constraint_error in any case.
4031
4032 -- No messages are generated if we already posted an error on this node
4033
4034 if not Error_Posted (N) then
4035 if Loc /= No_Location then
4036 Eloc := Loc;
4037 else
4038 Eloc := Sloc (N);
4039 end if;
4040
4041 -- Copy message to Msgc, converting any ? in the message into
4042 -- < instead, so that we have an error in GNATprove mode.
4043
4044 Msgl := Msg'Length;
4045
4046 for J in 1 .. Msgl loop
4047 if Msg (J) = '?' and then (J = 1 or else Msg (J) /= ''') then
4048 Msgc (J) := '<';
4049 else
4050 Msgc (J) := Msg (J);
4051 end if;
4052 end loop;
4053
4054 -- Message is a warning, even in Ada 95 case
4055
4056 if Msg (Msg'Last) = '?' or else Msg (Msg'Last) = '<' then
4057 Wmsg := True;
4058
4059 -- In Ada 83, all messages are warnings. In the private part and
4060 -- the body of an instance, constraint_checks are only warnings.
4061 -- We also make this a warning if the Warn parameter is set.
4062
4063 elsif Warn
4064 or else (Ada_Version = Ada_83 and then Comes_From_Source (N))
4065 then
4066 Msgl := Msgl + 1;
4067 Msgc (Msgl) := '<';
4068 Msgl := Msgl + 1;
4069 Msgc (Msgl) := '<';
4070 Wmsg := True;
4071
4072 elsif In_Instance_Not_Visible then
4073 Msgl := Msgl + 1;
4074 Msgc (Msgl) := '<';
4075 Msgl := Msgl + 1;
4076 Msgc (Msgl) := '<';
4077 Wmsg := True;
4078
4079 -- Otherwise we have a real error message (Ada 95 static case)
4080 -- and we make this an unconditional message. Note that in the
4081 -- warning case we do not make the message unconditional, it seems
4082 -- quite reasonable to delete messages like this (about exceptions
4083 -- that will be raised) in dead code.
4084
4085 else
4086 Wmsg := False;
4087 Msgl := Msgl + 1;
4088 Msgc (Msgl) := '!';
4089 end if;
4090
4091 -- One more test, skip the warning if the related expression is
4092 -- statically unevaluated, since we don't want to warn about what
4093 -- will happen when something is evaluated if it never will be
4094 -- evaluated.
4095
4096 if not Is_Statically_Unevaluated (N) then
4097 Error_Msg_Warn := SPARK_Mode /= On;
4098
4099 if Present (Ent) then
4100 Error_Msg_NEL (Msgc (1 .. Msgl), N, Ent, Eloc);
4101 else
4102 Error_Msg_NEL (Msgc (1 .. Msgl), N, Etype (N), Eloc);
4103 end if;
4104
4105 if Wmsg then
4106
4107 -- Check whether the context is an Init_Proc
4108
4109 if Inside_Init_Proc then
4110 declare
4111 Conc_Typ : constant Entity_Id :=
4112 Corresponding_Concurrent_Type
4113 (Entity (Parameter_Type (First
4114 (Parameter_Specifications
4115 (Parent (Current_Scope))))));
4116
4117 begin
4118 -- Don't complain if the corresponding concurrent type
4119 -- doesn't come from source (i.e. a single task/protected
4120 -- object).
4121
4122 if Present (Conc_Typ)
4123 and then not Comes_From_Source (Conc_Typ)
4124 then
4125 Error_Msg_NEL
4126 ("\& [<<", N, Standard_Constraint_Error, Eloc);
4127
4128 else
4129 if GNATprove_Mode then
4130 Error_Msg_NEL
4131 ("\& would have been raised for objects of this "
4132 & "type", N, Standard_Constraint_Error, Eloc);
4133 else
4134 Error_Msg_NEL
4135 ("\& will be raised for objects of this type??",
4136 N, Standard_Constraint_Error, Eloc);
4137 end if;
4138 end if;
4139 end;
4140
4141 else
4142 Error_Msg_NEL ("\& [<<", N, Standard_Constraint_Error, Eloc);
4143 end if;
4144
4145 else
4146 Error_Msg ("\static expression fails Constraint_Check", Eloc);
4147 Set_Error_Posted (N);
4148 end if;
4149 end if;
4150 end if;
4151
4152 return N;
4153 end Compile_Time_Constraint_Error;
4154
4155 -----------------------
4156 -- Conditional_Delay --
4157 -----------------------
4158
4159 procedure Conditional_Delay (New_Ent, Old_Ent : Entity_Id) is
4160 begin
4161 if Has_Delayed_Freeze (Old_Ent) and then not Is_Frozen (Old_Ent) then
4162 Set_Has_Delayed_Freeze (New_Ent);
4163 end if;
4164 end Conditional_Delay;
4165
4166 ----------------------------
4167 -- Contains_Refined_State --
4168 ----------------------------
4169
4170 function Contains_Refined_State (Prag : Node_Id) return Boolean is
4171 function Has_State_In_Dependency (List : Node_Id) return Boolean;
4172 -- Determine whether a dependency list mentions a state with a visible
4173 -- refinement.
4174
4175 function Has_State_In_Global (List : Node_Id) return Boolean;
4176 -- Determine whether a global list mentions a state with a visible
4177 -- refinement.
4178
4179 function Is_Refined_State (Item : Node_Id) return Boolean;
4180 -- Determine whether Item is a reference to an abstract state with a
4181 -- visible refinement.
4182
4183 -----------------------------
4184 -- Has_State_In_Dependency --
4185 -----------------------------
4186
4187 function Has_State_In_Dependency (List : Node_Id) return Boolean is
4188 Clause : Node_Id;
4189 Output : Node_Id;
4190
4191 begin
4192 -- A null dependency list does not mention any states
4193
4194 if Nkind (List) = N_Null then
4195 return False;
4196
4197 -- Dependency clauses appear as component associations of an
4198 -- aggregate.
4199
4200 elsif Nkind (List) = N_Aggregate
4201 and then Present (Component_Associations (List))
4202 then
4203 Clause := First (Component_Associations (List));
4204 while Present (Clause) loop
4205
4206 -- Inspect the outputs of a dependency clause
4207
4208 Output := First (Choices (Clause));
4209 while Present (Output) loop
4210 if Is_Refined_State (Output) then
4211 return True;
4212 end if;
4213
4214 Next (Output);
4215 end loop;
4216
4217 -- Inspect the outputs of a dependency clause
4218
4219 if Is_Refined_State (Expression (Clause)) then
4220 return True;
4221 end if;
4222
4223 Next (Clause);
4224 end loop;
4225
4226 -- If we get here, then none of the dependency clauses mention a
4227 -- state with visible refinement.
4228
4229 return False;
4230
4231 -- An illegal pragma managed to sneak in
4232
4233 else
4234 raise Program_Error;
4235 end if;
4236 end Has_State_In_Dependency;
4237
4238 -------------------------
4239 -- Has_State_In_Global --
4240 -------------------------
4241
4242 function Has_State_In_Global (List : Node_Id) return Boolean is
4243 Item : Node_Id;
4244
4245 begin
4246 -- A null global list does not mention any states
4247
4248 if Nkind (List) = N_Null then
4249 return False;
4250
4251 -- Simple global list or moded global list declaration
4252
4253 elsif Nkind (List) = N_Aggregate then
4254
4255 -- The declaration of a simple global list appear as a collection
4256 -- of expressions.
4257
4258 if Present (Expressions (List)) then
4259 Item := First (Expressions (List));
4260 while Present (Item) loop
4261 if Is_Refined_State (Item) then
4262 return True;
4263 end if;
4264
4265 Next (Item);
4266 end loop;
4267
4268 -- The declaration of a moded global list appears as a collection
4269 -- of component associations where individual choices denote
4270 -- modes.
4271
4272 else
4273 Item := First (Component_Associations (List));
4274 while Present (Item) loop
4275 if Has_State_In_Global (Expression (Item)) then
4276 return True;
4277 end if;
4278
4279 Next (Item);
4280 end loop;
4281 end if;
4282
4283 -- If we get here, then the simple/moded global list did not
4284 -- mention any states with a visible refinement.
4285
4286 return False;
4287
4288 -- Single global item declaration
4289
4290 elsif Is_Entity_Name (List) then
4291 return Is_Refined_State (List);
4292
4293 -- An illegal pragma managed to sneak in
4294
4295 else
4296 raise Program_Error;
4297 end if;
4298 end Has_State_In_Global;
4299
4300 ----------------------
4301 -- Is_Refined_State --
4302 ----------------------
4303
4304 function Is_Refined_State (Item : Node_Id) return Boolean is
4305 Elmt : Node_Id;
4306 Item_Id : Entity_Id;
4307
4308 begin
4309 if Nkind (Item) = N_Null then
4310 return False;
4311
4312 -- States cannot be subject to attribute 'Result. This case arises
4313 -- in dependency relations.
4314
4315 elsif Nkind (Item) = N_Attribute_Reference
4316 and then Attribute_Name (Item) = Name_Result
4317 then
4318 return False;
4319
4320 -- Multiple items appear as an aggregate. This case arises in
4321 -- dependency relations.
4322
4323 elsif Nkind (Item) = N_Aggregate
4324 and then Present (Expressions (Item))
4325 then
4326 Elmt := First (Expressions (Item));
4327 while Present (Elmt) loop
4328 if Is_Refined_State (Elmt) then
4329 return True;
4330 end if;
4331
4332 Next (Elmt);
4333 end loop;
4334
4335 -- If we get here, then none of the inputs or outputs reference a
4336 -- state with visible refinement.
4337
4338 return False;
4339
4340 -- Single item
4341
4342 else
4343 Item_Id := Entity_Of (Item);
4344
4345 return
4346 Present (Item_Id)
4347 and then Ekind (Item_Id) = E_Abstract_State
4348 and then Has_Visible_Refinement (Item_Id);
4349 end if;
4350 end Is_Refined_State;
4351
4352 -- Local variables
4353
4354 Arg : constant Node_Id :=
4355 Get_Pragma_Arg (First (Pragma_Argument_Associations (Prag)));
4356 Nam : constant Name_Id := Pragma_Name (Prag);
4357
4358 -- Start of processing for Contains_Refined_State
4359
4360 begin
4361 if Nam = Name_Depends then
4362 return Has_State_In_Dependency (Arg);
4363
4364 else pragma Assert (Nam = Name_Global);
4365 return Has_State_In_Global (Arg);
4366 end if;
4367 end Contains_Refined_State;
4368
4369 -------------------------
4370 -- Copy_Component_List --
4371 -------------------------
4372
4373 function Copy_Component_List
4374 (R_Typ : Entity_Id;
4375 Loc : Source_Ptr) return List_Id
4376 is
4377 Comp : Node_Id;
4378 Comps : constant List_Id := New_List;
4379
4380 begin
4381 Comp := First_Component (Underlying_Type (R_Typ));
4382 while Present (Comp) loop
4383 if Comes_From_Source (Comp) then
4384 declare
4385 Comp_Decl : constant Node_Id := Declaration_Node (Comp);
4386 begin
4387 Append_To (Comps,
4388 Make_Component_Declaration (Loc,
4389 Defining_Identifier =>
4390 Make_Defining_Identifier (Loc, Chars (Comp)),
4391 Component_Definition =>
4392 New_Copy_Tree
4393 (Component_Definition (Comp_Decl), New_Sloc => Loc)));
4394 end;
4395 end if;
4396
4397 Next_Component (Comp);
4398 end loop;
4399
4400 return Comps;
4401 end Copy_Component_List;
4402
4403 -------------------------
4404 -- Copy_Parameter_List --
4405 -------------------------
4406
4407 function Copy_Parameter_List (Subp_Id : Entity_Id) return List_Id is
4408 Loc : constant Source_Ptr := Sloc (Subp_Id);
4409 Plist : List_Id;
4410 Formal : Entity_Id;
4411
4412 begin
4413 if No (First_Formal (Subp_Id)) then
4414 return No_List;
4415 else
4416 Plist := New_List;
4417 Formal := First_Formal (Subp_Id);
4418 while Present (Formal) loop
4419 Append_To (Plist,
4420 Make_Parameter_Specification (Loc,
4421 Defining_Identifier =>
4422 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)),
4423 In_Present => In_Present (Parent (Formal)),
4424 Out_Present => Out_Present (Parent (Formal)),
4425 Parameter_Type =>
4426 New_Occurrence_Of (Etype (Formal), Loc),
4427 Expression =>
4428 New_Copy_Tree (Expression (Parent (Formal)))));
4429
4430 Next_Formal (Formal);
4431 end loop;
4432 end if;
4433
4434 return Plist;
4435 end Copy_Parameter_List;
4436
4437 --------------------------
4438 -- Copy_Subprogram_Spec --
4439 --------------------------
4440
4441 function Copy_Subprogram_Spec (Spec : Node_Id) return Node_Id is
4442 Def_Id : Node_Id;
4443 Formal_Spec : Node_Id;
4444 Result : Node_Id;
4445
4446 begin
4447 -- The structure of the original tree must be replicated without any
4448 -- alterations. Use New_Copy_Tree for this purpose.
4449
4450 Result := New_Copy_Tree (Spec);
4451
4452 -- Create a new entity for the defining unit name
4453
4454 Def_Id := Defining_Unit_Name (Result);
4455 Set_Defining_Unit_Name (Result,
4456 Make_Defining_Identifier (Sloc (Def_Id), Chars (Def_Id)));
4457
4458 -- Create new entities for the formal parameters
4459
4460 if Present (Parameter_Specifications (Result)) then
4461 Formal_Spec := First (Parameter_Specifications (Result));
4462 while Present (Formal_Spec) loop
4463 Def_Id := Defining_Identifier (Formal_Spec);
4464 Set_Defining_Identifier (Formal_Spec,
4465 Make_Defining_Identifier (Sloc (Def_Id), Chars (Def_Id)));
4466
4467 Next (Formal_Spec);
4468 end loop;
4469 end if;
4470
4471 return Result;
4472 end Copy_Subprogram_Spec;
4473
4474 --------------------------------
4475 -- Corresponding_Generic_Type --
4476 --------------------------------
4477
4478 function Corresponding_Generic_Type (T : Entity_Id) return Entity_Id is
4479 Inst : Entity_Id;
4480 Gen : Entity_Id;
4481 Typ : Entity_Id;
4482
4483 begin
4484 if not Is_Generic_Actual_Type (T) then
4485 return Any_Type;
4486
4487 -- If the actual is the actual of an enclosing instance, resolution
4488 -- was correct in the generic.
4489
4490 elsif Nkind (Parent (T)) = N_Subtype_Declaration
4491 and then Is_Entity_Name (Subtype_Indication (Parent (T)))
4492 and then
4493 Is_Generic_Actual_Type (Entity (Subtype_Indication (Parent (T))))
4494 then
4495 return Any_Type;
4496
4497 else
4498 Inst := Scope (T);
4499
4500 if Is_Wrapper_Package (Inst) then
4501 Inst := Related_Instance (Inst);
4502 end if;
4503
4504 Gen :=
4505 Generic_Parent
4506 (Specification (Unit_Declaration_Node (Inst)));
4507
4508 -- Generic actual has the same name as the corresponding formal
4509
4510 Typ := First_Entity (Gen);
4511 while Present (Typ) loop
4512 if Chars (Typ) = Chars (T) then
4513 return Typ;
4514 end if;
4515
4516 Next_Entity (Typ);
4517 end loop;
4518
4519 return Any_Type;
4520 end if;
4521 end Corresponding_Generic_Type;
4522
4523 ---------------------------
4524 -- Corresponding_Spec_Of --
4525 ---------------------------
4526
4527 function Corresponding_Spec_Of (Decl : Node_Id) return Entity_Id is
4528 begin
4529 if Nkind_In (Decl, N_Package_Body, N_Subprogram_Body)
4530 and then Present (Corresponding_Spec (Decl))
4531 then
4532 return Corresponding_Spec (Decl);
4533
4534 elsif Nkind_In (Decl, N_Package_Body_Stub, N_Subprogram_Body_Stub)
4535 and then Present (Corresponding_Spec_Of_Stub (Decl))
4536 then
4537 return Corresponding_Spec_Of_Stub (Decl);
4538
4539 else
4540 return Defining_Entity (Decl);
4541 end if;
4542 end Corresponding_Spec_Of;
4543
4544 -----------------------------
4545 -- Create_Generic_Contract --
4546 -----------------------------
4547
4548 procedure Create_Generic_Contract (Unit : Node_Id) is
4549 Templ : constant Node_Id := Original_Node (Unit);
4550 Templ_Id : constant Entity_Id := Defining_Entity (Templ);
4551
4552 procedure Add_Generic_Contract_Pragma (Prag : Node_Id);
4553 -- Add a single contract-related source pragma Prag to the contract of
4554 -- generic template Templ_Id.
4555
4556 ---------------------------------
4557 -- Add_Generic_Contract_Pragma --
4558 ---------------------------------
4559
4560 procedure Add_Generic_Contract_Pragma (Prag : Node_Id) is
4561 Prag_Templ : Node_Id;
4562
4563 begin
4564 -- Mark the pragma to prevent the premature capture of global
4565 -- references when capturing global references of the context
4566 -- (see Save_References_In_Pragma).
4567
4568 Set_Is_Generic_Contract_Pragma (Prag);
4569
4570 -- Pragmas that apply to a generic subprogram declaration are not
4571 -- part of the semantic structure of the generic template:
4572
4573 -- generic
4574 -- procedure Example (Formal : Integer);
4575 -- pragma Precondition (Formal > 0);
4576
4577 -- Create a generic template for such pragmas and link the template
4578 -- of the pragma with the generic template.
4579
4580 if Nkind (Templ) = N_Generic_Subprogram_Declaration then
4581 Rewrite
4582 (Prag, Copy_Generic_Node (Prag, Empty, Instantiating => False));
4583 Prag_Templ := Original_Node (Prag);
4584
4585 Set_Is_Generic_Contract_Pragma (Prag_Templ);
4586 Add_Contract_Item (Prag_Templ, Templ_Id);
4587
4588 -- Otherwise link the pragma with the generic template
4589
4590 else
4591 Add_Contract_Item (Prag, Templ_Id);
4592 end if;
4593 end Add_Generic_Contract_Pragma;
4594
4595 -- Local variables
4596
4597 Context : constant Node_Id := Parent (Unit);
4598 Decl : Node_Id := Empty;
4599
4600 -- Start of processing for Create_Generic_Contract
4601
4602 begin
4603 -- A generic package declaration carries contract-related source pragmas
4604 -- in its visible declarations.
4605
4606 if Nkind (Templ) = N_Generic_Package_Declaration then
4607 Set_Ekind (Templ_Id, E_Generic_Package);
4608
4609 if Present (Visible_Declarations (Specification (Templ))) then
4610 Decl := First (Visible_Declarations (Specification (Templ)));
4611 end if;
4612
4613 -- A generic package body carries contract-related source pragmas in its
4614 -- declarations.
4615
4616 elsif Nkind (Templ) = N_Package_Body then
4617 Set_Ekind (Templ_Id, E_Package_Body);
4618
4619 if Present (Declarations (Templ)) then
4620 Decl := First (Declarations (Templ));
4621 end if;
4622
4623 -- Generic subprogram declaration
4624
4625 elsif Nkind (Templ) = N_Generic_Subprogram_Declaration then
4626 if Nkind (Specification (Templ)) = N_Function_Specification then
4627 Set_Ekind (Templ_Id, E_Generic_Function);
4628 else
4629 Set_Ekind (Templ_Id, E_Generic_Procedure);
4630 end if;
4631
4632 -- When the generic subprogram acts as a compilation unit, inspect
4633 -- the Pragmas_After list for contract-related source pragmas.
4634
4635 if Nkind (Context) = N_Compilation_Unit then
4636 if Present (Aux_Decls_Node (Context))
4637 and then Present (Pragmas_After (Aux_Decls_Node (Context)))
4638 then
4639 Decl := First (Pragmas_After (Aux_Decls_Node (Context)));
4640 end if;
4641
4642 -- Otherwise inspect the successive declarations for contract-related
4643 -- source pragmas.
4644
4645 else
4646 Decl := Next (Unit);
4647 end if;
4648
4649 -- A generic subprogram body carries contract-related source pragmas in
4650 -- its declarations.
4651
4652 elsif Nkind (Templ) = N_Subprogram_Body then
4653 Set_Ekind (Templ_Id, E_Subprogram_Body);
4654
4655 if Present (Declarations (Templ)) then
4656 Decl := First (Declarations (Templ));
4657 end if;
4658 end if;
4659
4660 -- Inspect the relevant declarations looking for contract-related source
4661 -- pragmas and add them to the contract of the generic unit.
4662
4663 while Present (Decl) loop
4664 if Comes_From_Source (Decl) then
4665 if Nkind (Decl) = N_Pragma then
4666
4667 -- The source pragma is a contract annotation
4668
4669 if Is_Contract_Annotation (Decl) then
4670 Add_Generic_Contract_Pragma (Decl);
4671 end if;
4672
4673 -- The region where a contract-related source pragma may appear
4674 -- ends with the first source non-pragma declaration or statement.
4675
4676 else
4677 exit;
4678 end if;
4679 end if;
4680
4681 Next (Decl);
4682 end loop;
4683 end Create_Generic_Contract;
4684
4685 --------------------
4686 -- Current_Entity --
4687 --------------------
4688
4689 -- The currently visible definition for a given identifier is the
4690 -- one most chained at the start of the visibility chain, i.e. the
4691 -- one that is referenced by the Node_Id value of the name of the
4692 -- given identifier.
4693
4694 function Current_Entity (N : Node_Id) return Entity_Id is
4695 begin
4696 return Get_Name_Entity_Id (Chars (N));
4697 end Current_Entity;
4698
4699 -----------------------------
4700 -- Current_Entity_In_Scope --
4701 -----------------------------
4702
4703 function Current_Entity_In_Scope (N : Node_Id) return Entity_Id is
4704 E : Entity_Id;
4705 CS : constant Entity_Id := Current_Scope;
4706
4707 Transient_Case : constant Boolean := Scope_Is_Transient;
4708
4709 begin
4710 E := Get_Name_Entity_Id (Chars (N));
4711 while Present (E)
4712 and then Scope (E) /= CS
4713 and then (not Transient_Case or else Scope (E) /= Scope (CS))
4714 loop
4715 E := Homonym (E);
4716 end loop;
4717
4718 return E;
4719 end Current_Entity_In_Scope;
4720
4721 -------------------
4722 -- Current_Scope --
4723 -------------------
4724
4725 function Current_Scope return Entity_Id is
4726 begin
4727 if Scope_Stack.Last = -1 then
4728 return Standard_Standard;
4729 else
4730 declare
4731 C : constant Entity_Id :=
4732 Scope_Stack.Table (Scope_Stack.Last).Entity;
4733 begin
4734 if Present (C) then
4735 return C;
4736 else
4737 return Standard_Standard;
4738 end if;
4739 end;
4740 end if;
4741 end Current_Scope;
4742
4743 ------------------------
4744 -- Current_Subprogram --
4745 ------------------------
4746
4747 function Current_Subprogram return Entity_Id is
4748 Scop : constant Entity_Id := Current_Scope;
4749 begin
4750 if Is_Subprogram_Or_Generic_Subprogram (Scop) then
4751 return Scop;
4752 else
4753 return Enclosing_Subprogram (Scop);
4754 end if;
4755 end Current_Subprogram;
4756
4757 ----------------------------------
4758 -- Deepest_Type_Access_Level --
4759 ----------------------------------
4760
4761 function Deepest_Type_Access_Level (Typ : Entity_Id) return Uint is
4762 begin
4763 if Ekind (Typ) = E_Anonymous_Access_Type
4764 and then not Is_Local_Anonymous_Access (Typ)
4765 and then Nkind (Associated_Node_For_Itype (Typ)) = N_Object_Declaration
4766 then
4767 -- Typ is the type of an Ada 2012 stand-alone object of an anonymous
4768 -- access type.
4769
4770 return
4771 Scope_Depth (Enclosing_Dynamic_Scope
4772 (Defining_Identifier
4773 (Associated_Node_For_Itype (Typ))));
4774
4775 -- For generic formal type, return Int'Last (infinite).
4776 -- See comment preceding Is_Generic_Type call in Type_Access_Level.
4777
4778 elsif Is_Generic_Type (Root_Type (Typ)) then
4779 return UI_From_Int (Int'Last);
4780
4781 else
4782 return Type_Access_Level (Typ);
4783 end if;
4784 end Deepest_Type_Access_Level;
4785
4786 ---------------------
4787 -- Defining_Entity --
4788 ---------------------
4789
4790 function Defining_Entity (N : Node_Id) return Entity_Id is
4791 K : constant Node_Kind := Nkind (N);
4792 Err : Entity_Id := Empty;
4793
4794 begin
4795 case K is
4796 when
4797 N_Subprogram_Declaration |
4798 N_Abstract_Subprogram_Declaration |
4799 N_Subprogram_Body |
4800 N_Package_Declaration |
4801 N_Subprogram_Renaming_Declaration |
4802 N_Subprogram_Body_Stub |
4803 N_Generic_Subprogram_Declaration |
4804 N_Generic_Package_Declaration |
4805 N_Formal_Subprogram_Declaration |
4806 N_Expression_Function
4807 =>
4808 return Defining_Entity (Specification (N));
4809
4810 when
4811 N_Component_Declaration |
4812 N_Defining_Program_Unit_Name |
4813 N_Discriminant_Specification |
4814 N_Entry_Body |
4815 N_Entry_Declaration |
4816 N_Entry_Index_Specification |
4817 N_Exception_Declaration |
4818 N_Exception_Renaming_Declaration |
4819 N_Formal_Object_Declaration |
4820 N_Formal_Package_Declaration |
4821 N_Formal_Type_Declaration |
4822 N_Full_Type_Declaration |
4823 N_Implicit_Label_Declaration |
4824 N_Incomplete_Type_Declaration |
4825 N_Loop_Parameter_Specification |
4826 N_Number_Declaration |
4827 N_Object_Declaration |
4828 N_Object_Renaming_Declaration |
4829 N_Package_Body_Stub |
4830 N_Parameter_Specification |
4831 N_Private_Extension_Declaration |
4832 N_Private_Type_Declaration |
4833 N_Protected_Body |
4834 N_Protected_Body_Stub |
4835 N_Protected_Type_Declaration |
4836 N_Single_Protected_Declaration |
4837 N_Single_Task_Declaration |
4838 N_Subtype_Declaration |
4839 N_Task_Body |
4840 N_Task_Body_Stub |
4841 N_Task_Type_Declaration
4842 =>
4843 return Defining_Identifier (N);
4844
4845 when N_Subunit =>
4846 return Defining_Entity (Proper_Body (N));
4847
4848 when
4849 N_Function_Instantiation |
4850 N_Function_Specification |
4851 N_Generic_Function_Renaming_Declaration |
4852 N_Generic_Package_Renaming_Declaration |
4853 N_Generic_Procedure_Renaming_Declaration |
4854 N_Package_Body |
4855 N_Package_Instantiation |
4856 N_Package_Renaming_Declaration |
4857 N_Package_Specification |
4858 N_Procedure_Instantiation |
4859 N_Procedure_Specification
4860 =>
4861 declare
4862 Nam : constant Node_Id := Defining_Unit_Name (N);
4863
4864 begin
4865 if Nkind (Nam) in N_Entity then
4866 return Nam;
4867
4868 -- For Error, make up a name and attach to declaration
4869 -- so we can continue semantic analysis
4870
4871 elsif Nam = Error then
4872 Err := Make_Temporary (Sloc (N), 'T');
4873 Set_Defining_Unit_Name (N, Err);
4874
4875 return Err;
4876
4877 -- If not an entity, get defining identifier
4878
4879 else
4880 return Defining_Identifier (Nam);
4881 end if;
4882 end;
4883
4884 when
4885 N_Block_Statement |
4886 N_Loop_Statement
4887 =>
4888 return Entity (Identifier (N));
4889
4890 when others =>
4891 raise Program_Error;
4892
4893 end case;
4894 end Defining_Entity;
4895
4896 --------------------------
4897 -- Denotes_Discriminant --
4898 --------------------------
4899
4900 function Denotes_Discriminant
4901 (N : Node_Id;
4902 Check_Concurrent : Boolean := False) return Boolean
4903 is
4904 E : Entity_Id;
4905
4906 begin
4907 if not Is_Entity_Name (N) or else No (Entity (N)) then
4908 return False;
4909 else
4910 E := Entity (N);
4911 end if;
4912
4913 -- If we are checking for a protected type, the discriminant may have
4914 -- been rewritten as the corresponding discriminal of the original type
4915 -- or of the corresponding concurrent record, depending on whether we
4916 -- are in the spec or body of the protected type.
4917
4918 return Ekind (E) = E_Discriminant
4919 or else
4920 (Check_Concurrent
4921 and then Ekind (E) = E_In_Parameter
4922 and then Present (Discriminal_Link (E))
4923 and then
4924 (Is_Concurrent_Type (Scope (Discriminal_Link (E)))
4925 or else
4926 Is_Concurrent_Record_Type (Scope (Discriminal_Link (E)))));
4927
4928 end Denotes_Discriminant;
4929
4930 -------------------------
4931 -- Denotes_Same_Object --
4932 -------------------------
4933
4934 function Denotes_Same_Object (A1, A2 : Node_Id) return Boolean is
4935 Obj1 : Node_Id := A1;
4936 Obj2 : Node_Id := A2;
4937
4938 function Has_Prefix (N : Node_Id) return Boolean;
4939 -- Return True if N has attribute Prefix
4940
4941 function Is_Renaming (N : Node_Id) return Boolean;
4942 -- Return true if N names a renaming entity
4943
4944 function Is_Valid_Renaming (N : Node_Id) return Boolean;
4945 -- For renamings, return False if the prefix of any dereference within
4946 -- the renamed object_name is a variable, or any expression within the
4947 -- renamed object_name contains references to variables or calls on
4948 -- nonstatic functions; otherwise return True (RM 6.4.1(6.10/3))
4949
4950 ----------------
4951 -- Has_Prefix --
4952 ----------------
4953
4954 function Has_Prefix (N : Node_Id) return Boolean is
4955 begin
4956 return
4957 Nkind_In (N,
4958 N_Attribute_Reference,
4959 N_Expanded_Name,
4960 N_Explicit_Dereference,
4961 N_Indexed_Component,
4962 N_Reference,
4963 N_Selected_Component,
4964 N_Slice);
4965 end Has_Prefix;
4966
4967 -----------------
4968 -- Is_Renaming --
4969 -----------------
4970
4971 function Is_Renaming (N : Node_Id) return Boolean is
4972 begin
4973 return Is_Entity_Name (N)
4974 and then Present (Renamed_Entity (Entity (N)));
4975 end Is_Renaming;
4976
4977 -----------------------
4978 -- Is_Valid_Renaming --
4979 -----------------------
4980
4981 function Is_Valid_Renaming (N : Node_Id) return Boolean is
4982
4983 function Check_Renaming (N : Node_Id) return Boolean;
4984 -- Recursive function used to traverse all the prefixes of N
4985
4986 function Check_Renaming (N : Node_Id) return Boolean is
4987 begin
4988 if Is_Renaming (N)
4989 and then not Check_Renaming (Renamed_Entity (Entity (N)))
4990 then
4991 return False;
4992 end if;
4993
4994 if Nkind (N) = N_Indexed_Component then
4995 declare
4996 Indx : Node_Id;
4997
4998 begin
4999 Indx := First (Expressions (N));
5000 while Present (Indx) loop
5001 if not Is_OK_Static_Expression (Indx) then
5002 return False;
5003 end if;
5004
5005 Next_Index (Indx);
5006 end loop;
5007 end;
5008 end if;
5009
5010 if Has_Prefix (N) then
5011 declare
5012 P : constant Node_Id := Prefix (N);
5013
5014 begin
5015 if Nkind (N) = N_Explicit_Dereference
5016 and then Is_Variable (P)
5017 then
5018 return False;
5019
5020 elsif Is_Entity_Name (P)
5021 and then Ekind (Entity (P)) = E_Function
5022 then
5023 return False;
5024
5025 elsif Nkind (P) = N_Function_Call then
5026 return False;
5027 end if;
5028
5029 -- Recursion to continue traversing the prefix of the
5030 -- renaming expression
5031
5032 return Check_Renaming (P);
5033 end;
5034 end if;
5035
5036 return True;
5037 end Check_Renaming;
5038
5039 -- Start of processing for Is_Valid_Renaming
5040
5041 begin
5042 return Check_Renaming (N);
5043 end Is_Valid_Renaming;
5044
5045 -- Start of processing for Denotes_Same_Object
5046
5047 begin
5048 -- Both names statically denote the same stand-alone object or parameter
5049 -- (RM 6.4.1(6.5/3))
5050
5051 if Is_Entity_Name (Obj1)
5052 and then Is_Entity_Name (Obj2)
5053 and then Entity (Obj1) = Entity (Obj2)
5054 then
5055 return True;
5056 end if;
5057
5058 -- For renamings, the prefix of any dereference within the renamed
5059 -- object_name is not a variable, and any expression within the
5060 -- renamed object_name contains no references to variables nor
5061 -- calls on nonstatic functions (RM 6.4.1(6.10/3)).
5062
5063 if Is_Renaming (Obj1) then
5064 if Is_Valid_Renaming (Obj1) then
5065 Obj1 := Renamed_Entity (Entity (Obj1));
5066 else
5067 return False;
5068 end if;
5069 end if;
5070
5071 if Is_Renaming (Obj2) then
5072 if Is_Valid_Renaming (Obj2) then
5073 Obj2 := Renamed_Entity (Entity (Obj2));
5074 else
5075 return False;
5076 end if;
5077 end if;
5078
5079 -- No match if not same node kind (such cases are handled by
5080 -- Denotes_Same_Prefix)
5081
5082 if Nkind (Obj1) /= Nkind (Obj2) then
5083 return False;
5084
5085 -- After handling valid renamings, one of the two names statically
5086 -- denoted a renaming declaration whose renamed object_name is known
5087 -- to denote the same object as the other (RM 6.4.1(6.10/3))
5088
5089 elsif Is_Entity_Name (Obj1) then
5090 if Is_Entity_Name (Obj2) then
5091 return Entity (Obj1) = Entity (Obj2);
5092 else
5093 return False;
5094 end if;
5095
5096 -- Both names are selected_components, their prefixes are known to
5097 -- denote the same object, and their selector_names denote the same
5098 -- component (RM 6.4.1(6.6/3)).
5099
5100 elsif Nkind (Obj1) = N_Selected_Component then
5101 return Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2))
5102 and then
5103 Entity (Selector_Name (Obj1)) = Entity (Selector_Name (Obj2));
5104
5105 -- Both names are dereferences and the dereferenced names are known to
5106 -- denote the same object (RM 6.4.1(6.7/3))
5107
5108 elsif Nkind (Obj1) = N_Explicit_Dereference then
5109 return Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2));
5110
5111 -- Both names are indexed_components, their prefixes are known to denote
5112 -- the same object, and each of the pairs of corresponding index values
5113 -- are either both static expressions with the same static value or both
5114 -- names that are known to denote the same object (RM 6.4.1(6.8/3))
5115
5116 elsif Nkind (Obj1) = N_Indexed_Component then
5117 if not Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2)) then
5118 return False;
5119 else
5120 declare
5121 Indx1 : Node_Id;
5122 Indx2 : Node_Id;
5123
5124 begin
5125 Indx1 := First (Expressions (Obj1));
5126 Indx2 := First (Expressions (Obj2));
5127 while Present (Indx1) loop
5128
5129 -- Indexes must denote the same static value or same object
5130
5131 if Is_OK_Static_Expression (Indx1) then
5132 if not Is_OK_Static_Expression (Indx2) then
5133 return False;
5134
5135 elsif Expr_Value (Indx1) /= Expr_Value (Indx2) then
5136 return False;
5137 end if;
5138
5139 elsif not Denotes_Same_Object (Indx1, Indx2) then
5140 return False;
5141 end if;
5142
5143 Next (Indx1);
5144 Next (Indx2);
5145 end loop;
5146
5147 return True;
5148 end;
5149 end if;
5150
5151 -- Both names are slices, their prefixes are known to denote the same
5152 -- object, and the two slices have statically matching index constraints
5153 -- (RM 6.4.1(6.9/3))
5154
5155 elsif Nkind (Obj1) = N_Slice
5156 and then Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2))
5157 then
5158 declare
5159 Lo1, Lo2, Hi1, Hi2 : Node_Id;
5160
5161 begin
5162 Get_Index_Bounds (Etype (Obj1), Lo1, Hi1);
5163 Get_Index_Bounds (Etype (Obj2), Lo2, Hi2);
5164
5165 -- Check whether bounds are statically identical. There is no
5166 -- attempt to detect partial overlap of slices.
5167
5168 return Denotes_Same_Object (Lo1, Lo2)
5169 and then
5170 Denotes_Same_Object (Hi1, Hi2);
5171 end;
5172
5173 -- In the recursion, literals appear as indexes
5174
5175 elsif Nkind (Obj1) = N_Integer_Literal
5176 and then
5177 Nkind (Obj2) = N_Integer_Literal
5178 then
5179 return Intval (Obj1) = Intval (Obj2);
5180
5181 else
5182 return False;
5183 end if;
5184 end Denotes_Same_Object;
5185
5186 -------------------------
5187 -- Denotes_Same_Prefix --
5188 -------------------------
5189
5190 function Denotes_Same_Prefix (A1, A2 : Node_Id) return Boolean is
5191
5192 begin
5193 if Is_Entity_Name (A1) then
5194 if Nkind_In (A2, N_Selected_Component, N_Indexed_Component)
5195 and then not Is_Access_Type (Etype (A1))
5196 then
5197 return Denotes_Same_Object (A1, Prefix (A2))
5198 or else Denotes_Same_Prefix (A1, Prefix (A2));
5199 else
5200 return False;
5201 end if;
5202
5203 elsif Is_Entity_Name (A2) then
5204 return Denotes_Same_Prefix (A1 => A2, A2 => A1);
5205
5206 elsif Nkind_In (A1, N_Selected_Component, N_Indexed_Component, N_Slice)
5207 and then
5208 Nkind_In (A2, N_Selected_Component, N_Indexed_Component, N_Slice)
5209 then
5210 declare
5211 Root1, Root2 : Node_Id;
5212 Depth1, Depth2 : Int := 0;
5213
5214 begin
5215 Root1 := Prefix (A1);
5216 while not Is_Entity_Name (Root1) loop
5217 if not Nkind_In
5218 (Root1, N_Selected_Component, N_Indexed_Component)
5219 then
5220 return False;
5221 else
5222 Root1 := Prefix (Root1);
5223 end if;
5224
5225 Depth1 := Depth1 + 1;
5226 end loop;
5227
5228 Root2 := Prefix (A2);
5229 while not Is_Entity_Name (Root2) loop
5230 if not Nkind_In (Root2, N_Selected_Component,
5231 N_Indexed_Component)
5232 then
5233 return False;
5234 else
5235 Root2 := Prefix (Root2);
5236 end if;
5237
5238 Depth2 := Depth2 + 1;
5239 end loop;
5240
5241 -- If both have the same depth and they do not denote the same
5242 -- object, they are disjoint and no warning is needed.
5243
5244 if Depth1 = Depth2 then
5245 return False;
5246
5247 elsif Depth1 > Depth2 then
5248 Root1 := Prefix (A1);
5249 for J in 1 .. Depth1 - Depth2 - 1 loop
5250 Root1 := Prefix (Root1);
5251 end loop;
5252
5253 return Denotes_Same_Object (Root1, A2);
5254
5255 else
5256 Root2 := Prefix (A2);
5257 for J in 1 .. Depth2 - Depth1 - 1 loop
5258 Root2 := Prefix (Root2);
5259 end loop;
5260
5261 return Denotes_Same_Object (A1, Root2);
5262 end if;
5263 end;
5264
5265 else
5266 return False;
5267 end if;
5268 end Denotes_Same_Prefix;
5269
5270 ----------------------
5271 -- Denotes_Variable --
5272 ----------------------
5273
5274 function Denotes_Variable (N : Node_Id) return Boolean is
5275 begin
5276 return Is_Variable (N) and then Paren_Count (N) = 0;
5277 end Denotes_Variable;
5278
5279 -----------------------------
5280 -- Depends_On_Discriminant --
5281 -----------------------------
5282
5283 function Depends_On_Discriminant (N : Node_Id) return Boolean is
5284 L : Node_Id;
5285 H : Node_Id;
5286
5287 begin
5288 Get_Index_Bounds (N, L, H);
5289 return Denotes_Discriminant (L) or else Denotes_Discriminant (H);
5290 end Depends_On_Discriminant;
5291
5292 -------------------------
5293 -- Designate_Same_Unit --
5294 -------------------------
5295
5296 function Designate_Same_Unit
5297 (Name1 : Node_Id;
5298 Name2 : Node_Id) return Boolean
5299 is
5300 K1 : constant Node_Kind := Nkind (Name1);
5301 K2 : constant Node_Kind := Nkind (Name2);
5302
5303 function Prefix_Node (N : Node_Id) return Node_Id;
5304 -- Returns the parent unit name node of a defining program unit name
5305 -- or the prefix if N is a selected component or an expanded name.
5306
5307 function Select_Node (N : Node_Id) return Node_Id;
5308 -- Returns the defining identifier node of a defining program unit
5309 -- name or the selector node if N is a selected component or an
5310 -- expanded name.
5311
5312 -----------------
5313 -- Prefix_Node --
5314 -----------------
5315
5316 function Prefix_Node (N : Node_Id) return Node_Id is
5317 begin
5318 if Nkind (N) = N_Defining_Program_Unit_Name then
5319 return Name (N);
5320 else
5321 return Prefix (N);
5322 end if;
5323 end Prefix_Node;
5324
5325 -----------------
5326 -- Select_Node --
5327 -----------------
5328
5329 function Select_Node (N : Node_Id) return Node_Id is
5330 begin
5331 if Nkind (N) = N_Defining_Program_Unit_Name then
5332 return Defining_Identifier (N);
5333 else
5334 return Selector_Name (N);
5335 end if;
5336 end Select_Node;
5337
5338 -- Start of processing for Designate_Same_Unit
5339
5340 begin
5341 if Nkind_In (K1, N_Identifier, N_Defining_Identifier)
5342 and then
5343 Nkind_In (K2, N_Identifier, N_Defining_Identifier)
5344 then
5345 return Chars (Name1) = Chars (Name2);
5346
5347 elsif Nkind_In (K1, N_Expanded_Name,
5348 N_Selected_Component,
5349 N_Defining_Program_Unit_Name)
5350 and then
5351 Nkind_In (K2, N_Expanded_Name,
5352 N_Selected_Component,
5353 N_Defining_Program_Unit_Name)
5354 then
5355 return
5356 (Chars (Select_Node (Name1)) = Chars (Select_Node (Name2)))
5357 and then
5358 Designate_Same_Unit (Prefix_Node (Name1), Prefix_Node (Name2));
5359
5360 else
5361 return False;
5362 end if;
5363 end Designate_Same_Unit;
5364
5365 ------------------------------------------
5366 -- function Dynamic_Accessibility_Level --
5367 ------------------------------------------
5368
5369 function Dynamic_Accessibility_Level (Expr : Node_Id) return Node_Id is
5370 E : Entity_Id;
5371 Loc : constant Source_Ptr := Sloc (Expr);
5372
5373 function Make_Level_Literal (Level : Uint) return Node_Id;
5374 -- Construct an integer literal representing an accessibility level
5375 -- with its type set to Natural.
5376
5377 ------------------------
5378 -- Make_Level_Literal --
5379 ------------------------
5380
5381 function Make_Level_Literal (Level : Uint) return Node_Id is
5382 Result : constant Node_Id := Make_Integer_Literal (Loc, Level);
5383 begin
5384 Set_Etype (Result, Standard_Natural);
5385 return Result;
5386 end Make_Level_Literal;
5387
5388 -- Start of processing for Dynamic_Accessibility_Level
5389
5390 begin
5391 if Is_Entity_Name (Expr) then
5392 E := Entity (Expr);
5393
5394 if Present (Renamed_Object (E)) then
5395 return Dynamic_Accessibility_Level (Renamed_Object (E));
5396 end if;
5397
5398 if Is_Formal (E) or else Ekind_In (E, E_Variable, E_Constant) then
5399 if Present (Extra_Accessibility (E)) then
5400 return New_Occurrence_Of (Extra_Accessibility (E), Loc);
5401 end if;
5402 end if;
5403 end if;
5404
5405 -- Unimplemented: Ptr.all'Access, where Ptr has Extra_Accessibility ???
5406
5407 case Nkind (Expr) is
5408
5409 -- For access discriminant, the level of the enclosing object
5410
5411 when N_Selected_Component =>
5412 if Ekind (Entity (Selector_Name (Expr))) = E_Discriminant
5413 and then Ekind (Etype (Entity (Selector_Name (Expr)))) =
5414 E_Anonymous_Access_Type
5415 then
5416 return Make_Level_Literal (Object_Access_Level (Expr));
5417 end if;
5418
5419 when N_Attribute_Reference =>
5420 case Get_Attribute_Id (Attribute_Name (Expr)) is
5421
5422 -- For X'Access, the level of the prefix X
5423
5424 when Attribute_Access =>
5425 return Make_Level_Literal
5426 (Object_Access_Level (Prefix (Expr)));
5427
5428 -- Treat the unchecked attributes as library-level
5429
5430 when Attribute_Unchecked_Access |
5431 Attribute_Unrestricted_Access =>
5432 return Make_Level_Literal (Scope_Depth (Standard_Standard));
5433
5434 -- No other access-valued attributes
5435
5436 when others =>
5437 raise Program_Error;
5438 end case;
5439
5440 when N_Allocator =>
5441
5442 -- Unimplemented: depends on context. As an actual parameter where
5443 -- formal type is anonymous, use
5444 -- Scope_Depth (Current_Scope) + 1.
5445 -- For other cases, see 3.10.2(14/3) and following. ???
5446
5447 null;
5448
5449 when N_Type_Conversion =>
5450 if not Is_Local_Anonymous_Access (Etype (Expr)) then
5451
5452 -- Handle type conversions introduced for a rename of an
5453 -- Ada 2012 stand-alone object of an anonymous access type.
5454
5455 return Dynamic_Accessibility_Level (Expression (Expr));
5456 end if;
5457
5458 when others =>
5459 null;
5460 end case;
5461
5462 return Make_Level_Literal (Type_Access_Level (Etype (Expr)));
5463 end Dynamic_Accessibility_Level;
5464
5465 -----------------------------------
5466 -- Effective_Extra_Accessibility --
5467 -----------------------------------
5468
5469 function Effective_Extra_Accessibility (Id : Entity_Id) return Entity_Id is
5470 begin
5471 if Present (Renamed_Object (Id))
5472 and then Is_Entity_Name (Renamed_Object (Id))
5473 then
5474 return Effective_Extra_Accessibility (Entity (Renamed_Object (Id)));
5475 else
5476 return Extra_Accessibility (Id);
5477 end if;
5478 end Effective_Extra_Accessibility;
5479
5480 -----------------------------
5481 -- Effective_Reads_Enabled --
5482 -----------------------------
5483
5484 function Effective_Reads_Enabled (Id : Entity_Id) return Boolean is
5485 begin
5486 return Has_Enabled_Property (Id, Name_Effective_Reads);
5487 end Effective_Reads_Enabled;
5488
5489 ------------------------------
5490 -- Effective_Writes_Enabled --
5491 ------------------------------
5492
5493 function Effective_Writes_Enabled (Id : Entity_Id) return Boolean is
5494 begin
5495 return Has_Enabled_Property (Id, Name_Effective_Writes);
5496 end Effective_Writes_Enabled;
5497
5498 ------------------------------
5499 -- Enclosing_Comp_Unit_Node --
5500 ------------------------------
5501
5502 function Enclosing_Comp_Unit_Node (N : Node_Id) return Node_Id is
5503 Current_Node : Node_Id;
5504
5505 begin
5506 Current_Node := N;
5507 while Present (Current_Node)
5508 and then Nkind (Current_Node) /= N_Compilation_Unit
5509 loop
5510 Current_Node := Parent (Current_Node);
5511 end loop;
5512
5513 if Nkind (Current_Node) /= N_Compilation_Unit then
5514 return Empty;
5515 else
5516 return Current_Node;
5517 end if;
5518 end Enclosing_Comp_Unit_Node;
5519
5520 --------------------------
5521 -- Enclosing_CPP_Parent --
5522 --------------------------
5523
5524 function Enclosing_CPP_Parent (Typ : Entity_Id) return Entity_Id is
5525 Parent_Typ : Entity_Id := Typ;
5526
5527 begin
5528 while not Is_CPP_Class (Parent_Typ)
5529 and then Etype (Parent_Typ) /= Parent_Typ
5530 loop
5531 Parent_Typ := Etype (Parent_Typ);
5532
5533 if Is_Private_Type (Parent_Typ) then
5534 Parent_Typ := Full_View (Base_Type (Parent_Typ));
5535 end if;
5536 end loop;
5537
5538 pragma Assert (Is_CPP_Class (Parent_Typ));
5539 return Parent_Typ;
5540 end Enclosing_CPP_Parent;
5541
5542 ----------------------------
5543 -- Enclosing_Generic_Body --
5544 ----------------------------
5545
5546 function Enclosing_Generic_Body
5547 (N : Node_Id) return Node_Id
5548 is
5549 P : Node_Id;
5550 Decl : Node_Id;
5551 Spec : Node_Id;
5552
5553 begin
5554 P := Parent (N);
5555 while Present (P) loop
5556 if Nkind (P) = N_Package_Body
5557 or else Nkind (P) = N_Subprogram_Body
5558 then
5559 Spec := Corresponding_Spec (P);
5560
5561 if Present (Spec) then
5562 Decl := Unit_Declaration_Node (Spec);
5563
5564 if Nkind (Decl) = N_Generic_Package_Declaration
5565 or else Nkind (Decl) = N_Generic_Subprogram_Declaration
5566 then
5567 return P;
5568 end if;
5569 end if;
5570 end if;
5571
5572 P := Parent (P);
5573 end loop;
5574
5575 return Empty;
5576 end Enclosing_Generic_Body;
5577
5578 ----------------------------
5579 -- Enclosing_Generic_Unit --
5580 ----------------------------
5581
5582 function Enclosing_Generic_Unit
5583 (N : Node_Id) return Node_Id
5584 is
5585 P : Node_Id;
5586 Decl : Node_Id;
5587 Spec : Node_Id;
5588
5589 begin
5590 P := Parent (N);
5591 while Present (P) loop
5592 if Nkind (P) = N_Generic_Package_Declaration
5593 or else Nkind (P) = N_Generic_Subprogram_Declaration
5594 then
5595 return P;
5596
5597 elsif Nkind (P) = N_Package_Body
5598 or else Nkind (P) = N_Subprogram_Body
5599 then
5600 Spec := Corresponding_Spec (P);
5601
5602 if Present (Spec) then
5603 Decl := Unit_Declaration_Node (Spec);
5604
5605 if Nkind (Decl) = N_Generic_Package_Declaration
5606 or else Nkind (Decl) = N_Generic_Subprogram_Declaration
5607 then
5608 return Decl;
5609 end if;
5610 end if;
5611 end if;
5612
5613 P := Parent (P);
5614 end loop;
5615
5616 return Empty;
5617 end Enclosing_Generic_Unit;
5618
5619 -------------------------------
5620 -- Enclosing_Lib_Unit_Entity --
5621 -------------------------------
5622
5623 function Enclosing_Lib_Unit_Entity
5624 (E : Entity_Id := Current_Scope) return Entity_Id
5625 is
5626 Unit_Entity : Entity_Id;
5627
5628 begin
5629 -- Look for enclosing library unit entity by following scope links.
5630 -- Equivalent to, but faster than indexing through the scope stack.
5631
5632 Unit_Entity := E;
5633 while (Present (Scope (Unit_Entity))
5634 and then Scope (Unit_Entity) /= Standard_Standard)
5635 and not Is_Child_Unit (Unit_Entity)
5636 loop
5637 Unit_Entity := Scope (Unit_Entity);
5638 end loop;
5639
5640 return Unit_Entity;
5641 end Enclosing_Lib_Unit_Entity;
5642
5643 -----------------------
5644 -- Enclosing_Package --
5645 -----------------------
5646
5647 function Enclosing_Package (E : Entity_Id) return Entity_Id is
5648 Dynamic_Scope : constant Entity_Id := Enclosing_Dynamic_Scope (E);
5649
5650 begin
5651 if Dynamic_Scope = Standard_Standard then
5652 return Standard_Standard;
5653
5654 elsif Dynamic_Scope = Empty then
5655 return Empty;
5656
5657 elsif Ekind_In (Dynamic_Scope, E_Package, E_Package_Body,
5658 E_Generic_Package)
5659 then
5660 return Dynamic_Scope;
5661
5662 else
5663 return Enclosing_Package (Dynamic_Scope);
5664 end if;
5665 end Enclosing_Package;
5666
5667 --------------------------
5668 -- Enclosing_Subprogram --
5669 --------------------------
5670
5671 function Enclosing_Subprogram (E : Entity_Id) return Entity_Id is
5672 Dynamic_Scope : constant Entity_Id := Enclosing_Dynamic_Scope (E);
5673
5674 begin
5675 if Dynamic_Scope = Standard_Standard then
5676 return Empty;
5677
5678 elsif Dynamic_Scope = Empty then
5679 return Empty;
5680
5681 elsif Ekind (Dynamic_Scope) = E_Subprogram_Body then
5682 return Corresponding_Spec (Parent (Parent (Dynamic_Scope)));
5683
5684 elsif Ekind (Dynamic_Scope) = E_Block
5685 or else Ekind (Dynamic_Scope) = E_Return_Statement
5686 then
5687 return Enclosing_Subprogram (Dynamic_Scope);
5688
5689 elsif Ekind (Dynamic_Scope) = E_Task_Type then
5690 return Get_Task_Body_Procedure (Dynamic_Scope);
5691
5692 elsif Ekind (Dynamic_Scope) = E_Limited_Private_Type
5693 and then Present (Full_View (Dynamic_Scope))
5694 and then Ekind (Full_View (Dynamic_Scope)) = E_Task_Type
5695 then
5696 return Get_Task_Body_Procedure (Full_View (Dynamic_Scope));
5697
5698 -- No body is generated if the protected operation is eliminated
5699
5700 elsif Convention (Dynamic_Scope) = Convention_Protected
5701 and then not Is_Eliminated (Dynamic_Scope)
5702 and then Present (Protected_Body_Subprogram (Dynamic_Scope))
5703 then
5704 return Protected_Body_Subprogram (Dynamic_Scope);
5705
5706 else
5707 return Dynamic_Scope;
5708 end if;
5709 end Enclosing_Subprogram;
5710
5711 ------------------------
5712 -- Ensure_Freeze_Node --
5713 ------------------------
5714
5715 procedure Ensure_Freeze_Node (E : Entity_Id) is
5716 FN : Node_Id;
5717 begin
5718 if No (Freeze_Node (E)) then
5719 FN := Make_Freeze_Entity (Sloc (E));
5720 Set_Has_Delayed_Freeze (E);
5721 Set_Freeze_Node (E, FN);
5722 Set_Access_Types_To_Process (FN, No_Elist);
5723 Set_TSS_Elist (FN, No_Elist);
5724 Set_Entity (FN, E);
5725 end if;
5726 end Ensure_Freeze_Node;
5727
5728 ----------------
5729 -- Enter_Name --
5730 ----------------
5731
5732 procedure Enter_Name (Def_Id : Entity_Id) is
5733 C : constant Entity_Id := Current_Entity (Def_Id);
5734 E : constant Entity_Id := Current_Entity_In_Scope (Def_Id);
5735 S : constant Entity_Id := Current_Scope;
5736
5737 begin
5738 Generate_Definition (Def_Id);
5739
5740 -- Add new name to current scope declarations. Check for duplicate
5741 -- declaration, which may or may not be a genuine error.
5742
5743 if Present (E) then
5744
5745 -- Case of previous entity entered because of a missing declaration
5746 -- or else a bad subtype indication. Best is to use the new entity,
5747 -- and make the previous one invisible.
5748
5749 if Etype (E) = Any_Type then
5750 Set_Is_Immediately_Visible (E, False);
5751
5752 -- Case of renaming declaration constructed for package instances.
5753 -- if there is an explicit declaration with the same identifier,
5754 -- the renaming is not immediately visible any longer, but remains
5755 -- visible through selected component notation.
5756
5757 elsif Nkind (Parent (E)) = N_Package_Renaming_Declaration
5758 and then not Comes_From_Source (E)
5759 then
5760 Set_Is_Immediately_Visible (E, False);
5761
5762 -- The new entity may be the package renaming, which has the same
5763 -- same name as a generic formal which has been seen already.
5764
5765 elsif Nkind (Parent (Def_Id)) = N_Package_Renaming_Declaration
5766 and then not Comes_From_Source (Def_Id)
5767 then
5768 Set_Is_Immediately_Visible (E, False);
5769
5770 -- For a fat pointer corresponding to a remote access to subprogram,
5771 -- we use the same identifier as the RAS type, so that the proper
5772 -- name appears in the stub. This type is only retrieved through
5773 -- the RAS type and never by visibility, and is not added to the
5774 -- visibility list (see below).
5775
5776 elsif Nkind (Parent (Def_Id)) = N_Full_Type_Declaration
5777 and then Ekind (Def_Id) = E_Record_Type
5778 and then Present (Corresponding_Remote_Type (Def_Id))
5779 then
5780 null;
5781
5782 -- Case of an implicit operation or derived literal. The new entity
5783 -- hides the implicit one, which is removed from all visibility,
5784 -- i.e. the entity list of its scope, and homonym chain of its name.
5785
5786 elsif (Is_Overloadable (E) and then Is_Inherited_Operation (E))
5787 or else Is_Internal (E)
5788 then
5789 declare
5790 Prev : Entity_Id;
5791 Prev_Vis : Entity_Id;
5792 Decl : constant Node_Id := Parent (E);
5793
5794 begin
5795 -- If E is an implicit declaration, it cannot be the first
5796 -- entity in the scope.
5797
5798 Prev := First_Entity (Current_Scope);
5799 while Present (Prev) and then Next_Entity (Prev) /= E loop
5800 Next_Entity (Prev);
5801 end loop;
5802
5803 if No (Prev) then
5804
5805 -- If E is not on the entity chain of the current scope,
5806 -- it is an implicit declaration in the generic formal
5807 -- part of a generic subprogram. When analyzing the body,
5808 -- the generic formals are visible but not on the entity
5809 -- chain of the subprogram. The new entity will become
5810 -- the visible one in the body.
5811
5812 pragma Assert
5813 (Nkind (Parent (Decl)) = N_Generic_Subprogram_Declaration);
5814 null;
5815
5816 else
5817 Set_Next_Entity (Prev, Next_Entity (E));
5818
5819 if No (Next_Entity (Prev)) then
5820 Set_Last_Entity (Current_Scope, Prev);
5821 end if;
5822
5823 if E = Current_Entity (E) then
5824 Prev_Vis := Empty;
5825
5826 else
5827 Prev_Vis := Current_Entity (E);
5828 while Homonym (Prev_Vis) /= E loop
5829 Prev_Vis := Homonym (Prev_Vis);
5830 end loop;
5831 end if;
5832
5833 if Present (Prev_Vis) then
5834
5835 -- Skip E in the visibility chain
5836
5837 Set_Homonym (Prev_Vis, Homonym (E));
5838
5839 else
5840 Set_Name_Entity_Id (Chars (E), Homonym (E));
5841 end if;
5842 end if;
5843 end;
5844
5845 -- This section of code could use a comment ???
5846
5847 elsif Present (Etype (E))
5848 and then Is_Concurrent_Type (Etype (E))
5849 and then E = Def_Id
5850 then
5851 return;
5852
5853 -- If the homograph is a protected component renaming, it should not
5854 -- be hiding the current entity. Such renamings are treated as weak
5855 -- declarations.
5856
5857 elsif Is_Prival (E) then
5858 Set_Is_Immediately_Visible (E, False);
5859
5860 -- In this case the current entity is a protected component renaming.
5861 -- Perform minimal decoration by setting the scope and return since
5862 -- the prival should not be hiding other visible entities.
5863
5864 elsif Is_Prival (Def_Id) then
5865 Set_Scope (Def_Id, Current_Scope);
5866 return;
5867
5868 -- Analogous to privals, the discriminal generated for an entry index
5869 -- parameter acts as a weak declaration. Perform minimal decoration
5870 -- to avoid bogus errors.
5871
5872 elsif Is_Discriminal (Def_Id)
5873 and then Ekind (Discriminal_Link (Def_Id)) = E_Entry_Index_Parameter
5874 then
5875 Set_Scope (Def_Id, Current_Scope);
5876 return;
5877
5878 -- In the body or private part of an instance, a type extension may
5879 -- introduce a component with the same name as that of an actual. The
5880 -- legality rule is not enforced, but the semantics of the full type
5881 -- with two components of same name are not clear at this point???
5882
5883 elsif In_Instance_Not_Visible then
5884 null;
5885
5886 -- When compiling a package body, some child units may have become
5887 -- visible. They cannot conflict with local entities that hide them.
5888
5889 elsif Is_Child_Unit (E)
5890 and then In_Open_Scopes (Scope (E))
5891 and then not Is_Immediately_Visible (E)
5892 then
5893 null;
5894
5895 -- Conversely, with front-end inlining we may compile the parent body
5896 -- first, and a child unit subsequently. The context is now the
5897 -- parent spec, and body entities are not visible.
5898
5899 elsif Is_Child_Unit (Def_Id)
5900 and then Is_Package_Body_Entity (E)
5901 and then not In_Package_Body (Current_Scope)
5902 then
5903 null;
5904
5905 -- Case of genuine duplicate declaration
5906
5907 else
5908 Error_Msg_Sloc := Sloc (E);
5909
5910 -- If the previous declaration is an incomplete type declaration
5911 -- this may be an attempt to complete it with a private type. The
5912 -- following avoids confusing cascaded errors.
5913
5914 if Nkind (Parent (E)) = N_Incomplete_Type_Declaration
5915 and then Nkind (Parent (Def_Id)) = N_Private_Type_Declaration
5916 then
5917 Error_Msg_N
5918 ("incomplete type cannot be completed with a private " &
5919 "declaration", Parent (Def_Id));
5920 Set_Is_Immediately_Visible (E, False);
5921 Set_Full_View (E, Def_Id);
5922
5923 -- An inherited component of a record conflicts with a new
5924 -- discriminant. The discriminant is inserted first in the scope,
5925 -- but the error should be posted on it, not on the component.
5926
5927 elsif Ekind (E) = E_Discriminant
5928 and then Present (Scope (Def_Id))
5929 and then Scope (Def_Id) /= Current_Scope
5930 then
5931 Error_Msg_Sloc := Sloc (Def_Id);
5932 Error_Msg_N ("& conflicts with declaration#", E);
5933 return;
5934
5935 -- If the name of the unit appears in its own context clause, a
5936 -- dummy package with the name has already been created, and the
5937 -- error emitted. Try to continue quietly.
5938
5939 elsif Error_Posted (E)
5940 and then Sloc (E) = No_Location
5941 and then Nkind (Parent (E)) = N_Package_Specification
5942 and then Current_Scope = Standard_Standard
5943 then
5944 Set_Scope (Def_Id, Current_Scope);
5945 return;
5946
5947 else
5948 Error_Msg_N ("& conflicts with declaration#", Def_Id);
5949
5950 -- Avoid cascaded messages with duplicate components in
5951 -- derived types.
5952
5953 if Ekind_In (E, E_Component, E_Discriminant) then
5954 return;
5955 end if;
5956 end if;
5957
5958 if Nkind (Parent (Parent (Def_Id))) =
5959 N_Generic_Subprogram_Declaration
5960 and then Def_Id =
5961 Defining_Entity (Specification (Parent (Parent (Def_Id))))
5962 then
5963 Error_Msg_N ("\generic units cannot be overloaded", Def_Id);
5964 end if;
5965
5966 -- If entity is in standard, then we are in trouble, because it
5967 -- means that we have a library package with a duplicated name.
5968 -- That's hard to recover from, so abort.
5969
5970 if S = Standard_Standard then
5971 raise Unrecoverable_Error;
5972
5973 -- Otherwise we continue with the declaration. Having two
5974 -- identical declarations should not cause us too much trouble.
5975
5976 else
5977 null;
5978 end if;
5979 end if;
5980 end if;
5981
5982 -- If we fall through, declaration is OK, at least OK enough to continue
5983
5984 -- If Def_Id is a discriminant or a record component we are in the midst
5985 -- of inheriting components in a derived record definition. Preserve
5986 -- their Ekind and Etype.
5987
5988 if Ekind_In (Def_Id, E_Discriminant, E_Component) then
5989 null;
5990
5991 -- If a type is already set, leave it alone (happens when a type
5992 -- declaration is reanalyzed following a call to the optimizer).
5993
5994 elsif Present (Etype (Def_Id)) then
5995 null;
5996
5997 -- Otherwise, the kind E_Void insures that premature uses of the entity
5998 -- will be detected. Any_Type insures that no cascaded errors will occur
5999
6000 else
6001 Set_Ekind (Def_Id, E_Void);
6002 Set_Etype (Def_Id, Any_Type);
6003 end if;
6004
6005 -- Inherited discriminants and components in derived record types are
6006 -- immediately visible. Itypes are not.
6007
6008 -- Unless the Itype is for a record type with a corresponding remote
6009 -- type (what is that about, it was not commented ???)
6010
6011 if Ekind_In (Def_Id, E_Discriminant, E_Component)
6012 or else
6013 ((not Is_Record_Type (Def_Id)
6014 or else No (Corresponding_Remote_Type (Def_Id)))
6015 and then not Is_Itype (Def_Id))
6016 then
6017 Set_Is_Immediately_Visible (Def_Id);
6018 Set_Current_Entity (Def_Id);
6019 end if;
6020
6021 Set_Homonym (Def_Id, C);
6022 Append_Entity (Def_Id, S);
6023 Set_Public_Status (Def_Id);
6024
6025 -- Declaring a homonym is not allowed in SPARK ...
6026
6027 if Present (C) and then Restriction_Check_Required (SPARK_05) then
6028 declare
6029 Enclosing_Subp : constant Node_Id := Enclosing_Subprogram (Def_Id);
6030 Enclosing_Pack : constant Node_Id := Enclosing_Package (Def_Id);
6031 Other_Scope : constant Node_Id := Enclosing_Dynamic_Scope (C);
6032
6033 begin
6034 -- ... unless the new declaration is in a subprogram, and the
6035 -- visible declaration is a variable declaration or a parameter
6036 -- specification outside that subprogram.
6037
6038 if Present (Enclosing_Subp)
6039 and then Nkind_In (Parent (C), N_Object_Declaration,
6040 N_Parameter_Specification)
6041 and then not Scope_Within_Or_Same (Other_Scope, Enclosing_Subp)
6042 then
6043 null;
6044
6045 -- ... or the new declaration is in a package, and the visible
6046 -- declaration occurs outside that package.
6047
6048 elsif Present (Enclosing_Pack)
6049 and then not Scope_Within_Or_Same (Other_Scope, Enclosing_Pack)
6050 then
6051 null;
6052
6053 -- ... or the new declaration is a component declaration in a
6054 -- record type definition.
6055
6056 elsif Nkind (Parent (Def_Id)) = N_Component_Declaration then
6057 null;
6058
6059 -- Don't issue error for non-source entities
6060
6061 elsif Comes_From_Source (Def_Id)
6062 and then Comes_From_Source (C)
6063 then
6064 Error_Msg_Sloc := Sloc (C);
6065 Check_SPARK_05_Restriction
6066 ("redeclaration of identifier &#", Def_Id);
6067 end if;
6068 end;
6069 end if;
6070
6071 -- Warn if new entity hides an old one
6072
6073 if Warn_On_Hiding and then Present (C)
6074
6075 -- Don't warn for record components since they always have a well
6076 -- defined scope which does not confuse other uses. Note that in
6077 -- some cases, Ekind has not been set yet.
6078
6079 and then Ekind (C) /= E_Component
6080 and then Ekind (C) /= E_Discriminant
6081 and then Nkind (Parent (C)) /= N_Component_Declaration
6082 and then Ekind (Def_Id) /= E_Component
6083 and then Ekind (Def_Id) /= E_Discriminant
6084 and then Nkind (Parent (Def_Id)) /= N_Component_Declaration
6085
6086 -- Don't warn for one character variables. It is too common to use
6087 -- such variables as locals and will just cause too many false hits.
6088
6089 and then Length_Of_Name (Chars (C)) /= 1
6090
6091 -- Don't warn for non-source entities
6092
6093 and then Comes_From_Source (C)
6094 and then Comes_From_Source (Def_Id)
6095
6096 -- Don't warn unless entity in question is in extended main source
6097
6098 and then In_Extended_Main_Source_Unit (Def_Id)
6099
6100 -- Finally, the hidden entity must be either immediately visible or
6101 -- use visible (i.e. from a used package).
6102
6103 and then
6104 (Is_Immediately_Visible (C)
6105 or else
6106 Is_Potentially_Use_Visible (C))
6107 then
6108 Error_Msg_Sloc := Sloc (C);
6109 Error_Msg_N ("declaration hides &#?h?", Def_Id);
6110 end if;
6111 end Enter_Name;
6112
6113 ---------------
6114 -- Entity_Of --
6115 ---------------
6116
6117 function Entity_Of (N : Node_Id) return Entity_Id is
6118 Id : Entity_Id;
6119
6120 begin
6121 Id := Empty;
6122
6123 if Is_Entity_Name (N) then
6124 Id := Entity (N);
6125
6126 -- Follow a possible chain of renamings to reach the root renamed
6127 -- object.
6128
6129 while Present (Id) and then Present (Renamed_Object (Id)) loop
6130 if Is_Entity_Name (Renamed_Object (Id)) then
6131 Id := Entity (Renamed_Object (Id));
6132 else
6133 Id := Empty;
6134 exit;
6135 end if;
6136 end loop;
6137 end if;
6138
6139 return Id;
6140 end Entity_Of;
6141
6142 --------------------------
6143 -- Explain_Limited_Type --
6144 --------------------------
6145
6146 procedure Explain_Limited_Type (T : Entity_Id; N : Node_Id) is
6147 C : Entity_Id;
6148
6149 begin
6150 -- For array, component type must be limited
6151
6152 if Is_Array_Type (T) then
6153 Error_Msg_Node_2 := T;
6154 Error_Msg_NE
6155 ("\component type& of type& is limited", N, Component_Type (T));
6156 Explain_Limited_Type (Component_Type (T), N);
6157
6158 elsif Is_Record_Type (T) then
6159
6160 -- No need for extra messages if explicit limited record
6161
6162 if Is_Limited_Record (Base_Type (T)) then
6163 return;
6164 end if;
6165
6166 -- Otherwise find a limited component. Check only components that
6167 -- come from source, or inherited components that appear in the
6168 -- source of the ancestor.
6169
6170 C := First_Component (T);
6171 while Present (C) loop
6172 if Is_Limited_Type (Etype (C))
6173 and then
6174 (Comes_From_Source (C)
6175 or else
6176 (Present (Original_Record_Component (C))
6177 and then
6178 Comes_From_Source (Original_Record_Component (C))))
6179 then
6180 Error_Msg_Node_2 := T;
6181 Error_Msg_NE ("\component& of type& has limited type", N, C);
6182 Explain_Limited_Type (Etype (C), N);
6183 return;
6184 end if;
6185
6186 Next_Component (C);
6187 end loop;
6188
6189 -- The type may be declared explicitly limited, even if no component
6190 -- of it is limited, in which case we fall out of the loop.
6191 return;
6192 end if;
6193 end Explain_Limited_Type;
6194
6195 -------------------------------
6196 -- Extensions_Visible_Status --
6197 -------------------------------
6198
6199 function Extensions_Visible_Status
6200 (Id : Entity_Id) return Extensions_Visible_Mode
6201 is
6202 Arg : Node_Id;
6203 Decl : Node_Id;
6204 Expr : Node_Id;
6205 Prag : Node_Id;
6206 Subp : Entity_Id;
6207
6208 begin
6209 -- When a formal parameter is subject to Extensions_Visible, the pragma
6210 -- is stored in the contract of related subprogram.
6211
6212 if Is_Formal (Id) then
6213 Subp := Scope (Id);
6214
6215 elsif Is_Subprogram_Or_Generic_Subprogram (Id) then
6216 Subp := Id;
6217
6218 -- No other construct carries this pragma
6219
6220 else
6221 return Extensions_Visible_None;
6222 end if;
6223
6224 Prag := Get_Pragma (Subp, Pragma_Extensions_Visible);
6225
6226 -- In certain cases analysis may request the Extensions_Visible status
6227 -- of an expression function before the pragma has been analyzed yet.
6228 -- Inspect the declarative items after the expression function looking
6229 -- for the pragma (if any).
6230
6231 if No (Prag) and then Is_Expression_Function (Subp) then
6232 Decl := Next (Unit_Declaration_Node (Subp));
6233 while Present (Decl) loop
6234 if Nkind (Decl) = N_Pragma
6235 and then Pragma_Name (Decl) = Name_Extensions_Visible
6236 then
6237 Prag := Decl;
6238 exit;
6239
6240 -- A source construct ends the region where Extensions_Visible may
6241 -- appear, stop the traversal. An expanded expression function is
6242 -- no longer a source construct, but it must still be recognized.
6243
6244 elsif Comes_From_Source (Decl)
6245 or else
6246 (Nkind_In (Decl, N_Subprogram_Body,
6247 N_Subprogram_Declaration)
6248 and then Is_Expression_Function (Defining_Entity (Decl)))
6249 then
6250 exit;
6251 end if;
6252
6253 Next (Decl);
6254 end loop;
6255 end if;
6256
6257 -- Extract the value from the Boolean expression (if any)
6258
6259 if Present (Prag) then
6260 Arg := First (Pragma_Argument_Associations (Prag));
6261
6262 if Present (Arg) then
6263 Expr := Get_Pragma_Arg (Arg);
6264
6265 -- When the associated subprogram is an expression function, the
6266 -- argument of the pragma may not have been analyzed.
6267
6268 if not Analyzed (Expr) then
6269 Preanalyze_And_Resolve (Expr, Standard_Boolean);
6270 end if;
6271
6272 -- Guard against cascading errors when the argument of pragma
6273 -- Extensions_Visible is not a valid static Boolean expression.
6274
6275 if Error_Posted (Expr) then
6276 return Extensions_Visible_None;
6277
6278 elsif Is_True (Expr_Value (Expr)) then
6279 return Extensions_Visible_True;
6280
6281 else
6282 return Extensions_Visible_False;
6283 end if;
6284
6285 -- Otherwise the aspect or pragma defaults to True
6286
6287 else
6288 return Extensions_Visible_True;
6289 end if;
6290
6291 -- Otherwise aspect or pragma Extensions_Visible is not inherited or
6292 -- directly specified. In SPARK code, its value defaults to "False".
6293
6294 elsif SPARK_Mode = On then
6295 return Extensions_Visible_False;
6296
6297 -- In non-SPARK code, aspect or pragma Extensions_Visible defaults to
6298 -- "True".
6299
6300 else
6301 return Extensions_Visible_True;
6302 end if;
6303 end Extensions_Visible_Status;
6304
6305 -----------------
6306 -- Find_Actual --
6307 -----------------
6308
6309 procedure Find_Actual
6310 (N : Node_Id;
6311 Formal : out Entity_Id;
6312 Call : out Node_Id)
6313 is
6314 Parnt : constant Node_Id := Parent (N);
6315 Actual : Node_Id;
6316
6317 begin
6318 if Nkind_In (Parnt, N_Indexed_Component, N_Selected_Component)
6319 and then N = Prefix (Parnt)
6320 then
6321 Find_Actual (Parnt, Formal, Call);
6322 return;
6323
6324 elsif Nkind (Parnt) = N_Parameter_Association
6325 and then N = Explicit_Actual_Parameter (Parnt)
6326 then
6327 Call := Parent (Parnt);
6328
6329 elsif Nkind (Parnt) in N_Subprogram_Call then
6330 Call := Parnt;
6331
6332 else
6333 Formal := Empty;
6334 Call := Empty;
6335 return;
6336 end if;
6337
6338 -- If we have a call to a subprogram look for the parameter. Note that
6339 -- we exclude overloaded calls, since we don't know enough to be sure
6340 -- of giving the right answer in this case.
6341
6342 if Nkind_In (Call, N_Function_Call, N_Procedure_Call_Statement)
6343 and then Is_Entity_Name (Name (Call))
6344 and then Present (Entity (Name (Call)))
6345 and then Is_Overloadable (Entity (Name (Call)))
6346 and then not Is_Overloaded (Name (Call))
6347 then
6348 -- If node is name in call it is not an actual
6349
6350 if N = Name (Call) then
6351 Call := Empty;
6352 Formal := Empty;
6353 return;
6354 end if;
6355
6356 -- Fall here if we are definitely a parameter
6357
6358 Actual := First_Actual (Call);
6359 Formal := First_Formal (Entity (Name (Call)));
6360 while Present (Formal) and then Present (Actual) loop
6361 if Actual = N then
6362 return;
6363
6364 -- An actual that is the prefix in a prefixed call may have
6365 -- been rewritten in the call, after the deferred reference
6366 -- was collected. Check if sloc and kinds and names match.
6367
6368 elsif Sloc (Actual) = Sloc (N)
6369 and then Nkind (Actual) = N_Identifier
6370 and then Nkind (Actual) = Nkind (N)
6371 and then Chars (Actual) = Chars (N)
6372 then
6373 return;
6374
6375 else
6376 Actual := Next_Actual (Actual);
6377 Formal := Next_Formal (Formal);
6378 end if;
6379 end loop;
6380 end if;
6381
6382 -- Fall through here if we did not find matching actual
6383
6384 Formal := Empty;
6385 Call := Empty;
6386 end Find_Actual;
6387
6388 ---------------------------
6389 -- Find_Body_Discriminal --
6390 ---------------------------
6391
6392 function Find_Body_Discriminal
6393 (Spec_Discriminant : Entity_Id) return Entity_Id
6394 is
6395 Tsk : Entity_Id;
6396 Disc : Entity_Id;
6397
6398 begin
6399 -- If expansion is suppressed, then the scope can be the concurrent type
6400 -- itself rather than a corresponding concurrent record type.
6401
6402 if Is_Concurrent_Type (Scope (Spec_Discriminant)) then
6403 Tsk := Scope (Spec_Discriminant);
6404
6405 else
6406 pragma Assert (Is_Concurrent_Record_Type (Scope (Spec_Discriminant)));
6407
6408 Tsk := Corresponding_Concurrent_Type (Scope (Spec_Discriminant));
6409 end if;
6410
6411 -- Find discriminant of original concurrent type, and use its current
6412 -- discriminal, which is the renaming within the task/protected body.
6413
6414 Disc := First_Discriminant (Tsk);
6415 while Present (Disc) loop
6416 if Chars (Disc) = Chars (Spec_Discriminant) then
6417 return Discriminal (Disc);
6418 end if;
6419
6420 Next_Discriminant (Disc);
6421 end loop;
6422
6423 -- That loop should always succeed in finding a matching entry and
6424 -- returning. Fatal error if not.
6425
6426 raise Program_Error;
6427 end Find_Body_Discriminal;
6428
6429 -------------------------------------
6430 -- Find_Corresponding_Discriminant --
6431 -------------------------------------
6432
6433 function Find_Corresponding_Discriminant
6434 (Id : Node_Id;
6435 Typ : Entity_Id) return Entity_Id
6436 is
6437 Par_Disc : Entity_Id;
6438 Old_Disc : Entity_Id;
6439 New_Disc : Entity_Id;
6440
6441 begin
6442 Par_Disc := Original_Record_Component (Original_Discriminant (Id));
6443
6444 -- The original type may currently be private, and the discriminant
6445 -- only appear on its full view.
6446
6447 if Is_Private_Type (Scope (Par_Disc))
6448 and then not Has_Discriminants (Scope (Par_Disc))
6449 and then Present (Full_View (Scope (Par_Disc)))
6450 then
6451 Old_Disc := First_Discriminant (Full_View (Scope (Par_Disc)));
6452 else
6453 Old_Disc := First_Discriminant (Scope (Par_Disc));
6454 end if;
6455
6456 if Is_Class_Wide_Type (Typ) then
6457 New_Disc := First_Discriminant (Root_Type (Typ));
6458 else
6459 New_Disc := First_Discriminant (Typ);
6460 end if;
6461
6462 while Present (Old_Disc) and then Present (New_Disc) loop
6463 if Old_Disc = Par_Disc then
6464 return New_Disc;
6465 end if;
6466
6467 Next_Discriminant (Old_Disc);
6468 Next_Discriminant (New_Disc);
6469 end loop;
6470
6471 -- Should always find it
6472
6473 raise Program_Error;
6474 end Find_Corresponding_Discriminant;
6475
6476 ----------------------------------
6477 -- Find_Enclosing_Iterator_Loop --
6478 ----------------------------------
6479
6480 function Find_Enclosing_Iterator_Loop (Id : Entity_Id) return Entity_Id is
6481 Constr : Node_Id;
6482 S : Entity_Id;
6483
6484 begin
6485 -- Traverse the scope chain looking for an iterator loop. Such loops are
6486 -- usually transformed into blocks, hence the use of Original_Node.
6487
6488 S := Id;
6489 while Present (S) and then S /= Standard_Standard loop
6490 if Ekind (S) = E_Loop
6491 and then Nkind (Parent (S)) = N_Implicit_Label_Declaration
6492 then
6493 Constr := Original_Node (Label_Construct (Parent (S)));
6494
6495 if Nkind (Constr) = N_Loop_Statement
6496 and then Present (Iteration_Scheme (Constr))
6497 and then Nkind (Iterator_Specification
6498 (Iteration_Scheme (Constr))) =
6499 N_Iterator_Specification
6500 then
6501 return S;
6502 end if;
6503 end if;
6504
6505 S := Scope (S);
6506 end loop;
6507
6508 return Empty;
6509 end Find_Enclosing_Iterator_Loop;
6510
6511 ------------------------------------
6512 -- Find_Loop_In_Conditional_Block --
6513 ------------------------------------
6514
6515 function Find_Loop_In_Conditional_Block (N : Node_Id) return Node_Id is
6516 Stmt : Node_Id;
6517
6518 begin
6519 Stmt := N;
6520
6521 if Nkind (Stmt) = N_If_Statement then
6522 Stmt := First (Then_Statements (Stmt));
6523 end if;
6524
6525 pragma Assert (Nkind (Stmt) = N_Block_Statement);
6526
6527 -- Inspect the statements of the conditional block. In general the loop
6528 -- should be the first statement in the statement sequence of the block,
6529 -- but the finalization machinery may have introduced extra object
6530 -- declarations.
6531
6532 Stmt := First (Statements (Handled_Statement_Sequence (Stmt)));
6533 while Present (Stmt) loop
6534 if Nkind (Stmt) = N_Loop_Statement then
6535 return Stmt;
6536 end if;
6537
6538 Next (Stmt);
6539 end loop;
6540
6541 -- The expansion of attribute 'Loop_Entry produced a malformed block
6542
6543 raise Program_Error;
6544 end Find_Loop_In_Conditional_Block;
6545
6546 --------------------------
6547 -- Find_Overlaid_Entity --
6548 --------------------------
6549
6550 procedure Find_Overlaid_Entity
6551 (N : Node_Id;
6552 Ent : out Entity_Id;
6553 Off : out Boolean)
6554 is
6555 Expr : Node_Id;
6556
6557 begin
6558 -- We are looking for one of the two following forms:
6559
6560 -- for X'Address use Y'Address
6561
6562 -- or
6563
6564 -- Const : constant Address := expr;
6565 -- ...
6566 -- for X'Address use Const;
6567
6568 -- In the second case, the expr is either Y'Address, or recursively a
6569 -- constant that eventually references Y'Address.
6570
6571 Ent := Empty;
6572 Off := False;
6573
6574 if Nkind (N) = N_Attribute_Definition_Clause
6575 and then Chars (N) = Name_Address
6576 then
6577 Expr := Expression (N);
6578
6579 -- This loop checks the form of the expression for Y'Address,
6580 -- using recursion to deal with intermediate constants.
6581
6582 loop
6583 -- Check for Y'Address
6584
6585 if Nkind (Expr) = N_Attribute_Reference
6586 and then Attribute_Name (Expr) = Name_Address
6587 then
6588 Expr := Prefix (Expr);
6589 exit;
6590
6591 -- Check for Const where Const is a constant entity
6592
6593 elsif Is_Entity_Name (Expr)
6594 and then Ekind (Entity (Expr)) = E_Constant
6595 then
6596 Expr := Constant_Value (Entity (Expr));
6597
6598 -- Anything else does not need checking
6599
6600 else
6601 return;
6602 end if;
6603 end loop;
6604
6605 -- This loop checks the form of the prefix for an entity, using
6606 -- recursion to deal with intermediate components.
6607
6608 loop
6609 -- Check for Y where Y is an entity
6610
6611 if Is_Entity_Name (Expr) then
6612 Ent := Entity (Expr);
6613 return;
6614
6615 -- Check for components
6616
6617 elsif
6618 Nkind_In (Expr, N_Selected_Component, N_Indexed_Component)
6619 then
6620 Expr := Prefix (Expr);
6621 Off := True;
6622
6623 -- Anything else does not need checking
6624
6625 else
6626 return;
6627 end if;
6628 end loop;
6629 end if;
6630 end Find_Overlaid_Entity;
6631
6632 -------------------------
6633 -- Find_Parameter_Type --
6634 -------------------------
6635
6636 function Find_Parameter_Type (Param : Node_Id) return Entity_Id is
6637 begin
6638 if Nkind (Param) /= N_Parameter_Specification then
6639 return Empty;
6640
6641 -- For an access parameter, obtain the type from the formal entity
6642 -- itself, because access to subprogram nodes do not carry a type.
6643 -- Shouldn't we always use the formal entity ???
6644
6645 elsif Nkind (Parameter_Type (Param)) = N_Access_Definition then
6646 return Etype (Defining_Identifier (Param));
6647
6648 else
6649 return Etype (Parameter_Type (Param));
6650 end if;
6651 end Find_Parameter_Type;
6652
6653 -----------------------------------
6654 -- Find_Placement_In_State_Space --
6655 -----------------------------------
6656
6657 procedure Find_Placement_In_State_Space
6658 (Item_Id : Entity_Id;
6659 Placement : out State_Space_Kind;
6660 Pack_Id : out Entity_Id)
6661 is
6662 Context : Entity_Id;
6663
6664 begin
6665 -- Assume that the item does not appear in the state space of a package
6666
6667 Placement := Not_In_Package;
6668 Pack_Id := Empty;
6669
6670 -- Climb the scope stack and examine the enclosing context
6671
6672 Context := Scope (Item_Id);
6673 while Present (Context) and then Context /= Standard_Standard loop
6674 if Ekind (Context) = E_Package then
6675 Pack_Id := Context;
6676
6677 -- A package body is a cut off point for the traversal as the item
6678 -- cannot be visible to the outside from this point on. Note that
6679 -- this test must be done first as a body is also classified as a
6680 -- private part.
6681
6682 if In_Package_Body (Context) then
6683 Placement := Body_State_Space;
6684 return;
6685
6686 -- The private part of a package is a cut off point for the
6687 -- traversal as the item cannot be visible to the outside from
6688 -- this point on.
6689
6690 elsif In_Private_Part (Context) then
6691 Placement := Private_State_Space;
6692 return;
6693
6694 -- When the item appears in the visible state space of a package,
6695 -- continue to climb the scope stack as this may not be the final
6696 -- state space.
6697
6698 else
6699 Placement := Visible_State_Space;
6700
6701 -- The visible state space of a child unit acts as the proper
6702 -- placement of an item.
6703
6704 if Is_Child_Unit (Context) then
6705 return;
6706 end if;
6707 end if;
6708
6709 -- The item or its enclosing package appear in a construct that has
6710 -- no state space.
6711
6712 else
6713 Placement := Not_In_Package;
6714 return;
6715 end if;
6716
6717 Context := Scope (Context);
6718 end loop;
6719 end Find_Placement_In_State_Space;
6720
6721 ------------------------
6722 -- Find_Specific_Type --
6723 ------------------------
6724
6725 function Find_Specific_Type (CW : Entity_Id) return Entity_Id is
6726 Typ : Entity_Id := Root_Type (CW);
6727
6728 begin
6729 if Ekind (Typ) = E_Incomplete_Type then
6730 if From_Limited_With (Typ) then
6731 Typ := Non_Limited_View (Typ);
6732 else
6733 Typ := Full_View (Typ);
6734 end if;
6735 end if;
6736
6737 if Is_Private_Type (Typ)
6738 and then not Is_Tagged_Type (Typ)
6739 and then Present (Full_View (Typ))
6740 then
6741 return Full_View (Typ);
6742 else
6743 return Typ;
6744 end if;
6745 end Find_Specific_Type;
6746
6747 -----------------------------
6748 -- Find_Static_Alternative --
6749 -----------------------------
6750
6751 function Find_Static_Alternative (N : Node_Id) return Node_Id is
6752 Expr : constant Node_Id := Expression (N);
6753 Val : constant Uint := Expr_Value (Expr);
6754 Alt : Node_Id;
6755 Choice : Node_Id;
6756
6757 begin
6758 Alt := First (Alternatives (N));
6759
6760 Search : loop
6761 if Nkind (Alt) /= N_Pragma then
6762 Choice := First (Discrete_Choices (Alt));
6763 while Present (Choice) loop
6764
6765 -- Others choice, always matches
6766
6767 if Nkind (Choice) = N_Others_Choice then
6768 exit Search;
6769
6770 -- Range, check if value is in the range
6771
6772 elsif Nkind (Choice) = N_Range then
6773 exit Search when
6774 Val >= Expr_Value (Low_Bound (Choice))
6775 and then
6776 Val <= Expr_Value (High_Bound (Choice));
6777
6778 -- Choice is a subtype name. Note that we know it must
6779 -- be a static subtype, since otherwise it would have
6780 -- been diagnosed as illegal.
6781
6782 elsif Is_Entity_Name (Choice)
6783 and then Is_Type (Entity (Choice))
6784 then
6785 exit Search when Is_In_Range (Expr, Etype (Choice),
6786 Assume_Valid => False);
6787
6788 -- Choice is a subtype indication
6789
6790 elsif Nkind (Choice) = N_Subtype_Indication then
6791 declare
6792 C : constant Node_Id := Constraint (Choice);
6793 R : constant Node_Id := Range_Expression (C);
6794
6795 begin
6796 exit Search when
6797 Val >= Expr_Value (Low_Bound (R))
6798 and then
6799 Val <= Expr_Value (High_Bound (R));
6800 end;
6801
6802 -- Choice is a simple expression
6803
6804 else
6805 exit Search when Val = Expr_Value (Choice);
6806 end if;
6807
6808 Next (Choice);
6809 end loop;
6810 end if;
6811
6812 Next (Alt);
6813 pragma Assert (Present (Alt));
6814 end loop Search;
6815
6816 -- The above loop *must* terminate by finding a match, since
6817 -- we know the case statement is valid, and the value of the
6818 -- expression is known at compile time. When we fall out of
6819 -- the loop, Alt points to the alternative that we know will
6820 -- be selected at run time.
6821
6822 return Alt;
6823 end Find_Static_Alternative;
6824
6825 ------------------
6826 -- First_Actual --
6827 ------------------
6828
6829 function First_Actual (Node : Node_Id) return Node_Id is
6830 N : Node_Id;
6831
6832 begin
6833 if No (Parameter_Associations (Node)) then
6834 return Empty;
6835 end if;
6836
6837 N := First (Parameter_Associations (Node));
6838
6839 if Nkind (N) = N_Parameter_Association then
6840 return First_Named_Actual (Node);
6841 else
6842 return N;
6843 end if;
6844 end First_Actual;
6845
6846 -----------------------
6847 -- Gather_Components --
6848 -----------------------
6849
6850 procedure Gather_Components
6851 (Typ : Entity_Id;
6852 Comp_List : Node_Id;
6853 Governed_By : List_Id;
6854 Into : Elist_Id;
6855 Report_Errors : out Boolean)
6856 is
6857 Assoc : Node_Id;
6858 Variant : Node_Id;
6859 Discrete_Choice : Node_Id;
6860 Comp_Item : Node_Id;
6861
6862 Discrim : Entity_Id;
6863 Discrim_Name : Node_Id;
6864 Discrim_Value : Node_Id;
6865
6866 begin
6867 Report_Errors := False;
6868
6869 if No (Comp_List) or else Null_Present (Comp_List) then
6870 return;
6871
6872 elsif Present (Component_Items (Comp_List)) then
6873 Comp_Item := First (Component_Items (Comp_List));
6874
6875 else
6876 Comp_Item := Empty;
6877 end if;
6878
6879 while Present (Comp_Item) loop
6880
6881 -- Skip the tag of a tagged record, the interface tags, as well
6882 -- as all items that are not user components (anonymous types,
6883 -- rep clauses, Parent field, controller field).
6884
6885 if Nkind (Comp_Item) = N_Component_Declaration then
6886 declare
6887 Comp : constant Entity_Id := Defining_Identifier (Comp_Item);
6888 begin
6889 if not Is_Tag (Comp) and then Chars (Comp) /= Name_uParent then
6890 Append_Elmt (Comp, Into);
6891 end if;
6892 end;
6893 end if;
6894
6895 Next (Comp_Item);
6896 end loop;
6897
6898 if No (Variant_Part (Comp_List)) then
6899 return;
6900 else
6901 Discrim_Name := Name (Variant_Part (Comp_List));
6902 Variant := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
6903 end if;
6904
6905 -- Look for the discriminant that governs this variant part.
6906 -- The discriminant *must* be in the Governed_By List
6907
6908 Assoc := First (Governed_By);
6909 Find_Constraint : loop
6910 Discrim := First (Choices (Assoc));
6911 exit Find_Constraint when Chars (Discrim_Name) = Chars (Discrim)
6912 or else (Present (Corresponding_Discriminant (Entity (Discrim)))
6913 and then
6914 Chars (Corresponding_Discriminant (Entity (Discrim))) =
6915 Chars (Discrim_Name))
6916 or else Chars (Original_Record_Component (Entity (Discrim)))
6917 = Chars (Discrim_Name);
6918
6919 if No (Next (Assoc)) then
6920 if not Is_Constrained (Typ)
6921 and then Is_Derived_Type (Typ)
6922 and then Present (Stored_Constraint (Typ))
6923 then
6924 -- If the type is a tagged type with inherited discriminants,
6925 -- use the stored constraint on the parent in order to find
6926 -- the values of discriminants that are otherwise hidden by an
6927 -- explicit constraint. Renamed discriminants are handled in
6928 -- the code above.
6929
6930 -- If several parent discriminants are renamed by a single
6931 -- discriminant of the derived type, the call to obtain the
6932 -- Corresponding_Discriminant field only retrieves the last
6933 -- of them. We recover the constraint on the others from the
6934 -- Stored_Constraint as well.
6935
6936 declare
6937 D : Entity_Id;
6938 C : Elmt_Id;
6939
6940 begin
6941 D := First_Discriminant (Etype (Typ));
6942 C := First_Elmt (Stored_Constraint (Typ));
6943 while Present (D) and then Present (C) loop
6944 if Chars (Discrim_Name) = Chars (D) then
6945 if Is_Entity_Name (Node (C))
6946 and then Entity (Node (C)) = Entity (Discrim)
6947 then
6948 -- D is renamed by Discrim, whose value is given in
6949 -- Assoc.
6950
6951 null;
6952
6953 else
6954 Assoc :=
6955 Make_Component_Association (Sloc (Typ),
6956 New_List
6957 (New_Occurrence_Of (D, Sloc (Typ))),
6958 Duplicate_Subexpr_No_Checks (Node (C)));
6959 end if;
6960 exit Find_Constraint;
6961 end if;
6962
6963 Next_Discriminant (D);
6964 Next_Elmt (C);
6965 end loop;
6966 end;
6967 end if;
6968 end if;
6969
6970 if No (Next (Assoc)) then
6971 Error_Msg_NE (" missing value for discriminant&",
6972 First (Governed_By), Discrim_Name);
6973 Report_Errors := True;
6974 return;
6975 end if;
6976
6977 Next (Assoc);
6978 end loop Find_Constraint;
6979
6980 Discrim_Value := Expression (Assoc);
6981
6982 if not Is_OK_Static_Expression (Discrim_Value) then
6983
6984 -- If the variant part is governed by a discriminant of the type
6985 -- this is an error. If the variant part and the discriminant are
6986 -- inherited from an ancestor this is legal (AI05-120) unless the
6987 -- components are being gathered for an aggregate, in which case
6988 -- the caller must check Report_Errors.
6989
6990 if Scope (Original_Record_Component
6991 ((Entity (First (Choices (Assoc)))))) = Typ
6992 then
6993 Error_Msg_FE
6994 ("value for discriminant & must be static!",
6995 Discrim_Value, Discrim);
6996 Why_Not_Static (Discrim_Value);
6997 end if;
6998
6999 Report_Errors := True;
7000 return;
7001 end if;
7002
7003 Search_For_Discriminant_Value : declare
7004 Low : Node_Id;
7005 High : Node_Id;
7006
7007 UI_High : Uint;
7008 UI_Low : Uint;
7009 UI_Discrim_Value : constant Uint := Expr_Value (Discrim_Value);
7010
7011 begin
7012 Find_Discrete_Value : while Present (Variant) loop
7013 Discrete_Choice := First (Discrete_Choices (Variant));
7014 while Present (Discrete_Choice) loop
7015 exit Find_Discrete_Value when
7016 Nkind (Discrete_Choice) = N_Others_Choice;
7017
7018 Get_Index_Bounds (Discrete_Choice, Low, High);
7019
7020 UI_Low := Expr_Value (Low);
7021 UI_High := Expr_Value (High);
7022
7023 exit Find_Discrete_Value when
7024 UI_Low <= UI_Discrim_Value
7025 and then
7026 UI_High >= UI_Discrim_Value;
7027
7028 Next (Discrete_Choice);
7029 end loop;
7030
7031 Next_Non_Pragma (Variant);
7032 end loop Find_Discrete_Value;
7033 end Search_For_Discriminant_Value;
7034
7035 if No (Variant) then
7036 Error_Msg_NE
7037 ("value of discriminant & is out of range", Discrim_Value, Discrim);
7038 Report_Errors := True;
7039 return;
7040 end if;
7041
7042 -- If we have found the corresponding choice, recursively add its
7043 -- components to the Into list.
7044
7045 Gather_Components
7046 (Empty, Component_List (Variant), Governed_By, Into, Report_Errors);
7047 end Gather_Components;
7048
7049 ------------------------
7050 -- Get_Actual_Subtype --
7051 ------------------------
7052
7053 function Get_Actual_Subtype (N : Node_Id) return Entity_Id is
7054 Typ : constant Entity_Id := Etype (N);
7055 Utyp : Entity_Id := Underlying_Type (Typ);
7056 Decl : Node_Id;
7057 Atyp : Entity_Id;
7058
7059 begin
7060 if No (Utyp) then
7061 Utyp := Typ;
7062 end if;
7063
7064 -- If what we have is an identifier that references a subprogram
7065 -- formal, or a variable or constant object, then we get the actual
7066 -- subtype from the referenced entity if one has been built.
7067
7068 if Nkind (N) = N_Identifier
7069 and then
7070 (Is_Formal (Entity (N))
7071 or else Ekind (Entity (N)) = E_Constant
7072 or else Ekind (Entity (N)) = E_Variable)
7073 and then Present (Actual_Subtype (Entity (N)))
7074 then
7075 return Actual_Subtype (Entity (N));
7076
7077 -- Actual subtype of unchecked union is always itself. We never need
7078 -- the "real" actual subtype. If we did, we couldn't get it anyway
7079 -- because the discriminant is not available. The restrictions on
7080 -- Unchecked_Union are designed to make sure that this is OK.
7081
7082 elsif Is_Unchecked_Union (Base_Type (Utyp)) then
7083 return Typ;
7084
7085 -- Here for the unconstrained case, we must find actual subtype
7086 -- No actual subtype is available, so we must build it on the fly.
7087
7088 -- Checking the type, not the underlying type, for constrainedness
7089 -- seems to be necessary. Maybe all the tests should be on the type???
7090
7091 elsif (not Is_Constrained (Typ))
7092 and then (Is_Array_Type (Utyp)
7093 or else (Is_Record_Type (Utyp)
7094 and then Has_Discriminants (Utyp)))
7095 and then not Has_Unknown_Discriminants (Utyp)
7096 and then not (Ekind (Utyp) = E_String_Literal_Subtype)
7097 then
7098 -- Nothing to do if in spec expression (why not???)
7099
7100 if In_Spec_Expression then
7101 return Typ;
7102
7103 elsif Is_Private_Type (Typ) and then not Has_Discriminants (Typ) then
7104
7105 -- If the type has no discriminants, there is no subtype to
7106 -- build, even if the underlying type is discriminated.
7107
7108 return Typ;
7109
7110 -- Else build the actual subtype
7111
7112 else
7113 Decl := Build_Actual_Subtype (Typ, N);
7114 Atyp := Defining_Identifier (Decl);
7115
7116 -- If Build_Actual_Subtype generated a new declaration then use it
7117
7118 if Atyp /= Typ then
7119
7120 -- The actual subtype is an Itype, so analyze the declaration,
7121 -- but do not attach it to the tree, to get the type defined.
7122
7123 Set_Parent (Decl, N);
7124 Set_Is_Itype (Atyp);
7125 Analyze (Decl, Suppress => All_Checks);
7126 Set_Associated_Node_For_Itype (Atyp, N);
7127 Set_Has_Delayed_Freeze (Atyp, False);
7128
7129 -- We need to freeze the actual subtype immediately. This is
7130 -- needed, because otherwise this Itype will not get frozen
7131 -- at all, and it is always safe to freeze on creation because
7132 -- any associated types must be frozen at this point.
7133
7134 Freeze_Itype (Atyp, N);
7135 return Atyp;
7136
7137 -- Otherwise we did not build a declaration, so return original
7138
7139 else
7140 return Typ;
7141 end if;
7142 end if;
7143
7144 -- For all remaining cases, the actual subtype is the same as
7145 -- the nominal type.
7146
7147 else
7148 return Typ;
7149 end if;
7150 end Get_Actual_Subtype;
7151
7152 -------------------------------------
7153 -- Get_Actual_Subtype_If_Available --
7154 -------------------------------------
7155
7156 function Get_Actual_Subtype_If_Available (N : Node_Id) return Entity_Id is
7157 Typ : constant Entity_Id := Etype (N);
7158
7159 begin
7160 -- If what we have is an identifier that references a subprogram
7161 -- formal, or a variable or constant object, then we get the actual
7162 -- subtype from the referenced entity if one has been built.
7163
7164 if Nkind (N) = N_Identifier
7165 and then
7166 (Is_Formal (Entity (N))
7167 or else Ekind (Entity (N)) = E_Constant
7168 or else Ekind (Entity (N)) = E_Variable)
7169 and then Present (Actual_Subtype (Entity (N)))
7170 then
7171 return Actual_Subtype (Entity (N));
7172
7173 -- Otherwise the Etype of N is returned unchanged
7174
7175 else
7176 return Typ;
7177 end if;
7178 end Get_Actual_Subtype_If_Available;
7179
7180 ------------------------
7181 -- Get_Body_From_Stub --
7182 ------------------------
7183
7184 function Get_Body_From_Stub (N : Node_Id) return Node_Id is
7185 begin
7186 return Proper_Body (Unit (Library_Unit (N)));
7187 end Get_Body_From_Stub;
7188
7189 ---------------------
7190 -- Get_Cursor_Type --
7191 ---------------------
7192
7193 function Get_Cursor_Type
7194 (Aspect : Node_Id;
7195 Typ : Entity_Id) return Entity_Id
7196 is
7197 Assoc : Node_Id;
7198 Func : Entity_Id;
7199 First_Op : Entity_Id;
7200 Cursor : Entity_Id;
7201
7202 begin
7203 -- If error already detected, return
7204
7205 if Error_Posted (Aspect) then
7206 return Any_Type;
7207 end if;
7208
7209 -- The cursor type for an Iterable aspect is the return type of a
7210 -- non-overloaded First primitive operation. Locate association for
7211 -- First.
7212
7213 Assoc := First (Component_Associations (Expression (Aspect)));
7214 First_Op := Any_Id;
7215 while Present (Assoc) loop
7216 if Chars (First (Choices (Assoc))) = Name_First then
7217 First_Op := Expression (Assoc);
7218 exit;
7219 end if;
7220
7221 Next (Assoc);
7222 end loop;
7223
7224 if First_Op = Any_Id then
7225 Error_Msg_N ("aspect Iterable must specify First operation", Aspect);
7226 return Any_Type;
7227 end if;
7228
7229 Cursor := Any_Type;
7230
7231 -- Locate function with desired name and profile in scope of type
7232
7233 Func := First_Entity (Scope (Typ));
7234 while Present (Func) loop
7235 if Chars (Func) = Chars (First_Op)
7236 and then Ekind (Func) = E_Function
7237 and then Present (First_Formal (Func))
7238 and then Etype (First_Formal (Func)) = Typ
7239 and then No (Next_Formal (First_Formal (Func)))
7240 then
7241 if Cursor /= Any_Type then
7242 Error_Msg_N
7243 ("Operation First for iterable type must be unique", Aspect);
7244 return Any_Type;
7245 else
7246 Cursor := Etype (Func);
7247 end if;
7248 end if;
7249
7250 Next_Entity (Func);
7251 end loop;
7252
7253 -- If not found, no way to resolve remaining primitives.
7254
7255 if Cursor = Any_Type then
7256 Error_Msg_N
7257 ("No legal primitive operation First for Iterable type", Aspect);
7258 end if;
7259
7260 return Cursor;
7261 end Get_Cursor_Type;
7262
7263 -------------------------------
7264 -- Get_Default_External_Name --
7265 -------------------------------
7266
7267 function Get_Default_External_Name (E : Node_Or_Entity_Id) return Node_Id is
7268 begin
7269 Get_Decoded_Name_String (Chars (E));
7270
7271 if Opt.External_Name_Imp_Casing = Uppercase then
7272 Set_Casing (All_Upper_Case);
7273 else
7274 Set_Casing (All_Lower_Case);
7275 end if;
7276
7277 return
7278 Make_String_Literal (Sloc (E),
7279 Strval => String_From_Name_Buffer);
7280 end Get_Default_External_Name;
7281
7282 --------------------------
7283 -- Get_Enclosing_Object --
7284 --------------------------
7285
7286 function Get_Enclosing_Object (N : Node_Id) return Entity_Id is
7287 begin
7288 if Is_Entity_Name (N) then
7289 return Entity (N);
7290 else
7291 case Nkind (N) is
7292 when N_Indexed_Component |
7293 N_Slice |
7294 N_Selected_Component =>
7295
7296 -- If not generating code, a dereference may be left implicit.
7297 -- In thoses cases, return Empty.
7298
7299 if Is_Access_Type (Etype (Prefix (N))) then
7300 return Empty;
7301 else
7302 return Get_Enclosing_Object (Prefix (N));
7303 end if;
7304
7305 when N_Type_Conversion =>
7306 return Get_Enclosing_Object (Expression (N));
7307
7308 when others =>
7309 return Empty;
7310 end case;
7311 end if;
7312 end Get_Enclosing_Object;
7313
7314 ---------------------------
7315 -- Get_Enum_Lit_From_Pos --
7316 ---------------------------
7317
7318 function Get_Enum_Lit_From_Pos
7319 (T : Entity_Id;
7320 Pos : Uint;
7321 Loc : Source_Ptr) return Node_Id
7322 is
7323 Btyp : Entity_Id := Base_Type (T);
7324 Lit : Node_Id;
7325
7326 begin
7327 -- In the case where the literal is of type Character, Wide_Character
7328 -- or Wide_Wide_Character or of a type derived from them, there needs
7329 -- to be some special handling since there is no explicit chain of
7330 -- literals to search. Instead, an N_Character_Literal node is created
7331 -- with the appropriate Char_Code and Chars fields.
7332
7333 if Is_Standard_Character_Type (T) then
7334 Set_Character_Literal_Name (UI_To_CC (Pos));
7335 return
7336 Make_Character_Literal (Loc,
7337 Chars => Name_Find,
7338 Char_Literal_Value => Pos);
7339
7340 -- For all other cases, we have a complete table of literals, and
7341 -- we simply iterate through the chain of literal until the one
7342 -- with the desired position value is found.
7343
7344 else
7345 if Is_Private_Type (Btyp) and then Present (Full_View (Btyp)) then
7346 Btyp := Full_View (Btyp);
7347 end if;
7348
7349 Lit := First_Literal (Btyp);
7350 for J in 1 .. UI_To_Int (Pos) loop
7351 Next_Literal (Lit);
7352 end loop;
7353
7354 return New_Occurrence_Of (Lit, Loc);
7355 end if;
7356 end Get_Enum_Lit_From_Pos;
7357
7358 ------------------------
7359 -- Get_Generic_Entity --
7360 ------------------------
7361
7362 function Get_Generic_Entity (N : Node_Id) return Entity_Id is
7363 Ent : constant Entity_Id := Entity (Name (N));
7364 begin
7365 if Present (Renamed_Object (Ent)) then
7366 return Renamed_Object (Ent);
7367 else
7368 return Ent;
7369 end if;
7370 end Get_Generic_Entity;
7371
7372 -------------------------------------
7373 -- Get_Incomplete_View_Of_Ancestor --
7374 -------------------------------------
7375
7376 function Get_Incomplete_View_Of_Ancestor (E : Entity_Id) return Entity_Id is
7377 Cur_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
7378 Par_Scope : Entity_Id;
7379 Par_Type : Entity_Id;
7380
7381 begin
7382 -- The incomplete view of an ancestor is only relevant for private
7383 -- derived types in child units.
7384
7385 if not Is_Derived_Type (E)
7386 or else not Is_Child_Unit (Cur_Unit)
7387 then
7388 return Empty;
7389
7390 else
7391 Par_Scope := Scope (Cur_Unit);
7392 if No (Par_Scope) then
7393 return Empty;
7394 end if;
7395
7396 Par_Type := Etype (Base_Type (E));
7397
7398 -- Traverse list of ancestor types until we find one declared in
7399 -- a parent or grandparent unit (two levels seem sufficient).
7400
7401 while Present (Par_Type) loop
7402 if Scope (Par_Type) = Par_Scope
7403 or else Scope (Par_Type) = Scope (Par_Scope)
7404 then
7405 return Par_Type;
7406
7407 elsif not Is_Derived_Type (Par_Type) then
7408 return Empty;
7409
7410 else
7411 Par_Type := Etype (Base_Type (Par_Type));
7412 end if;
7413 end loop;
7414
7415 -- If none found, there is no relevant ancestor type.
7416
7417 return Empty;
7418 end if;
7419 end Get_Incomplete_View_Of_Ancestor;
7420
7421 ----------------------
7422 -- Get_Index_Bounds --
7423 ----------------------
7424
7425 procedure Get_Index_Bounds (N : Node_Id; L, H : out Node_Id) is
7426 Kind : constant Node_Kind := Nkind (N);
7427 R : Node_Id;
7428
7429 begin
7430 if Kind = N_Range then
7431 L := Low_Bound (N);
7432 H := High_Bound (N);
7433
7434 elsif Kind = N_Subtype_Indication then
7435 R := Range_Expression (Constraint (N));
7436
7437 if R = Error then
7438 L := Error;
7439 H := Error;
7440 return;
7441
7442 else
7443 L := Low_Bound (Range_Expression (Constraint (N)));
7444 H := High_Bound (Range_Expression (Constraint (N)));
7445 end if;
7446
7447 elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
7448 if Error_Posted (Scalar_Range (Entity (N))) then
7449 L := Error;
7450 H := Error;
7451
7452 elsif Nkind (Scalar_Range (Entity (N))) = N_Subtype_Indication then
7453 Get_Index_Bounds (Scalar_Range (Entity (N)), L, H);
7454
7455 else
7456 L := Low_Bound (Scalar_Range (Entity (N)));
7457 H := High_Bound (Scalar_Range (Entity (N)));
7458 end if;
7459
7460 else
7461 -- N is an expression, indicating a range with one value
7462
7463 L := N;
7464 H := N;
7465 end if;
7466 end Get_Index_Bounds;
7467
7468 ---------------------------------
7469 -- Get_Iterable_Type_Primitive --
7470 ---------------------------------
7471
7472 function Get_Iterable_Type_Primitive
7473 (Typ : Entity_Id;
7474 Nam : Name_Id) return Entity_Id
7475 is
7476 Funcs : constant Node_Id := Find_Value_Of_Aspect (Typ, Aspect_Iterable);
7477 Assoc : Node_Id;
7478
7479 begin
7480 if No (Funcs) then
7481 return Empty;
7482
7483 else
7484 Assoc := First (Component_Associations (Funcs));
7485 while Present (Assoc) loop
7486 if Chars (First (Choices (Assoc))) = Nam then
7487 return Entity (Expression (Assoc));
7488 end if;
7489
7490 Assoc := Next (Assoc);
7491 end loop;
7492
7493 return Empty;
7494 end if;
7495 end Get_Iterable_Type_Primitive;
7496
7497 ----------------------------------
7498 -- Get_Library_Unit_Name_string --
7499 ----------------------------------
7500
7501 procedure Get_Library_Unit_Name_String (Decl_Node : Node_Id) is
7502 Unit_Name_Id : constant Unit_Name_Type := Get_Unit_Name (Decl_Node);
7503
7504 begin
7505 Get_Unit_Name_String (Unit_Name_Id);
7506
7507 -- Remove seven last character (" (spec)" or " (body)")
7508
7509 Name_Len := Name_Len - 7;
7510 pragma Assert (Name_Buffer (Name_Len + 1) = ' ');
7511 end Get_Library_Unit_Name_String;
7512
7513 ------------------------
7514 -- Get_Name_Entity_Id --
7515 ------------------------
7516
7517 function Get_Name_Entity_Id (Id : Name_Id) return Entity_Id is
7518 begin
7519 return Entity_Id (Get_Name_Table_Int (Id));
7520 end Get_Name_Entity_Id;
7521
7522 ------------------------------
7523 -- Get_Name_From_CTC_Pragma --
7524 ------------------------------
7525
7526 function Get_Name_From_CTC_Pragma (N : Node_Id) return String_Id is
7527 Arg : constant Node_Id :=
7528 Get_Pragma_Arg (First (Pragma_Argument_Associations (N)));
7529 begin
7530 return Strval (Expr_Value_S (Arg));
7531 end Get_Name_From_CTC_Pragma;
7532
7533 -----------------------
7534 -- Get_Parent_Entity --
7535 -----------------------
7536
7537 function Get_Parent_Entity (Unit : Node_Id) return Entity_Id is
7538 begin
7539 if Nkind (Unit) = N_Package_Body
7540 and then Nkind (Original_Node (Unit)) = N_Package_Instantiation
7541 then
7542 return Defining_Entity
7543 (Specification (Instance_Spec (Original_Node (Unit))));
7544 elsif Nkind (Unit) = N_Package_Instantiation then
7545 return Defining_Entity (Specification (Instance_Spec (Unit)));
7546 else
7547 return Defining_Entity (Unit);
7548 end if;
7549 end Get_Parent_Entity;
7550 -------------------
7551 -- Get_Pragma_Id --
7552 -------------------
7553
7554 function Get_Pragma_Id (N : Node_Id) return Pragma_Id is
7555 begin
7556 return Get_Pragma_Id (Pragma_Name (N));
7557 end Get_Pragma_Id;
7558
7559 -----------------------
7560 -- Get_Reason_String --
7561 -----------------------
7562
7563 procedure Get_Reason_String (N : Node_Id) is
7564 begin
7565 if Nkind (N) = N_String_Literal then
7566 Store_String_Chars (Strval (N));
7567
7568 elsif Nkind (N) = N_Op_Concat then
7569 Get_Reason_String (Left_Opnd (N));
7570 Get_Reason_String (Right_Opnd (N));
7571
7572 -- If not of required form, error
7573
7574 else
7575 Error_Msg_N
7576 ("Reason for pragma Warnings has wrong form", N);
7577 Error_Msg_N
7578 ("\must be string literal or concatenation of string literals", N);
7579 return;
7580 end if;
7581 end Get_Reason_String;
7582
7583 ---------------------------
7584 -- Get_Referenced_Object --
7585 ---------------------------
7586
7587 function Get_Referenced_Object (N : Node_Id) return Node_Id is
7588 R : Node_Id;
7589
7590 begin
7591 R := N;
7592 while Is_Entity_Name (R)
7593 and then Present (Renamed_Object (Entity (R)))
7594 loop
7595 R := Renamed_Object (Entity (R));
7596 end loop;
7597
7598 return R;
7599 end Get_Referenced_Object;
7600
7601 ------------------------
7602 -- Get_Renamed_Entity --
7603 ------------------------
7604
7605 function Get_Renamed_Entity (E : Entity_Id) return Entity_Id is
7606 R : Entity_Id;
7607
7608 begin
7609 R := E;
7610 while Present (Renamed_Entity (R)) loop
7611 R := Renamed_Entity (R);
7612 end loop;
7613
7614 return R;
7615 end Get_Renamed_Entity;
7616
7617 -------------------------
7618 -- Get_Subprogram_Body --
7619 -------------------------
7620
7621 function Get_Subprogram_Body (E : Entity_Id) return Node_Id is
7622 Decl : Node_Id;
7623
7624 begin
7625 Decl := Unit_Declaration_Node (E);
7626
7627 if Nkind (Decl) = N_Subprogram_Body then
7628 return Decl;
7629
7630 -- The below comment is bad, because it is possible for
7631 -- Nkind (Decl) to be an N_Subprogram_Body_Stub ???
7632
7633 else -- Nkind (Decl) = N_Subprogram_Declaration
7634
7635 if Present (Corresponding_Body (Decl)) then
7636 return Unit_Declaration_Node (Corresponding_Body (Decl));
7637
7638 -- Imported subprogram case
7639
7640 else
7641 return Empty;
7642 end if;
7643 end if;
7644 end Get_Subprogram_Body;
7645
7646 ---------------------------
7647 -- Get_Subprogram_Entity --
7648 ---------------------------
7649
7650 function Get_Subprogram_Entity (Nod : Node_Id) return Entity_Id is
7651 Subp : Node_Id;
7652 Subp_Id : Entity_Id;
7653
7654 begin
7655 if Nkind (Nod) = N_Accept_Statement then
7656 Subp := Entry_Direct_Name (Nod);
7657
7658 elsif Nkind (Nod) = N_Slice then
7659 Subp := Prefix (Nod);
7660
7661 else
7662 Subp := Name (Nod);
7663 end if;
7664
7665 -- Strip the subprogram call
7666
7667 loop
7668 if Nkind_In (Subp, N_Explicit_Dereference,
7669 N_Indexed_Component,
7670 N_Selected_Component)
7671 then
7672 Subp := Prefix (Subp);
7673
7674 elsif Nkind_In (Subp, N_Type_Conversion,
7675 N_Unchecked_Type_Conversion)
7676 then
7677 Subp := Expression (Subp);
7678
7679 else
7680 exit;
7681 end if;
7682 end loop;
7683
7684 -- Extract the entity of the subprogram call
7685
7686 if Is_Entity_Name (Subp) then
7687 Subp_Id := Entity (Subp);
7688
7689 if Ekind (Subp_Id) = E_Access_Subprogram_Type then
7690 Subp_Id := Directly_Designated_Type (Subp_Id);
7691 end if;
7692
7693 if Is_Subprogram (Subp_Id) then
7694 return Subp_Id;
7695 else
7696 return Empty;
7697 end if;
7698
7699 -- The search did not find a construct that denotes a subprogram
7700
7701 else
7702 return Empty;
7703 end if;
7704 end Get_Subprogram_Entity;
7705
7706 -----------------------------
7707 -- Get_Task_Body_Procedure --
7708 -----------------------------
7709
7710 function Get_Task_Body_Procedure (E : Entity_Id) return Node_Id is
7711 begin
7712 -- Note: A task type may be the completion of a private type with
7713 -- discriminants. When performing elaboration checks on a task
7714 -- declaration, the current view of the type may be the private one,
7715 -- and the procedure that holds the body of the task is held in its
7716 -- underlying type.
7717
7718 -- This is an odd function, why not have Task_Body_Procedure do
7719 -- the following digging???
7720
7721 return Task_Body_Procedure (Underlying_Type (Root_Type (E)));
7722 end Get_Task_Body_Procedure;
7723
7724 -----------------------
7725 -- Has_Access_Values --
7726 -----------------------
7727
7728 function Has_Access_Values (T : Entity_Id) return Boolean is
7729 Typ : constant Entity_Id := Underlying_Type (T);
7730
7731 begin
7732 -- Case of a private type which is not completed yet. This can only
7733 -- happen in the case of a generic format type appearing directly, or
7734 -- as a component of the type to which this function is being applied
7735 -- at the top level. Return False in this case, since we certainly do
7736 -- not know that the type contains access types.
7737
7738 if No (Typ) then
7739 return False;
7740
7741 elsif Is_Access_Type (Typ) then
7742 return True;
7743
7744 elsif Is_Array_Type (Typ) then
7745 return Has_Access_Values (Component_Type (Typ));
7746
7747 elsif Is_Record_Type (Typ) then
7748 declare
7749 Comp : Entity_Id;
7750
7751 begin
7752 -- Loop to Check components
7753
7754 Comp := First_Component_Or_Discriminant (Typ);
7755 while Present (Comp) loop
7756
7757 -- Check for access component, tag field does not count, even
7758 -- though it is implemented internally using an access type.
7759
7760 if Has_Access_Values (Etype (Comp))
7761 and then Chars (Comp) /= Name_uTag
7762 then
7763 return True;
7764 end if;
7765
7766 Next_Component_Or_Discriminant (Comp);
7767 end loop;
7768 end;
7769
7770 return False;
7771
7772 else
7773 return False;
7774 end if;
7775 end Has_Access_Values;
7776
7777 ------------------------------
7778 -- Has_Compatible_Alignment --
7779 ------------------------------
7780
7781 function Has_Compatible_Alignment
7782 (Obj : Entity_Id;
7783 Expr : Node_Id) return Alignment_Result
7784 is
7785 function Has_Compatible_Alignment_Internal
7786 (Obj : Entity_Id;
7787 Expr : Node_Id;
7788 Default : Alignment_Result) return Alignment_Result;
7789 -- This is the internal recursive function that actually does the work.
7790 -- There is one additional parameter, which says what the result should
7791 -- be if no alignment information is found, and there is no definite
7792 -- indication of compatible alignments. At the outer level, this is set
7793 -- to Unknown, but for internal recursive calls in the case where types
7794 -- are known to be correct, it is set to Known_Compatible.
7795
7796 ---------------------------------------
7797 -- Has_Compatible_Alignment_Internal --
7798 ---------------------------------------
7799
7800 function Has_Compatible_Alignment_Internal
7801 (Obj : Entity_Id;
7802 Expr : Node_Id;
7803 Default : Alignment_Result) return Alignment_Result
7804 is
7805 Result : Alignment_Result := Known_Compatible;
7806 -- Holds the current status of the result. Note that once a value of
7807 -- Known_Incompatible is set, it is sticky and does not get changed
7808 -- to Unknown (the value in Result only gets worse as we go along,
7809 -- never better).
7810
7811 Offs : Uint := No_Uint;
7812 -- Set to a factor of the offset from the base object when Expr is a
7813 -- selected or indexed component, based on Component_Bit_Offset and
7814 -- Component_Size respectively. A negative value is used to represent
7815 -- a value which is not known at compile time.
7816
7817 procedure Check_Prefix;
7818 -- Checks the prefix recursively in the case where the expression
7819 -- is an indexed or selected component.
7820
7821 procedure Set_Result (R : Alignment_Result);
7822 -- If R represents a worse outcome (unknown instead of known
7823 -- compatible, or known incompatible), then set Result to R.
7824
7825 ------------------
7826 -- Check_Prefix --
7827 ------------------
7828
7829 procedure Check_Prefix is
7830 begin
7831 -- The subtlety here is that in doing a recursive call to check
7832 -- the prefix, we have to decide what to do in the case where we
7833 -- don't find any specific indication of an alignment problem.
7834
7835 -- At the outer level, we normally set Unknown as the result in
7836 -- this case, since we can only set Known_Compatible if we really
7837 -- know that the alignment value is OK, but for the recursive
7838 -- call, in the case where the types match, and we have not
7839 -- specified a peculiar alignment for the object, we are only
7840 -- concerned about suspicious rep clauses, the default case does
7841 -- not affect us, since the compiler will, in the absence of such
7842 -- rep clauses, ensure that the alignment is correct.
7843
7844 if Default = Known_Compatible
7845 or else
7846 (Etype (Obj) = Etype (Expr)
7847 and then (Unknown_Alignment (Obj)
7848 or else
7849 Alignment (Obj) = Alignment (Etype (Obj))))
7850 then
7851 Set_Result
7852 (Has_Compatible_Alignment_Internal
7853 (Obj, Prefix (Expr), Known_Compatible));
7854
7855 -- In all other cases, we need a full check on the prefix
7856
7857 else
7858 Set_Result
7859 (Has_Compatible_Alignment_Internal
7860 (Obj, Prefix (Expr), Unknown));
7861 end if;
7862 end Check_Prefix;
7863
7864 ----------------
7865 -- Set_Result --
7866 ----------------
7867
7868 procedure Set_Result (R : Alignment_Result) is
7869 begin
7870 if R > Result then
7871 Result := R;
7872 end if;
7873 end Set_Result;
7874
7875 -- Start of processing for Has_Compatible_Alignment_Internal
7876
7877 begin
7878 -- If Expr is a selected component, we must make sure there is no
7879 -- potentially troublesome component clause, and that the record is
7880 -- not packed.
7881
7882 if Nkind (Expr) = N_Selected_Component then
7883
7884 -- Packed record always generate unknown alignment
7885
7886 if Is_Packed (Etype (Prefix (Expr))) then
7887 Set_Result (Unknown);
7888 end if;
7889
7890 -- Check prefix and component offset
7891
7892 Check_Prefix;
7893 Offs := Component_Bit_Offset (Entity (Selector_Name (Expr)));
7894
7895 -- If Expr is an indexed component, we must make sure there is no
7896 -- potentially troublesome Component_Size clause and that the array
7897 -- is not bit-packed.
7898
7899 elsif Nkind (Expr) = N_Indexed_Component then
7900 declare
7901 Typ : constant Entity_Id := Etype (Prefix (Expr));
7902 Ind : constant Node_Id := First_Index (Typ);
7903
7904 begin
7905 -- Bit packed array always generates unknown alignment
7906
7907 if Is_Bit_Packed_Array (Typ) then
7908 Set_Result (Unknown);
7909 end if;
7910
7911 -- Check prefix and component offset
7912
7913 Check_Prefix;
7914 Offs := Component_Size (Typ);
7915
7916 -- Small optimization: compute the full offset when possible
7917
7918 if Offs /= No_Uint
7919 and then Offs > Uint_0
7920 and then Present (Ind)
7921 and then Nkind (Ind) = N_Range
7922 and then Compile_Time_Known_Value (Low_Bound (Ind))
7923 and then Compile_Time_Known_Value (First (Expressions (Expr)))
7924 then
7925 Offs := Offs * (Expr_Value (First (Expressions (Expr)))
7926 - Expr_Value (Low_Bound ((Ind))));
7927 end if;
7928 end;
7929 end if;
7930
7931 -- If we have a null offset, the result is entirely determined by
7932 -- the base object and has already been computed recursively.
7933
7934 if Offs = Uint_0 then
7935 null;
7936
7937 -- Case where we know the alignment of the object
7938
7939 elsif Known_Alignment (Obj) then
7940 declare
7941 ObjA : constant Uint := Alignment (Obj);
7942 ExpA : Uint := No_Uint;
7943 SizA : Uint := No_Uint;
7944
7945 begin
7946 -- If alignment of Obj is 1, then we are always OK
7947
7948 if ObjA = 1 then
7949 Set_Result (Known_Compatible);
7950
7951 -- Alignment of Obj is greater than 1, so we need to check
7952
7953 else
7954 -- If we have an offset, see if it is compatible
7955
7956 if Offs /= No_Uint and Offs > Uint_0 then
7957 if Offs mod (System_Storage_Unit * ObjA) /= 0 then
7958 Set_Result (Known_Incompatible);
7959 end if;
7960
7961 -- See if Expr is an object with known alignment
7962
7963 elsif Is_Entity_Name (Expr)
7964 and then Known_Alignment (Entity (Expr))
7965 then
7966 ExpA := Alignment (Entity (Expr));
7967
7968 -- Otherwise, we can use the alignment of the type of
7969 -- Expr given that we already checked for
7970 -- discombobulating rep clauses for the cases of indexed
7971 -- and selected components above.
7972
7973 elsif Known_Alignment (Etype (Expr)) then
7974 ExpA := Alignment (Etype (Expr));
7975
7976 -- Otherwise the alignment is unknown
7977
7978 else
7979 Set_Result (Default);
7980 end if;
7981
7982 -- If we got an alignment, see if it is acceptable
7983
7984 if ExpA /= No_Uint and then ExpA < ObjA then
7985 Set_Result (Known_Incompatible);
7986 end if;
7987
7988 -- If Expr is not a piece of a larger object, see if size
7989 -- is given. If so, check that it is not too small for the
7990 -- required alignment.
7991
7992 if Offs /= No_Uint then
7993 null;
7994
7995 -- See if Expr is an object with known size
7996
7997 elsif Is_Entity_Name (Expr)
7998 and then Known_Static_Esize (Entity (Expr))
7999 then
8000 SizA := Esize (Entity (Expr));
8001
8002 -- Otherwise, we check the object size of the Expr type
8003
8004 elsif Known_Static_Esize (Etype (Expr)) then
8005 SizA := Esize (Etype (Expr));
8006 end if;
8007
8008 -- If we got a size, see if it is a multiple of the Obj
8009 -- alignment, if not, then the alignment cannot be
8010 -- acceptable, since the size is always a multiple of the
8011 -- alignment.
8012
8013 if SizA /= No_Uint then
8014 if SizA mod (ObjA * Ttypes.System_Storage_Unit) /= 0 then
8015 Set_Result (Known_Incompatible);
8016 end if;
8017 end if;
8018 end if;
8019 end;
8020
8021 -- If we do not know required alignment, any non-zero offset is a
8022 -- potential problem (but certainly may be OK, so result is unknown).
8023
8024 elsif Offs /= No_Uint then
8025 Set_Result (Unknown);
8026
8027 -- If we can't find the result by direct comparison of alignment
8028 -- values, then there is still one case that we can determine known
8029 -- result, and that is when we can determine that the types are the
8030 -- same, and no alignments are specified. Then we known that the
8031 -- alignments are compatible, even if we don't know the alignment
8032 -- value in the front end.
8033
8034 elsif Etype (Obj) = Etype (Expr) then
8035
8036 -- Types are the same, but we have to check for possible size
8037 -- and alignments on the Expr object that may make the alignment
8038 -- different, even though the types are the same.
8039
8040 if Is_Entity_Name (Expr) then
8041
8042 -- First check alignment of the Expr object. Any alignment less
8043 -- than Maximum_Alignment is worrisome since this is the case
8044 -- where we do not know the alignment of Obj.
8045
8046 if Known_Alignment (Entity (Expr))
8047 and then UI_To_Int (Alignment (Entity (Expr))) <
8048 Ttypes.Maximum_Alignment
8049 then
8050 Set_Result (Unknown);
8051
8052 -- Now check size of Expr object. Any size that is not an
8053 -- even multiple of Maximum_Alignment is also worrisome
8054 -- since it may cause the alignment of the object to be less
8055 -- than the alignment of the type.
8056
8057 elsif Known_Static_Esize (Entity (Expr))
8058 and then
8059 (UI_To_Int (Esize (Entity (Expr))) mod
8060 (Ttypes.Maximum_Alignment * Ttypes.System_Storage_Unit))
8061 /= 0
8062 then
8063 Set_Result (Unknown);
8064
8065 -- Otherwise same type is decisive
8066
8067 else
8068 Set_Result (Known_Compatible);
8069 end if;
8070 end if;
8071
8072 -- Another case to deal with is when there is an explicit size or
8073 -- alignment clause when the types are not the same. If so, then the
8074 -- result is Unknown. We don't need to do this test if the Default is
8075 -- Unknown, since that result will be set in any case.
8076
8077 elsif Default /= Unknown
8078 and then (Has_Size_Clause (Etype (Expr))
8079 or else
8080 Has_Alignment_Clause (Etype (Expr)))
8081 then
8082 Set_Result (Unknown);
8083
8084 -- If no indication found, set default
8085
8086 else
8087 Set_Result (Default);
8088 end if;
8089
8090 -- Return worst result found
8091
8092 return Result;
8093 end Has_Compatible_Alignment_Internal;
8094
8095 -- Start of processing for Has_Compatible_Alignment
8096
8097 begin
8098 -- If Obj has no specified alignment, then set alignment from the type
8099 -- alignment. Perhaps we should always do this, but for sure we should
8100 -- do it when there is an address clause since we can do more if the
8101 -- alignment is known.
8102
8103 if Unknown_Alignment (Obj) then
8104 Set_Alignment (Obj, Alignment (Etype (Obj)));
8105 end if;
8106
8107 -- Now do the internal call that does all the work
8108
8109 return Has_Compatible_Alignment_Internal (Obj, Expr, Unknown);
8110 end Has_Compatible_Alignment;
8111
8112 ----------------------
8113 -- Has_Declarations --
8114 ----------------------
8115
8116 function Has_Declarations (N : Node_Id) return Boolean is
8117 begin
8118 return Nkind_In (Nkind (N), N_Accept_Statement,
8119 N_Block_Statement,
8120 N_Compilation_Unit_Aux,
8121 N_Entry_Body,
8122 N_Package_Body,
8123 N_Protected_Body,
8124 N_Subprogram_Body,
8125 N_Task_Body,
8126 N_Package_Specification);
8127 end Has_Declarations;
8128
8129 ---------------------------------
8130 -- Has_Defaulted_Discriminants --
8131 ---------------------------------
8132
8133 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean is
8134 begin
8135 return Has_Discriminants (Typ)
8136 and then Present (First_Discriminant (Typ))
8137 and then Present (Discriminant_Default_Value
8138 (First_Discriminant (Typ)));
8139 end Has_Defaulted_Discriminants;
8140
8141 -------------------
8142 -- Has_Denormals --
8143 -------------------
8144
8145 function Has_Denormals (E : Entity_Id) return Boolean is
8146 begin
8147 return Is_Floating_Point_Type (E) and then Denorm_On_Target;
8148 end Has_Denormals;
8149
8150 -------------------------------------------
8151 -- Has_Discriminant_Dependent_Constraint --
8152 -------------------------------------------
8153
8154 function Has_Discriminant_Dependent_Constraint
8155 (Comp : Entity_Id) return Boolean
8156 is
8157 Comp_Decl : constant Node_Id := Parent (Comp);
8158 Subt_Indic : Node_Id;
8159 Constr : Node_Id;
8160 Assn : Node_Id;
8161
8162 begin
8163 -- Discriminants can't depend on discriminants
8164
8165 if Ekind (Comp) = E_Discriminant then
8166 return False;
8167
8168 else
8169 Subt_Indic := Subtype_Indication (Component_Definition (Comp_Decl));
8170
8171 if Nkind (Subt_Indic) = N_Subtype_Indication then
8172 Constr := Constraint (Subt_Indic);
8173
8174 if Nkind (Constr) = N_Index_Or_Discriminant_Constraint then
8175 Assn := First (Constraints (Constr));
8176 while Present (Assn) loop
8177 case Nkind (Assn) is
8178 when N_Subtype_Indication |
8179 N_Range |
8180 N_Identifier
8181 =>
8182 if Depends_On_Discriminant (Assn) then
8183 return True;
8184 end if;
8185
8186 when N_Discriminant_Association =>
8187 if Depends_On_Discriminant (Expression (Assn)) then
8188 return True;
8189 end if;
8190
8191 when others =>
8192 null;
8193 end case;
8194
8195 Next (Assn);
8196 end loop;
8197 end if;
8198 end if;
8199 end if;
8200
8201 return False;
8202 end Has_Discriminant_Dependent_Constraint;
8203
8204 --------------------------
8205 -- Has_Enabled_Property --
8206 --------------------------
8207
8208 function Has_Enabled_Property
8209 (Item_Id : Entity_Id;
8210 Property : Name_Id) return Boolean
8211 is
8212 function State_Has_Enabled_Property return Boolean;
8213 -- Determine whether a state denoted by Item_Id has the property enabled
8214
8215 function Variable_Has_Enabled_Property return Boolean;
8216 -- Determine whether a variable denoted by Item_Id has the property
8217 -- enabled.
8218
8219 --------------------------------
8220 -- State_Has_Enabled_Property --
8221 --------------------------------
8222
8223 function State_Has_Enabled_Property return Boolean is
8224 Decl : constant Node_Id := Parent (Item_Id);
8225 Opt : Node_Id;
8226 Opt_Nam : Node_Id;
8227 Prop : Node_Id;
8228 Prop_Nam : Node_Id;
8229 Props : Node_Id;
8230
8231 begin
8232 -- The declaration of an external abstract state appears as an
8233 -- extension aggregate. If this is not the case, properties can never
8234 -- be set.
8235
8236 if Nkind (Decl) /= N_Extension_Aggregate then
8237 return False;
8238 end if;
8239
8240 -- When External appears as a simple option, it automatically enables
8241 -- all properties.
8242
8243 Opt := First (Expressions (Decl));
8244 while Present (Opt) loop
8245 if Nkind (Opt) = N_Identifier
8246 and then Chars (Opt) = Name_External
8247 then
8248 return True;
8249 end if;
8250
8251 Next (Opt);
8252 end loop;
8253
8254 -- When External specifies particular properties, inspect those and
8255 -- find the desired one (if any).
8256
8257 Opt := First (Component_Associations (Decl));
8258 while Present (Opt) loop
8259 Opt_Nam := First (Choices (Opt));
8260
8261 if Nkind (Opt_Nam) = N_Identifier
8262 and then Chars (Opt_Nam) = Name_External
8263 then
8264 Props := Expression (Opt);
8265
8266 -- Multiple properties appear as an aggregate
8267
8268 if Nkind (Props) = N_Aggregate then
8269
8270 -- Simple property form
8271
8272 Prop := First (Expressions (Props));
8273 while Present (Prop) loop
8274 if Chars (Prop) = Property then
8275 return True;
8276 end if;
8277
8278 Next (Prop);
8279 end loop;
8280
8281 -- Property with expression form
8282
8283 Prop := First (Component_Associations (Props));
8284 while Present (Prop) loop
8285 Prop_Nam := First (Choices (Prop));
8286
8287 -- The property can be represented in two ways:
8288 -- others => <value>
8289 -- <property> => <value>
8290
8291 if Nkind (Prop_Nam) = N_Others_Choice
8292 or else (Nkind (Prop_Nam) = N_Identifier
8293 and then Chars (Prop_Nam) = Property)
8294 then
8295 return Is_True (Expr_Value (Expression (Prop)));
8296 end if;
8297
8298 Next (Prop);
8299 end loop;
8300
8301 -- Single property
8302
8303 else
8304 return Chars (Props) = Property;
8305 end if;
8306 end if;
8307
8308 Next (Opt);
8309 end loop;
8310
8311 return False;
8312 end State_Has_Enabled_Property;
8313
8314 -----------------------------------
8315 -- Variable_Has_Enabled_Property --
8316 -----------------------------------
8317
8318 function Variable_Has_Enabled_Property return Boolean is
8319 function Is_Enabled (Prag : Node_Id) return Boolean;
8320 -- Determine whether property pragma Prag (if present) denotes an
8321 -- enabled property.
8322
8323 ----------------
8324 -- Is_Enabled --
8325 ----------------
8326
8327 function Is_Enabled (Prag : Node_Id) return Boolean is
8328 Arg2 : Node_Id;
8329
8330 begin
8331 if Present (Prag) then
8332 Arg2 := Next (First (Pragma_Argument_Associations (Prag)));
8333
8334 -- The pragma has an optional Boolean expression, the related
8335 -- property is enabled only when the expression evaluates to
8336 -- True.
8337
8338 if Present (Arg2) then
8339 return Is_True (Expr_Value (Get_Pragma_Arg (Arg2)));
8340
8341 -- Otherwise the lack of expression enables the property by
8342 -- default.
8343
8344 else
8345 return True;
8346 end if;
8347
8348 -- The property was never set in the first place
8349
8350 else
8351 return False;
8352 end if;
8353 end Is_Enabled;
8354
8355 -- Local variables
8356
8357 AR : constant Node_Id :=
8358 Get_Pragma (Item_Id, Pragma_Async_Readers);
8359 AW : constant Node_Id :=
8360 Get_Pragma (Item_Id, Pragma_Async_Writers);
8361 ER : constant Node_Id :=
8362 Get_Pragma (Item_Id, Pragma_Effective_Reads);
8363 EW : constant Node_Id :=
8364 Get_Pragma (Item_Id, Pragma_Effective_Writes);
8365
8366 -- Start of processing for Variable_Has_Enabled_Property
8367
8368 begin
8369 -- A non-effectively volatile object can never possess external
8370 -- properties.
8371
8372 if not Is_Effectively_Volatile (Item_Id) then
8373 return False;
8374
8375 -- External properties related to variables come in two flavors -
8376 -- explicit and implicit. The explicit case is characterized by the
8377 -- presence of a property pragma with an optional Boolean flag. The
8378 -- property is enabled when the flag evaluates to True or the flag is
8379 -- missing altogether.
8380
8381 elsif Property = Name_Async_Readers and then Is_Enabled (AR) then
8382 return True;
8383
8384 elsif Property = Name_Async_Writers and then Is_Enabled (AW) then
8385 return True;
8386
8387 elsif Property = Name_Effective_Reads and then Is_Enabled (ER) then
8388 return True;
8389
8390 elsif Property = Name_Effective_Writes and then Is_Enabled (EW) then
8391 return True;
8392
8393 -- The implicit case lacks all property pragmas
8394
8395 elsif No (AR) and then No (AW) and then No (ER) and then No (EW) then
8396 return True;
8397
8398 else
8399 return False;
8400 end if;
8401 end Variable_Has_Enabled_Property;
8402
8403 -- Start of processing for Has_Enabled_Property
8404
8405 begin
8406 -- Abstract states and variables have a flexible scheme of specifying
8407 -- external properties.
8408
8409 if Ekind (Item_Id) = E_Abstract_State then
8410 return State_Has_Enabled_Property;
8411
8412 elsif Ekind (Item_Id) = E_Variable then
8413 return Variable_Has_Enabled_Property;
8414
8415 -- Otherwise a property is enabled when the related item is effectively
8416 -- volatile.
8417
8418 else
8419 return Is_Effectively_Volatile (Item_Id);
8420 end if;
8421 end Has_Enabled_Property;
8422
8423 --------------------
8424 -- Has_Infinities --
8425 --------------------
8426
8427 function Has_Infinities (E : Entity_Id) return Boolean is
8428 begin
8429 return
8430 Is_Floating_Point_Type (E)
8431 and then Nkind (Scalar_Range (E)) = N_Range
8432 and then Includes_Infinities (Scalar_Range (E));
8433 end Has_Infinities;
8434
8435 --------------------
8436 -- Has_Interfaces --
8437 --------------------
8438
8439 function Has_Interfaces
8440 (T : Entity_Id;
8441 Use_Full_View : Boolean := True) return Boolean
8442 is
8443 Typ : Entity_Id := Base_Type (T);
8444
8445 begin
8446 -- Handle concurrent types
8447
8448 if Is_Concurrent_Type (Typ) then
8449 Typ := Corresponding_Record_Type (Typ);
8450 end if;
8451
8452 if not Present (Typ)
8453 or else not Is_Record_Type (Typ)
8454 or else not Is_Tagged_Type (Typ)
8455 then
8456 return False;
8457 end if;
8458
8459 -- Handle private types
8460
8461 if Use_Full_View and then Present (Full_View (Typ)) then
8462 Typ := Full_View (Typ);
8463 end if;
8464
8465 -- Handle concurrent record types
8466
8467 if Is_Concurrent_Record_Type (Typ)
8468 and then Is_Non_Empty_List (Abstract_Interface_List (Typ))
8469 then
8470 return True;
8471 end if;
8472
8473 loop
8474 if Is_Interface (Typ)
8475 or else
8476 (Is_Record_Type (Typ)
8477 and then Present (Interfaces (Typ))
8478 and then not Is_Empty_Elmt_List (Interfaces (Typ)))
8479 then
8480 return True;
8481 end if;
8482
8483 exit when Etype (Typ) = Typ
8484
8485 -- Handle private types
8486
8487 or else (Present (Full_View (Etype (Typ)))
8488 and then Full_View (Etype (Typ)) = Typ)
8489
8490 -- Protect frontend against wrong sources with cyclic derivations
8491
8492 or else Etype (Typ) = T;
8493
8494 -- Climb to the ancestor type handling private types
8495
8496 if Present (Full_View (Etype (Typ))) then
8497 Typ := Full_View (Etype (Typ));
8498 else
8499 Typ := Etype (Typ);
8500 end if;
8501 end loop;
8502
8503 return False;
8504 end Has_Interfaces;
8505
8506 ---------------------------------
8507 -- Has_No_Obvious_Side_Effects --
8508 ---------------------------------
8509
8510 function Has_No_Obvious_Side_Effects (N : Node_Id) return Boolean is
8511 begin
8512 -- For now, just handle literals, constants, and non-volatile
8513 -- variables and expressions combining these with operators or
8514 -- short circuit forms.
8515
8516 if Nkind (N) in N_Numeric_Or_String_Literal then
8517 return True;
8518
8519 elsif Nkind (N) = N_Character_Literal then
8520 return True;
8521
8522 elsif Nkind (N) in N_Unary_Op then
8523 return Has_No_Obvious_Side_Effects (Right_Opnd (N));
8524
8525 elsif Nkind (N) in N_Binary_Op or else Nkind (N) in N_Short_Circuit then
8526 return Has_No_Obvious_Side_Effects (Left_Opnd (N))
8527 and then
8528 Has_No_Obvious_Side_Effects (Right_Opnd (N));
8529
8530 elsif Nkind (N) = N_Expression_With_Actions
8531 and then Is_Empty_List (Actions (N))
8532 then
8533 return Has_No_Obvious_Side_Effects (Expression (N));
8534
8535 elsif Nkind (N) in N_Has_Entity then
8536 return Present (Entity (N))
8537 and then Ekind_In (Entity (N), E_Variable,
8538 E_Constant,
8539 E_Enumeration_Literal,
8540 E_In_Parameter,
8541 E_Out_Parameter,
8542 E_In_Out_Parameter)
8543 and then not Is_Volatile (Entity (N));
8544
8545 else
8546 return False;
8547 end if;
8548 end Has_No_Obvious_Side_Effects;
8549
8550 ------------------------
8551 -- Has_Null_Exclusion --
8552 ------------------------
8553
8554 function Has_Null_Exclusion (N : Node_Id) return Boolean is
8555 begin
8556 case Nkind (N) is
8557 when N_Access_Definition |
8558 N_Access_Function_Definition |
8559 N_Access_Procedure_Definition |
8560 N_Access_To_Object_Definition |
8561 N_Allocator |
8562 N_Derived_Type_Definition |
8563 N_Function_Specification |
8564 N_Subtype_Declaration =>
8565 return Null_Exclusion_Present (N);
8566
8567 when N_Component_Definition |
8568 N_Formal_Object_Declaration |
8569 N_Object_Renaming_Declaration =>
8570 if Present (Subtype_Mark (N)) then
8571 return Null_Exclusion_Present (N);
8572 else pragma Assert (Present (Access_Definition (N)));
8573 return Null_Exclusion_Present (Access_Definition (N));
8574 end if;
8575
8576 when N_Discriminant_Specification =>
8577 if Nkind (Discriminant_Type (N)) = N_Access_Definition then
8578 return Null_Exclusion_Present (Discriminant_Type (N));
8579 else
8580 return Null_Exclusion_Present (N);
8581 end if;
8582
8583 when N_Object_Declaration =>
8584 if Nkind (Object_Definition (N)) = N_Access_Definition then
8585 return Null_Exclusion_Present (Object_Definition (N));
8586 else
8587 return Null_Exclusion_Present (N);
8588 end if;
8589
8590 when N_Parameter_Specification =>
8591 if Nkind (Parameter_Type (N)) = N_Access_Definition then
8592 return Null_Exclusion_Present (Parameter_Type (N));
8593 else
8594 return Null_Exclusion_Present (N);
8595 end if;
8596
8597 when others =>
8598 return False;
8599
8600 end case;
8601 end Has_Null_Exclusion;
8602
8603 ------------------------
8604 -- Has_Null_Extension --
8605 ------------------------
8606
8607 function Has_Null_Extension (T : Entity_Id) return Boolean is
8608 B : constant Entity_Id := Base_Type (T);
8609 Comps : Node_Id;
8610 Ext : Node_Id;
8611
8612 begin
8613 if Nkind (Parent (B)) = N_Full_Type_Declaration
8614 and then Present (Record_Extension_Part (Type_Definition (Parent (B))))
8615 then
8616 Ext := Record_Extension_Part (Type_Definition (Parent (B)));
8617
8618 if Present (Ext) then
8619 if Null_Present (Ext) then
8620 return True;
8621 else
8622 Comps := Component_List (Ext);
8623
8624 -- The null component list is rewritten during analysis to
8625 -- include the parent component. Any other component indicates
8626 -- that the extension was not originally null.
8627
8628 return Null_Present (Comps)
8629 or else No (Next (First (Component_Items (Comps))));
8630 end if;
8631 else
8632 return False;
8633 end if;
8634
8635 else
8636 return False;
8637 end if;
8638 end Has_Null_Extension;
8639
8640 -------------------------------
8641 -- Has_Overriding_Initialize --
8642 -------------------------------
8643
8644 function Has_Overriding_Initialize (T : Entity_Id) return Boolean is
8645 BT : constant Entity_Id := Base_Type (T);
8646 P : Elmt_Id;
8647
8648 begin
8649 if Is_Controlled (BT) then
8650 if Is_RTU (Scope (BT), Ada_Finalization) then
8651 return False;
8652
8653 elsif Present (Primitive_Operations (BT)) then
8654 P := First_Elmt (Primitive_Operations (BT));
8655 while Present (P) loop
8656 declare
8657 Init : constant Entity_Id := Node (P);
8658 Formal : constant Entity_Id := First_Formal (Init);
8659 begin
8660 if Ekind (Init) = E_Procedure
8661 and then Chars (Init) = Name_Initialize
8662 and then Comes_From_Source (Init)
8663 and then Present (Formal)
8664 and then Etype (Formal) = BT
8665 and then No (Next_Formal (Formal))
8666 and then (Ada_Version < Ada_2012
8667 or else not Null_Present (Parent (Init)))
8668 then
8669 return True;
8670 end if;
8671 end;
8672
8673 Next_Elmt (P);
8674 end loop;
8675 end if;
8676
8677 -- Here if type itself does not have a non-null Initialize operation:
8678 -- check immediate ancestor.
8679
8680 if Is_Derived_Type (BT)
8681 and then Has_Overriding_Initialize (Etype (BT))
8682 then
8683 return True;
8684 end if;
8685 end if;
8686
8687 return False;
8688 end Has_Overriding_Initialize;
8689
8690 --------------------------------------
8691 -- Has_Preelaborable_Initialization --
8692 --------------------------------------
8693
8694 function Has_Preelaborable_Initialization (E : Entity_Id) return Boolean is
8695 Has_PE : Boolean;
8696
8697 procedure Check_Components (E : Entity_Id);
8698 -- Check component/discriminant chain, sets Has_PE False if a component
8699 -- or discriminant does not meet the preelaborable initialization rules.
8700
8701 ----------------------
8702 -- Check_Components --
8703 ----------------------
8704
8705 procedure Check_Components (E : Entity_Id) is
8706 Ent : Entity_Id;
8707 Exp : Node_Id;
8708
8709 function Is_Preelaborable_Expression (N : Node_Id) return Boolean;
8710 -- Returns True if and only if the expression denoted by N does not
8711 -- violate restrictions on preelaborable constructs (RM-10.2.1(5-9)).
8712
8713 ---------------------------------
8714 -- Is_Preelaborable_Expression --
8715 ---------------------------------
8716
8717 function Is_Preelaborable_Expression (N : Node_Id) return Boolean is
8718 Exp : Node_Id;
8719 Assn : Node_Id;
8720 Choice : Node_Id;
8721 Comp_Type : Entity_Id;
8722 Is_Array_Aggr : Boolean;
8723
8724 begin
8725 if Is_OK_Static_Expression (N) then
8726 return True;
8727
8728 elsif Nkind (N) = N_Null then
8729 return True;
8730
8731 -- Attributes are allowed in general, even if their prefix is a
8732 -- formal type. (It seems that certain attributes known not to be
8733 -- static might not be allowed, but there are no rules to prevent
8734 -- them.)
8735
8736 elsif Nkind (N) = N_Attribute_Reference then
8737 return True;
8738
8739 -- The name of a discriminant evaluated within its parent type is
8740 -- defined to be preelaborable (10.2.1(8)). Note that we test for
8741 -- names that denote discriminals as well as discriminants to
8742 -- catch references occurring within init procs.
8743
8744 elsif Is_Entity_Name (N)
8745 and then
8746 (Ekind (Entity (N)) = E_Discriminant
8747 or else (Ekind_In (Entity (N), E_Constant, E_In_Parameter)
8748 and then Present (Discriminal_Link (Entity (N)))))
8749 then
8750 return True;
8751
8752 elsif Nkind (N) = N_Qualified_Expression then
8753 return Is_Preelaborable_Expression (Expression (N));
8754
8755 -- For aggregates we have to check that each of the associations
8756 -- is preelaborable.
8757
8758 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
8759 Is_Array_Aggr := Is_Array_Type (Etype (N));
8760
8761 if Is_Array_Aggr then
8762 Comp_Type := Component_Type (Etype (N));
8763 end if;
8764
8765 -- Check the ancestor part of extension aggregates, which must
8766 -- be either the name of a type that has preelaborable init or
8767 -- an expression that is preelaborable.
8768
8769 if Nkind (N) = N_Extension_Aggregate then
8770 declare
8771 Anc_Part : constant Node_Id := Ancestor_Part (N);
8772
8773 begin
8774 if Is_Entity_Name (Anc_Part)
8775 and then Is_Type (Entity (Anc_Part))
8776 then
8777 if not Has_Preelaborable_Initialization
8778 (Entity (Anc_Part))
8779 then
8780 return False;
8781 end if;
8782
8783 elsif not Is_Preelaborable_Expression (Anc_Part) then
8784 return False;
8785 end if;
8786 end;
8787 end if;
8788
8789 -- Check positional associations
8790
8791 Exp := First (Expressions (N));
8792 while Present (Exp) loop
8793 if not Is_Preelaborable_Expression (Exp) then
8794 return False;
8795 end if;
8796
8797 Next (Exp);
8798 end loop;
8799
8800 -- Check named associations
8801
8802 Assn := First (Component_Associations (N));
8803 while Present (Assn) loop
8804 Choice := First (Choices (Assn));
8805 while Present (Choice) loop
8806 if Is_Array_Aggr then
8807 if Nkind (Choice) = N_Others_Choice then
8808 null;
8809
8810 elsif Nkind (Choice) = N_Range then
8811 if not Is_OK_Static_Range (Choice) then
8812 return False;
8813 end if;
8814
8815 elsif not Is_OK_Static_Expression (Choice) then
8816 return False;
8817 end if;
8818
8819 else
8820 Comp_Type := Etype (Choice);
8821 end if;
8822
8823 Next (Choice);
8824 end loop;
8825
8826 -- If the association has a <> at this point, then we have
8827 -- to check whether the component's type has preelaborable
8828 -- initialization. Note that this only occurs when the
8829 -- association's corresponding component does not have a
8830 -- default expression, the latter case having already been
8831 -- expanded as an expression for the association.
8832
8833 if Box_Present (Assn) then
8834 if not Has_Preelaborable_Initialization (Comp_Type) then
8835 return False;
8836 end if;
8837
8838 -- In the expression case we check whether the expression
8839 -- is preelaborable.
8840
8841 elsif
8842 not Is_Preelaborable_Expression (Expression (Assn))
8843 then
8844 return False;
8845 end if;
8846
8847 Next (Assn);
8848 end loop;
8849
8850 -- If we get here then aggregate as a whole is preelaborable
8851
8852 return True;
8853
8854 -- All other cases are not preelaborable
8855
8856 else
8857 return False;
8858 end if;
8859 end Is_Preelaborable_Expression;
8860
8861 -- Start of processing for Check_Components
8862
8863 begin
8864 -- Loop through entities of record or protected type
8865
8866 Ent := E;
8867 while Present (Ent) loop
8868
8869 -- We are interested only in components and discriminants
8870
8871 Exp := Empty;
8872
8873 case Ekind (Ent) is
8874 when E_Component =>
8875
8876 -- Get default expression if any. If there is no declaration
8877 -- node, it means we have an internal entity. The parent and
8878 -- tag fields are examples of such entities. For such cases,
8879 -- we just test the type of the entity.
8880
8881 if Present (Declaration_Node (Ent)) then
8882 Exp := Expression (Declaration_Node (Ent));
8883 end if;
8884
8885 when E_Discriminant =>
8886
8887 -- Note: for a renamed discriminant, the Declaration_Node
8888 -- may point to the one from the ancestor, and have a
8889 -- different expression, so use the proper attribute to
8890 -- retrieve the expression from the derived constraint.
8891
8892 Exp := Discriminant_Default_Value (Ent);
8893
8894 when others =>
8895 goto Check_Next_Entity;
8896 end case;
8897
8898 -- A component has PI if it has no default expression and the
8899 -- component type has PI.
8900
8901 if No (Exp) then
8902 if not Has_Preelaborable_Initialization (Etype (Ent)) then
8903 Has_PE := False;
8904 exit;
8905 end if;
8906
8907 -- Require the default expression to be preelaborable
8908
8909 elsif not Is_Preelaborable_Expression (Exp) then
8910 Has_PE := False;
8911 exit;
8912 end if;
8913
8914 <<Check_Next_Entity>>
8915 Next_Entity (Ent);
8916 end loop;
8917 end Check_Components;
8918
8919 -- Start of processing for Has_Preelaborable_Initialization
8920
8921 begin
8922 -- Immediate return if already marked as known preelaborable init. This
8923 -- covers types for which this function has already been called once
8924 -- and returned True (in which case the result is cached), and also
8925 -- types to which a pragma Preelaborable_Initialization applies.
8926
8927 if Known_To_Have_Preelab_Init (E) then
8928 return True;
8929 end if;
8930
8931 -- If the type is a subtype representing a generic actual type, then
8932 -- test whether its base type has preelaborable initialization since
8933 -- the subtype representing the actual does not inherit this attribute
8934 -- from the actual or formal. (but maybe it should???)
8935
8936 if Is_Generic_Actual_Type (E) then
8937 return Has_Preelaborable_Initialization (Base_Type (E));
8938 end if;
8939
8940 -- All elementary types have preelaborable initialization
8941
8942 if Is_Elementary_Type (E) then
8943 Has_PE := True;
8944
8945 -- Array types have PI if the component type has PI
8946
8947 elsif Is_Array_Type (E) then
8948 Has_PE := Has_Preelaborable_Initialization (Component_Type (E));
8949
8950 -- A derived type has preelaborable initialization if its parent type
8951 -- has preelaborable initialization and (in the case of a derived record
8952 -- extension) if the non-inherited components all have preelaborable
8953 -- initialization. However, a user-defined controlled type with an
8954 -- overriding Initialize procedure does not have preelaborable
8955 -- initialization.
8956
8957 elsif Is_Derived_Type (E) then
8958
8959 -- If the derived type is a private extension then it doesn't have
8960 -- preelaborable initialization.
8961
8962 if Ekind (Base_Type (E)) = E_Record_Type_With_Private then
8963 return False;
8964 end if;
8965
8966 -- First check whether ancestor type has preelaborable initialization
8967
8968 Has_PE := Has_Preelaborable_Initialization (Etype (Base_Type (E)));
8969
8970 -- If OK, check extension components (if any)
8971
8972 if Has_PE and then Is_Record_Type (E) then
8973 Check_Components (First_Entity (E));
8974 end if;
8975
8976 -- Check specifically for 10.2.1(11.4/2) exception: a controlled type
8977 -- with a user defined Initialize procedure does not have PI. If
8978 -- the type is untagged, the control primitives come from a component
8979 -- that has already been checked.
8980
8981 if Has_PE
8982 and then Is_Controlled (E)
8983 and then Is_Tagged_Type (E)
8984 and then Has_Overriding_Initialize (E)
8985 then
8986 Has_PE := False;
8987 end if;
8988
8989 -- Private types not derived from a type having preelaborable init and
8990 -- that are not marked with pragma Preelaborable_Initialization do not
8991 -- have preelaborable initialization.
8992
8993 elsif Is_Private_Type (E) then
8994 return False;
8995
8996 -- Record type has PI if it is non private and all components have PI
8997
8998 elsif Is_Record_Type (E) then
8999 Has_PE := True;
9000 Check_Components (First_Entity (E));
9001
9002 -- Protected types must not have entries, and components must meet
9003 -- same set of rules as for record components.
9004
9005 elsif Is_Protected_Type (E) then
9006 if Has_Entries (E) then
9007 Has_PE := False;
9008 else
9009 Has_PE := True;
9010 Check_Components (First_Entity (E));
9011 Check_Components (First_Private_Entity (E));
9012 end if;
9013
9014 -- Type System.Address always has preelaborable initialization
9015
9016 elsif Is_RTE (E, RE_Address) then
9017 Has_PE := True;
9018
9019 -- In all other cases, type does not have preelaborable initialization
9020
9021 else
9022 return False;
9023 end if;
9024
9025 -- If type has preelaborable initialization, cache result
9026
9027 if Has_PE then
9028 Set_Known_To_Have_Preelab_Init (E);
9029 end if;
9030
9031 return Has_PE;
9032 end Has_Preelaborable_Initialization;
9033
9034 ---------------------------
9035 -- Has_Private_Component --
9036 ---------------------------
9037
9038 function Has_Private_Component (Type_Id : Entity_Id) return Boolean is
9039 Btype : Entity_Id := Base_Type (Type_Id);
9040 Component : Entity_Id;
9041
9042 begin
9043 if Error_Posted (Type_Id)
9044 or else Error_Posted (Btype)
9045 then
9046 return False;
9047 end if;
9048
9049 if Is_Class_Wide_Type (Btype) then
9050 Btype := Root_Type (Btype);
9051 end if;
9052
9053 if Is_Private_Type (Btype) then
9054 declare
9055 UT : constant Entity_Id := Underlying_Type (Btype);
9056 begin
9057 if No (UT) then
9058 if No (Full_View (Btype)) then
9059 return not Is_Generic_Type (Btype)
9060 and then
9061 not Is_Generic_Type (Root_Type (Btype));
9062 else
9063 return not Is_Generic_Type (Root_Type (Full_View (Btype)));
9064 end if;
9065 else
9066 return not Is_Frozen (UT) and then Has_Private_Component (UT);
9067 end if;
9068 end;
9069
9070 elsif Is_Array_Type (Btype) then
9071 return Has_Private_Component (Component_Type (Btype));
9072
9073 elsif Is_Record_Type (Btype) then
9074 Component := First_Component (Btype);
9075 while Present (Component) loop
9076 if Has_Private_Component (Etype (Component)) then
9077 return True;
9078 end if;
9079
9080 Next_Component (Component);
9081 end loop;
9082
9083 return False;
9084
9085 elsif Is_Protected_Type (Btype)
9086 and then Present (Corresponding_Record_Type (Btype))
9087 then
9088 return Has_Private_Component (Corresponding_Record_Type (Btype));
9089
9090 else
9091 return False;
9092 end if;
9093 end Has_Private_Component;
9094
9095 ----------------------
9096 -- Has_Signed_Zeros --
9097 ----------------------
9098
9099 function Has_Signed_Zeros (E : Entity_Id) return Boolean is
9100 begin
9101 return Is_Floating_Point_Type (E) and then Signed_Zeros_On_Target;
9102 end Has_Signed_Zeros;
9103
9104 ------------------------------
9105 -- Has_Significant_Contract --
9106 ------------------------------
9107
9108 function Has_Significant_Contract (Subp_Id : Entity_Id) return Boolean is
9109 Subp_Nam : constant Name_Id := Chars (Subp_Id);
9110
9111 begin
9112 -- _Finalizer procedure
9113
9114 if Subp_Nam = Name_uFinalizer then
9115 return False;
9116
9117 -- _Postconditions procedure
9118
9119 elsif Subp_Nam = Name_uPostconditions then
9120 return False;
9121
9122 -- Predicate function
9123
9124 elsif Ekind (Subp_Id) = E_Function
9125 and then Is_Predicate_Function (Subp_Id)
9126 then
9127 return False;
9128
9129 -- TSS subprogram
9130
9131 elsif Get_TSS_Name (Subp_Id) /= TSS_Null then
9132 return False;
9133
9134 else
9135 return True;
9136 end if;
9137 end Has_Significant_Contract;
9138
9139 -----------------------------
9140 -- Has_Static_Array_Bounds --
9141 -----------------------------
9142
9143 function Has_Static_Array_Bounds (Typ : Node_Id) return Boolean is
9144 Ndims : constant Nat := Number_Dimensions (Typ);
9145
9146 Index : Node_Id;
9147 Low : Node_Id;
9148 High : Node_Id;
9149
9150 begin
9151 -- Unconstrained types do not have static bounds
9152
9153 if not Is_Constrained (Typ) then
9154 return False;
9155 end if;
9156
9157 -- First treat string literals specially, as the lower bound and length
9158 -- of string literals are not stored like those of arrays.
9159
9160 -- A string literal always has static bounds
9161
9162 if Ekind (Typ) = E_String_Literal_Subtype then
9163 return True;
9164 end if;
9165
9166 -- Treat all dimensions in turn
9167
9168 Index := First_Index (Typ);
9169 for Indx in 1 .. Ndims loop
9170
9171 -- In case of an illegal index which is not a discrete type, return
9172 -- that the type is not static.
9173
9174 if not Is_Discrete_Type (Etype (Index))
9175 or else Etype (Index) = Any_Type
9176 then
9177 return False;
9178 end if;
9179
9180 Get_Index_Bounds (Index, Low, High);
9181
9182 if Error_Posted (Low) or else Error_Posted (High) then
9183 return False;
9184 end if;
9185
9186 if Is_OK_Static_Expression (Low)
9187 and then
9188 Is_OK_Static_Expression (High)
9189 then
9190 null;
9191 else
9192 return False;
9193 end if;
9194
9195 Next (Index);
9196 end loop;
9197
9198 -- If we fall through the loop, all indexes matched
9199
9200 return True;
9201 end Has_Static_Array_Bounds;
9202
9203 ----------------
9204 -- Has_Stream --
9205 ----------------
9206
9207 function Has_Stream (T : Entity_Id) return Boolean is
9208 E : Entity_Id;
9209
9210 begin
9211 if No (T) then
9212 return False;
9213
9214 elsif Is_RTE (Root_Type (T), RE_Root_Stream_Type) then
9215 return True;
9216
9217 elsif Is_Array_Type (T) then
9218 return Has_Stream (Component_Type (T));
9219
9220 elsif Is_Record_Type (T) then
9221 E := First_Component (T);
9222 while Present (E) loop
9223 if Has_Stream (Etype (E)) then
9224 return True;
9225 else
9226 Next_Component (E);
9227 end if;
9228 end loop;
9229
9230 return False;
9231
9232 elsif Is_Private_Type (T) then
9233 return Has_Stream (Underlying_Type (T));
9234
9235 else
9236 return False;
9237 end if;
9238 end Has_Stream;
9239
9240 ----------------
9241 -- Has_Suffix --
9242 ----------------
9243
9244 function Has_Suffix (E : Entity_Id; Suffix : Character) return Boolean is
9245 begin
9246 Get_Name_String (Chars (E));
9247 return Name_Buffer (Name_Len) = Suffix;
9248 end Has_Suffix;
9249
9250 ----------------
9251 -- Add_Suffix --
9252 ----------------
9253
9254 function Add_Suffix (E : Entity_Id; Suffix : Character) return Name_Id is
9255 begin
9256 Get_Name_String (Chars (E));
9257 Add_Char_To_Name_Buffer (Suffix);
9258 return Name_Find;
9259 end Add_Suffix;
9260
9261 -------------------
9262 -- Remove_Suffix --
9263 -------------------
9264
9265 function Remove_Suffix (E : Entity_Id; Suffix : Character) return Name_Id is
9266 begin
9267 pragma Assert (Has_Suffix (E, Suffix));
9268 Get_Name_String (Chars (E));
9269 Name_Len := Name_Len - 1;
9270 return Name_Find;
9271 end Remove_Suffix;
9272
9273 --------------------------
9274 -- Has_Tagged_Component --
9275 --------------------------
9276
9277 function Has_Tagged_Component (Typ : Entity_Id) return Boolean is
9278 Comp : Entity_Id;
9279
9280 begin
9281 if Is_Private_Type (Typ) and then Present (Underlying_Type (Typ)) then
9282 return Has_Tagged_Component (Underlying_Type (Typ));
9283
9284 elsif Is_Array_Type (Typ) then
9285 return Has_Tagged_Component (Component_Type (Typ));
9286
9287 elsif Is_Tagged_Type (Typ) then
9288 return True;
9289
9290 elsif Is_Record_Type (Typ) then
9291 Comp := First_Component (Typ);
9292 while Present (Comp) loop
9293 if Has_Tagged_Component (Etype (Comp)) then
9294 return True;
9295 end if;
9296
9297 Next_Component (Comp);
9298 end loop;
9299
9300 return False;
9301
9302 else
9303 return False;
9304 end if;
9305 end Has_Tagged_Component;
9306
9307 ----------------------------
9308 -- Has_Volatile_Component --
9309 ----------------------------
9310
9311 function Has_Volatile_Component (Typ : Entity_Id) return Boolean is
9312 Comp : Entity_Id;
9313
9314 begin
9315 if Has_Volatile_Components (Typ) then
9316 return True;
9317
9318 elsif Is_Array_Type (Typ) then
9319 return Is_Volatile (Component_Type (Typ));
9320
9321 elsif Is_Record_Type (Typ) then
9322 Comp := First_Component (Typ);
9323 while Present (Comp) loop
9324 if Is_Volatile_Object (Comp) then
9325 return True;
9326 end if;
9327
9328 Comp := Next_Component (Comp);
9329 end loop;
9330 end if;
9331
9332 return False;
9333 end Has_Volatile_Component;
9334
9335 -------------------------
9336 -- Implementation_Kind --
9337 -------------------------
9338
9339 function Implementation_Kind (Subp : Entity_Id) return Name_Id is
9340 Impl_Prag : constant Node_Id := Get_Rep_Pragma (Subp, Name_Implemented);
9341 Arg : Node_Id;
9342 begin
9343 pragma Assert (Present (Impl_Prag));
9344 Arg := Last (Pragma_Argument_Associations (Impl_Prag));
9345 return Chars (Get_Pragma_Arg (Arg));
9346 end Implementation_Kind;
9347
9348 --------------------------
9349 -- Implements_Interface --
9350 --------------------------
9351
9352 function Implements_Interface
9353 (Typ_Ent : Entity_Id;
9354 Iface_Ent : Entity_Id;
9355 Exclude_Parents : Boolean := False) return Boolean
9356 is
9357 Ifaces_List : Elist_Id;
9358 Elmt : Elmt_Id;
9359 Iface : Entity_Id := Base_Type (Iface_Ent);
9360 Typ : Entity_Id := Base_Type (Typ_Ent);
9361
9362 begin
9363 if Is_Class_Wide_Type (Typ) then
9364 Typ := Root_Type (Typ);
9365 end if;
9366
9367 if not Has_Interfaces (Typ) then
9368 return False;
9369 end if;
9370
9371 if Is_Class_Wide_Type (Iface) then
9372 Iface := Root_Type (Iface);
9373 end if;
9374
9375 Collect_Interfaces (Typ, Ifaces_List);
9376
9377 Elmt := First_Elmt (Ifaces_List);
9378 while Present (Elmt) loop
9379 if Is_Ancestor (Node (Elmt), Typ, Use_Full_View => True)
9380 and then Exclude_Parents
9381 then
9382 null;
9383
9384 elsif Node (Elmt) = Iface then
9385 return True;
9386 end if;
9387
9388 Next_Elmt (Elmt);
9389 end loop;
9390
9391 return False;
9392 end Implements_Interface;
9393
9394 ------------------------------------
9395 -- In_Assertion_Expression_Pragma --
9396 ------------------------------------
9397
9398 function In_Assertion_Expression_Pragma (N : Node_Id) return Boolean is
9399 Par : Node_Id;
9400 Prag : Node_Id := Empty;
9401
9402 begin
9403 -- Climb the parent chain looking for an enclosing pragma
9404
9405 Par := N;
9406 while Present (Par) loop
9407 if Nkind (Par) = N_Pragma then
9408 Prag := Par;
9409 exit;
9410
9411 -- Precondition-like pragmas are expanded into if statements, check
9412 -- the original node instead.
9413
9414 elsif Nkind (Original_Node (Par)) = N_Pragma then
9415 Prag := Original_Node (Par);
9416 exit;
9417
9418 -- The expansion of attribute 'Old generates a constant to capture
9419 -- the result of the prefix. If the parent traversal reaches
9420 -- one of these constants, then the node technically came from a
9421 -- postcondition-like pragma. Note that the Ekind is not tested here
9422 -- because N may be the expression of an object declaration which is
9423 -- currently being analyzed. Such objects carry Ekind of E_Void.
9424
9425 elsif Nkind (Par) = N_Object_Declaration
9426 and then Constant_Present (Par)
9427 and then Stores_Attribute_Old_Prefix (Defining_Entity (Par))
9428 then
9429 return True;
9430
9431 -- Prevent the search from going too far
9432
9433 elsif Is_Body_Or_Package_Declaration (Par) then
9434 return False;
9435 end if;
9436
9437 Par := Parent (Par);
9438 end loop;
9439
9440 return
9441 Present (Prag)
9442 and then Assertion_Expression_Pragma (Get_Pragma_Id (Prag));
9443 end In_Assertion_Expression_Pragma;
9444
9445 -----------------
9446 -- In_Instance --
9447 -----------------
9448
9449 function In_Instance return Boolean is
9450 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
9451 S : Entity_Id;
9452
9453 begin
9454 S := Current_Scope;
9455 while Present (S) and then S /= Standard_Standard loop
9456 if Ekind_In (S, E_Function, E_Package, E_Procedure)
9457 and then Is_Generic_Instance (S)
9458 then
9459 -- A child instance is always compiled in the context of a parent
9460 -- instance. Nevertheless, the actuals are not analyzed in an
9461 -- instance context. We detect this case by examining the current
9462 -- compilation unit, which must be a child instance, and checking
9463 -- that it is not currently on the scope stack.
9464
9465 if Is_Child_Unit (Curr_Unit)
9466 and then Nkind (Unit (Cunit (Current_Sem_Unit))) =
9467 N_Package_Instantiation
9468 and then not In_Open_Scopes (Curr_Unit)
9469 then
9470 return False;
9471 else
9472 return True;
9473 end if;
9474 end if;
9475
9476 S := Scope (S);
9477 end loop;
9478
9479 return False;
9480 end In_Instance;
9481
9482 ----------------------
9483 -- In_Instance_Body --
9484 ----------------------
9485
9486 function In_Instance_Body return Boolean is
9487 S : Entity_Id;
9488
9489 begin
9490 S := Current_Scope;
9491 while Present (S) and then S /= Standard_Standard loop
9492 if Ekind_In (S, E_Function, E_Procedure)
9493 and then Is_Generic_Instance (S)
9494 then
9495 return True;
9496
9497 elsif Ekind (S) = E_Package
9498 and then In_Package_Body (S)
9499 and then Is_Generic_Instance (S)
9500 then
9501 return True;
9502 end if;
9503
9504 S := Scope (S);
9505 end loop;
9506
9507 return False;
9508 end In_Instance_Body;
9509
9510 -----------------------------
9511 -- In_Instance_Not_Visible --
9512 -----------------------------
9513
9514 function In_Instance_Not_Visible return Boolean is
9515 S : Entity_Id;
9516
9517 begin
9518 S := Current_Scope;
9519 while Present (S) and then S /= Standard_Standard loop
9520 if Ekind_In (S, E_Function, E_Procedure)
9521 and then Is_Generic_Instance (S)
9522 then
9523 return True;
9524
9525 elsif Ekind (S) = E_Package
9526 and then (In_Package_Body (S) or else In_Private_Part (S))
9527 and then Is_Generic_Instance (S)
9528 then
9529 return True;
9530 end if;
9531
9532 S := Scope (S);
9533 end loop;
9534
9535 return False;
9536 end In_Instance_Not_Visible;
9537
9538 ------------------------------
9539 -- In_Instance_Visible_Part --
9540 ------------------------------
9541
9542 function In_Instance_Visible_Part return Boolean is
9543 S : Entity_Id;
9544
9545 begin
9546 S := Current_Scope;
9547 while Present (S) and then S /= Standard_Standard loop
9548 if Ekind (S) = E_Package
9549 and then Is_Generic_Instance (S)
9550 and then not In_Package_Body (S)
9551 and then not In_Private_Part (S)
9552 then
9553 return True;
9554 end if;
9555
9556 S := Scope (S);
9557 end loop;
9558
9559 return False;
9560 end In_Instance_Visible_Part;
9561
9562 ---------------------
9563 -- In_Package_Body --
9564 ---------------------
9565
9566 function In_Package_Body return Boolean is
9567 S : Entity_Id;
9568
9569 begin
9570 S := Current_Scope;
9571 while Present (S) and then S /= Standard_Standard loop
9572 if Ekind (S) = E_Package and then In_Package_Body (S) then
9573 return True;
9574 else
9575 S := Scope (S);
9576 end if;
9577 end loop;
9578
9579 return False;
9580 end In_Package_Body;
9581
9582 --------------------------------
9583 -- In_Parameter_Specification --
9584 --------------------------------
9585
9586 function In_Parameter_Specification (N : Node_Id) return Boolean is
9587 PN : Node_Id;
9588
9589 begin
9590 PN := Parent (N);
9591 while Present (PN) loop
9592 if Nkind (PN) = N_Parameter_Specification then
9593 return True;
9594 end if;
9595
9596 PN := Parent (PN);
9597 end loop;
9598
9599 return False;
9600 end In_Parameter_Specification;
9601
9602 --------------------------
9603 -- In_Pragma_Expression --
9604 --------------------------
9605
9606 function In_Pragma_Expression (N : Node_Id; Nam : Name_Id) return Boolean is
9607 P : Node_Id;
9608 begin
9609 P := Parent (N);
9610 loop
9611 if No (P) then
9612 return False;
9613 elsif Nkind (P) = N_Pragma and then Pragma_Name (P) = Nam then
9614 return True;
9615 else
9616 P := Parent (P);
9617 end if;
9618 end loop;
9619 end In_Pragma_Expression;
9620
9621 -------------------------------------
9622 -- In_Reverse_Storage_Order_Object --
9623 -------------------------------------
9624
9625 function In_Reverse_Storage_Order_Object (N : Node_Id) return Boolean is
9626 Pref : Node_Id;
9627 Btyp : Entity_Id := Empty;
9628
9629 begin
9630 -- Climb up indexed components
9631
9632 Pref := N;
9633 loop
9634 case Nkind (Pref) is
9635 when N_Selected_Component =>
9636 Pref := Prefix (Pref);
9637 exit;
9638
9639 when N_Indexed_Component =>
9640 Pref := Prefix (Pref);
9641
9642 when others =>
9643 Pref := Empty;
9644 exit;
9645 end case;
9646 end loop;
9647
9648 if Present (Pref) then
9649 Btyp := Base_Type (Etype (Pref));
9650 end if;
9651
9652 return Present (Btyp)
9653 and then (Is_Record_Type (Btyp) or else Is_Array_Type (Btyp))
9654 and then Reverse_Storage_Order (Btyp);
9655 end In_Reverse_Storage_Order_Object;
9656
9657 --------------------------------------
9658 -- In_Subprogram_Or_Concurrent_Unit --
9659 --------------------------------------
9660
9661 function In_Subprogram_Or_Concurrent_Unit return Boolean is
9662 E : Entity_Id;
9663 K : Entity_Kind;
9664
9665 begin
9666 -- Use scope chain to check successively outer scopes
9667
9668 E := Current_Scope;
9669 loop
9670 K := Ekind (E);
9671
9672 if K in Subprogram_Kind
9673 or else K in Concurrent_Kind
9674 or else K in Generic_Subprogram_Kind
9675 then
9676 return True;
9677
9678 elsif E = Standard_Standard then
9679 return False;
9680 end if;
9681
9682 E := Scope (E);
9683 end loop;
9684 end In_Subprogram_Or_Concurrent_Unit;
9685
9686 ---------------------
9687 -- In_Visible_Part --
9688 ---------------------
9689
9690 function In_Visible_Part (Scope_Id : Entity_Id) return Boolean is
9691 begin
9692 return Is_Package_Or_Generic_Package (Scope_Id)
9693 and then In_Open_Scopes (Scope_Id)
9694 and then not In_Package_Body (Scope_Id)
9695 and then not In_Private_Part (Scope_Id);
9696 end In_Visible_Part;
9697
9698 --------------------------------
9699 -- Incomplete_Or_Partial_View --
9700 --------------------------------
9701
9702 function Incomplete_Or_Partial_View (Id : Entity_Id) return Entity_Id is
9703 function Inspect_Decls
9704 (Decls : List_Id;
9705 Taft : Boolean := False) return Entity_Id;
9706 -- Check whether a declarative region contains the incomplete or partial
9707 -- view of Id.
9708
9709 -------------------
9710 -- Inspect_Decls --
9711 -------------------
9712
9713 function Inspect_Decls
9714 (Decls : List_Id;
9715 Taft : Boolean := False) return Entity_Id
9716 is
9717 Decl : Node_Id;
9718 Match : Node_Id;
9719
9720 begin
9721 Decl := First (Decls);
9722 while Present (Decl) loop
9723 Match := Empty;
9724
9725 if Taft then
9726 if Nkind (Decl) = N_Incomplete_Type_Declaration then
9727 Match := Defining_Identifier (Decl);
9728 end if;
9729
9730 else
9731 if Nkind_In (Decl, N_Private_Extension_Declaration,
9732 N_Private_Type_Declaration)
9733 then
9734 Match := Defining_Identifier (Decl);
9735 end if;
9736 end if;
9737
9738 if Present (Match)
9739 and then Present (Full_View (Match))
9740 and then Full_View (Match) = Id
9741 then
9742 return Match;
9743 end if;
9744
9745 Next (Decl);
9746 end loop;
9747
9748 return Empty;
9749 end Inspect_Decls;
9750
9751 -- Local variables
9752
9753 Prev : Entity_Id;
9754
9755 -- Start of processing for Incomplete_Or_Partial_View
9756
9757 begin
9758 -- Deferred constant or incomplete type case
9759
9760 Prev := Current_Entity_In_Scope (Id);
9761
9762 if Present (Prev)
9763 and then (Is_Incomplete_Type (Prev) or else Ekind (Prev) = E_Constant)
9764 and then Present (Full_View (Prev))
9765 and then Full_View (Prev) = Id
9766 then
9767 return Prev;
9768 end if;
9769
9770 -- Private or Taft amendment type case
9771
9772 declare
9773 Pkg : constant Entity_Id := Scope (Id);
9774 Pkg_Decl : Node_Id := Pkg;
9775
9776 begin
9777 if Present (Pkg) and then Ekind (Pkg) = E_Package then
9778 while Nkind (Pkg_Decl) /= N_Package_Specification loop
9779 Pkg_Decl := Parent (Pkg_Decl);
9780 end loop;
9781
9782 -- It is knows that Typ has a private view, look for it in the
9783 -- visible declarations of the enclosing scope. A special case
9784 -- of this is when the two views have been exchanged - the full
9785 -- appears earlier than the private.
9786
9787 if Has_Private_Declaration (Id) then
9788 Prev := Inspect_Decls (Visible_Declarations (Pkg_Decl));
9789
9790 -- Exchanged view case, look in the private declarations
9791
9792 if No (Prev) then
9793 Prev := Inspect_Decls (Private_Declarations (Pkg_Decl));
9794 end if;
9795
9796 return Prev;
9797
9798 -- Otherwise if this is the package body, then Typ is a potential
9799 -- Taft amendment type. The incomplete view should be located in
9800 -- the private declarations of the enclosing scope.
9801
9802 elsif In_Package_Body (Pkg) then
9803 return Inspect_Decls (Private_Declarations (Pkg_Decl), True);
9804 end if;
9805 end if;
9806 end;
9807
9808 -- The type has no incomplete or private view
9809
9810 return Empty;
9811 end Incomplete_Or_Partial_View;
9812
9813 -----------------------------------------
9814 -- Inherit_Default_Init_Cond_Procedure --
9815 -----------------------------------------
9816
9817 procedure Inherit_Default_Init_Cond_Procedure (Typ : Entity_Id) is
9818 Par_Typ : constant Entity_Id := Etype (Typ);
9819
9820 begin
9821 -- A derived type inherits the default initial condition procedure of
9822 -- its parent type.
9823
9824 if No (Default_Init_Cond_Procedure (Typ)) then
9825 Set_Default_Init_Cond_Procedure
9826 (Typ, Default_Init_Cond_Procedure (Par_Typ));
9827 end if;
9828 end Inherit_Default_Init_Cond_Procedure;
9829
9830 ----------------------------
9831 -- Inherit_Rep_Item_Chain --
9832 ----------------------------
9833
9834 procedure Inherit_Rep_Item_Chain (Typ : Entity_Id; From_Typ : Entity_Id) is
9835 From_Item : constant Node_Id := First_Rep_Item (From_Typ);
9836 Item : Node_Id := Empty;
9837 Last_Item : Node_Id := Empty;
9838
9839 begin
9840 -- Reach the end of the destination type's chain (if any) and capture
9841 -- the last item.
9842
9843 Item := First_Rep_Item (Typ);
9844 while Present (Item) loop
9845
9846 -- Do not inherit a chain that has been inherited already
9847
9848 if Item = From_Item then
9849 return;
9850 end if;
9851
9852 Last_Item := Item;
9853 Item := Next_Rep_Item (Item);
9854 end loop;
9855
9856 -- When the destination type has a rep item chain, the chain of the
9857 -- source type is appended to it.
9858
9859 if Present (Last_Item) then
9860 Set_Next_Rep_Item (Last_Item, From_Item);
9861
9862 -- Otherwise the destination type directly inherits the rep item chain
9863 -- of the source type (if any).
9864
9865 else
9866 Set_First_Rep_Item (Typ, From_Item);
9867 end if;
9868 end Inherit_Rep_Item_Chain;
9869
9870 ---------------------------------
9871 -- Inherit_Subprogram_Contract --
9872 ---------------------------------
9873
9874 procedure Inherit_Subprogram_Contract
9875 (Subp : Entity_Id;
9876 From_Subp : Entity_Id)
9877 is
9878 procedure Inherit_Pragma (Prag_Id : Pragma_Id);
9879 -- Propagate a pragma denoted by Prag_Id from From_Subp's contract to
9880 -- Subp's contract.
9881
9882 --------------------
9883 -- Inherit_Pragma --
9884 --------------------
9885
9886 procedure Inherit_Pragma (Prag_Id : Pragma_Id) is
9887 Prag : constant Node_Id := Get_Pragma (From_Subp, Prag_Id);
9888 New_Prag : Node_Id;
9889
9890 begin
9891 -- A pragma cannot be part of more than one First_Pragma/Next_Pragma
9892 -- chains, therefore the node must be replicated. The new pragma is
9893 -- flagged is inherited for distrinction purposes.
9894
9895 if Present (Prag) then
9896 New_Prag := New_Copy_Tree (Prag);
9897 Set_Is_Inherited (New_Prag);
9898
9899 Add_Contract_Item (New_Prag, Subp);
9900 end if;
9901 end Inherit_Pragma;
9902
9903 -- Start of processing for Inherit_Subprogram_Contract
9904
9905 begin
9906 -- Inheritance is carried out only when both entities are subprograms
9907 -- with contracts.
9908
9909 if Is_Subprogram_Or_Generic_Subprogram (Subp)
9910 and then Is_Subprogram_Or_Generic_Subprogram (From_Subp)
9911 and then Present (Contract (From_Subp))
9912 then
9913 Inherit_Pragma (Pragma_Extensions_Visible);
9914 end if;
9915 end Inherit_Subprogram_Contract;
9916
9917 ---------------------------------
9918 -- Insert_Explicit_Dereference --
9919 ---------------------------------
9920
9921 procedure Insert_Explicit_Dereference (N : Node_Id) is
9922 New_Prefix : constant Node_Id := Relocate_Node (N);
9923 Ent : Entity_Id := Empty;
9924 Pref : Node_Id;
9925 I : Interp_Index;
9926 It : Interp;
9927 T : Entity_Id;
9928
9929 begin
9930 Save_Interps (N, New_Prefix);
9931
9932 Rewrite (N,
9933 Make_Explicit_Dereference (Sloc (Parent (N)),
9934 Prefix => New_Prefix));
9935
9936 Set_Etype (N, Designated_Type (Etype (New_Prefix)));
9937
9938 if Is_Overloaded (New_Prefix) then
9939
9940 -- The dereference is also overloaded, and its interpretations are
9941 -- the designated types of the interpretations of the original node.
9942
9943 Set_Etype (N, Any_Type);
9944
9945 Get_First_Interp (New_Prefix, I, It);
9946 while Present (It.Nam) loop
9947 T := It.Typ;
9948
9949 if Is_Access_Type (T) then
9950 Add_One_Interp (N, Designated_Type (T), Designated_Type (T));
9951 end if;
9952
9953 Get_Next_Interp (I, It);
9954 end loop;
9955
9956 End_Interp_List;
9957
9958 else
9959 -- Prefix is unambiguous: mark the original prefix (which might
9960 -- Come_From_Source) as a reference, since the new (relocated) one
9961 -- won't be taken into account.
9962
9963 if Is_Entity_Name (New_Prefix) then
9964 Ent := Entity (New_Prefix);
9965 Pref := New_Prefix;
9966
9967 -- For a retrieval of a subcomponent of some composite object,
9968 -- retrieve the ultimate entity if there is one.
9969
9970 elsif Nkind_In (New_Prefix, N_Selected_Component,
9971 N_Indexed_Component)
9972 then
9973 Pref := Prefix (New_Prefix);
9974 while Present (Pref)
9975 and then Nkind_In (Pref, N_Selected_Component,
9976 N_Indexed_Component)
9977 loop
9978 Pref := Prefix (Pref);
9979 end loop;
9980
9981 if Present (Pref) and then Is_Entity_Name (Pref) then
9982 Ent := Entity (Pref);
9983 end if;
9984 end if;
9985
9986 -- Place the reference on the entity node
9987
9988 if Present (Ent) then
9989 Generate_Reference (Ent, Pref);
9990 end if;
9991 end if;
9992 end Insert_Explicit_Dereference;
9993
9994 ------------------------------------------
9995 -- Inspect_Deferred_Constant_Completion --
9996 ------------------------------------------
9997
9998 procedure Inspect_Deferred_Constant_Completion (Decls : List_Id) is
9999 Decl : Node_Id;
10000
10001 begin
10002 Decl := First (Decls);
10003 while Present (Decl) loop
10004
10005 -- Deferred constant signature
10006
10007 if Nkind (Decl) = N_Object_Declaration
10008 and then Constant_Present (Decl)
10009 and then No (Expression (Decl))
10010
10011 -- No need to check internally generated constants
10012
10013 and then Comes_From_Source (Decl)
10014
10015 -- The constant is not completed. A full object declaration or a
10016 -- pragma Import complete a deferred constant.
10017
10018 and then not Has_Completion (Defining_Identifier (Decl))
10019 then
10020 Error_Msg_N
10021 ("constant declaration requires initialization expression",
10022 Defining_Identifier (Decl));
10023 end if;
10024
10025 Decl := Next (Decl);
10026 end loop;
10027 end Inspect_Deferred_Constant_Completion;
10028
10029 -----------------------------
10030 -- Install_Generic_Formals --
10031 -----------------------------
10032
10033 procedure Install_Generic_Formals (Subp_Id : Entity_Id) is
10034 E : Entity_Id;
10035
10036 begin
10037 pragma Assert (Is_Generic_Subprogram (Subp_Id));
10038
10039 E := First_Entity (Subp_Id);
10040 while Present (E) loop
10041 Install_Entity (E);
10042 Next_Entity (E);
10043 end loop;
10044 end Install_Generic_Formals;
10045
10046 -----------------------------
10047 -- Is_Actual_Out_Parameter --
10048 -----------------------------
10049
10050 function Is_Actual_Out_Parameter (N : Node_Id) return Boolean is
10051 Formal : Entity_Id;
10052 Call : Node_Id;
10053 begin
10054 Find_Actual (N, Formal, Call);
10055 return Present (Formal) and then Ekind (Formal) = E_Out_Parameter;
10056 end Is_Actual_Out_Parameter;
10057
10058 -------------------------
10059 -- Is_Actual_Parameter --
10060 -------------------------
10061
10062 function Is_Actual_Parameter (N : Node_Id) return Boolean is
10063 PK : constant Node_Kind := Nkind (Parent (N));
10064
10065 begin
10066 case PK is
10067 when N_Parameter_Association =>
10068 return N = Explicit_Actual_Parameter (Parent (N));
10069
10070 when N_Subprogram_Call =>
10071 return Is_List_Member (N)
10072 and then
10073 List_Containing (N) = Parameter_Associations (Parent (N));
10074
10075 when others =>
10076 return False;
10077 end case;
10078 end Is_Actual_Parameter;
10079
10080 --------------------------------
10081 -- Is_Actual_Tagged_Parameter --
10082 --------------------------------
10083
10084 function Is_Actual_Tagged_Parameter (N : Node_Id) return Boolean is
10085 Formal : Entity_Id;
10086 Call : Node_Id;
10087 begin
10088 Find_Actual (N, Formal, Call);
10089 return Present (Formal) and then Is_Tagged_Type (Etype (Formal));
10090 end Is_Actual_Tagged_Parameter;
10091
10092 ---------------------
10093 -- Is_Aliased_View --
10094 ---------------------
10095
10096 function Is_Aliased_View (Obj : Node_Id) return Boolean is
10097 E : Entity_Id;
10098
10099 begin
10100 if Is_Entity_Name (Obj) then
10101 E := Entity (Obj);
10102
10103 return
10104 (Is_Object (E)
10105 and then
10106 (Is_Aliased (E)
10107 or else (Present (Renamed_Object (E))
10108 and then Is_Aliased_View (Renamed_Object (E)))))
10109
10110 or else ((Is_Formal (E)
10111 or else Ekind_In (E, E_Generic_In_Out_Parameter,
10112 E_Generic_In_Parameter))
10113 and then Is_Tagged_Type (Etype (E)))
10114
10115 or else (Is_Concurrent_Type (E) and then In_Open_Scopes (E))
10116
10117 -- Current instance of type, either directly or as rewritten
10118 -- reference to the current object.
10119
10120 or else (Is_Entity_Name (Original_Node (Obj))
10121 and then Present (Entity (Original_Node (Obj)))
10122 and then Is_Type (Entity (Original_Node (Obj))))
10123
10124 or else (Is_Type (E) and then E = Current_Scope)
10125
10126 or else (Is_Incomplete_Or_Private_Type (E)
10127 and then Full_View (E) = Current_Scope)
10128
10129 -- Ada 2012 AI05-0053: the return object of an extended return
10130 -- statement is aliased if its type is immutably limited.
10131
10132 or else (Is_Return_Object (E)
10133 and then Is_Limited_View (Etype (E)));
10134
10135 elsif Nkind (Obj) = N_Selected_Component then
10136 return Is_Aliased (Entity (Selector_Name (Obj)));
10137
10138 elsif Nkind (Obj) = N_Indexed_Component then
10139 return Has_Aliased_Components (Etype (Prefix (Obj)))
10140 or else
10141 (Is_Access_Type (Etype (Prefix (Obj)))
10142 and then Has_Aliased_Components
10143 (Designated_Type (Etype (Prefix (Obj)))));
10144
10145 elsif Nkind_In (Obj, N_Unchecked_Type_Conversion, N_Type_Conversion) then
10146 return Is_Tagged_Type (Etype (Obj))
10147 and then Is_Aliased_View (Expression (Obj));
10148
10149 elsif Nkind (Obj) = N_Explicit_Dereference then
10150 return Nkind (Original_Node (Obj)) /= N_Function_Call;
10151
10152 else
10153 return False;
10154 end if;
10155 end Is_Aliased_View;
10156
10157 -------------------------
10158 -- Is_Ancestor_Package --
10159 -------------------------
10160
10161 function Is_Ancestor_Package
10162 (E1 : Entity_Id;
10163 E2 : Entity_Id) return Boolean
10164 is
10165 Par : Entity_Id;
10166
10167 begin
10168 Par := E2;
10169 while Present (Par) and then Par /= Standard_Standard loop
10170 if Par = E1 then
10171 return True;
10172 end if;
10173
10174 Par := Scope (Par);
10175 end loop;
10176
10177 return False;
10178 end Is_Ancestor_Package;
10179
10180 ----------------------
10181 -- Is_Atomic_Object --
10182 ----------------------
10183
10184 function Is_Atomic_Object (N : Node_Id) return Boolean is
10185
10186 function Object_Has_Atomic_Components (N : Node_Id) return Boolean;
10187 -- Determines if given object has atomic components
10188
10189 function Is_Atomic_Prefix (N : Node_Id) return Boolean;
10190 -- If prefix is an implicit dereference, examine designated type
10191
10192 ----------------------
10193 -- Is_Atomic_Prefix --
10194 ----------------------
10195
10196 function Is_Atomic_Prefix (N : Node_Id) return Boolean is
10197 begin
10198 if Is_Access_Type (Etype (N)) then
10199 return
10200 Has_Atomic_Components (Designated_Type (Etype (N)));
10201 else
10202 return Object_Has_Atomic_Components (N);
10203 end if;
10204 end Is_Atomic_Prefix;
10205
10206 ----------------------------------
10207 -- Object_Has_Atomic_Components --
10208 ----------------------------------
10209
10210 function Object_Has_Atomic_Components (N : Node_Id) return Boolean is
10211 begin
10212 if Has_Atomic_Components (Etype (N))
10213 or else Is_Atomic (Etype (N))
10214 then
10215 return True;
10216
10217 elsif Is_Entity_Name (N)
10218 and then (Has_Atomic_Components (Entity (N))
10219 or else Is_Atomic (Entity (N)))
10220 then
10221 return True;
10222
10223 elsif Nkind (N) = N_Selected_Component
10224 and then Is_Atomic (Entity (Selector_Name (N)))
10225 then
10226 return True;
10227
10228 elsif Nkind (N) = N_Indexed_Component
10229 or else Nkind (N) = N_Selected_Component
10230 then
10231 return Is_Atomic_Prefix (Prefix (N));
10232
10233 else
10234 return False;
10235 end if;
10236 end Object_Has_Atomic_Components;
10237
10238 -- Start of processing for Is_Atomic_Object
10239
10240 begin
10241 -- Predicate is not relevant to subprograms
10242
10243 if Is_Entity_Name (N) and then Is_Overloadable (Entity (N)) then
10244 return False;
10245
10246 elsif Is_Atomic (Etype (N))
10247 or else (Is_Entity_Name (N) and then Is_Atomic (Entity (N)))
10248 then
10249 return True;
10250
10251 elsif Nkind (N) = N_Selected_Component
10252 and then Is_Atomic (Entity (Selector_Name (N)))
10253 then
10254 return True;
10255
10256 elsif Nkind (N) = N_Indexed_Component
10257 or else Nkind (N) = N_Selected_Component
10258 then
10259 return Is_Atomic_Prefix (Prefix (N));
10260
10261 else
10262 return False;
10263 end if;
10264 end Is_Atomic_Object;
10265
10266 -------------------------
10267 -- Is_Attribute_Result --
10268 -------------------------
10269
10270 function Is_Attribute_Result (N : Node_Id) return Boolean is
10271 begin
10272 return Nkind (N) = N_Attribute_Reference
10273 and then Attribute_Name (N) = Name_Result;
10274 end Is_Attribute_Result;
10275
10276 ------------------------------------
10277 -- Is_Body_Or_Package_Declaration --
10278 ------------------------------------
10279
10280 function Is_Body_Or_Package_Declaration (N : Node_Id) return Boolean is
10281 begin
10282 return Nkind_In (N, N_Entry_Body,
10283 N_Package_Body,
10284 N_Package_Declaration,
10285 N_Protected_Body,
10286 N_Subprogram_Body,
10287 N_Task_Body);
10288 end Is_Body_Or_Package_Declaration;
10289
10290 -----------------------
10291 -- Is_Bounded_String --
10292 -----------------------
10293
10294 function Is_Bounded_String (T : Entity_Id) return Boolean is
10295 Under : constant Entity_Id := Underlying_Type (Root_Type (T));
10296
10297 begin
10298 -- Check whether T is ultimately derived from Ada.Strings.Superbounded.
10299 -- Super_String, or one of the [Wide_]Wide_ versions. This will
10300 -- be True for all the Bounded_String types in instances of the
10301 -- Generic_Bounded_Length generics, and for types derived from those.
10302
10303 return Present (Under)
10304 and then (Is_RTE (Root_Type (Under), RO_SU_Super_String) or else
10305 Is_RTE (Root_Type (Under), RO_WI_Super_String) or else
10306 Is_RTE (Root_Type (Under), RO_WW_Super_String));
10307 end Is_Bounded_String;
10308
10309 -------------------------
10310 -- Is_Child_Or_Sibling --
10311 -------------------------
10312
10313 function Is_Child_Or_Sibling
10314 (Pack_1 : Entity_Id;
10315 Pack_2 : Entity_Id) return Boolean
10316 is
10317 function Distance_From_Standard (Pack : Entity_Id) return Nat;
10318 -- Given an arbitrary package, return the number of "climbs" necessary
10319 -- to reach scope Standard_Standard.
10320
10321 procedure Equalize_Depths
10322 (Pack : in out Entity_Id;
10323 Depth : in out Nat;
10324 Depth_To_Reach : Nat);
10325 -- Given an arbitrary package, its depth and a target depth to reach,
10326 -- climb the scope chain until the said depth is reached. The pointer
10327 -- to the package and its depth a modified during the climb.
10328
10329 ----------------------------
10330 -- Distance_From_Standard --
10331 ----------------------------
10332
10333 function Distance_From_Standard (Pack : Entity_Id) return Nat is
10334 Dist : Nat;
10335 Scop : Entity_Id;
10336
10337 begin
10338 Dist := 0;
10339 Scop := Pack;
10340 while Present (Scop) and then Scop /= Standard_Standard loop
10341 Dist := Dist + 1;
10342 Scop := Scope (Scop);
10343 end loop;
10344
10345 return Dist;
10346 end Distance_From_Standard;
10347
10348 ---------------------
10349 -- Equalize_Depths --
10350 ---------------------
10351
10352 procedure Equalize_Depths
10353 (Pack : in out Entity_Id;
10354 Depth : in out Nat;
10355 Depth_To_Reach : Nat)
10356 is
10357 begin
10358 -- The package must be at a greater or equal depth
10359
10360 if Depth < Depth_To_Reach then
10361 raise Program_Error;
10362 end if;
10363
10364 -- Climb the scope chain until the desired depth is reached
10365
10366 while Present (Pack) and then Depth /= Depth_To_Reach loop
10367 Pack := Scope (Pack);
10368 Depth := Depth - 1;
10369 end loop;
10370 end Equalize_Depths;
10371
10372 -- Local variables
10373
10374 P_1 : Entity_Id := Pack_1;
10375 P_1_Child : Boolean := False;
10376 P_1_Depth : Nat := Distance_From_Standard (P_1);
10377 P_2 : Entity_Id := Pack_2;
10378 P_2_Child : Boolean := False;
10379 P_2_Depth : Nat := Distance_From_Standard (P_2);
10380
10381 -- Start of processing for Is_Child_Or_Sibling
10382
10383 begin
10384 pragma Assert
10385 (Ekind (Pack_1) = E_Package and then Ekind (Pack_2) = E_Package);
10386
10387 -- Both packages denote the same entity, therefore they cannot be
10388 -- children or siblings.
10389
10390 if P_1 = P_2 then
10391 return False;
10392
10393 -- One of the packages is at a deeper level than the other. Note that
10394 -- both may still come from differen hierarchies.
10395
10396 -- (root) P_2
10397 -- / \ :
10398 -- X P_2 or X
10399 -- : :
10400 -- P_1 P_1
10401
10402 elsif P_1_Depth > P_2_Depth then
10403 Equalize_Depths
10404 (Pack => P_1,
10405 Depth => P_1_Depth,
10406 Depth_To_Reach => P_2_Depth);
10407 P_1_Child := True;
10408
10409 -- (root) P_1
10410 -- / \ :
10411 -- P_1 X or X
10412 -- : :
10413 -- P_2 P_2
10414
10415 elsif P_2_Depth > P_1_Depth then
10416 Equalize_Depths
10417 (Pack => P_2,
10418 Depth => P_2_Depth,
10419 Depth_To_Reach => P_1_Depth);
10420 P_2_Child := True;
10421 end if;
10422
10423 -- At this stage the package pointers have been elevated to the same
10424 -- depth. If the related entities are the same, then one package is a
10425 -- potential child of the other:
10426
10427 -- P_1
10428 -- :
10429 -- X became P_1 P_2 or vica versa
10430 -- :
10431 -- P_2
10432
10433 if P_1 = P_2 then
10434 if P_1_Child then
10435 return Is_Child_Unit (Pack_1);
10436
10437 else pragma Assert (P_2_Child);
10438 return Is_Child_Unit (Pack_2);
10439 end if;
10440
10441 -- The packages may come from the same package chain or from entirely
10442 -- different hierarcies. To determine this, climb the scope stack until
10443 -- a common root is found.
10444
10445 -- (root) (root 1) (root 2)
10446 -- / \ | |
10447 -- P_1 P_2 P_1 P_2
10448
10449 else
10450 while Present (P_1) and then Present (P_2) loop
10451
10452 -- The two packages may be siblings
10453
10454 if P_1 = P_2 then
10455 return Is_Child_Unit (Pack_1) and then Is_Child_Unit (Pack_2);
10456 end if;
10457
10458 P_1 := Scope (P_1);
10459 P_2 := Scope (P_2);
10460 end loop;
10461 end if;
10462
10463 return False;
10464 end Is_Child_Or_Sibling;
10465
10466 -----------------------------
10467 -- Is_Concurrent_Interface --
10468 -----------------------------
10469
10470 function Is_Concurrent_Interface (T : Entity_Id) return Boolean is
10471 begin
10472 return Is_Interface (T)
10473 and then
10474 (Is_Protected_Interface (T)
10475 or else Is_Synchronized_Interface (T)
10476 or else Is_Task_Interface (T));
10477 end Is_Concurrent_Interface;
10478
10479 -----------------------
10480 -- Is_Constant_Bound --
10481 -----------------------
10482
10483 function Is_Constant_Bound (Exp : Node_Id) return Boolean is
10484 begin
10485 if Compile_Time_Known_Value (Exp) then
10486 return True;
10487
10488 elsif Is_Entity_Name (Exp) and then Present (Entity (Exp)) then
10489 return Is_Constant_Object (Entity (Exp))
10490 or else Ekind (Entity (Exp)) = E_Enumeration_Literal;
10491
10492 elsif Nkind (Exp) in N_Binary_Op then
10493 return Is_Constant_Bound (Left_Opnd (Exp))
10494 and then Is_Constant_Bound (Right_Opnd (Exp))
10495 and then Scope (Entity (Exp)) = Standard_Standard;
10496
10497 else
10498 return False;
10499 end if;
10500 end Is_Constant_Bound;
10501
10502 ---------------------------
10503 -- Is_Container_Element --
10504 ---------------------------
10505
10506 function Is_Container_Element (Exp : Node_Id) return Boolean is
10507 Loc : constant Source_Ptr := Sloc (Exp);
10508 Pref : constant Node_Id := Prefix (Exp);
10509
10510 Call : Node_Id;
10511 -- Call to an indexing aspect
10512
10513 Cont_Typ : Entity_Id;
10514 -- The type of the container being accessed
10515
10516 Elem_Typ : Entity_Id;
10517 -- Its element type
10518
10519 Indexing : Entity_Id;
10520 Is_Const : Boolean;
10521 -- Indicates that constant indexing is used, and the element is thus
10522 -- a constant.
10523
10524 Ref_Typ : Entity_Id;
10525 -- The reference type returned by the indexing operation
10526
10527 begin
10528 -- If C is a container, in a context that imposes the element type of
10529 -- that container, the indexing notation C (X) is rewritten as:
10530
10531 -- Indexing (C, X).Discr.all
10532
10533 -- where Indexing is one of the indexing aspects of the container.
10534 -- If the context does not require a reference, the construct can be
10535 -- rewritten as
10536
10537 -- Element (C, X)
10538
10539 -- First, verify that the construct has the proper form
10540
10541 if not Expander_Active then
10542 return False;
10543
10544 elsif Nkind (Pref) /= N_Selected_Component then
10545 return False;
10546
10547 elsif Nkind (Prefix (Pref)) /= N_Function_Call then
10548 return False;
10549
10550 else
10551 Call := Prefix (Pref);
10552 Ref_Typ := Etype (Call);
10553 end if;
10554
10555 if not Has_Implicit_Dereference (Ref_Typ)
10556 or else No (First (Parameter_Associations (Call)))
10557 or else not Is_Entity_Name (Name (Call))
10558 then
10559 return False;
10560 end if;
10561
10562 -- Retrieve type of container object, and its iterator aspects
10563
10564 Cont_Typ := Etype (First (Parameter_Associations (Call)));
10565 Indexing := Find_Value_Of_Aspect (Cont_Typ, Aspect_Constant_Indexing);
10566 Is_Const := False;
10567
10568 if No (Indexing) then
10569
10570 -- Container should have at least one indexing operation
10571
10572 return False;
10573
10574 elsif Entity (Name (Call)) /= Entity (Indexing) then
10575
10576 -- This may be a variable indexing operation
10577
10578 Indexing := Find_Value_Of_Aspect (Cont_Typ, Aspect_Variable_Indexing);
10579
10580 if No (Indexing)
10581 or else Entity (Name (Call)) /= Entity (Indexing)
10582 then
10583 return False;
10584 end if;
10585
10586 else
10587 Is_Const := True;
10588 end if;
10589
10590 Elem_Typ := Find_Value_Of_Aspect (Cont_Typ, Aspect_Iterator_Element);
10591
10592 if No (Elem_Typ) or else Entity (Elem_Typ) /= Etype (Exp) then
10593 return False;
10594 end if;
10595
10596 -- Check that the expression is not the target of an assignment, in
10597 -- which case the rewriting is not possible.
10598
10599 if not Is_Const then
10600 declare
10601 Par : Node_Id;
10602
10603 begin
10604 Par := Exp;
10605 while Present (Par)
10606 loop
10607 if Nkind (Parent (Par)) = N_Assignment_Statement
10608 and then Par = Name (Parent (Par))
10609 then
10610 return False;
10611
10612 -- A renaming produces a reference, and the transformation
10613 -- does not apply.
10614
10615 elsif Nkind (Parent (Par)) = N_Object_Renaming_Declaration then
10616 return False;
10617
10618 elsif Nkind_In
10619 (Nkind (Parent (Par)), N_Function_Call,
10620 N_Procedure_Call_Statement,
10621 N_Entry_Call_Statement)
10622 then
10623 -- Check that the element is not part of an actual for an
10624 -- in-out parameter.
10625
10626 declare
10627 F : Entity_Id;
10628 A : Node_Id;
10629
10630 begin
10631 F := First_Formal (Entity (Name (Parent (Par))));
10632 A := First (Parameter_Associations (Parent (Par)));
10633 while Present (F) loop
10634 if A = Par and then Ekind (F) /= E_In_Parameter then
10635 return False;
10636 end if;
10637
10638 Next_Formal (F);
10639 Next (A);
10640 end loop;
10641 end;
10642
10643 -- E_In_Parameter in a call: element is not modified.
10644
10645 exit;
10646 end if;
10647
10648 Par := Parent (Par);
10649 end loop;
10650 end;
10651 end if;
10652
10653 -- The expression has the proper form and the context requires the
10654 -- element type. Retrieve the Element function of the container and
10655 -- rewrite the construct as a call to it.
10656
10657 declare
10658 Op : Elmt_Id;
10659
10660 begin
10661 Op := First_Elmt (Primitive_Operations (Cont_Typ));
10662 while Present (Op) loop
10663 exit when Chars (Node (Op)) = Name_Element;
10664 Next_Elmt (Op);
10665 end loop;
10666
10667 if No (Op) then
10668 return False;
10669
10670 else
10671 Rewrite (Exp,
10672 Make_Function_Call (Loc,
10673 Name => New_Occurrence_Of (Node (Op), Loc),
10674 Parameter_Associations => Parameter_Associations (Call)));
10675 Analyze_And_Resolve (Exp, Entity (Elem_Typ));
10676 return True;
10677 end if;
10678 end;
10679 end Is_Container_Element;
10680
10681 ----------------------------
10682 -- Is_Contract_Annotation --
10683 ----------------------------
10684
10685 function Is_Contract_Annotation (Item : Node_Id) return Boolean is
10686 begin
10687 return Is_Package_Contract_Annotation (Item)
10688 or else
10689 Is_Subprogram_Contract_Annotation (Item);
10690 end Is_Contract_Annotation;
10691
10692 --------------------------------------
10693 -- Is_Controlling_Limited_Procedure --
10694 --------------------------------------
10695
10696 function Is_Controlling_Limited_Procedure
10697 (Proc_Nam : Entity_Id) return Boolean
10698 is
10699 Param_Typ : Entity_Id := Empty;
10700
10701 begin
10702 if Ekind (Proc_Nam) = E_Procedure
10703 and then Present (Parameter_Specifications (Parent (Proc_Nam)))
10704 then
10705 Param_Typ := Etype (Parameter_Type (First (
10706 Parameter_Specifications (Parent (Proc_Nam)))));
10707
10708 -- In this case where an Itype was created, the procedure call has been
10709 -- rewritten.
10710
10711 elsif Present (Associated_Node_For_Itype (Proc_Nam))
10712 and then Present (Original_Node (Associated_Node_For_Itype (Proc_Nam)))
10713 and then
10714 Present (Parameter_Associations
10715 (Associated_Node_For_Itype (Proc_Nam)))
10716 then
10717 Param_Typ :=
10718 Etype (First (Parameter_Associations
10719 (Associated_Node_For_Itype (Proc_Nam))));
10720 end if;
10721
10722 if Present (Param_Typ) then
10723 return
10724 Is_Interface (Param_Typ)
10725 and then Is_Limited_Record (Param_Typ);
10726 end if;
10727
10728 return False;
10729 end Is_Controlling_Limited_Procedure;
10730
10731 -----------------------------
10732 -- Is_CPP_Constructor_Call --
10733 -----------------------------
10734
10735 function Is_CPP_Constructor_Call (N : Node_Id) return Boolean is
10736 begin
10737 return Nkind (N) = N_Function_Call
10738 and then Is_CPP_Class (Etype (Etype (N)))
10739 and then Is_Constructor (Entity (Name (N)))
10740 and then Is_Imported (Entity (Name (N)));
10741 end Is_CPP_Constructor_Call;
10742
10743 --------------------
10744 -- Is_Declaration --
10745 --------------------
10746
10747 function Is_Declaration (N : Node_Id) return Boolean is
10748 begin
10749 case Nkind (N) is
10750 when N_Abstract_Subprogram_Declaration |
10751 N_Exception_Declaration |
10752 N_Exception_Renaming_Declaration |
10753 N_Full_Type_Declaration |
10754 N_Generic_Function_Renaming_Declaration |
10755 N_Generic_Package_Declaration |
10756 N_Generic_Package_Renaming_Declaration |
10757 N_Generic_Procedure_Renaming_Declaration |
10758 N_Generic_Subprogram_Declaration |
10759 N_Number_Declaration |
10760 N_Object_Declaration |
10761 N_Object_Renaming_Declaration |
10762 N_Package_Declaration |
10763 N_Package_Renaming_Declaration |
10764 N_Private_Extension_Declaration |
10765 N_Private_Type_Declaration |
10766 N_Subprogram_Declaration |
10767 N_Subprogram_Renaming_Declaration |
10768 N_Subtype_Declaration =>
10769 return True;
10770
10771 when others =>
10772 return False;
10773 end case;
10774 end Is_Declaration;
10775
10776 -----------------
10777 -- Is_Delegate --
10778 -----------------
10779
10780 function Is_Delegate (T : Entity_Id) return Boolean is
10781 Desig_Type : Entity_Id;
10782
10783 begin
10784 if VM_Target /= CLI_Target then
10785 return False;
10786 end if;
10787
10788 -- Access-to-subprograms are delegates in CIL
10789
10790 if Ekind (T) = E_Access_Subprogram_Type then
10791 return True;
10792 end if;
10793
10794 if not Is_Access_Type (T) then
10795
10796 -- A delegate is a managed pointer. If no designated type is defined
10797 -- it means that it's not a delegate.
10798
10799 return False;
10800 end if;
10801
10802 Desig_Type := Etype (Directly_Designated_Type (T));
10803
10804 if not Is_Tagged_Type (Desig_Type) then
10805 return False;
10806 end if;
10807
10808 -- Test if the type is inherited from [mscorlib]System.Delegate
10809
10810 while Etype (Desig_Type) /= Desig_Type loop
10811 if Chars (Scope (Desig_Type)) /= No_Name
10812 and then Is_Imported (Scope (Desig_Type))
10813 and then Get_Name_String (Chars (Scope (Desig_Type))) = "delegate"
10814 then
10815 return True;
10816 end if;
10817
10818 Desig_Type := Etype (Desig_Type);
10819 end loop;
10820
10821 return False;
10822 end Is_Delegate;
10823
10824 ----------------------------------------------
10825 -- Is_Dependent_Component_Of_Mutable_Object --
10826 ----------------------------------------------
10827
10828 function Is_Dependent_Component_Of_Mutable_Object
10829 (Object : Node_Id) return Boolean
10830 is
10831 function Is_Declared_Within_Variant (Comp : Entity_Id) return Boolean;
10832 -- Returns True if and only if Comp is declared within a variant part
10833
10834 --------------------------------
10835 -- Is_Declared_Within_Variant --
10836 --------------------------------
10837
10838 function Is_Declared_Within_Variant (Comp : Entity_Id) return Boolean is
10839 Comp_Decl : constant Node_Id := Parent (Comp);
10840 Comp_List : constant Node_Id := Parent (Comp_Decl);
10841 begin
10842 return Nkind (Parent (Comp_List)) = N_Variant;
10843 end Is_Declared_Within_Variant;
10844
10845 P : Node_Id;
10846 Prefix_Type : Entity_Id;
10847 P_Aliased : Boolean := False;
10848 Comp : Entity_Id;
10849
10850 Deref : Node_Id := Object;
10851 -- Dereference node, in something like X.all.Y(2)
10852
10853 -- Start of processing for Is_Dependent_Component_Of_Mutable_Object
10854
10855 begin
10856 -- Find the dereference node if any
10857
10858 while Nkind_In (Deref, N_Indexed_Component,
10859 N_Selected_Component,
10860 N_Slice)
10861 loop
10862 Deref := Prefix (Deref);
10863 end loop;
10864
10865 -- Ada 2005: If we have a component or slice of a dereference,
10866 -- something like X.all.Y (2), and the type of X is access-to-constant,
10867 -- Is_Variable will return False, because it is indeed a constant
10868 -- view. But it might be a view of a variable object, so we want the
10869 -- following condition to be True in that case.
10870
10871 if Is_Variable (Object)
10872 or else (Ada_Version >= Ada_2005
10873 and then Nkind (Deref) = N_Explicit_Dereference)
10874 then
10875 if Nkind (Object) = N_Selected_Component then
10876 P := Prefix (Object);
10877 Prefix_Type := Etype (P);
10878
10879 if Is_Entity_Name (P) then
10880 if Ekind (Entity (P)) = E_Generic_In_Out_Parameter then
10881 Prefix_Type := Base_Type (Prefix_Type);
10882 end if;
10883
10884 if Is_Aliased (Entity (P)) then
10885 P_Aliased := True;
10886 end if;
10887
10888 -- A discriminant check on a selected component may be expanded
10889 -- into a dereference when removing side-effects. Recover the
10890 -- original node and its type, which may be unconstrained.
10891
10892 elsif Nkind (P) = N_Explicit_Dereference
10893 and then not (Comes_From_Source (P))
10894 then
10895 P := Original_Node (P);
10896 Prefix_Type := Etype (P);
10897
10898 else
10899 -- Check for prefix being an aliased component???
10900
10901 null;
10902
10903 end if;
10904
10905 -- A heap object is constrained by its initial value
10906
10907 -- Ada 2005 (AI-363): Always assume the object could be mutable in
10908 -- the dereferenced case, since the access value might denote an
10909 -- unconstrained aliased object, whereas in Ada 95 the designated
10910 -- object is guaranteed to be constrained. A worst-case assumption
10911 -- has to apply in Ada 2005 because we can't tell at compile
10912 -- time whether the object is "constrained by its initial value"
10913 -- (despite the fact that 3.10.2(26/2) and 8.5.1(5/2) are semantic
10914 -- rules (these rules are acknowledged to need fixing).
10915
10916 if Ada_Version < Ada_2005 then
10917 if Is_Access_Type (Prefix_Type)
10918 or else Nkind (P) = N_Explicit_Dereference
10919 then
10920 return False;
10921 end if;
10922
10923 else pragma Assert (Ada_Version >= Ada_2005);
10924 if Is_Access_Type (Prefix_Type) then
10925
10926 -- If the access type is pool-specific, and there is no
10927 -- constrained partial view of the designated type, then the
10928 -- designated object is known to be constrained.
10929
10930 if Ekind (Prefix_Type) = E_Access_Type
10931 and then not Object_Type_Has_Constrained_Partial_View
10932 (Typ => Designated_Type (Prefix_Type),
10933 Scop => Current_Scope)
10934 then
10935 return False;
10936
10937 -- Otherwise (general access type, or there is a constrained
10938 -- partial view of the designated type), we need to check
10939 -- based on the designated type.
10940
10941 else
10942 Prefix_Type := Designated_Type (Prefix_Type);
10943 end if;
10944 end if;
10945 end if;
10946
10947 Comp :=
10948 Original_Record_Component (Entity (Selector_Name (Object)));
10949
10950 -- As per AI-0017, the renaming is illegal in a generic body, even
10951 -- if the subtype is indefinite.
10952
10953 -- Ada 2005 (AI-363): In Ada 2005 an aliased object can be mutable
10954
10955 if not Is_Constrained (Prefix_Type)
10956 and then (not Is_Indefinite_Subtype (Prefix_Type)
10957 or else
10958 (Is_Generic_Type (Prefix_Type)
10959 and then Ekind (Current_Scope) = E_Generic_Package
10960 and then In_Package_Body (Current_Scope)))
10961
10962 and then (Is_Declared_Within_Variant (Comp)
10963 or else Has_Discriminant_Dependent_Constraint (Comp))
10964 and then (not P_Aliased or else Ada_Version >= Ada_2005)
10965 then
10966 return True;
10967
10968 -- If the prefix is of an access type at this point, then we want
10969 -- to return False, rather than calling this function recursively
10970 -- on the access object (which itself might be a discriminant-
10971 -- dependent component of some other object, but that isn't
10972 -- relevant to checking the object passed to us). This avoids
10973 -- issuing wrong errors when compiling with -gnatc, where there
10974 -- can be implicit dereferences that have not been expanded.
10975
10976 elsif Is_Access_Type (Etype (Prefix (Object))) then
10977 return False;
10978
10979 else
10980 return
10981 Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
10982 end if;
10983
10984 elsif Nkind (Object) = N_Indexed_Component
10985 or else Nkind (Object) = N_Slice
10986 then
10987 return Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
10988
10989 -- A type conversion that Is_Variable is a view conversion:
10990 -- go back to the denoted object.
10991
10992 elsif Nkind (Object) = N_Type_Conversion then
10993 return
10994 Is_Dependent_Component_Of_Mutable_Object (Expression (Object));
10995 end if;
10996 end if;
10997
10998 return False;
10999 end Is_Dependent_Component_Of_Mutable_Object;
11000
11001 ---------------------
11002 -- Is_Dereferenced --
11003 ---------------------
11004
11005 function Is_Dereferenced (N : Node_Id) return Boolean is
11006 P : constant Node_Id := Parent (N);
11007 begin
11008 return Nkind_In (P, N_Selected_Component,
11009 N_Explicit_Dereference,
11010 N_Indexed_Component,
11011 N_Slice)
11012 and then Prefix (P) = N;
11013 end Is_Dereferenced;
11014
11015 ----------------------
11016 -- Is_Descendent_Of --
11017 ----------------------
11018
11019 function Is_Descendent_Of (T1 : Entity_Id; T2 : Entity_Id) return Boolean is
11020 T : Entity_Id;
11021 Etyp : Entity_Id;
11022
11023 begin
11024 pragma Assert (Nkind (T1) in N_Entity);
11025 pragma Assert (Nkind (T2) in N_Entity);
11026
11027 T := Base_Type (T1);
11028
11029 -- Immediate return if the types match
11030
11031 if T = T2 then
11032 return True;
11033
11034 -- Comment needed here ???
11035
11036 elsif Ekind (T) = E_Class_Wide_Type then
11037 return Etype (T) = T2;
11038
11039 -- All other cases
11040
11041 else
11042 loop
11043 Etyp := Etype (T);
11044
11045 -- Done if we found the type we are looking for
11046
11047 if Etyp = T2 then
11048 return True;
11049
11050 -- Done if no more derivations to check
11051
11052 elsif T = T1
11053 or else T = Etyp
11054 then
11055 return False;
11056
11057 -- Following test catches error cases resulting from prev errors
11058
11059 elsif No (Etyp) then
11060 return False;
11061
11062 elsif Is_Private_Type (T) and then Etyp = Full_View (T) then
11063 return False;
11064
11065 elsif Is_Private_Type (Etyp) and then Full_View (Etyp) = T then
11066 return False;
11067 end if;
11068
11069 T := Base_Type (Etyp);
11070 end loop;
11071 end if;
11072 end Is_Descendent_Of;
11073
11074 -----------------------------
11075 -- Is_Effectively_Volatile --
11076 -----------------------------
11077
11078 function Is_Effectively_Volatile (Id : Entity_Id) return Boolean is
11079 begin
11080 if Is_Type (Id) then
11081
11082 -- An arbitrary type is effectively volatile when it is subject to
11083 -- pragma Atomic or Volatile.
11084
11085 if Is_Volatile (Id) then
11086 return True;
11087
11088 -- An array type is effectively volatile when it is subject to pragma
11089 -- Atomic_Components or Volatile_Components or its compolent type is
11090 -- effectively volatile.
11091
11092 elsif Is_Array_Type (Id) then
11093 return
11094 Has_Volatile_Components (Id)
11095 or else
11096 Is_Effectively_Volatile (Component_Type (Base_Type (Id)));
11097
11098 else
11099 return False;
11100 end if;
11101
11102 -- Otherwise Id denotes an object
11103
11104 else
11105 return
11106 Is_Volatile (Id)
11107 or else Has_Volatile_Components (Id)
11108 or else Is_Effectively_Volatile (Etype (Id));
11109 end if;
11110 end Is_Effectively_Volatile;
11111
11112 ------------------------------------
11113 -- Is_Effectively_Volatile_Object --
11114 ------------------------------------
11115
11116 function Is_Effectively_Volatile_Object (N : Node_Id) return Boolean is
11117 begin
11118 if Is_Entity_Name (N) then
11119 return Is_Effectively_Volatile (Entity (N));
11120
11121 elsif Nkind (N) = N_Expanded_Name then
11122 return Is_Effectively_Volatile (Entity (N));
11123
11124 elsif Nkind (N) = N_Indexed_Component then
11125 return Is_Effectively_Volatile_Object (Prefix (N));
11126
11127 elsif Nkind (N) = N_Selected_Component then
11128 return
11129 Is_Effectively_Volatile_Object (Prefix (N))
11130 or else
11131 Is_Effectively_Volatile_Object (Selector_Name (N));
11132
11133 else
11134 return False;
11135 end if;
11136 end Is_Effectively_Volatile_Object;
11137
11138 ----------------------------
11139 -- Is_Expression_Function --
11140 ----------------------------
11141
11142 function Is_Expression_Function (Subp : Entity_Id) return Boolean is
11143 Decl : Node_Id;
11144
11145 begin
11146 if Ekind (Subp) /= E_Function then
11147 return False;
11148
11149 else
11150 Decl := Unit_Declaration_Node (Subp);
11151 return Nkind (Decl) = N_Subprogram_Declaration
11152 and then
11153 (Nkind (Original_Node (Decl)) = N_Expression_Function
11154 or else
11155 (Present (Corresponding_Body (Decl))
11156 and then
11157 Nkind (Original_Node
11158 (Unit_Declaration_Node
11159 (Corresponding_Body (Decl)))) =
11160 N_Expression_Function));
11161 end if;
11162 end Is_Expression_Function;
11163
11164 -----------------------
11165 -- Is_EVF_Expression --
11166 -----------------------
11167
11168 function Is_EVF_Expression (N : Node_Id) return Boolean is
11169 Orig_N : constant Node_Id := Original_Node (N);
11170 Alt : Node_Id;
11171 Expr : Node_Id;
11172 Id : Entity_Id;
11173
11174 begin
11175 -- Detect a reference to a formal parameter of a specific tagged type
11176 -- whose related subprogram is subject to pragma Expresions_Visible with
11177 -- value "False".
11178
11179 if Is_Entity_Name (N) and then Present (Entity (N)) then
11180 Id := Entity (N);
11181
11182 return
11183 Is_Formal (Id)
11184 and then Is_Specific_Tagged_Type (Etype (Id))
11185 and then Extensions_Visible_Status (Id) =
11186 Extensions_Visible_False;
11187
11188 -- A case expression is an EVF expression when it contains at least one
11189 -- EVF dependent_expression. Note that a case expression may have been
11190 -- expanded, hence the use of Original_Node.
11191
11192 elsif Nkind (Orig_N) = N_Case_Expression then
11193 Alt := First (Alternatives (Orig_N));
11194 while Present (Alt) loop
11195 if Is_EVF_Expression (Expression (Alt)) then
11196 return True;
11197 end if;
11198
11199 Next (Alt);
11200 end loop;
11201
11202 -- An if expression is an EVF expression when it contains at least one
11203 -- EVF dependent_expression. Note that an if expression may have been
11204 -- expanded, hence the use of Original_Node.
11205
11206 elsif Nkind (Orig_N) = N_If_Expression then
11207 Expr := Next (First (Expressions (Orig_N)));
11208 while Present (Expr) loop
11209 if Is_EVF_Expression (Expr) then
11210 return True;
11211 end if;
11212
11213 Next (Expr);
11214 end loop;
11215
11216 -- A qualified expression or a type conversion is an EVF expression when
11217 -- its operand is an EVF expression.
11218
11219 elsif Nkind_In (N, N_Qualified_Expression,
11220 N_Unchecked_Type_Conversion,
11221 N_Type_Conversion)
11222 then
11223 return Is_EVF_Expression (Expression (N));
11224
11225 -- Attributes 'Loop_Entry, 'Old and 'Update are an EVF expression when
11226 -- their prefix denotes an EVF expression.
11227
11228 elsif Nkind (N) = N_Attribute_Reference
11229 and then Nam_In (Attribute_Name (N), Name_Loop_Entry,
11230 Name_Old,
11231 Name_Update)
11232 then
11233 return Is_EVF_Expression (Prefix (N));
11234 end if;
11235
11236 return False;
11237 end Is_EVF_Expression;
11238
11239 --------------
11240 -- Is_False --
11241 --------------
11242
11243 function Is_False (U : Uint) return Boolean is
11244 begin
11245 return (U = 0);
11246 end Is_False;
11247
11248 ---------------------------
11249 -- Is_Fixed_Model_Number --
11250 ---------------------------
11251
11252 function Is_Fixed_Model_Number (U : Ureal; T : Entity_Id) return Boolean is
11253 S : constant Ureal := Small_Value (T);
11254 M : Urealp.Save_Mark;
11255 R : Boolean;
11256 begin
11257 M := Urealp.Mark;
11258 R := (U = UR_Trunc (U / S) * S);
11259 Urealp.Release (M);
11260 return R;
11261 end Is_Fixed_Model_Number;
11262
11263 -------------------------------
11264 -- Is_Fully_Initialized_Type --
11265 -------------------------------
11266
11267 function Is_Fully_Initialized_Type (Typ : Entity_Id) return Boolean is
11268 begin
11269 -- Scalar types
11270
11271 if Is_Scalar_Type (Typ) then
11272
11273 -- A scalar type with an aspect Default_Value is fully initialized
11274
11275 -- Note: Iniitalize/Normalize_Scalars also ensure full initialization
11276 -- of a scalar type, but we don't take that into account here, since
11277 -- we don't want these to affect warnings.
11278
11279 return Has_Default_Aspect (Typ);
11280
11281 elsif Is_Access_Type (Typ) then
11282 return True;
11283
11284 elsif Is_Array_Type (Typ) then
11285 if Is_Fully_Initialized_Type (Component_Type (Typ))
11286 or else (Ada_Version >= Ada_2012 and then Has_Default_Aspect (Typ))
11287 then
11288 return True;
11289 end if;
11290
11291 -- An interesting case, if we have a constrained type one of whose
11292 -- bounds is known to be null, then there are no elements to be
11293 -- initialized, so all the elements are initialized.
11294
11295 if Is_Constrained (Typ) then
11296 declare
11297 Indx : Node_Id;
11298 Indx_Typ : Entity_Id;
11299 Lbd, Hbd : Node_Id;
11300
11301 begin
11302 Indx := First_Index (Typ);
11303 while Present (Indx) loop
11304 if Etype (Indx) = Any_Type then
11305 return False;
11306
11307 -- If index is a range, use directly
11308
11309 elsif Nkind (Indx) = N_Range then
11310 Lbd := Low_Bound (Indx);
11311 Hbd := High_Bound (Indx);
11312
11313 else
11314 Indx_Typ := Etype (Indx);
11315
11316 if Is_Private_Type (Indx_Typ) then
11317 Indx_Typ := Full_View (Indx_Typ);
11318 end if;
11319
11320 if No (Indx_Typ) or else Etype (Indx_Typ) = Any_Type then
11321 return False;
11322 else
11323 Lbd := Type_Low_Bound (Indx_Typ);
11324 Hbd := Type_High_Bound (Indx_Typ);
11325 end if;
11326 end if;
11327
11328 if Compile_Time_Known_Value (Lbd)
11329 and then
11330 Compile_Time_Known_Value (Hbd)
11331 then
11332 if Expr_Value (Hbd) < Expr_Value (Lbd) then
11333 return True;
11334 end if;
11335 end if;
11336
11337 Next_Index (Indx);
11338 end loop;
11339 end;
11340 end if;
11341
11342 -- If no null indexes, then type is not fully initialized
11343
11344 return False;
11345
11346 -- Record types
11347
11348 elsif Is_Record_Type (Typ) then
11349 if Has_Discriminants (Typ)
11350 and then
11351 Present (Discriminant_Default_Value (First_Discriminant (Typ)))
11352 and then Is_Fully_Initialized_Variant (Typ)
11353 then
11354 return True;
11355 end if;
11356
11357 -- We consider bounded string types to be fully initialized, because
11358 -- otherwise we get false alarms when the Data component is not
11359 -- default-initialized.
11360
11361 if Is_Bounded_String (Typ) then
11362 return True;
11363 end if;
11364
11365 -- Controlled records are considered to be fully initialized if
11366 -- there is a user defined Initialize routine. This may not be
11367 -- entirely correct, but as the spec notes, we are guessing here
11368 -- what is best from the point of view of issuing warnings.
11369
11370 if Is_Controlled (Typ) then
11371 declare
11372 Utyp : constant Entity_Id := Underlying_Type (Typ);
11373
11374 begin
11375 if Present (Utyp) then
11376 declare
11377 Init : constant Entity_Id :=
11378 (Find_Prim_Op
11379 (Underlying_Type (Typ), Name_Initialize));
11380
11381 begin
11382 if Present (Init)
11383 and then Comes_From_Source (Init)
11384 and then not
11385 Is_Predefined_File_Name
11386 (File_Name (Get_Source_File_Index (Sloc (Init))))
11387 then
11388 return True;
11389
11390 elsif Has_Null_Extension (Typ)
11391 and then
11392 Is_Fully_Initialized_Type
11393 (Etype (Base_Type (Typ)))
11394 then
11395 return True;
11396 end if;
11397 end;
11398 end if;
11399 end;
11400 end if;
11401
11402 -- Otherwise see if all record components are initialized
11403
11404 declare
11405 Ent : Entity_Id;
11406
11407 begin
11408 Ent := First_Entity (Typ);
11409 while Present (Ent) loop
11410 if Ekind (Ent) = E_Component
11411 and then (No (Parent (Ent))
11412 or else No (Expression (Parent (Ent))))
11413 and then not Is_Fully_Initialized_Type (Etype (Ent))
11414
11415 -- Special VM case for tag components, which need to be
11416 -- defined in this case, but are never initialized as VMs
11417 -- are using other dispatching mechanisms. Ignore this
11418 -- uninitialized case. Note that this applies both to the
11419 -- uTag entry and the main vtable pointer (CPP_Class case).
11420
11421 and then (Tagged_Type_Expansion or else not Is_Tag (Ent))
11422 then
11423 return False;
11424 end if;
11425
11426 Next_Entity (Ent);
11427 end loop;
11428 end;
11429
11430 -- No uninitialized components, so type is fully initialized.
11431 -- Note that this catches the case of no components as well.
11432
11433 return True;
11434
11435 elsif Is_Concurrent_Type (Typ) then
11436 return True;
11437
11438 elsif Is_Private_Type (Typ) then
11439 declare
11440 U : constant Entity_Id := Underlying_Type (Typ);
11441
11442 begin
11443 if No (U) then
11444 return False;
11445 else
11446 return Is_Fully_Initialized_Type (U);
11447 end if;
11448 end;
11449
11450 else
11451 return False;
11452 end if;
11453 end Is_Fully_Initialized_Type;
11454
11455 ----------------------------------
11456 -- Is_Fully_Initialized_Variant --
11457 ----------------------------------
11458
11459 function Is_Fully_Initialized_Variant (Typ : Entity_Id) return Boolean is
11460 Loc : constant Source_Ptr := Sloc (Typ);
11461 Constraints : constant List_Id := New_List;
11462 Components : constant Elist_Id := New_Elmt_List;
11463 Comp_Elmt : Elmt_Id;
11464 Comp_Id : Node_Id;
11465 Comp_List : Node_Id;
11466 Discr : Entity_Id;
11467 Discr_Val : Node_Id;
11468
11469 Report_Errors : Boolean;
11470 pragma Warnings (Off, Report_Errors);
11471
11472 begin
11473 if Serious_Errors_Detected > 0 then
11474 return False;
11475 end if;
11476
11477 if Is_Record_Type (Typ)
11478 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
11479 and then Nkind (Type_Definition (Parent (Typ))) = N_Record_Definition
11480 then
11481 Comp_List := Component_List (Type_Definition (Parent (Typ)));
11482
11483 Discr := First_Discriminant (Typ);
11484 while Present (Discr) loop
11485 if Nkind (Parent (Discr)) = N_Discriminant_Specification then
11486 Discr_Val := Expression (Parent (Discr));
11487
11488 if Present (Discr_Val)
11489 and then Is_OK_Static_Expression (Discr_Val)
11490 then
11491 Append_To (Constraints,
11492 Make_Component_Association (Loc,
11493 Choices => New_List (New_Occurrence_Of (Discr, Loc)),
11494 Expression => New_Copy (Discr_Val)));
11495 else
11496 return False;
11497 end if;
11498 else
11499 return False;
11500 end if;
11501
11502 Next_Discriminant (Discr);
11503 end loop;
11504
11505 Gather_Components
11506 (Typ => Typ,
11507 Comp_List => Comp_List,
11508 Governed_By => Constraints,
11509 Into => Components,
11510 Report_Errors => Report_Errors);
11511
11512 -- Check that each component present is fully initialized
11513
11514 Comp_Elmt := First_Elmt (Components);
11515 while Present (Comp_Elmt) loop
11516 Comp_Id := Node (Comp_Elmt);
11517
11518 if Ekind (Comp_Id) = E_Component
11519 and then (No (Parent (Comp_Id))
11520 or else No (Expression (Parent (Comp_Id))))
11521 and then not Is_Fully_Initialized_Type (Etype (Comp_Id))
11522 then
11523 return False;
11524 end if;
11525
11526 Next_Elmt (Comp_Elmt);
11527 end loop;
11528
11529 return True;
11530
11531 elsif Is_Private_Type (Typ) then
11532 declare
11533 U : constant Entity_Id := Underlying_Type (Typ);
11534
11535 begin
11536 if No (U) then
11537 return False;
11538 else
11539 return Is_Fully_Initialized_Variant (U);
11540 end if;
11541 end;
11542
11543 else
11544 return False;
11545 end if;
11546 end Is_Fully_Initialized_Variant;
11547
11548 ------------------------------------
11549 -- Is_Generic_Declaration_Or_Body --
11550 ------------------------------------
11551
11552 function Is_Generic_Declaration_Or_Body (Decl : Node_Id) return Boolean is
11553 Spec_Decl : Node_Id;
11554
11555 begin
11556 -- Package/subprogram body
11557
11558 if Nkind_In (Decl, N_Package_Body, N_Subprogram_Body)
11559 and then Present (Corresponding_Spec (Decl))
11560 then
11561 Spec_Decl := Unit_Declaration_Node (Corresponding_Spec (Decl));
11562
11563 -- Package/subprogram body stub
11564
11565 elsif Nkind_In (Decl, N_Package_Body_Stub, N_Subprogram_Body_Stub)
11566 and then Present (Corresponding_Spec_Of_Stub (Decl))
11567 then
11568 Spec_Decl :=
11569 Unit_Declaration_Node (Corresponding_Spec_Of_Stub (Decl));
11570
11571 -- All other cases
11572
11573 else
11574 Spec_Decl := Decl;
11575 end if;
11576
11577 -- Rather than inspecting the defining entity of the spec declaration,
11578 -- look at its Nkind. This takes care of the case where the analysis of
11579 -- a generic body modifies the Ekind of its spec to allow for recursive
11580 -- calls.
11581
11582 return
11583 Nkind_In (Spec_Decl, N_Generic_Package_Declaration,
11584 N_Generic_Subprogram_Declaration);
11585 end Is_Generic_Declaration_Or_Body;
11586
11587 ----------------------------
11588 -- Is_Inherited_Operation --
11589 ----------------------------
11590
11591 function Is_Inherited_Operation (E : Entity_Id) return Boolean is
11592 pragma Assert (Is_Overloadable (E));
11593 Kind : constant Node_Kind := Nkind (Parent (E));
11594 begin
11595 return Kind = N_Full_Type_Declaration
11596 or else Kind = N_Private_Extension_Declaration
11597 or else Kind = N_Subtype_Declaration
11598 or else (Ekind (E) = E_Enumeration_Literal
11599 and then Is_Derived_Type (Etype (E)));
11600 end Is_Inherited_Operation;
11601
11602 -------------------------------------
11603 -- Is_Inherited_Operation_For_Type --
11604 -------------------------------------
11605
11606 function Is_Inherited_Operation_For_Type
11607 (E : Entity_Id;
11608 Typ : Entity_Id) return Boolean
11609 is
11610 begin
11611 -- Check that the operation has been created by the type declaration
11612
11613 return Is_Inherited_Operation (E)
11614 and then Defining_Identifier (Parent (E)) = Typ;
11615 end Is_Inherited_Operation_For_Type;
11616
11617 -----------------
11618 -- Is_Iterator --
11619 -----------------
11620
11621 function Is_Iterator (Typ : Entity_Id) return Boolean is
11622 Ifaces_List : Elist_Id;
11623 Iface_Elmt : Elmt_Id;
11624 Iface : Entity_Id;
11625
11626 begin
11627 if Is_Class_Wide_Type (Typ)
11628 and then Nam_In (Chars (Etype (Typ)), Name_Forward_Iterator,
11629 Name_Reversible_Iterator)
11630 and then
11631 Is_Predefined_File_Name
11632 (Unit_File_Name (Get_Source_Unit (Etype (Typ))))
11633 then
11634 return True;
11635
11636 elsif not Is_Tagged_Type (Typ) or else not Is_Derived_Type (Typ) then
11637 return False;
11638
11639 elsif Present (Find_Value_Of_Aspect (Typ, Aspect_Iterable)) then
11640 return True;
11641
11642 else
11643 Collect_Interfaces (Typ, Ifaces_List);
11644
11645 Iface_Elmt := First_Elmt (Ifaces_List);
11646 while Present (Iface_Elmt) loop
11647 Iface := Node (Iface_Elmt);
11648 if Chars (Iface) = Name_Forward_Iterator
11649 and then
11650 Is_Predefined_File_Name
11651 (Unit_File_Name (Get_Source_Unit (Iface)))
11652 then
11653 return True;
11654 end if;
11655
11656 Next_Elmt (Iface_Elmt);
11657 end loop;
11658
11659 return False;
11660 end if;
11661 end Is_Iterator;
11662
11663 ------------
11664 -- Is_LHS --
11665 ------------
11666
11667 -- We seem to have a lot of overlapping functions that do similar things
11668 -- (testing for left hand sides or lvalues???).
11669
11670 function Is_LHS (N : Node_Id) return Is_LHS_Result is
11671 P : constant Node_Id := Parent (N);
11672
11673 begin
11674 -- Return True if we are the left hand side of an assignment statement
11675
11676 if Nkind (P) = N_Assignment_Statement then
11677 if Name (P) = N then
11678 return Yes;
11679 else
11680 return No;
11681 end if;
11682
11683 -- Case of prefix of indexed or selected component or slice
11684
11685 elsif Nkind_In (P, N_Indexed_Component, N_Selected_Component, N_Slice)
11686 and then N = Prefix (P)
11687 then
11688 -- Here we have the case where the parent P is N.Q or N(Q .. R).
11689 -- If P is an LHS, then N is also effectively an LHS, but there
11690 -- is an important exception. If N is of an access type, then
11691 -- what we really have is N.all.Q (or N.all(Q .. R)). In either
11692 -- case this makes N.all a left hand side but not N itself.
11693
11694 -- If we don't know the type yet, this is the case where we return
11695 -- Unknown, since the answer depends on the type which is unknown.
11696
11697 if No (Etype (N)) then
11698 return Unknown;
11699
11700 -- We have an Etype set, so we can check it
11701
11702 elsif Is_Access_Type (Etype (N)) then
11703 return No;
11704
11705 -- OK, not access type case, so just test whole expression
11706
11707 else
11708 return Is_LHS (P);
11709 end if;
11710
11711 -- All other cases are not left hand sides
11712
11713 else
11714 return No;
11715 end if;
11716 end Is_LHS;
11717
11718 -----------------------------
11719 -- Is_Library_Level_Entity --
11720 -----------------------------
11721
11722 function Is_Library_Level_Entity (E : Entity_Id) return Boolean is
11723 begin
11724 -- The following is a small optimization, and it also properly handles
11725 -- discriminals, which in task bodies might appear in expressions before
11726 -- the corresponding procedure has been created, and which therefore do
11727 -- not have an assigned scope.
11728
11729 if Is_Formal (E) then
11730 return False;
11731 end if;
11732
11733 -- Normal test is simply that the enclosing dynamic scope is Standard
11734
11735 return Enclosing_Dynamic_Scope (E) = Standard_Standard;
11736 end Is_Library_Level_Entity;
11737
11738 --------------------------------
11739 -- Is_Limited_Class_Wide_Type --
11740 --------------------------------
11741
11742 function Is_Limited_Class_Wide_Type (Typ : Entity_Id) return Boolean is
11743 begin
11744 return
11745 Is_Class_Wide_Type (Typ)
11746 and then (Is_Limited_Type (Typ) or else From_Limited_With (Typ));
11747 end Is_Limited_Class_Wide_Type;
11748
11749 ---------------------------------
11750 -- Is_Local_Variable_Reference --
11751 ---------------------------------
11752
11753 function Is_Local_Variable_Reference (Expr : Node_Id) return Boolean is
11754 begin
11755 if not Is_Entity_Name (Expr) then
11756 return False;
11757
11758 else
11759 declare
11760 Ent : constant Entity_Id := Entity (Expr);
11761 Sub : constant Entity_Id := Enclosing_Subprogram (Ent);
11762 begin
11763 if not Ekind_In (Ent, E_Variable, E_In_Out_Parameter) then
11764 return False;
11765 else
11766 return Present (Sub) and then Sub = Current_Subprogram;
11767 end if;
11768 end;
11769 end if;
11770 end Is_Local_Variable_Reference;
11771
11772 -------------------------
11773 -- Is_Object_Reference --
11774 -------------------------
11775
11776 function Is_Object_Reference (N : Node_Id) return Boolean is
11777
11778 function Is_Internally_Generated_Renaming (N : Node_Id) return Boolean;
11779 -- Determine whether N is the name of an internally-generated renaming
11780
11781 --------------------------------------
11782 -- Is_Internally_Generated_Renaming --
11783 --------------------------------------
11784
11785 function Is_Internally_Generated_Renaming (N : Node_Id) return Boolean is
11786 P : Node_Id;
11787
11788 begin
11789 P := N;
11790 while Present (P) loop
11791 if Nkind (P) = N_Object_Renaming_Declaration then
11792 return not Comes_From_Source (P);
11793 elsif Is_List_Member (P) then
11794 return False;
11795 end if;
11796
11797 P := Parent (P);
11798 end loop;
11799
11800 return False;
11801 end Is_Internally_Generated_Renaming;
11802
11803 -- Start of processing for Is_Object_Reference
11804
11805 begin
11806 if Is_Entity_Name (N) then
11807 return Present (Entity (N)) and then Is_Object (Entity (N));
11808
11809 else
11810 case Nkind (N) is
11811 when N_Indexed_Component | N_Slice =>
11812 return
11813 Is_Object_Reference (Prefix (N))
11814 or else Is_Access_Type (Etype (Prefix (N)));
11815
11816 -- In Ada 95, a function call is a constant object; a procedure
11817 -- call is not.
11818
11819 when N_Function_Call =>
11820 return Etype (N) /= Standard_Void_Type;
11821
11822 -- Attributes 'Input, 'Old and 'Result produce objects
11823
11824 when N_Attribute_Reference =>
11825 return
11826 Nam_In
11827 (Attribute_Name (N), Name_Input, Name_Old, Name_Result);
11828
11829 when N_Selected_Component =>
11830 return
11831 Is_Object_Reference (Selector_Name (N))
11832 and then
11833 (Is_Object_Reference (Prefix (N))
11834 or else Is_Access_Type (Etype (Prefix (N))));
11835
11836 when N_Explicit_Dereference =>
11837 return True;
11838
11839 -- A view conversion of a tagged object is an object reference
11840
11841 when N_Type_Conversion =>
11842 return Is_Tagged_Type (Etype (Subtype_Mark (N)))
11843 and then Is_Tagged_Type (Etype (Expression (N)))
11844 and then Is_Object_Reference (Expression (N));
11845
11846 -- An unchecked type conversion is considered to be an object if
11847 -- the operand is an object (this construction arises only as a
11848 -- result of expansion activities).
11849
11850 when N_Unchecked_Type_Conversion =>
11851 return True;
11852
11853 -- Allow string literals to act as objects as long as they appear
11854 -- in internally-generated renamings. The expansion of iterators
11855 -- may generate such renamings when the range involves a string
11856 -- literal.
11857
11858 when N_String_Literal =>
11859 return Is_Internally_Generated_Renaming (Parent (N));
11860
11861 -- AI05-0003: In Ada 2012 a qualified expression is a name.
11862 -- This allows disambiguation of function calls and the use
11863 -- of aggregates in more contexts.
11864
11865 when N_Qualified_Expression =>
11866 if Ada_Version < Ada_2012 then
11867 return False;
11868 else
11869 return Is_Object_Reference (Expression (N))
11870 or else Nkind (Expression (N)) = N_Aggregate;
11871 end if;
11872
11873 when others =>
11874 return False;
11875 end case;
11876 end if;
11877 end Is_Object_Reference;
11878
11879 -----------------------------------
11880 -- Is_OK_Variable_For_Out_Formal --
11881 -----------------------------------
11882
11883 function Is_OK_Variable_For_Out_Formal (AV : Node_Id) return Boolean is
11884 begin
11885 Note_Possible_Modification (AV, Sure => True);
11886
11887 -- We must reject parenthesized variable names. Comes_From_Source is
11888 -- checked because there are currently cases where the compiler violates
11889 -- this rule (e.g. passing a task object to its controlled Initialize
11890 -- routine). This should be properly documented in sinfo???
11891
11892 if Paren_Count (AV) > 0 and then Comes_From_Source (AV) then
11893 return False;
11894
11895 -- A variable is always allowed
11896
11897 elsif Is_Variable (AV) then
11898 return True;
11899
11900 -- Generalized indexing operations are rewritten as explicit
11901 -- dereferences, and it is only during resolution that we can
11902 -- check whether the context requires an access_to_variable type.
11903
11904 elsif Nkind (AV) = N_Explicit_Dereference
11905 and then Ada_Version >= Ada_2012
11906 and then Nkind (Original_Node (AV)) = N_Indexed_Component
11907 and then Present (Etype (Original_Node (AV)))
11908 and then Has_Implicit_Dereference (Etype (Original_Node (AV)))
11909 then
11910 return not Is_Access_Constant (Etype (Prefix (AV)));
11911
11912 -- Unchecked conversions are allowed only if they come from the
11913 -- generated code, which sometimes uses unchecked conversions for out
11914 -- parameters in cases where code generation is unaffected. We tell
11915 -- source unchecked conversions by seeing if they are rewrites of
11916 -- an original Unchecked_Conversion function call, or of an explicit
11917 -- conversion of a function call or an aggregate (as may happen in the
11918 -- expansion of a packed array aggregate).
11919
11920 elsif Nkind (AV) = N_Unchecked_Type_Conversion then
11921 if Nkind_In (Original_Node (AV), N_Function_Call, N_Aggregate) then
11922 return False;
11923
11924 elsif Comes_From_Source (AV)
11925 and then Nkind (Original_Node (Expression (AV))) = N_Function_Call
11926 then
11927 return False;
11928
11929 elsif Nkind (Original_Node (AV)) = N_Type_Conversion then
11930 return Is_OK_Variable_For_Out_Formal (Expression (AV));
11931
11932 else
11933 return True;
11934 end if;
11935
11936 -- Normal type conversions are allowed if argument is a variable
11937
11938 elsif Nkind (AV) = N_Type_Conversion then
11939 if Is_Variable (Expression (AV))
11940 and then Paren_Count (Expression (AV)) = 0
11941 then
11942 Note_Possible_Modification (Expression (AV), Sure => True);
11943 return True;
11944
11945 -- We also allow a non-parenthesized expression that raises
11946 -- constraint error if it rewrites what used to be a variable
11947
11948 elsif Raises_Constraint_Error (Expression (AV))
11949 and then Paren_Count (Expression (AV)) = 0
11950 and then Is_Variable (Original_Node (Expression (AV)))
11951 then
11952 return True;
11953
11954 -- Type conversion of something other than a variable
11955
11956 else
11957 return False;
11958 end if;
11959
11960 -- If this node is rewritten, then test the original form, if that is
11961 -- OK, then we consider the rewritten node OK (for example, if the
11962 -- original node is a conversion, then Is_Variable will not be true
11963 -- but we still want to allow the conversion if it converts a variable).
11964
11965 elsif Original_Node (AV) /= AV then
11966
11967 -- In Ada 2012, the explicit dereference may be a rewritten call to a
11968 -- Reference function.
11969
11970 if Ada_Version >= Ada_2012
11971 and then Nkind (Original_Node (AV)) = N_Function_Call
11972 and then
11973 Has_Implicit_Dereference (Etype (Name (Original_Node (AV))))
11974 then
11975 return True;
11976
11977 else
11978 return Is_OK_Variable_For_Out_Formal (Original_Node (AV));
11979 end if;
11980
11981 -- All other non-variables are rejected
11982
11983 else
11984 return False;
11985 end if;
11986 end Is_OK_Variable_For_Out_Formal;
11987
11988 ------------------------------------
11989 -- Is_Package_Contract_Annotation --
11990 ------------------------------------
11991
11992 function Is_Package_Contract_Annotation (Item : Node_Id) return Boolean is
11993 Nam : Name_Id;
11994
11995 begin
11996 if Nkind (Item) = N_Aspect_Specification then
11997 Nam := Chars (Identifier (Item));
11998
11999 else pragma Assert (Nkind (Item) = N_Pragma);
12000 Nam := Pragma_Name (Item);
12001 end if;
12002
12003 return Nam = Name_Abstract_State
12004 or else Nam = Name_Initial_Condition
12005 or else Nam = Name_Initializes
12006 or else Nam = Name_Refined_State;
12007 end Is_Package_Contract_Annotation;
12008
12009 -----------------------------------
12010 -- Is_Partially_Initialized_Type --
12011 -----------------------------------
12012
12013 function Is_Partially_Initialized_Type
12014 (Typ : Entity_Id;
12015 Include_Implicit : Boolean := True) return Boolean
12016 is
12017 begin
12018 if Is_Scalar_Type (Typ) then
12019 return False;
12020
12021 elsif Is_Access_Type (Typ) then
12022 return Include_Implicit;
12023
12024 elsif Is_Array_Type (Typ) then
12025
12026 -- If component type is partially initialized, so is array type
12027
12028 if Is_Partially_Initialized_Type
12029 (Component_Type (Typ), Include_Implicit)
12030 then
12031 return True;
12032
12033 -- Otherwise we are only partially initialized if we are fully
12034 -- initialized (this is the empty array case, no point in us
12035 -- duplicating that code here).
12036
12037 else
12038 return Is_Fully_Initialized_Type (Typ);
12039 end if;
12040
12041 elsif Is_Record_Type (Typ) then
12042
12043 -- A discriminated type is always partially initialized if in
12044 -- all mode
12045
12046 if Has_Discriminants (Typ) and then Include_Implicit then
12047 return True;
12048
12049 -- A tagged type is always partially initialized
12050
12051 elsif Is_Tagged_Type (Typ) then
12052 return True;
12053
12054 -- Case of non-discriminated record
12055
12056 else
12057 declare
12058 Ent : Entity_Id;
12059
12060 Component_Present : Boolean := False;
12061 -- Set True if at least one component is present. If no
12062 -- components are present, then record type is fully
12063 -- initialized (another odd case, like the null array).
12064
12065 begin
12066 -- Loop through components
12067
12068 Ent := First_Entity (Typ);
12069 while Present (Ent) loop
12070 if Ekind (Ent) = E_Component then
12071 Component_Present := True;
12072
12073 -- If a component has an initialization expression then
12074 -- the enclosing record type is partially initialized
12075
12076 if Present (Parent (Ent))
12077 and then Present (Expression (Parent (Ent)))
12078 then
12079 return True;
12080
12081 -- If a component is of a type which is itself partially
12082 -- initialized, then the enclosing record type is also.
12083
12084 elsif Is_Partially_Initialized_Type
12085 (Etype (Ent), Include_Implicit)
12086 then
12087 return True;
12088 end if;
12089 end if;
12090
12091 Next_Entity (Ent);
12092 end loop;
12093
12094 -- No initialized components found. If we found any components
12095 -- they were all uninitialized so the result is false.
12096
12097 if Component_Present then
12098 return False;
12099
12100 -- But if we found no components, then all the components are
12101 -- initialized so we consider the type to be initialized.
12102
12103 else
12104 return True;
12105 end if;
12106 end;
12107 end if;
12108
12109 -- Concurrent types are always fully initialized
12110
12111 elsif Is_Concurrent_Type (Typ) then
12112 return True;
12113
12114 -- For a private type, go to underlying type. If there is no underlying
12115 -- type then just assume this partially initialized. Not clear if this
12116 -- can happen in a non-error case, but no harm in testing for this.
12117
12118 elsif Is_Private_Type (Typ) then
12119 declare
12120 U : constant Entity_Id := Underlying_Type (Typ);
12121 begin
12122 if No (U) then
12123 return True;
12124 else
12125 return Is_Partially_Initialized_Type (U, Include_Implicit);
12126 end if;
12127 end;
12128
12129 -- For any other type (are there any?) assume partially initialized
12130
12131 else
12132 return True;
12133 end if;
12134 end Is_Partially_Initialized_Type;
12135
12136 ------------------------------------
12137 -- Is_Potentially_Persistent_Type --
12138 ------------------------------------
12139
12140 function Is_Potentially_Persistent_Type (T : Entity_Id) return Boolean is
12141 Comp : Entity_Id;
12142 Indx : Node_Id;
12143
12144 begin
12145 -- For private type, test corresponding full type
12146
12147 if Is_Private_Type (T) then
12148 return Is_Potentially_Persistent_Type (Full_View (T));
12149
12150 -- Scalar types are potentially persistent
12151
12152 elsif Is_Scalar_Type (T) then
12153 return True;
12154
12155 -- Record type is potentially persistent if not tagged and the types of
12156 -- all it components are potentially persistent, and no component has
12157 -- an initialization expression.
12158
12159 elsif Is_Record_Type (T)
12160 and then not Is_Tagged_Type (T)
12161 and then not Is_Partially_Initialized_Type (T)
12162 then
12163 Comp := First_Component (T);
12164 while Present (Comp) loop
12165 if not Is_Potentially_Persistent_Type (Etype (Comp)) then
12166 return False;
12167 else
12168 Next_Entity (Comp);
12169 end if;
12170 end loop;
12171
12172 return True;
12173
12174 -- Array type is potentially persistent if its component type is
12175 -- potentially persistent and if all its constraints are static.
12176
12177 elsif Is_Array_Type (T) then
12178 if not Is_Potentially_Persistent_Type (Component_Type (T)) then
12179 return False;
12180 end if;
12181
12182 Indx := First_Index (T);
12183 while Present (Indx) loop
12184 if not Is_OK_Static_Subtype (Etype (Indx)) then
12185 return False;
12186 else
12187 Next_Index (Indx);
12188 end if;
12189 end loop;
12190
12191 return True;
12192
12193 -- All other types are not potentially persistent
12194
12195 else
12196 return False;
12197 end if;
12198 end Is_Potentially_Persistent_Type;
12199
12200 --------------------------------
12201 -- Is_Potentially_Unevaluated --
12202 --------------------------------
12203
12204 function Is_Potentially_Unevaluated (N : Node_Id) return Boolean is
12205 Par : Node_Id;
12206 Expr : Node_Id;
12207
12208 begin
12209 Expr := N;
12210 Par := Parent (N);
12211
12212 -- A postcondition whose expression is a short-circuit is broken down
12213 -- into individual aspects for better exception reporting. The original
12214 -- short-circuit expression is rewritten as the second operand, and an
12215 -- occurrence of 'Old in that operand is potentially unevaluated.
12216 -- See Sem_ch13.adb for details of this transformation.
12217
12218 if Nkind (Original_Node (Par)) = N_And_Then then
12219 return True;
12220 end if;
12221
12222 while not Nkind_In (Par, N_If_Expression,
12223 N_Case_Expression,
12224 N_And_Then,
12225 N_Or_Else,
12226 N_In,
12227 N_Not_In)
12228 loop
12229 Expr := Par;
12230 Par := Parent (Par);
12231
12232 -- If the context is not an expression, or if is the result of
12233 -- expansion of an enclosing construct (such as another attribute)
12234 -- the predicate does not apply.
12235
12236 if Nkind (Par) not in N_Subexpr
12237 or else not Comes_From_Source (Par)
12238 then
12239 return False;
12240 end if;
12241 end loop;
12242
12243 if Nkind (Par) = N_If_Expression then
12244 return Is_Elsif (Par) or else Expr /= First (Expressions (Par));
12245
12246 elsif Nkind (Par) = N_Case_Expression then
12247 return Expr /= Expression (Par);
12248
12249 elsif Nkind_In (Par, N_And_Then, N_Or_Else) then
12250 return Expr = Right_Opnd (Par);
12251
12252 elsif Nkind_In (Par, N_In, N_Not_In) then
12253 return Expr /= Left_Opnd (Par);
12254
12255 else
12256 return False;
12257 end if;
12258 end Is_Potentially_Unevaluated;
12259
12260 ---------------------------------
12261 -- Is_Protected_Self_Reference --
12262 ---------------------------------
12263
12264 function Is_Protected_Self_Reference (N : Node_Id) return Boolean is
12265
12266 function In_Access_Definition (N : Node_Id) return Boolean;
12267 -- Returns true if N belongs to an access definition
12268
12269 --------------------------
12270 -- In_Access_Definition --
12271 --------------------------
12272
12273 function In_Access_Definition (N : Node_Id) return Boolean is
12274 P : Node_Id;
12275
12276 begin
12277 P := Parent (N);
12278 while Present (P) loop
12279 if Nkind (P) = N_Access_Definition then
12280 return True;
12281 end if;
12282
12283 P := Parent (P);
12284 end loop;
12285
12286 return False;
12287 end In_Access_Definition;
12288
12289 -- Start of processing for Is_Protected_Self_Reference
12290
12291 begin
12292 -- Verify that prefix is analyzed and has the proper form. Note that
12293 -- the attributes Elab_Spec, Elab_Body, Elab_Subp_Body and UET_Address,
12294 -- which also produce the address of an entity, do not analyze their
12295 -- prefix because they denote entities that are not necessarily visible.
12296 -- Neither of them can apply to a protected type.
12297
12298 return Ada_Version >= Ada_2005
12299 and then Is_Entity_Name (N)
12300 and then Present (Entity (N))
12301 and then Is_Protected_Type (Entity (N))
12302 and then In_Open_Scopes (Entity (N))
12303 and then not In_Access_Definition (N);
12304 end Is_Protected_Self_Reference;
12305
12306 -----------------------------
12307 -- Is_RCI_Pkg_Spec_Or_Body --
12308 -----------------------------
12309
12310 function Is_RCI_Pkg_Spec_Or_Body (Cunit : Node_Id) return Boolean is
12311
12312 function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean;
12313 -- Return True if the unit of Cunit is an RCI package declaration
12314
12315 ---------------------------
12316 -- Is_RCI_Pkg_Decl_Cunit --
12317 ---------------------------
12318
12319 function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean is
12320 The_Unit : constant Node_Id := Unit (Cunit);
12321
12322 begin
12323 if Nkind (The_Unit) /= N_Package_Declaration then
12324 return False;
12325 end if;
12326
12327 return Is_Remote_Call_Interface (Defining_Entity (The_Unit));
12328 end Is_RCI_Pkg_Decl_Cunit;
12329
12330 -- Start of processing for Is_RCI_Pkg_Spec_Or_Body
12331
12332 begin
12333 return Is_RCI_Pkg_Decl_Cunit (Cunit)
12334 or else
12335 (Nkind (Unit (Cunit)) = N_Package_Body
12336 and then Is_RCI_Pkg_Decl_Cunit (Library_Unit (Cunit)));
12337 end Is_RCI_Pkg_Spec_Or_Body;
12338
12339 -----------------------------------------
12340 -- Is_Remote_Access_To_Class_Wide_Type --
12341 -----------------------------------------
12342
12343 function Is_Remote_Access_To_Class_Wide_Type
12344 (E : Entity_Id) return Boolean
12345 is
12346 begin
12347 -- A remote access to class-wide type is a general access to object type
12348 -- declared in the visible part of a Remote_Types or Remote_Call_
12349 -- Interface unit.
12350
12351 return Ekind (E) = E_General_Access_Type
12352 and then (Is_Remote_Call_Interface (E) or else Is_Remote_Types (E));
12353 end Is_Remote_Access_To_Class_Wide_Type;
12354
12355 -----------------------------------------
12356 -- Is_Remote_Access_To_Subprogram_Type --
12357 -----------------------------------------
12358
12359 function Is_Remote_Access_To_Subprogram_Type
12360 (E : Entity_Id) return Boolean
12361 is
12362 begin
12363 return (Ekind (E) = E_Access_Subprogram_Type
12364 or else (Ekind (E) = E_Record_Type
12365 and then Present (Corresponding_Remote_Type (E))))
12366 and then (Is_Remote_Call_Interface (E) or else Is_Remote_Types (E));
12367 end Is_Remote_Access_To_Subprogram_Type;
12368
12369 --------------------
12370 -- Is_Remote_Call --
12371 --------------------
12372
12373 function Is_Remote_Call (N : Node_Id) return Boolean is
12374 begin
12375 if Nkind (N) not in N_Subprogram_Call then
12376
12377 -- An entry call cannot be remote
12378
12379 return False;
12380
12381 elsif Nkind (Name (N)) in N_Has_Entity
12382 and then Is_Remote_Call_Interface (Entity (Name (N)))
12383 then
12384 -- A subprogram declared in the spec of a RCI package is remote
12385
12386 return True;
12387
12388 elsif Nkind (Name (N)) = N_Explicit_Dereference
12389 and then Is_Remote_Access_To_Subprogram_Type
12390 (Etype (Prefix (Name (N))))
12391 then
12392 -- The dereference of a RAS is a remote call
12393
12394 return True;
12395
12396 elsif Present (Controlling_Argument (N))
12397 and then Is_Remote_Access_To_Class_Wide_Type
12398 (Etype (Controlling_Argument (N)))
12399 then
12400 -- Any primitive operation call with a controlling argument of
12401 -- a RACW type is a remote call.
12402
12403 return True;
12404 end if;
12405
12406 -- All other calls are local calls
12407
12408 return False;
12409 end Is_Remote_Call;
12410
12411 ----------------------
12412 -- Is_Renamed_Entry --
12413 ----------------------
12414
12415 function Is_Renamed_Entry (Proc_Nam : Entity_Id) return Boolean is
12416 Orig_Node : Node_Id := Empty;
12417 Subp_Decl : Node_Id := Parent (Parent (Proc_Nam));
12418
12419 function Is_Entry (Nam : Node_Id) return Boolean;
12420 -- Determine whether Nam is an entry. Traverse selectors if there are
12421 -- nested selected components.
12422
12423 --------------
12424 -- Is_Entry --
12425 --------------
12426
12427 function Is_Entry (Nam : Node_Id) return Boolean is
12428 begin
12429 if Nkind (Nam) = N_Selected_Component then
12430 return Is_Entry (Selector_Name (Nam));
12431 end if;
12432
12433 return Ekind (Entity (Nam)) = E_Entry;
12434 end Is_Entry;
12435
12436 -- Start of processing for Is_Renamed_Entry
12437
12438 begin
12439 if Present (Alias (Proc_Nam)) then
12440 Subp_Decl := Parent (Parent (Alias (Proc_Nam)));
12441 end if;
12442
12443 -- Look for a rewritten subprogram renaming declaration
12444
12445 if Nkind (Subp_Decl) = N_Subprogram_Declaration
12446 and then Present (Original_Node (Subp_Decl))
12447 then
12448 Orig_Node := Original_Node (Subp_Decl);
12449 end if;
12450
12451 -- The rewritten subprogram is actually an entry
12452
12453 if Present (Orig_Node)
12454 and then Nkind (Orig_Node) = N_Subprogram_Renaming_Declaration
12455 and then Is_Entry (Name (Orig_Node))
12456 then
12457 return True;
12458 end if;
12459
12460 return False;
12461 end Is_Renamed_Entry;
12462
12463 ----------------------------
12464 -- Is_Reversible_Iterator --
12465 ----------------------------
12466
12467 function Is_Reversible_Iterator (Typ : Entity_Id) return Boolean is
12468 Ifaces_List : Elist_Id;
12469 Iface_Elmt : Elmt_Id;
12470 Iface : Entity_Id;
12471
12472 begin
12473 if Is_Class_Wide_Type (Typ)
12474 and then Chars (Etype (Typ)) = Name_Reversible_Iterator
12475 and then Is_Predefined_File_Name
12476 (Unit_File_Name (Get_Source_Unit (Etype (Typ))))
12477 then
12478 return True;
12479
12480 elsif not Is_Tagged_Type (Typ) or else not Is_Derived_Type (Typ) then
12481 return False;
12482
12483 else
12484 Collect_Interfaces (Typ, Ifaces_List);
12485
12486 Iface_Elmt := First_Elmt (Ifaces_List);
12487 while Present (Iface_Elmt) loop
12488 Iface := Node (Iface_Elmt);
12489 if Chars (Iface) = Name_Reversible_Iterator
12490 and then
12491 Is_Predefined_File_Name
12492 (Unit_File_Name (Get_Source_Unit (Iface)))
12493 then
12494 return True;
12495 end if;
12496
12497 Next_Elmt (Iface_Elmt);
12498 end loop;
12499 end if;
12500
12501 return False;
12502 end Is_Reversible_Iterator;
12503
12504 ----------------------
12505 -- Is_Selector_Name --
12506 ----------------------
12507
12508 function Is_Selector_Name (N : Node_Id) return Boolean is
12509 begin
12510 if not Is_List_Member (N) then
12511 declare
12512 P : constant Node_Id := Parent (N);
12513 begin
12514 return Nkind_In (P, N_Expanded_Name,
12515 N_Generic_Association,
12516 N_Parameter_Association,
12517 N_Selected_Component)
12518 and then Selector_Name (P) = N;
12519 end;
12520
12521 else
12522 declare
12523 L : constant List_Id := List_Containing (N);
12524 P : constant Node_Id := Parent (L);
12525 begin
12526 return (Nkind (P) = N_Discriminant_Association
12527 and then Selector_Names (P) = L)
12528 or else
12529 (Nkind (P) = N_Component_Association
12530 and then Choices (P) = L);
12531 end;
12532 end if;
12533 end Is_Selector_Name;
12534
12535 -------------------------------------
12536 -- Is_SPARK_05_Initialization_Expr --
12537 -------------------------------------
12538
12539 function Is_SPARK_05_Initialization_Expr (N : Node_Id) return Boolean is
12540 Is_Ok : Boolean;
12541 Expr : Node_Id;
12542 Comp_Assn : Node_Id;
12543 Orig_N : constant Node_Id := Original_Node (N);
12544
12545 begin
12546 Is_Ok := True;
12547
12548 if not Comes_From_Source (Orig_N) then
12549 goto Done;
12550 end if;
12551
12552 pragma Assert (Nkind (Orig_N) in N_Subexpr);
12553
12554 case Nkind (Orig_N) is
12555 when N_Character_Literal |
12556 N_Integer_Literal |
12557 N_Real_Literal |
12558 N_String_Literal =>
12559 null;
12560
12561 when N_Identifier |
12562 N_Expanded_Name =>
12563 if Is_Entity_Name (Orig_N)
12564 and then Present (Entity (Orig_N)) -- needed in some cases
12565 then
12566 case Ekind (Entity (Orig_N)) is
12567 when E_Constant |
12568 E_Enumeration_Literal |
12569 E_Named_Integer |
12570 E_Named_Real =>
12571 null;
12572 when others =>
12573 if Is_Type (Entity (Orig_N)) then
12574 null;
12575 else
12576 Is_Ok := False;
12577 end if;
12578 end case;
12579 end if;
12580
12581 when N_Qualified_Expression |
12582 N_Type_Conversion =>
12583 Is_Ok := Is_SPARK_05_Initialization_Expr (Expression (Orig_N));
12584
12585 when N_Unary_Op =>
12586 Is_Ok := Is_SPARK_05_Initialization_Expr (Right_Opnd (Orig_N));
12587
12588 when N_Binary_Op |
12589 N_Short_Circuit |
12590 N_Membership_Test =>
12591 Is_Ok := Is_SPARK_05_Initialization_Expr (Left_Opnd (Orig_N))
12592 and then
12593 Is_SPARK_05_Initialization_Expr (Right_Opnd (Orig_N));
12594
12595 when N_Aggregate |
12596 N_Extension_Aggregate =>
12597 if Nkind (Orig_N) = N_Extension_Aggregate then
12598 Is_Ok :=
12599 Is_SPARK_05_Initialization_Expr (Ancestor_Part (Orig_N));
12600 end if;
12601
12602 Expr := First (Expressions (Orig_N));
12603 while Present (Expr) loop
12604 if not Is_SPARK_05_Initialization_Expr (Expr) then
12605 Is_Ok := False;
12606 goto Done;
12607 end if;
12608
12609 Next (Expr);
12610 end loop;
12611
12612 Comp_Assn := First (Component_Associations (Orig_N));
12613 while Present (Comp_Assn) loop
12614 Expr := Expression (Comp_Assn);
12615
12616 -- Note: test for Present here needed for box assocation
12617
12618 if Present (Expr)
12619 and then not Is_SPARK_05_Initialization_Expr (Expr)
12620 then
12621 Is_Ok := False;
12622 goto Done;
12623 end if;
12624
12625 Next (Comp_Assn);
12626 end loop;
12627
12628 when N_Attribute_Reference =>
12629 if Nkind (Prefix (Orig_N)) in N_Subexpr then
12630 Is_Ok := Is_SPARK_05_Initialization_Expr (Prefix (Orig_N));
12631 end if;
12632
12633 Expr := First (Expressions (Orig_N));
12634 while Present (Expr) loop
12635 if not Is_SPARK_05_Initialization_Expr (Expr) then
12636 Is_Ok := False;
12637 goto Done;
12638 end if;
12639
12640 Next (Expr);
12641 end loop;
12642
12643 -- Selected components might be expanded named not yet resolved, so
12644 -- default on the safe side. (Eg on sparklex.ads)
12645
12646 when N_Selected_Component =>
12647 null;
12648
12649 when others =>
12650 Is_Ok := False;
12651 end case;
12652
12653 <<Done>>
12654 return Is_Ok;
12655 end Is_SPARK_05_Initialization_Expr;
12656
12657 ----------------------------------
12658 -- Is_SPARK_05_Object_Reference --
12659 ----------------------------------
12660
12661 function Is_SPARK_05_Object_Reference (N : Node_Id) return Boolean is
12662 begin
12663 if Is_Entity_Name (N) then
12664 return Present (Entity (N))
12665 and then
12666 (Ekind_In (Entity (N), E_Constant, E_Variable)
12667 or else Ekind (Entity (N)) in Formal_Kind);
12668
12669 else
12670 case Nkind (N) is
12671 when N_Selected_Component =>
12672 return Is_SPARK_05_Object_Reference (Prefix (N));
12673
12674 when others =>
12675 return False;
12676 end case;
12677 end if;
12678 end Is_SPARK_05_Object_Reference;
12679
12680 -----------------------------
12681 -- Is_Specific_Tagged_Type --
12682 -----------------------------
12683
12684 function Is_Specific_Tagged_Type (Typ : Entity_Id) return Boolean is
12685 Full_Typ : Entity_Id;
12686
12687 begin
12688 -- Handle private types
12689
12690 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
12691 Full_Typ := Full_View (Typ);
12692 else
12693 Full_Typ := Typ;
12694 end if;
12695
12696 -- A specific tagged type is a non-class-wide tagged type
12697
12698 return Is_Tagged_Type (Full_Typ) and not Is_Class_Wide_Type (Full_Typ);
12699 end Is_Specific_Tagged_Type;
12700
12701 ------------------
12702 -- Is_Statement --
12703 ------------------
12704
12705 function Is_Statement (N : Node_Id) return Boolean is
12706 begin
12707 return
12708 Nkind (N) in N_Statement_Other_Than_Procedure_Call
12709 or else Nkind (N) = N_Procedure_Call_Statement;
12710 end Is_Statement;
12711
12712 ---------------------------------------
12713 -- Is_Subprogram_Contract_Annotation --
12714 ---------------------------------------
12715
12716 function Is_Subprogram_Contract_Annotation
12717 (Item : Node_Id) return Boolean
12718 is
12719 Nam : Name_Id;
12720
12721 begin
12722 if Nkind (Item) = N_Aspect_Specification then
12723 Nam := Chars (Identifier (Item));
12724
12725 else pragma Assert (Nkind (Item) = N_Pragma);
12726 Nam := Pragma_Name (Item);
12727 end if;
12728
12729 return Nam = Name_Contract_Cases
12730 or else Nam = Name_Depends
12731 or else Nam = Name_Extensions_Visible
12732 or else Nam = Name_Global
12733 or else Nam = Name_Post
12734 or else Nam = Name_Post_Class
12735 or else Nam = Name_Postcondition
12736 or else Nam = Name_Pre
12737 or else Nam = Name_Pre_Class
12738 or else Nam = Name_Precondition
12739 or else Nam = Name_Refined_Depends
12740 or else Nam = Name_Refined_Global
12741 or else Nam = Name_Refined_Post
12742 or else Nam = Name_Test_Case;
12743 end Is_Subprogram_Contract_Annotation;
12744
12745 --------------------------------------------------
12746 -- Is_Subprogram_Stub_Without_Prior_Declaration --
12747 --------------------------------------------------
12748
12749 function Is_Subprogram_Stub_Without_Prior_Declaration
12750 (N : Node_Id) return Boolean
12751 is
12752 begin
12753 -- A subprogram stub without prior declaration serves as declaration for
12754 -- the actual subprogram body. As such, it has an attached defining
12755 -- entity of E_[Generic_]Function or E_[Generic_]Procedure.
12756
12757 return Nkind (N) = N_Subprogram_Body_Stub
12758 and then Ekind (Defining_Entity (N)) /= E_Subprogram_Body;
12759 end Is_Subprogram_Stub_Without_Prior_Declaration;
12760
12761 ---------------------------------
12762 -- Is_Synchronized_Tagged_Type --
12763 ---------------------------------
12764
12765 function Is_Synchronized_Tagged_Type (E : Entity_Id) return Boolean is
12766 Kind : constant Entity_Kind := Ekind (Base_Type (E));
12767
12768 begin
12769 -- A task or protected type derived from an interface is a tagged type.
12770 -- Such a tagged type is called a synchronized tagged type, as are
12771 -- synchronized interfaces and private extensions whose declaration
12772 -- includes the reserved word synchronized.
12773
12774 return (Is_Tagged_Type (E)
12775 and then (Kind = E_Task_Type
12776 or else
12777 Kind = E_Protected_Type))
12778 or else
12779 (Is_Interface (E)
12780 and then Is_Synchronized_Interface (E))
12781 or else
12782 (Ekind (E) = E_Record_Type_With_Private
12783 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
12784 and then (Synchronized_Present (Parent (E))
12785 or else Is_Synchronized_Interface (Etype (E))));
12786 end Is_Synchronized_Tagged_Type;
12787
12788 -----------------
12789 -- Is_Transfer --
12790 -----------------
12791
12792 function Is_Transfer (N : Node_Id) return Boolean is
12793 Kind : constant Node_Kind := Nkind (N);
12794
12795 begin
12796 if Kind = N_Simple_Return_Statement
12797 or else
12798 Kind = N_Extended_Return_Statement
12799 or else
12800 Kind = N_Goto_Statement
12801 or else
12802 Kind = N_Raise_Statement
12803 or else
12804 Kind = N_Requeue_Statement
12805 then
12806 return True;
12807
12808 elsif (Kind = N_Exit_Statement or else Kind in N_Raise_xxx_Error)
12809 and then No (Condition (N))
12810 then
12811 return True;
12812
12813 elsif Kind = N_Procedure_Call_Statement
12814 and then Is_Entity_Name (Name (N))
12815 and then Present (Entity (Name (N)))
12816 and then No_Return (Entity (Name (N)))
12817 then
12818 return True;
12819
12820 elsif Nkind (Original_Node (N)) = N_Raise_Statement then
12821 return True;
12822
12823 else
12824 return False;
12825 end if;
12826 end Is_Transfer;
12827
12828 -------------
12829 -- Is_True --
12830 -------------
12831
12832 function Is_True (U : Uint) return Boolean is
12833 begin
12834 return (U /= 0);
12835 end Is_True;
12836
12837 --------------------------------------
12838 -- Is_Unchecked_Conversion_Instance --
12839 --------------------------------------
12840
12841 function Is_Unchecked_Conversion_Instance (Id : Entity_Id) return Boolean is
12842 Gen_Par : Entity_Id;
12843
12844 begin
12845 -- Look for a function whose generic parent is the predefined intrinsic
12846 -- function Unchecked_Conversion.
12847
12848 if Ekind (Id) = E_Function then
12849 Gen_Par := Generic_Parent (Parent (Id));
12850
12851 return
12852 Present (Gen_Par)
12853 and then Chars (Gen_Par) = Name_Unchecked_Conversion
12854 and then Is_Intrinsic_Subprogram (Gen_Par)
12855 and then Is_Predefined_File_Name
12856 (Unit_File_Name (Get_Source_Unit (Gen_Par)));
12857 end if;
12858
12859 return False;
12860 end Is_Unchecked_Conversion_Instance;
12861
12862 -------------------------------
12863 -- Is_Universal_Numeric_Type --
12864 -------------------------------
12865
12866 function Is_Universal_Numeric_Type (T : Entity_Id) return Boolean is
12867 begin
12868 return T = Universal_Integer or else T = Universal_Real;
12869 end Is_Universal_Numeric_Type;
12870
12871 -------------------
12872 -- Is_Value_Type --
12873 -------------------
12874
12875 function Is_Value_Type (T : Entity_Id) return Boolean is
12876 begin
12877 return VM_Target = CLI_Target
12878 and then Nkind (T) in N_Has_Chars
12879 and then Chars (T) /= No_Name
12880 and then Get_Name_String (Chars (T)) = "valuetype";
12881 end Is_Value_Type;
12882
12883 ----------------------------
12884 -- Is_Variable_Size_Array --
12885 ----------------------------
12886
12887 function Is_Variable_Size_Array (E : Entity_Id) return Boolean is
12888 Idx : Node_Id;
12889
12890 begin
12891 pragma Assert (Is_Array_Type (E));
12892
12893 -- Check if some index is initialized with a non-constant value
12894
12895 Idx := First_Index (E);
12896 while Present (Idx) loop
12897 if Nkind (Idx) = N_Range then
12898 if not Is_Constant_Bound (Low_Bound (Idx))
12899 or else not Is_Constant_Bound (High_Bound (Idx))
12900 then
12901 return True;
12902 end if;
12903 end if;
12904
12905 Idx := Next_Index (Idx);
12906 end loop;
12907
12908 return False;
12909 end Is_Variable_Size_Array;
12910
12911 -----------------------------
12912 -- Is_Variable_Size_Record --
12913 -----------------------------
12914
12915 function Is_Variable_Size_Record (E : Entity_Id) return Boolean is
12916 Comp : Entity_Id;
12917 Comp_Typ : Entity_Id;
12918
12919 begin
12920 pragma Assert (Is_Record_Type (E));
12921
12922 Comp := First_Entity (E);
12923 while Present (Comp) loop
12924 Comp_Typ := Etype (Comp);
12925
12926 -- Recursive call if the record type has discriminants
12927
12928 if Is_Record_Type (Comp_Typ)
12929 and then Has_Discriminants (Comp_Typ)
12930 and then Is_Variable_Size_Record (Comp_Typ)
12931 then
12932 return True;
12933
12934 elsif Is_Array_Type (Comp_Typ)
12935 and then Is_Variable_Size_Array (Comp_Typ)
12936 then
12937 return True;
12938 end if;
12939
12940 Next_Entity (Comp);
12941 end loop;
12942
12943 return False;
12944 end Is_Variable_Size_Record;
12945
12946 -----------------
12947 -- Is_Variable --
12948 -----------------
12949
12950 function Is_Variable
12951 (N : Node_Id;
12952 Use_Original_Node : Boolean := True) return Boolean
12953 is
12954 Orig_Node : Node_Id;
12955
12956 function In_Protected_Function (E : Entity_Id) return Boolean;
12957 -- Within a protected function, the private components of the enclosing
12958 -- protected type are constants. A function nested within a (protected)
12959 -- procedure is not itself protected. Within the body of a protected
12960 -- function the current instance of the protected type is a constant.
12961
12962 function Is_Variable_Prefix (P : Node_Id) return Boolean;
12963 -- Prefixes can involve implicit dereferences, in which case we must
12964 -- test for the case of a reference of a constant access type, which can
12965 -- can never be a variable.
12966
12967 ---------------------------
12968 -- In_Protected_Function --
12969 ---------------------------
12970
12971 function In_Protected_Function (E : Entity_Id) return Boolean is
12972 Prot : Entity_Id;
12973 S : Entity_Id;
12974
12975 begin
12976 -- E is the current instance of a type
12977
12978 if Is_Type (E) then
12979 Prot := E;
12980
12981 -- E is an object
12982
12983 else
12984 Prot := Scope (E);
12985 end if;
12986
12987 if not Is_Protected_Type (Prot) then
12988 return False;
12989
12990 else
12991 S := Current_Scope;
12992 while Present (S) and then S /= Prot loop
12993 if Ekind (S) = E_Function and then Scope (S) = Prot then
12994 return True;
12995 end if;
12996
12997 S := Scope (S);
12998 end loop;
12999
13000 return False;
13001 end if;
13002 end In_Protected_Function;
13003
13004 ------------------------
13005 -- Is_Variable_Prefix --
13006 ------------------------
13007
13008 function Is_Variable_Prefix (P : Node_Id) return Boolean is
13009 begin
13010 if Is_Access_Type (Etype (P)) then
13011 return not Is_Access_Constant (Root_Type (Etype (P)));
13012
13013 -- For the case of an indexed component whose prefix has a packed
13014 -- array type, the prefix has been rewritten into a type conversion.
13015 -- Determine variable-ness from the converted expression.
13016
13017 elsif Nkind (P) = N_Type_Conversion
13018 and then not Comes_From_Source (P)
13019 and then Is_Array_Type (Etype (P))
13020 and then Is_Packed (Etype (P))
13021 then
13022 return Is_Variable (Expression (P));
13023
13024 else
13025 return Is_Variable (P);
13026 end if;
13027 end Is_Variable_Prefix;
13028
13029 -- Start of processing for Is_Variable
13030
13031 begin
13032 -- Special check, allow x'Deref(expr) as a variable
13033
13034 if Nkind (N) = N_Attribute_Reference
13035 and then Attribute_Name (N) = Name_Deref
13036 then
13037 return True;
13038 end if;
13039
13040 -- Check if we perform the test on the original node since this may be a
13041 -- test of syntactic categories which must not be disturbed by whatever
13042 -- rewriting might have occurred. For example, an aggregate, which is
13043 -- certainly NOT a variable, could be turned into a variable by
13044 -- expansion.
13045
13046 if Use_Original_Node then
13047 Orig_Node := Original_Node (N);
13048 else
13049 Orig_Node := N;
13050 end if;
13051
13052 -- Definitely OK if Assignment_OK is set. Since this is something that
13053 -- only gets set for expanded nodes, the test is on N, not Orig_Node.
13054
13055 if Nkind (N) in N_Subexpr and then Assignment_OK (N) then
13056 return True;
13057
13058 -- Normally we go to the original node, but there is one exception where
13059 -- we use the rewritten node, namely when it is an explicit dereference.
13060 -- The generated code may rewrite a prefix which is an access type with
13061 -- an explicit dereference. The dereference is a variable, even though
13062 -- the original node may not be (since it could be a constant of the
13063 -- access type).
13064
13065 -- In Ada 2005 we have a further case to consider: the prefix may be a
13066 -- function call given in prefix notation. The original node appears to
13067 -- be a selected component, but we need to examine the call.
13068
13069 elsif Nkind (N) = N_Explicit_Dereference
13070 and then Nkind (Orig_Node) /= N_Explicit_Dereference
13071 and then Present (Etype (Orig_Node))
13072 and then Is_Access_Type (Etype (Orig_Node))
13073 then
13074 -- Note that if the prefix is an explicit dereference that does not
13075 -- come from source, we must check for a rewritten function call in
13076 -- prefixed notation before other forms of rewriting, to prevent a
13077 -- compiler crash.
13078
13079 return
13080 (Nkind (Orig_Node) = N_Function_Call
13081 and then not Is_Access_Constant (Etype (Prefix (N))))
13082 or else
13083 Is_Variable_Prefix (Original_Node (Prefix (N)));
13084
13085 -- in Ada 2012, the dereference may have been added for a type with
13086 -- a declared implicit dereference aspect. Check that it is not an
13087 -- access to constant.
13088
13089 elsif Nkind (N) = N_Explicit_Dereference
13090 and then Present (Etype (Orig_Node))
13091 and then Ada_Version >= Ada_2012
13092 and then Has_Implicit_Dereference (Etype (Orig_Node))
13093 then
13094 return not Is_Access_Constant (Etype (Prefix (N)));
13095
13096 -- A function call is never a variable
13097
13098 elsif Nkind (N) = N_Function_Call then
13099 return False;
13100
13101 -- All remaining checks use the original node
13102
13103 elsif Is_Entity_Name (Orig_Node)
13104 and then Present (Entity (Orig_Node))
13105 then
13106 declare
13107 E : constant Entity_Id := Entity (Orig_Node);
13108 K : constant Entity_Kind := Ekind (E);
13109
13110 begin
13111 return (K = E_Variable
13112 and then Nkind (Parent (E)) /= N_Exception_Handler)
13113 or else (K = E_Component
13114 and then not In_Protected_Function (E))
13115 or else K = E_Out_Parameter
13116 or else K = E_In_Out_Parameter
13117 or else K = E_Generic_In_Out_Parameter
13118
13119 -- Current instance of type. If this is a protected type, check
13120 -- we are not within the body of one of its protected functions.
13121
13122 or else (Is_Type (E)
13123 and then In_Open_Scopes (E)
13124 and then not In_Protected_Function (E))
13125
13126 or else (Is_Incomplete_Or_Private_Type (E)
13127 and then In_Open_Scopes (Full_View (E)));
13128 end;
13129
13130 else
13131 case Nkind (Orig_Node) is
13132 when N_Indexed_Component | N_Slice =>
13133 return Is_Variable_Prefix (Prefix (Orig_Node));
13134
13135 when N_Selected_Component =>
13136 return (Is_Variable (Selector_Name (Orig_Node))
13137 and then Is_Variable_Prefix (Prefix (Orig_Node)))
13138 or else
13139 (Nkind (N) = N_Expanded_Name
13140 and then Scope (Entity (N)) = Entity (Prefix (N)));
13141
13142 -- For an explicit dereference, the type of the prefix cannot
13143 -- be an access to constant or an access to subprogram.
13144
13145 when N_Explicit_Dereference =>
13146 declare
13147 Typ : constant Entity_Id := Etype (Prefix (Orig_Node));
13148 begin
13149 return Is_Access_Type (Typ)
13150 and then not Is_Access_Constant (Root_Type (Typ))
13151 and then Ekind (Typ) /= E_Access_Subprogram_Type;
13152 end;
13153
13154 -- The type conversion is the case where we do not deal with the
13155 -- context dependent special case of an actual parameter. Thus
13156 -- the type conversion is only considered a variable for the
13157 -- purposes of this routine if the target type is tagged. However,
13158 -- a type conversion is considered to be a variable if it does not
13159 -- come from source (this deals for example with the conversions
13160 -- of expressions to their actual subtypes).
13161
13162 when N_Type_Conversion =>
13163 return Is_Variable (Expression (Orig_Node))
13164 and then
13165 (not Comes_From_Source (Orig_Node)
13166 or else
13167 (Is_Tagged_Type (Etype (Subtype_Mark (Orig_Node)))
13168 and then
13169 Is_Tagged_Type (Etype (Expression (Orig_Node)))));
13170
13171 -- GNAT allows an unchecked type conversion as a variable. This
13172 -- only affects the generation of internal expanded code, since
13173 -- calls to instantiations of Unchecked_Conversion are never
13174 -- considered variables (since they are function calls).
13175
13176 when N_Unchecked_Type_Conversion =>
13177 return Is_Variable (Expression (Orig_Node));
13178
13179 when others =>
13180 return False;
13181 end case;
13182 end if;
13183 end Is_Variable;
13184
13185 ---------------------------
13186 -- Is_Visibly_Controlled --
13187 ---------------------------
13188
13189 function Is_Visibly_Controlled (T : Entity_Id) return Boolean is
13190 Root : constant Entity_Id := Root_Type (T);
13191 begin
13192 return Chars (Scope (Root)) = Name_Finalization
13193 and then Chars (Scope (Scope (Root))) = Name_Ada
13194 and then Scope (Scope (Scope (Root))) = Standard_Standard;
13195 end Is_Visibly_Controlled;
13196
13197 ------------------------
13198 -- Is_Volatile_Object --
13199 ------------------------
13200
13201 function Is_Volatile_Object (N : Node_Id) return Boolean is
13202
13203 function Is_Volatile_Prefix (N : Node_Id) return Boolean;
13204 -- If prefix is an implicit dereference, examine designated type
13205
13206 function Object_Has_Volatile_Components (N : Node_Id) return Boolean;
13207 -- Determines if given object has volatile components
13208
13209 ------------------------
13210 -- Is_Volatile_Prefix --
13211 ------------------------
13212
13213 function Is_Volatile_Prefix (N : Node_Id) return Boolean is
13214 Typ : constant Entity_Id := Etype (N);
13215
13216 begin
13217 if Is_Access_Type (Typ) then
13218 declare
13219 Dtyp : constant Entity_Id := Designated_Type (Typ);
13220
13221 begin
13222 return Is_Volatile (Dtyp)
13223 or else Has_Volatile_Components (Dtyp);
13224 end;
13225
13226 else
13227 return Object_Has_Volatile_Components (N);
13228 end if;
13229 end Is_Volatile_Prefix;
13230
13231 ------------------------------------
13232 -- Object_Has_Volatile_Components --
13233 ------------------------------------
13234
13235 function Object_Has_Volatile_Components (N : Node_Id) return Boolean is
13236 Typ : constant Entity_Id := Etype (N);
13237
13238 begin
13239 if Is_Volatile (Typ)
13240 or else Has_Volatile_Components (Typ)
13241 then
13242 return True;
13243
13244 elsif Is_Entity_Name (N)
13245 and then (Has_Volatile_Components (Entity (N))
13246 or else Is_Volatile (Entity (N)))
13247 then
13248 return True;
13249
13250 elsif Nkind (N) = N_Indexed_Component
13251 or else Nkind (N) = N_Selected_Component
13252 then
13253 return Is_Volatile_Prefix (Prefix (N));
13254
13255 else
13256 return False;
13257 end if;
13258 end Object_Has_Volatile_Components;
13259
13260 -- Start of processing for Is_Volatile_Object
13261
13262 begin
13263 if Nkind (N) = N_Defining_Identifier then
13264 return Is_Volatile (N) or else Is_Volatile (Etype (N));
13265
13266 elsif Nkind (N) = N_Expanded_Name then
13267 return Is_Volatile_Object (Entity (N));
13268
13269 elsif Is_Volatile (Etype (N))
13270 or else (Is_Entity_Name (N) and then Is_Volatile (Entity (N)))
13271 then
13272 return True;
13273
13274 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component)
13275 and then Is_Volatile_Prefix (Prefix (N))
13276 then
13277 return True;
13278
13279 elsif Nkind (N) = N_Selected_Component
13280 and then Is_Volatile (Entity (Selector_Name (N)))
13281 then
13282 return True;
13283
13284 else
13285 return False;
13286 end if;
13287 end Is_Volatile_Object;
13288
13289 ---------------------------
13290 -- Itype_Has_Declaration --
13291 ---------------------------
13292
13293 function Itype_Has_Declaration (Id : Entity_Id) return Boolean is
13294 begin
13295 pragma Assert (Is_Itype (Id));
13296 return Present (Parent (Id))
13297 and then Nkind_In (Parent (Id), N_Full_Type_Declaration,
13298 N_Subtype_Declaration)
13299 and then Defining_Entity (Parent (Id)) = Id;
13300 end Itype_Has_Declaration;
13301
13302 -------------------------
13303 -- Kill_Current_Values --
13304 -------------------------
13305
13306 procedure Kill_Current_Values
13307 (Ent : Entity_Id;
13308 Last_Assignment_Only : Boolean := False)
13309 is
13310 begin
13311 if Is_Assignable (Ent) then
13312 Set_Last_Assignment (Ent, Empty);
13313 end if;
13314
13315 if Is_Object (Ent) then
13316 if not Last_Assignment_Only then
13317 Kill_Checks (Ent);
13318 Set_Current_Value (Ent, Empty);
13319
13320 -- Do not reset the Is_Known_[Non_]Null and Is_Known_Valid flags
13321 -- for a constant. Once the constant is elaborated, its value is
13322 -- not changed, therefore the associated flags that describe the
13323 -- value should not be modified either.
13324
13325 if Ekind (Ent) = E_Constant then
13326 null;
13327
13328 -- Non-constant entities
13329
13330 else
13331 if not Can_Never_Be_Null (Ent) then
13332 Set_Is_Known_Non_Null (Ent, False);
13333 end if;
13334
13335 Set_Is_Known_Null (Ent, False);
13336
13337 -- Reset the Is_Known_Valid flag unless the type is always
13338 -- valid. This does not apply to a loop parameter because its
13339 -- bounds are defined by the loop header and therefore always
13340 -- valid.
13341
13342 if not Is_Known_Valid (Etype (Ent))
13343 and then Ekind (Ent) /= E_Loop_Parameter
13344 then
13345 Set_Is_Known_Valid (Ent, False);
13346 end if;
13347 end if;
13348 end if;
13349 end if;
13350 end Kill_Current_Values;
13351
13352 procedure Kill_Current_Values (Last_Assignment_Only : Boolean := False) is
13353 S : Entity_Id;
13354
13355 procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id);
13356 -- Clear current value for entity E and all entities chained to E
13357
13358 ------------------------------------------
13359 -- Kill_Current_Values_For_Entity_Chain --
13360 ------------------------------------------
13361
13362 procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id) is
13363 Ent : Entity_Id;
13364 begin
13365 Ent := E;
13366 while Present (Ent) loop
13367 Kill_Current_Values (Ent, Last_Assignment_Only);
13368 Next_Entity (Ent);
13369 end loop;
13370 end Kill_Current_Values_For_Entity_Chain;
13371
13372 -- Start of processing for Kill_Current_Values
13373
13374 begin
13375 -- Kill all saved checks, a special case of killing saved values
13376
13377 if not Last_Assignment_Only then
13378 Kill_All_Checks;
13379 end if;
13380
13381 -- Loop through relevant scopes, which includes the current scope and
13382 -- any parent scopes if the current scope is a block or a package.
13383
13384 S := Current_Scope;
13385 Scope_Loop : loop
13386
13387 -- Clear current values of all entities in current scope
13388
13389 Kill_Current_Values_For_Entity_Chain (First_Entity (S));
13390
13391 -- If scope is a package, also clear current values of all private
13392 -- entities in the scope.
13393
13394 if Is_Package_Or_Generic_Package (S)
13395 or else Is_Concurrent_Type (S)
13396 then
13397 Kill_Current_Values_For_Entity_Chain (First_Private_Entity (S));
13398 end if;
13399
13400 -- If this is a not a subprogram, deal with parents
13401
13402 if not Is_Subprogram (S) then
13403 S := Scope (S);
13404 exit Scope_Loop when S = Standard_Standard;
13405 else
13406 exit Scope_Loop;
13407 end if;
13408 end loop Scope_Loop;
13409 end Kill_Current_Values;
13410
13411 --------------------------
13412 -- Kill_Size_Check_Code --
13413 --------------------------
13414
13415 procedure Kill_Size_Check_Code (E : Entity_Id) is
13416 begin
13417 if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
13418 and then Present (Size_Check_Code (E))
13419 then
13420 Remove (Size_Check_Code (E));
13421 Set_Size_Check_Code (E, Empty);
13422 end if;
13423 end Kill_Size_Check_Code;
13424
13425 --------------------------
13426 -- Known_To_Be_Assigned --
13427 --------------------------
13428
13429 function Known_To_Be_Assigned (N : Node_Id) return Boolean is
13430 P : constant Node_Id := Parent (N);
13431
13432 begin
13433 case Nkind (P) is
13434
13435 -- Test left side of assignment
13436
13437 when N_Assignment_Statement =>
13438 return N = Name (P);
13439
13440 -- Function call arguments are never lvalues
13441
13442 when N_Function_Call =>
13443 return False;
13444
13445 -- Positional parameter for procedure or accept call
13446
13447 when N_Procedure_Call_Statement |
13448 N_Accept_Statement
13449 =>
13450 declare
13451 Proc : Entity_Id;
13452 Form : Entity_Id;
13453 Act : Node_Id;
13454
13455 begin
13456 Proc := Get_Subprogram_Entity (P);
13457
13458 if No (Proc) then
13459 return False;
13460 end if;
13461
13462 -- If we are not a list member, something is strange, so
13463 -- be conservative and return False.
13464
13465 if not Is_List_Member (N) then
13466 return False;
13467 end if;
13468
13469 -- We are going to find the right formal by stepping forward
13470 -- through the formals, as we step backwards in the actuals.
13471
13472 Form := First_Formal (Proc);
13473 Act := N;
13474 loop
13475 -- If no formal, something is weird, so be conservative
13476 -- and return False.
13477
13478 if No (Form) then
13479 return False;
13480 end if;
13481
13482 Prev (Act);
13483 exit when No (Act);
13484 Next_Formal (Form);
13485 end loop;
13486
13487 return Ekind (Form) /= E_In_Parameter;
13488 end;
13489
13490 -- Named parameter for procedure or accept call
13491
13492 when N_Parameter_Association =>
13493 declare
13494 Proc : Entity_Id;
13495 Form : Entity_Id;
13496
13497 begin
13498 Proc := Get_Subprogram_Entity (Parent (P));
13499
13500 if No (Proc) then
13501 return False;
13502 end if;
13503
13504 -- Loop through formals to find the one that matches
13505
13506 Form := First_Formal (Proc);
13507 loop
13508 -- If no matching formal, that's peculiar, some kind of
13509 -- previous error, so return False to be conservative.
13510 -- Actually this also happens in legal code in the case
13511 -- where P is a parameter association for an Extra_Formal???
13512
13513 if No (Form) then
13514 return False;
13515 end if;
13516
13517 -- Else test for match
13518
13519 if Chars (Form) = Chars (Selector_Name (P)) then
13520 return Ekind (Form) /= E_In_Parameter;
13521 end if;
13522
13523 Next_Formal (Form);
13524 end loop;
13525 end;
13526
13527 -- Test for appearing in a conversion that itself appears
13528 -- in an lvalue context, since this should be an lvalue.
13529
13530 when N_Type_Conversion =>
13531 return Known_To_Be_Assigned (P);
13532
13533 -- All other references are definitely not known to be modifications
13534
13535 when others =>
13536 return False;
13537
13538 end case;
13539 end Known_To_Be_Assigned;
13540
13541 ---------------------------
13542 -- Last_Source_Statement --
13543 ---------------------------
13544
13545 function Last_Source_Statement (HSS : Node_Id) return Node_Id is
13546 N : Node_Id;
13547
13548 begin
13549 N := Last (Statements (HSS));
13550 while Present (N) loop
13551 exit when Comes_From_Source (N);
13552 Prev (N);
13553 end loop;
13554
13555 return N;
13556 end Last_Source_Statement;
13557
13558 ----------------------------------
13559 -- Matching_Static_Array_Bounds --
13560 ----------------------------------
13561
13562 function Matching_Static_Array_Bounds
13563 (L_Typ : Node_Id;
13564 R_Typ : Node_Id) return Boolean
13565 is
13566 L_Ndims : constant Nat := Number_Dimensions (L_Typ);
13567 R_Ndims : constant Nat := Number_Dimensions (R_Typ);
13568
13569 L_Index : Node_Id;
13570 R_Index : Node_Id;
13571 L_Low : Node_Id;
13572 L_High : Node_Id;
13573 L_Len : Uint;
13574 R_Low : Node_Id;
13575 R_High : Node_Id;
13576 R_Len : Uint;
13577
13578 begin
13579 if L_Ndims /= R_Ndims then
13580 return False;
13581 end if;
13582
13583 -- Unconstrained types do not have static bounds
13584
13585 if not Is_Constrained (L_Typ) or else not Is_Constrained (R_Typ) then
13586 return False;
13587 end if;
13588
13589 -- First treat specially the first dimension, as the lower bound and
13590 -- length of string literals are not stored like those of arrays.
13591
13592 if Ekind (L_Typ) = E_String_Literal_Subtype then
13593 L_Low := String_Literal_Low_Bound (L_Typ);
13594 L_Len := String_Literal_Length (L_Typ);
13595 else
13596 L_Index := First_Index (L_Typ);
13597 Get_Index_Bounds (L_Index, L_Low, L_High);
13598
13599 if Is_OK_Static_Expression (L_Low)
13600 and then
13601 Is_OK_Static_Expression (L_High)
13602 then
13603 if Expr_Value (L_High) < Expr_Value (L_Low) then
13604 L_Len := Uint_0;
13605 else
13606 L_Len := (Expr_Value (L_High) - Expr_Value (L_Low)) + 1;
13607 end if;
13608 else
13609 return False;
13610 end if;
13611 end if;
13612
13613 if Ekind (R_Typ) = E_String_Literal_Subtype then
13614 R_Low := String_Literal_Low_Bound (R_Typ);
13615 R_Len := String_Literal_Length (R_Typ);
13616 else
13617 R_Index := First_Index (R_Typ);
13618 Get_Index_Bounds (R_Index, R_Low, R_High);
13619
13620 if Is_OK_Static_Expression (R_Low)
13621 and then
13622 Is_OK_Static_Expression (R_High)
13623 then
13624 if Expr_Value (R_High) < Expr_Value (R_Low) then
13625 R_Len := Uint_0;
13626 else
13627 R_Len := (Expr_Value (R_High) - Expr_Value (R_Low)) + 1;
13628 end if;
13629 else
13630 return False;
13631 end if;
13632 end if;
13633
13634 if (Is_OK_Static_Expression (L_Low)
13635 and then
13636 Is_OK_Static_Expression (R_Low))
13637 and then Expr_Value (L_Low) = Expr_Value (R_Low)
13638 and then L_Len = R_Len
13639 then
13640 null;
13641 else
13642 return False;
13643 end if;
13644
13645 -- Then treat all other dimensions
13646
13647 for Indx in 2 .. L_Ndims loop
13648 Next (L_Index);
13649 Next (R_Index);
13650
13651 Get_Index_Bounds (L_Index, L_Low, L_High);
13652 Get_Index_Bounds (R_Index, R_Low, R_High);
13653
13654 if (Is_OK_Static_Expression (L_Low) and then
13655 Is_OK_Static_Expression (L_High) and then
13656 Is_OK_Static_Expression (R_Low) and then
13657 Is_OK_Static_Expression (R_High))
13658 and then (Expr_Value (L_Low) = Expr_Value (R_Low)
13659 and then
13660 Expr_Value (L_High) = Expr_Value (R_High))
13661 then
13662 null;
13663 else
13664 return False;
13665 end if;
13666 end loop;
13667
13668 -- If we fall through the loop, all indexes matched
13669
13670 return True;
13671 end Matching_Static_Array_Bounds;
13672
13673 -------------------
13674 -- May_Be_Lvalue --
13675 -------------------
13676
13677 function May_Be_Lvalue (N : Node_Id) return Boolean is
13678 P : constant Node_Id := Parent (N);
13679
13680 begin
13681 case Nkind (P) is
13682
13683 -- Test left side of assignment
13684
13685 when N_Assignment_Statement =>
13686 return N = Name (P);
13687
13688 -- Test prefix of component or attribute. Note that the prefix of an
13689 -- explicit or implicit dereference cannot be an l-value.
13690
13691 when N_Attribute_Reference =>
13692 return N = Prefix (P)
13693 and then Name_Implies_Lvalue_Prefix (Attribute_Name (P));
13694
13695 -- For an expanded name, the name is an lvalue if the expanded name
13696 -- is an lvalue, but the prefix is never an lvalue, since it is just
13697 -- the scope where the name is found.
13698
13699 when N_Expanded_Name =>
13700 if N = Prefix (P) then
13701 return May_Be_Lvalue (P);
13702 else
13703 return False;
13704 end if;
13705
13706 -- For a selected component A.B, A is certainly an lvalue if A.B is.
13707 -- B is a little interesting, if we have A.B := 3, there is some
13708 -- discussion as to whether B is an lvalue or not, we choose to say
13709 -- it is. Note however that A is not an lvalue if it is of an access
13710 -- type since this is an implicit dereference.
13711
13712 when N_Selected_Component =>
13713 if N = Prefix (P)
13714 and then Present (Etype (N))
13715 and then Is_Access_Type (Etype (N))
13716 then
13717 return False;
13718 else
13719 return May_Be_Lvalue (P);
13720 end if;
13721
13722 -- For an indexed component or slice, the index or slice bounds is
13723 -- never an lvalue. The prefix is an lvalue if the indexed component
13724 -- or slice is an lvalue, except if it is an access type, where we
13725 -- have an implicit dereference.
13726
13727 when N_Indexed_Component | N_Slice =>
13728 if N /= Prefix (P)
13729 or else (Present (Etype (N)) and then Is_Access_Type (Etype (N)))
13730 then
13731 return False;
13732 else
13733 return May_Be_Lvalue (P);
13734 end if;
13735
13736 -- Prefix of a reference is an lvalue if the reference is an lvalue
13737
13738 when N_Reference =>
13739 return May_Be_Lvalue (P);
13740
13741 -- Prefix of explicit dereference is never an lvalue
13742
13743 when N_Explicit_Dereference =>
13744 return False;
13745
13746 -- Positional parameter for subprogram, entry, or accept call.
13747 -- In older versions of Ada function call arguments are never
13748 -- lvalues. In Ada 2012 functions can have in-out parameters.
13749
13750 when N_Subprogram_Call |
13751 N_Entry_Call_Statement |
13752 N_Accept_Statement
13753 =>
13754 if Nkind (P) = N_Function_Call and then Ada_Version < Ada_2012 then
13755 return False;
13756 end if;
13757
13758 -- The following mechanism is clumsy and fragile. A single flag
13759 -- set in Resolve_Actuals would be preferable ???
13760
13761 declare
13762 Proc : Entity_Id;
13763 Form : Entity_Id;
13764 Act : Node_Id;
13765
13766 begin
13767 Proc := Get_Subprogram_Entity (P);
13768
13769 if No (Proc) then
13770 return True;
13771 end if;
13772
13773 -- If we are not a list member, something is strange, so be
13774 -- conservative and return True.
13775
13776 if not Is_List_Member (N) then
13777 return True;
13778 end if;
13779
13780 -- We are going to find the right formal by stepping forward
13781 -- through the formals, as we step backwards in the actuals.
13782
13783 Form := First_Formal (Proc);
13784 Act := N;
13785 loop
13786 -- If no formal, something is weird, so be conservative and
13787 -- return True.
13788
13789 if No (Form) then
13790 return True;
13791 end if;
13792
13793 Prev (Act);
13794 exit when No (Act);
13795 Next_Formal (Form);
13796 end loop;
13797
13798 return Ekind (Form) /= E_In_Parameter;
13799 end;
13800
13801 -- Named parameter for procedure or accept call
13802
13803 when N_Parameter_Association =>
13804 declare
13805 Proc : Entity_Id;
13806 Form : Entity_Id;
13807
13808 begin
13809 Proc := Get_Subprogram_Entity (Parent (P));
13810
13811 if No (Proc) then
13812 return True;
13813 end if;
13814
13815 -- Loop through formals to find the one that matches
13816
13817 Form := First_Formal (Proc);
13818 loop
13819 -- If no matching formal, that's peculiar, some kind of
13820 -- previous error, so return True to be conservative.
13821 -- Actually happens with legal code for an unresolved call
13822 -- where we may get the wrong homonym???
13823
13824 if No (Form) then
13825 return True;
13826 end if;
13827
13828 -- Else test for match
13829
13830 if Chars (Form) = Chars (Selector_Name (P)) then
13831 return Ekind (Form) /= E_In_Parameter;
13832 end if;
13833
13834 Next_Formal (Form);
13835 end loop;
13836 end;
13837
13838 -- Test for appearing in a conversion that itself appears in an
13839 -- lvalue context, since this should be an lvalue.
13840
13841 when N_Type_Conversion =>
13842 return May_Be_Lvalue (P);
13843
13844 -- Test for appearance in object renaming declaration
13845
13846 when N_Object_Renaming_Declaration =>
13847 return True;
13848
13849 -- All other references are definitely not lvalues
13850
13851 when others =>
13852 return False;
13853
13854 end case;
13855 end May_Be_Lvalue;
13856
13857 -----------------------
13858 -- Mark_Coextensions --
13859 -----------------------
13860
13861 procedure Mark_Coextensions (Context_Nod : Node_Id; Root_Nod : Node_Id) is
13862 Is_Dynamic : Boolean;
13863 -- Indicates whether the context causes nested coextensions to be
13864 -- dynamic or static
13865
13866 function Mark_Allocator (N : Node_Id) return Traverse_Result;
13867 -- Recognize an allocator node and label it as a dynamic coextension
13868
13869 --------------------
13870 -- Mark_Allocator --
13871 --------------------
13872
13873 function Mark_Allocator (N : Node_Id) return Traverse_Result is
13874 begin
13875 if Nkind (N) = N_Allocator then
13876 if Is_Dynamic then
13877 Set_Is_Dynamic_Coextension (N);
13878
13879 -- If the allocator expression is potentially dynamic, it may
13880 -- be expanded out of order and require dynamic allocation
13881 -- anyway, so we treat the coextension itself as dynamic.
13882 -- Potential optimization ???
13883
13884 elsif Nkind (Expression (N)) = N_Qualified_Expression
13885 and then Nkind (Expression (Expression (N))) = N_Op_Concat
13886 then
13887 Set_Is_Dynamic_Coextension (N);
13888 else
13889 Set_Is_Static_Coextension (N);
13890 end if;
13891 end if;
13892
13893 return OK;
13894 end Mark_Allocator;
13895
13896 procedure Mark_Allocators is new Traverse_Proc (Mark_Allocator);
13897
13898 -- Start of processing Mark_Coextensions
13899
13900 begin
13901 case Nkind (Context_Nod) is
13902
13903 -- Comment here ???
13904
13905 when N_Assignment_Statement =>
13906 Is_Dynamic := Nkind (Expression (Context_Nod)) = N_Allocator;
13907
13908 -- An allocator that is a component of a returned aggregate
13909 -- must be dynamic.
13910
13911 when N_Simple_Return_Statement =>
13912 declare
13913 Expr : constant Node_Id := Expression (Context_Nod);
13914 begin
13915 Is_Dynamic :=
13916 Nkind (Expr) = N_Allocator
13917 or else
13918 (Nkind (Expr) = N_Qualified_Expression
13919 and then Nkind (Expression (Expr)) = N_Aggregate);
13920 end;
13921
13922 -- An alloctor within an object declaration in an extended return
13923 -- statement is of necessity dynamic.
13924
13925 when N_Object_Declaration =>
13926 Is_Dynamic := Nkind (Root_Nod) = N_Allocator
13927 or else
13928 Nkind (Parent (Context_Nod)) = N_Extended_Return_Statement;
13929
13930 -- This routine should not be called for constructs which may not
13931 -- contain coextensions.
13932
13933 when others =>
13934 raise Program_Error;
13935 end case;
13936
13937 Mark_Allocators (Root_Nod);
13938 end Mark_Coextensions;
13939
13940 ----------------------
13941 -- Needs_One_Actual --
13942 ----------------------
13943
13944 function Needs_One_Actual (E : Entity_Id) return Boolean is
13945 Formal : Entity_Id;
13946
13947 begin
13948 -- Ada 2005 or later, and formals present
13949
13950 if Ada_Version >= Ada_2005 and then Present (First_Formal (E)) then
13951 Formal := Next_Formal (First_Formal (E));
13952 while Present (Formal) loop
13953 if No (Default_Value (Formal)) then
13954 return False;
13955 end if;
13956
13957 Next_Formal (Formal);
13958 end loop;
13959
13960 return True;
13961
13962 -- Ada 83/95 or no formals
13963
13964 else
13965 return False;
13966 end if;
13967 end Needs_One_Actual;
13968
13969 ------------------------
13970 -- New_Copy_List_Tree --
13971 ------------------------
13972
13973 function New_Copy_List_Tree (List : List_Id) return List_Id is
13974 NL : List_Id;
13975 E : Node_Id;
13976
13977 begin
13978 if List = No_List then
13979 return No_List;
13980
13981 else
13982 NL := New_List;
13983 E := First (List);
13984
13985 while Present (E) loop
13986 Append (New_Copy_Tree (E), NL);
13987 E := Next (E);
13988 end loop;
13989
13990 return NL;
13991 end if;
13992 end New_Copy_List_Tree;
13993
13994 --------------------------------------------------
13995 -- New_Copy_Tree Auxiliary Data and Subprograms --
13996 --------------------------------------------------
13997
13998 use Atree.Unchecked_Access;
13999 use Atree_Private_Part;
14000
14001 -- Our approach here requires a two pass traversal of the tree. The
14002 -- first pass visits all nodes that eventually will be copied looking
14003 -- for defining Itypes. If any defining Itypes are found, then they are
14004 -- copied, and an entry is added to the replacement map. In the second
14005 -- phase, the tree is copied, using the replacement map to replace any
14006 -- Itype references within the copied tree.
14007
14008 -- The following hash tables are used if the Map supplied has more
14009 -- than hash threshold entries to speed up access to the map. If
14010 -- there are fewer entries, then the map is searched sequentially
14011 -- (because setting up a hash table for only a few entries takes
14012 -- more time than it saves.
14013
14014 function New_Copy_Hash (E : Entity_Id) return NCT_Header_Num;
14015 -- Hash function used for hash operations
14016
14017 -------------------
14018 -- New_Copy_Hash --
14019 -------------------
14020
14021 function New_Copy_Hash (E : Entity_Id) return NCT_Header_Num is
14022 begin
14023 return Nat (E) mod (NCT_Header_Num'Last + 1);
14024 end New_Copy_Hash;
14025
14026 ---------------
14027 -- NCT_Assoc --
14028 ---------------
14029
14030 -- The hash table NCT_Assoc associates old entities in the table
14031 -- with their corresponding new entities (i.e. the pairs of entries
14032 -- presented in the original Map argument are Key-Element pairs).
14033
14034 package NCT_Assoc is new Simple_HTable (
14035 Header_Num => NCT_Header_Num,
14036 Element => Entity_Id,
14037 No_Element => Empty,
14038 Key => Entity_Id,
14039 Hash => New_Copy_Hash,
14040 Equal => Types."=");
14041
14042 ---------------------
14043 -- NCT_Itype_Assoc --
14044 ---------------------
14045
14046 -- The hash table NCT_Itype_Assoc contains entries only for those
14047 -- old nodes which have a non-empty Associated_Node_For_Itype set.
14048 -- The key is the associated node, and the element is the new node
14049 -- itself (NOT the associated node for the new node).
14050
14051 package NCT_Itype_Assoc is new Simple_HTable (
14052 Header_Num => NCT_Header_Num,
14053 Element => Entity_Id,
14054 No_Element => Empty,
14055 Key => Entity_Id,
14056 Hash => New_Copy_Hash,
14057 Equal => Types."=");
14058
14059 -------------------
14060 -- New_Copy_Tree --
14061 -------------------
14062
14063 function New_Copy_Tree
14064 (Source : Node_Id;
14065 Map : Elist_Id := No_Elist;
14066 New_Sloc : Source_Ptr := No_Location;
14067 New_Scope : Entity_Id := Empty) return Node_Id
14068 is
14069 Actual_Map : Elist_Id := Map;
14070 -- This is the actual map for the copy. It is initialized with the
14071 -- given elements, and then enlarged as required for Itypes that are
14072 -- copied during the first phase of the copy operation. The visit
14073 -- procedures add elements to this map as Itypes are encountered.
14074 -- The reason we cannot use Map directly, is that it may well be
14075 -- (and normally is) initialized to No_Elist, and if we have mapped
14076 -- entities, we have to reset it to point to a real Elist.
14077
14078 function Assoc (N : Node_Or_Entity_Id) return Node_Id;
14079 -- Called during second phase to map entities into their corresponding
14080 -- copies using Actual_Map. If the argument is not an entity, or is not
14081 -- in Actual_Map, then it is returned unchanged.
14082
14083 procedure Build_NCT_Hash_Tables;
14084 -- Builds hash tables (number of elements >= threshold value)
14085
14086 function Copy_Elist_With_Replacement
14087 (Old_Elist : Elist_Id) return Elist_Id;
14088 -- Called during second phase to copy element list doing replacements
14089
14090 procedure Copy_Itype_With_Replacement (New_Itype : Entity_Id);
14091 -- Called during the second phase to process a copied Itype. The actual
14092 -- copy happened during the first phase (so that we could make the entry
14093 -- in the mapping), but we still have to deal with the descendents of
14094 -- the copied Itype and copy them where necessary.
14095
14096 function Copy_List_With_Replacement (Old_List : List_Id) return List_Id;
14097 -- Called during second phase to copy list doing replacements
14098
14099 function Copy_Node_With_Replacement (Old_Node : Node_Id) return Node_Id;
14100 -- Called during second phase to copy node doing replacements
14101
14102 procedure Visit_Elist (E : Elist_Id);
14103 -- Called during first phase to visit all elements of an Elist
14104
14105 procedure Visit_Field (F : Union_Id; N : Node_Id);
14106 -- Visit a single field, recursing to call Visit_Node or Visit_List
14107 -- if the field is a syntactic descendent of the current node (i.e.
14108 -- its parent is Node N).
14109
14110 procedure Visit_Itype (Old_Itype : Entity_Id);
14111 -- Called during first phase to visit subsidiary fields of a defining
14112 -- Itype, and also create a copy and make an entry in the replacement
14113 -- map for the new copy.
14114
14115 procedure Visit_List (L : List_Id);
14116 -- Called during first phase to visit all elements of a List
14117
14118 procedure Visit_Node (N : Node_Or_Entity_Id);
14119 -- Called during first phase to visit a node and all its subtrees
14120
14121 -----------
14122 -- Assoc --
14123 -----------
14124
14125 function Assoc (N : Node_Or_Entity_Id) return Node_Id is
14126 E : Elmt_Id;
14127 Ent : Entity_Id;
14128
14129 begin
14130 if not Has_Extension (N) or else No (Actual_Map) then
14131 return N;
14132
14133 elsif NCT_Hash_Tables_Used then
14134 Ent := NCT_Assoc.Get (Entity_Id (N));
14135
14136 if Present (Ent) then
14137 return Ent;
14138 else
14139 return N;
14140 end if;
14141
14142 -- No hash table used, do serial search
14143
14144 else
14145 E := First_Elmt (Actual_Map);
14146 while Present (E) loop
14147 if Node (E) = N then
14148 return Node (Next_Elmt (E));
14149 else
14150 E := Next_Elmt (Next_Elmt (E));
14151 end if;
14152 end loop;
14153 end if;
14154
14155 return N;
14156 end Assoc;
14157
14158 ---------------------------
14159 -- Build_NCT_Hash_Tables --
14160 ---------------------------
14161
14162 procedure Build_NCT_Hash_Tables is
14163 Elmt : Elmt_Id;
14164 Ent : Entity_Id;
14165 begin
14166 if NCT_Hash_Table_Setup then
14167 NCT_Assoc.Reset;
14168 NCT_Itype_Assoc.Reset;
14169 end if;
14170
14171 Elmt := First_Elmt (Actual_Map);
14172 while Present (Elmt) loop
14173 Ent := Node (Elmt);
14174
14175 -- Get new entity, and associate old and new
14176
14177 Next_Elmt (Elmt);
14178 NCT_Assoc.Set (Ent, Node (Elmt));
14179
14180 if Is_Type (Ent) then
14181 declare
14182 Anode : constant Entity_Id :=
14183 Associated_Node_For_Itype (Ent);
14184
14185 begin
14186 if Present (Anode) then
14187
14188 -- Enter a link between the associated node of the
14189 -- old Itype and the new Itype, for updating later
14190 -- when node is copied.
14191
14192 NCT_Itype_Assoc.Set (Anode, Node (Elmt));
14193 end if;
14194 end;
14195 end if;
14196
14197 Next_Elmt (Elmt);
14198 end loop;
14199
14200 NCT_Hash_Tables_Used := True;
14201 NCT_Hash_Table_Setup := True;
14202 end Build_NCT_Hash_Tables;
14203
14204 ---------------------------------
14205 -- Copy_Elist_With_Replacement --
14206 ---------------------------------
14207
14208 function Copy_Elist_With_Replacement
14209 (Old_Elist : Elist_Id) return Elist_Id
14210 is
14211 M : Elmt_Id;
14212 New_Elist : Elist_Id;
14213
14214 begin
14215 if No (Old_Elist) then
14216 return No_Elist;
14217
14218 else
14219 New_Elist := New_Elmt_List;
14220
14221 M := First_Elmt (Old_Elist);
14222 while Present (M) loop
14223 Append_Elmt (Copy_Node_With_Replacement (Node (M)), New_Elist);
14224 Next_Elmt (M);
14225 end loop;
14226 end if;
14227
14228 return New_Elist;
14229 end Copy_Elist_With_Replacement;
14230
14231 ---------------------------------
14232 -- Copy_Itype_With_Replacement --
14233 ---------------------------------
14234
14235 -- This routine exactly parallels its phase one analog Visit_Itype,
14236
14237 procedure Copy_Itype_With_Replacement (New_Itype : Entity_Id) is
14238 begin
14239 -- Translate Next_Entity, Scope and Etype fields, in case they
14240 -- reference entities that have been mapped into copies.
14241
14242 Set_Next_Entity (New_Itype, Assoc (Next_Entity (New_Itype)));
14243 Set_Etype (New_Itype, Assoc (Etype (New_Itype)));
14244
14245 if Present (New_Scope) then
14246 Set_Scope (New_Itype, New_Scope);
14247 else
14248 Set_Scope (New_Itype, Assoc (Scope (New_Itype)));
14249 end if;
14250
14251 -- Copy referenced fields
14252
14253 if Is_Discrete_Type (New_Itype) then
14254 Set_Scalar_Range (New_Itype,
14255 Copy_Node_With_Replacement (Scalar_Range (New_Itype)));
14256
14257 elsif Has_Discriminants (Base_Type (New_Itype)) then
14258 Set_Discriminant_Constraint (New_Itype,
14259 Copy_Elist_With_Replacement
14260 (Discriminant_Constraint (New_Itype)));
14261
14262 elsif Is_Array_Type (New_Itype) then
14263 if Present (First_Index (New_Itype)) then
14264 Set_First_Index (New_Itype,
14265 First (Copy_List_With_Replacement
14266 (List_Containing (First_Index (New_Itype)))));
14267 end if;
14268
14269 if Is_Packed (New_Itype) then
14270 Set_Packed_Array_Impl_Type (New_Itype,
14271 Copy_Node_With_Replacement
14272 (Packed_Array_Impl_Type (New_Itype)));
14273 end if;
14274 end if;
14275 end Copy_Itype_With_Replacement;
14276
14277 --------------------------------
14278 -- Copy_List_With_Replacement --
14279 --------------------------------
14280
14281 function Copy_List_With_Replacement
14282 (Old_List : List_Id) return List_Id
14283 is
14284 New_List : List_Id;
14285 E : Node_Id;
14286
14287 begin
14288 if Old_List = No_List then
14289 return No_List;
14290
14291 else
14292 New_List := Empty_List;
14293
14294 E := First (Old_List);
14295 while Present (E) loop
14296 Append (Copy_Node_With_Replacement (E), New_List);
14297 Next (E);
14298 end loop;
14299
14300 return New_List;
14301 end if;
14302 end Copy_List_With_Replacement;
14303
14304 --------------------------------
14305 -- Copy_Node_With_Replacement --
14306 --------------------------------
14307
14308 function Copy_Node_With_Replacement
14309 (Old_Node : Node_Id) return Node_Id
14310 is
14311 New_Node : Node_Id;
14312
14313 procedure Adjust_Named_Associations
14314 (Old_Node : Node_Id;
14315 New_Node : Node_Id);
14316 -- If a call node has named associations, these are chained through
14317 -- the First_Named_Actual, Next_Named_Actual links. These must be
14318 -- propagated separately to the new parameter list, because these
14319 -- are not syntactic fields.
14320
14321 function Copy_Field_With_Replacement
14322 (Field : Union_Id) return Union_Id;
14323 -- Given Field, which is a field of Old_Node, return a copy of it
14324 -- if it is a syntactic field (i.e. its parent is Node), setting
14325 -- the parent of the copy to poit to New_Node. Otherwise returns
14326 -- the field (possibly mapped if it is an entity).
14327
14328 -------------------------------
14329 -- Adjust_Named_Associations --
14330 -------------------------------
14331
14332 procedure Adjust_Named_Associations
14333 (Old_Node : Node_Id;
14334 New_Node : Node_Id)
14335 is
14336 Old_E : Node_Id;
14337 New_E : Node_Id;
14338
14339 Old_Next : Node_Id;
14340 New_Next : Node_Id;
14341
14342 begin
14343 Old_E := First (Parameter_Associations (Old_Node));
14344 New_E := First (Parameter_Associations (New_Node));
14345 while Present (Old_E) loop
14346 if Nkind (Old_E) = N_Parameter_Association
14347 and then Present (Next_Named_Actual (Old_E))
14348 then
14349 if First_Named_Actual (Old_Node)
14350 = Explicit_Actual_Parameter (Old_E)
14351 then
14352 Set_First_Named_Actual
14353 (New_Node, Explicit_Actual_Parameter (New_E));
14354 end if;
14355
14356 -- Now scan parameter list from the beginning,to locate
14357 -- next named actual, which can be out of order.
14358
14359 Old_Next := First (Parameter_Associations (Old_Node));
14360 New_Next := First (Parameter_Associations (New_Node));
14361
14362 while Nkind (Old_Next) /= N_Parameter_Association
14363 or else Explicit_Actual_Parameter (Old_Next) /=
14364 Next_Named_Actual (Old_E)
14365 loop
14366 Next (Old_Next);
14367 Next (New_Next);
14368 end loop;
14369
14370 Set_Next_Named_Actual
14371 (New_E, Explicit_Actual_Parameter (New_Next));
14372 end if;
14373
14374 Next (Old_E);
14375 Next (New_E);
14376 end loop;
14377 end Adjust_Named_Associations;
14378
14379 ---------------------------------
14380 -- Copy_Field_With_Replacement --
14381 ---------------------------------
14382
14383 function Copy_Field_With_Replacement
14384 (Field : Union_Id) return Union_Id
14385 is
14386 begin
14387 if Field = Union_Id (Empty) then
14388 return Field;
14389
14390 elsif Field in Node_Range then
14391 declare
14392 Old_N : constant Node_Id := Node_Id (Field);
14393 New_N : Node_Id;
14394
14395 begin
14396 -- If syntactic field, as indicated by the parent pointer
14397 -- being set, then copy the referenced node recursively.
14398
14399 if Parent (Old_N) = Old_Node then
14400 New_N := Copy_Node_With_Replacement (Old_N);
14401
14402 if New_N /= Old_N then
14403 Set_Parent (New_N, New_Node);
14404 end if;
14405
14406 -- For semantic fields, update possible entity reference
14407 -- from the replacement map.
14408
14409 else
14410 New_N := Assoc (Old_N);
14411 end if;
14412
14413 return Union_Id (New_N);
14414 end;
14415
14416 elsif Field in List_Range then
14417 declare
14418 Old_L : constant List_Id := List_Id (Field);
14419 New_L : List_Id;
14420
14421 begin
14422 -- If syntactic field, as indicated by the parent pointer,
14423 -- then recursively copy the entire referenced list.
14424
14425 if Parent (Old_L) = Old_Node then
14426 New_L := Copy_List_With_Replacement (Old_L);
14427 Set_Parent (New_L, New_Node);
14428
14429 -- For semantic list, just returned unchanged
14430
14431 else
14432 New_L := Old_L;
14433 end if;
14434
14435 return Union_Id (New_L);
14436 end;
14437
14438 -- Anything other than a list or a node is returned unchanged
14439
14440 else
14441 return Field;
14442 end if;
14443 end Copy_Field_With_Replacement;
14444
14445 -- Start of processing for Copy_Node_With_Replacement
14446
14447 begin
14448 if Old_Node <= Empty_Or_Error then
14449 return Old_Node;
14450
14451 elsif Has_Extension (Old_Node) then
14452 return Assoc (Old_Node);
14453
14454 else
14455 New_Node := New_Copy (Old_Node);
14456
14457 -- If the node we are copying is the associated node of a
14458 -- previously copied Itype, then adjust the associated node
14459 -- of the copy of that Itype accordingly.
14460
14461 if Present (Actual_Map) then
14462 declare
14463 E : Elmt_Id;
14464 Ent : Entity_Id;
14465
14466 begin
14467 -- Case of hash table used
14468
14469 if NCT_Hash_Tables_Used then
14470 Ent := NCT_Itype_Assoc.Get (Old_Node);
14471
14472 if Present (Ent) then
14473 Set_Associated_Node_For_Itype (Ent, New_Node);
14474 end if;
14475
14476 -- Case of no hash table used
14477
14478 else
14479 E := First_Elmt (Actual_Map);
14480 while Present (E) loop
14481 if Is_Itype (Node (E))
14482 and then
14483 Old_Node = Associated_Node_For_Itype (Node (E))
14484 then
14485 Set_Associated_Node_For_Itype
14486 (Node (Next_Elmt (E)), New_Node);
14487 end if;
14488
14489 E := Next_Elmt (Next_Elmt (E));
14490 end loop;
14491 end if;
14492 end;
14493 end if;
14494
14495 -- Recursively copy descendents
14496
14497 Set_Field1
14498 (New_Node, Copy_Field_With_Replacement (Field1 (New_Node)));
14499 Set_Field2
14500 (New_Node, Copy_Field_With_Replacement (Field2 (New_Node)));
14501 Set_Field3
14502 (New_Node, Copy_Field_With_Replacement (Field3 (New_Node)));
14503 Set_Field4
14504 (New_Node, Copy_Field_With_Replacement (Field4 (New_Node)));
14505 Set_Field5
14506 (New_Node, Copy_Field_With_Replacement (Field5 (New_Node)));
14507
14508 -- Adjust Sloc of new node if necessary
14509
14510 if New_Sloc /= No_Location then
14511 Set_Sloc (New_Node, New_Sloc);
14512
14513 -- If we adjust the Sloc, then we are essentially making
14514 -- a completely new node, so the Comes_From_Source flag
14515 -- should be reset to the proper default value.
14516
14517 Nodes.Table (New_Node).Comes_From_Source :=
14518 Default_Node.Comes_From_Source;
14519 end if;
14520
14521 -- If the node is call and has named associations,
14522 -- set the corresponding links in the copy.
14523
14524 if (Nkind (Old_Node) = N_Function_Call
14525 or else Nkind (Old_Node) = N_Entry_Call_Statement
14526 or else
14527 Nkind (Old_Node) = N_Procedure_Call_Statement)
14528 and then Present (First_Named_Actual (Old_Node))
14529 then
14530 Adjust_Named_Associations (Old_Node, New_Node);
14531 end if;
14532
14533 -- Reset First_Real_Statement for Handled_Sequence_Of_Statements.
14534 -- The replacement mechanism applies to entities, and is not used
14535 -- here. Eventually we may need a more general graph-copying
14536 -- routine. For now, do a sequential search to find desired node.
14537
14538 if Nkind (Old_Node) = N_Handled_Sequence_Of_Statements
14539 and then Present (First_Real_Statement (Old_Node))
14540 then
14541 declare
14542 Old_F : constant Node_Id := First_Real_Statement (Old_Node);
14543 N1, N2 : Node_Id;
14544
14545 begin
14546 N1 := First (Statements (Old_Node));
14547 N2 := First (Statements (New_Node));
14548
14549 while N1 /= Old_F loop
14550 Next (N1);
14551 Next (N2);
14552 end loop;
14553
14554 Set_First_Real_Statement (New_Node, N2);
14555 end;
14556 end if;
14557 end if;
14558
14559 -- All done, return copied node
14560
14561 return New_Node;
14562 end Copy_Node_With_Replacement;
14563
14564 -----------------
14565 -- Visit_Elist --
14566 -----------------
14567
14568 procedure Visit_Elist (E : Elist_Id) is
14569 Elmt : Elmt_Id;
14570 begin
14571 if Present (E) then
14572 Elmt := First_Elmt (E);
14573
14574 while Elmt /= No_Elmt loop
14575 Visit_Node (Node (Elmt));
14576 Next_Elmt (Elmt);
14577 end loop;
14578 end if;
14579 end Visit_Elist;
14580
14581 -----------------
14582 -- Visit_Field --
14583 -----------------
14584
14585 procedure Visit_Field (F : Union_Id; N : Node_Id) is
14586 begin
14587 if F = Union_Id (Empty) then
14588 return;
14589
14590 elsif F in Node_Range then
14591
14592 -- Copy node if it is syntactic, i.e. its parent pointer is
14593 -- set to point to the field that referenced it (certain
14594 -- Itypes will also meet this criterion, which is fine, since
14595 -- these are clearly Itypes that do need to be copied, since
14596 -- we are copying their parent.)
14597
14598 if Parent (Node_Id (F)) = N then
14599 Visit_Node (Node_Id (F));
14600 return;
14601
14602 -- Another case, if we are pointing to an Itype, then we want
14603 -- to copy it if its associated node is somewhere in the tree
14604 -- being copied.
14605
14606 -- Note: the exclusion of self-referential copies is just an
14607 -- optimization, since the search of the already copied list
14608 -- would catch it, but it is a common case (Etype pointing
14609 -- to itself for an Itype that is a base type).
14610
14611 elsif Has_Extension (Node_Id (F))
14612 and then Is_Itype (Entity_Id (F))
14613 and then Node_Id (F) /= N
14614 then
14615 declare
14616 P : Node_Id;
14617
14618 begin
14619 P := Associated_Node_For_Itype (Node_Id (F));
14620 while Present (P) loop
14621 if P = Source then
14622 Visit_Node (Node_Id (F));
14623 return;
14624 else
14625 P := Parent (P);
14626 end if;
14627 end loop;
14628
14629 -- An Itype whose parent is not being copied definitely
14630 -- should NOT be copied, since it does not belong in any
14631 -- sense to the copied subtree.
14632
14633 return;
14634 end;
14635 end if;
14636
14637 elsif F in List_Range and then Parent (List_Id (F)) = N then
14638 Visit_List (List_Id (F));
14639 return;
14640 end if;
14641 end Visit_Field;
14642
14643 -----------------
14644 -- Visit_Itype --
14645 -----------------
14646
14647 procedure Visit_Itype (Old_Itype : Entity_Id) is
14648 New_Itype : Entity_Id;
14649 E : Elmt_Id;
14650 Ent : Entity_Id;
14651
14652 begin
14653 -- Itypes that describe the designated type of access to subprograms
14654 -- have the structure of subprogram declarations, with signatures,
14655 -- etc. Either we duplicate the signatures completely, or choose to
14656 -- share such itypes, which is fine because their elaboration will
14657 -- have no side effects.
14658
14659 if Ekind (Old_Itype) = E_Subprogram_Type then
14660 return;
14661 end if;
14662
14663 New_Itype := New_Copy (Old_Itype);
14664
14665 -- The new Itype has all the attributes of the old one, and
14666 -- we just copy the contents of the entity. However, the back-end
14667 -- needs different names for debugging purposes, so we create a
14668 -- new internal name for it in all cases.
14669
14670 Set_Chars (New_Itype, New_Internal_Name ('T'));
14671
14672 -- If our associated node is an entity that has already been copied,
14673 -- then set the associated node of the copy to point to the right
14674 -- copy. If we have copied an Itype that is itself the associated
14675 -- node of some previously copied Itype, then we set the right
14676 -- pointer in the other direction.
14677
14678 if Present (Actual_Map) then
14679
14680 -- Case of hash tables used
14681
14682 if NCT_Hash_Tables_Used then
14683
14684 Ent := NCT_Assoc.Get (Associated_Node_For_Itype (Old_Itype));
14685
14686 if Present (Ent) then
14687 Set_Associated_Node_For_Itype (New_Itype, Ent);
14688 end if;
14689
14690 Ent := NCT_Itype_Assoc.Get (Old_Itype);
14691 if Present (Ent) then
14692 Set_Associated_Node_For_Itype (Ent, New_Itype);
14693
14694 -- If the hash table has no association for this Itype and
14695 -- its associated node, enter one now.
14696
14697 else
14698 NCT_Itype_Assoc.Set
14699 (Associated_Node_For_Itype (Old_Itype), New_Itype);
14700 end if;
14701
14702 -- Case of hash tables not used
14703
14704 else
14705 E := First_Elmt (Actual_Map);
14706 while Present (E) loop
14707 if Associated_Node_For_Itype (Old_Itype) = Node (E) then
14708 Set_Associated_Node_For_Itype
14709 (New_Itype, Node (Next_Elmt (E)));
14710 end if;
14711
14712 if Is_Type (Node (E))
14713 and then Old_Itype = Associated_Node_For_Itype (Node (E))
14714 then
14715 Set_Associated_Node_For_Itype
14716 (Node (Next_Elmt (E)), New_Itype);
14717 end if;
14718
14719 E := Next_Elmt (Next_Elmt (E));
14720 end loop;
14721 end if;
14722 end if;
14723
14724 if Present (Freeze_Node (New_Itype)) then
14725 Set_Is_Frozen (New_Itype, False);
14726 Set_Freeze_Node (New_Itype, Empty);
14727 end if;
14728
14729 -- Add new association to map
14730
14731 if No (Actual_Map) then
14732 Actual_Map := New_Elmt_List;
14733 end if;
14734
14735 Append_Elmt (Old_Itype, Actual_Map);
14736 Append_Elmt (New_Itype, Actual_Map);
14737
14738 if NCT_Hash_Tables_Used then
14739 NCT_Assoc.Set (Old_Itype, New_Itype);
14740
14741 else
14742 NCT_Table_Entries := NCT_Table_Entries + 1;
14743
14744 if NCT_Table_Entries > NCT_Hash_Threshold then
14745 Build_NCT_Hash_Tables;
14746 end if;
14747 end if;
14748
14749 -- If a record subtype is simply copied, the entity list will be
14750 -- shared. Thus cloned_Subtype must be set to indicate the sharing.
14751
14752 if Ekind_In (Old_Itype, E_Record_Subtype, E_Class_Wide_Subtype) then
14753 Set_Cloned_Subtype (New_Itype, Old_Itype);
14754 end if;
14755
14756 -- Visit descendents that eventually get copied
14757
14758 Visit_Field (Union_Id (Etype (Old_Itype)), Old_Itype);
14759
14760 if Is_Discrete_Type (Old_Itype) then
14761 Visit_Field (Union_Id (Scalar_Range (Old_Itype)), Old_Itype);
14762
14763 elsif Has_Discriminants (Base_Type (Old_Itype)) then
14764 -- ??? This should involve call to Visit_Field
14765 Visit_Elist (Discriminant_Constraint (Old_Itype));
14766
14767 elsif Is_Array_Type (Old_Itype) then
14768 if Present (First_Index (Old_Itype)) then
14769 Visit_Field (Union_Id (List_Containing
14770 (First_Index (Old_Itype))),
14771 Old_Itype);
14772 end if;
14773
14774 if Is_Packed (Old_Itype) then
14775 Visit_Field (Union_Id (Packed_Array_Impl_Type (Old_Itype)),
14776 Old_Itype);
14777 end if;
14778 end if;
14779 end Visit_Itype;
14780
14781 ----------------
14782 -- Visit_List --
14783 ----------------
14784
14785 procedure Visit_List (L : List_Id) is
14786 N : Node_Id;
14787 begin
14788 if L /= No_List then
14789 N := First (L);
14790
14791 while Present (N) loop
14792 Visit_Node (N);
14793 Next (N);
14794 end loop;
14795 end if;
14796 end Visit_List;
14797
14798 ----------------
14799 -- Visit_Node --
14800 ----------------
14801
14802 procedure Visit_Node (N : Node_Or_Entity_Id) is
14803
14804 -- Start of processing for Visit_Node
14805
14806 begin
14807 -- Handle case of an Itype, which must be copied
14808
14809 if Has_Extension (N) and then Is_Itype (N) then
14810
14811 -- Nothing to do if already in the list. This can happen with an
14812 -- Itype entity that appears more than once in the tree.
14813 -- Note that we do not want to visit descendents in this case.
14814
14815 -- Test for already in list when hash table is used
14816
14817 if NCT_Hash_Tables_Used then
14818 if Present (NCT_Assoc.Get (Entity_Id (N))) then
14819 return;
14820 end if;
14821
14822 -- Test for already in list when hash table not used
14823
14824 else
14825 declare
14826 E : Elmt_Id;
14827 begin
14828 if Present (Actual_Map) then
14829 E := First_Elmt (Actual_Map);
14830 while Present (E) loop
14831 if Node (E) = N then
14832 return;
14833 else
14834 E := Next_Elmt (Next_Elmt (E));
14835 end if;
14836 end loop;
14837 end if;
14838 end;
14839 end if;
14840
14841 Visit_Itype (N);
14842 end if;
14843
14844 -- Visit descendents
14845
14846 Visit_Field (Field1 (N), N);
14847 Visit_Field (Field2 (N), N);
14848 Visit_Field (Field3 (N), N);
14849 Visit_Field (Field4 (N), N);
14850 Visit_Field (Field5 (N), N);
14851 end Visit_Node;
14852
14853 -- Start of processing for New_Copy_Tree
14854
14855 begin
14856 Actual_Map := Map;
14857
14858 -- See if we should use hash table
14859
14860 if No (Actual_Map) then
14861 NCT_Hash_Tables_Used := False;
14862
14863 else
14864 declare
14865 Elmt : Elmt_Id;
14866
14867 begin
14868 NCT_Table_Entries := 0;
14869
14870 Elmt := First_Elmt (Actual_Map);
14871 while Present (Elmt) loop
14872 NCT_Table_Entries := NCT_Table_Entries + 1;
14873 Next_Elmt (Elmt);
14874 Next_Elmt (Elmt);
14875 end loop;
14876
14877 if NCT_Table_Entries > NCT_Hash_Threshold then
14878 Build_NCT_Hash_Tables;
14879 else
14880 NCT_Hash_Tables_Used := False;
14881 end if;
14882 end;
14883 end if;
14884
14885 -- Hash table set up if required, now start phase one by visiting
14886 -- top node (we will recursively visit the descendents).
14887
14888 Visit_Node (Source);
14889
14890 -- Now the second phase of the copy can start. First we process
14891 -- all the mapped entities, copying their descendents.
14892
14893 if Present (Actual_Map) then
14894 declare
14895 Elmt : Elmt_Id;
14896 New_Itype : Entity_Id;
14897 begin
14898 Elmt := First_Elmt (Actual_Map);
14899 while Present (Elmt) loop
14900 Next_Elmt (Elmt);
14901 New_Itype := Node (Elmt);
14902 Copy_Itype_With_Replacement (New_Itype);
14903 Next_Elmt (Elmt);
14904 end loop;
14905 end;
14906 end if;
14907
14908 -- Now we can copy the actual tree
14909
14910 return Copy_Node_With_Replacement (Source);
14911 end New_Copy_Tree;
14912
14913 -------------------------
14914 -- New_External_Entity --
14915 -------------------------
14916
14917 function New_External_Entity
14918 (Kind : Entity_Kind;
14919 Scope_Id : Entity_Id;
14920 Sloc_Value : Source_Ptr;
14921 Related_Id : Entity_Id;
14922 Suffix : Character;
14923 Suffix_Index : Nat := 0;
14924 Prefix : Character := ' ') return Entity_Id
14925 is
14926 N : constant Entity_Id :=
14927 Make_Defining_Identifier (Sloc_Value,
14928 New_External_Name
14929 (Chars (Related_Id), Suffix, Suffix_Index, Prefix));
14930
14931 begin
14932 Set_Ekind (N, Kind);
14933 Set_Is_Internal (N, True);
14934 Append_Entity (N, Scope_Id);
14935 Set_Public_Status (N);
14936
14937 if Kind in Type_Kind then
14938 Init_Size_Align (N);
14939 end if;
14940
14941 return N;
14942 end New_External_Entity;
14943
14944 -------------------------
14945 -- New_Internal_Entity --
14946 -------------------------
14947
14948 function New_Internal_Entity
14949 (Kind : Entity_Kind;
14950 Scope_Id : Entity_Id;
14951 Sloc_Value : Source_Ptr;
14952 Id_Char : Character) return Entity_Id
14953 is
14954 N : constant Entity_Id := Make_Temporary (Sloc_Value, Id_Char);
14955
14956 begin
14957 Set_Ekind (N, Kind);
14958 Set_Is_Internal (N, True);
14959 Append_Entity (N, Scope_Id);
14960
14961 if Kind in Type_Kind then
14962 Init_Size_Align (N);
14963 end if;
14964
14965 return N;
14966 end New_Internal_Entity;
14967
14968 -----------------
14969 -- Next_Actual --
14970 -----------------
14971
14972 function Next_Actual (Actual_Id : Node_Id) return Node_Id is
14973 N : Node_Id;
14974
14975 begin
14976 -- If we are pointing at a positional parameter, it is a member of a
14977 -- node list (the list of parameters), and the next parameter is the
14978 -- next node on the list, unless we hit a parameter association, then
14979 -- we shift to using the chain whose head is the First_Named_Actual in
14980 -- the parent, and then is threaded using the Next_Named_Actual of the
14981 -- Parameter_Association. All this fiddling is because the original node
14982 -- list is in the textual call order, and what we need is the
14983 -- declaration order.
14984
14985 if Is_List_Member (Actual_Id) then
14986 N := Next (Actual_Id);
14987
14988 if Nkind (N) = N_Parameter_Association then
14989 return First_Named_Actual (Parent (Actual_Id));
14990 else
14991 return N;
14992 end if;
14993
14994 else
14995 return Next_Named_Actual (Parent (Actual_Id));
14996 end if;
14997 end Next_Actual;
14998
14999 procedure Next_Actual (Actual_Id : in out Node_Id) is
15000 begin
15001 Actual_Id := Next_Actual (Actual_Id);
15002 end Next_Actual;
15003
15004 -----------------------
15005 -- Normalize_Actuals --
15006 -----------------------
15007
15008 -- Chain actuals according to formals of subprogram. If there are no named
15009 -- associations, the chain is simply the list of Parameter Associations,
15010 -- since the order is the same as the declaration order. If there are named
15011 -- associations, then the First_Named_Actual field in the N_Function_Call
15012 -- or N_Procedure_Call_Statement node points to the Parameter_Association
15013 -- node for the parameter that comes first in declaration order. The
15014 -- remaining named parameters are then chained in declaration order using
15015 -- Next_Named_Actual.
15016
15017 -- This routine also verifies that the number of actuals is compatible with
15018 -- the number and default values of formals, but performs no type checking
15019 -- (type checking is done by the caller).
15020
15021 -- If the matching succeeds, Success is set to True and the caller proceeds
15022 -- with type-checking. If the match is unsuccessful, then Success is set to
15023 -- False, and the caller attempts a different interpretation, if there is
15024 -- one.
15025
15026 -- If the flag Report is on, the call is not overloaded, and a failure to
15027 -- match can be reported here, rather than in the caller.
15028
15029 procedure Normalize_Actuals
15030 (N : Node_Id;
15031 S : Entity_Id;
15032 Report : Boolean;
15033 Success : out Boolean)
15034 is
15035 Actuals : constant List_Id := Parameter_Associations (N);
15036 Actual : Node_Id := Empty;
15037 Formal : Entity_Id;
15038 Last : Node_Id := Empty;
15039 First_Named : Node_Id := Empty;
15040 Found : Boolean;
15041
15042 Formals_To_Match : Integer := 0;
15043 Actuals_To_Match : Integer := 0;
15044
15045 procedure Chain (A : Node_Id);
15046 -- Add named actual at the proper place in the list, using the
15047 -- Next_Named_Actual link.
15048
15049 function Reporting return Boolean;
15050 -- Determines if an error is to be reported. To report an error, we
15051 -- need Report to be True, and also we do not report errors caused
15052 -- by calls to init procs that occur within other init procs. Such
15053 -- errors must always be cascaded errors, since if all the types are
15054 -- declared correctly, the compiler will certainly build decent calls.
15055
15056 -----------
15057 -- Chain --
15058 -----------
15059
15060 procedure Chain (A : Node_Id) is
15061 begin
15062 if No (Last) then
15063
15064 -- Call node points to first actual in list
15065
15066 Set_First_Named_Actual (N, Explicit_Actual_Parameter (A));
15067
15068 else
15069 Set_Next_Named_Actual (Last, Explicit_Actual_Parameter (A));
15070 end if;
15071
15072 Last := A;
15073 Set_Next_Named_Actual (Last, Empty);
15074 end Chain;
15075
15076 ---------------
15077 -- Reporting --
15078 ---------------
15079
15080 function Reporting return Boolean is
15081 begin
15082 if not Report then
15083 return False;
15084
15085 elsif not Within_Init_Proc then
15086 return True;
15087
15088 elsif Is_Init_Proc (Entity (Name (N))) then
15089 return False;
15090
15091 else
15092 return True;
15093 end if;
15094 end Reporting;
15095
15096 -- Start of processing for Normalize_Actuals
15097
15098 begin
15099 if Is_Access_Type (S) then
15100
15101 -- The name in the call is a function call that returns an access
15102 -- to subprogram. The designated type has the list of formals.
15103
15104 Formal := First_Formal (Designated_Type (S));
15105 else
15106 Formal := First_Formal (S);
15107 end if;
15108
15109 while Present (Formal) loop
15110 Formals_To_Match := Formals_To_Match + 1;
15111 Next_Formal (Formal);
15112 end loop;
15113
15114 -- Find if there is a named association, and verify that no positional
15115 -- associations appear after named ones.
15116
15117 if Present (Actuals) then
15118 Actual := First (Actuals);
15119 end if;
15120
15121 while Present (Actual)
15122 and then Nkind (Actual) /= N_Parameter_Association
15123 loop
15124 Actuals_To_Match := Actuals_To_Match + 1;
15125 Next (Actual);
15126 end loop;
15127
15128 if No (Actual) and Actuals_To_Match = Formals_To_Match then
15129
15130 -- Most common case: positional notation, no defaults
15131
15132 Success := True;
15133 return;
15134
15135 elsif Actuals_To_Match > Formals_To_Match then
15136
15137 -- Too many actuals: will not work
15138
15139 if Reporting then
15140 if Is_Entity_Name (Name (N)) then
15141 Error_Msg_N ("too many arguments in call to&", Name (N));
15142 else
15143 Error_Msg_N ("too many arguments in call", N);
15144 end if;
15145 end if;
15146
15147 Success := False;
15148 return;
15149 end if;
15150
15151 First_Named := Actual;
15152
15153 while Present (Actual) loop
15154 if Nkind (Actual) /= N_Parameter_Association then
15155 Error_Msg_N
15156 ("positional parameters not allowed after named ones", Actual);
15157 Success := False;
15158 return;
15159
15160 else
15161 Actuals_To_Match := Actuals_To_Match + 1;
15162 end if;
15163
15164 Next (Actual);
15165 end loop;
15166
15167 if Present (Actuals) then
15168 Actual := First (Actuals);
15169 end if;
15170
15171 Formal := First_Formal (S);
15172 while Present (Formal) loop
15173
15174 -- Match the formals in order. If the corresponding actual is
15175 -- positional, nothing to do. Else scan the list of named actuals
15176 -- to find the one with the right name.
15177
15178 if Present (Actual)
15179 and then Nkind (Actual) /= N_Parameter_Association
15180 then
15181 Next (Actual);
15182 Actuals_To_Match := Actuals_To_Match - 1;
15183 Formals_To_Match := Formals_To_Match - 1;
15184
15185 else
15186 -- For named parameters, search the list of actuals to find
15187 -- one that matches the next formal name.
15188
15189 Actual := First_Named;
15190 Found := False;
15191 while Present (Actual) loop
15192 if Chars (Selector_Name (Actual)) = Chars (Formal) then
15193 Found := True;
15194 Chain (Actual);
15195 Actuals_To_Match := Actuals_To_Match - 1;
15196 Formals_To_Match := Formals_To_Match - 1;
15197 exit;
15198 end if;
15199
15200 Next (Actual);
15201 end loop;
15202
15203 if not Found then
15204 if Ekind (Formal) /= E_In_Parameter
15205 or else No (Default_Value (Formal))
15206 then
15207 if Reporting then
15208 if (Comes_From_Source (S)
15209 or else Sloc (S) = Standard_Location)
15210 and then Is_Overloadable (S)
15211 then
15212 if No (Actuals)
15213 and then
15214 Nkind_In (Parent (N), N_Procedure_Call_Statement,
15215 N_Function_Call,
15216 N_Parameter_Association)
15217 and then Ekind (S) /= E_Function
15218 then
15219 Set_Etype (N, Etype (S));
15220
15221 else
15222 Error_Msg_Name_1 := Chars (S);
15223 Error_Msg_Sloc := Sloc (S);
15224 Error_Msg_NE
15225 ("missing argument for parameter & "
15226 & "in call to % declared #", N, Formal);
15227 end if;
15228
15229 elsif Is_Overloadable (S) then
15230 Error_Msg_Name_1 := Chars (S);
15231
15232 -- Point to type derivation that generated the
15233 -- operation.
15234
15235 Error_Msg_Sloc := Sloc (Parent (S));
15236
15237 Error_Msg_NE
15238 ("missing argument for parameter & "
15239 & "in call to % (inherited) #", N, Formal);
15240
15241 else
15242 Error_Msg_NE
15243 ("missing argument for parameter &", N, Formal);
15244 end if;
15245 end if;
15246
15247 Success := False;
15248 return;
15249
15250 else
15251 Formals_To_Match := Formals_To_Match - 1;
15252 end if;
15253 end if;
15254 end if;
15255
15256 Next_Formal (Formal);
15257 end loop;
15258
15259 if Formals_To_Match = 0 and then Actuals_To_Match = 0 then
15260 Success := True;
15261 return;
15262
15263 else
15264 if Reporting then
15265
15266 -- Find some superfluous named actual that did not get
15267 -- attached to the list of associations.
15268
15269 Actual := First (Actuals);
15270 while Present (Actual) loop
15271 if Nkind (Actual) = N_Parameter_Association
15272 and then Actual /= Last
15273 and then No (Next_Named_Actual (Actual))
15274 then
15275 Error_Msg_N ("unmatched actual & in call",
15276 Selector_Name (Actual));
15277 exit;
15278 end if;
15279
15280 Next (Actual);
15281 end loop;
15282 end if;
15283
15284 Success := False;
15285 return;
15286 end if;
15287 end Normalize_Actuals;
15288
15289 --------------------------------
15290 -- Note_Possible_Modification --
15291 --------------------------------
15292
15293 procedure Note_Possible_Modification (N : Node_Id; Sure : Boolean) is
15294 Modification_Comes_From_Source : constant Boolean :=
15295 Comes_From_Source (Parent (N));
15296
15297 Ent : Entity_Id;
15298 Exp : Node_Id;
15299
15300 begin
15301 -- Loop to find referenced entity, if there is one
15302
15303 Exp := N;
15304 loop
15305 Ent := Empty;
15306
15307 if Is_Entity_Name (Exp) then
15308 Ent := Entity (Exp);
15309
15310 -- If the entity is missing, it is an undeclared identifier,
15311 -- and there is nothing to annotate.
15312
15313 if No (Ent) then
15314 return;
15315 end if;
15316
15317 elsif Nkind (Exp) = N_Explicit_Dereference then
15318 declare
15319 P : constant Node_Id := Prefix (Exp);
15320
15321 begin
15322 -- In formal verification mode, keep track of all reads and
15323 -- writes through explicit dereferences.
15324
15325 if GNATprove_Mode then
15326 SPARK_Specific.Generate_Dereference (N, 'm');
15327 end if;
15328
15329 if Nkind (P) = N_Selected_Component
15330 and then Present (Entry_Formal (Entity (Selector_Name (P))))
15331 then
15332 -- Case of a reference to an entry formal
15333
15334 Ent := Entry_Formal (Entity (Selector_Name (P)));
15335
15336 elsif Nkind (P) = N_Identifier
15337 and then Nkind (Parent (Entity (P))) = N_Object_Declaration
15338 and then Present (Expression (Parent (Entity (P))))
15339 and then Nkind (Expression (Parent (Entity (P)))) =
15340 N_Reference
15341 then
15342 -- Case of a reference to a value on which side effects have
15343 -- been removed.
15344
15345 Exp := Prefix (Expression (Parent (Entity (P))));
15346 goto Continue;
15347
15348 else
15349 return;
15350 end if;
15351 end;
15352
15353 elsif Nkind_In (Exp, N_Type_Conversion,
15354 N_Unchecked_Type_Conversion)
15355 then
15356 Exp := Expression (Exp);
15357 goto Continue;
15358
15359 elsif Nkind_In (Exp, N_Slice,
15360 N_Indexed_Component,
15361 N_Selected_Component)
15362 then
15363 -- Special check, if the prefix is an access type, then return
15364 -- since we are modifying the thing pointed to, not the prefix.
15365 -- When we are expanding, most usually the prefix is replaced
15366 -- by an explicit dereference, and this test is not needed, but
15367 -- in some cases (notably -gnatc mode and generics) when we do
15368 -- not do full expansion, we need this special test.
15369
15370 if Is_Access_Type (Etype (Prefix (Exp))) then
15371 return;
15372
15373 -- Otherwise go to prefix and keep going
15374
15375 else
15376 Exp := Prefix (Exp);
15377 goto Continue;
15378 end if;
15379
15380 -- All other cases, not a modification
15381
15382 else
15383 return;
15384 end if;
15385
15386 -- Now look for entity being referenced
15387
15388 if Present (Ent) then
15389 if Is_Object (Ent) then
15390 if Comes_From_Source (Exp)
15391 or else Modification_Comes_From_Source
15392 then
15393 -- Give warning if pragma unmodified given and we are
15394 -- sure this is a modification.
15395
15396 if Has_Pragma_Unmodified (Ent) and then Sure then
15397 Error_Msg_NE ("??pragma Unmodified given for &!", N, Ent);
15398 end if;
15399
15400 Set_Never_Set_In_Source (Ent, False);
15401 end if;
15402
15403 Set_Is_True_Constant (Ent, False);
15404 Set_Current_Value (Ent, Empty);
15405 Set_Is_Known_Null (Ent, False);
15406
15407 if not Can_Never_Be_Null (Ent) then
15408 Set_Is_Known_Non_Null (Ent, False);
15409 end if;
15410
15411 -- Follow renaming chain
15412
15413 if (Ekind (Ent) = E_Variable or else Ekind (Ent) = E_Constant)
15414 and then Present (Renamed_Object (Ent))
15415 then
15416 Exp := Renamed_Object (Ent);
15417
15418 -- If the entity is the loop variable in an iteration over
15419 -- a container, retrieve container expression to indicate
15420 -- possible modification.
15421
15422 if Present (Related_Expression (Ent))
15423 and then Nkind (Parent (Related_Expression (Ent))) =
15424 N_Iterator_Specification
15425 then
15426 Exp := Original_Node (Related_Expression (Ent));
15427 end if;
15428
15429 goto Continue;
15430
15431 -- The expression may be the renaming of a subcomponent of an
15432 -- array or container. The assignment to the subcomponent is
15433 -- a modification of the container.
15434
15435 elsif Comes_From_Source (Original_Node (Exp))
15436 and then Nkind_In (Original_Node (Exp), N_Selected_Component,
15437 N_Indexed_Component)
15438 then
15439 Exp := Prefix (Original_Node (Exp));
15440 goto Continue;
15441 end if;
15442
15443 -- Generate a reference only if the assignment comes from
15444 -- source. This excludes, for example, calls to a dispatching
15445 -- assignment operation when the left-hand side is tagged. In
15446 -- GNATprove mode, we need those references also on generated
15447 -- code, as these are used to compute the local effects of
15448 -- subprograms.
15449
15450 if Modification_Comes_From_Source or GNATprove_Mode then
15451 Generate_Reference (Ent, Exp, 'm');
15452
15453 -- If the target of the assignment is the bound variable
15454 -- in an iterator, indicate that the corresponding array
15455 -- or container is also modified.
15456
15457 if Ada_Version >= Ada_2012
15458 and then Nkind (Parent (Ent)) = N_Iterator_Specification
15459 then
15460 declare
15461 Domain : constant Node_Id := Name (Parent (Ent));
15462
15463 begin
15464 -- TBD : in the full version of the construct, the
15465 -- domain of iteration can be given by an expression.
15466
15467 if Is_Entity_Name (Domain) then
15468 Generate_Reference (Entity (Domain), Exp, 'm');
15469 Set_Is_True_Constant (Entity (Domain), False);
15470 Set_Never_Set_In_Source (Entity (Domain), False);
15471 end if;
15472 end;
15473 end if;
15474 end if;
15475
15476 Check_Nested_Access (Ent);
15477 end if;
15478
15479 Kill_Checks (Ent);
15480
15481 -- If we are sure this is a modification from source, and we know
15482 -- this modifies a constant, then give an appropriate warning.
15483
15484 if Overlays_Constant (Ent)
15485 and then (Modification_Comes_From_Source and Sure)
15486 then
15487 declare
15488 A : constant Node_Id := Address_Clause (Ent);
15489 begin
15490 if Present (A) then
15491 declare
15492 Exp : constant Node_Id := Expression (A);
15493 begin
15494 if Nkind (Exp) = N_Attribute_Reference
15495 and then Attribute_Name (Exp) = Name_Address
15496 and then Is_Entity_Name (Prefix (Exp))
15497 then
15498 Error_Msg_Sloc := Sloc (A);
15499 Error_Msg_NE
15500 ("constant& may be modified via address "
15501 & "clause#??", N, Entity (Prefix (Exp)));
15502 end if;
15503 end;
15504 end if;
15505 end;
15506 end if;
15507
15508 return;
15509 end if;
15510
15511 <<Continue>>
15512 null;
15513 end loop;
15514 end Note_Possible_Modification;
15515
15516 -------------------------
15517 -- Object_Access_Level --
15518 -------------------------
15519
15520 -- Returns the static accessibility level of the view denoted by Obj. Note
15521 -- that the value returned is the result of a call to Scope_Depth. Only
15522 -- scope depths associated with dynamic scopes can actually be returned.
15523 -- Since only relative levels matter for accessibility checking, the fact
15524 -- that the distance between successive levels of accessibility is not
15525 -- always one is immaterial (invariant: if level(E2) is deeper than
15526 -- level(E1), then Scope_Depth(E1) < Scope_Depth(E2)).
15527
15528 function Object_Access_Level (Obj : Node_Id) return Uint is
15529 function Is_Interface_Conversion (N : Node_Id) return Boolean;
15530 -- Determine whether N is a construct of the form
15531 -- Some_Type (Operand._tag'Address)
15532 -- This construct appears in the context of dispatching calls.
15533
15534 function Reference_To (Obj : Node_Id) return Node_Id;
15535 -- An explicit dereference is created when removing side-effects from
15536 -- expressions for constraint checking purposes. In this case a local
15537 -- access type is created for it. The correct access level is that of
15538 -- the original source node. We detect this case by noting that the
15539 -- prefix of the dereference is created by an object declaration whose
15540 -- initial expression is a reference.
15541
15542 -----------------------------
15543 -- Is_Interface_Conversion --
15544 -----------------------------
15545
15546 function Is_Interface_Conversion (N : Node_Id) return Boolean is
15547 begin
15548 return Nkind (N) = N_Unchecked_Type_Conversion
15549 and then Nkind (Expression (N)) = N_Attribute_Reference
15550 and then Attribute_Name (Expression (N)) = Name_Address;
15551 end Is_Interface_Conversion;
15552
15553 ------------------
15554 -- Reference_To --
15555 ------------------
15556
15557 function Reference_To (Obj : Node_Id) return Node_Id is
15558 Pref : constant Node_Id := Prefix (Obj);
15559 begin
15560 if Is_Entity_Name (Pref)
15561 and then Nkind (Parent (Entity (Pref))) = N_Object_Declaration
15562 and then Present (Expression (Parent (Entity (Pref))))
15563 and then Nkind (Expression (Parent (Entity (Pref)))) = N_Reference
15564 then
15565 return (Prefix (Expression (Parent (Entity (Pref)))));
15566 else
15567 return Empty;
15568 end if;
15569 end Reference_To;
15570
15571 -- Local variables
15572
15573 E : Entity_Id;
15574
15575 -- Start of processing for Object_Access_Level
15576
15577 begin
15578 if Nkind (Obj) = N_Defining_Identifier
15579 or else Is_Entity_Name (Obj)
15580 then
15581 if Nkind (Obj) = N_Defining_Identifier then
15582 E := Obj;
15583 else
15584 E := Entity (Obj);
15585 end if;
15586
15587 if Is_Prival (E) then
15588 E := Prival_Link (E);
15589 end if;
15590
15591 -- If E is a type then it denotes a current instance. For this case
15592 -- we add one to the normal accessibility level of the type to ensure
15593 -- that current instances are treated as always being deeper than
15594 -- than the level of any visible named access type (see 3.10.2(21)).
15595
15596 if Is_Type (E) then
15597 return Type_Access_Level (E) + 1;
15598
15599 elsif Present (Renamed_Object (E)) then
15600 return Object_Access_Level (Renamed_Object (E));
15601
15602 -- Similarly, if E is a component of the current instance of a
15603 -- protected type, any instance of it is assumed to be at a deeper
15604 -- level than the type. For a protected object (whose type is an
15605 -- anonymous protected type) its components are at the same level
15606 -- as the type itself.
15607
15608 elsif not Is_Overloadable (E)
15609 and then Ekind (Scope (E)) = E_Protected_Type
15610 and then Comes_From_Source (Scope (E))
15611 then
15612 return Type_Access_Level (Scope (E)) + 1;
15613
15614 else
15615 -- Aliased formals take their access level from the point of call.
15616 -- This is smaller than the level of the subprogram itself.
15617
15618 if Is_Formal (E) and then Is_Aliased (E) then
15619 return Type_Access_Level (Etype (E));
15620
15621 else
15622 return Scope_Depth (Enclosing_Dynamic_Scope (E));
15623 end if;
15624 end if;
15625
15626 elsif Nkind (Obj) = N_Selected_Component then
15627 if Is_Access_Type (Etype (Prefix (Obj))) then
15628 return Type_Access_Level (Etype (Prefix (Obj)));
15629 else
15630 return Object_Access_Level (Prefix (Obj));
15631 end if;
15632
15633 elsif Nkind (Obj) = N_Indexed_Component then
15634 if Is_Access_Type (Etype (Prefix (Obj))) then
15635 return Type_Access_Level (Etype (Prefix (Obj)));
15636 else
15637 return Object_Access_Level (Prefix (Obj));
15638 end if;
15639
15640 elsif Nkind (Obj) = N_Explicit_Dereference then
15641
15642 -- If the prefix is a selected access discriminant then we make a
15643 -- recursive call on the prefix, which will in turn check the level
15644 -- of the prefix object of the selected discriminant.
15645
15646 -- In Ada 2012, if the discriminant has implicit dereference and
15647 -- the context is a selected component, treat this as an object of
15648 -- unknown scope (see below). This is necessary in compile-only mode;
15649 -- otherwise expansion will already have transformed the prefix into
15650 -- a temporary.
15651
15652 if Nkind (Prefix (Obj)) = N_Selected_Component
15653 and then Ekind (Etype (Prefix (Obj))) = E_Anonymous_Access_Type
15654 and then
15655 Ekind (Entity (Selector_Name (Prefix (Obj)))) = E_Discriminant
15656 and then
15657 (not Has_Implicit_Dereference
15658 (Entity (Selector_Name (Prefix (Obj))))
15659 or else Nkind (Parent (Obj)) /= N_Selected_Component)
15660 then
15661 return Object_Access_Level (Prefix (Obj));
15662
15663 -- Detect an interface conversion in the context of a dispatching
15664 -- call. Use the original form of the conversion to find the access
15665 -- level of the operand.
15666
15667 elsif Is_Interface (Etype (Obj))
15668 and then Is_Interface_Conversion (Prefix (Obj))
15669 and then Nkind (Original_Node (Obj)) = N_Type_Conversion
15670 then
15671 return Object_Access_Level (Original_Node (Obj));
15672
15673 elsif not Comes_From_Source (Obj) then
15674 declare
15675 Ref : constant Node_Id := Reference_To (Obj);
15676 begin
15677 if Present (Ref) then
15678 return Object_Access_Level (Ref);
15679 else
15680 return Type_Access_Level (Etype (Prefix (Obj)));
15681 end if;
15682 end;
15683
15684 else
15685 return Type_Access_Level (Etype (Prefix (Obj)));
15686 end if;
15687
15688 elsif Nkind_In (Obj, N_Type_Conversion, N_Unchecked_Type_Conversion) then
15689 return Object_Access_Level (Expression (Obj));
15690
15691 elsif Nkind (Obj) = N_Function_Call then
15692
15693 -- Function results are objects, so we get either the access level of
15694 -- the function or, in the case of an indirect call, the level of the
15695 -- access-to-subprogram type. (This code is used for Ada 95, but it
15696 -- looks wrong, because it seems that we should be checking the level
15697 -- of the call itself, even for Ada 95. However, using the Ada 2005
15698 -- version of the code causes regressions in several tests that are
15699 -- compiled with -gnat95. ???)
15700
15701 if Ada_Version < Ada_2005 then
15702 if Is_Entity_Name (Name (Obj)) then
15703 return Subprogram_Access_Level (Entity (Name (Obj)));
15704 else
15705 return Type_Access_Level (Etype (Prefix (Name (Obj))));
15706 end if;
15707
15708 -- For Ada 2005, the level of the result object of a function call is
15709 -- defined to be the level of the call's innermost enclosing master.
15710 -- We determine that by querying the depth of the innermost enclosing
15711 -- dynamic scope.
15712
15713 else
15714 Return_Master_Scope_Depth_Of_Call : declare
15715
15716 function Innermost_Master_Scope_Depth
15717 (N : Node_Id) return Uint;
15718 -- Returns the scope depth of the given node's innermost
15719 -- enclosing dynamic scope (effectively the accessibility
15720 -- level of the innermost enclosing master).
15721
15722 ----------------------------------
15723 -- Innermost_Master_Scope_Depth --
15724 ----------------------------------
15725
15726 function Innermost_Master_Scope_Depth
15727 (N : Node_Id) return Uint
15728 is
15729 Node_Par : Node_Id := Parent (N);
15730
15731 begin
15732 -- Locate the nearest enclosing node (by traversing Parents)
15733 -- that Defining_Entity can be applied to, and return the
15734 -- depth of that entity's nearest enclosing dynamic scope.
15735
15736 while Present (Node_Par) loop
15737 case Nkind (Node_Par) is
15738 when N_Component_Declaration |
15739 N_Entry_Declaration |
15740 N_Formal_Object_Declaration |
15741 N_Formal_Type_Declaration |
15742 N_Full_Type_Declaration |
15743 N_Incomplete_Type_Declaration |
15744 N_Loop_Parameter_Specification |
15745 N_Object_Declaration |
15746 N_Protected_Type_Declaration |
15747 N_Private_Extension_Declaration |
15748 N_Private_Type_Declaration |
15749 N_Subtype_Declaration |
15750 N_Function_Specification |
15751 N_Procedure_Specification |
15752 N_Task_Type_Declaration |
15753 N_Body_Stub |
15754 N_Generic_Instantiation |
15755 N_Proper_Body |
15756 N_Implicit_Label_Declaration |
15757 N_Package_Declaration |
15758 N_Single_Task_Declaration |
15759 N_Subprogram_Declaration |
15760 N_Generic_Declaration |
15761 N_Renaming_Declaration |
15762 N_Block_Statement |
15763 N_Formal_Subprogram_Declaration |
15764 N_Abstract_Subprogram_Declaration |
15765 N_Entry_Body |
15766 N_Exception_Declaration |
15767 N_Formal_Package_Declaration |
15768 N_Number_Declaration |
15769 N_Package_Specification |
15770 N_Parameter_Specification |
15771 N_Single_Protected_Declaration |
15772 N_Subunit =>
15773
15774 return Scope_Depth
15775 (Nearest_Dynamic_Scope
15776 (Defining_Entity (Node_Par)));
15777
15778 when others =>
15779 null;
15780 end case;
15781
15782 Node_Par := Parent (Node_Par);
15783 end loop;
15784
15785 pragma Assert (False);
15786
15787 -- Should never reach the following return
15788
15789 return Scope_Depth (Current_Scope) + 1;
15790 end Innermost_Master_Scope_Depth;
15791
15792 -- Start of processing for Return_Master_Scope_Depth_Of_Call
15793
15794 begin
15795 return Innermost_Master_Scope_Depth (Obj);
15796 end Return_Master_Scope_Depth_Of_Call;
15797 end if;
15798
15799 -- For convenience we handle qualified expressions, even though they
15800 -- aren't technically object names.
15801
15802 elsif Nkind (Obj) = N_Qualified_Expression then
15803 return Object_Access_Level (Expression (Obj));
15804
15805 -- Ditto for aggregates. They have the level of the temporary that
15806 -- will hold their value.
15807
15808 elsif Nkind (Obj) = N_Aggregate then
15809 return Object_Access_Level (Current_Scope);
15810
15811 -- Otherwise return the scope level of Standard. (If there are cases
15812 -- that fall through to this point they will be treated as having
15813 -- global accessibility for now. ???)
15814
15815 else
15816 return Scope_Depth (Standard_Standard);
15817 end if;
15818 end Object_Access_Level;
15819
15820 ---------------------------------
15821 -- Original_Aspect_Pragma_Name --
15822 ---------------------------------
15823
15824 function Original_Aspect_Pragma_Name (N : Node_Id) return Name_Id is
15825 Item : Node_Id;
15826 Item_Nam : Name_Id;
15827
15828 begin
15829 pragma Assert (Nkind_In (N, N_Aspect_Specification, N_Pragma));
15830
15831 Item := N;
15832
15833 -- The pragma was generated to emulate an aspect, use the original
15834 -- aspect specification.
15835
15836 if Nkind (Item) = N_Pragma and then From_Aspect_Specification (Item) then
15837 Item := Corresponding_Aspect (Item);
15838 end if;
15839
15840 -- Retrieve the name of the aspect/pragma. Note that Pre, Pre_Class,
15841 -- Post and Post_Class rewrite their pragma identifier to preserve the
15842 -- original name.
15843 -- ??? this is kludgey
15844
15845 if Nkind (Item) = N_Pragma then
15846 Item_Nam := Chars (Original_Node (Pragma_Identifier (Item)));
15847
15848 else
15849 pragma Assert (Nkind (Item) = N_Aspect_Specification);
15850 Item_Nam := Chars (Identifier (Item));
15851 end if;
15852
15853 -- Deal with 'Class by converting the name to its _XXX form
15854
15855 if Class_Present (Item) then
15856 if Item_Nam = Name_Invariant then
15857 Item_Nam := Name_uInvariant;
15858
15859 elsif Item_Nam = Name_Post then
15860 Item_Nam := Name_uPost;
15861
15862 elsif Item_Nam = Name_Pre then
15863 Item_Nam := Name_uPre;
15864
15865 elsif Nam_In (Item_Nam, Name_Type_Invariant,
15866 Name_Type_Invariant_Class)
15867 then
15868 Item_Nam := Name_uType_Invariant;
15869
15870 -- Nothing to do for other cases (e.g. a Check that derived from
15871 -- Pre_Class and has the flag set). Also we do nothing if the name
15872 -- is already in special _xxx form.
15873
15874 end if;
15875 end if;
15876
15877 return Item_Nam;
15878 end Original_Aspect_Pragma_Name;
15879
15880 --------------------------------------
15881 -- Original_Corresponding_Operation --
15882 --------------------------------------
15883
15884 function Original_Corresponding_Operation (S : Entity_Id) return Entity_Id
15885 is
15886 Typ : constant Entity_Id := Find_Dispatching_Type (S);
15887
15888 begin
15889 -- If S is an inherited primitive S2 the original corresponding
15890 -- operation of S is the original corresponding operation of S2
15891
15892 if Present (Alias (S))
15893 and then Find_Dispatching_Type (Alias (S)) /= Typ
15894 then
15895 return Original_Corresponding_Operation (Alias (S));
15896
15897 -- If S overrides an inherited subprogram S2 the original corresponding
15898 -- operation of S is the original corresponding operation of S2
15899
15900 elsif Present (Overridden_Operation (S)) then
15901 return Original_Corresponding_Operation (Overridden_Operation (S));
15902
15903 -- otherwise it is S itself
15904
15905 else
15906 return S;
15907 end if;
15908 end Original_Corresponding_Operation;
15909
15910 ----------------------
15911 -- Policy_In_Effect --
15912 ----------------------
15913
15914 function Policy_In_Effect (Policy : Name_Id) return Name_Id is
15915 function Policy_In_List (List : Node_Id) return Name_Id;
15916 -- Determine the the mode of a policy in a N_Pragma list
15917
15918 --------------------
15919 -- Policy_In_List --
15920 --------------------
15921
15922 function Policy_In_List (List : Node_Id) return Name_Id is
15923 Arg : Node_Id;
15924 Expr : Node_Id;
15925 Prag : Node_Id;
15926
15927 begin
15928 Prag := List;
15929 while Present (Prag) loop
15930 Arg := First (Pragma_Argument_Associations (Prag));
15931 Expr := Get_Pragma_Arg (Arg);
15932
15933 -- The current Check_Policy pragma matches the requested policy,
15934 -- return the second argument which denotes the policy identifier.
15935
15936 if Chars (Expr) = Policy then
15937 return Chars (Get_Pragma_Arg (Next (Arg)));
15938 end if;
15939
15940 Prag := Next_Pragma (Prag);
15941 end loop;
15942
15943 return No_Name;
15944 end Policy_In_List;
15945
15946 -- Local variables
15947
15948 Kind : Name_Id;
15949
15950 -- Start of processing for Policy_In_Effect
15951
15952 begin
15953 if not Is_Valid_Assertion_Kind (Policy) then
15954 raise Program_Error;
15955 end if;
15956
15957 -- Inspect all policy pragmas that appear within scopes (if any)
15958
15959 Kind := Policy_In_List (Check_Policy_List);
15960
15961 -- Inspect all configuration policy pragmas (if any)
15962
15963 if Kind = No_Name then
15964 Kind := Policy_In_List (Check_Policy_List_Config);
15965 end if;
15966
15967 -- The context lacks policy pragmas, determine the mode based on whether
15968 -- assertions are enabled at the configuration level. This ensures that
15969 -- the policy is preserved when analyzing generics.
15970
15971 if Kind = No_Name then
15972 if Assertions_Enabled_Config then
15973 Kind := Name_Check;
15974 else
15975 Kind := Name_Ignore;
15976 end if;
15977 end if;
15978
15979 return Kind;
15980 end Policy_In_Effect;
15981
15982 ----------------------------------
15983 -- Predicate_Tests_On_Arguments --
15984 ----------------------------------
15985
15986 function Predicate_Tests_On_Arguments (Subp : Entity_Id) return Boolean is
15987 begin
15988 -- Always test predicates on indirect call
15989
15990 if Ekind (Subp) = E_Subprogram_Type then
15991 return True;
15992
15993 -- Do not test predicates on call to generated default Finalize, since
15994 -- we are not interested in whether something we are finalizing (and
15995 -- typically destroying) satisfies its predicates.
15996
15997 elsif Chars (Subp) = Name_Finalize
15998 and then not Comes_From_Source (Subp)
15999 then
16000 return False;
16001
16002 -- Do not test predicates on any internally generated routines
16003
16004 elsif Is_Internal_Name (Chars (Subp)) then
16005 return False;
16006
16007 -- Do not test predicates on call to Init_Proc, since if needed the
16008 -- predicate test will occur at some other point.
16009
16010 elsif Is_Init_Proc (Subp) then
16011 return False;
16012
16013 -- Do not test predicates on call to predicate function, since this
16014 -- would cause infinite recursion.
16015
16016 elsif Ekind (Subp) = E_Function
16017 and then (Is_Predicate_Function (Subp)
16018 or else
16019 Is_Predicate_Function_M (Subp))
16020 then
16021 return False;
16022
16023 -- For now, no other exceptions
16024
16025 else
16026 return True;
16027 end if;
16028 end Predicate_Tests_On_Arguments;
16029
16030 -----------------------
16031 -- Private_Component --
16032 -----------------------
16033
16034 function Private_Component (Type_Id : Entity_Id) return Entity_Id is
16035 Ancestor : constant Entity_Id := Base_Type (Type_Id);
16036
16037 function Trace_Components
16038 (T : Entity_Id;
16039 Check : Boolean) return Entity_Id;
16040 -- Recursive function that does the work, and checks against circular
16041 -- definition for each subcomponent type.
16042
16043 ----------------------
16044 -- Trace_Components --
16045 ----------------------
16046
16047 function Trace_Components
16048 (T : Entity_Id;
16049 Check : Boolean) return Entity_Id
16050 is
16051 Btype : constant Entity_Id := Base_Type (T);
16052 Component : Entity_Id;
16053 P : Entity_Id;
16054 Candidate : Entity_Id := Empty;
16055
16056 begin
16057 if Check and then Btype = Ancestor then
16058 Error_Msg_N ("circular type definition", Type_Id);
16059 return Any_Type;
16060 end if;
16061
16062 if Is_Private_Type (Btype) and then not Is_Generic_Type (Btype) then
16063 if Present (Full_View (Btype))
16064 and then Is_Record_Type (Full_View (Btype))
16065 and then not Is_Frozen (Btype)
16066 then
16067 -- To indicate that the ancestor depends on a private type, the
16068 -- current Btype is sufficient. However, to check for circular
16069 -- definition we must recurse on the full view.
16070
16071 Candidate := Trace_Components (Full_View (Btype), True);
16072
16073 if Candidate = Any_Type then
16074 return Any_Type;
16075 else
16076 return Btype;
16077 end if;
16078
16079 else
16080 return Btype;
16081 end if;
16082
16083 elsif Is_Array_Type (Btype) then
16084 return Trace_Components (Component_Type (Btype), True);
16085
16086 elsif Is_Record_Type (Btype) then
16087 Component := First_Entity (Btype);
16088 while Present (Component)
16089 and then Comes_From_Source (Component)
16090 loop
16091 -- Skip anonymous types generated by constrained components
16092
16093 if not Is_Type (Component) then
16094 P := Trace_Components (Etype (Component), True);
16095
16096 if Present (P) then
16097 if P = Any_Type then
16098 return P;
16099 else
16100 Candidate := P;
16101 end if;
16102 end if;
16103 end if;
16104
16105 Next_Entity (Component);
16106 end loop;
16107
16108 return Candidate;
16109
16110 else
16111 return Empty;
16112 end if;
16113 end Trace_Components;
16114
16115 -- Start of processing for Private_Component
16116
16117 begin
16118 return Trace_Components (Type_Id, False);
16119 end Private_Component;
16120
16121 ---------------------------
16122 -- Primitive_Names_Match --
16123 ---------------------------
16124
16125 function Primitive_Names_Match (E1, E2 : Entity_Id) return Boolean is
16126
16127 function Non_Internal_Name (E : Entity_Id) return Name_Id;
16128 -- Given an internal name, returns the corresponding non-internal name
16129
16130 ------------------------
16131 -- Non_Internal_Name --
16132 ------------------------
16133
16134 function Non_Internal_Name (E : Entity_Id) return Name_Id is
16135 begin
16136 Get_Name_String (Chars (E));
16137 Name_Len := Name_Len - 1;
16138 return Name_Find;
16139 end Non_Internal_Name;
16140
16141 -- Start of processing for Primitive_Names_Match
16142
16143 begin
16144 pragma Assert (Present (E1) and then Present (E2));
16145
16146 return Chars (E1) = Chars (E2)
16147 or else
16148 (not Is_Internal_Name (Chars (E1))
16149 and then Is_Internal_Name (Chars (E2))
16150 and then Non_Internal_Name (E2) = Chars (E1))
16151 or else
16152 (not Is_Internal_Name (Chars (E2))
16153 and then Is_Internal_Name (Chars (E1))
16154 and then Non_Internal_Name (E1) = Chars (E2))
16155 or else
16156 (Is_Predefined_Dispatching_Operation (E1)
16157 and then Is_Predefined_Dispatching_Operation (E2)
16158 and then Same_TSS (E1, E2))
16159 or else
16160 (Is_Init_Proc (E1) and then Is_Init_Proc (E2));
16161 end Primitive_Names_Match;
16162
16163 -----------------------
16164 -- Process_End_Label --
16165 -----------------------
16166
16167 procedure Process_End_Label
16168 (N : Node_Id;
16169 Typ : Character;
16170 Ent : Entity_Id)
16171 is
16172 Loc : Source_Ptr;
16173 Nam : Node_Id;
16174 Scop : Entity_Id;
16175
16176 Label_Ref : Boolean;
16177 -- Set True if reference to end label itself is required
16178
16179 Endl : Node_Id;
16180 -- Gets set to the operator symbol or identifier that references the
16181 -- entity Ent. For the child unit case, this is the identifier from the
16182 -- designator. For other cases, this is simply Endl.
16183
16184 procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id);
16185 -- N is an identifier node that appears as a parent unit reference in
16186 -- the case where Ent is a child unit. This procedure generates an
16187 -- appropriate cross-reference entry. E is the corresponding entity.
16188
16189 -------------------------
16190 -- Generate_Parent_Ref --
16191 -------------------------
16192
16193 procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id) is
16194 begin
16195 -- If names do not match, something weird, skip reference
16196
16197 if Chars (E) = Chars (N) then
16198
16199 -- Generate the reference. We do NOT consider this as a reference
16200 -- for unreferenced symbol purposes.
16201
16202 Generate_Reference (E, N, 'r', Set_Ref => False, Force => True);
16203
16204 if Style_Check then
16205 Style.Check_Identifier (N, E);
16206 end if;
16207 end if;
16208 end Generate_Parent_Ref;
16209
16210 -- Start of processing for Process_End_Label
16211
16212 begin
16213 -- If no node, ignore. This happens in some error situations, and
16214 -- also for some internally generated structures where no end label
16215 -- references are required in any case.
16216
16217 if No (N) then
16218 return;
16219 end if;
16220
16221 -- Nothing to do if no End_Label, happens for internally generated
16222 -- constructs where we don't want an end label reference anyway. Also
16223 -- nothing to do if Endl is a string literal, which means there was
16224 -- some prior error (bad operator symbol)
16225
16226 Endl := End_Label (N);
16227
16228 if No (Endl) or else Nkind (Endl) = N_String_Literal then
16229 return;
16230 end if;
16231
16232 -- Reference node is not in extended main source unit
16233
16234 if not In_Extended_Main_Source_Unit (N) then
16235
16236 -- Generally we do not collect references except for the extended
16237 -- main source unit. The one exception is the 'e' entry for a
16238 -- package spec, where it is useful for a client to have the
16239 -- ending information to define scopes.
16240
16241 if Typ /= 'e' then
16242 return;
16243
16244 else
16245 Label_Ref := False;
16246
16247 -- For this case, we can ignore any parent references, but we
16248 -- need the package name itself for the 'e' entry.
16249
16250 if Nkind (Endl) = N_Designator then
16251 Endl := Identifier (Endl);
16252 end if;
16253 end if;
16254
16255 -- Reference is in extended main source unit
16256
16257 else
16258 Label_Ref := True;
16259
16260 -- For designator, generate references for the parent entries
16261
16262 if Nkind (Endl) = N_Designator then
16263
16264 -- Generate references for the prefix if the END line comes from
16265 -- source (otherwise we do not need these references) We climb the
16266 -- scope stack to find the expected entities.
16267
16268 if Comes_From_Source (Endl) then
16269 Nam := Name (Endl);
16270 Scop := Current_Scope;
16271 while Nkind (Nam) = N_Selected_Component loop
16272 Scop := Scope (Scop);
16273 exit when No (Scop);
16274 Generate_Parent_Ref (Selector_Name (Nam), Scop);
16275 Nam := Prefix (Nam);
16276 end loop;
16277
16278 if Present (Scop) then
16279 Generate_Parent_Ref (Nam, Scope (Scop));
16280 end if;
16281 end if;
16282
16283 Endl := Identifier (Endl);
16284 end if;
16285 end if;
16286
16287 -- If the end label is not for the given entity, then either we have
16288 -- some previous error, or this is a generic instantiation for which
16289 -- we do not need to make a cross-reference in this case anyway. In
16290 -- either case we simply ignore the call.
16291
16292 if Chars (Ent) /= Chars (Endl) then
16293 return;
16294 end if;
16295
16296 -- If label was really there, then generate a normal reference and then
16297 -- adjust the location in the end label to point past the name (which
16298 -- should almost always be the semicolon).
16299
16300 Loc := Sloc (Endl);
16301
16302 if Comes_From_Source (Endl) then
16303
16304 -- If a label reference is required, then do the style check and
16305 -- generate an l-type cross-reference entry for the label
16306
16307 if Label_Ref then
16308 if Style_Check then
16309 Style.Check_Identifier (Endl, Ent);
16310 end if;
16311
16312 Generate_Reference (Ent, Endl, 'l', Set_Ref => False);
16313 end if;
16314
16315 -- Set the location to point past the label (normally this will
16316 -- mean the semicolon immediately following the label). This is
16317 -- done for the sake of the 'e' or 't' entry generated below.
16318
16319 Get_Decoded_Name_String (Chars (Endl));
16320 Set_Sloc (Endl, Sloc (Endl) + Source_Ptr (Name_Len));
16321
16322 else
16323 -- In SPARK mode, no missing label is allowed for packages and
16324 -- subprogram bodies. Detect those cases by testing whether
16325 -- Process_End_Label was called for a body (Typ = 't') or a package.
16326
16327 if Restriction_Check_Required (SPARK_05)
16328 and then (Typ = 't' or else Ekind (Ent) = E_Package)
16329 then
16330 Error_Msg_Node_1 := Endl;
16331 Check_SPARK_05_Restriction
16332 ("`END &` required", Endl, Force => True);
16333 end if;
16334 end if;
16335
16336 -- Now generate the e/t reference
16337
16338 Generate_Reference (Ent, Endl, Typ, Set_Ref => False, Force => True);
16339
16340 -- Restore Sloc, in case modified above, since we have an identifier
16341 -- and the normal Sloc should be left set in the tree.
16342
16343 Set_Sloc (Endl, Loc);
16344 end Process_End_Label;
16345
16346 ----------------
16347 -- Referenced --
16348 ----------------
16349
16350 function Referenced (Id : Entity_Id; Expr : Node_Id) return Boolean is
16351 Seen : Boolean := False;
16352
16353 function Is_Reference (N : Node_Id) return Traverse_Result;
16354 -- Determine whether node N denotes a reference to Id. If this is the
16355 -- case, set global flag Seen to True and stop the traversal.
16356
16357 ------------------
16358 -- Is_Reference --
16359 ------------------
16360
16361 function Is_Reference (N : Node_Id) return Traverse_Result is
16362 begin
16363 if Is_Entity_Name (N)
16364 and then Present (Entity (N))
16365 and then Entity (N) = Id
16366 then
16367 Seen := True;
16368 return Abandon;
16369 else
16370 return OK;
16371 end if;
16372 end Is_Reference;
16373
16374 procedure Inspect_Expression is new Traverse_Proc (Is_Reference);
16375
16376 -- Start of processing for Referenced
16377
16378 begin
16379 Inspect_Expression (Expr);
16380 return Seen;
16381 end Referenced;
16382
16383 ------------------------------------
16384 -- References_Generic_Formal_Type --
16385 ------------------------------------
16386
16387 function References_Generic_Formal_Type (N : Node_Id) return Boolean is
16388
16389 function Process (N : Node_Id) return Traverse_Result;
16390 -- Process one node in search for generic formal type
16391
16392 -------------
16393 -- Process --
16394 -------------
16395
16396 function Process (N : Node_Id) return Traverse_Result is
16397 begin
16398 if Nkind (N) in N_Has_Entity then
16399 declare
16400 E : constant Entity_Id := Entity (N);
16401 begin
16402 if Present (E) then
16403 if Is_Generic_Type (E) then
16404 return Abandon;
16405 elsif Present (Etype (E))
16406 and then Is_Generic_Type (Etype (E))
16407 then
16408 return Abandon;
16409 end if;
16410 end if;
16411 end;
16412 end if;
16413
16414 return Atree.OK;
16415 end Process;
16416
16417 function Traverse is new Traverse_Func (Process);
16418 -- Traverse tree to look for generic type
16419
16420 begin
16421 if Inside_A_Generic then
16422 return Traverse (N) = Abandon;
16423 else
16424 return False;
16425 end if;
16426 end References_Generic_Formal_Type;
16427
16428 --------------------
16429 -- Remove_Homonym --
16430 --------------------
16431
16432 procedure Remove_Homonym (E : Entity_Id) is
16433 Prev : Entity_Id := Empty;
16434 H : Entity_Id;
16435
16436 begin
16437 if E = Current_Entity (E) then
16438 if Present (Homonym (E)) then
16439 Set_Current_Entity (Homonym (E));
16440 else
16441 Set_Name_Entity_Id (Chars (E), Empty);
16442 end if;
16443
16444 else
16445 H := Current_Entity (E);
16446 while Present (H) and then H /= E loop
16447 Prev := H;
16448 H := Homonym (H);
16449 end loop;
16450
16451 -- If E is not on the homonym chain, nothing to do
16452
16453 if Present (H) then
16454 Set_Homonym (Prev, Homonym (E));
16455 end if;
16456 end if;
16457 end Remove_Homonym;
16458
16459 ---------------------
16460 -- Rep_To_Pos_Flag --
16461 ---------------------
16462
16463 function Rep_To_Pos_Flag (E : Entity_Id; Loc : Source_Ptr) return Node_Id is
16464 begin
16465 return New_Occurrence_Of
16466 (Boolean_Literals (not Range_Checks_Suppressed (E)), Loc);
16467 end Rep_To_Pos_Flag;
16468
16469 --------------------
16470 -- Require_Entity --
16471 --------------------
16472
16473 procedure Require_Entity (N : Node_Id) is
16474 begin
16475 if Is_Entity_Name (N) and then No (Entity (N)) then
16476 if Total_Errors_Detected /= 0 then
16477 Set_Entity (N, Any_Id);
16478 else
16479 raise Program_Error;
16480 end if;
16481 end if;
16482 end Require_Entity;
16483
16484 -------------------------------
16485 -- Requires_State_Refinement --
16486 -------------------------------
16487
16488 function Requires_State_Refinement
16489 (Spec_Id : Entity_Id;
16490 Body_Id : Entity_Id) return Boolean
16491 is
16492 function Mode_Is_Off (Prag : Node_Id) return Boolean;
16493 -- Given pragma SPARK_Mode, determine whether the mode is Off
16494
16495 -----------------
16496 -- Mode_Is_Off --
16497 -----------------
16498
16499 function Mode_Is_Off (Prag : Node_Id) return Boolean is
16500 Mode : Node_Id;
16501
16502 begin
16503 -- The default SPARK mode is On
16504
16505 if No (Prag) then
16506 return False;
16507 end if;
16508
16509 Mode := Get_Pragma_Arg (First (Pragma_Argument_Associations (Prag)));
16510
16511 -- Then the pragma lacks an argument, the default mode is On
16512
16513 if No (Mode) then
16514 return False;
16515 else
16516 return Chars (Mode) = Name_Off;
16517 end if;
16518 end Mode_Is_Off;
16519
16520 -- Start of processing for Requires_State_Refinement
16521
16522 begin
16523 -- A package that does not define at least one abstract state cannot
16524 -- possibly require refinement.
16525
16526 if No (Abstract_States (Spec_Id)) then
16527 return False;
16528
16529 -- The package instroduces a single null state which does not merit
16530 -- refinement.
16531
16532 elsif Has_Null_Abstract_State (Spec_Id) then
16533 return False;
16534
16535 -- Check whether the package body is subject to pragma SPARK_Mode. If
16536 -- it is and the mode is Off, the package body is considered to be in
16537 -- regular Ada and does not require refinement.
16538
16539 elsif Mode_Is_Off (SPARK_Pragma (Body_Id)) then
16540 return False;
16541
16542 -- The body's SPARK_Mode may be inherited from a similar pragma that
16543 -- appears in the private declarations of the spec. The pragma we are
16544 -- interested appears as the second entry in SPARK_Pragma.
16545
16546 elsif Present (SPARK_Pragma (Spec_Id))
16547 and then Mode_Is_Off (Next_Pragma (SPARK_Pragma (Spec_Id)))
16548 then
16549 return False;
16550
16551 -- The spec defines at least one abstract state and the body has no way
16552 -- of circumventing the refinement.
16553
16554 else
16555 return True;
16556 end if;
16557 end Requires_State_Refinement;
16558
16559 ------------------------------
16560 -- Requires_Transient_Scope --
16561 ------------------------------
16562
16563 -- A transient scope is required when variable-sized temporaries are
16564 -- allocated in the primary or secondary stack, or when finalization
16565 -- actions must be generated before the next instruction.
16566
16567 function Requires_Transient_Scope (Id : Entity_Id) return Boolean is
16568 Typ : constant Entity_Id := Underlying_Type (Id);
16569
16570 -- Start of processing for Requires_Transient_Scope
16571
16572 begin
16573 -- This is a private type which is not completed yet. This can only
16574 -- happen in a default expression (of a formal parameter or of a
16575 -- record component). Do not expand transient scope in this case
16576
16577 if No (Typ) then
16578 return False;
16579
16580 -- Do not expand transient scope for non-existent procedure return
16581
16582 elsif Typ = Standard_Void_Type then
16583 return False;
16584
16585 -- Elementary types do not require a transient scope
16586
16587 elsif Is_Elementary_Type (Typ) then
16588 return False;
16589
16590 -- Generally, indefinite subtypes require a transient scope, since the
16591 -- back end cannot generate temporaries, since this is not a valid type
16592 -- for declaring an object. It might be possible to relax this in the
16593 -- future, e.g. by declaring the maximum possible space for the type.
16594
16595 elsif Is_Indefinite_Subtype (Typ) then
16596 return True;
16597
16598 -- Functions returning tagged types may dispatch on result so their
16599 -- returned value is allocated on the secondary stack. Controlled
16600 -- type temporaries need finalization.
16601
16602 elsif Is_Tagged_Type (Typ)
16603 or else Has_Controlled_Component (Typ)
16604 then
16605 return not Is_Value_Type (Typ);
16606
16607 -- Record type
16608
16609 elsif Is_Record_Type (Typ) then
16610 declare
16611 Comp : Entity_Id;
16612 begin
16613 Comp := First_Entity (Typ);
16614 while Present (Comp) loop
16615 if Ekind (Comp) = E_Component
16616 and then Requires_Transient_Scope (Etype (Comp))
16617 then
16618 return True;
16619 else
16620 Next_Entity (Comp);
16621 end if;
16622 end loop;
16623 end;
16624
16625 return False;
16626
16627 -- String literal types never require transient scope
16628
16629 elsif Ekind (Typ) = E_String_Literal_Subtype then
16630 return False;
16631
16632 -- Array type. Note that we already know that this is a constrained
16633 -- array, since unconstrained arrays will fail the indefinite test.
16634
16635 elsif Is_Array_Type (Typ) then
16636
16637 -- If component type requires a transient scope, the array does too
16638
16639 if Requires_Transient_Scope (Component_Type (Typ)) then
16640 return True;
16641
16642 -- Otherwise, we only need a transient scope if the size depends on
16643 -- the value of one or more discriminants.
16644
16645 else
16646 return Size_Depends_On_Discriminant (Typ);
16647 end if;
16648
16649 -- All other cases do not require a transient scope
16650
16651 else
16652 return False;
16653 end if;
16654 end Requires_Transient_Scope;
16655
16656 --------------------------
16657 -- Reset_Analyzed_Flags --
16658 --------------------------
16659
16660 procedure Reset_Analyzed_Flags (N : Node_Id) is
16661
16662 function Clear_Analyzed (N : Node_Id) return Traverse_Result;
16663 -- Function used to reset Analyzed flags in tree. Note that we do
16664 -- not reset Analyzed flags in entities, since there is no need to
16665 -- reanalyze entities, and indeed, it is wrong to do so, since it
16666 -- can result in generating auxiliary stuff more than once.
16667
16668 --------------------
16669 -- Clear_Analyzed --
16670 --------------------
16671
16672 function Clear_Analyzed (N : Node_Id) return Traverse_Result is
16673 begin
16674 if not Has_Extension (N) then
16675 Set_Analyzed (N, False);
16676 end if;
16677
16678 return OK;
16679 end Clear_Analyzed;
16680
16681 procedure Reset_Analyzed is new Traverse_Proc (Clear_Analyzed);
16682
16683 -- Start of processing for Reset_Analyzed_Flags
16684
16685 begin
16686 Reset_Analyzed (N);
16687 end Reset_Analyzed_Flags;
16688
16689 ------------------------
16690 -- Restore_SPARK_Mode --
16691 ------------------------
16692
16693 procedure Restore_SPARK_Mode (Mode : SPARK_Mode_Type) is
16694 begin
16695 SPARK_Mode := Mode;
16696 end Restore_SPARK_Mode;
16697
16698 --------------------------------
16699 -- Returns_Unconstrained_Type --
16700 --------------------------------
16701
16702 function Returns_Unconstrained_Type (Subp : Entity_Id) return Boolean is
16703 begin
16704 return Ekind (Subp) = E_Function
16705 and then not Is_Scalar_Type (Etype (Subp))
16706 and then not Is_Access_Type (Etype (Subp))
16707 and then not Is_Constrained (Etype (Subp));
16708 end Returns_Unconstrained_Type;
16709
16710 ----------------------------
16711 -- Root_Type_Of_Full_View --
16712 ----------------------------
16713
16714 function Root_Type_Of_Full_View (T : Entity_Id) return Entity_Id is
16715 Rtyp : constant Entity_Id := Root_Type (T);
16716
16717 begin
16718 -- The root type of the full view may itself be a private type. Keep
16719 -- looking for the ultimate derivation parent.
16720
16721 if Is_Private_Type (Rtyp) and then Present (Full_View (Rtyp)) then
16722 return Root_Type_Of_Full_View (Full_View (Rtyp));
16723 else
16724 return Rtyp;
16725 end if;
16726 end Root_Type_Of_Full_View;
16727
16728 ---------------------------
16729 -- Safe_To_Capture_Value --
16730 ---------------------------
16731
16732 function Safe_To_Capture_Value
16733 (N : Node_Id;
16734 Ent : Entity_Id;
16735 Cond : Boolean := False) return Boolean
16736 is
16737 begin
16738 -- The only entities for which we track constant values are variables
16739 -- which are not renamings, constants, out parameters, and in out
16740 -- parameters, so check if we have this case.
16741
16742 -- Note: it may seem odd to track constant values for constants, but in
16743 -- fact this routine is used for other purposes than simply capturing
16744 -- the value. In particular, the setting of Known[_Non]_Null.
16745
16746 if (Ekind (Ent) = E_Variable and then No (Renamed_Object (Ent)))
16747 or else
16748 Ekind_In (Ent, E_Constant, E_Out_Parameter, E_In_Out_Parameter)
16749 then
16750 null;
16751
16752 -- For conditionals, we also allow loop parameters and all formals,
16753 -- including in parameters.
16754
16755 elsif Cond and then Ekind_In (Ent, E_Loop_Parameter, E_In_Parameter) then
16756 null;
16757
16758 -- For all other cases, not just unsafe, but impossible to capture
16759 -- Current_Value, since the above are the only entities which have
16760 -- Current_Value fields.
16761
16762 else
16763 return False;
16764 end if;
16765
16766 -- Skip if volatile or aliased, since funny things might be going on in
16767 -- these cases which we cannot necessarily track. Also skip any variable
16768 -- for which an address clause is given, or whose address is taken. Also
16769 -- never capture value of library level variables (an attempt to do so
16770 -- can occur in the case of package elaboration code).
16771
16772 if Treat_As_Volatile (Ent)
16773 or else Is_Aliased (Ent)
16774 or else Present (Address_Clause (Ent))
16775 or else Address_Taken (Ent)
16776 or else (Is_Library_Level_Entity (Ent)
16777 and then Ekind (Ent) = E_Variable)
16778 then
16779 return False;
16780 end if;
16781
16782 -- OK, all above conditions are met. We also require that the scope of
16783 -- the reference be the same as the scope of the entity, not counting
16784 -- packages and blocks and loops.
16785
16786 declare
16787 E_Scope : constant Entity_Id := Scope (Ent);
16788 R_Scope : Entity_Id;
16789
16790 begin
16791 R_Scope := Current_Scope;
16792 while R_Scope /= Standard_Standard loop
16793 exit when R_Scope = E_Scope;
16794
16795 if not Ekind_In (R_Scope, E_Package, E_Block, E_Loop) then
16796 return False;
16797 else
16798 R_Scope := Scope (R_Scope);
16799 end if;
16800 end loop;
16801 end;
16802
16803 -- We also require that the reference does not appear in a context
16804 -- where it is not sure to be executed (i.e. a conditional context
16805 -- or an exception handler). We skip this if Cond is True, since the
16806 -- capturing of values from conditional tests handles this ok.
16807
16808 if Cond then
16809 return True;
16810 end if;
16811
16812 declare
16813 Desc : Node_Id;
16814 P : Node_Id;
16815
16816 begin
16817 Desc := N;
16818
16819 -- Seems dubious that case expressions are not handled here ???
16820
16821 P := Parent (N);
16822 while Present (P) loop
16823 if Nkind (P) = N_If_Statement
16824 or else Nkind (P) = N_Case_Statement
16825 or else (Nkind (P) in N_Short_Circuit
16826 and then Desc = Right_Opnd (P))
16827 or else (Nkind (P) = N_If_Expression
16828 and then Desc /= First (Expressions (P)))
16829 or else Nkind (P) = N_Exception_Handler
16830 or else Nkind (P) = N_Selective_Accept
16831 or else Nkind (P) = N_Conditional_Entry_Call
16832 or else Nkind (P) = N_Timed_Entry_Call
16833 or else Nkind (P) = N_Asynchronous_Select
16834 then
16835 return False;
16836
16837 else
16838 Desc := P;
16839 P := Parent (P);
16840
16841 -- A special Ada 2012 case: the original node may be part
16842 -- of the else_actions of a conditional expression, in which
16843 -- case it might not have been expanded yet, and appears in
16844 -- a non-syntactic list of actions. In that case it is clearly
16845 -- not safe to save a value.
16846
16847 if No (P)
16848 and then Is_List_Member (Desc)
16849 and then No (Parent (List_Containing (Desc)))
16850 then
16851 return False;
16852 end if;
16853 end if;
16854 end loop;
16855 end;
16856
16857 -- OK, looks safe to set value
16858
16859 return True;
16860 end Safe_To_Capture_Value;
16861
16862 ---------------
16863 -- Same_Name --
16864 ---------------
16865
16866 function Same_Name (N1, N2 : Node_Id) return Boolean is
16867 K1 : constant Node_Kind := Nkind (N1);
16868 K2 : constant Node_Kind := Nkind (N2);
16869
16870 begin
16871 if (K1 = N_Identifier or else K1 = N_Defining_Identifier)
16872 and then (K2 = N_Identifier or else K2 = N_Defining_Identifier)
16873 then
16874 return Chars (N1) = Chars (N2);
16875
16876 elsif (K1 = N_Selected_Component or else K1 = N_Expanded_Name)
16877 and then (K2 = N_Selected_Component or else K2 = N_Expanded_Name)
16878 then
16879 return Same_Name (Selector_Name (N1), Selector_Name (N2))
16880 and then Same_Name (Prefix (N1), Prefix (N2));
16881
16882 else
16883 return False;
16884 end if;
16885 end Same_Name;
16886
16887 -----------------
16888 -- Same_Object --
16889 -----------------
16890
16891 function Same_Object (Node1, Node2 : Node_Id) return Boolean is
16892 N1 : constant Node_Id := Original_Node (Node1);
16893 N2 : constant Node_Id := Original_Node (Node2);
16894 -- We do the tests on original nodes, since we are most interested
16895 -- in the original source, not any expansion that got in the way.
16896
16897 K1 : constant Node_Kind := Nkind (N1);
16898 K2 : constant Node_Kind := Nkind (N2);
16899
16900 begin
16901 -- First case, both are entities with same entity
16902
16903 if K1 in N_Has_Entity and then K2 in N_Has_Entity then
16904 declare
16905 EN1 : constant Entity_Id := Entity (N1);
16906 EN2 : constant Entity_Id := Entity (N2);
16907 begin
16908 if Present (EN1) and then Present (EN2)
16909 and then (Ekind_In (EN1, E_Variable, E_Constant)
16910 or else Is_Formal (EN1))
16911 and then EN1 = EN2
16912 then
16913 return True;
16914 end if;
16915 end;
16916 end if;
16917
16918 -- Second case, selected component with same selector, same record
16919
16920 if K1 = N_Selected_Component
16921 and then K2 = N_Selected_Component
16922 and then Chars (Selector_Name (N1)) = Chars (Selector_Name (N2))
16923 then
16924 return Same_Object (Prefix (N1), Prefix (N2));
16925
16926 -- Third case, indexed component with same subscripts, same array
16927
16928 elsif K1 = N_Indexed_Component
16929 and then K2 = N_Indexed_Component
16930 and then Same_Object (Prefix (N1), Prefix (N2))
16931 then
16932 declare
16933 E1, E2 : Node_Id;
16934 begin
16935 E1 := First (Expressions (N1));
16936 E2 := First (Expressions (N2));
16937 while Present (E1) loop
16938 if not Same_Value (E1, E2) then
16939 return False;
16940 else
16941 Next (E1);
16942 Next (E2);
16943 end if;
16944 end loop;
16945
16946 return True;
16947 end;
16948
16949 -- Fourth case, slice of same array with same bounds
16950
16951 elsif K1 = N_Slice
16952 and then K2 = N_Slice
16953 and then Nkind (Discrete_Range (N1)) = N_Range
16954 and then Nkind (Discrete_Range (N2)) = N_Range
16955 and then Same_Value (Low_Bound (Discrete_Range (N1)),
16956 Low_Bound (Discrete_Range (N2)))
16957 and then Same_Value (High_Bound (Discrete_Range (N1)),
16958 High_Bound (Discrete_Range (N2)))
16959 then
16960 return Same_Name (Prefix (N1), Prefix (N2));
16961
16962 -- All other cases, not clearly the same object
16963
16964 else
16965 return False;
16966 end if;
16967 end Same_Object;
16968
16969 ---------------
16970 -- Same_Type --
16971 ---------------
16972
16973 function Same_Type (T1, T2 : Entity_Id) return Boolean is
16974 begin
16975 if T1 = T2 then
16976 return True;
16977
16978 elsif not Is_Constrained (T1)
16979 and then not Is_Constrained (T2)
16980 and then Base_Type (T1) = Base_Type (T2)
16981 then
16982 return True;
16983
16984 -- For now don't bother with case of identical constraints, to be
16985 -- fiddled with later on perhaps (this is only used for optimization
16986 -- purposes, so it is not critical to do a best possible job)
16987
16988 else
16989 return False;
16990 end if;
16991 end Same_Type;
16992
16993 ----------------
16994 -- Same_Value --
16995 ----------------
16996
16997 function Same_Value (Node1, Node2 : Node_Id) return Boolean is
16998 begin
16999 if Compile_Time_Known_Value (Node1)
17000 and then Compile_Time_Known_Value (Node2)
17001 and then Expr_Value (Node1) = Expr_Value (Node2)
17002 then
17003 return True;
17004 elsif Same_Object (Node1, Node2) then
17005 return True;
17006 else
17007 return False;
17008 end if;
17009 end Same_Value;
17010
17011 -----------------------------
17012 -- Save_SPARK_Mode_And_Set --
17013 -----------------------------
17014
17015 procedure Save_SPARK_Mode_And_Set
17016 (Context : Entity_Id;
17017 Mode : out SPARK_Mode_Type)
17018 is
17019 begin
17020 -- Save the current mode in effect
17021
17022 Mode := SPARK_Mode;
17023
17024 -- Do not consider illegal or partially decorated constructs
17025
17026 if Ekind (Context) = E_Void or else Error_Posted (Context) then
17027 null;
17028
17029 elsif Present (SPARK_Pragma (Context)) then
17030 SPARK_Mode := Get_SPARK_Mode_From_Pragma (SPARK_Pragma (Context));
17031 end if;
17032 end Save_SPARK_Mode_And_Set;
17033
17034 -------------------------
17035 -- Scalar_Part_Present --
17036 -------------------------
17037
17038 function Scalar_Part_Present (T : Entity_Id) return Boolean is
17039 C : Entity_Id;
17040
17041 begin
17042 if Is_Scalar_Type (T) then
17043 return True;
17044
17045 elsif Is_Array_Type (T) then
17046 return Scalar_Part_Present (Component_Type (T));
17047
17048 elsif Is_Record_Type (T) or else Has_Discriminants (T) then
17049 C := First_Component_Or_Discriminant (T);
17050 while Present (C) loop
17051 if Scalar_Part_Present (Etype (C)) then
17052 return True;
17053 else
17054 Next_Component_Or_Discriminant (C);
17055 end if;
17056 end loop;
17057 end if;
17058
17059 return False;
17060 end Scalar_Part_Present;
17061
17062 ------------------------
17063 -- Scope_Is_Transient --
17064 ------------------------
17065
17066 function Scope_Is_Transient return Boolean is
17067 begin
17068 return Scope_Stack.Table (Scope_Stack.Last).Is_Transient;
17069 end Scope_Is_Transient;
17070
17071 ------------------
17072 -- Scope_Within --
17073 ------------------
17074
17075 function Scope_Within (Scope1, Scope2 : Entity_Id) return Boolean is
17076 Scop : Entity_Id;
17077
17078 begin
17079 Scop := Scope1;
17080 while Scop /= Standard_Standard loop
17081 Scop := Scope (Scop);
17082
17083 if Scop = Scope2 then
17084 return True;
17085 end if;
17086 end loop;
17087
17088 return False;
17089 end Scope_Within;
17090
17091 --------------------------
17092 -- Scope_Within_Or_Same --
17093 --------------------------
17094
17095 function Scope_Within_Or_Same (Scope1, Scope2 : Entity_Id) return Boolean is
17096 Scop : Entity_Id;
17097
17098 begin
17099 Scop := Scope1;
17100 while Scop /= Standard_Standard loop
17101 if Scop = Scope2 then
17102 return True;
17103 else
17104 Scop := Scope (Scop);
17105 end if;
17106 end loop;
17107
17108 return False;
17109 end Scope_Within_Or_Same;
17110
17111 --------------------
17112 -- Set_Convention --
17113 --------------------
17114
17115 procedure Set_Convention (E : Entity_Id; Val : Snames.Convention_Id) is
17116 begin
17117 Basic_Set_Convention (E, Val);
17118
17119 if Is_Type (E)
17120 and then Is_Access_Subprogram_Type (Base_Type (E))
17121 and then Has_Foreign_Convention (E)
17122 then
17123
17124 -- A pragma Convention in an instance may apply to the subtype
17125 -- created for a formal, in which case we have already verified
17126 -- that conventions of actual and formal match and there is nothing
17127 -- to flag on the subtype.
17128
17129 if In_Instance then
17130 null;
17131 else
17132 Set_Can_Use_Internal_Rep (E, False);
17133 end if;
17134 end if;
17135
17136 -- If E is an object or component, and the type of E is an anonymous
17137 -- access type with no convention set, then also set the convention of
17138 -- the anonymous access type. We do not do this for anonymous protected
17139 -- types, since protected types always have the default convention.
17140
17141 if Present (Etype (E))
17142 and then (Is_Object (E)
17143 or else Ekind (E) = E_Component
17144
17145 -- Allow E_Void (happens for pragma Convention appearing
17146 -- in the middle of a record applying to a component)
17147
17148 or else Ekind (E) = E_Void)
17149 then
17150 declare
17151 Typ : constant Entity_Id := Etype (E);
17152
17153 begin
17154 if Ekind_In (Typ, E_Anonymous_Access_Type,
17155 E_Anonymous_Access_Subprogram_Type)
17156 and then not Has_Convention_Pragma (Typ)
17157 then
17158 Basic_Set_Convention (Typ, Val);
17159 Set_Has_Convention_Pragma (Typ);
17160
17161 -- And for the access subprogram type, deal similarly with the
17162 -- designated E_Subprogram_Type if it is also internal (which
17163 -- it always is?)
17164
17165 if Ekind (Typ) = E_Anonymous_Access_Subprogram_Type then
17166 declare
17167 Dtype : constant Entity_Id := Designated_Type (Typ);
17168 begin
17169 if Ekind (Dtype) = E_Subprogram_Type
17170 and then Is_Itype (Dtype)
17171 and then not Has_Convention_Pragma (Dtype)
17172 then
17173 Basic_Set_Convention (Dtype, Val);
17174 Set_Has_Convention_Pragma (Dtype);
17175 end if;
17176 end;
17177 end if;
17178 end if;
17179 end;
17180 end if;
17181 end Set_Convention;
17182
17183 ------------------------
17184 -- Set_Current_Entity --
17185 ------------------------
17186
17187 -- The given entity is to be set as the currently visible definition of its
17188 -- associated name (i.e. the Node_Id associated with its name). All we have
17189 -- to do is to get the name from the identifier, and then set the
17190 -- associated Node_Id to point to the given entity.
17191
17192 procedure Set_Current_Entity (E : Entity_Id) is
17193 begin
17194 Set_Name_Entity_Id (Chars (E), E);
17195 end Set_Current_Entity;
17196
17197 ---------------------------
17198 -- Set_Debug_Info_Needed --
17199 ---------------------------
17200
17201 procedure Set_Debug_Info_Needed (T : Entity_Id) is
17202
17203 procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id);
17204 pragma Inline (Set_Debug_Info_Needed_If_Not_Set);
17205 -- Used to set debug info in a related node if not set already
17206
17207 --------------------------------------
17208 -- Set_Debug_Info_Needed_If_Not_Set --
17209 --------------------------------------
17210
17211 procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id) is
17212 begin
17213 if Present (E) and then not Needs_Debug_Info (E) then
17214 Set_Debug_Info_Needed (E);
17215
17216 -- For a private type, indicate that the full view also needs
17217 -- debug information.
17218
17219 if Is_Type (E)
17220 and then Is_Private_Type (E)
17221 and then Present (Full_View (E))
17222 then
17223 Set_Debug_Info_Needed (Full_View (E));
17224 end if;
17225 end if;
17226 end Set_Debug_Info_Needed_If_Not_Set;
17227
17228 -- Start of processing for Set_Debug_Info_Needed
17229
17230 begin
17231 -- Nothing to do if argument is Empty or has Debug_Info_Off set, which
17232 -- indicates that Debug_Info_Needed is never required for the entity.
17233 -- Nothing to do if entity comes from a predefined file. Library files
17234 -- are compiled without debug information, but inlined bodies of these
17235 -- routines may appear in user code, and debug information on them ends
17236 -- up complicating debugging the user code.
17237
17238 if No (T)
17239 or else Debug_Info_Off (T)
17240 then
17241 return;
17242
17243 elsif In_Inlined_Body
17244 and then Is_Predefined_File_Name
17245 (Unit_File_Name (Get_Source_Unit (Sloc (T))))
17246 then
17247 Set_Needs_Debug_Info (T, False);
17248 end if;
17249
17250 -- Set flag in entity itself. Note that we will go through the following
17251 -- circuitry even if the flag is already set on T. That's intentional,
17252 -- it makes sure that the flag will be set in subsidiary entities.
17253
17254 Set_Needs_Debug_Info (T);
17255
17256 -- Set flag on subsidiary entities if not set already
17257
17258 if Is_Object (T) then
17259 Set_Debug_Info_Needed_If_Not_Set (Etype (T));
17260
17261 elsif Is_Type (T) then
17262 Set_Debug_Info_Needed_If_Not_Set (Etype (T));
17263
17264 if Is_Record_Type (T) then
17265 declare
17266 Ent : Entity_Id := First_Entity (T);
17267 begin
17268 while Present (Ent) loop
17269 Set_Debug_Info_Needed_If_Not_Set (Ent);
17270 Next_Entity (Ent);
17271 end loop;
17272 end;
17273
17274 -- For a class wide subtype, we also need debug information
17275 -- for the equivalent type.
17276
17277 if Ekind (T) = E_Class_Wide_Subtype then
17278 Set_Debug_Info_Needed_If_Not_Set (Equivalent_Type (T));
17279 end if;
17280
17281 elsif Is_Array_Type (T) then
17282 Set_Debug_Info_Needed_If_Not_Set (Component_Type (T));
17283
17284 declare
17285 Indx : Node_Id := First_Index (T);
17286 begin
17287 while Present (Indx) loop
17288 Set_Debug_Info_Needed_If_Not_Set (Etype (Indx));
17289 Indx := Next_Index (Indx);
17290 end loop;
17291 end;
17292
17293 -- For a packed array type, we also need debug information for
17294 -- the type used to represent the packed array. Conversely, we
17295 -- also need it for the former if we need it for the latter.
17296
17297 if Is_Packed (T) then
17298 Set_Debug_Info_Needed_If_Not_Set (Packed_Array_Impl_Type (T));
17299 end if;
17300
17301 if Is_Packed_Array_Impl_Type (T) then
17302 Set_Debug_Info_Needed_If_Not_Set (Original_Array_Type (T));
17303 end if;
17304
17305 elsif Is_Access_Type (T) then
17306 Set_Debug_Info_Needed_If_Not_Set (Directly_Designated_Type (T));
17307
17308 elsif Is_Private_Type (T) then
17309 Set_Debug_Info_Needed_If_Not_Set (Full_View (T));
17310
17311 elsif Is_Protected_Type (T) then
17312 Set_Debug_Info_Needed_If_Not_Set (Corresponding_Record_Type (T));
17313
17314 elsif Is_Scalar_Type (T) then
17315
17316 -- If the subrange bounds are materialized by dedicated constant
17317 -- objects, also include them in the debug info to make sure the
17318 -- debugger can properly use them.
17319
17320 if Present (Scalar_Range (T))
17321 and then Nkind (Scalar_Range (T)) = N_Range
17322 then
17323 declare
17324 Low_Bnd : constant Node_Id := Type_Low_Bound (T);
17325 High_Bnd : constant Node_Id := Type_High_Bound (T);
17326
17327 begin
17328 if Is_Entity_Name (Low_Bnd) then
17329 Set_Debug_Info_Needed_If_Not_Set (Entity (Low_Bnd));
17330 end if;
17331
17332 if Is_Entity_Name (High_Bnd) then
17333 Set_Debug_Info_Needed_If_Not_Set (Entity (High_Bnd));
17334 end if;
17335 end;
17336 end if;
17337 end if;
17338 end if;
17339 end Set_Debug_Info_Needed;
17340
17341 ----------------------------
17342 -- Set_Entity_With_Checks --
17343 ----------------------------
17344
17345 procedure Set_Entity_With_Checks (N : Node_Id; Val : Entity_Id) is
17346 Val_Actual : Entity_Id;
17347 Nod : Node_Id;
17348 Post_Node : Node_Id;
17349
17350 begin
17351 -- Unconditionally set the entity
17352
17353 Set_Entity (N, Val);
17354
17355 -- The node to post on is the selector in the case of an expanded name,
17356 -- and otherwise the node itself.
17357
17358 if Nkind (N) = N_Expanded_Name then
17359 Post_Node := Selector_Name (N);
17360 else
17361 Post_Node := N;
17362 end if;
17363
17364 -- Check for violation of No_Fixed_IO
17365
17366 if Restriction_Check_Required (No_Fixed_IO)
17367 and then
17368 ((RTU_Loaded (Ada_Text_IO)
17369 and then (Is_RTE (Val, RE_Decimal_IO)
17370 or else
17371 Is_RTE (Val, RE_Fixed_IO)))
17372
17373 or else
17374 (RTU_Loaded (Ada_Wide_Text_IO)
17375 and then (Is_RTE (Val, RO_WT_Decimal_IO)
17376 or else
17377 Is_RTE (Val, RO_WT_Fixed_IO)))
17378
17379 or else
17380 (RTU_Loaded (Ada_Wide_Wide_Text_IO)
17381 and then (Is_RTE (Val, RO_WW_Decimal_IO)
17382 or else
17383 Is_RTE (Val, RO_WW_Fixed_IO))))
17384
17385 -- A special extra check, don't complain about a reference from within
17386 -- the Ada.Interrupts package itself!
17387
17388 and then not In_Same_Extended_Unit (N, Val)
17389 then
17390 Check_Restriction (No_Fixed_IO, Post_Node);
17391 end if;
17392
17393 -- Remaining checks are only done on source nodes. Note that we test
17394 -- for violation of No_Fixed_IO even on non-source nodes, because the
17395 -- cases for checking violations of this restriction are instantiations
17396 -- where the reference in the instance has Comes_From_Source False.
17397
17398 if not Comes_From_Source (N) then
17399 return;
17400 end if;
17401
17402 -- Check for violation of No_Abort_Statements, which is triggered by
17403 -- call to Ada.Task_Identification.Abort_Task.
17404
17405 if Restriction_Check_Required (No_Abort_Statements)
17406 and then (Is_RTE (Val, RE_Abort_Task))
17407
17408 -- A special extra check, don't complain about a reference from within
17409 -- the Ada.Task_Identification package itself!
17410
17411 and then not In_Same_Extended_Unit (N, Val)
17412 then
17413 Check_Restriction (No_Abort_Statements, Post_Node);
17414 end if;
17415
17416 if Val = Standard_Long_Long_Integer then
17417 Check_Restriction (No_Long_Long_Integers, Post_Node);
17418 end if;
17419
17420 -- Check for violation of No_Dynamic_Attachment
17421
17422 if Restriction_Check_Required (No_Dynamic_Attachment)
17423 and then RTU_Loaded (Ada_Interrupts)
17424 and then (Is_RTE (Val, RE_Is_Reserved) or else
17425 Is_RTE (Val, RE_Is_Attached) or else
17426 Is_RTE (Val, RE_Current_Handler) or else
17427 Is_RTE (Val, RE_Attach_Handler) or else
17428 Is_RTE (Val, RE_Exchange_Handler) or else
17429 Is_RTE (Val, RE_Detach_Handler) or else
17430 Is_RTE (Val, RE_Reference))
17431
17432 -- A special extra check, don't complain about a reference from within
17433 -- the Ada.Interrupts package itself!
17434
17435 and then not In_Same_Extended_Unit (N, Val)
17436 then
17437 Check_Restriction (No_Dynamic_Attachment, Post_Node);
17438 end if;
17439
17440 -- Check for No_Implementation_Identifiers
17441
17442 if Restriction_Check_Required (No_Implementation_Identifiers) then
17443
17444 -- We have an implementation defined entity if it is marked as
17445 -- implementation defined, or is defined in a package marked as
17446 -- implementation defined. However, library packages themselves
17447 -- are excluded (we don't want to flag Interfaces itself, just
17448 -- the entities within it).
17449
17450 if (Is_Implementation_Defined (Val)
17451 or else
17452 (Present (Scope (Val))
17453 and then Is_Implementation_Defined (Scope (Val))))
17454 and then not (Ekind_In (Val, E_Package, E_Generic_Package)
17455 and then Is_Library_Level_Entity (Val))
17456 then
17457 Check_Restriction (No_Implementation_Identifiers, Post_Node);
17458 end if;
17459 end if;
17460
17461 -- Do the style check
17462
17463 if Style_Check
17464 and then not Suppress_Style_Checks (Val)
17465 and then not In_Instance
17466 then
17467 if Nkind (N) = N_Identifier then
17468 Nod := N;
17469 elsif Nkind (N) = N_Expanded_Name then
17470 Nod := Selector_Name (N);
17471 else
17472 return;
17473 end if;
17474
17475 -- A special situation arises for derived operations, where we want
17476 -- to do the check against the parent (since the Sloc of the derived
17477 -- operation points to the derived type declaration itself).
17478
17479 Val_Actual := Val;
17480 while not Comes_From_Source (Val_Actual)
17481 and then Nkind (Val_Actual) in N_Entity
17482 and then (Ekind (Val_Actual) = E_Enumeration_Literal
17483 or else Is_Subprogram_Or_Generic_Subprogram (Val_Actual))
17484 and then Present (Alias (Val_Actual))
17485 loop
17486 Val_Actual := Alias (Val_Actual);
17487 end loop;
17488
17489 -- Renaming declarations for generic actuals do not come from source,
17490 -- and have a different name from that of the entity they rename, so
17491 -- there is no style check to perform here.
17492
17493 if Chars (Nod) = Chars (Val_Actual) then
17494 Style.Check_Identifier (Nod, Val_Actual);
17495 end if;
17496 end if;
17497
17498 Set_Entity (N, Val);
17499 end Set_Entity_With_Checks;
17500
17501 ------------------------
17502 -- Set_Name_Entity_Id --
17503 ------------------------
17504
17505 procedure Set_Name_Entity_Id (Id : Name_Id; Val : Entity_Id) is
17506 begin
17507 Set_Name_Table_Int (Id, Int (Val));
17508 end Set_Name_Entity_Id;
17509
17510 ---------------------
17511 -- Set_Next_Actual --
17512 ---------------------
17513
17514 procedure Set_Next_Actual (Ass1_Id : Node_Id; Ass2_Id : Node_Id) is
17515 begin
17516 if Nkind (Parent (Ass1_Id)) = N_Parameter_Association then
17517 Set_First_Named_Actual (Parent (Ass1_Id), Ass2_Id);
17518 end if;
17519 end Set_Next_Actual;
17520
17521 ----------------------------------
17522 -- Set_Optimize_Alignment_Flags --
17523 ----------------------------------
17524
17525 procedure Set_Optimize_Alignment_Flags (E : Entity_Id) is
17526 begin
17527 if Optimize_Alignment = 'S' then
17528 Set_Optimize_Alignment_Space (E);
17529 elsif Optimize_Alignment = 'T' then
17530 Set_Optimize_Alignment_Time (E);
17531 end if;
17532 end Set_Optimize_Alignment_Flags;
17533
17534 -----------------------
17535 -- Set_Public_Status --
17536 -----------------------
17537
17538 procedure Set_Public_Status (Id : Entity_Id) is
17539 S : constant Entity_Id := Current_Scope;
17540
17541 function Within_HSS_Or_If (E : Entity_Id) return Boolean;
17542 -- Determines if E is defined within handled statement sequence or
17543 -- an if statement, returns True if so, False otherwise.
17544
17545 ----------------------
17546 -- Within_HSS_Or_If --
17547 ----------------------
17548
17549 function Within_HSS_Or_If (E : Entity_Id) return Boolean is
17550 N : Node_Id;
17551 begin
17552 N := Declaration_Node (E);
17553 loop
17554 N := Parent (N);
17555
17556 if No (N) then
17557 return False;
17558
17559 elsif Nkind_In (N, N_Handled_Sequence_Of_Statements,
17560 N_If_Statement)
17561 then
17562 return True;
17563 end if;
17564 end loop;
17565 end Within_HSS_Or_If;
17566
17567 -- Start of processing for Set_Public_Status
17568
17569 begin
17570 -- Everything in the scope of Standard is public
17571
17572 if S = Standard_Standard then
17573 Set_Is_Public (Id);
17574
17575 -- Entity is definitely not public if enclosing scope is not public
17576
17577 elsif not Is_Public (S) then
17578 return;
17579
17580 -- An object or function declaration that occurs in a handled sequence
17581 -- of statements or within an if statement is the declaration for a
17582 -- temporary object or local subprogram generated by the expander. It
17583 -- never needs to be made public and furthermore, making it public can
17584 -- cause back end problems.
17585
17586 elsif Nkind_In (Parent (Id), N_Object_Declaration,
17587 N_Function_Specification)
17588 and then Within_HSS_Or_If (Id)
17589 then
17590 return;
17591
17592 -- Entities in public packages or records are public
17593
17594 elsif Ekind (S) = E_Package or Is_Record_Type (S) then
17595 Set_Is_Public (Id);
17596
17597 -- The bounds of an entry family declaration can generate object
17598 -- declarations that are visible to the back-end, e.g. in the
17599 -- the declaration of a composite type that contains tasks.
17600
17601 elsif Is_Concurrent_Type (S)
17602 and then not Has_Completion (S)
17603 and then Nkind (Parent (Id)) = N_Object_Declaration
17604 then
17605 Set_Is_Public (Id);
17606 end if;
17607 end Set_Public_Status;
17608
17609 -----------------------------
17610 -- Set_Referenced_Modified --
17611 -----------------------------
17612
17613 procedure Set_Referenced_Modified (N : Node_Id; Out_Param : Boolean) is
17614 Pref : Node_Id;
17615
17616 begin
17617 -- Deal with indexed or selected component where prefix is modified
17618
17619 if Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
17620 Pref := Prefix (N);
17621
17622 -- If prefix is access type, then it is the designated object that is
17623 -- being modified, which means we have no entity to set the flag on.
17624
17625 if No (Etype (Pref)) or else Is_Access_Type (Etype (Pref)) then
17626 return;
17627
17628 -- Otherwise chase the prefix
17629
17630 else
17631 Set_Referenced_Modified (Pref, Out_Param);
17632 end if;
17633
17634 -- Otherwise see if we have an entity name (only other case to process)
17635
17636 elsif Is_Entity_Name (N) and then Present (Entity (N)) then
17637 Set_Referenced_As_LHS (Entity (N), not Out_Param);
17638 Set_Referenced_As_Out_Parameter (Entity (N), Out_Param);
17639 end if;
17640 end Set_Referenced_Modified;
17641
17642 ----------------------------
17643 -- Set_Scope_Is_Transient --
17644 ----------------------------
17645
17646 procedure Set_Scope_Is_Transient (V : Boolean := True) is
17647 begin
17648 Scope_Stack.Table (Scope_Stack.Last).Is_Transient := V;
17649 end Set_Scope_Is_Transient;
17650
17651 -------------------
17652 -- Set_Size_Info --
17653 -------------------
17654
17655 procedure Set_Size_Info (T1, T2 : Entity_Id) is
17656 begin
17657 -- We copy Esize, but not RM_Size, since in general RM_Size is
17658 -- subtype specific and does not get inherited by all subtypes.
17659
17660 Set_Esize (T1, Esize (T2));
17661 Set_Has_Biased_Representation (T1, Has_Biased_Representation (T2));
17662
17663 if Is_Discrete_Or_Fixed_Point_Type (T1)
17664 and then
17665 Is_Discrete_Or_Fixed_Point_Type (T2)
17666 then
17667 Set_Is_Unsigned_Type (T1, Is_Unsigned_Type (T2));
17668 end if;
17669
17670 Set_Alignment (T1, Alignment (T2));
17671 end Set_Size_Info;
17672
17673 --------------------
17674 -- Static_Boolean --
17675 --------------------
17676
17677 function Static_Boolean (N : Node_Id) return Uint is
17678 begin
17679 Analyze_And_Resolve (N, Standard_Boolean);
17680
17681 if N = Error
17682 or else Error_Posted (N)
17683 or else Etype (N) = Any_Type
17684 then
17685 return No_Uint;
17686 end if;
17687
17688 if Is_OK_Static_Expression (N) then
17689 if not Raises_Constraint_Error (N) then
17690 return Expr_Value (N);
17691 else
17692 return No_Uint;
17693 end if;
17694
17695 elsif Etype (N) = Any_Type then
17696 return No_Uint;
17697
17698 else
17699 Flag_Non_Static_Expr
17700 ("static boolean expression required here", N);
17701 return No_Uint;
17702 end if;
17703 end Static_Boolean;
17704
17705 --------------------
17706 -- Static_Integer --
17707 --------------------
17708
17709 function Static_Integer (N : Node_Id) return Uint is
17710 begin
17711 Analyze_And_Resolve (N, Any_Integer);
17712
17713 if N = Error
17714 or else Error_Posted (N)
17715 or else Etype (N) = Any_Type
17716 then
17717 return No_Uint;
17718 end if;
17719
17720 if Is_OK_Static_Expression (N) then
17721 if not Raises_Constraint_Error (N) then
17722 return Expr_Value (N);
17723 else
17724 return No_Uint;
17725 end if;
17726
17727 elsif Etype (N) = Any_Type then
17728 return No_Uint;
17729
17730 else
17731 Flag_Non_Static_Expr
17732 ("static integer expression required here", N);
17733 return No_Uint;
17734 end if;
17735 end Static_Integer;
17736
17737 --------------------------
17738 -- Statically_Different --
17739 --------------------------
17740
17741 function Statically_Different (E1, E2 : Node_Id) return Boolean is
17742 R1 : constant Node_Id := Get_Referenced_Object (E1);
17743 R2 : constant Node_Id := Get_Referenced_Object (E2);
17744 begin
17745 return Is_Entity_Name (R1)
17746 and then Is_Entity_Name (R2)
17747 and then Entity (R1) /= Entity (R2)
17748 and then not Is_Formal (Entity (R1))
17749 and then not Is_Formal (Entity (R2));
17750 end Statically_Different;
17751
17752 --------------------------------------
17753 -- Subject_To_Loop_Entry_Attributes --
17754 --------------------------------------
17755
17756 function Subject_To_Loop_Entry_Attributes (N : Node_Id) return Boolean is
17757 Stmt : Node_Id;
17758
17759 begin
17760 Stmt := N;
17761
17762 -- The expansion mechanism transform a loop subject to at least one
17763 -- 'Loop_Entry attribute into a conditional block. Infinite loops lack
17764 -- the conditional part.
17765
17766 if Nkind_In (Stmt, N_Block_Statement, N_If_Statement)
17767 and then Nkind (Original_Node (N)) = N_Loop_Statement
17768 then
17769 Stmt := Original_Node (N);
17770 end if;
17771
17772 return
17773 Nkind (Stmt) = N_Loop_Statement
17774 and then Present (Identifier (Stmt))
17775 and then Present (Entity (Identifier (Stmt)))
17776 and then Has_Loop_Entry_Attributes (Entity (Identifier (Stmt)));
17777 end Subject_To_Loop_Entry_Attributes;
17778
17779 -----------------------------
17780 -- Subprogram_Access_Level --
17781 -----------------------------
17782
17783 function Subprogram_Access_Level (Subp : Entity_Id) return Uint is
17784 begin
17785 if Present (Alias (Subp)) then
17786 return Subprogram_Access_Level (Alias (Subp));
17787 else
17788 return Scope_Depth (Enclosing_Dynamic_Scope (Subp));
17789 end if;
17790 end Subprogram_Access_Level;
17791
17792 -------------------------------
17793 -- Support_Atomic_Primitives --
17794 -------------------------------
17795
17796 function Support_Atomic_Primitives (Typ : Entity_Id) return Boolean is
17797 Size : Int;
17798
17799 begin
17800 -- Verify the alignment of Typ is known
17801
17802 if not Known_Alignment (Typ) then
17803 return False;
17804 end if;
17805
17806 if Known_Static_Esize (Typ) then
17807 Size := UI_To_Int (Esize (Typ));
17808
17809 -- If the Esize (Object_Size) is unknown at compile time, look at the
17810 -- RM_Size (Value_Size) which may have been set by an explicit rep item.
17811
17812 elsif Known_Static_RM_Size (Typ) then
17813 Size := UI_To_Int (RM_Size (Typ));
17814
17815 -- Otherwise, the size is considered to be unknown.
17816
17817 else
17818 return False;
17819 end if;
17820
17821 -- Check that the size of the component is 8, 16, 32 or 64 bits and that
17822 -- Typ is properly aligned.
17823
17824 case Size is
17825 when 8 | 16 | 32 | 64 =>
17826 return Size = UI_To_Int (Alignment (Typ)) * 8;
17827 when others =>
17828 return False;
17829 end case;
17830 end Support_Atomic_Primitives;
17831
17832 -----------------
17833 -- Trace_Scope --
17834 -----------------
17835
17836 procedure Trace_Scope (N : Node_Id; E : Entity_Id; Msg : String) is
17837 begin
17838 if Debug_Flag_W then
17839 for J in 0 .. Scope_Stack.Last loop
17840 Write_Str (" ");
17841 end loop;
17842
17843 Write_Str (Msg);
17844 Write_Name (Chars (E));
17845 Write_Str (" from ");
17846 Write_Location (Sloc (N));
17847 Write_Eol;
17848 end if;
17849 end Trace_Scope;
17850
17851 -----------------------
17852 -- Transfer_Entities --
17853 -----------------------
17854
17855 procedure Transfer_Entities (From : Entity_Id; To : Entity_Id) is
17856 procedure Set_Public_Status_Of (Id : Entity_Id);
17857 -- Set the Is_Public attribute of arbitrary entity Id by calling routine
17858 -- Set_Public_Status. If successfull and Id denotes a record type, set
17859 -- the Is_Public attribute of its fields.
17860
17861 --------------------------
17862 -- Set_Public_Status_Of --
17863 --------------------------
17864
17865 procedure Set_Public_Status_Of (Id : Entity_Id) is
17866 Field : Entity_Id;
17867
17868 begin
17869 if not Is_Public (Id) then
17870 Set_Public_Status (Id);
17871
17872 -- When the input entity is a public record type, ensure that all
17873 -- its internal fields are also exposed to the linker. The fields
17874 -- of a class-wide type are never made public.
17875
17876 if Is_Public (Id)
17877 and then Is_Record_Type (Id)
17878 and then not Is_Class_Wide_Type (Id)
17879 then
17880 Field := First_Entity (Id);
17881 while Present (Field) loop
17882 Set_Is_Public (Field);
17883 Next_Entity (Field);
17884 end loop;
17885 end if;
17886 end if;
17887 end Set_Public_Status_Of;
17888
17889 -- Local variables
17890
17891 Full_Id : Entity_Id;
17892 Id : Entity_Id;
17893
17894 -- Start of processing for Transfer_Entities
17895
17896 begin
17897 Id := First_Entity (From);
17898
17899 if Present (Id) then
17900
17901 -- Merge the entity chain of the source scope with that of the
17902 -- destination scope.
17903
17904 if Present (Last_Entity (To)) then
17905 Set_Next_Entity (Last_Entity (To), Id);
17906 else
17907 Set_First_Entity (To, Id);
17908 end if;
17909
17910 Set_Last_Entity (To, Last_Entity (From));
17911
17912 -- Inspect the entities of the source scope and update their Scope
17913 -- attribute.
17914
17915 while Present (Id) loop
17916 Set_Scope (Id, To);
17917 Set_Public_Status_Of (Id);
17918
17919 -- Handle an internally generated full view for a private type
17920
17921 if Is_Private_Type (Id)
17922 and then Present (Full_View (Id))
17923 and then Is_Itype (Full_View (Id))
17924 then
17925 Full_Id := Full_View (Id);
17926
17927 Set_Scope (Full_Id, To);
17928 Set_Public_Status_Of (Full_Id);
17929 end if;
17930
17931 Next_Entity (Id);
17932 end loop;
17933
17934 Set_First_Entity (From, Empty);
17935 Set_Last_Entity (From, Empty);
17936 end if;
17937 end Transfer_Entities;
17938
17939 -----------------------
17940 -- Type_Access_Level --
17941 -----------------------
17942
17943 function Type_Access_Level (Typ : Entity_Id) return Uint is
17944 Btyp : Entity_Id;
17945
17946 begin
17947 Btyp := Base_Type (Typ);
17948
17949 -- Ada 2005 (AI-230): For most cases of anonymous access types, we
17950 -- simply use the level where the type is declared. This is true for
17951 -- stand-alone object declarations, and for anonymous access types
17952 -- associated with components the level is the same as that of the
17953 -- enclosing composite type. However, special treatment is needed for
17954 -- the cases of access parameters, return objects of an anonymous access
17955 -- type, and, in Ada 95, access discriminants of limited types.
17956
17957 if Is_Access_Type (Btyp) then
17958 if Ekind (Btyp) = E_Anonymous_Access_Type then
17959
17960 -- If the type is a nonlocal anonymous access type (such as for
17961 -- an access parameter) we treat it as being declared at the
17962 -- library level to ensure that names such as X.all'access don't
17963 -- fail static accessibility checks.
17964
17965 if not Is_Local_Anonymous_Access (Typ) then
17966 return Scope_Depth (Standard_Standard);
17967
17968 -- If this is a return object, the accessibility level is that of
17969 -- the result subtype of the enclosing function. The test here is
17970 -- little complicated, because we have to account for extended
17971 -- return statements that have been rewritten as blocks, in which
17972 -- case we have to find and the Is_Return_Object attribute of the
17973 -- itype's associated object. It would be nice to find a way to
17974 -- simplify this test, but it doesn't seem worthwhile to add a new
17975 -- flag just for purposes of this test. ???
17976
17977 elsif Ekind (Scope (Btyp)) = E_Return_Statement
17978 or else
17979 (Is_Itype (Btyp)
17980 and then Nkind (Associated_Node_For_Itype (Btyp)) =
17981 N_Object_Declaration
17982 and then Is_Return_Object
17983 (Defining_Identifier
17984 (Associated_Node_For_Itype (Btyp))))
17985 then
17986 declare
17987 Scop : Entity_Id;
17988
17989 begin
17990 Scop := Scope (Scope (Btyp));
17991 while Present (Scop) loop
17992 exit when Ekind (Scop) = E_Function;
17993 Scop := Scope (Scop);
17994 end loop;
17995
17996 -- Treat the return object's type as having the level of the
17997 -- function's result subtype (as per RM05-6.5(5.3/2)).
17998
17999 return Type_Access_Level (Etype (Scop));
18000 end;
18001 end if;
18002 end if;
18003
18004 Btyp := Root_Type (Btyp);
18005
18006 -- The accessibility level of anonymous access types associated with
18007 -- discriminants is that of the current instance of the type, and
18008 -- that's deeper than the type itself (AARM 3.10.2 (12.3.21)).
18009
18010 -- AI-402: access discriminants have accessibility based on the
18011 -- object rather than the type in Ada 2005, so the above paragraph
18012 -- doesn't apply.
18013
18014 -- ??? Needs completion with rules from AI-416
18015
18016 if Ada_Version <= Ada_95
18017 and then Ekind (Typ) = E_Anonymous_Access_Type
18018 and then Present (Associated_Node_For_Itype (Typ))
18019 and then Nkind (Associated_Node_For_Itype (Typ)) =
18020 N_Discriminant_Specification
18021 then
18022 return Scope_Depth (Enclosing_Dynamic_Scope (Btyp)) + 1;
18023 end if;
18024 end if;
18025
18026 -- Return library level for a generic formal type. This is done because
18027 -- RM(10.3.2) says that "The statically deeper relationship does not
18028 -- apply to ... a descendant of a generic formal type". Rather than
18029 -- checking at each point where a static accessibility check is
18030 -- performed to see if we are dealing with a formal type, this rule is
18031 -- implemented by having Type_Access_Level and Deepest_Type_Access_Level
18032 -- return extreme values for a formal type; Deepest_Type_Access_Level
18033 -- returns Int'Last. By calling the appropriate function from among the
18034 -- two, we ensure that the static accessibility check will pass if we
18035 -- happen to run into a formal type. More specifically, we should call
18036 -- Deepest_Type_Access_Level instead of Type_Access_Level whenever the
18037 -- call occurs as part of a static accessibility check and the error
18038 -- case is the case where the type's level is too shallow (as opposed
18039 -- to too deep).
18040
18041 if Is_Generic_Type (Root_Type (Btyp)) then
18042 return Scope_Depth (Standard_Standard);
18043 end if;
18044
18045 return Scope_Depth (Enclosing_Dynamic_Scope (Btyp));
18046 end Type_Access_Level;
18047
18048 ------------------------------------
18049 -- Type_Without_Stream_Operation --
18050 ------------------------------------
18051
18052 function Type_Without_Stream_Operation
18053 (T : Entity_Id;
18054 Op : TSS_Name_Type := TSS_Null) return Entity_Id
18055 is
18056 BT : constant Entity_Id := Base_Type (T);
18057 Op_Missing : Boolean;
18058
18059 begin
18060 if not Restriction_Active (No_Default_Stream_Attributes) then
18061 return Empty;
18062 end if;
18063
18064 if Is_Elementary_Type (T) then
18065 if Op = TSS_Null then
18066 Op_Missing :=
18067 No (TSS (BT, TSS_Stream_Read))
18068 or else No (TSS (BT, TSS_Stream_Write));
18069
18070 else
18071 Op_Missing := No (TSS (BT, Op));
18072 end if;
18073
18074 if Op_Missing then
18075 return T;
18076 else
18077 return Empty;
18078 end if;
18079
18080 elsif Is_Array_Type (T) then
18081 return Type_Without_Stream_Operation (Component_Type (T), Op);
18082
18083 elsif Is_Record_Type (T) then
18084 declare
18085 Comp : Entity_Id;
18086 C_Typ : Entity_Id;
18087
18088 begin
18089 Comp := First_Component (T);
18090 while Present (Comp) loop
18091 C_Typ := Type_Without_Stream_Operation (Etype (Comp), Op);
18092
18093 if Present (C_Typ) then
18094 return C_Typ;
18095 end if;
18096
18097 Next_Component (Comp);
18098 end loop;
18099
18100 return Empty;
18101 end;
18102
18103 elsif Is_Private_Type (T) and then Present (Full_View (T)) then
18104 return Type_Without_Stream_Operation (Full_View (T), Op);
18105 else
18106 return Empty;
18107 end if;
18108 end Type_Without_Stream_Operation;
18109
18110 ----------------------------
18111 -- Unique_Defining_Entity --
18112 ----------------------------
18113
18114 function Unique_Defining_Entity (N : Node_Id) return Entity_Id is
18115 begin
18116 return Unique_Entity (Defining_Entity (N));
18117 end Unique_Defining_Entity;
18118
18119 -------------------
18120 -- Unique_Entity --
18121 -------------------
18122
18123 function Unique_Entity (E : Entity_Id) return Entity_Id is
18124 U : Entity_Id := E;
18125 P : Node_Id;
18126
18127 begin
18128 case Ekind (E) is
18129 when E_Constant =>
18130 if Present (Full_View (E)) then
18131 U := Full_View (E);
18132 end if;
18133
18134 when Type_Kind =>
18135 if Present (Full_View (E)) then
18136 U := Full_View (E);
18137 end if;
18138
18139 when E_Package_Body =>
18140 P := Parent (E);
18141
18142 if Nkind (P) = N_Defining_Program_Unit_Name then
18143 P := Parent (P);
18144 end if;
18145
18146 U := Corresponding_Spec (P);
18147
18148 when E_Subprogram_Body =>
18149 P := Parent (E);
18150
18151 if Nkind (P) = N_Defining_Program_Unit_Name then
18152 P := Parent (P);
18153 end if;
18154
18155 P := Parent (P);
18156
18157 if Nkind (P) = N_Subprogram_Body_Stub then
18158 if Present (Library_Unit (P)) then
18159
18160 -- Get to the function or procedure (generic) entity through
18161 -- the body entity.
18162
18163 U :=
18164 Unique_Entity (Defining_Entity (Get_Body_From_Stub (P)));
18165 end if;
18166 else
18167 U := Corresponding_Spec (P);
18168 end if;
18169
18170 when Formal_Kind =>
18171 if Present (Spec_Entity (E)) then
18172 U := Spec_Entity (E);
18173 end if;
18174
18175 when others =>
18176 null;
18177 end case;
18178
18179 return U;
18180 end Unique_Entity;
18181
18182 -----------------
18183 -- Unique_Name --
18184 -----------------
18185
18186 function Unique_Name (E : Entity_Id) return String is
18187
18188 -- Names of E_Subprogram_Body or E_Package_Body entities are not
18189 -- reliable, as they may not include the overloading suffix. Instead,
18190 -- when looking for the name of E or one of its enclosing scope, we get
18191 -- the name of the corresponding Unique_Entity.
18192
18193 function Get_Scoped_Name (E : Entity_Id) return String;
18194 -- Return the name of E prefixed by all the names of the scopes to which
18195 -- E belongs, except for Standard.
18196
18197 ---------------------
18198 -- Get_Scoped_Name --
18199 ---------------------
18200
18201 function Get_Scoped_Name (E : Entity_Id) return String is
18202 Name : constant String := Get_Name_String (Chars (E));
18203 begin
18204 if Has_Fully_Qualified_Name (E)
18205 or else Scope (E) = Standard_Standard
18206 then
18207 return Name;
18208 else
18209 return Get_Scoped_Name (Unique_Entity (Scope (E))) & "__" & Name;
18210 end if;
18211 end Get_Scoped_Name;
18212
18213 -- Start of processing for Unique_Name
18214
18215 begin
18216 if E = Standard_Standard then
18217 return Get_Name_String (Name_Standard);
18218
18219 elsif Scope (E) = Standard_Standard
18220 and then not (Ekind (E) = E_Package or else Is_Subprogram (E))
18221 then
18222 return Get_Name_String (Name_Standard) & "__" &
18223 Get_Name_String (Chars (E));
18224
18225 elsif Ekind (E) = E_Enumeration_Literal then
18226 return Unique_Name (Etype (E)) & "__" & Get_Name_String (Chars (E));
18227
18228 else
18229 return Get_Scoped_Name (Unique_Entity (E));
18230 end if;
18231 end Unique_Name;
18232
18233 ---------------------
18234 -- Unit_Is_Visible --
18235 ---------------------
18236
18237 function Unit_Is_Visible (U : Entity_Id) return Boolean is
18238 Curr : constant Node_Id := Cunit (Current_Sem_Unit);
18239 Curr_Entity : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
18240
18241 function Unit_In_Parent_Context (Par_Unit : Node_Id) return Boolean;
18242 -- For a child unit, check whether unit appears in a with_clause
18243 -- of a parent.
18244
18245 function Unit_In_Context (Comp_Unit : Node_Id) return Boolean;
18246 -- Scan the context clause of one compilation unit looking for a
18247 -- with_clause for the unit in question.
18248
18249 ----------------------------
18250 -- Unit_In_Parent_Context --
18251 ----------------------------
18252
18253 function Unit_In_Parent_Context (Par_Unit : Node_Id) return Boolean is
18254 begin
18255 if Unit_In_Context (Par_Unit) then
18256 return True;
18257
18258 elsif Is_Child_Unit (Defining_Entity (Unit (Par_Unit))) then
18259 return Unit_In_Parent_Context (Parent_Spec (Unit (Par_Unit)));
18260
18261 else
18262 return False;
18263 end if;
18264 end Unit_In_Parent_Context;
18265
18266 ---------------------
18267 -- Unit_In_Context --
18268 ---------------------
18269
18270 function Unit_In_Context (Comp_Unit : Node_Id) return Boolean is
18271 Clause : Node_Id;
18272
18273 begin
18274 Clause := First (Context_Items (Comp_Unit));
18275 while Present (Clause) loop
18276 if Nkind (Clause) = N_With_Clause then
18277 if Library_Unit (Clause) = U then
18278 return True;
18279
18280 -- The with_clause may denote a renaming of the unit we are
18281 -- looking for, eg. Text_IO which renames Ada.Text_IO.
18282
18283 elsif
18284 Renamed_Entity (Entity (Name (Clause))) =
18285 Defining_Entity (Unit (U))
18286 then
18287 return True;
18288 end if;
18289 end if;
18290
18291 Next (Clause);
18292 end loop;
18293
18294 return False;
18295 end Unit_In_Context;
18296
18297 -- Start of processing for Unit_Is_Visible
18298
18299 begin
18300 -- The currrent unit is directly visible
18301
18302 if Curr = U then
18303 return True;
18304
18305 elsif Unit_In_Context (Curr) then
18306 return True;
18307
18308 -- If the current unit is a body, check the context of the spec
18309
18310 elsif Nkind (Unit (Curr)) = N_Package_Body
18311 or else
18312 (Nkind (Unit (Curr)) = N_Subprogram_Body
18313 and then not Acts_As_Spec (Unit (Curr)))
18314 then
18315 if Unit_In_Context (Library_Unit (Curr)) then
18316 return True;
18317 end if;
18318 end if;
18319
18320 -- If the spec is a child unit, examine the parents
18321
18322 if Is_Child_Unit (Curr_Entity) then
18323 if Nkind (Unit (Curr)) in N_Unit_Body then
18324 return
18325 Unit_In_Parent_Context
18326 (Parent_Spec (Unit (Library_Unit (Curr))));
18327 else
18328 return Unit_In_Parent_Context (Parent_Spec (Unit (Curr)));
18329 end if;
18330
18331 else
18332 return False;
18333 end if;
18334 end Unit_Is_Visible;
18335
18336 ------------------------------
18337 -- Universal_Interpretation --
18338 ------------------------------
18339
18340 function Universal_Interpretation (Opnd : Node_Id) return Entity_Id is
18341 Index : Interp_Index;
18342 It : Interp;
18343
18344 begin
18345 -- The argument may be a formal parameter of an operator or subprogram
18346 -- with multiple interpretations, or else an expression for an actual.
18347
18348 if Nkind (Opnd) = N_Defining_Identifier
18349 or else not Is_Overloaded (Opnd)
18350 then
18351 if Etype (Opnd) = Universal_Integer
18352 or else Etype (Opnd) = Universal_Real
18353 then
18354 return Etype (Opnd);
18355 else
18356 return Empty;
18357 end if;
18358
18359 else
18360 Get_First_Interp (Opnd, Index, It);
18361 while Present (It.Typ) loop
18362 if It.Typ = Universal_Integer
18363 or else It.Typ = Universal_Real
18364 then
18365 return It.Typ;
18366 end if;
18367
18368 Get_Next_Interp (Index, It);
18369 end loop;
18370
18371 return Empty;
18372 end if;
18373 end Universal_Interpretation;
18374
18375 ---------------
18376 -- Unqualify --
18377 ---------------
18378
18379 function Unqualify (Expr : Node_Id) return Node_Id is
18380 begin
18381 -- Recurse to handle unlikely case of multiple levels of qualification
18382
18383 if Nkind (Expr) = N_Qualified_Expression then
18384 return Unqualify (Expression (Expr));
18385
18386 -- Normal case, not a qualified expression
18387
18388 else
18389 return Expr;
18390 end if;
18391 end Unqualify;
18392
18393 -----------------------
18394 -- Visible_Ancestors --
18395 -----------------------
18396
18397 function Visible_Ancestors (Typ : Entity_Id) return Elist_Id is
18398 List_1 : Elist_Id;
18399 List_2 : Elist_Id;
18400 Elmt : Elmt_Id;
18401
18402 begin
18403 pragma Assert (Is_Record_Type (Typ) and then Is_Tagged_Type (Typ));
18404
18405 -- Collect all the parents and progenitors of Typ. If the full-view of
18406 -- private parents and progenitors is available then it is used to
18407 -- generate the list of visible ancestors; otherwise their partial
18408 -- view is added to the resulting list.
18409
18410 Collect_Parents
18411 (T => Typ,
18412 List => List_1,
18413 Use_Full_View => True);
18414
18415 Collect_Interfaces
18416 (T => Typ,
18417 Ifaces_List => List_2,
18418 Exclude_Parents => True,
18419 Use_Full_View => True);
18420
18421 -- Join the two lists. Avoid duplications because an interface may
18422 -- simultaneously be parent and progenitor of a type.
18423
18424 Elmt := First_Elmt (List_2);
18425 while Present (Elmt) loop
18426 Append_Unique_Elmt (Node (Elmt), List_1);
18427 Next_Elmt (Elmt);
18428 end loop;
18429
18430 return List_1;
18431 end Visible_Ancestors;
18432
18433 ----------------------
18434 -- Within_Init_Proc --
18435 ----------------------
18436
18437 function Within_Init_Proc return Boolean is
18438 S : Entity_Id;
18439
18440 begin
18441 S := Current_Scope;
18442 while not Is_Overloadable (S) loop
18443 if S = Standard_Standard then
18444 return False;
18445 else
18446 S := Scope (S);
18447 end if;
18448 end loop;
18449
18450 return Is_Init_Proc (S);
18451 end Within_Init_Proc;
18452
18453 ------------------
18454 -- Within_Scope --
18455 ------------------
18456
18457 function Within_Scope (E : Entity_Id; S : Entity_Id) return Boolean is
18458 SE : Entity_Id;
18459 begin
18460 SE := Scope (E);
18461 loop
18462 if SE = S then
18463 return True;
18464 elsif SE = Standard_Standard then
18465 return False;
18466 else
18467 SE := Scope (SE);
18468 end if;
18469 end loop;
18470 end Within_Scope;
18471
18472 ----------------
18473 -- Wrong_Type --
18474 ----------------
18475
18476 procedure Wrong_Type (Expr : Node_Id; Expected_Type : Entity_Id) is
18477 Found_Type : constant Entity_Id := First_Subtype (Etype (Expr));
18478 Expec_Type : constant Entity_Id := First_Subtype (Expected_Type);
18479
18480 Matching_Field : Entity_Id;
18481 -- Entity to give a more precise suggestion on how to write a one-
18482 -- element positional aggregate.
18483
18484 function Has_One_Matching_Field return Boolean;
18485 -- Determines if Expec_Type is a record type with a single component or
18486 -- discriminant whose type matches the found type or is one dimensional
18487 -- array whose component type matches the found type. In the case of
18488 -- one discriminant, we ignore the variant parts. That's not accurate,
18489 -- but good enough for the warning.
18490
18491 ----------------------------
18492 -- Has_One_Matching_Field --
18493 ----------------------------
18494
18495 function Has_One_Matching_Field return Boolean is
18496 E : Entity_Id;
18497
18498 begin
18499 Matching_Field := Empty;
18500
18501 if Is_Array_Type (Expec_Type)
18502 and then Number_Dimensions (Expec_Type) = 1
18503 and then Covers (Etype (Component_Type (Expec_Type)), Found_Type)
18504 then
18505 -- Use type name if available. This excludes multidimensional
18506 -- arrays and anonymous arrays.
18507
18508 if Comes_From_Source (Expec_Type) then
18509 Matching_Field := Expec_Type;
18510
18511 -- For an assignment, use name of target
18512
18513 elsif Nkind (Parent (Expr)) = N_Assignment_Statement
18514 and then Is_Entity_Name (Name (Parent (Expr)))
18515 then
18516 Matching_Field := Entity (Name (Parent (Expr)));
18517 end if;
18518
18519 return True;
18520
18521 elsif not Is_Record_Type (Expec_Type) then
18522 return False;
18523
18524 else
18525 E := First_Entity (Expec_Type);
18526 loop
18527 if No (E) then
18528 return False;
18529
18530 elsif not Ekind_In (E, E_Discriminant, E_Component)
18531 or else Nam_In (Chars (E), Name_uTag, Name_uParent)
18532 then
18533 Next_Entity (E);
18534
18535 else
18536 exit;
18537 end if;
18538 end loop;
18539
18540 if not Covers (Etype (E), Found_Type) then
18541 return False;
18542
18543 elsif Present (Next_Entity (E))
18544 and then (Ekind (E) = E_Component
18545 or else Ekind (Next_Entity (E)) = E_Discriminant)
18546 then
18547 return False;
18548
18549 else
18550 Matching_Field := E;
18551 return True;
18552 end if;
18553 end if;
18554 end Has_One_Matching_Field;
18555
18556 -- Start of processing for Wrong_Type
18557
18558 begin
18559 -- Don't output message if either type is Any_Type, or if a message
18560 -- has already been posted for this node. We need to do the latter
18561 -- check explicitly (it is ordinarily done in Errout), because we
18562 -- are using ! to force the output of the error messages.
18563
18564 if Expec_Type = Any_Type
18565 or else Found_Type = Any_Type
18566 or else Error_Posted (Expr)
18567 then
18568 return;
18569
18570 -- If one of the types is a Taft-Amendment type and the other it its
18571 -- completion, it must be an illegal use of a TAT in the spec, for
18572 -- which an error was already emitted. Avoid cascaded errors.
18573
18574 elsif Is_Incomplete_Type (Expec_Type)
18575 and then Has_Completion_In_Body (Expec_Type)
18576 and then Full_View (Expec_Type) = Etype (Expr)
18577 then
18578 return;
18579
18580 elsif Is_Incomplete_Type (Etype (Expr))
18581 and then Has_Completion_In_Body (Etype (Expr))
18582 and then Full_View (Etype (Expr)) = Expec_Type
18583 then
18584 return;
18585
18586 -- In an instance, there is an ongoing problem with completion of
18587 -- type derived from private types. Their structure is what Gigi
18588 -- expects, but the Etype is the parent type rather than the
18589 -- derived private type itself. Do not flag error in this case. The
18590 -- private completion is an entity without a parent, like an Itype.
18591 -- Similarly, full and partial views may be incorrect in the instance.
18592 -- There is no simple way to insure that it is consistent ???
18593
18594 -- A similar view discrepancy can happen in an inlined body, for the
18595 -- same reason: inserted body may be outside of the original package
18596 -- and only partial views are visible at the point of insertion.
18597
18598 elsif In_Instance or else In_Inlined_Body then
18599 if Etype (Etype (Expr)) = Etype (Expected_Type)
18600 and then
18601 (Has_Private_Declaration (Expected_Type)
18602 or else Has_Private_Declaration (Etype (Expr)))
18603 and then No (Parent (Expected_Type))
18604 then
18605 return;
18606
18607 elsif Nkind (Parent (Expr)) = N_Qualified_Expression
18608 and then Entity (Subtype_Mark (Parent (Expr))) = Expected_Type
18609 then
18610 return;
18611
18612 elsif Is_Private_Type (Expected_Type)
18613 and then Present (Full_View (Expected_Type))
18614 and then Covers (Full_View (Expected_Type), Etype (Expr))
18615 then
18616 return;
18617 end if;
18618 end if;
18619
18620 -- An interesting special check. If the expression is parenthesized
18621 -- and its type corresponds to the type of the sole component of the
18622 -- expected record type, or to the component type of the expected one
18623 -- dimensional array type, then assume we have a bad aggregate attempt.
18624
18625 if Nkind (Expr) in N_Subexpr
18626 and then Paren_Count (Expr) /= 0
18627 and then Has_One_Matching_Field
18628 then
18629 Error_Msg_N ("positional aggregate cannot have one component", Expr);
18630 if Present (Matching_Field) then
18631 if Is_Array_Type (Expec_Type) then
18632 Error_Msg_NE
18633 ("\write instead `&''First ='> ...`", Expr, Matching_Field);
18634
18635 else
18636 Error_Msg_NE
18637 ("\write instead `& ='> ...`", Expr, Matching_Field);
18638 end if;
18639 end if;
18640
18641 -- Another special check, if we are looking for a pool-specific access
18642 -- type and we found an E_Access_Attribute_Type, then we have the case
18643 -- of an Access attribute being used in a context which needs a pool-
18644 -- specific type, which is never allowed. The one extra check we make
18645 -- is that the expected designated type covers the Found_Type.
18646
18647 elsif Is_Access_Type (Expec_Type)
18648 and then Ekind (Found_Type) = E_Access_Attribute_Type
18649 and then Ekind (Base_Type (Expec_Type)) /= E_General_Access_Type
18650 and then Ekind (Base_Type (Expec_Type)) /= E_Anonymous_Access_Type
18651 and then Covers
18652 (Designated_Type (Expec_Type), Designated_Type (Found_Type))
18653 then
18654 Error_Msg_N -- CODEFIX
18655 ("result must be general access type!", Expr);
18656 Error_Msg_NE -- CODEFIX
18657 ("add ALL to }!", Expr, Expec_Type);
18658
18659 -- Another special check, if the expected type is an integer type,
18660 -- but the expression is of type System.Address, and the parent is
18661 -- an addition or subtraction operation whose left operand is the
18662 -- expression in question and whose right operand is of an integral
18663 -- type, then this is an attempt at address arithmetic, so give
18664 -- appropriate message.
18665
18666 elsif Is_Integer_Type (Expec_Type)
18667 and then Is_RTE (Found_Type, RE_Address)
18668 and then Nkind_In (Parent (Expr), N_Op_Add, N_Op_Subtract)
18669 and then Expr = Left_Opnd (Parent (Expr))
18670 and then Is_Integer_Type (Etype (Right_Opnd (Parent (Expr))))
18671 then
18672 Error_Msg_N
18673 ("address arithmetic not predefined in package System",
18674 Parent (Expr));
18675 Error_Msg_N
18676 ("\possible missing with/use of System.Storage_Elements",
18677 Parent (Expr));
18678 return;
18679
18680 -- If the expected type is an anonymous access type, as for access
18681 -- parameters and discriminants, the error is on the designated types.
18682
18683 elsif Ekind (Expec_Type) = E_Anonymous_Access_Type then
18684 if Comes_From_Source (Expec_Type) then
18685 Error_Msg_NE ("expected}!", Expr, Expec_Type);
18686 else
18687 Error_Msg_NE
18688 ("expected an access type with designated}",
18689 Expr, Designated_Type (Expec_Type));
18690 end if;
18691
18692 if Is_Access_Type (Found_Type)
18693 and then not Comes_From_Source (Found_Type)
18694 then
18695 Error_Msg_NE
18696 ("\\found an access type with designated}!",
18697 Expr, Designated_Type (Found_Type));
18698 else
18699 if From_Limited_With (Found_Type) then
18700 Error_Msg_NE ("\\found incomplete}!", Expr, Found_Type);
18701 Error_Msg_Qual_Level := 99;
18702 Error_Msg_NE -- CODEFIX
18703 ("\\missing `WITH &;", Expr, Scope (Found_Type));
18704 Error_Msg_Qual_Level := 0;
18705 else
18706 Error_Msg_NE ("found}!", Expr, Found_Type);
18707 end if;
18708 end if;
18709
18710 -- Normal case of one type found, some other type expected
18711
18712 else
18713 -- If the names of the two types are the same, see if some number
18714 -- of levels of qualification will help. Don't try more than three
18715 -- levels, and if we get to standard, it's no use (and probably
18716 -- represents an error in the compiler) Also do not bother with
18717 -- internal scope names.
18718
18719 declare
18720 Expec_Scope : Entity_Id;
18721 Found_Scope : Entity_Id;
18722
18723 begin
18724 Expec_Scope := Expec_Type;
18725 Found_Scope := Found_Type;
18726
18727 for Levels in Int range 0 .. 3 loop
18728 if Chars (Expec_Scope) /= Chars (Found_Scope) then
18729 Error_Msg_Qual_Level := Levels;
18730 exit;
18731 end if;
18732
18733 Expec_Scope := Scope (Expec_Scope);
18734 Found_Scope := Scope (Found_Scope);
18735
18736 exit when Expec_Scope = Standard_Standard
18737 or else Found_Scope = Standard_Standard
18738 or else not Comes_From_Source (Expec_Scope)
18739 or else not Comes_From_Source (Found_Scope);
18740 end loop;
18741 end;
18742
18743 if Is_Record_Type (Expec_Type)
18744 and then Present (Corresponding_Remote_Type (Expec_Type))
18745 then
18746 Error_Msg_NE ("expected}!", Expr,
18747 Corresponding_Remote_Type (Expec_Type));
18748 else
18749 Error_Msg_NE ("expected}!", Expr, Expec_Type);
18750 end if;
18751
18752 if Is_Entity_Name (Expr)
18753 and then Is_Package_Or_Generic_Package (Entity (Expr))
18754 then
18755 Error_Msg_N ("\\found package name!", Expr);
18756
18757 elsif Is_Entity_Name (Expr)
18758 and then Ekind_In (Entity (Expr), E_Procedure, E_Generic_Procedure)
18759 then
18760 if Ekind (Expec_Type) = E_Access_Subprogram_Type then
18761 Error_Msg_N
18762 ("found procedure name, possibly missing Access attribute!",
18763 Expr);
18764 else
18765 Error_Msg_N
18766 ("\\found procedure name instead of function!", Expr);
18767 end if;
18768
18769 elsif Nkind (Expr) = N_Function_Call
18770 and then Ekind (Expec_Type) = E_Access_Subprogram_Type
18771 and then Etype (Designated_Type (Expec_Type)) = Etype (Expr)
18772 and then No (Parameter_Associations (Expr))
18773 then
18774 Error_Msg_N
18775 ("found function name, possibly missing Access attribute!",
18776 Expr);
18777
18778 -- Catch common error: a prefix or infix operator which is not
18779 -- directly visible because the type isn't.
18780
18781 elsif Nkind (Expr) in N_Op
18782 and then Is_Overloaded (Expr)
18783 and then not Is_Immediately_Visible (Expec_Type)
18784 and then not Is_Potentially_Use_Visible (Expec_Type)
18785 and then not In_Use (Expec_Type)
18786 and then Has_Compatible_Type (Right_Opnd (Expr), Expec_Type)
18787 then
18788 Error_Msg_N
18789 ("operator of the type is not directly visible!", Expr);
18790
18791 elsif Ekind (Found_Type) = E_Void
18792 and then Present (Parent (Found_Type))
18793 and then Nkind (Parent (Found_Type)) = N_Full_Type_Declaration
18794 then
18795 Error_Msg_NE ("\\found premature usage of}!", Expr, Found_Type);
18796
18797 else
18798 Error_Msg_NE ("\\found}!", Expr, Found_Type);
18799 end if;
18800
18801 -- A special check for cases like M1 and M2 = 0 where M1 and M2 are
18802 -- of the same modular type, and (M1 and M2) = 0 was intended.
18803
18804 if Expec_Type = Standard_Boolean
18805 and then Is_Modular_Integer_Type (Found_Type)
18806 and then Nkind_In (Parent (Expr), N_Op_And, N_Op_Or, N_Op_Xor)
18807 and then Nkind (Right_Opnd (Parent (Expr))) in N_Op_Compare
18808 then
18809 declare
18810 Op : constant Node_Id := Right_Opnd (Parent (Expr));
18811 L : constant Node_Id := Left_Opnd (Op);
18812 R : constant Node_Id := Right_Opnd (Op);
18813
18814 begin
18815 -- The case for the message is when the left operand of the
18816 -- comparison is the same modular type, or when it is an
18817 -- integer literal (or other universal integer expression),
18818 -- which would have been typed as the modular type if the
18819 -- parens had been there.
18820
18821 if (Etype (L) = Found_Type
18822 or else
18823 Etype (L) = Universal_Integer)
18824 and then Is_Integer_Type (Etype (R))
18825 then
18826 Error_Msg_N
18827 ("\\possible missing parens for modular operation", Expr);
18828 end if;
18829 end;
18830 end if;
18831
18832 -- Reset error message qualification indication
18833
18834 Error_Msg_Qual_Level := 0;
18835 end if;
18836 end Wrong_Type;
18837
18838 end Sem_Util;