[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-2014, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
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_Ch8; use Sem_Ch8;
51 with Sem_Disp; use Sem_Disp;
52 with Sem_Eval; use Sem_Eval;
53 with Sem_Prag; use Sem_Prag;
54 with Sem_Res; use Sem_Res;
55 with Sem_Type; use Sem_Type;
56 with Sinfo; use Sinfo;
57 with Sinput; use Sinput;
58 with Stand; use Stand;
59 with Style;
60 with Stringt; use Stringt;
61 with Targparm; use Targparm;
62 with Tbuild; use Tbuild;
63 with Ttypes; use Ttypes;
64 with Uname; use Uname;
65
66 with GNAT.HTable; use GNAT.HTable;
67
68 package body Sem_Util is
69
70 ----------------------------------------
71 -- Global_Variables for New_Copy_Tree --
72 ----------------------------------------
73
74 -- These global variables are used by New_Copy_Tree. See description
75 -- of the body of this subprogram for details. Global variables can be
76 -- safely used by New_Copy_Tree, since there is no case of a recursive
77 -- call from the processing inside New_Copy_Tree.
78
79 NCT_Hash_Threshold : constant := 20;
80 -- If there are more than this number of pairs of entries in the
81 -- map, then Hash_Tables_Used will be set, and the hash tables will
82 -- be initialized and used for the searches.
83
84 NCT_Hash_Tables_Used : Boolean := False;
85 -- Set to True if hash tables are in use
86
87 NCT_Table_Entries : Nat := 0;
88 -- Count entries in table to see if threshold is reached
89
90 NCT_Hash_Table_Setup : Boolean := False;
91 -- Set to True if hash table contains data. We set this True if we
92 -- setup the hash table with data, and leave it set permanently
93 -- from then on, this is a signal that second and subsequent users
94 -- of the hash table must clear the old entries before reuse.
95
96 subtype NCT_Header_Num is Int range 0 .. 511;
97 -- Defines range of headers in hash tables (512 headers)
98
99 -----------------------
100 -- Local Subprograms --
101 -----------------------
102
103 function Build_Component_Subtype
104 (C : List_Id;
105 Loc : Source_Ptr;
106 T : Entity_Id) return Node_Id;
107 -- This function builds the subtype for Build_Actual_Subtype_Of_Component
108 -- and Build_Discriminal_Subtype_Of_Component. C is a list of constraints,
109 -- Loc is the source location, T is the original subtype.
110
111 function Is_Fully_Initialized_Variant (Typ : Entity_Id) return Boolean;
112 -- Subsidiary to Is_Fully_Initialized_Type. For an unconstrained type
113 -- with discriminants whose default values are static, examine only the
114 -- components in the selected variant to determine whether all of them
115 -- have a default.
116
117 function Has_Enabled_Property
118 (Item_Id : Entity_Id;
119 Property : Name_Id) return Boolean;
120 -- Subsidiary to routines Async_xxx_Enabled and Effective_xxx_Enabled.
121 -- Determine whether an abstract state or a variable denoted by entity
122 -- Item_Id has enabled property Property.
123
124 function Has_Null_Extension (T : Entity_Id) return Boolean;
125 -- T is a derived tagged type. Check whether the type extension is null.
126 -- If the parent type is fully initialized, T can be treated as such.
127
128 ------------------------------
129 -- Abstract_Interface_List --
130 ------------------------------
131
132 function Abstract_Interface_List (Typ : Entity_Id) return List_Id is
133 Nod : Node_Id;
134
135 begin
136 if Is_Concurrent_Type (Typ) then
137
138 -- If we are dealing with a synchronized subtype, go to the base
139 -- type, whose declaration has the interface list.
140
141 -- Shouldn't this be Declaration_Node???
142
143 Nod := Parent (Base_Type (Typ));
144
145 if Nkind (Nod) = N_Full_Type_Declaration then
146 return Empty_List;
147 end if;
148
149 elsif Ekind (Typ) = E_Record_Type_With_Private then
150 if Nkind (Parent (Typ)) = N_Full_Type_Declaration then
151 Nod := Type_Definition (Parent (Typ));
152
153 elsif Nkind (Parent (Typ)) = N_Private_Type_Declaration then
154 if Present (Full_View (Typ))
155 and then Nkind (Parent (Full_View (Typ)))
156 = N_Full_Type_Declaration
157 then
158 Nod := Type_Definition (Parent (Full_View (Typ)));
159
160 -- If the full-view is not available we cannot do anything else
161 -- here (the source has errors).
162
163 else
164 return Empty_List;
165 end if;
166
167 -- Support for generic formals with interfaces is still missing ???
168
169 elsif Nkind (Parent (Typ)) = N_Formal_Type_Declaration then
170 return Empty_List;
171
172 else
173 pragma Assert
174 (Nkind (Parent (Typ)) = N_Private_Extension_Declaration);
175 Nod := Parent (Typ);
176 end if;
177
178 elsif Ekind (Typ) = E_Record_Subtype then
179 Nod := Type_Definition (Parent (Etype (Typ)));
180
181 elsif Ekind (Typ) = E_Record_Subtype_With_Private then
182
183 -- Recurse, because parent may still be a private extension. Also
184 -- note that the full view of the subtype or the full view of its
185 -- base type may (both) be unavailable.
186
187 return Abstract_Interface_List (Etype (Typ));
188
189 else pragma Assert ((Ekind (Typ)) = E_Record_Type);
190 if Nkind (Parent (Typ)) = N_Formal_Type_Declaration then
191 Nod := Formal_Type_Definition (Parent (Typ));
192 else
193 Nod := Type_Definition (Parent (Typ));
194 end if;
195 end if;
196
197 return Interface_List (Nod);
198 end Abstract_Interface_List;
199
200 --------------------------------
201 -- Add_Access_Type_To_Process --
202 --------------------------------
203
204 procedure Add_Access_Type_To_Process (E : Entity_Id; A : Entity_Id) is
205 L : Elist_Id;
206
207 begin
208 Ensure_Freeze_Node (E);
209 L := Access_Types_To_Process (Freeze_Node (E));
210
211 if No (L) then
212 L := New_Elmt_List;
213 Set_Access_Types_To_Process (Freeze_Node (E), L);
214 end if;
215
216 Append_Elmt (A, L);
217 end Add_Access_Type_To_Process;
218
219 --------------------------
220 -- Add_Block_Identifier --
221 --------------------------
222
223 procedure Add_Block_Identifier (N : Node_Id; Id : out Entity_Id) is
224 Loc : constant Source_Ptr := Sloc (N);
225
226 begin
227 pragma Assert (Nkind (N) = N_Block_Statement);
228
229 -- The block already has a label, return its entity
230
231 if Present (Identifier (N)) then
232 Id := Entity (Identifier (N));
233
234 -- Create a new block label and set its attributes
235
236 else
237 Id := New_Internal_Entity (E_Block, Current_Scope, Loc, 'B');
238 Set_Etype (Id, Standard_Void_Type);
239 Set_Parent (Id, N);
240
241 Set_Identifier (N, New_Occurrence_Of (Id, Loc));
242 Set_Block_Node (Id, Identifier (N));
243 end if;
244 end Add_Block_Identifier;
245
246 -----------------------
247 -- Add_Contract_Item --
248 -----------------------
249
250 procedure Add_Contract_Item (Prag : Node_Id; Id : Entity_Id) is
251 Items : constant Node_Id := Contract (Id);
252 Nam : Name_Id;
253 N : Node_Id;
254
255 begin
256 -- The related context must have a contract and the item to be added
257 -- must be a pragma.
258
259 pragma Assert (Present (Items));
260 pragma Assert (Nkind (Prag) = N_Pragma);
261
262 Nam := Original_Aspect_Name (Prag);
263
264 -- Contract items related to [generic] packages or instantiations. The
265 -- applicable pragmas are:
266 -- Abstract_States
267 -- Initial_Condition
268 -- Initializes
269 -- Part_Of (instantiation only)
270
271 if Ekind_In (Id, E_Generic_Package, E_Package) then
272 if Nam_In (Nam, Name_Abstract_State,
273 Name_Initial_Condition,
274 Name_Initializes)
275 then
276 Set_Next_Pragma (Prag, Classifications (Items));
277 Set_Classifications (Items, Prag);
278
279 -- Indicator Part_Of must be associated with a package instantiation
280
281 elsif Nam = Name_Part_Of and then Is_Generic_Instance (Id) then
282 Set_Next_Pragma (Prag, Classifications (Items));
283 Set_Classifications (Items, Prag);
284
285 -- The pragma is not a proper contract item
286
287 else
288 raise Program_Error;
289 end if;
290
291 -- Contract items related to package bodies. The applicable pragmas are:
292 -- Refined_States
293
294 elsif Ekind (Id) = E_Package_Body then
295 if Nam = Name_Refined_State then
296 Set_Next_Pragma (Prag, Classifications (Items));
297 Set_Classifications (Items, Prag);
298
299 -- The pragma is not a proper contract item
300
301 else
302 raise Program_Error;
303 end if;
304
305 -- Contract items related to subprogram or entry declarations. The
306 -- applicable pragmas are:
307 -- Contract_Cases
308 -- Depends
309 -- Global
310 -- Post
311 -- Postcondition
312 -- Pre
313 -- Precondition
314 -- Test_Case
315
316 elsif Ekind_In (Id, E_Entry, E_Entry_Family)
317 or else Is_Generic_Subprogram (Id)
318 or else Is_Subprogram (Id)
319 then
320 if Nam_In (Nam, Name_Precondition,
321 Name_Postcondition,
322 Name_Pre,
323 Name_Post,
324 Name_uPre,
325 Name_uPost)
326 then
327 -- Before we add a precondition or postcondition to the list,
328 -- make sure we do not have a disallowed duplicate, which can
329 -- happen if we use a pragma for Pre[_Class] or Post[_Class]
330 -- instead of the corresponding aspect.
331
332 if not From_Aspect_Specification (Prag)
333 and then Nam_In (Nam, Name_Pre_Class,
334 Name_Pre,
335 Name_uPre,
336 Name_Post_Class,
337 Name_Post,
338 Name_uPost)
339 then
340 N := Pre_Post_Conditions (Items);
341 while Present (N) loop
342 if not Split_PPC (N)
343 and then Original_Aspect_Name (N) = Nam
344 then
345 Error_Msg_Sloc := Sloc (N);
346 Error_Msg_NE
347 ("duplication of aspect for & given#", Prag, Id);
348 return;
349 else
350 N := Next_Pragma (N);
351 end if;
352 end loop;
353 end if;
354
355 Set_Next_Pragma (Prag, Pre_Post_Conditions (Items));
356 Set_Pre_Post_Conditions (Items, Prag);
357
358 elsif Nam_In (Nam, Name_Contract_Cases, Name_Test_Case) then
359 Set_Next_Pragma (Prag, Contract_Test_Cases (Items));
360 Set_Contract_Test_Cases (Items, Prag);
361
362 elsif Nam_In (Nam, Name_Depends, Name_Global) then
363 Set_Next_Pragma (Prag, Classifications (Items));
364 Set_Classifications (Items, Prag);
365
366 -- The pragma is not a proper contract item
367
368 else
369 raise Program_Error;
370 end if;
371
372 -- Contract items related to subprogram bodies. The applicable pragmas
373 -- are:
374 -- Refined_Depends
375 -- Refined_Global
376 -- Refined_Post
377
378 elsif Ekind (Id) = E_Subprogram_Body then
379 if Nam = Name_Refined_Post then
380 Set_Next_Pragma (Prag, Pre_Post_Conditions (Items));
381 Set_Pre_Post_Conditions (Items, Prag);
382
383 elsif Nam_In (Nam, Name_Refined_Depends, Name_Refined_Global) then
384 Set_Next_Pragma (Prag, Classifications (Items));
385 Set_Classifications (Items, Prag);
386
387 -- The pragma is not a proper contract item
388
389 else
390 raise Program_Error;
391 end if;
392
393 -- Contract items related to variables. The applicable pragmas are:
394 -- Async_Readers
395 -- Async_Writers
396 -- Effective_Reads
397 -- Effective_Writes
398 -- Part_Of
399
400 elsif Ekind (Id) = E_Variable then
401 if Nam_In (Nam, Name_Async_Readers,
402 Name_Async_Writers,
403 Name_Effective_Reads,
404 Name_Effective_Writes,
405 Name_Part_Of)
406 then
407 Set_Next_Pragma (Prag, Classifications (Items));
408 Set_Classifications (Items, Prag);
409
410 -- The pragma is not a proper contract item
411
412 else
413 raise Program_Error;
414 end if;
415 end if;
416 end Add_Contract_Item;
417
418 ----------------------------
419 -- Add_Global_Declaration --
420 ----------------------------
421
422 procedure Add_Global_Declaration (N : Node_Id) is
423 Aux_Node : constant Node_Id := Aux_Decls_Node (Cunit (Current_Sem_Unit));
424
425 begin
426 if No (Declarations (Aux_Node)) then
427 Set_Declarations (Aux_Node, New_List);
428 end if;
429
430 Append_To (Declarations (Aux_Node), N);
431 Analyze (N);
432 end Add_Global_Declaration;
433
434 --------------------------------
435 -- Address_Integer_Convert_OK --
436 --------------------------------
437
438 function Address_Integer_Convert_OK (T1, T2 : Entity_Id) return Boolean is
439 begin
440 if Allow_Integer_Address
441 and then ((Is_Descendent_Of_Address (T1)
442 and then Is_Private_Type (T1)
443 and then Is_Integer_Type (T2))
444 or else
445 (Is_Descendent_Of_Address (T2)
446 and then Is_Private_Type (T2)
447 and then Is_Integer_Type (T1)))
448 then
449 return True;
450 else
451 return False;
452 end if;
453 end Address_Integer_Convert_OK;
454
455 -----------------
456 -- Addressable --
457 -----------------
458
459 -- For now, just 8/16/32/64. but analyze later if AAMP is special???
460
461 function Addressable (V : Uint) return Boolean is
462 begin
463 return V = Uint_8 or else
464 V = Uint_16 or else
465 V = Uint_32 or else
466 V = Uint_64;
467 end Addressable;
468
469 function Addressable (V : Int) return Boolean is
470 begin
471 return V = 8 or else
472 V = 16 or else
473 V = 32 or else
474 V = 64;
475 end Addressable;
476
477 -----------------------
478 -- Alignment_In_Bits --
479 -----------------------
480
481 function Alignment_In_Bits (E : Entity_Id) return Uint is
482 begin
483 return Alignment (E) * System_Storage_Unit;
484 end Alignment_In_Bits;
485
486 ---------------------------------
487 -- Append_Inherited_Subprogram --
488 ---------------------------------
489
490 procedure Append_Inherited_Subprogram (S : Entity_Id) is
491 Par : constant Entity_Id := Alias (S);
492 -- The parent subprogram
493
494 Scop : constant Entity_Id := Scope (Par);
495 -- The scope of definition of the parent subprogram
496
497 Typ : constant Entity_Id := Defining_Entity (Parent (S));
498 -- The derived type of which S is a primitive operation
499
500 Decl : Node_Id;
501 Next_E : Entity_Id;
502
503 begin
504 if Ekind (Current_Scope) = E_Package
505 and then In_Private_Part (Current_Scope)
506 and then Has_Private_Declaration (Typ)
507 and then Is_Tagged_Type (Typ)
508 and then Scop = Current_Scope
509 then
510 -- The inherited operation is available at the earliest place after
511 -- the derived type declaration ( RM 7.3.1 (6/1)). This is only
512 -- relevant for type extensions. If the parent operation appears
513 -- after the type extension, the operation is not visible.
514
515 Decl := First
516 (Visible_Declarations
517 (Package_Specification (Current_Scope)));
518 while Present (Decl) loop
519 if Nkind (Decl) = N_Private_Extension_Declaration
520 and then Defining_Entity (Decl) = Typ
521 then
522 if Sloc (Decl) > Sloc (Par) then
523 Next_E := Next_Entity (Par);
524 Set_Next_Entity (Par, S);
525 Set_Next_Entity (S, Next_E);
526 return;
527
528 else
529 exit;
530 end if;
531 end if;
532
533 Next (Decl);
534 end loop;
535 end if;
536
537 -- If partial view is not a type extension, or it appears before the
538 -- subprogram declaration, insert normally at end of entity list.
539
540 Append_Entity (S, Current_Scope);
541 end Append_Inherited_Subprogram;
542
543 -----------------------------------------
544 -- Apply_Compile_Time_Constraint_Error --
545 -----------------------------------------
546
547 procedure Apply_Compile_Time_Constraint_Error
548 (N : Node_Id;
549 Msg : String;
550 Reason : RT_Exception_Code;
551 Ent : Entity_Id := Empty;
552 Typ : Entity_Id := Empty;
553 Loc : Source_Ptr := No_Location;
554 Rep : Boolean := True;
555 Warn : Boolean := False)
556 is
557 Stat : constant Boolean := Is_Static_Expression (N);
558 R_Stat : constant Node_Id :=
559 Make_Raise_Constraint_Error (Sloc (N), Reason => Reason);
560 Rtyp : Entity_Id;
561
562 begin
563 if No (Typ) then
564 Rtyp := Etype (N);
565 else
566 Rtyp := Typ;
567 end if;
568
569 Discard_Node
570 (Compile_Time_Constraint_Error (N, Msg, Ent, Loc, Warn => Warn));
571
572 if not Rep then
573 return;
574 end if;
575
576 -- Now we replace the node by an N_Raise_Constraint_Error node
577 -- This does not need reanalyzing, so set it as analyzed now.
578
579 Rewrite (N, R_Stat);
580 Set_Analyzed (N, True);
581
582 Set_Etype (N, Rtyp);
583 Set_Raises_Constraint_Error (N);
584
585 -- Now deal with possible local raise handling
586
587 Possible_Local_Raise (N, Standard_Constraint_Error);
588
589 -- If the original expression was marked as static, the result is
590 -- still marked as static, but the Raises_Constraint_Error flag is
591 -- always set so that further static evaluation is not attempted.
592
593 if Stat then
594 Set_Is_Static_Expression (N);
595 end if;
596 end Apply_Compile_Time_Constraint_Error;
597
598 ---------------------------
599 -- Async_Readers_Enabled --
600 ---------------------------
601
602 function Async_Readers_Enabled (Id : Entity_Id) return Boolean is
603 begin
604 return Has_Enabled_Property (Id, Name_Async_Readers);
605 end Async_Readers_Enabled;
606
607 ---------------------------
608 -- Async_Writers_Enabled --
609 ---------------------------
610
611 function Async_Writers_Enabled (Id : Entity_Id) return Boolean is
612 begin
613 return Has_Enabled_Property (Id, Name_Async_Writers);
614 end Async_Writers_Enabled;
615
616 --------------------------------------
617 -- Available_Full_View_Of_Component --
618 --------------------------------------
619
620 function Available_Full_View_Of_Component (T : Entity_Id) return Boolean is
621 ST : constant Entity_Id := Scope (T);
622 SCT : constant Entity_Id := Scope (Component_Type (T));
623 begin
624 return In_Open_Scopes (ST)
625 and then In_Open_Scopes (SCT)
626 and then Scope_Depth (ST) >= Scope_Depth (SCT);
627 end Available_Full_View_Of_Component;
628
629 -------------------
630 -- Bad_Attribute --
631 -------------------
632
633 procedure Bad_Attribute
634 (N : Node_Id;
635 Nam : Name_Id;
636 Warn : Boolean := False)
637 is
638 begin
639 Error_Msg_Warn := Warn;
640 Error_Msg_N ("unrecognized attribute&<<", N);
641
642 -- Check for possible misspelling
643
644 Error_Msg_Name_1 := First_Attribute_Name;
645 while Error_Msg_Name_1 <= Last_Attribute_Name loop
646 if Is_Bad_Spelling_Of (Nam, Error_Msg_Name_1) then
647 Error_Msg_N -- CODEFIX
648 ("\possible misspelling of %<<", N);
649 exit;
650 end if;
651
652 Error_Msg_Name_1 := Error_Msg_Name_1 + 1;
653 end loop;
654 end Bad_Attribute;
655
656 --------------------------------
657 -- Bad_Predicated_Subtype_Use --
658 --------------------------------
659
660 procedure Bad_Predicated_Subtype_Use
661 (Msg : String;
662 N : Node_Id;
663 Typ : Entity_Id;
664 Suggest_Static : Boolean := False)
665 is
666 begin
667 if Has_Predicates (Typ) then
668 if Is_Generic_Actual_Type (Typ) then
669 Error_Msg_Warn := SPARK_Mode /= On;
670 Error_Msg_FE (Msg & "<<", N, Typ);
671 Error_Msg_F ("\Program_Error [<<", N);
672 Insert_Action (N,
673 Make_Raise_Program_Error (Sloc (N),
674 Reason => PE_Bad_Predicated_Generic_Type));
675
676 else
677 Error_Msg_FE (Msg, N, Typ);
678 end if;
679
680 -- Emit an optional suggestion on how to remedy the error if the
681 -- context warrants it.
682
683 if Suggest_Static and then Present (Static_Predicate (Typ)) then
684 Error_Msg_FE ("\predicate of & should be marked static", N, Typ);
685 end if;
686 end if;
687 end Bad_Predicated_Subtype_Use;
688
689 -----------------------------------------
690 -- Bad_Unordered_Enumeration_Reference --
691 -----------------------------------------
692
693 function Bad_Unordered_Enumeration_Reference
694 (N : Node_Id;
695 T : Entity_Id) return Boolean
696 is
697 begin
698 return Is_Enumeration_Type (T)
699 and then Comes_From_Source (N)
700 and then Warn_On_Unordered_Enumeration_Type
701 and then not Has_Pragma_Ordered (T)
702 and then not In_Same_Extended_Unit (N, T);
703 end Bad_Unordered_Enumeration_Reference;
704
705 --------------------------
706 -- Build_Actual_Subtype --
707 --------------------------
708
709 function Build_Actual_Subtype
710 (T : Entity_Id;
711 N : Node_Or_Entity_Id) return Node_Id
712 is
713 Loc : Source_Ptr;
714 -- Normally Sloc (N), but may point to corresponding body in some cases
715
716 Constraints : List_Id;
717 Decl : Node_Id;
718 Discr : Entity_Id;
719 Hi : Node_Id;
720 Lo : Node_Id;
721 Subt : Entity_Id;
722 Disc_Type : Entity_Id;
723 Obj : Node_Id;
724
725 begin
726 Loc := Sloc (N);
727
728 if Nkind (N) = N_Defining_Identifier then
729 Obj := New_Occurrence_Of (N, Loc);
730
731 -- If this is a formal parameter of a subprogram declaration, and
732 -- we are compiling the body, we want the declaration for the
733 -- actual subtype to carry the source position of the body, to
734 -- prevent anomalies in gdb when stepping through the code.
735
736 if Is_Formal (N) then
737 declare
738 Decl : constant Node_Id := Unit_Declaration_Node (Scope (N));
739 begin
740 if Nkind (Decl) = N_Subprogram_Declaration
741 and then Present (Corresponding_Body (Decl))
742 then
743 Loc := Sloc (Corresponding_Body (Decl));
744 end if;
745 end;
746 end if;
747
748 else
749 Obj := N;
750 end if;
751
752 if Is_Array_Type (T) then
753 Constraints := New_List;
754 for J in 1 .. Number_Dimensions (T) loop
755
756 -- Build an array subtype declaration with the nominal subtype and
757 -- the bounds of the actual. Add the declaration in front of the
758 -- local declarations for the subprogram, for analysis before any
759 -- reference to the formal in the body.
760
761 Lo :=
762 Make_Attribute_Reference (Loc,
763 Prefix =>
764 Duplicate_Subexpr_No_Checks (Obj, Name_Req => True),
765 Attribute_Name => Name_First,
766 Expressions => New_List (
767 Make_Integer_Literal (Loc, J)));
768
769 Hi :=
770 Make_Attribute_Reference (Loc,
771 Prefix =>
772 Duplicate_Subexpr_No_Checks (Obj, Name_Req => True),
773 Attribute_Name => Name_Last,
774 Expressions => New_List (
775 Make_Integer_Literal (Loc, J)));
776
777 Append (Make_Range (Loc, Lo, Hi), Constraints);
778 end loop;
779
780 -- If the type has unknown discriminants there is no constrained
781 -- subtype to build. This is never called for a formal or for a
782 -- lhs, so returning the type is ok ???
783
784 elsif Has_Unknown_Discriminants (T) then
785 return T;
786
787 else
788 Constraints := New_List;
789
790 -- Type T is a generic derived type, inherit the discriminants from
791 -- the parent type.
792
793 if Is_Private_Type (T)
794 and then No (Full_View (T))
795
796 -- T was flagged as an error if it was declared as a formal
797 -- derived type with known discriminants. In this case there
798 -- is no need to look at the parent type since T already carries
799 -- its own discriminants.
800
801 and then not Error_Posted (T)
802 then
803 Disc_Type := Etype (Base_Type (T));
804 else
805 Disc_Type := T;
806 end if;
807
808 Discr := First_Discriminant (Disc_Type);
809 while Present (Discr) loop
810 Append_To (Constraints,
811 Make_Selected_Component (Loc,
812 Prefix =>
813 Duplicate_Subexpr_No_Checks (Obj),
814 Selector_Name => New_Occurrence_Of (Discr, Loc)));
815 Next_Discriminant (Discr);
816 end loop;
817 end if;
818
819 Subt := Make_Temporary (Loc, 'S', Related_Node => N);
820 Set_Is_Internal (Subt);
821
822 Decl :=
823 Make_Subtype_Declaration (Loc,
824 Defining_Identifier => Subt,
825 Subtype_Indication =>
826 Make_Subtype_Indication (Loc,
827 Subtype_Mark => New_Occurrence_Of (T, Loc),
828 Constraint =>
829 Make_Index_Or_Discriminant_Constraint (Loc,
830 Constraints => Constraints)));
831
832 Mark_Rewrite_Insertion (Decl);
833 return Decl;
834 end Build_Actual_Subtype;
835
836 ---------------------------------------
837 -- Build_Actual_Subtype_Of_Component --
838 ---------------------------------------
839
840 function Build_Actual_Subtype_Of_Component
841 (T : Entity_Id;
842 N : Node_Id) return Node_Id
843 is
844 Loc : constant Source_Ptr := Sloc (N);
845 P : constant Node_Id := Prefix (N);
846 D : Elmt_Id;
847 Id : Node_Id;
848 Index_Typ : Entity_Id;
849
850 Desig_Typ : Entity_Id;
851 -- This is either a copy of T, or if T is an access type, then it is
852 -- the directly designated type of this access type.
853
854 function Build_Actual_Array_Constraint return List_Id;
855 -- If one or more of the bounds of the component depends on
856 -- discriminants, build actual constraint using the discriminants
857 -- of the prefix.
858
859 function Build_Actual_Record_Constraint return List_Id;
860 -- Similar to previous one, for discriminated components constrained
861 -- by the discriminant of the enclosing object.
862
863 -----------------------------------
864 -- Build_Actual_Array_Constraint --
865 -----------------------------------
866
867 function Build_Actual_Array_Constraint return List_Id is
868 Constraints : constant List_Id := New_List;
869 Indx : Node_Id;
870 Hi : Node_Id;
871 Lo : Node_Id;
872 Old_Hi : Node_Id;
873 Old_Lo : Node_Id;
874
875 begin
876 Indx := First_Index (Desig_Typ);
877 while Present (Indx) loop
878 Old_Lo := Type_Low_Bound (Etype (Indx));
879 Old_Hi := Type_High_Bound (Etype (Indx));
880
881 if Denotes_Discriminant (Old_Lo) then
882 Lo :=
883 Make_Selected_Component (Loc,
884 Prefix => New_Copy_Tree (P),
885 Selector_Name => New_Occurrence_Of (Entity (Old_Lo), Loc));
886
887 else
888 Lo := New_Copy_Tree (Old_Lo);
889
890 -- The new bound will be reanalyzed in the enclosing
891 -- declaration. For literal bounds that come from a type
892 -- declaration, the type of the context must be imposed, so
893 -- insure that analysis will take place. For non-universal
894 -- types this is not strictly necessary.
895
896 Set_Analyzed (Lo, False);
897 end if;
898
899 if Denotes_Discriminant (Old_Hi) then
900 Hi :=
901 Make_Selected_Component (Loc,
902 Prefix => New_Copy_Tree (P),
903 Selector_Name => New_Occurrence_Of (Entity (Old_Hi), Loc));
904
905 else
906 Hi := New_Copy_Tree (Old_Hi);
907 Set_Analyzed (Hi, False);
908 end if;
909
910 Append (Make_Range (Loc, Lo, Hi), Constraints);
911 Next_Index (Indx);
912 end loop;
913
914 return Constraints;
915 end Build_Actual_Array_Constraint;
916
917 ------------------------------------
918 -- Build_Actual_Record_Constraint --
919 ------------------------------------
920
921 function Build_Actual_Record_Constraint return List_Id is
922 Constraints : constant List_Id := New_List;
923 D : Elmt_Id;
924 D_Val : Node_Id;
925
926 begin
927 D := First_Elmt (Discriminant_Constraint (Desig_Typ));
928 while Present (D) loop
929 if Denotes_Discriminant (Node (D)) then
930 D_Val := Make_Selected_Component (Loc,
931 Prefix => New_Copy_Tree (P),
932 Selector_Name => New_Occurrence_Of (Entity (Node (D)), Loc));
933
934 else
935 D_Val := New_Copy_Tree (Node (D));
936 end if;
937
938 Append (D_Val, Constraints);
939 Next_Elmt (D);
940 end loop;
941
942 return Constraints;
943 end Build_Actual_Record_Constraint;
944
945 -- Start of processing for Build_Actual_Subtype_Of_Component
946
947 begin
948 -- Why the test for Spec_Expression mode here???
949
950 if In_Spec_Expression then
951 return Empty;
952
953 -- More comments for the rest of this body would be good ???
954
955 elsif Nkind (N) = N_Explicit_Dereference then
956 if Is_Composite_Type (T)
957 and then not Is_Constrained (T)
958 and then not (Is_Class_Wide_Type (T)
959 and then Is_Constrained (Root_Type (T)))
960 and then not Has_Unknown_Discriminants (T)
961 then
962 -- If the type of the dereference is already constrained, it is an
963 -- actual subtype.
964
965 if Is_Array_Type (Etype (N))
966 and then Is_Constrained (Etype (N))
967 then
968 return Empty;
969 else
970 Remove_Side_Effects (P);
971 return Build_Actual_Subtype (T, N);
972 end if;
973 else
974 return Empty;
975 end if;
976 end if;
977
978 if Ekind (T) = E_Access_Subtype then
979 Desig_Typ := Designated_Type (T);
980 else
981 Desig_Typ := T;
982 end if;
983
984 if Ekind (Desig_Typ) = E_Array_Subtype then
985 Id := First_Index (Desig_Typ);
986 while Present (Id) loop
987 Index_Typ := Underlying_Type (Etype (Id));
988
989 if Denotes_Discriminant (Type_Low_Bound (Index_Typ))
990 or else
991 Denotes_Discriminant (Type_High_Bound (Index_Typ))
992 then
993 Remove_Side_Effects (P);
994 return
995 Build_Component_Subtype
996 (Build_Actual_Array_Constraint, Loc, Base_Type (T));
997 end if;
998
999 Next_Index (Id);
1000 end loop;
1001
1002 elsif Is_Composite_Type (Desig_Typ)
1003 and then Has_Discriminants (Desig_Typ)
1004 and then not Has_Unknown_Discriminants (Desig_Typ)
1005 then
1006 if Is_Private_Type (Desig_Typ)
1007 and then No (Discriminant_Constraint (Desig_Typ))
1008 then
1009 Desig_Typ := Full_View (Desig_Typ);
1010 end if;
1011
1012 D := First_Elmt (Discriminant_Constraint (Desig_Typ));
1013 while Present (D) loop
1014 if Denotes_Discriminant (Node (D)) then
1015 Remove_Side_Effects (P);
1016 return
1017 Build_Component_Subtype (
1018 Build_Actual_Record_Constraint, Loc, Base_Type (T));
1019 end if;
1020
1021 Next_Elmt (D);
1022 end loop;
1023 end if;
1024
1025 -- If none of the above, the actual and nominal subtypes are the same
1026
1027 return Empty;
1028 end Build_Actual_Subtype_Of_Component;
1029
1030 -----------------------------
1031 -- Build_Component_Subtype --
1032 -----------------------------
1033
1034 function Build_Component_Subtype
1035 (C : List_Id;
1036 Loc : Source_Ptr;
1037 T : Entity_Id) return Node_Id
1038 is
1039 Subt : Entity_Id;
1040 Decl : Node_Id;
1041
1042 begin
1043 -- Unchecked_Union components do not require component subtypes
1044
1045 if Is_Unchecked_Union (T) then
1046 return Empty;
1047 end if;
1048
1049 Subt := Make_Temporary (Loc, 'S');
1050 Set_Is_Internal (Subt);
1051
1052 Decl :=
1053 Make_Subtype_Declaration (Loc,
1054 Defining_Identifier => Subt,
1055 Subtype_Indication =>
1056 Make_Subtype_Indication (Loc,
1057 Subtype_Mark => New_Occurrence_Of (Base_Type (T), Loc),
1058 Constraint =>
1059 Make_Index_Or_Discriminant_Constraint (Loc,
1060 Constraints => C)));
1061
1062 Mark_Rewrite_Insertion (Decl);
1063 return Decl;
1064 end Build_Component_Subtype;
1065
1066 ---------------------------
1067 -- Build_Default_Subtype --
1068 ---------------------------
1069
1070 function Build_Default_Subtype
1071 (T : Entity_Id;
1072 N : Node_Id) return Entity_Id
1073 is
1074 Loc : constant Source_Ptr := Sloc (N);
1075 Disc : Entity_Id;
1076
1077 Bas : Entity_Id;
1078 -- The base type that is to be constrained by the defaults
1079
1080 begin
1081 if not Has_Discriminants (T) or else Is_Constrained (T) then
1082 return T;
1083 end if;
1084
1085 Bas := Base_Type (T);
1086
1087 -- If T is non-private but its base type is private, this is the
1088 -- completion of a subtype declaration whose parent type is private
1089 -- (see Complete_Private_Subtype in Sem_Ch3). The proper discriminants
1090 -- are to be found in the full view of the base.
1091
1092 if Is_Private_Type (Bas) and then Present (Full_View (Bas)) then
1093 Bas := Full_View (Bas);
1094 end if;
1095
1096 Disc := First_Discriminant (T);
1097
1098 if No (Discriminant_Default_Value (Disc)) then
1099 return T;
1100 end if;
1101
1102 declare
1103 Act : constant Entity_Id := Make_Temporary (Loc, 'S');
1104 Constraints : constant List_Id := New_List;
1105 Decl : Node_Id;
1106
1107 begin
1108 while Present (Disc) loop
1109 Append_To (Constraints,
1110 New_Copy_Tree (Discriminant_Default_Value (Disc)));
1111 Next_Discriminant (Disc);
1112 end loop;
1113
1114 Decl :=
1115 Make_Subtype_Declaration (Loc,
1116 Defining_Identifier => Act,
1117 Subtype_Indication =>
1118 Make_Subtype_Indication (Loc,
1119 Subtype_Mark => New_Occurrence_Of (Bas, Loc),
1120 Constraint =>
1121 Make_Index_Or_Discriminant_Constraint (Loc,
1122 Constraints => Constraints)));
1123
1124 Insert_Action (N, Decl);
1125 Analyze (Decl);
1126 return Act;
1127 end;
1128 end Build_Default_Subtype;
1129
1130 --------------------------------------------
1131 -- Build_Discriminal_Subtype_Of_Component --
1132 --------------------------------------------
1133
1134 function Build_Discriminal_Subtype_Of_Component
1135 (T : Entity_Id) return Node_Id
1136 is
1137 Loc : constant Source_Ptr := Sloc (T);
1138 D : Elmt_Id;
1139 Id : Node_Id;
1140
1141 function Build_Discriminal_Array_Constraint return List_Id;
1142 -- If one or more of the bounds of the component depends on
1143 -- discriminants, build actual constraint using the discriminants
1144 -- of the prefix.
1145
1146 function Build_Discriminal_Record_Constraint return List_Id;
1147 -- Similar to previous one, for discriminated components constrained by
1148 -- the discriminant of the enclosing object.
1149
1150 ----------------------------------------
1151 -- Build_Discriminal_Array_Constraint --
1152 ----------------------------------------
1153
1154 function Build_Discriminal_Array_Constraint return List_Id is
1155 Constraints : constant List_Id := New_List;
1156 Indx : Node_Id;
1157 Hi : Node_Id;
1158 Lo : Node_Id;
1159 Old_Hi : Node_Id;
1160 Old_Lo : Node_Id;
1161
1162 begin
1163 Indx := First_Index (T);
1164 while Present (Indx) loop
1165 Old_Lo := Type_Low_Bound (Etype (Indx));
1166 Old_Hi := Type_High_Bound (Etype (Indx));
1167
1168 if Denotes_Discriminant (Old_Lo) then
1169 Lo := New_Occurrence_Of (Discriminal (Entity (Old_Lo)), Loc);
1170
1171 else
1172 Lo := New_Copy_Tree (Old_Lo);
1173 end if;
1174
1175 if Denotes_Discriminant (Old_Hi) then
1176 Hi := New_Occurrence_Of (Discriminal (Entity (Old_Hi)), Loc);
1177
1178 else
1179 Hi := New_Copy_Tree (Old_Hi);
1180 end if;
1181
1182 Append (Make_Range (Loc, Lo, Hi), Constraints);
1183 Next_Index (Indx);
1184 end loop;
1185
1186 return Constraints;
1187 end Build_Discriminal_Array_Constraint;
1188
1189 -----------------------------------------
1190 -- Build_Discriminal_Record_Constraint --
1191 -----------------------------------------
1192
1193 function Build_Discriminal_Record_Constraint return List_Id is
1194 Constraints : constant List_Id := New_List;
1195 D : Elmt_Id;
1196 D_Val : Node_Id;
1197
1198 begin
1199 D := First_Elmt (Discriminant_Constraint (T));
1200 while Present (D) loop
1201 if Denotes_Discriminant (Node (D)) then
1202 D_Val :=
1203 New_Occurrence_Of (Discriminal (Entity (Node (D))), Loc);
1204
1205 else
1206 D_Val := New_Copy_Tree (Node (D));
1207 end if;
1208
1209 Append (D_Val, Constraints);
1210 Next_Elmt (D);
1211 end loop;
1212
1213 return Constraints;
1214 end Build_Discriminal_Record_Constraint;
1215
1216 -- Start of processing for Build_Discriminal_Subtype_Of_Component
1217
1218 begin
1219 if Ekind (T) = E_Array_Subtype then
1220 Id := First_Index (T);
1221 while Present (Id) loop
1222 if Denotes_Discriminant (Type_Low_Bound (Etype (Id))) or else
1223 Denotes_Discriminant (Type_High_Bound (Etype (Id)))
1224 then
1225 return Build_Component_Subtype
1226 (Build_Discriminal_Array_Constraint, Loc, T);
1227 end if;
1228
1229 Next_Index (Id);
1230 end loop;
1231
1232 elsif Ekind (T) = E_Record_Subtype
1233 and then Has_Discriminants (T)
1234 and then not Has_Unknown_Discriminants (T)
1235 then
1236 D := First_Elmt (Discriminant_Constraint (T));
1237 while Present (D) loop
1238 if Denotes_Discriminant (Node (D)) then
1239 return Build_Component_Subtype
1240 (Build_Discriminal_Record_Constraint, Loc, T);
1241 end if;
1242
1243 Next_Elmt (D);
1244 end loop;
1245 end if;
1246
1247 -- If none of the above, the actual and nominal subtypes are the same
1248
1249 return Empty;
1250 end Build_Discriminal_Subtype_Of_Component;
1251
1252 ------------------------------
1253 -- Build_Elaboration_Entity --
1254 ------------------------------
1255
1256 procedure Build_Elaboration_Entity (N : Node_Id; Spec_Id : Entity_Id) is
1257 Loc : constant Source_Ptr := Sloc (N);
1258 Decl : Node_Id;
1259 Elab_Ent : Entity_Id;
1260
1261 procedure Set_Package_Name (Ent : Entity_Id);
1262 -- Given an entity, sets the fully qualified name of the entity in
1263 -- Name_Buffer, with components separated by double underscores. This
1264 -- is a recursive routine that climbs the scope chain to Standard.
1265
1266 ----------------------
1267 -- Set_Package_Name --
1268 ----------------------
1269
1270 procedure Set_Package_Name (Ent : Entity_Id) is
1271 begin
1272 if Scope (Ent) /= Standard_Standard then
1273 Set_Package_Name (Scope (Ent));
1274
1275 declare
1276 Nam : constant String := Get_Name_String (Chars (Ent));
1277 begin
1278 Name_Buffer (Name_Len + 1) := '_';
1279 Name_Buffer (Name_Len + 2) := '_';
1280 Name_Buffer (Name_Len + 3 .. Name_Len + Nam'Length + 2) := Nam;
1281 Name_Len := Name_Len + Nam'Length + 2;
1282 end;
1283
1284 else
1285 Get_Name_String (Chars (Ent));
1286 end if;
1287 end Set_Package_Name;
1288
1289 -- Start of processing for Build_Elaboration_Entity
1290
1291 begin
1292 -- Ignore if already constructed
1293
1294 if Present (Elaboration_Entity (Spec_Id)) then
1295 return;
1296 end if;
1297
1298 -- Ignore in ASIS mode, elaboration entity is not in source and plays
1299 -- no role in analysis.
1300
1301 if ASIS_Mode then
1302 return;
1303 end if;
1304
1305 -- Construct name of elaboration entity as xxx_E, where xxx is the unit
1306 -- name with dots replaced by double underscore. We have to manually
1307 -- construct this name, since it will be elaborated in the outer scope,
1308 -- and thus will not have the unit name automatically prepended.
1309
1310 Set_Package_Name (Spec_Id);
1311 Add_Str_To_Name_Buffer ("_E");
1312
1313 -- Create elaboration counter
1314
1315 Elab_Ent := Make_Defining_Identifier (Loc, Chars => Name_Find);
1316 Set_Elaboration_Entity (Spec_Id, Elab_Ent);
1317
1318 Decl :=
1319 Make_Object_Declaration (Loc,
1320 Defining_Identifier => Elab_Ent,
1321 Object_Definition =>
1322 New_Occurrence_Of (Standard_Short_Integer, Loc),
1323 Expression => Make_Integer_Literal (Loc, Uint_0));
1324
1325 Push_Scope (Standard_Standard);
1326 Add_Global_Declaration (Decl);
1327 Pop_Scope;
1328
1329 -- Reset True_Constant indication, since we will indeed assign a value
1330 -- to the variable in the binder main. We also kill the Current_Value
1331 -- and Last_Assignment fields for the same reason.
1332
1333 Set_Is_True_Constant (Elab_Ent, False);
1334 Set_Current_Value (Elab_Ent, Empty);
1335 Set_Last_Assignment (Elab_Ent, Empty);
1336
1337 -- We do not want any further qualification of the name (if we did not
1338 -- do this, we would pick up the name of the generic package in the case
1339 -- of a library level generic instantiation).
1340
1341 Set_Has_Qualified_Name (Elab_Ent);
1342 Set_Has_Fully_Qualified_Name (Elab_Ent);
1343 end Build_Elaboration_Entity;
1344
1345 --------------------------------
1346 -- Build_Explicit_Dereference --
1347 --------------------------------
1348
1349 procedure Build_Explicit_Dereference
1350 (Expr : Node_Id;
1351 Disc : Entity_Id)
1352 is
1353 Loc : constant Source_Ptr := Sloc (Expr);
1354 begin
1355
1356 -- An entity of a type with a reference aspect is overloaded with
1357 -- both interpretations: with and without the dereference. Now that
1358 -- the dereference is made explicit, set the type of the node properly,
1359 -- to prevent anomalies in the backend. Same if the expression is an
1360 -- overloaded function call whose return type has a reference aspect.
1361
1362 if Is_Entity_Name (Expr) then
1363 Set_Etype (Expr, Etype (Entity (Expr)));
1364
1365 elsif Nkind (Expr) = N_Function_Call then
1366 Set_Etype (Expr, Etype (Name (Expr)));
1367 end if;
1368
1369 Set_Is_Overloaded (Expr, False);
1370
1371 -- The expression will often be a generalized indexing that yields a
1372 -- container element that is then dereferenced, in which case the
1373 -- generalized indexing call is also non-overloaded.
1374
1375 if Nkind (Expr) = N_Indexed_Component
1376 and then Present (Generalized_Indexing (Expr))
1377 then
1378 Set_Is_Overloaded (Generalized_Indexing (Expr), False);
1379 end if;
1380
1381 Rewrite (Expr,
1382 Make_Explicit_Dereference (Loc,
1383 Prefix =>
1384 Make_Selected_Component (Loc,
1385 Prefix => Relocate_Node (Expr),
1386 Selector_Name => New_Occurrence_Of (Disc, Loc))));
1387 Set_Etype (Prefix (Expr), Etype (Disc));
1388 Set_Etype (Expr, Designated_Type (Etype (Disc)));
1389 end Build_Explicit_Dereference;
1390
1391 -----------------------------------
1392 -- Cannot_Raise_Constraint_Error --
1393 -----------------------------------
1394
1395 function Cannot_Raise_Constraint_Error (Expr : Node_Id) return Boolean is
1396 begin
1397 if Compile_Time_Known_Value (Expr) then
1398 return True;
1399
1400 elsif Do_Range_Check (Expr) then
1401 return False;
1402
1403 elsif Raises_Constraint_Error (Expr) then
1404 return False;
1405
1406 else
1407 case Nkind (Expr) is
1408 when N_Identifier =>
1409 return True;
1410
1411 when N_Expanded_Name =>
1412 return True;
1413
1414 when N_Selected_Component =>
1415 return not Do_Discriminant_Check (Expr);
1416
1417 when N_Attribute_Reference =>
1418 if Do_Overflow_Check (Expr) then
1419 return False;
1420
1421 elsif No (Expressions (Expr)) then
1422 return True;
1423
1424 else
1425 declare
1426 N : Node_Id;
1427
1428 begin
1429 N := First (Expressions (Expr));
1430 while Present (N) loop
1431 if Cannot_Raise_Constraint_Error (N) then
1432 Next (N);
1433 else
1434 return False;
1435 end if;
1436 end loop;
1437
1438 return True;
1439 end;
1440 end if;
1441
1442 when N_Type_Conversion =>
1443 if Do_Overflow_Check (Expr)
1444 or else Do_Length_Check (Expr)
1445 or else Do_Tag_Check (Expr)
1446 then
1447 return False;
1448 else
1449 return Cannot_Raise_Constraint_Error (Expression (Expr));
1450 end if;
1451
1452 when N_Unchecked_Type_Conversion =>
1453 return Cannot_Raise_Constraint_Error (Expression (Expr));
1454
1455 when N_Unary_Op =>
1456 if Do_Overflow_Check (Expr) then
1457 return False;
1458 else
1459 return Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
1460 end if;
1461
1462 when N_Op_Divide |
1463 N_Op_Mod |
1464 N_Op_Rem
1465 =>
1466 if Do_Division_Check (Expr)
1467 or else Do_Overflow_Check (Expr)
1468 then
1469 return False;
1470 else
1471 return
1472 Cannot_Raise_Constraint_Error (Left_Opnd (Expr))
1473 and then
1474 Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
1475 end if;
1476
1477 when N_Op_Add |
1478 N_Op_And |
1479 N_Op_Concat |
1480 N_Op_Eq |
1481 N_Op_Expon |
1482 N_Op_Ge |
1483 N_Op_Gt |
1484 N_Op_Le |
1485 N_Op_Lt |
1486 N_Op_Multiply |
1487 N_Op_Ne |
1488 N_Op_Or |
1489 N_Op_Rotate_Left |
1490 N_Op_Rotate_Right |
1491 N_Op_Shift_Left |
1492 N_Op_Shift_Right |
1493 N_Op_Shift_Right_Arithmetic |
1494 N_Op_Subtract |
1495 N_Op_Xor
1496 =>
1497 if Do_Overflow_Check (Expr) then
1498 return False;
1499 else
1500 return
1501 Cannot_Raise_Constraint_Error (Left_Opnd (Expr))
1502 and then
1503 Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
1504 end if;
1505
1506 when others =>
1507 return False;
1508 end case;
1509 end if;
1510 end Cannot_Raise_Constraint_Error;
1511
1512 -----------------------------------------
1513 -- Check_Dynamically_Tagged_Expression --
1514 -----------------------------------------
1515
1516 procedure Check_Dynamically_Tagged_Expression
1517 (Expr : Node_Id;
1518 Typ : Entity_Id;
1519 Related_Nod : Node_Id)
1520 is
1521 begin
1522 pragma Assert (Is_Tagged_Type (Typ));
1523
1524 -- In order to avoid spurious errors when analyzing the expanded code,
1525 -- this check is done only for nodes that come from source and for
1526 -- actuals of generic instantiations.
1527
1528 if (Comes_From_Source (Related_Nod)
1529 or else In_Generic_Actual (Expr))
1530 and then (Is_Class_Wide_Type (Etype (Expr))
1531 or else Is_Dynamically_Tagged (Expr))
1532 and then Is_Tagged_Type (Typ)
1533 and then not Is_Class_Wide_Type (Typ)
1534 then
1535 Error_Msg_N ("dynamically tagged expression not allowed!", Expr);
1536 end if;
1537 end Check_Dynamically_Tagged_Expression;
1538
1539 -----------------------------------------------
1540 -- Check_Expression_Against_Static_Predicate --
1541 -----------------------------------------------
1542
1543 procedure Check_Expression_Against_Static_Predicate
1544 (Expr : Node_Id;
1545 Typ : Entity_Id)
1546 is
1547 begin
1548 -- When the predicate is static and the value of the expression is known
1549 -- at compile time, evaluate the predicate check. A type is non-static
1550 -- when it has aspect Dynamic_Predicate.
1551
1552 if Compile_Time_Known_Value (Expr)
1553 and then Has_Predicates (Typ)
1554 and then Present (Static_Predicate (Typ))
1555 and then not Has_Dynamic_Predicate_Aspect (Typ)
1556 then
1557 -- Either -gnatc is enabled or the expression is ok
1558
1559 if Operating_Mode < Generate_Code
1560 or else Eval_Static_Predicate_Check (Expr, Typ)
1561 then
1562 null;
1563
1564 -- The expression is prohibited by the static predicate
1565
1566 else
1567 Error_Msg_NE
1568 ("??static expression fails static predicate check on &",
1569 Expr, Typ);
1570 end if;
1571 end if;
1572 end Check_Expression_Against_Static_Predicate;
1573
1574 --------------------------
1575 -- Check_Fully_Declared --
1576 --------------------------
1577
1578 procedure Check_Fully_Declared (T : Entity_Id; N : Node_Id) is
1579 begin
1580 if Ekind (T) = E_Incomplete_Type then
1581
1582 -- Ada 2005 (AI-50217): If the type is available through a limited
1583 -- with_clause, verify that its full view has been analyzed.
1584
1585 if From_Limited_With (T)
1586 and then Present (Non_Limited_View (T))
1587 and then Ekind (Non_Limited_View (T)) /= E_Incomplete_Type
1588 then
1589 -- The non-limited view is fully declared
1590 null;
1591
1592 else
1593 Error_Msg_NE
1594 ("premature usage of incomplete}", N, First_Subtype (T));
1595 end if;
1596
1597 -- Need comments for these tests ???
1598
1599 elsif Has_Private_Component (T)
1600 and then not Is_Generic_Type (Root_Type (T))
1601 and then not In_Spec_Expression
1602 then
1603 -- Special case: if T is the anonymous type created for a single
1604 -- task or protected object, use the name of the source object.
1605
1606 if Is_Concurrent_Type (T)
1607 and then not Comes_From_Source (T)
1608 and then Nkind (N) = N_Object_Declaration
1609 then
1610 Error_Msg_NE ("type of& has incomplete component", N,
1611 Defining_Identifier (N));
1612
1613 else
1614 Error_Msg_NE
1615 ("premature usage of incomplete}", N, First_Subtype (T));
1616 end if;
1617 end if;
1618 end Check_Fully_Declared;
1619
1620 -------------------------------------
1621 -- Check_Function_Writable_Actuals --
1622 -------------------------------------
1623
1624 procedure Check_Function_Writable_Actuals (N : Node_Id) is
1625 Writable_Actuals_List : Elist_Id := No_Elist;
1626 Identifiers_List : Elist_Id := No_Elist;
1627 Error_Node : Node_Id := Empty;
1628
1629 procedure Collect_Identifiers (N : Node_Id);
1630 -- In a single traversal of subtree N collect in Writable_Actuals_List
1631 -- all the actuals of functions with writable actuals, and in the list
1632 -- Identifiers_List collect all the identifiers that are not actuals of
1633 -- functions with writable actuals. If a writable actual is referenced
1634 -- twice as writable actual then Error_Node is set to reference its
1635 -- second occurrence, the error is reported, and the tree traversal
1636 -- is abandoned.
1637
1638 function Get_Function_Id (Call : Node_Id) return Entity_Id;
1639 -- Return the entity associated with the function call
1640
1641 procedure Preanalyze_Without_Errors (N : Node_Id);
1642 -- Preanalyze N without reporting errors. Very dubious, you can't just
1643 -- go analyzing things more than once???
1644
1645 -------------------------
1646 -- Collect_Identifiers --
1647 -------------------------
1648
1649 procedure Collect_Identifiers (N : Node_Id) is
1650
1651 function Check_Node (N : Node_Id) return Traverse_Result;
1652 -- Process a single node during the tree traversal to collect the
1653 -- writable actuals of functions and all the identifiers which are
1654 -- not writable actuals of functions.
1655
1656 function Contains (List : Elist_Id; N : Node_Id) return Boolean;
1657 -- Returns True if List has a node whose Entity is Entity (N)
1658
1659 -------------------------
1660 -- Check_Function_Call --
1661 -------------------------
1662
1663 function Check_Node (N : Node_Id) return Traverse_Result is
1664 Is_Writable_Actual : Boolean := False;
1665 Id : Entity_Id;
1666
1667 begin
1668 if Nkind (N) = N_Identifier then
1669
1670 -- No analysis possible if the entity is not decorated
1671
1672 if No (Entity (N)) then
1673 return Skip;
1674
1675 -- Don't collect identifiers of packages, called functions, etc
1676
1677 elsif Ekind_In (Entity (N), E_Package,
1678 E_Function,
1679 E_Procedure,
1680 E_Entry)
1681 then
1682 return Skip;
1683
1684 -- Analyze if N is a writable actual of a function
1685
1686 elsif Nkind (Parent (N)) = N_Function_Call then
1687 declare
1688 Call : constant Node_Id := Parent (N);
1689 Actual : Node_Id;
1690 Formal : Node_Id;
1691
1692 begin
1693 Id := Get_Function_Id (Call);
1694
1695 Formal := First_Formal (Id);
1696 Actual := First_Actual (Call);
1697 while Present (Actual) and then Present (Formal) loop
1698 if Actual = N then
1699 if Ekind_In (Formal, E_Out_Parameter,
1700 E_In_Out_Parameter)
1701 then
1702 Is_Writable_Actual := True;
1703 end if;
1704
1705 exit;
1706 end if;
1707
1708 Next_Formal (Formal);
1709 Next_Actual (Actual);
1710 end loop;
1711 end;
1712 end if;
1713
1714 if Is_Writable_Actual then
1715 if Contains (Writable_Actuals_List, N) then
1716 Error_Msg_NE
1717 ("value may be affected by call to& "
1718 & "because order of evaluation is arbitrary", N, Id);
1719 Error_Node := N;
1720 return Abandon;
1721 end if;
1722
1723 if Writable_Actuals_List = No_Elist then
1724 Writable_Actuals_List := New_Elmt_List;
1725 end if;
1726
1727 Append_Elmt (N, Writable_Actuals_List);
1728 else
1729 if Identifiers_List = No_Elist then
1730 Identifiers_List := New_Elmt_List;
1731 end if;
1732
1733 Append_Unique_Elmt (N, Identifiers_List);
1734 end if;
1735 end if;
1736
1737 return OK;
1738 end Check_Node;
1739
1740 --------------
1741 -- Contains --
1742 --------------
1743
1744 function Contains
1745 (List : Elist_Id;
1746 N : Node_Id) return Boolean
1747 is
1748 pragma Assert (Nkind (N) in N_Has_Entity);
1749
1750 Elmt : Elmt_Id;
1751
1752 begin
1753 if List = No_Elist then
1754 return False;
1755 end if;
1756
1757 Elmt := First_Elmt (List);
1758 while Present (Elmt) loop
1759 if Entity (Node (Elmt)) = Entity (N) then
1760 return True;
1761 else
1762 Next_Elmt (Elmt);
1763 end if;
1764 end loop;
1765
1766 return False;
1767 end Contains;
1768
1769 ------------------
1770 -- Do_Traversal --
1771 ------------------
1772
1773 procedure Do_Traversal is new Traverse_Proc (Check_Node);
1774 -- The traversal procedure
1775
1776 -- Start of processing for Collect_Identifiers
1777
1778 begin
1779 if Present (Error_Node) then
1780 return;
1781 end if;
1782
1783 if Nkind (N) in N_Subexpr
1784 and then Is_Static_Expression (N)
1785 then
1786 return;
1787 end if;
1788
1789 Do_Traversal (N);
1790 end Collect_Identifiers;
1791
1792 ---------------------
1793 -- Get_Function_Id --
1794 ---------------------
1795
1796 function Get_Function_Id (Call : Node_Id) return Entity_Id is
1797 Nam : constant Node_Id := Name (Call);
1798 Id : Entity_Id;
1799
1800 begin
1801 if Nkind (Nam) = N_Explicit_Dereference then
1802 Id := Etype (Nam);
1803 pragma Assert (Ekind (Id) = E_Subprogram_Type);
1804
1805 elsif Nkind (Nam) = N_Selected_Component then
1806 Id := Entity (Selector_Name (Nam));
1807
1808 elsif Nkind (Nam) = N_Indexed_Component then
1809 Id := Entity (Selector_Name (Prefix (Nam)));
1810
1811 else
1812 Id := Entity (Nam);
1813 end if;
1814
1815 return Id;
1816 end Get_Function_Id;
1817
1818 ---------------------------
1819 -- Preanalyze_Expression --
1820 ---------------------------
1821
1822 procedure Preanalyze_Without_Errors (N : Node_Id) is
1823 Status : constant Boolean := Get_Ignore_Errors;
1824 begin
1825 Set_Ignore_Errors (True);
1826 Preanalyze (N);
1827 Set_Ignore_Errors (Status);
1828 end Preanalyze_Without_Errors;
1829
1830 -- Start of processing for Check_Function_Writable_Actuals
1831
1832 begin
1833 -- The check only applies to Ada 2012 code, and only to constructs that
1834 -- have multiple constituents whose order of evaluation is not specified
1835 -- by the language.
1836
1837 if Ada_Version < Ada_2012
1838 or else (not (Nkind (N) in N_Op)
1839 and then not (Nkind (N) in N_Membership_Test)
1840 and then not Nkind_In (N, N_Range,
1841 N_Aggregate,
1842 N_Extension_Aggregate,
1843 N_Full_Type_Declaration,
1844 N_Function_Call,
1845 N_Procedure_Call_Statement,
1846 N_Entry_Call_Statement))
1847 or else (Nkind (N) = N_Full_Type_Declaration
1848 and then not Is_Record_Type (Defining_Identifier (N)))
1849
1850 -- In addition, this check only applies to source code, not to code
1851 -- generated by constraint checks.
1852
1853 or else not Comes_From_Source (N)
1854 then
1855 return;
1856 end if;
1857
1858 -- If a construct C has two or more direct constituents that are names
1859 -- or expressions whose evaluation may occur in an arbitrary order, at
1860 -- least one of which contains a function call with an in out or out
1861 -- parameter, then the construct is legal only if: for each name N that
1862 -- is passed as a parameter of mode in out or out to some inner function
1863 -- call C2 (not including the construct C itself), there is no other
1864 -- name anywhere within a direct constituent of the construct C other
1865 -- than the one containing C2, that is known to refer to the same
1866 -- object (RM 6.4.1(6.17/3)).
1867
1868 case Nkind (N) is
1869 when N_Range =>
1870 Collect_Identifiers (Low_Bound (N));
1871 Collect_Identifiers (High_Bound (N));
1872
1873 when N_Op | N_Membership_Test =>
1874 declare
1875 Expr : Node_Id;
1876 begin
1877 Collect_Identifiers (Left_Opnd (N));
1878
1879 if Present (Right_Opnd (N)) then
1880 Collect_Identifiers (Right_Opnd (N));
1881 end if;
1882
1883 if Nkind_In (N, N_In, N_Not_In)
1884 and then Present (Alternatives (N))
1885 then
1886 Expr := First (Alternatives (N));
1887 while Present (Expr) loop
1888 Collect_Identifiers (Expr);
1889
1890 Next (Expr);
1891 end loop;
1892 end if;
1893 end;
1894
1895 when N_Full_Type_Declaration =>
1896 declare
1897 function Get_Record_Part (N : Node_Id) return Node_Id;
1898 -- Return the record part of this record type definition
1899
1900 function Get_Record_Part (N : Node_Id) return Node_Id is
1901 Type_Def : constant Node_Id := Type_Definition (N);
1902 begin
1903 if Nkind (Type_Def) = N_Derived_Type_Definition then
1904 return Record_Extension_Part (Type_Def);
1905 else
1906 return Type_Def;
1907 end if;
1908 end Get_Record_Part;
1909
1910 Comp : Node_Id;
1911 Def_Id : Entity_Id := Defining_Identifier (N);
1912 Rec : Node_Id := Get_Record_Part (N);
1913
1914 begin
1915 -- No need to perform any analysis if the record has no
1916 -- components
1917
1918 if No (Rec) or else No (Component_List (Rec)) then
1919 return;
1920 end if;
1921
1922 -- Collect the identifiers starting from the deepest
1923 -- derivation. Done to report the error in the deepest
1924 -- derivation.
1925
1926 loop
1927 if Present (Component_List (Rec)) then
1928 Comp := First (Component_Items (Component_List (Rec)));
1929 while Present (Comp) loop
1930 if Nkind (Comp) = N_Component_Declaration
1931 and then Present (Expression (Comp))
1932 then
1933 Collect_Identifiers (Expression (Comp));
1934 end if;
1935
1936 Next (Comp);
1937 end loop;
1938 end if;
1939
1940 exit when No (Underlying_Type (Etype (Def_Id)))
1941 or else Base_Type (Underlying_Type (Etype (Def_Id)))
1942 = Def_Id;
1943
1944 Def_Id := Base_Type (Underlying_Type (Etype (Def_Id)));
1945 Rec := Get_Record_Part (Parent (Def_Id));
1946 end loop;
1947 end;
1948
1949 when N_Subprogram_Call |
1950 N_Entry_Call_Statement =>
1951 declare
1952 Id : constant Entity_Id := Get_Function_Id (N);
1953 Formal : Node_Id;
1954 Actual : Node_Id;
1955
1956 begin
1957 Formal := First_Formal (Id);
1958 Actual := First_Actual (N);
1959 while Present (Actual) and then Present (Formal) loop
1960 if Ekind_In (Formal, E_Out_Parameter,
1961 E_In_Out_Parameter)
1962 then
1963 Collect_Identifiers (Actual);
1964 end if;
1965
1966 Next_Formal (Formal);
1967 Next_Actual (Actual);
1968 end loop;
1969 end;
1970
1971 when N_Aggregate |
1972 N_Extension_Aggregate =>
1973 declare
1974 Assoc : Node_Id;
1975 Choice : Node_Id;
1976 Comp_Expr : Node_Id;
1977
1978 begin
1979 -- Handle the N_Others_Choice of array aggregates with static
1980 -- bounds. There is no need to perform this analysis in
1981 -- aggregates without static bounds since we cannot evaluate
1982 -- if the N_Others_Choice covers several elements. There is
1983 -- no need to handle the N_Others choice of record aggregates
1984 -- since at this stage it has been already expanded by
1985 -- Resolve_Record_Aggregate.
1986
1987 if Is_Array_Type (Etype (N))
1988 and then Nkind (N) = N_Aggregate
1989 and then Present (Aggregate_Bounds (N))
1990 and then Compile_Time_Known_Bounds (Etype (N))
1991 and then Expr_Value (High_Bound (Aggregate_Bounds (N)))
1992 > Expr_Value (Low_Bound (Aggregate_Bounds (N)))
1993 then
1994 declare
1995 Count_Components : Uint := Uint_0;
1996 Num_Components : Uint;
1997 Others_Assoc : Node_Id;
1998 Others_Choice : Node_Id := Empty;
1999 Others_Box_Present : Boolean := False;
2000
2001 begin
2002 -- Count positional associations
2003
2004 if Present (Expressions (N)) then
2005 Comp_Expr := First (Expressions (N));
2006 while Present (Comp_Expr) loop
2007 Count_Components := Count_Components + 1;
2008 Next (Comp_Expr);
2009 end loop;
2010 end if;
2011
2012 -- Count the rest of elements and locate the N_Others
2013 -- choice (if any)
2014
2015 Assoc := First (Component_Associations (N));
2016 while Present (Assoc) loop
2017 Choice := First (Choices (Assoc));
2018 while Present (Choice) loop
2019 if Nkind (Choice) = N_Others_Choice then
2020 Others_Assoc := Assoc;
2021 Others_Choice := Choice;
2022 Others_Box_Present := Box_Present (Assoc);
2023
2024 -- Count several components
2025
2026 elsif Nkind_In (Choice, N_Range,
2027 N_Subtype_Indication)
2028 or else (Is_Entity_Name (Choice)
2029 and then Is_Type (Entity (Choice)))
2030 then
2031 declare
2032 L, H : Node_Id;
2033 begin
2034 Get_Index_Bounds (Choice, L, H);
2035 pragma Assert
2036 (Compile_Time_Known_Value (L)
2037 and then Compile_Time_Known_Value (H));
2038 Count_Components :=
2039 Count_Components
2040 + Expr_Value (H) - Expr_Value (L) + 1;
2041 end;
2042
2043 -- Count single component. No other case available
2044 -- since we are handling an aggregate with static
2045 -- bounds.
2046
2047 else
2048 pragma Assert (Is_Static_Expression (Choice)
2049 or else Nkind (Choice) = N_Identifier
2050 or else Nkind (Choice) = N_Integer_Literal);
2051
2052 Count_Components := Count_Components + 1;
2053 end if;
2054
2055 Next (Choice);
2056 end loop;
2057
2058 Next (Assoc);
2059 end loop;
2060
2061 Num_Components :=
2062 Expr_Value (High_Bound (Aggregate_Bounds (N))) -
2063 Expr_Value (Low_Bound (Aggregate_Bounds (N))) + 1;
2064
2065 pragma Assert (Count_Components <= Num_Components);
2066
2067 -- Handle the N_Others choice if it covers several
2068 -- components
2069
2070 if Present (Others_Choice)
2071 and then (Num_Components - Count_Components) > 1
2072 then
2073 if not Others_Box_Present then
2074
2075 -- At this stage, if expansion is active, the
2076 -- expression of the others choice has not been
2077 -- analyzed. Hence we generate a duplicate and
2078 -- we analyze it silently to have available the
2079 -- minimum decoration required to collect the
2080 -- identifiers.
2081
2082 if not Expander_Active then
2083 Comp_Expr := Expression (Others_Assoc);
2084 else
2085 Comp_Expr :=
2086 New_Copy_Tree (Expression (Others_Assoc));
2087 Preanalyze_Without_Errors (Comp_Expr);
2088 end if;
2089
2090 Collect_Identifiers (Comp_Expr);
2091
2092 if Writable_Actuals_List /= No_Elist then
2093
2094 -- As suggested by Robert, at current stage we
2095 -- report occurrences of this case as warnings.
2096
2097 Error_Msg_N
2098 ("writable function parameter may affect "
2099 & "value in other component because order "
2100 & "of evaluation is unspecified??",
2101 Node (First_Elmt (Writable_Actuals_List)));
2102 end if;
2103 end if;
2104 end if;
2105 end;
2106 end if;
2107
2108 -- Handle ancestor part of extension aggregates
2109
2110 if Nkind (N) = N_Extension_Aggregate then
2111 Collect_Identifiers (Ancestor_Part (N));
2112 end if;
2113
2114 -- Handle positional associations
2115
2116 if Present (Expressions (N)) then
2117 Comp_Expr := First (Expressions (N));
2118 while Present (Comp_Expr) loop
2119 if not Is_Static_Expression (Comp_Expr) then
2120 Collect_Identifiers (Comp_Expr);
2121 end if;
2122
2123 Next (Comp_Expr);
2124 end loop;
2125 end if;
2126
2127 -- Handle discrete associations
2128
2129 if Present (Component_Associations (N)) then
2130 Assoc := First (Component_Associations (N));
2131 while Present (Assoc) loop
2132
2133 if not Box_Present (Assoc) then
2134 Choice := First (Choices (Assoc));
2135 while Present (Choice) loop
2136
2137 -- For now we skip discriminants since it requires
2138 -- performing the analysis in two phases: first one
2139 -- analyzing discriminants and second one analyzing
2140 -- the rest of components since discriminants are
2141 -- evaluated prior to components: too much extra
2142 -- work to detect a corner case???
2143
2144 if Nkind (Choice) in N_Has_Entity
2145 and then Present (Entity (Choice))
2146 and then Ekind (Entity (Choice)) = E_Discriminant
2147 then
2148 null;
2149
2150 elsif Box_Present (Assoc) then
2151 null;
2152
2153 else
2154 if not Analyzed (Expression (Assoc)) then
2155 Comp_Expr :=
2156 New_Copy_Tree (Expression (Assoc));
2157 Set_Parent (Comp_Expr, Parent (N));
2158 Preanalyze_Without_Errors (Comp_Expr);
2159 else
2160 Comp_Expr := Expression (Assoc);
2161 end if;
2162
2163 Collect_Identifiers (Comp_Expr);
2164 end if;
2165
2166 Next (Choice);
2167 end loop;
2168 end if;
2169
2170 Next (Assoc);
2171 end loop;
2172 end if;
2173 end;
2174
2175 when others =>
2176 return;
2177 end case;
2178
2179 -- No further action needed if we already reported an error
2180
2181 if Present (Error_Node) then
2182 return;
2183 end if;
2184
2185 -- Check if some writable argument of a function is referenced
2186
2187 if Writable_Actuals_List /= No_Elist
2188 and then Identifiers_List /= No_Elist
2189 then
2190 declare
2191 Elmt_1 : Elmt_Id;
2192 Elmt_2 : Elmt_Id;
2193
2194 begin
2195 Elmt_1 := First_Elmt (Writable_Actuals_List);
2196 while Present (Elmt_1) loop
2197 Elmt_2 := First_Elmt (Identifiers_List);
2198 while Present (Elmt_2) loop
2199 if Entity (Node (Elmt_1)) = Entity (Node (Elmt_2)) then
2200 case Nkind (Parent (Node (Elmt_2))) is
2201 when N_Aggregate |
2202 N_Component_Association |
2203 N_Component_Declaration =>
2204 Error_Msg_N
2205 ("value may be affected by call in other "
2206 & "component because they are evaluated "
2207 & "in unspecified order",
2208 Node (Elmt_2));
2209
2210 when N_In | N_Not_In =>
2211 Error_Msg_N
2212 ("value may be affected by call in other "
2213 & "alternative because they are evaluated "
2214 & "in unspecified order",
2215 Node (Elmt_2));
2216
2217 when others =>
2218 Error_Msg_N
2219 ("value of actual may be affected by call in "
2220 & "other actual because they are evaluated "
2221 & "in unspecified order",
2222 Node (Elmt_2));
2223 end case;
2224 end if;
2225
2226 Next_Elmt (Elmt_2);
2227 end loop;
2228
2229 Next_Elmt (Elmt_1);
2230 end loop;
2231 end;
2232 end if;
2233 end Check_Function_Writable_Actuals;
2234
2235 --------------------------------
2236 -- Check_Implicit_Dereference --
2237 --------------------------------
2238
2239 procedure Check_Implicit_Dereference (Nam : Node_Id; Typ : Entity_Id) is
2240 Disc : Entity_Id;
2241 Desig : Entity_Id;
2242
2243 begin
2244 if Ada_Version < Ada_2012
2245 or else not Has_Implicit_Dereference (Base_Type (Typ))
2246 then
2247 return;
2248
2249 elsif not Comes_From_Source (Nam) then
2250 return;
2251
2252 elsif Is_Entity_Name (Nam)
2253 and then Is_Type (Entity (Nam))
2254 then
2255 null;
2256
2257 else
2258 Disc := First_Discriminant (Typ);
2259 while Present (Disc) loop
2260 if Has_Implicit_Dereference (Disc) then
2261 Desig := Designated_Type (Etype (Disc));
2262 Add_One_Interp (Nam, Disc, Desig);
2263 exit;
2264 end if;
2265
2266 Next_Discriminant (Disc);
2267 end loop;
2268 end if;
2269 end Check_Implicit_Dereference;
2270
2271 ----------------------------------
2272 -- Check_Internal_Protected_Use --
2273 ----------------------------------
2274
2275 procedure Check_Internal_Protected_Use (N : Node_Id; Nam : Entity_Id) is
2276 S : Entity_Id;
2277 Prot : Entity_Id;
2278
2279 begin
2280 S := Current_Scope;
2281 while Present (S) loop
2282 if S = Standard_Standard then
2283 return;
2284
2285 elsif Ekind (S) = E_Function
2286 and then Ekind (Scope (S)) = E_Protected_Type
2287 then
2288 Prot := Scope (S);
2289 exit;
2290 end if;
2291
2292 S := Scope (S);
2293 end loop;
2294
2295 if Scope (Nam) = Prot and then Ekind (Nam) /= E_Function then
2296
2297 -- An indirect function call (e.g. a callback within a protected
2298 -- function body) is not statically illegal. If the access type is
2299 -- anonymous and is the type of an access parameter, the scope of Nam
2300 -- will be the protected type, but it is not a protected operation.
2301
2302 if Ekind (Nam) = E_Subprogram_Type
2303 and then
2304 Nkind (Associated_Node_For_Itype (Nam)) = N_Function_Specification
2305 then
2306 null;
2307
2308 elsif Nkind (N) = N_Subprogram_Renaming_Declaration then
2309 Error_Msg_N
2310 ("within protected function cannot use protected "
2311 & "procedure in renaming or as generic actual", N);
2312
2313 elsif Nkind (N) = N_Attribute_Reference then
2314 Error_Msg_N
2315 ("within protected function cannot take access of "
2316 & " protected procedure", N);
2317
2318 else
2319 Error_Msg_N
2320 ("within protected function, protected object is constant", N);
2321 Error_Msg_N
2322 ("\cannot call operation that may modify it", N);
2323 end if;
2324 end if;
2325 end Check_Internal_Protected_Use;
2326
2327 ---------------------------------------
2328 -- Check_Later_Vs_Basic_Declarations --
2329 ---------------------------------------
2330
2331 procedure Check_Later_Vs_Basic_Declarations
2332 (Decls : List_Id;
2333 During_Parsing : Boolean)
2334 is
2335 Body_Sloc : Source_Ptr;
2336 Decl : Node_Id;
2337
2338 function Is_Later_Declarative_Item (Decl : Node_Id) return Boolean;
2339 -- Return whether Decl is considered as a declarative item.
2340 -- When During_Parsing is True, the semantics of Ada 83 is followed.
2341 -- When During_Parsing is False, the semantics of SPARK is followed.
2342
2343 -------------------------------
2344 -- Is_Later_Declarative_Item --
2345 -------------------------------
2346
2347 function Is_Later_Declarative_Item (Decl : Node_Id) return Boolean is
2348 begin
2349 if Nkind (Decl) in N_Later_Decl_Item then
2350 return True;
2351
2352 elsif Nkind (Decl) = N_Pragma then
2353 return True;
2354
2355 elsif During_Parsing then
2356 return False;
2357
2358 -- In SPARK, a package declaration is not considered as a later
2359 -- declarative item.
2360
2361 elsif Nkind (Decl) = N_Package_Declaration then
2362 return False;
2363
2364 -- In SPARK, a renaming is considered as a later declarative item
2365
2366 elsif Nkind (Decl) in N_Renaming_Declaration then
2367 return True;
2368
2369 else
2370 return False;
2371 end if;
2372 end Is_Later_Declarative_Item;
2373
2374 -- Start of Check_Later_Vs_Basic_Declarations
2375
2376 begin
2377 Decl := First (Decls);
2378
2379 -- Loop through sequence of basic declarative items
2380
2381 Outer : while Present (Decl) loop
2382 if not Nkind_In (Decl, N_Subprogram_Body, N_Package_Body, N_Task_Body)
2383 and then Nkind (Decl) not in N_Body_Stub
2384 then
2385 Next (Decl);
2386
2387 -- Once a body is encountered, we only allow later declarative
2388 -- items. The inner loop checks the rest of the list.
2389
2390 else
2391 Body_Sloc := Sloc (Decl);
2392
2393 Inner : while Present (Decl) loop
2394 if not Is_Later_Declarative_Item (Decl) then
2395 if During_Parsing then
2396 if Ada_Version = Ada_83 then
2397 Error_Msg_Sloc := Body_Sloc;
2398 Error_Msg_N
2399 ("(Ada 83) decl cannot appear after body#", Decl);
2400 end if;
2401 else
2402 Error_Msg_Sloc := Body_Sloc;
2403 Check_SPARK_Restriction
2404 ("decl cannot appear after body#", Decl);
2405 end if;
2406 end if;
2407
2408 Next (Decl);
2409 end loop Inner;
2410 end if;
2411 end loop Outer;
2412 end Check_Later_Vs_Basic_Declarations;
2413
2414 -------------------------
2415 -- Check_Nested_Access --
2416 -------------------------
2417
2418 procedure Check_Nested_Access (Ent : Entity_Id) is
2419 Scop : constant Entity_Id := Current_Scope;
2420 Current_Subp : Entity_Id;
2421 Enclosing : Entity_Id;
2422
2423 begin
2424 -- Currently only enabled for VM back-ends for efficiency, should we
2425 -- enable it more systematically ???
2426
2427 -- Check for Is_Imported needs commenting below ???
2428
2429 if VM_Target /= No_VM
2430 and then (Ekind (Ent) = E_Variable
2431 or else
2432 Ekind (Ent) = E_Constant
2433 or else
2434 Ekind (Ent) = E_Loop_Parameter)
2435 and then Scope (Ent) /= Empty
2436 and then not Is_Library_Level_Entity (Ent)
2437 and then not Is_Imported (Ent)
2438 then
2439 if Is_Subprogram (Scop)
2440 or else Is_Generic_Subprogram (Scop)
2441 or else Is_Entry (Scop)
2442 then
2443 Current_Subp := Scop;
2444 else
2445 Current_Subp := Current_Subprogram;
2446 end if;
2447
2448 Enclosing := Enclosing_Subprogram (Ent);
2449
2450 if Enclosing /= Empty
2451 and then Enclosing /= Current_Subp
2452 then
2453 Set_Has_Up_Level_Access (Ent, True);
2454 end if;
2455 end if;
2456 end Check_Nested_Access;
2457
2458 ---------------------------
2459 -- Check_No_Hidden_State --
2460 ---------------------------
2461
2462 procedure Check_No_Hidden_State (Id : Entity_Id) is
2463 function Has_Null_Abstract_State (Pkg : Entity_Id) return Boolean;
2464 -- Determine whether the entity of a package denoted by Pkg has a null
2465 -- abstract state.
2466
2467 -----------------------------
2468 -- Has_Null_Abstract_State --
2469 -----------------------------
2470
2471 function Has_Null_Abstract_State (Pkg : Entity_Id) return Boolean is
2472 States : constant Elist_Id := Abstract_States (Pkg);
2473
2474 begin
2475 -- Check first available state of related package. A null abstract
2476 -- state always appears as the sole element of the state list.
2477
2478 return
2479 Present (States)
2480 and then Is_Null_State (Node (First_Elmt (States)));
2481 end Has_Null_Abstract_State;
2482
2483 -- Local variables
2484
2485 Context : Entity_Id := Empty;
2486 Not_Visible : Boolean := False;
2487 Scop : Entity_Id;
2488
2489 -- Start of processing for Check_No_Hidden_State
2490
2491 begin
2492 pragma Assert (Ekind_In (Id, E_Abstract_State, E_Variable));
2493
2494 -- Find the proper context where the object or state appears
2495
2496 Scop := Scope (Id);
2497 while Present (Scop) loop
2498 Context := Scop;
2499
2500 -- Keep track of the context's visibility
2501
2502 Not_Visible := Not_Visible or else In_Private_Part (Context);
2503
2504 -- Prevent the search from going too far
2505
2506 if Context = Standard_Standard then
2507 return;
2508
2509 -- Objects and states that appear immediately within a subprogram or
2510 -- inside a construct nested within a subprogram do not introduce a
2511 -- hidden state. They behave as local variable declarations.
2512
2513 elsif Is_Subprogram (Context) then
2514 return;
2515
2516 -- When examining a package body, use the entity of the spec as it
2517 -- carries the abstract state declarations.
2518
2519 elsif Ekind (Context) = E_Package_Body then
2520 Context := Spec_Entity (Context);
2521 end if;
2522
2523 -- Stop the traversal when a package subject to a null abstract state
2524 -- has been found.
2525
2526 if Ekind_In (Context, E_Generic_Package, E_Package)
2527 and then Has_Null_Abstract_State (Context)
2528 then
2529 exit;
2530 end if;
2531
2532 Scop := Scope (Scop);
2533 end loop;
2534
2535 -- At this point we know that there is at least one package with a null
2536 -- abstract state in visibility. Emit an error message unconditionally
2537 -- if the entity being processed is a state because the placement of the
2538 -- related package is irrelevant. This is not the case for objects as
2539 -- the intermediate context matters.
2540
2541 if Present (Context)
2542 and then (Ekind (Id) = E_Abstract_State or else Not_Visible)
2543 then
2544 Error_Msg_N ("cannot introduce hidden state &", Id);
2545 Error_Msg_NE ("\package & has null abstract state", Id, Context);
2546 end if;
2547 end Check_No_Hidden_State;
2548
2549 ------------------------------------------
2550 -- Check_Potentially_Blocking_Operation --
2551 ------------------------------------------
2552
2553 procedure Check_Potentially_Blocking_Operation (N : Node_Id) is
2554 S : Entity_Id;
2555
2556 begin
2557 -- N is one of the potentially blocking operations listed in 9.5.1(8).
2558 -- When pragma Detect_Blocking is active, the run time will raise
2559 -- Program_Error. Here we only issue a warning, since we generally
2560 -- support the use of potentially blocking operations in the absence
2561 -- of the pragma.
2562
2563 -- Indirect blocking through a subprogram call cannot be diagnosed
2564 -- statically without interprocedural analysis, so we do not attempt
2565 -- to do it here.
2566
2567 S := Scope (Current_Scope);
2568 while Present (S) and then S /= Standard_Standard loop
2569 if Is_Protected_Type (S) then
2570 Error_Msg_N
2571 ("potentially blocking operation in protected operation??", N);
2572 return;
2573 end if;
2574
2575 S := Scope (S);
2576 end loop;
2577 end Check_Potentially_Blocking_Operation;
2578
2579 ---------------------------------
2580 -- Check_Result_And_Post_State --
2581 ---------------------------------
2582
2583 procedure Check_Result_And_Post_State
2584 (Prag : Node_Id;
2585 Result_Seen : in out Boolean)
2586 is
2587 procedure Check_Expression (Expr : Node_Id);
2588 -- Perform the 'Result and post-state checks on a given expression
2589
2590 function Is_Function_Result (N : Node_Id) return Traverse_Result;
2591 -- Attempt to find attribute 'Result in a subtree denoted by N
2592
2593 function Is_Trivial_Boolean (N : Node_Id) return Boolean;
2594 -- Determine whether source node N denotes "True" or "False"
2595
2596 function Mentions_Post_State (N : Node_Id) return Boolean;
2597 -- Determine whether a subtree denoted by N mentions any construct that
2598 -- denotes a post-state.
2599
2600 procedure Check_Function_Result is
2601 new Traverse_Proc (Is_Function_Result);
2602
2603 ----------------------
2604 -- Check_Expression --
2605 ----------------------
2606
2607 procedure Check_Expression (Expr : Node_Id) is
2608 begin
2609 if not Is_Trivial_Boolean (Expr) then
2610 Check_Function_Result (Expr);
2611
2612 if not Mentions_Post_State (Expr) then
2613 if Pragma_Name (Prag) = Name_Contract_Cases then
2614 Error_Msg_N
2615 ("contract case refers only to pre-state?T?", Expr);
2616
2617 elsif Pragma_Name (Prag) = Name_Refined_Post then
2618 Error_Msg_N
2619 ("refined postcondition refers only to pre-state?T?",
2620 Prag);
2621
2622 else
2623 Error_Msg_N
2624 ("postcondition refers only to pre-state?T?", Prag);
2625 end if;
2626 end if;
2627 end if;
2628 end Check_Expression;
2629
2630 ------------------------
2631 -- Is_Function_Result --
2632 ------------------------
2633
2634 function Is_Function_Result (N : Node_Id) return Traverse_Result is
2635 begin
2636 if Is_Attribute_Result (N) then
2637 Result_Seen := True;
2638 return Abandon;
2639
2640 -- Continue the traversal
2641
2642 else
2643 return OK;
2644 end if;
2645 end Is_Function_Result;
2646
2647 ------------------------
2648 -- Is_Trivial_Boolean --
2649 ------------------------
2650
2651 function Is_Trivial_Boolean (N : Node_Id) return Boolean is
2652 begin
2653 return
2654 Comes_From_Source (N)
2655 and then Is_Entity_Name (N)
2656 and then (Entity (N) = Standard_True
2657 or else Entity (N) = Standard_False);
2658 end Is_Trivial_Boolean;
2659
2660 -------------------------
2661 -- Mentions_Post_State --
2662 -------------------------
2663
2664 function Mentions_Post_State (N : Node_Id) return Boolean is
2665 Post_State_Seen : Boolean := False;
2666
2667 function Is_Post_State (N : Node_Id) return Traverse_Result;
2668 -- Attempt to find a construct that denotes a post-state. If this is
2669 -- the case, set flag Post_State_Seen.
2670
2671 -------------------
2672 -- Is_Post_State --
2673 -------------------
2674
2675 function Is_Post_State (N : Node_Id) return Traverse_Result is
2676 Ent : Entity_Id;
2677
2678 begin
2679 if Nkind_In (N, N_Explicit_Dereference, N_Function_Call) then
2680 Post_State_Seen := True;
2681 return Abandon;
2682
2683 elsif Nkind_In (N, N_Expanded_Name, N_Identifier) then
2684 Ent := Entity (N);
2685
2686 -- The entity may be modifiable through an implicit dereference
2687
2688 if No (Ent)
2689 or else Ekind (Ent) in Assignable_Kind
2690 or else (Is_Access_Type (Etype (Ent))
2691 and then Nkind (Parent (N)) = N_Selected_Component)
2692 then
2693 Post_State_Seen := True;
2694 return Abandon;
2695 end if;
2696
2697 elsif Nkind (N) = N_Attribute_Reference then
2698 if Attribute_Name (N) = Name_Old then
2699 return Skip;
2700
2701 elsif Attribute_Name (N) = Name_Result then
2702 Post_State_Seen := True;
2703 return Abandon;
2704 end if;
2705 end if;
2706
2707 return OK;
2708 end Is_Post_State;
2709
2710 procedure Find_Post_State is new Traverse_Proc (Is_Post_State);
2711
2712 -- Start of processing for Mentions_Post_State
2713
2714 begin
2715 Find_Post_State (N);
2716
2717 return Post_State_Seen;
2718 end Mentions_Post_State;
2719
2720 -- Local variables
2721
2722 Expr : constant Node_Id :=
2723 Get_Pragma_Arg (First (Pragma_Argument_Associations (Prag)));
2724 Nam : constant Name_Id := Pragma_Name (Prag);
2725 CCase : Node_Id;
2726
2727 -- Start of processing for Check_Result_And_Post_State
2728
2729 begin
2730 -- Examine all consequences
2731
2732 if Nam = Name_Contract_Cases then
2733 CCase := First (Component_Associations (Expr));
2734 while Present (CCase) loop
2735 Check_Expression (Expression (CCase));
2736
2737 Next (CCase);
2738 end loop;
2739
2740 -- Examine the expression of a postcondition
2741
2742 else pragma Assert (Nam_In (Nam, Name_Postcondition, Name_Refined_Post));
2743 Check_Expression (Expr);
2744 end if;
2745 end Check_Result_And_Post_State;
2746
2747 ---------------------------------
2748 -- Check_SPARK_Mode_In_Generic --
2749 ---------------------------------
2750
2751 procedure Check_SPARK_Mode_In_Generic (N : Node_Id) is
2752 Aspect : Node_Id;
2753
2754 begin
2755 -- Try to find aspect SPARK_Mode and flag it as illegal
2756
2757 if Has_Aspects (N) then
2758 Aspect := First (Aspect_Specifications (N));
2759 while Present (Aspect) loop
2760 if Get_Aspect_Id (Aspect) = Aspect_SPARK_Mode then
2761 Error_Msg_Name_1 := Name_SPARK_Mode;
2762 Error_Msg_N
2763 ("incorrect placement of aspect % on a generic", Aspect);
2764 exit;
2765 end if;
2766
2767 Next (Aspect);
2768 end loop;
2769 end if;
2770 end Check_SPARK_Mode_In_Generic;
2771
2772 ------------------------------
2773 -- Check_Unprotected_Access --
2774 ------------------------------
2775
2776 procedure Check_Unprotected_Access
2777 (Context : Node_Id;
2778 Expr : Node_Id)
2779 is
2780 Cont_Encl_Typ : Entity_Id;
2781 Pref_Encl_Typ : Entity_Id;
2782
2783 function Enclosing_Protected_Type (Obj : Node_Id) return Entity_Id;
2784 -- Check whether Obj is a private component of a protected object.
2785 -- Return the protected type where the component resides, Empty
2786 -- otherwise.
2787
2788 function Is_Public_Operation return Boolean;
2789 -- Verify that the enclosing operation is callable from outside the
2790 -- protected object, to minimize false positives.
2791
2792 ------------------------------
2793 -- Enclosing_Protected_Type --
2794 ------------------------------
2795
2796 function Enclosing_Protected_Type (Obj : Node_Id) return Entity_Id is
2797 begin
2798 if Is_Entity_Name (Obj) then
2799 declare
2800 Ent : Entity_Id := Entity (Obj);
2801
2802 begin
2803 -- The object can be a renaming of a private component, use
2804 -- the original record component.
2805
2806 if Is_Prival (Ent) then
2807 Ent := Prival_Link (Ent);
2808 end if;
2809
2810 if Is_Protected_Type (Scope (Ent)) then
2811 return Scope (Ent);
2812 end if;
2813 end;
2814 end if;
2815
2816 -- For indexed and selected components, recursively check the prefix
2817
2818 if Nkind_In (Obj, N_Indexed_Component, N_Selected_Component) then
2819 return Enclosing_Protected_Type (Prefix (Obj));
2820
2821 -- The object does not denote a protected component
2822
2823 else
2824 return Empty;
2825 end if;
2826 end Enclosing_Protected_Type;
2827
2828 -------------------------
2829 -- Is_Public_Operation --
2830 -------------------------
2831
2832 function Is_Public_Operation return Boolean is
2833 S : Entity_Id;
2834 E : Entity_Id;
2835
2836 begin
2837 S := Current_Scope;
2838 while Present (S)
2839 and then S /= Pref_Encl_Typ
2840 loop
2841 if Scope (S) = Pref_Encl_Typ then
2842 E := First_Entity (Pref_Encl_Typ);
2843 while Present (E)
2844 and then E /= First_Private_Entity (Pref_Encl_Typ)
2845 loop
2846 if E = S then
2847 return True;
2848 end if;
2849 Next_Entity (E);
2850 end loop;
2851 end if;
2852
2853 S := Scope (S);
2854 end loop;
2855
2856 return False;
2857 end Is_Public_Operation;
2858
2859 -- Start of processing for Check_Unprotected_Access
2860
2861 begin
2862 if Nkind (Expr) = N_Attribute_Reference
2863 and then Attribute_Name (Expr) = Name_Unchecked_Access
2864 then
2865 Cont_Encl_Typ := Enclosing_Protected_Type (Context);
2866 Pref_Encl_Typ := Enclosing_Protected_Type (Prefix (Expr));
2867
2868 -- Check whether we are trying to export a protected component to a
2869 -- context with an equal or lower access level.
2870
2871 if Present (Pref_Encl_Typ)
2872 and then No (Cont_Encl_Typ)
2873 and then Is_Public_Operation
2874 and then Scope_Depth (Pref_Encl_Typ) >=
2875 Object_Access_Level (Context)
2876 then
2877 Error_Msg_N
2878 ("??possible unprotected access to protected data", Expr);
2879 end if;
2880 end if;
2881 end Check_Unprotected_Access;
2882
2883 ---------------
2884 -- Check_VMS --
2885 ---------------
2886
2887 procedure Check_VMS (Construct : Node_Id) is
2888 begin
2889 if not OpenVMS_On_Target then
2890 Error_Msg_N
2891 ("this construct is allowed only in Open'V'M'S", Construct);
2892 end if;
2893 end Check_VMS;
2894
2895 ------------------------
2896 -- Collect_Interfaces --
2897 ------------------------
2898
2899 procedure Collect_Interfaces
2900 (T : Entity_Id;
2901 Ifaces_List : out Elist_Id;
2902 Exclude_Parents : Boolean := False;
2903 Use_Full_View : Boolean := True)
2904 is
2905 procedure Collect (Typ : Entity_Id);
2906 -- Subsidiary subprogram used to traverse the whole list
2907 -- of directly and indirectly implemented interfaces
2908
2909 -------------
2910 -- Collect --
2911 -------------
2912
2913 procedure Collect (Typ : Entity_Id) is
2914 Ancestor : Entity_Id;
2915 Full_T : Entity_Id;
2916 Id : Node_Id;
2917 Iface : Entity_Id;
2918
2919 begin
2920 Full_T := Typ;
2921
2922 -- Handle private types
2923
2924 if Use_Full_View
2925 and then Is_Private_Type (Typ)
2926 and then Present (Full_View (Typ))
2927 then
2928 Full_T := Full_View (Typ);
2929 end if;
2930
2931 -- Include the ancestor if we are generating the whole list of
2932 -- abstract interfaces.
2933
2934 if Etype (Full_T) /= Typ
2935
2936 -- Protect the frontend against wrong sources. For example:
2937
2938 -- package P is
2939 -- type A is tagged null record;
2940 -- type B is new A with private;
2941 -- type C is new A with private;
2942 -- private
2943 -- type B is new C with null record;
2944 -- type C is new B with null record;
2945 -- end P;
2946
2947 and then Etype (Full_T) /= T
2948 then
2949 Ancestor := Etype (Full_T);
2950 Collect (Ancestor);
2951
2952 if Is_Interface (Ancestor)
2953 and then not Exclude_Parents
2954 then
2955 Append_Unique_Elmt (Ancestor, Ifaces_List);
2956 end if;
2957 end if;
2958
2959 -- Traverse the graph of ancestor interfaces
2960
2961 if Is_Non_Empty_List (Abstract_Interface_List (Full_T)) then
2962 Id := First (Abstract_Interface_List (Full_T));
2963 while Present (Id) loop
2964 Iface := Etype (Id);
2965
2966 -- Protect against wrong uses. For example:
2967 -- type I is interface;
2968 -- type O is tagged null record;
2969 -- type Wrong is new I and O with null record; -- ERROR
2970
2971 if Is_Interface (Iface) then
2972 if Exclude_Parents
2973 and then Etype (T) /= T
2974 and then Interface_Present_In_Ancestor (Etype (T), Iface)
2975 then
2976 null;
2977 else
2978 Collect (Iface);
2979 Append_Unique_Elmt (Iface, Ifaces_List);
2980 end if;
2981 end if;
2982
2983 Next (Id);
2984 end loop;
2985 end if;
2986 end Collect;
2987
2988 -- Start of processing for Collect_Interfaces
2989
2990 begin
2991 pragma Assert (Is_Tagged_Type (T) or else Is_Concurrent_Type (T));
2992 Ifaces_List := New_Elmt_List;
2993 Collect (T);
2994 end Collect_Interfaces;
2995
2996 ----------------------------------
2997 -- Collect_Interface_Components --
2998 ----------------------------------
2999
3000 procedure Collect_Interface_Components
3001 (Tagged_Type : Entity_Id;
3002 Components_List : out Elist_Id)
3003 is
3004 procedure Collect (Typ : Entity_Id);
3005 -- Subsidiary subprogram used to climb to the parents
3006
3007 -------------
3008 -- Collect --
3009 -------------
3010
3011 procedure Collect (Typ : Entity_Id) is
3012 Tag_Comp : Entity_Id;
3013 Parent_Typ : Entity_Id;
3014
3015 begin
3016 -- Handle private types
3017
3018 if Present (Full_View (Etype (Typ))) then
3019 Parent_Typ := Full_View (Etype (Typ));
3020 else
3021 Parent_Typ := Etype (Typ);
3022 end if;
3023
3024 if Parent_Typ /= Typ
3025
3026 -- Protect the frontend against wrong sources. For example:
3027
3028 -- package P is
3029 -- type A is tagged null record;
3030 -- type B is new A with private;
3031 -- type C is new A with private;
3032 -- private
3033 -- type B is new C with null record;
3034 -- type C is new B with null record;
3035 -- end P;
3036
3037 and then Parent_Typ /= Tagged_Type
3038 then
3039 Collect (Parent_Typ);
3040 end if;
3041
3042 -- Collect the components containing tags of secondary dispatch
3043 -- tables.
3044
3045 Tag_Comp := Next_Tag_Component (First_Tag_Component (Typ));
3046 while Present (Tag_Comp) loop
3047 pragma Assert (Present (Related_Type (Tag_Comp)));
3048 Append_Elmt (Tag_Comp, Components_List);
3049
3050 Tag_Comp := Next_Tag_Component (Tag_Comp);
3051 end loop;
3052 end Collect;
3053
3054 -- Start of processing for Collect_Interface_Components
3055
3056 begin
3057 pragma Assert (Ekind (Tagged_Type) = E_Record_Type
3058 and then Is_Tagged_Type (Tagged_Type));
3059
3060 Components_List := New_Elmt_List;
3061 Collect (Tagged_Type);
3062 end Collect_Interface_Components;
3063
3064 -----------------------------
3065 -- Collect_Interfaces_Info --
3066 -----------------------------
3067
3068 procedure Collect_Interfaces_Info
3069 (T : Entity_Id;
3070 Ifaces_List : out Elist_Id;
3071 Components_List : out Elist_Id;
3072 Tags_List : out Elist_Id)
3073 is
3074 Comps_List : Elist_Id;
3075 Comp_Elmt : Elmt_Id;
3076 Comp_Iface : Entity_Id;
3077 Iface_Elmt : Elmt_Id;
3078 Iface : Entity_Id;
3079
3080 function Search_Tag (Iface : Entity_Id) return Entity_Id;
3081 -- Search for the secondary tag associated with the interface type
3082 -- Iface that is implemented by T.
3083
3084 ----------------
3085 -- Search_Tag --
3086 ----------------
3087
3088 function Search_Tag (Iface : Entity_Id) return Entity_Id is
3089 ADT : Elmt_Id;
3090 begin
3091 if not Is_CPP_Class (T) then
3092 ADT := Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (T))));
3093 else
3094 ADT := Next_Elmt (First_Elmt (Access_Disp_Table (T)));
3095 end if;
3096
3097 while Present (ADT)
3098 and then Is_Tag (Node (ADT))
3099 and then Related_Type (Node (ADT)) /= Iface
3100 loop
3101 -- Skip secondary dispatch table referencing thunks to user
3102 -- defined primitives covered by this interface.
3103
3104 pragma Assert (Has_Suffix (Node (ADT), 'P'));
3105 Next_Elmt (ADT);
3106
3107 -- Skip secondary dispatch tables of Ada types
3108
3109 if not Is_CPP_Class (T) then
3110
3111 -- Skip secondary dispatch table referencing thunks to
3112 -- predefined primitives.
3113
3114 pragma Assert (Has_Suffix (Node (ADT), 'Y'));
3115 Next_Elmt (ADT);
3116
3117 -- Skip secondary dispatch table referencing user-defined
3118 -- primitives covered by this interface.
3119
3120 pragma Assert (Has_Suffix (Node (ADT), 'D'));
3121 Next_Elmt (ADT);
3122
3123 -- Skip secondary dispatch table referencing predefined
3124 -- primitives.
3125
3126 pragma Assert (Has_Suffix (Node (ADT), 'Z'));
3127 Next_Elmt (ADT);
3128 end if;
3129 end loop;
3130
3131 pragma Assert (Is_Tag (Node (ADT)));
3132 return Node (ADT);
3133 end Search_Tag;
3134
3135 -- Start of processing for Collect_Interfaces_Info
3136
3137 begin
3138 Collect_Interfaces (T, Ifaces_List);
3139 Collect_Interface_Components (T, Comps_List);
3140
3141 -- Search for the record component and tag associated with each
3142 -- interface type of T.
3143
3144 Components_List := New_Elmt_List;
3145 Tags_List := New_Elmt_List;
3146
3147 Iface_Elmt := First_Elmt (Ifaces_List);
3148 while Present (Iface_Elmt) loop
3149 Iface := Node (Iface_Elmt);
3150
3151 -- Associate the primary tag component and the primary dispatch table
3152 -- with all the interfaces that are parents of T
3153
3154 if Is_Ancestor (Iface, T, Use_Full_View => True) then
3155 Append_Elmt (First_Tag_Component (T), Components_List);
3156 Append_Elmt (Node (First_Elmt (Access_Disp_Table (T))), Tags_List);
3157
3158 -- Otherwise search for the tag component and secondary dispatch
3159 -- table of Iface
3160
3161 else
3162 Comp_Elmt := First_Elmt (Comps_List);
3163 while Present (Comp_Elmt) loop
3164 Comp_Iface := Related_Type (Node (Comp_Elmt));
3165
3166 if Comp_Iface = Iface
3167 or else Is_Ancestor (Iface, Comp_Iface, Use_Full_View => True)
3168 then
3169 Append_Elmt (Node (Comp_Elmt), Components_List);
3170 Append_Elmt (Search_Tag (Comp_Iface), Tags_List);
3171 exit;
3172 end if;
3173
3174 Next_Elmt (Comp_Elmt);
3175 end loop;
3176 pragma Assert (Present (Comp_Elmt));
3177 end if;
3178
3179 Next_Elmt (Iface_Elmt);
3180 end loop;
3181 end Collect_Interfaces_Info;
3182
3183 ---------------------
3184 -- Collect_Parents --
3185 ---------------------
3186
3187 procedure Collect_Parents
3188 (T : Entity_Id;
3189 List : out Elist_Id;
3190 Use_Full_View : Boolean := True)
3191 is
3192 Current_Typ : Entity_Id := T;
3193 Parent_Typ : Entity_Id;
3194
3195 begin
3196 List := New_Elmt_List;
3197
3198 -- No action if the if the type has no parents
3199
3200 if T = Etype (T) then
3201 return;
3202 end if;
3203
3204 loop
3205 Parent_Typ := Etype (Current_Typ);
3206
3207 if Is_Private_Type (Parent_Typ)
3208 and then Present (Full_View (Parent_Typ))
3209 and then Use_Full_View
3210 then
3211 Parent_Typ := Full_View (Base_Type (Parent_Typ));
3212 end if;
3213
3214 Append_Elmt (Parent_Typ, List);
3215
3216 exit when Parent_Typ = Current_Typ;
3217 Current_Typ := Parent_Typ;
3218 end loop;
3219 end Collect_Parents;
3220
3221 ----------------------------------
3222 -- Collect_Primitive_Operations --
3223 ----------------------------------
3224
3225 function Collect_Primitive_Operations (T : Entity_Id) return Elist_Id is
3226 B_Type : constant Entity_Id := Base_Type (T);
3227 B_Decl : constant Node_Id := Original_Node (Parent (B_Type));
3228 B_Scope : Entity_Id := Scope (B_Type);
3229 Op_List : Elist_Id;
3230 Formal : Entity_Id;
3231 Is_Prim : Boolean;
3232 Is_Type_In_Pkg : Boolean;
3233 Formal_Derived : Boolean := False;
3234 Id : Entity_Id;
3235
3236 function Match (E : Entity_Id) return Boolean;
3237 -- True if E's base type is B_Type, or E is of an anonymous access type
3238 -- and the base type of its designated type is B_Type.
3239
3240 -----------
3241 -- Match --
3242 -----------
3243
3244 function Match (E : Entity_Id) return Boolean is
3245 Etyp : Entity_Id := Etype (E);
3246
3247 begin
3248 if Ekind (Etyp) = E_Anonymous_Access_Type then
3249 Etyp := Designated_Type (Etyp);
3250 end if;
3251
3252 return Base_Type (Etyp) = B_Type;
3253 end Match;
3254
3255 -- Start of processing for Collect_Primitive_Operations
3256
3257 begin
3258 -- For tagged types, the primitive operations are collected as they
3259 -- are declared, and held in an explicit list which is simply returned.
3260
3261 if Is_Tagged_Type (B_Type) then
3262 return Primitive_Operations (B_Type);
3263
3264 -- An untagged generic type that is a derived type inherits the
3265 -- primitive operations of its parent type. Other formal types only
3266 -- have predefined operators, which are not explicitly represented.
3267
3268 elsif Is_Generic_Type (B_Type) then
3269 if Nkind (B_Decl) = N_Formal_Type_Declaration
3270 and then Nkind (Formal_Type_Definition (B_Decl))
3271 = N_Formal_Derived_Type_Definition
3272 then
3273 Formal_Derived := True;
3274 else
3275 return New_Elmt_List;
3276 end if;
3277 end if;
3278
3279 Op_List := New_Elmt_List;
3280
3281 if B_Scope = Standard_Standard then
3282 if B_Type = Standard_String then
3283 Append_Elmt (Standard_Op_Concat, Op_List);
3284
3285 elsif B_Type = Standard_Wide_String then
3286 Append_Elmt (Standard_Op_Concatw, Op_List);
3287
3288 else
3289 null;
3290 end if;
3291
3292 -- Locate the primitive subprograms of the type
3293
3294 else
3295 -- The primitive operations appear after the base type, except
3296 -- if the derivation happens within the private part of B_Scope
3297 -- and the type is a private type, in which case both the type
3298 -- and some primitive operations may appear before the base
3299 -- type, and the list of candidates starts after the type.
3300
3301 if In_Open_Scopes (B_Scope)
3302 and then Scope (T) = B_Scope
3303 and then In_Private_Part (B_Scope)
3304 then
3305 Id := Next_Entity (T);
3306 else
3307 Id := Next_Entity (B_Type);
3308 end if;
3309
3310 -- Set flag if this is a type in a package spec
3311
3312 Is_Type_In_Pkg :=
3313 Is_Package_Or_Generic_Package (B_Scope)
3314 and then
3315 Nkind (Parent (Declaration_Node (First_Subtype (T)))) /=
3316 N_Package_Body;
3317
3318 while Present (Id) loop
3319
3320 -- Test whether the result type or any of the parameter types of
3321 -- each subprogram following the type match that type when the
3322 -- type is declared in a package spec, is a derived type, or the
3323 -- subprogram is marked as primitive. (The Is_Primitive test is
3324 -- needed to find primitives of nonderived types in declarative
3325 -- parts that happen to override the predefined "=" operator.)
3326
3327 -- Note that generic formal subprograms are not considered to be
3328 -- primitive operations and thus are never inherited.
3329
3330 if Is_Overloadable (Id)
3331 and then (Is_Type_In_Pkg
3332 or else Is_Derived_Type (B_Type)
3333 or else Is_Primitive (Id))
3334 and then Nkind (Parent (Parent (Id)))
3335 not in N_Formal_Subprogram_Declaration
3336 then
3337 Is_Prim := False;
3338
3339 if Match (Id) then
3340 Is_Prim := True;
3341
3342 else
3343 Formal := First_Formal (Id);
3344 while Present (Formal) loop
3345 if Match (Formal) then
3346 Is_Prim := True;
3347 exit;
3348 end if;
3349
3350 Next_Formal (Formal);
3351 end loop;
3352 end if;
3353
3354 -- For a formal derived type, the only primitives are the ones
3355 -- inherited from the parent type. Operations appearing in the
3356 -- package declaration are not primitive for it.
3357
3358 if Is_Prim
3359 and then (not Formal_Derived
3360 or else Present (Alias (Id)))
3361 then
3362 -- In the special case of an equality operator aliased to
3363 -- an overriding dispatching equality belonging to the same
3364 -- type, we don't include it in the list of primitives.
3365 -- This avoids inheriting multiple equality operators when
3366 -- deriving from untagged private types whose full type is
3367 -- tagged, which can otherwise cause ambiguities. Note that
3368 -- this should only happen for this kind of untagged parent
3369 -- type, since normally dispatching operations are inherited
3370 -- using the type's Primitive_Operations list.
3371
3372 if Chars (Id) = Name_Op_Eq
3373 and then Is_Dispatching_Operation (Id)
3374 and then Present (Alias (Id))
3375 and then Present (Overridden_Operation (Alias (Id)))
3376 and then Base_Type (Etype (First_Entity (Id))) =
3377 Base_Type (Etype (First_Entity (Alias (Id))))
3378 then
3379 null;
3380
3381 -- Include the subprogram in the list of primitives
3382
3383 else
3384 Append_Elmt (Id, Op_List);
3385 end if;
3386 end if;
3387 end if;
3388
3389 Next_Entity (Id);
3390
3391 -- For a type declared in System, some of its operations may
3392 -- appear in the target-specific extension to System.
3393
3394 if No (Id)
3395 and then B_Scope = RTU_Entity (System)
3396 and then Present_System_Aux
3397 then
3398 B_Scope := System_Aux_Id;
3399 Id := First_Entity (System_Aux_Id);
3400 end if;
3401 end loop;
3402 end if;
3403
3404 return Op_List;
3405 end Collect_Primitive_Operations;
3406
3407 -----------------------------------
3408 -- Compile_Time_Constraint_Error --
3409 -----------------------------------
3410
3411 function Compile_Time_Constraint_Error
3412 (N : Node_Id;
3413 Msg : String;
3414 Ent : Entity_Id := Empty;
3415 Loc : Source_Ptr := No_Location;
3416 Warn : Boolean := False) return Node_Id
3417 is
3418 Msgc : String (1 .. Msg'Length + 3);
3419 -- Copy of message, with room for possible ?? or << and ! at end
3420
3421 Msgl : Natural;
3422 Wmsg : Boolean;
3423 P : Node_Id;
3424 OldP : Node_Id;
3425 Msgs : Boolean;
3426 Eloc : Source_Ptr;
3427
3428 begin
3429 -- If this is a warning, convert it into an error if we are in code
3430 -- subject to SPARK_Mode being set ON.
3431
3432 Error_Msg_Warn := SPARK_Mode /= On;
3433
3434 -- A static constraint error in an instance body is not a fatal error.
3435 -- we choose to inhibit the message altogether, because there is no
3436 -- obvious node (for now) on which to post it. On the other hand the
3437 -- offending node must be replaced with a constraint_error in any case.
3438
3439 -- No messages are generated if we already posted an error on this node
3440
3441 if not Error_Posted (N) then
3442 if Loc /= No_Location then
3443 Eloc := Loc;
3444 else
3445 Eloc := Sloc (N);
3446 end if;
3447
3448 -- Copy message to Msgc, converting any ? in the message into
3449 -- < instead, so that we have an error in GNATprove mode.
3450
3451 Msgl := Msg'Length;
3452
3453 for J in 1 .. Msgl loop
3454 if Msg (J) = '?' and then (J = 1 or else Msg (J) /= ''') then
3455 Msgc (J) := '<';
3456 else
3457 Msgc (J) := Msg (J);
3458 end if;
3459 end loop;
3460
3461 -- Message is a warning, even in Ada 95 case
3462
3463 if Msg (Msg'Last) = '?' or else Msg (Msg'Last) = '<' then
3464 Wmsg := True;
3465
3466 -- In Ada 83, all messages are warnings. In the private part and
3467 -- the body of an instance, constraint_checks are only warnings.
3468 -- We also make this a warning if the Warn parameter is set.
3469
3470 elsif Warn
3471 or else (Ada_Version = Ada_83 and then Comes_From_Source (N))
3472 then
3473 Msgl := Msgl + 1;
3474 Msgc (Msgl) := '<';
3475 Msgl := Msgl + 1;
3476 Msgc (Msgl) := '<';
3477 Wmsg := True;
3478
3479 elsif In_Instance_Not_Visible then
3480 Msgl := Msgl + 1;
3481 Msgc (Msgl) := '<';
3482 Msgl := Msgl + 1;
3483 Msgc (Msgl) := '<';
3484 Wmsg := True;
3485
3486 -- Otherwise we have a real error message (Ada 95 static case)
3487 -- and we make this an unconditional message. Note that in the
3488 -- warning case we do not make the message unconditional, it seems
3489 -- quite reasonable to delete messages like this (about exceptions
3490 -- that will be raised) in dead code.
3491
3492 else
3493 Wmsg := False;
3494 Msgl := Msgl + 1;
3495 Msgc (Msgl) := '!';
3496 end if;
3497
3498 -- Should we generate a warning? The answer is not quite yes. The
3499 -- very annoying exception occurs in the case of a short circuit
3500 -- operator where the left operand is static and decisive. Climb
3501 -- parents to see if that is the case we have here. Conditional
3502 -- expressions with decisive conditions are a similar situation.
3503
3504 Msgs := True;
3505 P := N;
3506 loop
3507 OldP := P;
3508 P := Parent (P);
3509
3510 -- And then with False as left operand
3511
3512 if Nkind (P) = N_And_Then
3513 and then Compile_Time_Known_Value (Left_Opnd (P))
3514 and then Is_False (Expr_Value (Left_Opnd (P)))
3515 then
3516 Msgs := False;
3517 exit;
3518
3519 -- OR ELSE with True as left operand
3520
3521 elsif Nkind (P) = N_Or_Else
3522 and then Compile_Time_Known_Value (Left_Opnd (P))
3523 and then Is_True (Expr_Value (Left_Opnd (P)))
3524 then
3525 Msgs := False;
3526 exit;
3527
3528 -- If expression
3529
3530 elsif Nkind (P) = N_If_Expression then
3531 declare
3532 Cond : constant Node_Id := First (Expressions (P));
3533 Texp : constant Node_Id := Next (Cond);
3534 Fexp : constant Node_Id := Next (Texp);
3535
3536 begin
3537 if Compile_Time_Known_Value (Cond) then
3538
3539 -- Condition is True and we are in the right operand
3540
3541 if Is_True (Expr_Value (Cond))
3542 and then OldP = Fexp
3543 then
3544 Msgs := False;
3545 exit;
3546
3547 -- Condition is False and we are in the left operand
3548
3549 elsif Is_False (Expr_Value (Cond))
3550 and then OldP = Texp
3551 then
3552 Msgs := False;
3553 exit;
3554 end if;
3555 end if;
3556 end;
3557
3558 -- Special case for component association in aggregates, where
3559 -- we want to keep climbing up to the parent aggregate.
3560
3561 elsif Nkind (P) = N_Component_Association
3562 and then Nkind (Parent (P)) = N_Aggregate
3563 then
3564 null;
3565
3566 -- Keep going if within subexpression
3567
3568 else
3569 exit when Nkind (P) not in N_Subexpr;
3570 end if;
3571 end loop;
3572
3573 if Msgs then
3574 Error_Msg_Warn := SPARK_Mode /= On;
3575
3576 if Present (Ent) then
3577 Error_Msg_NEL (Msgc (1 .. Msgl), N, Ent, Eloc);
3578 else
3579 Error_Msg_NEL (Msgc (1 .. Msgl), N, Etype (N), Eloc);
3580 end if;
3581
3582 if Wmsg then
3583
3584 -- Check whether the context is an Init_Proc
3585
3586 if Inside_Init_Proc then
3587 declare
3588 Conc_Typ : constant Entity_Id :=
3589 Corresponding_Concurrent_Type
3590 (Entity (Parameter_Type (First
3591 (Parameter_Specifications
3592 (Parent (Current_Scope))))));
3593
3594 begin
3595 -- Don't complain if the corresponding concurrent type
3596 -- doesn't come from source (i.e. a single task/protected
3597 -- object).
3598
3599 if Present (Conc_Typ)
3600 and then not Comes_From_Source (Conc_Typ)
3601 then
3602 Error_Msg_NEL
3603 ("\& [<<", N, Standard_Constraint_Error, Eloc);
3604
3605 else
3606 if GNATprove_Mode then
3607 Error_Msg_NEL
3608 ("\& would have been raised for objects of this "
3609 & "type", N, Standard_Constraint_Error, Eloc);
3610 else
3611 Error_Msg_NEL
3612 ("\& will be raised for objects of this type??",
3613 N, Standard_Constraint_Error, Eloc);
3614 end if;
3615 end if;
3616 end;
3617
3618 else
3619 Error_Msg_NEL ("\& [<<", N, Standard_Constraint_Error, Eloc);
3620 end if;
3621
3622 else
3623 Error_Msg ("\static expression fails Constraint_Check", Eloc);
3624 Set_Error_Posted (N);
3625 end if;
3626 end if;
3627 end if;
3628
3629 return N;
3630 end Compile_Time_Constraint_Error;
3631
3632 -----------------------
3633 -- Conditional_Delay --
3634 -----------------------
3635
3636 procedure Conditional_Delay (New_Ent, Old_Ent : Entity_Id) is
3637 begin
3638 if Has_Delayed_Freeze (Old_Ent) and then not Is_Frozen (Old_Ent) then
3639 Set_Has_Delayed_Freeze (New_Ent);
3640 end if;
3641 end Conditional_Delay;
3642
3643 ----------------------------
3644 -- Contains_Refined_State --
3645 ----------------------------
3646
3647 function Contains_Refined_State (Prag : Node_Id) return Boolean is
3648 function Has_State_In_Dependency (List : Node_Id) return Boolean;
3649 -- Determine whether a dependency list mentions a state with a visible
3650 -- refinement.
3651
3652 function Has_State_In_Global (List : Node_Id) return Boolean;
3653 -- Determine whether a global list mentions a state with a visible
3654 -- refinement.
3655
3656 function Is_Refined_State (Item : Node_Id) return Boolean;
3657 -- Determine whether Item is a reference to an abstract state with a
3658 -- visible refinement.
3659
3660 -----------------------------
3661 -- Has_State_In_Dependency --
3662 -----------------------------
3663
3664 function Has_State_In_Dependency (List : Node_Id) return Boolean is
3665 Clause : Node_Id;
3666 Output : Node_Id;
3667
3668 begin
3669 -- A null dependency list does not mention any states
3670
3671 if Nkind (List) = N_Null then
3672 return False;
3673
3674 -- Dependency clauses appear as component associations of an
3675 -- aggregate.
3676
3677 elsif Nkind (List) = N_Aggregate
3678 and then Present (Component_Associations (List))
3679 then
3680 Clause := First (Component_Associations (List));
3681 while Present (Clause) loop
3682
3683 -- Inspect the outputs of a dependency clause
3684
3685 Output := First (Choices (Clause));
3686 while Present (Output) loop
3687 if Is_Refined_State (Output) then
3688 return True;
3689 end if;
3690
3691 Next (Output);
3692 end loop;
3693
3694 -- Inspect the outputs of a dependency clause
3695
3696 if Is_Refined_State (Expression (Clause)) then
3697 return True;
3698 end if;
3699
3700 Next (Clause);
3701 end loop;
3702
3703 -- If we get here, then none of the dependency clauses mention a
3704 -- state with visible refinement.
3705
3706 return False;
3707
3708 -- An illegal pragma managed to sneak in
3709
3710 else
3711 raise Program_Error;
3712 end if;
3713 end Has_State_In_Dependency;
3714
3715 -------------------------
3716 -- Has_State_In_Global --
3717 -------------------------
3718
3719 function Has_State_In_Global (List : Node_Id) return Boolean is
3720 Item : Node_Id;
3721
3722 begin
3723 -- A null global list does not mention any states
3724
3725 if Nkind (List) = N_Null then
3726 return False;
3727
3728 -- Simple global list or moded global list declaration
3729
3730 elsif Nkind (List) = N_Aggregate then
3731
3732 -- The declaration of a simple global list appear as a collection
3733 -- of expressions.
3734
3735 if Present (Expressions (List)) then
3736 Item := First (Expressions (List));
3737 while Present (Item) loop
3738 if Is_Refined_State (Item) then
3739 return True;
3740 end if;
3741
3742 Next (Item);
3743 end loop;
3744
3745 -- The declaration of a moded global list appears as a collection
3746 -- of component associations where individual choices denote
3747 -- modes.
3748
3749 else
3750 Item := First (Component_Associations (List));
3751 while Present (Item) loop
3752 if Has_State_In_Global (Expression (Item)) then
3753 return True;
3754 end if;
3755
3756 Next (Item);
3757 end loop;
3758 end if;
3759
3760 -- If we get here, then the simple/moded global list did not
3761 -- mention any states with a visible refinement.
3762
3763 return False;
3764
3765 -- Single global item declaration
3766
3767 elsif Is_Entity_Name (List) then
3768 return Is_Refined_State (List);
3769
3770 -- An illegal pragma managed to sneak in
3771
3772 else
3773 raise Program_Error;
3774 end if;
3775 end Has_State_In_Global;
3776
3777 ----------------------
3778 -- Is_Refined_State --
3779 ----------------------
3780
3781 function Is_Refined_State (Item : Node_Id) return Boolean is
3782 Elmt : Node_Id;
3783 Item_Id : Entity_Id;
3784
3785 begin
3786 if Nkind (Item) = N_Null then
3787 return False;
3788
3789 -- States cannot be subject to attribute 'Result. This case arises
3790 -- in dependency relations.
3791
3792 elsif Nkind (Item) = N_Attribute_Reference
3793 and then Attribute_Name (Item) = Name_Result
3794 then
3795 return False;
3796
3797 -- Multiple items appear as an aggregate. This case arises in
3798 -- dependency relations.
3799
3800 elsif Nkind (Item) = N_Aggregate
3801 and then Present (Expressions (Item))
3802 then
3803 Elmt := First (Expressions (Item));
3804 while Present (Elmt) loop
3805 if Is_Refined_State (Elmt) then
3806 return True;
3807 end if;
3808
3809 Next (Elmt);
3810 end loop;
3811
3812 -- If we get here, then none of the inputs or outputs reference a
3813 -- state with visible refinement.
3814
3815 return False;
3816
3817 -- Single item
3818
3819 else
3820 Item_Id := Entity_Of (Item);
3821
3822 return
3823 Present (Item_Id)
3824 and then Ekind (Item_Id) = E_Abstract_State
3825 and then Has_Visible_Refinement (Item_Id);
3826 end if;
3827 end Is_Refined_State;
3828
3829 -- Local variables
3830
3831 Arg : constant Node_Id :=
3832 Get_Pragma_Arg (First (Pragma_Argument_Associations (Prag)));
3833 Nam : constant Name_Id := Pragma_Name (Prag);
3834
3835 -- Start of processing for Contains_Refined_State
3836
3837 begin
3838 if Nam = Name_Depends then
3839 return Has_State_In_Dependency (Arg);
3840
3841 else pragma Assert (Nam = Name_Global);
3842 return Has_State_In_Global (Arg);
3843 end if;
3844 end Contains_Refined_State;
3845
3846 -------------------------
3847 -- Copy_Component_List --
3848 -------------------------
3849
3850 function Copy_Component_List
3851 (R_Typ : Entity_Id;
3852 Loc : Source_Ptr) return List_Id
3853 is
3854 Comp : Node_Id;
3855 Comps : constant List_Id := New_List;
3856
3857 begin
3858 Comp := First_Component (Underlying_Type (R_Typ));
3859 while Present (Comp) loop
3860 if Comes_From_Source (Comp) then
3861 declare
3862 Comp_Decl : constant Node_Id := Declaration_Node (Comp);
3863 begin
3864 Append_To (Comps,
3865 Make_Component_Declaration (Loc,
3866 Defining_Identifier =>
3867 Make_Defining_Identifier (Loc, Chars (Comp)),
3868 Component_Definition =>
3869 New_Copy_Tree
3870 (Component_Definition (Comp_Decl), New_Sloc => Loc)));
3871 end;
3872 end if;
3873
3874 Next_Component (Comp);
3875 end loop;
3876
3877 return Comps;
3878 end Copy_Component_List;
3879
3880 -------------------------
3881 -- Copy_Parameter_List --
3882 -------------------------
3883
3884 function Copy_Parameter_List (Subp_Id : Entity_Id) return List_Id is
3885 Loc : constant Source_Ptr := Sloc (Subp_Id);
3886 Plist : List_Id;
3887 Formal : Entity_Id;
3888
3889 begin
3890 if No (First_Formal (Subp_Id)) then
3891 return No_List;
3892 else
3893 Plist := New_List;
3894 Formal := First_Formal (Subp_Id);
3895 while Present (Formal) loop
3896 Append
3897 (Make_Parameter_Specification (Loc,
3898 Defining_Identifier =>
3899 Make_Defining_Identifier (Sloc (Formal),
3900 Chars => Chars (Formal)),
3901 In_Present => In_Present (Parent (Formal)),
3902 Out_Present => Out_Present (Parent (Formal)),
3903 Parameter_Type =>
3904 New_Occurrence_Of (Etype (Formal), Loc),
3905 Expression =>
3906 New_Copy_Tree (Expression (Parent (Formal)))),
3907 Plist);
3908
3909 Next_Formal (Formal);
3910 end loop;
3911 end if;
3912
3913 return Plist;
3914 end Copy_Parameter_List;
3915
3916 --------------------------------
3917 -- Corresponding_Generic_Type --
3918 --------------------------------
3919
3920 function Corresponding_Generic_Type (T : Entity_Id) return Entity_Id is
3921 Inst : Entity_Id;
3922 Gen : Entity_Id;
3923 Typ : Entity_Id;
3924
3925 begin
3926 if not Is_Generic_Actual_Type (T) then
3927 return Any_Type;
3928
3929 -- If the actual is the actual of an enclosing instance, resolution
3930 -- was correct in the generic.
3931
3932 elsif Nkind (Parent (T)) = N_Subtype_Declaration
3933 and then Is_Entity_Name (Subtype_Indication (Parent (T)))
3934 and then
3935 Is_Generic_Actual_Type (Entity (Subtype_Indication (Parent (T))))
3936 then
3937 return Any_Type;
3938
3939 else
3940 Inst := Scope (T);
3941
3942 if Is_Wrapper_Package (Inst) then
3943 Inst := Related_Instance (Inst);
3944 end if;
3945
3946 Gen :=
3947 Generic_Parent
3948 (Specification (Unit_Declaration_Node (Inst)));
3949
3950 -- Generic actual has the same name as the corresponding formal
3951
3952 Typ := First_Entity (Gen);
3953 while Present (Typ) loop
3954 if Chars (Typ) = Chars (T) then
3955 return Typ;
3956 end if;
3957
3958 Next_Entity (Typ);
3959 end loop;
3960
3961 return Any_Type;
3962 end if;
3963 end Corresponding_Generic_Type;
3964
3965 --------------------
3966 -- Current_Entity --
3967 --------------------
3968
3969 -- The currently visible definition for a given identifier is the
3970 -- one most chained at the start of the visibility chain, i.e. the
3971 -- one that is referenced by the Node_Id value of the name of the
3972 -- given identifier.
3973
3974 function Current_Entity (N : Node_Id) return Entity_Id is
3975 begin
3976 return Get_Name_Entity_Id (Chars (N));
3977 end Current_Entity;
3978
3979 -----------------------------
3980 -- Current_Entity_In_Scope --
3981 -----------------------------
3982
3983 function Current_Entity_In_Scope (N : Node_Id) return Entity_Id is
3984 E : Entity_Id;
3985 CS : constant Entity_Id := Current_Scope;
3986
3987 Transient_Case : constant Boolean := Scope_Is_Transient;
3988
3989 begin
3990 E := Get_Name_Entity_Id (Chars (N));
3991 while Present (E)
3992 and then Scope (E) /= CS
3993 and then (not Transient_Case or else Scope (E) /= Scope (CS))
3994 loop
3995 E := Homonym (E);
3996 end loop;
3997
3998 return E;
3999 end Current_Entity_In_Scope;
4000
4001 -------------------
4002 -- Current_Scope --
4003 -------------------
4004
4005 function Current_Scope return Entity_Id is
4006 begin
4007 if Scope_Stack.Last = -1 then
4008 return Standard_Standard;
4009 else
4010 declare
4011 C : constant Entity_Id :=
4012 Scope_Stack.Table (Scope_Stack.Last).Entity;
4013 begin
4014 if Present (C) then
4015 return C;
4016 else
4017 return Standard_Standard;
4018 end if;
4019 end;
4020 end if;
4021 end Current_Scope;
4022
4023 ------------------------
4024 -- Current_Subprogram --
4025 ------------------------
4026
4027 function Current_Subprogram return Entity_Id is
4028 Scop : constant Entity_Id := Current_Scope;
4029 begin
4030 if Is_Subprogram (Scop) or else Is_Generic_Subprogram (Scop) then
4031 return Scop;
4032 else
4033 return Enclosing_Subprogram (Scop);
4034 end if;
4035 end Current_Subprogram;
4036
4037 ----------------------------------
4038 -- Deepest_Type_Access_Level --
4039 ----------------------------------
4040
4041 function Deepest_Type_Access_Level (Typ : Entity_Id) return Uint is
4042 begin
4043 if Ekind (Typ) = E_Anonymous_Access_Type
4044 and then not Is_Local_Anonymous_Access (Typ)
4045 and then Nkind (Associated_Node_For_Itype (Typ)) = N_Object_Declaration
4046 then
4047 -- Typ is the type of an Ada 2012 stand-alone object of an anonymous
4048 -- access type.
4049
4050 return
4051 Scope_Depth (Enclosing_Dynamic_Scope
4052 (Defining_Identifier
4053 (Associated_Node_For_Itype (Typ))));
4054
4055 -- For generic formal type, return Int'Last (infinite).
4056 -- See comment preceding Is_Generic_Type call in Type_Access_Level.
4057
4058 elsif Is_Generic_Type (Root_Type (Typ)) then
4059 return UI_From_Int (Int'Last);
4060
4061 else
4062 return Type_Access_Level (Typ);
4063 end if;
4064 end Deepest_Type_Access_Level;
4065
4066 ---------------------
4067 -- Defining_Entity --
4068 ---------------------
4069
4070 function Defining_Entity (N : Node_Id) return Entity_Id is
4071 K : constant Node_Kind := Nkind (N);
4072 Err : Entity_Id := Empty;
4073
4074 begin
4075 case K is
4076 when
4077 N_Subprogram_Declaration |
4078 N_Abstract_Subprogram_Declaration |
4079 N_Subprogram_Body |
4080 N_Package_Declaration |
4081 N_Subprogram_Renaming_Declaration |
4082 N_Subprogram_Body_Stub |
4083 N_Generic_Subprogram_Declaration |
4084 N_Generic_Package_Declaration |
4085 N_Formal_Subprogram_Declaration |
4086 N_Expression_Function
4087 =>
4088 return Defining_Entity (Specification (N));
4089
4090 when
4091 N_Component_Declaration |
4092 N_Defining_Program_Unit_Name |
4093 N_Discriminant_Specification |
4094 N_Entry_Body |
4095 N_Entry_Declaration |
4096 N_Entry_Index_Specification |
4097 N_Exception_Declaration |
4098 N_Exception_Renaming_Declaration |
4099 N_Formal_Object_Declaration |
4100 N_Formal_Package_Declaration |
4101 N_Formal_Type_Declaration |
4102 N_Full_Type_Declaration |
4103 N_Implicit_Label_Declaration |
4104 N_Incomplete_Type_Declaration |
4105 N_Loop_Parameter_Specification |
4106 N_Number_Declaration |
4107 N_Object_Declaration |
4108 N_Object_Renaming_Declaration |
4109 N_Package_Body_Stub |
4110 N_Parameter_Specification |
4111 N_Private_Extension_Declaration |
4112 N_Private_Type_Declaration |
4113 N_Protected_Body |
4114 N_Protected_Body_Stub |
4115 N_Protected_Type_Declaration |
4116 N_Single_Protected_Declaration |
4117 N_Single_Task_Declaration |
4118 N_Subtype_Declaration |
4119 N_Task_Body |
4120 N_Task_Body_Stub |
4121 N_Task_Type_Declaration
4122 =>
4123 return Defining_Identifier (N);
4124
4125 when N_Subunit =>
4126 return Defining_Entity (Proper_Body (N));
4127
4128 when
4129 N_Function_Instantiation |
4130 N_Function_Specification |
4131 N_Generic_Function_Renaming_Declaration |
4132 N_Generic_Package_Renaming_Declaration |
4133 N_Generic_Procedure_Renaming_Declaration |
4134 N_Package_Body |
4135 N_Package_Instantiation |
4136 N_Package_Renaming_Declaration |
4137 N_Package_Specification |
4138 N_Procedure_Instantiation |
4139 N_Procedure_Specification
4140 =>
4141 declare
4142 Nam : constant Node_Id := Defining_Unit_Name (N);
4143
4144 begin
4145 if Nkind (Nam) in N_Entity then
4146 return Nam;
4147
4148 -- For Error, make up a name and attach to declaration
4149 -- so we can continue semantic analysis
4150
4151 elsif Nam = Error then
4152 Err := Make_Temporary (Sloc (N), 'T');
4153 Set_Defining_Unit_Name (N, Err);
4154
4155 return Err;
4156
4157 -- If not an entity, get defining identifier
4158
4159 else
4160 return Defining_Identifier (Nam);
4161 end if;
4162 end;
4163
4164 when N_Block_Statement =>
4165 return Entity (Identifier (N));
4166
4167 when others =>
4168 raise Program_Error;
4169
4170 end case;
4171 end Defining_Entity;
4172
4173 --------------------------
4174 -- Denotes_Discriminant --
4175 --------------------------
4176
4177 function Denotes_Discriminant
4178 (N : Node_Id;
4179 Check_Concurrent : Boolean := False) return Boolean
4180 is
4181 E : Entity_Id;
4182 begin
4183 if not Is_Entity_Name (N)
4184 or else No (Entity (N))
4185 then
4186 return False;
4187 else
4188 E := Entity (N);
4189 end if;
4190
4191 -- If we are checking for a protected type, the discriminant may have
4192 -- been rewritten as the corresponding discriminal of the original type
4193 -- or of the corresponding concurrent record, depending on whether we
4194 -- are in the spec or body of the protected type.
4195
4196 return Ekind (E) = E_Discriminant
4197 or else
4198 (Check_Concurrent
4199 and then Ekind (E) = E_In_Parameter
4200 and then Present (Discriminal_Link (E))
4201 and then
4202 (Is_Concurrent_Type (Scope (Discriminal_Link (E)))
4203 or else
4204 Is_Concurrent_Record_Type (Scope (Discriminal_Link (E)))));
4205
4206 end Denotes_Discriminant;
4207
4208 -------------------------
4209 -- Denotes_Same_Object --
4210 -------------------------
4211
4212 function Denotes_Same_Object (A1, A2 : Node_Id) return Boolean is
4213 Obj1 : Node_Id := A1;
4214 Obj2 : Node_Id := A2;
4215
4216 function Has_Prefix (N : Node_Id) return Boolean;
4217 -- Return True if N has attribute Prefix
4218
4219 function Is_Renaming (N : Node_Id) return Boolean;
4220 -- Return true if N names a renaming entity
4221
4222 function Is_Valid_Renaming (N : Node_Id) return Boolean;
4223 -- For renamings, return False if the prefix of any dereference within
4224 -- the renamed object_name is a variable, or any expression within the
4225 -- renamed object_name contains references to variables or calls on
4226 -- nonstatic functions; otherwise return True (RM 6.4.1(6.10/3))
4227
4228 ----------------
4229 -- Has_Prefix --
4230 ----------------
4231
4232 function Has_Prefix (N : Node_Id) return Boolean is
4233 begin
4234 return
4235 Nkind_In (N,
4236 N_Attribute_Reference,
4237 N_Expanded_Name,
4238 N_Explicit_Dereference,
4239 N_Indexed_Component,
4240 N_Reference,
4241 N_Selected_Component,
4242 N_Slice);
4243 end Has_Prefix;
4244
4245 -----------------
4246 -- Is_Renaming --
4247 -----------------
4248
4249 function Is_Renaming (N : Node_Id) return Boolean is
4250 begin
4251 return Is_Entity_Name (N)
4252 and then Present (Renamed_Entity (Entity (N)));
4253 end Is_Renaming;
4254
4255 -----------------------
4256 -- Is_Valid_Renaming --
4257 -----------------------
4258
4259 function Is_Valid_Renaming (N : Node_Id) return Boolean is
4260
4261 function Check_Renaming (N : Node_Id) return Boolean;
4262 -- Recursive function used to traverse all the prefixes of N
4263
4264 function Check_Renaming (N : Node_Id) return Boolean is
4265 begin
4266 if Is_Renaming (N)
4267 and then not Check_Renaming (Renamed_Entity (Entity (N)))
4268 then
4269 return False;
4270 end if;
4271
4272 if Nkind (N) = N_Indexed_Component then
4273 declare
4274 Indx : Node_Id;
4275
4276 begin
4277 Indx := First (Expressions (N));
4278 while Present (Indx) loop
4279 if not Is_OK_Static_Expression (Indx) then
4280 return False;
4281 end if;
4282
4283 Next_Index (Indx);
4284 end loop;
4285 end;
4286 end if;
4287
4288 if Has_Prefix (N) then
4289 declare
4290 P : constant Node_Id := Prefix (N);
4291
4292 begin
4293 if Nkind (N) = N_Explicit_Dereference
4294 and then Is_Variable (P)
4295 then
4296 return False;
4297
4298 elsif Is_Entity_Name (P)
4299 and then Ekind (Entity (P)) = E_Function
4300 then
4301 return False;
4302
4303 elsif Nkind (P) = N_Function_Call then
4304 return False;
4305 end if;
4306
4307 -- Recursion to continue traversing the prefix of the
4308 -- renaming expression
4309
4310 return Check_Renaming (P);
4311 end;
4312 end if;
4313
4314 return True;
4315 end Check_Renaming;
4316
4317 -- Start of processing for Is_Valid_Renaming
4318
4319 begin
4320 return Check_Renaming (N);
4321 end Is_Valid_Renaming;
4322
4323 -- Start of processing for Denotes_Same_Object
4324
4325 begin
4326 -- Both names statically denote the same stand-alone object or parameter
4327 -- (RM 6.4.1(6.5/3))
4328
4329 if Is_Entity_Name (Obj1)
4330 and then Is_Entity_Name (Obj2)
4331 and then Entity (Obj1) = Entity (Obj2)
4332 then
4333 return True;
4334 end if;
4335
4336 -- For renamings, the prefix of any dereference within the renamed
4337 -- object_name is not a variable, and any expression within the
4338 -- renamed object_name contains no references to variables nor
4339 -- calls on nonstatic functions (RM 6.4.1(6.10/3)).
4340
4341 if Is_Renaming (Obj1) then
4342 if Is_Valid_Renaming (Obj1) then
4343 Obj1 := Renamed_Entity (Entity (Obj1));
4344 else
4345 return False;
4346 end if;
4347 end if;
4348
4349 if Is_Renaming (Obj2) then
4350 if Is_Valid_Renaming (Obj2) then
4351 Obj2 := Renamed_Entity (Entity (Obj2));
4352 else
4353 return False;
4354 end if;
4355 end if;
4356
4357 -- No match if not same node kind (such cases are handled by
4358 -- Denotes_Same_Prefix)
4359
4360 if Nkind (Obj1) /= Nkind (Obj2) then
4361 return False;
4362
4363 -- After handling valid renamings, one of the two names statically
4364 -- denoted a renaming declaration whose renamed object_name is known
4365 -- to denote the same object as the other (RM 6.4.1(6.10/3))
4366
4367 elsif Is_Entity_Name (Obj1) then
4368 if Is_Entity_Name (Obj2) then
4369 return Entity (Obj1) = Entity (Obj2);
4370 else
4371 return False;
4372 end if;
4373
4374 -- Both names are selected_components, their prefixes are known to
4375 -- denote the same object, and their selector_names denote the same
4376 -- component (RM 6.4.1(6.6/3)
4377
4378 elsif Nkind (Obj1) = N_Selected_Component then
4379 return Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2))
4380 and then
4381 Entity (Selector_Name (Obj1)) = Entity (Selector_Name (Obj2));
4382
4383 -- Both names are dereferences and the dereferenced names are known to
4384 -- denote the same object (RM 6.4.1(6.7/3))
4385
4386 elsif Nkind (Obj1) = N_Explicit_Dereference then
4387 return Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2));
4388
4389 -- Both names are indexed_components, their prefixes are known to denote
4390 -- the same object, and each of the pairs of corresponding index values
4391 -- are either both static expressions with the same static value or both
4392 -- names that are known to denote the same object (RM 6.4.1(6.8/3))
4393
4394 elsif Nkind (Obj1) = N_Indexed_Component then
4395 if not Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2)) then
4396 return False;
4397 else
4398 declare
4399 Indx1 : Node_Id;
4400 Indx2 : Node_Id;
4401
4402 begin
4403 Indx1 := First (Expressions (Obj1));
4404 Indx2 := First (Expressions (Obj2));
4405 while Present (Indx1) loop
4406
4407 -- Indexes must denote the same static value or same object
4408
4409 if Is_OK_Static_Expression (Indx1) then
4410 if not Is_OK_Static_Expression (Indx2) then
4411 return False;
4412
4413 elsif Expr_Value (Indx1) /= Expr_Value (Indx2) then
4414 return False;
4415 end if;
4416
4417 elsif not Denotes_Same_Object (Indx1, Indx2) then
4418 return False;
4419 end if;
4420
4421 Next (Indx1);
4422 Next (Indx2);
4423 end loop;
4424
4425 return True;
4426 end;
4427 end if;
4428
4429 -- Both names are slices, their prefixes are known to denote the same
4430 -- object, and the two slices have statically matching index constraints
4431 -- (RM 6.4.1(6.9/3))
4432
4433 elsif Nkind (Obj1) = N_Slice
4434 and then Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2))
4435 then
4436 declare
4437 Lo1, Lo2, Hi1, Hi2 : Node_Id;
4438
4439 begin
4440 Get_Index_Bounds (Etype (Obj1), Lo1, Hi1);
4441 Get_Index_Bounds (Etype (Obj2), Lo2, Hi2);
4442
4443 -- Check whether bounds are statically identical. There is no
4444 -- attempt to detect partial overlap of slices.
4445
4446 return Denotes_Same_Object (Lo1, Lo2)
4447 and then Denotes_Same_Object (Hi1, Hi2);
4448 end;
4449
4450 -- In the recursion, literals appear as indexes.
4451
4452 elsif Nkind (Obj1) = N_Integer_Literal
4453 and then Nkind (Obj2) = N_Integer_Literal
4454 then
4455 return Intval (Obj1) = Intval (Obj2);
4456
4457 else
4458 return False;
4459 end if;
4460 end Denotes_Same_Object;
4461
4462 -------------------------
4463 -- Denotes_Same_Prefix --
4464 -------------------------
4465
4466 function Denotes_Same_Prefix (A1, A2 : Node_Id) return Boolean is
4467
4468 begin
4469 if Is_Entity_Name (A1) then
4470 if Nkind_In (A2, N_Selected_Component, N_Indexed_Component)
4471 and then not Is_Access_Type (Etype (A1))
4472 then
4473 return Denotes_Same_Object (A1, Prefix (A2))
4474 or else Denotes_Same_Prefix (A1, Prefix (A2));
4475 else
4476 return False;
4477 end if;
4478
4479 elsif Is_Entity_Name (A2) then
4480 return Denotes_Same_Prefix (A1 => A2, A2 => A1);
4481
4482 elsif Nkind_In (A1, N_Selected_Component, N_Indexed_Component, N_Slice)
4483 and then
4484 Nkind_In (A2, N_Selected_Component, N_Indexed_Component, N_Slice)
4485 then
4486 declare
4487 Root1, Root2 : Node_Id;
4488 Depth1, Depth2 : Int := 0;
4489
4490 begin
4491 Root1 := Prefix (A1);
4492 while not Is_Entity_Name (Root1) loop
4493 if not Nkind_In
4494 (Root1, N_Selected_Component, N_Indexed_Component)
4495 then
4496 return False;
4497 else
4498 Root1 := Prefix (Root1);
4499 end if;
4500
4501 Depth1 := Depth1 + 1;
4502 end loop;
4503
4504 Root2 := Prefix (A2);
4505 while not Is_Entity_Name (Root2) loop
4506 if not Nkind_In
4507 (Root2, N_Selected_Component, N_Indexed_Component)
4508 then
4509 return False;
4510 else
4511 Root2 := Prefix (Root2);
4512 end if;
4513
4514 Depth2 := Depth2 + 1;
4515 end loop;
4516
4517 -- If both have the same depth and they do not denote the same
4518 -- object, they are disjoint and no warning is needed.
4519
4520 if Depth1 = Depth2 then
4521 return False;
4522
4523 elsif Depth1 > Depth2 then
4524 Root1 := Prefix (A1);
4525 for I in 1 .. Depth1 - Depth2 - 1 loop
4526 Root1 := Prefix (Root1);
4527 end loop;
4528
4529 return Denotes_Same_Object (Root1, A2);
4530
4531 else
4532 Root2 := Prefix (A2);
4533 for I in 1 .. Depth2 - Depth1 - 1 loop
4534 Root2 := Prefix (Root2);
4535 end loop;
4536
4537 return Denotes_Same_Object (A1, Root2);
4538 end if;
4539 end;
4540
4541 else
4542 return False;
4543 end if;
4544 end Denotes_Same_Prefix;
4545
4546 ----------------------
4547 -- Denotes_Variable --
4548 ----------------------
4549
4550 function Denotes_Variable (N : Node_Id) return Boolean is
4551 begin
4552 return Is_Variable (N) and then Paren_Count (N) = 0;
4553 end Denotes_Variable;
4554
4555 -----------------------------
4556 -- Depends_On_Discriminant --
4557 -----------------------------
4558
4559 function Depends_On_Discriminant (N : Node_Id) return Boolean is
4560 L : Node_Id;
4561 H : Node_Id;
4562
4563 begin
4564 Get_Index_Bounds (N, L, H);
4565 return Denotes_Discriminant (L) or else Denotes_Discriminant (H);
4566 end Depends_On_Discriminant;
4567
4568 -------------------------
4569 -- Designate_Same_Unit --
4570 -------------------------
4571
4572 function Designate_Same_Unit
4573 (Name1 : Node_Id;
4574 Name2 : Node_Id) return Boolean
4575 is
4576 K1 : constant Node_Kind := Nkind (Name1);
4577 K2 : constant Node_Kind := Nkind (Name2);
4578
4579 function Prefix_Node (N : Node_Id) return Node_Id;
4580 -- Returns the parent unit name node of a defining program unit name
4581 -- or the prefix if N is a selected component or an expanded name.
4582
4583 function Select_Node (N : Node_Id) return Node_Id;
4584 -- Returns the defining identifier node of a defining program unit
4585 -- name or the selector node if N is a selected component or an
4586 -- expanded name.
4587
4588 -----------------
4589 -- Prefix_Node --
4590 -----------------
4591
4592 function Prefix_Node (N : Node_Id) return Node_Id is
4593 begin
4594 if Nkind (N) = N_Defining_Program_Unit_Name then
4595 return Name (N);
4596
4597 else
4598 return Prefix (N);
4599 end if;
4600 end Prefix_Node;
4601
4602 -----------------
4603 -- Select_Node --
4604 -----------------
4605
4606 function Select_Node (N : Node_Id) return Node_Id is
4607 begin
4608 if Nkind (N) = N_Defining_Program_Unit_Name then
4609 return Defining_Identifier (N);
4610
4611 else
4612 return Selector_Name (N);
4613 end if;
4614 end Select_Node;
4615
4616 -- Start of processing for Designate_Next_Unit
4617
4618 begin
4619 if (K1 = N_Identifier or else
4620 K1 = N_Defining_Identifier)
4621 and then
4622 (K2 = N_Identifier or else
4623 K2 = N_Defining_Identifier)
4624 then
4625 return Chars (Name1) = Chars (Name2);
4626
4627 elsif
4628 (K1 = N_Expanded_Name or else
4629 K1 = N_Selected_Component or else
4630 K1 = N_Defining_Program_Unit_Name)
4631 and then
4632 (K2 = N_Expanded_Name or else
4633 K2 = N_Selected_Component or else
4634 K2 = N_Defining_Program_Unit_Name)
4635 then
4636 return
4637 (Chars (Select_Node (Name1)) = Chars (Select_Node (Name2)))
4638 and then
4639 Designate_Same_Unit (Prefix_Node (Name1), Prefix_Node (Name2));
4640
4641 else
4642 return False;
4643 end if;
4644 end Designate_Same_Unit;
4645
4646 ------------------------------------------
4647 -- function Dynamic_Accessibility_Level --
4648 ------------------------------------------
4649
4650 function Dynamic_Accessibility_Level (Expr : Node_Id) return Node_Id is
4651 E : Entity_Id;
4652 Loc : constant Source_Ptr := Sloc (Expr);
4653
4654 function Make_Level_Literal (Level : Uint) return Node_Id;
4655 -- Construct an integer literal representing an accessibility level
4656 -- with its type set to Natural.
4657
4658 ------------------------
4659 -- Make_Level_Literal --
4660 ------------------------
4661
4662 function Make_Level_Literal (Level : Uint) return Node_Id is
4663 Result : constant Node_Id := Make_Integer_Literal (Loc, Level);
4664 begin
4665 Set_Etype (Result, Standard_Natural);
4666 return Result;
4667 end Make_Level_Literal;
4668
4669 -- Start of processing for Dynamic_Accessibility_Level
4670
4671 begin
4672 if Is_Entity_Name (Expr) then
4673 E := Entity (Expr);
4674
4675 if Present (Renamed_Object (E)) then
4676 return Dynamic_Accessibility_Level (Renamed_Object (E));
4677 end if;
4678
4679 if Is_Formal (E) or else Ekind_In (E, E_Variable, E_Constant) then
4680 if Present (Extra_Accessibility (E)) then
4681 return New_Occurrence_Of (Extra_Accessibility (E), Loc);
4682 end if;
4683 end if;
4684 end if;
4685
4686 -- Unimplemented: Ptr.all'Access, where Ptr has Extra_Accessibility ???
4687
4688 case Nkind (Expr) is
4689
4690 -- For access discriminant, the level of the enclosing object
4691
4692 when N_Selected_Component =>
4693 if Ekind (Entity (Selector_Name (Expr))) = E_Discriminant
4694 and then Ekind (Etype (Entity (Selector_Name (Expr)))) =
4695 E_Anonymous_Access_Type
4696 then
4697 return Make_Level_Literal (Object_Access_Level (Expr));
4698 end if;
4699
4700 when N_Attribute_Reference =>
4701 case Get_Attribute_Id (Attribute_Name (Expr)) is
4702
4703 -- For X'Access, the level of the prefix X
4704
4705 when Attribute_Access =>
4706 return Make_Level_Literal
4707 (Object_Access_Level (Prefix (Expr)));
4708
4709 -- Treat the unchecked attributes as library-level
4710
4711 when Attribute_Unchecked_Access |
4712 Attribute_Unrestricted_Access =>
4713 return Make_Level_Literal (Scope_Depth (Standard_Standard));
4714
4715 -- No other access-valued attributes
4716
4717 when others =>
4718 raise Program_Error;
4719 end case;
4720
4721 when N_Allocator =>
4722
4723 -- Unimplemented: depends on context. As an actual parameter where
4724 -- formal type is anonymous, use
4725 -- Scope_Depth (Current_Scope) + 1.
4726 -- For other cases, see 3.10.2(14/3) and following. ???
4727
4728 null;
4729
4730 when N_Type_Conversion =>
4731 if not Is_Local_Anonymous_Access (Etype (Expr)) then
4732
4733 -- Handle type conversions introduced for a rename of an
4734 -- Ada 2012 stand-alone object of an anonymous access type.
4735
4736 return Dynamic_Accessibility_Level (Expression (Expr));
4737 end if;
4738
4739 when others =>
4740 null;
4741 end case;
4742
4743 return Make_Level_Literal (Type_Access_Level (Etype (Expr)));
4744 end Dynamic_Accessibility_Level;
4745
4746 -----------------------------------
4747 -- Effective_Extra_Accessibility --
4748 -----------------------------------
4749
4750 function Effective_Extra_Accessibility (Id : Entity_Id) return Entity_Id is
4751 begin
4752 if Present (Renamed_Object (Id))
4753 and then Is_Entity_Name (Renamed_Object (Id))
4754 then
4755 return Effective_Extra_Accessibility (Entity (Renamed_Object (Id)));
4756 else
4757 return Extra_Accessibility (Id);
4758 end if;
4759 end Effective_Extra_Accessibility;
4760
4761 -----------------------------
4762 -- Effective_Reads_Enabled --
4763 -----------------------------
4764
4765 function Effective_Reads_Enabled (Id : Entity_Id) return Boolean is
4766 begin
4767 return Has_Enabled_Property (Id, Name_Effective_Reads);
4768 end Effective_Reads_Enabled;
4769
4770 ------------------------------
4771 -- Effective_Writes_Enabled --
4772 ------------------------------
4773
4774 function Effective_Writes_Enabled (Id : Entity_Id) return Boolean is
4775 begin
4776 return Has_Enabled_Property (Id, Name_Effective_Writes);
4777 end Effective_Writes_Enabled;
4778
4779 ------------------------------
4780 -- Enclosing_Comp_Unit_Node --
4781 ------------------------------
4782
4783 function Enclosing_Comp_Unit_Node (N : Node_Id) return Node_Id is
4784 Current_Node : Node_Id;
4785
4786 begin
4787 Current_Node := N;
4788 while Present (Current_Node)
4789 and then Nkind (Current_Node) /= N_Compilation_Unit
4790 loop
4791 Current_Node := Parent (Current_Node);
4792 end loop;
4793
4794 if Nkind (Current_Node) /= N_Compilation_Unit then
4795 return Empty;
4796 else
4797 return Current_Node;
4798 end if;
4799 end Enclosing_Comp_Unit_Node;
4800
4801 --------------------------
4802 -- Enclosing_CPP_Parent --
4803 --------------------------
4804
4805 function Enclosing_CPP_Parent (Typ : Entity_Id) return Entity_Id is
4806 Parent_Typ : Entity_Id := Typ;
4807
4808 begin
4809 while not Is_CPP_Class (Parent_Typ)
4810 and then Etype (Parent_Typ) /= Parent_Typ
4811 loop
4812 Parent_Typ := Etype (Parent_Typ);
4813
4814 if Is_Private_Type (Parent_Typ) then
4815 Parent_Typ := Full_View (Base_Type (Parent_Typ));
4816 end if;
4817 end loop;
4818
4819 pragma Assert (Is_CPP_Class (Parent_Typ));
4820 return Parent_Typ;
4821 end Enclosing_CPP_Parent;
4822
4823 ----------------------------
4824 -- Enclosing_Generic_Body --
4825 ----------------------------
4826
4827 function Enclosing_Generic_Body
4828 (N : Node_Id) return Node_Id
4829 is
4830 P : Node_Id;
4831 Decl : Node_Id;
4832 Spec : Node_Id;
4833
4834 begin
4835 P := Parent (N);
4836 while Present (P) loop
4837 if Nkind (P) = N_Package_Body
4838 or else Nkind (P) = N_Subprogram_Body
4839 then
4840 Spec := Corresponding_Spec (P);
4841
4842 if Present (Spec) then
4843 Decl := Unit_Declaration_Node (Spec);
4844
4845 if Nkind (Decl) = N_Generic_Package_Declaration
4846 or else Nkind (Decl) = N_Generic_Subprogram_Declaration
4847 then
4848 return P;
4849 end if;
4850 end if;
4851 end if;
4852
4853 P := Parent (P);
4854 end loop;
4855
4856 return Empty;
4857 end Enclosing_Generic_Body;
4858
4859 ----------------------------
4860 -- Enclosing_Generic_Unit --
4861 ----------------------------
4862
4863 function Enclosing_Generic_Unit
4864 (N : Node_Id) return Node_Id
4865 is
4866 P : Node_Id;
4867 Decl : Node_Id;
4868 Spec : Node_Id;
4869
4870 begin
4871 P := Parent (N);
4872 while Present (P) loop
4873 if Nkind (P) = N_Generic_Package_Declaration
4874 or else Nkind (P) = N_Generic_Subprogram_Declaration
4875 then
4876 return P;
4877
4878 elsif Nkind (P) = N_Package_Body
4879 or else Nkind (P) = N_Subprogram_Body
4880 then
4881 Spec := Corresponding_Spec (P);
4882
4883 if Present (Spec) then
4884 Decl := Unit_Declaration_Node (Spec);
4885
4886 if Nkind (Decl) = N_Generic_Package_Declaration
4887 or else Nkind (Decl) = N_Generic_Subprogram_Declaration
4888 then
4889 return Decl;
4890 end if;
4891 end if;
4892 end if;
4893
4894 P := Parent (P);
4895 end loop;
4896
4897 return Empty;
4898 end Enclosing_Generic_Unit;
4899
4900 -------------------------------
4901 -- Enclosing_Lib_Unit_Entity --
4902 -------------------------------
4903
4904 function Enclosing_Lib_Unit_Entity
4905 (E : Entity_Id := Current_Scope) return Entity_Id
4906 is
4907 Unit_Entity : Entity_Id;
4908
4909 begin
4910 -- Look for enclosing library unit entity by following scope links.
4911 -- Equivalent to, but faster than indexing through the scope stack.
4912
4913 Unit_Entity := E;
4914 while (Present (Scope (Unit_Entity))
4915 and then Scope (Unit_Entity) /= Standard_Standard)
4916 and not Is_Child_Unit (Unit_Entity)
4917 loop
4918 Unit_Entity := Scope (Unit_Entity);
4919 end loop;
4920
4921 return Unit_Entity;
4922 end Enclosing_Lib_Unit_Entity;
4923
4924 -----------------------
4925 -- Enclosing_Package --
4926 -----------------------
4927
4928 function Enclosing_Package (E : Entity_Id) return Entity_Id is
4929 Dynamic_Scope : constant Entity_Id := Enclosing_Dynamic_Scope (E);
4930
4931 begin
4932 if Dynamic_Scope = Standard_Standard then
4933 return Standard_Standard;
4934
4935 elsif Dynamic_Scope = Empty then
4936 return Empty;
4937
4938 elsif Ekind_In (Dynamic_Scope, E_Package, E_Package_Body,
4939 E_Generic_Package)
4940 then
4941 return Dynamic_Scope;
4942
4943 else
4944 return Enclosing_Package (Dynamic_Scope);
4945 end if;
4946 end Enclosing_Package;
4947
4948 --------------------------
4949 -- Enclosing_Subprogram --
4950 --------------------------
4951
4952 function Enclosing_Subprogram (E : Entity_Id) return Entity_Id is
4953 Dynamic_Scope : constant Entity_Id := Enclosing_Dynamic_Scope (E);
4954
4955 begin
4956 if Dynamic_Scope = Standard_Standard then
4957 return Empty;
4958
4959 elsif Dynamic_Scope = Empty then
4960 return Empty;
4961
4962 elsif Ekind (Dynamic_Scope) = E_Subprogram_Body then
4963 return Corresponding_Spec (Parent (Parent (Dynamic_Scope)));
4964
4965 elsif Ekind (Dynamic_Scope) = E_Block
4966 or else Ekind (Dynamic_Scope) = E_Return_Statement
4967 then
4968 return Enclosing_Subprogram (Dynamic_Scope);
4969
4970 elsif Ekind (Dynamic_Scope) = E_Task_Type then
4971 return Get_Task_Body_Procedure (Dynamic_Scope);
4972
4973 elsif Ekind (Dynamic_Scope) = E_Limited_Private_Type
4974 and then Present (Full_View (Dynamic_Scope))
4975 and then Ekind (Full_View (Dynamic_Scope)) = E_Task_Type
4976 then
4977 return Get_Task_Body_Procedure (Full_View (Dynamic_Scope));
4978
4979 -- No body is generated if the protected operation is eliminated
4980
4981 elsif Convention (Dynamic_Scope) = Convention_Protected
4982 and then not Is_Eliminated (Dynamic_Scope)
4983 and then Present (Protected_Body_Subprogram (Dynamic_Scope))
4984 then
4985 return Protected_Body_Subprogram (Dynamic_Scope);
4986
4987 else
4988 return Dynamic_Scope;
4989 end if;
4990 end Enclosing_Subprogram;
4991
4992 ------------------------
4993 -- Ensure_Freeze_Node --
4994 ------------------------
4995
4996 procedure Ensure_Freeze_Node (E : Entity_Id) is
4997 FN : Node_Id;
4998 begin
4999 if No (Freeze_Node (E)) then
5000 FN := Make_Freeze_Entity (Sloc (E));
5001 Set_Has_Delayed_Freeze (E);
5002 Set_Freeze_Node (E, FN);
5003 Set_Access_Types_To_Process (FN, No_Elist);
5004 Set_TSS_Elist (FN, No_Elist);
5005 Set_Entity (FN, E);
5006 end if;
5007 end Ensure_Freeze_Node;
5008
5009 ----------------
5010 -- Enter_Name --
5011 ----------------
5012
5013 procedure Enter_Name (Def_Id : Entity_Id) is
5014 C : constant Entity_Id := Current_Entity (Def_Id);
5015 E : constant Entity_Id := Current_Entity_In_Scope (Def_Id);
5016 S : constant Entity_Id := Current_Scope;
5017
5018 begin
5019 Generate_Definition (Def_Id);
5020
5021 -- Add new name to current scope declarations. Check for duplicate
5022 -- declaration, which may or may not be a genuine error.
5023
5024 if Present (E) then
5025
5026 -- Case of previous entity entered because of a missing declaration
5027 -- or else a bad subtype indication. Best is to use the new entity,
5028 -- and make the previous one invisible.
5029
5030 if Etype (E) = Any_Type then
5031 Set_Is_Immediately_Visible (E, False);
5032
5033 -- Case of renaming declaration constructed for package instances.
5034 -- if there is an explicit declaration with the same identifier,
5035 -- the renaming is not immediately visible any longer, but remains
5036 -- visible through selected component notation.
5037
5038 elsif Nkind (Parent (E)) = N_Package_Renaming_Declaration
5039 and then not Comes_From_Source (E)
5040 then
5041 Set_Is_Immediately_Visible (E, False);
5042
5043 -- The new entity may be the package renaming, which has the same
5044 -- same name as a generic formal which has been seen already.
5045
5046 elsif Nkind (Parent (Def_Id)) = N_Package_Renaming_Declaration
5047 and then not Comes_From_Source (Def_Id)
5048 then
5049 Set_Is_Immediately_Visible (E, False);
5050
5051 -- For a fat pointer corresponding to a remote access to subprogram,
5052 -- we use the same identifier as the RAS type, so that the proper
5053 -- name appears in the stub. This type is only retrieved through
5054 -- the RAS type and never by visibility, and is not added to the
5055 -- visibility list (see below).
5056
5057 elsif Nkind (Parent (Def_Id)) = N_Full_Type_Declaration
5058 and then Ekind (Def_Id) = E_Record_Type
5059 and then Present (Corresponding_Remote_Type (Def_Id))
5060 then
5061 null;
5062
5063 -- Case of an implicit operation or derived literal. The new entity
5064 -- hides the implicit one, which is removed from all visibility,
5065 -- i.e. the entity list of its scope, and homonym chain of its name.
5066
5067 elsif (Is_Overloadable (E) and then Is_Inherited_Operation (E))
5068 or else Is_Internal (E)
5069 then
5070 declare
5071 Prev : Entity_Id;
5072 Prev_Vis : Entity_Id;
5073 Decl : constant Node_Id := Parent (E);
5074
5075 begin
5076 -- If E is an implicit declaration, it cannot be the first
5077 -- entity in the scope.
5078
5079 Prev := First_Entity (Current_Scope);
5080 while Present (Prev)
5081 and then Next_Entity (Prev) /= E
5082 loop
5083 Next_Entity (Prev);
5084 end loop;
5085
5086 if No (Prev) then
5087
5088 -- If E is not on the entity chain of the current scope,
5089 -- it is an implicit declaration in the generic formal
5090 -- part of a generic subprogram. When analyzing the body,
5091 -- the generic formals are visible but not on the entity
5092 -- chain of the subprogram. The new entity will become
5093 -- the visible one in the body.
5094
5095 pragma Assert
5096 (Nkind (Parent (Decl)) = N_Generic_Subprogram_Declaration);
5097 null;
5098
5099 else
5100 Set_Next_Entity (Prev, Next_Entity (E));
5101
5102 if No (Next_Entity (Prev)) then
5103 Set_Last_Entity (Current_Scope, Prev);
5104 end if;
5105
5106 if E = Current_Entity (E) then
5107 Prev_Vis := Empty;
5108
5109 else
5110 Prev_Vis := Current_Entity (E);
5111 while Homonym (Prev_Vis) /= E loop
5112 Prev_Vis := Homonym (Prev_Vis);
5113 end loop;
5114 end if;
5115
5116 if Present (Prev_Vis) then
5117
5118 -- Skip E in the visibility chain
5119
5120 Set_Homonym (Prev_Vis, Homonym (E));
5121
5122 else
5123 Set_Name_Entity_Id (Chars (E), Homonym (E));
5124 end if;
5125 end if;
5126 end;
5127
5128 -- This section of code could use a comment ???
5129
5130 elsif Present (Etype (E))
5131 and then Is_Concurrent_Type (Etype (E))
5132 and then E = Def_Id
5133 then
5134 return;
5135
5136 -- If the homograph is a protected component renaming, it should not
5137 -- be hiding the current entity. Such renamings are treated as weak
5138 -- declarations.
5139
5140 elsif Is_Prival (E) then
5141 Set_Is_Immediately_Visible (E, False);
5142
5143 -- In this case the current entity is a protected component renaming.
5144 -- Perform minimal decoration by setting the scope and return since
5145 -- the prival should not be hiding other visible entities.
5146
5147 elsif Is_Prival (Def_Id) then
5148 Set_Scope (Def_Id, Current_Scope);
5149 return;
5150
5151 -- Analogous to privals, the discriminal generated for an entry index
5152 -- parameter acts as a weak declaration. Perform minimal decoration
5153 -- to avoid bogus errors.
5154
5155 elsif Is_Discriminal (Def_Id)
5156 and then Ekind (Discriminal_Link (Def_Id)) = E_Entry_Index_Parameter
5157 then
5158 Set_Scope (Def_Id, Current_Scope);
5159 return;
5160
5161 -- In the body or private part of an instance, a type extension may
5162 -- introduce a component with the same name as that of an actual. The
5163 -- legality rule is not enforced, but the semantics of the full type
5164 -- with two components of same name are not clear at this point???
5165
5166 elsif In_Instance_Not_Visible then
5167 null;
5168
5169 -- When compiling a package body, some child units may have become
5170 -- visible. They cannot conflict with local entities that hide them.
5171
5172 elsif Is_Child_Unit (E)
5173 and then In_Open_Scopes (Scope (E))
5174 and then not Is_Immediately_Visible (E)
5175 then
5176 null;
5177
5178 -- Conversely, with front-end inlining we may compile the parent body
5179 -- first, and a child unit subsequently. The context is now the
5180 -- parent spec, and body entities are not visible.
5181
5182 elsif Is_Child_Unit (Def_Id)
5183 and then Is_Package_Body_Entity (E)
5184 and then not In_Package_Body (Current_Scope)
5185 then
5186 null;
5187
5188 -- Case of genuine duplicate declaration
5189
5190 else
5191 Error_Msg_Sloc := Sloc (E);
5192
5193 -- If the previous declaration is an incomplete type declaration
5194 -- this may be an attempt to complete it with a private type. The
5195 -- following avoids confusing cascaded errors.
5196
5197 if Nkind (Parent (E)) = N_Incomplete_Type_Declaration
5198 and then Nkind (Parent (Def_Id)) = N_Private_Type_Declaration
5199 then
5200 Error_Msg_N
5201 ("incomplete type cannot be completed with a private " &
5202 "declaration", Parent (Def_Id));
5203 Set_Is_Immediately_Visible (E, False);
5204 Set_Full_View (E, Def_Id);
5205
5206 -- An inherited component of a record conflicts with a new
5207 -- discriminant. The discriminant is inserted first in the scope,
5208 -- but the error should be posted on it, not on the component.
5209
5210 elsif Ekind (E) = E_Discriminant
5211 and then Present (Scope (Def_Id))
5212 and then Scope (Def_Id) /= Current_Scope
5213 then
5214 Error_Msg_Sloc := Sloc (Def_Id);
5215 Error_Msg_N ("& conflicts with declaration#", E);
5216 return;
5217
5218 -- If the name of the unit appears in its own context clause, a
5219 -- dummy package with the name has already been created, and the
5220 -- error emitted. Try to continue quietly.
5221
5222 elsif Error_Posted (E)
5223 and then Sloc (E) = No_Location
5224 and then Nkind (Parent (E)) = N_Package_Specification
5225 and then Current_Scope = Standard_Standard
5226 then
5227 Set_Scope (Def_Id, Current_Scope);
5228 return;
5229
5230 else
5231 Error_Msg_N ("& conflicts with declaration#", Def_Id);
5232
5233 -- Avoid cascaded messages with duplicate components in
5234 -- derived types.
5235
5236 if Ekind_In (E, E_Component, E_Discriminant) then
5237 return;
5238 end if;
5239 end if;
5240
5241 if Nkind (Parent (Parent (Def_Id))) =
5242 N_Generic_Subprogram_Declaration
5243 and then Def_Id =
5244 Defining_Entity (Specification (Parent (Parent (Def_Id))))
5245 then
5246 Error_Msg_N ("\generic units cannot be overloaded", Def_Id);
5247 end if;
5248
5249 -- If entity is in standard, then we are in trouble, because it
5250 -- means that we have a library package with a duplicated name.
5251 -- That's hard to recover from, so abort.
5252
5253 if S = Standard_Standard then
5254 raise Unrecoverable_Error;
5255
5256 -- Otherwise we continue with the declaration. Having two
5257 -- identical declarations should not cause us too much trouble.
5258
5259 else
5260 null;
5261 end if;
5262 end if;
5263 end if;
5264
5265 -- If we fall through, declaration is OK, at least OK enough to continue
5266
5267 -- If Def_Id is a discriminant or a record component we are in the midst
5268 -- of inheriting components in a derived record definition. Preserve
5269 -- their Ekind and Etype.
5270
5271 if Ekind_In (Def_Id, E_Discriminant, E_Component) then
5272 null;
5273
5274 -- If a type is already set, leave it alone (happens when a type
5275 -- declaration is reanalyzed following a call to the optimizer).
5276
5277 elsif Present (Etype (Def_Id)) then
5278 null;
5279
5280 -- Otherwise, the kind E_Void insures that premature uses of the entity
5281 -- will be detected. Any_Type insures that no cascaded errors will occur
5282
5283 else
5284 Set_Ekind (Def_Id, E_Void);
5285 Set_Etype (Def_Id, Any_Type);
5286 end if;
5287
5288 -- Inherited discriminants and components in derived record types are
5289 -- immediately visible. Itypes are not.
5290
5291 -- Unless the Itype is for a record type with a corresponding remote
5292 -- type (what is that about, it was not commented ???)
5293
5294 if Ekind_In (Def_Id, E_Discriminant, E_Component)
5295 or else
5296 ((not Is_Record_Type (Def_Id)
5297 or else No (Corresponding_Remote_Type (Def_Id)))
5298 and then not Is_Itype (Def_Id))
5299 then
5300 Set_Is_Immediately_Visible (Def_Id);
5301 Set_Current_Entity (Def_Id);
5302 end if;
5303
5304 Set_Homonym (Def_Id, C);
5305 Append_Entity (Def_Id, S);
5306 Set_Public_Status (Def_Id);
5307
5308 -- Declaring a homonym is not allowed in SPARK ...
5309
5310 if Present (C)
5311 and then Restriction_Check_Required (SPARK_05)
5312 then
5313 declare
5314 Enclosing_Subp : constant Node_Id := Enclosing_Subprogram (Def_Id);
5315 Enclosing_Pack : constant Node_Id := Enclosing_Package (Def_Id);
5316 Other_Scope : constant Node_Id := Enclosing_Dynamic_Scope (C);
5317
5318 begin
5319 -- ... unless the new declaration is in a subprogram, and the
5320 -- visible declaration is a variable declaration or a parameter
5321 -- specification outside that subprogram.
5322
5323 if Present (Enclosing_Subp)
5324 and then Nkind_In (Parent (C), N_Object_Declaration,
5325 N_Parameter_Specification)
5326 and then not Scope_Within_Or_Same (Other_Scope, Enclosing_Subp)
5327 then
5328 null;
5329
5330 -- ... or the new declaration is in a package, and the visible
5331 -- declaration occurs outside that package.
5332
5333 elsif Present (Enclosing_Pack)
5334 and then not Scope_Within_Or_Same (Other_Scope, Enclosing_Pack)
5335 then
5336 null;
5337
5338 -- ... or the new declaration is a component declaration in a
5339 -- record type definition.
5340
5341 elsif Nkind (Parent (Def_Id)) = N_Component_Declaration then
5342 null;
5343
5344 -- Don't issue error for non-source entities
5345
5346 elsif Comes_From_Source (Def_Id)
5347 and then Comes_From_Source (C)
5348 then
5349 Error_Msg_Sloc := Sloc (C);
5350 Check_SPARK_Restriction
5351 ("redeclaration of identifier &#", Def_Id);
5352 end if;
5353 end;
5354 end if;
5355
5356 -- Warn if new entity hides an old one
5357
5358 if Warn_On_Hiding and then Present (C)
5359
5360 -- Don't warn for record components since they always have a well
5361 -- defined scope which does not confuse other uses. Note that in
5362 -- some cases, Ekind has not been set yet.
5363
5364 and then Ekind (C) /= E_Component
5365 and then Ekind (C) /= E_Discriminant
5366 and then Nkind (Parent (C)) /= N_Component_Declaration
5367 and then Ekind (Def_Id) /= E_Component
5368 and then Ekind (Def_Id) /= E_Discriminant
5369 and then Nkind (Parent (Def_Id)) /= N_Component_Declaration
5370
5371 -- Don't warn for one character variables. It is too common to use
5372 -- such variables as locals and will just cause too many false hits.
5373
5374 and then Length_Of_Name (Chars (C)) /= 1
5375
5376 -- Don't warn for non-source entities
5377
5378 and then Comes_From_Source (C)
5379 and then Comes_From_Source (Def_Id)
5380
5381 -- Don't warn unless entity in question is in extended main source
5382
5383 and then In_Extended_Main_Source_Unit (Def_Id)
5384
5385 -- Finally, the hidden entity must be either immediately visible or
5386 -- use visible (i.e. from a used package).
5387
5388 and then
5389 (Is_Immediately_Visible (C)
5390 or else
5391 Is_Potentially_Use_Visible (C))
5392 then
5393 Error_Msg_Sloc := Sloc (C);
5394 Error_Msg_N ("declaration hides &#?h?", Def_Id);
5395 end if;
5396 end Enter_Name;
5397
5398 ---------------
5399 -- Entity_Of --
5400 ---------------
5401
5402 function Entity_Of (N : Node_Id) return Entity_Id is
5403 Id : Entity_Id;
5404
5405 begin
5406 Id := Empty;
5407
5408 if Is_Entity_Name (N) then
5409 Id := Entity (N);
5410
5411 -- Follow a possible chain of renamings to reach the root renamed
5412 -- object.
5413
5414 while Present (Id) and then Present (Renamed_Object (Id)) loop
5415 if Is_Entity_Name (Renamed_Object (Id)) then
5416 Id := Entity (Renamed_Object (Id));
5417 else
5418 Id := Empty;
5419 exit;
5420 end if;
5421 end loop;
5422 end if;
5423
5424 return Id;
5425 end Entity_Of;
5426
5427 --------------------------
5428 -- Explain_Limited_Type --
5429 --------------------------
5430
5431 procedure Explain_Limited_Type (T : Entity_Id; N : Node_Id) is
5432 C : Entity_Id;
5433
5434 begin
5435 -- For array, component type must be limited
5436
5437 if Is_Array_Type (T) then
5438 Error_Msg_Node_2 := T;
5439 Error_Msg_NE
5440 ("\component type& of type& is limited", N, Component_Type (T));
5441 Explain_Limited_Type (Component_Type (T), N);
5442
5443 elsif Is_Record_Type (T) then
5444
5445 -- No need for extra messages if explicit limited record
5446
5447 if Is_Limited_Record (Base_Type (T)) then
5448 return;
5449 end if;
5450
5451 -- Otherwise find a limited component. Check only components that
5452 -- come from source, or inherited components that appear in the
5453 -- source of the ancestor.
5454
5455 C := First_Component (T);
5456 while Present (C) loop
5457 if Is_Limited_Type (Etype (C))
5458 and then
5459 (Comes_From_Source (C)
5460 or else
5461 (Present (Original_Record_Component (C))
5462 and then
5463 Comes_From_Source (Original_Record_Component (C))))
5464 then
5465 Error_Msg_Node_2 := T;
5466 Error_Msg_NE ("\component& of type& has limited type", N, C);
5467 Explain_Limited_Type (Etype (C), N);
5468 return;
5469 end if;
5470
5471 Next_Component (C);
5472 end loop;
5473
5474 -- The type may be declared explicitly limited, even if no component
5475 -- of it is limited, in which case we fall out of the loop.
5476 return;
5477 end if;
5478 end Explain_Limited_Type;
5479
5480 -----------------
5481 -- Find_Actual --
5482 -----------------
5483
5484 procedure Find_Actual
5485 (N : Node_Id;
5486 Formal : out Entity_Id;
5487 Call : out Node_Id)
5488 is
5489 Parnt : constant Node_Id := Parent (N);
5490 Actual : Node_Id;
5491
5492 begin
5493 if (Nkind (Parnt) = N_Indexed_Component
5494 or else
5495 Nkind (Parnt) = N_Selected_Component)
5496 and then N = Prefix (Parnt)
5497 then
5498 Find_Actual (Parnt, Formal, Call);
5499 return;
5500
5501 elsif Nkind (Parnt) = N_Parameter_Association
5502 and then N = Explicit_Actual_Parameter (Parnt)
5503 then
5504 Call := Parent (Parnt);
5505
5506 elsif Nkind (Parnt) in N_Subprogram_Call then
5507 Call := Parnt;
5508
5509 else
5510 Formal := Empty;
5511 Call := Empty;
5512 return;
5513 end if;
5514
5515 -- If we have a call to a subprogram look for the parameter. Note that
5516 -- we exclude overloaded calls, since we don't know enough to be sure
5517 -- of giving the right answer in this case.
5518
5519 if Nkind_In (Call, N_Function_Call, N_Procedure_Call_Statement)
5520 and then Is_Entity_Name (Name (Call))
5521 and then Present (Entity (Name (Call)))
5522 and then Is_Overloadable (Entity (Name (Call)))
5523 and then not Is_Overloaded (Name (Call))
5524 then
5525 -- Fall here if we are definitely a parameter
5526
5527 Actual := First_Actual (Call);
5528 Formal := First_Formal (Entity (Name (Call)));
5529 while Present (Formal) and then Present (Actual) loop
5530 if Actual = N then
5531 return;
5532
5533 -- An actual that is the prefix in a prefixed call may have
5534 -- been rewritten in the call, after the deferred reference
5535 -- was collected. Check if sloc and kinds and names match.
5536
5537 elsif Sloc (Actual) = Sloc (N)
5538 and then Nkind (Actual) = N_Identifier
5539 and then Nkind (Actual) = Nkind (N)
5540 and then Chars (Actual) = Chars (N)
5541 then
5542 return;
5543
5544 else
5545 Actual := Next_Actual (Actual);
5546 Formal := Next_Formal (Formal);
5547 end if;
5548 end loop;
5549 end if;
5550
5551 -- Fall through here if we did not find matching actual
5552
5553 Formal := Empty;
5554 Call := Empty;
5555 end Find_Actual;
5556
5557 ---------------------------
5558 -- Find_Body_Discriminal --
5559 ---------------------------
5560
5561 function Find_Body_Discriminal
5562 (Spec_Discriminant : Entity_Id) return Entity_Id
5563 is
5564 Tsk : Entity_Id;
5565 Disc : Entity_Id;
5566
5567 begin
5568 -- If expansion is suppressed, then the scope can be the concurrent type
5569 -- itself rather than a corresponding concurrent record type.
5570
5571 if Is_Concurrent_Type (Scope (Spec_Discriminant)) then
5572 Tsk := Scope (Spec_Discriminant);
5573
5574 else
5575 pragma Assert (Is_Concurrent_Record_Type (Scope (Spec_Discriminant)));
5576
5577 Tsk := Corresponding_Concurrent_Type (Scope (Spec_Discriminant));
5578 end if;
5579
5580 -- Find discriminant of original concurrent type, and use its current
5581 -- discriminal, which is the renaming within the task/protected body.
5582
5583 Disc := First_Discriminant (Tsk);
5584 while Present (Disc) loop
5585 if Chars (Disc) = Chars (Spec_Discriminant) then
5586 return Discriminal (Disc);
5587 end if;
5588
5589 Next_Discriminant (Disc);
5590 end loop;
5591
5592 -- That loop should always succeed in finding a matching entry and
5593 -- returning. Fatal error if not.
5594
5595 raise Program_Error;
5596 end Find_Body_Discriminal;
5597
5598 -------------------------------------
5599 -- Find_Corresponding_Discriminant --
5600 -------------------------------------
5601
5602 function Find_Corresponding_Discriminant
5603 (Id : Node_Id;
5604 Typ : Entity_Id) return Entity_Id
5605 is
5606 Par_Disc : Entity_Id;
5607 Old_Disc : Entity_Id;
5608 New_Disc : Entity_Id;
5609
5610 begin
5611 Par_Disc := Original_Record_Component (Original_Discriminant (Id));
5612
5613 -- The original type may currently be private, and the discriminant
5614 -- only appear on its full view.
5615
5616 if Is_Private_Type (Scope (Par_Disc))
5617 and then not Has_Discriminants (Scope (Par_Disc))
5618 and then Present (Full_View (Scope (Par_Disc)))
5619 then
5620 Old_Disc := First_Discriminant (Full_View (Scope (Par_Disc)));
5621 else
5622 Old_Disc := First_Discriminant (Scope (Par_Disc));
5623 end if;
5624
5625 if Is_Class_Wide_Type (Typ) then
5626 New_Disc := First_Discriminant (Root_Type (Typ));
5627 else
5628 New_Disc := First_Discriminant (Typ);
5629 end if;
5630
5631 while Present (Old_Disc) and then Present (New_Disc) loop
5632 if Old_Disc = Par_Disc then
5633 return New_Disc;
5634 else
5635 Next_Discriminant (Old_Disc);
5636 Next_Discriminant (New_Disc);
5637 end if;
5638 end loop;
5639
5640 -- Should always find it
5641
5642 raise Program_Error;
5643 end Find_Corresponding_Discriminant;
5644
5645 ----------------------------------
5646 -- Find_Enclosing_Iterator_Loop --
5647 ----------------------------------
5648
5649 function Find_Enclosing_Iterator_Loop (Id : Entity_Id) return Entity_Id is
5650 Constr : Node_Id;
5651 S : Entity_Id;
5652
5653 begin
5654 -- Traverse the scope chain looking for an iterator loop. Such loops are
5655 -- usually transformed into blocks, hence the use of Original_Node.
5656
5657 S := Id;
5658 while Present (S) and then S /= Standard_Standard loop
5659 if Ekind (S) = E_Loop
5660 and then Nkind (Parent (S)) = N_Implicit_Label_Declaration
5661 then
5662 Constr := Original_Node (Label_Construct (Parent (S)));
5663
5664 if Nkind (Constr) = N_Loop_Statement
5665 and then Present (Iteration_Scheme (Constr))
5666 and then Nkind (Iterator_Specification
5667 (Iteration_Scheme (Constr))) =
5668 N_Iterator_Specification
5669 then
5670 return S;
5671 end if;
5672 end if;
5673
5674 S := Scope (S);
5675 end loop;
5676
5677 return Empty;
5678 end Find_Enclosing_Iterator_Loop;
5679
5680 ------------------------------------
5681 -- Find_Loop_In_Conditional_Block --
5682 ------------------------------------
5683
5684 function Find_Loop_In_Conditional_Block (N : Node_Id) return Node_Id is
5685 Stmt : Node_Id;
5686
5687 begin
5688 Stmt := N;
5689
5690 if Nkind (Stmt) = N_If_Statement then
5691 Stmt := First (Then_Statements (Stmt));
5692 end if;
5693
5694 pragma Assert (Nkind (Stmt) = N_Block_Statement);
5695
5696 -- Inspect the statements of the conditional block. In general the loop
5697 -- should be the first statement in the statement sequence of the block,
5698 -- but the finalization machinery may have introduced extra object
5699 -- declarations.
5700
5701 Stmt := First (Statements (Handled_Statement_Sequence (Stmt)));
5702 while Present (Stmt) loop
5703 if Nkind (Stmt) = N_Loop_Statement then
5704 return Stmt;
5705 end if;
5706
5707 Next (Stmt);
5708 end loop;
5709
5710 -- The expansion of attribute 'Loop_Entry produced a malformed block
5711
5712 raise Program_Error;
5713 end Find_Loop_In_Conditional_Block;
5714
5715 --------------------------
5716 -- Find_Overlaid_Entity --
5717 --------------------------
5718
5719 procedure Find_Overlaid_Entity
5720 (N : Node_Id;
5721 Ent : out Entity_Id;
5722 Off : out Boolean)
5723 is
5724 Expr : Node_Id;
5725
5726 begin
5727 -- We are looking for one of the two following forms:
5728
5729 -- for X'Address use Y'Address
5730
5731 -- or
5732
5733 -- Const : constant Address := expr;
5734 -- ...
5735 -- for X'Address use Const;
5736
5737 -- In the second case, the expr is either Y'Address, or recursively a
5738 -- constant that eventually references Y'Address.
5739
5740 Ent := Empty;
5741 Off := False;
5742
5743 if Nkind (N) = N_Attribute_Definition_Clause
5744 and then Chars (N) = Name_Address
5745 then
5746 Expr := Expression (N);
5747
5748 -- This loop checks the form of the expression for Y'Address,
5749 -- using recursion to deal with intermediate constants.
5750
5751 loop
5752 -- Check for Y'Address
5753
5754 if Nkind (Expr) = N_Attribute_Reference
5755 and then Attribute_Name (Expr) = Name_Address
5756 then
5757 Expr := Prefix (Expr);
5758 exit;
5759
5760 -- Check for Const where Const is a constant entity
5761
5762 elsif Is_Entity_Name (Expr)
5763 and then Ekind (Entity (Expr)) = E_Constant
5764 then
5765 Expr := Constant_Value (Entity (Expr));
5766
5767 -- Anything else does not need checking
5768
5769 else
5770 return;
5771 end if;
5772 end loop;
5773
5774 -- This loop checks the form of the prefix for an entity, using
5775 -- recursion to deal with intermediate components.
5776
5777 loop
5778 -- Check for Y where Y is an entity
5779
5780 if Is_Entity_Name (Expr) then
5781 Ent := Entity (Expr);
5782 return;
5783
5784 -- Check for components
5785
5786 elsif
5787 Nkind_In (Expr, N_Selected_Component, N_Indexed_Component)
5788 then
5789 Expr := Prefix (Expr);
5790 Off := True;
5791
5792 -- Anything else does not need checking
5793
5794 else
5795 return;
5796 end if;
5797 end loop;
5798 end if;
5799 end Find_Overlaid_Entity;
5800
5801 -------------------------
5802 -- Find_Parameter_Type --
5803 -------------------------
5804
5805 function Find_Parameter_Type (Param : Node_Id) return Entity_Id is
5806 begin
5807 if Nkind (Param) /= N_Parameter_Specification then
5808 return Empty;
5809
5810 -- For an access parameter, obtain the type from the formal entity
5811 -- itself, because access to subprogram nodes do not carry a type.
5812 -- Shouldn't we always use the formal entity ???
5813
5814 elsif Nkind (Parameter_Type (Param)) = N_Access_Definition then
5815 return Etype (Defining_Identifier (Param));
5816
5817 else
5818 return Etype (Parameter_Type (Param));
5819 end if;
5820 end Find_Parameter_Type;
5821
5822 -----------------------------------
5823 -- Find_Placement_In_State_Space --
5824 -----------------------------------
5825
5826 procedure Find_Placement_In_State_Space
5827 (Item_Id : Entity_Id;
5828 Placement : out State_Space_Kind;
5829 Pack_Id : out Entity_Id)
5830 is
5831 Context : Entity_Id;
5832
5833 begin
5834 -- Assume that the item does not appear in the state space of a package
5835
5836 Placement := Not_In_Package;
5837 Pack_Id := Empty;
5838
5839 -- Climb the scope stack and examine the enclosing context
5840
5841 Context := Scope (Item_Id);
5842 while Present (Context) and then Context /= Standard_Standard loop
5843 if Ekind (Context) = E_Package then
5844 Pack_Id := Context;
5845
5846 -- A package body is a cut off point for the traversal as the item
5847 -- cannot be visible to the outside from this point on. Note that
5848 -- this test must be done first as a body is also classified as a
5849 -- private part.
5850
5851 if In_Package_Body (Context) then
5852 Placement := Body_State_Space;
5853 return;
5854
5855 -- The private part of a package is a cut off point for the
5856 -- traversal as the item cannot be visible to the outside from
5857 -- this point on.
5858
5859 elsif In_Private_Part (Context) then
5860 Placement := Private_State_Space;
5861 return;
5862
5863 -- When the item appears in the visible state space of a package,
5864 -- continue to climb the scope stack as this may not be the final
5865 -- state space.
5866
5867 else
5868 Placement := Visible_State_Space;
5869
5870 -- The visible state space of a child unit acts as the proper
5871 -- placement of an item.
5872
5873 if Is_Child_Unit (Context) then
5874 return;
5875 end if;
5876 end if;
5877
5878 -- The item or its enclosing package appear in a construct that has
5879 -- no state space.
5880
5881 else
5882 Placement := Not_In_Package;
5883 return;
5884 end if;
5885
5886 Context := Scope (Context);
5887 end loop;
5888 end Find_Placement_In_State_Space;
5889
5890 -----------------------------
5891 -- Find_Static_Alternative --
5892 -----------------------------
5893
5894 function Find_Static_Alternative (N : Node_Id) return Node_Id is
5895 Expr : constant Node_Id := Expression (N);
5896 Val : constant Uint := Expr_Value (Expr);
5897 Alt : Node_Id;
5898 Choice : Node_Id;
5899
5900 begin
5901 Alt := First (Alternatives (N));
5902
5903 Search : loop
5904 if Nkind (Alt) /= N_Pragma then
5905 Choice := First (Discrete_Choices (Alt));
5906 while Present (Choice) loop
5907
5908 -- Others choice, always matches
5909
5910 if Nkind (Choice) = N_Others_Choice then
5911 exit Search;
5912
5913 -- Range, check if value is in the range
5914
5915 elsif Nkind (Choice) = N_Range then
5916 exit Search when
5917 Val >= Expr_Value (Low_Bound (Choice))
5918 and then
5919 Val <= Expr_Value (High_Bound (Choice));
5920
5921 -- Choice is a subtype name. Note that we know it must
5922 -- be a static subtype, since otherwise it would have
5923 -- been diagnosed as illegal.
5924
5925 elsif Is_Entity_Name (Choice)
5926 and then Is_Type (Entity (Choice))
5927 then
5928 exit Search when Is_In_Range (Expr, Etype (Choice),
5929 Assume_Valid => False);
5930
5931 -- Choice is a subtype indication
5932
5933 elsif Nkind (Choice) = N_Subtype_Indication then
5934 declare
5935 C : constant Node_Id := Constraint (Choice);
5936 R : constant Node_Id := Range_Expression (C);
5937
5938 begin
5939 exit Search when
5940 Val >= Expr_Value (Low_Bound (R))
5941 and then
5942 Val <= Expr_Value (High_Bound (R));
5943 end;
5944
5945 -- Choice is a simple expression
5946
5947 else
5948 exit Search when Val = Expr_Value (Choice);
5949 end if;
5950
5951 Next (Choice);
5952 end loop;
5953 end if;
5954
5955 Next (Alt);
5956 pragma Assert (Present (Alt));
5957 end loop Search;
5958
5959 -- The above loop *must* terminate by finding a match, since
5960 -- we know the case statement is valid, and the value of the
5961 -- expression is known at compile time. When we fall out of
5962 -- the loop, Alt points to the alternative that we know will
5963 -- be selected at run time.
5964
5965 return Alt;
5966 end Find_Static_Alternative;
5967
5968 ------------------
5969 -- First_Actual --
5970 ------------------
5971
5972 function First_Actual (Node : Node_Id) return Node_Id is
5973 N : Node_Id;
5974
5975 begin
5976 if No (Parameter_Associations (Node)) then
5977 return Empty;
5978 end if;
5979
5980 N := First (Parameter_Associations (Node));
5981
5982 if Nkind (N) = N_Parameter_Association then
5983 return First_Named_Actual (Node);
5984 else
5985 return N;
5986 end if;
5987 end First_Actual;
5988
5989 -----------------------
5990 -- Gather_Components --
5991 -----------------------
5992
5993 procedure Gather_Components
5994 (Typ : Entity_Id;
5995 Comp_List : Node_Id;
5996 Governed_By : List_Id;
5997 Into : Elist_Id;
5998 Report_Errors : out Boolean)
5999 is
6000 Assoc : Node_Id;
6001 Variant : Node_Id;
6002 Discrete_Choice : Node_Id;
6003 Comp_Item : Node_Id;
6004
6005 Discrim : Entity_Id;
6006 Discrim_Name : Node_Id;
6007 Discrim_Value : Node_Id;
6008
6009 begin
6010 Report_Errors := False;
6011
6012 if No (Comp_List) or else Null_Present (Comp_List) then
6013 return;
6014
6015 elsif Present (Component_Items (Comp_List)) then
6016 Comp_Item := First (Component_Items (Comp_List));
6017
6018 else
6019 Comp_Item := Empty;
6020 end if;
6021
6022 while Present (Comp_Item) loop
6023
6024 -- Skip the tag of a tagged record, the interface tags, as well
6025 -- as all items that are not user components (anonymous types,
6026 -- rep clauses, Parent field, controller field).
6027
6028 if Nkind (Comp_Item) = N_Component_Declaration then
6029 declare
6030 Comp : constant Entity_Id := Defining_Identifier (Comp_Item);
6031 begin
6032 if not Is_Tag (Comp)
6033 and then Chars (Comp) /= Name_uParent
6034 then
6035 Append_Elmt (Comp, Into);
6036 end if;
6037 end;
6038 end if;
6039
6040 Next (Comp_Item);
6041 end loop;
6042
6043 if No (Variant_Part (Comp_List)) then
6044 return;
6045 else
6046 Discrim_Name := Name (Variant_Part (Comp_List));
6047 Variant := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
6048 end if;
6049
6050 -- Look for the discriminant that governs this variant part.
6051 -- The discriminant *must* be in the Governed_By List
6052
6053 Assoc := First (Governed_By);
6054 Find_Constraint : loop
6055 Discrim := First (Choices (Assoc));
6056 exit Find_Constraint when Chars (Discrim_Name) = Chars (Discrim)
6057 or else (Present (Corresponding_Discriminant (Entity (Discrim)))
6058 and then
6059 Chars (Corresponding_Discriminant (Entity (Discrim))) =
6060 Chars (Discrim_Name))
6061 or else Chars (Original_Record_Component (Entity (Discrim)))
6062 = Chars (Discrim_Name);
6063
6064 if No (Next (Assoc)) then
6065 if not Is_Constrained (Typ)
6066 and then Is_Derived_Type (Typ)
6067 and then Present (Stored_Constraint (Typ))
6068 then
6069 -- If the type is a tagged type with inherited discriminants,
6070 -- use the stored constraint on the parent in order to find
6071 -- the values of discriminants that are otherwise hidden by an
6072 -- explicit constraint. Renamed discriminants are handled in
6073 -- the code above.
6074
6075 -- If several parent discriminants are renamed by a single
6076 -- discriminant of the derived type, the call to obtain the
6077 -- Corresponding_Discriminant field only retrieves the last
6078 -- of them. We recover the constraint on the others from the
6079 -- Stored_Constraint as well.
6080
6081 declare
6082 D : Entity_Id;
6083 C : Elmt_Id;
6084
6085 begin
6086 D := First_Discriminant (Etype (Typ));
6087 C := First_Elmt (Stored_Constraint (Typ));
6088 while Present (D) and then Present (C) loop
6089 if Chars (Discrim_Name) = Chars (D) then
6090 if Is_Entity_Name (Node (C))
6091 and then Entity (Node (C)) = Entity (Discrim)
6092 then
6093 -- D is renamed by Discrim, whose value is given in
6094 -- Assoc.
6095
6096 null;
6097
6098 else
6099 Assoc :=
6100 Make_Component_Association (Sloc (Typ),
6101 New_List
6102 (New_Occurrence_Of (D, Sloc (Typ))),
6103 Duplicate_Subexpr_No_Checks (Node (C)));
6104 end if;
6105 exit Find_Constraint;
6106 end if;
6107
6108 Next_Discriminant (D);
6109 Next_Elmt (C);
6110 end loop;
6111 end;
6112 end if;
6113 end if;
6114
6115 if No (Next (Assoc)) then
6116 Error_Msg_NE (" missing value for discriminant&",
6117 First (Governed_By), Discrim_Name);
6118 Report_Errors := True;
6119 return;
6120 end if;
6121
6122 Next (Assoc);
6123 end loop Find_Constraint;
6124
6125 Discrim_Value := Expression (Assoc);
6126
6127 if not Is_OK_Static_Expression (Discrim_Value) then
6128 Error_Msg_FE
6129 ("value for discriminant & must be static!",
6130 Discrim_Value, Discrim);
6131 Why_Not_Static (Discrim_Value);
6132 Report_Errors := True;
6133 return;
6134 end if;
6135
6136 Search_For_Discriminant_Value : declare
6137 Low : Node_Id;
6138 High : Node_Id;
6139
6140 UI_High : Uint;
6141 UI_Low : Uint;
6142 UI_Discrim_Value : constant Uint := Expr_Value (Discrim_Value);
6143
6144 begin
6145 Find_Discrete_Value : while Present (Variant) loop
6146 Discrete_Choice := First (Discrete_Choices (Variant));
6147 while Present (Discrete_Choice) loop
6148 exit Find_Discrete_Value when
6149 Nkind (Discrete_Choice) = N_Others_Choice;
6150
6151 Get_Index_Bounds (Discrete_Choice, Low, High);
6152
6153 UI_Low := Expr_Value (Low);
6154 UI_High := Expr_Value (High);
6155
6156 exit Find_Discrete_Value when
6157 UI_Low <= UI_Discrim_Value
6158 and then
6159 UI_High >= UI_Discrim_Value;
6160
6161 Next (Discrete_Choice);
6162 end loop;
6163
6164 Next_Non_Pragma (Variant);
6165 end loop Find_Discrete_Value;
6166 end Search_For_Discriminant_Value;
6167
6168 if No (Variant) then
6169 Error_Msg_NE
6170 ("value of discriminant & is out of range", Discrim_Value, Discrim);
6171 Report_Errors := True;
6172 return;
6173 end if;
6174
6175 -- If we have found the corresponding choice, recursively add its
6176 -- components to the Into list.
6177
6178 Gather_Components
6179 (Empty, Component_List (Variant), Governed_By, Into, Report_Errors);
6180 end Gather_Components;
6181
6182 ------------------------
6183 -- Get_Actual_Subtype --
6184 ------------------------
6185
6186 function Get_Actual_Subtype (N : Node_Id) return Entity_Id is
6187 Typ : constant Entity_Id := Etype (N);
6188 Utyp : Entity_Id := Underlying_Type (Typ);
6189 Decl : Node_Id;
6190 Atyp : Entity_Id;
6191
6192 begin
6193 if No (Utyp) then
6194 Utyp := Typ;
6195 end if;
6196
6197 -- If what we have is an identifier that references a subprogram
6198 -- formal, or a variable or constant object, then we get the actual
6199 -- subtype from the referenced entity if one has been built.
6200
6201 if Nkind (N) = N_Identifier
6202 and then
6203 (Is_Formal (Entity (N))
6204 or else Ekind (Entity (N)) = E_Constant
6205 or else Ekind (Entity (N)) = E_Variable)
6206 and then Present (Actual_Subtype (Entity (N)))
6207 then
6208 return Actual_Subtype (Entity (N));
6209
6210 -- Actual subtype of unchecked union is always itself. We never need
6211 -- the "real" actual subtype. If we did, we couldn't get it anyway
6212 -- because the discriminant is not available. The restrictions on
6213 -- Unchecked_Union are designed to make sure that this is OK.
6214
6215 elsif Is_Unchecked_Union (Base_Type (Utyp)) then
6216 return Typ;
6217
6218 -- Here for the unconstrained case, we must find actual subtype
6219 -- No actual subtype is available, so we must build it on the fly.
6220
6221 -- Checking the type, not the underlying type, for constrainedness
6222 -- seems to be necessary. Maybe all the tests should be on the type???
6223
6224 elsif (not Is_Constrained (Typ))
6225 and then (Is_Array_Type (Utyp)
6226 or else (Is_Record_Type (Utyp)
6227 and then Has_Discriminants (Utyp)))
6228 and then not Has_Unknown_Discriminants (Utyp)
6229 and then not (Ekind (Utyp) = E_String_Literal_Subtype)
6230 then
6231 -- Nothing to do if in spec expression (why not???)
6232
6233 if In_Spec_Expression then
6234 return Typ;
6235
6236 elsif Is_Private_Type (Typ)
6237 and then not Has_Discriminants (Typ)
6238 then
6239 -- If the type has no discriminants, there is no subtype to
6240 -- build, even if the underlying type is discriminated.
6241
6242 return Typ;
6243
6244 -- Else build the actual subtype
6245
6246 else
6247 Decl := Build_Actual_Subtype (Typ, N);
6248 Atyp := Defining_Identifier (Decl);
6249
6250 -- If Build_Actual_Subtype generated a new declaration then use it
6251
6252 if Atyp /= Typ then
6253
6254 -- The actual subtype is an Itype, so analyze the declaration,
6255 -- but do not attach it to the tree, to get the type defined.
6256
6257 Set_Parent (Decl, N);
6258 Set_Is_Itype (Atyp);
6259 Analyze (Decl, Suppress => All_Checks);
6260 Set_Associated_Node_For_Itype (Atyp, N);
6261 Set_Has_Delayed_Freeze (Atyp, False);
6262
6263 -- We need to freeze the actual subtype immediately. This is
6264 -- needed, because otherwise this Itype will not get frozen
6265 -- at all, and it is always safe to freeze on creation because
6266 -- any associated types must be frozen at this point.
6267
6268 Freeze_Itype (Atyp, N);
6269 return Atyp;
6270
6271 -- Otherwise we did not build a declaration, so return original
6272
6273 else
6274 return Typ;
6275 end if;
6276 end if;
6277
6278 -- For all remaining cases, the actual subtype is the same as
6279 -- the nominal type.
6280
6281 else
6282 return Typ;
6283 end if;
6284 end Get_Actual_Subtype;
6285
6286 -------------------------------------
6287 -- Get_Actual_Subtype_If_Available --
6288 -------------------------------------
6289
6290 function Get_Actual_Subtype_If_Available (N : Node_Id) return Entity_Id is
6291 Typ : constant Entity_Id := Etype (N);
6292
6293 begin
6294 -- If what we have is an identifier that references a subprogram
6295 -- formal, or a variable or constant object, then we get the actual
6296 -- subtype from the referenced entity if one has been built.
6297
6298 if Nkind (N) = N_Identifier
6299 and then
6300 (Is_Formal (Entity (N))
6301 or else Ekind (Entity (N)) = E_Constant
6302 or else Ekind (Entity (N)) = E_Variable)
6303 and then Present (Actual_Subtype (Entity (N)))
6304 then
6305 return Actual_Subtype (Entity (N));
6306
6307 -- Otherwise the Etype of N is returned unchanged
6308
6309 else
6310 return Typ;
6311 end if;
6312 end Get_Actual_Subtype_If_Available;
6313
6314 ------------------------
6315 -- Get_Body_From_Stub --
6316 ------------------------
6317
6318 function Get_Body_From_Stub (N : Node_Id) return Node_Id is
6319 begin
6320 return Proper_Body (Unit (Library_Unit (N)));
6321 end Get_Body_From_Stub;
6322
6323 ---------------------
6324 -- Get_Cursor_Type --
6325 ---------------------
6326
6327 function Get_Cursor_Type
6328 (Aspect : Node_Id;
6329 Typ : Entity_Id) return Entity_Id
6330 is
6331 Assoc : Node_Id;
6332 Func : Entity_Id;
6333 First_Op : Entity_Id;
6334 Cursor : Entity_Id;
6335
6336 begin
6337 -- If error already detected, return
6338
6339 if Error_Posted (Aspect) then
6340 return Any_Type;
6341 end if;
6342
6343 -- The cursor type for an Iterable aspect is the return type of a
6344 -- non-overloaded First primitive operation. Locate association for
6345 -- First.
6346
6347 Assoc := First (Component_Associations (Expression (Aspect)));
6348 First_Op := Any_Id;
6349 while Present (Assoc) loop
6350 if Chars (First (Choices (Assoc))) = Name_First then
6351 First_Op := Expression (Assoc);
6352 exit;
6353 end if;
6354
6355 Next (Assoc);
6356 end loop;
6357
6358 if First_Op = Any_Id then
6359 Error_Msg_N ("aspect Iterable must specify First operation", Aspect);
6360 return Any_Type;
6361 end if;
6362
6363 Cursor := Any_Type;
6364
6365 -- Locate function with desired name and profile in scope of type
6366
6367 Func := First_Entity (Scope (Typ));
6368 while Present (Func) loop
6369 if Chars (Func) = Chars (First_Op)
6370 and then Ekind (Func) = E_Function
6371 and then Present (First_Formal (Func))
6372 and then Etype (First_Formal (Func)) = Typ
6373 and then No (Next_Formal (First_Formal (Func)))
6374 then
6375 if Cursor /= Any_Type then
6376 Error_Msg_N
6377 ("Operation First for iterable type must be unique", Aspect);
6378 return Any_Type;
6379 else
6380 Cursor := Etype (Func);
6381 end if;
6382 end if;
6383
6384 Next_Entity (Func);
6385 end loop;
6386
6387 -- If not found, no way to resolve remaining primitives.
6388
6389 if Cursor = Any_Type then
6390 Error_Msg_N
6391 ("No legal primitive operation First for Iterable type", Aspect);
6392 end if;
6393
6394 return Cursor;
6395 end Get_Cursor_Type;
6396
6397 -------------------------------
6398 -- Get_Default_External_Name --
6399 -------------------------------
6400
6401 function Get_Default_External_Name (E : Node_Or_Entity_Id) return Node_Id is
6402 begin
6403 Get_Decoded_Name_String (Chars (E));
6404
6405 if Opt.External_Name_Imp_Casing = Uppercase then
6406 Set_Casing (All_Upper_Case);
6407 else
6408 Set_Casing (All_Lower_Case);
6409 end if;
6410
6411 return
6412 Make_String_Literal (Sloc (E),
6413 Strval => String_From_Name_Buffer);
6414 end Get_Default_External_Name;
6415
6416 --------------------------
6417 -- Get_Enclosing_Object --
6418 --------------------------
6419
6420 function Get_Enclosing_Object (N : Node_Id) return Entity_Id is
6421 begin
6422 if Is_Entity_Name (N) then
6423 return Entity (N);
6424 else
6425 case Nkind (N) is
6426 when N_Indexed_Component |
6427 N_Slice |
6428 N_Selected_Component =>
6429
6430 -- If not generating code, a dereference may be left implicit.
6431 -- In thoses cases, return Empty.
6432
6433 if Is_Access_Type (Etype (Prefix (N))) then
6434 return Empty;
6435 else
6436 return Get_Enclosing_Object (Prefix (N));
6437 end if;
6438
6439 when N_Type_Conversion =>
6440 return Get_Enclosing_Object (Expression (N));
6441
6442 when others =>
6443 return Empty;
6444 end case;
6445 end if;
6446 end Get_Enclosing_Object;
6447
6448 ---------------------------
6449 -- Get_Enum_Lit_From_Pos --
6450 ---------------------------
6451
6452 function Get_Enum_Lit_From_Pos
6453 (T : Entity_Id;
6454 Pos : Uint;
6455 Loc : Source_Ptr) return Node_Id
6456 is
6457 Btyp : Entity_Id := Base_Type (T);
6458 Lit : Node_Id;
6459
6460 begin
6461 -- In the case where the literal is of type Character, Wide_Character
6462 -- or Wide_Wide_Character or of a type derived from them, there needs
6463 -- to be some special handling since there is no explicit chain of
6464 -- literals to search. Instead, an N_Character_Literal node is created
6465 -- with the appropriate Char_Code and Chars fields.
6466
6467 if Is_Standard_Character_Type (T) then
6468 Set_Character_Literal_Name (UI_To_CC (Pos));
6469 return
6470 Make_Character_Literal (Loc,
6471 Chars => Name_Find,
6472 Char_Literal_Value => Pos);
6473
6474 -- For all other cases, we have a complete table of literals, and
6475 -- we simply iterate through the chain of literal until the one
6476 -- with the desired position value is found.
6477 --
6478
6479 else
6480 if Is_Private_Type (Btyp) and then Present (Full_View (Btyp)) then
6481 Btyp := Full_View (Btyp);
6482 end if;
6483
6484 Lit := First_Literal (Btyp);
6485 for J in 1 .. UI_To_Int (Pos) loop
6486 Next_Literal (Lit);
6487 end loop;
6488
6489 return New_Occurrence_Of (Lit, Loc);
6490 end if;
6491 end Get_Enum_Lit_From_Pos;
6492
6493 ---------------------------------
6494 -- Get_Ensures_From_CTC_Pragma --
6495 ---------------------------------
6496
6497 function Get_Ensures_From_CTC_Pragma (N : Node_Id) return Node_Id is
6498 Args : constant List_Id := Pragma_Argument_Associations (N);
6499 Res : Node_Id;
6500
6501 begin
6502 if List_Length (Args) = 4 then
6503 Res := Pick (Args, 4);
6504
6505 elsif List_Length (Args) = 3 then
6506 Res := Pick (Args, 3);
6507
6508 if Chars (Res) /= Name_Ensures then
6509 Res := Empty;
6510 end if;
6511
6512 else
6513 Res := Empty;
6514 end if;
6515
6516 return Res;
6517 end Get_Ensures_From_CTC_Pragma;
6518
6519 ------------------------
6520 -- Get_Generic_Entity --
6521 ------------------------
6522
6523 function Get_Generic_Entity (N : Node_Id) return Entity_Id is
6524 Ent : constant Entity_Id := Entity (Name (N));
6525 begin
6526 if Present (Renamed_Object (Ent)) then
6527 return Renamed_Object (Ent);
6528 else
6529 return Ent;
6530 end if;
6531 end Get_Generic_Entity;
6532
6533 -------------------------------------
6534 -- Get_Incomplete_View_Of_Ancestor --
6535 -------------------------------------
6536
6537 function Get_Incomplete_View_Of_Ancestor (E : Entity_Id) return Entity_Id is
6538 Cur_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
6539 Par_Scope : Entity_Id;
6540 Par_Type : Entity_Id;
6541
6542 begin
6543 -- The incomplete view of an ancestor is only relevant for private
6544 -- derived types in child units.
6545
6546 if not Is_Derived_Type (E)
6547 or else not Is_Child_Unit (Cur_Unit)
6548 then
6549 return Empty;
6550
6551 else
6552 Par_Scope := Scope (Cur_Unit);
6553 if No (Par_Scope) then
6554 return Empty;
6555 end if;
6556
6557 Par_Type := Etype (Base_Type (E));
6558
6559 -- Traverse list of ancestor types until we find one declared in
6560 -- a parent or grandparent unit (two levels seem sufficient).
6561
6562 while Present (Par_Type) loop
6563 if Scope (Par_Type) = Par_Scope
6564 or else Scope (Par_Type) = Scope (Par_Scope)
6565 then
6566 return Par_Type;
6567
6568 elsif not Is_Derived_Type (Par_Type) then
6569 return Empty;
6570
6571 else
6572 Par_Type := Etype (Base_Type (Par_Type));
6573 end if;
6574 end loop;
6575
6576 -- If none found, there is no relevant ancestor type.
6577
6578 return Empty;
6579 end if;
6580 end Get_Incomplete_View_Of_Ancestor;
6581
6582 ----------------------
6583 -- Get_Index_Bounds --
6584 ----------------------
6585
6586 procedure Get_Index_Bounds (N : Node_Id; L, H : out Node_Id) is
6587 Kind : constant Node_Kind := Nkind (N);
6588 R : Node_Id;
6589
6590 begin
6591 if Kind = N_Range then
6592 L := Low_Bound (N);
6593 H := High_Bound (N);
6594
6595 elsif Kind = N_Subtype_Indication then
6596 R := Range_Expression (Constraint (N));
6597
6598 if R = Error then
6599 L := Error;
6600 H := Error;
6601 return;
6602
6603 else
6604 L := Low_Bound (Range_Expression (Constraint (N)));
6605 H := High_Bound (Range_Expression (Constraint (N)));
6606 end if;
6607
6608 elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
6609 if Error_Posted (Scalar_Range (Entity (N))) then
6610 L := Error;
6611 H := Error;
6612
6613 elsif Nkind (Scalar_Range (Entity (N))) = N_Subtype_Indication then
6614 Get_Index_Bounds (Scalar_Range (Entity (N)), L, H);
6615
6616 else
6617 L := Low_Bound (Scalar_Range (Entity (N)));
6618 H := High_Bound (Scalar_Range (Entity (N)));
6619 end if;
6620
6621 else
6622 -- N is an expression, indicating a range with one value
6623
6624 L := N;
6625 H := N;
6626 end if;
6627 end Get_Index_Bounds;
6628
6629 ---------------------------------
6630 -- Get_Iterable_Type_Primitive --
6631 ---------------------------------
6632
6633 function Get_Iterable_Type_Primitive
6634 (Typ : Entity_Id;
6635 Nam : Name_Id) return Entity_Id
6636 is
6637 Funcs : constant Node_Id := Find_Value_Of_Aspect (Typ, Aspect_Iterable);
6638 Assoc : Node_Id;
6639
6640 begin
6641 if No (Funcs) then
6642 return Empty;
6643
6644 else
6645 Assoc := First (Component_Associations (Funcs));
6646 while Present (Assoc) loop
6647 if Chars (First (Choices (Assoc))) = Nam then
6648 return Entity (Expression (Assoc));
6649 end if;
6650
6651 Assoc := Next (Assoc);
6652 end loop;
6653
6654 return Empty;
6655 end if;
6656 end Get_Iterable_Type_Primitive;
6657
6658 ----------------------------------
6659 -- Get_Library_Unit_Name_string --
6660 ----------------------------------
6661
6662 procedure Get_Library_Unit_Name_String (Decl_Node : Node_Id) is
6663 Unit_Name_Id : constant Unit_Name_Type := Get_Unit_Name (Decl_Node);
6664
6665 begin
6666 Get_Unit_Name_String (Unit_Name_Id);
6667
6668 -- Remove seven last character (" (spec)" or " (body)")
6669
6670 Name_Len := Name_Len - 7;
6671 pragma Assert (Name_Buffer (Name_Len + 1) = ' ');
6672 end Get_Library_Unit_Name_String;
6673
6674 ------------------------
6675 -- Get_Name_Entity_Id --
6676 ------------------------
6677
6678 function Get_Name_Entity_Id (Id : Name_Id) return Entity_Id is
6679 begin
6680 return Entity_Id (Get_Name_Table_Info (Id));
6681 end Get_Name_Entity_Id;
6682
6683 ------------------------------
6684 -- Get_Name_From_CTC_Pragma --
6685 ------------------------------
6686
6687 function Get_Name_From_CTC_Pragma (N : Node_Id) return String_Id is
6688 Arg : constant Node_Id :=
6689 Get_Pragma_Arg (First (Pragma_Argument_Associations (N)));
6690 begin
6691 return Strval (Expr_Value_S (Arg));
6692 end Get_Name_From_CTC_Pragma;
6693
6694 -------------------
6695 -- Get_Pragma_Id --
6696 -------------------
6697
6698 function Get_Pragma_Id (N : Node_Id) return Pragma_Id is
6699 begin
6700 return Get_Pragma_Id (Pragma_Name (N));
6701 end Get_Pragma_Id;
6702
6703 -----------------------
6704 -- Get_Reason_String --
6705 -----------------------
6706
6707 procedure Get_Reason_String (N : Node_Id) is
6708 begin
6709 if Nkind (N) = N_String_Literal then
6710 Store_String_Chars (Strval (N));
6711
6712 elsif Nkind (N) = N_Op_Concat then
6713 Get_Reason_String (Left_Opnd (N));
6714 Get_Reason_String (Right_Opnd (N));
6715
6716 -- If not of required form, error
6717
6718 else
6719 Error_Msg_N
6720 ("Reason for pragma Warnings has wrong form", N);
6721 Error_Msg_N
6722 ("\must be string literal or concatenation of string literals", N);
6723 return;
6724 end if;
6725 end Get_Reason_String;
6726
6727 ---------------------------
6728 -- Get_Referenced_Object --
6729 ---------------------------
6730
6731 function Get_Referenced_Object (N : Node_Id) return Node_Id is
6732 R : Node_Id;
6733
6734 begin
6735 R := N;
6736 while Is_Entity_Name (R)
6737 and then Present (Renamed_Object (Entity (R)))
6738 loop
6739 R := Renamed_Object (Entity (R));
6740 end loop;
6741
6742 return R;
6743 end Get_Referenced_Object;
6744
6745 ------------------------
6746 -- Get_Renamed_Entity --
6747 ------------------------
6748
6749 function Get_Renamed_Entity (E : Entity_Id) return Entity_Id is
6750 R : Entity_Id;
6751
6752 begin
6753 R := E;
6754 while Present (Renamed_Entity (R)) loop
6755 R := Renamed_Entity (R);
6756 end loop;
6757
6758 return R;
6759 end Get_Renamed_Entity;
6760
6761 ----------------------------------
6762 -- Get_Requires_From_CTC_Pragma --
6763 ----------------------------------
6764
6765 function Get_Requires_From_CTC_Pragma (N : Node_Id) return Node_Id is
6766 Args : constant List_Id := Pragma_Argument_Associations (N);
6767 Res : Node_Id;
6768
6769 begin
6770 if List_Length (Args) >= 3 then
6771 Res := Pick (Args, 3);
6772
6773 if Chars (Res) /= Name_Requires then
6774 Res := Empty;
6775 end if;
6776
6777 else
6778 Res := Empty;
6779 end if;
6780
6781 return Res;
6782 end Get_Requires_From_CTC_Pragma;
6783
6784 -------------------------
6785 -- Get_Subprogram_Body --
6786 -------------------------
6787
6788 function Get_Subprogram_Body (E : Entity_Id) return Node_Id is
6789 Decl : Node_Id;
6790
6791 begin
6792 Decl := Unit_Declaration_Node (E);
6793
6794 if Nkind (Decl) = N_Subprogram_Body then
6795 return Decl;
6796
6797 -- The below comment is bad, because it is possible for
6798 -- Nkind (Decl) to be an N_Subprogram_Body_Stub ???
6799
6800 else -- Nkind (Decl) = N_Subprogram_Declaration
6801
6802 if Present (Corresponding_Body (Decl)) then
6803 return Unit_Declaration_Node (Corresponding_Body (Decl));
6804
6805 -- Imported subprogram case
6806
6807 else
6808 return Empty;
6809 end if;
6810 end if;
6811 end Get_Subprogram_Body;
6812
6813 ---------------------------
6814 -- Get_Subprogram_Entity --
6815 ---------------------------
6816
6817 function Get_Subprogram_Entity (Nod : Node_Id) return Entity_Id is
6818 Subp : Node_Id;
6819 Subp_Id : Entity_Id;
6820
6821 begin
6822 if Nkind (Nod) = N_Accept_Statement then
6823 Subp := Entry_Direct_Name (Nod);
6824
6825 elsif Nkind (Nod) = N_Slice then
6826 Subp := Prefix (Nod);
6827
6828 else
6829 Subp := Name (Nod);
6830 end if;
6831
6832 -- Strip the subprogram call
6833
6834 loop
6835 if Nkind_In (Subp, N_Explicit_Dereference,
6836 N_Indexed_Component,
6837 N_Selected_Component)
6838 then
6839 Subp := Prefix (Subp);
6840
6841 elsif Nkind_In (Subp, N_Type_Conversion,
6842 N_Unchecked_Type_Conversion)
6843 then
6844 Subp := Expression (Subp);
6845
6846 else
6847 exit;
6848 end if;
6849 end loop;
6850
6851 -- Extract the entity of the subprogram call
6852
6853 if Is_Entity_Name (Subp) then
6854 Subp_Id := Entity (Subp);
6855
6856 if Ekind (Subp_Id) = E_Access_Subprogram_Type then
6857 Subp_Id := Directly_Designated_Type (Subp_Id);
6858 end if;
6859
6860 if Is_Subprogram (Subp_Id) then
6861 return Subp_Id;
6862 else
6863 return Empty;
6864 end if;
6865
6866 -- The search did not find a construct that denotes a subprogram
6867
6868 else
6869 return Empty;
6870 end if;
6871 end Get_Subprogram_Entity;
6872
6873 -----------------------------
6874 -- Get_Task_Body_Procedure --
6875 -----------------------------
6876
6877 function Get_Task_Body_Procedure (E : Entity_Id) return Node_Id is
6878 begin
6879 -- Note: A task type may be the completion of a private type with
6880 -- discriminants. When performing elaboration checks on a task
6881 -- declaration, the current view of the type may be the private one,
6882 -- and the procedure that holds the body of the task is held in its
6883 -- underlying type.
6884
6885 -- This is an odd function, why not have Task_Body_Procedure do
6886 -- the following digging???
6887
6888 return Task_Body_Procedure (Underlying_Type (Root_Type (E)));
6889 end Get_Task_Body_Procedure;
6890
6891 -----------------------
6892 -- Has_Access_Values --
6893 -----------------------
6894
6895 function Has_Access_Values (T : Entity_Id) return Boolean is
6896 Typ : constant Entity_Id := Underlying_Type (T);
6897
6898 begin
6899 -- Case of a private type which is not completed yet. This can only
6900 -- happen in the case of a generic format type appearing directly, or
6901 -- as a component of the type to which this function is being applied
6902 -- at the top level. Return False in this case, since we certainly do
6903 -- not know that the type contains access types.
6904
6905 if No (Typ) then
6906 return False;
6907
6908 elsif Is_Access_Type (Typ) then
6909 return True;
6910
6911 elsif Is_Array_Type (Typ) then
6912 return Has_Access_Values (Component_Type (Typ));
6913
6914 elsif Is_Record_Type (Typ) then
6915 declare
6916 Comp : Entity_Id;
6917
6918 begin
6919 -- Loop to Check components
6920
6921 Comp := First_Component_Or_Discriminant (Typ);
6922 while Present (Comp) loop
6923
6924 -- Check for access component, tag field does not count, even
6925 -- though it is implemented internally using an access type.
6926
6927 if Has_Access_Values (Etype (Comp))
6928 and then Chars (Comp) /= Name_uTag
6929 then
6930 return True;
6931 end if;
6932
6933 Next_Component_Or_Discriminant (Comp);
6934 end loop;
6935 end;
6936
6937 return False;
6938
6939 else
6940 return False;
6941 end if;
6942 end Has_Access_Values;
6943
6944 ------------------------------
6945 -- Has_Compatible_Alignment --
6946 ------------------------------
6947
6948 function Has_Compatible_Alignment
6949 (Obj : Entity_Id;
6950 Expr : Node_Id) return Alignment_Result
6951 is
6952 function Has_Compatible_Alignment_Internal
6953 (Obj : Entity_Id;
6954 Expr : Node_Id;
6955 Default : Alignment_Result) return Alignment_Result;
6956 -- This is the internal recursive function that actually does the work.
6957 -- There is one additional parameter, which says what the result should
6958 -- be if no alignment information is found, and there is no definite
6959 -- indication of compatible alignments. At the outer level, this is set
6960 -- to Unknown, but for internal recursive calls in the case where types
6961 -- are known to be correct, it is set to Known_Compatible.
6962
6963 ---------------------------------------
6964 -- Has_Compatible_Alignment_Internal --
6965 ---------------------------------------
6966
6967 function Has_Compatible_Alignment_Internal
6968 (Obj : Entity_Id;
6969 Expr : Node_Id;
6970 Default : Alignment_Result) return Alignment_Result
6971 is
6972 Result : Alignment_Result := Known_Compatible;
6973 -- Holds the current status of the result. Note that once a value of
6974 -- Known_Incompatible is set, it is sticky and does not get changed
6975 -- to Unknown (the value in Result only gets worse as we go along,
6976 -- never better).
6977
6978 Offs : Uint := No_Uint;
6979 -- Set to a factor of the offset from the base object when Expr is a
6980 -- selected or indexed component, based on Component_Bit_Offset and
6981 -- Component_Size respectively. A negative value is used to represent
6982 -- a value which is not known at compile time.
6983
6984 procedure Check_Prefix;
6985 -- Checks the prefix recursively in the case where the expression
6986 -- is an indexed or selected component.
6987
6988 procedure Set_Result (R : Alignment_Result);
6989 -- If R represents a worse outcome (unknown instead of known
6990 -- compatible, or known incompatible), then set Result to R.
6991
6992 ------------------
6993 -- Check_Prefix --
6994 ------------------
6995
6996 procedure Check_Prefix is
6997 begin
6998 -- The subtlety here is that in doing a recursive call to check
6999 -- the prefix, we have to decide what to do in the case where we
7000 -- don't find any specific indication of an alignment problem.
7001
7002 -- At the outer level, we normally set Unknown as the result in
7003 -- this case, since we can only set Known_Compatible if we really
7004 -- know that the alignment value is OK, but for the recursive
7005 -- call, in the case where the types match, and we have not
7006 -- specified a peculiar alignment for the object, we are only
7007 -- concerned about suspicious rep clauses, the default case does
7008 -- not affect us, since the compiler will, in the absence of such
7009 -- rep clauses, ensure that the alignment is correct.
7010
7011 if Default = Known_Compatible
7012 or else
7013 (Etype (Obj) = Etype (Expr)
7014 and then (Unknown_Alignment (Obj)
7015 or else
7016 Alignment (Obj) = Alignment (Etype (Obj))))
7017 then
7018 Set_Result
7019 (Has_Compatible_Alignment_Internal
7020 (Obj, Prefix (Expr), Known_Compatible));
7021
7022 -- In all other cases, we need a full check on the prefix
7023
7024 else
7025 Set_Result
7026 (Has_Compatible_Alignment_Internal
7027 (Obj, Prefix (Expr), Unknown));
7028 end if;
7029 end Check_Prefix;
7030
7031 ----------------
7032 -- Set_Result --
7033 ----------------
7034
7035 procedure Set_Result (R : Alignment_Result) is
7036 begin
7037 if R > Result then
7038 Result := R;
7039 end if;
7040 end Set_Result;
7041
7042 -- Start of processing for Has_Compatible_Alignment_Internal
7043
7044 begin
7045 -- If Expr is a selected component, we must make sure there is no
7046 -- potentially troublesome component clause, and that the record is
7047 -- not packed.
7048
7049 if Nkind (Expr) = N_Selected_Component then
7050
7051 -- Packed record always generate unknown alignment
7052
7053 if Is_Packed (Etype (Prefix (Expr))) then
7054 Set_Result (Unknown);
7055 end if;
7056
7057 -- Check prefix and component offset
7058
7059 Check_Prefix;
7060 Offs := Component_Bit_Offset (Entity (Selector_Name (Expr)));
7061
7062 -- If Expr is an indexed component, we must make sure there is no
7063 -- potentially troublesome Component_Size clause and that the array
7064 -- is not bit-packed.
7065
7066 elsif Nkind (Expr) = N_Indexed_Component then
7067 declare
7068 Typ : constant Entity_Id := Etype (Prefix (Expr));
7069 Ind : constant Node_Id := First_Index (Typ);
7070
7071 begin
7072 -- Bit packed array always generates unknown alignment
7073
7074 if Is_Bit_Packed_Array (Typ) then
7075 Set_Result (Unknown);
7076 end if;
7077
7078 -- Check prefix and component offset
7079
7080 Check_Prefix;
7081 Offs := Component_Size (Typ);
7082
7083 -- Small optimization: compute the full offset when possible
7084
7085 if Offs /= No_Uint
7086 and then Offs > Uint_0
7087 and then Present (Ind)
7088 and then Nkind (Ind) = N_Range
7089 and then Compile_Time_Known_Value (Low_Bound (Ind))
7090 and then Compile_Time_Known_Value (First (Expressions (Expr)))
7091 then
7092 Offs := Offs * (Expr_Value (First (Expressions (Expr)))
7093 - Expr_Value (Low_Bound ((Ind))));
7094 end if;
7095 end;
7096 end if;
7097
7098 -- If we have a null offset, the result is entirely determined by
7099 -- the base object and has already been computed recursively.
7100
7101 if Offs = Uint_0 then
7102 null;
7103
7104 -- Case where we know the alignment of the object
7105
7106 elsif Known_Alignment (Obj) then
7107 declare
7108 ObjA : constant Uint := Alignment (Obj);
7109 ExpA : Uint := No_Uint;
7110 SizA : Uint := No_Uint;
7111
7112 begin
7113 -- If alignment of Obj is 1, then we are always OK
7114
7115 if ObjA = 1 then
7116 Set_Result (Known_Compatible);
7117
7118 -- Alignment of Obj is greater than 1, so we need to check
7119
7120 else
7121 -- If we have an offset, see if it is compatible
7122
7123 if Offs /= No_Uint and Offs > Uint_0 then
7124 if Offs mod (System_Storage_Unit * ObjA) /= 0 then
7125 Set_Result (Known_Incompatible);
7126 end if;
7127
7128 -- See if Expr is an object with known alignment
7129
7130 elsif Is_Entity_Name (Expr)
7131 and then Known_Alignment (Entity (Expr))
7132 then
7133 ExpA := Alignment (Entity (Expr));
7134
7135 -- Otherwise, we can use the alignment of the type of
7136 -- Expr given that we already checked for
7137 -- discombobulating rep clauses for the cases of indexed
7138 -- and selected components above.
7139
7140 elsif Known_Alignment (Etype (Expr)) then
7141 ExpA := Alignment (Etype (Expr));
7142
7143 -- Otherwise the alignment is unknown
7144
7145 else
7146 Set_Result (Default);
7147 end if;
7148
7149 -- If we got an alignment, see if it is acceptable
7150
7151 if ExpA /= No_Uint and then ExpA < ObjA then
7152 Set_Result (Known_Incompatible);
7153 end if;
7154
7155 -- If Expr is not a piece of a larger object, see if size
7156 -- is given. If so, check that it is not too small for the
7157 -- required alignment.
7158
7159 if Offs /= No_Uint then
7160 null;
7161
7162 -- See if Expr is an object with known size
7163
7164 elsif Is_Entity_Name (Expr)
7165 and then Known_Static_Esize (Entity (Expr))
7166 then
7167 SizA := Esize (Entity (Expr));
7168
7169 -- Otherwise, we check the object size of the Expr type
7170
7171 elsif Known_Static_Esize (Etype (Expr)) then
7172 SizA := Esize (Etype (Expr));
7173 end if;
7174
7175 -- If we got a size, see if it is a multiple of the Obj
7176 -- alignment, if not, then the alignment cannot be
7177 -- acceptable, since the size is always a multiple of the
7178 -- alignment.
7179
7180 if SizA /= No_Uint then
7181 if SizA mod (ObjA * Ttypes.System_Storage_Unit) /= 0 then
7182 Set_Result (Known_Incompatible);
7183 end if;
7184 end if;
7185 end if;
7186 end;
7187
7188 -- If we do not know required alignment, any non-zero offset is a
7189 -- potential problem (but certainly may be OK, so result is unknown).
7190
7191 elsif Offs /= No_Uint then
7192 Set_Result (Unknown);
7193
7194 -- If we can't find the result by direct comparison of alignment
7195 -- values, then there is still one case that we can determine known
7196 -- result, and that is when we can determine that the types are the
7197 -- same, and no alignments are specified. Then we known that the
7198 -- alignments are compatible, even if we don't know the alignment
7199 -- value in the front end.
7200
7201 elsif Etype (Obj) = Etype (Expr) then
7202
7203 -- Types are the same, but we have to check for possible size
7204 -- and alignments on the Expr object that may make the alignment
7205 -- different, even though the types are the same.
7206
7207 if Is_Entity_Name (Expr) then
7208
7209 -- First check alignment of the Expr object. Any alignment less
7210 -- than Maximum_Alignment is worrisome since this is the case
7211 -- where we do not know the alignment of Obj.
7212
7213 if Known_Alignment (Entity (Expr))
7214 and then
7215 UI_To_Int (Alignment (Entity (Expr))) <
7216 Ttypes.Maximum_Alignment
7217 then
7218 Set_Result (Unknown);
7219
7220 -- Now check size of Expr object. Any size that is not an
7221 -- even multiple of Maximum_Alignment is also worrisome
7222 -- since it may cause the alignment of the object to be less
7223 -- than the alignment of the type.
7224
7225 elsif Known_Static_Esize (Entity (Expr))
7226 and then
7227 (UI_To_Int (Esize (Entity (Expr))) mod
7228 (Ttypes.Maximum_Alignment * Ttypes.System_Storage_Unit))
7229 /= 0
7230 then
7231 Set_Result (Unknown);
7232
7233 -- Otherwise same type is decisive
7234
7235 else
7236 Set_Result (Known_Compatible);
7237 end if;
7238 end if;
7239
7240 -- Another case to deal with is when there is an explicit size or
7241 -- alignment clause when the types are not the same. If so, then the
7242 -- result is Unknown. We don't need to do this test if the Default is
7243 -- Unknown, since that result will be set in any case.
7244
7245 elsif Default /= Unknown
7246 and then (Has_Size_Clause (Etype (Expr))
7247 or else
7248 Has_Alignment_Clause (Etype (Expr)))
7249 then
7250 Set_Result (Unknown);
7251
7252 -- If no indication found, set default
7253
7254 else
7255 Set_Result (Default);
7256 end if;
7257
7258 -- Return worst result found
7259
7260 return Result;
7261 end Has_Compatible_Alignment_Internal;
7262
7263 -- Start of processing for Has_Compatible_Alignment
7264
7265 begin
7266 -- If Obj has no specified alignment, then set alignment from the type
7267 -- alignment. Perhaps we should always do this, but for sure we should
7268 -- do it when there is an address clause since we can do more if the
7269 -- alignment is known.
7270
7271 if Unknown_Alignment (Obj) then
7272 Set_Alignment (Obj, Alignment (Etype (Obj)));
7273 end if;
7274
7275 -- Now do the internal call that does all the work
7276
7277 return Has_Compatible_Alignment_Internal (Obj, Expr, Unknown);
7278 end Has_Compatible_Alignment;
7279
7280 ----------------------
7281 -- Has_Declarations --
7282 ----------------------
7283
7284 function Has_Declarations (N : Node_Id) return Boolean is
7285 begin
7286 return Nkind_In (Nkind (N), N_Accept_Statement,
7287 N_Block_Statement,
7288 N_Compilation_Unit_Aux,
7289 N_Entry_Body,
7290 N_Package_Body,
7291 N_Protected_Body,
7292 N_Subprogram_Body,
7293 N_Task_Body,
7294 N_Package_Specification);
7295 end Has_Declarations;
7296
7297 -------------------
7298 -- Has_Denormals --
7299 -------------------
7300
7301 function Has_Denormals (E : Entity_Id) return Boolean is
7302 begin
7303 return Is_Floating_Point_Type (E)
7304 and then Denorm_On_Target
7305 and then not Vax_Float (E);
7306 end Has_Denormals;
7307
7308 -------------------------------------------
7309 -- Has_Discriminant_Dependent_Constraint --
7310 -------------------------------------------
7311
7312 function Has_Discriminant_Dependent_Constraint
7313 (Comp : Entity_Id) return Boolean
7314 is
7315 Comp_Decl : constant Node_Id := Parent (Comp);
7316 Subt_Indic : Node_Id;
7317 Constr : Node_Id;
7318 Assn : Node_Id;
7319
7320 begin
7321 -- Discriminants can't depend on discriminants
7322
7323 if Ekind (Comp) = E_Discriminant then
7324 return False;
7325
7326 else
7327 Subt_Indic := Subtype_Indication (Component_Definition (Comp_Decl));
7328
7329 if Nkind (Subt_Indic) = N_Subtype_Indication then
7330 Constr := Constraint (Subt_Indic);
7331
7332 if Nkind (Constr) = N_Index_Or_Discriminant_Constraint then
7333 Assn := First (Constraints (Constr));
7334 while Present (Assn) loop
7335 case Nkind (Assn) is
7336 when N_Subtype_Indication |
7337 N_Range |
7338 N_Identifier
7339 =>
7340 if Depends_On_Discriminant (Assn) then
7341 return True;
7342 end if;
7343
7344 when N_Discriminant_Association =>
7345 if Depends_On_Discriminant (Expression (Assn)) then
7346 return True;
7347 end if;
7348
7349 when others =>
7350 null;
7351 end case;
7352
7353 Next (Assn);
7354 end loop;
7355 end if;
7356 end if;
7357 end if;
7358
7359 return False;
7360 end Has_Discriminant_Dependent_Constraint;
7361
7362 --------------------------
7363 -- Has_Enabled_Property --
7364 --------------------------
7365
7366 function Has_Enabled_Property
7367 (Item_Id : Entity_Id;
7368 Property : Name_Id) return Boolean
7369 is
7370 function State_Has_Enabled_Property return Boolean;
7371 -- Determine whether a state denoted by Item_Id has the property
7372
7373 function Variable_Has_Enabled_Property return Boolean;
7374 -- Determine whether a variable denoted by Item_Id has the property
7375
7376 --------------------------------
7377 -- State_Has_Enabled_Property --
7378 --------------------------------
7379
7380 function State_Has_Enabled_Property return Boolean is
7381 Decl : constant Node_Id := Parent (Item_Id);
7382 Opt : Node_Id;
7383 Opt_Nam : Node_Id;
7384 Prop : Node_Id;
7385 Prop_Nam : Node_Id;
7386 Props : Node_Id;
7387
7388 begin
7389 -- The declaration of an external abstract state appears as an
7390 -- extension aggregate. If this is not the case, properties can never
7391 -- be set.
7392
7393 if Nkind (Decl) /= N_Extension_Aggregate then
7394 return False;
7395 end if;
7396
7397 -- When External appears as a simple option, it automatically enables
7398 -- all properties.
7399
7400 Opt := First (Expressions (Decl));
7401 while Present (Opt) loop
7402 if Nkind (Opt) = N_Identifier
7403 and then Chars (Opt) = Name_External
7404 then
7405 return True;
7406 end if;
7407
7408 Next (Opt);
7409 end loop;
7410
7411 -- When External specifies particular properties, inspect those and
7412 -- find the desired one (if any).
7413
7414 Opt := First (Component_Associations (Decl));
7415 while Present (Opt) loop
7416 Opt_Nam := First (Choices (Opt));
7417
7418 if Nkind (Opt_Nam) = N_Identifier
7419 and then Chars (Opt_Nam) = Name_External
7420 then
7421 Props := Expression (Opt);
7422
7423 -- Multiple properties appear as an aggregate
7424
7425 if Nkind (Props) = N_Aggregate then
7426
7427 -- Simple property form
7428
7429 Prop := First (Expressions (Props));
7430 while Present (Prop) loop
7431 if Chars (Prop) = Property then
7432 return True;
7433 end if;
7434
7435 Next (Prop);
7436 end loop;
7437
7438 -- Property with expression form
7439
7440 Prop := First (Component_Associations (Props));
7441 while Present (Prop) loop
7442 Prop_Nam := First (Choices (Prop));
7443
7444 -- The property can be represented in two ways:
7445 -- others => <value>
7446 -- <property> => <value>
7447
7448 if Nkind (Prop_Nam) = N_Others_Choice
7449 or else (Nkind (Prop_Nam) = N_Identifier
7450 and then Chars (Prop_Nam) = Property)
7451 then
7452 return Is_True (Expr_Value (Expression (Prop)));
7453 end if;
7454
7455 Next (Prop);
7456 end loop;
7457
7458 -- Single property
7459
7460 else
7461 return Chars (Props) = Property;
7462 end if;
7463 end if;
7464
7465 Next (Opt);
7466 end loop;
7467
7468 return False;
7469 end State_Has_Enabled_Property;
7470
7471 -----------------------------------
7472 -- Variable_Has_Enabled_Property --
7473 -----------------------------------
7474
7475 function Variable_Has_Enabled_Property return Boolean is
7476 AR : constant Node_Id :=
7477 Get_Pragma (Item_Id, Pragma_Async_Readers);
7478 AW : constant Node_Id :=
7479 Get_Pragma (Item_Id, Pragma_Async_Writers);
7480 ER : constant Node_Id :=
7481 Get_Pragma (Item_Id, Pragma_Effective_Reads);
7482 EW : constant Node_Id :=
7483 Get_Pragma (Item_Id, Pragma_Effective_Writes);
7484 begin
7485 -- A non-volatile object can never possess external properties
7486
7487 if not Is_SPARK_Volatile (Item_Id) then
7488 return False;
7489
7490 -- External properties related to variables come in two flavors -
7491 -- explicit and implicit. The explicit case is characterized by the
7492 -- presence of a property pragma while the implicit case lacks all
7493 -- such pragmas.
7494
7495 elsif Property = Name_Async_Readers
7496 and then
7497 (Present (AR)
7498 or else
7499 (No (AW) and then No (ER) and then No (EW)))
7500 then
7501 return True;
7502
7503 elsif Property = Name_Async_Writers
7504 and then
7505 (Present (AW)
7506 or else
7507 (No (AR) and then No (ER) and then No (EW)))
7508 then
7509 return True;
7510
7511 elsif Property = Name_Effective_Reads
7512 and then
7513 (Present (ER)
7514 or else
7515 (No (AR) and then No (AW) and then No (EW)))
7516 then
7517 return True;
7518
7519 elsif Property = Name_Effective_Writes
7520 and then
7521 (Present (EW) or else (No (AR) and then No (AW) and then No (ER)))
7522 then
7523 return True;
7524
7525 else
7526 return False;
7527 end if;
7528 end Variable_Has_Enabled_Property;
7529
7530 -- Start of processing for Has_Enabled_Property
7531
7532 begin
7533 -- Abstract states and variables have a flexible scheme of specifying
7534 -- external properties.
7535
7536 if Ekind (Item_Id) = E_Abstract_State then
7537 return State_Has_Enabled_Property;
7538
7539 elsif Ekind (Item_Id) = E_Variable then
7540 return Variable_Has_Enabled_Property;
7541
7542 -- Otherwise a property is enabled when the related object is volatile
7543
7544 else
7545 return Is_SPARK_Volatile (Item_Id);
7546 end if;
7547 end Has_Enabled_Property;
7548
7549 --------------------
7550 -- Has_Infinities --
7551 --------------------
7552
7553 function Has_Infinities (E : Entity_Id) return Boolean is
7554 begin
7555 return
7556 Is_Floating_Point_Type (E)
7557 and then Nkind (Scalar_Range (E)) = N_Range
7558 and then Includes_Infinities (Scalar_Range (E));
7559 end Has_Infinities;
7560
7561 --------------------
7562 -- Has_Interfaces --
7563 --------------------
7564
7565 function Has_Interfaces
7566 (T : Entity_Id;
7567 Use_Full_View : Boolean := True) return Boolean
7568 is
7569 Typ : Entity_Id := Base_Type (T);
7570
7571 begin
7572 -- Handle concurrent types
7573
7574 if Is_Concurrent_Type (Typ) then
7575 Typ := Corresponding_Record_Type (Typ);
7576 end if;
7577
7578 if not Present (Typ)
7579 or else not Is_Record_Type (Typ)
7580 or else not Is_Tagged_Type (Typ)
7581 then
7582 return False;
7583 end if;
7584
7585 -- Handle private types
7586
7587 if Use_Full_View
7588 and then Present (Full_View (Typ))
7589 then
7590 Typ := Full_View (Typ);
7591 end if;
7592
7593 -- Handle concurrent record types
7594
7595 if Is_Concurrent_Record_Type (Typ)
7596 and then Is_Non_Empty_List (Abstract_Interface_List (Typ))
7597 then
7598 return True;
7599 end if;
7600
7601 loop
7602 if Is_Interface (Typ)
7603 or else
7604 (Is_Record_Type (Typ)
7605 and then Present (Interfaces (Typ))
7606 and then not Is_Empty_Elmt_List (Interfaces (Typ)))
7607 then
7608 return True;
7609 end if;
7610
7611 exit when Etype (Typ) = Typ
7612
7613 -- Handle private types
7614
7615 or else (Present (Full_View (Etype (Typ)))
7616 and then Full_View (Etype (Typ)) = Typ)
7617
7618 -- Protect the frontend against wrong source with cyclic
7619 -- derivations
7620
7621 or else Etype (Typ) = T;
7622
7623 -- Climb to the ancestor type handling private types
7624
7625 if Present (Full_View (Etype (Typ))) then
7626 Typ := Full_View (Etype (Typ));
7627 else
7628 Typ := Etype (Typ);
7629 end if;
7630 end loop;
7631
7632 return False;
7633 end Has_Interfaces;
7634
7635 ---------------------------------
7636 -- Has_No_Obvious_Side_Effects --
7637 ---------------------------------
7638
7639 function Has_No_Obvious_Side_Effects (N : Node_Id) return Boolean is
7640 begin
7641 -- For now, just handle literals, constants, and non-volatile
7642 -- variables and expressions combining these with operators or
7643 -- short circuit forms.
7644
7645 if Nkind (N) in N_Numeric_Or_String_Literal then
7646 return True;
7647
7648 elsif Nkind (N) = N_Character_Literal then
7649 return True;
7650
7651 elsif Nkind (N) in N_Unary_Op then
7652 return Has_No_Obvious_Side_Effects (Right_Opnd (N));
7653
7654 elsif Nkind (N) in N_Binary_Op or else Nkind (N) in N_Short_Circuit then
7655 return Has_No_Obvious_Side_Effects (Left_Opnd (N))
7656 and then
7657 Has_No_Obvious_Side_Effects (Right_Opnd (N));
7658
7659 elsif Nkind (N) = N_Expression_With_Actions
7660 and then
7661 Is_Empty_List (Actions (N))
7662 then
7663 return Has_No_Obvious_Side_Effects (Expression (N));
7664
7665 elsif Nkind (N) in N_Has_Entity then
7666 return Present (Entity (N))
7667 and then Ekind_In (Entity (N), E_Variable,
7668 E_Constant,
7669 E_Enumeration_Literal,
7670 E_In_Parameter,
7671 E_Out_Parameter,
7672 E_In_Out_Parameter)
7673 and then not Is_Volatile (Entity (N));
7674
7675 else
7676 return False;
7677 end if;
7678 end Has_No_Obvious_Side_Effects;
7679
7680 ------------------------
7681 -- Has_Null_Exclusion --
7682 ------------------------
7683
7684 function Has_Null_Exclusion (N : Node_Id) return Boolean is
7685 begin
7686 case Nkind (N) is
7687 when N_Access_Definition |
7688 N_Access_Function_Definition |
7689 N_Access_Procedure_Definition |
7690 N_Access_To_Object_Definition |
7691 N_Allocator |
7692 N_Derived_Type_Definition |
7693 N_Function_Specification |
7694 N_Subtype_Declaration =>
7695 return Null_Exclusion_Present (N);
7696
7697 when N_Component_Definition |
7698 N_Formal_Object_Declaration |
7699 N_Object_Renaming_Declaration =>
7700 if Present (Subtype_Mark (N)) then
7701 return Null_Exclusion_Present (N);
7702 else pragma Assert (Present (Access_Definition (N)));
7703 return Null_Exclusion_Present (Access_Definition (N));
7704 end if;
7705
7706 when N_Discriminant_Specification =>
7707 if Nkind (Discriminant_Type (N)) = N_Access_Definition then
7708 return Null_Exclusion_Present (Discriminant_Type (N));
7709 else
7710 return Null_Exclusion_Present (N);
7711 end if;
7712
7713 when N_Object_Declaration =>
7714 if Nkind (Object_Definition (N)) = N_Access_Definition then
7715 return Null_Exclusion_Present (Object_Definition (N));
7716 else
7717 return Null_Exclusion_Present (N);
7718 end if;
7719
7720 when N_Parameter_Specification =>
7721 if Nkind (Parameter_Type (N)) = N_Access_Definition then
7722 return Null_Exclusion_Present (Parameter_Type (N));
7723 else
7724 return Null_Exclusion_Present (N);
7725 end if;
7726
7727 when others =>
7728 return False;
7729
7730 end case;
7731 end Has_Null_Exclusion;
7732
7733 ------------------------
7734 -- Has_Null_Extension --
7735 ------------------------
7736
7737 function Has_Null_Extension (T : Entity_Id) return Boolean is
7738 B : constant Entity_Id := Base_Type (T);
7739 Comps : Node_Id;
7740 Ext : Node_Id;
7741
7742 begin
7743 if Nkind (Parent (B)) = N_Full_Type_Declaration
7744 and then Present (Record_Extension_Part (Type_Definition (Parent (B))))
7745 then
7746 Ext := Record_Extension_Part (Type_Definition (Parent (B)));
7747
7748 if Present (Ext) then
7749 if Null_Present (Ext) then
7750 return True;
7751 else
7752 Comps := Component_List (Ext);
7753
7754 -- The null component list is rewritten during analysis to
7755 -- include the parent component. Any other component indicates
7756 -- that the extension was not originally null.
7757
7758 return Null_Present (Comps)
7759 or else No (Next (First (Component_Items (Comps))));
7760 end if;
7761 else
7762 return False;
7763 end if;
7764
7765 else
7766 return False;
7767 end if;
7768 end Has_Null_Extension;
7769
7770 -------------------------------
7771 -- Has_Overriding_Initialize --
7772 -------------------------------
7773
7774 function Has_Overriding_Initialize (T : Entity_Id) return Boolean is
7775 BT : constant Entity_Id := Base_Type (T);
7776 P : Elmt_Id;
7777
7778 begin
7779 if Is_Controlled (BT) then
7780 if Is_RTU (Scope (BT), Ada_Finalization) then
7781 return False;
7782
7783 elsif Present (Primitive_Operations (BT)) then
7784 P := First_Elmt (Primitive_Operations (BT));
7785 while Present (P) loop
7786 declare
7787 Init : constant Entity_Id := Node (P);
7788 Formal : constant Entity_Id := First_Formal (Init);
7789 begin
7790 if Ekind (Init) = E_Procedure
7791 and then Chars (Init) = Name_Initialize
7792 and then Comes_From_Source (Init)
7793 and then Present (Formal)
7794 and then Etype (Formal) = BT
7795 and then No (Next_Formal (Formal))
7796 and then (Ada_Version < Ada_2012
7797 or else not Null_Present (Parent (Init)))
7798 then
7799 return True;
7800 end if;
7801 end;
7802
7803 Next_Elmt (P);
7804 end loop;
7805 end if;
7806
7807 -- Here if type itself does not have a non-null Initialize operation:
7808 -- check immediate ancestor.
7809
7810 if Is_Derived_Type (BT)
7811 and then Has_Overriding_Initialize (Etype (BT))
7812 then
7813 return True;
7814 end if;
7815 end if;
7816
7817 return False;
7818 end Has_Overriding_Initialize;
7819
7820 --------------------------------------
7821 -- Has_Preelaborable_Initialization --
7822 --------------------------------------
7823
7824 function Has_Preelaborable_Initialization (E : Entity_Id) return Boolean is
7825 Has_PE : Boolean;
7826
7827 procedure Check_Components (E : Entity_Id);
7828 -- Check component/discriminant chain, sets Has_PE False if a component
7829 -- or discriminant does not meet the preelaborable initialization rules.
7830
7831 ----------------------
7832 -- Check_Components --
7833 ----------------------
7834
7835 procedure Check_Components (E : Entity_Id) is
7836 Ent : Entity_Id;
7837 Exp : Node_Id;
7838
7839 function Is_Preelaborable_Expression (N : Node_Id) return Boolean;
7840 -- Returns True if and only if the expression denoted by N does not
7841 -- violate restrictions on preelaborable constructs (RM-10.2.1(5-9)).
7842
7843 ---------------------------------
7844 -- Is_Preelaborable_Expression --
7845 ---------------------------------
7846
7847 function Is_Preelaborable_Expression (N : Node_Id) return Boolean is
7848 Exp : Node_Id;
7849 Assn : Node_Id;
7850 Choice : Node_Id;
7851 Comp_Type : Entity_Id;
7852 Is_Array_Aggr : Boolean;
7853
7854 begin
7855 if Is_Static_Expression (N) then
7856 return True;
7857
7858 elsif Nkind (N) = N_Null then
7859 return True;
7860
7861 -- Attributes are allowed in general, even if their prefix is a
7862 -- formal type. (It seems that certain attributes known not to be
7863 -- static might not be allowed, but there are no rules to prevent
7864 -- them.)
7865
7866 elsif Nkind (N) = N_Attribute_Reference then
7867 return True;
7868
7869 -- The name of a discriminant evaluated within its parent type is
7870 -- defined to be preelaborable (10.2.1(8)). Note that we test for
7871 -- names that denote discriminals as well as discriminants to
7872 -- catch references occurring within init procs.
7873
7874 elsif Is_Entity_Name (N)
7875 and then
7876 (Ekind (Entity (N)) = E_Discriminant
7877 or else
7878 ((Ekind (Entity (N)) = E_Constant
7879 or else Ekind (Entity (N)) = E_In_Parameter)
7880 and then Present (Discriminal_Link (Entity (N)))))
7881 then
7882 return True;
7883
7884 elsif Nkind (N) = N_Qualified_Expression then
7885 return Is_Preelaborable_Expression (Expression (N));
7886
7887 -- For aggregates we have to check that each of the associations
7888 -- is preelaborable.
7889
7890 elsif Nkind (N) = N_Aggregate
7891 or else Nkind (N) = N_Extension_Aggregate
7892 then
7893 Is_Array_Aggr := Is_Array_Type (Etype (N));
7894
7895 if Is_Array_Aggr then
7896 Comp_Type := Component_Type (Etype (N));
7897 end if;
7898
7899 -- Check the ancestor part of extension aggregates, which must
7900 -- be either the name of a type that has preelaborable init or
7901 -- an expression that is preelaborable.
7902
7903 if Nkind (N) = N_Extension_Aggregate then
7904 declare
7905 Anc_Part : constant Node_Id := Ancestor_Part (N);
7906
7907 begin
7908 if Is_Entity_Name (Anc_Part)
7909 and then Is_Type (Entity (Anc_Part))
7910 then
7911 if not Has_Preelaborable_Initialization
7912 (Entity (Anc_Part))
7913 then
7914 return False;
7915 end if;
7916
7917 elsif not Is_Preelaborable_Expression (Anc_Part) then
7918 return False;
7919 end if;
7920 end;
7921 end if;
7922
7923 -- Check positional associations
7924
7925 Exp := First (Expressions (N));
7926 while Present (Exp) loop
7927 if not Is_Preelaborable_Expression (Exp) then
7928 return False;
7929 end if;
7930
7931 Next (Exp);
7932 end loop;
7933
7934 -- Check named associations
7935
7936 Assn := First (Component_Associations (N));
7937 while Present (Assn) loop
7938 Choice := First (Choices (Assn));
7939 while Present (Choice) loop
7940 if Is_Array_Aggr then
7941 if Nkind (Choice) = N_Others_Choice then
7942 null;
7943
7944 elsif Nkind (Choice) = N_Range then
7945 if not Is_Static_Range (Choice) then
7946 return False;
7947 end if;
7948
7949 elsif not Is_Static_Expression (Choice) then
7950 return False;
7951 end if;
7952
7953 else
7954 Comp_Type := Etype (Choice);
7955 end if;
7956
7957 Next (Choice);
7958 end loop;
7959
7960 -- If the association has a <> at this point, then we have
7961 -- to check whether the component's type has preelaborable
7962 -- initialization. Note that this only occurs when the
7963 -- association's corresponding component does not have a
7964 -- default expression, the latter case having already been
7965 -- expanded as an expression for the association.
7966
7967 if Box_Present (Assn) then
7968 if not Has_Preelaborable_Initialization (Comp_Type) then
7969 return False;
7970 end if;
7971
7972 -- In the expression case we check whether the expression
7973 -- is preelaborable.
7974
7975 elsif
7976 not Is_Preelaborable_Expression (Expression (Assn))
7977 then
7978 return False;
7979 end if;
7980
7981 Next (Assn);
7982 end loop;
7983
7984 -- If we get here then aggregate as a whole is preelaborable
7985
7986 return True;
7987
7988 -- All other cases are not preelaborable
7989
7990 else
7991 return False;
7992 end if;
7993 end Is_Preelaborable_Expression;
7994
7995 -- Start of processing for Check_Components
7996
7997 begin
7998 -- Loop through entities of record or protected type
7999
8000 Ent := E;
8001 while Present (Ent) loop
8002
8003 -- We are interested only in components and discriminants
8004
8005 Exp := Empty;
8006
8007 case Ekind (Ent) is
8008 when E_Component =>
8009
8010 -- Get default expression if any. If there is no declaration
8011 -- node, it means we have an internal entity. The parent and
8012 -- tag fields are examples of such entities. For such cases,
8013 -- we just test the type of the entity.
8014
8015 if Present (Declaration_Node (Ent)) then
8016 Exp := Expression (Declaration_Node (Ent));
8017 end if;
8018
8019 when E_Discriminant =>
8020
8021 -- Note: for a renamed discriminant, the Declaration_Node
8022 -- may point to the one from the ancestor, and have a
8023 -- different expression, so use the proper attribute to
8024 -- retrieve the expression from the derived constraint.
8025
8026 Exp := Discriminant_Default_Value (Ent);
8027
8028 when others =>
8029 goto Check_Next_Entity;
8030 end case;
8031
8032 -- A component has PI if it has no default expression and the
8033 -- component type has PI.
8034
8035 if No (Exp) then
8036 if not Has_Preelaborable_Initialization (Etype (Ent)) then
8037 Has_PE := False;
8038 exit;
8039 end if;
8040
8041 -- Require the default expression to be preelaborable
8042
8043 elsif not Is_Preelaborable_Expression (Exp) then
8044 Has_PE := False;
8045 exit;
8046 end if;
8047
8048 <<Check_Next_Entity>>
8049 Next_Entity (Ent);
8050 end loop;
8051 end Check_Components;
8052
8053 -- Start of processing for Has_Preelaborable_Initialization
8054
8055 begin
8056 -- Immediate return if already marked as known preelaborable init. This
8057 -- covers types for which this function has already been called once
8058 -- and returned True (in which case the result is cached), and also
8059 -- types to which a pragma Preelaborable_Initialization applies.
8060
8061 if Known_To_Have_Preelab_Init (E) then
8062 return True;
8063 end if;
8064
8065 -- If the type is a subtype representing a generic actual type, then
8066 -- test whether its base type has preelaborable initialization since
8067 -- the subtype representing the actual does not inherit this attribute
8068 -- from the actual or formal. (but maybe it should???)
8069
8070 if Is_Generic_Actual_Type (E) then
8071 return Has_Preelaborable_Initialization (Base_Type (E));
8072 end if;
8073
8074 -- All elementary types have preelaborable initialization
8075
8076 if Is_Elementary_Type (E) then
8077 Has_PE := True;
8078
8079 -- Array types have PI if the component type has PI
8080
8081 elsif Is_Array_Type (E) then
8082 Has_PE := Has_Preelaborable_Initialization (Component_Type (E));
8083
8084 -- A derived type has preelaborable initialization if its parent type
8085 -- has preelaborable initialization and (in the case of a derived record
8086 -- extension) if the non-inherited components all have preelaborable
8087 -- initialization. However, a user-defined controlled type with an
8088 -- overriding Initialize procedure does not have preelaborable
8089 -- initialization.
8090
8091 elsif Is_Derived_Type (E) then
8092
8093 -- If the derived type is a private extension then it doesn't have
8094 -- preelaborable initialization.
8095
8096 if Ekind (Base_Type (E)) = E_Record_Type_With_Private then
8097 return False;
8098 end if;
8099
8100 -- First check whether ancestor type has preelaborable initialization
8101
8102 Has_PE := Has_Preelaborable_Initialization (Etype (Base_Type (E)));
8103
8104 -- If OK, check extension components (if any)
8105
8106 if Has_PE and then Is_Record_Type (E) then
8107 Check_Components (First_Entity (E));
8108 end if;
8109
8110 -- Check specifically for 10.2.1(11.4/2) exception: a controlled type
8111 -- with a user defined Initialize procedure does not have PI.
8112
8113 if Has_PE
8114 and then Is_Controlled (E)
8115 and then Has_Overriding_Initialize (E)
8116 then
8117 Has_PE := False;
8118 end if;
8119
8120 -- Private types not derived from a type having preelaborable init and
8121 -- that are not marked with pragma Preelaborable_Initialization do not
8122 -- have preelaborable initialization.
8123
8124 elsif Is_Private_Type (E) then
8125 return False;
8126
8127 -- Record type has PI if it is non private and all components have PI
8128
8129 elsif Is_Record_Type (E) then
8130 Has_PE := True;
8131 Check_Components (First_Entity (E));
8132
8133 -- Protected types must not have entries, and components must meet
8134 -- same set of rules as for record components.
8135
8136 elsif Is_Protected_Type (E) then
8137 if Has_Entries (E) then
8138 Has_PE := False;
8139 else
8140 Has_PE := True;
8141 Check_Components (First_Entity (E));
8142 Check_Components (First_Private_Entity (E));
8143 end if;
8144
8145 -- Type System.Address always has preelaborable initialization
8146
8147 elsif Is_RTE (E, RE_Address) then
8148 Has_PE := True;
8149
8150 -- In all other cases, type does not have preelaborable initialization
8151
8152 else
8153 return False;
8154 end if;
8155
8156 -- If type has preelaborable initialization, cache result
8157
8158 if Has_PE then
8159 Set_Known_To_Have_Preelab_Init (E);
8160 end if;
8161
8162 return Has_PE;
8163 end Has_Preelaborable_Initialization;
8164
8165 ---------------------------
8166 -- Has_Private_Component --
8167 ---------------------------
8168
8169 function Has_Private_Component (Type_Id : Entity_Id) return Boolean is
8170 Btype : Entity_Id := Base_Type (Type_Id);
8171 Component : Entity_Id;
8172
8173 begin
8174 if Error_Posted (Type_Id)
8175 or else Error_Posted (Btype)
8176 then
8177 return False;
8178 end if;
8179
8180 if Is_Class_Wide_Type (Btype) then
8181 Btype := Root_Type (Btype);
8182 end if;
8183
8184 if Is_Private_Type (Btype) then
8185 declare
8186 UT : constant Entity_Id := Underlying_Type (Btype);
8187 begin
8188 if No (UT) then
8189 if No (Full_View (Btype)) then
8190 return not Is_Generic_Type (Btype)
8191 and then not Is_Generic_Type (Root_Type (Btype));
8192 else
8193 return not Is_Generic_Type (Root_Type (Full_View (Btype)));
8194 end if;
8195 else
8196 return not Is_Frozen (UT) and then Has_Private_Component (UT);
8197 end if;
8198 end;
8199
8200 elsif Is_Array_Type (Btype) then
8201 return Has_Private_Component (Component_Type (Btype));
8202
8203 elsif Is_Record_Type (Btype) then
8204 Component := First_Component (Btype);
8205 while Present (Component) loop
8206 if Has_Private_Component (Etype (Component)) then
8207 return True;
8208 end if;
8209
8210 Next_Component (Component);
8211 end loop;
8212
8213 return False;
8214
8215 elsif Is_Protected_Type (Btype)
8216 and then Present (Corresponding_Record_Type (Btype))
8217 then
8218 return Has_Private_Component (Corresponding_Record_Type (Btype));
8219
8220 else
8221 return False;
8222 end if;
8223 end Has_Private_Component;
8224
8225 ----------------------
8226 -- Has_Signed_Zeros --
8227 ----------------------
8228
8229 function Has_Signed_Zeros (E : Entity_Id) return Boolean is
8230 begin
8231 return Is_Floating_Point_Type (E)
8232 and then Signed_Zeros_On_Target
8233 and then not Vax_Float (E);
8234 end Has_Signed_Zeros;
8235
8236 -----------------------------
8237 -- Has_Static_Array_Bounds --
8238 -----------------------------
8239
8240 function Has_Static_Array_Bounds (Typ : Node_Id) return Boolean is
8241 Ndims : constant Nat := Number_Dimensions (Typ);
8242
8243 Index : Node_Id;
8244 Low : Node_Id;
8245 High : Node_Id;
8246
8247 begin
8248 -- Unconstrained types do not have static bounds
8249
8250 if not Is_Constrained (Typ) then
8251 return False;
8252 end if;
8253
8254 -- First treat string literals specially, as the lower bound and length
8255 -- of string literals are not stored like those of arrays.
8256
8257 -- A string literal always has static bounds
8258
8259 if Ekind (Typ) = E_String_Literal_Subtype then
8260 return True;
8261 end if;
8262
8263 -- Treat all dimensions in turn
8264
8265 Index := First_Index (Typ);
8266 for Indx in 1 .. Ndims loop
8267
8268 -- In case of an illegal index which is not a discrete type, return
8269 -- that the type is not static.
8270
8271 if not Is_Discrete_Type (Etype (Index))
8272 or else Etype (Index) = Any_Type
8273 then
8274 return False;
8275 end if;
8276
8277 Get_Index_Bounds (Index, Low, High);
8278
8279 if Error_Posted (Low) or else Error_Posted (High) then
8280 return False;
8281 end if;
8282
8283 if Is_OK_Static_Expression (Low)
8284 and then
8285 Is_OK_Static_Expression (High)
8286 then
8287 null;
8288 else
8289 return False;
8290 end if;
8291
8292 Next (Index);
8293 end loop;
8294
8295 -- If we fall through the loop, all indexes matched
8296
8297 return True;
8298 end Has_Static_Array_Bounds;
8299
8300 ----------------
8301 -- Has_Stream --
8302 ----------------
8303
8304 function Has_Stream (T : Entity_Id) return Boolean is
8305 E : Entity_Id;
8306
8307 begin
8308 if No (T) then
8309 return False;
8310
8311 elsif Is_RTE (Root_Type (T), RE_Root_Stream_Type) then
8312 return True;
8313
8314 elsif Is_Array_Type (T) then
8315 return Has_Stream (Component_Type (T));
8316
8317 elsif Is_Record_Type (T) then
8318 E := First_Component (T);
8319 while Present (E) loop
8320 if Has_Stream (Etype (E)) then
8321 return True;
8322 else
8323 Next_Component (E);
8324 end if;
8325 end loop;
8326
8327 return False;
8328
8329 elsif Is_Private_Type (T) then
8330 return Has_Stream (Underlying_Type (T));
8331
8332 else
8333 return False;
8334 end if;
8335 end Has_Stream;
8336
8337 ----------------
8338 -- Has_Suffix --
8339 ----------------
8340
8341 function Has_Suffix (E : Entity_Id; Suffix : Character) return Boolean is
8342 begin
8343 Get_Name_String (Chars (E));
8344 return Name_Buffer (Name_Len) = Suffix;
8345 end Has_Suffix;
8346
8347 ----------------
8348 -- Add_Suffix --
8349 ----------------
8350
8351 function Add_Suffix (E : Entity_Id; Suffix : Character) return Name_Id is
8352 begin
8353 Get_Name_String (Chars (E));
8354 Add_Char_To_Name_Buffer (Suffix);
8355 return Name_Find;
8356 end Add_Suffix;
8357
8358 -------------------
8359 -- Remove_Suffix --
8360 -------------------
8361
8362 function Remove_Suffix (E : Entity_Id; Suffix : Character) return Name_Id is
8363 begin
8364 pragma Assert (Has_Suffix (E, Suffix));
8365 Get_Name_String (Chars (E));
8366 Name_Len := Name_Len - 1;
8367 return Name_Find;
8368 end Remove_Suffix;
8369
8370 --------------------------
8371 -- Has_Tagged_Component --
8372 --------------------------
8373
8374 function Has_Tagged_Component (Typ : Entity_Id) return Boolean is
8375 Comp : Entity_Id;
8376
8377 begin
8378 if Is_Private_Type (Typ)
8379 and then Present (Underlying_Type (Typ))
8380 then
8381 return Has_Tagged_Component (Underlying_Type (Typ));
8382
8383 elsif Is_Array_Type (Typ) then
8384 return Has_Tagged_Component (Component_Type (Typ));
8385
8386 elsif Is_Tagged_Type (Typ) then
8387 return True;
8388
8389 elsif Is_Record_Type (Typ) then
8390 Comp := First_Component (Typ);
8391 while Present (Comp) loop
8392 if Has_Tagged_Component (Etype (Comp)) then
8393 return True;
8394 end if;
8395
8396 Next_Component (Comp);
8397 end loop;
8398
8399 return False;
8400
8401 else
8402 return False;
8403 end if;
8404 end Has_Tagged_Component;
8405
8406 ----------------------------
8407 -- Has_Volatile_Component --
8408 ----------------------------
8409
8410 function Has_Volatile_Component (Typ : Entity_Id) return Boolean is
8411 Comp : Entity_Id;
8412
8413 begin
8414 if Has_Volatile_Components (Typ) then
8415 return True;
8416
8417 elsif Is_Array_Type (Typ) then
8418 return Is_Volatile (Component_Type (Typ));
8419
8420 elsif Is_Record_Type (Typ) then
8421 Comp := First_Component (Typ);
8422 while Present (Comp) loop
8423 if Is_Volatile_Object (Comp) then
8424 return True;
8425 end if;
8426
8427 Comp := Next_Component (Comp);
8428 end loop;
8429 end if;
8430
8431 return False;
8432 end Has_Volatile_Component;
8433
8434 -------------------------
8435 -- Implementation_Kind --
8436 -------------------------
8437
8438 function Implementation_Kind (Subp : Entity_Id) return Name_Id is
8439 Impl_Prag : constant Node_Id := Get_Rep_Pragma (Subp, Name_Implemented);
8440 Arg : Node_Id;
8441 begin
8442 pragma Assert (Present (Impl_Prag));
8443 Arg := Last (Pragma_Argument_Associations (Impl_Prag));
8444 return Chars (Get_Pragma_Arg (Arg));
8445 end Implementation_Kind;
8446
8447 --------------------------
8448 -- Implements_Interface --
8449 --------------------------
8450
8451 function Implements_Interface
8452 (Typ_Ent : Entity_Id;
8453 Iface_Ent : Entity_Id;
8454 Exclude_Parents : Boolean := False) return Boolean
8455 is
8456 Ifaces_List : Elist_Id;
8457 Elmt : Elmt_Id;
8458 Iface : Entity_Id := Base_Type (Iface_Ent);
8459 Typ : Entity_Id := Base_Type (Typ_Ent);
8460
8461 begin
8462 if Is_Class_Wide_Type (Typ) then
8463 Typ := Root_Type (Typ);
8464 end if;
8465
8466 if not Has_Interfaces (Typ) then
8467 return False;
8468 end if;
8469
8470 if Is_Class_Wide_Type (Iface) then
8471 Iface := Root_Type (Iface);
8472 end if;
8473
8474 Collect_Interfaces (Typ, Ifaces_List);
8475
8476 Elmt := First_Elmt (Ifaces_List);
8477 while Present (Elmt) loop
8478 if Is_Ancestor (Node (Elmt), Typ, Use_Full_View => True)
8479 and then Exclude_Parents
8480 then
8481 null;
8482
8483 elsif Node (Elmt) = Iface then
8484 return True;
8485 end if;
8486
8487 Next_Elmt (Elmt);
8488 end loop;
8489
8490 return False;
8491 end Implements_Interface;
8492
8493 ------------------------------------
8494 -- In_Assertion_Expression_Pragma --
8495 ------------------------------------
8496
8497 function In_Assertion_Expression_Pragma (N : Node_Id) return Boolean is
8498 Par : Node_Id;
8499 Prag : Node_Id := Empty;
8500
8501 begin
8502 -- Climb the parent chain looking for an enclosing pragma
8503
8504 Par := N;
8505 while Present (Par) loop
8506 if Nkind (Par) = N_Pragma then
8507 Prag := Par;
8508 exit;
8509
8510 -- Precondition-like pragmas are expanded into if statements, check
8511 -- the original node instead.
8512
8513 elsif Nkind (Original_Node (Par)) = N_Pragma then
8514 Prag := Original_Node (Par);
8515 exit;
8516
8517 -- Prevent the search from going too far
8518
8519 elsif Is_Body_Or_Package_Declaration (Par) then
8520 return False;
8521 end if;
8522
8523 Par := Parent (Par);
8524 end loop;
8525
8526 return
8527 Present (Prag)
8528 and then Assertion_Expression_Pragma (Get_Pragma_Id (Prag));
8529 end In_Assertion_Expression_Pragma;
8530
8531 -----------------
8532 -- In_Instance --
8533 -----------------
8534
8535 function In_Instance return Boolean is
8536 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
8537 S : Entity_Id;
8538
8539 begin
8540 S := Current_Scope;
8541 while Present (S)
8542 and then S /= Standard_Standard
8543 loop
8544 if (Ekind (S) = E_Function
8545 or else Ekind (S) = E_Package
8546 or else Ekind (S) = E_Procedure)
8547 and then Is_Generic_Instance (S)
8548 then
8549 -- A child instance is always compiled in the context of a parent
8550 -- instance. Nevertheless, the actuals are not analyzed in an
8551 -- instance context. We detect this case by examining the current
8552 -- compilation unit, which must be a child instance, and checking
8553 -- that it is not currently on the scope stack.
8554
8555 if Is_Child_Unit (Curr_Unit)
8556 and then
8557 Nkind (Unit (Cunit (Current_Sem_Unit)))
8558 = N_Package_Instantiation
8559 and then not In_Open_Scopes (Curr_Unit)
8560 then
8561 return False;
8562 else
8563 return True;
8564 end if;
8565 end if;
8566
8567 S := Scope (S);
8568 end loop;
8569
8570 return False;
8571 end In_Instance;
8572
8573 ----------------------
8574 -- In_Instance_Body --
8575 ----------------------
8576
8577 function In_Instance_Body return Boolean is
8578 S : Entity_Id;
8579
8580 begin
8581 S := Current_Scope;
8582 while Present (S)
8583 and then S /= Standard_Standard
8584 loop
8585 if (Ekind (S) = E_Function
8586 or else Ekind (S) = E_Procedure)
8587 and then Is_Generic_Instance (S)
8588 then
8589 return True;
8590
8591 elsif Ekind (S) = E_Package
8592 and then In_Package_Body (S)
8593 and then Is_Generic_Instance (S)
8594 then
8595 return True;
8596 end if;
8597
8598 S := Scope (S);
8599 end loop;
8600
8601 return False;
8602 end In_Instance_Body;
8603
8604 -----------------------------
8605 -- In_Instance_Not_Visible --
8606 -----------------------------
8607
8608 function In_Instance_Not_Visible return Boolean is
8609 S : Entity_Id;
8610
8611 begin
8612 S := Current_Scope;
8613 while Present (S)
8614 and then S /= Standard_Standard
8615 loop
8616 if (Ekind (S) = E_Function
8617 or else Ekind (S) = E_Procedure)
8618 and then Is_Generic_Instance (S)
8619 then
8620 return True;
8621
8622 elsif Ekind (S) = E_Package
8623 and then (In_Package_Body (S) or else In_Private_Part (S))
8624 and then Is_Generic_Instance (S)
8625 then
8626 return True;
8627 end if;
8628
8629 S := Scope (S);
8630 end loop;
8631
8632 return False;
8633 end In_Instance_Not_Visible;
8634
8635 ------------------------------
8636 -- In_Instance_Visible_Part --
8637 ------------------------------
8638
8639 function In_Instance_Visible_Part return Boolean is
8640 S : Entity_Id;
8641
8642 begin
8643 S := Current_Scope;
8644 while Present (S)
8645 and then S /= Standard_Standard
8646 loop
8647 if Ekind (S) = E_Package
8648 and then Is_Generic_Instance (S)
8649 and then not In_Package_Body (S)
8650 and then not In_Private_Part (S)
8651 then
8652 return True;
8653 end if;
8654
8655 S := Scope (S);
8656 end loop;
8657
8658 return False;
8659 end In_Instance_Visible_Part;
8660
8661 ---------------------
8662 -- In_Package_Body --
8663 ---------------------
8664
8665 function In_Package_Body return Boolean is
8666 S : Entity_Id;
8667
8668 begin
8669 S := Current_Scope;
8670 while Present (S)
8671 and then S /= Standard_Standard
8672 loop
8673 if Ekind (S) = E_Package
8674 and then In_Package_Body (S)
8675 then
8676 return True;
8677 else
8678 S := Scope (S);
8679 end if;
8680 end loop;
8681
8682 return False;
8683 end In_Package_Body;
8684
8685 --------------------------------
8686 -- In_Parameter_Specification --
8687 --------------------------------
8688
8689 function In_Parameter_Specification (N : Node_Id) return Boolean is
8690 PN : Node_Id;
8691
8692 begin
8693 PN := Parent (N);
8694 while Present (PN) loop
8695 if Nkind (PN) = N_Parameter_Specification then
8696 return True;
8697 end if;
8698
8699 PN := Parent (PN);
8700 end loop;
8701
8702 return False;
8703 end In_Parameter_Specification;
8704
8705 --------------------------
8706 -- In_Pragma_Expression --
8707 --------------------------
8708
8709 function In_Pragma_Expression (N : Node_Id; Nam : Name_Id) return Boolean is
8710 P : Node_Id;
8711 begin
8712 P := Parent (N);
8713 loop
8714 if No (P) then
8715 return False;
8716 elsif Nkind (P) = N_Pragma and then Pragma_Name (P) = Nam then
8717 return True;
8718 else
8719 P := Parent (P);
8720 end if;
8721 end loop;
8722 end In_Pragma_Expression;
8723
8724 -------------------------------------
8725 -- In_Reverse_Storage_Order_Object --
8726 -------------------------------------
8727
8728 function In_Reverse_Storage_Order_Object (N : Node_Id) return Boolean is
8729 Pref : Node_Id;
8730 Btyp : Entity_Id := Empty;
8731
8732 begin
8733 -- Climb up indexed components
8734
8735 Pref := N;
8736 loop
8737 case Nkind (Pref) is
8738 when N_Selected_Component =>
8739 Pref := Prefix (Pref);
8740 exit;
8741
8742 when N_Indexed_Component =>
8743 Pref := Prefix (Pref);
8744
8745 when others =>
8746 Pref := Empty;
8747 exit;
8748 end case;
8749 end loop;
8750
8751 if Present (Pref) then
8752 Btyp := Base_Type (Etype (Pref));
8753 end if;
8754
8755 return
8756 Present (Btyp)
8757 and then (Is_Record_Type (Btyp) or else Is_Array_Type (Btyp))
8758 and then Reverse_Storage_Order (Btyp);
8759 end In_Reverse_Storage_Order_Object;
8760
8761 --------------------------------------
8762 -- In_Subprogram_Or_Concurrent_Unit --
8763 --------------------------------------
8764
8765 function In_Subprogram_Or_Concurrent_Unit return Boolean is
8766 E : Entity_Id;
8767 K : Entity_Kind;
8768
8769 begin
8770 -- Use scope chain to check successively outer scopes
8771
8772 E := Current_Scope;
8773 loop
8774 K := Ekind (E);
8775
8776 if K in Subprogram_Kind
8777 or else K in Concurrent_Kind
8778 or else K in Generic_Subprogram_Kind
8779 then
8780 return True;
8781
8782 elsif E = Standard_Standard then
8783 return False;
8784 end if;
8785
8786 E := Scope (E);
8787 end loop;
8788 end In_Subprogram_Or_Concurrent_Unit;
8789
8790 ---------------------
8791 -- In_Visible_Part --
8792 ---------------------
8793
8794 function In_Visible_Part (Scope_Id : Entity_Id) return Boolean is
8795 begin
8796 return
8797 Is_Package_Or_Generic_Package (Scope_Id)
8798 and then In_Open_Scopes (Scope_Id)
8799 and then not In_Package_Body (Scope_Id)
8800 and then not In_Private_Part (Scope_Id);
8801 end In_Visible_Part;
8802
8803 --------------------------------
8804 -- Incomplete_Or_Private_View --
8805 --------------------------------
8806
8807 function Incomplete_Or_Private_View (Typ : Entity_Id) return Entity_Id is
8808 function Inspect_Decls
8809 (Decls : List_Id;
8810 Taft : Boolean := False) return Entity_Id;
8811 -- Check whether a declarative region contains the incomplete or private
8812 -- view of Typ.
8813
8814 -------------------
8815 -- Inspect_Decls --
8816 -------------------
8817
8818 function Inspect_Decls
8819 (Decls : List_Id;
8820 Taft : Boolean := False) return Entity_Id
8821 is
8822 Decl : Node_Id;
8823 Match : Node_Id;
8824
8825 begin
8826 Decl := First (Decls);
8827 while Present (Decl) loop
8828 Match := Empty;
8829
8830 if Taft then
8831 if Nkind (Decl) = N_Incomplete_Type_Declaration then
8832 Match := Defining_Identifier (Decl);
8833 end if;
8834
8835 else
8836 if Nkind_In (Decl, N_Private_Extension_Declaration,
8837 N_Private_Type_Declaration)
8838 then
8839 Match := Defining_Identifier (Decl);
8840 end if;
8841 end if;
8842
8843 if Present (Match)
8844 and then Present (Full_View (Match))
8845 and then Full_View (Match) = Typ
8846 then
8847 return Match;
8848 end if;
8849
8850 Next (Decl);
8851 end loop;
8852
8853 return Empty;
8854 end Inspect_Decls;
8855
8856 -- Local variables
8857
8858 Prev : Entity_Id;
8859
8860 -- Start of processing for Incomplete_Or_Partial_View
8861
8862 begin
8863 -- Incomplete type case
8864
8865 Prev := Current_Entity_In_Scope (Typ);
8866
8867 if Present (Prev)
8868 and then Is_Incomplete_Type (Prev)
8869 and then Present (Full_View (Prev))
8870 and then Full_View (Prev) = Typ
8871 then
8872 return Prev;
8873 end if;
8874
8875 -- Private or Taft amendment type case
8876
8877 declare
8878 Pkg : constant Entity_Id := Scope (Typ);
8879 Pkg_Decl : Node_Id := Pkg;
8880
8881 begin
8882 if Ekind (Pkg) = E_Package then
8883 while Nkind (Pkg_Decl) /= N_Package_Specification loop
8884 Pkg_Decl := Parent (Pkg_Decl);
8885 end loop;
8886
8887 -- It is knows that Typ has a private view, look for it in the
8888 -- visible declarations of the enclosing scope. A special case
8889 -- of this is when the two views have been exchanged - the full
8890 -- appears earlier than the private.
8891
8892 if Has_Private_Declaration (Typ) then
8893 Prev := Inspect_Decls (Visible_Declarations (Pkg_Decl));
8894
8895 -- Exchanged view case, look in the private declarations
8896
8897 if No (Prev) then
8898 Prev := Inspect_Decls (Private_Declarations (Pkg_Decl));
8899 end if;
8900
8901 return Prev;
8902
8903 -- Otherwise if this is the package body, then Typ is a potential
8904 -- Taft amendment type. The incomplete view should be located in
8905 -- the private declarations of the enclosing scope.
8906
8907 elsif In_Package_Body (Pkg) then
8908 return Inspect_Decls (Private_Declarations (Pkg_Decl), True);
8909 end if;
8910 end if;
8911 end;
8912
8913 -- The type has no incomplete or private view
8914
8915 return Empty;
8916 end Incomplete_Or_Private_View;
8917
8918 ---------------------------------
8919 -- Insert_Explicit_Dereference --
8920 ---------------------------------
8921
8922 procedure Insert_Explicit_Dereference (N : Node_Id) is
8923 New_Prefix : constant Node_Id := Relocate_Node (N);
8924 Ent : Entity_Id := Empty;
8925 Pref : Node_Id;
8926 I : Interp_Index;
8927 It : Interp;
8928 T : Entity_Id;
8929
8930 begin
8931 Save_Interps (N, New_Prefix);
8932
8933 Rewrite (N,
8934 Make_Explicit_Dereference (Sloc (Parent (N)),
8935 Prefix => New_Prefix));
8936
8937 Set_Etype (N, Designated_Type (Etype (New_Prefix)));
8938
8939 if Is_Overloaded (New_Prefix) then
8940
8941 -- The dereference is also overloaded, and its interpretations are
8942 -- the designated types of the interpretations of the original node.
8943
8944 Set_Etype (N, Any_Type);
8945
8946 Get_First_Interp (New_Prefix, I, It);
8947 while Present (It.Nam) loop
8948 T := It.Typ;
8949
8950 if Is_Access_Type (T) then
8951 Add_One_Interp (N, Designated_Type (T), Designated_Type (T));
8952 end if;
8953
8954 Get_Next_Interp (I, It);
8955 end loop;
8956
8957 End_Interp_List;
8958
8959 else
8960 -- Prefix is unambiguous: mark the original prefix (which might
8961 -- Come_From_Source) as a reference, since the new (relocated) one
8962 -- won't be taken into account.
8963
8964 if Is_Entity_Name (New_Prefix) then
8965 Ent := Entity (New_Prefix);
8966 Pref := New_Prefix;
8967
8968 -- For a retrieval of a subcomponent of some composite object,
8969 -- retrieve the ultimate entity if there is one.
8970
8971 elsif Nkind (New_Prefix) = N_Selected_Component
8972 or else Nkind (New_Prefix) = N_Indexed_Component
8973 then
8974 Pref := Prefix (New_Prefix);
8975 while Present (Pref)
8976 and then
8977 (Nkind (Pref) = N_Selected_Component
8978 or else Nkind (Pref) = N_Indexed_Component)
8979 loop
8980 Pref := Prefix (Pref);
8981 end loop;
8982
8983 if Present (Pref) and then Is_Entity_Name (Pref) then
8984 Ent := Entity (Pref);
8985 end if;
8986 end if;
8987
8988 -- Place the reference on the entity node
8989
8990 if Present (Ent) then
8991 Generate_Reference (Ent, Pref);
8992 end if;
8993 end if;
8994 end Insert_Explicit_Dereference;
8995
8996 ------------------------------------------
8997 -- Inspect_Deferred_Constant_Completion --
8998 ------------------------------------------
8999
9000 procedure Inspect_Deferred_Constant_Completion (Decls : List_Id) is
9001 Decl : Node_Id;
9002
9003 begin
9004 Decl := First (Decls);
9005 while Present (Decl) loop
9006
9007 -- Deferred constant signature
9008
9009 if Nkind (Decl) = N_Object_Declaration
9010 and then Constant_Present (Decl)
9011 and then No (Expression (Decl))
9012
9013 -- No need to check internally generated constants
9014
9015 and then Comes_From_Source (Decl)
9016
9017 -- The constant is not completed. A full object declaration or a
9018 -- pragma Import complete a deferred constant.
9019
9020 and then not Has_Completion (Defining_Identifier (Decl))
9021 then
9022 Error_Msg_N
9023 ("constant declaration requires initialization expression",
9024 Defining_Identifier (Decl));
9025 end if;
9026
9027 Decl := Next (Decl);
9028 end loop;
9029 end Inspect_Deferred_Constant_Completion;
9030
9031 -----------------------------
9032 -- Is_Actual_Out_Parameter --
9033 -----------------------------
9034
9035 function Is_Actual_Out_Parameter (N : Node_Id) return Boolean is
9036 Formal : Entity_Id;
9037 Call : Node_Id;
9038 begin
9039 Find_Actual (N, Formal, Call);
9040 return Present (Formal) and then Ekind (Formal) = E_Out_Parameter;
9041 end Is_Actual_Out_Parameter;
9042
9043 -------------------------
9044 -- Is_Actual_Parameter --
9045 -------------------------
9046
9047 function Is_Actual_Parameter (N : Node_Id) return Boolean is
9048 PK : constant Node_Kind := Nkind (Parent (N));
9049
9050 begin
9051 case PK is
9052 when N_Parameter_Association =>
9053 return N = Explicit_Actual_Parameter (Parent (N));
9054
9055 when N_Subprogram_Call =>
9056 return Is_List_Member (N)
9057 and then
9058 List_Containing (N) = Parameter_Associations (Parent (N));
9059
9060 when others =>
9061 return False;
9062 end case;
9063 end Is_Actual_Parameter;
9064
9065 --------------------------------
9066 -- Is_Actual_Tagged_Parameter --
9067 --------------------------------
9068
9069 function Is_Actual_Tagged_Parameter (N : Node_Id) return Boolean is
9070 Formal : Entity_Id;
9071 Call : Node_Id;
9072 begin
9073 Find_Actual (N, Formal, Call);
9074 return Present (Formal) and then Is_Tagged_Type (Etype (Formal));
9075 end Is_Actual_Tagged_Parameter;
9076
9077 ---------------------
9078 -- Is_Aliased_View --
9079 ---------------------
9080
9081 function Is_Aliased_View (Obj : Node_Id) return Boolean is
9082 E : Entity_Id;
9083
9084 begin
9085 if Is_Entity_Name (Obj) then
9086 E := Entity (Obj);
9087
9088 return
9089 (Is_Object (E)
9090 and then
9091 (Is_Aliased (E)
9092 or else (Present (Renamed_Object (E))
9093 and then Is_Aliased_View (Renamed_Object (E)))))
9094
9095 or else ((Is_Formal (E)
9096 or else Ekind (E) = E_Generic_In_Out_Parameter
9097 or else Ekind (E) = E_Generic_In_Parameter)
9098 and then Is_Tagged_Type (Etype (E)))
9099
9100 or else (Is_Concurrent_Type (E) and then In_Open_Scopes (E))
9101
9102 -- Current instance of type, either directly or as rewritten
9103 -- reference to the current object.
9104
9105 or else (Is_Entity_Name (Original_Node (Obj))
9106 and then Present (Entity (Original_Node (Obj)))
9107 and then Is_Type (Entity (Original_Node (Obj))))
9108
9109 or else (Is_Type (E) and then E = Current_Scope)
9110
9111 or else (Is_Incomplete_Or_Private_Type (E)
9112 and then Full_View (E) = Current_Scope)
9113
9114 -- Ada 2012 AI05-0053: the return object of an extended return
9115 -- statement is aliased if its type is immutably limited.
9116
9117 or else (Is_Return_Object (E)
9118 and then Is_Limited_View (Etype (E)));
9119
9120 elsif Nkind (Obj) = N_Selected_Component then
9121 return Is_Aliased (Entity (Selector_Name (Obj)));
9122
9123 elsif Nkind (Obj) = N_Indexed_Component then
9124 return Has_Aliased_Components (Etype (Prefix (Obj)))
9125 or else
9126 (Is_Access_Type (Etype (Prefix (Obj)))
9127 and then Has_Aliased_Components
9128 (Designated_Type (Etype (Prefix (Obj)))));
9129
9130 elsif Nkind_In (Obj, N_Unchecked_Type_Conversion, N_Type_Conversion) then
9131 return Is_Tagged_Type (Etype (Obj))
9132 and then Is_Aliased_View (Expression (Obj));
9133
9134 elsif Nkind (Obj) = N_Explicit_Dereference then
9135 return Nkind (Original_Node (Obj)) /= N_Function_Call;
9136
9137 else
9138 return False;
9139 end if;
9140 end Is_Aliased_View;
9141
9142 -------------------------
9143 -- Is_Ancestor_Package --
9144 -------------------------
9145
9146 function Is_Ancestor_Package
9147 (E1 : Entity_Id;
9148 E2 : Entity_Id) return Boolean
9149 is
9150 Par : Entity_Id;
9151
9152 begin
9153 Par := E2;
9154 while Present (Par)
9155 and then Par /= Standard_Standard
9156 loop
9157 if Par = E1 then
9158 return True;
9159 end if;
9160
9161 Par := Scope (Par);
9162 end loop;
9163
9164 return False;
9165 end Is_Ancestor_Package;
9166
9167 ----------------------
9168 -- Is_Atomic_Object --
9169 ----------------------
9170
9171 function Is_Atomic_Object (N : Node_Id) return Boolean is
9172
9173 function Object_Has_Atomic_Components (N : Node_Id) return Boolean;
9174 -- Determines if given object has atomic components
9175
9176 function Is_Atomic_Prefix (N : Node_Id) return Boolean;
9177 -- If prefix is an implicit dereference, examine designated type
9178
9179 ----------------------
9180 -- Is_Atomic_Prefix --
9181 ----------------------
9182
9183 function Is_Atomic_Prefix (N : Node_Id) return Boolean is
9184 begin
9185 if Is_Access_Type (Etype (N)) then
9186 return
9187 Has_Atomic_Components (Designated_Type (Etype (N)));
9188 else
9189 return Object_Has_Atomic_Components (N);
9190 end if;
9191 end Is_Atomic_Prefix;
9192
9193 ----------------------------------
9194 -- Object_Has_Atomic_Components --
9195 ----------------------------------
9196
9197 function Object_Has_Atomic_Components (N : Node_Id) return Boolean is
9198 begin
9199 if Has_Atomic_Components (Etype (N))
9200 or else Is_Atomic (Etype (N))
9201 then
9202 return True;
9203
9204 elsif Is_Entity_Name (N)
9205 and then (Has_Atomic_Components (Entity (N))
9206 or else Is_Atomic (Entity (N)))
9207 then
9208 return True;
9209
9210 elsif Nkind (N) = N_Selected_Component
9211 and then Is_Atomic (Entity (Selector_Name (N)))
9212 then
9213 return True;
9214
9215 elsif Nkind (N) = N_Indexed_Component
9216 or else Nkind (N) = N_Selected_Component
9217 then
9218 return Is_Atomic_Prefix (Prefix (N));
9219
9220 else
9221 return False;
9222 end if;
9223 end Object_Has_Atomic_Components;
9224
9225 -- Start of processing for Is_Atomic_Object
9226
9227 begin
9228 -- Predicate is not relevant to subprograms
9229
9230 if Is_Entity_Name (N) and then Is_Overloadable (Entity (N)) then
9231 return False;
9232
9233 elsif Is_Atomic (Etype (N))
9234 or else (Is_Entity_Name (N) and then Is_Atomic (Entity (N)))
9235 then
9236 return True;
9237
9238 elsif Nkind (N) = N_Selected_Component
9239 and then Is_Atomic (Entity (Selector_Name (N)))
9240 then
9241 return True;
9242
9243 elsif Nkind (N) = N_Indexed_Component
9244 or else Nkind (N) = N_Selected_Component
9245 then
9246 return Is_Atomic_Prefix (Prefix (N));
9247
9248 else
9249 return False;
9250 end if;
9251 end Is_Atomic_Object;
9252
9253 -------------------------
9254 -- Is_Attribute_Result --
9255 -------------------------
9256
9257 function Is_Attribute_Result (N : Node_Id) return Boolean is
9258 begin
9259 return
9260 Nkind (N) = N_Attribute_Reference
9261 and then Attribute_Name (N) = Name_Result;
9262 end Is_Attribute_Result;
9263
9264 ------------------------------------
9265 -- Is_Body_Or_Package_Declaration --
9266 ------------------------------------
9267
9268 function Is_Body_Or_Package_Declaration (N : Node_Id) return Boolean is
9269 begin
9270 return Nkind_In (N, N_Entry_Body,
9271 N_Package_Body,
9272 N_Package_Declaration,
9273 N_Protected_Body,
9274 N_Subprogram_Body,
9275 N_Task_Body);
9276 end Is_Body_Or_Package_Declaration;
9277
9278 -----------------------
9279 -- Is_Bounded_String --
9280 -----------------------
9281
9282 function Is_Bounded_String (T : Entity_Id) return Boolean is
9283 Under : constant Entity_Id := Underlying_Type (Root_Type (T));
9284
9285 begin
9286 -- Check whether T is ultimately derived from Ada.Strings.Superbounded.
9287 -- Super_String, or one of the [Wide_]Wide_ versions. This will
9288 -- be True for all the Bounded_String types in instances of the
9289 -- Generic_Bounded_Length generics, and for types derived from those.
9290
9291 return Present (Under)
9292 and then (Is_RTE (Root_Type (Under), RO_SU_Super_String) or else
9293 Is_RTE (Root_Type (Under), RO_WI_Super_String) or else
9294 Is_RTE (Root_Type (Under), RO_WW_Super_String));
9295 end Is_Bounded_String;
9296
9297 -------------------------
9298 -- Is_Child_Or_Sibling --
9299 -------------------------
9300
9301 function Is_Child_Or_Sibling
9302 (Pack_1 : Entity_Id;
9303 Pack_2 : Entity_Id) return Boolean
9304 is
9305 function Distance_From_Standard (Pack : Entity_Id) return Nat;
9306 -- Given an arbitrary package, return the number of "climbs" necessary
9307 -- to reach scope Standard_Standard.
9308
9309 procedure Equalize_Depths
9310 (Pack : in out Entity_Id;
9311 Depth : in out Nat;
9312 Depth_To_Reach : Nat);
9313 -- Given an arbitrary package, its depth and a target depth to reach,
9314 -- climb the scope chain until the said depth is reached. The pointer
9315 -- to the package and its depth a modified during the climb.
9316
9317 ----------------------------
9318 -- Distance_From_Standard --
9319 ----------------------------
9320
9321 function Distance_From_Standard (Pack : Entity_Id) return Nat is
9322 Dist : Nat;
9323 Scop : Entity_Id;
9324
9325 begin
9326 Dist := 0;
9327 Scop := Pack;
9328 while Present (Scop) and then Scop /= Standard_Standard loop
9329 Dist := Dist + 1;
9330 Scop := Scope (Scop);
9331 end loop;
9332
9333 return Dist;
9334 end Distance_From_Standard;
9335
9336 ---------------------
9337 -- Equalize_Depths --
9338 ---------------------
9339
9340 procedure Equalize_Depths
9341 (Pack : in out Entity_Id;
9342 Depth : in out Nat;
9343 Depth_To_Reach : Nat)
9344 is
9345 begin
9346 -- The package must be at a greater or equal depth
9347
9348 if Depth < Depth_To_Reach then
9349 raise Program_Error;
9350 end if;
9351
9352 -- Climb the scope chain until the desired depth is reached
9353
9354 while Present (Pack) and then Depth /= Depth_To_Reach loop
9355 Pack := Scope (Pack);
9356 Depth := Depth - 1;
9357 end loop;
9358 end Equalize_Depths;
9359
9360 -- Local variables
9361
9362 P_1 : Entity_Id := Pack_1;
9363 P_1_Child : Boolean := False;
9364 P_1_Depth : Nat := Distance_From_Standard (P_1);
9365 P_2 : Entity_Id := Pack_2;
9366 P_2_Child : Boolean := False;
9367 P_2_Depth : Nat := Distance_From_Standard (P_2);
9368
9369 -- Start of processing for Is_Child_Or_Sibling
9370
9371 begin
9372 pragma Assert
9373 (Ekind (Pack_1) = E_Package and then Ekind (Pack_2) = E_Package);
9374
9375 -- Both packages denote the same entity, therefore they cannot be
9376 -- children or siblings.
9377
9378 if P_1 = P_2 then
9379 return False;
9380
9381 -- One of the packages is at a deeper level than the other. Note that
9382 -- both may still come from differen hierarchies.
9383
9384 -- (root) P_2
9385 -- / \ :
9386 -- X P_2 or X
9387 -- : :
9388 -- P_1 P_1
9389
9390 elsif P_1_Depth > P_2_Depth then
9391 Equalize_Depths
9392 (Pack => P_1,
9393 Depth => P_1_Depth,
9394 Depth_To_Reach => P_2_Depth);
9395 P_1_Child := True;
9396
9397 -- (root) P_1
9398 -- / \ :
9399 -- P_1 X or X
9400 -- : :
9401 -- P_2 P_2
9402
9403 elsif P_2_Depth > P_1_Depth then
9404 Equalize_Depths
9405 (Pack => P_2,
9406 Depth => P_2_Depth,
9407 Depth_To_Reach => P_1_Depth);
9408 P_2_Child := True;
9409 end if;
9410
9411 -- At this stage the package pointers have been elevated to the same
9412 -- depth. If the related entities are the same, then one package is a
9413 -- potential child of the other:
9414
9415 -- P_1
9416 -- :
9417 -- X became P_1 P_2 or vica versa
9418 -- :
9419 -- P_2
9420
9421 if P_1 = P_2 then
9422 if P_1_Child then
9423 return Is_Child_Unit (Pack_1);
9424
9425 else pragma Assert (P_2_Child);
9426 return Is_Child_Unit (Pack_2);
9427 end if;
9428
9429 -- The packages may come from the same package chain or from entirely
9430 -- different hierarcies. To determine this, climb the scope stack until
9431 -- a common root is found.
9432
9433 -- (root) (root 1) (root 2)
9434 -- / \ | |
9435 -- P_1 P_2 P_1 P_2
9436
9437 else
9438 while Present (P_1) and then Present (P_2) loop
9439
9440 -- The two packages may be siblings
9441
9442 if P_1 = P_2 then
9443 return Is_Child_Unit (Pack_1) and then Is_Child_Unit (Pack_2);
9444 end if;
9445
9446 P_1 := Scope (P_1);
9447 P_2 := Scope (P_2);
9448 end loop;
9449 end if;
9450
9451 return False;
9452 end Is_Child_Or_Sibling;
9453
9454 -----------------------------
9455 -- Is_Concurrent_Interface --
9456 -----------------------------
9457
9458 function Is_Concurrent_Interface (T : Entity_Id) return Boolean is
9459 begin
9460 return
9461 Is_Interface (T)
9462 and then
9463 (Is_Protected_Interface (T)
9464 or else Is_Synchronized_Interface (T)
9465 or else Is_Task_Interface (T));
9466 end Is_Concurrent_Interface;
9467
9468 ---------------------------
9469 -- Is_Container_Element --
9470 ---------------------------
9471
9472 function Is_Container_Element (Exp : Node_Id) return Boolean is
9473 Loc : constant Source_Ptr := Sloc (Exp);
9474 Pref : constant Node_Id := Prefix (Exp);
9475
9476 Call : Node_Id;
9477 -- Call to an indexing aspect
9478
9479 Cont_Typ : Entity_Id;
9480 -- The type of the container being accessed
9481
9482 Elem_Typ : Entity_Id;
9483 -- Its element type
9484
9485 Indexing : Entity_Id;
9486 Is_Const : Boolean;
9487 -- Indicates that constant indexing is used, and the element is thus
9488 -- a constant.
9489
9490 Ref_Typ : Entity_Id;
9491 -- The reference type returned by the indexing operation
9492
9493 begin
9494 -- If C is a container, in a context that imposes the element type of
9495 -- that container, the indexing notation C (X) is rewritten as:
9496
9497 -- Indexing (C, X).Discr.all
9498
9499 -- where Indexing is one of the indexing aspects of the container.
9500 -- If the context does not require a reference, the construct can be
9501 -- rewritten as
9502
9503 -- Element (C, X)
9504
9505 -- First, verify that the construct has the proper form
9506
9507 if not Expander_Active then
9508 return False;
9509
9510 elsif Nkind (Pref) /= N_Selected_Component then
9511 return False;
9512
9513 elsif Nkind (Prefix (Pref)) /= N_Function_Call then
9514 return False;
9515
9516 else
9517 Call := Prefix (Pref);
9518 Ref_Typ := Etype (Call);
9519 end if;
9520
9521 if not Has_Implicit_Dereference (Ref_Typ)
9522 or else No (First (Parameter_Associations (Call)))
9523 or else not Is_Entity_Name (Name (Call))
9524 then
9525 return False;
9526 end if;
9527
9528 -- Retrieve type of container object, and its iterator aspects
9529
9530 Cont_Typ := Etype (First (Parameter_Associations (Call)));
9531 Indexing := Find_Value_Of_Aspect (Cont_Typ, Aspect_Constant_Indexing);
9532 Is_Const := False;
9533
9534 if No (Indexing) then
9535
9536 -- Container should have at least one indexing operation
9537
9538 return False;
9539
9540 elsif Entity (Name (Call)) /= Entity (Indexing) then
9541
9542 -- This may be a variable indexing operation
9543
9544 Indexing := Find_Value_Of_Aspect (Cont_Typ, Aspect_Variable_Indexing);
9545
9546 if No (Indexing)
9547 or else Entity (Name (Call)) /= Entity (Indexing)
9548 then
9549 return False;
9550 end if;
9551
9552 else
9553 Is_Const := True;
9554 end if;
9555
9556 Elem_Typ := Find_Value_Of_Aspect (Cont_Typ, Aspect_Iterator_Element);
9557
9558 if No (Elem_Typ) or else Entity (Elem_Typ) /= Etype (Exp) then
9559 return False;
9560 end if;
9561
9562 -- Check that the expression is not the target of an assignment, in
9563 -- which case the rewriting is not possible.
9564
9565 if not Is_Const then
9566 declare
9567 Par : Node_Id;
9568
9569 begin
9570 Par := Exp;
9571 while Present (Par)
9572 loop
9573 if Nkind (Parent (Par)) = N_Assignment_Statement
9574 and then Par = Name (Parent (Par))
9575 then
9576 return False;
9577
9578 -- A renaming produces a reference, and the transformation
9579 -- does not apply.
9580
9581 elsif Nkind (Parent (Par)) = N_Object_Renaming_Declaration then
9582 return False;
9583
9584 elsif Nkind_In
9585 (Nkind (Parent (Par)), N_Function_Call,
9586 N_Procedure_Call_Statement,
9587 N_Entry_Call_Statement)
9588 then
9589 -- Check that the element is not part of an actual for an
9590 -- in-out parameter.
9591
9592 declare
9593 F : Entity_Id;
9594 A : Node_Id;
9595
9596 begin
9597 F := First_Formal (Entity (Name (Parent (Par))));
9598 A := First (Parameter_Associations (Parent (Par)));
9599 while Present (F) loop
9600 if A = Par and then Ekind (F) /= E_In_Parameter then
9601 return False;
9602 end if;
9603
9604 Next_Formal (F);
9605 Next (A);
9606 end loop;
9607 end;
9608
9609 -- E_In_Parameter in a call: element is not modified.
9610
9611 exit;
9612 end if;
9613
9614 Par := Parent (Par);
9615 end loop;
9616 end;
9617 end if;
9618
9619 -- The expression has the proper form and the context requires the
9620 -- element type. Retrieve the Element function of the container and
9621 -- rewrite the construct as a call to it.
9622
9623 declare
9624 Op : Elmt_Id;
9625
9626 begin
9627 Op := First_Elmt (Primitive_Operations (Cont_Typ));
9628 while Present (Op) loop
9629 exit when Chars (Node (Op)) = Name_Element;
9630 Next_Elmt (Op);
9631 end loop;
9632
9633 if No (Op) then
9634 return False;
9635
9636 else
9637 Rewrite (Exp,
9638 Make_Function_Call (Loc,
9639 Name => New_Occurrence_Of (Node (Op), Loc),
9640 Parameter_Associations => Parameter_Associations (Call)));
9641 Analyze_And_Resolve (Exp, Entity (Elem_Typ));
9642 return True;
9643 end if;
9644 end;
9645 end Is_Container_Element;
9646
9647 -----------------------
9648 -- Is_Constant_Bound --
9649 -----------------------
9650
9651 function Is_Constant_Bound (Exp : Node_Id) return Boolean is
9652 begin
9653 if Compile_Time_Known_Value (Exp) then
9654 return True;
9655
9656 elsif Is_Entity_Name (Exp) and then Present (Entity (Exp)) then
9657 return Is_Constant_Object (Entity (Exp))
9658 or else Ekind (Entity (Exp)) = E_Enumeration_Literal;
9659
9660 elsif Nkind (Exp) in N_Binary_Op then
9661 return Is_Constant_Bound (Left_Opnd (Exp))
9662 and then Is_Constant_Bound (Right_Opnd (Exp))
9663 and then Scope (Entity (Exp)) = Standard_Standard;
9664
9665 else
9666 return False;
9667 end if;
9668 end Is_Constant_Bound;
9669
9670 --------------------------------------
9671 -- Is_Controlling_Limited_Procedure --
9672 --------------------------------------
9673
9674 function Is_Controlling_Limited_Procedure
9675 (Proc_Nam : Entity_Id) return Boolean
9676 is
9677 Param_Typ : Entity_Id := Empty;
9678
9679 begin
9680 if Ekind (Proc_Nam) = E_Procedure
9681 and then Present (Parameter_Specifications (Parent (Proc_Nam)))
9682 then
9683 Param_Typ := Etype (Parameter_Type (First (
9684 Parameter_Specifications (Parent (Proc_Nam)))));
9685
9686 -- In this case where an Itype was created, the procedure call has been
9687 -- rewritten.
9688
9689 elsif Present (Associated_Node_For_Itype (Proc_Nam))
9690 and then Present (Original_Node (Associated_Node_For_Itype (Proc_Nam)))
9691 and then
9692 Present (Parameter_Associations
9693 (Associated_Node_For_Itype (Proc_Nam)))
9694 then
9695 Param_Typ :=
9696 Etype (First (Parameter_Associations
9697 (Associated_Node_For_Itype (Proc_Nam))));
9698 end if;
9699
9700 if Present (Param_Typ) then
9701 return
9702 Is_Interface (Param_Typ)
9703 and then Is_Limited_Record (Param_Typ);
9704 end if;
9705
9706 return False;
9707 end Is_Controlling_Limited_Procedure;
9708
9709 -----------------------------
9710 -- Is_CPP_Constructor_Call --
9711 -----------------------------
9712
9713 function Is_CPP_Constructor_Call (N : Node_Id) return Boolean is
9714 begin
9715 return Nkind (N) = N_Function_Call
9716 and then Is_CPP_Class (Etype (Etype (N)))
9717 and then Is_Constructor (Entity (Name (N)))
9718 and then Is_Imported (Entity (Name (N)));
9719 end Is_CPP_Constructor_Call;
9720
9721 -----------------
9722 -- Is_Delegate --
9723 -----------------
9724
9725 function Is_Delegate (T : Entity_Id) return Boolean is
9726 Desig_Type : Entity_Id;
9727
9728 begin
9729 if VM_Target /= CLI_Target then
9730 return False;
9731 end if;
9732
9733 -- Access-to-subprograms are delegates in CIL
9734
9735 if Ekind (T) = E_Access_Subprogram_Type then
9736 return True;
9737 end if;
9738
9739 if not Is_Access_Type (T) then
9740
9741 -- A delegate is a managed pointer. If no designated type is defined
9742 -- it means that it's not a delegate.
9743
9744 return False;
9745 end if;
9746
9747 Desig_Type := Etype (Directly_Designated_Type (T));
9748
9749 if not Is_Tagged_Type (Desig_Type) then
9750 return False;
9751 end if;
9752
9753 -- Test if the type is inherited from [mscorlib]System.Delegate
9754
9755 while Etype (Desig_Type) /= Desig_Type loop
9756 if Chars (Scope (Desig_Type)) /= No_Name
9757 and then Is_Imported (Scope (Desig_Type))
9758 and then Get_Name_String (Chars (Scope (Desig_Type))) = "delegate"
9759 then
9760 return True;
9761 end if;
9762
9763 Desig_Type := Etype (Desig_Type);
9764 end loop;
9765
9766 return False;
9767 end Is_Delegate;
9768
9769 ----------------------------------------------
9770 -- Is_Dependent_Component_Of_Mutable_Object --
9771 ----------------------------------------------
9772
9773 function Is_Dependent_Component_Of_Mutable_Object
9774 (Object : Node_Id) return Boolean
9775 is
9776 function Is_Declared_Within_Variant (Comp : Entity_Id) return Boolean;
9777 -- Returns True if and only if Comp is declared within a variant part
9778
9779 --------------------------------
9780 -- Is_Declared_Within_Variant --
9781 --------------------------------
9782
9783 function Is_Declared_Within_Variant (Comp : Entity_Id) return Boolean is
9784 Comp_Decl : constant Node_Id := Parent (Comp);
9785 Comp_List : constant Node_Id := Parent (Comp_Decl);
9786 begin
9787 return Nkind (Parent (Comp_List)) = N_Variant;
9788 end Is_Declared_Within_Variant;
9789
9790 P : Node_Id;
9791 Prefix_Type : Entity_Id;
9792 P_Aliased : Boolean := False;
9793 Comp : Entity_Id;
9794
9795 Deref : Node_Id := Object;
9796 -- Dereference node, in something like X.all.Y(2)
9797
9798 -- Start of processing for Is_Dependent_Component_Of_Mutable_Object
9799
9800 begin
9801 -- Find the dereference node if any
9802
9803 while Nkind_In (Deref, N_Indexed_Component,
9804 N_Selected_Component,
9805 N_Slice)
9806 loop
9807 Deref := Prefix (Deref);
9808 end loop;
9809
9810 -- Ada 2005: If we have a component or slice of a dereference,
9811 -- something like X.all.Y (2), and the type of X is access-to-constant,
9812 -- Is_Variable will return False, because it is indeed a constant
9813 -- view. But it might be a view of a variable object, so we want the
9814 -- following condition to be True in that case.
9815
9816 if Is_Variable (Object)
9817 or else (Ada_Version >= Ada_2005
9818 and then Nkind (Deref) = N_Explicit_Dereference)
9819 then
9820 if Nkind (Object) = N_Selected_Component then
9821 P := Prefix (Object);
9822 Prefix_Type := Etype (P);
9823
9824 if Is_Entity_Name (P) then
9825 if Ekind (Entity (P)) = E_Generic_In_Out_Parameter then
9826 Prefix_Type := Base_Type (Prefix_Type);
9827 end if;
9828
9829 if Is_Aliased (Entity (P)) then
9830 P_Aliased := True;
9831 end if;
9832
9833 -- A discriminant check on a selected component may be expanded
9834 -- into a dereference when removing side-effects. Recover the
9835 -- original node and its type, which may be unconstrained.
9836
9837 elsif Nkind (P) = N_Explicit_Dereference
9838 and then not (Comes_From_Source (P))
9839 then
9840 P := Original_Node (P);
9841 Prefix_Type := Etype (P);
9842
9843 else
9844 -- Check for prefix being an aliased component???
9845
9846 null;
9847
9848 end if;
9849
9850 -- A heap object is constrained by its initial value
9851
9852 -- Ada 2005 (AI-363): Always assume the object could be mutable in
9853 -- the dereferenced case, since the access value might denote an
9854 -- unconstrained aliased object, whereas in Ada 95 the designated
9855 -- object is guaranteed to be constrained. A worst-case assumption
9856 -- has to apply in Ada 2005 because we can't tell at compile
9857 -- time whether the object is "constrained by its initial value"
9858 -- (despite the fact that 3.10.2(26/2) and 8.5.1(5/2) are semantic
9859 -- rules (these rules are acknowledged to need fixing).
9860
9861 if Ada_Version < Ada_2005 then
9862 if Is_Access_Type (Prefix_Type)
9863 or else Nkind (P) = N_Explicit_Dereference
9864 then
9865 return False;
9866 end if;
9867
9868 else pragma Assert (Ada_Version >= Ada_2005);
9869 if Is_Access_Type (Prefix_Type) then
9870
9871 -- If the access type is pool-specific, and there is no
9872 -- constrained partial view of the designated type, then the
9873 -- designated object is known to be constrained.
9874
9875 if Ekind (Prefix_Type) = E_Access_Type
9876 and then not Object_Type_Has_Constrained_Partial_View
9877 (Typ => Designated_Type (Prefix_Type),
9878 Scop => Current_Scope)
9879 then
9880 return False;
9881
9882 -- Otherwise (general access type, or there is a constrained
9883 -- partial view of the designated type), we need to check
9884 -- based on the designated type.
9885
9886 else
9887 Prefix_Type := Designated_Type (Prefix_Type);
9888 end if;
9889 end if;
9890 end if;
9891
9892 Comp :=
9893 Original_Record_Component (Entity (Selector_Name (Object)));
9894
9895 -- As per AI-0017, the renaming is illegal in a generic body, even
9896 -- if the subtype is indefinite.
9897
9898 -- Ada 2005 (AI-363): In Ada 2005 an aliased object can be mutable
9899
9900 if not Is_Constrained (Prefix_Type)
9901 and then (not Is_Indefinite_Subtype (Prefix_Type)
9902 or else
9903 (Is_Generic_Type (Prefix_Type)
9904 and then Ekind (Current_Scope) = E_Generic_Package
9905 and then In_Package_Body (Current_Scope)))
9906
9907 and then (Is_Declared_Within_Variant (Comp)
9908 or else Has_Discriminant_Dependent_Constraint (Comp))
9909 and then (not P_Aliased or else Ada_Version >= Ada_2005)
9910 then
9911 return True;
9912
9913 -- If the prefix is of an access type at this point, then we want
9914 -- to return False, rather than calling this function recursively
9915 -- on the access object (which itself might be a discriminant-
9916 -- dependent component of some other object, but that isn't
9917 -- relevant to checking the object passed to us). This avoids
9918 -- issuing wrong errors when compiling with -gnatc, where there
9919 -- can be implicit dereferences that have not been expanded.
9920
9921 elsif Is_Access_Type (Etype (Prefix (Object))) then
9922 return False;
9923
9924 else
9925 return
9926 Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
9927 end if;
9928
9929 elsif Nkind (Object) = N_Indexed_Component
9930 or else Nkind (Object) = N_Slice
9931 then
9932 return Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
9933
9934 -- A type conversion that Is_Variable is a view conversion:
9935 -- go back to the denoted object.
9936
9937 elsif Nkind (Object) = N_Type_Conversion then
9938 return
9939 Is_Dependent_Component_Of_Mutable_Object (Expression (Object));
9940 end if;
9941 end if;
9942
9943 return False;
9944 end Is_Dependent_Component_Of_Mutable_Object;
9945
9946 ---------------------
9947 -- Is_Dereferenced --
9948 ---------------------
9949
9950 function Is_Dereferenced (N : Node_Id) return Boolean is
9951 P : constant Node_Id := Parent (N);
9952 begin
9953 return
9954 (Nkind (P) = N_Selected_Component
9955 or else
9956 Nkind (P) = N_Explicit_Dereference
9957 or else
9958 Nkind (P) = N_Indexed_Component
9959 or else
9960 Nkind (P) = N_Slice)
9961 and then Prefix (P) = N;
9962 end Is_Dereferenced;
9963
9964 ----------------------
9965 -- Is_Descendent_Of --
9966 ----------------------
9967
9968 function Is_Descendent_Of (T1 : Entity_Id; T2 : Entity_Id) return Boolean is
9969 T : Entity_Id;
9970 Etyp : Entity_Id;
9971
9972 begin
9973 pragma Assert (Nkind (T1) in N_Entity);
9974 pragma Assert (Nkind (T2) in N_Entity);
9975
9976 T := Base_Type (T1);
9977
9978 -- Immediate return if the types match
9979
9980 if T = T2 then
9981 return True;
9982
9983 -- Comment needed here ???
9984
9985 elsif Ekind (T) = E_Class_Wide_Type then
9986 return Etype (T) = T2;
9987
9988 -- All other cases
9989
9990 else
9991 loop
9992 Etyp := Etype (T);
9993
9994 -- Done if we found the type we are looking for
9995
9996 if Etyp = T2 then
9997 return True;
9998
9999 -- Done if no more derivations to check
10000
10001 elsif T = T1
10002 or else T = Etyp
10003 then
10004 return False;
10005
10006 -- Following test catches error cases resulting from prev errors
10007
10008 elsif No (Etyp) then
10009 return False;
10010
10011 elsif Is_Private_Type (T) and then Etyp = Full_View (T) then
10012 return False;
10013
10014 elsif Is_Private_Type (Etyp) and then Full_View (Etyp) = T then
10015 return False;
10016 end if;
10017
10018 T := Base_Type (Etyp);
10019 end loop;
10020 end if;
10021 end Is_Descendent_Of;
10022
10023 ----------------------------
10024 -- Is_Expression_Function --
10025 ----------------------------
10026
10027 function Is_Expression_Function (Subp : Entity_Id) return Boolean is
10028 Decl : Node_Id;
10029
10030 begin
10031 if Ekind (Subp) /= E_Function then
10032 return False;
10033
10034 else
10035 Decl := Unit_Declaration_Node (Subp);
10036 return Nkind (Decl) = N_Subprogram_Declaration
10037 and then
10038 (Nkind (Original_Node (Decl)) = N_Expression_Function
10039 or else
10040 (Present (Corresponding_Body (Decl))
10041 and then
10042 Nkind (Original_Node
10043 (Unit_Declaration_Node
10044 (Corresponding_Body (Decl)))) =
10045 N_Expression_Function));
10046 end if;
10047 end Is_Expression_Function;
10048
10049 --------------
10050 -- Is_False --
10051 --------------
10052
10053 function Is_False (U : Uint) return Boolean is
10054 begin
10055 return (U = 0);
10056 end Is_False;
10057
10058 ---------------------------
10059 -- Is_Fixed_Model_Number --
10060 ---------------------------
10061
10062 function Is_Fixed_Model_Number (U : Ureal; T : Entity_Id) return Boolean is
10063 S : constant Ureal := Small_Value (T);
10064 M : Urealp.Save_Mark;
10065 R : Boolean;
10066 begin
10067 M := Urealp.Mark;
10068 R := (U = UR_Trunc (U / S) * S);
10069 Urealp.Release (M);
10070 return R;
10071 end Is_Fixed_Model_Number;
10072
10073 -------------------------------
10074 -- Is_Fully_Initialized_Type --
10075 -------------------------------
10076
10077 function Is_Fully_Initialized_Type (Typ : Entity_Id) return Boolean is
10078 begin
10079 -- In Ada2012, a scalar type with an aspect Default_Value
10080 -- is fully initialized.
10081
10082 if Is_Scalar_Type (Typ) then
10083 return Ada_Version >= Ada_2012 and then Has_Default_Aspect (Typ);
10084
10085 elsif Is_Access_Type (Typ) then
10086 return True;
10087
10088 elsif Is_Array_Type (Typ) then
10089 if Is_Fully_Initialized_Type (Component_Type (Typ))
10090 or else (Ada_Version >= Ada_2012 and then Has_Default_Aspect (Typ))
10091 then
10092 return True;
10093 end if;
10094
10095 -- An interesting case, if we have a constrained type one of whose
10096 -- bounds is known to be null, then there are no elements to be
10097 -- initialized, so all the elements are initialized.
10098
10099 if Is_Constrained (Typ) then
10100 declare
10101 Indx : Node_Id;
10102 Indx_Typ : Entity_Id;
10103 Lbd, Hbd : Node_Id;
10104
10105 begin
10106 Indx := First_Index (Typ);
10107 while Present (Indx) loop
10108 if Etype (Indx) = Any_Type then
10109 return False;
10110
10111 -- If index is a range, use directly
10112
10113 elsif Nkind (Indx) = N_Range then
10114 Lbd := Low_Bound (Indx);
10115 Hbd := High_Bound (Indx);
10116
10117 else
10118 Indx_Typ := Etype (Indx);
10119
10120 if Is_Private_Type (Indx_Typ) then
10121 Indx_Typ := Full_View (Indx_Typ);
10122 end if;
10123
10124 if No (Indx_Typ) or else Etype (Indx_Typ) = Any_Type then
10125 return False;
10126 else
10127 Lbd := Type_Low_Bound (Indx_Typ);
10128 Hbd := Type_High_Bound (Indx_Typ);
10129 end if;
10130 end if;
10131
10132 if Compile_Time_Known_Value (Lbd)
10133 and then Compile_Time_Known_Value (Hbd)
10134 then
10135 if Expr_Value (Hbd) < Expr_Value (Lbd) then
10136 return True;
10137 end if;
10138 end if;
10139
10140 Next_Index (Indx);
10141 end loop;
10142 end;
10143 end if;
10144
10145 -- If no null indexes, then type is not fully initialized
10146
10147 return False;
10148
10149 -- Record types
10150
10151 elsif Is_Record_Type (Typ) then
10152 if Has_Discriminants (Typ)
10153 and then
10154 Present (Discriminant_Default_Value (First_Discriminant (Typ)))
10155 and then Is_Fully_Initialized_Variant (Typ)
10156 then
10157 return True;
10158 end if;
10159
10160 -- We consider bounded string types to be fully initialized, because
10161 -- otherwise we get false alarms when the Data component is not
10162 -- default-initialized.
10163
10164 if Is_Bounded_String (Typ) then
10165 return True;
10166 end if;
10167
10168 -- Controlled records are considered to be fully initialized if
10169 -- there is a user defined Initialize routine. This may not be
10170 -- entirely correct, but as the spec notes, we are guessing here
10171 -- what is best from the point of view of issuing warnings.
10172
10173 if Is_Controlled (Typ) then
10174 declare
10175 Utyp : constant Entity_Id := Underlying_Type (Typ);
10176
10177 begin
10178 if Present (Utyp) then
10179 declare
10180 Init : constant Entity_Id :=
10181 (Find_Prim_Op
10182 (Underlying_Type (Typ), Name_Initialize));
10183
10184 begin
10185 if Present (Init)
10186 and then Comes_From_Source (Init)
10187 and then not
10188 Is_Predefined_File_Name
10189 (File_Name (Get_Source_File_Index (Sloc (Init))))
10190 then
10191 return True;
10192
10193 elsif Has_Null_Extension (Typ)
10194 and then
10195 Is_Fully_Initialized_Type
10196 (Etype (Base_Type (Typ)))
10197 then
10198 return True;
10199 end if;
10200 end;
10201 end if;
10202 end;
10203 end if;
10204
10205 -- Otherwise see if all record components are initialized
10206
10207 declare
10208 Ent : Entity_Id;
10209
10210 begin
10211 Ent := First_Entity (Typ);
10212 while Present (Ent) loop
10213 if Ekind (Ent) = E_Component
10214 and then (No (Parent (Ent))
10215 or else No (Expression (Parent (Ent))))
10216 and then not Is_Fully_Initialized_Type (Etype (Ent))
10217
10218 -- Special VM case for tag components, which need to be
10219 -- defined in this case, but are never initialized as VMs
10220 -- are using other dispatching mechanisms. Ignore this
10221 -- uninitialized case. Note that this applies both to the
10222 -- uTag entry and the main vtable pointer (CPP_Class case).
10223
10224 and then (Tagged_Type_Expansion or else not Is_Tag (Ent))
10225 then
10226 return False;
10227 end if;
10228
10229 Next_Entity (Ent);
10230 end loop;
10231 end;
10232
10233 -- No uninitialized components, so type is fully initialized.
10234 -- Note that this catches the case of no components as well.
10235
10236 return True;
10237
10238 elsif Is_Concurrent_Type (Typ) then
10239 return True;
10240
10241 elsif Is_Private_Type (Typ) then
10242 declare
10243 U : constant Entity_Id := Underlying_Type (Typ);
10244
10245 begin
10246 if No (U) then
10247 return False;
10248 else
10249 return Is_Fully_Initialized_Type (U);
10250 end if;
10251 end;
10252
10253 else
10254 return False;
10255 end if;
10256 end Is_Fully_Initialized_Type;
10257
10258 ----------------------------------
10259 -- Is_Fully_Initialized_Variant --
10260 ----------------------------------
10261
10262 function Is_Fully_Initialized_Variant (Typ : Entity_Id) return Boolean is
10263 Loc : constant Source_Ptr := Sloc (Typ);
10264 Constraints : constant List_Id := New_List;
10265 Components : constant Elist_Id := New_Elmt_List;
10266 Comp_Elmt : Elmt_Id;
10267 Comp_Id : Node_Id;
10268 Comp_List : Node_Id;
10269 Discr : Entity_Id;
10270 Discr_Val : Node_Id;
10271
10272 Report_Errors : Boolean;
10273 pragma Warnings (Off, Report_Errors);
10274
10275 begin
10276 if Serious_Errors_Detected > 0 then
10277 return False;
10278 end if;
10279
10280 if Is_Record_Type (Typ)
10281 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
10282 and then Nkind (Type_Definition (Parent (Typ))) = N_Record_Definition
10283 then
10284 Comp_List := Component_List (Type_Definition (Parent (Typ)));
10285
10286 Discr := First_Discriminant (Typ);
10287 while Present (Discr) loop
10288 if Nkind (Parent (Discr)) = N_Discriminant_Specification then
10289 Discr_Val := Expression (Parent (Discr));
10290
10291 if Present (Discr_Val)
10292 and then Is_OK_Static_Expression (Discr_Val)
10293 then
10294 Append_To (Constraints,
10295 Make_Component_Association (Loc,
10296 Choices => New_List (New_Occurrence_Of (Discr, Loc)),
10297 Expression => New_Copy (Discr_Val)));
10298 else
10299 return False;
10300 end if;
10301 else
10302 return False;
10303 end if;
10304
10305 Next_Discriminant (Discr);
10306 end loop;
10307
10308 Gather_Components
10309 (Typ => Typ,
10310 Comp_List => Comp_List,
10311 Governed_By => Constraints,
10312 Into => Components,
10313 Report_Errors => Report_Errors);
10314
10315 -- Check that each component present is fully initialized
10316
10317 Comp_Elmt := First_Elmt (Components);
10318 while Present (Comp_Elmt) loop
10319 Comp_Id := Node (Comp_Elmt);
10320
10321 if Ekind (Comp_Id) = E_Component
10322 and then (No (Parent (Comp_Id))
10323 or else No (Expression (Parent (Comp_Id))))
10324 and then not Is_Fully_Initialized_Type (Etype (Comp_Id))
10325 then
10326 return False;
10327 end if;
10328
10329 Next_Elmt (Comp_Elmt);
10330 end loop;
10331
10332 return True;
10333
10334 elsif Is_Private_Type (Typ) then
10335 declare
10336 U : constant Entity_Id := Underlying_Type (Typ);
10337
10338 begin
10339 if No (U) then
10340 return False;
10341 else
10342 return Is_Fully_Initialized_Variant (U);
10343 end if;
10344 end;
10345
10346 else
10347 return False;
10348 end if;
10349 end Is_Fully_Initialized_Variant;
10350
10351 ----------------------------
10352 -- Is_Inherited_Operation --
10353 ----------------------------
10354
10355 function Is_Inherited_Operation (E : Entity_Id) return Boolean is
10356 pragma Assert (Is_Overloadable (E));
10357 Kind : constant Node_Kind := Nkind (Parent (E));
10358 begin
10359 return Kind = N_Full_Type_Declaration
10360 or else Kind = N_Private_Extension_Declaration
10361 or else Kind = N_Subtype_Declaration
10362 or else (Ekind (E) = E_Enumeration_Literal
10363 and then Is_Derived_Type (Etype (E)));
10364 end Is_Inherited_Operation;
10365
10366 -------------------------------------
10367 -- Is_Inherited_Operation_For_Type --
10368 -------------------------------------
10369
10370 function Is_Inherited_Operation_For_Type
10371 (E : Entity_Id;
10372 Typ : Entity_Id) return Boolean
10373 is
10374 begin
10375 -- Check that the operation has been created by the type declaration
10376
10377 return Is_Inherited_Operation (E)
10378 and then Defining_Identifier (Parent (E)) = Typ;
10379 end Is_Inherited_Operation_For_Type;
10380
10381 -----------------
10382 -- Is_Iterator --
10383 -----------------
10384
10385 function Is_Iterator (Typ : Entity_Id) return Boolean is
10386 Ifaces_List : Elist_Id;
10387 Iface_Elmt : Elmt_Id;
10388 Iface : Entity_Id;
10389
10390 begin
10391 if Is_Class_Wide_Type (Typ)
10392 and then
10393 Nam_In (Chars (Etype (Typ)), Name_Forward_Iterator,
10394 Name_Reversible_Iterator)
10395 and then
10396 Is_Predefined_File_Name
10397 (Unit_File_Name (Get_Source_Unit (Etype (Typ))))
10398 then
10399 return True;
10400
10401 elsif not Is_Tagged_Type (Typ) or else not Is_Derived_Type (Typ) then
10402 return False;
10403
10404 elsif Present (Find_Value_Of_Aspect (Typ, Aspect_Iterable)) then
10405 return True;
10406
10407 else
10408 Collect_Interfaces (Typ, Ifaces_List);
10409
10410 Iface_Elmt := First_Elmt (Ifaces_List);
10411 while Present (Iface_Elmt) loop
10412 Iface := Node (Iface_Elmt);
10413 if Chars (Iface) = Name_Forward_Iterator
10414 and then
10415 Is_Predefined_File_Name
10416 (Unit_File_Name (Get_Source_Unit (Iface)))
10417 then
10418 return True;
10419 end if;
10420
10421 Next_Elmt (Iface_Elmt);
10422 end loop;
10423
10424 return False;
10425 end if;
10426 end Is_Iterator;
10427
10428 ------------------
10429 -- Is_Junk_Name --
10430 ------------------
10431
10432 function Is_Junk_Name (N : Name_Id) return Boolean is
10433 function Match (S : String) return Boolean;
10434 -- Return true if substring S is found in Name_Buffer (1 .. Name_Len)
10435
10436 -----------
10437 -- Match --
10438 -----------
10439
10440 function Match (S : String) return Boolean is
10441 Slen1 : constant Integer := S'Length - 1;
10442
10443 begin
10444 for J in 1 .. Name_Len - S'Length + 1 loop
10445 if Name_Buffer (J .. J + Slen1) = S then
10446 return True;
10447 end if;
10448 end loop;
10449
10450 return False;
10451 end Match;
10452
10453 -- Start of processing for Is_Junk_Name
10454
10455 begin
10456 Get_Unqualified_Decoded_Name_String (N);
10457 Set_All_Upper_Case;
10458
10459 return
10460 Match ("DISCARD") or else
10461 Match ("DUMMY") or else
10462 Match ("IGNORE") or else
10463 Match ("JUNK") or else
10464 Match ("UNUSED");
10465 end Is_Junk_Name;
10466
10467 ------------
10468 -- Is_LHS --
10469 ------------
10470
10471 -- We seem to have a lot of overlapping functions that do similar things
10472 -- (testing for left hand sides or lvalues???).
10473
10474 function Is_LHS (N : Node_Id) return Is_LHS_Result is
10475 P : constant Node_Id := Parent (N);
10476
10477 begin
10478 -- Return True if we are the left hand side of an assignment statement
10479
10480 if Nkind (P) = N_Assignment_Statement then
10481 if Name (P) = N then
10482 return Yes;
10483 else
10484 return No;
10485 end if;
10486
10487 -- Case of prefix of indexed or selected component or slice
10488
10489 elsif Nkind_In (P, N_Indexed_Component, N_Selected_Component, N_Slice)
10490 and then N = Prefix (P)
10491 then
10492 -- Here we have the case where the parent P is N.Q or N(Q .. R).
10493 -- If P is an LHS, then N is also effectively an LHS, but there
10494 -- is an important exception. If N is of an access type, then
10495 -- what we really have is N.all.Q (or N.all(Q .. R)). In either
10496 -- case this makes N.all a left hand side but not N itself.
10497
10498 -- If we don't know the type yet, this is the case where we return
10499 -- Unknown, since the answer depends on the type which is unknown.
10500
10501 if No (Etype (N)) then
10502 return Unknown;
10503
10504 -- We have an Etype set, so we can check it
10505
10506 elsif Is_Access_Type (Etype (N)) then
10507 return No;
10508
10509 -- OK, not access type case, so just test whole expression
10510
10511 else
10512 return Is_LHS (P);
10513 end if;
10514
10515 -- All other cases are not left hand sides
10516
10517 else
10518 return No;
10519 end if;
10520 end Is_LHS;
10521
10522 -----------------------------
10523 -- Is_Library_Level_Entity --
10524 -----------------------------
10525
10526 function Is_Library_Level_Entity (E : Entity_Id) return Boolean is
10527 begin
10528 -- The following is a small optimization, and it also properly handles
10529 -- discriminals, which in task bodies might appear in expressions before
10530 -- the corresponding procedure has been created, and which therefore do
10531 -- not have an assigned scope.
10532
10533 if Is_Formal (E) then
10534 return False;
10535 end if;
10536
10537 -- Normal test is simply that the enclosing dynamic scope is Standard
10538
10539 return Enclosing_Dynamic_Scope (E) = Standard_Standard;
10540 end Is_Library_Level_Entity;
10541
10542 --------------------------------
10543 -- Is_Limited_Class_Wide_Type --
10544 --------------------------------
10545
10546 function Is_Limited_Class_Wide_Type (Typ : Entity_Id) return Boolean is
10547 begin
10548 return
10549 Is_Class_Wide_Type (Typ)
10550 and then (Is_Limited_Type (Typ) or else From_Limited_With (Typ));
10551 end Is_Limited_Class_Wide_Type;
10552
10553 ---------------------------------
10554 -- Is_Local_Variable_Reference --
10555 ---------------------------------
10556
10557 function Is_Local_Variable_Reference (Expr : Node_Id) return Boolean is
10558 begin
10559 if not Is_Entity_Name (Expr) then
10560 return False;
10561
10562 else
10563 declare
10564 Ent : constant Entity_Id := Entity (Expr);
10565 Sub : constant Entity_Id := Enclosing_Subprogram (Ent);
10566 begin
10567 if not Ekind_In (Ent, E_Variable, E_In_Out_Parameter) then
10568 return False;
10569 else
10570 return Present (Sub) and then Sub = Current_Subprogram;
10571 end if;
10572 end;
10573 end if;
10574 end Is_Local_Variable_Reference;
10575
10576 -------------------------
10577 -- Is_Object_Reference --
10578 -------------------------
10579
10580 function Is_Object_Reference (N : Node_Id) return Boolean is
10581
10582 function Is_Internally_Generated_Renaming (N : Node_Id) return Boolean;
10583 -- Determine whether N is the name of an internally-generated renaming
10584
10585 --------------------------------------
10586 -- Is_Internally_Generated_Renaming --
10587 --------------------------------------
10588
10589 function Is_Internally_Generated_Renaming (N : Node_Id) return Boolean is
10590 P : Node_Id;
10591
10592 begin
10593 P := N;
10594 while Present (P) loop
10595 if Nkind (P) = N_Object_Renaming_Declaration then
10596 return not Comes_From_Source (P);
10597 elsif Is_List_Member (P) then
10598 return False;
10599 end if;
10600
10601 P := Parent (P);
10602 end loop;
10603
10604 return False;
10605 end Is_Internally_Generated_Renaming;
10606
10607 -- Start of processing for Is_Object_Reference
10608
10609 begin
10610 if Is_Entity_Name (N) then
10611 return Present (Entity (N)) and then Is_Object (Entity (N));
10612
10613 else
10614 case Nkind (N) is
10615 when N_Indexed_Component | N_Slice =>
10616 return
10617 Is_Object_Reference (Prefix (N))
10618 or else Is_Access_Type (Etype (Prefix (N)));
10619
10620 -- In Ada 95, a function call is a constant object; a procedure
10621 -- call is not.
10622
10623 when N_Function_Call =>
10624 return Etype (N) /= Standard_Void_Type;
10625
10626 -- Attributes 'Input, 'Old and 'Result produce objects
10627
10628 when N_Attribute_Reference =>
10629 return
10630 Nam_In
10631 (Attribute_Name (N), Name_Input, Name_Old, Name_Result);
10632
10633 when N_Selected_Component =>
10634 return
10635 Is_Object_Reference (Selector_Name (N))
10636 and then
10637 (Is_Object_Reference (Prefix (N))
10638 or else Is_Access_Type (Etype (Prefix (N))));
10639
10640 when N_Explicit_Dereference =>
10641 return True;
10642
10643 -- A view conversion of a tagged object is an object reference
10644
10645 when N_Type_Conversion =>
10646 return Is_Tagged_Type (Etype (Subtype_Mark (N)))
10647 and then Is_Tagged_Type (Etype (Expression (N)))
10648 and then Is_Object_Reference (Expression (N));
10649
10650 -- An unchecked type conversion is considered to be an object if
10651 -- the operand is an object (this construction arises only as a
10652 -- result of expansion activities).
10653
10654 when N_Unchecked_Type_Conversion =>
10655 return True;
10656
10657 -- Allow string literals to act as objects as long as they appear
10658 -- in internally-generated renamings. The expansion of iterators
10659 -- may generate such renamings when the range involves a string
10660 -- literal.
10661
10662 when N_String_Literal =>
10663 return Is_Internally_Generated_Renaming (Parent (N));
10664
10665 -- AI05-0003: In Ada 2012 a qualified expression is a name.
10666 -- This allows disambiguation of function calls and the use
10667 -- of aggregates in more contexts.
10668
10669 when N_Qualified_Expression =>
10670 if Ada_Version < Ada_2012 then
10671 return False;
10672 else
10673 return Is_Object_Reference (Expression (N))
10674 or else Nkind (Expression (N)) = N_Aggregate;
10675 end if;
10676
10677 when others =>
10678 return False;
10679 end case;
10680 end if;
10681 end Is_Object_Reference;
10682
10683 -----------------------------------
10684 -- Is_OK_Variable_For_Out_Formal --
10685 -----------------------------------
10686
10687 function Is_OK_Variable_For_Out_Formal (AV : Node_Id) return Boolean is
10688 begin
10689 Note_Possible_Modification (AV, Sure => True);
10690
10691 -- We must reject parenthesized variable names. Comes_From_Source is
10692 -- checked because there are currently cases where the compiler violates
10693 -- this rule (e.g. passing a task object to its controlled Initialize
10694 -- routine). This should be properly documented in sinfo???
10695
10696 if Paren_Count (AV) > 0 and then Comes_From_Source (AV) then
10697 return False;
10698
10699 -- A variable is always allowed
10700
10701 elsif Is_Variable (AV) then
10702 return True;
10703
10704 -- Unchecked conversions are allowed only if they come from the
10705 -- generated code, which sometimes uses unchecked conversions for out
10706 -- parameters in cases where code generation is unaffected. We tell
10707 -- source unchecked conversions by seeing if they are rewrites of
10708 -- an original Unchecked_Conversion function call, or of an explicit
10709 -- conversion of a function call or an aggregate (as may happen in the
10710 -- expansion of a packed array aggregate).
10711
10712 elsif Nkind (AV) = N_Unchecked_Type_Conversion then
10713 if Nkind_In (Original_Node (AV), N_Function_Call, N_Aggregate) then
10714 return False;
10715
10716 elsif Comes_From_Source (AV)
10717 and then Nkind (Original_Node (Expression (AV))) = N_Function_Call
10718 then
10719 return False;
10720
10721 elsif Nkind (Original_Node (AV)) = N_Type_Conversion then
10722 return Is_OK_Variable_For_Out_Formal (Expression (AV));
10723
10724 else
10725 return True;
10726 end if;
10727
10728 -- Normal type conversions are allowed if argument is a variable
10729
10730 elsif Nkind (AV) = N_Type_Conversion then
10731 if Is_Variable (Expression (AV))
10732 and then Paren_Count (Expression (AV)) = 0
10733 then
10734 Note_Possible_Modification (Expression (AV), Sure => True);
10735 return True;
10736
10737 -- We also allow a non-parenthesized expression that raises
10738 -- constraint error if it rewrites what used to be a variable
10739
10740 elsif Raises_Constraint_Error (Expression (AV))
10741 and then Paren_Count (Expression (AV)) = 0
10742 and then Is_Variable (Original_Node (Expression (AV)))
10743 then
10744 return True;
10745
10746 -- Type conversion of something other than a variable
10747
10748 else
10749 return False;
10750 end if;
10751
10752 -- If this node is rewritten, then test the original form, if that is
10753 -- OK, then we consider the rewritten node OK (for example, if the
10754 -- original node is a conversion, then Is_Variable will not be true
10755 -- but we still want to allow the conversion if it converts a variable).
10756
10757 elsif Original_Node (AV) /= AV then
10758
10759 -- In Ada 2012, the explicit dereference may be a rewritten call to a
10760 -- Reference function.
10761
10762 if Ada_Version >= Ada_2012
10763 and then Nkind (Original_Node (AV)) = N_Function_Call
10764 and then
10765 Has_Implicit_Dereference (Etype (Name (Original_Node (AV))))
10766 then
10767 return True;
10768
10769 else
10770 return Is_OK_Variable_For_Out_Formal (Original_Node (AV));
10771 end if;
10772
10773 -- All other non-variables are rejected
10774
10775 else
10776 return False;
10777 end if;
10778 end Is_OK_Variable_For_Out_Formal;
10779
10780 -----------------------------------
10781 -- Is_Partially_Initialized_Type --
10782 -----------------------------------
10783
10784 function Is_Partially_Initialized_Type
10785 (Typ : Entity_Id;
10786 Include_Implicit : Boolean := True) return Boolean
10787 is
10788 begin
10789 if Is_Scalar_Type (Typ) then
10790 return False;
10791
10792 elsif Is_Access_Type (Typ) then
10793 return Include_Implicit;
10794
10795 elsif Is_Array_Type (Typ) then
10796
10797 -- If component type is partially initialized, so is array type
10798
10799 if Is_Partially_Initialized_Type
10800 (Component_Type (Typ), Include_Implicit)
10801 then
10802 return True;
10803
10804 -- Otherwise we are only partially initialized if we are fully
10805 -- initialized (this is the empty array case, no point in us
10806 -- duplicating that code here).
10807
10808 else
10809 return Is_Fully_Initialized_Type (Typ);
10810 end if;
10811
10812 elsif Is_Record_Type (Typ) then
10813
10814 -- A discriminated type is always partially initialized if in
10815 -- all mode
10816
10817 if Has_Discriminants (Typ) and then Include_Implicit then
10818 return True;
10819
10820 -- A tagged type is always partially initialized
10821
10822 elsif Is_Tagged_Type (Typ) then
10823 return True;
10824
10825 -- Case of non-discriminated record
10826
10827 else
10828 declare
10829 Ent : Entity_Id;
10830
10831 Component_Present : Boolean := False;
10832 -- Set True if at least one component is present. If no
10833 -- components are present, then record type is fully
10834 -- initialized (another odd case, like the null array).
10835
10836 begin
10837 -- Loop through components
10838
10839 Ent := First_Entity (Typ);
10840 while Present (Ent) loop
10841 if Ekind (Ent) = E_Component then
10842 Component_Present := True;
10843
10844 -- If a component has an initialization expression then
10845 -- the enclosing record type is partially initialized
10846
10847 if Present (Parent (Ent))
10848 and then Present (Expression (Parent (Ent)))
10849 then
10850 return True;
10851
10852 -- If a component is of a type which is itself partially
10853 -- initialized, then the enclosing record type is also.
10854
10855 elsif Is_Partially_Initialized_Type
10856 (Etype (Ent), Include_Implicit)
10857 then
10858 return True;
10859 end if;
10860 end if;
10861
10862 Next_Entity (Ent);
10863 end loop;
10864
10865 -- No initialized components found. If we found any components
10866 -- they were all uninitialized so the result is false.
10867
10868 if Component_Present then
10869 return False;
10870
10871 -- But if we found no components, then all the components are
10872 -- initialized so we consider the type to be initialized.
10873
10874 else
10875 return True;
10876 end if;
10877 end;
10878 end if;
10879
10880 -- Concurrent types are always fully initialized
10881
10882 elsif Is_Concurrent_Type (Typ) then
10883 return True;
10884
10885 -- For a private type, go to underlying type. If there is no underlying
10886 -- type then just assume this partially initialized. Not clear if this
10887 -- can happen in a non-error case, but no harm in testing for this.
10888
10889 elsif Is_Private_Type (Typ) then
10890 declare
10891 U : constant Entity_Id := Underlying_Type (Typ);
10892 begin
10893 if No (U) then
10894 return True;
10895 else
10896 return Is_Partially_Initialized_Type (U, Include_Implicit);
10897 end if;
10898 end;
10899
10900 -- For any other type (are there any?) assume partially initialized
10901
10902 else
10903 return True;
10904 end if;
10905 end Is_Partially_Initialized_Type;
10906
10907 ------------------------------------
10908 -- Is_Potentially_Persistent_Type --
10909 ------------------------------------
10910
10911 function Is_Potentially_Persistent_Type (T : Entity_Id) return Boolean is
10912 Comp : Entity_Id;
10913 Indx : Node_Id;
10914
10915 begin
10916 -- For private type, test corresponding full type
10917
10918 if Is_Private_Type (T) then
10919 return Is_Potentially_Persistent_Type (Full_View (T));
10920
10921 -- Scalar types are potentially persistent
10922
10923 elsif Is_Scalar_Type (T) then
10924 return True;
10925
10926 -- Record type is potentially persistent if not tagged and the types of
10927 -- all it components are potentially persistent, and no component has
10928 -- an initialization expression.
10929
10930 elsif Is_Record_Type (T)
10931 and then not Is_Tagged_Type (T)
10932 and then not Is_Partially_Initialized_Type (T)
10933 then
10934 Comp := First_Component (T);
10935 while Present (Comp) loop
10936 if not Is_Potentially_Persistent_Type (Etype (Comp)) then
10937 return False;
10938 else
10939 Next_Entity (Comp);
10940 end if;
10941 end loop;
10942
10943 return True;
10944
10945 -- Array type is potentially persistent if its component type is
10946 -- potentially persistent and if all its constraints are static.
10947
10948 elsif Is_Array_Type (T) then
10949 if not Is_Potentially_Persistent_Type (Component_Type (T)) then
10950 return False;
10951 end if;
10952
10953 Indx := First_Index (T);
10954 while Present (Indx) loop
10955 if not Is_OK_Static_Subtype (Etype (Indx)) then
10956 return False;
10957 else
10958 Next_Index (Indx);
10959 end if;
10960 end loop;
10961
10962 return True;
10963
10964 -- All other types are not potentially persistent
10965
10966 else
10967 return False;
10968 end if;
10969 end Is_Potentially_Persistent_Type;
10970
10971 --------------------------------
10972 -- Is_Potentially_Unevaluated --
10973 --------------------------------
10974
10975 function Is_Potentially_Unevaluated (N : Node_Id) return Boolean is
10976 Par : Node_Id;
10977 Expr : Node_Id;
10978
10979 begin
10980 Expr := N;
10981 Par := Parent (N);
10982 while not Nkind_In (Par, N_If_Expression,
10983 N_Case_Expression,
10984 N_And_Then,
10985 N_Or_Else,
10986 N_In,
10987 N_Not_In)
10988 loop
10989 Expr := Par;
10990 Par := Parent (Par);
10991
10992 -- If the context is not an expression, or if is the result of
10993 -- expansion of an enclosing construct (such as another attribute)
10994 -- the predicate does not apply.
10995
10996 if Nkind (Par) not in N_Subexpr
10997 or else not Comes_From_Source (Par)
10998 then
10999 return False;
11000 end if;
11001 end loop;
11002
11003 if Nkind (Par) = N_If_Expression then
11004 return Is_Elsif (Par) or else Expr /= First (Expressions (Par));
11005
11006 elsif Nkind (Par) = N_Case_Expression then
11007 return Expr /= Expression (Par);
11008
11009 elsif Nkind_In (Par, N_And_Then, N_Or_Else) then
11010 return Expr = Right_Opnd (Par);
11011
11012 elsif Nkind_In (Par, N_In, N_Not_In) then
11013 return Expr /= Left_Opnd (Par);
11014
11015 else
11016 return False;
11017 end if;
11018 end Is_Potentially_Unevaluated;
11019
11020 ---------------------------------
11021 -- Is_Protected_Self_Reference --
11022 ---------------------------------
11023
11024 function Is_Protected_Self_Reference (N : Node_Id) return Boolean is
11025
11026 function In_Access_Definition (N : Node_Id) return Boolean;
11027 -- Returns true if N belongs to an access definition
11028
11029 --------------------------
11030 -- In_Access_Definition --
11031 --------------------------
11032
11033 function In_Access_Definition (N : Node_Id) return Boolean is
11034 P : Node_Id;
11035
11036 begin
11037 P := Parent (N);
11038 while Present (P) loop
11039 if Nkind (P) = N_Access_Definition then
11040 return True;
11041 end if;
11042
11043 P := Parent (P);
11044 end loop;
11045
11046 return False;
11047 end In_Access_Definition;
11048
11049 -- Start of processing for Is_Protected_Self_Reference
11050
11051 begin
11052 -- Verify that prefix is analyzed and has the proper form. Note that
11053 -- the attributes Elab_Spec, Elab_Body, Elab_Subp_Body and UET_Address,
11054 -- which also produce the address of an entity, do not analyze their
11055 -- prefix because they denote entities that are not necessarily visible.
11056 -- Neither of them can apply to a protected type.
11057
11058 return Ada_Version >= Ada_2005
11059 and then Is_Entity_Name (N)
11060 and then Present (Entity (N))
11061 and then Is_Protected_Type (Entity (N))
11062 and then In_Open_Scopes (Entity (N))
11063 and then not In_Access_Definition (N);
11064 end Is_Protected_Self_Reference;
11065
11066 -----------------------------
11067 -- Is_RCI_Pkg_Spec_Or_Body --
11068 -----------------------------
11069
11070 function Is_RCI_Pkg_Spec_Or_Body (Cunit : Node_Id) return Boolean is
11071
11072 function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean;
11073 -- Return True if the unit of Cunit is an RCI package declaration
11074
11075 ---------------------------
11076 -- Is_RCI_Pkg_Decl_Cunit --
11077 ---------------------------
11078
11079 function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean is
11080 The_Unit : constant Node_Id := Unit (Cunit);
11081
11082 begin
11083 if Nkind (The_Unit) /= N_Package_Declaration then
11084 return False;
11085 end if;
11086
11087 return Is_Remote_Call_Interface (Defining_Entity (The_Unit));
11088 end Is_RCI_Pkg_Decl_Cunit;
11089
11090 -- Start of processing for Is_RCI_Pkg_Spec_Or_Body
11091
11092 begin
11093 return Is_RCI_Pkg_Decl_Cunit (Cunit)
11094 or else
11095 (Nkind (Unit (Cunit)) = N_Package_Body
11096 and then Is_RCI_Pkg_Decl_Cunit (Library_Unit (Cunit)));
11097 end Is_RCI_Pkg_Spec_Or_Body;
11098
11099 -----------------------------------------
11100 -- Is_Remote_Access_To_Class_Wide_Type --
11101 -----------------------------------------
11102
11103 function Is_Remote_Access_To_Class_Wide_Type
11104 (E : Entity_Id) return Boolean
11105 is
11106 begin
11107 -- A remote access to class-wide type is a general access to object type
11108 -- declared in the visible part of a Remote_Types or Remote_Call_
11109 -- Interface unit.
11110
11111 return Ekind (E) = E_General_Access_Type
11112 and then (Is_Remote_Call_Interface (E) or else Is_Remote_Types (E));
11113 end Is_Remote_Access_To_Class_Wide_Type;
11114
11115 -----------------------------------------
11116 -- Is_Remote_Access_To_Subprogram_Type --
11117 -----------------------------------------
11118
11119 function Is_Remote_Access_To_Subprogram_Type
11120 (E : Entity_Id) return Boolean
11121 is
11122 begin
11123 return (Ekind (E) = E_Access_Subprogram_Type
11124 or else (Ekind (E) = E_Record_Type
11125 and then Present (Corresponding_Remote_Type (E))))
11126 and then (Is_Remote_Call_Interface (E) or else Is_Remote_Types (E));
11127 end Is_Remote_Access_To_Subprogram_Type;
11128
11129 --------------------
11130 -- Is_Remote_Call --
11131 --------------------
11132
11133 function Is_Remote_Call (N : Node_Id) return Boolean is
11134 begin
11135 if Nkind (N) not in N_Subprogram_Call then
11136
11137 -- An entry call cannot be remote
11138
11139 return False;
11140
11141 elsif Nkind (Name (N)) in N_Has_Entity
11142 and then Is_Remote_Call_Interface (Entity (Name (N)))
11143 then
11144 -- A subprogram declared in the spec of a RCI package is remote
11145
11146 return True;
11147
11148 elsif Nkind (Name (N)) = N_Explicit_Dereference
11149 and then Is_Remote_Access_To_Subprogram_Type
11150 (Etype (Prefix (Name (N))))
11151 then
11152 -- The dereference of a RAS is a remote call
11153
11154 return True;
11155
11156 elsif Present (Controlling_Argument (N))
11157 and then Is_Remote_Access_To_Class_Wide_Type
11158 (Etype (Controlling_Argument (N)))
11159 then
11160 -- Any primitive operation call with a controlling argument of
11161 -- a RACW type is a remote call.
11162
11163 return True;
11164 end if;
11165
11166 -- All other calls are local calls
11167
11168 return False;
11169 end Is_Remote_Call;
11170
11171 ----------------------
11172 -- Is_Renamed_Entry --
11173 ----------------------
11174
11175 function Is_Renamed_Entry (Proc_Nam : Entity_Id) return Boolean is
11176 Orig_Node : Node_Id := Empty;
11177 Subp_Decl : Node_Id := Parent (Parent (Proc_Nam));
11178
11179 function Is_Entry (Nam : Node_Id) return Boolean;
11180 -- Determine whether Nam is an entry. Traverse selectors if there are
11181 -- nested selected components.
11182
11183 --------------
11184 -- Is_Entry --
11185 --------------
11186
11187 function Is_Entry (Nam : Node_Id) return Boolean is
11188 begin
11189 if Nkind (Nam) = N_Selected_Component then
11190 return Is_Entry (Selector_Name (Nam));
11191 end if;
11192
11193 return Ekind (Entity (Nam)) = E_Entry;
11194 end Is_Entry;
11195
11196 -- Start of processing for Is_Renamed_Entry
11197
11198 begin
11199 if Present (Alias (Proc_Nam)) then
11200 Subp_Decl := Parent (Parent (Alias (Proc_Nam)));
11201 end if;
11202
11203 -- Look for a rewritten subprogram renaming declaration
11204
11205 if Nkind (Subp_Decl) = N_Subprogram_Declaration
11206 and then Present (Original_Node (Subp_Decl))
11207 then
11208 Orig_Node := Original_Node (Subp_Decl);
11209 end if;
11210
11211 -- The rewritten subprogram is actually an entry
11212
11213 if Present (Orig_Node)
11214 and then Nkind (Orig_Node) = N_Subprogram_Renaming_Declaration
11215 and then Is_Entry (Name (Orig_Node))
11216 then
11217 return True;
11218 end if;
11219
11220 return False;
11221 end Is_Renamed_Entry;
11222
11223 ----------------------------
11224 -- Is_Reversible_Iterator --
11225 ----------------------------
11226
11227 function Is_Reversible_Iterator (Typ : Entity_Id) return Boolean is
11228 Ifaces_List : Elist_Id;
11229 Iface_Elmt : Elmt_Id;
11230 Iface : Entity_Id;
11231
11232 begin
11233 if Is_Class_Wide_Type (Typ)
11234 and then Chars (Etype (Typ)) = Name_Reversible_Iterator
11235 and then
11236 Is_Predefined_File_Name
11237 (Unit_File_Name (Get_Source_Unit (Etype (Typ))))
11238 then
11239 return True;
11240
11241 elsif not Is_Tagged_Type (Typ)
11242 or else not Is_Derived_Type (Typ)
11243 then
11244 return False;
11245
11246 else
11247 Collect_Interfaces (Typ, Ifaces_List);
11248
11249 Iface_Elmt := First_Elmt (Ifaces_List);
11250 while Present (Iface_Elmt) loop
11251 Iface := Node (Iface_Elmt);
11252 if Chars (Iface) = Name_Reversible_Iterator
11253 and then
11254 Is_Predefined_File_Name
11255 (Unit_File_Name (Get_Source_Unit (Iface)))
11256 then
11257 return True;
11258 end if;
11259
11260 Next_Elmt (Iface_Elmt);
11261 end loop;
11262 end if;
11263
11264 return False;
11265 end Is_Reversible_Iterator;
11266
11267 ----------------------
11268 -- Is_Selector_Name --
11269 ----------------------
11270
11271 function Is_Selector_Name (N : Node_Id) return Boolean is
11272 begin
11273 if not Is_List_Member (N) then
11274 declare
11275 P : constant Node_Id := Parent (N);
11276 K : constant Node_Kind := Nkind (P);
11277 begin
11278 return
11279 (K = N_Expanded_Name or else
11280 K = N_Generic_Association or else
11281 K = N_Parameter_Association or else
11282 K = N_Selected_Component)
11283 and then Selector_Name (P) = N;
11284 end;
11285
11286 else
11287 declare
11288 L : constant List_Id := List_Containing (N);
11289 P : constant Node_Id := Parent (L);
11290 begin
11291 return (Nkind (P) = N_Discriminant_Association
11292 and then Selector_Names (P) = L)
11293 or else
11294 (Nkind (P) = N_Component_Association
11295 and then Choices (P) = L);
11296 end;
11297 end if;
11298 end Is_Selector_Name;
11299
11300 ----------------------------------
11301 -- Is_SPARK_Initialization_Expr --
11302 ----------------------------------
11303
11304 function Is_SPARK_Initialization_Expr (N : Node_Id) return Boolean is
11305 Is_Ok : Boolean;
11306 Expr : Node_Id;
11307 Comp_Assn : Node_Id;
11308 Orig_N : constant Node_Id := Original_Node (N);
11309
11310 begin
11311 Is_Ok := True;
11312
11313 if not Comes_From_Source (Orig_N) then
11314 goto Done;
11315 end if;
11316
11317 pragma Assert (Nkind (Orig_N) in N_Subexpr);
11318
11319 case Nkind (Orig_N) is
11320 when N_Character_Literal |
11321 N_Integer_Literal |
11322 N_Real_Literal |
11323 N_String_Literal =>
11324 null;
11325
11326 when N_Identifier |
11327 N_Expanded_Name =>
11328 if Is_Entity_Name (Orig_N)
11329 and then Present (Entity (Orig_N)) -- needed in some cases
11330 then
11331 case Ekind (Entity (Orig_N)) is
11332 when E_Constant |
11333 E_Enumeration_Literal |
11334 E_Named_Integer |
11335 E_Named_Real =>
11336 null;
11337 when others =>
11338 if Is_Type (Entity (Orig_N)) then
11339 null;
11340 else
11341 Is_Ok := False;
11342 end if;
11343 end case;
11344 end if;
11345
11346 when N_Qualified_Expression |
11347 N_Type_Conversion =>
11348 Is_Ok := Is_SPARK_Initialization_Expr (Expression (Orig_N));
11349
11350 when N_Unary_Op =>
11351 Is_Ok := Is_SPARK_Initialization_Expr (Right_Opnd (Orig_N));
11352
11353 when N_Binary_Op |
11354 N_Short_Circuit |
11355 N_Membership_Test =>
11356 Is_Ok := Is_SPARK_Initialization_Expr (Left_Opnd (Orig_N))
11357 and then Is_SPARK_Initialization_Expr (Right_Opnd (Orig_N));
11358
11359 when N_Aggregate |
11360 N_Extension_Aggregate =>
11361 if Nkind (Orig_N) = N_Extension_Aggregate then
11362 Is_Ok := Is_SPARK_Initialization_Expr (Ancestor_Part (Orig_N));
11363 end if;
11364
11365 Expr := First (Expressions (Orig_N));
11366 while Present (Expr) loop
11367 if not Is_SPARK_Initialization_Expr (Expr) then
11368 Is_Ok := False;
11369 goto Done;
11370 end if;
11371
11372 Next (Expr);
11373 end loop;
11374
11375 Comp_Assn := First (Component_Associations (Orig_N));
11376 while Present (Comp_Assn) loop
11377 Expr := Expression (Comp_Assn);
11378 if Present (Expr) -- needed for box association
11379 and then not Is_SPARK_Initialization_Expr (Expr)
11380 then
11381 Is_Ok := False;
11382 goto Done;
11383 end if;
11384
11385 Next (Comp_Assn);
11386 end loop;
11387
11388 when N_Attribute_Reference =>
11389 if Nkind (Prefix (Orig_N)) in N_Subexpr then
11390 Is_Ok := Is_SPARK_Initialization_Expr (Prefix (Orig_N));
11391 end if;
11392
11393 Expr := First (Expressions (Orig_N));
11394 while Present (Expr) loop
11395 if not Is_SPARK_Initialization_Expr (Expr) then
11396 Is_Ok := False;
11397 goto Done;
11398 end if;
11399
11400 Next (Expr);
11401 end loop;
11402
11403 -- Selected components might be expanded named not yet resolved, so
11404 -- default on the safe side. (Eg on sparklex.ads)
11405
11406 when N_Selected_Component =>
11407 null;
11408
11409 when others =>
11410 Is_Ok := False;
11411 end case;
11412
11413 <<Done>>
11414 return Is_Ok;
11415 end Is_SPARK_Initialization_Expr;
11416
11417 -------------------------------
11418 -- Is_SPARK_Object_Reference --
11419 -------------------------------
11420
11421 function Is_SPARK_Object_Reference (N : Node_Id) return Boolean is
11422 begin
11423 if Is_Entity_Name (N) then
11424 return Present (Entity (N))
11425 and then
11426 (Ekind_In (Entity (N), E_Constant, E_Variable)
11427 or else Ekind (Entity (N)) in Formal_Kind);
11428
11429 else
11430 case Nkind (N) is
11431 when N_Selected_Component =>
11432 return Is_SPARK_Object_Reference (Prefix (N));
11433
11434 when others =>
11435 return False;
11436 end case;
11437 end if;
11438 end Is_SPARK_Object_Reference;
11439
11440 -----------------------
11441 -- Is_SPARK_Volatile --
11442 -----------------------
11443
11444 function Is_SPARK_Volatile (Id : Entity_Id) return Boolean is
11445 begin
11446 return Is_Volatile (Id) or else Is_Volatile (Etype (Id));
11447 end Is_SPARK_Volatile;
11448
11449 ------------------------------
11450 -- Is_SPARK_Volatile_Object --
11451 ------------------------------
11452
11453 function Is_SPARK_Volatile_Object (N : Node_Id) return Boolean is
11454 begin
11455 if Is_Entity_Name (N) then
11456 return Is_SPARK_Volatile (Entity (N));
11457
11458 elsif Nkind (N) = N_Expanded_Name then
11459 return Is_SPARK_Volatile (Entity (N));
11460
11461 elsif Nkind (N) = N_Indexed_Component then
11462 return Is_SPARK_Volatile_Object (Prefix (N));
11463
11464 elsif Nkind (N) = N_Selected_Component then
11465 return
11466 Is_SPARK_Volatile_Object (Prefix (N))
11467 or else
11468 Is_SPARK_Volatile_Object (Selector_Name (N));
11469
11470 else
11471 return False;
11472 end if;
11473 end Is_SPARK_Volatile_Object;
11474
11475 ------------------
11476 -- Is_Statement --
11477 ------------------
11478
11479 function Is_Statement (N : Node_Id) return Boolean is
11480 begin
11481 return
11482 Nkind (N) in N_Statement_Other_Than_Procedure_Call
11483 or else Nkind (N) = N_Procedure_Call_Statement;
11484 end Is_Statement;
11485
11486 --------------------------------------------------
11487 -- Is_Subprogram_Stub_Without_Prior_Declaration --
11488 --------------------------------------------------
11489
11490 function Is_Subprogram_Stub_Without_Prior_Declaration
11491 (N : Node_Id) return Boolean
11492 is
11493 begin
11494 -- A subprogram stub without prior declaration serves as declaration for
11495 -- the actual subprogram body. As such, it has an attached defining
11496 -- entity of E_[Generic_]Function or E_[Generic_]Procedure.
11497
11498 return Nkind (N) = N_Subprogram_Body_Stub
11499 and then Ekind (Defining_Entity (N)) /= E_Subprogram_Body;
11500 end Is_Subprogram_Stub_Without_Prior_Declaration;
11501
11502 ---------------------------------
11503 -- Is_Synchronized_Tagged_Type --
11504 ---------------------------------
11505
11506 function Is_Synchronized_Tagged_Type (E : Entity_Id) return Boolean is
11507 Kind : constant Entity_Kind := Ekind (Base_Type (E));
11508
11509 begin
11510 -- A task or protected type derived from an interface is a tagged type.
11511 -- Such a tagged type is called a synchronized tagged type, as are
11512 -- synchronized interfaces and private extensions whose declaration
11513 -- includes the reserved word synchronized.
11514
11515 return (Is_Tagged_Type (E)
11516 and then (Kind = E_Task_Type
11517 or else Kind = E_Protected_Type))
11518 or else
11519 (Is_Interface (E)
11520 and then Is_Synchronized_Interface (E))
11521 or else
11522 (Ekind (E) = E_Record_Type_With_Private
11523 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
11524 and then (Synchronized_Present (Parent (E))
11525 or else Is_Synchronized_Interface (Etype (E))));
11526 end Is_Synchronized_Tagged_Type;
11527
11528 -----------------
11529 -- Is_Transfer --
11530 -----------------
11531
11532 function Is_Transfer (N : Node_Id) return Boolean is
11533 Kind : constant Node_Kind := Nkind (N);
11534
11535 begin
11536 if Kind = N_Simple_Return_Statement
11537 or else
11538 Kind = N_Extended_Return_Statement
11539 or else
11540 Kind = N_Goto_Statement
11541 or else
11542 Kind = N_Raise_Statement
11543 or else
11544 Kind = N_Requeue_Statement
11545 then
11546 return True;
11547
11548 elsif (Kind = N_Exit_Statement or else Kind in N_Raise_xxx_Error)
11549 and then No (Condition (N))
11550 then
11551 return True;
11552
11553 elsif Kind = N_Procedure_Call_Statement
11554 and then Is_Entity_Name (Name (N))
11555 and then Present (Entity (Name (N)))
11556 and then No_Return (Entity (Name (N)))
11557 then
11558 return True;
11559
11560 elsif Nkind (Original_Node (N)) = N_Raise_Statement then
11561 return True;
11562
11563 else
11564 return False;
11565 end if;
11566 end Is_Transfer;
11567
11568 -------------
11569 -- Is_True --
11570 -------------
11571
11572 function Is_True (U : Uint) return Boolean is
11573 begin
11574 return (U /= 0);
11575 end Is_True;
11576
11577 --------------------------------------
11578 -- Is_Unchecked_Conversion_Instance --
11579 --------------------------------------
11580
11581 function Is_Unchecked_Conversion_Instance (Id : Entity_Id) return Boolean is
11582 Gen_Par : Entity_Id;
11583
11584 begin
11585 -- Look for a function whose generic parent is the predefined intrinsic
11586 -- function Unchecked_Conversion.
11587
11588 if Ekind (Id) = E_Function then
11589 Gen_Par := Generic_Parent (Parent (Id));
11590
11591 return
11592 Present (Gen_Par)
11593 and then Chars (Gen_Par) = Name_Unchecked_Conversion
11594 and then Is_Intrinsic_Subprogram (Gen_Par)
11595 and then Is_Predefined_File_Name
11596 (Unit_File_Name (Get_Source_Unit (Gen_Par)));
11597 end if;
11598
11599 return False;
11600 end Is_Unchecked_Conversion_Instance;
11601
11602 -------------------------------
11603 -- Is_Universal_Numeric_Type --
11604 -------------------------------
11605
11606 function Is_Universal_Numeric_Type (T : Entity_Id) return Boolean is
11607 begin
11608 return T = Universal_Integer or else T = Universal_Real;
11609 end Is_Universal_Numeric_Type;
11610
11611 -------------------
11612 -- Is_Value_Type --
11613 -------------------
11614
11615 function Is_Value_Type (T : Entity_Id) return Boolean is
11616 begin
11617 return VM_Target = CLI_Target
11618 and then Nkind (T) in N_Has_Chars
11619 and then Chars (T) /= No_Name
11620 and then Get_Name_String (Chars (T)) = "valuetype";
11621 end Is_Value_Type;
11622
11623 ----------------------------
11624 -- Is_Variable_Size_Array --
11625 ----------------------------
11626
11627 function Is_Variable_Size_Array (E : Entity_Id) return Boolean is
11628 Idx : Node_Id;
11629
11630 begin
11631 pragma Assert (Is_Array_Type (E));
11632
11633 -- Check if some index is initialized with a non-constant value
11634
11635 Idx := First_Index (E);
11636 while Present (Idx) loop
11637 if Nkind (Idx) = N_Range then
11638 if not Is_Constant_Bound (Low_Bound (Idx))
11639 or else not Is_Constant_Bound (High_Bound (Idx))
11640 then
11641 return True;
11642 end if;
11643 end if;
11644
11645 Idx := Next_Index (Idx);
11646 end loop;
11647
11648 return False;
11649 end Is_Variable_Size_Array;
11650
11651 -----------------------------
11652 -- Is_Variable_Size_Record --
11653 -----------------------------
11654
11655 function Is_Variable_Size_Record (E : Entity_Id) return Boolean is
11656 Comp : Entity_Id;
11657 Comp_Typ : Entity_Id;
11658
11659 begin
11660 pragma Assert (Is_Record_Type (E));
11661
11662 Comp := First_Entity (E);
11663 while Present (Comp) loop
11664 Comp_Typ := Etype (Comp);
11665
11666 -- Recursive call if the record type has discriminants
11667
11668 if Is_Record_Type (Comp_Typ)
11669 and then Has_Discriminants (Comp_Typ)
11670 and then Is_Variable_Size_Record (Comp_Typ)
11671 then
11672 return True;
11673
11674 elsif Is_Array_Type (Comp_Typ)
11675 and then Is_Variable_Size_Array (Comp_Typ)
11676 then
11677 return True;
11678 end if;
11679
11680 Next_Entity (Comp);
11681 end loop;
11682
11683 return False;
11684 end Is_Variable_Size_Record;
11685
11686 ---------------------
11687 -- Is_VMS_Operator --
11688 ---------------------
11689
11690 function Is_VMS_Operator (Op : Entity_Id) return Boolean is
11691 begin
11692 -- The VMS operators are declared in a child of System that is loaded
11693 -- through pragma Extend_System. In some rare cases a program is run
11694 -- with this extension but without indicating that the target is VMS.
11695
11696 return Ekind (Op) = E_Function
11697 and then Is_Intrinsic_Subprogram (Op)
11698 and then
11699 ((Present_System_Aux and then Scope (Op) = System_Aux_Id)
11700 or else
11701 (True_VMS_Target
11702 and then Scope (Scope (Op)) = RTU_Entity (System)));
11703 end Is_VMS_Operator;
11704
11705 -----------------
11706 -- Is_Variable --
11707 -----------------
11708
11709 function Is_Variable
11710 (N : Node_Id;
11711 Use_Original_Node : Boolean := True) return Boolean
11712 is
11713 Orig_Node : Node_Id;
11714
11715 function In_Protected_Function (E : Entity_Id) return Boolean;
11716 -- Within a protected function, the private components of the enclosing
11717 -- protected type are constants. A function nested within a (protected)
11718 -- procedure is not itself protected. Within the body of a protected
11719 -- function the current instance of the protected type is a constant.
11720
11721 function Is_Variable_Prefix (P : Node_Id) return Boolean;
11722 -- Prefixes can involve implicit dereferences, in which case we must
11723 -- test for the case of a reference of a constant access type, which can
11724 -- can never be a variable.
11725
11726 ---------------------------
11727 -- In_Protected_Function --
11728 ---------------------------
11729
11730 function In_Protected_Function (E : Entity_Id) return Boolean is
11731 Prot : Entity_Id;
11732 S : Entity_Id;
11733
11734 begin
11735 -- E is the current instance of a type
11736
11737 if Is_Type (E) then
11738 Prot := E;
11739
11740 -- E is an object
11741
11742 else
11743 Prot := Scope (E);
11744 end if;
11745
11746 if not Is_Protected_Type (Prot) then
11747 return False;
11748
11749 else
11750 S := Current_Scope;
11751 while Present (S) and then S /= Prot loop
11752 if Ekind (S) = E_Function and then Scope (S) = Prot then
11753 return True;
11754 end if;
11755
11756 S := Scope (S);
11757 end loop;
11758
11759 return False;
11760 end if;
11761 end In_Protected_Function;
11762
11763 ------------------------
11764 -- Is_Variable_Prefix --
11765 ------------------------
11766
11767 function Is_Variable_Prefix (P : Node_Id) return Boolean is
11768 begin
11769 if Is_Access_Type (Etype (P)) then
11770 return not Is_Access_Constant (Root_Type (Etype (P)));
11771
11772 -- For the case of an indexed component whose prefix has a packed
11773 -- array type, the prefix has been rewritten into a type conversion.
11774 -- Determine variable-ness from the converted expression.
11775
11776 elsif Nkind (P) = N_Type_Conversion
11777 and then not Comes_From_Source (P)
11778 and then Is_Array_Type (Etype (P))
11779 and then Is_Packed (Etype (P))
11780 then
11781 return Is_Variable (Expression (P));
11782
11783 else
11784 return Is_Variable (P);
11785 end if;
11786 end Is_Variable_Prefix;
11787
11788 -- Start of processing for Is_Variable
11789
11790 begin
11791 -- Check if we perform the test on the original node since this may be a
11792 -- test of syntactic categories which must not be disturbed by whatever
11793 -- rewriting might have occurred. For example, an aggregate, which is
11794 -- certainly NOT a variable, could be turned into a variable by
11795 -- expansion.
11796
11797 if Use_Original_Node then
11798 Orig_Node := Original_Node (N);
11799 else
11800 Orig_Node := N;
11801 end if;
11802
11803 -- Definitely OK if Assignment_OK is set. Since this is something that
11804 -- only gets set for expanded nodes, the test is on N, not Orig_Node.
11805
11806 if Nkind (N) in N_Subexpr and then Assignment_OK (N) then
11807 return True;
11808
11809 -- Normally we go to the original node, but there is one exception where
11810 -- we use the rewritten node, namely when it is an explicit dereference.
11811 -- The generated code may rewrite a prefix which is an access type with
11812 -- an explicit dereference. The dereference is a variable, even though
11813 -- the original node may not be (since it could be a constant of the
11814 -- access type).
11815
11816 -- In Ada 2005 we have a further case to consider: the prefix may be a
11817 -- function call given in prefix notation. The original node appears to
11818 -- be a selected component, but we need to examine the call.
11819
11820 elsif Nkind (N) = N_Explicit_Dereference
11821 and then Nkind (Orig_Node) /= N_Explicit_Dereference
11822 and then Present (Etype (Orig_Node))
11823 and then Is_Access_Type (Etype (Orig_Node))
11824 then
11825 -- Note that if the prefix is an explicit dereference that does not
11826 -- come from source, we must check for a rewritten function call in
11827 -- prefixed notation before other forms of rewriting, to prevent a
11828 -- compiler crash.
11829
11830 return
11831 (Nkind (Orig_Node) = N_Function_Call
11832 and then not Is_Access_Constant (Etype (Prefix (N))))
11833 or else
11834 Is_Variable_Prefix (Original_Node (Prefix (N)));
11835
11836 -- in Ada 2012, the dereference may have been added for a type with
11837 -- a declared implicit dereference aspect.
11838
11839 elsif Nkind (N) = N_Explicit_Dereference
11840 and then Present (Etype (Orig_Node))
11841 and then Ada_Version >= Ada_2012
11842 and then Has_Implicit_Dereference (Etype (Orig_Node))
11843 then
11844 return True;
11845
11846 -- A function call is never a variable
11847
11848 elsif Nkind (N) = N_Function_Call then
11849 return False;
11850
11851 -- All remaining checks use the original node
11852
11853 elsif Is_Entity_Name (Orig_Node)
11854 and then Present (Entity (Orig_Node))
11855 then
11856 declare
11857 E : constant Entity_Id := Entity (Orig_Node);
11858 K : constant Entity_Kind := Ekind (E);
11859
11860 begin
11861 return (K = E_Variable
11862 and then Nkind (Parent (E)) /= N_Exception_Handler)
11863 or else (K = E_Component
11864 and then not In_Protected_Function (E))
11865 or else K = E_Out_Parameter
11866 or else K = E_In_Out_Parameter
11867 or else K = E_Generic_In_Out_Parameter
11868
11869 -- Current instance of type. If this is a protected type, check
11870 -- we are not within the body of one of its protected functions.
11871
11872 or else (Is_Type (E)
11873 and then In_Open_Scopes (E)
11874 and then not In_Protected_Function (E))
11875
11876 or else (Is_Incomplete_Or_Private_Type (E)
11877 and then In_Open_Scopes (Full_View (E)));
11878 end;
11879
11880 else
11881 case Nkind (Orig_Node) is
11882 when N_Indexed_Component | N_Slice =>
11883 return Is_Variable_Prefix (Prefix (Orig_Node));
11884
11885 when N_Selected_Component =>
11886 return (Is_Variable (Selector_Name (Orig_Node))
11887 and then Is_Variable_Prefix (Prefix (Orig_Node)))
11888 or else
11889 (Nkind (N) = N_Expanded_Name
11890 and then Scope (Entity (N)) = Entity (Prefix (N)));
11891
11892 -- For an explicit dereference, the type of the prefix cannot
11893 -- be an access to constant or an access to subprogram.
11894
11895 when N_Explicit_Dereference =>
11896 declare
11897 Typ : constant Entity_Id := Etype (Prefix (Orig_Node));
11898 begin
11899 return Is_Access_Type (Typ)
11900 and then not Is_Access_Constant (Root_Type (Typ))
11901 and then Ekind (Typ) /= E_Access_Subprogram_Type;
11902 end;
11903
11904 -- The type conversion is the case where we do not deal with the
11905 -- context dependent special case of an actual parameter. Thus
11906 -- the type conversion is only considered a variable for the
11907 -- purposes of this routine if the target type is tagged. However,
11908 -- a type conversion is considered to be a variable if it does not
11909 -- come from source (this deals for example with the conversions
11910 -- of expressions to their actual subtypes).
11911
11912 when N_Type_Conversion =>
11913 return Is_Variable (Expression (Orig_Node))
11914 and then
11915 (not Comes_From_Source (Orig_Node)
11916 or else
11917 (Is_Tagged_Type (Etype (Subtype_Mark (Orig_Node)))
11918 and then
11919 Is_Tagged_Type (Etype (Expression (Orig_Node)))));
11920
11921 -- GNAT allows an unchecked type conversion as a variable. This
11922 -- only affects the generation of internal expanded code, since
11923 -- calls to instantiations of Unchecked_Conversion are never
11924 -- considered variables (since they are function calls).
11925
11926 when N_Unchecked_Type_Conversion =>
11927 return Is_Variable (Expression (Orig_Node));
11928
11929 when others =>
11930 return False;
11931 end case;
11932 end if;
11933 end Is_Variable;
11934
11935 ---------------------------
11936 -- Is_Visibly_Controlled --
11937 ---------------------------
11938
11939 function Is_Visibly_Controlled (T : Entity_Id) return Boolean is
11940 Root : constant Entity_Id := Root_Type (T);
11941 begin
11942 return Chars (Scope (Root)) = Name_Finalization
11943 and then Chars (Scope (Scope (Root))) = Name_Ada
11944 and then Scope (Scope (Scope (Root))) = Standard_Standard;
11945 end Is_Visibly_Controlled;
11946
11947 ------------------------
11948 -- Is_Volatile_Object --
11949 ------------------------
11950
11951 function Is_Volatile_Object (N : Node_Id) return Boolean is
11952
11953 function Is_Volatile_Prefix (N : Node_Id) return Boolean;
11954 -- If prefix is an implicit dereference, examine designated type
11955
11956 function Object_Has_Volatile_Components (N : Node_Id) return Boolean;
11957 -- Determines if given object has volatile components
11958
11959 ------------------------
11960 -- Is_Volatile_Prefix --
11961 ------------------------
11962
11963 function Is_Volatile_Prefix (N : Node_Id) return Boolean is
11964 Typ : constant Entity_Id := Etype (N);
11965
11966 begin
11967 if Is_Access_Type (Typ) then
11968 declare
11969 Dtyp : constant Entity_Id := Designated_Type (Typ);
11970
11971 begin
11972 return Is_Volatile (Dtyp)
11973 or else Has_Volatile_Components (Dtyp);
11974 end;
11975
11976 else
11977 return Object_Has_Volatile_Components (N);
11978 end if;
11979 end Is_Volatile_Prefix;
11980
11981 ------------------------------------
11982 -- Object_Has_Volatile_Components --
11983 ------------------------------------
11984
11985 function Object_Has_Volatile_Components (N : Node_Id) return Boolean is
11986 Typ : constant Entity_Id := Etype (N);
11987
11988 begin
11989 if Is_Volatile (Typ)
11990 or else Has_Volatile_Components (Typ)
11991 then
11992 return True;
11993
11994 elsif Is_Entity_Name (N)
11995 and then (Has_Volatile_Components (Entity (N))
11996 or else Is_Volatile (Entity (N)))
11997 then
11998 return True;
11999
12000 elsif Nkind (N) = N_Indexed_Component
12001 or else Nkind (N) = N_Selected_Component
12002 then
12003 return Is_Volatile_Prefix (Prefix (N));
12004
12005 else
12006 return False;
12007 end if;
12008 end Object_Has_Volatile_Components;
12009
12010 -- Start of processing for Is_Volatile_Object
12011
12012 begin
12013 if Nkind (N) = N_Defining_Identifier then
12014 return Is_Volatile (N) or else Is_Volatile (Etype (N));
12015
12016 elsif Nkind (N) = N_Expanded_Name then
12017 return Is_Volatile_Object (Entity (N));
12018
12019 elsif Is_Volatile (Etype (N))
12020 or else (Is_Entity_Name (N) and then Is_Volatile (Entity (N)))
12021 then
12022 return True;
12023
12024 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component)
12025 and then Is_Volatile_Prefix (Prefix (N))
12026 then
12027 return True;
12028
12029 elsif Nkind (N) = N_Selected_Component
12030 and then Is_Volatile (Entity (Selector_Name (N)))
12031 then
12032 return True;
12033
12034 else
12035 return False;
12036 end if;
12037 end Is_Volatile_Object;
12038
12039 ---------------------------
12040 -- Itype_Has_Declaration --
12041 ---------------------------
12042
12043 function Itype_Has_Declaration (Id : Entity_Id) return Boolean is
12044 begin
12045 pragma Assert (Is_Itype (Id));
12046 return Present (Parent (Id))
12047 and then Nkind_In (Parent (Id), N_Full_Type_Declaration,
12048 N_Subtype_Declaration)
12049 and then Defining_Entity (Parent (Id)) = Id;
12050 end Itype_Has_Declaration;
12051
12052 -------------------------
12053 -- Kill_Current_Values --
12054 -------------------------
12055
12056 procedure Kill_Current_Values
12057 (Ent : Entity_Id;
12058 Last_Assignment_Only : Boolean := False)
12059 is
12060 begin
12061 if Is_Assignable (Ent) then
12062 Set_Last_Assignment (Ent, Empty);
12063 end if;
12064
12065 if Is_Object (Ent) then
12066 if not Last_Assignment_Only then
12067 Kill_Checks (Ent);
12068 Set_Current_Value (Ent, Empty);
12069
12070 if not Can_Never_Be_Null (Ent) then
12071 Set_Is_Known_Non_Null (Ent, False);
12072 end if;
12073
12074 Set_Is_Known_Null (Ent, False);
12075
12076 -- Reset Is_Known_Valid unless type is always valid, or if we have
12077 -- a loop parameter (loop parameters are always valid, since their
12078 -- bounds are defined by the bounds given in the loop header).
12079
12080 if not Is_Known_Valid (Etype (Ent))
12081 and then Ekind (Ent) /= E_Loop_Parameter
12082 then
12083 Set_Is_Known_Valid (Ent, False);
12084 end if;
12085 end if;
12086 end if;
12087 end Kill_Current_Values;
12088
12089 procedure Kill_Current_Values (Last_Assignment_Only : Boolean := False) is
12090 S : Entity_Id;
12091
12092 procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id);
12093 -- Clear current value for entity E and all entities chained to E
12094
12095 ------------------------------------------
12096 -- Kill_Current_Values_For_Entity_Chain --
12097 ------------------------------------------
12098
12099 procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id) is
12100 Ent : Entity_Id;
12101 begin
12102 Ent := E;
12103 while Present (Ent) loop
12104 Kill_Current_Values (Ent, Last_Assignment_Only);
12105 Next_Entity (Ent);
12106 end loop;
12107 end Kill_Current_Values_For_Entity_Chain;
12108
12109 -- Start of processing for Kill_Current_Values
12110
12111 begin
12112 -- Kill all saved checks, a special case of killing saved values
12113
12114 if not Last_Assignment_Only then
12115 Kill_All_Checks;
12116 end if;
12117
12118 -- Loop through relevant scopes, which includes the current scope and
12119 -- any parent scopes if the current scope is a block or a package.
12120
12121 S := Current_Scope;
12122 Scope_Loop : loop
12123
12124 -- Clear current values of all entities in current scope
12125
12126 Kill_Current_Values_For_Entity_Chain (First_Entity (S));
12127
12128 -- If scope is a package, also clear current values of all private
12129 -- entities in the scope.
12130
12131 if Is_Package_Or_Generic_Package (S)
12132 or else Is_Concurrent_Type (S)
12133 then
12134 Kill_Current_Values_For_Entity_Chain (First_Private_Entity (S));
12135 end if;
12136
12137 -- If this is a not a subprogram, deal with parents
12138
12139 if not Is_Subprogram (S) then
12140 S := Scope (S);
12141 exit Scope_Loop when S = Standard_Standard;
12142 else
12143 exit Scope_Loop;
12144 end if;
12145 end loop Scope_Loop;
12146 end Kill_Current_Values;
12147
12148 --------------------------
12149 -- Kill_Size_Check_Code --
12150 --------------------------
12151
12152 procedure Kill_Size_Check_Code (E : Entity_Id) is
12153 begin
12154 if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
12155 and then Present (Size_Check_Code (E))
12156 then
12157 Remove (Size_Check_Code (E));
12158 Set_Size_Check_Code (E, Empty);
12159 end if;
12160 end Kill_Size_Check_Code;
12161
12162 --------------------------
12163 -- Known_To_Be_Assigned --
12164 --------------------------
12165
12166 function Known_To_Be_Assigned (N : Node_Id) return Boolean is
12167 P : constant Node_Id := Parent (N);
12168
12169 begin
12170 case Nkind (P) is
12171
12172 -- Test left side of assignment
12173
12174 when N_Assignment_Statement =>
12175 return N = Name (P);
12176
12177 -- Function call arguments are never lvalues
12178
12179 when N_Function_Call =>
12180 return False;
12181
12182 -- Positional parameter for procedure or accept call
12183
12184 when N_Procedure_Call_Statement |
12185 N_Accept_Statement
12186 =>
12187 declare
12188 Proc : Entity_Id;
12189 Form : Entity_Id;
12190 Act : Node_Id;
12191
12192 begin
12193 Proc := Get_Subprogram_Entity (P);
12194
12195 if No (Proc) then
12196 return False;
12197 end if;
12198
12199 -- If we are not a list member, something is strange, so
12200 -- be conservative and return False.
12201
12202 if not Is_List_Member (N) then
12203 return False;
12204 end if;
12205
12206 -- We are going to find the right formal by stepping forward
12207 -- through the formals, as we step backwards in the actuals.
12208
12209 Form := First_Formal (Proc);
12210 Act := N;
12211 loop
12212 -- If no formal, something is weird, so be conservative
12213 -- and return False.
12214
12215 if No (Form) then
12216 return False;
12217 end if;
12218
12219 Prev (Act);
12220 exit when No (Act);
12221 Next_Formal (Form);
12222 end loop;
12223
12224 return Ekind (Form) /= E_In_Parameter;
12225 end;
12226
12227 -- Named parameter for procedure or accept call
12228
12229 when N_Parameter_Association =>
12230 declare
12231 Proc : Entity_Id;
12232 Form : Entity_Id;
12233
12234 begin
12235 Proc := Get_Subprogram_Entity (Parent (P));
12236
12237 if No (Proc) then
12238 return False;
12239 end if;
12240
12241 -- Loop through formals to find the one that matches
12242
12243 Form := First_Formal (Proc);
12244 loop
12245 -- If no matching formal, that's peculiar, some kind of
12246 -- previous error, so return False to be conservative.
12247 -- Actually this also happens in legal code in the case
12248 -- where P is a parameter association for an Extra_Formal???
12249
12250 if No (Form) then
12251 return False;
12252 end if;
12253
12254 -- Else test for match
12255
12256 if Chars (Form) = Chars (Selector_Name (P)) then
12257 return Ekind (Form) /= E_In_Parameter;
12258 end if;
12259
12260 Next_Formal (Form);
12261 end loop;
12262 end;
12263
12264 -- Test for appearing in a conversion that itself appears
12265 -- in an lvalue context, since this should be an lvalue.
12266
12267 when N_Type_Conversion =>
12268 return Known_To_Be_Assigned (P);
12269
12270 -- All other references are definitely not known to be modifications
12271
12272 when others =>
12273 return False;
12274
12275 end case;
12276 end Known_To_Be_Assigned;
12277
12278 ---------------------------
12279 -- Last_Source_Statement --
12280 ---------------------------
12281
12282 function Last_Source_Statement (HSS : Node_Id) return Node_Id is
12283 N : Node_Id;
12284
12285 begin
12286 N := Last (Statements (HSS));
12287 while Present (N) loop
12288 exit when Comes_From_Source (N);
12289 Prev (N);
12290 end loop;
12291
12292 return N;
12293 end Last_Source_Statement;
12294
12295 ----------------------------------
12296 -- Matching_Static_Array_Bounds --
12297 ----------------------------------
12298
12299 function Matching_Static_Array_Bounds
12300 (L_Typ : Node_Id;
12301 R_Typ : Node_Id) return Boolean
12302 is
12303 L_Ndims : constant Nat := Number_Dimensions (L_Typ);
12304 R_Ndims : constant Nat := Number_Dimensions (R_Typ);
12305
12306 L_Index : Node_Id;
12307 R_Index : Node_Id;
12308 L_Low : Node_Id;
12309 L_High : Node_Id;
12310 L_Len : Uint;
12311 R_Low : Node_Id;
12312 R_High : Node_Id;
12313 R_Len : Uint;
12314
12315 begin
12316 if L_Ndims /= R_Ndims then
12317 return False;
12318 end if;
12319
12320 -- Unconstrained types do not have static bounds
12321
12322 if not Is_Constrained (L_Typ) or else not Is_Constrained (R_Typ) then
12323 return False;
12324 end if;
12325
12326 -- First treat specially the first dimension, as the lower bound and
12327 -- length of string literals are not stored like those of arrays.
12328
12329 if Ekind (L_Typ) = E_String_Literal_Subtype then
12330 L_Low := String_Literal_Low_Bound (L_Typ);
12331 L_Len := String_Literal_Length (L_Typ);
12332 else
12333 L_Index := First_Index (L_Typ);
12334 Get_Index_Bounds (L_Index, L_Low, L_High);
12335
12336 if Is_OK_Static_Expression (L_Low)
12337 and then Is_OK_Static_Expression (L_High)
12338 then
12339 if Expr_Value (L_High) < Expr_Value (L_Low) then
12340 L_Len := Uint_0;
12341 else
12342 L_Len := (Expr_Value (L_High) - Expr_Value (L_Low)) + 1;
12343 end if;
12344 else
12345 return False;
12346 end if;
12347 end if;
12348
12349 if Ekind (R_Typ) = E_String_Literal_Subtype then
12350 R_Low := String_Literal_Low_Bound (R_Typ);
12351 R_Len := String_Literal_Length (R_Typ);
12352 else
12353 R_Index := First_Index (R_Typ);
12354 Get_Index_Bounds (R_Index, R_Low, R_High);
12355
12356 if Is_OK_Static_Expression (R_Low)
12357 and then Is_OK_Static_Expression (R_High)
12358 then
12359 if Expr_Value (R_High) < Expr_Value (R_Low) then
12360 R_Len := Uint_0;
12361 else
12362 R_Len := (Expr_Value (R_High) - Expr_Value (R_Low)) + 1;
12363 end if;
12364 else
12365 return False;
12366 end if;
12367 end if;
12368
12369 if Is_OK_Static_Expression (L_Low)
12370 and then Is_OK_Static_Expression (R_Low)
12371 and then Expr_Value (L_Low) = Expr_Value (R_Low)
12372 and then L_Len = R_Len
12373 then
12374 null;
12375 else
12376 return False;
12377 end if;
12378
12379 -- Then treat all other dimensions
12380
12381 for Indx in 2 .. L_Ndims loop
12382 Next (L_Index);
12383 Next (R_Index);
12384
12385 Get_Index_Bounds (L_Index, L_Low, L_High);
12386 Get_Index_Bounds (R_Index, R_Low, R_High);
12387
12388 if Is_OK_Static_Expression (L_Low)
12389 and then Is_OK_Static_Expression (L_High)
12390 and then Is_OK_Static_Expression (R_Low)
12391 and then Is_OK_Static_Expression (R_High)
12392 and then Expr_Value (L_Low) = Expr_Value (R_Low)
12393 and then Expr_Value (L_High) = Expr_Value (R_High)
12394 then
12395 null;
12396 else
12397 return False;
12398 end if;
12399 end loop;
12400
12401 -- If we fall through the loop, all indexes matched
12402
12403 return True;
12404 end Matching_Static_Array_Bounds;
12405
12406 -------------------
12407 -- May_Be_Lvalue --
12408 -------------------
12409
12410 function May_Be_Lvalue (N : Node_Id) return Boolean is
12411 P : constant Node_Id := Parent (N);
12412
12413 begin
12414 case Nkind (P) is
12415
12416 -- Test left side of assignment
12417
12418 when N_Assignment_Statement =>
12419 return N = Name (P);
12420
12421 -- Test prefix of component or attribute. Note that the prefix of an
12422 -- explicit or implicit dereference cannot be an l-value.
12423
12424 when N_Attribute_Reference =>
12425 return N = Prefix (P)
12426 and then Name_Implies_Lvalue_Prefix (Attribute_Name (P));
12427
12428 -- For an expanded name, the name is an lvalue if the expanded name
12429 -- is an lvalue, but the prefix is never an lvalue, since it is just
12430 -- the scope where the name is found.
12431
12432 when N_Expanded_Name =>
12433 if N = Prefix (P) then
12434 return May_Be_Lvalue (P);
12435 else
12436 return False;
12437 end if;
12438
12439 -- For a selected component A.B, A is certainly an lvalue if A.B is.
12440 -- B is a little interesting, if we have A.B := 3, there is some
12441 -- discussion as to whether B is an lvalue or not, we choose to say
12442 -- it is. Note however that A is not an lvalue if it is of an access
12443 -- type since this is an implicit dereference.
12444
12445 when N_Selected_Component =>
12446 if N = Prefix (P)
12447 and then Present (Etype (N))
12448 and then Is_Access_Type (Etype (N))
12449 then
12450 return False;
12451 else
12452 return May_Be_Lvalue (P);
12453 end if;
12454
12455 -- For an indexed component or slice, the index or slice bounds is
12456 -- never an lvalue. The prefix is an lvalue if the indexed component
12457 -- or slice is an lvalue, except if it is an access type, where we
12458 -- have an implicit dereference.
12459
12460 when N_Indexed_Component | N_Slice =>
12461 if N /= Prefix (P)
12462 or else (Present (Etype (N)) and then Is_Access_Type (Etype (N)))
12463 then
12464 return False;
12465 else
12466 return May_Be_Lvalue (P);
12467 end if;
12468
12469 -- Prefix of a reference is an lvalue if the reference is an lvalue
12470
12471 when N_Reference =>
12472 return May_Be_Lvalue (P);
12473
12474 -- Prefix of explicit dereference is never an lvalue
12475
12476 when N_Explicit_Dereference =>
12477 return False;
12478
12479 -- Positional parameter for subprogram, entry, or accept call.
12480 -- In older versions of Ada function call arguments are never
12481 -- lvalues. In Ada 2012 functions can have in-out parameters.
12482
12483 when N_Subprogram_Call |
12484 N_Entry_Call_Statement |
12485 N_Accept_Statement
12486 =>
12487 if Nkind (P) = N_Function_Call and then Ada_Version < Ada_2012 then
12488 return False;
12489 end if;
12490
12491 -- The following mechanism is clumsy and fragile. A single flag
12492 -- set in Resolve_Actuals would be preferable ???
12493
12494 declare
12495 Proc : Entity_Id;
12496 Form : Entity_Id;
12497 Act : Node_Id;
12498
12499 begin
12500 Proc := Get_Subprogram_Entity (P);
12501
12502 if No (Proc) then
12503 return True;
12504 end if;
12505
12506 -- If we are not a list member, something is strange, so be
12507 -- conservative and return True.
12508
12509 if not Is_List_Member (N) then
12510 return True;
12511 end if;
12512
12513 -- We are going to find the right formal by stepping forward
12514 -- through the formals, as we step backwards in the actuals.
12515
12516 Form := First_Formal (Proc);
12517 Act := N;
12518 loop
12519 -- If no formal, something is weird, so be conservative and
12520 -- return True.
12521
12522 if No (Form) then
12523 return True;
12524 end if;
12525
12526 Prev (Act);
12527 exit when No (Act);
12528 Next_Formal (Form);
12529 end loop;
12530
12531 return Ekind (Form) /= E_In_Parameter;
12532 end;
12533
12534 -- Named parameter for procedure or accept call
12535
12536 when N_Parameter_Association =>
12537 declare
12538 Proc : Entity_Id;
12539 Form : Entity_Id;
12540
12541 begin
12542 Proc := Get_Subprogram_Entity (Parent (P));
12543
12544 if No (Proc) then
12545 return True;
12546 end if;
12547
12548 -- Loop through formals to find the one that matches
12549
12550 Form := First_Formal (Proc);
12551 loop
12552 -- If no matching formal, that's peculiar, some kind of
12553 -- previous error, so return True to be conservative.
12554 -- Actually happens with legal code for an unresolved call
12555 -- where we may get the wrong homonym???
12556
12557 if No (Form) then
12558 return True;
12559 end if;
12560
12561 -- Else test for match
12562
12563 if Chars (Form) = Chars (Selector_Name (P)) then
12564 return Ekind (Form) /= E_In_Parameter;
12565 end if;
12566
12567 Next_Formal (Form);
12568 end loop;
12569 end;
12570
12571 -- Test for appearing in a conversion that itself appears in an
12572 -- lvalue context, since this should be an lvalue.
12573
12574 when N_Type_Conversion =>
12575 return May_Be_Lvalue (P);
12576
12577 -- Test for appearance in object renaming declaration
12578
12579 when N_Object_Renaming_Declaration =>
12580 return True;
12581
12582 -- All other references are definitely not lvalues
12583
12584 when others =>
12585 return False;
12586
12587 end case;
12588 end May_Be_Lvalue;
12589
12590 -----------------------
12591 -- Mark_Coextensions --
12592 -----------------------
12593
12594 procedure Mark_Coextensions (Context_Nod : Node_Id; Root_Nod : Node_Id) is
12595 Is_Dynamic : Boolean;
12596 -- Indicates whether the context causes nested coextensions to be
12597 -- dynamic or static
12598
12599 function Mark_Allocator (N : Node_Id) return Traverse_Result;
12600 -- Recognize an allocator node and label it as a dynamic coextension
12601
12602 --------------------
12603 -- Mark_Allocator --
12604 --------------------
12605
12606 function Mark_Allocator (N : Node_Id) return Traverse_Result is
12607 begin
12608 if Nkind (N) = N_Allocator then
12609 if Is_Dynamic then
12610 Set_Is_Dynamic_Coextension (N);
12611
12612 -- If the allocator expression is potentially dynamic, it may
12613 -- be expanded out of order and require dynamic allocation
12614 -- anyway, so we treat the coextension itself as dynamic.
12615 -- Potential optimization ???
12616
12617 elsif Nkind (Expression (N)) = N_Qualified_Expression
12618 and then Nkind (Expression (Expression (N))) = N_Op_Concat
12619 then
12620 Set_Is_Dynamic_Coextension (N);
12621 else
12622 Set_Is_Static_Coextension (N);
12623 end if;
12624 end if;
12625
12626 return OK;
12627 end Mark_Allocator;
12628
12629 procedure Mark_Allocators is new Traverse_Proc (Mark_Allocator);
12630
12631 -- Start of processing Mark_Coextensions
12632
12633 begin
12634 case Nkind (Context_Nod) is
12635
12636 -- Comment here ???
12637
12638 when N_Assignment_Statement =>
12639 Is_Dynamic := Nkind (Expression (Context_Nod)) = N_Allocator;
12640
12641 -- An allocator that is a component of a returned aggregate
12642 -- must be dynamic.
12643
12644 when N_Simple_Return_Statement =>
12645 declare
12646 Expr : constant Node_Id := Expression (Context_Nod);
12647 begin
12648 Is_Dynamic :=
12649 Nkind (Expr) = N_Allocator
12650 or else
12651 (Nkind (Expr) = N_Qualified_Expression
12652 and then Nkind (Expression (Expr)) = N_Aggregate);
12653 end;
12654
12655 -- An alloctor within an object declaration in an extended return
12656 -- statement is of necessity dynamic.
12657
12658 when N_Object_Declaration =>
12659 Is_Dynamic := Nkind (Root_Nod) = N_Allocator
12660 or else
12661 Nkind (Parent (Context_Nod)) = N_Extended_Return_Statement;
12662
12663 -- This routine should not be called for constructs which may not
12664 -- contain coextensions.
12665
12666 when others =>
12667 raise Program_Error;
12668 end case;
12669
12670 Mark_Allocators (Root_Nod);
12671 end Mark_Coextensions;
12672
12673 -----------------
12674 -- Must_Inline --
12675 -----------------
12676
12677 function Must_Inline (Subp : Entity_Id) return Boolean is
12678 begin
12679 return
12680 (Optimization_Level = 0
12681
12682 -- AAMP and VM targets have no support for inlining in the backend.
12683 -- Hence we do as much inlining as possible in the front end.
12684
12685 or else AAMP_On_Target
12686 or else VM_Target /= No_VM)
12687 and then Has_Pragma_Inline (Subp)
12688 and then (Has_Pragma_Inline_Always (Subp) or else Front_End_Inlining);
12689 end Must_Inline;
12690
12691 ----------------------
12692 -- Needs_One_Actual --
12693 ----------------------
12694
12695 function Needs_One_Actual (E : Entity_Id) return Boolean is
12696 Formal : Entity_Id;
12697
12698 begin
12699 -- Ada 2005 or later, and formals present
12700
12701 if Ada_Version >= Ada_2005 and then Present (First_Formal (E)) then
12702 Formal := Next_Formal (First_Formal (E));
12703 while Present (Formal) loop
12704 if No (Default_Value (Formal)) then
12705 return False;
12706 end if;
12707
12708 Next_Formal (Formal);
12709 end loop;
12710
12711 return True;
12712
12713 -- Ada 83/95 or no formals
12714
12715 else
12716 return False;
12717 end if;
12718 end Needs_One_Actual;
12719
12720 ------------------------
12721 -- New_Copy_List_Tree --
12722 ------------------------
12723
12724 function New_Copy_List_Tree (List : List_Id) return List_Id is
12725 NL : List_Id;
12726 E : Node_Id;
12727
12728 begin
12729 if List = No_List then
12730 return No_List;
12731
12732 else
12733 NL := New_List;
12734 E := First (List);
12735
12736 while Present (E) loop
12737 Append (New_Copy_Tree (E), NL);
12738 E := Next (E);
12739 end loop;
12740
12741 return NL;
12742 end if;
12743 end New_Copy_List_Tree;
12744
12745 -------------------
12746 -- New_Copy_Tree --
12747 -------------------
12748
12749 use Atree.Unchecked_Access;
12750 use Atree_Private_Part;
12751
12752 -- Our approach here requires a two pass traversal of the tree. The
12753 -- first pass visits all nodes that eventually will be copied looking
12754 -- for defining Itypes. If any defining Itypes are found, then they are
12755 -- copied, and an entry is added to the replacement map. In the second
12756 -- phase, the tree is copied, using the replacement map to replace any
12757 -- Itype references within the copied tree.
12758
12759 -- The following hash tables are used if the Map supplied has more
12760 -- than hash threshold entries to speed up access to the map. If
12761 -- there are fewer entries, then the map is searched sequentially
12762 -- (because setting up a hash table for only a few entries takes
12763 -- more time than it saves.
12764
12765 function New_Copy_Hash (E : Entity_Id) return NCT_Header_Num;
12766 -- Hash function used for hash operations
12767
12768 -------------------
12769 -- New_Copy_Hash --
12770 -------------------
12771
12772 function New_Copy_Hash (E : Entity_Id) return NCT_Header_Num is
12773 begin
12774 return Nat (E) mod (NCT_Header_Num'Last + 1);
12775 end New_Copy_Hash;
12776
12777 ---------------
12778 -- NCT_Assoc --
12779 ---------------
12780
12781 -- The hash table NCT_Assoc associates old entities in the table
12782 -- with their corresponding new entities (i.e. the pairs of entries
12783 -- presented in the original Map argument are Key-Element pairs).
12784
12785 package NCT_Assoc is new Simple_HTable (
12786 Header_Num => NCT_Header_Num,
12787 Element => Entity_Id,
12788 No_Element => Empty,
12789 Key => Entity_Id,
12790 Hash => New_Copy_Hash,
12791 Equal => Types."=");
12792
12793 ---------------------
12794 -- NCT_Itype_Assoc --
12795 ---------------------
12796
12797 -- The hash table NCT_Itype_Assoc contains entries only for those
12798 -- old nodes which have a non-empty Associated_Node_For_Itype set.
12799 -- The key is the associated node, and the element is the new node
12800 -- itself (NOT the associated node for the new node).
12801
12802 package NCT_Itype_Assoc is new Simple_HTable (
12803 Header_Num => NCT_Header_Num,
12804 Element => Entity_Id,
12805 No_Element => Empty,
12806 Key => Entity_Id,
12807 Hash => New_Copy_Hash,
12808 Equal => Types."=");
12809
12810 -- Start of processing for New_Copy_Tree function
12811
12812 function New_Copy_Tree
12813 (Source : Node_Id;
12814 Map : Elist_Id := No_Elist;
12815 New_Sloc : Source_Ptr := No_Location;
12816 New_Scope : Entity_Id := Empty) return Node_Id
12817 is
12818 Actual_Map : Elist_Id := Map;
12819 -- This is the actual map for the copy. It is initialized with the
12820 -- given elements, and then enlarged as required for Itypes that are
12821 -- copied during the first phase of the copy operation. The visit
12822 -- procedures add elements to this map as Itypes are encountered.
12823 -- The reason we cannot use Map directly, is that it may well be
12824 -- (and normally is) initialized to No_Elist, and if we have mapped
12825 -- entities, we have to reset it to point to a real Elist.
12826
12827 function Assoc (N : Node_Or_Entity_Id) return Node_Id;
12828 -- Called during second phase to map entities into their corresponding
12829 -- copies using Actual_Map. If the argument is not an entity, or is not
12830 -- in Actual_Map, then it is returned unchanged.
12831
12832 procedure Build_NCT_Hash_Tables;
12833 -- Builds hash tables (number of elements >= threshold value)
12834
12835 function Copy_Elist_With_Replacement
12836 (Old_Elist : Elist_Id) return Elist_Id;
12837 -- Called during second phase to copy element list doing replacements
12838
12839 procedure Copy_Itype_With_Replacement (New_Itype : Entity_Id);
12840 -- Called during the second phase to process a copied Itype. The actual
12841 -- copy happened during the first phase (so that we could make the entry
12842 -- in the mapping), but we still have to deal with the descendents of
12843 -- the copied Itype and copy them where necessary.
12844
12845 function Copy_List_With_Replacement (Old_List : List_Id) return List_Id;
12846 -- Called during second phase to copy list doing replacements
12847
12848 function Copy_Node_With_Replacement (Old_Node : Node_Id) return Node_Id;
12849 -- Called during second phase to copy node doing replacements
12850
12851 procedure Visit_Elist (E : Elist_Id);
12852 -- Called during first phase to visit all elements of an Elist
12853
12854 procedure Visit_Field (F : Union_Id; N : Node_Id);
12855 -- Visit a single field, recursing to call Visit_Node or Visit_List
12856 -- if the field is a syntactic descendent of the current node (i.e.
12857 -- its parent is Node N).
12858
12859 procedure Visit_Itype (Old_Itype : Entity_Id);
12860 -- Called during first phase to visit subsidiary fields of a defining
12861 -- Itype, and also create a copy and make an entry in the replacement
12862 -- map for the new copy.
12863
12864 procedure Visit_List (L : List_Id);
12865 -- Called during first phase to visit all elements of a List
12866
12867 procedure Visit_Node (N : Node_Or_Entity_Id);
12868 -- Called during first phase to visit a node and all its subtrees
12869
12870 -----------
12871 -- Assoc --
12872 -----------
12873
12874 function Assoc (N : Node_Or_Entity_Id) return Node_Id is
12875 E : Elmt_Id;
12876 Ent : Entity_Id;
12877
12878 begin
12879 if not Has_Extension (N) or else No (Actual_Map) then
12880 return N;
12881
12882 elsif NCT_Hash_Tables_Used then
12883 Ent := NCT_Assoc.Get (Entity_Id (N));
12884
12885 if Present (Ent) then
12886 return Ent;
12887 else
12888 return N;
12889 end if;
12890
12891 -- No hash table used, do serial search
12892
12893 else
12894 E := First_Elmt (Actual_Map);
12895 while Present (E) loop
12896 if Node (E) = N then
12897 return Node (Next_Elmt (E));
12898 else
12899 E := Next_Elmt (Next_Elmt (E));
12900 end if;
12901 end loop;
12902 end if;
12903
12904 return N;
12905 end Assoc;
12906
12907 ---------------------------
12908 -- Build_NCT_Hash_Tables --
12909 ---------------------------
12910
12911 procedure Build_NCT_Hash_Tables is
12912 Elmt : Elmt_Id;
12913 Ent : Entity_Id;
12914 begin
12915 if NCT_Hash_Table_Setup then
12916 NCT_Assoc.Reset;
12917 NCT_Itype_Assoc.Reset;
12918 end if;
12919
12920 Elmt := First_Elmt (Actual_Map);
12921 while Present (Elmt) loop
12922 Ent := Node (Elmt);
12923
12924 -- Get new entity, and associate old and new
12925
12926 Next_Elmt (Elmt);
12927 NCT_Assoc.Set (Ent, Node (Elmt));
12928
12929 if Is_Type (Ent) then
12930 declare
12931 Anode : constant Entity_Id :=
12932 Associated_Node_For_Itype (Ent);
12933
12934 begin
12935 if Present (Anode) then
12936
12937 -- Enter a link between the associated node of the
12938 -- old Itype and the new Itype, for updating later
12939 -- when node is copied.
12940
12941 NCT_Itype_Assoc.Set (Anode, Node (Elmt));
12942 end if;
12943 end;
12944 end if;
12945
12946 Next_Elmt (Elmt);
12947 end loop;
12948
12949 NCT_Hash_Tables_Used := True;
12950 NCT_Hash_Table_Setup := True;
12951 end Build_NCT_Hash_Tables;
12952
12953 ---------------------------------
12954 -- Copy_Elist_With_Replacement --
12955 ---------------------------------
12956
12957 function Copy_Elist_With_Replacement
12958 (Old_Elist : Elist_Id) return Elist_Id
12959 is
12960 M : Elmt_Id;
12961 New_Elist : Elist_Id;
12962
12963 begin
12964 if No (Old_Elist) then
12965 return No_Elist;
12966
12967 else
12968 New_Elist := New_Elmt_List;
12969
12970 M := First_Elmt (Old_Elist);
12971 while Present (M) loop
12972 Append_Elmt (Copy_Node_With_Replacement (Node (M)), New_Elist);
12973 Next_Elmt (M);
12974 end loop;
12975 end if;
12976
12977 return New_Elist;
12978 end Copy_Elist_With_Replacement;
12979
12980 ---------------------------------
12981 -- Copy_Itype_With_Replacement --
12982 ---------------------------------
12983
12984 -- This routine exactly parallels its phase one analog Visit_Itype,
12985
12986 procedure Copy_Itype_With_Replacement (New_Itype : Entity_Id) is
12987 begin
12988 -- Translate Next_Entity, Scope and Etype fields, in case they
12989 -- reference entities that have been mapped into copies.
12990
12991 Set_Next_Entity (New_Itype, Assoc (Next_Entity (New_Itype)));
12992 Set_Etype (New_Itype, Assoc (Etype (New_Itype)));
12993
12994 if Present (New_Scope) then
12995 Set_Scope (New_Itype, New_Scope);
12996 else
12997 Set_Scope (New_Itype, Assoc (Scope (New_Itype)));
12998 end if;
12999
13000 -- Copy referenced fields
13001
13002 if Is_Discrete_Type (New_Itype) then
13003 Set_Scalar_Range (New_Itype,
13004 Copy_Node_With_Replacement (Scalar_Range (New_Itype)));
13005
13006 elsif Has_Discriminants (Base_Type (New_Itype)) then
13007 Set_Discriminant_Constraint (New_Itype,
13008 Copy_Elist_With_Replacement
13009 (Discriminant_Constraint (New_Itype)));
13010
13011 elsif Is_Array_Type (New_Itype) then
13012 if Present (First_Index (New_Itype)) then
13013 Set_First_Index (New_Itype,
13014 First (Copy_List_With_Replacement
13015 (List_Containing (First_Index (New_Itype)))));
13016 end if;
13017
13018 if Is_Packed (New_Itype) then
13019 Set_Packed_Array_Type (New_Itype,
13020 Copy_Node_With_Replacement
13021 (Packed_Array_Type (New_Itype)));
13022 end if;
13023 end if;
13024 end Copy_Itype_With_Replacement;
13025
13026 --------------------------------
13027 -- Copy_List_With_Replacement --
13028 --------------------------------
13029
13030 function Copy_List_With_Replacement
13031 (Old_List : List_Id) return List_Id
13032 is
13033 New_List : List_Id;
13034 E : Node_Id;
13035
13036 begin
13037 if Old_List = No_List then
13038 return No_List;
13039
13040 else
13041 New_List := Empty_List;
13042
13043 E := First (Old_List);
13044 while Present (E) loop
13045 Append (Copy_Node_With_Replacement (E), New_List);
13046 Next (E);
13047 end loop;
13048
13049 return New_List;
13050 end if;
13051 end Copy_List_With_Replacement;
13052
13053 --------------------------------
13054 -- Copy_Node_With_Replacement --
13055 --------------------------------
13056
13057 function Copy_Node_With_Replacement
13058 (Old_Node : Node_Id) return Node_Id
13059 is
13060 New_Node : Node_Id;
13061
13062 procedure Adjust_Named_Associations
13063 (Old_Node : Node_Id;
13064 New_Node : Node_Id);
13065 -- If a call node has named associations, these are chained through
13066 -- the First_Named_Actual, Next_Named_Actual links. These must be
13067 -- propagated separately to the new parameter list, because these
13068 -- are not syntactic fields.
13069
13070 function Copy_Field_With_Replacement
13071 (Field : Union_Id) return Union_Id;
13072 -- Given Field, which is a field of Old_Node, return a copy of it
13073 -- if it is a syntactic field (i.e. its parent is Node), setting
13074 -- the parent of the copy to poit to New_Node. Otherwise returns
13075 -- the field (possibly mapped if it is an entity).
13076
13077 -------------------------------
13078 -- Adjust_Named_Associations --
13079 -------------------------------
13080
13081 procedure Adjust_Named_Associations
13082 (Old_Node : Node_Id;
13083 New_Node : Node_Id)
13084 is
13085 Old_E : Node_Id;
13086 New_E : Node_Id;
13087
13088 Old_Next : Node_Id;
13089 New_Next : Node_Id;
13090
13091 begin
13092 Old_E := First (Parameter_Associations (Old_Node));
13093 New_E := First (Parameter_Associations (New_Node));
13094 while Present (Old_E) loop
13095 if Nkind (Old_E) = N_Parameter_Association
13096 and then Present (Next_Named_Actual (Old_E))
13097 then
13098 if First_Named_Actual (Old_Node)
13099 = Explicit_Actual_Parameter (Old_E)
13100 then
13101 Set_First_Named_Actual
13102 (New_Node, Explicit_Actual_Parameter (New_E));
13103 end if;
13104
13105 -- Now scan parameter list from the beginning,to locate
13106 -- next named actual, which can be out of order.
13107
13108 Old_Next := First (Parameter_Associations (Old_Node));
13109 New_Next := First (Parameter_Associations (New_Node));
13110
13111 while Nkind (Old_Next) /= N_Parameter_Association
13112 or else Explicit_Actual_Parameter (Old_Next)
13113 /= Next_Named_Actual (Old_E)
13114 loop
13115 Next (Old_Next);
13116 Next (New_Next);
13117 end loop;
13118
13119 Set_Next_Named_Actual
13120 (New_E, Explicit_Actual_Parameter (New_Next));
13121 end if;
13122
13123 Next (Old_E);
13124 Next (New_E);
13125 end loop;
13126 end Adjust_Named_Associations;
13127
13128 ---------------------------------
13129 -- Copy_Field_With_Replacement --
13130 ---------------------------------
13131
13132 function Copy_Field_With_Replacement
13133 (Field : Union_Id) return Union_Id
13134 is
13135 begin
13136 if Field = Union_Id (Empty) then
13137 return Field;
13138
13139 elsif Field in Node_Range then
13140 declare
13141 Old_N : constant Node_Id := Node_Id (Field);
13142 New_N : Node_Id;
13143
13144 begin
13145 -- If syntactic field, as indicated by the parent pointer
13146 -- being set, then copy the referenced node recursively.
13147
13148 if Parent (Old_N) = Old_Node then
13149 New_N := Copy_Node_With_Replacement (Old_N);
13150
13151 if New_N /= Old_N then
13152 Set_Parent (New_N, New_Node);
13153 end if;
13154
13155 -- For semantic fields, update possible entity reference
13156 -- from the replacement map.
13157
13158 else
13159 New_N := Assoc (Old_N);
13160 end if;
13161
13162 return Union_Id (New_N);
13163 end;
13164
13165 elsif Field in List_Range then
13166 declare
13167 Old_L : constant List_Id := List_Id (Field);
13168 New_L : List_Id;
13169
13170 begin
13171 -- If syntactic field, as indicated by the parent pointer,
13172 -- then recursively copy the entire referenced list.
13173
13174 if Parent (Old_L) = Old_Node then
13175 New_L := Copy_List_With_Replacement (Old_L);
13176 Set_Parent (New_L, New_Node);
13177
13178 -- For semantic list, just returned unchanged
13179
13180 else
13181 New_L := Old_L;
13182 end if;
13183
13184 return Union_Id (New_L);
13185 end;
13186
13187 -- Anything other than a list or a node is returned unchanged
13188
13189 else
13190 return Field;
13191 end if;
13192 end Copy_Field_With_Replacement;
13193
13194 -- Start of processing for Copy_Node_With_Replacement
13195
13196 begin
13197 if Old_Node <= Empty_Or_Error then
13198 return Old_Node;
13199
13200 elsif Has_Extension (Old_Node) then
13201 return Assoc (Old_Node);
13202
13203 else
13204 New_Node := New_Copy (Old_Node);
13205
13206 -- If the node we are copying is the associated node of a
13207 -- previously copied Itype, then adjust the associated node
13208 -- of the copy of that Itype accordingly.
13209
13210 if Present (Actual_Map) then
13211 declare
13212 E : Elmt_Id;
13213 Ent : Entity_Id;
13214
13215 begin
13216 -- Case of hash table used
13217
13218 if NCT_Hash_Tables_Used then
13219 Ent := NCT_Itype_Assoc.Get (Old_Node);
13220
13221 if Present (Ent) then
13222 Set_Associated_Node_For_Itype (Ent, New_Node);
13223 end if;
13224
13225 -- Case of no hash table used
13226
13227 else
13228 E := First_Elmt (Actual_Map);
13229 while Present (E) loop
13230 if Is_Itype (Node (E))
13231 and then
13232 Old_Node = Associated_Node_For_Itype (Node (E))
13233 then
13234 Set_Associated_Node_For_Itype
13235 (Node (Next_Elmt (E)), New_Node);
13236 end if;
13237
13238 E := Next_Elmt (Next_Elmt (E));
13239 end loop;
13240 end if;
13241 end;
13242 end if;
13243
13244 -- Recursively copy descendents
13245
13246 Set_Field1
13247 (New_Node, Copy_Field_With_Replacement (Field1 (New_Node)));
13248 Set_Field2
13249 (New_Node, Copy_Field_With_Replacement (Field2 (New_Node)));
13250 Set_Field3
13251 (New_Node, Copy_Field_With_Replacement (Field3 (New_Node)));
13252 Set_Field4
13253 (New_Node, Copy_Field_With_Replacement (Field4 (New_Node)));
13254 Set_Field5
13255 (New_Node, Copy_Field_With_Replacement (Field5 (New_Node)));
13256
13257 -- Adjust Sloc of new node if necessary
13258
13259 if New_Sloc /= No_Location then
13260 Set_Sloc (New_Node, New_Sloc);
13261
13262 -- If we adjust the Sloc, then we are essentially making
13263 -- a completely new node, so the Comes_From_Source flag
13264 -- should be reset to the proper default value.
13265
13266 Nodes.Table (New_Node).Comes_From_Source :=
13267 Default_Node.Comes_From_Source;
13268 end if;
13269
13270 -- If the node is call and has named associations,
13271 -- set the corresponding links in the copy.
13272
13273 if (Nkind (Old_Node) = N_Function_Call
13274 or else Nkind (Old_Node) = N_Entry_Call_Statement
13275 or else
13276 Nkind (Old_Node) = N_Procedure_Call_Statement)
13277 and then Present (First_Named_Actual (Old_Node))
13278 then
13279 Adjust_Named_Associations (Old_Node, New_Node);
13280 end if;
13281
13282 -- Reset First_Real_Statement for Handled_Sequence_Of_Statements.
13283 -- The replacement mechanism applies to entities, and is not used
13284 -- here. Eventually we may need a more general graph-copying
13285 -- routine. For now, do a sequential search to find desired node.
13286
13287 if Nkind (Old_Node) = N_Handled_Sequence_Of_Statements
13288 and then Present (First_Real_Statement (Old_Node))
13289 then
13290 declare
13291 Old_F : constant Node_Id := First_Real_Statement (Old_Node);
13292 N1, N2 : Node_Id;
13293
13294 begin
13295 N1 := First (Statements (Old_Node));
13296 N2 := First (Statements (New_Node));
13297
13298 while N1 /= Old_F loop
13299 Next (N1);
13300 Next (N2);
13301 end loop;
13302
13303 Set_First_Real_Statement (New_Node, N2);
13304 end;
13305 end if;
13306 end if;
13307
13308 -- All done, return copied node
13309
13310 return New_Node;
13311 end Copy_Node_With_Replacement;
13312
13313 -----------------
13314 -- Visit_Elist --
13315 -----------------
13316
13317 procedure Visit_Elist (E : Elist_Id) is
13318 Elmt : Elmt_Id;
13319 begin
13320 if Present (E) then
13321 Elmt := First_Elmt (E);
13322
13323 while Elmt /= No_Elmt loop
13324 Visit_Node (Node (Elmt));
13325 Next_Elmt (Elmt);
13326 end loop;
13327 end if;
13328 end Visit_Elist;
13329
13330 -----------------
13331 -- Visit_Field --
13332 -----------------
13333
13334 procedure Visit_Field (F : Union_Id; N : Node_Id) is
13335 begin
13336 if F = Union_Id (Empty) then
13337 return;
13338
13339 elsif F in Node_Range then
13340
13341 -- Copy node if it is syntactic, i.e. its parent pointer is
13342 -- set to point to the field that referenced it (certain
13343 -- Itypes will also meet this criterion, which is fine, since
13344 -- these are clearly Itypes that do need to be copied, since
13345 -- we are copying their parent.)
13346
13347 if Parent (Node_Id (F)) = N then
13348 Visit_Node (Node_Id (F));
13349 return;
13350
13351 -- Another case, if we are pointing to an Itype, then we want
13352 -- to copy it if its associated node is somewhere in the tree
13353 -- being copied.
13354
13355 -- Note: the exclusion of self-referential copies is just an
13356 -- optimization, since the search of the already copied list
13357 -- would catch it, but it is a common case (Etype pointing
13358 -- to itself for an Itype that is a base type).
13359
13360 elsif Has_Extension (Node_Id (F))
13361 and then Is_Itype (Entity_Id (F))
13362 and then Node_Id (F) /= N
13363 then
13364 declare
13365 P : Node_Id;
13366
13367 begin
13368 P := Associated_Node_For_Itype (Node_Id (F));
13369 while Present (P) loop
13370 if P = Source then
13371 Visit_Node (Node_Id (F));
13372 return;
13373 else
13374 P := Parent (P);
13375 end if;
13376 end loop;
13377
13378 -- An Itype whose parent is not being copied definitely
13379 -- should NOT be copied, since it does not belong in any
13380 -- sense to the copied subtree.
13381
13382 return;
13383 end;
13384 end if;
13385
13386 elsif F in List_Range
13387 and then Parent (List_Id (F)) = N
13388 then
13389 Visit_List (List_Id (F));
13390 return;
13391 end if;
13392 end Visit_Field;
13393
13394 -----------------
13395 -- Visit_Itype --
13396 -----------------
13397
13398 procedure Visit_Itype (Old_Itype : Entity_Id) is
13399 New_Itype : Entity_Id;
13400 E : Elmt_Id;
13401 Ent : Entity_Id;
13402
13403 begin
13404 -- Itypes that describe the designated type of access to subprograms
13405 -- have the structure of subprogram declarations, with signatures,
13406 -- etc. Either we duplicate the signatures completely, or choose to
13407 -- share such itypes, which is fine because their elaboration will
13408 -- have no side effects.
13409
13410 if Ekind (Old_Itype) = E_Subprogram_Type then
13411 return;
13412 end if;
13413
13414 New_Itype := New_Copy (Old_Itype);
13415
13416 -- The new Itype has all the attributes of the old one, and
13417 -- we just copy the contents of the entity. However, the back-end
13418 -- needs different names for debugging purposes, so we create a
13419 -- new internal name for it in all cases.
13420
13421 Set_Chars (New_Itype, New_Internal_Name ('T'));
13422
13423 -- If our associated node is an entity that has already been copied,
13424 -- then set the associated node of the copy to point to the right
13425 -- copy. If we have copied an Itype that is itself the associated
13426 -- node of some previously copied Itype, then we set the right
13427 -- pointer in the other direction.
13428
13429 if Present (Actual_Map) then
13430
13431 -- Case of hash tables used
13432
13433 if NCT_Hash_Tables_Used then
13434
13435 Ent := NCT_Assoc.Get (Associated_Node_For_Itype (Old_Itype));
13436
13437 if Present (Ent) then
13438 Set_Associated_Node_For_Itype (New_Itype, Ent);
13439 end if;
13440
13441 Ent := NCT_Itype_Assoc.Get (Old_Itype);
13442 if Present (Ent) then
13443 Set_Associated_Node_For_Itype (Ent, New_Itype);
13444
13445 -- If the hash table has no association for this Itype and
13446 -- its associated node, enter one now.
13447
13448 else
13449 NCT_Itype_Assoc.Set
13450 (Associated_Node_For_Itype (Old_Itype), New_Itype);
13451 end if;
13452
13453 -- Case of hash tables not used
13454
13455 else
13456 E := First_Elmt (Actual_Map);
13457 while Present (E) loop
13458 if Associated_Node_For_Itype (Old_Itype) = Node (E) then
13459 Set_Associated_Node_For_Itype
13460 (New_Itype, Node (Next_Elmt (E)));
13461 end if;
13462
13463 if Is_Type (Node (E))
13464 and then
13465 Old_Itype = Associated_Node_For_Itype (Node (E))
13466 then
13467 Set_Associated_Node_For_Itype
13468 (Node (Next_Elmt (E)), New_Itype);
13469 end if;
13470
13471 E := Next_Elmt (Next_Elmt (E));
13472 end loop;
13473 end if;
13474 end if;
13475
13476 if Present (Freeze_Node (New_Itype)) then
13477 Set_Is_Frozen (New_Itype, False);
13478 Set_Freeze_Node (New_Itype, Empty);
13479 end if;
13480
13481 -- Add new association to map
13482
13483 if No (Actual_Map) then
13484 Actual_Map := New_Elmt_List;
13485 end if;
13486
13487 Append_Elmt (Old_Itype, Actual_Map);
13488 Append_Elmt (New_Itype, Actual_Map);
13489
13490 if NCT_Hash_Tables_Used then
13491 NCT_Assoc.Set (Old_Itype, New_Itype);
13492
13493 else
13494 NCT_Table_Entries := NCT_Table_Entries + 1;
13495
13496 if NCT_Table_Entries > NCT_Hash_Threshold then
13497 Build_NCT_Hash_Tables;
13498 end if;
13499 end if;
13500
13501 -- If a record subtype is simply copied, the entity list will be
13502 -- shared. Thus cloned_Subtype must be set to indicate the sharing.
13503
13504 if Ekind_In (Old_Itype, E_Record_Subtype, E_Class_Wide_Subtype) then
13505 Set_Cloned_Subtype (New_Itype, Old_Itype);
13506 end if;
13507
13508 -- Visit descendents that eventually get copied
13509
13510 Visit_Field (Union_Id (Etype (Old_Itype)), Old_Itype);
13511
13512 if Is_Discrete_Type (Old_Itype) then
13513 Visit_Field (Union_Id (Scalar_Range (Old_Itype)), Old_Itype);
13514
13515 elsif Has_Discriminants (Base_Type (Old_Itype)) then
13516 -- ??? This should involve call to Visit_Field
13517 Visit_Elist (Discriminant_Constraint (Old_Itype));
13518
13519 elsif Is_Array_Type (Old_Itype) then
13520 if Present (First_Index (Old_Itype)) then
13521 Visit_Field (Union_Id (List_Containing
13522 (First_Index (Old_Itype))),
13523 Old_Itype);
13524 end if;
13525
13526 if Is_Packed (Old_Itype) then
13527 Visit_Field (Union_Id (Packed_Array_Type (Old_Itype)),
13528 Old_Itype);
13529 end if;
13530 end if;
13531 end Visit_Itype;
13532
13533 ----------------
13534 -- Visit_List --
13535 ----------------
13536
13537 procedure Visit_List (L : List_Id) is
13538 N : Node_Id;
13539 begin
13540 if L /= No_List then
13541 N := First (L);
13542
13543 while Present (N) loop
13544 Visit_Node (N);
13545 Next (N);
13546 end loop;
13547 end if;
13548 end Visit_List;
13549
13550 ----------------
13551 -- Visit_Node --
13552 ----------------
13553
13554 procedure Visit_Node (N : Node_Or_Entity_Id) is
13555
13556 -- Start of processing for Visit_Node
13557
13558 begin
13559 -- Handle case of an Itype, which must be copied
13560
13561 if Has_Extension (N)
13562 and then Is_Itype (N)
13563 then
13564 -- Nothing to do if already in the list. This can happen with an
13565 -- Itype entity that appears more than once in the tree.
13566 -- Note that we do not want to visit descendents in this case.
13567
13568 -- Test for already in list when hash table is used
13569
13570 if NCT_Hash_Tables_Used then
13571 if Present (NCT_Assoc.Get (Entity_Id (N))) then
13572 return;
13573 end if;
13574
13575 -- Test for already in list when hash table not used
13576
13577 else
13578 declare
13579 E : Elmt_Id;
13580 begin
13581 if Present (Actual_Map) then
13582 E := First_Elmt (Actual_Map);
13583 while Present (E) loop
13584 if Node (E) = N then
13585 return;
13586 else
13587 E := Next_Elmt (Next_Elmt (E));
13588 end if;
13589 end loop;
13590 end if;
13591 end;
13592 end if;
13593
13594 Visit_Itype (N);
13595 end if;
13596
13597 -- Visit descendents
13598
13599 Visit_Field (Field1 (N), N);
13600 Visit_Field (Field2 (N), N);
13601 Visit_Field (Field3 (N), N);
13602 Visit_Field (Field4 (N), N);
13603 Visit_Field (Field5 (N), N);
13604 end Visit_Node;
13605
13606 -- Start of processing for New_Copy_Tree
13607
13608 begin
13609 Actual_Map := Map;
13610
13611 -- See if we should use hash table
13612
13613 if No (Actual_Map) then
13614 NCT_Hash_Tables_Used := False;
13615
13616 else
13617 declare
13618 Elmt : Elmt_Id;
13619
13620 begin
13621 NCT_Table_Entries := 0;
13622
13623 Elmt := First_Elmt (Actual_Map);
13624 while Present (Elmt) loop
13625 NCT_Table_Entries := NCT_Table_Entries + 1;
13626 Next_Elmt (Elmt);
13627 Next_Elmt (Elmt);
13628 end loop;
13629
13630 if NCT_Table_Entries > NCT_Hash_Threshold then
13631 Build_NCT_Hash_Tables;
13632 else
13633 NCT_Hash_Tables_Used := False;
13634 end if;
13635 end;
13636 end if;
13637
13638 -- Hash table set up if required, now start phase one by visiting
13639 -- top node (we will recursively visit the descendents).
13640
13641 Visit_Node (Source);
13642
13643 -- Now the second phase of the copy can start. First we process
13644 -- all the mapped entities, copying their descendents.
13645
13646 if Present (Actual_Map) then
13647 declare
13648 Elmt : Elmt_Id;
13649 New_Itype : Entity_Id;
13650 begin
13651 Elmt := First_Elmt (Actual_Map);
13652 while Present (Elmt) loop
13653 Next_Elmt (Elmt);
13654 New_Itype := Node (Elmt);
13655 Copy_Itype_With_Replacement (New_Itype);
13656 Next_Elmt (Elmt);
13657 end loop;
13658 end;
13659 end if;
13660
13661 -- Now we can copy the actual tree
13662
13663 return Copy_Node_With_Replacement (Source);
13664 end New_Copy_Tree;
13665
13666 -------------------------
13667 -- New_External_Entity --
13668 -------------------------
13669
13670 function New_External_Entity
13671 (Kind : Entity_Kind;
13672 Scope_Id : Entity_Id;
13673 Sloc_Value : Source_Ptr;
13674 Related_Id : Entity_Id;
13675 Suffix : Character;
13676 Suffix_Index : Nat := 0;
13677 Prefix : Character := ' ') return Entity_Id
13678 is
13679 N : constant Entity_Id :=
13680 Make_Defining_Identifier (Sloc_Value,
13681 New_External_Name
13682 (Chars (Related_Id), Suffix, Suffix_Index, Prefix));
13683
13684 begin
13685 Set_Ekind (N, Kind);
13686 Set_Is_Internal (N, True);
13687 Append_Entity (N, Scope_Id);
13688 Set_Public_Status (N);
13689
13690 if Kind in Type_Kind then
13691 Init_Size_Align (N);
13692 end if;
13693
13694 return N;
13695 end New_External_Entity;
13696
13697 -------------------------
13698 -- New_Internal_Entity --
13699 -------------------------
13700
13701 function New_Internal_Entity
13702 (Kind : Entity_Kind;
13703 Scope_Id : Entity_Id;
13704 Sloc_Value : Source_Ptr;
13705 Id_Char : Character) return Entity_Id
13706 is
13707 N : constant Entity_Id := Make_Temporary (Sloc_Value, Id_Char);
13708
13709 begin
13710 Set_Ekind (N, Kind);
13711 Set_Is_Internal (N, True);
13712 Append_Entity (N, Scope_Id);
13713
13714 if Kind in Type_Kind then
13715 Init_Size_Align (N);
13716 end if;
13717
13718 return N;
13719 end New_Internal_Entity;
13720
13721 -----------------
13722 -- Next_Actual --
13723 -----------------
13724
13725 function Next_Actual (Actual_Id : Node_Id) return Node_Id is
13726 N : Node_Id;
13727
13728 begin
13729 -- If we are pointing at a positional parameter, it is a member of a
13730 -- node list (the list of parameters), and the next parameter is the
13731 -- next node on the list, unless we hit a parameter association, then
13732 -- we shift to using the chain whose head is the First_Named_Actual in
13733 -- the parent, and then is threaded using the Next_Named_Actual of the
13734 -- Parameter_Association. All this fiddling is because the original node
13735 -- list is in the textual call order, and what we need is the
13736 -- declaration order.
13737
13738 if Is_List_Member (Actual_Id) then
13739 N := Next (Actual_Id);
13740
13741 if Nkind (N) = N_Parameter_Association then
13742 return First_Named_Actual (Parent (Actual_Id));
13743 else
13744 return N;
13745 end if;
13746
13747 else
13748 return Next_Named_Actual (Parent (Actual_Id));
13749 end if;
13750 end Next_Actual;
13751
13752 procedure Next_Actual (Actual_Id : in out Node_Id) is
13753 begin
13754 Actual_Id := Next_Actual (Actual_Id);
13755 end Next_Actual;
13756
13757 ---------------------
13758 -- No_Scalar_Parts --
13759 ---------------------
13760
13761 function No_Scalar_Parts (T : Entity_Id) return Boolean is
13762 C : Entity_Id;
13763
13764 begin
13765 if Is_Scalar_Type (T) then
13766 return False;
13767
13768 elsif Is_Array_Type (T) then
13769 return No_Scalar_Parts (Component_Type (T));
13770
13771 elsif Is_Record_Type (T) or else Has_Discriminants (T) then
13772 C := First_Component_Or_Discriminant (T);
13773 while Present (C) loop
13774 if not No_Scalar_Parts (Etype (C)) then
13775 return False;
13776 else
13777 Next_Component_Or_Discriminant (C);
13778 end if;
13779 end loop;
13780 end if;
13781
13782 return True;
13783 end No_Scalar_Parts;
13784
13785 -----------------------
13786 -- Normalize_Actuals --
13787 -----------------------
13788
13789 -- Chain actuals according to formals of subprogram. If there are no named
13790 -- associations, the chain is simply the list of Parameter Associations,
13791 -- since the order is the same as the declaration order. If there are named
13792 -- associations, then the First_Named_Actual field in the N_Function_Call
13793 -- or N_Procedure_Call_Statement node points to the Parameter_Association
13794 -- node for the parameter that comes first in declaration order. The
13795 -- remaining named parameters are then chained in declaration order using
13796 -- Next_Named_Actual.
13797
13798 -- This routine also verifies that the number of actuals is compatible with
13799 -- the number and default values of formals, but performs no type checking
13800 -- (type checking is done by the caller).
13801
13802 -- If the matching succeeds, Success is set to True and the caller proceeds
13803 -- with type-checking. If the match is unsuccessful, then Success is set to
13804 -- False, and the caller attempts a different interpretation, if there is
13805 -- one.
13806
13807 -- If the flag Report is on, the call is not overloaded, and a failure to
13808 -- match can be reported here, rather than in the caller.
13809
13810 procedure Normalize_Actuals
13811 (N : Node_Id;
13812 S : Entity_Id;
13813 Report : Boolean;
13814 Success : out Boolean)
13815 is
13816 Actuals : constant List_Id := Parameter_Associations (N);
13817 Actual : Node_Id := Empty;
13818 Formal : Entity_Id;
13819 Last : Node_Id := Empty;
13820 First_Named : Node_Id := Empty;
13821 Found : Boolean;
13822
13823 Formals_To_Match : Integer := 0;
13824 Actuals_To_Match : Integer := 0;
13825
13826 procedure Chain (A : Node_Id);
13827 -- Add named actual at the proper place in the list, using the
13828 -- Next_Named_Actual link.
13829
13830 function Reporting return Boolean;
13831 -- Determines if an error is to be reported. To report an error, we
13832 -- need Report to be True, and also we do not report errors caused
13833 -- by calls to init procs that occur within other init procs. Such
13834 -- errors must always be cascaded errors, since if all the types are
13835 -- declared correctly, the compiler will certainly build decent calls.
13836
13837 -----------
13838 -- Chain --
13839 -----------
13840
13841 procedure Chain (A : Node_Id) is
13842 begin
13843 if No (Last) then
13844
13845 -- Call node points to first actual in list
13846
13847 Set_First_Named_Actual (N, Explicit_Actual_Parameter (A));
13848
13849 else
13850 Set_Next_Named_Actual (Last, Explicit_Actual_Parameter (A));
13851 end if;
13852
13853 Last := A;
13854 Set_Next_Named_Actual (Last, Empty);
13855 end Chain;
13856
13857 ---------------
13858 -- Reporting --
13859 ---------------
13860
13861 function Reporting return Boolean is
13862 begin
13863 if not Report then
13864 return False;
13865
13866 elsif not Within_Init_Proc then
13867 return True;
13868
13869 elsif Is_Init_Proc (Entity (Name (N))) then
13870 return False;
13871
13872 else
13873 return True;
13874 end if;
13875 end Reporting;
13876
13877 -- Start of processing for Normalize_Actuals
13878
13879 begin
13880 if Is_Access_Type (S) then
13881
13882 -- The name in the call is a function call that returns an access
13883 -- to subprogram. The designated type has the list of formals.
13884
13885 Formal := First_Formal (Designated_Type (S));
13886 else
13887 Formal := First_Formal (S);
13888 end if;
13889
13890 while Present (Formal) loop
13891 Formals_To_Match := Formals_To_Match + 1;
13892 Next_Formal (Formal);
13893 end loop;
13894
13895 -- Find if there is a named association, and verify that no positional
13896 -- associations appear after named ones.
13897
13898 if Present (Actuals) then
13899 Actual := First (Actuals);
13900 end if;
13901
13902 while Present (Actual)
13903 and then Nkind (Actual) /= N_Parameter_Association
13904 loop
13905 Actuals_To_Match := Actuals_To_Match + 1;
13906 Next (Actual);
13907 end loop;
13908
13909 if No (Actual) and Actuals_To_Match = Formals_To_Match then
13910
13911 -- Most common case: positional notation, no defaults
13912
13913 Success := True;
13914 return;
13915
13916 elsif Actuals_To_Match > Formals_To_Match then
13917
13918 -- Too many actuals: will not work
13919
13920 if Reporting then
13921 if Is_Entity_Name (Name (N)) then
13922 Error_Msg_N ("too many arguments in call to&", Name (N));
13923 else
13924 Error_Msg_N ("too many arguments in call", N);
13925 end if;
13926 end if;
13927
13928 Success := False;
13929 return;
13930 end if;
13931
13932 First_Named := Actual;
13933
13934 while Present (Actual) loop
13935 if Nkind (Actual) /= N_Parameter_Association then
13936 Error_Msg_N
13937 ("positional parameters not allowed after named ones", Actual);
13938 Success := False;
13939 return;
13940
13941 else
13942 Actuals_To_Match := Actuals_To_Match + 1;
13943 end if;
13944
13945 Next (Actual);
13946 end loop;
13947
13948 if Present (Actuals) then
13949 Actual := First (Actuals);
13950 end if;
13951
13952 Formal := First_Formal (S);
13953 while Present (Formal) loop
13954
13955 -- Match the formals in order. If the corresponding actual is
13956 -- positional, nothing to do. Else scan the list of named actuals
13957 -- to find the one with the right name.
13958
13959 if Present (Actual)
13960 and then Nkind (Actual) /= N_Parameter_Association
13961 then
13962 Next (Actual);
13963 Actuals_To_Match := Actuals_To_Match - 1;
13964 Formals_To_Match := Formals_To_Match - 1;
13965
13966 else
13967 -- For named parameters, search the list of actuals to find
13968 -- one that matches the next formal name.
13969
13970 Actual := First_Named;
13971 Found := False;
13972 while Present (Actual) loop
13973 if Chars (Selector_Name (Actual)) = Chars (Formal) then
13974 Found := True;
13975 Chain (Actual);
13976 Actuals_To_Match := Actuals_To_Match - 1;
13977 Formals_To_Match := Formals_To_Match - 1;
13978 exit;
13979 end if;
13980
13981 Next (Actual);
13982 end loop;
13983
13984 if not Found then
13985 if Ekind (Formal) /= E_In_Parameter
13986 or else No (Default_Value (Formal))
13987 then
13988 if Reporting then
13989 if (Comes_From_Source (S)
13990 or else Sloc (S) = Standard_Location)
13991 and then Is_Overloadable (S)
13992 then
13993 if No (Actuals)
13994 and then
13995 (Nkind (Parent (N)) = N_Procedure_Call_Statement
13996 or else
13997 (Nkind (Parent (N)) = N_Function_Call
13998 or else
13999 Nkind (Parent (N)) = N_Parameter_Association))
14000 and then Ekind (S) /= E_Function
14001 then
14002 Set_Etype (N, Etype (S));
14003 else
14004 Error_Msg_Name_1 := Chars (S);
14005 Error_Msg_Sloc := Sloc (S);
14006 Error_Msg_NE
14007 ("missing argument for parameter & " &
14008 "in call to % declared #", N, Formal);
14009 end if;
14010
14011 elsif Is_Overloadable (S) then
14012 Error_Msg_Name_1 := Chars (S);
14013
14014 -- Point to type derivation that generated the
14015 -- operation.
14016
14017 Error_Msg_Sloc := Sloc (Parent (S));
14018
14019 Error_Msg_NE
14020 ("missing argument for parameter & " &
14021 "in call to % (inherited) #", N, Formal);
14022
14023 else
14024 Error_Msg_NE
14025 ("missing argument for parameter &", N, Formal);
14026 end if;
14027 end if;
14028
14029 Success := False;
14030 return;
14031
14032 else
14033 Formals_To_Match := Formals_To_Match - 1;
14034 end if;
14035 end if;
14036 end if;
14037
14038 Next_Formal (Formal);
14039 end loop;
14040
14041 if Formals_To_Match = 0 and then Actuals_To_Match = 0 then
14042 Success := True;
14043 return;
14044
14045 else
14046 if Reporting then
14047
14048 -- Find some superfluous named actual that did not get
14049 -- attached to the list of associations.
14050
14051 Actual := First (Actuals);
14052 while Present (Actual) loop
14053 if Nkind (Actual) = N_Parameter_Association
14054 and then Actual /= Last
14055 and then No (Next_Named_Actual (Actual))
14056 then
14057 Error_Msg_N ("unmatched actual & in call",
14058 Selector_Name (Actual));
14059 exit;
14060 end if;
14061
14062 Next (Actual);
14063 end loop;
14064 end if;
14065
14066 Success := False;
14067 return;
14068 end if;
14069 end Normalize_Actuals;
14070
14071 --------------------------------
14072 -- Note_Possible_Modification --
14073 --------------------------------
14074
14075 procedure Note_Possible_Modification (N : Node_Id; Sure : Boolean) is
14076 Modification_Comes_From_Source : constant Boolean :=
14077 Comes_From_Source (Parent (N));
14078
14079 Ent : Entity_Id;
14080 Exp : Node_Id;
14081
14082 begin
14083 -- Loop to find referenced entity, if there is one
14084
14085 Exp := N;
14086 loop
14087 Ent := Empty;
14088
14089 if Is_Entity_Name (Exp) then
14090 Ent := Entity (Exp);
14091
14092 -- If the entity is missing, it is an undeclared identifier,
14093 -- and there is nothing to annotate.
14094
14095 if No (Ent) then
14096 return;
14097 end if;
14098
14099 elsif Nkind (Exp) = N_Explicit_Dereference then
14100 declare
14101 P : constant Node_Id := Prefix (Exp);
14102
14103 begin
14104 -- In formal verification mode, keep track of all reads and
14105 -- writes through explicit dereferences.
14106
14107 if GNATprove_Mode then
14108 SPARK_Specific.Generate_Dereference (N, 'm');
14109 end if;
14110
14111 if Nkind (P) = N_Selected_Component
14112 and then Present (Entry_Formal (Entity (Selector_Name (P))))
14113 then
14114 -- Case of a reference to an entry formal
14115
14116 Ent := Entry_Formal (Entity (Selector_Name (P)));
14117
14118 elsif Nkind (P) = N_Identifier
14119 and then Nkind (Parent (Entity (P))) = N_Object_Declaration
14120 and then Present (Expression (Parent (Entity (P))))
14121 and then Nkind (Expression (Parent (Entity (P)))) =
14122 N_Reference
14123 then
14124 -- Case of a reference to a value on which side effects have
14125 -- been removed.
14126
14127 Exp := Prefix (Expression (Parent (Entity (P))));
14128 goto Continue;
14129
14130 else
14131 return;
14132 end if;
14133 end;
14134
14135 elsif Nkind_In (Exp, N_Type_Conversion,
14136 N_Unchecked_Type_Conversion)
14137 then
14138 Exp := Expression (Exp);
14139 goto Continue;
14140
14141 elsif Nkind_In (Exp, N_Slice,
14142 N_Indexed_Component,
14143 N_Selected_Component)
14144 then
14145 -- Special check, if the prefix is an access type, then return
14146 -- since we are modifying the thing pointed to, not the prefix.
14147 -- When we are expanding, most usually the prefix is replaced
14148 -- by an explicit dereference, and this test is not needed, but
14149 -- in some cases (notably -gnatc mode and generics) when we do
14150 -- not do full expansion, we need this special test.
14151
14152 if Is_Access_Type (Etype (Prefix (Exp))) then
14153 return;
14154
14155 -- Otherwise go to prefix and keep going
14156
14157 else
14158 Exp := Prefix (Exp);
14159 goto Continue;
14160 end if;
14161
14162 -- All other cases, not a modification
14163
14164 else
14165 return;
14166 end if;
14167
14168 -- Now look for entity being referenced
14169
14170 if Present (Ent) then
14171 if Is_Object (Ent) then
14172 if Comes_From_Source (Exp)
14173 or else Modification_Comes_From_Source
14174 then
14175 -- Give warning if pragma unmodified given and we are
14176 -- sure this is a modification.
14177
14178 if Has_Pragma_Unmodified (Ent) and then Sure then
14179 Error_Msg_NE
14180 ("??pragma Unmodified given for &!", N, Ent);
14181 end if;
14182
14183 Set_Never_Set_In_Source (Ent, False);
14184 end if;
14185
14186 Set_Is_True_Constant (Ent, False);
14187 Set_Current_Value (Ent, Empty);
14188 Set_Is_Known_Null (Ent, False);
14189
14190 if not Can_Never_Be_Null (Ent) then
14191 Set_Is_Known_Non_Null (Ent, False);
14192 end if;
14193
14194 -- Follow renaming chain
14195
14196 if (Ekind (Ent) = E_Variable or else Ekind (Ent) = E_Constant)
14197 and then Present (Renamed_Object (Ent))
14198 then
14199 Exp := Renamed_Object (Ent);
14200
14201 -- If the entity is the loop variable in an iteration over
14202 -- a container, retrieve container expression to indicate
14203 -- possible modificastion.
14204
14205 if Present (Related_Expression (Ent))
14206 and then Nkind (Parent (Related_Expression (Ent))) =
14207 N_Iterator_Specification
14208 then
14209 Exp := Original_Node (Related_Expression (Ent));
14210 end if;
14211
14212 goto Continue;
14213
14214 -- The expression may be the renaming of a subcomponent of an
14215 -- array or container. The assignment to the subcomponent is
14216 -- a modification of the container.
14217
14218 elsif Comes_From_Source (Original_Node (Exp))
14219 and then Nkind_In (Original_Node (Exp), N_Selected_Component,
14220 N_Indexed_Component)
14221 then
14222 Exp := Prefix (Original_Node (Exp));
14223 goto Continue;
14224 end if;
14225
14226 -- Generate a reference only if the assignment comes from
14227 -- source. This excludes, for example, calls to a dispatching
14228 -- assignment operation when the left-hand side is tagged. In
14229 -- GNATprove mode, we need those references also on generated
14230 -- code, as these are used to compute the local effects of
14231 -- subprograms.
14232
14233 if Modification_Comes_From_Source or GNATprove_Mode then
14234 Generate_Reference (Ent, Exp, 'm');
14235
14236 -- If the target of the assignment is the bound variable
14237 -- in an iterator, indicate that the corresponding array
14238 -- or container is also modified.
14239
14240 if Ada_Version >= Ada_2012
14241 and then
14242 Nkind (Parent (Ent)) = N_Iterator_Specification
14243 then
14244 declare
14245 Domain : constant Node_Id := Name (Parent (Ent));
14246
14247 begin
14248 -- TBD : in the full version of the construct, the
14249 -- domain of iteration can be given by an expression.
14250
14251 if Is_Entity_Name (Domain) then
14252 Generate_Reference (Entity (Domain), Exp, 'm');
14253 Set_Is_True_Constant (Entity (Domain), False);
14254 Set_Never_Set_In_Source (Entity (Domain), False);
14255 end if;
14256 end;
14257 end if;
14258 end if;
14259
14260 Check_Nested_Access (Ent);
14261 end if;
14262
14263 Kill_Checks (Ent);
14264
14265 -- If we are sure this is a modification from source, and we know
14266 -- this modifies a constant, then give an appropriate warning.
14267
14268 if Overlays_Constant (Ent)
14269 and then Modification_Comes_From_Source
14270 and then Sure
14271 then
14272 declare
14273 A : constant Node_Id := Address_Clause (Ent);
14274 begin
14275 if Present (A) then
14276 declare
14277 Exp : constant Node_Id := Expression (A);
14278 begin
14279 if Nkind (Exp) = N_Attribute_Reference
14280 and then Attribute_Name (Exp) = Name_Address
14281 and then Is_Entity_Name (Prefix (Exp))
14282 then
14283 Error_Msg_Sloc := Sloc (A);
14284 Error_Msg_NE
14285 ("constant& may be modified via address "
14286 & "clause#??", N, Entity (Prefix (Exp)));
14287 end if;
14288 end;
14289 end if;
14290 end;
14291 end if;
14292
14293 return;
14294 end if;
14295
14296 <<Continue>>
14297 null;
14298 end loop;
14299 end Note_Possible_Modification;
14300
14301 -------------------------
14302 -- Object_Access_Level --
14303 -------------------------
14304
14305 -- Returns the static accessibility level of the view denoted by Obj. Note
14306 -- that the value returned is the result of a call to Scope_Depth. Only
14307 -- scope depths associated with dynamic scopes can actually be returned.
14308 -- Since only relative levels matter for accessibility checking, the fact
14309 -- that the distance between successive levels of accessibility is not
14310 -- always one is immaterial (invariant: if level(E2) is deeper than
14311 -- level(E1), then Scope_Depth(E1) < Scope_Depth(E2)).
14312
14313 function Object_Access_Level (Obj : Node_Id) return Uint is
14314 function Is_Interface_Conversion (N : Node_Id) return Boolean;
14315 -- Determine whether N is a construct of the form
14316 -- Some_Type (Operand._tag'Address)
14317 -- This construct appears in the context of dispatching calls.
14318
14319 function Reference_To (Obj : Node_Id) return Node_Id;
14320 -- An explicit dereference is created when removing side-effects from
14321 -- expressions for constraint checking purposes. In this case a local
14322 -- access type is created for it. The correct access level is that of
14323 -- the original source node. We detect this case by noting that the
14324 -- prefix of the dereference is created by an object declaration whose
14325 -- initial expression is a reference.
14326
14327 -----------------------------
14328 -- Is_Interface_Conversion --
14329 -----------------------------
14330
14331 function Is_Interface_Conversion (N : Node_Id) return Boolean is
14332 begin
14333 return
14334 Nkind (N) = N_Unchecked_Type_Conversion
14335 and then Nkind (Expression (N)) = N_Attribute_Reference
14336 and then Attribute_Name (Expression (N)) = Name_Address;
14337 end Is_Interface_Conversion;
14338
14339 ------------------
14340 -- Reference_To --
14341 ------------------
14342
14343 function Reference_To (Obj : Node_Id) return Node_Id is
14344 Pref : constant Node_Id := Prefix (Obj);
14345 begin
14346 if Is_Entity_Name (Pref)
14347 and then Nkind (Parent (Entity (Pref))) = N_Object_Declaration
14348 and then Present (Expression (Parent (Entity (Pref))))
14349 and then Nkind (Expression (Parent (Entity (Pref)))) = N_Reference
14350 then
14351 return (Prefix (Expression (Parent (Entity (Pref)))));
14352 else
14353 return Empty;
14354 end if;
14355 end Reference_To;
14356
14357 -- Local variables
14358
14359 E : Entity_Id;
14360
14361 -- Start of processing for Object_Access_Level
14362
14363 begin
14364 if Nkind (Obj) = N_Defining_Identifier
14365 or else Is_Entity_Name (Obj)
14366 then
14367 if Nkind (Obj) = N_Defining_Identifier then
14368 E := Obj;
14369 else
14370 E := Entity (Obj);
14371 end if;
14372
14373 if Is_Prival (E) then
14374 E := Prival_Link (E);
14375 end if;
14376
14377 -- If E is a type then it denotes a current instance. For this case
14378 -- we add one to the normal accessibility level of the type to ensure
14379 -- that current instances are treated as always being deeper than
14380 -- than the level of any visible named access type (see 3.10.2(21)).
14381
14382 if Is_Type (E) then
14383 return Type_Access_Level (E) + 1;
14384
14385 elsif Present (Renamed_Object (E)) then
14386 return Object_Access_Level (Renamed_Object (E));
14387
14388 -- Similarly, if E is a component of the current instance of a
14389 -- protected type, any instance of it is assumed to be at a deeper
14390 -- level than the type. For a protected object (whose type is an
14391 -- anonymous protected type) its components are at the same level
14392 -- as the type itself.
14393
14394 elsif not Is_Overloadable (E)
14395 and then Ekind (Scope (E)) = E_Protected_Type
14396 and then Comes_From_Source (Scope (E))
14397 then
14398 return Type_Access_Level (Scope (E)) + 1;
14399
14400 else
14401 return Scope_Depth (Enclosing_Dynamic_Scope (E));
14402 end if;
14403
14404 elsif Nkind (Obj) = N_Selected_Component then
14405 if Is_Access_Type (Etype (Prefix (Obj))) then
14406 return Type_Access_Level (Etype (Prefix (Obj)));
14407 else
14408 return Object_Access_Level (Prefix (Obj));
14409 end if;
14410
14411 elsif Nkind (Obj) = N_Indexed_Component then
14412 if Is_Access_Type (Etype (Prefix (Obj))) then
14413 return Type_Access_Level (Etype (Prefix (Obj)));
14414 else
14415 return Object_Access_Level (Prefix (Obj));
14416 end if;
14417
14418 elsif Nkind (Obj) = N_Explicit_Dereference then
14419
14420 -- If the prefix is a selected access discriminant then we make a
14421 -- recursive call on the prefix, which will in turn check the level
14422 -- of the prefix object of the selected discriminant.
14423
14424 if Nkind (Prefix (Obj)) = N_Selected_Component
14425 and then Ekind (Etype (Prefix (Obj))) = E_Anonymous_Access_Type
14426 and then
14427 Ekind (Entity (Selector_Name (Prefix (Obj)))) = E_Discriminant
14428 then
14429 return Object_Access_Level (Prefix (Obj));
14430
14431 -- Detect an interface conversion in the context of a dispatching
14432 -- call. Use the original form of the conversion to find the access
14433 -- level of the operand.
14434
14435 elsif Is_Interface (Etype (Obj))
14436 and then Is_Interface_Conversion (Prefix (Obj))
14437 and then Nkind (Original_Node (Obj)) = N_Type_Conversion
14438 then
14439 return Object_Access_Level (Original_Node (Obj));
14440
14441 elsif not Comes_From_Source (Obj) then
14442 declare
14443 Ref : constant Node_Id := Reference_To (Obj);
14444 begin
14445 if Present (Ref) then
14446 return Object_Access_Level (Ref);
14447 else
14448 return Type_Access_Level (Etype (Prefix (Obj)));
14449 end if;
14450 end;
14451
14452 else
14453 return Type_Access_Level (Etype (Prefix (Obj)));
14454 end if;
14455
14456 elsif Nkind_In (Obj, N_Type_Conversion, N_Unchecked_Type_Conversion) then
14457 return Object_Access_Level (Expression (Obj));
14458
14459 elsif Nkind (Obj) = N_Function_Call then
14460
14461 -- Function results are objects, so we get either the access level of
14462 -- the function or, in the case of an indirect call, the level of the
14463 -- access-to-subprogram type. (This code is used for Ada 95, but it
14464 -- looks wrong, because it seems that we should be checking the level
14465 -- of the call itself, even for Ada 95. However, using the Ada 2005
14466 -- version of the code causes regressions in several tests that are
14467 -- compiled with -gnat95. ???)
14468
14469 if Ada_Version < Ada_2005 then
14470 if Is_Entity_Name (Name (Obj)) then
14471 return Subprogram_Access_Level (Entity (Name (Obj)));
14472 else
14473 return Type_Access_Level (Etype (Prefix (Name (Obj))));
14474 end if;
14475
14476 -- For Ada 2005, the level of the result object of a function call is
14477 -- defined to be the level of the call's innermost enclosing master.
14478 -- We determine that by querying the depth of the innermost enclosing
14479 -- dynamic scope.
14480
14481 else
14482 Return_Master_Scope_Depth_Of_Call : declare
14483
14484 function Innermost_Master_Scope_Depth
14485 (N : Node_Id) return Uint;
14486 -- Returns the scope depth of the given node's innermost
14487 -- enclosing dynamic scope (effectively the accessibility
14488 -- level of the innermost enclosing master).
14489
14490 ----------------------------------
14491 -- Innermost_Master_Scope_Depth --
14492 ----------------------------------
14493
14494 function Innermost_Master_Scope_Depth
14495 (N : Node_Id) return Uint
14496 is
14497 Node_Par : Node_Id := Parent (N);
14498
14499 begin
14500 -- Locate the nearest enclosing node (by traversing Parents)
14501 -- that Defining_Entity can be applied to, and return the
14502 -- depth of that entity's nearest enclosing dynamic scope.
14503
14504 while Present (Node_Par) loop
14505 case Nkind (Node_Par) is
14506 when N_Component_Declaration |
14507 N_Entry_Declaration |
14508 N_Formal_Object_Declaration |
14509 N_Formal_Type_Declaration |
14510 N_Full_Type_Declaration |
14511 N_Incomplete_Type_Declaration |
14512 N_Loop_Parameter_Specification |
14513 N_Object_Declaration |
14514 N_Protected_Type_Declaration |
14515 N_Private_Extension_Declaration |
14516 N_Private_Type_Declaration |
14517 N_Subtype_Declaration |
14518 N_Function_Specification |
14519 N_Procedure_Specification |
14520 N_Task_Type_Declaration |
14521 N_Body_Stub |
14522 N_Generic_Instantiation |
14523 N_Proper_Body |
14524 N_Implicit_Label_Declaration |
14525 N_Package_Declaration |
14526 N_Single_Task_Declaration |
14527 N_Subprogram_Declaration |
14528 N_Generic_Declaration |
14529 N_Renaming_Declaration |
14530 N_Block_Statement |
14531 N_Formal_Subprogram_Declaration |
14532 N_Abstract_Subprogram_Declaration |
14533 N_Entry_Body |
14534 N_Exception_Declaration |
14535 N_Formal_Package_Declaration |
14536 N_Number_Declaration |
14537 N_Package_Specification |
14538 N_Parameter_Specification |
14539 N_Single_Protected_Declaration |
14540 N_Subunit =>
14541
14542 return Scope_Depth
14543 (Nearest_Dynamic_Scope
14544 (Defining_Entity (Node_Par)));
14545
14546 when others =>
14547 null;
14548 end case;
14549
14550 Node_Par := Parent (Node_Par);
14551 end loop;
14552
14553 pragma Assert (False);
14554
14555 -- Should never reach the following return
14556
14557 return Scope_Depth (Current_Scope) + 1;
14558 end Innermost_Master_Scope_Depth;
14559
14560 -- Start of processing for Return_Master_Scope_Depth_Of_Call
14561
14562 begin
14563 return Innermost_Master_Scope_Depth (Obj);
14564 end Return_Master_Scope_Depth_Of_Call;
14565 end if;
14566
14567 -- For convenience we handle qualified expressions, even though they
14568 -- aren't technically object names.
14569
14570 elsif Nkind (Obj) = N_Qualified_Expression then
14571 return Object_Access_Level (Expression (Obj));
14572
14573 -- Otherwise return the scope level of Standard. (If there are cases
14574 -- that fall through to this point they will be treated as having
14575 -- global accessibility for now. ???)
14576
14577 else
14578 return Scope_Depth (Standard_Standard);
14579 end if;
14580 end Object_Access_Level;
14581
14582 --------------------------
14583 -- Original_Aspect_Name --
14584 --------------------------
14585
14586 function Original_Aspect_Name (N : Node_Id) return Name_Id is
14587 Pras : Node_Id;
14588 Name : Name_Id;
14589
14590 begin
14591 pragma Assert (Nkind_In (N, N_Aspect_Specification, N_Pragma));
14592 Pras := N;
14593
14594 if Is_Rewrite_Substitution (Pras)
14595 and then Nkind (Original_Node (Pras)) = N_Pragma
14596 then
14597 Pras := Original_Node (Pras);
14598 end if;
14599
14600 -- Case where we came from aspect specication
14601
14602 if Nkind (Pras) = N_Pragma and then From_Aspect_Specification (Pras) then
14603 Pras := Corresponding_Aspect (Pras);
14604 end if;
14605
14606 -- Get name from aspect or pragma
14607
14608 if Nkind (Pras) = N_Pragma then
14609 Name := Pragma_Name (Pras);
14610 else
14611 Name := Chars (Identifier (Pras));
14612 end if;
14613
14614 -- Deal with 'Class
14615
14616 if Class_Present (Pras) then
14617 case Name is
14618
14619 -- Names that need converting to special _xxx form
14620
14621 when Name_Pre |
14622 Name_Pre_Class =>
14623 Name := Name_uPre;
14624
14625 when Name_Post |
14626 Name_Post_Class =>
14627 Name := Name_uPost;
14628
14629 when Name_Invariant =>
14630 Name := Name_uInvariant;
14631
14632 when Name_Type_Invariant |
14633 Name_Type_Invariant_Class =>
14634 Name := Name_uType_Invariant;
14635
14636 -- Nothing to do for other cases (e.g. a Check that derived
14637 -- from Pre_Class and has the flag set). Also we do nothing
14638 -- if the name is already in special _xxx form.
14639
14640 when others =>
14641 null;
14642 end case;
14643 end if;
14644
14645 return Name;
14646 end Original_Aspect_Name;
14647 --------------------------------------
14648 -- Original_Corresponding_Operation --
14649 --------------------------------------
14650
14651 function Original_Corresponding_Operation (S : Entity_Id) return Entity_Id
14652 is
14653 Typ : constant Entity_Id := Find_Dispatching_Type (S);
14654
14655 begin
14656 -- If S is an inherited primitive S2 the original corresponding
14657 -- operation of S is the original corresponding operation of S2
14658
14659 if Present (Alias (S))
14660 and then Find_Dispatching_Type (Alias (S)) /= Typ
14661 then
14662 return Original_Corresponding_Operation (Alias (S));
14663
14664 -- If S overrides an inherited subprogram S2 the original corresponding
14665 -- operation of S is the original corresponding operation of S2
14666
14667 elsif Present (Overridden_Operation (S)) then
14668 return Original_Corresponding_Operation (Overridden_Operation (S));
14669
14670 -- otherwise it is S itself
14671
14672 else
14673 return S;
14674 end if;
14675 end Original_Corresponding_Operation;
14676
14677 -----------------------
14678 -- Private_Component --
14679 -----------------------
14680
14681 function Private_Component (Type_Id : Entity_Id) return Entity_Id is
14682 Ancestor : constant Entity_Id := Base_Type (Type_Id);
14683
14684 function Trace_Components
14685 (T : Entity_Id;
14686 Check : Boolean) return Entity_Id;
14687 -- Recursive function that does the work, and checks against circular
14688 -- definition for each subcomponent type.
14689
14690 ----------------------
14691 -- Trace_Components --
14692 ----------------------
14693
14694 function Trace_Components
14695 (T : Entity_Id;
14696 Check : Boolean) return Entity_Id
14697 is
14698 Btype : constant Entity_Id := Base_Type (T);
14699 Component : Entity_Id;
14700 P : Entity_Id;
14701 Candidate : Entity_Id := Empty;
14702
14703 begin
14704 if Check and then Btype = Ancestor then
14705 Error_Msg_N ("circular type definition", Type_Id);
14706 return Any_Type;
14707 end if;
14708
14709 if Is_Private_Type (Btype)
14710 and then not Is_Generic_Type (Btype)
14711 then
14712 if Present (Full_View (Btype))
14713 and then Is_Record_Type (Full_View (Btype))
14714 and then not Is_Frozen (Btype)
14715 then
14716 -- To indicate that the ancestor depends on a private type, the
14717 -- current Btype is sufficient. However, to check for circular
14718 -- definition we must recurse on the full view.
14719
14720 Candidate := Trace_Components (Full_View (Btype), True);
14721
14722 if Candidate = Any_Type then
14723 return Any_Type;
14724 else
14725 return Btype;
14726 end if;
14727
14728 else
14729 return Btype;
14730 end if;
14731
14732 elsif Is_Array_Type (Btype) then
14733 return Trace_Components (Component_Type (Btype), True);
14734
14735 elsif Is_Record_Type (Btype) then
14736 Component := First_Entity (Btype);
14737 while Present (Component)
14738 and then Comes_From_Source (Component)
14739 loop
14740 -- Skip anonymous types generated by constrained components
14741
14742 if not Is_Type (Component) then
14743 P := Trace_Components (Etype (Component), True);
14744
14745 if Present (P) then
14746 if P = Any_Type then
14747 return P;
14748 else
14749 Candidate := P;
14750 end if;
14751 end if;
14752 end if;
14753
14754 Next_Entity (Component);
14755 end loop;
14756
14757 return Candidate;
14758
14759 else
14760 return Empty;
14761 end if;
14762 end Trace_Components;
14763
14764 -- Start of processing for Private_Component
14765
14766 begin
14767 return Trace_Components (Type_Id, False);
14768 end Private_Component;
14769
14770 ---------------------------
14771 -- Primitive_Names_Match --
14772 ---------------------------
14773
14774 function Primitive_Names_Match (E1, E2 : Entity_Id) return Boolean is
14775
14776 function Non_Internal_Name (E : Entity_Id) return Name_Id;
14777 -- Given an internal name, returns the corresponding non-internal name
14778
14779 ------------------------
14780 -- Non_Internal_Name --
14781 ------------------------
14782
14783 function Non_Internal_Name (E : Entity_Id) return Name_Id is
14784 begin
14785 Get_Name_String (Chars (E));
14786 Name_Len := Name_Len - 1;
14787 return Name_Find;
14788 end Non_Internal_Name;
14789
14790 -- Start of processing for Primitive_Names_Match
14791
14792 begin
14793 pragma Assert (Present (E1) and then Present (E2));
14794
14795 return Chars (E1) = Chars (E2)
14796 or else
14797 (not Is_Internal_Name (Chars (E1))
14798 and then Is_Internal_Name (Chars (E2))
14799 and then Non_Internal_Name (E2) = Chars (E1))
14800 or else
14801 (not Is_Internal_Name (Chars (E2))
14802 and then Is_Internal_Name (Chars (E1))
14803 and then Non_Internal_Name (E1) = Chars (E2))
14804 or else
14805 (Is_Predefined_Dispatching_Operation (E1)
14806 and then Is_Predefined_Dispatching_Operation (E2)
14807 and then Same_TSS (E1, E2))
14808 or else
14809 (Is_Init_Proc (E1) and then Is_Init_Proc (E2));
14810 end Primitive_Names_Match;
14811
14812 -----------------------
14813 -- Process_End_Label --
14814 -----------------------
14815
14816 procedure Process_End_Label
14817 (N : Node_Id;
14818 Typ : Character;
14819 Ent : Entity_Id)
14820 is
14821 Loc : Source_Ptr;
14822 Nam : Node_Id;
14823 Scop : Entity_Id;
14824
14825 Label_Ref : Boolean;
14826 -- Set True if reference to end label itself is required
14827
14828 Endl : Node_Id;
14829 -- Gets set to the operator symbol or identifier that references the
14830 -- entity Ent. For the child unit case, this is the identifier from the
14831 -- designator. For other cases, this is simply Endl.
14832
14833 procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id);
14834 -- N is an identifier node that appears as a parent unit reference in
14835 -- the case where Ent is a child unit. This procedure generates an
14836 -- appropriate cross-reference entry. E is the corresponding entity.
14837
14838 -------------------------
14839 -- Generate_Parent_Ref --
14840 -------------------------
14841
14842 procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id) is
14843 begin
14844 -- If names do not match, something weird, skip reference
14845
14846 if Chars (E) = Chars (N) then
14847
14848 -- Generate the reference. We do NOT consider this as a reference
14849 -- for unreferenced symbol purposes.
14850
14851 Generate_Reference (E, N, 'r', Set_Ref => False, Force => True);
14852
14853 if Style_Check then
14854 Style.Check_Identifier (N, E);
14855 end if;
14856 end if;
14857 end Generate_Parent_Ref;
14858
14859 -- Start of processing for Process_End_Label
14860
14861 begin
14862 -- If no node, ignore. This happens in some error situations, and
14863 -- also for some internally generated structures where no end label
14864 -- references are required in any case.
14865
14866 if No (N) then
14867 return;
14868 end if;
14869
14870 -- Nothing to do if no End_Label, happens for internally generated
14871 -- constructs where we don't want an end label reference anyway. Also
14872 -- nothing to do if Endl is a string literal, which means there was
14873 -- some prior error (bad operator symbol)
14874
14875 Endl := End_Label (N);
14876
14877 if No (Endl) or else Nkind (Endl) = N_String_Literal then
14878 return;
14879 end if;
14880
14881 -- Reference node is not in extended main source unit
14882
14883 if not In_Extended_Main_Source_Unit (N) then
14884
14885 -- Generally we do not collect references except for the extended
14886 -- main source unit. The one exception is the 'e' entry for a
14887 -- package spec, where it is useful for a client to have the
14888 -- ending information to define scopes.
14889
14890 if Typ /= 'e' then
14891 return;
14892
14893 else
14894 Label_Ref := False;
14895
14896 -- For this case, we can ignore any parent references, but we
14897 -- need the package name itself for the 'e' entry.
14898
14899 if Nkind (Endl) = N_Designator then
14900 Endl := Identifier (Endl);
14901 end if;
14902 end if;
14903
14904 -- Reference is in extended main source unit
14905
14906 else
14907 Label_Ref := True;
14908
14909 -- For designator, generate references for the parent entries
14910
14911 if Nkind (Endl) = N_Designator then
14912
14913 -- Generate references for the prefix if the END line comes from
14914 -- source (otherwise we do not need these references) We climb the
14915 -- scope stack to find the expected entities.
14916
14917 if Comes_From_Source (Endl) then
14918 Nam := Name (Endl);
14919 Scop := Current_Scope;
14920 while Nkind (Nam) = N_Selected_Component loop
14921 Scop := Scope (Scop);
14922 exit when No (Scop);
14923 Generate_Parent_Ref (Selector_Name (Nam), Scop);
14924 Nam := Prefix (Nam);
14925 end loop;
14926
14927 if Present (Scop) then
14928 Generate_Parent_Ref (Nam, Scope (Scop));
14929 end if;
14930 end if;
14931
14932 Endl := Identifier (Endl);
14933 end if;
14934 end if;
14935
14936 -- If the end label is not for the given entity, then either we have
14937 -- some previous error, or this is a generic instantiation for which
14938 -- we do not need to make a cross-reference in this case anyway. In
14939 -- either case we simply ignore the call.
14940
14941 if Chars (Ent) /= Chars (Endl) then
14942 return;
14943 end if;
14944
14945 -- If label was really there, then generate a normal reference and then
14946 -- adjust the location in the end label to point past the name (which
14947 -- should almost always be the semicolon).
14948
14949 Loc := Sloc (Endl);
14950
14951 if Comes_From_Source (Endl) then
14952
14953 -- If a label reference is required, then do the style check and
14954 -- generate an l-type cross-reference entry for the label
14955
14956 if Label_Ref then
14957 if Style_Check then
14958 Style.Check_Identifier (Endl, Ent);
14959 end if;
14960
14961 Generate_Reference (Ent, Endl, 'l', Set_Ref => False);
14962 end if;
14963
14964 -- Set the location to point past the label (normally this will
14965 -- mean the semicolon immediately following the label). This is
14966 -- done for the sake of the 'e' or 't' entry generated below.
14967
14968 Get_Decoded_Name_String (Chars (Endl));
14969 Set_Sloc (Endl, Sloc (Endl) + Source_Ptr (Name_Len));
14970
14971 else
14972 -- In SPARK mode, no missing label is allowed for packages and
14973 -- subprogram bodies. Detect those cases by testing whether
14974 -- Process_End_Label was called for a body (Typ = 't') or a package.
14975
14976 if Restriction_Check_Required (SPARK_05)
14977 and then (Typ = 't' or else Ekind (Ent) = E_Package)
14978 then
14979 Error_Msg_Node_1 := Endl;
14980 Check_SPARK_Restriction ("`END &` required", Endl, Force => True);
14981 end if;
14982 end if;
14983
14984 -- Now generate the e/t reference
14985
14986 Generate_Reference (Ent, Endl, Typ, Set_Ref => False, Force => True);
14987
14988 -- Restore Sloc, in case modified above, since we have an identifier
14989 -- and the normal Sloc should be left set in the tree.
14990
14991 Set_Sloc (Endl, Loc);
14992 end Process_End_Label;
14993
14994 ----------------
14995 -- Referenced --
14996 ----------------
14997
14998 function Referenced (Id : Entity_Id; Expr : Node_Id) return Boolean is
14999 Seen : Boolean := False;
15000
15001 function Is_Reference (N : Node_Id) return Traverse_Result;
15002 -- Determine whether node N denotes a reference to Id. If this is the
15003 -- case, set global flag Seen to True and stop the traversal.
15004
15005 ------------------
15006 -- Is_Reference --
15007 ------------------
15008
15009 function Is_Reference (N : Node_Id) return Traverse_Result is
15010 begin
15011 if Is_Entity_Name (N)
15012 and then Present (Entity (N))
15013 and then Entity (N) = Id
15014 then
15015 Seen := True;
15016 return Abandon;
15017 else
15018 return OK;
15019 end if;
15020 end Is_Reference;
15021
15022 procedure Inspect_Expression is new Traverse_Proc (Is_Reference);
15023
15024 -- Start of processing for Referenced
15025
15026 begin
15027 Inspect_Expression (Expr);
15028 return Seen;
15029 end Referenced;
15030
15031 ------------------------------------
15032 -- References_Generic_Formal_Type --
15033 ------------------------------------
15034
15035 function References_Generic_Formal_Type (N : Node_Id) return Boolean is
15036
15037 function Process (N : Node_Id) return Traverse_Result;
15038 -- Process one node in search for generic formal type
15039
15040 -------------
15041 -- Process --
15042 -------------
15043
15044 function Process (N : Node_Id) return Traverse_Result is
15045 begin
15046 if Nkind (N) in N_Has_Entity then
15047 declare
15048 E : constant Entity_Id := Entity (N);
15049 begin
15050 if Present (E) then
15051 if Is_Generic_Type (E) then
15052 return Abandon;
15053 elsif Present (Etype (E))
15054 and then Is_Generic_Type (Etype (E))
15055 then
15056 return Abandon;
15057 end if;
15058 end if;
15059 end;
15060 end if;
15061
15062 return Atree.OK;
15063 end Process;
15064
15065 function Traverse is new Traverse_Func (Process);
15066 -- Traverse tree to look for generic type
15067
15068 begin
15069 if Inside_A_Generic then
15070 return Traverse (N) = Abandon;
15071 else
15072 return False;
15073 end if;
15074 end References_Generic_Formal_Type;
15075
15076 --------------------
15077 -- Remove_Homonym --
15078 --------------------
15079
15080 procedure Remove_Homonym (E : Entity_Id) is
15081 Prev : Entity_Id := Empty;
15082 H : Entity_Id;
15083
15084 begin
15085 if E = Current_Entity (E) then
15086 if Present (Homonym (E)) then
15087 Set_Current_Entity (Homonym (E));
15088 else
15089 Set_Name_Entity_Id (Chars (E), Empty);
15090 end if;
15091
15092 else
15093 H := Current_Entity (E);
15094 while Present (H) and then H /= E loop
15095 Prev := H;
15096 H := Homonym (H);
15097 end loop;
15098
15099 -- If E is not on the homonym chain, nothing to do
15100
15101 if Present (H) then
15102 Set_Homonym (Prev, Homonym (E));
15103 end if;
15104 end if;
15105 end Remove_Homonym;
15106
15107 ---------------------
15108 -- Rep_To_Pos_Flag --
15109 ---------------------
15110
15111 function Rep_To_Pos_Flag (E : Entity_Id; Loc : Source_Ptr) return Node_Id is
15112 begin
15113 return New_Occurrence_Of
15114 (Boolean_Literals (not Range_Checks_Suppressed (E)), Loc);
15115 end Rep_To_Pos_Flag;
15116
15117 --------------------
15118 -- Require_Entity --
15119 --------------------
15120
15121 procedure Require_Entity (N : Node_Id) is
15122 begin
15123 if Is_Entity_Name (N) and then No (Entity (N)) then
15124 if Total_Errors_Detected /= 0 then
15125 Set_Entity (N, Any_Id);
15126 else
15127 raise Program_Error;
15128 end if;
15129 end if;
15130 end Require_Entity;
15131
15132 -------------------------------
15133 -- Requires_State_Refinement --
15134 -------------------------------
15135
15136 function Requires_State_Refinement
15137 (Spec_Id : Entity_Id;
15138 Body_Id : Entity_Id) return Boolean
15139 is
15140 function Mode_Is_Off (Prag : Node_Id) return Boolean;
15141 -- Given pragma SPARK_Mode, determine whether the mode is Off
15142
15143 -----------------
15144 -- Mode_Is_Off --
15145 -----------------
15146
15147 function Mode_Is_Off (Prag : Node_Id) return Boolean is
15148 Mode : Node_Id;
15149
15150 begin
15151 -- The default SPARK mode is On
15152
15153 if No (Prag) then
15154 return False;
15155 end if;
15156
15157 Mode := Get_Pragma_Arg (First (Pragma_Argument_Associations (Prag)));
15158
15159 -- Then the pragma lacks an argument, the default mode is On
15160
15161 if No (Mode) then
15162 return False;
15163 else
15164 return Chars (Mode) = Name_Off;
15165 end if;
15166 end Mode_Is_Off;
15167
15168 -- Start of processing for Requires_State_Refinement
15169
15170 begin
15171 -- A package that does not define at least one abstract state cannot
15172 -- possibly require refinement.
15173
15174 if No (Abstract_States (Spec_Id)) then
15175 return False;
15176
15177 -- The package instroduces a single null state which does not merit
15178 -- refinement.
15179
15180 elsif Has_Null_Abstract_State (Spec_Id) then
15181 return False;
15182
15183 -- Check whether the package body is subject to pragma SPARK_Mode. If
15184 -- it is and the mode is Off, the package body is considered to be in
15185 -- regular Ada and does not require refinement.
15186
15187 elsif Mode_Is_Off (SPARK_Pragma (Body_Id)) then
15188 return False;
15189
15190 -- The body's SPARK_Mode may be inherited from a similar pragma that
15191 -- appears in the private declarations of the spec. The pragma we are
15192 -- interested appears as the second entry in SPARK_Pragma.
15193
15194 elsif Present (SPARK_Pragma (Spec_Id))
15195 and then Mode_Is_Off (Next_Pragma (SPARK_Pragma (Spec_Id)))
15196 then
15197 return False;
15198
15199 -- The spec defines at least one abstract state and the body has no way
15200 -- of circumventing the refinement.
15201
15202 else
15203 return True;
15204 end if;
15205 end Requires_State_Refinement;
15206
15207 ------------------------------
15208 -- Requires_Transient_Scope --
15209 ------------------------------
15210
15211 -- A transient scope is required when variable-sized temporaries are
15212 -- allocated in the primary or secondary stack, or when finalization
15213 -- actions must be generated before the next instruction.
15214
15215 function Requires_Transient_Scope (Id : Entity_Id) return Boolean is
15216 Typ : constant Entity_Id := Underlying_Type (Id);
15217
15218 -- Start of processing for Requires_Transient_Scope
15219
15220 begin
15221 -- This is a private type which is not completed yet. This can only
15222 -- happen in a default expression (of a formal parameter or of a
15223 -- record component). Do not expand transient scope in this case
15224
15225 if No (Typ) then
15226 return False;
15227
15228 -- Do not expand transient scope for non-existent procedure return
15229
15230 elsif Typ = Standard_Void_Type then
15231 return False;
15232
15233 -- Elementary types do not require a transient scope
15234
15235 elsif Is_Elementary_Type (Typ) then
15236 return False;
15237
15238 -- Generally, indefinite subtypes require a transient scope, since the
15239 -- back end cannot generate temporaries, since this is not a valid type
15240 -- for declaring an object. It might be possible to relax this in the
15241 -- future, e.g. by declaring the maximum possible space for the type.
15242
15243 elsif Is_Indefinite_Subtype (Typ) then
15244 return True;
15245
15246 -- Functions returning tagged types may dispatch on result so their
15247 -- returned value is allocated on the secondary stack. Controlled
15248 -- type temporaries need finalization.
15249
15250 elsif Is_Tagged_Type (Typ)
15251 or else Has_Controlled_Component (Typ)
15252 then
15253 return not Is_Value_Type (Typ);
15254
15255 -- Record type
15256
15257 elsif Is_Record_Type (Typ) then
15258 declare
15259 Comp : Entity_Id;
15260 begin
15261 Comp := First_Entity (Typ);
15262 while Present (Comp) loop
15263 if Ekind (Comp) = E_Component
15264 and then Requires_Transient_Scope (Etype (Comp))
15265 then
15266 return True;
15267 else
15268 Next_Entity (Comp);
15269 end if;
15270 end loop;
15271 end;
15272
15273 return False;
15274
15275 -- String literal types never require transient scope
15276
15277 elsif Ekind (Typ) = E_String_Literal_Subtype then
15278 return False;
15279
15280 -- Array type. Note that we already know that this is a constrained
15281 -- array, since unconstrained arrays will fail the indefinite test.
15282
15283 elsif Is_Array_Type (Typ) then
15284
15285 -- If component type requires a transient scope, the array does too
15286
15287 if Requires_Transient_Scope (Component_Type (Typ)) then
15288 return True;
15289
15290 -- Otherwise, we only need a transient scope if the size depends on
15291 -- the value of one or more discriminants.
15292
15293 else
15294 return Size_Depends_On_Discriminant (Typ);
15295 end if;
15296
15297 -- All other cases do not require a transient scope
15298
15299 else
15300 return False;
15301 end if;
15302 end Requires_Transient_Scope;
15303
15304 --------------------------
15305 -- Reset_Analyzed_Flags --
15306 --------------------------
15307
15308 procedure Reset_Analyzed_Flags (N : Node_Id) is
15309
15310 function Clear_Analyzed (N : Node_Id) return Traverse_Result;
15311 -- Function used to reset Analyzed flags in tree. Note that we do
15312 -- not reset Analyzed flags in entities, since there is no need to
15313 -- reanalyze entities, and indeed, it is wrong to do so, since it
15314 -- can result in generating auxiliary stuff more than once.
15315
15316 --------------------
15317 -- Clear_Analyzed --
15318 --------------------
15319
15320 function Clear_Analyzed (N : Node_Id) return Traverse_Result is
15321 begin
15322 if not Has_Extension (N) then
15323 Set_Analyzed (N, False);
15324 end if;
15325
15326 return OK;
15327 end Clear_Analyzed;
15328
15329 procedure Reset_Analyzed is new Traverse_Proc (Clear_Analyzed);
15330
15331 -- Start of processing for Reset_Analyzed_Flags
15332
15333 begin
15334 Reset_Analyzed (N);
15335 end Reset_Analyzed_Flags;
15336
15337 ------------------------
15338 -- Restore_SPARK_Mode --
15339 ------------------------
15340
15341 procedure Restore_SPARK_Mode (Mode : SPARK_Mode_Type) is
15342 begin
15343 SPARK_Mode := Mode;
15344 end Restore_SPARK_Mode;
15345
15346 --------------------------------
15347 -- Returns_Unconstrained_Type --
15348 --------------------------------
15349
15350 function Returns_Unconstrained_Type (Subp : Entity_Id) return Boolean is
15351 begin
15352 return Ekind (Subp) = E_Function
15353 and then not Is_Scalar_Type (Etype (Subp))
15354 and then not Is_Access_Type (Etype (Subp))
15355 and then not Is_Constrained (Etype (Subp));
15356 end Returns_Unconstrained_Type;
15357
15358 ---------------------------
15359 -- Safe_To_Capture_Value --
15360 ---------------------------
15361
15362 function Safe_To_Capture_Value
15363 (N : Node_Id;
15364 Ent : Entity_Id;
15365 Cond : Boolean := False) return Boolean
15366 is
15367 begin
15368 -- The only entities for which we track constant values are variables
15369 -- which are not renamings, constants, out parameters, and in out
15370 -- parameters, so check if we have this case.
15371
15372 -- Note: it may seem odd to track constant values for constants, but in
15373 -- fact this routine is used for other purposes than simply capturing
15374 -- the value. In particular, the setting of Known[_Non]_Null.
15375
15376 if (Ekind (Ent) = E_Variable and then No (Renamed_Object (Ent)))
15377 or else
15378 Ekind (Ent) = E_Constant
15379 or else
15380 Ekind (Ent) = E_Out_Parameter
15381 or else
15382 Ekind (Ent) = E_In_Out_Parameter
15383 then
15384 null;
15385
15386 -- For conditionals, we also allow loop parameters and all formals,
15387 -- including in parameters.
15388
15389 elsif Cond
15390 and then
15391 (Ekind (Ent) = E_Loop_Parameter
15392 or else
15393 Ekind (Ent) = E_In_Parameter)
15394 then
15395 null;
15396
15397 -- For all other cases, not just unsafe, but impossible to capture
15398 -- Current_Value, since the above are the only entities which have
15399 -- Current_Value fields.
15400
15401 else
15402 return False;
15403 end if;
15404
15405 -- Skip if volatile or aliased, since funny things might be going on in
15406 -- these cases which we cannot necessarily track. Also skip any variable
15407 -- for which an address clause is given, or whose address is taken. Also
15408 -- never capture value of library level variables (an attempt to do so
15409 -- can occur in the case of package elaboration code).
15410
15411 if Treat_As_Volatile (Ent)
15412 or else Is_Aliased (Ent)
15413 or else Present (Address_Clause (Ent))
15414 or else Address_Taken (Ent)
15415 or else (Is_Library_Level_Entity (Ent)
15416 and then Ekind (Ent) = E_Variable)
15417 then
15418 return False;
15419 end if;
15420
15421 -- OK, all above conditions are met. We also require that the scope of
15422 -- the reference be the same as the scope of the entity, not counting
15423 -- packages and blocks and loops.
15424
15425 declare
15426 E_Scope : constant Entity_Id := Scope (Ent);
15427 R_Scope : Entity_Id;
15428
15429 begin
15430 R_Scope := Current_Scope;
15431 while R_Scope /= Standard_Standard loop
15432 exit when R_Scope = E_Scope;
15433
15434 if not Ekind_In (R_Scope, E_Package, E_Block, E_Loop) then
15435 return False;
15436 else
15437 R_Scope := Scope (R_Scope);
15438 end if;
15439 end loop;
15440 end;
15441
15442 -- We also require that the reference does not appear in a context
15443 -- where it is not sure to be executed (i.e. a conditional context
15444 -- or an exception handler). We skip this if Cond is True, since the
15445 -- capturing of values from conditional tests handles this ok.
15446
15447 if Cond then
15448 return True;
15449 end if;
15450
15451 declare
15452 Desc : Node_Id;
15453 P : Node_Id;
15454
15455 begin
15456 Desc := N;
15457
15458 -- Seems dubious that case expressions are not handled here ???
15459
15460 P := Parent (N);
15461 while Present (P) loop
15462 if Nkind (P) = N_If_Statement
15463 or else Nkind (P) = N_Case_Statement
15464 or else (Nkind (P) in N_Short_Circuit
15465 and then Desc = Right_Opnd (P))
15466 or else (Nkind (P) = N_If_Expression
15467 and then Desc /= First (Expressions (P)))
15468 or else Nkind (P) = N_Exception_Handler
15469 or else Nkind (P) = N_Selective_Accept
15470 or else Nkind (P) = N_Conditional_Entry_Call
15471 or else Nkind (P) = N_Timed_Entry_Call
15472 or else Nkind (P) = N_Asynchronous_Select
15473 then
15474 return False;
15475 else
15476 Desc := P;
15477 P := Parent (P);
15478
15479 -- A special Ada 2012 case: the original node may be part
15480 -- of the else_actions of a conditional expression, in which
15481 -- case it might not have been expanded yet, and appears in
15482 -- a non-syntactic list of actions. In that case it is clearly
15483 -- not safe to save a value.
15484
15485 if No (P)
15486 and then Is_List_Member (Desc)
15487 and then No (Parent (List_Containing (Desc)))
15488 then
15489 return False;
15490 end if;
15491 end if;
15492 end loop;
15493 end;
15494
15495 -- OK, looks safe to set value
15496
15497 return True;
15498 end Safe_To_Capture_Value;
15499
15500 ---------------
15501 -- Same_Name --
15502 ---------------
15503
15504 function Same_Name (N1, N2 : Node_Id) return Boolean is
15505 K1 : constant Node_Kind := Nkind (N1);
15506 K2 : constant Node_Kind := Nkind (N2);
15507
15508 begin
15509 if (K1 = N_Identifier or else K1 = N_Defining_Identifier)
15510 and then (K2 = N_Identifier or else K2 = N_Defining_Identifier)
15511 then
15512 return Chars (N1) = Chars (N2);
15513
15514 elsif (K1 = N_Selected_Component or else K1 = N_Expanded_Name)
15515 and then (K2 = N_Selected_Component or else K2 = N_Expanded_Name)
15516 then
15517 return Same_Name (Selector_Name (N1), Selector_Name (N2))
15518 and then Same_Name (Prefix (N1), Prefix (N2));
15519
15520 else
15521 return False;
15522 end if;
15523 end Same_Name;
15524
15525 -----------------
15526 -- Same_Object --
15527 -----------------
15528
15529 function Same_Object (Node1, Node2 : Node_Id) return Boolean is
15530 N1 : constant Node_Id := Original_Node (Node1);
15531 N2 : constant Node_Id := Original_Node (Node2);
15532 -- We do the tests on original nodes, since we are most interested
15533 -- in the original source, not any expansion that got in the way.
15534
15535 K1 : constant Node_Kind := Nkind (N1);
15536 K2 : constant Node_Kind := Nkind (N2);
15537
15538 begin
15539 -- First case, both are entities with same entity
15540
15541 if K1 in N_Has_Entity and then K2 in N_Has_Entity then
15542 declare
15543 EN1 : constant Entity_Id := Entity (N1);
15544 EN2 : constant Entity_Id := Entity (N2);
15545 begin
15546 if Present (EN1) and then Present (EN2)
15547 and then (Ekind_In (EN1, E_Variable, E_Constant)
15548 or else Is_Formal (EN1))
15549 and then EN1 = EN2
15550 then
15551 return True;
15552 end if;
15553 end;
15554 end if;
15555
15556 -- Second case, selected component with same selector, same record
15557
15558 if K1 = N_Selected_Component
15559 and then K2 = N_Selected_Component
15560 and then Chars (Selector_Name (N1)) = Chars (Selector_Name (N2))
15561 then
15562 return Same_Object (Prefix (N1), Prefix (N2));
15563
15564 -- Third case, indexed component with same subscripts, same array
15565
15566 elsif K1 = N_Indexed_Component
15567 and then K2 = N_Indexed_Component
15568 and then Same_Object (Prefix (N1), Prefix (N2))
15569 then
15570 declare
15571 E1, E2 : Node_Id;
15572 begin
15573 E1 := First (Expressions (N1));
15574 E2 := First (Expressions (N2));
15575 while Present (E1) loop
15576 if not Same_Value (E1, E2) then
15577 return False;
15578 else
15579 Next (E1);
15580 Next (E2);
15581 end if;
15582 end loop;
15583
15584 return True;
15585 end;
15586
15587 -- Fourth case, slice of same array with same bounds
15588
15589 elsif K1 = N_Slice
15590 and then K2 = N_Slice
15591 and then Nkind (Discrete_Range (N1)) = N_Range
15592 and then Nkind (Discrete_Range (N2)) = N_Range
15593 and then Same_Value (Low_Bound (Discrete_Range (N1)),
15594 Low_Bound (Discrete_Range (N2)))
15595 and then Same_Value (High_Bound (Discrete_Range (N1)),
15596 High_Bound (Discrete_Range (N2)))
15597 then
15598 return Same_Name (Prefix (N1), Prefix (N2));
15599
15600 -- All other cases, not clearly the same object
15601
15602 else
15603 return False;
15604 end if;
15605 end Same_Object;
15606
15607 ---------------
15608 -- Same_Type --
15609 ---------------
15610
15611 function Same_Type (T1, T2 : Entity_Id) return Boolean is
15612 begin
15613 if T1 = T2 then
15614 return True;
15615
15616 elsif not Is_Constrained (T1)
15617 and then not Is_Constrained (T2)
15618 and then Base_Type (T1) = Base_Type (T2)
15619 then
15620 return True;
15621
15622 -- For now don't bother with case of identical constraints, to be
15623 -- fiddled with later on perhaps (this is only used for optimization
15624 -- purposes, so it is not critical to do a best possible job)
15625
15626 else
15627 return False;
15628 end if;
15629 end Same_Type;
15630
15631 ----------------
15632 -- Same_Value --
15633 ----------------
15634
15635 function Same_Value (Node1, Node2 : Node_Id) return Boolean is
15636 begin
15637 if Compile_Time_Known_Value (Node1)
15638 and then Compile_Time_Known_Value (Node2)
15639 and then Expr_Value (Node1) = Expr_Value (Node2)
15640 then
15641 return True;
15642 elsif Same_Object (Node1, Node2) then
15643 return True;
15644 else
15645 return False;
15646 end if;
15647 end Same_Value;
15648
15649 -----------------------------
15650 -- Save_SPARK_Mode_And_Set --
15651 -----------------------------
15652
15653 procedure Save_SPARK_Mode_And_Set
15654 (Context : Entity_Id;
15655 Mode : out SPARK_Mode_Type)
15656 is
15657 Prag : constant Node_Id := SPARK_Pragma (Context);
15658
15659 begin
15660 -- Save the current mode in effect
15661
15662 Mode := SPARK_Mode;
15663
15664 -- Set the mode of the context as the current SPARK mode
15665
15666 if Present (Prag) then
15667 SPARK_Mode := Get_SPARK_Mode_From_Pragma (Prag);
15668 end if;
15669 end Save_SPARK_Mode_And_Set;
15670
15671 ------------------------
15672 -- Scope_Is_Transient --
15673 ------------------------
15674
15675 function Scope_Is_Transient return Boolean is
15676 begin
15677 return Scope_Stack.Table (Scope_Stack.Last).Is_Transient;
15678 end Scope_Is_Transient;
15679
15680 ------------------
15681 -- Scope_Within --
15682 ------------------
15683
15684 function Scope_Within (Scope1, Scope2 : Entity_Id) return Boolean is
15685 Scop : Entity_Id;
15686
15687 begin
15688 Scop := Scope1;
15689 while Scop /= Standard_Standard loop
15690 Scop := Scope (Scop);
15691
15692 if Scop = Scope2 then
15693 return True;
15694 end if;
15695 end loop;
15696
15697 return False;
15698 end Scope_Within;
15699
15700 --------------------------
15701 -- Scope_Within_Or_Same --
15702 --------------------------
15703
15704 function Scope_Within_Or_Same (Scope1, Scope2 : Entity_Id) return Boolean is
15705 Scop : Entity_Id;
15706
15707 begin
15708 Scop := Scope1;
15709 while Scop /= Standard_Standard loop
15710 if Scop = Scope2 then
15711 return True;
15712 else
15713 Scop := Scope (Scop);
15714 end if;
15715 end loop;
15716
15717 return False;
15718 end Scope_Within_Or_Same;
15719
15720 --------------------
15721 -- Set_Convention --
15722 --------------------
15723
15724 procedure Set_Convention (E : Entity_Id; Val : Snames.Convention_Id) is
15725 begin
15726 Basic_Set_Convention (E, Val);
15727
15728 if Is_Type (E)
15729 and then Is_Access_Subprogram_Type (Base_Type (E))
15730 and then Has_Foreign_Convention (E)
15731 then
15732 Set_Can_Use_Internal_Rep (E, False);
15733 end if;
15734
15735 -- If E is an object or component, and the type of E is an anonymous
15736 -- access type with no convention set, then also set the convention of
15737 -- the anonymous access type. We do not do this for anonymous protected
15738 -- types, since protected types always have the default convention.
15739
15740 if Present (Etype (E))
15741 and then (Is_Object (E)
15742 or else Ekind (E) = E_Component
15743
15744 -- Allow E_Void (happens for pragma Convention appearing
15745 -- in the middle of a record applying to a component)
15746
15747 or else Ekind (E) = E_Void)
15748 then
15749 declare
15750 Typ : constant Entity_Id := Etype (E);
15751
15752 begin
15753 if Ekind_In (Typ, E_Anonymous_Access_Type,
15754 E_Anonymous_Access_Subprogram_Type)
15755 and then not Has_Convention_Pragma (Typ)
15756 then
15757 Basic_Set_Convention (Typ, Val);
15758 Set_Has_Convention_Pragma (Typ);
15759
15760 -- And for the access subprogram type, deal similarly with the
15761 -- designated E_Subprogram_Type if it is also internal (which
15762 -- it always is?)
15763
15764 if Ekind (Typ) = E_Anonymous_Access_Subprogram_Type then
15765 declare
15766 Dtype : constant Entity_Id := Designated_Type (Typ);
15767 begin
15768 if Ekind (Dtype) = E_Subprogram_Type
15769 and then Is_Itype (Dtype)
15770 and then not Has_Convention_Pragma (Dtype)
15771 then
15772 Basic_Set_Convention (Dtype, Val);
15773 Set_Has_Convention_Pragma (Dtype);
15774 end if;
15775 end;
15776 end if;
15777 end if;
15778 end;
15779 end if;
15780 end Set_Convention;
15781
15782 ------------------------
15783 -- Set_Current_Entity --
15784 ------------------------
15785
15786 -- The given entity is to be set as the currently visible definition of its
15787 -- associated name (i.e. the Node_Id associated with its name). All we have
15788 -- to do is to get the name from the identifier, and then set the
15789 -- associated Node_Id to point to the given entity.
15790
15791 procedure Set_Current_Entity (E : Entity_Id) is
15792 begin
15793 Set_Name_Entity_Id (Chars (E), E);
15794 end Set_Current_Entity;
15795
15796 ---------------------------
15797 -- Set_Debug_Info_Needed --
15798 ---------------------------
15799
15800 procedure Set_Debug_Info_Needed (T : Entity_Id) is
15801
15802 procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id);
15803 pragma Inline (Set_Debug_Info_Needed_If_Not_Set);
15804 -- Used to set debug info in a related node if not set already
15805
15806 --------------------------------------
15807 -- Set_Debug_Info_Needed_If_Not_Set --
15808 --------------------------------------
15809
15810 procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id) is
15811 begin
15812 if Present (E)
15813 and then not Needs_Debug_Info (E)
15814 then
15815 Set_Debug_Info_Needed (E);
15816
15817 -- For a private type, indicate that the full view also needs
15818 -- debug information.
15819
15820 if Is_Type (E)
15821 and then Is_Private_Type (E)
15822 and then Present (Full_View (E))
15823 then
15824 Set_Debug_Info_Needed (Full_View (E));
15825 end if;
15826 end if;
15827 end Set_Debug_Info_Needed_If_Not_Set;
15828
15829 -- Start of processing for Set_Debug_Info_Needed
15830
15831 begin
15832 -- Nothing to do if argument is Empty or has Debug_Info_Off set, which
15833 -- indicates that Debug_Info_Needed is never required for the entity.
15834
15835 if No (T)
15836 or else Debug_Info_Off (T)
15837 then
15838 return;
15839 end if;
15840
15841 -- Set flag in entity itself. Note that we will go through the following
15842 -- circuitry even if the flag is already set on T. That's intentional,
15843 -- it makes sure that the flag will be set in subsidiary entities.
15844
15845 Set_Needs_Debug_Info (T);
15846
15847 -- Set flag on subsidiary entities if not set already
15848
15849 if Is_Object (T) then
15850 Set_Debug_Info_Needed_If_Not_Set (Etype (T));
15851
15852 elsif Is_Type (T) then
15853 Set_Debug_Info_Needed_If_Not_Set (Etype (T));
15854
15855 if Is_Record_Type (T) then
15856 declare
15857 Ent : Entity_Id := First_Entity (T);
15858 begin
15859 while Present (Ent) loop
15860 Set_Debug_Info_Needed_If_Not_Set (Ent);
15861 Next_Entity (Ent);
15862 end loop;
15863 end;
15864
15865 -- For a class wide subtype, we also need debug information
15866 -- for the equivalent type.
15867
15868 if Ekind (T) = E_Class_Wide_Subtype then
15869 Set_Debug_Info_Needed_If_Not_Set (Equivalent_Type (T));
15870 end if;
15871
15872 elsif Is_Array_Type (T) then
15873 Set_Debug_Info_Needed_If_Not_Set (Component_Type (T));
15874
15875 declare
15876 Indx : Node_Id := First_Index (T);
15877 begin
15878 while Present (Indx) loop
15879 Set_Debug_Info_Needed_If_Not_Set (Etype (Indx));
15880 Indx := Next_Index (Indx);
15881 end loop;
15882 end;
15883
15884 -- For a packed array type, we also need debug information for
15885 -- the type used to represent the packed array. Conversely, we
15886 -- also need it for the former if we need it for the latter.
15887
15888 if Is_Packed (T) then
15889 Set_Debug_Info_Needed_If_Not_Set (Packed_Array_Type (T));
15890 end if;
15891
15892 if Is_Packed_Array_Type (T) then
15893 Set_Debug_Info_Needed_If_Not_Set (Original_Array_Type (T));
15894 end if;
15895
15896 elsif Is_Access_Type (T) then
15897 Set_Debug_Info_Needed_If_Not_Set (Directly_Designated_Type (T));
15898
15899 elsif Is_Private_Type (T) then
15900 Set_Debug_Info_Needed_If_Not_Set (Full_View (T));
15901
15902 elsif Is_Protected_Type (T) then
15903 Set_Debug_Info_Needed_If_Not_Set (Corresponding_Record_Type (T));
15904 end if;
15905 end if;
15906 end Set_Debug_Info_Needed;
15907
15908 ----------------------------
15909 -- Set_Entity_With_Checks --
15910 ----------------------------
15911
15912 procedure Set_Entity_With_Checks (N : Node_Id; Val : Entity_Id) is
15913 Val_Actual : Entity_Id;
15914 Nod : Node_Id;
15915 Post_Node : Node_Id;
15916
15917 begin
15918 -- Unconditionally set the entity
15919
15920 Set_Entity (N, Val);
15921
15922 -- The node to post on is the selector in the case of an expanded name,
15923 -- and otherwise the node itself.
15924
15925 if Nkind (N) = N_Expanded_Name then
15926 Post_Node := Selector_Name (N);
15927 else
15928 Post_Node := N;
15929 end if;
15930
15931 -- Check for violation of No_Fixed_IO
15932
15933 if Restriction_Check_Required (No_Fixed_IO)
15934 and then
15935 ((RTU_Loaded (Ada_Text_IO)
15936 and then (Is_RTE (Val, RE_Decimal_IO)
15937 or else
15938 Is_RTE (Val, RE_Fixed_IO)))
15939
15940 or else
15941 (RTU_Loaded (Ada_Wide_Text_IO)
15942 and then (Is_RTE (Val, RO_WT_Decimal_IO)
15943 or else
15944 Is_RTE (Val, RO_WT_Fixed_IO)))
15945
15946 or else
15947 (RTU_Loaded (Ada_Wide_Wide_Text_IO)
15948 and then (Is_RTE (Val, RO_WW_Decimal_IO)
15949 or else
15950 Is_RTE (Val, RO_WW_Fixed_IO))))
15951
15952 -- A special extra check, don't complain about a reference from within
15953 -- the Ada.Interrupts package itself!
15954
15955 and then not In_Same_Extended_Unit (N, Val)
15956 then
15957 Check_Restriction (No_Fixed_IO, Post_Node);
15958 end if;
15959
15960 -- Remaining checks are only done on source nodes. Note that we test
15961 -- for violation of No_Fixed_IO even on non-source nodes, because the
15962 -- cases for checking violations of this restriction are instantiations
15963 -- where the reference in the instance has Comes_From_Source False.
15964
15965 if not Comes_From_Source (N) then
15966 return;
15967 end if;
15968
15969 -- Check for violation of No_Abort_Statements, which is triggered by
15970 -- call to Ada.Task_Identification.Abort_Task.
15971
15972 if Restriction_Check_Required (No_Abort_Statements)
15973 and then (Is_RTE (Val, RE_Abort_Task))
15974
15975 -- A special extra check, don't complain about a reference from within
15976 -- the Ada.Task_Identification package itself!
15977
15978 and then not In_Same_Extended_Unit (N, Val)
15979 then
15980 Check_Restriction (No_Abort_Statements, Post_Node);
15981 end if;
15982
15983 if Val = Standard_Long_Long_Integer then
15984 Check_Restriction (No_Long_Long_Integers, Post_Node);
15985 end if;
15986
15987 -- Check for violation of No_Dynamic_Attachment
15988
15989 if Restriction_Check_Required (No_Dynamic_Attachment)
15990 and then RTU_Loaded (Ada_Interrupts)
15991 and then (Is_RTE (Val, RE_Is_Reserved) or else
15992 Is_RTE (Val, RE_Is_Attached) or else
15993 Is_RTE (Val, RE_Current_Handler) or else
15994 Is_RTE (Val, RE_Attach_Handler) or else
15995 Is_RTE (Val, RE_Exchange_Handler) or else
15996 Is_RTE (Val, RE_Detach_Handler) or else
15997 Is_RTE (Val, RE_Reference))
15998
15999 -- A special extra check, don't complain about a reference from within
16000 -- the Ada.Interrupts package itself!
16001
16002 and then not In_Same_Extended_Unit (N, Val)
16003 then
16004 Check_Restriction (No_Dynamic_Attachment, Post_Node);
16005 end if;
16006
16007 -- Check for No_Implementation_Identifiers
16008
16009 if Restriction_Check_Required (No_Implementation_Identifiers) then
16010
16011 -- We have an implementation defined entity if it is marked as
16012 -- implementation defined, or is defined in a package marked as
16013 -- implementation defined. However, library packages themselves
16014 -- are excluded (we don't want to flag Interfaces itself, just
16015 -- the entities within it).
16016
16017 if (Is_Implementation_Defined (Val)
16018 or else
16019 Is_Implementation_Defined (Scope (Val)))
16020 and then not (Ekind_In (Val, E_Package, E_Generic_Package)
16021 and then Is_Library_Level_Entity (Val))
16022 then
16023 Check_Restriction (No_Implementation_Identifiers, Post_Node);
16024 end if;
16025 end if;
16026
16027 -- Do the style check
16028
16029 if Style_Check
16030 and then not Suppress_Style_Checks (Val)
16031 and then not In_Instance
16032 then
16033 if Nkind (N) = N_Identifier then
16034 Nod := N;
16035 elsif Nkind (N) = N_Expanded_Name then
16036 Nod := Selector_Name (N);
16037 else
16038 return;
16039 end if;
16040
16041 -- A special situation arises for derived operations, where we want
16042 -- to do the check against the parent (since the Sloc of the derived
16043 -- operation points to the derived type declaration itself).
16044
16045 Val_Actual := Val;
16046 while not Comes_From_Source (Val_Actual)
16047 and then Nkind (Val_Actual) in N_Entity
16048 and then (Ekind (Val_Actual) = E_Enumeration_Literal
16049 or else Is_Subprogram (Val_Actual)
16050 or else Is_Generic_Subprogram (Val_Actual))
16051 and then Present (Alias (Val_Actual))
16052 loop
16053 Val_Actual := Alias (Val_Actual);
16054 end loop;
16055
16056 -- Renaming declarations for generic actuals do not come from source,
16057 -- and have a different name from that of the entity they rename, so
16058 -- there is no style check to perform here.
16059
16060 if Chars (Nod) = Chars (Val_Actual) then
16061 Style.Check_Identifier (Nod, Val_Actual);
16062 end if;
16063 end if;
16064
16065 Set_Entity (N, Val);
16066 end Set_Entity_With_Checks;
16067
16068 ------------------------
16069 -- Set_Name_Entity_Id --
16070 ------------------------
16071
16072 procedure Set_Name_Entity_Id (Id : Name_Id; Val : Entity_Id) is
16073 begin
16074 Set_Name_Table_Info (Id, Int (Val));
16075 end Set_Name_Entity_Id;
16076
16077 ---------------------
16078 -- Set_Next_Actual --
16079 ---------------------
16080
16081 procedure Set_Next_Actual (Ass1_Id : Node_Id; Ass2_Id : Node_Id) is
16082 begin
16083 if Nkind (Parent (Ass1_Id)) = N_Parameter_Association then
16084 Set_First_Named_Actual (Parent (Ass1_Id), Ass2_Id);
16085 end if;
16086 end Set_Next_Actual;
16087
16088 ----------------------------------
16089 -- Set_Optimize_Alignment_Flags --
16090 ----------------------------------
16091
16092 procedure Set_Optimize_Alignment_Flags (E : Entity_Id) is
16093 begin
16094 if Optimize_Alignment = 'S' then
16095 Set_Optimize_Alignment_Space (E);
16096 elsif Optimize_Alignment = 'T' then
16097 Set_Optimize_Alignment_Time (E);
16098 end if;
16099 end Set_Optimize_Alignment_Flags;
16100
16101 -----------------------
16102 -- Set_Public_Status --
16103 -----------------------
16104
16105 procedure Set_Public_Status (Id : Entity_Id) is
16106 S : constant Entity_Id := Current_Scope;
16107
16108 function Within_HSS_Or_If (E : Entity_Id) return Boolean;
16109 -- Determines if E is defined within handled statement sequence or
16110 -- an if statement, returns True if so, False otherwise.
16111
16112 ----------------------
16113 -- Within_HSS_Or_If --
16114 ----------------------
16115
16116 function Within_HSS_Or_If (E : Entity_Id) return Boolean is
16117 N : Node_Id;
16118 begin
16119 N := Declaration_Node (E);
16120 loop
16121 N := Parent (N);
16122
16123 if No (N) then
16124 return False;
16125
16126 elsif Nkind_In (N, N_Handled_Sequence_Of_Statements,
16127 N_If_Statement)
16128 then
16129 return True;
16130 end if;
16131 end loop;
16132 end Within_HSS_Or_If;
16133
16134 -- Start of processing for Set_Public_Status
16135
16136 begin
16137 -- Everything in the scope of Standard is public
16138
16139 if S = Standard_Standard then
16140 Set_Is_Public (Id);
16141
16142 -- Entity is definitely not public if enclosing scope is not public
16143
16144 elsif not Is_Public (S) then
16145 return;
16146
16147 -- An object or function declaration that occurs in a handled sequence
16148 -- of statements or within an if statement is the declaration for a
16149 -- temporary object or local subprogram generated by the expander. It
16150 -- never needs to be made public and furthermore, making it public can
16151 -- cause back end problems.
16152
16153 elsif Nkind_In (Parent (Id), N_Object_Declaration,
16154 N_Function_Specification)
16155 and then Within_HSS_Or_If (Id)
16156 then
16157 return;
16158
16159 -- Entities in public packages or records are public
16160
16161 elsif Ekind (S) = E_Package or Is_Record_Type (S) then
16162 Set_Is_Public (Id);
16163
16164 -- The bounds of an entry family declaration can generate object
16165 -- declarations that are visible to the back-end, e.g. in the
16166 -- the declaration of a composite type that contains tasks.
16167
16168 elsif Is_Concurrent_Type (S)
16169 and then not Has_Completion (S)
16170 and then Nkind (Parent (Id)) = N_Object_Declaration
16171 then
16172 Set_Is_Public (Id);
16173 end if;
16174 end Set_Public_Status;
16175
16176 -----------------------------
16177 -- Set_Referenced_Modified --
16178 -----------------------------
16179
16180 procedure Set_Referenced_Modified (N : Node_Id; Out_Param : Boolean) is
16181 Pref : Node_Id;
16182
16183 begin
16184 -- Deal with indexed or selected component where prefix is modified
16185
16186 if Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
16187 Pref := Prefix (N);
16188
16189 -- If prefix is access type, then it is the designated object that is
16190 -- being modified, which means we have no entity to set the flag on.
16191
16192 if No (Etype (Pref)) or else Is_Access_Type (Etype (Pref)) then
16193 return;
16194
16195 -- Otherwise chase the prefix
16196
16197 else
16198 Set_Referenced_Modified (Pref, Out_Param);
16199 end if;
16200
16201 -- Otherwise see if we have an entity name (only other case to process)
16202
16203 elsif Is_Entity_Name (N) and then Present (Entity (N)) then
16204 Set_Referenced_As_LHS (Entity (N), not Out_Param);
16205 Set_Referenced_As_Out_Parameter (Entity (N), Out_Param);
16206 end if;
16207 end Set_Referenced_Modified;
16208
16209 ----------------------------
16210 -- Set_Scope_Is_Transient --
16211 ----------------------------
16212
16213 procedure Set_Scope_Is_Transient (V : Boolean := True) is
16214 begin
16215 Scope_Stack.Table (Scope_Stack.Last).Is_Transient := V;
16216 end Set_Scope_Is_Transient;
16217
16218 -------------------
16219 -- Set_Size_Info --
16220 -------------------
16221
16222 procedure Set_Size_Info (T1, T2 : Entity_Id) is
16223 begin
16224 -- We copy Esize, but not RM_Size, since in general RM_Size is
16225 -- subtype specific and does not get inherited by all subtypes.
16226
16227 Set_Esize (T1, Esize (T2));
16228 Set_Has_Biased_Representation (T1, Has_Biased_Representation (T2));
16229
16230 if Is_Discrete_Or_Fixed_Point_Type (T1)
16231 and then
16232 Is_Discrete_Or_Fixed_Point_Type (T2)
16233 then
16234 Set_Is_Unsigned_Type (T1, Is_Unsigned_Type (T2));
16235 end if;
16236
16237 Set_Alignment (T1, Alignment (T2));
16238 end Set_Size_Info;
16239
16240 --------------------
16241 -- Static_Boolean --
16242 --------------------
16243
16244 function Static_Boolean (N : Node_Id) return Uint is
16245 begin
16246 Analyze_And_Resolve (N, Standard_Boolean);
16247
16248 if N = Error
16249 or else Error_Posted (N)
16250 or else Etype (N) = Any_Type
16251 then
16252 return No_Uint;
16253 end if;
16254
16255 if Is_Static_Expression (N) then
16256 if not Raises_Constraint_Error (N) then
16257 return Expr_Value (N);
16258 else
16259 return No_Uint;
16260 end if;
16261
16262 elsif Etype (N) = Any_Type then
16263 return No_Uint;
16264
16265 else
16266 Flag_Non_Static_Expr
16267 ("static boolean expression required here", N);
16268 return No_Uint;
16269 end if;
16270 end Static_Boolean;
16271
16272 --------------------
16273 -- Static_Integer --
16274 --------------------
16275
16276 function Static_Integer (N : Node_Id) return Uint is
16277 begin
16278 Analyze_And_Resolve (N, Any_Integer);
16279
16280 if N = Error
16281 or else Error_Posted (N)
16282 or else Etype (N) = Any_Type
16283 then
16284 return No_Uint;
16285 end if;
16286
16287 if Is_Static_Expression (N) then
16288 if not Raises_Constraint_Error (N) then
16289 return Expr_Value (N);
16290 else
16291 return No_Uint;
16292 end if;
16293
16294 elsif Etype (N) = Any_Type then
16295 return No_Uint;
16296
16297 else
16298 Flag_Non_Static_Expr
16299 ("static integer expression required here", N);
16300 return No_Uint;
16301 end if;
16302 end Static_Integer;
16303
16304 --------------------------
16305 -- Statically_Different --
16306 --------------------------
16307
16308 function Statically_Different (E1, E2 : Node_Id) return Boolean is
16309 R1 : constant Node_Id := Get_Referenced_Object (E1);
16310 R2 : constant Node_Id := Get_Referenced_Object (E2);
16311 begin
16312 return Is_Entity_Name (R1)
16313 and then Is_Entity_Name (R2)
16314 and then Entity (R1) /= Entity (R2)
16315 and then not Is_Formal (Entity (R1))
16316 and then not Is_Formal (Entity (R2));
16317 end Statically_Different;
16318
16319 --------------------------------------
16320 -- Subject_To_Loop_Entry_Attributes --
16321 --------------------------------------
16322
16323 function Subject_To_Loop_Entry_Attributes (N : Node_Id) return Boolean is
16324 Stmt : Node_Id;
16325
16326 begin
16327 Stmt := N;
16328
16329 -- The expansion mechanism transform a loop subject to at least one
16330 -- 'Loop_Entry attribute into a conditional block. Infinite loops lack
16331 -- the conditional part.
16332
16333 if Nkind_In (Stmt, N_Block_Statement, N_If_Statement)
16334 and then Nkind (Original_Node (N)) = N_Loop_Statement
16335 then
16336 Stmt := Original_Node (N);
16337 end if;
16338
16339 return
16340 Nkind (Stmt) = N_Loop_Statement
16341 and then Present (Identifier (Stmt))
16342 and then Present (Entity (Identifier (Stmt)))
16343 and then Has_Loop_Entry_Attributes (Entity (Identifier (Stmt)));
16344 end Subject_To_Loop_Entry_Attributes;
16345
16346 -----------------------------
16347 -- Subprogram_Access_Level --
16348 -----------------------------
16349
16350 function Subprogram_Access_Level (Subp : Entity_Id) return Uint is
16351 begin
16352 if Present (Alias (Subp)) then
16353 return Subprogram_Access_Level (Alias (Subp));
16354 else
16355 return Scope_Depth (Enclosing_Dynamic_Scope (Subp));
16356 end if;
16357 end Subprogram_Access_Level;
16358
16359 -------------------------------
16360 -- Support_Atomic_Primitives --
16361 -------------------------------
16362
16363 function Support_Atomic_Primitives (Typ : Entity_Id) return Boolean is
16364 Size : Int;
16365
16366 begin
16367 -- Verify the alignment of Typ is known
16368
16369 if not Known_Alignment (Typ) then
16370 return False;
16371 end if;
16372
16373 if Known_Static_Esize (Typ) then
16374 Size := UI_To_Int (Esize (Typ));
16375
16376 -- If the Esize (Object_Size) is unknown at compile time, look at the
16377 -- RM_Size (Value_Size) which may have been set by an explicit rep item.
16378
16379 elsif Known_Static_RM_Size (Typ) then
16380 Size := UI_To_Int (RM_Size (Typ));
16381
16382 -- Otherwise, the size is considered to be unknown.
16383
16384 else
16385 return False;
16386 end if;
16387
16388 -- Check that the size of the component is 8, 16, 32 or 64 bits and that
16389 -- Typ is properly aligned.
16390
16391 case Size is
16392 when 8 | 16 | 32 | 64 =>
16393 return Size = UI_To_Int (Alignment (Typ)) * 8;
16394 when others =>
16395 return False;
16396 end case;
16397 end Support_Atomic_Primitives;
16398
16399 -----------------
16400 -- Trace_Scope --
16401 -----------------
16402
16403 procedure Trace_Scope (N : Node_Id; E : Entity_Id; Msg : String) is
16404 begin
16405 if Debug_Flag_W then
16406 for J in 0 .. Scope_Stack.Last loop
16407 Write_Str (" ");
16408 end loop;
16409
16410 Write_Str (Msg);
16411 Write_Name (Chars (E));
16412 Write_Str (" from ");
16413 Write_Location (Sloc (N));
16414 Write_Eol;
16415 end if;
16416 end Trace_Scope;
16417
16418 -----------------------
16419 -- Transfer_Entities --
16420 -----------------------
16421
16422 procedure Transfer_Entities (From : Entity_Id; To : Entity_Id) is
16423 Ent : Entity_Id := First_Entity (From);
16424
16425 begin
16426 if No (Ent) then
16427 return;
16428 end if;
16429
16430 if (Last_Entity (To)) = Empty then
16431 Set_First_Entity (To, Ent);
16432 else
16433 Set_Next_Entity (Last_Entity (To), Ent);
16434 end if;
16435
16436 Set_Last_Entity (To, Last_Entity (From));
16437
16438 while Present (Ent) loop
16439 Set_Scope (Ent, To);
16440
16441 if not Is_Public (Ent) then
16442 Set_Public_Status (Ent);
16443
16444 if Is_Public (Ent)
16445 and then Ekind (Ent) = E_Record_Subtype
16446
16447 then
16448 -- The components of the propagated Itype must be public
16449 -- as well.
16450
16451 declare
16452 Comp : Entity_Id;
16453 begin
16454 Comp := First_Entity (Ent);
16455 while Present (Comp) loop
16456 Set_Is_Public (Comp);
16457 Next_Entity (Comp);
16458 end loop;
16459 end;
16460 end if;
16461 end if;
16462
16463 Next_Entity (Ent);
16464 end loop;
16465
16466 Set_First_Entity (From, Empty);
16467 Set_Last_Entity (From, Empty);
16468 end Transfer_Entities;
16469
16470 -----------------------
16471 -- Type_Access_Level --
16472 -----------------------
16473
16474 function Type_Access_Level (Typ : Entity_Id) return Uint is
16475 Btyp : Entity_Id;
16476
16477 begin
16478 Btyp := Base_Type (Typ);
16479
16480 -- Ada 2005 (AI-230): For most cases of anonymous access types, we
16481 -- simply use the level where the type is declared. This is true for
16482 -- stand-alone object declarations, and for anonymous access types
16483 -- associated with components the level is the same as that of the
16484 -- enclosing composite type. However, special treatment is needed for
16485 -- the cases of access parameters, return objects of an anonymous access
16486 -- type, and, in Ada 95, access discriminants of limited types.
16487
16488 if Is_Access_Type (Btyp) then
16489 if Ekind (Btyp) = E_Anonymous_Access_Type then
16490
16491 -- If the type is a nonlocal anonymous access type (such as for
16492 -- an access parameter) we treat it as being declared at the
16493 -- library level to ensure that names such as X.all'access don't
16494 -- fail static accessibility checks.
16495
16496 if not Is_Local_Anonymous_Access (Typ) then
16497 return Scope_Depth (Standard_Standard);
16498
16499 -- If this is a return object, the accessibility level is that of
16500 -- the result subtype of the enclosing function. The test here is
16501 -- little complicated, because we have to account for extended
16502 -- return statements that have been rewritten as blocks, in which
16503 -- case we have to find and the Is_Return_Object attribute of the
16504 -- itype's associated object. It would be nice to find a way to
16505 -- simplify this test, but it doesn't seem worthwhile to add a new
16506 -- flag just for purposes of this test. ???
16507
16508 elsif Ekind (Scope (Btyp)) = E_Return_Statement
16509 or else
16510 (Is_Itype (Btyp)
16511 and then Nkind (Associated_Node_For_Itype (Btyp)) =
16512 N_Object_Declaration
16513 and then Is_Return_Object
16514 (Defining_Identifier
16515 (Associated_Node_For_Itype (Btyp))))
16516 then
16517 declare
16518 Scop : Entity_Id;
16519
16520 begin
16521 Scop := Scope (Scope (Btyp));
16522 while Present (Scop) loop
16523 exit when Ekind (Scop) = E_Function;
16524 Scop := Scope (Scop);
16525 end loop;
16526
16527 -- Treat the return object's type as having the level of the
16528 -- function's result subtype (as per RM05-6.5(5.3/2)).
16529
16530 return Type_Access_Level (Etype (Scop));
16531 end;
16532 end if;
16533 end if;
16534
16535 Btyp := Root_Type (Btyp);
16536
16537 -- The accessibility level of anonymous access types associated with
16538 -- discriminants is that of the current instance of the type, and
16539 -- that's deeper than the type itself (AARM 3.10.2 (12.3.21)).
16540
16541 -- AI-402: access discriminants have accessibility based on the
16542 -- object rather than the type in Ada 2005, so the above paragraph
16543 -- doesn't apply.
16544
16545 -- ??? Needs completion with rules from AI-416
16546
16547 if Ada_Version <= Ada_95
16548 and then Ekind (Typ) = E_Anonymous_Access_Type
16549 and then Present (Associated_Node_For_Itype (Typ))
16550 and then Nkind (Associated_Node_For_Itype (Typ)) =
16551 N_Discriminant_Specification
16552 then
16553 return Scope_Depth (Enclosing_Dynamic_Scope (Btyp)) + 1;
16554 end if;
16555 end if;
16556
16557 -- Return library level for a generic formal type. This is done because
16558 -- RM(10.3.2) says that "The statically deeper relationship does not
16559 -- apply to ... a descendant of a generic formal type". Rather than
16560 -- checking at each point where a static accessibility check is
16561 -- performed to see if we are dealing with a formal type, this rule is
16562 -- implemented by having Type_Access_Level and Deepest_Type_Access_Level
16563 -- return extreme values for a formal type; Deepest_Type_Access_Level
16564 -- returns Int'Last. By calling the appropriate function from among the
16565 -- two, we ensure that the static accessibility check will pass if we
16566 -- happen to run into a formal type. More specifically, we should call
16567 -- Deepest_Type_Access_Level instead of Type_Access_Level whenever the
16568 -- call occurs as part of a static accessibility check and the error
16569 -- case is the case where the type's level is too shallow (as opposed
16570 -- to too deep).
16571
16572 if Is_Generic_Type (Root_Type (Btyp)) then
16573 return Scope_Depth (Standard_Standard);
16574 end if;
16575
16576 return Scope_Depth (Enclosing_Dynamic_Scope (Btyp));
16577 end Type_Access_Level;
16578
16579 ------------------------------------
16580 -- Type_Without_Stream_Operation --
16581 ------------------------------------
16582
16583 function Type_Without_Stream_Operation
16584 (T : Entity_Id;
16585 Op : TSS_Name_Type := TSS_Null) return Entity_Id
16586 is
16587 BT : constant Entity_Id := Base_Type (T);
16588 Op_Missing : Boolean;
16589
16590 begin
16591 if not Restriction_Active (No_Default_Stream_Attributes) then
16592 return Empty;
16593 end if;
16594
16595 if Is_Elementary_Type (T) then
16596 if Op = TSS_Null then
16597 Op_Missing :=
16598 No (TSS (BT, TSS_Stream_Read))
16599 or else No (TSS (BT, TSS_Stream_Write));
16600
16601 else
16602 Op_Missing := No (TSS (BT, Op));
16603 end if;
16604
16605 if Op_Missing then
16606 return T;
16607 else
16608 return Empty;
16609 end if;
16610
16611 elsif Is_Array_Type (T) then
16612 return Type_Without_Stream_Operation (Component_Type (T), Op);
16613
16614 elsif Is_Record_Type (T) then
16615 declare
16616 Comp : Entity_Id;
16617 C_Typ : Entity_Id;
16618
16619 begin
16620 Comp := First_Component (T);
16621 while Present (Comp) loop
16622 C_Typ := Type_Without_Stream_Operation (Etype (Comp), Op);
16623
16624 if Present (C_Typ) then
16625 return C_Typ;
16626 end if;
16627
16628 Next_Component (Comp);
16629 end loop;
16630
16631 return Empty;
16632 end;
16633
16634 elsif Is_Private_Type (T)
16635 and then Present (Full_View (T))
16636 then
16637 return Type_Without_Stream_Operation (Full_View (T), Op);
16638 else
16639 return Empty;
16640 end if;
16641 end Type_Without_Stream_Operation;
16642
16643 ----------------------------
16644 -- Unique_Defining_Entity --
16645 ----------------------------
16646
16647 function Unique_Defining_Entity (N : Node_Id) return Entity_Id is
16648 begin
16649 return Unique_Entity (Defining_Entity (N));
16650 end Unique_Defining_Entity;
16651
16652 -------------------
16653 -- Unique_Entity --
16654 -------------------
16655
16656 function Unique_Entity (E : Entity_Id) return Entity_Id is
16657 U : Entity_Id := E;
16658 P : Node_Id;
16659
16660 begin
16661 case Ekind (E) is
16662 when E_Constant =>
16663 if Present (Full_View (E)) then
16664 U := Full_View (E);
16665 end if;
16666
16667 when Type_Kind =>
16668 if Present (Full_View (E)) then
16669 U := Full_View (E);
16670 end if;
16671
16672 when E_Package_Body =>
16673 P := Parent (E);
16674
16675 if Nkind (P) = N_Defining_Program_Unit_Name then
16676 P := Parent (P);
16677 end if;
16678
16679 U := Corresponding_Spec (P);
16680
16681 when E_Subprogram_Body =>
16682 P := Parent (E);
16683
16684 if Nkind (P) = N_Defining_Program_Unit_Name then
16685 P := Parent (P);
16686 end if;
16687
16688 P := Parent (P);
16689
16690 if Nkind (P) = N_Subprogram_Body_Stub then
16691 if Present (Library_Unit (P)) then
16692
16693 -- Get to the function or procedure (generic) entity through
16694 -- the body entity.
16695
16696 U :=
16697 Unique_Entity (Defining_Entity (Get_Body_From_Stub (P)));
16698 end if;
16699 else
16700 U := Corresponding_Spec (P);
16701 end if;
16702
16703 when Formal_Kind =>
16704 if Present (Spec_Entity (E)) then
16705 U := Spec_Entity (E);
16706 end if;
16707
16708 when others =>
16709 null;
16710 end case;
16711
16712 return U;
16713 end Unique_Entity;
16714
16715 -----------------
16716 -- Unique_Name --
16717 -----------------
16718
16719 function Unique_Name (E : Entity_Id) return String is
16720
16721 -- Names of E_Subprogram_Body or E_Package_Body entities are not
16722 -- reliable, as they may not include the overloading suffix. Instead,
16723 -- when looking for the name of E or one of its enclosing scope, we get
16724 -- the name of the corresponding Unique_Entity.
16725
16726 function Get_Scoped_Name (E : Entity_Id) return String;
16727 -- Return the name of E prefixed by all the names of the scopes to which
16728 -- E belongs, except for Standard.
16729
16730 ---------------------
16731 -- Get_Scoped_Name --
16732 ---------------------
16733
16734 function Get_Scoped_Name (E : Entity_Id) return String is
16735 Name : constant String := Get_Name_String (Chars (E));
16736 begin
16737 if Has_Fully_Qualified_Name (E)
16738 or else Scope (E) = Standard_Standard
16739 then
16740 return Name;
16741 else
16742 return Get_Scoped_Name (Unique_Entity (Scope (E))) & "__" & Name;
16743 end if;
16744 end Get_Scoped_Name;
16745
16746 -- Start of processing for Unique_Name
16747
16748 begin
16749 if E = Standard_Standard then
16750 return Get_Name_String (Name_Standard);
16751
16752 elsif Scope (E) = Standard_Standard
16753 and then not (Ekind (E) = E_Package or else Is_Subprogram (E))
16754 then
16755 return Get_Name_String (Name_Standard) & "__" &
16756 Get_Name_String (Chars (E));
16757
16758 elsif Ekind (E) = E_Enumeration_Literal then
16759 return Unique_Name (Etype (E)) & "__" & Get_Name_String (Chars (E));
16760
16761 else
16762 return Get_Scoped_Name (Unique_Entity (E));
16763 end if;
16764 end Unique_Name;
16765
16766 ---------------------
16767 -- Unit_Is_Visible --
16768 ---------------------
16769
16770 function Unit_Is_Visible (U : Entity_Id) return Boolean is
16771 Curr : constant Node_Id := Cunit (Current_Sem_Unit);
16772 Curr_Entity : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
16773
16774 function Unit_In_Parent_Context (Par_Unit : Node_Id) return Boolean;
16775 -- For a child unit, check whether unit appears in a with_clause
16776 -- of a parent.
16777
16778 function Unit_In_Context (Comp_Unit : Node_Id) return Boolean;
16779 -- Scan the context clause of one compilation unit looking for a
16780 -- with_clause for the unit in question.
16781
16782 ----------------------------
16783 -- Unit_In_Parent_Context --
16784 ----------------------------
16785
16786 function Unit_In_Parent_Context (Par_Unit : Node_Id) return Boolean is
16787 begin
16788 if Unit_In_Context (Par_Unit) then
16789 return True;
16790
16791 elsif Is_Child_Unit (Defining_Entity (Unit (Par_Unit))) then
16792 return Unit_In_Parent_Context (Parent_Spec (Unit (Par_Unit)));
16793
16794 else
16795 return False;
16796 end if;
16797 end Unit_In_Parent_Context;
16798
16799 ---------------------
16800 -- Unit_In_Context --
16801 ---------------------
16802
16803 function Unit_In_Context (Comp_Unit : Node_Id) return Boolean is
16804 Clause : Node_Id;
16805
16806 begin
16807 Clause := First (Context_Items (Comp_Unit));
16808 while Present (Clause) loop
16809 if Nkind (Clause) = N_With_Clause then
16810 if Library_Unit (Clause) = U then
16811 return True;
16812
16813 -- The with_clause may denote a renaming of the unit we are
16814 -- looking for, eg. Text_IO which renames Ada.Text_IO.
16815
16816 elsif
16817 Renamed_Entity (Entity (Name (Clause))) =
16818 Defining_Entity (Unit (U))
16819 then
16820 return True;
16821 end if;
16822 end if;
16823
16824 Next (Clause);
16825 end loop;
16826
16827 return False;
16828 end Unit_In_Context;
16829
16830 -- Start of processing for Unit_Is_Visible
16831
16832 begin
16833 -- The currrent unit is directly visible
16834
16835 if Curr = U then
16836 return True;
16837
16838 elsif Unit_In_Context (Curr) then
16839 return True;
16840
16841 -- If the current unit is a body, check the context of the spec
16842
16843 elsif Nkind (Unit (Curr)) = N_Package_Body
16844 or else
16845 (Nkind (Unit (Curr)) = N_Subprogram_Body
16846 and then not Acts_As_Spec (Unit (Curr)))
16847 then
16848 if Unit_In_Context (Library_Unit (Curr)) then
16849 return True;
16850 end if;
16851 end if;
16852
16853 -- If the spec is a child unit, examine the parents
16854
16855 if Is_Child_Unit (Curr_Entity) then
16856 if Nkind (Unit (Curr)) in N_Unit_Body then
16857 return
16858 Unit_In_Parent_Context
16859 (Parent_Spec (Unit (Library_Unit (Curr))));
16860 else
16861 return Unit_In_Parent_Context (Parent_Spec (Unit (Curr)));
16862 end if;
16863
16864 else
16865 return False;
16866 end if;
16867 end Unit_Is_Visible;
16868
16869 ------------------------------
16870 -- Universal_Interpretation --
16871 ------------------------------
16872
16873 function Universal_Interpretation (Opnd : Node_Id) return Entity_Id is
16874 Index : Interp_Index;
16875 It : Interp;
16876
16877 begin
16878 -- The argument may be a formal parameter of an operator or subprogram
16879 -- with multiple interpretations, or else an expression for an actual.
16880
16881 if Nkind (Opnd) = N_Defining_Identifier
16882 or else not Is_Overloaded (Opnd)
16883 then
16884 if Etype (Opnd) = Universal_Integer
16885 or else Etype (Opnd) = Universal_Real
16886 then
16887 return Etype (Opnd);
16888 else
16889 return Empty;
16890 end if;
16891
16892 else
16893 Get_First_Interp (Opnd, Index, It);
16894 while Present (It.Typ) loop
16895 if It.Typ = Universal_Integer
16896 or else It.Typ = Universal_Real
16897 then
16898 return It.Typ;
16899 end if;
16900
16901 Get_Next_Interp (Index, It);
16902 end loop;
16903
16904 return Empty;
16905 end if;
16906 end Universal_Interpretation;
16907
16908 ---------------
16909 -- Unqualify --
16910 ---------------
16911
16912 function Unqualify (Expr : Node_Id) return Node_Id is
16913 begin
16914 -- Recurse to handle unlikely case of multiple levels of qualification
16915
16916 if Nkind (Expr) = N_Qualified_Expression then
16917 return Unqualify (Expression (Expr));
16918
16919 -- Normal case, not a qualified expression
16920
16921 else
16922 return Expr;
16923 end if;
16924 end Unqualify;
16925
16926 -----------------------
16927 -- Visible_Ancestors --
16928 -----------------------
16929
16930 function Visible_Ancestors (Typ : Entity_Id) return Elist_Id is
16931 List_1 : Elist_Id;
16932 List_2 : Elist_Id;
16933 Elmt : Elmt_Id;
16934
16935 begin
16936 pragma Assert (Is_Record_Type (Typ)
16937 and then Is_Tagged_Type (Typ));
16938
16939 -- Collect all the parents and progenitors of Typ. If the full-view of
16940 -- private parents and progenitors is available then it is used to
16941 -- generate the list of visible ancestors; otherwise their partial
16942 -- view is added to the resulting list.
16943
16944 Collect_Parents
16945 (T => Typ,
16946 List => List_1,
16947 Use_Full_View => True);
16948
16949 Collect_Interfaces
16950 (T => Typ,
16951 Ifaces_List => List_2,
16952 Exclude_Parents => True,
16953 Use_Full_View => True);
16954
16955 -- Join the two lists. Avoid duplications because an interface may
16956 -- simultaneously be parent and progenitor of a type.
16957
16958 Elmt := First_Elmt (List_2);
16959 while Present (Elmt) loop
16960 Append_Unique_Elmt (Node (Elmt), List_1);
16961 Next_Elmt (Elmt);
16962 end loop;
16963
16964 return List_1;
16965 end Visible_Ancestors;
16966
16967 ----------------------
16968 -- Within_Init_Proc --
16969 ----------------------
16970
16971 function Within_Init_Proc return Boolean is
16972 S : Entity_Id;
16973
16974 begin
16975 S := Current_Scope;
16976 while not Is_Overloadable (S) loop
16977 if S = Standard_Standard then
16978 return False;
16979 else
16980 S := Scope (S);
16981 end if;
16982 end loop;
16983
16984 return Is_Init_Proc (S);
16985 end Within_Init_Proc;
16986
16987 ------------------
16988 -- Within_Scope --
16989 ------------------
16990
16991 function Within_Scope (E : Entity_Id; S : Entity_Id) return Boolean is
16992 SE : Entity_Id;
16993 begin
16994 SE := Scope (E);
16995 loop
16996 if SE = S then
16997 return True;
16998 elsif SE = Standard_Standard then
16999 return False;
17000 else
17001 SE := Scope (SE);
17002 end if;
17003 end loop;
17004 end Within_Scope;
17005
17006 ----------------
17007 -- Wrong_Type --
17008 ----------------
17009
17010 procedure Wrong_Type (Expr : Node_Id; Expected_Type : Entity_Id) is
17011 Found_Type : constant Entity_Id := First_Subtype (Etype (Expr));
17012 Expec_Type : constant Entity_Id := First_Subtype (Expected_Type);
17013
17014 Matching_Field : Entity_Id;
17015 -- Entity to give a more precise suggestion on how to write a one-
17016 -- element positional aggregate.
17017
17018 function Has_One_Matching_Field return Boolean;
17019 -- Determines if Expec_Type is a record type with a single component or
17020 -- discriminant whose type matches the found type or is one dimensional
17021 -- array whose component type matches the found type. In the case of
17022 -- one discriminant, we ignore the variant parts. That's not accurate,
17023 -- but good enough for the warning.
17024
17025 ----------------------------
17026 -- Has_One_Matching_Field --
17027 ----------------------------
17028
17029 function Has_One_Matching_Field return Boolean is
17030 E : Entity_Id;
17031
17032 begin
17033 Matching_Field := Empty;
17034
17035 if Is_Array_Type (Expec_Type)
17036 and then Number_Dimensions (Expec_Type) = 1
17037 and then
17038 Covers (Etype (Component_Type (Expec_Type)), Found_Type)
17039 then
17040 -- Use type name if available. This excludes multidimensional
17041 -- arrays and anonymous arrays.
17042
17043 if Comes_From_Source (Expec_Type) then
17044 Matching_Field := Expec_Type;
17045
17046 -- For an assignment, use name of target
17047
17048 elsif Nkind (Parent (Expr)) = N_Assignment_Statement
17049 and then Is_Entity_Name (Name (Parent (Expr)))
17050 then
17051 Matching_Field := Entity (Name (Parent (Expr)));
17052 end if;
17053
17054 return True;
17055
17056 elsif not Is_Record_Type (Expec_Type) then
17057 return False;
17058
17059 else
17060 E := First_Entity (Expec_Type);
17061 loop
17062 if No (E) then
17063 return False;
17064
17065 elsif not Ekind_In (E, E_Discriminant, E_Component)
17066 or else Nam_In (Chars (E), Name_uTag, Name_uParent)
17067 then
17068 Next_Entity (E);
17069
17070 else
17071 exit;
17072 end if;
17073 end loop;
17074
17075 if not Covers (Etype (E), Found_Type) then
17076 return False;
17077
17078 elsif Present (Next_Entity (E))
17079 and then (Ekind (E) = E_Component
17080 or else Ekind (Next_Entity (E)) = E_Discriminant)
17081 then
17082 return False;
17083
17084 else
17085 Matching_Field := E;
17086 return True;
17087 end if;
17088 end if;
17089 end Has_One_Matching_Field;
17090
17091 -- Start of processing for Wrong_Type
17092
17093 begin
17094 -- Don't output message if either type is Any_Type, or if a message
17095 -- has already been posted for this node. We need to do the latter
17096 -- check explicitly (it is ordinarily done in Errout), because we
17097 -- are using ! to force the output of the error messages.
17098
17099 if Expec_Type = Any_Type
17100 or else Found_Type = Any_Type
17101 or else Error_Posted (Expr)
17102 then
17103 return;
17104
17105 -- If one of the types is a Taft-Amendment type and the other it its
17106 -- completion, it must be an illegal use of a TAT in the spec, for
17107 -- which an error was already emitted. Avoid cascaded errors.
17108
17109 elsif Is_Incomplete_Type (Expec_Type)
17110 and then Has_Completion_In_Body (Expec_Type)
17111 and then Full_View (Expec_Type) = Etype (Expr)
17112 then
17113 return;
17114
17115 elsif Is_Incomplete_Type (Etype (Expr))
17116 and then Has_Completion_In_Body (Etype (Expr))
17117 and then Full_View (Etype (Expr)) = Expec_Type
17118 then
17119 return;
17120
17121 -- In an instance, there is an ongoing problem with completion of
17122 -- type derived from private types. Their structure is what Gigi
17123 -- expects, but the Etype is the parent type rather than the
17124 -- derived private type itself. Do not flag error in this case. The
17125 -- private completion is an entity without a parent, like an Itype.
17126 -- Similarly, full and partial views may be incorrect in the instance.
17127 -- There is no simple way to insure that it is consistent ???
17128
17129 elsif In_Instance then
17130 if Etype (Etype (Expr)) = Etype (Expected_Type)
17131 and then
17132 (Has_Private_Declaration (Expected_Type)
17133 or else Has_Private_Declaration (Etype (Expr)))
17134 and then No (Parent (Expected_Type))
17135 then
17136 return;
17137 end if;
17138 end if;
17139
17140 -- An interesting special check. If the expression is parenthesized
17141 -- and its type corresponds to the type of the sole component of the
17142 -- expected record type, or to the component type of the expected one
17143 -- dimensional array type, then assume we have a bad aggregate attempt.
17144
17145 if Nkind (Expr) in N_Subexpr
17146 and then Paren_Count (Expr) /= 0
17147 and then Has_One_Matching_Field
17148 then
17149 Error_Msg_N ("positional aggregate cannot have one component", Expr);
17150 if Present (Matching_Field) then
17151 if Is_Array_Type (Expec_Type) then
17152 Error_Msg_NE
17153 ("\write instead `&''First ='> ...`", Expr, Matching_Field);
17154
17155 else
17156 Error_Msg_NE
17157 ("\write instead `& ='> ...`", Expr, Matching_Field);
17158 end if;
17159 end if;
17160
17161 -- Another special check, if we are looking for a pool-specific access
17162 -- type and we found an E_Access_Attribute_Type, then we have the case
17163 -- of an Access attribute being used in a context which needs a pool-
17164 -- specific type, which is never allowed. The one extra check we make
17165 -- is that the expected designated type covers the Found_Type.
17166
17167 elsif Is_Access_Type (Expec_Type)
17168 and then Ekind (Found_Type) = E_Access_Attribute_Type
17169 and then Ekind (Base_Type (Expec_Type)) /= E_General_Access_Type
17170 and then Ekind (Base_Type (Expec_Type)) /= E_Anonymous_Access_Type
17171 and then Covers
17172 (Designated_Type (Expec_Type), Designated_Type (Found_Type))
17173 then
17174 Error_Msg_N -- CODEFIX
17175 ("result must be general access type!", Expr);
17176 Error_Msg_NE -- CODEFIX
17177 ("add ALL to }!", Expr, Expec_Type);
17178
17179 -- Another special check, if the expected type is an integer type,
17180 -- but the expression is of type System.Address, and the parent is
17181 -- an addition or subtraction operation whose left operand is the
17182 -- expression in question and whose right operand is of an integral
17183 -- type, then this is an attempt at address arithmetic, so give
17184 -- appropriate message.
17185
17186 elsif Is_Integer_Type (Expec_Type)
17187 and then Is_RTE (Found_Type, RE_Address)
17188 and then (Nkind (Parent (Expr)) = N_Op_Add
17189 or else
17190 Nkind (Parent (Expr)) = N_Op_Subtract)
17191 and then Expr = Left_Opnd (Parent (Expr))
17192 and then Is_Integer_Type (Etype (Right_Opnd (Parent (Expr))))
17193 then
17194 Error_Msg_N
17195 ("address arithmetic not predefined in package System",
17196 Parent (Expr));
17197 Error_Msg_N
17198 ("\possible missing with/use of System.Storage_Elements",
17199 Parent (Expr));
17200 return;
17201
17202 -- If the expected type is an anonymous access type, as for access
17203 -- parameters and discriminants, the error is on the designated types.
17204
17205 elsif Ekind (Expec_Type) = E_Anonymous_Access_Type then
17206 if Comes_From_Source (Expec_Type) then
17207 Error_Msg_NE ("expected}!", Expr, Expec_Type);
17208 else
17209 Error_Msg_NE
17210 ("expected an access type with designated}",
17211 Expr, Designated_Type (Expec_Type));
17212 end if;
17213
17214 if Is_Access_Type (Found_Type)
17215 and then not Comes_From_Source (Found_Type)
17216 then
17217 Error_Msg_NE
17218 ("\\found an access type with designated}!",
17219 Expr, Designated_Type (Found_Type));
17220 else
17221 if From_Limited_With (Found_Type) then
17222 Error_Msg_NE ("\\found incomplete}!", Expr, Found_Type);
17223 Error_Msg_Qual_Level := 99;
17224 Error_Msg_NE -- CODEFIX
17225 ("\\missing `WITH &;", Expr, Scope (Found_Type));
17226 Error_Msg_Qual_Level := 0;
17227 else
17228 Error_Msg_NE ("found}!", Expr, Found_Type);
17229 end if;
17230 end if;
17231
17232 -- Normal case of one type found, some other type expected
17233
17234 else
17235 -- If the names of the two types are the same, see if some number
17236 -- of levels of qualification will help. Don't try more than three
17237 -- levels, and if we get to standard, it's no use (and probably
17238 -- represents an error in the compiler) Also do not bother with
17239 -- internal scope names.
17240
17241 declare
17242 Expec_Scope : Entity_Id;
17243 Found_Scope : Entity_Id;
17244
17245 begin
17246 Expec_Scope := Expec_Type;
17247 Found_Scope := Found_Type;
17248
17249 for Levels in Int range 0 .. 3 loop
17250 if Chars (Expec_Scope) /= Chars (Found_Scope) then
17251 Error_Msg_Qual_Level := Levels;
17252 exit;
17253 end if;
17254
17255 Expec_Scope := Scope (Expec_Scope);
17256 Found_Scope := Scope (Found_Scope);
17257
17258 exit when Expec_Scope = Standard_Standard
17259 or else Found_Scope = Standard_Standard
17260 or else not Comes_From_Source (Expec_Scope)
17261 or else not Comes_From_Source (Found_Scope);
17262 end loop;
17263 end;
17264
17265 if Is_Record_Type (Expec_Type)
17266 and then Present (Corresponding_Remote_Type (Expec_Type))
17267 then
17268 Error_Msg_NE ("expected}!", Expr,
17269 Corresponding_Remote_Type (Expec_Type));
17270 else
17271 Error_Msg_NE ("expected}!", Expr, Expec_Type);
17272 end if;
17273
17274 if Is_Entity_Name (Expr)
17275 and then Is_Package_Or_Generic_Package (Entity (Expr))
17276 then
17277 Error_Msg_N ("\\found package name!", Expr);
17278
17279 elsif Is_Entity_Name (Expr)
17280 and then
17281 (Ekind (Entity (Expr)) = E_Procedure
17282 or else
17283 Ekind (Entity (Expr)) = E_Generic_Procedure)
17284 then
17285 if Ekind (Expec_Type) = E_Access_Subprogram_Type then
17286 Error_Msg_N
17287 ("found procedure name, possibly missing Access attribute!",
17288 Expr);
17289 else
17290 Error_Msg_N
17291 ("\\found procedure name instead of function!", Expr);
17292 end if;
17293
17294 elsif Nkind (Expr) = N_Function_Call
17295 and then Ekind (Expec_Type) = E_Access_Subprogram_Type
17296 and then Etype (Designated_Type (Expec_Type)) = Etype (Expr)
17297 and then No (Parameter_Associations (Expr))
17298 then
17299 Error_Msg_N
17300 ("found function name, possibly missing Access attribute!",
17301 Expr);
17302
17303 -- Catch common error: a prefix or infix operator which is not
17304 -- directly visible because the type isn't.
17305
17306 elsif Nkind (Expr) in N_Op
17307 and then Is_Overloaded (Expr)
17308 and then not Is_Immediately_Visible (Expec_Type)
17309 and then not Is_Potentially_Use_Visible (Expec_Type)
17310 and then not In_Use (Expec_Type)
17311 and then Has_Compatible_Type (Right_Opnd (Expr), Expec_Type)
17312 then
17313 Error_Msg_N
17314 ("operator of the type is not directly visible!", Expr);
17315
17316 elsif Ekind (Found_Type) = E_Void
17317 and then Present (Parent (Found_Type))
17318 and then Nkind (Parent (Found_Type)) = N_Full_Type_Declaration
17319 then
17320 Error_Msg_NE ("\\found premature usage of}!", Expr, Found_Type);
17321
17322 else
17323 Error_Msg_NE ("\\found}!", Expr, Found_Type);
17324 end if;
17325
17326 -- A special check for cases like M1 and M2 = 0 where M1 and M2 are
17327 -- of the same modular type, and (M1 and M2) = 0 was intended.
17328
17329 if Expec_Type = Standard_Boolean
17330 and then Is_Modular_Integer_Type (Found_Type)
17331 and then Nkind_In (Parent (Expr), N_Op_And, N_Op_Or, N_Op_Xor)
17332 and then Nkind (Right_Opnd (Parent (Expr))) in N_Op_Compare
17333 then
17334 declare
17335 Op : constant Node_Id := Right_Opnd (Parent (Expr));
17336 L : constant Node_Id := Left_Opnd (Op);
17337 R : constant Node_Id := Right_Opnd (Op);
17338 begin
17339 -- The case for the message is when the left operand of the
17340 -- comparison is the same modular type, or when it is an
17341 -- integer literal (or other universal integer expression),
17342 -- which would have been typed as the modular type if the
17343 -- parens had been there.
17344
17345 if (Etype (L) = Found_Type
17346 or else
17347 Etype (L) = Universal_Integer)
17348 and then Is_Integer_Type (Etype (R))
17349 then
17350 Error_Msg_N
17351 ("\\possible missing parens for modular operation", Expr);
17352 end if;
17353 end;
17354 end if;
17355
17356 -- Reset error message qualification indication
17357
17358 Error_Msg_Qual_Level := 0;
17359 end if;
17360 end Wrong_Type;
17361
17362 end Sem_Util;