]> gcc.gnu.org Git - gcc.git/blob - gcc/ada/sem_util.adb
[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_Ch13; use Sem_Ch13;
52 with Sem_Disp; use Sem_Disp;
53 with Sem_Eval; use Sem_Eval;
54 with Sem_Prag; use Sem_Prag;
55 with Sem_Res; use Sem_Res;
56 with Sem_Warn; use Sem_Warn;
57 with Sem_Type; use Sem_Type;
58 with Sinfo; use Sinfo;
59 with Sinput; use Sinput;
60 with Stand; use Stand;
61 with Style;
62 with Stringt; use Stringt;
63 with Targparm; use Targparm;
64 with Tbuild; use Tbuild;
65 with Ttypes; use Ttypes;
66 with Uname; use Uname;
67
68 with GNAT.HTable; use GNAT.HTable;
69
70 package body Sem_Util is
71
72 ----------------------------------------
73 -- Global_Variables for New_Copy_Tree --
74 ----------------------------------------
75
76 -- These global variables are used by New_Copy_Tree. See description of the
77 -- body of this subprogram for details. Global variables can be safely used
78 -- by New_Copy_Tree, since there is no case of a recursive call from the
79 -- processing inside New_Copy_Tree.
80
81 NCT_Hash_Threshold : constant := 20;
82 -- If there are more than this number of pairs of entries in the map, then
83 -- Hash_Tables_Used will be set, and the hash tables will be initialized
84 -- and used for the searches.
85
86 NCT_Hash_Tables_Used : Boolean := False;
87 -- Set to True if hash tables are in use
88
89 NCT_Table_Entries : Nat := 0;
90 -- Count entries in table to see if threshold is reached
91
92 NCT_Hash_Table_Setup : Boolean := False;
93 -- Set to True if hash table contains data. We set this True if we setup
94 -- the hash table with data, and leave it set permanently from then on,
95 -- this is a signal that second and subsequent users of the hash table
96 -- must clear the old entries before reuse.
97
98 subtype NCT_Header_Num is Int range 0 .. 511;
99 -- Defines range of headers in hash tables (512 headers)
100
101 -----------------------
102 -- Local Subprograms --
103 -----------------------
104
105 function Build_Component_Subtype
106 (C : List_Id;
107 Loc : Source_Ptr;
108 T : Entity_Id) return Node_Id;
109 -- This function builds the subtype for Build_Actual_Subtype_Of_Component
110 -- and Build_Discriminal_Subtype_Of_Component. C is a list of constraints,
111 -- Loc is the source location, T is the original subtype.
112
113 function Is_Fully_Initialized_Variant (Typ : Entity_Id) return Boolean;
114 -- Subsidiary to Is_Fully_Initialized_Type. For an unconstrained type
115 -- with discriminants whose default values are static, examine only the
116 -- components in the selected variant to determine whether all of them
117 -- have a default.
118
119 function Has_Enabled_Property
120 (Item_Id : Entity_Id;
121 Property : Name_Id) return Boolean;
122 -- Subsidiary to routines Async_xxx_Enabled and Effective_xxx_Enabled.
123 -- Determine whether an abstract state or a variable denoted by entity
124 -- Item_Id has enabled property Property.
125
126 function Has_Null_Extension (T : Entity_Id) return Boolean;
127 -- T is a derived tagged type. Check whether the type extension is null.
128 -- If the parent type is fully initialized, T can be treated as such.
129
130 ------------------------------
131 -- Abstract_Interface_List --
132 ------------------------------
133
134 function Abstract_Interface_List (Typ : Entity_Id) return List_Id is
135 Nod : Node_Id;
136
137 begin
138 if Is_Concurrent_Type (Typ) then
139
140 -- If we are dealing with a synchronized subtype, go to the base
141 -- type, whose declaration has the interface list.
142
143 -- Shouldn't this be Declaration_Node???
144
145 Nod := Parent (Base_Type (Typ));
146
147 if Nkind (Nod) = N_Full_Type_Declaration then
148 return Empty_List;
149 end if;
150
151 elsif Ekind (Typ) = E_Record_Type_With_Private then
152 if Nkind (Parent (Typ)) = N_Full_Type_Declaration then
153 Nod := Type_Definition (Parent (Typ));
154
155 elsif Nkind (Parent (Typ)) = N_Private_Type_Declaration then
156 if Present (Full_View (Typ))
157 and then
158 Nkind (Parent (Full_View (Typ))) = N_Full_Type_Declaration
159 then
160 Nod := Type_Definition (Parent (Full_View (Typ)));
161
162 -- If the full-view is not available we cannot do anything else
163 -- here (the source has errors).
164
165 else
166 return Empty_List;
167 end if;
168
169 -- Support for generic formals with interfaces is still missing ???
170
171 elsif Nkind (Parent (Typ)) = N_Formal_Type_Declaration then
172 return Empty_List;
173
174 else
175 pragma Assert
176 (Nkind (Parent (Typ)) = N_Private_Extension_Declaration);
177 Nod := Parent (Typ);
178 end if;
179
180 elsif Ekind (Typ) = E_Record_Subtype then
181 Nod := Type_Definition (Parent (Etype (Typ)));
182
183 elsif Ekind (Typ) = E_Record_Subtype_With_Private then
184
185 -- Recurse, because parent may still be a private extension. Also
186 -- note that the full view of the subtype or the full view of its
187 -- base type may (both) be unavailable.
188
189 return Abstract_Interface_List (Etype (Typ));
190
191 else pragma Assert ((Ekind (Typ)) = E_Record_Type);
192 if Nkind (Parent (Typ)) = N_Formal_Type_Declaration then
193 Nod := Formal_Type_Definition (Parent (Typ));
194 else
195 Nod := Type_Definition (Parent (Typ));
196 end if;
197 end if;
198
199 return Interface_List (Nod);
200 end Abstract_Interface_List;
201
202 --------------------------------
203 -- Add_Access_Type_To_Process --
204 --------------------------------
205
206 procedure Add_Access_Type_To_Process (E : Entity_Id; A : Entity_Id) is
207 L : Elist_Id;
208
209 begin
210 Ensure_Freeze_Node (E);
211 L := Access_Types_To_Process (Freeze_Node (E));
212
213 if No (L) then
214 L := New_Elmt_List;
215 Set_Access_Types_To_Process (Freeze_Node (E), L);
216 end if;
217
218 Append_Elmt (A, L);
219 end Add_Access_Type_To_Process;
220
221 --------------------------
222 -- Add_Block_Identifier --
223 --------------------------
224
225 procedure Add_Block_Identifier (N : Node_Id; Id : out Entity_Id) is
226 Loc : constant Source_Ptr := Sloc (N);
227
228 begin
229 pragma Assert (Nkind (N) = N_Block_Statement);
230
231 -- The block already has a label, return its entity
232
233 if Present (Identifier (N)) then
234 Id := Entity (Identifier (N));
235
236 -- Create a new block label and set its attributes
237
238 else
239 Id := New_Internal_Entity (E_Block, Current_Scope, Loc, 'B');
240 Set_Etype (Id, Standard_Void_Type);
241 Set_Parent (Id, N);
242
243 Set_Identifier (N, New_Occurrence_Of (Id, Loc));
244 Set_Block_Node (Id, Identifier (N));
245 end if;
246 end Add_Block_Identifier;
247
248 -----------------------
249 -- Add_Contract_Item --
250 -----------------------
251
252 procedure Add_Contract_Item (Prag : Node_Id; Id : Entity_Id) is
253 Items : constant Node_Id := Contract (Id);
254
255 procedure Add_Classification;
256 -- Prepend Prag to the list of classifications
257
258 procedure Add_Contract_Test_Case;
259 -- Prepend Prag to the list of contract and test cases
260
261 procedure Add_Pre_Post_Condition;
262 -- Prepend Prag to the list of pre- and postconditions
263
264 ------------------------
265 -- Add_Classification --
266 ------------------------
267
268 procedure Add_Classification is
269 begin
270 Set_Next_Pragma (Prag, Classifications (Items));
271 Set_Classifications (Items, Prag);
272 end Add_Classification;
273
274 ----------------------------
275 -- Add_Contract_Test_Case --
276 ----------------------------
277
278 procedure Add_Contract_Test_Case is
279 begin
280 Set_Next_Pragma (Prag, Contract_Test_Cases (Items));
281 Set_Contract_Test_Cases (Items, Prag);
282 end Add_Contract_Test_Case;
283
284 ----------------------------
285 -- Add_Pre_Post_Condition --
286 ----------------------------
287
288 procedure Add_Pre_Post_Condition is
289 begin
290 Set_Next_Pragma (Prag, Pre_Post_Conditions (Items));
291 Set_Pre_Post_Conditions (Items, Prag);
292 end Add_Pre_Post_Condition;
293
294 -- Local variables
295
296 Nam : Name_Id;
297 PPC : Node_Id;
298
299 -- Start of processing for Add_Contract_Item
300
301 begin
302 -- The related context must have a contract and the item to be added
303 -- must be a pragma.
304
305 pragma Assert (Present (Items));
306 pragma Assert (Nkind (Prag) = N_Pragma);
307
308 Nam := Original_Aspect_Name (Prag);
309
310 -- Contract items related to [generic] packages or instantiations. The
311 -- applicable pragmas are:
312 -- Abstract_States
313 -- Initial_Condition
314 -- Initializes
315 -- Part_Of (instantiation only)
316
317 if Ekind_In (Id, E_Generic_Package, E_Package) then
318 if Nam_In (Nam, Name_Abstract_State,
319 Name_Initial_Condition,
320 Name_Initializes)
321 then
322 Add_Classification;
323
324 -- Indicator Part_Of must be associated with a package instantiation
325
326 elsif Nam = Name_Part_Of and then Is_Generic_Instance (Id) then
327 Add_Classification;
328
329 -- The pragma is not a proper contract item
330
331 else
332 raise Program_Error;
333 end if;
334
335 -- Contract items related to package bodies. The applicable pragmas are:
336 -- Refined_States
337
338 elsif Ekind (Id) = E_Package_Body then
339 if Nam = Name_Refined_State then
340 Add_Classification;
341
342 -- The pragma is not a proper contract item
343
344 else
345 raise Program_Error;
346 end if;
347
348 -- Contract items related to subprogram or entry declarations. The
349 -- applicable pragmas are:
350 -- Contract_Cases
351 -- Depends
352 -- Extensions_Visible
353 -- Global
354 -- Post
355 -- Postcondition
356 -- Pre
357 -- Precondition
358 -- Test_Case
359
360 elsif Ekind_In (Id, E_Entry, E_Entry_Family)
361 or else Is_Generic_Subprogram (Id)
362 or else Is_Subprogram (Id)
363 then
364 if Nam_In (Nam, Name_Pre,
365 Name_Precondition,
366 Name_uPre,
367 Name_Post,
368 Name_Postcondition,
369 Name_uPost)
370 then
371 -- Before we add a precondition or postcondition to the list, make
372 -- sure we do not have a disallowed duplicate, which can happen if
373 -- we use a pragma for Pre[_Class] or Post[_Class] instead of the
374 -- corresponding aspect.
375
376 if not From_Aspect_Specification (Prag)
377 and then Nam_In (Nam, Name_Pre,
378 Name_uPre,
379 Name_Post,
380 Name_Post_Class)
381 then
382 PPC := Pre_Post_Conditions (Items);
383 while Present (PPC) loop
384 if not Split_PPC (PPC)
385 and then Original_Aspect_Name (PPC) = Nam
386 then
387 Error_Msg_Sloc := Sloc (PPC);
388 Error_Msg_NE
389 ("duplication of aspect for & given#", Prag, Id);
390 return;
391 end if;
392
393 PPC := Next_Pragma (PPC);
394 end loop;
395 end if;
396
397 Add_Pre_Post_Condition;
398
399 elsif Nam_In (Nam, Name_Contract_Cases, Name_Test_Case) then
400 Add_Contract_Test_Case;
401
402 elsif Nam_In (Nam, Name_Depends,
403 Name_Extensions_Visible,
404 Name_Global)
405 then
406 Add_Classification;
407
408 -- The pragma is not a proper contract item
409
410 else
411 raise Program_Error;
412 end if;
413
414 -- Contract items related to subprogram bodies. Applicable pragmas are:
415 -- Refined_Depends
416 -- Refined_Global
417 -- Refined_Post
418
419 elsif Ekind (Id) = E_Subprogram_Body then
420 if Nam_In (Nam, Name_Refined_Depends, Name_Refined_Global) then
421 Add_Classification;
422
423 elsif Nam = Name_Refined_Post then
424 Add_Pre_Post_Condition;
425
426 -- The pragma is not a proper contract item
427
428 else
429 raise Program_Error;
430 end if;
431
432 -- Contract items related to variables. Applicable pragmas are:
433 -- Async_Readers
434 -- Async_Writers
435 -- Effective_Reads
436 -- Effective_Writes
437 -- Part_Of
438
439 elsif Ekind (Id) = E_Variable then
440 if Nam_In (Nam, Name_Async_Readers,
441 Name_Async_Writers,
442 Name_Effective_Reads,
443 Name_Effective_Writes,
444 Name_Part_Of)
445 then
446 Add_Classification;
447
448 -- The pragma is not a proper contract item
449
450 else
451 raise Program_Error;
452 end if;
453 end if;
454 end Add_Contract_Item;
455
456 ----------------------------
457 -- Add_Global_Declaration --
458 ----------------------------
459
460 procedure Add_Global_Declaration (N : Node_Id) is
461 Aux_Node : constant Node_Id := Aux_Decls_Node (Cunit (Current_Sem_Unit));
462
463 begin
464 if No (Declarations (Aux_Node)) then
465 Set_Declarations (Aux_Node, New_List);
466 end if;
467
468 Append_To (Declarations (Aux_Node), N);
469 Analyze (N);
470 end Add_Global_Declaration;
471
472 --------------------------------
473 -- Address_Integer_Convert_OK --
474 --------------------------------
475
476 function Address_Integer_Convert_OK (T1, T2 : Entity_Id) return Boolean is
477 begin
478 if Allow_Integer_Address
479 and then ((Is_Descendent_Of_Address (T1)
480 and then Is_Private_Type (T1)
481 and then Is_Integer_Type (T2))
482 or else
483 (Is_Descendent_Of_Address (T2)
484 and then Is_Private_Type (T2)
485 and then Is_Integer_Type (T1)))
486 then
487 return True;
488 else
489 return False;
490 end if;
491 end Address_Integer_Convert_OK;
492
493 -----------------
494 -- Addressable --
495 -----------------
496
497 -- For now, just 8/16/32/64. but analyze later if AAMP is special???
498
499 function Addressable (V : Uint) return Boolean is
500 begin
501 return V = Uint_8 or else
502 V = Uint_16 or else
503 V = Uint_32 or else
504 V = Uint_64;
505 end Addressable;
506
507 function Addressable (V : Int) return Boolean is
508 begin
509 return V = 8 or else
510 V = 16 or else
511 V = 32 or else
512 V = 64;
513 end Addressable;
514
515 ---------------------------------
516 -- Aggregate_Constraint_Checks --
517 ---------------------------------
518
519 procedure Aggregate_Constraint_Checks
520 (Exp : Node_Id;
521 Check_Typ : Entity_Id)
522 is
523 Exp_Typ : constant Entity_Id := Etype (Exp);
524
525 begin
526 if Raises_Constraint_Error (Exp) then
527 return;
528 end if;
529
530 -- Ada 2005 (AI-230): Generate a conversion to an anonymous access
531 -- component's type to force the appropriate accessibility checks.
532
533 -- Ada 2005 (AI-231): Generate conversion to the null-excluding
534 -- type to force the corresponding run-time check
535
536 if Is_Access_Type (Check_Typ)
537 and then ((Is_Local_Anonymous_Access (Check_Typ))
538 or else (Can_Never_Be_Null (Check_Typ)
539 and then not Can_Never_Be_Null (Exp_Typ)))
540 then
541 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
542 Analyze_And_Resolve (Exp, Check_Typ);
543 Check_Unset_Reference (Exp);
544 end if;
545
546 -- This is really expansion activity, so make sure that expansion is
547 -- on and is allowed. In GNATprove mode, we also want check flags to
548 -- be added in the tree, so that the formal verification can rely on
549 -- those to be present. In GNATprove mode for formal verification, some
550 -- treatment typically only done during expansion needs to be performed
551 -- on the tree, but it should not be applied inside generics. Otherwise,
552 -- this breaks the name resolution mechanism for generic instances.
553
554 if not Expander_Active
555 and (Inside_A_Generic or not Full_Analysis or not GNATprove_Mode)
556 then
557 return;
558 end if;
559
560 -- First check if we have to insert discriminant checks
561
562 if Has_Discriminants (Exp_Typ) then
563 Apply_Discriminant_Check (Exp, Check_Typ);
564
565 -- Next emit length checks for array aggregates
566
567 elsif Is_Array_Type (Exp_Typ) then
568 Apply_Length_Check (Exp, Check_Typ);
569
570 -- Finally emit scalar and string checks. If we are dealing with a
571 -- scalar literal we need to check by hand because the Etype of
572 -- literals is not necessarily correct.
573
574 elsif Is_Scalar_Type (Exp_Typ)
575 and then Compile_Time_Known_Value (Exp)
576 then
577 if Is_Out_Of_Range (Exp, Base_Type (Check_Typ)) then
578 Apply_Compile_Time_Constraint_Error
579 (Exp, "value not in range of}??", CE_Range_Check_Failed,
580 Ent => Base_Type (Check_Typ),
581 Typ => Base_Type (Check_Typ));
582
583 elsif Is_Out_Of_Range (Exp, Check_Typ) then
584 Apply_Compile_Time_Constraint_Error
585 (Exp, "value not in range of}??", CE_Range_Check_Failed,
586 Ent => Check_Typ,
587 Typ => Check_Typ);
588
589 elsif not Range_Checks_Suppressed (Check_Typ) then
590 Apply_Scalar_Range_Check (Exp, Check_Typ);
591 end if;
592
593 -- Verify that target type is also scalar, to prevent view anomalies
594 -- in instantiations.
595
596 elsif (Is_Scalar_Type (Exp_Typ)
597 or else Nkind (Exp) = N_String_Literal)
598 and then Is_Scalar_Type (Check_Typ)
599 and then Exp_Typ /= Check_Typ
600 then
601 if Is_Entity_Name (Exp)
602 and then Ekind (Entity (Exp)) = E_Constant
603 then
604 -- If expression is a constant, it is worthwhile checking whether
605 -- it is a bound of the type.
606
607 if (Is_Entity_Name (Type_Low_Bound (Check_Typ))
608 and then Entity (Exp) = Entity (Type_Low_Bound (Check_Typ)))
609 or else
610 (Is_Entity_Name (Type_High_Bound (Check_Typ))
611 and then Entity (Exp) = Entity (Type_High_Bound (Check_Typ)))
612 then
613 return;
614
615 else
616 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
617 Analyze_And_Resolve (Exp, Check_Typ);
618 Check_Unset_Reference (Exp);
619 end if;
620
621 -- Could use a comment on this case ???
622
623 else
624 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
625 Analyze_And_Resolve (Exp, Check_Typ);
626 Check_Unset_Reference (Exp);
627 end if;
628
629 end if;
630 end Aggregate_Constraint_Checks;
631
632 -----------------------
633 -- Alignment_In_Bits --
634 -----------------------
635
636 function Alignment_In_Bits (E : Entity_Id) return Uint is
637 begin
638 return Alignment (E) * System_Storage_Unit;
639 end Alignment_In_Bits;
640
641 ---------------------------------
642 -- Append_Inherited_Subprogram --
643 ---------------------------------
644
645 procedure Append_Inherited_Subprogram (S : Entity_Id) is
646 Par : constant Entity_Id := Alias (S);
647 -- The parent subprogram
648
649 Scop : constant Entity_Id := Scope (Par);
650 -- The scope of definition of the parent subprogram
651
652 Typ : constant Entity_Id := Defining_Entity (Parent (S));
653 -- The derived type of which S is a primitive operation
654
655 Decl : Node_Id;
656 Next_E : Entity_Id;
657
658 begin
659 if Ekind (Current_Scope) = E_Package
660 and then In_Private_Part (Current_Scope)
661 and then Has_Private_Declaration (Typ)
662 and then Is_Tagged_Type (Typ)
663 and then Scop = Current_Scope
664 then
665 -- The inherited operation is available at the earliest place after
666 -- the derived type declaration ( RM 7.3.1 (6/1)). This is only
667 -- relevant for type extensions. If the parent operation appears
668 -- after the type extension, the operation is not visible.
669
670 Decl := First
671 (Visible_Declarations
672 (Package_Specification (Current_Scope)));
673 while Present (Decl) loop
674 if Nkind (Decl) = N_Private_Extension_Declaration
675 and then Defining_Entity (Decl) = Typ
676 then
677 if Sloc (Decl) > Sloc (Par) then
678 Next_E := Next_Entity (Par);
679 Set_Next_Entity (Par, S);
680 Set_Next_Entity (S, Next_E);
681 return;
682
683 else
684 exit;
685 end if;
686 end if;
687
688 Next (Decl);
689 end loop;
690 end if;
691
692 -- If partial view is not a type extension, or it appears before the
693 -- subprogram declaration, insert normally at end of entity list.
694
695 Append_Entity (S, Current_Scope);
696 end Append_Inherited_Subprogram;
697
698 -----------------------------------------
699 -- Apply_Compile_Time_Constraint_Error --
700 -----------------------------------------
701
702 procedure Apply_Compile_Time_Constraint_Error
703 (N : Node_Id;
704 Msg : String;
705 Reason : RT_Exception_Code;
706 Ent : Entity_Id := Empty;
707 Typ : Entity_Id := Empty;
708 Loc : Source_Ptr := No_Location;
709 Rep : Boolean := True;
710 Warn : Boolean := False)
711 is
712 Stat : constant Boolean := Is_Static_Expression (N);
713 R_Stat : constant Node_Id :=
714 Make_Raise_Constraint_Error (Sloc (N), Reason => Reason);
715 Rtyp : Entity_Id;
716
717 begin
718 if No (Typ) then
719 Rtyp := Etype (N);
720 else
721 Rtyp := Typ;
722 end if;
723
724 Discard_Node
725 (Compile_Time_Constraint_Error (N, Msg, Ent, Loc, Warn => Warn));
726
727 if not Rep then
728 return;
729 end if;
730
731 -- Now we replace the node by an N_Raise_Constraint_Error node
732 -- This does not need reanalyzing, so set it as analyzed now.
733
734 Rewrite (N, R_Stat);
735 Set_Analyzed (N, True);
736
737 Set_Etype (N, Rtyp);
738 Set_Raises_Constraint_Error (N);
739
740 -- Now deal with possible local raise handling
741
742 Possible_Local_Raise (N, Standard_Constraint_Error);
743
744 -- If the original expression was marked as static, the result is
745 -- still marked as static, but the Raises_Constraint_Error flag is
746 -- always set so that further static evaluation is not attempted.
747
748 if Stat then
749 Set_Is_Static_Expression (N);
750 end if;
751 end Apply_Compile_Time_Constraint_Error;
752
753 ---------------------------
754 -- Async_Readers_Enabled --
755 ---------------------------
756
757 function Async_Readers_Enabled (Id : Entity_Id) return Boolean is
758 begin
759 return Has_Enabled_Property (Id, Name_Async_Readers);
760 end Async_Readers_Enabled;
761
762 ---------------------------
763 -- Async_Writers_Enabled --
764 ---------------------------
765
766 function Async_Writers_Enabled (Id : Entity_Id) return Boolean is
767 begin
768 return Has_Enabled_Property (Id, Name_Async_Writers);
769 end Async_Writers_Enabled;
770
771 --------------------------------------
772 -- Available_Full_View_Of_Component --
773 --------------------------------------
774
775 function Available_Full_View_Of_Component (T : Entity_Id) return Boolean is
776 ST : constant Entity_Id := Scope (T);
777 SCT : constant Entity_Id := Scope (Component_Type (T));
778 begin
779 return In_Open_Scopes (ST)
780 and then In_Open_Scopes (SCT)
781 and then Scope_Depth (ST) >= Scope_Depth (SCT);
782 end Available_Full_View_Of_Component;
783
784 -------------------
785 -- Bad_Attribute --
786 -------------------
787
788 procedure Bad_Attribute
789 (N : Node_Id;
790 Nam : Name_Id;
791 Warn : Boolean := False)
792 is
793 begin
794 Error_Msg_Warn := Warn;
795 Error_Msg_N ("unrecognized attribute&<<", N);
796
797 -- Check for possible misspelling
798
799 Error_Msg_Name_1 := First_Attribute_Name;
800 while Error_Msg_Name_1 <= Last_Attribute_Name loop
801 if Is_Bad_Spelling_Of (Nam, Error_Msg_Name_1) then
802 Error_Msg_N -- CODEFIX
803 ("\possible misspelling of %<<", N);
804 exit;
805 end if;
806
807 Error_Msg_Name_1 := Error_Msg_Name_1 + 1;
808 end loop;
809 end Bad_Attribute;
810
811 --------------------------------
812 -- Bad_Predicated_Subtype_Use --
813 --------------------------------
814
815 procedure Bad_Predicated_Subtype_Use
816 (Msg : String;
817 N : Node_Id;
818 Typ : Entity_Id;
819 Suggest_Static : Boolean := False)
820 is
821 Gen : Entity_Id;
822
823 begin
824 -- Avoid cascaded errors
825
826 if Error_Posted (N) then
827 return;
828 end if;
829
830 if Inside_A_Generic then
831 Gen := Current_Scope;
832 while Present (Gen) and then Ekind (Gen) /= E_Generic_Package loop
833 Gen := Scope (Gen);
834 end loop;
835
836 if No (Gen) then
837 return;
838 end if;
839
840 if Is_Generic_Formal (Typ) and then Is_Discrete_Type (Typ) then
841 Set_No_Predicate_On_Actual (Typ);
842 end if;
843
844 elsif Has_Predicates (Typ) then
845 if Is_Generic_Actual_Type (Typ) then
846
847 -- The restriction on loop parameters is only that the type
848 -- should have no dynamic predicates.
849
850 if Nkind (Parent (N)) = N_Loop_Parameter_Specification
851 and then not Has_Dynamic_Predicate_Aspect (Typ)
852 and then Is_OK_Static_Subtype (Typ)
853 then
854 return;
855 end if;
856
857 Gen := Current_Scope;
858 while not Is_Generic_Instance (Gen) loop
859 Gen := Scope (Gen);
860 end loop;
861
862 pragma Assert (Present (Gen));
863
864 if Ekind (Gen) = E_Package and then In_Package_Body (Gen) then
865 Error_Msg_Warn := SPARK_Mode /= On;
866 Error_Msg_FE (Msg & "<<", N, Typ);
867 Error_Msg_F ("\Program_Error [<<", N);
868
869 Insert_Action (N,
870 Make_Raise_Program_Error (Sloc (N),
871 Reason => PE_Bad_Predicated_Generic_Type));
872
873 else
874 Error_Msg_FE (Msg & "<<", N, Typ);
875 end if;
876
877 else
878 Error_Msg_FE (Msg, N, Typ);
879 end if;
880
881 -- Emit an optional suggestion on how to remedy the error if the
882 -- context warrants it.
883
884 if Suggest_Static and then Has_Static_Predicate (Typ) then
885 Error_Msg_FE ("\predicate of & should be marked static", N, Typ);
886 end if;
887 end if;
888 end Bad_Predicated_Subtype_Use;
889
890 -----------------------------------------
891 -- Bad_Unordered_Enumeration_Reference --
892 -----------------------------------------
893
894 function Bad_Unordered_Enumeration_Reference
895 (N : Node_Id;
896 T : Entity_Id) return Boolean
897 is
898 begin
899 return Is_Enumeration_Type (T)
900 and then Warn_On_Unordered_Enumeration_Type
901 and then not Is_Generic_Type (T)
902 and then Comes_From_Source (N)
903 and then not Has_Pragma_Ordered (T)
904 and then not In_Same_Extended_Unit (N, T);
905 end Bad_Unordered_Enumeration_Reference;
906
907 --------------------------
908 -- Build_Actual_Subtype --
909 --------------------------
910
911 function Build_Actual_Subtype
912 (T : Entity_Id;
913 N : Node_Or_Entity_Id) return Node_Id
914 is
915 Loc : Source_Ptr;
916 -- Normally Sloc (N), but may point to corresponding body in some cases
917
918 Constraints : List_Id;
919 Decl : Node_Id;
920 Discr : Entity_Id;
921 Hi : Node_Id;
922 Lo : Node_Id;
923 Subt : Entity_Id;
924 Disc_Type : Entity_Id;
925 Obj : Node_Id;
926
927 begin
928 Loc := Sloc (N);
929
930 if Nkind (N) = N_Defining_Identifier then
931 Obj := New_Occurrence_Of (N, Loc);
932
933 -- If this is a formal parameter of a subprogram declaration, and
934 -- we are compiling the body, we want the declaration for the
935 -- actual subtype to carry the source position of the body, to
936 -- prevent anomalies in gdb when stepping through the code.
937
938 if Is_Formal (N) then
939 declare
940 Decl : constant Node_Id := Unit_Declaration_Node (Scope (N));
941 begin
942 if Nkind (Decl) = N_Subprogram_Declaration
943 and then Present (Corresponding_Body (Decl))
944 then
945 Loc := Sloc (Corresponding_Body (Decl));
946 end if;
947 end;
948 end if;
949
950 else
951 Obj := N;
952 end if;
953
954 if Is_Array_Type (T) then
955 Constraints := New_List;
956 for J in 1 .. Number_Dimensions (T) loop
957
958 -- Build an array subtype declaration with the nominal subtype and
959 -- the bounds of the actual. Add the declaration in front of the
960 -- local declarations for the subprogram, for analysis before any
961 -- reference to the formal in the body.
962
963 Lo :=
964 Make_Attribute_Reference (Loc,
965 Prefix =>
966 Duplicate_Subexpr_No_Checks (Obj, Name_Req => True),
967 Attribute_Name => Name_First,
968 Expressions => New_List (
969 Make_Integer_Literal (Loc, J)));
970
971 Hi :=
972 Make_Attribute_Reference (Loc,
973 Prefix =>
974 Duplicate_Subexpr_No_Checks (Obj, Name_Req => True),
975 Attribute_Name => Name_Last,
976 Expressions => New_List (
977 Make_Integer_Literal (Loc, J)));
978
979 Append (Make_Range (Loc, Lo, Hi), Constraints);
980 end loop;
981
982 -- If the type has unknown discriminants there is no constrained
983 -- subtype to build. This is never called for a formal or for a
984 -- lhs, so returning the type is ok ???
985
986 elsif Has_Unknown_Discriminants (T) then
987 return T;
988
989 else
990 Constraints := New_List;
991
992 -- Type T is a generic derived type, inherit the discriminants from
993 -- the parent type.
994
995 if Is_Private_Type (T)
996 and then No (Full_View (T))
997
998 -- T was flagged as an error if it was declared as a formal
999 -- derived type with known discriminants. In this case there
1000 -- is no need to look at the parent type since T already carries
1001 -- its own discriminants.
1002
1003 and then not Error_Posted (T)
1004 then
1005 Disc_Type := Etype (Base_Type (T));
1006 else
1007 Disc_Type := T;
1008 end if;
1009
1010 Discr := First_Discriminant (Disc_Type);
1011 while Present (Discr) loop
1012 Append_To (Constraints,
1013 Make_Selected_Component (Loc,
1014 Prefix =>
1015 Duplicate_Subexpr_No_Checks (Obj),
1016 Selector_Name => New_Occurrence_Of (Discr, Loc)));
1017 Next_Discriminant (Discr);
1018 end loop;
1019 end if;
1020
1021 Subt := Make_Temporary (Loc, 'S', Related_Node => N);
1022 Set_Is_Internal (Subt);
1023
1024 Decl :=
1025 Make_Subtype_Declaration (Loc,
1026 Defining_Identifier => Subt,
1027 Subtype_Indication =>
1028 Make_Subtype_Indication (Loc,
1029 Subtype_Mark => New_Occurrence_Of (T, Loc),
1030 Constraint =>
1031 Make_Index_Or_Discriminant_Constraint (Loc,
1032 Constraints => Constraints)));
1033
1034 Mark_Rewrite_Insertion (Decl);
1035 return Decl;
1036 end Build_Actual_Subtype;
1037
1038 ---------------------------------------
1039 -- Build_Actual_Subtype_Of_Component --
1040 ---------------------------------------
1041
1042 function Build_Actual_Subtype_Of_Component
1043 (T : Entity_Id;
1044 N : Node_Id) return Node_Id
1045 is
1046 Loc : constant Source_Ptr := Sloc (N);
1047 P : constant Node_Id := Prefix (N);
1048 D : Elmt_Id;
1049 Id : Node_Id;
1050 Index_Typ : Entity_Id;
1051
1052 Desig_Typ : Entity_Id;
1053 -- This is either a copy of T, or if T is an access type, then it is
1054 -- the directly designated type of this access type.
1055
1056 function Build_Actual_Array_Constraint return List_Id;
1057 -- If one or more of the bounds of the component depends on
1058 -- discriminants, build actual constraint using the discriminants
1059 -- of the prefix.
1060
1061 function Build_Actual_Record_Constraint return List_Id;
1062 -- Similar to previous one, for discriminated components constrained
1063 -- by the discriminant of the enclosing object.
1064
1065 -----------------------------------
1066 -- Build_Actual_Array_Constraint --
1067 -----------------------------------
1068
1069 function Build_Actual_Array_Constraint return List_Id is
1070 Constraints : constant List_Id := New_List;
1071 Indx : Node_Id;
1072 Hi : Node_Id;
1073 Lo : Node_Id;
1074 Old_Hi : Node_Id;
1075 Old_Lo : Node_Id;
1076
1077 begin
1078 Indx := First_Index (Desig_Typ);
1079 while Present (Indx) loop
1080 Old_Lo := Type_Low_Bound (Etype (Indx));
1081 Old_Hi := Type_High_Bound (Etype (Indx));
1082
1083 if Denotes_Discriminant (Old_Lo) then
1084 Lo :=
1085 Make_Selected_Component (Loc,
1086 Prefix => New_Copy_Tree (P),
1087 Selector_Name => New_Occurrence_Of (Entity (Old_Lo), Loc));
1088
1089 else
1090 Lo := New_Copy_Tree (Old_Lo);
1091
1092 -- The new bound will be reanalyzed in the enclosing
1093 -- declaration. For literal bounds that come from a type
1094 -- declaration, the type of the context must be imposed, so
1095 -- insure that analysis will take place. For non-universal
1096 -- types this is not strictly necessary.
1097
1098 Set_Analyzed (Lo, False);
1099 end if;
1100
1101 if Denotes_Discriminant (Old_Hi) then
1102 Hi :=
1103 Make_Selected_Component (Loc,
1104 Prefix => New_Copy_Tree (P),
1105 Selector_Name => New_Occurrence_Of (Entity (Old_Hi), Loc));
1106
1107 else
1108 Hi := New_Copy_Tree (Old_Hi);
1109 Set_Analyzed (Hi, False);
1110 end if;
1111
1112 Append (Make_Range (Loc, Lo, Hi), Constraints);
1113 Next_Index (Indx);
1114 end loop;
1115
1116 return Constraints;
1117 end Build_Actual_Array_Constraint;
1118
1119 ------------------------------------
1120 -- Build_Actual_Record_Constraint --
1121 ------------------------------------
1122
1123 function Build_Actual_Record_Constraint return List_Id is
1124 Constraints : constant List_Id := New_List;
1125 D : Elmt_Id;
1126 D_Val : Node_Id;
1127
1128 begin
1129 D := First_Elmt (Discriminant_Constraint (Desig_Typ));
1130 while Present (D) loop
1131 if Denotes_Discriminant (Node (D)) then
1132 D_Val := Make_Selected_Component (Loc,
1133 Prefix => New_Copy_Tree (P),
1134 Selector_Name => New_Occurrence_Of (Entity (Node (D)), Loc));
1135
1136 else
1137 D_Val := New_Copy_Tree (Node (D));
1138 end if;
1139
1140 Append (D_Val, Constraints);
1141 Next_Elmt (D);
1142 end loop;
1143
1144 return Constraints;
1145 end Build_Actual_Record_Constraint;
1146
1147 -- Start of processing for Build_Actual_Subtype_Of_Component
1148
1149 begin
1150 -- Why the test for Spec_Expression mode here???
1151
1152 if In_Spec_Expression then
1153 return Empty;
1154
1155 -- More comments for the rest of this body would be good ???
1156
1157 elsif Nkind (N) = N_Explicit_Dereference then
1158 if Is_Composite_Type (T)
1159 and then not Is_Constrained (T)
1160 and then not (Is_Class_Wide_Type (T)
1161 and then Is_Constrained (Root_Type (T)))
1162 and then not Has_Unknown_Discriminants (T)
1163 then
1164 -- If the type of the dereference is already constrained, it is an
1165 -- actual subtype.
1166
1167 if Is_Array_Type (Etype (N))
1168 and then Is_Constrained (Etype (N))
1169 then
1170 return Empty;
1171 else
1172 Remove_Side_Effects (P);
1173 return Build_Actual_Subtype (T, N);
1174 end if;
1175 else
1176 return Empty;
1177 end if;
1178 end if;
1179
1180 if Ekind (T) = E_Access_Subtype then
1181 Desig_Typ := Designated_Type (T);
1182 else
1183 Desig_Typ := T;
1184 end if;
1185
1186 if Ekind (Desig_Typ) = E_Array_Subtype then
1187 Id := First_Index (Desig_Typ);
1188 while Present (Id) loop
1189 Index_Typ := Underlying_Type (Etype (Id));
1190
1191 if Denotes_Discriminant (Type_Low_Bound (Index_Typ))
1192 or else
1193 Denotes_Discriminant (Type_High_Bound (Index_Typ))
1194 then
1195 Remove_Side_Effects (P);
1196 return
1197 Build_Component_Subtype
1198 (Build_Actual_Array_Constraint, Loc, Base_Type (T));
1199 end if;
1200
1201 Next_Index (Id);
1202 end loop;
1203
1204 elsif Is_Composite_Type (Desig_Typ)
1205 and then Has_Discriminants (Desig_Typ)
1206 and then not Has_Unknown_Discriminants (Desig_Typ)
1207 then
1208 if Is_Private_Type (Desig_Typ)
1209 and then No (Discriminant_Constraint (Desig_Typ))
1210 then
1211 Desig_Typ := Full_View (Desig_Typ);
1212 end if;
1213
1214 D := First_Elmt (Discriminant_Constraint (Desig_Typ));
1215 while Present (D) loop
1216 if Denotes_Discriminant (Node (D)) then
1217 Remove_Side_Effects (P);
1218 return
1219 Build_Component_Subtype (
1220 Build_Actual_Record_Constraint, Loc, Base_Type (T));
1221 end if;
1222
1223 Next_Elmt (D);
1224 end loop;
1225 end if;
1226
1227 -- If none of the above, the actual and nominal subtypes are the same
1228
1229 return Empty;
1230 end Build_Actual_Subtype_Of_Component;
1231
1232 -----------------------------
1233 -- Build_Component_Subtype --
1234 -----------------------------
1235
1236 function Build_Component_Subtype
1237 (C : List_Id;
1238 Loc : Source_Ptr;
1239 T : Entity_Id) return Node_Id
1240 is
1241 Subt : Entity_Id;
1242 Decl : Node_Id;
1243
1244 begin
1245 -- Unchecked_Union components do not require component subtypes
1246
1247 if Is_Unchecked_Union (T) then
1248 return Empty;
1249 end if;
1250
1251 Subt := Make_Temporary (Loc, 'S');
1252 Set_Is_Internal (Subt);
1253
1254 Decl :=
1255 Make_Subtype_Declaration (Loc,
1256 Defining_Identifier => Subt,
1257 Subtype_Indication =>
1258 Make_Subtype_Indication (Loc,
1259 Subtype_Mark => New_Occurrence_Of (Base_Type (T), Loc),
1260 Constraint =>
1261 Make_Index_Or_Discriminant_Constraint (Loc,
1262 Constraints => C)));
1263
1264 Mark_Rewrite_Insertion (Decl);
1265 return Decl;
1266 end Build_Component_Subtype;
1267
1268 ----------------------------------
1269 -- Build_Default_Init_Cond_Call --
1270 ----------------------------------
1271
1272 function Build_Default_Init_Cond_Call
1273 (Loc : Source_Ptr;
1274 Obj_Id : Entity_Id;
1275 Typ : Entity_Id) return Node_Id
1276 is
1277 Proc_Id : constant Entity_Id := Default_Init_Cond_Procedure (Typ);
1278 Formal_Typ : constant Entity_Id := Etype (First_Formal (Proc_Id));
1279
1280 begin
1281 return
1282 Make_Procedure_Call_Statement (Loc,
1283 Name => New_Occurrence_Of (Proc_Id, Loc),
1284 Parameter_Associations => New_List (
1285 Make_Unchecked_Type_Conversion (Loc,
1286 Subtype_Mark => New_Occurrence_Of (Formal_Typ, Loc),
1287 Expression => New_Occurrence_Of (Obj_Id, Loc))));
1288 end Build_Default_Init_Cond_Call;
1289
1290 ----------------------------------------------
1291 -- Build_Default_Init_Cond_Procedure_Bodies --
1292 ----------------------------------------------
1293
1294 procedure Build_Default_Init_Cond_Procedure_Bodies (Priv_Decls : List_Id) is
1295 procedure Build_Default_Init_Cond_Procedure_Body (Typ : Entity_Id);
1296 -- If type Typ is subject to pragma Default_Initial_Condition, build the
1297 -- body of the procedure which verifies the assumption of the pragma at
1298 -- run time. The generated body is added after the type declaration.
1299
1300 --------------------------------------------
1301 -- Build_Default_Init_Cond_Procedure_Body --
1302 --------------------------------------------
1303
1304 procedure Build_Default_Init_Cond_Procedure_Body (Typ : Entity_Id) is
1305 Param_Id : Entity_Id;
1306 -- The entity of the sole formal parameter of the default initial
1307 -- condition procedure.
1308
1309 procedure Replace_Type_Reference (N : Node_Id);
1310 -- Replace a single reference to type Typ with a reference to formal
1311 -- parameter Param_Id.
1312
1313 ----------------------------
1314 -- Replace_Type_Reference --
1315 ----------------------------
1316
1317 procedure Replace_Type_Reference (N : Node_Id) is
1318 begin
1319 Rewrite (N, New_Occurrence_Of (Param_Id, Sloc (N)));
1320 end Replace_Type_Reference;
1321
1322 procedure Replace_Type_References is
1323 new Replace_Type_References_Generic (Replace_Type_Reference);
1324
1325 -- Local variables
1326
1327 Loc : constant Source_Ptr := Sloc (Typ);
1328 Prag : constant Node_Id :=
1329 Get_Pragma (Typ, Pragma_Default_Initial_Condition);
1330 Proc_Id : constant Entity_Id := Default_Init_Cond_Procedure (Typ);
1331 Spec_Decl : constant Node_Id := Unit_Declaration_Node (Proc_Id);
1332 Body_Decl : Node_Id;
1333 Expr : Node_Id;
1334 Stmt : Node_Id;
1335
1336 -- Start of processing for Build_Default_Init_Cond_Procedure_Body
1337
1338 begin
1339 -- The procedure should be generated only for [sub]types subject to
1340 -- pragma Default_Initial_Condition. Types that inherit the pragma do
1341 -- not get this specialized procedure.
1342
1343 pragma Assert (Has_Default_Init_Cond (Typ));
1344 pragma Assert (Present (Prag));
1345 pragma Assert (Present (Proc_Id));
1346
1347 -- Nothing to do if the body was already built
1348
1349 if Present (Corresponding_Body (Spec_Decl)) then
1350 return;
1351 end if;
1352
1353 Param_Id := First_Formal (Proc_Id);
1354
1355 -- The pragma has an argument. Note that the argument is analyzed
1356 -- after all references to the current instance of the type are
1357 -- replaced.
1358
1359 if Present (Pragma_Argument_Associations (Prag)) then
1360 Expr :=
1361 Get_Pragma_Arg (First (Pragma_Argument_Associations (Prag)));
1362
1363 if Nkind (Expr) = N_Null then
1364 Stmt := Make_Null_Statement (Loc);
1365
1366 -- Preserve the original argument of the pragma by replicating it.
1367 -- Replace all references to the current instance of the type with
1368 -- references to the formal parameter.
1369
1370 else
1371 Expr := New_Copy_Tree (Expr);
1372 Replace_Type_References (Expr, Typ);
1373
1374 -- Generate:
1375 -- pragma Check (Default_Initial_Condition, <Expr>);
1376
1377 Stmt :=
1378 Make_Pragma (Loc,
1379 Pragma_Identifier =>
1380 Make_Identifier (Loc, Name_Check),
1381
1382 Pragma_Argument_Associations => New_List (
1383 Make_Pragma_Argument_Association (Loc,
1384 Expression =>
1385 Make_Identifier (Loc,
1386 Chars => Name_Default_Initial_Condition)),
1387 Make_Pragma_Argument_Association (Loc,
1388 Expression => Expr)));
1389 end if;
1390
1391 -- Otherwise the pragma appears without an argument
1392
1393 else
1394 Stmt := Make_Null_Statement (Loc);
1395 end if;
1396
1397 -- Generate:
1398 -- procedure <Typ>Default_Init_Cond (I : <Typ>) is
1399 -- begin
1400 -- <Stmt>;
1401 -- end <Typ>Default_Init_Cond;
1402
1403 Body_Decl :=
1404 Make_Subprogram_Body (Loc,
1405 Specification =>
1406 Copy_Separate_Tree (Specification (Spec_Decl)),
1407 Declarations => Empty_List,
1408 Handled_Statement_Sequence =>
1409 Make_Handled_Sequence_Of_Statements (Loc,
1410 Statements => New_List (Stmt)));
1411
1412 -- Link the spec and body of the default initial condition procedure
1413 -- to prevent the generation of a duplicate body.
1414
1415 Set_Corresponding_Body (Spec_Decl, Defining_Entity (Body_Decl));
1416 Set_Corresponding_Spec (Body_Decl, Proc_Id);
1417
1418 Insert_After_And_Analyze (Declaration_Node (Typ), Body_Decl);
1419 end Build_Default_Init_Cond_Procedure_Body;
1420
1421 -- Local variables
1422
1423 Decl : Node_Id;
1424 Typ : Entity_Id;
1425
1426 -- Start of processing for Build_Default_Init_Cond_Procedure_Bodies
1427
1428 begin
1429 -- Inspect the private declarations looking for [sub]type declarations
1430
1431 Decl := First (Priv_Decls);
1432 while Present (Decl) loop
1433 if Nkind_In (Decl, N_Full_Type_Declaration,
1434 N_Subtype_Declaration)
1435 then
1436 Typ := Defining_Entity (Decl);
1437
1438 -- Guard against partially decorate types due to previous errors
1439
1440 if Is_Type (Typ) then
1441
1442 -- If the type is subject to pragma Default_Initial_Condition,
1443 -- generate the body of the internal procedure which verifies
1444 -- the assertion of the pragma at run time.
1445
1446 if Has_Default_Init_Cond (Typ) then
1447 Build_Default_Init_Cond_Procedure_Body (Typ);
1448
1449 -- A derived type inherits the default initial condition
1450 -- procedure from its parent type.
1451
1452 elsif Has_Inherited_Default_Init_Cond (Typ) then
1453 Inherit_Default_Init_Cond_Procedure (Typ);
1454 end if;
1455 end if;
1456 end if;
1457
1458 Next (Decl);
1459 end loop;
1460 end Build_Default_Init_Cond_Procedure_Bodies;
1461
1462 ---------------------------------------------------
1463 -- Build_Default_Init_Cond_Procedure_Declaration --
1464 ---------------------------------------------------
1465
1466 procedure Build_Default_Init_Cond_Procedure_Declaration (Typ : Entity_Id) is
1467 Loc : constant Source_Ptr := Sloc (Typ);
1468 Prag : constant Node_Id :=
1469 Get_Pragma (Typ, Pragma_Default_Initial_Condition);
1470 Proc_Id : Entity_Id;
1471
1472 begin
1473 -- The procedure should be generated only for types subject to pragma
1474 -- Default_Initial_Condition. Types that inherit the pragma do not get
1475 -- this specialized procedure.
1476
1477 pragma Assert (Has_Default_Init_Cond (Typ));
1478 pragma Assert (Present (Prag));
1479
1480 -- Nothing to do if default initial condition procedure already built
1481
1482 if Present (Default_Init_Cond_Procedure (Typ)) then
1483 return;
1484 end if;
1485
1486 Proc_Id :=
1487 Make_Defining_Identifier (Loc,
1488 Chars => New_External_Name (Chars (Typ), "Default_Init_Cond"));
1489
1490 -- Associate default initial condition procedure with the private type
1491
1492 Set_Ekind (Proc_Id, E_Procedure);
1493 Set_Is_Default_Init_Cond_Procedure (Proc_Id);
1494 Set_Default_Init_Cond_Procedure (Typ, Proc_Id);
1495
1496 -- Generate:
1497 -- procedure <Typ>Default_Init_Cond (Inn : <Typ>);
1498
1499 Insert_After_And_Analyze (Prag,
1500 Make_Subprogram_Declaration (Loc,
1501 Specification =>
1502 Make_Procedure_Specification (Loc,
1503 Defining_Unit_Name => Proc_Id,
1504 Parameter_Specifications => New_List (
1505 Make_Parameter_Specification (Loc,
1506 Defining_Identifier => Make_Temporary (Loc, 'I'),
1507 Parameter_Type => New_Occurrence_Of (Typ, Loc))))));
1508 end Build_Default_Init_Cond_Procedure_Declaration;
1509
1510 ---------------------------
1511 -- Build_Default_Subtype --
1512 ---------------------------
1513
1514 function Build_Default_Subtype
1515 (T : Entity_Id;
1516 N : Node_Id) return Entity_Id
1517 is
1518 Loc : constant Source_Ptr := Sloc (N);
1519 Disc : Entity_Id;
1520
1521 Bas : Entity_Id;
1522 -- The base type that is to be constrained by the defaults
1523
1524 begin
1525 if not Has_Discriminants (T) or else Is_Constrained (T) then
1526 return T;
1527 end if;
1528
1529 Bas := Base_Type (T);
1530
1531 -- If T is non-private but its base type is private, this is the
1532 -- completion of a subtype declaration whose parent type is private
1533 -- (see Complete_Private_Subtype in Sem_Ch3). The proper discriminants
1534 -- are to be found in the full view of the base. Check that the private
1535 -- status of T and its base differ.
1536
1537 if Is_Private_Type (Bas)
1538 and then not Is_Private_Type (T)
1539 and then Present (Full_View (Bas))
1540 then
1541 Bas := Full_View (Bas);
1542 end if;
1543
1544 Disc := First_Discriminant (T);
1545
1546 if No (Discriminant_Default_Value (Disc)) then
1547 return T;
1548 end if;
1549
1550 declare
1551 Act : constant Entity_Id := Make_Temporary (Loc, 'S');
1552 Constraints : constant List_Id := New_List;
1553 Decl : Node_Id;
1554
1555 begin
1556 while Present (Disc) loop
1557 Append_To (Constraints,
1558 New_Copy_Tree (Discriminant_Default_Value (Disc)));
1559 Next_Discriminant (Disc);
1560 end loop;
1561
1562 Decl :=
1563 Make_Subtype_Declaration (Loc,
1564 Defining_Identifier => Act,
1565 Subtype_Indication =>
1566 Make_Subtype_Indication (Loc,
1567 Subtype_Mark => New_Occurrence_Of (Bas, Loc),
1568 Constraint =>
1569 Make_Index_Or_Discriminant_Constraint (Loc,
1570 Constraints => Constraints)));
1571
1572 Insert_Action (N, Decl);
1573 Analyze (Decl);
1574 return Act;
1575 end;
1576 end Build_Default_Subtype;
1577
1578 --------------------------------------------
1579 -- Build_Discriminal_Subtype_Of_Component --
1580 --------------------------------------------
1581
1582 function Build_Discriminal_Subtype_Of_Component
1583 (T : Entity_Id) return Node_Id
1584 is
1585 Loc : constant Source_Ptr := Sloc (T);
1586 D : Elmt_Id;
1587 Id : Node_Id;
1588
1589 function Build_Discriminal_Array_Constraint return List_Id;
1590 -- If one or more of the bounds of the component depends on
1591 -- discriminants, build actual constraint using the discriminants
1592 -- of the prefix.
1593
1594 function Build_Discriminal_Record_Constraint return List_Id;
1595 -- Similar to previous one, for discriminated components constrained by
1596 -- the discriminant of the enclosing object.
1597
1598 ----------------------------------------
1599 -- Build_Discriminal_Array_Constraint --
1600 ----------------------------------------
1601
1602 function Build_Discriminal_Array_Constraint return List_Id is
1603 Constraints : constant List_Id := New_List;
1604 Indx : Node_Id;
1605 Hi : Node_Id;
1606 Lo : Node_Id;
1607 Old_Hi : Node_Id;
1608 Old_Lo : Node_Id;
1609
1610 begin
1611 Indx := First_Index (T);
1612 while Present (Indx) loop
1613 Old_Lo := Type_Low_Bound (Etype (Indx));
1614 Old_Hi := Type_High_Bound (Etype (Indx));
1615
1616 if Denotes_Discriminant (Old_Lo) then
1617 Lo := New_Occurrence_Of (Discriminal (Entity (Old_Lo)), Loc);
1618
1619 else
1620 Lo := New_Copy_Tree (Old_Lo);
1621 end if;
1622
1623 if Denotes_Discriminant (Old_Hi) then
1624 Hi := New_Occurrence_Of (Discriminal (Entity (Old_Hi)), Loc);
1625
1626 else
1627 Hi := New_Copy_Tree (Old_Hi);
1628 end if;
1629
1630 Append (Make_Range (Loc, Lo, Hi), Constraints);
1631 Next_Index (Indx);
1632 end loop;
1633
1634 return Constraints;
1635 end Build_Discriminal_Array_Constraint;
1636
1637 -----------------------------------------
1638 -- Build_Discriminal_Record_Constraint --
1639 -----------------------------------------
1640
1641 function Build_Discriminal_Record_Constraint return List_Id is
1642 Constraints : constant List_Id := New_List;
1643 D : Elmt_Id;
1644 D_Val : Node_Id;
1645
1646 begin
1647 D := First_Elmt (Discriminant_Constraint (T));
1648 while Present (D) loop
1649 if Denotes_Discriminant (Node (D)) then
1650 D_Val :=
1651 New_Occurrence_Of (Discriminal (Entity (Node (D))), Loc);
1652 else
1653 D_Val := New_Copy_Tree (Node (D));
1654 end if;
1655
1656 Append (D_Val, Constraints);
1657 Next_Elmt (D);
1658 end loop;
1659
1660 return Constraints;
1661 end Build_Discriminal_Record_Constraint;
1662
1663 -- Start of processing for Build_Discriminal_Subtype_Of_Component
1664
1665 begin
1666 if Ekind (T) = E_Array_Subtype then
1667 Id := First_Index (T);
1668 while Present (Id) loop
1669 if Denotes_Discriminant (Type_Low_Bound (Etype (Id)))
1670 or else
1671 Denotes_Discriminant (Type_High_Bound (Etype (Id)))
1672 then
1673 return Build_Component_Subtype
1674 (Build_Discriminal_Array_Constraint, Loc, T);
1675 end if;
1676
1677 Next_Index (Id);
1678 end loop;
1679
1680 elsif Ekind (T) = E_Record_Subtype
1681 and then Has_Discriminants (T)
1682 and then not Has_Unknown_Discriminants (T)
1683 then
1684 D := First_Elmt (Discriminant_Constraint (T));
1685 while Present (D) loop
1686 if Denotes_Discriminant (Node (D)) then
1687 return Build_Component_Subtype
1688 (Build_Discriminal_Record_Constraint, Loc, T);
1689 end if;
1690
1691 Next_Elmt (D);
1692 end loop;
1693 end if;
1694
1695 -- If none of the above, the actual and nominal subtypes are the same
1696
1697 return Empty;
1698 end Build_Discriminal_Subtype_Of_Component;
1699
1700 ------------------------------
1701 -- Build_Elaboration_Entity --
1702 ------------------------------
1703
1704 procedure Build_Elaboration_Entity (N : Node_Id; Spec_Id : Entity_Id) is
1705 Loc : constant Source_Ptr := Sloc (N);
1706 Decl : Node_Id;
1707 Elab_Ent : Entity_Id;
1708
1709 procedure Set_Package_Name (Ent : Entity_Id);
1710 -- Given an entity, sets the fully qualified name of the entity in
1711 -- Name_Buffer, with components separated by double underscores. This
1712 -- is a recursive routine that climbs the scope chain to Standard.
1713
1714 ----------------------
1715 -- Set_Package_Name --
1716 ----------------------
1717
1718 procedure Set_Package_Name (Ent : Entity_Id) is
1719 begin
1720 if Scope (Ent) /= Standard_Standard then
1721 Set_Package_Name (Scope (Ent));
1722
1723 declare
1724 Nam : constant String := Get_Name_String (Chars (Ent));
1725 begin
1726 Name_Buffer (Name_Len + 1) := '_';
1727 Name_Buffer (Name_Len + 2) := '_';
1728 Name_Buffer (Name_Len + 3 .. Name_Len + Nam'Length + 2) := Nam;
1729 Name_Len := Name_Len + Nam'Length + 2;
1730 end;
1731
1732 else
1733 Get_Name_String (Chars (Ent));
1734 end if;
1735 end Set_Package_Name;
1736
1737 -- Start of processing for Build_Elaboration_Entity
1738
1739 begin
1740 -- Ignore call if already constructed
1741
1742 if Present (Elaboration_Entity (Spec_Id)) then
1743 return;
1744
1745 -- Ignore in ASIS mode, elaboration entity is not in source and plays
1746 -- no role in analysis.
1747
1748 elsif ASIS_Mode then
1749 return;
1750
1751 -- See if we need elaboration entity. We always need it for the dynamic
1752 -- elaboration model, since it is needed to properly generate the PE
1753 -- exception for access before elaboration.
1754
1755 elsif Dynamic_Elaboration_Checks then
1756 null;
1757
1758 -- For the static model, we don't need the elaboration counter if this
1759 -- unit is sure to have no elaboration code, since that means there
1760 -- is no elaboration unit to be called. Note that we can't just decide
1761 -- after the fact by looking to see whether there was elaboration code,
1762 -- because that's too late to make this decision.
1763
1764 elsif Restriction_Active (No_Elaboration_Code) then
1765 return;
1766
1767 -- Similarly, for the static model, we can skip the elaboration counter
1768 -- if we have the No_Multiple_Elaboration restriction, since for the
1769 -- static model, that's the only purpose of the counter (to avoid
1770 -- multiple elaboration).
1771
1772 elsif Restriction_Active (No_Multiple_Elaboration) then
1773 return;
1774 end if;
1775
1776 -- Here we need the elaboration entity
1777
1778 -- Construct name of elaboration entity as xxx_E, where xxx is the unit
1779 -- name with dots replaced by double underscore. We have to manually
1780 -- construct this name, since it will be elaborated in the outer scope,
1781 -- and thus will not have the unit name automatically prepended.
1782
1783 Set_Package_Name (Spec_Id);
1784 Add_Str_To_Name_Buffer ("_E");
1785
1786 -- Create elaboration counter
1787
1788 Elab_Ent := Make_Defining_Identifier (Loc, Chars => Name_Find);
1789 Set_Elaboration_Entity (Spec_Id, Elab_Ent);
1790
1791 Decl :=
1792 Make_Object_Declaration (Loc,
1793 Defining_Identifier => Elab_Ent,
1794 Object_Definition =>
1795 New_Occurrence_Of (Standard_Short_Integer, Loc),
1796 Expression => Make_Integer_Literal (Loc, Uint_0));
1797
1798 Push_Scope (Standard_Standard);
1799 Add_Global_Declaration (Decl);
1800 Pop_Scope;
1801
1802 -- Reset True_Constant indication, since we will indeed assign a value
1803 -- to the variable in the binder main. We also kill the Current_Value
1804 -- and Last_Assignment fields for the same reason.
1805
1806 Set_Is_True_Constant (Elab_Ent, False);
1807 Set_Current_Value (Elab_Ent, Empty);
1808 Set_Last_Assignment (Elab_Ent, Empty);
1809
1810 -- We do not want any further qualification of the name (if we did not
1811 -- do this, we would pick up the name of the generic package in the case
1812 -- of a library level generic instantiation).
1813
1814 Set_Has_Qualified_Name (Elab_Ent);
1815 Set_Has_Fully_Qualified_Name (Elab_Ent);
1816 end Build_Elaboration_Entity;
1817
1818 --------------------------------
1819 -- Build_Explicit_Dereference --
1820 --------------------------------
1821
1822 procedure Build_Explicit_Dereference
1823 (Expr : Node_Id;
1824 Disc : Entity_Id)
1825 is
1826 Loc : constant Source_Ptr := Sloc (Expr);
1827
1828 begin
1829 -- An entity of a type with a reference aspect is overloaded with
1830 -- both interpretations: with and without the dereference. Now that
1831 -- the dereference is made explicit, set the type of the node properly,
1832 -- to prevent anomalies in the backend. Same if the expression is an
1833 -- overloaded function call whose return type has a reference aspect.
1834
1835 if Is_Entity_Name (Expr) then
1836 Set_Etype (Expr, Etype (Entity (Expr)));
1837
1838 elsif Nkind (Expr) = N_Function_Call then
1839 Set_Etype (Expr, Etype (Name (Expr)));
1840 end if;
1841
1842 Set_Is_Overloaded (Expr, False);
1843
1844 -- The expression will often be a generalized indexing that yields a
1845 -- container element that is then dereferenced, in which case the
1846 -- generalized indexing call is also non-overloaded.
1847
1848 if Nkind (Expr) = N_Indexed_Component
1849 and then Present (Generalized_Indexing (Expr))
1850 then
1851 Set_Is_Overloaded (Generalized_Indexing (Expr), False);
1852 end if;
1853
1854 Rewrite (Expr,
1855 Make_Explicit_Dereference (Loc,
1856 Prefix =>
1857 Make_Selected_Component (Loc,
1858 Prefix => Relocate_Node (Expr),
1859 Selector_Name => New_Occurrence_Of (Disc, Loc))));
1860 Set_Etype (Prefix (Expr), Etype (Disc));
1861 Set_Etype (Expr, Designated_Type (Etype (Disc)));
1862 end Build_Explicit_Dereference;
1863
1864 -----------------------------------
1865 -- Cannot_Raise_Constraint_Error --
1866 -----------------------------------
1867
1868 function Cannot_Raise_Constraint_Error (Expr : Node_Id) return Boolean is
1869 begin
1870 if Compile_Time_Known_Value (Expr) then
1871 return True;
1872
1873 elsif Do_Range_Check (Expr) then
1874 return False;
1875
1876 elsif Raises_Constraint_Error (Expr) then
1877 return False;
1878
1879 else
1880 case Nkind (Expr) is
1881 when N_Identifier =>
1882 return True;
1883
1884 when N_Expanded_Name =>
1885 return True;
1886
1887 when N_Selected_Component =>
1888 return not Do_Discriminant_Check (Expr);
1889
1890 when N_Attribute_Reference =>
1891 if Do_Overflow_Check (Expr) then
1892 return False;
1893
1894 elsif No (Expressions (Expr)) then
1895 return True;
1896
1897 else
1898 declare
1899 N : Node_Id;
1900
1901 begin
1902 N := First (Expressions (Expr));
1903 while Present (N) loop
1904 if Cannot_Raise_Constraint_Error (N) then
1905 Next (N);
1906 else
1907 return False;
1908 end if;
1909 end loop;
1910
1911 return True;
1912 end;
1913 end if;
1914
1915 when N_Type_Conversion =>
1916 if Do_Overflow_Check (Expr)
1917 or else Do_Length_Check (Expr)
1918 or else Do_Tag_Check (Expr)
1919 then
1920 return False;
1921 else
1922 return Cannot_Raise_Constraint_Error (Expression (Expr));
1923 end if;
1924
1925 when N_Unchecked_Type_Conversion =>
1926 return Cannot_Raise_Constraint_Error (Expression (Expr));
1927
1928 when N_Unary_Op =>
1929 if Do_Overflow_Check (Expr) then
1930 return False;
1931 else
1932 return Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
1933 end if;
1934
1935 when N_Op_Divide |
1936 N_Op_Mod |
1937 N_Op_Rem
1938 =>
1939 if Do_Division_Check (Expr)
1940 or else
1941 Do_Overflow_Check (Expr)
1942 then
1943 return False;
1944 else
1945 return
1946 Cannot_Raise_Constraint_Error (Left_Opnd (Expr))
1947 and then
1948 Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
1949 end if;
1950
1951 when N_Op_Add |
1952 N_Op_And |
1953 N_Op_Concat |
1954 N_Op_Eq |
1955 N_Op_Expon |
1956 N_Op_Ge |
1957 N_Op_Gt |
1958 N_Op_Le |
1959 N_Op_Lt |
1960 N_Op_Multiply |
1961 N_Op_Ne |
1962 N_Op_Or |
1963 N_Op_Rotate_Left |
1964 N_Op_Rotate_Right |
1965 N_Op_Shift_Left |
1966 N_Op_Shift_Right |
1967 N_Op_Shift_Right_Arithmetic |
1968 N_Op_Subtract |
1969 N_Op_Xor
1970 =>
1971 if Do_Overflow_Check (Expr) then
1972 return False;
1973 else
1974 return
1975 Cannot_Raise_Constraint_Error (Left_Opnd (Expr))
1976 and then
1977 Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
1978 end if;
1979
1980 when others =>
1981 return False;
1982 end case;
1983 end if;
1984 end Cannot_Raise_Constraint_Error;
1985
1986 -----------------------------------------
1987 -- Check_Dynamically_Tagged_Expression --
1988 -----------------------------------------
1989
1990 procedure Check_Dynamically_Tagged_Expression
1991 (Expr : Node_Id;
1992 Typ : Entity_Id;
1993 Related_Nod : Node_Id)
1994 is
1995 begin
1996 pragma Assert (Is_Tagged_Type (Typ));
1997
1998 -- In order to avoid spurious errors when analyzing the expanded code,
1999 -- this check is done only for nodes that come from source and for
2000 -- actuals of generic instantiations.
2001
2002 if (Comes_From_Source (Related_Nod)
2003 or else In_Generic_Actual (Expr))
2004 and then (Is_Class_Wide_Type (Etype (Expr))
2005 or else Is_Dynamically_Tagged (Expr))
2006 and then Is_Tagged_Type (Typ)
2007 and then not Is_Class_Wide_Type (Typ)
2008 then
2009 Error_Msg_N ("dynamically tagged expression not allowed!", Expr);
2010 end if;
2011 end Check_Dynamically_Tagged_Expression;
2012
2013 --------------------------
2014 -- Check_Fully_Declared --
2015 --------------------------
2016
2017 procedure Check_Fully_Declared (T : Entity_Id; N : Node_Id) is
2018 begin
2019 if Ekind (T) = E_Incomplete_Type then
2020
2021 -- Ada 2005 (AI-50217): If the type is available through a limited
2022 -- with_clause, verify that its full view has been analyzed.
2023
2024 if From_Limited_With (T)
2025 and then Present (Non_Limited_View (T))
2026 and then Ekind (Non_Limited_View (T)) /= E_Incomplete_Type
2027 then
2028 -- The non-limited view is fully declared
2029
2030 null;
2031
2032 else
2033 Error_Msg_NE
2034 ("premature usage of incomplete}", N, First_Subtype (T));
2035 end if;
2036
2037 -- Need comments for these tests ???
2038
2039 elsif Has_Private_Component (T)
2040 and then not Is_Generic_Type (Root_Type (T))
2041 and then not In_Spec_Expression
2042 then
2043 -- Special case: if T is the anonymous type created for a single
2044 -- task or protected object, use the name of the source object.
2045
2046 if Is_Concurrent_Type (T)
2047 and then not Comes_From_Source (T)
2048 and then Nkind (N) = N_Object_Declaration
2049 then
2050 Error_Msg_NE
2051 ("type of& has incomplete component",
2052 N, Defining_Identifier (N));
2053 else
2054 Error_Msg_NE
2055 ("premature usage of incomplete}",
2056 N, First_Subtype (T));
2057 end if;
2058 end if;
2059 end Check_Fully_Declared;
2060
2061 -------------------------------------
2062 -- Check_Function_Writable_Actuals --
2063 -------------------------------------
2064
2065 procedure Check_Function_Writable_Actuals (N : Node_Id) is
2066 Writable_Actuals_List : Elist_Id := No_Elist;
2067 Identifiers_List : Elist_Id := No_Elist;
2068 Error_Node : Node_Id := Empty;
2069
2070 procedure Collect_Identifiers (N : Node_Id);
2071 -- In a single traversal of subtree N collect in Writable_Actuals_List
2072 -- all the actuals of functions with writable actuals, and in the list
2073 -- Identifiers_List collect all the identifiers that are not actuals of
2074 -- functions with writable actuals. If a writable actual is referenced
2075 -- twice as writable actual then Error_Node is set to reference its
2076 -- second occurrence, the error is reported, and the tree traversal
2077 -- is abandoned.
2078
2079 function Get_Function_Id (Call : Node_Id) return Entity_Id;
2080 -- Return the entity associated with the function call
2081
2082 procedure Preanalyze_Without_Errors (N : Node_Id);
2083 -- Preanalyze N without reporting errors. Very dubious, you can't just
2084 -- go analyzing things more than once???
2085
2086 -------------------------
2087 -- Collect_Identifiers --
2088 -------------------------
2089
2090 procedure Collect_Identifiers (N : Node_Id) is
2091
2092 function Check_Node (N : Node_Id) return Traverse_Result;
2093 -- Process a single node during the tree traversal to collect the
2094 -- writable actuals of functions and all the identifiers which are
2095 -- not writable actuals of functions.
2096
2097 function Contains (List : Elist_Id; N : Node_Id) return Boolean;
2098 -- Returns True if List has a node whose Entity is Entity (N)
2099
2100 -------------------------
2101 -- Check_Function_Call --
2102 -------------------------
2103
2104 function Check_Node (N : Node_Id) return Traverse_Result is
2105 Is_Writable_Actual : Boolean := False;
2106 Id : Entity_Id;
2107
2108 begin
2109 if Nkind (N) = N_Identifier then
2110
2111 -- No analysis possible if the entity is not decorated
2112
2113 if No (Entity (N)) then
2114 return Skip;
2115
2116 -- Don't collect identifiers of packages, called functions, etc
2117
2118 elsif Ekind_In (Entity (N), E_Package,
2119 E_Function,
2120 E_Procedure,
2121 E_Entry)
2122 then
2123 return Skip;
2124
2125 -- Analyze if N is a writable actual of a function
2126
2127 elsif Nkind (Parent (N)) = N_Function_Call then
2128 declare
2129 Call : constant Node_Id := Parent (N);
2130 Actual : Node_Id;
2131 Formal : Node_Id;
2132
2133 begin
2134 Id := Get_Function_Id (Call);
2135
2136 -- In case of previous error, no check is possible
2137
2138 if No (Id) then
2139 return Abandon;
2140 end if;
2141
2142 Formal := First_Formal (Id);
2143 Actual := First_Actual (Call);
2144 while Present (Actual) and then Present (Formal) loop
2145 if Actual = N then
2146 if Ekind_In (Formal, E_Out_Parameter,
2147 E_In_Out_Parameter)
2148 then
2149 Is_Writable_Actual := True;
2150 end if;
2151
2152 exit;
2153 end if;
2154
2155 Next_Formal (Formal);
2156 Next_Actual (Actual);
2157 end loop;
2158 end;
2159 end if;
2160
2161 if Is_Writable_Actual then
2162 if Contains (Writable_Actuals_List, N) then
2163 Error_Msg_NE
2164 ("value may be affected by call to& "
2165 & "because order of evaluation is arbitrary", N, Id);
2166 Error_Node := N;
2167 return Abandon;
2168 end if;
2169
2170 Append_New_Elmt (N, To => Writable_Actuals_List);
2171
2172 else
2173 if Identifiers_List = No_Elist then
2174 Identifiers_List := New_Elmt_List;
2175 end if;
2176
2177 Append_Unique_Elmt (N, Identifiers_List);
2178 end if;
2179 end if;
2180
2181 return OK;
2182 end Check_Node;
2183
2184 --------------
2185 -- Contains --
2186 --------------
2187
2188 function Contains
2189 (List : Elist_Id;
2190 N : Node_Id) return Boolean
2191 is
2192 pragma Assert (Nkind (N) in N_Has_Entity);
2193
2194 Elmt : Elmt_Id;
2195
2196 begin
2197 if List = No_Elist then
2198 return False;
2199 end if;
2200
2201 Elmt := First_Elmt (List);
2202 while Present (Elmt) loop
2203 if Entity (Node (Elmt)) = Entity (N) then
2204 return True;
2205 else
2206 Next_Elmt (Elmt);
2207 end if;
2208 end loop;
2209
2210 return False;
2211 end Contains;
2212
2213 ------------------
2214 -- Do_Traversal --
2215 ------------------
2216
2217 procedure Do_Traversal is new Traverse_Proc (Check_Node);
2218 -- The traversal procedure
2219
2220 -- Start of processing for Collect_Identifiers
2221
2222 begin
2223 if Present (Error_Node) then
2224 return;
2225 end if;
2226
2227 if Nkind (N) in N_Subexpr and then Is_OK_Static_Expression (N) then
2228 return;
2229 end if;
2230
2231 Do_Traversal (N);
2232 end Collect_Identifiers;
2233
2234 ---------------------
2235 -- Get_Function_Id --
2236 ---------------------
2237
2238 function Get_Function_Id (Call : Node_Id) return Entity_Id is
2239 Nam : constant Node_Id := Name (Call);
2240 Id : Entity_Id;
2241
2242 begin
2243 if Nkind (Nam) = N_Explicit_Dereference then
2244 Id := Etype (Nam);
2245 pragma Assert (Ekind (Id) = E_Subprogram_Type);
2246
2247 elsif Nkind (Nam) = N_Selected_Component then
2248 Id := Entity (Selector_Name (Nam));
2249
2250 elsif Nkind (Nam) = N_Indexed_Component then
2251 Id := Entity (Selector_Name (Prefix (Nam)));
2252
2253 else
2254 Id := Entity (Nam);
2255 end if;
2256
2257 return Id;
2258 end Get_Function_Id;
2259
2260 ---------------------------
2261 -- Preanalyze_Expression --
2262 ---------------------------
2263
2264 procedure Preanalyze_Without_Errors (N : Node_Id) is
2265 Status : constant Boolean := Get_Ignore_Errors;
2266 begin
2267 Set_Ignore_Errors (True);
2268 Preanalyze (N);
2269 Set_Ignore_Errors (Status);
2270 end Preanalyze_Without_Errors;
2271
2272 -- Start of processing for Check_Function_Writable_Actuals
2273
2274 begin
2275 -- The check only applies to Ada 2012 code, and only to constructs that
2276 -- have multiple constituents whose order of evaluation is not specified
2277 -- by the language.
2278
2279 if Ada_Version < Ada_2012
2280 or else (not (Nkind (N) in N_Op)
2281 and then not (Nkind (N) in N_Membership_Test)
2282 and then not Nkind_In (N, N_Range,
2283 N_Aggregate,
2284 N_Extension_Aggregate,
2285 N_Full_Type_Declaration,
2286 N_Function_Call,
2287 N_Procedure_Call_Statement,
2288 N_Entry_Call_Statement))
2289 or else (Nkind (N) = N_Full_Type_Declaration
2290 and then not Is_Record_Type (Defining_Identifier (N)))
2291
2292 -- In addition, this check only applies to source code, not to code
2293 -- generated by constraint checks.
2294
2295 or else not Comes_From_Source (N)
2296 then
2297 return;
2298 end if;
2299
2300 -- If a construct C has two or more direct constituents that are names
2301 -- or expressions whose evaluation may occur in an arbitrary order, at
2302 -- least one of which contains a function call with an in out or out
2303 -- parameter, then the construct is legal only if: for each name N that
2304 -- is passed as a parameter of mode in out or out to some inner function
2305 -- call C2 (not including the construct C itself), there is no other
2306 -- name anywhere within a direct constituent of the construct C other
2307 -- than the one containing C2, that is known to refer to the same
2308 -- object (RM 6.4.1(6.17/3)).
2309
2310 case Nkind (N) is
2311 when N_Range =>
2312 Collect_Identifiers (Low_Bound (N));
2313 Collect_Identifiers (High_Bound (N));
2314
2315 when N_Op | N_Membership_Test =>
2316 declare
2317 Expr : Node_Id;
2318
2319 begin
2320 Collect_Identifiers (Left_Opnd (N));
2321
2322 if Present (Right_Opnd (N)) then
2323 Collect_Identifiers (Right_Opnd (N));
2324 end if;
2325
2326 if Nkind_In (N, N_In, N_Not_In)
2327 and then Present (Alternatives (N))
2328 then
2329 Expr := First (Alternatives (N));
2330 while Present (Expr) loop
2331 Collect_Identifiers (Expr);
2332
2333 Next (Expr);
2334 end loop;
2335 end if;
2336 end;
2337
2338 when N_Full_Type_Declaration =>
2339 declare
2340 function Get_Record_Part (N : Node_Id) return Node_Id;
2341 -- Return the record part of this record type definition
2342
2343 function Get_Record_Part (N : Node_Id) return Node_Id is
2344 Type_Def : constant Node_Id := Type_Definition (N);
2345 begin
2346 if Nkind (Type_Def) = N_Derived_Type_Definition then
2347 return Record_Extension_Part (Type_Def);
2348 else
2349 return Type_Def;
2350 end if;
2351 end Get_Record_Part;
2352
2353 Comp : Node_Id;
2354 Def_Id : Entity_Id := Defining_Identifier (N);
2355 Rec : Node_Id := Get_Record_Part (N);
2356
2357 begin
2358 -- No need to perform any analysis if the record has no
2359 -- components
2360
2361 if No (Rec) or else No (Component_List (Rec)) then
2362 return;
2363 end if;
2364
2365 -- Collect the identifiers starting from the deepest
2366 -- derivation. Done to report the error in the deepest
2367 -- derivation.
2368
2369 loop
2370 if Present (Component_List (Rec)) then
2371 Comp := First (Component_Items (Component_List (Rec)));
2372 while Present (Comp) loop
2373 if Nkind (Comp) = N_Component_Declaration
2374 and then Present (Expression (Comp))
2375 then
2376 Collect_Identifiers (Expression (Comp));
2377 end if;
2378
2379 Next (Comp);
2380 end loop;
2381 end if;
2382
2383 exit when No (Underlying_Type (Etype (Def_Id)))
2384 or else Base_Type (Underlying_Type (Etype (Def_Id)))
2385 = Def_Id;
2386
2387 Def_Id := Base_Type (Underlying_Type (Etype (Def_Id)));
2388 Rec := Get_Record_Part (Parent (Def_Id));
2389 end loop;
2390 end;
2391
2392 when N_Subprogram_Call |
2393 N_Entry_Call_Statement =>
2394 declare
2395 Id : constant Entity_Id := Get_Function_Id (N);
2396 Formal : Node_Id;
2397 Actual : Node_Id;
2398
2399 begin
2400 Formal := First_Formal (Id);
2401 Actual := First_Actual (N);
2402 while Present (Actual) and then Present (Formal) loop
2403 if Ekind_In (Formal, E_Out_Parameter,
2404 E_In_Out_Parameter)
2405 then
2406 Collect_Identifiers (Actual);
2407 end if;
2408
2409 Next_Formal (Formal);
2410 Next_Actual (Actual);
2411 end loop;
2412 end;
2413
2414 when N_Aggregate |
2415 N_Extension_Aggregate =>
2416 declare
2417 Assoc : Node_Id;
2418 Choice : Node_Id;
2419 Comp_Expr : Node_Id;
2420
2421 begin
2422 -- Handle the N_Others_Choice of array aggregates with static
2423 -- bounds. There is no need to perform this analysis in
2424 -- aggregates without static bounds since we cannot evaluate
2425 -- if the N_Others_Choice covers several elements. There is
2426 -- no need to handle the N_Others choice of record aggregates
2427 -- since at this stage it has been already expanded by
2428 -- Resolve_Record_Aggregate.
2429
2430 if Is_Array_Type (Etype (N))
2431 and then Nkind (N) = N_Aggregate
2432 and then Present (Aggregate_Bounds (N))
2433 and then Compile_Time_Known_Bounds (Etype (N))
2434 and then Expr_Value (High_Bound (Aggregate_Bounds (N)))
2435 >
2436 Expr_Value (Low_Bound (Aggregate_Bounds (N)))
2437 then
2438 declare
2439 Count_Components : Uint := Uint_0;
2440 Num_Components : Uint;
2441 Others_Assoc : Node_Id;
2442 Others_Choice : Node_Id := Empty;
2443 Others_Box_Present : Boolean := False;
2444
2445 begin
2446 -- Count positional associations
2447
2448 if Present (Expressions (N)) then
2449 Comp_Expr := First (Expressions (N));
2450 while Present (Comp_Expr) loop
2451 Count_Components := Count_Components + 1;
2452 Next (Comp_Expr);
2453 end loop;
2454 end if;
2455
2456 -- Count the rest of elements and locate the N_Others
2457 -- choice (if any)
2458
2459 Assoc := First (Component_Associations (N));
2460 while Present (Assoc) loop
2461 Choice := First (Choices (Assoc));
2462 while Present (Choice) loop
2463 if Nkind (Choice) = N_Others_Choice then
2464 Others_Assoc := Assoc;
2465 Others_Choice := Choice;
2466 Others_Box_Present := Box_Present (Assoc);
2467
2468 -- Count several components
2469
2470 elsif Nkind_In (Choice, N_Range,
2471 N_Subtype_Indication)
2472 or else (Is_Entity_Name (Choice)
2473 and then Is_Type (Entity (Choice)))
2474 then
2475 declare
2476 L, H : Node_Id;
2477 begin
2478 Get_Index_Bounds (Choice, L, H);
2479 pragma Assert
2480 (Compile_Time_Known_Value (L)
2481 and then Compile_Time_Known_Value (H));
2482 Count_Components :=
2483 Count_Components
2484 + Expr_Value (H) - Expr_Value (L) + 1;
2485 end;
2486
2487 -- Count single component. No other case available
2488 -- since we are handling an aggregate with static
2489 -- bounds.
2490
2491 else
2492 pragma Assert (Is_OK_Static_Expression (Choice)
2493 or else Nkind (Choice) = N_Identifier
2494 or else Nkind (Choice) = N_Integer_Literal);
2495
2496 Count_Components := Count_Components + 1;
2497 end if;
2498
2499 Next (Choice);
2500 end loop;
2501
2502 Next (Assoc);
2503 end loop;
2504
2505 Num_Components :=
2506 Expr_Value (High_Bound (Aggregate_Bounds (N))) -
2507 Expr_Value (Low_Bound (Aggregate_Bounds (N))) + 1;
2508
2509 pragma Assert (Count_Components <= Num_Components);
2510
2511 -- Handle the N_Others choice if it covers several
2512 -- components
2513
2514 if Present (Others_Choice)
2515 and then (Num_Components - Count_Components) > 1
2516 then
2517 if not Others_Box_Present then
2518
2519 -- At this stage, if expansion is active, the
2520 -- expression of the others choice has not been
2521 -- analyzed. Hence we generate a duplicate and
2522 -- we analyze it silently to have available the
2523 -- minimum decoration required to collect the
2524 -- identifiers.
2525
2526 if not Expander_Active then
2527 Comp_Expr := Expression (Others_Assoc);
2528 else
2529 Comp_Expr :=
2530 New_Copy_Tree (Expression (Others_Assoc));
2531 Preanalyze_Without_Errors (Comp_Expr);
2532 end if;
2533
2534 Collect_Identifiers (Comp_Expr);
2535
2536 if Writable_Actuals_List /= No_Elist then
2537
2538 -- As suggested by Robert, at current stage we
2539 -- report occurrences of this case as warnings.
2540
2541 Error_Msg_N
2542 ("writable function parameter may affect "
2543 & "value in other component because order "
2544 & "of evaluation is unspecified??",
2545 Node (First_Elmt (Writable_Actuals_List)));
2546 end if;
2547 end if;
2548 end if;
2549 end;
2550 end if;
2551
2552 -- Handle ancestor part of extension aggregates
2553
2554 if Nkind (N) = N_Extension_Aggregate then
2555 Collect_Identifiers (Ancestor_Part (N));
2556 end if;
2557
2558 -- Handle positional associations
2559
2560 if Present (Expressions (N)) then
2561 Comp_Expr := First (Expressions (N));
2562 while Present (Comp_Expr) loop
2563 if not Is_OK_Static_Expression (Comp_Expr) then
2564 Collect_Identifiers (Comp_Expr);
2565 end if;
2566
2567 Next (Comp_Expr);
2568 end loop;
2569 end if;
2570
2571 -- Handle discrete associations
2572
2573 if Present (Component_Associations (N)) then
2574 Assoc := First (Component_Associations (N));
2575 while Present (Assoc) loop
2576
2577 if not Box_Present (Assoc) then
2578 Choice := First (Choices (Assoc));
2579 while Present (Choice) loop
2580
2581 -- For now we skip discriminants since it requires
2582 -- performing the analysis in two phases: first one
2583 -- analyzing discriminants and second one analyzing
2584 -- the rest of components since discriminants are
2585 -- evaluated prior to components: too much extra
2586 -- work to detect a corner case???
2587
2588 if Nkind (Choice) in N_Has_Entity
2589 and then Present (Entity (Choice))
2590 and then Ekind (Entity (Choice)) = E_Discriminant
2591 then
2592 null;
2593
2594 elsif Box_Present (Assoc) then
2595 null;
2596
2597 else
2598 if not Analyzed (Expression (Assoc)) then
2599 Comp_Expr :=
2600 New_Copy_Tree (Expression (Assoc));
2601 Set_Parent (Comp_Expr, Parent (N));
2602 Preanalyze_Without_Errors (Comp_Expr);
2603 else
2604 Comp_Expr := Expression (Assoc);
2605 end if;
2606
2607 Collect_Identifiers (Comp_Expr);
2608 end if;
2609
2610 Next (Choice);
2611 end loop;
2612 end if;
2613
2614 Next (Assoc);
2615 end loop;
2616 end if;
2617 end;
2618
2619 when others =>
2620 return;
2621 end case;
2622
2623 -- No further action needed if we already reported an error
2624
2625 if Present (Error_Node) then
2626 return;
2627 end if;
2628
2629 -- Check if some writable argument of a function is referenced
2630
2631 if Writable_Actuals_List /= No_Elist
2632 and then Identifiers_List /= No_Elist
2633 then
2634 declare
2635 Elmt_1 : Elmt_Id;
2636 Elmt_2 : Elmt_Id;
2637
2638 begin
2639 Elmt_1 := First_Elmt (Writable_Actuals_List);
2640 while Present (Elmt_1) loop
2641 Elmt_2 := First_Elmt (Identifiers_List);
2642 while Present (Elmt_2) loop
2643 if Entity (Node (Elmt_1)) = Entity (Node (Elmt_2)) then
2644 case Nkind (Parent (Node (Elmt_2))) is
2645 when N_Aggregate |
2646 N_Component_Association |
2647 N_Component_Declaration =>
2648 Error_Msg_N
2649 ("value may be affected by call in other "
2650 & "component because they are evaluated "
2651 & "in unspecified order",
2652 Node (Elmt_2));
2653
2654 when N_In | N_Not_In =>
2655 Error_Msg_N
2656 ("value may be affected by call in other "
2657 & "alternative because they are evaluated "
2658 & "in unspecified order",
2659 Node (Elmt_2));
2660
2661 when others =>
2662 Error_Msg_N
2663 ("value of actual may be affected by call in "
2664 & "other actual because they are evaluated "
2665 & "in unspecified order",
2666 Node (Elmt_2));
2667 end case;
2668 end if;
2669
2670 Next_Elmt (Elmt_2);
2671 end loop;
2672
2673 Next_Elmt (Elmt_1);
2674 end loop;
2675 end;
2676 end if;
2677 end Check_Function_Writable_Actuals;
2678
2679 ----------------------------
2680 -- Check_Ghost_Completion --
2681 ----------------------------
2682
2683 procedure Check_Ghost_Completion
2684 (Partial_View : Entity_Id;
2685 Full_View : Entity_Id)
2686 is
2687 Policy : constant Name_Id := Policy_In_Effect (Name_Ghost);
2688
2689 begin
2690 -- The Ghost policy in effect at the point of declaration and at the
2691 -- point of completion must match (SPARK RM 6.9(15)).
2692
2693 if Is_Checked_Ghost_Entity (Partial_View)
2694 and then Policy = Name_Ignore
2695 then
2696 Error_Msg_Sloc := Sloc (Full_View);
2697
2698 Error_Msg_N ("incompatible ghost policies in effect", Partial_View);
2699 Error_Msg_N ("\& declared with ghost policy Check", Partial_View);
2700 Error_Msg_N ("\& completed # with ghost policy Ignore", Partial_View);
2701
2702 elsif Is_Ignored_Ghost_Entity (Partial_View)
2703 and then Policy = Name_Check
2704 then
2705 Error_Msg_Sloc := Sloc (Full_View);
2706
2707 Error_Msg_N ("incompatible ghost policies in effect", Partial_View);
2708 Error_Msg_N ("\& declared with ghost policy Ignore", Partial_View);
2709 Error_Msg_N ("\& completed # with ghost policy Check", Partial_View);
2710 end if;
2711 end Check_Ghost_Completion;
2712
2713 ----------------------------
2714 -- Check_Ghost_Derivation --
2715 ----------------------------
2716
2717 procedure Check_Ghost_Derivation (Typ : Entity_Id) is
2718 Parent_Typ : constant Entity_Id := Etype (Typ);
2719 Iface : Entity_Id;
2720 Iface_Elmt : Elmt_Id;
2721
2722 begin
2723 -- Allow untagged derivations from predefined types such as Integer as
2724 -- those are not Ghost by definition.
2725
2726 if Is_Scalar_Type (Typ) and then Parent_Typ = Base_Type (Typ) then
2727 null;
2728
2729 -- The parent type of a Ghost type extension must be Ghost
2730
2731 elsif not Is_Ghost_Entity (Parent_Typ) then
2732 Error_Msg_N ("type extension & cannot be ghost", Typ);
2733 Error_Msg_NE ("\parent type & is not ghost", Typ, Parent_Typ);
2734 return;
2735 end if;
2736
2737 -- All progenitors (if any) must be Ghost as well
2738
2739 if Is_Tagged_Type (Typ) and then Present (Interfaces (Typ)) then
2740 Iface_Elmt := First_Elmt (Interfaces (Typ));
2741 while Present (Iface_Elmt) loop
2742 Iface := Node (Iface_Elmt);
2743
2744 if not Is_Ghost_Entity (Iface) then
2745 Error_Msg_N ("type extension & cannot be ghost", Typ);
2746 Error_Msg_NE ("\interface type & is not ghost", Typ, Iface);
2747 return;
2748 end if;
2749
2750 Next_Elmt (Iface_Elmt);
2751 end loop;
2752 end if;
2753 end Check_Ghost_Derivation;
2754
2755 --------------------------------
2756 -- Check_Implicit_Dereference --
2757 --------------------------------
2758
2759 procedure Check_Implicit_Dereference (N : Node_Id; Typ : Entity_Id) is
2760 Disc : Entity_Id;
2761 Desig : Entity_Id;
2762 Nam : Node_Id;
2763
2764 begin
2765 if Nkind (N) = N_Indexed_Component
2766 and then Present (Generalized_Indexing (N))
2767 then
2768 Nam := Generalized_Indexing (N);
2769 else
2770 Nam := N;
2771 end if;
2772
2773 if Ada_Version < Ada_2012
2774 or else not Has_Implicit_Dereference (Base_Type (Typ))
2775 then
2776 return;
2777
2778 elsif not Comes_From_Source (N)
2779 and then Nkind (N) /= N_Indexed_Component
2780 then
2781 return;
2782
2783 elsif Is_Entity_Name (Nam) and then Is_Type (Entity (Nam)) then
2784 null;
2785
2786 else
2787 Disc := First_Discriminant (Typ);
2788 while Present (Disc) loop
2789 if Has_Implicit_Dereference (Disc) then
2790 Desig := Designated_Type (Etype (Disc));
2791 Add_One_Interp (Nam, Disc, Desig);
2792
2793 -- If the node is a generalized indexing, add interpretation
2794 -- to that node as well, for subsequent resolution.
2795
2796 if Nkind (N) = N_Indexed_Component then
2797 Add_One_Interp (N, Disc, Desig);
2798 end if;
2799
2800 -- If the operation comes from a generic unit and the context
2801 -- is a selected component, the selector name may be global
2802 -- and set in the instance already. Remove the entity to
2803 -- force resolution of the selected component, and the
2804 -- generation of an explicit dereference if needed.
2805
2806 if In_Instance
2807 and then Nkind (Parent (Nam)) = N_Selected_Component
2808 then
2809 Set_Entity (Selector_Name (Parent (Nam)), Empty);
2810 end if;
2811
2812 exit;
2813 end if;
2814
2815 Next_Discriminant (Disc);
2816 end loop;
2817 end if;
2818 end Check_Implicit_Dereference;
2819
2820 ----------------------------------
2821 -- Check_Internal_Protected_Use --
2822 ----------------------------------
2823
2824 procedure Check_Internal_Protected_Use (N : Node_Id; Nam : Entity_Id) is
2825 S : Entity_Id;
2826 Prot : Entity_Id;
2827
2828 begin
2829 S := Current_Scope;
2830 while Present (S) loop
2831 if S = Standard_Standard then
2832 return;
2833
2834 elsif Ekind (S) = E_Function
2835 and then Ekind (Scope (S)) = E_Protected_Type
2836 then
2837 Prot := Scope (S);
2838 exit;
2839 end if;
2840
2841 S := Scope (S);
2842 end loop;
2843
2844 if Scope (Nam) = Prot and then Ekind (Nam) /= E_Function then
2845
2846 -- An indirect function call (e.g. a callback within a protected
2847 -- function body) is not statically illegal. If the access type is
2848 -- anonymous and is the type of an access parameter, the scope of Nam
2849 -- will be the protected type, but it is not a protected operation.
2850
2851 if Ekind (Nam) = E_Subprogram_Type
2852 and then
2853 Nkind (Associated_Node_For_Itype (Nam)) = N_Function_Specification
2854 then
2855 null;
2856
2857 elsif Nkind (N) = N_Subprogram_Renaming_Declaration then
2858 Error_Msg_N
2859 ("within protected function cannot use protected "
2860 & "procedure in renaming or as generic actual", N);
2861
2862 elsif Nkind (N) = N_Attribute_Reference then
2863 Error_Msg_N
2864 ("within protected function cannot take access of "
2865 & " protected procedure", N);
2866
2867 else
2868 Error_Msg_N
2869 ("within protected function, protected object is constant", N);
2870 Error_Msg_N
2871 ("\cannot call operation that may modify it", N);
2872 end if;
2873 end if;
2874 end Check_Internal_Protected_Use;
2875
2876 ---------------------------------------
2877 -- Check_Later_Vs_Basic_Declarations --
2878 ---------------------------------------
2879
2880 procedure Check_Later_Vs_Basic_Declarations
2881 (Decls : List_Id;
2882 During_Parsing : Boolean)
2883 is
2884 Body_Sloc : Source_Ptr;
2885 Decl : Node_Id;
2886
2887 function Is_Later_Declarative_Item (Decl : Node_Id) return Boolean;
2888 -- Return whether Decl is considered as a declarative item.
2889 -- When During_Parsing is True, the semantics of Ada 83 is followed.
2890 -- When During_Parsing is False, the semantics of SPARK is followed.
2891
2892 -------------------------------
2893 -- Is_Later_Declarative_Item --
2894 -------------------------------
2895
2896 function Is_Later_Declarative_Item (Decl : Node_Id) return Boolean is
2897 begin
2898 if Nkind (Decl) in N_Later_Decl_Item then
2899 return True;
2900
2901 elsif Nkind (Decl) = N_Pragma then
2902 return True;
2903
2904 elsif During_Parsing then
2905 return False;
2906
2907 -- In SPARK, a package declaration is not considered as a later
2908 -- declarative item.
2909
2910 elsif Nkind (Decl) = N_Package_Declaration then
2911 return False;
2912
2913 -- In SPARK, a renaming is considered as a later declarative item
2914
2915 elsif Nkind (Decl) in N_Renaming_Declaration then
2916 return True;
2917
2918 else
2919 return False;
2920 end if;
2921 end Is_Later_Declarative_Item;
2922
2923 -- Start of Check_Later_Vs_Basic_Declarations
2924
2925 begin
2926 Decl := First (Decls);
2927
2928 -- Loop through sequence of basic declarative items
2929
2930 Outer : while Present (Decl) loop
2931 if not Nkind_In (Decl, N_Subprogram_Body, N_Package_Body, N_Task_Body)
2932 and then Nkind (Decl) not in N_Body_Stub
2933 then
2934 Next (Decl);
2935
2936 -- Once a body is encountered, we only allow later declarative
2937 -- items. The inner loop checks the rest of the list.
2938
2939 else
2940 Body_Sloc := Sloc (Decl);
2941
2942 Inner : while Present (Decl) loop
2943 if not Is_Later_Declarative_Item (Decl) then
2944 if During_Parsing then
2945 if Ada_Version = Ada_83 then
2946 Error_Msg_Sloc := Body_Sloc;
2947 Error_Msg_N
2948 ("(Ada 83) decl cannot appear after body#", Decl);
2949 end if;
2950 else
2951 Error_Msg_Sloc := Body_Sloc;
2952 Check_SPARK_05_Restriction
2953 ("decl cannot appear after body#", Decl);
2954 end if;
2955 end if;
2956
2957 Next (Decl);
2958 end loop Inner;
2959 end if;
2960 end loop Outer;
2961 end Check_Later_Vs_Basic_Declarations;
2962
2963 -------------------------
2964 -- Check_Nested_Access --
2965 -------------------------
2966
2967 procedure Check_Nested_Access (Ent : Entity_Id) is
2968 Scop : constant Entity_Id := Current_Scope;
2969 Current_Subp : Entity_Id;
2970 Enclosing : Entity_Id;
2971
2972 begin
2973 -- Currently only enabled for VM back-ends for efficiency, should we
2974 -- enable it more systematically ???
2975
2976 -- Check for Is_Imported needs commenting below ???
2977
2978 if VM_Target /= No_VM
2979 and then Ekind_In (Ent, E_Variable, E_Constant, E_Loop_Parameter)
2980 and then Scope (Ent) /= Empty
2981 and then not Is_Library_Level_Entity (Ent)
2982 and then not Is_Imported (Ent)
2983 then
2984 if Is_Subprogram (Scop)
2985 or else Is_Generic_Subprogram (Scop)
2986 or else Is_Entry (Scop)
2987 then
2988 Current_Subp := Scop;
2989 else
2990 Current_Subp := Current_Subprogram;
2991 end if;
2992
2993 Enclosing := Enclosing_Subprogram (Ent);
2994
2995 if Enclosing /= Empty and then Enclosing /= Current_Subp then
2996 Set_Has_Up_Level_Access (Ent, True);
2997 end if;
2998 end if;
2999 end Check_Nested_Access;
3000
3001 ---------------------------
3002 -- Check_No_Hidden_State --
3003 ---------------------------
3004
3005 procedure Check_No_Hidden_State (Id : Entity_Id) is
3006 function Has_Null_Abstract_State (Pkg : Entity_Id) return Boolean;
3007 -- Determine whether the entity of a package denoted by Pkg has a null
3008 -- abstract state.
3009
3010 -----------------------------
3011 -- Has_Null_Abstract_State --
3012 -----------------------------
3013
3014 function Has_Null_Abstract_State (Pkg : Entity_Id) return Boolean is
3015 States : constant Elist_Id := Abstract_States (Pkg);
3016
3017 begin
3018 -- Check first available state of related package. A null abstract
3019 -- state always appears as the sole element of the state list.
3020
3021 return
3022 Present (States)
3023 and then Is_Null_State (Node (First_Elmt (States)));
3024 end Has_Null_Abstract_State;
3025
3026 -- Local variables
3027
3028 Context : Entity_Id := Empty;
3029 Not_Visible : Boolean := False;
3030 Scop : Entity_Id;
3031
3032 -- Start of processing for Check_No_Hidden_State
3033
3034 begin
3035 pragma Assert (Ekind_In (Id, E_Abstract_State, E_Variable));
3036
3037 -- Find the proper context where the object or state appears
3038
3039 Scop := Scope (Id);
3040 while Present (Scop) loop
3041 Context := Scop;
3042
3043 -- Keep track of the context's visibility
3044
3045 Not_Visible := Not_Visible or else In_Private_Part (Context);
3046
3047 -- Prevent the search from going too far
3048
3049 if Context = Standard_Standard then
3050 return;
3051
3052 -- Objects and states that appear immediately within a subprogram or
3053 -- inside a construct nested within a subprogram do not introduce a
3054 -- hidden state. They behave as local variable declarations.
3055
3056 elsif Is_Subprogram (Context) then
3057 return;
3058
3059 -- When examining a package body, use the entity of the spec as it
3060 -- carries the abstract state declarations.
3061
3062 elsif Ekind (Context) = E_Package_Body then
3063 Context := Spec_Entity (Context);
3064 end if;
3065
3066 -- Stop the traversal when a package subject to a null abstract state
3067 -- has been found.
3068
3069 if Ekind_In (Context, E_Generic_Package, E_Package)
3070 and then Has_Null_Abstract_State (Context)
3071 then
3072 exit;
3073 end if;
3074
3075 Scop := Scope (Scop);
3076 end loop;
3077
3078 -- At this point we know that there is at least one package with a null
3079 -- abstract state in visibility. Emit an error message unconditionally
3080 -- if the entity being processed is a state because the placement of the
3081 -- related package is irrelevant. This is not the case for objects as
3082 -- the intermediate context matters.
3083
3084 if Present (Context)
3085 and then (Ekind (Id) = E_Abstract_State or else Not_Visible)
3086 then
3087 Error_Msg_N ("cannot introduce hidden state &", Id);
3088 Error_Msg_NE ("\package & has null abstract state", Id, Context);
3089 end if;
3090 end Check_No_Hidden_State;
3091
3092 ------------------------------------------
3093 -- Check_Potentially_Blocking_Operation --
3094 ------------------------------------------
3095
3096 procedure Check_Potentially_Blocking_Operation (N : Node_Id) is
3097 S : Entity_Id;
3098
3099 begin
3100 -- N is one of the potentially blocking operations listed in 9.5.1(8).
3101 -- When pragma Detect_Blocking is active, the run time will raise
3102 -- Program_Error. Here we only issue a warning, since we generally
3103 -- support the use of potentially blocking operations in the absence
3104 -- of the pragma.
3105
3106 -- Indirect blocking through a subprogram call cannot be diagnosed
3107 -- statically without interprocedural analysis, so we do not attempt
3108 -- to do it here.
3109
3110 S := Scope (Current_Scope);
3111 while Present (S) and then S /= Standard_Standard loop
3112 if Is_Protected_Type (S) then
3113 Error_Msg_N
3114 ("potentially blocking operation in protected operation??", N);
3115 return;
3116 end if;
3117
3118 S := Scope (S);
3119 end loop;
3120 end Check_Potentially_Blocking_Operation;
3121
3122 ---------------------------------
3123 -- Check_Result_And_Post_State --
3124 ---------------------------------
3125
3126 procedure Check_Result_And_Post_State
3127 (Prag : Node_Id;
3128 Result_Seen : in out Boolean)
3129 is
3130 procedure Check_Expression (Expr : Node_Id);
3131 -- Perform the 'Result and post-state checks on a given expression
3132
3133 function Is_Function_Result (N : Node_Id) return Traverse_Result;
3134 -- Attempt to find attribute 'Result in a subtree denoted by N
3135
3136 function Is_Trivial_Boolean (N : Node_Id) return Boolean;
3137 -- Determine whether source node N denotes "True" or "False"
3138
3139 function Mentions_Post_State (N : Node_Id) return Boolean;
3140 -- Determine whether a subtree denoted by N mentions any construct that
3141 -- denotes a post-state.
3142
3143 procedure Check_Function_Result is
3144 new Traverse_Proc (Is_Function_Result);
3145
3146 ----------------------
3147 -- Check_Expression --
3148 ----------------------
3149
3150 procedure Check_Expression (Expr : Node_Id) is
3151 begin
3152 if not Is_Trivial_Boolean (Expr) then
3153 Check_Function_Result (Expr);
3154
3155 if not Mentions_Post_State (Expr) then
3156 if Pragma_Name (Prag) = Name_Contract_Cases then
3157 Error_Msg_N
3158 ("contract case refers only to pre-state?T?", Expr);
3159
3160 elsif Pragma_Name (Prag) = Name_Refined_Post then
3161 Error_Msg_N
3162 ("refined postcondition refers only to pre-state?T?",
3163 Prag);
3164
3165 else
3166 Error_Msg_N
3167 ("postcondition refers only to pre-state?T?", Prag);
3168 end if;
3169 end if;
3170 end if;
3171 end Check_Expression;
3172
3173 ------------------------
3174 -- Is_Function_Result --
3175 ------------------------
3176
3177 function Is_Function_Result (N : Node_Id) return Traverse_Result is
3178 begin
3179 if Is_Attribute_Result (N) then
3180 Result_Seen := True;
3181 return Abandon;
3182
3183 -- Continue the traversal
3184
3185 else
3186 return OK;
3187 end if;
3188 end Is_Function_Result;
3189
3190 ------------------------
3191 -- Is_Trivial_Boolean --
3192 ------------------------
3193
3194 function Is_Trivial_Boolean (N : Node_Id) return Boolean is
3195 begin
3196 return
3197 Comes_From_Source (N)
3198 and then Is_Entity_Name (N)
3199 and then (Entity (N) = Standard_True
3200 or else
3201 Entity (N) = Standard_False);
3202 end Is_Trivial_Boolean;
3203
3204 -------------------------
3205 -- Mentions_Post_State --
3206 -------------------------
3207
3208 function Mentions_Post_State (N : Node_Id) return Boolean is
3209 Post_State_Seen : Boolean := False;
3210
3211 function Is_Post_State (N : Node_Id) return Traverse_Result;
3212 -- Attempt to find a construct that denotes a post-state. If this is
3213 -- the case, set flag Post_State_Seen.
3214
3215 -------------------
3216 -- Is_Post_State --
3217 -------------------
3218
3219 function Is_Post_State (N : Node_Id) return Traverse_Result is
3220 Ent : Entity_Id;
3221
3222 begin
3223 if Nkind_In (N, N_Explicit_Dereference, N_Function_Call) then
3224 Post_State_Seen := True;
3225 return Abandon;
3226
3227 elsif Nkind_In (N, N_Expanded_Name, N_Identifier) then
3228 Ent := Entity (N);
3229
3230 -- The entity may be modifiable through an implicit dereference
3231
3232 if No (Ent)
3233 or else Ekind (Ent) in Assignable_Kind
3234 or else (Is_Access_Type (Etype (Ent))
3235 and then Nkind (Parent (N)) = N_Selected_Component)
3236 then
3237 Post_State_Seen := True;
3238 return Abandon;
3239 end if;
3240
3241 elsif Nkind (N) = N_Attribute_Reference then
3242 if Attribute_Name (N) = Name_Old then
3243 return Skip;
3244
3245 elsif Attribute_Name (N) = Name_Result then
3246 Post_State_Seen := True;
3247 return Abandon;
3248 end if;
3249 end if;
3250
3251 return OK;
3252 end Is_Post_State;
3253
3254 procedure Find_Post_State is new Traverse_Proc (Is_Post_State);
3255
3256 -- Start of processing for Mentions_Post_State
3257
3258 begin
3259 Find_Post_State (N);
3260
3261 return Post_State_Seen;
3262 end Mentions_Post_State;
3263
3264 -- Local variables
3265
3266 Expr : constant Node_Id :=
3267 Get_Pragma_Arg (First (Pragma_Argument_Associations (Prag)));
3268 Nam : constant Name_Id := Pragma_Name (Prag);
3269 CCase : Node_Id;
3270
3271 -- Start of processing for Check_Result_And_Post_State
3272
3273 begin
3274 -- Examine all consequences
3275
3276 if Nam = Name_Contract_Cases then
3277 CCase := First (Component_Associations (Expr));
3278 while Present (CCase) loop
3279 Check_Expression (Expression (CCase));
3280
3281 Next (CCase);
3282 end loop;
3283
3284 -- Examine the expression of a postcondition
3285
3286 else pragma Assert (Nam_In (Nam, Name_Postcondition, Name_Refined_Post));
3287 Check_Expression (Expr);
3288 end if;
3289 end Check_Result_And_Post_State;
3290
3291 ------------------------------
3292 -- Check_Unprotected_Access --
3293 ------------------------------
3294
3295 procedure Check_Unprotected_Access
3296 (Context : Node_Id;
3297 Expr : Node_Id)
3298 is
3299 Cont_Encl_Typ : Entity_Id;
3300 Pref_Encl_Typ : Entity_Id;
3301
3302 function Enclosing_Protected_Type (Obj : Node_Id) return Entity_Id;
3303 -- Check whether Obj is a private component of a protected object.
3304 -- Return the protected type where the component resides, Empty
3305 -- otherwise.
3306
3307 function Is_Public_Operation return Boolean;
3308 -- Verify that the enclosing operation is callable from outside the
3309 -- protected object, to minimize false positives.
3310
3311 ------------------------------
3312 -- Enclosing_Protected_Type --
3313 ------------------------------
3314
3315 function Enclosing_Protected_Type (Obj : Node_Id) return Entity_Id is
3316 begin
3317 if Is_Entity_Name (Obj) then
3318 declare
3319 Ent : Entity_Id := Entity (Obj);
3320
3321 begin
3322 -- The object can be a renaming of a private component, use
3323 -- the original record component.
3324
3325 if Is_Prival (Ent) then
3326 Ent := Prival_Link (Ent);
3327 end if;
3328
3329 if Is_Protected_Type (Scope (Ent)) then
3330 return Scope (Ent);
3331 end if;
3332 end;
3333 end if;
3334
3335 -- For indexed and selected components, recursively check the prefix
3336
3337 if Nkind_In (Obj, N_Indexed_Component, N_Selected_Component) then
3338 return Enclosing_Protected_Type (Prefix (Obj));
3339
3340 -- The object does not denote a protected component
3341
3342 else
3343 return Empty;
3344 end if;
3345 end Enclosing_Protected_Type;
3346
3347 -------------------------
3348 -- Is_Public_Operation --
3349 -------------------------
3350
3351 function Is_Public_Operation return Boolean is
3352 S : Entity_Id;
3353 E : Entity_Id;
3354
3355 begin
3356 S := Current_Scope;
3357 while Present (S) and then S /= Pref_Encl_Typ loop
3358 if Scope (S) = Pref_Encl_Typ then
3359 E := First_Entity (Pref_Encl_Typ);
3360 while Present (E)
3361 and then E /= First_Private_Entity (Pref_Encl_Typ)
3362 loop
3363 if E = S then
3364 return True;
3365 end if;
3366
3367 Next_Entity (E);
3368 end loop;
3369 end if;
3370
3371 S := Scope (S);
3372 end loop;
3373
3374 return False;
3375 end Is_Public_Operation;
3376
3377 -- Start of processing for Check_Unprotected_Access
3378
3379 begin
3380 if Nkind (Expr) = N_Attribute_Reference
3381 and then Attribute_Name (Expr) = Name_Unchecked_Access
3382 then
3383 Cont_Encl_Typ := Enclosing_Protected_Type (Context);
3384 Pref_Encl_Typ := Enclosing_Protected_Type (Prefix (Expr));
3385
3386 -- Check whether we are trying to export a protected component to a
3387 -- context with an equal or lower access level.
3388
3389 if Present (Pref_Encl_Typ)
3390 and then No (Cont_Encl_Typ)
3391 and then Is_Public_Operation
3392 and then Scope_Depth (Pref_Encl_Typ) >=
3393 Object_Access_Level (Context)
3394 then
3395 Error_Msg_N
3396 ("??possible unprotected access to protected data", Expr);
3397 end if;
3398 end if;
3399 end Check_Unprotected_Access;
3400
3401 ------------------------
3402 -- Collect_Interfaces --
3403 ------------------------
3404
3405 procedure Collect_Interfaces
3406 (T : Entity_Id;
3407 Ifaces_List : out Elist_Id;
3408 Exclude_Parents : Boolean := False;
3409 Use_Full_View : Boolean := True)
3410 is
3411 procedure Collect (Typ : Entity_Id);
3412 -- Subsidiary subprogram used to traverse the whole list
3413 -- of directly and indirectly implemented interfaces
3414
3415 -------------
3416 -- Collect --
3417 -------------
3418
3419 procedure Collect (Typ : Entity_Id) is
3420 Ancestor : Entity_Id;
3421 Full_T : Entity_Id;
3422 Id : Node_Id;
3423 Iface : Entity_Id;
3424
3425 begin
3426 Full_T := Typ;
3427
3428 -- Handle private types
3429
3430 if Use_Full_View
3431 and then Is_Private_Type (Typ)
3432 and then Present (Full_View (Typ))
3433 then
3434 Full_T := Full_View (Typ);
3435 end if;
3436
3437 -- Include the ancestor if we are generating the whole list of
3438 -- abstract interfaces.
3439
3440 if Etype (Full_T) /= Typ
3441
3442 -- Protect the frontend against wrong sources. For example:
3443
3444 -- package P is
3445 -- type A is tagged null record;
3446 -- type B is new A with private;
3447 -- type C is new A with private;
3448 -- private
3449 -- type B is new C with null record;
3450 -- type C is new B with null record;
3451 -- end P;
3452
3453 and then Etype (Full_T) /= T
3454 then
3455 Ancestor := Etype (Full_T);
3456 Collect (Ancestor);
3457
3458 if Is_Interface (Ancestor) and then not Exclude_Parents then
3459 Append_Unique_Elmt (Ancestor, Ifaces_List);
3460 end if;
3461 end if;
3462
3463 -- Traverse the graph of ancestor interfaces
3464
3465 if Is_Non_Empty_List (Abstract_Interface_List (Full_T)) then
3466 Id := First (Abstract_Interface_List (Full_T));
3467 while Present (Id) loop
3468 Iface := Etype (Id);
3469
3470 -- Protect against wrong uses. For example:
3471 -- type I is interface;
3472 -- type O is tagged null record;
3473 -- type Wrong is new I and O with null record; -- ERROR
3474
3475 if Is_Interface (Iface) then
3476 if Exclude_Parents
3477 and then Etype (T) /= T
3478 and then Interface_Present_In_Ancestor (Etype (T), Iface)
3479 then
3480 null;
3481 else
3482 Collect (Iface);
3483 Append_Unique_Elmt (Iface, Ifaces_List);
3484 end if;
3485 end if;
3486
3487 Next (Id);
3488 end loop;
3489 end if;
3490 end Collect;
3491
3492 -- Start of processing for Collect_Interfaces
3493
3494 begin
3495 pragma Assert (Is_Tagged_Type (T) or else Is_Concurrent_Type (T));
3496 Ifaces_List := New_Elmt_List;
3497 Collect (T);
3498 end Collect_Interfaces;
3499
3500 ----------------------------------
3501 -- Collect_Interface_Components --
3502 ----------------------------------
3503
3504 procedure Collect_Interface_Components
3505 (Tagged_Type : Entity_Id;
3506 Components_List : out Elist_Id)
3507 is
3508 procedure Collect (Typ : Entity_Id);
3509 -- Subsidiary subprogram used to climb to the parents
3510
3511 -------------
3512 -- Collect --
3513 -------------
3514
3515 procedure Collect (Typ : Entity_Id) is
3516 Tag_Comp : Entity_Id;
3517 Parent_Typ : Entity_Id;
3518
3519 begin
3520 -- Handle private types
3521
3522 if Present (Full_View (Etype (Typ))) then
3523 Parent_Typ := Full_View (Etype (Typ));
3524 else
3525 Parent_Typ := Etype (Typ);
3526 end if;
3527
3528 if Parent_Typ /= Typ
3529
3530 -- Protect the frontend against wrong sources. For example:
3531
3532 -- package P is
3533 -- type A is tagged null record;
3534 -- type B is new A with private;
3535 -- type C is new A with private;
3536 -- private
3537 -- type B is new C with null record;
3538 -- type C is new B with null record;
3539 -- end P;
3540
3541 and then Parent_Typ /= Tagged_Type
3542 then
3543 Collect (Parent_Typ);
3544 end if;
3545
3546 -- Collect the components containing tags of secondary dispatch
3547 -- tables.
3548
3549 Tag_Comp := Next_Tag_Component (First_Tag_Component (Typ));
3550 while Present (Tag_Comp) loop
3551 pragma Assert (Present (Related_Type (Tag_Comp)));
3552 Append_Elmt (Tag_Comp, Components_List);
3553
3554 Tag_Comp := Next_Tag_Component (Tag_Comp);
3555 end loop;
3556 end Collect;
3557
3558 -- Start of processing for Collect_Interface_Components
3559
3560 begin
3561 pragma Assert (Ekind (Tagged_Type) = E_Record_Type
3562 and then Is_Tagged_Type (Tagged_Type));
3563
3564 Components_List := New_Elmt_List;
3565 Collect (Tagged_Type);
3566 end Collect_Interface_Components;
3567
3568 -----------------------------
3569 -- Collect_Interfaces_Info --
3570 -----------------------------
3571
3572 procedure Collect_Interfaces_Info
3573 (T : Entity_Id;
3574 Ifaces_List : out Elist_Id;
3575 Components_List : out Elist_Id;
3576 Tags_List : out Elist_Id)
3577 is
3578 Comps_List : Elist_Id;
3579 Comp_Elmt : Elmt_Id;
3580 Comp_Iface : Entity_Id;
3581 Iface_Elmt : Elmt_Id;
3582 Iface : Entity_Id;
3583
3584 function Search_Tag (Iface : Entity_Id) return Entity_Id;
3585 -- Search for the secondary tag associated with the interface type
3586 -- Iface that is implemented by T.
3587
3588 ----------------
3589 -- Search_Tag --
3590 ----------------
3591
3592 function Search_Tag (Iface : Entity_Id) return Entity_Id is
3593 ADT : Elmt_Id;
3594 begin
3595 if not Is_CPP_Class (T) then
3596 ADT := Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (T))));
3597 else
3598 ADT := Next_Elmt (First_Elmt (Access_Disp_Table (T)));
3599 end if;
3600
3601 while Present (ADT)
3602 and then Is_Tag (Node (ADT))
3603 and then Related_Type (Node (ADT)) /= Iface
3604 loop
3605 -- Skip secondary dispatch table referencing thunks to user
3606 -- defined primitives covered by this interface.
3607
3608 pragma Assert (Has_Suffix (Node (ADT), 'P'));
3609 Next_Elmt (ADT);
3610
3611 -- Skip secondary dispatch tables of Ada types
3612
3613 if not Is_CPP_Class (T) then
3614
3615 -- Skip secondary dispatch table referencing thunks to
3616 -- predefined primitives.
3617
3618 pragma Assert (Has_Suffix (Node (ADT), 'Y'));
3619 Next_Elmt (ADT);
3620
3621 -- Skip secondary dispatch table referencing user-defined
3622 -- primitives covered by this interface.
3623
3624 pragma Assert (Has_Suffix (Node (ADT), 'D'));
3625 Next_Elmt (ADT);
3626
3627 -- Skip secondary dispatch table referencing predefined
3628 -- primitives.
3629
3630 pragma Assert (Has_Suffix (Node (ADT), 'Z'));
3631 Next_Elmt (ADT);
3632 end if;
3633 end loop;
3634
3635 pragma Assert (Is_Tag (Node (ADT)));
3636 return Node (ADT);
3637 end Search_Tag;
3638
3639 -- Start of processing for Collect_Interfaces_Info
3640
3641 begin
3642 Collect_Interfaces (T, Ifaces_List);
3643 Collect_Interface_Components (T, Comps_List);
3644
3645 -- Search for the record component and tag associated with each
3646 -- interface type of T.
3647
3648 Components_List := New_Elmt_List;
3649 Tags_List := New_Elmt_List;
3650
3651 Iface_Elmt := First_Elmt (Ifaces_List);
3652 while Present (Iface_Elmt) loop
3653 Iface := Node (Iface_Elmt);
3654
3655 -- Associate the primary tag component and the primary dispatch table
3656 -- with all the interfaces that are parents of T
3657
3658 if Is_Ancestor (Iface, T, Use_Full_View => True) then
3659 Append_Elmt (First_Tag_Component (T), Components_List);
3660 Append_Elmt (Node (First_Elmt (Access_Disp_Table (T))), Tags_List);
3661
3662 -- Otherwise search for the tag component and secondary dispatch
3663 -- table of Iface
3664
3665 else
3666 Comp_Elmt := First_Elmt (Comps_List);
3667 while Present (Comp_Elmt) loop
3668 Comp_Iface := Related_Type (Node (Comp_Elmt));
3669
3670 if Comp_Iface = Iface
3671 or else Is_Ancestor (Iface, Comp_Iface, Use_Full_View => True)
3672 then
3673 Append_Elmt (Node (Comp_Elmt), Components_List);
3674 Append_Elmt (Search_Tag (Comp_Iface), Tags_List);
3675 exit;
3676 end if;
3677
3678 Next_Elmt (Comp_Elmt);
3679 end loop;
3680 pragma Assert (Present (Comp_Elmt));
3681 end if;
3682
3683 Next_Elmt (Iface_Elmt);
3684 end loop;
3685 end Collect_Interfaces_Info;
3686
3687 ---------------------
3688 -- Collect_Parents --
3689 ---------------------
3690
3691 procedure Collect_Parents
3692 (T : Entity_Id;
3693 List : out Elist_Id;
3694 Use_Full_View : Boolean := True)
3695 is
3696 Current_Typ : Entity_Id := T;
3697 Parent_Typ : Entity_Id;
3698
3699 begin
3700 List := New_Elmt_List;
3701
3702 -- No action if the if the type has no parents
3703
3704 if T = Etype (T) then
3705 return;
3706 end if;
3707
3708 loop
3709 Parent_Typ := Etype (Current_Typ);
3710
3711 if Is_Private_Type (Parent_Typ)
3712 and then Present (Full_View (Parent_Typ))
3713 and then Use_Full_View
3714 then
3715 Parent_Typ := Full_View (Base_Type (Parent_Typ));
3716 end if;
3717
3718 Append_Elmt (Parent_Typ, List);
3719
3720 exit when Parent_Typ = Current_Typ;
3721 Current_Typ := Parent_Typ;
3722 end loop;
3723 end Collect_Parents;
3724
3725 ----------------------------------
3726 -- Collect_Primitive_Operations --
3727 ----------------------------------
3728
3729 function Collect_Primitive_Operations (T : Entity_Id) return Elist_Id is
3730 B_Type : constant Entity_Id := Base_Type (T);
3731 B_Decl : constant Node_Id := Original_Node (Parent (B_Type));
3732 B_Scope : Entity_Id := Scope (B_Type);
3733 Op_List : Elist_Id;
3734 Formal : Entity_Id;
3735 Is_Prim : Boolean;
3736 Is_Type_In_Pkg : Boolean;
3737 Formal_Derived : Boolean := False;
3738 Id : Entity_Id;
3739
3740 function Match (E : Entity_Id) return Boolean;
3741 -- True if E's base type is B_Type, or E is of an anonymous access type
3742 -- and the base type of its designated type is B_Type.
3743
3744 -----------
3745 -- Match --
3746 -----------
3747
3748 function Match (E : Entity_Id) return Boolean is
3749 Etyp : Entity_Id := Etype (E);
3750
3751 begin
3752 if Ekind (Etyp) = E_Anonymous_Access_Type then
3753 Etyp := Designated_Type (Etyp);
3754 end if;
3755
3756 -- In Ada 2012 a primitive operation may have a formal of an
3757 -- incomplete view of the parent type.
3758
3759 return Base_Type (Etyp) = B_Type
3760 or else
3761 (Ada_Version >= Ada_2012
3762 and then Ekind (Etyp) = E_Incomplete_Type
3763 and then Full_View (Etyp) = B_Type);
3764 end Match;
3765
3766 -- Start of processing for Collect_Primitive_Operations
3767
3768 begin
3769 -- For tagged types, the primitive operations are collected as they
3770 -- are declared, and held in an explicit list which is simply returned.
3771
3772 if Is_Tagged_Type (B_Type) then
3773 return Primitive_Operations (B_Type);
3774
3775 -- An untagged generic type that is a derived type inherits the
3776 -- primitive operations of its parent type. Other formal types only
3777 -- have predefined operators, which are not explicitly represented.
3778
3779 elsif Is_Generic_Type (B_Type) then
3780 if Nkind (B_Decl) = N_Formal_Type_Declaration
3781 and then Nkind (Formal_Type_Definition (B_Decl)) =
3782 N_Formal_Derived_Type_Definition
3783 then
3784 Formal_Derived := True;
3785 else
3786 return New_Elmt_List;
3787 end if;
3788 end if;
3789
3790 Op_List := New_Elmt_List;
3791
3792 if B_Scope = Standard_Standard then
3793 if B_Type = Standard_String then
3794 Append_Elmt (Standard_Op_Concat, Op_List);
3795
3796 elsif B_Type = Standard_Wide_String then
3797 Append_Elmt (Standard_Op_Concatw, Op_List);
3798
3799 else
3800 null;
3801 end if;
3802
3803 -- Locate the primitive subprograms of the type
3804
3805 else
3806 -- The primitive operations appear after the base type, except
3807 -- if the derivation happens within the private part of B_Scope
3808 -- and the type is a private type, in which case both the type
3809 -- and some primitive operations may appear before the base
3810 -- type, and the list of candidates starts after the type.
3811
3812 if In_Open_Scopes (B_Scope)
3813 and then Scope (T) = B_Scope
3814 and then In_Private_Part (B_Scope)
3815 then
3816 Id := Next_Entity (T);
3817
3818 -- In Ada 2012, If the type has an incomplete partial view, there
3819 -- may be primitive operations declared before the full view, so
3820 -- we need to start scanning from the incomplete view, which is
3821 -- earlier on the entity chain.
3822
3823 elsif Nkind (Parent (B_Type)) = N_Full_Type_Declaration
3824 and then Present (Incomplete_View (Parent (B_Type)))
3825 then
3826 Id := Defining_Entity (Incomplete_View (Parent (B_Type)));
3827
3828 else
3829 Id := Next_Entity (B_Type);
3830 end if;
3831
3832 -- Set flag if this is a type in a package spec
3833
3834 Is_Type_In_Pkg :=
3835 Is_Package_Or_Generic_Package (B_Scope)
3836 and then
3837 Nkind (Parent (Declaration_Node (First_Subtype (T)))) /=
3838 N_Package_Body;
3839
3840 while Present (Id) loop
3841
3842 -- Test whether the result type or any of the parameter types of
3843 -- each subprogram following the type match that type when the
3844 -- type is declared in a package spec, is a derived type, or the
3845 -- subprogram is marked as primitive. (The Is_Primitive test is
3846 -- needed to find primitives of nonderived types in declarative
3847 -- parts that happen to override the predefined "=" operator.)
3848
3849 -- Note that generic formal subprograms are not considered to be
3850 -- primitive operations and thus are never inherited.
3851
3852 if Is_Overloadable (Id)
3853 and then (Is_Type_In_Pkg
3854 or else Is_Derived_Type (B_Type)
3855 or else Is_Primitive (Id))
3856 and then Nkind (Parent (Parent (Id)))
3857 not in N_Formal_Subprogram_Declaration
3858 then
3859 Is_Prim := False;
3860
3861 if Match (Id) then
3862 Is_Prim := True;
3863
3864 else
3865 Formal := First_Formal (Id);
3866 while Present (Formal) loop
3867 if Match (Formal) then
3868 Is_Prim := True;
3869 exit;
3870 end if;
3871
3872 Next_Formal (Formal);
3873 end loop;
3874 end if;
3875
3876 -- For a formal derived type, the only primitives are the ones
3877 -- inherited from the parent type. Operations appearing in the
3878 -- package declaration are not primitive for it.
3879
3880 if Is_Prim
3881 and then (not Formal_Derived or else Present (Alias (Id)))
3882 then
3883 -- In the special case of an equality operator aliased to
3884 -- an overriding dispatching equality belonging to the same
3885 -- type, we don't include it in the list of primitives.
3886 -- This avoids inheriting multiple equality operators when
3887 -- deriving from untagged private types whose full type is
3888 -- tagged, which can otherwise cause ambiguities. Note that
3889 -- this should only happen for this kind of untagged parent
3890 -- type, since normally dispatching operations are inherited
3891 -- using the type's Primitive_Operations list.
3892
3893 if Chars (Id) = Name_Op_Eq
3894 and then Is_Dispatching_Operation (Id)
3895 and then Present (Alias (Id))
3896 and then Present (Overridden_Operation (Alias (Id)))
3897 and then Base_Type (Etype (First_Entity (Id))) =
3898 Base_Type (Etype (First_Entity (Alias (Id))))
3899 then
3900 null;
3901
3902 -- Include the subprogram in the list of primitives
3903
3904 else
3905 Append_Elmt (Id, Op_List);
3906 end if;
3907 end if;
3908 end if;
3909
3910 Next_Entity (Id);
3911
3912 -- For a type declared in System, some of its operations may
3913 -- appear in the target-specific extension to System.
3914
3915 if No (Id)
3916 and then B_Scope = RTU_Entity (System)
3917 and then Present_System_Aux
3918 then
3919 B_Scope := System_Aux_Id;
3920 Id := First_Entity (System_Aux_Id);
3921 end if;
3922 end loop;
3923 end if;
3924
3925 return Op_List;
3926 end Collect_Primitive_Operations;
3927
3928 -----------------------------------
3929 -- Compile_Time_Constraint_Error --
3930 -----------------------------------
3931
3932 function Compile_Time_Constraint_Error
3933 (N : Node_Id;
3934 Msg : String;
3935 Ent : Entity_Id := Empty;
3936 Loc : Source_Ptr := No_Location;
3937 Warn : Boolean := False) return Node_Id
3938 is
3939 Msgc : String (1 .. Msg'Length + 3);
3940 -- Copy of message, with room for possible ?? or << and ! at end
3941
3942 Msgl : Natural;
3943 Wmsg : Boolean;
3944 Eloc : Source_Ptr;
3945
3946 -- Start of processing for Compile_Time_Constraint_Error
3947
3948 begin
3949 -- If this is a warning, convert it into an error if we are in code
3950 -- subject to SPARK_Mode being set ON.
3951
3952 Error_Msg_Warn := SPARK_Mode /= On;
3953
3954 -- A static constraint error in an instance body is not a fatal error.
3955 -- we choose to inhibit the message altogether, because there is no
3956 -- obvious node (for now) on which to post it. On the other hand the
3957 -- offending node must be replaced with a constraint_error in any case.
3958
3959 -- No messages are generated if we already posted an error on this node
3960
3961 if not Error_Posted (N) then
3962 if Loc /= No_Location then
3963 Eloc := Loc;
3964 else
3965 Eloc := Sloc (N);
3966 end if;
3967
3968 -- Copy message to Msgc, converting any ? in the message into
3969 -- < instead, so that we have an error in GNATprove mode.
3970
3971 Msgl := Msg'Length;
3972
3973 for J in 1 .. Msgl loop
3974 if Msg (J) = '?' and then (J = 1 or else Msg (J) /= ''') then
3975 Msgc (J) := '<';
3976 else
3977 Msgc (J) := Msg (J);
3978 end if;
3979 end loop;
3980
3981 -- Message is a warning, even in Ada 95 case
3982
3983 if Msg (Msg'Last) = '?' or else Msg (Msg'Last) = '<' then
3984 Wmsg := True;
3985
3986 -- In Ada 83, all messages are warnings. In the private part and
3987 -- the body of an instance, constraint_checks are only warnings.
3988 -- We also make this a warning if the Warn parameter is set.
3989
3990 elsif Warn
3991 or else (Ada_Version = Ada_83 and then Comes_From_Source (N))
3992 then
3993 Msgl := Msgl + 1;
3994 Msgc (Msgl) := '<';
3995 Msgl := Msgl + 1;
3996 Msgc (Msgl) := '<';
3997 Wmsg := True;
3998
3999 elsif In_Instance_Not_Visible then
4000 Msgl := Msgl + 1;
4001 Msgc (Msgl) := '<';
4002 Msgl := Msgl + 1;
4003 Msgc (Msgl) := '<';
4004 Wmsg := True;
4005
4006 -- Otherwise we have a real error message (Ada 95 static case)
4007 -- and we make this an unconditional message. Note that in the
4008 -- warning case we do not make the message unconditional, it seems
4009 -- quite reasonable to delete messages like this (about exceptions
4010 -- that will be raised) in dead code.
4011
4012 else
4013 Wmsg := False;
4014 Msgl := Msgl + 1;
4015 Msgc (Msgl) := '!';
4016 end if;
4017
4018 -- One more test, skip the warning if the related expression is
4019 -- statically unevaluated, since we don't want to warn about what
4020 -- will happen when something is evaluated if it never will be
4021 -- evaluated.
4022
4023 if not Is_Statically_Unevaluated (N) then
4024 Error_Msg_Warn := SPARK_Mode /= On;
4025
4026 if Present (Ent) then
4027 Error_Msg_NEL (Msgc (1 .. Msgl), N, Ent, Eloc);
4028 else
4029 Error_Msg_NEL (Msgc (1 .. Msgl), N, Etype (N), Eloc);
4030 end if;
4031
4032 if Wmsg then
4033
4034 -- Check whether the context is an Init_Proc
4035
4036 if Inside_Init_Proc then
4037 declare
4038 Conc_Typ : constant Entity_Id :=
4039 Corresponding_Concurrent_Type
4040 (Entity (Parameter_Type (First
4041 (Parameter_Specifications
4042 (Parent (Current_Scope))))));
4043
4044 begin
4045 -- Don't complain if the corresponding concurrent type
4046 -- doesn't come from source (i.e. a single task/protected
4047 -- object).
4048
4049 if Present (Conc_Typ)
4050 and then not Comes_From_Source (Conc_Typ)
4051 then
4052 Error_Msg_NEL
4053 ("\& [<<", N, Standard_Constraint_Error, Eloc);
4054
4055 else
4056 if GNATprove_Mode then
4057 Error_Msg_NEL
4058 ("\& would have been raised for objects of this "
4059 & "type", N, Standard_Constraint_Error, Eloc);
4060 else
4061 Error_Msg_NEL
4062 ("\& will be raised for objects of this type??",
4063 N, Standard_Constraint_Error, Eloc);
4064 end if;
4065 end if;
4066 end;
4067
4068 else
4069 Error_Msg_NEL ("\& [<<", N, Standard_Constraint_Error, Eloc);
4070 end if;
4071
4072 else
4073 Error_Msg ("\static expression fails Constraint_Check", Eloc);
4074 Set_Error_Posted (N);
4075 end if;
4076 end if;
4077 end if;
4078
4079 return N;
4080 end Compile_Time_Constraint_Error;
4081
4082 -----------------------
4083 -- Conditional_Delay --
4084 -----------------------
4085
4086 procedure Conditional_Delay (New_Ent, Old_Ent : Entity_Id) is
4087 begin
4088 if Has_Delayed_Freeze (Old_Ent) and then not Is_Frozen (Old_Ent) then
4089 Set_Has_Delayed_Freeze (New_Ent);
4090 end if;
4091 end Conditional_Delay;
4092
4093 ----------------------------
4094 -- Contains_Refined_State --
4095 ----------------------------
4096
4097 function Contains_Refined_State (Prag : Node_Id) return Boolean is
4098 function Has_State_In_Dependency (List : Node_Id) return Boolean;
4099 -- Determine whether a dependency list mentions a state with a visible
4100 -- refinement.
4101
4102 function Has_State_In_Global (List : Node_Id) return Boolean;
4103 -- Determine whether a global list mentions a state with a visible
4104 -- refinement.
4105
4106 function Is_Refined_State (Item : Node_Id) return Boolean;
4107 -- Determine whether Item is a reference to an abstract state with a
4108 -- visible refinement.
4109
4110 -----------------------------
4111 -- Has_State_In_Dependency --
4112 -----------------------------
4113
4114 function Has_State_In_Dependency (List : Node_Id) return Boolean is
4115 Clause : Node_Id;
4116 Output : Node_Id;
4117
4118 begin
4119 -- A null dependency list does not mention any states
4120
4121 if Nkind (List) = N_Null then
4122 return False;
4123
4124 -- Dependency clauses appear as component associations of an
4125 -- aggregate.
4126
4127 elsif Nkind (List) = N_Aggregate
4128 and then Present (Component_Associations (List))
4129 then
4130 Clause := First (Component_Associations (List));
4131 while Present (Clause) loop
4132
4133 -- Inspect the outputs of a dependency clause
4134
4135 Output := First (Choices (Clause));
4136 while Present (Output) loop
4137 if Is_Refined_State (Output) then
4138 return True;
4139 end if;
4140
4141 Next (Output);
4142 end loop;
4143
4144 -- Inspect the outputs of a dependency clause
4145
4146 if Is_Refined_State (Expression (Clause)) then
4147 return True;
4148 end if;
4149
4150 Next (Clause);
4151 end loop;
4152
4153 -- If we get here, then none of the dependency clauses mention a
4154 -- state with visible refinement.
4155
4156 return False;
4157
4158 -- An illegal pragma managed to sneak in
4159
4160 else
4161 raise Program_Error;
4162 end if;
4163 end Has_State_In_Dependency;
4164
4165 -------------------------
4166 -- Has_State_In_Global --
4167 -------------------------
4168
4169 function Has_State_In_Global (List : Node_Id) return Boolean is
4170 Item : Node_Id;
4171
4172 begin
4173 -- A null global list does not mention any states
4174
4175 if Nkind (List) = N_Null then
4176 return False;
4177
4178 -- Simple global list or moded global list declaration
4179
4180 elsif Nkind (List) = N_Aggregate then
4181
4182 -- The declaration of a simple global list appear as a collection
4183 -- of expressions.
4184
4185 if Present (Expressions (List)) then
4186 Item := First (Expressions (List));
4187 while Present (Item) loop
4188 if Is_Refined_State (Item) then
4189 return True;
4190 end if;
4191
4192 Next (Item);
4193 end loop;
4194
4195 -- The declaration of a moded global list appears as a collection
4196 -- of component associations where individual choices denote
4197 -- modes.
4198
4199 else
4200 Item := First (Component_Associations (List));
4201 while Present (Item) loop
4202 if Has_State_In_Global (Expression (Item)) then
4203 return True;
4204 end if;
4205
4206 Next (Item);
4207 end loop;
4208 end if;
4209
4210 -- If we get here, then the simple/moded global list did not
4211 -- mention any states with a visible refinement.
4212
4213 return False;
4214
4215 -- Single global item declaration
4216
4217 elsif Is_Entity_Name (List) then
4218 return Is_Refined_State (List);
4219
4220 -- An illegal pragma managed to sneak in
4221
4222 else
4223 raise Program_Error;
4224 end if;
4225 end Has_State_In_Global;
4226
4227 ----------------------
4228 -- Is_Refined_State --
4229 ----------------------
4230
4231 function Is_Refined_State (Item : Node_Id) return Boolean is
4232 Elmt : Node_Id;
4233 Item_Id : Entity_Id;
4234
4235 begin
4236 if Nkind (Item) = N_Null then
4237 return False;
4238
4239 -- States cannot be subject to attribute 'Result. This case arises
4240 -- in dependency relations.
4241
4242 elsif Nkind (Item) = N_Attribute_Reference
4243 and then Attribute_Name (Item) = Name_Result
4244 then
4245 return False;
4246
4247 -- Multiple items appear as an aggregate. This case arises in
4248 -- dependency relations.
4249
4250 elsif Nkind (Item) = N_Aggregate
4251 and then Present (Expressions (Item))
4252 then
4253 Elmt := First (Expressions (Item));
4254 while Present (Elmt) loop
4255 if Is_Refined_State (Elmt) then
4256 return True;
4257 end if;
4258
4259 Next (Elmt);
4260 end loop;
4261
4262 -- If we get here, then none of the inputs or outputs reference a
4263 -- state with visible refinement.
4264
4265 return False;
4266
4267 -- Single item
4268
4269 else
4270 Item_Id := Entity_Of (Item);
4271
4272 return
4273 Present (Item_Id)
4274 and then Ekind (Item_Id) = E_Abstract_State
4275 and then Has_Visible_Refinement (Item_Id);
4276 end if;
4277 end Is_Refined_State;
4278
4279 -- Local variables
4280
4281 Arg : constant Node_Id :=
4282 Get_Pragma_Arg (First (Pragma_Argument_Associations (Prag)));
4283 Nam : constant Name_Id := Pragma_Name (Prag);
4284
4285 -- Start of processing for Contains_Refined_State
4286
4287 begin
4288 if Nam = Name_Depends then
4289 return Has_State_In_Dependency (Arg);
4290
4291 else pragma Assert (Nam = Name_Global);
4292 return Has_State_In_Global (Arg);
4293 end if;
4294 end Contains_Refined_State;
4295
4296 -------------------------
4297 -- Copy_Component_List --
4298 -------------------------
4299
4300 function Copy_Component_List
4301 (R_Typ : Entity_Id;
4302 Loc : Source_Ptr) return List_Id
4303 is
4304 Comp : Node_Id;
4305 Comps : constant List_Id := New_List;
4306
4307 begin
4308 Comp := First_Component (Underlying_Type (R_Typ));
4309 while Present (Comp) loop
4310 if Comes_From_Source (Comp) then
4311 declare
4312 Comp_Decl : constant Node_Id := Declaration_Node (Comp);
4313 begin
4314 Append_To (Comps,
4315 Make_Component_Declaration (Loc,
4316 Defining_Identifier =>
4317 Make_Defining_Identifier (Loc, Chars (Comp)),
4318 Component_Definition =>
4319 New_Copy_Tree
4320 (Component_Definition (Comp_Decl), New_Sloc => Loc)));
4321 end;
4322 end if;
4323
4324 Next_Component (Comp);
4325 end loop;
4326
4327 return Comps;
4328 end Copy_Component_List;
4329
4330 -------------------------
4331 -- Copy_Parameter_List --
4332 -------------------------
4333
4334 function Copy_Parameter_List (Subp_Id : Entity_Id) return List_Id is
4335 Loc : constant Source_Ptr := Sloc (Subp_Id);
4336 Plist : List_Id;
4337 Formal : Entity_Id;
4338
4339 begin
4340 if No (First_Formal (Subp_Id)) then
4341 return No_List;
4342 else
4343 Plist := New_List;
4344 Formal := First_Formal (Subp_Id);
4345 while Present (Formal) loop
4346 Append
4347 (Make_Parameter_Specification (Loc,
4348 Defining_Identifier =>
4349 Make_Defining_Identifier (Sloc (Formal),
4350 Chars => Chars (Formal)),
4351 In_Present => In_Present (Parent (Formal)),
4352 Out_Present => Out_Present (Parent (Formal)),
4353 Parameter_Type =>
4354 New_Occurrence_Of (Etype (Formal), Loc),
4355 Expression =>
4356 New_Copy_Tree (Expression (Parent (Formal)))),
4357 Plist);
4358
4359 Next_Formal (Formal);
4360 end loop;
4361 end if;
4362
4363 return Plist;
4364 end Copy_Parameter_List;
4365
4366 --------------------------------
4367 -- Corresponding_Generic_Type --
4368 --------------------------------
4369
4370 function Corresponding_Generic_Type (T : Entity_Id) return Entity_Id is
4371 Inst : Entity_Id;
4372 Gen : Entity_Id;
4373 Typ : Entity_Id;
4374
4375 begin
4376 if not Is_Generic_Actual_Type (T) then
4377 return Any_Type;
4378
4379 -- If the actual is the actual of an enclosing instance, resolution
4380 -- was correct in the generic.
4381
4382 elsif Nkind (Parent (T)) = N_Subtype_Declaration
4383 and then Is_Entity_Name (Subtype_Indication (Parent (T)))
4384 and then
4385 Is_Generic_Actual_Type (Entity (Subtype_Indication (Parent (T))))
4386 then
4387 return Any_Type;
4388
4389 else
4390 Inst := Scope (T);
4391
4392 if Is_Wrapper_Package (Inst) then
4393 Inst := Related_Instance (Inst);
4394 end if;
4395
4396 Gen :=
4397 Generic_Parent
4398 (Specification (Unit_Declaration_Node (Inst)));
4399
4400 -- Generic actual has the same name as the corresponding formal
4401
4402 Typ := First_Entity (Gen);
4403 while Present (Typ) loop
4404 if Chars (Typ) = Chars (T) then
4405 return Typ;
4406 end if;
4407
4408 Next_Entity (Typ);
4409 end loop;
4410
4411 return Any_Type;
4412 end if;
4413 end Corresponding_Generic_Type;
4414
4415 --------------------
4416 -- Current_Entity --
4417 --------------------
4418
4419 -- The currently visible definition for a given identifier is the
4420 -- one most chained at the start of the visibility chain, i.e. the
4421 -- one that is referenced by the Node_Id value of the name of the
4422 -- given identifier.
4423
4424 function Current_Entity (N : Node_Id) return Entity_Id is
4425 begin
4426 return Get_Name_Entity_Id (Chars (N));
4427 end Current_Entity;
4428
4429 -----------------------------
4430 -- Current_Entity_In_Scope --
4431 -----------------------------
4432
4433 function Current_Entity_In_Scope (N : Node_Id) return Entity_Id is
4434 E : Entity_Id;
4435 CS : constant Entity_Id := Current_Scope;
4436
4437 Transient_Case : constant Boolean := Scope_Is_Transient;
4438
4439 begin
4440 E := Get_Name_Entity_Id (Chars (N));
4441 while Present (E)
4442 and then Scope (E) /= CS
4443 and then (not Transient_Case or else Scope (E) /= Scope (CS))
4444 loop
4445 E := Homonym (E);
4446 end loop;
4447
4448 return E;
4449 end Current_Entity_In_Scope;
4450
4451 -------------------
4452 -- Current_Scope --
4453 -------------------
4454
4455 function Current_Scope return Entity_Id is
4456 begin
4457 if Scope_Stack.Last = -1 then
4458 return Standard_Standard;
4459 else
4460 declare
4461 C : constant Entity_Id :=
4462 Scope_Stack.Table (Scope_Stack.Last).Entity;
4463 begin
4464 if Present (C) then
4465 return C;
4466 else
4467 return Standard_Standard;
4468 end if;
4469 end;
4470 end if;
4471 end Current_Scope;
4472
4473 ------------------------
4474 -- Current_Subprogram --
4475 ------------------------
4476
4477 function Current_Subprogram return Entity_Id is
4478 Scop : constant Entity_Id := Current_Scope;
4479 begin
4480 if Is_Subprogram_Or_Generic_Subprogram (Scop) then
4481 return Scop;
4482 else
4483 return Enclosing_Subprogram (Scop);
4484 end if;
4485 end Current_Subprogram;
4486
4487 ----------------------------------
4488 -- Deepest_Type_Access_Level --
4489 ----------------------------------
4490
4491 function Deepest_Type_Access_Level (Typ : Entity_Id) return Uint is
4492 begin
4493 if Ekind (Typ) = E_Anonymous_Access_Type
4494 and then not Is_Local_Anonymous_Access (Typ)
4495 and then Nkind (Associated_Node_For_Itype (Typ)) = N_Object_Declaration
4496 then
4497 -- Typ is the type of an Ada 2012 stand-alone object of an anonymous
4498 -- access type.
4499
4500 return
4501 Scope_Depth (Enclosing_Dynamic_Scope
4502 (Defining_Identifier
4503 (Associated_Node_For_Itype (Typ))));
4504
4505 -- For generic formal type, return Int'Last (infinite).
4506 -- See comment preceding Is_Generic_Type call in Type_Access_Level.
4507
4508 elsif Is_Generic_Type (Root_Type (Typ)) then
4509 return UI_From_Int (Int'Last);
4510
4511 else
4512 return Type_Access_Level (Typ);
4513 end if;
4514 end Deepest_Type_Access_Level;
4515
4516 ---------------------
4517 -- Defining_Entity --
4518 ---------------------
4519
4520 function Defining_Entity (N : Node_Id) return Entity_Id is
4521 K : constant Node_Kind := Nkind (N);
4522 Err : Entity_Id := Empty;
4523
4524 begin
4525 case K is
4526 when
4527 N_Subprogram_Declaration |
4528 N_Abstract_Subprogram_Declaration |
4529 N_Subprogram_Body |
4530 N_Package_Declaration |
4531 N_Subprogram_Renaming_Declaration |
4532 N_Subprogram_Body_Stub |
4533 N_Generic_Subprogram_Declaration |
4534 N_Generic_Package_Declaration |
4535 N_Formal_Subprogram_Declaration |
4536 N_Expression_Function
4537 =>
4538 return Defining_Entity (Specification (N));
4539
4540 when
4541 N_Component_Declaration |
4542 N_Defining_Program_Unit_Name |
4543 N_Discriminant_Specification |
4544 N_Entry_Body |
4545 N_Entry_Declaration |
4546 N_Entry_Index_Specification |
4547 N_Exception_Declaration |
4548 N_Exception_Renaming_Declaration |
4549 N_Formal_Object_Declaration |
4550 N_Formal_Package_Declaration |
4551 N_Formal_Type_Declaration |
4552 N_Full_Type_Declaration |
4553 N_Implicit_Label_Declaration |
4554 N_Incomplete_Type_Declaration |
4555 N_Loop_Parameter_Specification |
4556 N_Number_Declaration |
4557 N_Object_Declaration |
4558 N_Object_Renaming_Declaration |
4559 N_Package_Body_Stub |
4560 N_Parameter_Specification |
4561 N_Private_Extension_Declaration |
4562 N_Private_Type_Declaration |
4563 N_Protected_Body |
4564 N_Protected_Body_Stub |
4565 N_Protected_Type_Declaration |
4566 N_Single_Protected_Declaration |
4567 N_Single_Task_Declaration |
4568 N_Subtype_Declaration |
4569 N_Task_Body |
4570 N_Task_Body_Stub |
4571 N_Task_Type_Declaration
4572 =>
4573 return Defining_Identifier (N);
4574
4575 when N_Subunit =>
4576 return Defining_Entity (Proper_Body (N));
4577
4578 when
4579 N_Function_Instantiation |
4580 N_Function_Specification |
4581 N_Generic_Function_Renaming_Declaration |
4582 N_Generic_Package_Renaming_Declaration |
4583 N_Generic_Procedure_Renaming_Declaration |
4584 N_Package_Body |
4585 N_Package_Instantiation |
4586 N_Package_Renaming_Declaration |
4587 N_Package_Specification |
4588 N_Procedure_Instantiation |
4589 N_Procedure_Specification
4590 =>
4591 declare
4592 Nam : constant Node_Id := Defining_Unit_Name (N);
4593
4594 begin
4595 if Nkind (Nam) in N_Entity then
4596 return Nam;
4597
4598 -- For Error, make up a name and attach to declaration
4599 -- so we can continue semantic analysis
4600
4601 elsif Nam = Error then
4602 Err := Make_Temporary (Sloc (N), 'T');
4603 Set_Defining_Unit_Name (N, Err);
4604
4605 return Err;
4606
4607 -- If not an entity, get defining identifier
4608
4609 else
4610 return Defining_Identifier (Nam);
4611 end if;
4612 end;
4613
4614 when
4615 N_Block_Statement |
4616 N_Loop_Statement
4617 =>
4618 return Entity (Identifier (N));
4619
4620 when others =>
4621 raise Program_Error;
4622
4623 end case;
4624 end Defining_Entity;
4625
4626 --------------------------
4627 -- Denotes_Discriminant --
4628 --------------------------
4629
4630 function Denotes_Discriminant
4631 (N : Node_Id;
4632 Check_Concurrent : Boolean := False) return Boolean
4633 is
4634 E : Entity_Id;
4635
4636 begin
4637 if not Is_Entity_Name (N) or else No (Entity (N)) then
4638 return False;
4639 else
4640 E := Entity (N);
4641 end if;
4642
4643 -- If we are checking for a protected type, the discriminant may have
4644 -- been rewritten as the corresponding discriminal of the original type
4645 -- or of the corresponding concurrent record, depending on whether we
4646 -- are in the spec or body of the protected type.
4647
4648 return Ekind (E) = E_Discriminant
4649 or else
4650 (Check_Concurrent
4651 and then Ekind (E) = E_In_Parameter
4652 and then Present (Discriminal_Link (E))
4653 and then
4654 (Is_Concurrent_Type (Scope (Discriminal_Link (E)))
4655 or else
4656 Is_Concurrent_Record_Type (Scope (Discriminal_Link (E)))));
4657
4658 end Denotes_Discriminant;
4659
4660 -------------------------
4661 -- Denotes_Same_Object --
4662 -------------------------
4663
4664 function Denotes_Same_Object (A1, A2 : Node_Id) return Boolean is
4665 Obj1 : Node_Id := A1;
4666 Obj2 : Node_Id := A2;
4667
4668 function Has_Prefix (N : Node_Id) return Boolean;
4669 -- Return True if N has attribute Prefix
4670
4671 function Is_Renaming (N : Node_Id) return Boolean;
4672 -- Return true if N names a renaming entity
4673
4674 function Is_Valid_Renaming (N : Node_Id) return Boolean;
4675 -- For renamings, return False if the prefix of any dereference within
4676 -- the renamed object_name is a variable, or any expression within the
4677 -- renamed object_name contains references to variables or calls on
4678 -- nonstatic functions; otherwise return True (RM 6.4.1(6.10/3))
4679
4680 ----------------
4681 -- Has_Prefix --
4682 ----------------
4683
4684 function Has_Prefix (N : Node_Id) return Boolean is
4685 begin
4686 return
4687 Nkind_In (N,
4688 N_Attribute_Reference,
4689 N_Expanded_Name,
4690 N_Explicit_Dereference,
4691 N_Indexed_Component,
4692 N_Reference,
4693 N_Selected_Component,
4694 N_Slice);
4695 end Has_Prefix;
4696
4697 -----------------
4698 -- Is_Renaming --
4699 -----------------
4700
4701 function Is_Renaming (N : Node_Id) return Boolean is
4702 begin
4703 return Is_Entity_Name (N)
4704 and then Present (Renamed_Entity (Entity (N)));
4705 end Is_Renaming;
4706
4707 -----------------------
4708 -- Is_Valid_Renaming --
4709 -----------------------
4710
4711 function Is_Valid_Renaming (N : Node_Id) return Boolean is
4712
4713 function Check_Renaming (N : Node_Id) return Boolean;
4714 -- Recursive function used to traverse all the prefixes of N
4715
4716 function Check_Renaming (N : Node_Id) return Boolean is
4717 begin
4718 if Is_Renaming (N)
4719 and then not Check_Renaming (Renamed_Entity (Entity (N)))
4720 then
4721 return False;
4722 end if;
4723
4724 if Nkind (N) = N_Indexed_Component then
4725 declare
4726 Indx : Node_Id;
4727
4728 begin
4729 Indx := First (Expressions (N));
4730 while Present (Indx) loop
4731 if not Is_OK_Static_Expression (Indx) then
4732 return False;
4733 end if;
4734
4735 Next_Index (Indx);
4736 end loop;
4737 end;
4738 end if;
4739
4740 if Has_Prefix (N) then
4741 declare
4742 P : constant Node_Id := Prefix (N);
4743
4744 begin
4745 if Nkind (N) = N_Explicit_Dereference
4746 and then Is_Variable (P)
4747 then
4748 return False;
4749
4750 elsif Is_Entity_Name (P)
4751 and then Ekind (Entity (P)) = E_Function
4752 then
4753 return False;
4754
4755 elsif Nkind (P) = N_Function_Call then
4756 return False;
4757 end if;
4758
4759 -- Recursion to continue traversing the prefix of the
4760 -- renaming expression
4761
4762 return Check_Renaming (P);
4763 end;
4764 end if;
4765
4766 return True;
4767 end Check_Renaming;
4768
4769 -- Start of processing for Is_Valid_Renaming
4770
4771 begin
4772 return Check_Renaming (N);
4773 end Is_Valid_Renaming;
4774
4775 -- Start of processing for Denotes_Same_Object
4776
4777 begin
4778 -- Both names statically denote the same stand-alone object or parameter
4779 -- (RM 6.4.1(6.5/3))
4780
4781 if Is_Entity_Name (Obj1)
4782 and then Is_Entity_Name (Obj2)
4783 and then Entity (Obj1) = Entity (Obj2)
4784 then
4785 return True;
4786 end if;
4787
4788 -- For renamings, the prefix of any dereference within the renamed
4789 -- object_name is not a variable, and any expression within the
4790 -- renamed object_name contains no references to variables nor
4791 -- calls on nonstatic functions (RM 6.4.1(6.10/3)).
4792
4793 if Is_Renaming (Obj1) then
4794 if Is_Valid_Renaming (Obj1) then
4795 Obj1 := Renamed_Entity (Entity (Obj1));
4796 else
4797 return False;
4798 end if;
4799 end if;
4800
4801 if Is_Renaming (Obj2) then
4802 if Is_Valid_Renaming (Obj2) then
4803 Obj2 := Renamed_Entity (Entity (Obj2));
4804 else
4805 return False;
4806 end if;
4807 end if;
4808
4809 -- No match if not same node kind (such cases are handled by
4810 -- Denotes_Same_Prefix)
4811
4812 if Nkind (Obj1) /= Nkind (Obj2) then
4813 return False;
4814
4815 -- After handling valid renamings, one of the two names statically
4816 -- denoted a renaming declaration whose renamed object_name is known
4817 -- to denote the same object as the other (RM 6.4.1(6.10/3))
4818
4819 elsif Is_Entity_Name (Obj1) then
4820 if Is_Entity_Name (Obj2) then
4821 return Entity (Obj1) = Entity (Obj2);
4822 else
4823 return False;
4824 end if;
4825
4826 -- Both names are selected_components, their prefixes are known to
4827 -- denote the same object, and their selector_names denote the same
4828 -- component (RM 6.4.1(6.6/3)
4829
4830 elsif Nkind (Obj1) = N_Selected_Component then
4831 return Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2))
4832 and then
4833 Entity (Selector_Name (Obj1)) = Entity (Selector_Name (Obj2));
4834
4835 -- Both names are dereferences and the dereferenced names are known to
4836 -- denote the same object (RM 6.4.1(6.7/3))
4837
4838 elsif Nkind (Obj1) = N_Explicit_Dereference then
4839 return Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2));
4840
4841 -- Both names are indexed_components, their prefixes are known to denote
4842 -- the same object, and each of the pairs of corresponding index values
4843 -- are either both static expressions with the same static value or both
4844 -- names that are known to denote the same object (RM 6.4.1(6.8/3))
4845
4846 elsif Nkind (Obj1) = N_Indexed_Component then
4847 if not Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2)) then
4848 return False;
4849 else
4850 declare
4851 Indx1 : Node_Id;
4852 Indx2 : Node_Id;
4853
4854 begin
4855 Indx1 := First (Expressions (Obj1));
4856 Indx2 := First (Expressions (Obj2));
4857 while Present (Indx1) loop
4858
4859 -- Indexes must denote the same static value or same object
4860
4861 if Is_OK_Static_Expression (Indx1) then
4862 if not Is_OK_Static_Expression (Indx2) then
4863 return False;
4864
4865 elsif Expr_Value (Indx1) /= Expr_Value (Indx2) then
4866 return False;
4867 end if;
4868
4869 elsif not Denotes_Same_Object (Indx1, Indx2) then
4870 return False;
4871 end if;
4872
4873 Next (Indx1);
4874 Next (Indx2);
4875 end loop;
4876
4877 return True;
4878 end;
4879 end if;
4880
4881 -- Both names are slices, their prefixes are known to denote the same
4882 -- object, and the two slices have statically matching index constraints
4883 -- (RM 6.4.1(6.9/3))
4884
4885 elsif Nkind (Obj1) = N_Slice
4886 and then Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2))
4887 then
4888 declare
4889 Lo1, Lo2, Hi1, Hi2 : Node_Id;
4890
4891 begin
4892 Get_Index_Bounds (Etype (Obj1), Lo1, Hi1);
4893 Get_Index_Bounds (Etype (Obj2), Lo2, Hi2);
4894
4895 -- Check whether bounds are statically identical. There is no
4896 -- attempt to detect partial overlap of slices.
4897
4898 return Denotes_Same_Object (Lo1, Lo2)
4899 and then
4900 Denotes_Same_Object (Hi1, Hi2);
4901 end;
4902
4903 -- In the recursion, literals appear as indexes
4904
4905 elsif Nkind (Obj1) = N_Integer_Literal
4906 and then
4907 Nkind (Obj2) = N_Integer_Literal
4908 then
4909 return Intval (Obj1) = Intval (Obj2);
4910
4911 else
4912 return False;
4913 end if;
4914 end Denotes_Same_Object;
4915
4916 -------------------------
4917 -- Denotes_Same_Prefix --
4918 -------------------------
4919
4920 function Denotes_Same_Prefix (A1, A2 : Node_Id) return Boolean is
4921
4922 begin
4923 if Is_Entity_Name (A1) then
4924 if Nkind_In (A2, N_Selected_Component, N_Indexed_Component)
4925 and then not Is_Access_Type (Etype (A1))
4926 then
4927 return Denotes_Same_Object (A1, Prefix (A2))
4928 or else Denotes_Same_Prefix (A1, Prefix (A2));
4929 else
4930 return False;
4931 end if;
4932
4933 elsif Is_Entity_Name (A2) then
4934 return Denotes_Same_Prefix (A1 => A2, A2 => A1);
4935
4936 elsif Nkind_In (A1, N_Selected_Component, N_Indexed_Component, N_Slice)
4937 and then
4938 Nkind_In (A2, N_Selected_Component, N_Indexed_Component, N_Slice)
4939 then
4940 declare
4941 Root1, Root2 : Node_Id;
4942 Depth1, Depth2 : Int := 0;
4943
4944 begin
4945 Root1 := Prefix (A1);
4946 while not Is_Entity_Name (Root1) loop
4947 if not Nkind_In
4948 (Root1, N_Selected_Component, N_Indexed_Component)
4949 then
4950 return False;
4951 else
4952 Root1 := Prefix (Root1);
4953 end if;
4954
4955 Depth1 := Depth1 + 1;
4956 end loop;
4957
4958 Root2 := Prefix (A2);
4959 while not Is_Entity_Name (Root2) loop
4960 if not Nkind_In (Root2, N_Selected_Component,
4961 N_Indexed_Component)
4962 then
4963 return False;
4964 else
4965 Root2 := Prefix (Root2);
4966 end if;
4967
4968 Depth2 := Depth2 + 1;
4969 end loop;
4970
4971 -- If both have the same depth and they do not denote the same
4972 -- object, they are disjoint and no warning is needed.
4973
4974 if Depth1 = Depth2 then
4975 return False;
4976
4977 elsif Depth1 > Depth2 then
4978 Root1 := Prefix (A1);
4979 for J in 1 .. Depth1 - Depth2 - 1 loop
4980 Root1 := Prefix (Root1);
4981 end loop;
4982
4983 return Denotes_Same_Object (Root1, A2);
4984
4985 else
4986 Root2 := Prefix (A2);
4987 for J in 1 .. Depth2 - Depth1 - 1 loop
4988 Root2 := Prefix (Root2);
4989 end loop;
4990
4991 return Denotes_Same_Object (A1, Root2);
4992 end if;
4993 end;
4994
4995 else
4996 return False;
4997 end if;
4998 end Denotes_Same_Prefix;
4999
5000 ----------------------
5001 -- Denotes_Variable --
5002 ----------------------
5003
5004 function Denotes_Variable (N : Node_Id) return Boolean is
5005 begin
5006 return Is_Variable (N) and then Paren_Count (N) = 0;
5007 end Denotes_Variable;
5008
5009 -----------------------------
5010 -- Depends_On_Discriminant --
5011 -----------------------------
5012
5013 function Depends_On_Discriminant (N : Node_Id) return Boolean is
5014 L : Node_Id;
5015 H : Node_Id;
5016
5017 begin
5018 Get_Index_Bounds (N, L, H);
5019 return Denotes_Discriminant (L) or else Denotes_Discriminant (H);
5020 end Depends_On_Discriminant;
5021
5022 -------------------------
5023 -- Designate_Same_Unit --
5024 -------------------------
5025
5026 function Designate_Same_Unit
5027 (Name1 : Node_Id;
5028 Name2 : Node_Id) return Boolean
5029 is
5030 K1 : constant Node_Kind := Nkind (Name1);
5031 K2 : constant Node_Kind := Nkind (Name2);
5032
5033 function Prefix_Node (N : Node_Id) return Node_Id;
5034 -- Returns the parent unit name node of a defining program unit name
5035 -- or the prefix if N is a selected component or an expanded name.
5036
5037 function Select_Node (N : Node_Id) return Node_Id;
5038 -- Returns the defining identifier node of a defining program unit
5039 -- name or the selector node if N is a selected component or an
5040 -- expanded name.
5041
5042 -----------------
5043 -- Prefix_Node --
5044 -----------------
5045
5046 function Prefix_Node (N : Node_Id) return Node_Id is
5047 begin
5048 if Nkind (N) = N_Defining_Program_Unit_Name then
5049 return Name (N);
5050 else
5051 return Prefix (N);
5052 end if;
5053 end Prefix_Node;
5054
5055 -----------------
5056 -- Select_Node --
5057 -----------------
5058
5059 function Select_Node (N : Node_Id) return Node_Id is
5060 begin
5061 if Nkind (N) = N_Defining_Program_Unit_Name then
5062 return Defining_Identifier (N);
5063 else
5064 return Selector_Name (N);
5065 end if;
5066 end Select_Node;
5067
5068 -- Start of processing for Designate_Same_Unit
5069
5070 begin
5071 if Nkind_In (K1, N_Identifier, N_Defining_Identifier)
5072 and then
5073 Nkind_In (K2, N_Identifier, N_Defining_Identifier)
5074 then
5075 return Chars (Name1) = Chars (Name2);
5076
5077 elsif Nkind_In (K1, N_Expanded_Name,
5078 N_Selected_Component,
5079 N_Defining_Program_Unit_Name)
5080 and then
5081 Nkind_In (K2, N_Expanded_Name,
5082 N_Selected_Component,
5083 N_Defining_Program_Unit_Name)
5084 then
5085 return
5086 (Chars (Select_Node (Name1)) = Chars (Select_Node (Name2)))
5087 and then
5088 Designate_Same_Unit (Prefix_Node (Name1), Prefix_Node (Name2));
5089
5090 else
5091 return False;
5092 end if;
5093 end Designate_Same_Unit;
5094
5095 ------------------------------------------
5096 -- function Dynamic_Accessibility_Level --
5097 ------------------------------------------
5098
5099 function Dynamic_Accessibility_Level (Expr : Node_Id) return Node_Id is
5100 E : Entity_Id;
5101 Loc : constant Source_Ptr := Sloc (Expr);
5102
5103 function Make_Level_Literal (Level : Uint) return Node_Id;
5104 -- Construct an integer literal representing an accessibility level
5105 -- with its type set to Natural.
5106
5107 ------------------------
5108 -- Make_Level_Literal --
5109 ------------------------
5110
5111 function Make_Level_Literal (Level : Uint) return Node_Id is
5112 Result : constant Node_Id := Make_Integer_Literal (Loc, Level);
5113 begin
5114 Set_Etype (Result, Standard_Natural);
5115 return Result;
5116 end Make_Level_Literal;
5117
5118 -- Start of processing for Dynamic_Accessibility_Level
5119
5120 begin
5121 if Is_Entity_Name (Expr) then
5122 E := Entity (Expr);
5123
5124 if Present (Renamed_Object (E)) then
5125 return Dynamic_Accessibility_Level (Renamed_Object (E));
5126 end if;
5127
5128 if Is_Formal (E) or else Ekind_In (E, E_Variable, E_Constant) then
5129 if Present (Extra_Accessibility (E)) then
5130 return New_Occurrence_Of (Extra_Accessibility (E), Loc);
5131 end if;
5132 end if;
5133 end if;
5134
5135 -- Unimplemented: Ptr.all'Access, where Ptr has Extra_Accessibility ???
5136
5137 case Nkind (Expr) is
5138
5139 -- For access discriminant, the level of the enclosing object
5140
5141 when N_Selected_Component =>
5142 if Ekind (Entity (Selector_Name (Expr))) = E_Discriminant
5143 and then Ekind (Etype (Entity (Selector_Name (Expr)))) =
5144 E_Anonymous_Access_Type
5145 then
5146 return Make_Level_Literal (Object_Access_Level (Expr));
5147 end if;
5148
5149 when N_Attribute_Reference =>
5150 case Get_Attribute_Id (Attribute_Name (Expr)) is
5151
5152 -- For X'Access, the level of the prefix X
5153
5154 when Attribute_Access =>
5155 return Make_Level_Literal
5156 (Object_Access_Level (Prefix (Expr)));
5157
5158 -- Treat the unchecked attributes as library-level
5159
5160 when Attribute_Unchecked_Access |
5161 Attribute_Unrestricted_Access =>
5162 return Make_Level_Literal (Scope_Depth (Standard_Standard));
5163
5164 -- No other access-valued attributes
5165
5166 when others =>
5167 raise Program_Error;
5168 end case;
5169
5170 when N_Allocator =>
5171
5172 -- Unimplemented: depends on context. As an actual parameter where
5173 -- formal type is anonymous, use
5174 -- Scope_Depth (Current_Scope) + 1.
5175 -- For other cases, see 3.10.2(14/3) and following. ???
5176
5177 null;
5178
5179 when N_Type_Conversion =>
5180 if not Is_Local_Anonymous_Access (Etype (Expr)) then
5181
5182 -- Handle type conversions introduced for a rename of an
5183 -- Ada 2012 stand-alone object of an anonymous access type.
5184
5185 return Dynamic_Accessibility_Level (Expression (Expr));
5186 end if;
5187
5188 when others =>
5189 null;
5190 end case;
5191
5192 return Make_Level_Literal (Type_Access_Level (Etype (Expr)));
5193 end Dynamic_Accessibility_Level;
5194
5195 -----------------------------------
5196 -- Effective_Extra_Accessibility --
5197 -----------------------------------
5198
5199 function Effective_Extra_Accessibility (Id : Entity_Id) return Entity_Id is
5200 begin
5201 if Present (Renamed_Object (Id))
5202 and then Is_Entity_Name (Renamed_Object (Id))
5203 then
5204 return Effective_Extra_Accessibility (Entity (Renamed_Object (Id)));
5205 else
5206 return Extra_Accessibility (Id);
5207 end if;
5208 end Effective_Extra_Accessibility;
5209
5210 -----------------------------
5211 -- Effective_Reads_Enabled --
5212 -----------------------------
5213
5214 function Effective_Reads_Enabled (Id : Entity_Id) return Boolean is
5215 begin
5216 return Has_Enabled_Property (Id, Name_Effective_Reads);
5217 end Effective_Reads_Enabled;
5218
5219 ------------------------------
5220 -- Effective_Writes_Enabled --
5221 ------------------------------
5222
5223 function Effective_Writes_Enabled (Id : Entity_Id) return Boolean is
5224 begin
5225 return Has_Enabled_Property (Id, Name_Effective_Writes);
5226 end Effective_Writes_Enabled;
5227
5228 ------------------------------
5229 -- Enclosing_Comp_Unit_Node --
5230 ------------------------------
5231
5232 function Enclosing_Comp_Unit_Node (N : Node_Id) return Node_Id is
5233 Current_Node : Node_Id;
5234
5235 begin
5236 Current_Node := N;
5237 while Present (Current_Node)
5238 and then Nkind (Current_Node) /= N_Compilation_Unit
5239 loop
5240 Current_Node := Parent (Current_Node);
5241 end loop;
5242
5243 if Nkind (Current_Node) /= N_Compilation_Unit then
5244 return Empty;
5245 else
5246 return Current_Node;
5247 end if;
5248 end Enclosing_Comp_Unit_Node;
5249
5250 --------------------------
5251 -- Enclosing_CPP_Parent --
5252 --------------------------
5253
5254 function Enclosing_CPP_Parent (Typ : Entity_Id) return Entity_Id is
5255 Parent_Typ : Entity_Id := Typ;
5256
5257 begin
5258 while not Is_CPP_Class (Parent_Typ)
5259 and then Etype (Parent_Typ) /= Parent_Typ
5260 loop
5261 Parent_Typ := Etype (Parent_Typ);
5262
5263 if Is_Private_Type (Parent_Typ) then
5264 Parent_Typ := Full_View (Base_Type (Parent_Typ));
5265 end if;
5266 end loop;
5267
5268 pragma Assert (Is_CPP_Class (Parent_Typ));
5269 return Parent_Typ;
5270 end Enclosing_CPP_Parent;
5271
5272 ----------------------------
5273 -- Enclosing_Generic_Body --
5274 ----------------------------
5275
5276 function Enclosing_Generic_Body
5277 (N : Node_Id) return Node_Id
5278 is
5279 P : Node_Id;
5280 Decl : Node_Id;
5281 Spec : Node_Id;
5282
5283 begin
5284 P := Parent (N);
5285 while Present (P) loop
5286 if Nkind (P) = N_Package_Body
5287 or else Nkind (P) = N_Subprogram_Body
5288 then
5289 Spec := Corresponding_Spec (P);
5290
5291 if Present (Spec) then
5292 Decl := Unit_Declaration_Node (Spec);
5293
5294 if Nkind (Decl) = N_Generic_Package_Declaration
5295 or else Nkind (Decl) = N_Generic_Subprogram_Declaration
5296 then
5297 return P;
5298 end if;
5299 end if;
5300 end if;
5301
5302 P := Parent (P);
5303 end loop;
5304
5305 return Empty;
5306 end Enclosing_Generic_Body;
5307
5308 ----------------------------
5309 -- Enclosing_Generic_Unit --
5310 ----------------------------
5311
5312 function Enclosing_Generic_Unit
5313 (N : Node_Id) return Node_Id
5314 is
5315 P : Node_Id;
5316 Decl : Node_Id;
5317 Spec : Node_Id;
5318
5319 begin
5320 P := Parent (N);
5321 while Present (P) loop
5322 if Nkind (P) = N_Generic_Package_Declaration
5323 or else Nkind (P) = N_Generic_Subprogram_Declaration
5324 then
5325 return P;
5326
5327 elsif Nkind (P) = N_Package_Body
5328 or else Nkind (P) = N_Subprogram_Body
5329 then
5330 Spec := Corresponding_Spec (P);
5331
5332 if Present (Spec) then
5333 Decl := Unit_Declaration_Node (Spec);
5334
5335 if Nkind (Decl) = N_Generic_Package_Declaration
5336 or else Nkind (Decl) = N_Generic_Subprogram_Declaration
5337 then
5338 return Decl;
5339 end if;
5340 end if;
5341 end if;
5342
5343 P := Parent (P);
5344 end loop;
5345
5346 return Empty;
5347 end Enclosing_Generic_Unit;
5348
5349 -------------------------------
5350 -- Enclosing_Lib_Unit_Entity --
5351 -------------------------------
5352
5353 function Enclosing_Lib_Unit_Entity
5354 (E : Entity_Id := Current_Scope) return Entity_Id
5355 is
5356 Unit_Entity : Entity_Id;
5357
5358 begin
5359 -- Look for enclosing library unit entity by following scope links.
5360 -- Equivalent to, but faster than indexing through the scope stack.
5361
5362 Unit_Entity := E;
5363 while (Present (Scope (Unit_Entity))
5364 and then Scope (Unit_Entity) /= Standard_Standard)
5365 and not Is_Child_Unit (Unit_Entity)
5366 loop
5367 Unit_Entity := Scope (Unit_Entity);
5368 end loop;
5369
5370 return Unit_Entity;
5371 end Enclosing_Lib_Unit_Entity;
5372
5373 -----------------------
5374 -- Enclosing_Package --
5375 -----------------------
5376
5377 function Enclosing_Package (E : Entity_Id) return Entity_Id is
5378 Dynamic_Scope : constant Entity_Id := Enclosing_Dynamic_Scope (E);
5379
5380 begin
5381 if Dynamic_Scope = Standard_Standard then
5382 return Standard_Standard;
5383
5384 elsif Dynamic_Scope = Empty then
5385 return Empty;
5386
5387 elsif Ekind_In (Dynamic_Scope, E_Package, E_Package_Body,
5388 E_Generic_Package)
5389 then
5390 return Dynamic_Scope;
5391
5392 else
5393 return Enclosing_Package (Dynamic_Scope);
5394 end if;
5395 end Enclosing_Package;
5396
5397 --------------------------
5398 -- Enclosing_Subprogram --
5399 --------------------------
5400
5401 function Enclosing_Subprogram (E : Entity_Id) return Entity_Id is
5402 Dynamic_Scope : constant Entity_Id := Enclosing_Dynamic_Scope (E);
5403
5404 begin
5405 if Dynamic_Scope = Standard_Standard then
5406 return Empty;
5407
5408 elsif Dynamic_Scope = Empty then
5409 return Empty;
5410
5411 elsif Ekind (Dynamic_Scope) = E_Subprogram_Body then
5412 return Corresponding_Spec (Parent (Parent (Dynamic_Scope)));
5413
5414 elsif Ekind (Dynamic_Scope) = E_Block
5415 or else Ekind (Dynamic_Scope) = E_Return_Statement
5416 then
5417 return Enclosing_Subprogram (Dynamic_Scope);
5418
5419 elsif Ekind (Dynamic_Scope) = E_Task_Type then
5420 return Get_Task_Body_Procedure (Dynamic_Scope);
5421
5422 elsif Ekind (Dynamic_Scope) = E_Limited_Private_Type
5423 and then Present (Full_View (Dynamic_Scope))
5424 and then Ekind (Full_View (Dynamic_Scope)) = E_Task_Type
5425 then
5426 return Get_Task_Body_Procedure (Full_View (Dynamic_Scope));
5427
5428 -- No body is generated if the protected operation is eliminated
5429
5430 elsif Convention (Dynamic_Scope) = Convention_Protected
5431 and then not Is_Eliminated (Dynamic_Scope)
5432 and then Present (Protected_Body_Subprogram (Dynamic_Scope))
5433 then
5434 return Protected_Body_Subprogram (Dynamic_Scope);
5435
5436 else
5437 return Dynamic_Scope;
5438 end if;
5439 end Enclosing_Subprogram;
5440
5441 ------------------------
5442 -- Ensure_Freeze_Node --
5443 ------------------------
5444
5445 procedure Ensure_Freeze_Node (E : Entity_Id) is
5446 FN : Node_Id;
5447 begin
5448 if No (Freeze_Node (E)) then
5449 FN := Make_Freeze_Entity (Sloc (E));
5450 Set_Has_Delayed_Freeze (E);
5451 Set_Freeze_Node (E, FN);
5452 Set_Access_Types_To_Process (FN, No_Elist);
5453 Set_TSS_Elist (FN, No_Elist);
5454 Set_Entity (FN, E);
5455 end if;
5456 end Ensure_Freeze_Node;
5457
5458 ----------------
5459 -- Enter_Name --
5460 ----------------
5461
5462 procedure Enter_Name (Def_Id : Entity_Id) is
5463 C : constant Entity_Id := Current_Entity (Def_Id);
5464 E : constant Entity_Id := Current_Entity_In_Scope (Def_Id);
5465 S : constant Entity_Id := Current_Scope;
5466
5467 begin
5468 Generate_Definition (Def_Id);
5469
5470 -- Add new name to current scope declarations. Check for duplicate
5471 -- declaration, which may or may not be a genuine error.
5472
5473 if Present (E) then
5474
5475 -- Case of previous entity entered because of a missing declaration
5476 -- or else a bad subtype indication. Best is to use the new entity,
5477 -- and make the previous one invisible.
5478
5479 if Etype (E) = Any_Type then
5480 Set_Is_Immediately_Visible (E, False);
5481
5482 -- Case of renaming declaration constructed for package instances.
5483 -- if there is an explicit declaration with the same identifier,
5484 -- the renaming is not immediately visible any longer, but remains
5485 -- visible through selected component notation.
5486
5487 elsif Nkind (Parent (E)) = N_Package_Renaming_Declaration
5488 and then not Comes_From_Source (E)
5489 then
5490 Set_Is_Immediately_Visible (E, False);
5491
5492 -- The new entity may be the package renaming, which has the same
5493 -- same name as a generic formal which has been seen already.
5494
5495 elsif Nkind (Parent (Def_Id)) = N_Package_Renaming_Declaration
5496 and then not Comes_From_Source (Def_Id)
5497 then
5498 Set_Is_Immediately_Visible (E, False);
5499
5500 -- For a fat pointer corresponding to a remote access to subprogram,
5501 -- we use the same identifier as the RAS type, so that the proper
5502 -- name appears in the stub. This type is only retrieved through
5503 -- the RAS type and never by visibility, and is not added to the
5504 -- visibility list (see below).
5505
5506 elsif Nkind (Parent (Def_Id)) = N_Full_Type_Declaration
5507 and then Ekind (Def_Id) = E_Record_Type
5508 and then Present (Corresponding_Remote_Type (Def_Id))
5509 then
5510 null;
5511
5512 -- Case of an implicit operation or derived literal. The new entity
5513 -- hides the implicit one, which is removed from all visibility,
5514 -- i.e. the entity list of its scope, and homonym chain of its name.
5515
5516 elsif (Is_Overloadable (E) and then Is_Inherited_Operation (E))
5517 or else Is_Internal (E)
5518 then
5519 declare
5520 Prev : Entity_Id;
5521 Prev_Vis : Entity_Id;
5522 Decl : constant Node_Id := Parent (E);
5523
5524 begin
5525 -- If E is an implicit declaration, it cannot be the first
5526 -- entity in the scope.
5527
5528 Prev := First_Entity (Current_Scope);
5529 while Present (Prev) and then Next_Entity (Prev) /= E loop
5530 Next_Entity (Prev);
5531 end loop;
5532
5533 if No (Prev) then
5534
5535 -- If E is not on the entity chain of the current scope,
5536 -- it is an implicit declaration in the generic formal
5537 -- part of a generic subprogram. When analyzing the body,
5538 -- the generic formals are visible but not on the entity
5539 -- chain of the subprogram. The new entity will become
5540 -- the visible one in the body.
5541
5542 pragma Assert
5543 (Nkind (Parent (Decl)) = N_Generic_Subprogram_Declaration);
5544 null;
5545
5546 else
5547 Set_Next_Entity (Prev, Next_Entity (E));
5548
5549 if No (Next_Entity (Prev)) then
5550 Set_Last_Entity (Current_Scope, Prev);
5551 end if;
5552
5553 if E = Current_Entity (E) then
5554 Prev_Vis := Empty;
5555
5556 else
5557 Prev_Vis := Current_Entity (E);
5558 while Homonym (Prev_Vis) /= E loop
5559 Prev_Vis := Homonym (Prev_Vis);
5560 end loop;
5561 end if;
5562
5563 if Present (Prev_Vis) then
5564
5565 -- Skip E in the visibility chain
5566
5567 Set_Homonym (Prev_Vis, Homonym (E));
5568
5569 else
5570 Set_Name_Entity_Id (Chars (E), Homonym (E));
5571 end if;
5572 end if;
5573 end;
5574
5575 -- This section of code could use a comment ???
5576
5577 elsif Present (Etype (E))
5578 and then Is_Concurrent_Type (Etype (E))
5579 and then E = Def_Id
5580 then
5581 return;
5582
5583 -- If the homograph is a protected component renaming, it should not
5584 -- be hiding the current entity. Such renamings are treated as weak
5585 -- declarations.
5586
5587 elsif Is_Prival (E) then
5588 Set_Is_Immediately_Visible (E, False);
5589
5590 -- In this case the current entity is a protected component renaming.
5591 -- Perform minimal decoration by setting the scope and return since
5592 -- the prival should not be hiding other visible entities.
5593
5594 elsif Is_Prival (Def_Id) then
5595 Set_Scope (Def_Id, Current_Scope);
5596 return;
5597
5598 -- Analogous to privals, the discriminal generated for an entry index
5599 -- parameter acts as a weak declaration. Perform minimal decoration
5600 -- to avoid bogus errors.
5601
5602 elsif Is_Discriminal (Def_Id)
5603 and then Ekind (Discriminal_Link (Def_Id)) = E_Entry_Index_Parameter
5604 then
5605 Set_Scope (Def_Id, Current_Scope);
5606 return;
5607
5608 -- In the body or private part of an instance, a type extension may
5609 -- introduce a component with the same name as that of an actual. The
5610 -- legality rule is not enforced, but the semantics of the full type
5611 -- with two components of same name are not clear at this point???
5612
5613 elsif In_Instance_Not_Visible then
5614 null;
5615
5616 -- When compiling a package body, some child units may have become
5617 -- visible. They cannot conflict with local entities that hide them.
5618
5619 elsif Is_Child_Unit (E)
5620 and then In_Open_Scopes (Scope (E))
5621 and then not Is_Immediately_Visible (E)
5622 then
5623 null;
5624
5625 -- Conversely, with front-end inlining we may compile the parent body
5626 -- first, and a child unit subsequently. The context is now the
5627 -- parent spec, and body entities are not visible.
5628
5629 elsif Is_Child_Unit (Def_Id)
5630 and then Is_Package_Body_Entity (E)
5631 and then not In_Package_Body (Current_Scope)
5632 then
5633 null;
5634
5635 -- Case of genuine duplicate declaration
5636
5637 else
5638 Error_Msg_Sloc := Sloc (E);
5639
5640 -- If the previous declaration is an incomplete type declaration
5641 -- this may be an attempt to complete it with a private type. The
5642 -- following avoids confusing cascaded errors.
5643
5644 if Nkind (Parent (E)) = N_Incomplete_Type_Declaration
5645 and then Nkind (Parent (Def_Id)) = N_Private_Type_Declaration
5646 then
5647 Error_Msg_N
5648 ("incomplete type cannot be completed with a private " &
5649 "declaration", Parent (Def_Id));
5650 Set_Is_Immediately_Visible (E, False);
5651 Set_Full_View (E, Def_Id);
5652
5653 -- An inherited component of a record conflicts with a new
5654 -- discriminant. The discriminant is inserted first in the scope,
5655 -- but the error should be posted on it, not on the component.
5656
5657 elsif Ekind (E) = E_Discriminant
5658 and then Present (Scope (Def_Id))
5659 and then Scope (Def_Id) /= Current_Scope
5660 then
5661 Error_Msg_Sloc := Sloc (Def_Id);
5662 Error_Msg_N ("& conflicts with declaration#", E);
5663 return;
5664
5665 -- If the name of the unit appears in its own context clause, a
5666 -- dummy package with the name has already been created, and the
5667 -- error emitted. Try to continue quietly.
5668
5669 elsif Error_Posted (E)
5670 and then Sloc (E) = No_Location
5671 and then Nkind (Parent (E)) = N_Package_Specification
5672 and then Current_Scope = Standard_Standard
5673 then
5674 Set_Scope (Def_Id, Current_Scope);
5675 return;
5676
5677 else
5678 Error_Msg_N ("& conflicts with declaration#", Def_Id);
5679
5680 -- Avoid cascaded messages with duplicate components in
5681 -- derived types.
5682
5683 if Ekind_In (E, E_Component, E_Discriminant) then
5684 return;
5685 end if;
5686 end if;
5687
5688 if Nkind (Parent (Parent (Def_Id))) =
5689 N_Generic_Subprogram_Declaration
5690 and then Def_Id =
5691 Defining_Entity (Specification (Parent (Parent (Def_Id))))
5692 then
5693 Error_Msg_N ("\generic units cannot be overloaded", Def_Id);
5694 end if;
5695
5696 -- If entity is in standard, then we are in trouble, because it
5697 -- means that we have a library package with a duplicated name.
5698 -- That's hard to recover from, so abort.
5699
5700 if S = Standard_Standard then
5701 raise Unrecoverable_Error;
5702
5703 -- Otherwise we continue with the declaration. Having two
5704 -- identical declarations should not cause us too much trouble.
5705
5706 else
5707 null;
5708 end if;
5709 end if;
5710 end if;
5711
5712 -- If we fall through, declaration is OK, at least OK enough to continue
5713
5714 -- If Def_Id is a discriminant or a record component we are in the midst
5715 -- of inheriting components in a derived record definition. Preserve
5716 -- their Ekind and Etype.
5717
5718 if Ekind_In (Def_Id, E_Discriminant, E_Component) then
5719 null;
5720
5721 -- If a type is already set, leave it alone (happens when a type
5722 -- declaration is reanalyzed following a call to the optimizer).
5723
5724 elsif Present (Etype (Def_Id)) then
5725 null;
5726
5727 -- Otherwise, the kind E_Void insures that premature uses of the entity
5728 -- will be detected. Any_Type insures that no cascaded errors will occur
5729
5730 else
5731 Set_Ekind (Def_Id, E_Void);
5732 Set_Etype (Def_Id, Any_Type);
5733 end if;
5734
5735 -- Inherited discriminants and components in derived record types are
5736 -- immediately visible. Itypes are not.
5737
5738 -- Unless the Itype is for a record type with a corresponding remote
5739 -- type (what is that about, it was not commented ???)
5740
5741 if Ekind_In (Def_Id, E_Discriminant, E_Component)
5742 or else
5743 ((not Is_Record_Type (Def_Id)
5744 or else No (Corresponding_Remote_Type (Def_Id)))
5745 and then not Is_Itype (Def_Id))
5746 then
5747 Set_Is_Immediately_Visible (Def_Id);
5748 Set_Current_Entity (Def_Id);
5749 end if;
5750
5751 Set_Homonym (Def_Id, C);
5752 Append_Entity (Def_Id, S);
5753 Set_Public_Status (Def_Id);
5754
5755 -- Declaring a homonym is not allowed in SPARK ...
5756
5757 if Present (C) and then Restriction_Check_Required (SPARK_05) then
5758 declare
5759 Enclosing_Subp : constant Node_Id := Enclosing_Subprogram (Def_Id);
5760 Enclosing_Pack : constant Node_Id := Enclosing_Package (Def_Id);
5761 Other_Scope : constant Node_Id := Enclosing_Dynamic_Scope (C);
5762
5763 begin
5764 -- ... unless the new declaration is in a subprogram, and the
5765 -- visible declaration is a variable declaration or a parameter
5766 -- specification outside that subprogram.
5767
5768 if Present (Enclosing_Subp)
5769 and then Nkind_In (Parent (C), N_Object_Declaration,
5770 N_Parameter_Specification)
5771 and then not Scope_Within_Or_Same (Other_Scope, Enclosing_Subp)
5772 then
5773 null;
5774
5775 -- ... or the new declaration is in a package, and the visible
5776 -- declaration occurs outside that package.
5777
5778 elsif Present (Enclosing_Pack)
5779 and then not Scope_Within_Or_Same (Other_Scope, Enclosing_Pack)
5780 then
5781 null;
5782
5783 -- ... or the new declaration is a component declaration in a
5784 -- record type definition.
5785
5786 elsif Nkind (Parent (Def_Id)) = N_Component_Declaration then
5787 null;
5788
5789 -- Don't issue error for non-source entities
5790
5791 elsif Comes_From_Source (Def_Id)
5792 and then Comes_From_Source (C)
5793 then
5794 Error_Msg_Sloc := Sloc (C);
5795 Check_SPARK_05_Restriction
5796 ("redeclaration of identifier &#", Def_Id);
5797 end if;
5798 end;
5799 end if;
5800
5801 -- Warn if new entity hides an old one
5802
5803 if Warn_On_Hiding and then Present (C)
5804
5805 -- Don't warn for record components since they always have a well
5806 -- defined scope which does not confuse other uses. Note that in
5807 -- some cases, Ekind has not been set yet.
5808
5809 and then Ekind (C) /= E_Component
5810 and then Ekind (C) /= E_Discriminant
5811 and then Nkind (Parent (C)) /= N_Component_Declaration
5812 and then Ekind (Def_Id) /= E_Component
5813 and then Ekind (Def_Id) /= E_Discriminant
5814 and then Nkind (Parent (Def_Id)) /= N_Component_Declaration
5815
5816 -- Don't warn for one character variables. It is too common to use
5817 -- such variables as locals and will just cause too many false hits.
5818
5819 and then Length_Of_Name (Chars (C)) /= 1
5820
5821 -- Don't warn for non-source entities
5822
5823 and then Comes_From_Source (C)
5824 and then Comes_From_Source (Def_Id)
5825
5826 -- Don't warn unless entity in question is in extended main source
5827
5828 and then In_Extended_Main_Source_Unit (Def_Id)
5829
5830 -- Finally, the hidden entity must be either immediately visible or
5831 -- use visible (i.e. from a used package).
5832
5833 and then
5834 (Is_Immediately_Visible (C)
5835 or else
5836 Is_Potentially_Use_Visible (C))
5837 then
5838 Error_Msg_Sloc := Sloc (C);
5839 Error_Msg_N ("declaration hides &#?h?", Def_Id);
5840 end if;
5841 end Enter_Name;
5842
5843 ---------------
5844 -- Entity_Of --
5845 ---------------
5846
5847 function Entity_Of (N : Node_Id) return Entity_Id is
5848 Id : Entity_Id;
5849
5850 begin
5851 Id := Empty;
5852
5853 if Is_Entity_Name (N) then
5854 Id := Entity (N);
5855
5856 -- Follow a possible chain of renamings to reach the root renamed
5857 -- object.
5858
5859 while Present (Id) and then Present (Renamed_Object (Id)) loop
5860 if Is_Entity_Name (Renamed_Object (Id)) then
5861 Id := Entity (Renamed_Object (Id));
5862 else
5863 Id := Empty;
5864 exit;
5865 end if;
5866 end loop;
5867 end if;
5868
5869 return Id;
5870 end Entity_Of;
5871
5872 --------------------------
5873 -- Explain_Limited_Type --
5874 --------------------------
5875
5876 procedure Explain_Limited_Type (T : Entity_Id; N : Node_Id) is
5877 C : Entity_Id;
5878
5879 begin
5880 -- For array, component type must be limited
5881
5882 if Is_Array_Type (T) then
5883 Error_Msg_Node_2 := T;
5884 Error_Msg_NE
5885 ("\component type& of type& is limited", N, Component_Type (T));
5886 Explain_Limited_Type (Component_Type (T), N);
5887
5888 elsif Is_Record_Type (T) then
5889
5890 -- No need for extra messages if explicit limited record
5891
5892 if Is_Limited_Record (Base_Type (T)) then
5893 return;
5894 end if;
5895
5896 -- Otherwise find a limited component. Check only components that
5897 -- come from source, or inherited components that appear in the
5898 -- source of the ancestor.
5899
5900 C := First_Component (T);
5901 while Present (C) loop
5902 if Is_Limited_Type (Etype (C))
5903 and then
5904 (Comes_From_Source (C)
5905 or else
5906 (Present (Original_Record_Component (C))
5907 and then
5908 Comes_From_Source (Original_Record_Component (C))))
5909 then
5910 Error_Msg_Node_2 := T;
5911 Error_Msg_NE ("\component& of type& has limited type", N, C);
5912 Explain_Limited_Type (Etype (C), N);
5913 return;
5914 end if;
5915
5916 Next_Component (C);
5917 end loop;
5918
5919 -- The type may be declared explicitly limited, even if no component
5920 -- of it is limited, in which case we fall out of the loop.
5921 return;
5922 end if;
5923 end Explain_Limited_Type;
5924
5925 -------------------------------
5926 -- Extensions_Visible_Status --
5927 -------------------------------
5928
5929 function Extensions_Visible_Status
5930 (Id : Entity_Id) return Extensions_Visible_Mode
5931 is
5932 Arg : Node_Id;
5933 Decl : Node_Id;
5934 Expr : Node_Id;
5935 Prag : Node_Id;
5936 Subp : Entity_Id;
5937
5938 begin
5939 -- When a formal parameter is subject to Extensions_Visible, the pragma
5940 -- is stored in the contract of related subprogram.
5941
5942 if Is_Formal (Id) then
5943 Subp := Scope (Id);
5944
5945 elsif Is_Subprogram_Or_Generic_Subprogram (Id) then
5946 Subp := Id;
5947
5948 -- No other construct carries this pragma
5949
5950 else
5951 return Extensions_Visible_None;
5952 end if;
5953
5954 Prag := Get_Pragma (Subp, Pragma_Extensions_Visible);
5955
5956 -- In certain cases analysis may request the Extensions_Visible status
5957 -- of an expression function before the pragma has been analyzed yet.
5958 -- Inspect the declarative items after the expression function looking
5959 -- for the pragma (if any).
5960
5961 if No (Prag) and then Is_Expression_Function (Subp) then
5962 Decl := Next (Unit_Declaration_Node (Subp));
5963 while Present (Decl) loop
5964 if Nkind (Decl) = N_Pragma
5965 and then Pragma_Name (Decl) = Name_Extensions_Visible
5966 then
5967 Prag := Decl;
5968 exit;
5969
5970 -- A source construct ends the region where Extensions_Visible may
5971 -- appear, stop the traversal. An expanded expression function is
5972 -- no longer a source construct, but it must still be recognized.
5973
5974 elsif Comes_From_Source (Decl)
5975 or else
5976 (Nkind_In (Decl, N_Subprogram_Body,
5977 N_Subprogram_Declaration)
5978 and then Is_Expression_Function (Defining_Entity (Decl)))
5979 then
5980 exit;
5981 end if;
5982
5983 Next (Decl);
5984 end loop;
5985 end if;
5986
5987 -- Extract the value from the Boolean expression (if any)
5988
5989 if Present (Prag) then
5990 Arg := First (Pragma_Argument_Associations (Prag));
5991
5992 if Present (Arg) then
5993 Expr := Get_Pragma_Arg (Arg);
5994
5995 -- When the associated subprogram is an expression function, the
5996 -- argument of the pragma may not have been analyzed.
5997
5998 if not Analyzed (Expr) then
5999 Preanalyze_And_Resolve (Expr, Standard_Boolean);
6000 end if;
6001
6002 -- Guard against cascading errors when the argument of pragma
6003 -- Extensions_Visible is not a valid static Boolean expression.
6004
6005 if Error_Posted (Expr) then
6006 return Extensions_Visible_None;
6007
6008 elsif Is_True (Expr_Value (Expr)) then
6009 return Extensions_Visible_True;
6010
6011 else
6012 return Extensions_Visible_False;
6013 end if;
6014
6015 -- Otherwise the aspect or pragma defaults to True
6016
6017 else
6018 return Extensions_Visible_True;
6019 end if;
6020
6021 -- Otherwise aspect or pragma Extensions_Visible is not inherited or
6022 -- directly specified. In SPARK code, its value defaults to "False".
6023
6024 elsif SPARK_Mode = On then
6025 return Extensions_Visible_False;
6026
6027 -- In non-SPARK code, aspect or pragma Extensions_Visible defaults to
6028 -- "True".
6029
6030 else
6031 return Extensions_Visible_True;
6032 end if;
6033 end Extensions_Visible_Status;
6034
6035 -----------------
6036 -- Find_Actual --
6037 -----------------
6038
6039 procedure Find_Actual
6040 (N : Node_Id;
6041 Formal : out Entity_Id;
6042 Call : out Node_Id)
6043 is
6044 Parnt : constant Node_Id := Parent (N);
6045 Actual : Node_Id;
6046
6047 begin
6048 if Nkind_In (Parnt, N_Indexed_Component, N_Selected_Component)
6049 and then N = Prefix (Parnt)
6050 then
6051 Find_Actual (Parnt, Formal, Call);
6052 return;
6053
6054 elsif Nkind (Parnt) = N_Parameter_Association
6055 and then N = Explicit_Actual_Parameter (Parnt)
6056 then
6057 Call := Parent (Parnt);
6058
6059 elsif Nkind (Parnt) in N_Subprogram_Call then
6060 Call := Parnt;
6061
6062 else
6063 Formal := Empty;
6064 Call := Empty;
6065 return;
6066 end if;
6067
6068 -- If we have a call to a subprogram look for the parameter. Note that
6069 -- we exclude overloaded calls, since we don't know enough to be sure
6070 -- of giving the right answer in this case.
6071
6072 if Nkind_In (Call, N_Function_Call, N_Procedure_Call_Statement)
6073 and then Is_Entity_Name (Name (Call))
6074 and then Present (Entity (Name (Call)))
6075 and then Is_Overloadable (Entity (Name (Call)))
6076 and then not Is_Overloaded (Name (Call))
6077 then
6078 -- Fall here if we are definitely a parameter
6079
6080 Actual := First_Actual (Call);
6081 Formal := First_Formal (Entity (Name (Call)));
6082 while Present (Formal) and then Present (Actual) loop
6083 if Actual = N then
6084 return;
6085
6086 -- An actual that is the prefix in a prefixed call may have
6087 -- been rewritten in the call, after the deferred reference
6088 -- was collected. Check if sloc and kinds and names match.
6089
6090 elsif Sloc (Actual) = Sloc (N)
6091 and then Nkind (Actual) = N_Identifier
6092 and then Nkind (Actual) = Nkind (N)
6093 and then Chars (Actual) = Chars (N)
6094 then
6095 return;
6096
6097 else
6098 Actual := Next_Actual (Actual);
6099 Formal := Next_Formal (Formal);
6100 end if;
6101 end loop;
6102 end if;
6103
6104 -- Fall through here if we did not find matching actual
6105
6106 Formal := Empty;
6107 Call := Empty;
6108 end Find_Actual;
6109
6110 ---------------------------
6111 -- Find_Body_Discriminal --
6112 ---------------------------
6113
6114 function Find_Body_Discriminal
6115 (Spec_Discriminant : Entity_Id) return Entity_Id
6116 is
6117 Tsk : Entity_Id;
6118 Disc : Entity_Id;
6119
6120 begin
6121 -- If expansion is suppressed, then the scope can be the concurrent type
6122 -- itself rather than a corresponding concurrent record type.
6123
6124 if Is_Concurrent_Type (Scope (Spec_Discriminant)) then
6125 Tsk := Scope (Spec_Discriminant);
6126
6127 else
6128 pragma Assert (Is_Concurrent_Record_Type (Scope (Spec_Discriminant)));
6129
6130 Tsk := Corresponding_Concurrent_Type (Scope (Spec_Discriminant));
6131 end if;
6132
6133 -- Find discriminant of original concurrent type, and use its current
6134 -- discriminal, which is the renaming within the task/protected body.
6135
6136 Disc := First_Discriminant (Tsk);
6137 while Present (Disc) loop
6138 if Chars (Disc) = Chars (Spec_Discriminant) then
6139 return Discriminal (Disc);
6140 end if;
6141
6142 Next_Discriminant (Disc);
6143 end loop;
6144
6145 -- That loop should always succeed in finding a matching entry and
6146 -- returning. Fatal error if not.
6147
6148 raise Program_Error;
6149 end Find_Body_Discriminal;
6150
6151 -------------------------------------
6152 -- Find_Corresponding_Discriminant --
6153 -------------------------------------
6154
6155 function Find_Corresponding_Discriminant
6156 (Id : Node_Id;
6157 Typ : Entity_Id) return Entity_Id
6158 is
6159 Par_Disc : Entity_Id;
6160 Old_Disc : Entity_Id;
6161 New_Disc : Entity_Id;
6162
6163 begin
6164 Par_Disc := Original_Record_Component (Original_Discriminant (Id));
6165
6166 -- The original type may currently be private, and the discriminant
6167 -- only appear on its full view.
6168
6169 if Is_Private_Type (Scope (Par_Disc))
6170 and then not Has_Discriminants (Scope (Par_Disc))
6171 and then Present (Full_View (Scope (Par_Disc)))
6172 then
6173 Old_Disc := First_Discriminant (Full_View (Scope (Par_Disc)));
6174 else
6175 Old_Disc := First_Discriminant (Scope (Par_Disc));
6176 end if;
6177
6178 if Is_Class_Wide_Type (Typ) then
6179 New_Disc := First_Discriminant (Root_Type (Typ));
6180 else
6181 New_Disc := First_Discriminant (Typ);
6182 end if;
6183
6184 while Present (Old_Disc) and then Present (New_Disc) loop
6185 if Old_Disc = Par_Disc then
6186 return New_Disc;
6187 end if;
6188
6189 Next_Discriminant (Old_Disc);
6190 Next_Discriminant (New_Disc);
6191 end loop;
6192
6193 -- Should always find it
6194
6195 raise Program_Error;
6196 end Find_Corresponding_Discriminant;
6197
6198 ----------------------------------
6199 -- Find_Enclosing_Iterator_Loop --
6200 ----------------------------------
6201
6202 function Find_Enclosing_Iterator_Loop (Id : Entity_Id) return Entity_Id is
6203 Constr : Node_Id;
6204 S : Entity_Id;
6205
6206 begin
6207 -- Traverse the scope chain looking for an iterator loop. Such loops are
6208 -- usually transformed into blocks, hence the use of Original_Node.
6209
6210 S := Id;
6211 while Present (S) and then S /= Standard_Standard loop
6212 if Ekind (S) = E_Loop
6213 and then Nkind (Parent (S)) = N_Implicit_Label_Declaration
6214 then
6215 Constr := Original_Node (Label_Construct (Parent (S)));
6216
6217 if Nkind (Constr) = N_Loop_Statement
6218 and then Present (Iteration_Scheme (Constr))
6219 and then Nkind (Iterator_Specification
6220 (Iteration_Scheme (Constr))) =
6221 N_Iterator_Specification
6222 then
6223 return S;
6224 end if;
6225 end if;
6226
6227 S := Scope (S);
6228 end loop;
6229
6230 return Empty;
6231 end Find_Enclosing_Iterator_Loop;
6232
6233 ------------------------------------
6234 -- Find_Loop_In_Conditional_Block --
6235 ------------------------------------
6236
6237 function Find_Loop_In_Conditional_Block (N : Node_Id) return Node_Id is
6238 Stmt : Node_Id;
6239
6240 begin
6241 Stmt := N;
6242
6243 if Nkind (Stmt) = N_If_Statement then
6244 Stmt := First (Then_Statements (Stmt));
6245 end if;
6246
6247 pragma Assert (Nkind (Stmt) = N_Block_Statement);
6248
6249 -- Inspect the statements of the conditional block. In general the loop
6250 -- should be the first statement in the statement sequence of the block,
6251 -- but the finalization machinery may have introduced extra object
6252 -- declarations.
6253
6254 Stmt := First (Statements (Handled_Statement_Sequence (Stmt)));
6255 while Present (Stmt) loop
6256 if Nkind (Stmt) = N_Loop_Statement then
6257 return Stmt;
6258 end if;
6259
6260 Next (Stmt);
6261 end loop;
6262
6263 -- The expansion of attribute 'Loop_Entry produced a malformed block
6264
6265 raise Program_Error;
6266 end Find_Loop_In_Conditional_Block;
6267
6268 --------------------------
6269 -- Find_Overlaid_Entity --
6270 --------------------------
6271
6272 procedure Find_Overlaid_Entity
6273 (N : Node_Id;
6274 Ent : out Entity_Id;
6275 Off : out Boolean)
6276 is
6277 Expr : Node_Id;
6278
6279 begin
6280 -- We are looking for one of the two following forms:
6281
6282 -- for X'Address use Y'Address
6283
6284 -- or
6285
6286 -- Const : constant Address := expr;
6287 -- ...
6288 -- for X'Address use Const;
6289
6290 -- In the second case, the expr is either Y'Address, or recursively a
6291 -- constant that eventually references Y'Address.
6292
6293 Ent := Empty;
6294 Off := False;
6295
6296 if Nkind (N) = N_Attribute_Definition_Clause
6297 and then Chars (N) = Name_Address
6298 then
6299 Expr := Expression (N);
6300
6301 -- This loop checks the form of the expression for Y'Address,
6302 -- using recursion to deal with intermediate constants.
6303
6304 loop
6305 -- Check for Y'Address
6306
6307 if Nkind (Expr) = N_Attribute_Reference
6308 and then Attribute_Name (Expr) = Name_Address
6309 then
6310 Expr := Prefix (Expr);
6311 exit;
6312
6313 -- Check for Const where Const is a constant entity
6314
6315 elsif Is_Entity_Name (Expr)
6316 and then Ekind (Entity (Expr)) = E_Constant
6317 then
6318 Expr := Constant_Value (Entity (Expr));
6319
6320 -- Anything else does not need checking
6321
6322 else
6323 return;
6324 end if;
6325 end loop;
6326
6327 -- This loop checks the form of the prefix for an entity, using
6328 -- recursion to deal with intermediate components.
6329
6330 loop
6331 -- Check for Y where Y is an entity
6332
6333 if Is_Entity_Name (Expr) then
6334 Ent := Entity (Expr);
6335 return;
6336
6337 -- Check for components
6338
6339 elsif
6340 Nkind_In (Expr, N_Selected_Component, N_Indexed_Component)
6341 then
6342 Expr := Prefix (Expr);
6343 Off := True;
6344
6345 -- Anything else does not need checking
6346
6347 else
6348 return;
6349 end if;
6350 end loop;
6351 end if;
6352 end Find_Overlaid_Entity;
6353
6354 -------------------------
6355 -- Find_Parameter_Type --
6356 -------------------------
6357
6358 function Find_Parameter_Type (Param : Node_Id) return Entity_Id is
6359 begin
6360 if Nkind (Param) /= N_Parameter_Specification then
6361 return Empty;
6362
6363 -- For an access parameter, obtain the type from the formal entity
6364 -- itself, because access to subprogram nodes do not carry a type.
6365 -- Shouldn't we always use the formal entity ???
6366
6367 elsif Nkind (Parameter_Type (Param)) = N_Access_Definition then
6368 return Etype (Defining_Identifier (Param));
6369
6370 else
6371 return Etype (Parameter_Type (Param));
6372 end if;
6373 end Find_Parameter_Type;
6374
6375 -----------------------------------
6376 -- Find_Placement_In_State_Space --
6377 -----------------------------------
6378
6379 procedure Find_Placement_In_State_Space
6380 (Item_Id : Entity_Id;
6381 Placement : out State_Space_Kind;
6382 Pack_Id : out Entity_Id)
6383 is
6384 Context : Entity_Id;
6385
6386 begin
6387 -- Assume that the item does not appear in the state space of a package
6388
6389 Placement := Not_In_Package;
6390 Pack_Id := Empty;
6391
6392 -- Climb the scope stack and examine the enclosing context
6393
6394 Context := Scope (Item_Id);
6395 while Present (Context) and then Context /= Standard_Standard loop
6396 if Ekind (Context) = E_Package then
6397 Pack_Id := Context;
6398
6399 -- A package body is a cut off point for the traversal as the item
6400 -- cannot be visible to the outside from this point on. Note that
6401 -- this test must be done first as a body is also classified as a
6402 -- private part.
6403
6404 if In_Package_Body (Context) then
6405 Placement := Body_State_Space;
6406 return;
6407
6408 -- The private part of a package is a cut off point for the
6409 -- traversal as the item cannot be visible to the outside from
6410 -- this point on.
6411
6412 elsif In_Private_Part (Context) then
6413 Placement := Private_State_Space;
6414 return;
6415
6416 -- When the item appears in the visible state space of a package,
6417 -- continue to climb the scope stack as this may not be the final
6418 -- state space.
6419
6420 else
6421 Placement := Visible_State_Space;
6422
6423 -- The visible state space of a child unit acts as the proper
6424 -- placement of an item.
6425
6426 if Is_Child_Unit (Context) then
6427 return;
6428 end if;
6429 end if;
6430
6431 -- The item or its enclosing package appear in a construct that has
6432 -- no state space.
6433
6434 else
6435 Placement := Not_In_Package;
6436 return;
6437 end if;
6438
6439 Context := Scope (Context);
6440 end loop;
6441 end Find_Placement_In_State_Space;
6442
6443 ------------------------
6444 -- Find_Specific_Type --
6445 ------------------------
6446
6447 function Find_Specific_Type (CW : Entity_Id) return Entity_Id is
6448 Typ : Entity_Id := Root_Type (CW);
6449
6450 begin
6451 if Ekind (Typ) = E_Incomplete_Type then
6452 if From_Limited_With (Typ) then
6453 Typ := Non_Limited_View (Typ);
6454 else
6455 Typ := Full_View (Typ);
6456 end if;
6457 end if;
6458
6459 if Is_Private_Type (Typ)
6460 and then not Is_Tagged_Type (Typ)
6461 and then Present (Full_View (Typ))
6462 then
6463 return Full_View (Typ);
6464 else
6465 return Typ;
6466 end if;
6467 end Find_Specific_Type;
6468
6469 -----------------------------
6470 -- Find_Static_Alternative --
6471 -----------------------------
6472
6473 function Find_Static_Alternative (N : Node_Id) return Node_Id is
6474 Expr : constant Node_Id := Expression (N);
6475 Val : constant Uint := Expr_Value (Expr);
6476 Alt : Node_Id;
6477 Choice : Node_Id;
6478
6479 begin
6480 Alt := First (Alternatives (N));
6481
6482 Search : loop
6483 if Nkind (Alt) /= N_Pragma then
6484 Choice := First (Discrete_Choices (Alt));
6485 while Present (Choice) loop
6486
6487 -- Others choice, always matches
6488
6489 if Nkind (Choice) = N_Others_Choice then
6490 exit Search;
6491
6492 -- Range, check if value is in the range
6493
6494 elsif Nkind (Choice) = N_Range then
6495 exit Search when
6496 Val >= Expr_Value (Low_Bound (Choice))
6497 and then
6498 Val <= Expr_Value (High_Bound (Choice));
6499
6500 -- Choice is a subtype name. Note that we know it must
6501 -- be a static subtype, since otherwise it would have
6502 -- been diagnosed as illegal.
6503
6504 elsif Is_Entity_Name (Choice)
6505 and then Is_Type (Entity (Choice))
6506 then
6507 exit Search when Is_In_Range (Expr, Etype (Choice),
6508 Assume_Valid => False);
6509
6510 -- Choice is a subtype indication
6511
6512 elsif Nkind (Choice) = N_Subtype_Indication then
6513 declare
6514 C : constant Node_Id := Constraint (Choice);
6515 R : constant Node_Id := Range_Expression (C);
6516
6517 begin
6518 exit Search when
6519 Val >= Expr_Value (Low_Bound (R))
6520 and then
6521 Val <= Expr_Value (High_Bound (R));
6522 end;
6523
6524 -- Choice is a simple expression
6525
6526 else
6527 exit Search when Val = Expr_Value (Choice);
6528 end if;
6529
6530 Next (Choice);
6531 end loop;
6532 end if;
6533
6534 Next (Alt);
6535 pragma Assert (Present (Alt));
6536 end loop Search;
6537
6538 -- The above loop *must* terminate by finding a match, since
6539 -- we know the case statement is valid, and the value of the
6540 -- expression is known at compile time. When we fall out of
6541 -- the loop, Alt points to the alternative that we know will
6542 -- be selected at run time.
6543
6544 return Alt;
6545 end Find_Static_Alternative;
6546
6547 ------------------
6548 -- First_Actual --
6549 ------------------
6550
6551 function First_Actual (Node : Node_Id) return Node_Id is
6552 N : Node_Id;
6553
6554 begin
6555 if No (Parameter_Associations (Node)) then
6556 return Empty;
6557 end if;
6558
6559 N := First (Parameter_Associations (Node));
6560
6561 if Nkind (N) = N_Parameter_Association then
6562 return First_Named_Actual (Node);
6563 else
6564 return N;
6565 end if;
6566 end First_Actual;
6567
6568 -----------------------
6569 -- Gather_Components --
6570 -----------------------
6571
6572 procedure Gather_Components
6573 (Typ : Entity_Id;
6574 Comp_List : Node_Id;
6575 Governed_By : List_Id;
6576 Into : Elist_Id;
6577 Report_Errors : out Boolean)
6578 is
6579 Assoc : Node_Id;
6580 Variant : Node_Id;
6581 Discrete_Choice : Node_Id;
6582 Comp_Item : Node_Id;
6583
6584 Discrim : Entity_Id;
6585 Discrim_Name : Node_Id;
6586 Discrim_Value : Node_Id;
6587
6588 begin
6589 Report_Errors := False;
6590
6591 if No (Comp_List) or else Null_Present (Comp_List) then
6592 return;
6593
6594 elsif Present (Component_Items (Comp_List)) then
6595 Comp_Item := First (Component_Items (Comp_List));
6596
6597 else
6598 Comp_Item := Empty;
6599 end if;
6600
6601 while Present (Comp_Item) loop
6602
6603 -- Skip the tag of a tagged record, the interface tags, as well
6604 -- as all items that are not user components (anonymous types,
6605 -- rep clauses, Parent field, controller field).
6606
6607 if Nkind (Comp_Item) = N_Component_Declaration then
6608 declare
6609 Comp : constant Entity_Id := Defining_Identifier (Comp_Item);
6610 begin
6611 if not Is_Tag (Comp) and then Chars (Comp) /= Name_uParent then
6612 Append_Elmt (Comp, Into);
6613 end if;
6614 end;
6615 end if;
6616
6617 Next (Comp_Item);
6618 end loop;
6619
6620 if No (Variant_Part (Comp_List)) then
6621 return;
6622 else
6623 Discrim_Name := Name (Variant_Part (Comp_List));
6624 Variant := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
6625 end if;
6626
6627 -- Look for the discriminant that governs this variant part.
6628 -- The discriminant *must* be in the Governed_By List
6629
6630 Assoc := First (Governed_By);
6631 Find_Constraint : loop
6632 Discrim := First (Choices (Assoc));
6633 exit Find_Constraint when Chars (Discrim_Name) = Chars (Discrim)
6634 or else (Present (Corresponding_Discriminant (Entity (Discrim)))
6635 and then
6636 Chars (Corresponding_Discriminant (Entity (Discrim))) =
6637 Chars (Discrim_Name))
6638 or else Chars (Original_Record_Component (Entity (Discrim)))
6639 = Chars (Discrim_Name);
6640
6641 if No (Next (Assoc)) then
6642 if not Is_Constrained (Typ)
6643 and then Is_Derived_Type (Typ)
6644 and then Present (Stored_Constraint (Typ))
6645 then
6646 -- If the type is a tagged type with inherited discriminants,
6647 -- use the stored constraint on the parent in order to find
6648 -- the values of discriminants that are otherwise hidden by an
6649 -- explicit constraint. Renamed discriminants are handled in
6650 -- the code above.
6651
6652 -- If several parent discriminants are renamed by a single
6653 -- discriminant of the derived type, the call to obtain the
6654 -- Corresponding_Discriminant field only retrieves the last
6655 -- of them. We recover the constraint on the others from the
6656 -- Stored_Constraint as well.
6657
6658 declare
6659 D : Entity_Id;
6660 C : Elmt_Id;
6661
6662 begin
6663 D := First_Discriminant (Etype (Typ));
6664 C := First_Elmt (Stored_Constraint (Typ));
6665 while Present (D) and then Present (C) loop
6666 if Chars (Discrim_Name) = Chars (D) then
6667 if Is_Entity_Name (Node (C))
6668 and then Entity (Node (C)) = Entity (Discrim)
6669 then
6670 -- D is renamed by Discrim, whose value is given in
6671 -- Assoc.
6672
6673 null;
6674
6675 else
6676 Assoc :=
6677 Make_Component_Association (Sloc (Typ),
6678 New_List
6679 (New_Occurrence_Of (D, Sloc (Typ))),
6680 Duplicate_Subexpr_No_Checks (Node (C)));
6681 end if;
6682 exit Find_Constraint;
6683 end if;
6684
6685 Next_Discriminant (D);
6686 Next_Elmt (C);
6687 end loop;
6688 end;
6689 end if;
6690 end if;
6691
6692 if No (Next (Assoc)) then
6693 Error_Msg_NE (" missing value for discriminant&",
6694 First (Governed_By), Discrim_Name);
6695 Report_Errors := True;
6696 return;
6697 end if;
6698
6699 Next (Assoc);
6700 end loop Find_Constraint;
6701
6702 Discrim_Value := Expression (Assoc);
6703
6704 if not Is_OK_Static_Expression (Discrim_Value) then
6705 Error_Msg_FE
6706 ("value for discriminant & must be static!",
6707 Discrim_Value, Discrim);
6708 Why_Not_Static (Discrim_Value);
6709 Report_Errors := True;
6710 return;
6711 end if;
6712
6713 Search_For_Discriminant_Value : declare
6714 Low : Node_Id;
6715 High : Node_Id;
6716
6717 UI_High : Uint;
6718 UI_Low : Uint;
6719 UI_Discrim_Value : constant Uint := Expr_Value (Discrim_Value);
6720
6721 begin
6722 Find_Discrete_Value : while Present (Variant) loop
6723 Discrete_Choice := First (Discrete_Choices (Variant));
6724 while Present (Discrete_Choice) loop
6725 exit Find_Discrete_Value when
6726 Nkind (Discrete_Choice) = N_Others_Choice;
6727
6728 Get_Index_Bounds (Discrete_Choice, Low, High);
6729
6730 UI_Low := Expr_Value (Low);
6731 UI_High := Expr_Value (High);
6732
6733 exit Find_Discrete_Value when
6734 UI_Low <= UI_Discrim_Value
6735 and then
6736 UI_High >= UI_Discrim_Value;
6737
6738 Next (Discrete_Choice);
6739 end loop;
6740
6741 Next_Non_Pragma (Variant);
6742 end loop Find_Discrete_Value;
6743 end Search_For_Discriminant_Value;
6744
6745 if No (Variant) then
6746 Error_Msg_NE
6747 ("value of discriminant & is out of range", Discrim_Value, Discrim);
6748 Report_Errors := True;
6749 return;
6750 end if;
6751
6752 -- If we have found the corresponding choice, recursively add its
6753 -- components to the Into list.
6754
6755 Gather_Components
6756 (Empty, Component_List (Variant), Governed_By, Into, Report_Errors);
6757 end Gather_Components;
6758
6759 ------------------------
6760 -- Get_Actual_Subtype --
6761 ------------------------
6762
6763 function Get_Actual_Subtype (N : Node_Id) return Entity_Id is
6764 Typ : constant Entity_Id := Etype (N);
6765 Utyp : Entity_Id := Underlying_Type (Typ);
6766 Decl : Node_Id;
6767 Atyp : Entity_Id;
6768
6769 begin
6770 if No (Utyp) then
6771 Utyp := Typ;
6772 end if;
6773
6774 -- If what we have is an identifier that references a subprogram
6775 -- formal, or a variable or constant object, then we get the actual
6776 -- subtype from the referenced entity if one has been built.
6777
6778 if Nkind (N) = N_Identifier
6779 and then
6780 (Is_Formal (Entity (N))
6781 or else Ekind (Entity (N)) = E_Constant
6782 or else Ekind (Entity (N)) = E_Variable)
6783 and then Present (Actual_Subtype (Entity (N)))
6784 then
6785 return Actual_Subtype (Entity (N));
6786
6787 -- Actual subtype of unchecked union is always itself. We never need
6788 -- the "real" actual subtype. If we did, we couldn't get it anyway
6789 -- because the discriminant is not available. The restrictions on
6790 -- Unchecked_Union are designed to make sure that this is OK.
6791
6792 elsif Is_Unchecked_Union (Base_Type (Utyp)) then
6793 return Typ;
6794
6795 -- Here for the unconstrained case, we must find actual subtype
6796 -- No actual subtype is available, so we must build it on the fly.
6797
6798 -- Checking the type, not the underlying type, for constrainedness
6799 -- seems to be necessary. Maybe all the tests should be on the type???
6800
6801 elsif (not Is_Constrained (Typ))
6802 and then (Is_Array_Type (Utyp)
6803 or else (Is_Record_Type (Utyp)
6804 and then Has_Discriminants (Utyp)))
6805 and then not Has_Unknown_Discriminants (Utyp)
6806 and then not (Ekind (Utyp) = E_String_Literal_Subtype)
6807 then
6808 -- Nothing to do if in spec expression (why not???)
6809
6810 if In_Spec_Expression then
6811 return Typ;
6812
6813 elsif Is_Private_Type (Typ) and then not Has_Discriminants (Typ) then
6814
6815 -- If the type has no discriminants, there is no subtype to
6816 -- build, even if the underlying type is discriminated.
6817
6818 return Typ;
6819
6820 -- Else build the actual subtype
6821
6822 else
6823 Decl := Build_Actual_Subtype (Typ, N);
6824 Atyp := Defining_Identifier (Decl);
6825
6826 -- If Build_Actual_Subtype generated a new declaration then use it
6827
6828 if Atyp /= Typ then
6829
6830 -- The actual subtype is an Itype, so analyze the declaration,
6831 -- but do not attach it to the tree, to get the type defined.
6832
6833 Set_Parent (Decl, N);
6834 Set_Is_Itype (Atyp);
6835 Analyze (Decl, Suppress => All_Checks);
6836 Set_Associated_Node_For_Itype (Atyp, N);
6837 Set_Has_Delayed_Freeze (Atyp, False);
6838
6839 -- We need to freeze the actual subtype immediately. This is
6840 -- needed, because otherwise this Itype will not get frozen
6841 -- at all, and it is always safe to freeze on creation because
6842 -- any associated types must be frozen at this point.
6843
6844 Freeze_Itype (Atyp, N);
6845 return Atyp;
6846
6847 -- Otherwise we did not build a declaration, so return original
6848
6849 else
6850 return Typ;
6851 end if;
6852 end if;
6853
6854 -- For all remaining cases, the actual subtype is the same as
6855 -- the nominal type.
6856
6857 else
6858 return Typ;
6859 end if;
6860 end Get_Actual_Subtype;
6861
6862 -------------------------------------
6863 -- Get_Actual_Subtype_If_Available --
6864 -------------------------------------
6865
6866 function Get_Actual_Subtype_If_Available (N : Node_Id) return Entity_Id is
6867 Typ : constant Entity_Id := Etype (N);
6868
6869 begin
6870 -- If what we have is an identifier that references a subprogram
6871 -- formal, or a variable or constant object, then we get the actual
6872 -- subtype from the referenced entity if one has been built.
6873
6874 if Nkind (N) = N_Identifier
6875 and then
6876 (Is_Formal (Entity (N))
6877 or else Ekind (Entity (N)) = E_Constant
6878 or else Ekind (Entity (N)) = E_Variable)
6879 and then Present (Actual_Subtype (Entity (N)))
6880 then
6881 return Actual_Subtype (Entity (N));
6882
6883 -- Otherwise the Etype of N is returned unchanged
6884
6885 else
6886 return Typ;
6887 end if;
6888 end Get_Actual_Subtype_If_Available;
6889
6890 ------------------------
6891 -- Get_Body_From_Stub --
6892 ------------------------
6893
6894 function Get_Body_From_Stub (N : Node_Id) return Node_Id is
6895 begin
6896 return Proper_Body (Unit (Library_Unit (N)));
6897 end Get_Body_From_Stub;
6898
6899 ---------------------
6900 -- Get_Cursor_Type --
6901 ---------------------
6902
6903 function Get_Cursor_Type
6904 (Aspect : Node_Id;
6905 Typ : Entity_Id) return Entity_Id
6906 is
6907 Assoc : Node_Id;
6908 Func : Entity_Id;
6909 First_Op : Entity_Id;
6910 Cursor : Entity_Id;
6911
6912 begin
6913 -- If error already detected, return
6914
6915 if Error_Posted (Aspect) then
6916 return Any_Type;
6917 end if;
6918
6919 -- The cursor type for an Iterable aspect is the return type of a
6920 -- non-overloaded First primitive operation. Locate association for
6921 -- First.
6922
6923 Assoc := First (Component_Associations (Expression (Aspect)));
6924 First_Op := Any_Id;
6925 while Present (Assoc) loop
6926 if Chars (First (Choices (Assoc))) = Name_First then
6927 First_Op := Expression (Assoc);
6928 exit;
6929 end if;
6930
6931 Next (Assoc);
6932 end loop;
6933
6934 if First_Op = Any_Id then
6935 Error_Msg_N ("aspect Iterable must specify First operation", Aspect);
6936 return Any_Type;
6937 end if;
6938
6939 Cursor := Any_Type;
6940
6941 -- Locate function with desired name and profile in scope of type
6942
6943 Func := First_Entity (Scope (Typ));
6944 while Present (Func) loop
6945 if Chars (Func) = Chars (First_Op)
6946 and then Ekind (Func) = E_Function
6947 and then Present (First_Formal (Func))
6948 and then Etype (First_Formal (Func)) = Typ
6949 and then No (Next_Formal (First_Formal (Func)))
6950 then
6951 if Cursor /= Any_Type then
6952 Error_Msg_N
6953 ("Operation First for iterable type must be unique", Aspect);
6954 return Any_Type;
6955 else
6956 Cursor := Etype (Func);
6957 end if;
6958 end if;
6959
6960 Next_Entity (Func);
6961 end loop;
6962
6963 -- If not found, no way to resolve remaining primitives.
6964
6965 if Cursor = Any_Type then
6966 Error_Msg_N
6967 ("No legal primitive operation First for Iterable type", Aspect);
6968 end if;
6969
6970 return Cursor;
6971 end Get_Cursor_Type;
6972
6973 -------------------------------
6974 -- Get_Default_External_Name --
6975 -------------------------------
6976
6977 function Get_Default_External_Name (E : Node_Or_Entity_Id) return Node_Id is
6978 begin
6979 Get_Decoded_Name_String (Chars (E));
6980
6981 if Opt.External_Name_Imp_Casing = Uppercase then
6982 Set_Casing (All_Upper_Case);
6983 else
6984 Set_Casing (All_Lower_Case);
6985 end if;
6986
6987 return
6988 Make_String_Literal (Sloc (E),
6989 Strval => String_From_Name_Buffer);
6990 end Get_Default_External_Name;
6991
6992 --------------------------
6993 -- Get_Enclosing_Object --
6994 --------------------------
6995
6996 function Get_Enclosing_Object (N : Node_Id) return Entity_Id is
6997 begin
6998 if Is_Entity_Name (N) then
6999 return Entity (N);
7000 else
7001 case Nkind (N) is
7002 when N_Indexed_Component |
7003 N_Slice |
7004 N_Selected_Component =>
7005
7006 -- If not generating code, a dereference may be left implicit.
7007 -- In thoses cases, return Empty.
7008
7009 if Is_Access_Type (Etype (Prefix (N))) then
7010 return Empty;
7011 else
7012 return Get_Enclosing_Object (Prefix (N));
7013 end if;
7014
7015 when N_Type_Conversion =>
7016 return Get_Enclosing_Object (Expression (N));
7017
7018 when others =>
7019 return Empty;
7020 end case;
7021 end if;
7022 end Get_Enclosing_Object;
7023
7024 ---------------------------
7025 -- Get_Enum_Lit_From_Pos --
7026 ---------------------------
7027
7028 function Get_Enum_Lit_From_Pos
7029 (T : Entity_Id;
7030 Pos : Uint;
7031 Loc : Source_Ptr) return Node_Id
7032 is
7033 Btyp : Entity_Id := Base_Type (T);
7034 Lit : Node_Id;
7035
7036 begin
7037 -- In the case where the literal is of type Character, Wide_Character
7038 -- or Wide_Wide_Character or of a type derived from them, there needs
7039 -- to be some special handling since there is no explicit chain of
7040 -- literals to search. Instead, an N_Character_Literal node is created
7041 -- with the appropriate Char_Code and Chars fields.
7042
7043 if Is_Standard_Character_Type (T) then
7044 Set_Character_Literal_Name (UI_To_CC (Pos));
7045 return
7046 Make_Character_Literal (Loc,
7047 Chars => Name_Find,
7048 Char_Literal_Value => Pos);
7049
7050 -- For all other cases, we have a complete table of literals, and
7051 -- we simply iterate through the chain of literal until the one
7052 -- with the desired position value is found.
7053
7054 else
7055 if Is_Private_Type (Btyp) and then Present (Full_View (Btyp)) then
7056 Btyp := Full_View (Btyp);
7057 end if;
7058
7059 Lit := First_Literal (Btyp);
7060 for J in 1 .. UI_To_Int (Pos) loop
7061 Next_Literal (Lit);
7062 end loop;
7063
7064 return New_Occurrence_Of (Lit, Loc);
7065 end if;
7066 end Get_Enum_Lit_From_Pos;
7067
7068 ---------------------------------
7069 -- Get_Ensures_From_CTC_Pragma --
7070 ---------------------------------
7071
7072 function Get_Ensures_From_CTC_Pragma (N : Node_Id) return Node_Id is
7073 Args : constant List_Id := Pragma_Argument_Associations (N);
7074 Res : Node_Id;
7075
7076 begin
7077 if List_Length (Args) = 4 then
7078 Res := Pick (Args, 4);
7079
7080 elsif List_Length (Args) = 3 then
7081 Res := Pick (Args, 3);
7082
7083 if Chars (Res) /= Name_Ensures then
7084 Res := Empty;
7085 end if;
7086
7087 else
7088 Res := Empty;
7089 end if;
7090
7091 return Res;
7092 end Get_Ensures_From_CTC_Pragma;
7093
7094 ------------------------
7095 -- Get_Generic_Entity --
7096 ------------------------
7097
7098 function Get_Generic_Entity (N : Node_Id) return Entity_Id is
7099 Ent : constant Entity_Id := Entity (Name (N));
7100 begin
7101 if Present (Renamed_Object (Ent)) then
7102 return Renamed_Object (Ent);
7103 else
7104 return Ent;
7105 end if;
7106 end Get_Generic_Entity;
7107
7108 -------------------------------------
7109 -- Get_Incomplete_View_Of_Ancestor --
7110 -------------------------------------
7111
7112 function Get_Incomplete_View_Of_Ancestor (E : Entity_Id) return Entity_Id is
7113 Cur_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
7114 Par_Scope : Entity_Id;
7115 Par_Type : Entity_Id;
7116
7117 begin
7118 -- The incomplete view of an ancestor is only relevant for private
7119 -- derived types in child units.
7120
7121 if not Is_Derived_Type (E)
7122 or else not Is_Child_Unit (Cur_Unit)
7123 then
7124 return Empty;
7125
7126 else
7127 Par_Scope := Scope (Cur_Unit);
7128 if No (Par_Scope) then
7129 return Empty;
7130 end if;
7131
7132 Par_Type := Etype (Base_Type (E));
7133
7134 -- Traverse list of ancestor types until we find one declared in
7135 -- a parent or grandparent unit (two levels seem sufficient).
7136
7137 while Present (Par_Type) loop
7138 if Scope (Par_Type) = Par_Scope
7139 or else Scope (Par_Type) = Scope (Par_Scope)
7140 then
7141 return Par_Type;
7142
7143 elsif not Is_Derived_Type (Par_Type) then
7144 return Empty;
7145
7146 else
7147 Par_Type := Etype (Base_Type (Par_Type));
7148 end if;
7149 end loop;
7150
7151 -- If none found, there is no relevant ancestor type.
7152
7153 return Empty;
7154 end if;
7155 end Get_Incomplete_View_Of_Ancestor;
7156
7157 ----------------------
7158 -- Get_Index_Bounds --
7159 ----------------------
7160
7161 procedure Get_Index_Bounds (N : Node_Id; L, H : out Node_Id) is
7162 Kind : constant Node_Kind := Nkind (N);
7163 R : Node_Id;
7164
7165 begin
7166 if Kind = N_Range then
7167 L := Low_Bound (N);
7168 H := High_Bound (N);
7169
7170 elsif Kind = N_Subtype_Indication then
7171 R := Range_Expression (Constraint (N));
7172
7173 if R = Error then
7174 L := Error;
7175 H := Error;
7176 return;
7177
7178 else
7179 L := Low_Bound (Range_Expression (Constraint (N)));
7180 H := High_Bound (Range_Expression (Constraint (N)));
7181 end if;
7182
7183 elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
7184 if Error_Posted (Scalar_Range (Entity (N))) then
7185 L := Error;
7186 H := Error;
7187
7188 elsif Nkind (Scalar_Range (Entity (N))) = N_Subtype_Indication then
7189 Get_Index_Bounds (Scalar_Range (Entity (N)), L, H);
7190
7191 else
7192 L := Low_Bound (Scalar_Range (Entity (N)));
7193 H := High_Bound (Scalar_Range (Entity (N)));
7194 end if;
7195
7196 else
7197 -- N is an expression, indicating a range with one value
7198
7199 L := N;
7200 H := N;
7201 end if;
7202 end Get_Index_Bounds;
7203
7204 ---------------------------------
7205 -- Get_Iterable_Type_Primitive --
7206 ---------------------------------
7207
7208 function Get_Iterable_Type_Primitive
7209 (Typ : Entity_Id;
7210 Nam : Name_Id) return Entity_Id
7211 is
7212 Funcs : constant Node_Id := Find_Value_Of_Aspect (Typ, Aspect_Iterable);
7213 Assoc : Node_Id;
7214
7215 begin
7216 if No (Funcs) then
7217 return Empty;
7218
7219 else
7220 Assoc := First (Component_Associations (Funcs));
7221 while Present (Assoc) loop
7222 if Chars (First (Choices (Assoc))) = Nam then
7223 return Entity (Expression (Assoc));
7224 end if;
7225
7226 Assoc := Next (Assoc);
7227 end loop;
7228
7229 return Empty;
7230 end if;
7231 end Get_Iterable_Type_Primitive;
7232
7233 ----------------------------------
7234 -- Get_Library_Unit_Name_string --
7235 ----------------------------------
7236
7237 procedure Get_Library_Unit_Name_String (Decl_Node : Node_Id) is
7238 Unit_Name_Id : constant Unit_Name_Type := Get_Unit_Name (Decl_Node);
7239
7240 begin
7241 Get_Unit_Name_String (Unit_Name_Id);
7242
7243 -- Remove seven last character (" (spec)" or " (body)")
7244
7245 Name_Len := Name_Len - 7;
7246 pragma Assert (Name_Buffer (Name_Len + 1) = ' ');
7247 end Get_Library_Unit_Name_String;
7248
7249 ------------------------
7250 -- Get_Name_Entity_Id --
7251 ------------------------
7252
7253 function Get_Name_Entity_Id (Id : Name_Id) return Entity_Id is
7254 begin
7255 return Entity_Id (Get_Name_Table_Int (Id));
7256 end Get_Name_Entity_Id;
7257
7258 ------------------------------
7259 -- Get_Name_From_CTC_Pragma --
7260 ------------------------------
7261
7262 function Get_Name_From_CTC_Pragma (N : Node_Id) return String_Id is
7263 Arg : constant Node_Id :=
7264 Get_Pragma_Arg (First (Pragma_Argument_Associations (N)));
7265 begin
7266 return Strval (Expr_Value_S (Arg));
7267 end Get_Name_From_CTC_Pragma;
7268
7269 -----------------------
7270 -- Get_Parent_Entity --
7271 -----------------------
7272
7273 function Get_Parent_Entity (Unit : Node_Id) return Entity_Id is
7274 begin
7275 if Nkind (Unit) = N_Package_Body
7276 and then Nkind (Original_Node (Unit)) = N_Package_Instantiation
7277 then
7278 return Defining_Entity
7279 (Specification (Instance_Spec (Original_Node (Unit))));
7280 elsif Nkind (Unit) = N_Package_Instantiation then
7281 return Defining_Entity (Specification (Instance_Spec (Unit)));
7282 else
7283 return Defining_Entity (Unit);
7284 end if;
7285 end Get_Parent_Entity;
7286 -------------------
7287 -- Get_Pragma_Id --
7288 -------------------
7289
7290 function Get_Pragma_Id (N : Node_Id) return Pragma_Id is
7291 begin
7292 return Get_Pragma_Id (Pragma_Name (N));
7293 end Get_Pragma_Id;
7294
7295 -----------------------
7296 -- Get_Reason_String --
7297 -----------------------
7298
7299 procedure Get_Reason_String (N : Node_Id) is
7300 begin
7301 if Nkind (N) = N_String_Literal then
7302 Store_String_Chars (Strval (N));
7303
7304 elsif Nkind (N) = N_Op_Concat then
7305 Get_Reason_String (Left_Opnd (N));
7306 Get_Reason_String (Right_Opnd (N));
7307
7308 -- If not of required form, error
7309
7310 else
7311 Error_Msg_N
7312 ("Reason for pragma Warnings has wrong form", N);
7313 Error_Msg_N
7314 ("\must be string literal or concatenation of string literals", N);
7315 return;
7316 end if;
7317 end Get_Reason_String;
7318
7319 ---------------------------
7320 -- Get_Referenced_Object --
7321 ---------------------------
7322
7323 function Get_Referenced_Object (N : Node_Id) return Node_Id is
7324 R : Node_Id;
7325
7326 begin
7327 R := N;
7328 while Is_Entity_Name (R)
7329 and then Present (Renamed_Object (Entity (R)))
7330 loop
7331 R := Renamed_Object (Entity (R));
7332 end loop;
7333
7334 return R;
7335 end Get_Referenced_Object;
7336
7337 ------------------------
7338 -- Get_Renamed_Entity --
7339 ------------------------
7340
7341 function Get_Renamed_Entity (E : Entity_Id) return Entity_Id is
7342 R : Entity_Id;
7343
7344 begin
7345 R := E;
7346 while Present (Renamed_Entity (R)) loop
7347 R := Renamed_Entity (R);
7348 end loop;
7349
7350 return R;
7351 end Get_Renamed_Entity;
7352
7353 ----------------------------------
7354 -- Get_Requires_From_CTC_Pragma --
7355 ----------------------------------
7356
7357 function Get_Requires_From_CTC_Pragma (N : Node_Id) return Node_Id is
7358 Args : constant List_Id := Pragma_Argument_Associations (N);
7359 Res : Node_Id;
7360
7361 begin
7362 if List_Length (Args) >= 3 then
7363 Res := Pick (Args, 3);
7364
7365 if Chars (Res) /= Name_Requires then
7366 Res := Empty;
7367 end if;
7368
7369 else
7370 Res := Empty;
7371 end if;
7372
7373 return Res;
7374 end Get_Requires_From_CTC_Pragma;
7375
7376 -------------------------
7377 -- Get_Subprogram_Body --
7378 -------------------------
7379
7380 function Get_Subprogram_Body (E : Entity_Id) return Node_Id is
7381 Decl : Node_Id;
7382
7383 begin
7384 Decl := Unit_Declaration_Node (E);
7385
7386 if Nkind (Decl) = N_Subprogram_Body then
7387 return Decl;
7388
7389 -- The below comment is bad, because it is possible for
7390 -- Nkind (Decl) to be an N_Subprogram_Body_Stub ???
7391
7392 else -- Nkind (Decl) = N_Subprogram_Declaration
7393
7394 if Present (Corresponding_Body (Decl)) then
7395 return Unit_Declaration_Node (Corresponding_Body (Decl));
7396
7397 -- Imported subprogram case
7398
7399 else
7400 return Empty;
7401 end if;
7402 end if;
7403 end Get_Subprogram_Body;
7404
7405 ---------------------------
7406 -- Get_Subprogram_Entity --
7407 ---------------------------
7408
7409 function Get_Subprogram_Entity (Nod : Node_Id) return Entity_Id is
7410 Subp : Node_Id;
7411 Subp_Id : Entity_Id;
7412
7413 begin
7414 if Nkind (Nod) = N_Accept_Statement then
7415 Subp := Entry_Direct_Name (Nod);
7416
7417 elsif Nkind (Nod) = N_Slice then
7418 Subp := Prefix (Nod);
7419
7420 else
7421 Subp := Name (Nod);
7422 end if;
7423
7424 -- Strip the subprogram call
7425
7426 loop
7427 if Nkind_In (Subp, N_Explicit_Dereference,
7428 N_Indexed_Component,
7429 N_Selected_Component)
7430 then
7431 Subp := Prefix (Subp);
7432
7433 elsif Nkind_In (Subp, N_Type_Conversion,
7434 N_Unchecked_Type_Conversion)
7435 then
7436 Subp := Expression (Subp);
7437
7438 else
7439 exit;
7440 end if;
7441 end loop;
7442
7443 -- Extract the entity of the subprogram call
7444
7445 if Is_Entity_Name (Subp) then
7446 Subp_Id := Entity (Subp);
7447
7448 if Ekind (Subp_Id) = E_Access_Subprogram_Type then
7449 Subp_Id := Directly_Designated_Type (Subp_Id);
7450 end if;
7451
7452 if Is_Subprogram (Subp_Id) then
7453 return Subp_Id;
7454 else
7455 return Empty;
7456 end if;
7457
7458 -- The search did not find a construct that denotes a subprogram
7459
7460 else
7461 return Empty;
7462 end if;
7463 end Get_Subprogram_Entity;
7464
7465 -----------------------------
7466 -- Get_Task_Body_Procedure --
7467 -----------------------------
7468
7469 function Get_Task_Body_Procedure (E : Entity_Id) return Node_Id is
7470 begin
7471 -- Note: A task type may be the completion of a private type with
7472 -- discriminants. When performing elaboration checks on a task
7473 -- declaration, the current view of the type may be the private one,
7474 -- and the procedure that holds the body of the task is held in its
7475 -- underlying type.
7476
7477 -- This is an odd function, why not have Task_Body_Procedure do
7478 -- the following digging???
7479
7480 return Task_Body_Procedure (Underlying_Type (Root_Type (E)));
7481 end Get_Task_Body_Procedure;
7482
7483 -----------------------
7484 -- Has_Access_Values --
7485 -----------------------
7486
7487 function Has_Access_Values (T : Entity_Id) return Boolean is
7488 Typ : constant Entity_Id := Underlying_Type (T);
7489
7490 begin
7491 -- Case of a private type which is not completed yet. This can only
7492 -- happen in the case of a generic format type appearing directly, or
7493 -- as a component of the type to which this function is being applied
7494 -- at the top level. Return False in this case, since we certainly do
7495 -- not know that the type contains access types.
7496
7497 if No (Typ) then
7498 return False;
7499
7500 elsif Is_Access_Type (Typ) then
7501 return True;
7502
7503 elsif Is_Array_Type (Typ) then
7504 return Has_Access_Values (Component_Type (Typ));
7505
7506 elsif Is_Record_Type (Typ) then
7507 declare
7508 Comp : Entity_Id;
7509
7510 begin
7511 -- Loop to Check components
7512
7513 Comp := First_Component_Or_Discriminant (Typ);
7514 while Present (Comp) loop
7515
7516 -- Check for access component, tag field does not count, even
7517 -- though it is implemented internally using an access type.
7518
7519 if Has_Access_Values (Etype (Comp))
7520 and then Chars (Comp) /= Name_uTag
7521 then
7522 return True;
7523 end if;
7524
7525 Next_Component_Or_Discriminant (Comp);
7526 end loop;
7527 end;
7528
7529 return False;
7530
7531 else
7532 return False;
7533 end if;
7534 end Has_Access_Values;
7535
7536 ------------------------------
7537 -- Has_Compatible_Alignment --
7538 ------------------------------
7539
7540 function Has_Compatible_Alignment
7541 (Obj : Entity_Id;
7542 Expr : Node_Id) return Alignment_Result
7543 is
7544 function Has_Compatible_Alignment_Internal
7545 (Obj : Entity_Id;
7546 Expr : Node_Id;
7547 Default : Alignment_Result) return Alignment_Result;
7548 -- This is the internal recursive function that actually does the work.
7549 -- There is one additional parameter, which says what the result should
7550 -- be if no alignment information is found, and there is no definite
7551 -- indication of compatible alignments. At the outer level, this is set
7552 -- to Unknown, but for internal recursive calls in the case where types
7553 -- are known to be correct, it is set to Known_Compatible.
7554
7555 ---------------------------------------
7556 -- Has_Compatible_Alignment_Internal --
7557 ---------------------------------------
7558
7559 function Has_Compatible_Alignment_Internal
7560 (Obj : Entity_Id;
7561 Expr : Node_Id;
7562 Default : Alignment_Result) return Alignment_Result
7563 is
7564 Result : Alignment_Result := Known_Compatible;
7565 -- Holds the current status of the result. Note that once a value of
7566 -- Known_Incompatible is set, it is sticky and does not get changed
7567 -- to Unknown (the value in Result only gets worse as we go along,
7568 -- never better).
7569
7570 Offs : Uint := No_Uint;
7571 -- Set to a factor of the offset from the base object when Expr is a
7572 -- selected or indexed component, based on Component_Bit_Offset and
7573 -- Component_Size respectively. A negative value is used to represent
7574 -- a value which is not known at compile time.
7575
7576 procedure Check_Prefix;
7577 -- Checks the prefix recursively in the case where the expression
7578 -- is an indexed or selected component.
7579
7580 procedure Set_Result (R : Alignment_Result);
7581 -- If R represents a worse outcome (unknown instead of known
7582 -- compatible, or known incompatible), then set Result to R.
7583
7584 ------------------
7585 -- Check_Prefix --
7586 ------------------
7587
7588 procedure Check_Prefix is
7589 begin
7590 -- The subtlety here is that in doing a recursive call to check
7591 -- the prefix, we have to decide what to do in the case where we
7592 -- don't find any specific indication of an alignment problem.
7593
7594 -- At the outer level, we normally set Unknown as the result in
7595 -- this case, since we can only set Known_Compatible if we really
7596 -- know that the alignment value is OK, but for the recursive
7597 -- call, in the case where the types match, and we have not
7598 -- specified a peculiar alignment for the object, we are only
7599 -- concerned about suspicious rep clauses, the default case does
7600 -- not affect us, since the compiler will, in the absence of such
7601 -- rep clauses, ensure that the alignment is correct.
7602
7603 if Default = Known_Compatible
7604 or else
7605 (Etype (Obj) = Etype (Expr)
7606 and then (Unknown_Alignment (Obj)
7607 or else
7608 Alignment (Obj) = Alignment (Etype (Obj))))
7609 then
7610 Set_Result
7611 (Has_Compatible_Alignment_Internal
7612 (Obj, Prefix (Expr), Known_Compatible));
7613
7614 -- In all other cases, we need a full check on the prefix
7615
7616 else
7617 Set_Result
7618 (Has_Compatible_Alignment_Internal
7619 (Obj, Prefix (Expr), Unknown));
7620 end if;
7621 end Check_Prefix;
7622
7623 ----------------
7624 -- Set_Result --
7625 ----------------
7626
7627 procedure Set_Result (R : Alignment_Result) is
7628 begin
7629 if R > Result then
7630 Result := R;
7631 end if;
7632 end Set_Result;
7633
7634 -- Start of processing for Has_Compatible_Alignment_Internal
7635
7636 begin
7637 -- If Expr is a selected component, we must make sure there is no
7638 -- potentially troublesome component clause, and that the record is
7639 -- not packed.
7640
7641 if Nkind (Expr) = N_Selected_Component then
7642
7643 -- Packed record always generate unknown alignment
7644
7645 if Is_Packed (Etype (Prefix (Expr))) then
7646 Set_Result (Unknown);
7647 end if;
7648
7649 -- Check prefix and component offset
7650
7651 Check_Prefix;
7652 Offs := Component_Bit_Offset (Entity (Selector_Name (Expr)));
7653
7654 -- If Expr is an indexed component, we must make sure there is no
7655 -- potentially troublesome Component_Size clause and that the array
7656 -- is not bit-packed.
7657
7658 elsif Nkind (Expr) = N_Indexed_Component then
7659 declare
7660 Typ : constant Entity_Id := Etype (Prefix (Expr));
7661 Ind : constant Node_Id := First_Index (Typ);
7662
7663 begin
7664 -- Bit packed array always generates unknown alignment
7665
7666 if Is_Bit_Packed_Array (Typ) then
7667 Set_Result (Unknown);
7668 end if;
7669
7670 -- Check prefix and component offset
7671
7672 Check_Prefix;
7673 Offs := Component_Size (Typ);
7674
7675 -- Small optimization: compute the full offset when possible
7676
7677 if Offs /= No_Uint
7678 and then Offs > Uint_0
7679 and then Present (Ind)
7680 and then Nkind (Ind) = N_Range
7681 and then Compile_Time_Known_Value (Low_Bound (Ind))
7682 and then Compile_Time_Known_Value (First (Expressions (Expr)))
7683 then
7684 Offs := Offs * (Expr_Value (First (Expressions (Expr)))
7685 - Expr_Value (Low_Bound ((Ind))));
7686 end if;
7687 end;
7688 end if;
7689
7690 -- If we have a null offset, the result is entirely determined by
7691 -- the base object and has already been computed recursively.
7692
7693 if Offs = Uint_0 then
7694 null;
7695
7696 -- Case where we know the alignment of the object
7697
7698 elsif Known_Alignment (Obj) then
7699 declare
7700 ObjA : constant Uint := Alignment (Obj);
7701 ExpA : Uint := No_Uint;
7702 SizA : Uint := No_Uint;
7703
7704 begin
7705 -- If alignment of Obj is 1, then we are always OK
7706
7707 if ObjA = 1 then
7708 Set_Result (Known_Compatible);
7709
7710 -- Alignment of Obj is greater than 1, so we need to check
7711
7712 else
7713 -- If we have an offset, see if it is compatible
7714
7715 if Offs /= No_Uint and Offs > Uint_0 then
7716 if Offs mod (System_Storage_Unit * ObjA) /= 0 then
7717 Set_Result (Known_Incompatible);
7718 end if;
7719
7720 -- See if Expr is an object with known alignment
7721
7722 elsif Is_Entity_Name (Expr)
7723 and then Known_Alignment (Entity (Expr))
7724 then
7725 ExpA := Alignment (Entity (Expr));
7726
7727 -- Otherwise, we can use the alignment of the type of
7728 -- Expr given that we already checked for
7729 -- discombobulating rep clauses for the cases of indexed
7730 -- and selected components above.
7731
7732 elsif Known_Alignment (Etype (Expr)) then
7733 ExpA := Alignment (Etype (Expr));
7734
7735 -- Otherwise the alignment is unknown
7736
7737 else
7738 Set_Result (Default);
7739 end if;
7740
7741 -- If we got an alignment, see if it is acceptable
7742
7743 if ExpA /= No_Uint and then ExpA < ObjA then
7744 Set_Result (Known_Incompatible);
7745 end if;
7746
7747 -- If Expr is not a piece of a larger object, see if size
7748 -- is given. If so, check that it is not too small for the
7749 -- required alignment.
7750
7751 if Offs /= No_Uint then
7752 null;
7753
7754 -- See if Expr is an object with known size
7755
7756 elsif Is_Entity_Name (Expr)
7757 and then Known_Static_Esize (Entity (Expr))
7758 then
7759 SizA := Esize (Entity (Expr));
7760
7761 -- Otherwise, we check the object size of the Expr type
7762
7763 elsif Known_Static_Esize (Etype (Expr)) then
7764 SizA := Esize (Etype (Expr));
7765 end if;
7766
7767 -- If we got a size, see if it is a multiple of the Obj
7768 -- alignment, if not, then the alignment cannot be
7769 -- acceptable, since the size is always a multiple of the
7770 -- alignment.
7771
7772 if SizA /= No_Uint then
7773 if SizA mod (ObjA * Ttypes.System_Storage_Unit) /= 0 then
7774 Set_Result (Known_Incompatible);
7775 end if;
7776 end if;
7777 end if;
7778 end;
7779
7780 -- If we do not know required alignment, any non-zero offset is a
7781 -- potential problem (but certainly may be OK, so result is unknown).
7782
7783 elsif Offs /= No_Uint then
7784 Set_Result (Unknown);
7785
7786 -- If we can't find the result by direct comparison of alignment
7787 -- values, then there is still one case that we can determine known
7788 -- result, and that is when we can determine that the types are the
7789 -- same, and no alignments are specified. Then we known that the
7790 -- alignments are compatible, even if we don't know the alignment
7791 -- value in the front end.
7792
7793 elsif Etype (Obj) = Etype (Expr) then
7794
7795 -- Types are the same, but we have to check for possible size
7796 -- and alignments on the Expr object that may make the alignment
7797 -- different, even though the types are the same.
7798
7799 if Is_Entity_Name (Expr) then
7800
7801 -- First check alignment of the Expr object. Any alignment less
7802 -- than Maximum_Alignment is worrisome since this is the case
7803 -- where we do not know the alignment of Obj.
7804
7805 if Known_Alignment (Entity (Expr))
7806 and then UI_To_Int (Alignment (Entity (Expr))) <
7807 Ttypes.Maximum_Alignment
7808 then
7809 Set_Result (Unknown);
7810
7811 -- Now check size of Expr object. Any size that is not an
7812 -- even multiple of Maximum_Alignment is also worrisome
7813 -- since it may cause the alignment of the object to be less
7814 -- than the alignment of the type.
7815
7816 elsif Known_Static_Esize (Entity (Expr))
7817 and then
7818 (UI_To_Int (Esize (Entity (Expr))) mod
7819 (Ttypes.Maximum_Alignment * Ttypes.System_Storage_Unit))
7820 /= 0
7821 then
7822 Set_Result (Unknown);
7823
7824 -- Otherwise same type is decisive
7825
7826 else
7827 Set_Result (Known_Compatible);
7828 end if;
7829 end if;
7830
7831 -- Another case to deal with is when there is an explicit size or
7832 -- alignment clause when the types are not the same. If so, then the
7833 -- result is Unknown. We don't need to do this test if the Default is
7834 -- Unknown, since that result will be set in any case.
7835
7836 elsif Default /= Unknown
7837 and then (Has_Size_Clause (Etype (Expr))
7838 or else
7839 Has_Alignment_Clause (Etype (Expr)))
7840 then
7841 Set_Result (Unknown);
7842
7843 -- If no indication found, set default
7844
7845 else
7846 Set_Result (Default);
7847 end if;
7848
7849 -- Return worst result found
7850
7851 return Result;
7852 end Has_Compatible_Alignment_Internal;
7853
7854 -- Start of processing for Has_Compatible_Alignment
7855
7856 begin
7857 -- If Obj has no specified alignment, then set alignment from the type
7858 -- alignment. Perhaps we should always do this, but for sure we should
7859 -- do it when there is an address clause since we can do more if the
7860 -- alignment is known.
7861
7862 if Unknown_Alignment (Obj) then
7863 Set_Alignment (Obj, Alignment (Etype (Obj)));
7864 end if;
7865
7866 -- Now do the internal call that does all the work
7867
7868 return Has_Compatible_Alignment_Internal (Obj, Expr, Unknown);
7869 end Has_Compatible_Alignment;
7870
7871 ----------------------
7872 -- Has_Declarations --
7873 ----------------------
7874
7875 function Has_Declarations (N : Node_Id) return Boolean is
7876 begin
7877 return Nkind_In (Nkind (N), N_Accept_Statement,
7878 N_Block_Statement,
7879 N_Compilation_Unit_Aux,
7880 N_Entry_Body,
7881 N_Package_Body,
7882 N_Protected_Body,
7883 N_Subprogram_Body,
7884 N_Task_Body,
7885 N_Package_Specification);
7886 end Has_Declarations;
7887
7888 ---------------------------------
7889 -- Has_Defaulted_Discriminants --
7890 ---------------------------------
7891
7892 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean is
7893 begin
7894 return Has_Discriminants (Typ)
7895 and then Present (First_Discriminant (Typ))
7896 and then Present (Discriminant_Default_Value
7897 (First_Discriminant (Typ)));
7898 end Has_Defaulted_Discriminants;
7899
7900 -------------------
7901 -- Has_Denormals --
7902 -------------------
7903
7904 function Has_Denormals (E : Entity_Id) return Boolean is
7905 begin
7906 return Is_Floating_Point_Type (E) and then Denorm_On_Target;
7907 end Has_Denormals;
7908
7909 -------------------------------------------
7910 -- Has_Discriminant_Dependent_Constraint --
7911 -------------------------------------------
7912
7913 function Has_Discriminant_Dependent_Constraint
7914 (Comp : Entity_Id) return Boolean
7915 is
7916 Comp_Decl : constant Node_Id := Parent (Comp);
7917 Subt_Indic : Node_Id;
7918 Constr : Node_Id;
7919 Assn : Node_Id;
7920
7921 begin
7922 -- Discriminants can't depend on discriminants
7923
7924 if Ekind (Comp) = E_Discriminant then
7925 return False;
7926
7927 else
7928 Subt_Indic := Subtype_Indication (Component_Definition (Comp_Decl));
7929
7930 if Nkind (Subt_Indic) = N_Subtype_Indication then
7931 Constr := Constraint (Subt_Indic);
7932
7933 if Nkind (Constr) = N_Index_Or_Discriminant_Constraint then
7934 Assn := First (Constraints (Constr));
7935 while Present (Assn) loop
7936 case Nkind (Assn) is
7937 when N_Subtype_Indication |
7938 N_Range |
7939 N_Identifier
7940 =>
7941 if Depends_On_Discriminant (Assn) then
7942 return True;
7943 end if;
7944
7945 when N_Discriminant_Association =>
7946 if Depends_On_Discriminant (Expression (Assn)) then
7947 return True;
7948 end if;
7949
7950 when others =>
7951 null;
7952 end case;
7953
7954 Next (Assn);
7955 end loop;
7956 end if;
7957 end if;
7958 end if;
7959
7960 return False;
7961 end Has_Discriminant_Dependent_Constraint;
7962
7963 --------------------------
7964 -- Has_Enabled_Property --
7965 --------------------------
7966
7967 function Has_Enabled_Property
7968 (Item_Id : Entity_Id;
7969 Property : Name_Id) return Boolean
7970 is
7971 function State_Has_Enabled_Property return Boolean;
7972 -- Determine whether a state denoted by Item_Id has the property enabled
7973
7974 function Variable_Has_Enabled_Property return Boolean;
7975 -- Determine whether a variable denoted by Item_Id has the property
7976 -- enabled.
7977
7978 --------------------------------
7979 -- State_Has_Enabled_Property --
7980 --------------------------------
7981
7982 function State_Has_Enabled_Property return Boolean is
7983 Decl : constant Node_Id := Parent (Item_Id);
7984 Opt : Node_Id;
7985 Opt_Nam : Node_Id;
7986 Prop : Node_Id;
7987 Prop_Nam : Node_Id;
7988 Props : Node_Id;
7989
7990 begin
7991 -- The declaration of an external abstract state appears as an
7992 -- extension aggregate. If this is not the case, properties can never
7993 -- be set.
7994
7995 if Nkind (Decl) /= N_Extension_Aggregate then
7996 return False;
7997 end if;
7998
7999 -- When External appears as a simple option, it automatically enables
8000 -- all properties.
8001
8002 Opt := First (Expressions (Decl));
8003 while Present (Opt) loop
8004 if Nkind (Opt) = N_Identifier
8005 and then Chars (Opt) = Name_External
8006 then
8007 return True;
8008 end if;
8009
8010 Next (Opt);
8011 end loop;
8012
8013 -- When External specifies particular properties, inspect those and
8014 -- find the desired one (if any).
8015
8016 Opt := First (Component_Associations (Decl));
8017 while Present (Opt) loop
8018 Opt_Nam := First (Choices (Opt));
8019
8020 if Nkind (Opt_Nam) = N_Identifier
8021 and then Chars (Opt_Nam) = Name_External
8022 then
8023 Props := Expression (Opt);
8024
8025 -- Multiple properties appear as an aggregate
8026
8027 if Nkind (Props) = N_Aggregate then
8028
8029 -- Simple property form
8030
8031 Prop := First (Expressions (Props));
8032 while Present (Prop) loop
8033 if Chars (Prop) = Property then
8034 return True;
8035 end if;
8036
8037 Next (Prop);
8038 end loop;
8039
8040 -- Property with expression form
8041
8042 Prop := First (Component_Associations (Props));
8043 while Present (Prop) loop
8044 Prop_Nam := First (Choices (Prop));
8045
8046 -- The property can be represented in two ways:
8047 -- others => <value>
8048 -- <property> => <value>
8049
8050 if Nkind (Prop_Nam) = N_Others_Choice
8051 or else (Nkind (Prop_Nam) = N_Identifier
8052 and then Chars (Prop_Nam) = Property)
8053 then
8054 return Is_True (Expr_Value (Expression (Prop)));
8055 end if;
8056
8057 Next (Prop);
8058 end loop;
8059
8060 -- Single property
8061
8062 else
8063 return Chars (Props) = Property;
8064 end if;
8065 end if;
8066
8067 Next (Opt);
8068 end loop;
8069
8070 return False;
8071 end State_Has_Enabled_Property;
8072
8073 -----------------------------------
8074 -- Variable_Has_Enabled_Property --
8075 -----------------------------------
8076
8077 function Variable_Has_Enabled_Property return Boolean is
8078 function Is_Enabled (Prag : Node_Id) return Boolean;
8079 -- Determine whether property pragma Prag (if present) denotes an
8080 -- enabled property.
8081
8082 ----------------
8083 -- Is_Enabled --
8084 ----------------
8085
8086 function Is_Enabled (Prag : Node_Id) return Boolean is
8087 Arg2 : Node_Id;
8088
8089 begin
8090 if Present (Prag) then
8091 Arg2 := Next (First (Pragma_Argument_Associations (Prag)));
8092
8093 -- The pragma has an optional Boolean expression, the related
8094 -- property is enabled only when the expression evaluates to
8095 -- True.
8096
8097 if Present (Arg2) then
8098 return Is_True (Expr_Value (Get_Pragma_Arg (Arg2)));
8099
8100 -- Otherwise the lack of expression enables the property by
8101 -- default.
8102
8103 else
8104 return True;
8105 end if;
8106
8107 -- The property was never set in the first place
8108
8109 else
8110 return False;
8111 end if;
8112 end Is_Enabled;
8113
8114 -- Local variables
8115
8116 AR : constant Node_Id :=
8117 Get_Pragma (Item_Id, Pragma_Async_Readers);
8118 AW : constant Node_Id :=
8119 Get_Pragma (Item_Id, Pragma_Async_Writers);
8120 ER : constant Node_Id :=
8121 Get_Pragma (Item_Id, Pragma_Effective_Reads);
8122 EW : constant Node_Id :=
8123 Get_Pragma (Item_Id, Pragma_Effective_Writes);
8124
8125 -- Start of processing for Variable_Has_Enabled_Property
8126
8127 begin
8128 -- A non-effectively volatile object can never possess external
8129 -- properties.
8130
8131 if not Is_Effectively_Volatile (Item_Id) then
8132 return False;
8133
8134 -- External properties related to variables come in two flavors -
8135 -- explicit and implicit. The explicit case is characterized by the
8136 -- presence of a property pragma with an optional Boolean flag. The
8137 -- property is enabled when the flag evaluates to True or the flag is
8138 -- missing altogether.
8139
8140 elsif Property = Name_Async_Readers and then Is_Enabled (AR) then
8141 return True;
8142
8143 elsif Property = Name_Async_Writers and then Is_Enabled (AW) then
8144 return True;
8145
8146 elsif Property = Name_Effective_Reads and then Is_Enabled (ER) then
8147 return True;
8148
8149 elsif Property = Name_Effective_Writes and then Is_Enabled (EW) then
8150 return True;
8151
8152 -- The implicit case lacks all property pragmas
8153
8154 elsif No (AR) and then No (AW) and then No (ER) and then No (EW) then
8155 return True;
8156
8157 else
8158 return False;
8159 end if;
8160 end Variable_Has_Enabled_Property;
8161
8162 -- Start of processing for Has_Enabled_Property
8163
8164 begin
8165 -- Abstract states and variables have a flexible scheme of specifying
8166 -- external properties.
8167
8168 if Ekind (Item_Id) = E_Abstract_State then
8169 return State_Has_Enabled_Property;
8170
8171 elsif Ekind (Item_Id) = E_Variable then
8172 return Variable_Has_Enabled_Property;
8173
8174 -- Otherwise a property is enabled when the related item is effectively
8175 -- volatile.
8176
8177 else
8178 return Is_Effectively_Volatile (Item_Id);
8179 end if;
8180 end Has_Enabled_Property;
8181
8182 --------------------
8183 -- Has_Infinities --
8184 --------------------
8185
8186 function Has_Infinities (E : Entity_Id) return Boolean is
8187 begin
8188 return
8189 Is_Floating_Point_Type (E)
8190 and then Nkind (Scalar_Range (E)) = N_Range
8191 and then Includes_Infinities (Scalar_Range (E));
8192 end Has_Infinities;
8193
8194 --------------------
8195 -- Has_Interfaces --
8196 --------------------
8197
8198 function Has_Interfaces
8199 (T : Entity_Id;
8200 Use_Full_View : Boolean := True) return Boolean
8201 is
8202 Typ : Entity_Id := Base_Type (T);
8203
8204 begin
8205 -- Handle concurrent types
8206
8207 if Is_Concurrent_Type (Typ) then
8208 Typ := Corresponding_Record_Type (Typ);
8209 end if;
8210
8211 if not Present (Typ)
8212 or else not Is_Record_Type (Typ)
8213 or else not Is_Tagged_Type (Typ)
8214 then
8215 return False;
8216 end if;
8217
8218 -- Handle private types
8219
8220 if Use_Full_View and then Present (Full_View (Typ)) then
8221 Typ := Full_View (Typ);
8222 end if;
8223
8224 -- Handle concurrent record types
8225
8226 if Is_Concurrent_Record_Type (Typ)
8227 and then Is_Non_Empty_List (Abstract_Interface_List (Typ))
8228 then
8229 return True;
8230 end if;
8231
8232 loop
8233 if Is_Interface (Typ)
8234 or else
8235 (Is_Record_Type (Typ)
8236 and then Present (Interfaces (Typ))
8237 and then not Is_Empty_Elmt_List (Interfaces (Typ)))
8238 then
8239 return True;
8240 end if;
8241
8242 exit when Etype (Typ) = Typ
8243
8244 -- Handle private types
8245
8246 or else (Present (Full_View (Etype (Typ)))
8247 and then Full_View (Etype (Typ)) = Typ)
8248
8249 -- Protect frontend against wrong sources with cyclic derivations
8250
8251 or else Etype (Typ) = T;
8252
8253 -- Climb to the ancestor type handling private types
8254
8255 if Present (Full_View (Etype (Typ))) then
8256 Typ := Full_View (Etype (Typ));
8257 else
8258 Typ := Etype (Typ);
8259 end if;
8260 end loop;
8261
8262 return False;
8263 end Has_Interfaces;
8264
8265 ---------------------------------
8266 -- Has_No_Obvious_Side_Effects --
8267 ---------------------------------
8268
8269 function Has_No_Obvious_Side_Effects (N : Node_Id) return Boolean is
8270 begin
8271 -- For now, just handle literals, constants, and non-volatile
8272 -- variables and expressions combining these with operators or
8273 -- short circuit forms.
8274
8275 if Nkind (N) in N_Numeric_Or_String_Literal then
8276 return True;
8277
8278 elsif Nkind (N) = N_Character_Literal then
8279 return True;
8280
8281 elsif Nkind (N) in N_Unary_Op then
8282 return Has_No_Obvious_Side_Effects (Right_Opnd (N));
8283
8284 elsif Nkind (N) in N_Binary_Op or else Nkind (N) in N_Short_Circuit then
8285 return Has_No_Obvious_Side_Effects (Left_Opnd (N))
8286 and then
8287 Has_No_Obvious_Side_Effects (Right_Opnd (N));
8288
8289 elsif Nkind (N) = N_Expression_With_Actions
8290 and then Is_Empty_List (Actions (N))
8291 then
8292 return Has_No_Obvious_Side_Effects (Expression (N));
8293
8294 elsif Nkind (N) in N_Has_Entity then
8295 return Present (Entity (N))
8296 and then Ekind_In (Entity (N), E_Variable,
8297 E_Constant,
8298 E_Enumeration_Literal,
8299 E_In_Parameter,
8300 E_Out_Parameter,
8301 E_In_Out_Parameter)
8302 and then not Is_Volatile (Entity (N));
8303
8304 else
8305 return False;
8306 end if;
8307 end Has_No_Obvious_Side_Effects;
8308
8309 ------------------------
8310 -- Has_Null_Exclusion --
8311 ------------------------
8312
8313 function Has_Null_Exclusion (N : Node_Id) return Boolean is
8314 begin
8315 case Nkind (N) is
8316 when N_Access_Definition |
8317 N_Access_Function_Definition |
8318 N_Access_Procedure_Definition |
8319 N_Access_To_Object_Definition |
8320 N_Allocator |
8321 N_Derived_Type_Definition |
8322 N_Function_Specification |
8323 N_Subtype_Declaration =>
8324 return Null_Exclusion_Present (N);
8325
8326 when N_Component_Definition |
8327 N_Formal_Object_Declaration |
8328 N_Object_Renaming_Declaration =>
8329 if Present (Subtype_Mark (N)) then
8330 return Null_Exclusion_Present (N);
8331 else pragma Assert (Present (Access_Definition (N)));
8332 return Null_Exclusion_Present (Access_Definition (N));
8333 end if;
8334
8335 when N_Discriminant_Specification =>
8336 if Nkind (Discriminant_Type (N)) = N_Access_Definition then
8337 return Null_Exclusion_Present (Discriminant_Type (N));
8338 else
8339 return Null_Exclusion_Present (N);
8340 end if;
8341
8342 when N_Object_Declaration =>
8343 if Nkind (Object_Definition (N)) = N_Access_Definition then
8344 return Null_Exclusion_Present (Object_Definition (N));
8345 else
8346 return Null_Exclusion_Present (N);
8347 end if;
8348
8349 when N_Parameter_Specification =>
8350 if Nkind (Parameter_Type (N)) = N_Access_Definition then
8351 return Null_Exclusion_Present (Parameter_Type (N));
8352 else
8353 return Null_Exclusion_Present (N);
8354 end if;
8355
8356 when others =>
8357 return False;
8358
8359 end case;
8360 end Has_Null_Exclusion;
8361
8362 ------------------------
8363 -- Has_Null_Extension --
8364 ------------------------
8365
8366 function Has_Null_Extension (T : Entity_Id) return Boolean is
8367 B : constant Entity_Id := Base_Type (T);
8368 Comps : Node_Id;
8369 Ext : Node_Id;
8370
8371 begin
8372 if Nkind (Parent (B)) = N_Full_Type_Declaration
8373 and then Present (Record_Extension_Part (Type_Definition (Parent (B))))
8374 then
8375 Ext := Record_Extension_Part (Type_Definition (Parent (B)));
8376
8377 if Present (Ext) then
8378 if Null_Present (Ext) then
8379 return True;
8380 else
8381 Comps := Component_List (Ext);
8382
8383 -- The null component list is rewritten during analysis to
8384 -- include the parent component. Any other component indicates
8385 -- that the extension was not originally null.
8386
8387 return Null_Present (Comps)
8388 or else No (Next (First (Component_Items (Comps))));
8389 end if;
8390 else
8391 return False;
8392 end if;
8393
8394 else
8395 return False;
8396 end if;
8397 end Has_Null_Extension;
8398
8399 -------------------------------
8400 -- Has_Overriding_Initialize --
8401 -------------------------------
8402
8403 function Has_Overriding_Initialize (T : Entity_Id) return Boolean is
8404 BT : constant Entity_Id := Base_Type (T);
8405 P : Elmt_Id;
8406
8407 begin
8408 if Is_Controlled (BT) then
8409 if Is_RTU (Scope (BT), Ada_Finalization) then
8410 return False;
8411
8412 elsif Present (Primitive_Operations (BT)) then
8413 P := First_Elmt (Primitive_Operations (BT));
8414 while Present (P) loop
8415 declare
8416 Init : constant Entity_Id := Node (P);
8417 Formal : constant Entity_Id := First_Formal (Init);
8418 begin
8419 if Ekind (Init) = E_Procedure
8420 and then Chars (Init) = Name_Initialize
8421 and then Comes_From_Source (Init)
8422 and then Present (Formal)
8423 and then Etype (Formal) = BT
8424 and then No (Next_Formal (Formal))
8425 and then (Ada_Version < Ada_2012
8426 or else not Null_Present (Parent (Init)))
8427 then
8428 return True;
8429 end if;
8430 end;
8431
8432 Next_Elmt (P);
8433 end loop;
8434 end if;
8435
8436 -- Here if type itself does not have a non-null Initialize operation:
8437 -- check immediate ancestor.
8438
8439 if Is_Derived_Type (BT)
8440 and then Has_Overriding_Initialize (Etype (BT))
8441 then
8442 return True;
8443 end if;
8444 end if;
8445
8446 return False;
8447 end Has_Overriding_Initialize;
8448
8449 --------------------------------------
8450 -- Has_Preelaborable_Initialization --
8451 --------------------------------------
8452
8453 function Has_Preelaborable_Initialization (E : Entity_Id) return Boolean is
8454 Has_PE : Boolean;
8455
8456 procedure Check_Components (E : Entity_Id);
8457 -- Check component/discriminant chain, sets Has_PE False if a component
8458 -- or discriminant does not meet the preelaborable initialization rules.
8459
8460 ----------------------
8461 -- Check_Components --
8462 ----------------------
8463
8464 procedure Check_Components (E : Entity_Id) is
8465 Ent : Entity_Id;
8466 Exp : Node_Id;
8467
8468 function Is_Preelaborable_Expression (N : Node_Id) return Boolean;
8469 -- Returns True if and only if the expression denoted by N does not
8470 -- violate restrictions on preelaborable constructs (RM-10.2.1(5-9)).
8471
8472 ---------------------------------
8473 -- Is_Preelaborable_Expression --
8474 ---------------------------------
8475
8476 function Is_Preelaborable_Expression (N : Node_Id) return Boolean is
8477 Exp : Node_Id;
8478 Assn : Node_Id;
8479 Choice : Node_Id;
8480 Comp_Type : Entity_Id;
8481 Is_Array_Aggr : Boolean;
8482
8483 begin
8484 if Is_OK_Static_Expression (N) then
8485 return True;
8486
8487 elsif Nkind (N) = N_Null then
8488 return True;
8489
8490 -- Attributes are allowed in general, even if their prefix is a
8491 -- formal type. (It seems that certain attributes known not to be
8492 -- static might not be allowed, but there are no rules to prevent
8493 -- them.)
8494
8495 elsif Nkind (N) = N_Attribute_Reference then
8496 return True;
8497
8498 -- The name of a discriminant evaluated within its parent type is
8499 -- defined to be preelaborable (10.2.1(8)). Note that we test for
8500 -- names that denote discriminals as well as discriminants to
8501 -- catch references occurring within init procs.
8502
8503 elsif Is_Entity_Name (N)
8504 and then
8505 (Ekind (Entity (N)) = E_Discriminant
8506 or else (Ekind_In (Entity (N), E_Constant, E_In_Parameter)
8507 and then Present (Discriminal_Link (Entity (N)))))
8508 then
8509 return True;
8510
8511 elsif Nkind (N) = N_Qualified_Expression then
8512 return Is_Preelaborable_Expression (Expression (N));
8513
8514 -- For aggregates we have to check that each of the associations
8515 -- is preelaborable.
8516
8517 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
8518 Is_Array_Aggr := Is_Array_Type (Etype (N));
8519
8520 if Is_Array_Aggr then
8521 Comp_Type := Component_Type (Etype (N));
8522 end if;
8523
8524 -- Check the ancestor part of extension aggregates, which must
8525 -- be either the name of a type that has preelaborable init or
8526 -- an expression that is preelaborable.
8527
8528 if Nkind (N) = N_Extension_Aggregate then
8529 declare
8530 Anc_Part : constant Node_Id := Ancestor_Part (N);
8531
8532 begin
8533 if Is_Entity_Name (Anc_Part)
8534 and then Is_Type (Entity (Anc_Part))
8535 then
8536 if not Has_Preelaborable_Initialization
8537 (Entity (Anc_Part))
8538 then
8539 return False;
8540 end if;
8541
8542 elsif not Is_Preelaborable_Expression (Anc_Part) then
8543 return False;
8544 end if;
8545 end;
8546 end if;
8547
8548 -- Check positional associations
8549
8550 Exp := First (Expressions (N));
8551 while Present (Exp) loop
8552 if not Is_Preelaborable_Expression (Exp) then
8553 return False;
8554 end if;
8555
8556 Next (Exp);
8557 end loop;
8558
8559 -- Check named associations
8560
8561 Assn := First (Component_Associations (N));
8562 while Present (Assn) loop
8563 Choice := First (Choices (Assn));
8564 while Present (Choice) loop
8565 if Is_Array_Aggr then
8566 if Nkind (Choice) = N_Others_Choice then
8567 null;
8568
8569 elsif Nkind (Choice) = N_Range then
8570 if not Is_OK_Static_Range (Choice) then
8571 return False;
8572 end if;
8573
8574 elsif not Is_OK_Static_Expression (Choice) then
8575 return False;
8576 end if;
8577
8578 else
8579 Comp_Type := Etype (Choice);
8580 end if;
8581
8582 Next (Choice);
8583 end loop;
8584
8585 -- If the association has a <> at this point, then we have
8586 -- to check whether the component's type has preelaborable
8587 -- initialization. Note that this only occurs when the
8588 -- association's corresponding component does not have a
8589 -- default expression, the latter case having already been
8590 -- expanded as an expression for the association.
8591
8592 if Box_Present (Assn) then
8593 if not Has_Preelaborable_Initialization (Comp_Type) then
8594 return False;
8595 end if;
8596
8597 -- In the expression case we check whether the expression
8598 -- is preelaborable.
8599
8600 elsif
8601 not Is_Preelaborable_Expression (Expression (Assn))
8602 then
8603 return False;
8604 end if;
8605
8606 Next (Assn);
8607 end loop;
8608
8609 -- If we get here then aggregate as a whole is preelaborable
8610
8611 return True;
8612
8613 -- All other cases are not preelaborable
8614
8615 else
8616 return False;
8617 end if;
8618 end Is_Preelaborable_Expression;
8619
8620 -- Start of processing for Check_Components
8621
8622 begin
8623 -- Loop through entities of record or protected type
8624
8625 Ent := E;
8626 while Present (Ent) loop
8627
8628 -- We are interested only in components and discriminants
8629
8630 Exp := Empty;
8631
8632 case Ekind (Ent) is
8633 when E_Component =>
8634
8635 -- Get default expression if any. If there is no declaration
8636 -- node, it means we have an internal entity. The parent and
8637 -- tag fields are examples of such entities. For such cases,
8638 -- we just test the type of the entity.
8639
8640 if Present (Declaration_Node (Ent)) then
8641 Exp := Expression (Declaration_Node (Ent));
8642 end if;
8643
8644 when E_Discriminant =>
8645
8646 -- Note: for a renamed discriminant, the Declaration_Node
8647 -- may point to the one from the ancestor, and have a
8648 -- different expression, so use the proper attribute to
8649 -- retrieve the expression from the derived constraint.
8650
8651 Exp := Discriminant_Default_Value (Ent);
8652
8653 when others =>
8654 goto Check_Next_Entity;
8655 end case;
8656
8657 -- A component has PI if it has no default expression and the
8658 -- component type has PI.
8659
8660 if No (Exp) then
8661 if not Has_Preelaborable_Initialization (Etype (Ent)) then
8662 Has_PE := False;
8663 exit;
8664 end if;
8665
8666 -- Require the default expression to be preelaborable
8667
8668 elsif not Is_Preelaborable_Expression (Exp) then
8669 Has_PE := False;
8670 exit;
8671 end if;
8672
8673 <<Check_Next_Entity>>
8674 Next_Entity (Ent);
8675 end loop;
8676 end Check_Components;
8677
8678 -- Start of processing for Has_Preelaborable_Initialization
8679
8680 begin
8681 -- Immediate return if already marked as known preelaborable init. This
8682 -- covers types for which this function has already been called once
8683 -- and returned True (in which case the result is cached), and also
8684 -- types to which a pragma Preelaborable_Initialization applies.
8685
8686 if Known_To_Have_Preelab_Init (E) then
8687 return True;
8688 end if;
8689
8690 -- If the type is a subtype representing a generic actual type, then
8691 -- test whether its base type has preelaborable initialization since
8692 -- the subtype representing the actual does not inherit this attribute
8693 -- from the actual or formal. (but maybe it should???)
8694
8695 if Is_Generic_Actual_Type (E) then
8696 return Has_Preelaborable_Initialization (Base_Type (E));
8697 end if;
8698
8699 -- All elementary types have preelaborable initialization
8700
8701 if Is_Elementary_Type (E) then
8702 Has_PE := True;
8703
8704 -- Array types have PI if the component type has PI
8705
8706 elsif Is_Array_Type (E) then
8707 Has_PE := Has_Preelaborable_Initialization (Component_Type (E));
8708
8709 -- A derived type has preelaborable initialization if its parent type
8710 -- has preelaborable initialization and (in the case of a derived record
8711 -- extension) if the non-inherited components all have preelaborable
8712 -- initialization. However, a user-defined controlled type with an
8713 -- overriding Initialize procedure does not have preelaborable
8714 -- initialization.
8715
8716 elsif Is_Derived_Type (E) then
8717
8718 -- If the derived type is a private extension then it doesn't have
8719 -- preelaborable initialization.
8720
8721 if Ekind (Base_Type (E)) = E_Record_Type_With_Private then
8722 return False;
8723 end if;
8724
8725 -- First check whether ancestor type has preelaborable initialization
8726
8727 Has_PE := Has_Preelaborable_Initialization (Etype (Base_Type (E)));
8728
8729 -- If OK, check extension components (if any)
8730
8731 if Has_PE and then Is_Record_Type (E) then
8732 Check_Components (First_Entity (E));
8733 end if;
8734
8735 -- Check specifically for 10.2.1(11.4/2) exception: a controlled type
8736 -- with a user defined Initialize procedure does not have PI. If
8737 -- the type is untagged, the control primitives come from a component
8738 -- that has already been checked.
8739
8740 if Has_PE
8741 and then Is_Controlled (E)
8742 and then Is_Tagged_Type (E)
8743 and then Has_Overriding_Initialize (E)
8744 then
8745 Has_PE := False;
8746 end if;
8747
8748 -- Private types not derived from a type having preelaborable init and
8749 -- that are not marked with pragma Preelaborable_Initialization do not
8750 -- have preelaborable initialization.
8751
8752 elsif Is_Private_Type (E) then
8753 return False;
8754
8755 -- Record type has PI if it is non private and all components have PI
8756
8757 elsif Is_Record_Type (E) then
8758 Has_PE := True;
8759 Check_Components (First_Entity (E));
8760
8761 -- Protected types must not have entries, and components must meet
8762 -- same set of rules as for record components.
8763
8764 elsif Is_Protected_Type (E) then
8765 if Has_Entries (E) then
8766 Has_PE := False;
8767 else
8768 Has_PE := True;
8769 Check_Components (First_Entity (E));
8770 Check_Components (First_Private_Entity (E));
8771 end if;
8772
8773 -- Type System.Address always has preelaborable initialization
8774
8775 elsif Is_RTE (E, RE_Address) then
8776 Has_PE := True;
8777
8778 -- In all other cases, type does not have preelaborable initialization
8779
8780 else
8781 return False;
8782 end if;
8783
8784 -- If type has preelaborable initialization, cache result
8785
8786 if Has_PE then
8787 Set_Known_To_Have_Preelab_Init (E);
8788 end if;
8789
8790 return Has_PE;
8791 end Has_Preelaborable_Initialization;
8792
8793 ---------------------------
8794 -- Has_Private_Component --
8795 ---------------------------
8796
8797 function Has_Private_Component (Type_Id : Entity_Id) return Boolean is
8798 Btype : Entity_Id := Base_Type (Type_Id);
8799 Component : Entity_Id;
8800
8801 begin
8802 if Error_Posted (Type_Id)
8803 or else Error_Posted (Btype)
8804 then
8805 return False;
8806 end if;
8807
8808 if Is_Class_Wide_Type (Btype) then
8809 Btype := Root_Type (Btype);
8810 end if;
8811
8812 if Is_Private_Type (Btype) then
8813 declare
8814 UT : constant Entity_Id := Underlying_Type (Btype);
8815 begin
8816 if No (UT) then
8817 if No (Full_View (Btype)) then
8818 return not Is_Generic_Type (Btype)
8819 and then
8820 not Is_Generic_Type (Root_Type (Btype));
8821 else
8822 return not Is_Generic_Type (Root_Type (Full_View (Btype)));
8823 end if;
8824 else
8825 return not Is_Frozen (UT) and then Has_Private_Component (UT);
8826 end if;
8827 end;
8828
8829 elsif Is_Array_Type (Btype) then
8830 return Has_Private_Component (Component_Type (Btype));
8831
8832 elsif Is_Record_Type (Btype) then
8833 Component := First_Component (Btype);
8834 while Present (Component) loop
8835 if Has_Private_Component (Etype (Component)) then
8836 return True;
8837 end if;
8838
8839 Next_Component (Component);
8840 end loop;
8841
8842 return False;
8843
8844 elsif Is_Protected_Type (Btype)
8845 and then Present (Corresponding_Record_Type (Btype))
8846 then
8847 return Has_Private_Component (Corresponding_Record_Type (Btype));
8848
8849 else
8850 return False;
8851 end if;
8852 end Has_Private_Component;
8853
8854 ----------------------
8855 -- Has_Signed_Zeros --
8856 ----------------------
8857
8858 function Has_Signed_Zeros (E : Entity_Id) return Boolean is
8859 begin
8860 return Is_Floating_Point_Type (E) and then Signed_Zeros_On_Target;
8861 end Has_Signed_Zeros;
8862
8863 -----------------------------
8864 -- Has_Static_Array_Bounds --
8865 -----------------------------
8866
8867 function Has_Static_Array_Bounds (Typ : Node_Id) return Boolean is
8868 Ndims : constant Nat := Number_Dimensions (Typ);
8869
8870 Index : Node_Id;
8871 Low : Node_Id;
8872 High : Node_Id;
8873
8874 begin
8875 -- Unconstrained types do not have static bounds
8876
8877 if not Is_Constrained (Typ) then
8878 return False;
8879 end if;
8880
8881 -- First treat string literals specially, as the lower bound and length
8882 -- of string literals are not stored like those of arrays.
8883
8884 -- A string literal always has static bounds
8885
8886 if Ekind (Typ) = E_String_Literal_Subtype then
8887 return True;
8888 end if;
8889
8890 -- Treat all dimensions in turn
8891
8892 Index := First_Index (Typ);
8893 for Indx in 1 .. Ndims loop
8894
8895 -- In case of an illegal index which is not a discrete type, return
8896 -- that the type is not static.
8897
8898 if not Is_Discrete_Type (Etype (Index))
8899 or else Etype (Index) = Any_Type
8900 then
8901 return False;
8902 end if;
8903
8904 Get_Index_Bounds (Index, Low, High);
8905
8906 if Error_Posted (Low) or else Error_Posted (High) then
8907 return False;
8908 end if;
8909
8910 if Is_OK_Static_Expression (Low)
8911 and then
8912 Is_OK_Static_Expression (High)
8913 then
8914 null;
8915 else
8916 return False;
8917 end if;
8918
8919 Next (Index);
8920 end loop;
8921
8922 -- If we fall through the loop, all indexes matched
8923
8924 return True;
8925 end Has_Static_Array_Bounds;
8926
8927 ----------------
8928 -- Has_Stream --
8929 ----------------
8930
8931 function Has_Stream (T : Entity_Id) return Boolean is
8932 E : Entity_Id;
8933
8934 begin
8935 if No (T) then
8936 return False;
8937
8938 elsif Is_RTE (Root_Type (T), RE_Root_Stream_Type) then
8939 return True;
8940
8941 elsif Is_Array_Type (T) then
8942 return Has_Stream (Component_Type (T));
8943
8944 elsif Is_Record_Type (T) then
8945 E := First_Component (T);
8946 while Present (E) loop
8947 if Has_Stream (Etype (E)) then
8948 return True;
8949 else
8950 Next_Component (E);
8951 end if;
8952 end loop;
8953
8954 return False;
8955
8956 elsif Is_Private_Type (T) then
8957 return Has_Stream (Underlying_Type (T));
8958
8959 else
8960 return False;
8961 end if;
8962 end Has_Stream;
8963
8964 ----------------
8965 -- Has_Suffix --
8966 ----------------
8967
8968 function Has_Suffix (E : Entity_Id; Suffix : Character) return Boolean is
8969 begin
8970 Get_Name_String (Chars (E));
8971 return Name_Buffer (Name_Len) = Suffix;
8972 end Has_Suffix;
8973
8974 ----------------
8975 -- Add_Suffix --
8976 ----------------
8977
8978 function Add_Suffix (E : Entity_Id; Suffix : Character) return Name_Id is
8979 begin
8980 Get_Name_String (Chars (E));
8981 Add_Char_To_Name_Buffer (Suffix);
8982 return Name_Find;
8983 end Add_Suffix;
8984
8985 -------------------
8986 -- Remove_Suffix --
8987 -------------------
8988
8989 function Remove_Suffix (E : Entity_Id; Suffix : Character) return Name_Id is
8990 begin
8991 pragma Assert (Has_Suffix (E, Suffix));
8992 Get_Name_String (Chars (E));
8993 Name_Len := Name_Len - 1;
8994 return Name_Find;
8995 end Remove_Suffix;
8996
8997 --------------------------
8998 -- Has_Tagged_Component --
8999 --------------------------
9000
9001 function Has_Tagged_Component (Typ : Entity_Id) return Boolean is
9002 Comp : Entity_Id;
9003
9004 begin
9005 if Is_Private_Type (Typ) and then Present (Underlying_Type (Typ)) then
9006 return Has_Tagged_Component (Underlying_Type (Typ));
9007
9008 elsif Is_Array_Type (Typ) then
9009 return Has_Tagged_Component (Component_Type (Typ));
9010
9011 elsif Is_Tagged_Type (Typ) then
9012 return True;
9013
9014 elsif Is_Record_Type (Typ) then
9015 Comp := First_Component (Typ);
9016 while Present (Comp) loop
9017 if Has_Tagged_Component (Etype (Comp)) then
9018 return True;
9019 end if;
9020
9021 Next_Component (Comp);
9022 end loop;
9023
9024 return False;
9025
9026 else
9027 return False;
9028 end if;
9029 end Has_Tagged_Component;
9030
9031 ----------------------------
9032 -- Has_Volatile_Component --
9033 ----------------------------
9034
9035 function Has_Volatile_Component (Typ : Entity_Id) return Boolean is
9036 Comp : Entity_Id;
9037
9038 begin
9039 if Has_Volatile_Components (Typ) then
9040 return True;
9041
9042 elsif Is_Array_Type (Typ) then
9043 return Is_Volatile (Component_Type (Typ));
9044
9045 elsif Is_Record_Type (Typ) then
9046 Comp := First_Component (Typ);
9047 while Present (Comp) loop
9048 if Is_Volatile_Object (Comp) then
9049 return True;
9050 end if;
9051
9052 Comp := Next_Component (Comp);
9053 end loop;
9054 end if;
9055
9056 return False;
9057 end Has_Volatile_Component;
9058
9059 -------------------------
9060 -- Implementation_Kind --
9061 -------------------------
9062
9063 function Implementation_Kind (Subp : Entity_Id) return Name_Id is
9064 Impl_Prag : constant Node_Id := Get_Rep_Pragma (Subp, Name_Implemented);
9065 Arg : Node_Id;
9066 begin
9067 pragma Assert (Present (Impl_Prag));
9068 Arg := Last (Pragma_Argument_Associations (Impl_Prag));
9069 return Chars (Get_Pragma_Arg (Arg));
9070 end Implementation_Kind;
9071
9072 --------------------------
9073 -- Implements_Interface --
9074 --------------------------
9075
9076 function Implements_Interface
9077 (Typ_Ent : Entity_Id;
9078 Iface_Ent : Entity_Id;
9079 Exclude_Parents : Boolean := False) return Boolean
9080 is
9081 Ifaces_List : Elist_Id;
9082 Elmt : Elmt_Id;
9083 Iface : Entity_Id := Base_Type (Iface_Ent);
9084 Typ : Entity_Id := Base_Type (Typ_Ent);
9085
9086 begin
9087 if Is_Class_Wide_Type (Typ) then
9088 Typ := Root_Type (Typ);
9089 end if;
9090
9091 if not Has_Interfaces (Typ) then
9092 return False;
9093 end if;
9094
9095 if Is_Class_Wide_Type (Iface) then
9096 Iface := Root_Type (Iface);
9097 end if;
9098
9099 Collect_Interfaces (Typ, Ifaces_List);
9100
9101 Elmt := First_Elmt (Ifaces_List);
9102 while Present (Elmt) loop
9103 if Is_Ancestor (Node (Elmt), Typ, Use_Full_View => True)
9104 and then Exclude_Parents
9105 then
9106 null;
9107
9108 elsif Node (Elmt) = Iface then
9109 return True;
9110 end if;
9111
9112 Next_Elmt (Elmt);
9113 end loop;
9114
9115 return False;
9116 end Implements_Interface;
9117
9118 ------------------------------------
9119 -- In_Assertion_Expression_Pragma --
9120 ------------------------------------
9121
9122 function In_Assertion_Expression_Pragma (N : Node_Id) return Boolean is
9123 Par : Node_Id;
9124 Prag : Node_Id := Empty;
9125
9126 begin
9127 -- Climb the parent chain looking for an enclosing pragma
9128
9129 Par := N;
9130 while Present (Par) loop
9131 if Nkind (Par) = N_Pragma then
9132 Prag := Par;
9133 exit;
9134
9135 -- Precondition-like pragmas are expanded into if statements, check
9136 -- the original node instead.
9137
9138 elsif Nkind (Original_Node (Par)) = N_Pragma then
9139 Prag := Original_Node (Par);
9140 exit;
9141
9142 -- The expansion of attribute 'Old generates a constant to capture
9143 -- the result of the prefix. If the parent traversal reaches
9144 -- one of these constants, then the node technically came from a
9145 -- postcondition-like pragma. Note that the Ekind is not tested here
9146 -- because N may be the expression of an object declaration which is
9147 -- currently being analyzed. Such objects carry Ekind of E_Void.
9148
9149 elsif Nkind (Par) = N_Object_Declaration
9150 and then Constant_Present (Par)
9151 and then Stores_Attribute_Old_Prefix (Defining_Entity (Par))
9152 then
9153 return True;
9154
9155 -- Prevent the search from going too far
9156
9157 elsif Is_Body_Or_Package_Declaration (Par) then
9158 return False;
9159 end if;
9160
9161 Par := Parent (Par);
9162 end loop;
9163
9164 return
9165 Present (Prag)
9166 and then Assertion_Expression_Pragma (Get_Pragma_Id (Prag));
9167 end In_Assertion_Expression_Pragma;
9168
9169 -----------------
9170 -- In_Instance --
9171 -----------------
9172
9173 function In_Instance return Boolean is
9174 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
9175 S : Entity_Id;
9176
9177 begin
9178 S := Current_Scope;
9179 while Present (S) and then S /= Standard_Standard loop
9180 if Ekind_In (S, E_Function, E_Package, E_Procedure)
9181 and then Is_Generic_Instance (S)
9182 then
9183 -- A child instance is always compiled in the context of a parent
9184 -- instance. Nevertheless, the actuals are not analyzed in an
9185 -- instance context. We detect this case by examining the current
9186 -- compilation unit, which must be a child instance, and checking
9187 -- that it is not currently on the scope stack.
9188
9189 if Is_Child_Unit (Curr_Unit)
9190 and then Nkind (Unit (Cunit (Current_Sem_Unit))) =
9191 N_Package_Instantiation
9192 and then not In_Open_Scopes (Curr_Unit)
9193 then
9194 return False;
9195 else
9196 return True;
9197 end if;
9198 end if;
9199
9200 S := Scope (S);
9201 end loop;
9202
9203 return False;
9204 end In_Instance;
9205
9206 ----------------------
9207 -- In_Instance_Body --
9208 ----------------------
9209
9210 function In_Instance_Body return Boolean is
9211 S : Entity_Id;
9212
9213 begin
9214 S := Current_Scope;
9215 while Present (S) and then S /= Standard_Standard loop
9216 if Ekind_In (S, E_Function, E_Procedure)
9217 and then Is_Generic_Instance (S)
9218 then
9219 return True;
9220
9221 elsif Ekind (S) = E_Package
9222 and then In_Package_Body (S)
9223 and then Is_Generic_Instance (S)
9224 then
9225 return True;
9226 end if;
9227
9228 S := Scope (S);
9229 end loop;
9230
9231 return False;
9232 end In_Instance_Body;
9233
9234 -----------------------------
9235 -- In_Instance_Not_Visible --
9236 -----------------------------
9237
9238 function In_Instance_Not_Visible return Boolean is
9239 S : Entity_Id;
9240
9241 begin
9242 S := Current_Scope;
9243 while Present (S) and then S /= Standard_Standard loop
9244 if Ekind_In (S, E_Function, E_Procedure)
9245 and then Is_Generic_Instance (S)
9246 then
9247 return True;
9248
9249 elsif Ekind (S) = E_Package
9250 and then (In_Package_Body (S) or else In_Private_Part (S))
9251 and then Is_Generic_Instance (S)
9252 then
9253 return True;
9254 end if;
9255
9256 S := Scope (S);
9257 end loop;
9258
9259 return False;
9260 end In_Instance_Not_Visible;
9261
9262 ------------------------------
9263 -- In_Instance_Visible_Part --
9264 ------------------------------
9265
9266 function In_Instance_Visible_Part return Boolean is
9267 S : Entity_Id;
9268
9269 begin
9270 S := Current_Scope;
9271 while Present (S) and then S /= Standard_Standard loop
9272 if Ekind (S) = E_Package
9273 and then Is_Generic_Instance (S)
9274 and then not In_Package_Body (S)
9275 and then not In_Private_Part (S)
9276 then
9277 return True;
9278 end if;
9279
9280 S := Scope (S);
9281 end loop;
9282
9283 return False;
9284 end In_Instance_Visible_Part;
9285
9286 ---------------------
9287 -- In_Package_Body --
9288 ---------------------
9289
9290 function In_Package_Body return Boolean is
9291 S : Entity_Id;
9292
9293 begin
9294 S := Current_Scope;
9295 while Present (S) and then S /= Standard_Standard loop
9296 if Ekind (S) = E_Package and then In_Package_Body (S) then
9297 return True;
9298 else
9299 S := Scope (S);
9300 end if;
9301 end loop;
9302
9303 return False;
9304 end In_Package_Body;
9305
9306 --------------------------------
9307 -- In_Parameter_Specification --
9308 --------------------------------
9309
9310 function In_Parameter_Specification (N : Node_Id) return Boolean is
9311 PN : Node_Id;
9312
9313 begin
9314 PN := Parent (N);
9315 while Present (PN) loop
9316 if Nkind (PN) = N_Parameter_Specification then
9317 return True;
9318 end if;
9319
9320 PN := Parent (PN);
9321 end loop;
9322
9323 return False;
9324 end In_Parameter_Specification;
9325
9326 --------------------------
9327 -- In_Pragma_Expression --
9328 --------------------------
9329
9330 function In_Pragma_Expression (N : Node_Id; Nam : Name_Id) return Boolean is
9331 P : Node_Id;
9332 begin
9333 P := Parent (N);
9334 loop
9335 if No (P) then
9336 return False;
9337 elsif Nkind (P) = N_Pragma and then Pragma_Name (P) = Nam then
9338 return True;
9339 else
9340 P := Parent (P);
9341 end if;
9342 end loop;
9343 end In_Pragma_Expression;
9344
9345 -------------------------------------
9346 -- In_Reverse_Storage_Order_Object --
9347 -------------------------------------
9348
9349 function In_Reverse_Storage_Order_Object (N : Node_Id) return Boolean is
9350 Pref : Node_Id;
9351 Btyp : Entity_Id := Empty;
9352
9353 begin
9354 -- Climb up indexed components
9355
9356 Pref := N;
9357 loop
9358 case Nkind (Pref) is
9359 when N_Selected_Component =>
9360 Pref := Prefix (Pref);
9361 exit;
9362
9363 when N_Indexed_Component =>
9364 Pref := Prefix (Pref);
9365
9366 when others =>
9367 Pref := Empty;
9368 exit;
9369 end case;
9370 end loop;
9371
9372 if Present (Pref) then
9373 Btyp := Base_Type (Etype (Pref));
9374 end if;
9375
9376 return Present (Btyp)
9377 and then (Is_Record_Type (Btyp) or else Is_Array_Type (Btyp))
9378 and then Reverse_Storage_Order (Btyp);
9379 end In_Reverse_Storage_Order_Object;
9380
9381 --------------------------------------
9382 -- In_Subprogram_Or_Concurrent_Unit --
9383 --------------------------------------
9384
9385 function In_Subprogram_Or_Concurrent_Unit return Boolean is
9386 E : Entity_Id;
9387 K : Entity_Kind;
9388
9389 begin
9390 -- Use scope chain to check successively outer scopes
9391
9392 E := Current_Scope;
9393 loop
9394 K := Ekind (E);
9395
9396 if K in Subprogram_Kind
9397 or else K in Concurrent_Kind
9398 or else K in Generic_Subprogram_Kind
9399 then
9400 return True;
9401
9402 elsif E = Standard_Standard then
9403 return False;
9404 end if;
9405
9406 E := Scope (E);
9407 end loop;
9408 end In_Subprogram_Or_Concurrent_Unit;
9409
9410 ---------------------
9411 -- In_Visible_Part --
9412 ---------------------
9413
9414 function In_Visible_Part (Scope_Id : Entity_Id) return Boolean is
9415 begin
9416 return Is_Package_Or_Generic_Package (Scope_Id)
9417 and then In_Open_Scopes (Scope_Id)
9418 and then not In_Package_Body (Scope_Id)
9419 and then not In_Private_Part (Scope_Id);
9420 end In_Visible_Part;
9421
9422 --------------------------------
9423 -- Incomplete_Or_Partial_View --
9424 --------------------------------
9425
9426 function Incomplete_Or_Partial_View (Id : Entity_Id) return Entity_Id is
9427 function Inspect_Decls
9428 (Decls : List_Id;
9429 Taft : Boolean := False) return Entity_Id;
9430 -- Check whether a declarative region contains the incomplete or partial
9431 -- view of Id.
9432
9433 -------------------
9434 -- Inspect_Decls --
9435 -------------------
9436
9437 function Inspect_Decls
9438 (Decls : List_Id;
9439 Taft : Boolean := False) return Entity_Id
9440 is
9441 Decl : Node_Id;
9442 Match : Node_Id;
9443
9444 begin
9445 Decl := First (Decls);
9446 while Present (Decl) loop
9447 Match := Empty;
9448
9449 if Taft then
9450 if Nkind (Decl) = N_Incomplete_Type_Declaration then
9451 Match := Defining_Identifier (Decl);
9452 end if;
9453
9454 else
9455 if Nkind_In (Decl, N_Private_Extension_Declaration,
9456 N_Private_Type_Declaration)
9457 then
9458 Match := Defining_Identifier (Decl);
9459 end if;
9460 end if;
9461
9462 if Present (Match)
9463 and then Present (Full_View (Match))
9464 and then Full_View (Match) = Id
9465 then
9466 return Match;
9467 end if;
9468
9469 Next (Decl);
9470 end loop;
9471
9472 return Empty;
9473 end Inspect_Decls;
9474
9475 -- Local variables
9476
9477 Prev : Entity_Id;
9478
9479 -- Start of processing for Incomplete_Or_Partial_View
9480
9481 begin
9482 -- Deferred constant or incomplete type case
9483
9484 Prev := Current_Entity_In_Scope (Id);
9485
9486 if Present (Prev)
9487 and then (Is_Incomplete_Type (Prev) or else Ekind (Prev) = E_Constant)
9488 and then Present (Full_View (Prev))
9489 and then Full_View (Prev) = Id
9490 then
9491 return Prev;
9492 end if;
9493
9494 -- Private or Taft amendment type case
9495
9496 declare
9497 Pkg : constant Entity_Id := Scope (Id);
9498 Pkg_Decl : Node_Id := Pkg;
9499
9500 begin
9501 if Ekind (Pkg) = E_Package then
9502 while Nkind (Pkg_Decl) /= N_Package_Specification loop
9503 Pkg_Decl := Parent (Pkg_Decl);
9504 end loop;
9505
9506 -- It is knows that Typ has a private view, look for it in the
9507 -- visible declarations of the enclosing scope. A special case
9508 -- of this is when the two views have been exchanged - the full
9509 -- appears earlier than the private.
9510
9511 if Has_Private_Declaration (Id) then
9512 Prev := Inspect_Decls (Visible_Declarations (Pkg_Decl));
9513
9514 -- Exchanged view case, look in the private declarations
9515
9516 if No (Prev) then
9517 Prev := Inspect_Decls (Private_Declarations (Pkg_Decl));
9518 end if;
9519
9520 return Prev;
9521
9522 -- Otherwise if this is the package body, then Typ is a potential
9523 -- Taft amendment type. The incomplete view should be located in
9524 -- the private declarations of the enclosing scope.
9525
9526 elsif In_Package_Body (Pkg) then
9527 return Inspect_Decls (Private_Declarations (Pkg_Decl), True);
9528 end if;
9529 end if;
9530 end;
9531
9532 -- The type has no incomplete or private view
9533
9534 return Empty;
9535 end Incomplete_Or_Partial_View;
9536
9537 -----------------------------------------
9538 -- Inherit_Default_Init_Cond_Procedure --
9539 -----------------------------------------
9540
9541 procedure Inherit_Default_Init_Cond_Procedure (Typ : Entity_Id) is
9542 Par_Typ : constant Entity_Id := Etype (Typ);
9543
9544 begin
9545 -- A derived type inherits the default initial condition procedure of
9546 -- its parent type.
9547
9548 if No (Default_Init_Cond_Procedure (Typ)) then
9549 Set_Default_Init_Cond_Procedure
9550 (Typ, Default_Init_Cond_Procedure (Par_Typ));
9551 end if;
9552 end Inherit_Default_Init_Cond_Procedure;
9553
9554 ----------------------------
9555 -- Inherit_Rep_Item_Chain --
9556 ----------------------------
9557
9558 procedure Inherit_Rep_Item_Chain (Typ : Entity_Id; From_Typ : Entity_Id) is
9559 From_Item : constant Node_Id := First_Rep_Item (From_Typ);
9560 Item : Node_Id := Empty;
9561 Last_Item : Node_Id := Empty;
9562
9563 begin
9564 -- Reach the end of the destination type's chain (if any) and capture
9565 -- the last item.
9566
9567 Item := First_Rep_Item (Typ);
9568 while Present (Item) loop
9569
9570 -- Do not inherit a chain that has been inherited already
9571
9572 if Item = From_Item then
9573 return;
9574 end if;
9575
9576 Last_Item := Item;
9577 Item := Next_Rep_Item (Item);
9578 end loop;
9579
9580 -- When the destination type has a rep item chain, the chain of the
9581 -- source type is appended to it.
9582
9583 if Present (Last_Item) then
9584 Set_Next_Rep_Item (Last_Item, From_Item);
9585
9586 -- Otherwise the destination type directly inherits the rep item chain
9587 -- of the source type (if any).
9588
9589 else
9590 Set_First_Rep_Item (Typ, From_Item);
9591 end if;
9592 end Inherit_Rep_Item_Chain;
9593
9594 ---------------------------------
9595 -- Inherit_Subprogram_Contract --
9596 ---------------------------------
9597
9598 procedure Inherit_Subprogram_Contract
9599 (Subp : Entity_Id;
9600 From_Subp : Entity_Id)
9601 is
9602 procedure Inherit_Pragma (Prag_Id : Pragma_Id);
9603 -- Propagate a pragma denoted by Prag_Id from From_Subp's contract to
9604 -- Subp's contract.
9605
9606 --------------------
9607 -- Inherit_Pragma --
9608 --------------------
9609
9610 procedure Inherit_Pragma (Prag_Id : Pragma_Id) is
9611 Prag : constant Node_Id := Get_Pragma (From_Subp, Prag_Id);
9612 New_Prag : Node_Id;
9613
9614 begin
9615 -- A pragma cannot be part of more than one First_Pragma/Next_Pragma
9616 -- chains, therefore the node must be replicated. The new pragma is
9617 -- flagged is inherited for distrinction purposes.
9618
9619 if Present (Prag) then
9620 New_Prag := New_Copy_Tree (Prag);
9621 Set_Is_Inherited (New_Prag);
9622
9623 Add_Contract_Item (New_Prag, Subp);
9624 end if;
9625 end Inherit_Pragma;
9626
9627 -- Start of processing for Inherit_Subprogram_Contract
9628
9629 begin
9630 -- Inheritance is carried out only when both entities are subprograms
9631 -- with contracts.
9632
9633 if Is_Subprogram_Or_Generic_Subprogram (Subp)
9634 and then Is_Subprogram_Or_Generic_Subprogram (From_Subp)
9635 and then Present (Contract (Subp))
9636 and then Present (Contract (From_Subp))
9637 then
9638 Inherit_Pragma (Pragma_Extensions_Visible);
9639 end if;
9640 end Inherit_Subprogram_Contract;
9641
9642 ---------------------------------
9643 -- Insert_Explicit_Dereference --
9644 ---------------------------------
9645
9646 procedure Insert_Explicit_Dereference (N : Node_Id) is
9647 New_Prefix : constant Node_Id := Relocate_Node (N);
9648 Ent : Entity_Id := Empty;
9649 Pref : Node_Id;
9650 I : Interp_Index;
9651 It : Interp;
9652 T : Entity_Id;
9653
9654 begin
9655 Save_Interps (N, New_Prefix);
9656
9657 Rewrite (N,
9658 Make_Explicit_Dereference (Sloc (Parent (N)),
9659 Prefix => New_Prefix));
9660
9661 Set_Etype (N, Designated_Type (Etype (New_Prefix)));
9662
9663 if Is_Overloaded (New_Prefix) then
9664
9665 -- The dereference is also overloaded, and its interpretations are
9666 -- the designated types of the interpretations of the original node.
9667
9668 Set_Etype (N, Any_Type);
9669
9670 Get_First_Interp (New_Prefix, I, It);
9671 while Present (It.Nam) loop
9672 T := It.Typ;
9673
9674 if Is_Access_Type (T) then
9675 Add_One_Interp (N, Designated_Type (T), Designated_Type (T));
9676 end if;
9677
9678 Get_Next_Interp (I, It);
9679 end loop;
9680
9681 End_Interp_List;
9682
9683 else
9684 -- Prefix is unambiguous: mark the original prefix (which might
9685 -- Come_From_Source) as a reference, since the new (relocated) one
9686 -- won't be taken into account.
9687
9688 if Is_Entity_Name (New_Prefix) then
9689 Ent := Entity (New_Prefix);
9690 Pref := New_Prefix;
9691
9692 -- For a retrieval of a subcomponent of some composite object,
9693 -- retrieve the ultimate entity if there is one.
9694
9695 elsif Nkind_In (New_Prefix, N_Selected_Component,
9696 N_Indexed_Component)
9697 then
9698 Pref := Prefix (New_Prefix);
9699 while Present (Pref)
9700 and then Nkind_In (Pref, N_Selected_Component,
9701 N_Indexed_Component)
9702 loop
9703 Pref := Prefix (Pref);
9704 end loop;
9705
9706 if Present (Pref) and then Is_Entity_Name (Pref) then
9707 Ent := Entity (Pref);
9708 end if;
9709 end if;
9710
9711 -- Place the reference on the entity node
9712
9713 if Present (Ent) then
9714 Generate_Reference (Ent, Pref);
9715 end if;
9716 end if;
9717 end Insert_Explicit_Dereference;
9718
9719 ------------------------------------------
9720 -- Inspect_Deferred_Constant_Completion --
9721 ------------------------------------------
9722
9723 procedure Inspect_Deferred_Constant_Completion (Decls : List_Id) is
9724 Decl : Node_Id;
9725
9726 begin
9727 Decl := First (Decls);
9728 while Present (Decl) loop
9729
9730 -- Deferred constant signature
9731
9732 if Nkind (Decl) = N_Object_Declaration
9733 and then Constant_Present (Decl)
9734 and then No (Expression (Decl))
9735
9736 -- No need to check internally generated constants
9737
9738 and then Comes_From_Source (Decl)
9739
9740 -- The constant is not completed. A full object declaration or a
9741 -- pragma Import complete a deferred constant.
9742
9743 and then not Has_Completion (Defining_Identifier (Decl))
9744 then
9745 Error_Msg_N
9746 ("constant declaration requires initialization expression",
9747 Defining_Identifier (Decl));
9748 end if;
9749
9750 Decl := Next (Decl);
9751 end loop;
9752 end Inspect_Deferred_Constant_Completion;
9753
9754 -----------------------------
9755 -- Is_Actual_Out_Parameter --
9756 -----------------------------
9757
9758 function Is_Actual_Out_Parameter (N : Node_Id) return Boolean is
9759 Formal : Entity_Id;
9760 Call : Node_Id;
9761 begin
9762 Find_Actual (N, Formal, Call);
9763 return Present (Formal) and then Ekind (Formal) = E_Out_Parameter;
9764 end Is_Actual_Out_Parameter;
9765
9766 -------------------------
9767 -- Is_Actual_Parameter --
9768 -------------------------
9769
9770 function Is_Actual_Parameter (N : Node_Id) return Boolean is
9771 PK : constant Node_Kind := Nkind (Parent (N));
9772
9773 begin
9774 case PK is
9775 when N_Parameter_Association =>
9776 return N = Explicit_Actual_Parameter (Parent (N));
9777
9778 when N_Subprogram_Call =>
9779 return Is_List_Member (N)
9780 and then
9781 List_Containing (N) = Parameter_Associations (Parent (N));
9782
9783 when others =>
9784 return False;
9785 end case;
9786 end Is_Actual_Parameter;
9787
9788 --------------------------------
9789 -- Is_Actual_Tagged_Parameter --
9790 --------------------------------
9791
9792 function Is_Actual_Tagged_Parameter (N : Node_Id) return Boolean is
9793 Formal : Entity_Id;
9794 Call : Node_Id;
9795 begin
9796 Find_Actual (N, Formal, Call);
9797 return Present (Formal) and then Is_Tagged_Type (Etype (Formal));
9798 end Is_Actual_Tagged_Parameter;
9799
9800 ---------------------
9801 -- Is_Aliased_View --
9802 ---------------------
9803
9804 function Is_Aliased_View (Obj : Node_Id) return Boolean is
9805 E : Entity_Id;
9806
9807 begin
9808 if Is_Entity_Name (Obj) then
9809 E := Entity (Obj);
9810
9811 return
9812 (Is_Object (E)
9813 and then
9814 (Is_Aliased (E)
9815 or else (Present (Renamed_Object (E))
9816 and then Is_Aliased_View (Renamed_Object (E)))))
9817
9818 or else ((Is_Formal (E)
9819 or else Ekind_In (E, E_Generic_In_Out_Parameter,
9820 E_Generic_In_Parameter))
9821 and then Is_Tagged_Type (Etype (E)))
9822
9823 or else (Is_Concurrent_Type (E) and then In_Open_Scopes (E))
9824
9825 -- Current instance of type, either directly or as rewritten
9826 -- reference to the current object.
9827
9828 or else (Is_Entity_Name (Original_Node (Obj))
9829 and then Present (Entity (Original_Node (Obj)))
9830 and then Is_Type (Entity (Original_Node (Obj))))
9831
9832 or else (Is_Type (E) and then E = Current_Scope)
9833
9834 or else (Is_Incomplete_Or_Private_Type (E)
9835 and then Full_View (E) = Current_Scope)
9836
9837 -- Ada 2012 AI05-0053: the return object of an extended return
9838 -- statement is aliased if its type is immutably limited.
9839
9840 or else (Is_Return_Object (E)
9841 and then Is_Limited_View (Etype (E)));
9842
9843 elsif Nkind (Obj) = N_Selected_Component then
9844 return Is_Aliased (Entity (Selector_Name (Obj)));
9845
9846 elsif Nkind (Obj) = N_Indexed_Component then
9847 return Has_Aliased_Components (Etype (Prefix (Obj)))
9848 or else
9849 (Is_Access_Type (Etype (Prefix (Obj)))
9850 and then Has_Aliased_Components
9851 (Designated_Type (Etype (Prefix (Obj)))));
9852
9853 elsif Nkind_In (Obj, N_Unchecked_Type_Conversion, N_Type_Conversion) then
9854 return Is_Tagged_Type (Etype (Obj))
9855 and then Is_Aliased_View (Expression (Obj));
9856
9857 elsif Nkind (Obj) = N_Explicit_Dereference then
9858 return Nkind (Original_Node (Obj)) /= N_Function_Call;
9859
9860 else
9861 return False;
9862 end if;
9863 end Is_Aliased_View;
9864
9865 -------------------------
9866 -- Is_Ancestor_Package --
9867 -------------------------
9868
9869 function Is_Ancestor_Package
9870 (E1 : Entity_Id;
9871 E2 : Entity_Id) return Boolean
9872 is
9873 Par : Entity_Id;
9874
9875 begin
9876 Par := E2;
9877 while Present (Par) and then Par /= Standard_Standard loop
9878 if Par = E1 then
9879 return True;
9880 end if;
9881
9882 Par := Scope (Par);
9883 end loop;
9884
9885 return False;
9886 end Is_Ancestor_Package;
9887
9888 ----------------------
9889 -- Is_Atomic_Object --
9890 ----------------------
9891
9892 function Is_Atomic_Object (N : Node_Id) return Boolean is
9893
9894 function Object_Has_Atomic_Components (N : Node_Id) return Boolean;
9895 -- Determines if given object has atomic components
9896
9897 function Is_Atomic_Prefix (N : Node_Id) return Boolean;
9898 -- If prefix is an implicit dereference, examine designated type
9899
9900 ----------------------
9901 -- Is_Atomic_Prefix --
9902 ----------------------
9903
9904 function Is_Atomic_Prefix (N : Node_Id) return Boolean is
9905 begin
9906 if Is_Access_Type (Etype (N)) then
9907 return
9908 Has_Atomic_Components (Designated_Type (Etype (N)));
9909 else
9910 return Object_Has_Atomic_Components (N);
9911 end if;
9912 end Is_Atomic_Prefix;
9913
9914 ----------------------------------
9915 -- Object_Has_Atomic_Components --
9916 ----------------------------------
9917
9918 function Object_Has_Atomic_Components (N : Node_Id) return Boolean is
9919 begin
9920 if Has_Atomic_Components (Etype (N))
9921 or else Is_Atomic (Etype (N))
9922 then
9923 return True;
9924
9925 elsif Is_Entity_Name (N)
9926 and then (Has_Atomic_Components (Entity (N))
9927 or else Is_Atomic (Entity (N)))
9928 then
9929 return True;
9930
9931 elsif Nkind (N) = N_Selected_Component
9932 and then Is_Atomic (Entity (Selector_Name (N)))
9933 then
9934 return True;
9935
9936 elsif Nkind (N) = N_Indexed_Component
9937 or else Nkind (N) = N_Selected_Component
9938 then
9939 return Is_Atomic_Prefix (Prefix (N));
9940
9941 else
9942 return False;
9943 end if;
9944 end Object_Has_Atomic_Components;
9945
9946 -- Start of processing for Is_Atomic_Object
9947
9948 begin
9949 -- Predicate is not relevant to subprograms
9950
9951 if Is_Entity_Name (N) and then Is_Overloadable (Entity (N)) then
9952 return False;
9953
9954 elsif Is_Atomic (Etype (N))
9955 or else (Is_Entity_Name (N) and then Is_Atomic (Entity (N)))
9956 then
9957 return True;
9958
9959 elsif Nkind (N) = N_Selected_Component
9960 and then Is_Atomic (Entity (Selector_Name (N)))
9961 then
9962 return True;
9963
9964 elsif Nkind (N) = N_Indexed_Component
9965 or else Nkind (N) = N_Selected_Component
9966 then
9967 return Is_Atomic_Prefix (Prefix (N));
9968
9969 else
9970 return False;
9971 end if;
9972 end Is_Atomic_Object;
9973
9974 -------------------------
9975 -- Is_Attribute_Result --
9976 -------------------------
9977
9978 function Is_Attribute_Result (N : Node_Id) return Boolean is
9979 begin
9980 return Nkind (N) = N_Attribute_Reference
9981 and then Attribute_Name (N) = Name_Result;
9982 end Is_Attribute_Result;
9983
9984 ------------------------------------
9985 -- Is_Body_Or_Package_Declaration --
9986 ------------------------------------
9987
9988 function Is_Body_Or_Package_Declaration (N : Node_Id) return Boolean is
9989 begin
9990 return Nkind_In (N, N_Entry_Body,
9991 N_Package_Body,
9992 N_Package_Declaration,
9993 N_Protected_Body,
9994 N_Subprogram_Body,
9995 N_Task_Body);
9996 end Is_Body_Or_Package_Declaration;
9997
9998 -----------------------
9999 -- Is_Bounded_String --
10000 -----------------------
10001
10002 function Is_Bounded_String (T : Entity_Id) return Boolean is
10003 Under : constant Entity_Id := Underlying_Type (Root_Type (T));
10004
10005 begin
10006 -- Check whether T is ultimately derived from Ada.Strings.Superbounded.
10007 -- Super_String, or one of the [Wide_]Wide_ versions. This will
10008 -- be True for all the Bounded_String types in instances of the
10009 -- Generic_Bounded_Length generics, and for types derived from those.
10010
10011 return Present (Under)
10012 and then (Is_RTE (Root_Type (Under), RO_SU_Super_String) or else
10013 Is_RTE (Root_Type (Under), RO_WI_Super_String) or else
10014 Is_RTE (Root_Type (Under), RO_WW_Super_String));
10015 end Is_Bounded_String;
10016
10017 -------------------------
10018 -- Is_Child_Or_Sibling --
10019 -------------------------
10020
10021 function Is_Child_Or_Sibling
10022 (Pack_1 : Entity_Id;
10023 Pack_2 : Entity_Id) return Boolean
10024 is
10025 function Distance_From_Standard (Pack : Entity_Id) return Nat;
10026 -- Given an arbitrary package, return the number of "climbs" necessary
10027 -- to reach scope Standard_Standard.
10028
10029 procedure Equalize_Depths
10030 (Pack : in out Entity_Id;
10031 Depth : in out Nat;
10032 Depth_To_Reach : Nat);
10033 -- Given an arbitrary package, its depth and a target depth to reach,
10034 -- climb the scope chain until the said depth is reached. The pointer
10035 -- to the package and its depth a modified during the climb.
10036
10037 ----------------------------
10038 -- Distance_From_Standard --
10039 ----------------------------
10040
10041 function Distance_From_Standard (Pack : Entity_Id) return Nat is
10042 Dist : Nat;
10043 Scop : Entity_Id;
10044
10045 begin
10046 Dist := 0;
10047 Scop := Pack;
10048 while Present (Scop) and then Scop /= Standard_Standard loop
10049 Dist := Dist + 1;
10050 Scop := Scope (Scop);
10051 end loop;
10052
10053 return Dist;
10054 end Distance_From_Standard;
10055
10056 ---------------------
10057 -- Equalize_Depths --
10058 ---------------------
10059
10060 procedure Equalize_Depths
10061 (Pack : in out Entity_Id;
10062 Depth : in out Nat;
10063 Depth_To_Reach : Nat)
10064 is
10065 begin
10066 -- The package must be at a greater or equal depth
10067
10068 if Depth < Depth_To_Reach then
10069 raise Program_Error;
10070 end if;
10071
10072 -- Climb the scope chain until the desired depth is reached
10073
10074 while Present (Pack) and then Depth /= Depth_To_Reach loop
10075 Pack := Scope (Pack);
10076 Depth := Depth - 1;
10077 end loop;
10078 end Equalize_Depths;
10079
10080 -- Local variables
10081
10082 P_1 : Entity_Id := Pack_1;
10083 P_1_Child : Boolean := False;
10084 P_1_Depth : Nat := Distance_From_Standard (P_1);
10085 P_2 : Entity_Id := Pack_2;
10086 P_2_Child : Boolean := False;
10087 P_2_Depth : Nat := Distance_From_Standard (P_2);
10088
10089 -- Start of processing for Is_Child_Or_Sibling
10090
10091 begin
10092 pragma Assert
10093 (Ekind (Pack_1) = E_Package and then Ekind (Pack_2) = E_Package);
10094
10095 -- Both packages denote the same entity, therefore they cannot be
10096 -- children or siblings.
10097
10098 if P_1 = P_2 then
10099 return False;
10100
10101 -- One of the packages is at a deeper level than the other. Note that
10102 -- both may still come from differen hierarchies.
10103
10104 -- (root) P_2
10105 -- / \ :
10106 -- X P_2 or X
10107 -- : :
10108 -- P_1 P_1
10109
10110 elsif P_1_Depth > P_2_Depth then
10111 Equalize_Depths
10112 (Pack => P_1,
10113 Depth => P_1_Depth,
10114 Depth_To_Reach => P_2_Depth);
10115 P_1_Child := True;
10116
10117 -- (root) P_1
10118 -- / \ :
10119 -- P_1 X or X
10120 -- : :
10121 -- P_2 P_2
10122
10123 elsif P_2_Depth > P_1_Depth then
10124 Equalize_Depths
10125 (Pack => P_2,
10126 Depth => P_2_Depth,
10127 Depth_To_Reach => P_1_Depth);
10128 P_2_Child := True;
10129 end if;
10130
10131 -- At this stage the package pointers have been elevated to the same
10132 -- depth. If the related entities are the same, then one package is a
10133 -- potential child of the other:
10134
10135 -- P_1
10136 -- :
10137 -- X became P_1 P_2 or vica versa
10138 -- :
10139 -- P_2
10140
10141 if P_1 = P_2 then
10142 if P_1_Child then
10143 return Is_Child_Unit (Pack_1);
10144
10145 else pragma Assert (P_2_Child);
10146 return Is_Child_Unit (Pack_2);
10147 end if;
10148
10149 -- The packages may come from the same package chain or from entirely
10150 -- different hierarcies. To determine this, climb the scope stack until
10151 -- a common root is found.
10152
10153 -- (root) (root 1) (root 2)
10154 -- / \ | |
10155 -- P_1 P_2 P_1 P_2
10156
10157 else
10158 while Present (P_1) and then Present (P_2) loop
10159
10160 -- The two packages may be siblings
10161
10162 if P_1 = P_2 then
10163 return Is_Child_Unit (Pack_1) and then Is_Child_Unit (Pack_2);
10164 end if;
10165
10166 P_1 := Scope (P_1);
10167 P_2 := Scope (P_2);
10168 end loop;
10169 end if;
10170
10171 return False;
10172 end Is_Child_Or_Sibling;
10173
10174 -----------------------------
10175 -- Is_Concurrent_Interface --
10176 -----------------------------
10177
10178 function Is_Concurrent_Interface (T : Entity_Id) return Boolean is
10179 begin
10180 return Is_Interface (T)
10181 and then
10182 (Is_Protected_Interface (T)
10183 or else Is_Synchronized_Interface (T)
10184 or else Is_Task_Interface (T));
10185 end Is_Concurrent_Interface;
10186
10187 ---------------------------
10188 -- Is_Container_Element --
10189 ---------------------------
10190
10191 function Is_Container_Element (Exp : Node_Id) return Boolean is
10192 Loc : constant Source_Ptr := Sloc (Exp);
10193 Pref : constant Node_Id := Prefix (Exp);
10194
10195 Call : Node_Id;
10196 -- Call to an indexing aspect
10197
10198 Cont_Typ : Entity_Id;
10199 -- The type of the container being accessed
10200
10201 Elem_Typ : Entity_Id;
10202 -- Its element type
10203
10204 Indexing : Entity_Id;
10205 Is_Const : Boolean;
10206 -- Indicates that constant indexing is used, and the element is thus
10207 -- a constant.
10208
10209 Ref_Typ : Entity_Id;
10210 -- The reference type returned by the indexing operation
10211
10212 begin
10213 -- If C is a container, in a context that imposes the element type of
10214 -- that container, the indexing notation C (X) is rewritten as:
10215
10216 -- Indexing (C, X).Discr.all
10217
10218 -- where Indexing is one of the indexing aspects of the container.
10219 -- If the context does not require a reference, the construct can be
10220 -- rewritten as
10221
10222 -- Element (C, X)
10223
10224 -- First, verify that the construct has the proper form
10225
10226 if not Expander_Active then
10227 return False;
10228
10229 elsif Nkind (Pref) /= N_Selected_Component then
10230 return False;
10231
10232 elsif Nkind (Prefix (Pref)) /= N_Function_Call then
10233 return False;
10234
10235 else
10236 Call := Prefix (Pref);
10237 Ref_Typ := Etype (Call);
10238 end if;
10239
10240 if not Has_Implicit_Dereference (Ref_Typ)
10241 or else No (First (Parameter_Associations (Call)))
10242 or else not Is_Entity_Name (Name (Call))
10243 then
10244 return False;
10245 end if;
10246
10247 -- Retrieve type of container object, and its iterator aspects
10248
10249 Cont_Typ := Etype (First (Parameter_Associations (Call)));
10250 Indexing := Find_Value_Of_Aspect (Cont_Typ, Aspect_Constant_Indexing);
10251 Is_Const := False;
10252
10253 if No (Indexing) then
10254
10255 -- Container should have at least one indexing operation
10256
10257 return False;
10258
10259 elsif Entity (Name (Call)) /= Entity (Indexing) then
10260
10261 -- This may be a variable indexing operation
10262
10263 Indexing := Find_Value_Of_Aspect (Cont_Typ, Aspect_Variable_Indexing);
10264
10265 if No (Indexing)
10266 or else Entity (Name (Call)) /= Entity (Indexing)
10267 then
10268 return False;
10269 end if;
10270
10271 else
10272 Is_Const := True;
10273 end if;
10274
10275 Elem_Typ := Find_Value_Of_Aspect (Cont_Typ, Aspect_Iterator_Element);
10276
10277 if No (Elem_Typ) or else Entity (Elem_Typ) /= Etype (Exp) then
10278 return False;
10279 end if;
10280
10281 -- Check that the expression is not the target of an assignment, in
10282 -- which case the rewriting is not possible.
10283
10284 if not Is_Const then
10285 declare
10286 Par : Node_Id;
10287
10288 begin
10289 Par := Exp;
10290 while Present (Par)
10291 loop
10292 if Nkind (Parent (Par)) = N_Assignment_Statement
10293 and then Par = Name (Parent (Par))
10294 then
10295 return False;
10296
10297 -- A renaming produces a reference, and the transformation
10298 -- does not apply.
10299
10300 elsif Nkind (Parent (Par)) = N_Object_Renaming_Declaration then
10301 return False;
10302
10303 elsif Nkind_In
10304 (Nkind (Parent (Par)), N_Function_Call,
10305 N_Procedure_Call_Statement,
10306 N_Entry_Call_Statement)
10307 then
10308 -- Check that the element is not part of an actual for an
10309 -- in-out parameter.
10310
10311 declare
10312 F : Entity_Id;
10313 A : Node_Id;
10314
10315 begin
10316 F := First_Formal (Entity (Name (Parent (Par))));
10317 A := First (Parameter_Associations (Parent (Par)));
10318 while Present (F) loop
10319 if A = Par and then Ekind (F) /= E_In_Parameter then
10320 return False;
10321 end if;
10322
10323 Next_Formal (F);
10324 Next (A);
10325 end loop;
10326 end;
10327
10328 -- E_In_Parameter in a call: element is not modified.
10329
10330 exit;
10331 end if;
10332
10333 Par := Parent (Par);
10334 end loop;
10335 end;
10336 end if;
10337
10338 -- The expression has the proper form and the context requires the
10339 -- element type. Retrieve the Element function of the container and
10340 -- rewrite the construct as a call to it.
10341
10342 declare
10343 Op : Elmt_Id;
10344
10345 begin
10346 Op := First_Elmt (Primitive_Operations (Cont_Typ));
10347 while Present (Op) loop
10348 exit when Chars (Node (Op)) = Name_Element;
10349 Next_Elmt (Op);
10350 end loop;
10351
10352 if No (Op) then
10353 return False;
10354
10355 else
10356 Rewrite (Exp,
10357 Make_Function_Call (Loc,
10358 Name => New_Occurrence_Of (Node (Op), Loc),
10359 Parameter_Associations => Parameter_Associations (Call)));
10360 Analyze_And_Resolve (Exp, Entity (Elem_Typ));
10361 return True;
10362 end if;
10363 end;
10364 end Is_Container_Element;
10365
10366 -----------------------
10367 -- Is_Constant_Bound --
10368 -----------------------
10369
10370 function Is_Constant_Bound (Exp : Node_Id) return Boolean is
10371 begin
10372 if Compile_Time_Known_Value (Exp) then
10373 return True;
10374
10375 elsif Is_Entity_Name (Exp) and then Present (Entity (Exp)) then
10376 return Is_Constant_Object (Entity (Exp))
10377 or else Ekind (Entity (Exp)) = E_Enumeration_Literal;
10378
10379 elsif Nkind (Exp) in N_Binary_Op then
10380 return Is_Constant_Bound (Left_Opnd (Exp))
10381 and then Is_Constant_Bound (Right_Opnd (Exp))
10382 and then Scope (Entity (Exp)) = Standard_Standard;
10383
10384 else
10385 return False;
10386 end if;
10387 end Is_Constant_Bound;
10388
10389 --------------------------------------
10390 -- Is_Controlling_Limited_Procedure --
10391 --------------------------------------
10392
10393 function Is_Controlling_Limited_Procedure
10394 (Proc_Nam : Entity_Id) return Boolean
10395 is
10396 Param_Typ : Entity_Id := Empty;
10397
10398 begin
10399 if Ekind (Proc_Nam) = E_Procedure
10400 and then Present (Parameter_Specifications (Parent (Proc_Nam)))
10401 then
10402 Param_Typ := Etype (Parameter_Type (First (
10403 Parameter_Specifications (Parent (Proc_Nam)))));
10404
10405 -- In this case where an Itype was created, the procedure call has been
10406 -- rewritten.
10407
10408 elsif Present (Associated_Node_For_Itype (Proc_Nam))
10409 and then Present (Original_Node (Associated_Node_For_Itype (Proc_Nam)))
10410 and then
10411 Present (Parameter_Associations
10412 (Associated_Node_For_Itype (Proc_Nam)))
10413 then
10414 Param_Typ :=
10415 Etype (First (Parameter_Associations
10416 (Associated_Node_For_Itype (Proc_Nam))));
10417 end if;
10418
10419 if Present (Param_Typ) then
10420 return
10421 Is_Interface (Param_Typ)
10422 and then Is_Limited_Record (Param_Typ);
10423 end if;
10424
10425 return False;
10426 end Is_Controlling_Limited_Procedure;
10427
10428 -----------------------------
10429 -- Is_CPP_Constructor_Call --
10430 -----------------------------
10431
10432 function Is_CPP_Constructor_Call (N : Node_Id) return Boolean is
10433 begin
10434 return Nkind (N) = N_Function_Call
10435 and then Is_CPP_Class (Etype (Etype (N)))
10436 and then Is_Constructor (Entity (Name (N)))
10437 and then Is_Imported (Entity (Name (N)));
10438 end Is_CPP_Constructor_Call;
10439
10440 -----------------
10441 -- Is_Delegate --
10442 -----------------
10443
10444 function Is_Delegate (T : Entity_Id) return Boolean is
10445 Desig_Type : Entity_Id;
10446
10447 begin
10448 if VM_Target /= CLI_Target then
10449 return False;
10450 end if;
10451
10452 -- Access-to-subprograms are delegates in CIL
10453
10454 if Ekind (T) = E_Access_Subprogram_Type then
10455 return True;
10456 end if;
10457
10458 if not Is_Access_Type (T) then
10459
10460 -- A delegate is a managed pointer. If no designated type is defined
10461 -- it means that it's not a delegate.
10462
10463 return False;
10464 end if;
10465
10466 Desig_Type := Etype (Directly_Designated_Type (T));
10467
10468 if not Is_Tagged_Type (Desig_Type) then
10469 return False;
10470 end if;
10471
10472 -- Test if the type is inherited from [mscorlib]System.Delegate
10473
10474 while Etype (Desig_Type) /= Desig_Type loop
10475 if Chars (Scope (Desig_Type)) /= No_Name
10476 and then Is_Imported (Scope (Desig_Type))
10477 and then Get_Name_String (Chars (Scope (Desig_Type))) = "delegate"
10478 then
10479 return True;
10480 end if;
10481
10482 Desig_Type := Etype (Desig_Type);
10483 end loop;
10484
10485 return False;
10486 end Is_Delegate;
10487
10488 ----------------------------------------------
10489 -- Is_Dependent_Component_Of_Mutable_Object --
10490 ----------------------------------------------
10491
10492 function Is_Dependent_Component_Of_Mutable_Object
10493 (Object : Node_Id) return Boolean
10494 is
10495 function Is_Declared_Within_Variant (Comp : Entity_Id) return Boolean;
10496 -- Returns True if and only if Comp is declared within a variant part
10497
10498 --------------------------------
10499 -- Is_Declared_Within_Variant --
10500 --------------------------------
10501
10502 function Is_Declared_Within_Variant (Comp : Entity_Id) return Boolean is
10503 Comp_Decl : constant Node_Id := Parent (Comp);
10504 Comp_List : constant Node_Id := Parent (Comp_Decl);
10505 begin
10506 return Nkind (Parent (Comp_List)) = N_Variant;
10507 end Is_Declared_Within_Variant;
10508
10509 P : Node_Id;
10510 Prefix_Type : Entity_Id;
10511 P_Aliased : Boolean := False;
10512 Comp : Entity_Id;
10513
10514 Deref : Node_Id := Object;
10515 -- Dereference node, in something like X.all.Y(2)
10516
10517 -- Start of processing for Is_Dependent_Component_Of_Mutable_Object
10518
10519 begin
10520 -- Find the dereference node if any
10521
10522 while Nkind_In (Deref, N_Indexed_Component,
10523 N_Selected_Component,
10524 N_Slice)
10525 loop
10526 Deref := Prefix (Deref);
10527 end loop;
10528
10529 -- Ada 2005: If we have a component or slice of a dereference,
10530 -- something like X.all.Y (2), and the type of X is access-to-constant,
10531 -- Is_Variable will return False, because it is indeed a constant
10532 -- view. But it might be a view of a variable object, so we want the
10533 -- following condition to be True in that case.
10534
10535 if Is_Variable (Object)
10536 or else (Ada_Version >= Ada_2005
10537 and then Nkind (Deref) = N_Explicit_Dereference)
10538 then
10539 if Nkind (Object) = N_Selected_Component then
10540 P := Prefix (Object);
10541 Prefix_Type := Etype (P);
10542
10543 if Is_Entity_Name (P) then
10544 if Ekind (Entity (P)) = E_Generic_In_Out_Parameter then
10545 Prefix_Type := Base_Type (Prefix_Type);
10546 end if;
10547
10548 if Is_Aliased (Entity (P)) then
10549 P_Aliased := True;
10550 end if;
10551
10552 -- A discriminant check on a selected component may be expanded
10553 -- into a dereference when removing side-effects. Recover the
10554 -- original node and its type, which may be unconstrained.
10555
10556 elsif Nkind (P) = N_Explicit_Dereference
10557 and then not (Comes_From_Source (P))
10558 then
10559 P := Original_Node (P);
10560 Prefix_Type := Etype (P);
10561
10562 else
10563 -- Check for prefix being an aliased component???
10564
10565 null;
10566
10567 end if;
10568
10569 -- A heap object is constrained by its initial value
10570
10571 -- Ada 2005 (AI-363): Always assume the object could be mutable in
10572 -- the dereferenced case, since the access value might denote an
10573 -- unconstrained aliased object, whereas in Ada 95 the designated
10574 -- object is guaranteed to be constrained. A worst-case assumption
10575 -- has to apply in Ada 2005 because we can't tell at compile
10576 -- time whether the object is "constrained by its initial value"
10577 -- (despite the fact that 3.10.2(26/2) and 8.5.1(5/2) are semantic
10578 -- rules (these rules are acknowledged to need fixing).
10579
10580 if Ada_Version < Ada_2005 then
10581 if Is_Access_Type (Prefix_Type)
10582 or else Nkind (P) = N_Explicit_Dereference
10583 then
10584 return False;
10585 end if;
10586
10587 else pragma Assert (Ada_Version >= Ada_2005);
10588 if Is_Access_Type (Prefix_Type) then
10589
10590 -- If the access type is pool-specific, and there is no
10591 -- constrained partial view of the designated type, then the
10592 -- designated object is known to be constrained.
10593
10594 if Ekind (Prefix_Type) = E_Access_Type
10595 and then not Object_Type_Has_Constrained_Partial_View
10596 (Typ => Designated_Type (Prefix_Type),
10597 Scop => Current_Scope)
10598 then
10599 return False;
10600
10601 -- Otherwise (general access type, or there is a constrained
10602 -- partial view of the designated type), we need to check
10603 -- based on the designated type.
10604
10605 else
10606 Prefix_Type := Designated_Type (Prefix_Type);
10607 end if;
10608 end if;
10609 end if;
10610
10611 Comp :=
10612 Original_Record_Component (Entity (Selector_Name (Object)));
10613
10614 -- As per AI-0017, the renaming is illegal in a generic body, even
10615 -- if the subtype is indefinite.
10616
10617 -- Ada 2005 (AI-363): In Ada 2005 an aliased object can be mutable
10618
10619 if not Is_Constrained (Prefix_Type)
10620 and then (not Is_Indefinite_Subtype (Prefix_Type)
10621 or else
10622 (Is_Generic_Type (Prefix_Type)
10623 and then Ekind (Current_Scope) = E_Generic_Package
10624 and then In_Package_Body (Current_Scope)))
10625
10626 and then (Is_Declared_Within_Variant (Comp)
10627 or else Has_Discriminant_Dependent_Constraint (Comp))
10628 and then (not P_Aliased or else Ada_Version >= Ada_2005)
10629 then
10630 return True;
10631
10632 -- If the prefix is of an access type at this point, then we want
10633 -- to return False, rather than calling this function recursively
10634 -- on the access object (which itself might be a discriminant-
10635 -- dependent component of some other object, but that isn't
10636 -- relevant to checking the object passed to us). This avoids
10637 -- issuing wrong errors when compiling with -gnatc, where there
10638 -- can be implicit dereferences that have not been expanded.
10639
10640 elsif Is_Access_Type (Etype (Prefix (Object))) then
10641 return False;
10642
10643 else
10644 return
10645 Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
10646 end if;
10647
10648 elsif Nkind (Object) = N_Indexed_Component
10649 or else Nkind (Object) = N_Slice
10650 then
10651 return Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
10652
10653 -- A type conversion that Is_Variable is a view conversion:
10654 -- go back to the denoted object.
10655
10656 elsif Nkind (Object) = N_Type_Conversion then
10657 return
10658 Is_Dependent_Component_Of_Mutable_Object (Expression (Object));
10659 end if;
10660 end if;
10661
10662 return False;
10663 end Is_Dependent_Component_Of_Mutable_Object;
10664
10665 ---------------------
10666 -- Is_Dereferenced --
10667 ---------------------
10668
10669 function Is_Dereferenced (N : Node_Id) return Boolean is
10670 P : constant Node_Id := Parent (N);
10671 begin
10672 return Nkind_In (P, N_Selected_Component,
10673 N_Explicit_Dereference,
10674 N_Indexed_Component,
10675 N_Slice)
10676 and then Prefix (P) = N;
10677 end Is_Dereferenced;
10678
10679 ----------------------
10680 -- Is_Descendent_Of --
10681 ----------------------
10682
10683 function Is_Descendent_Of (T1 : Entity_Id; T2 : Entity_Id) return Boolean is
10684 T : Entity_Id;
10685 Etyp : Entity_Id;
10686
10687 begin
10688 pragma Assert (Nkind (T1) in N_Entity);
10689 pragma Assert (Nkind (T2) in N_Entity);
10690
10691 T := Base_Type (T1);
10692
10693 -- Immediate return if the types match
10694
10695 if T = T2 then
10696 return True;
10697
10698 -- Comment needed here ???
10699
10700 elsif Ekind (T) = E_Class_Wide_Type then
10701 return Etype (T) = T2;
10702
10703 -- All other cases
10704
10705 else
10706 loop
10707 Etyp := Etype (T);
10708
10709 -- Done if we found the type we are looking for
10710
10711 if Etyp = T2 then
10712 return True;
10713
10714 -- Done if no more derivations to check
10715
10716 elsif T = T1
10717 or else T = Etyp
10718 then
10719 return False;
10720
10721 -- Following test catches error cases resulting from prev errors
10722
10723 elsif No (Etyp) then
10724 return False;
10725
10726 elsif Is_Private_Type (T) and then Etyp = Full_View (T) then
10727 return False;
10728
10729 elsif Is_Private_Type (Etyp) and then Full_View (Etyp) = T then
10730 return False;
10731 end if;
10732
10733 T := Base_Type (Etyp);
10734 end loop;
10735 end if;
10736 end Is_Descendent_Of;
10737
10738 -----------------------------
10739 -- Is_Effectively_Volatile --
10740 -----------------------------
10741
10742 function Is_Effectively_Volatile (Id : Entity_Id) return Boolean is
10743 begin
10744 if Is_Type (Id) then
10745
10746 -- An arbitrary type is effectively volatile when it is subject to
10747 -- pragma Atomic or Volatile.
10748
10749 if Is_Volatile (Id) then
10750 return True;
10751
10752 -- An array type is effectively volatile when it is subject to pragma
10753 -- Atomic_Components or Volatile_Components or its compolent type is
10754 -- effectively volatile.
10755
10756 elsif Is_Array_Type (Id) then
10757 return
10758 Has_Volatile_Components (Id)
10759 or else
10760 Is_Effectively_Volatile (Component_Type (Base_Type (Id)));
10761
10762 else
10763 return False;
10764 end if;
10765
10766 -- Otherwise Id denotes an object
10767
10768 else
10769 return
10770 Is_Volatile (Id)
10771 or else Has_Volatile_Components (Id)
10772 or else Is_Effectively_Volatile (Etype (Id));
10773 end if;
10774 end Is_Effectively_Volatile;
10775
10776 ------------------------------------
10777 -- Is_Effectively_Volatile_Object --
10778 ------------------------------------
10779
10780 function Is_Effectively_Volatile_Object (N : Node_Id) return Boolean is
10781 begin
10782 if Is_Entity_Name (N) then
10783 return Is_Effectively_Volatile (Entity (N));
10784
10785 elsif Nkind (N) = N_Expanded_Name then
10786 return Is_Effectively_Volatile (Entity (N));
10787
10788 elsif Nkind (N) = N_Indexed_Component then
10789 return Is_Effectively_Volatile_Object (Prefix (N));
10790
10791 elsif Nkind (N) = N_Selected_Component then
10792 return
10793 Is_Effectively_Volatile_Object (Prefix (N))
10794 or else
10795 Is_Effectively_Volatile_Object (Selector_Name (N));
10796
10797 else
10798 return False;
10799 end if;
10800 end Is_Effectively_Volatile_Object;
10801
10802 ----------------------------
10803 -- Is_Expression_Function --
10804 ----------------------------
10805
10806 function Is_Expression_Function (Subp : Entity_Id) return Boolean is
10807 Decl : Node_Id;
10808
10809 begin
10810 if Ekind (Subp) /= E_Function then
10811 return False;
10812
10813 else
10814 Decl := Unit_Declaration_Node (Subp);
10815 return Nkind (Decl) = N_Subprogram_Declaration
10816 and then
10817 (Nkind (Original_Node (Decl)) = N_Expression_Function
10818 or else
10819 (Present (Corresponding_Body (Decl))
10820 and then
10821 Nkind (Original_Node
10822 (Unit_Declaration_Node
10823 (Corresponding_Body (Decl)))) =
10824 N_Expression_Function));
10825 end if;
10826 end Is_Expression_Function;
10827
10828 -----------------------
10829 -- Is_EVF_Expression --
10830 -----------------------
10831
10832 function Is_EVF_Expression (N : Node_Id) return Boolean is
10833 Orig_N : constant Node_Id := Original_Node (N);
10834 Alt : Node_Id;
10835 Expr : Node_Id;
10836 Id : Entity_Id;
10837
10838 begin
10839 -- Detect a reference to a formal parameter of a specific tagged type
10840 -- whose related subprogram is subject to pragma Expresions_Visible with
10841 -- value "False".
10842
10843 if Is_Entity_Name (N) and then Present (Entity (N)) then
10844 Id := Entity (N);
10845
10846 return
10847 Is_Formal (Id)
10848 and then Is_Specific_Tagged_Type (Etype (Id))
10849 and then Extensions_Visible_Status (Id) =
10850 Extensions_Visible_False;
10851
10852 -- A case expression is an EVF expression when it contains at least one
10853 -- EVF dependent_expression. Note that a case expression may have been
10854 -- expanded, hence the use of Original_Node.
10855
10856 elsif Nkind (Orig_N) = N_Case_Expression then
10857 Alt := First (Alternatives (Orig_N));
10858 while Present (Alt) loop
10859 if Is_EVF_Expression (Expression (Alt)) then
10860 return True;
10861 end if;
10862
10863 Next (Alt);
10864 end loop;
10865
10866 -- An if expression is an EVF expression when it contains at least one
10867 -- EVF dependent_expression. Note that an if expression may have been
10868 -- expanded, hence the use of Original_Node.
10869
10870 elsif Nkind (Orig_N) = N_If_Expression then
10871 Expr := Next (First (Expressions (Orig_N)));
10872 while Present (Expr) loop
10873 if Is_EVF_Expression (Expr) then
10874 return True;
10875 end if;
10876
10877 Next (Expr);
10878 end loop;
10879
10880 -- A qualified expression or a type conversion is an EVF expression when
10881 -- its operand is an EVF expression.
10882
10883 elsif Nkind_In (N, N_Qualified_Expression,
10884 N_Unchecked_Type_Conversion,
10885 N_Type_Conversion)
10886 then
10887 return Is_EVF_Expression (Expression (N));
10888
10889 -- Attributes 'Loop_Entry, 'Old and 'Update are an EVF expression when
10890 -- their prefix denotes an EVF expression.
10891
10892 elsif Nkind (N) = N_Attribute_Reference
10893 and then Nam_In (Attribute_Name (N), Name_Loop_Entry,
10894 Name_Old,
10895 Name_Update)
10896 then
10897 return Is_EVF_Expression (Prefix (N));
10898 end if;
10899
10900 return False;
10901 end Is_EVF_Expression;
10902
10903 --------------
10904 -- Is_False --
10905 --------------
10906
10907 function Is_False (U : Uint) return Boolean is
10908 begin
10909 return (U = 0);
10910 end Is_False;
10911
10912 ---------------------------
10913 -- Is_Fixed_Model_Number --
10914 ---------------------------
10915
10916 function Is_Fixed_Model_Number (U : Ureal; T : Entity_Id) return Boolean is
10917 S : constant Ureal := Small_Value (T);
10918 M : Urealp.Save_Mark;
10919 R : Boolean;
10920 begin
10921 M := Urealp.Mark;
10922 R := (U = UR_Trunc (U / S) * S);
10923 Urealp.Release (M);
10924 return R;
10925 end Is_Fixed_Model_Number;
10926
10927 -------------------------------
10928 -- Is_Fully_Initialized_Type --
10929 -------------------------------
10930
10931 function Is_Fully_Initialized_Type (Typ : Entity_Id) return Boolean is
10932 begin
10933 -- Scalar types
10934
10935 if Is_Scalar_Type (Typ) then
10936
10937 -- A scalar type with an aspect Default_Value is fully initialized
10938
10939 -- Note: Iniitalize/Normalize_Scalars also ensure full initialization
10940 -- of a scalar type, but we don't take that into account here, since
10941 -- we don't want these to affect warnings.
10942
10943 return Has_Default_Aspect (Typ);
10944
10945 elsif Is_Access_Type (Typ) then
10946 return True;
10947
10948 elsif Is_Array_Type (Typ) then
10949 if Is_Fully_Initialized_Type (Component_Type (Typ))
10950 or else (Ada_Version >= Ada_2012 and then Has_Default_Aspect (Typ))
10951 then
10952 return True;
10953 end if;
10954
10955 -- An interesting case, if we have a constrained type one of whose
10956 -- bounds is known to be null, then there are no elements to be
10957 -- initialized, so all the elements are initialized.
10958
10959 if Is_Constrained (Typ) then
10960 declare
10961 Indx : Node_Id;
10962 Indx_Typ : Entity_Id;
10963 Lbd, Hbd : Node_Id;
10964
10965 begin
10966 Indx := First_Index (Typ);
10967 while Present (Indx) loop
10968 if Etype (Indx) = Any_Type then
10969 return False;
10970
10971 -- If index is a range, use directly
10972
10973 elsif Nkind (Indx) = N_Range then
10974 Lbd := Low_Bound (Indx);
10975 Hbd := High_Bound (Indx);
10976
10977 else
10978 Indx_Typ := Etype (Indx);
10979
10980 if Is_Private_Type (Indx_Typ) then
10981 Indx_Typ := Full_View (Indx_Typ);
10982 end if;
10983
10984 if No (Indx_Typ) or else Etype (Indx_Typ) = Any_Type then
10985 return False;
10986 else
10987 Lbd := Type_Low_Bound (Indx_Typ);
10988 Hbd := Type_High_Bound (Indx_Typ);
10989 end if;
10990 end if;
10991
10992 if Compile_Time_Known_Value (Lbd)
10993 and then
10994 Compile_Time_Known_Value (Hbd)
10995 then
10996 if Expr_Value (Hbd) < Expr_Value (Lbd) then
10997 return True;
10998 end if;
10999 end if;
11000
11001 Next_Index (Indx);
11002 end loop;
11003 end;
11004 end if;
11005
11006 -- If no null indexes, then type is not fully initialized
11007
11008 return False;
11009
11010 -- Record types
11011
11012 elsif Is_Record_Type (Typ) then
11013 if Has_Discriminants (Typ)
11014 and then
11015 Present (Discriminant_Default_Value (First_Discriminant (Typ)))
11016 and then Is_Fully_Initialized_Variant (Typ)
11017 then
11018 return True;
11019 end if;
11020
11021 -- We consider bounded string types to be fully initialized, because
11022 -- otherwise we get false alarms when the Data component is not
11023 -- default-initialized.
11024
11025 if Is_Bounded_String (Typ) then
11026 return True;
11027 end if;
11028
11029 -- Controlled records are considered to be fully initialized if
11030 -- there is a user defined Initialize routine. This may not be
11031 -- entirely correct, but as the spec notes, we are guessing here
11032 -- what is best from the point of view of issuing warnings.
11033
11034 if Is_Controlled (Typ) then
11035 declare
11036 Utyp : constant Entity_Id := Underlying_Type (Typ);
11037
11038 begin
11039 if Present (Utyp) then
11040 declare
11041 Init : constant Entity_Id :=
11042 (Find_Prim_Op
11043 (Underlying_Type (Typ), Name_Initialize));
11044
11045 begin
11046 if Present (Init)
11047 and then Comes_From_Source (Init)
11048 and then not
11049 Is_Predefined_File_Name
11050 (File_Name (Get_Source_File_Index (Sloc (Init))))
11051 then
11052 return True;
11053
11054 elsif Has_Null_Extension (Typ)
11055 and then
11056 Is_Fully_Initialized_Type
11057 (Etype (Base_Type (Typ)))
11058 then
11059 return True;
11060 end if;
11061 end;
11062 end if;
11063 end;
11064 end if;
11065
11066 -- Otherwise see if all record components are initialized
11067
11068 declare
11069 Ent : Entity_Id;
11070
11071 begin
11072 Ent := First_Entity (Typ);
11073 while Present (Ent) loop
11074 if Ekind (Ent) = E_Component
11075 and then (No (Parent (Ent))
11076 or else No (Expression (Parent (Ent))))
11077 and then not Is_Fully_Initialized_Type (Etype (Ent))
11078
11079 -- Special VM case for tag components, which need to be
11080 -- defined in this case, but are never initialized as VMs
11081 -- are using other dispatching mechanisms. Ignore this
11082 -- uninitialized case. Note that this applies both to the
11083 -- uTag entry and the main vtable pointer (CPP_Class case).
11084
11085 and then (Tagged_Type_Expansion or else not Is_Tag (Ent))
11086 then
11087 return False;
11088 end if;
11089
11090 Next_Entity (Ent);
11091 end loop;
11092 end;
11093
11094 -- No uninitialized components, so type is fully initialized.
11095 -- Note that this catches the case of no components as well.
11096
11097 return True;
11098
11099 elsif Is_Concurrent_Type (Typ) then
11100 return True;
11101
11102 elsif Is_Private_Type (Typ) then
11103 declare
11104 U : constant Entity_Id := Underlying_Type (Typ);
11105
11106 begin
11107 if No (U) then
11108 return False;
11109 else
11110 return Is_Fully_Initialized_Type (U);
11111 end if;
11112 end;
11113
11114 else
11115 return False;
11116 end if;
11117 end Is_Fully_Initialized_Type;
11118
11119 ----------------------------------
11120 -- Is_Fully_Initialized_Variant --
11121 ----------------------------------
11122
11123 function Is_Fully_Initialized_Variant (Typ : Entity_Id) return Boolean is
11124 Loc : constant Source_Ptr := Sloc (Typ);
11125 Constraints : constant List_Id := New_List;
11126 Components : constant Elist_Id := New_Elmt_List;
11127 Comp_Elmt : Elmt_Id;
11128 Comp_Id : Node_Id;
11129 Comp_List : Node_Id;
11130 Discr : Entity_Id;
11131 Discr_Val : Node_Id;
11132
11133 Report_Errors : Boolean;
11134 pragma Warnings (Off, Report_Errors);
11135
11136 begin
11137 if Serious_Errors_Detected > 0 then
11138 return False;
11139 end if;
11140
11141 if Is_Record_Type (Typ)
11142 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
11143 and then Nkind (Type_Definition (Parent (Typ))) = N_Record_Definition
11144 then
11145 Comp_List := Component_List (Type_Definition (Parent (Typ)));
11146
11147 Discr := First_Discriminant (Typ);
11148 while Present (Discr) loop
11149 if Nkind (Parent (Discr)) = N_Discriminant_Specification then
11150 Discr_Val := Expression (Parent (Discr));
11151
11152 if Present (Discr_Val)
11153 and then Is_OK_Static_Expression (Discr_Val)
11154 then
11155 Append_To (Constraints,
11156 Make_Component_Association (Loc,
11157 Choices => New_List (New_Occurrence_Of (Discr, Loc)),
11158 Expression => New_Copy (Discr_Val)));
11159 else
11160 return False;
11161 end if;
11162 else
11163 return False;
11164 end if;
11165
11166 Next_Discriminant (Discr);
11167 end loop;
11168
11169 Gather_Components
11170 (Typ => Typ,
11171 Comp_List => Comp_List,
11172 Governed_By => Constraints,
11173 Into => Components,
11174 Report_Errors => Report_Errors);
11175
11176 -- Check that each component present is fully initialized
11177
11178 Comp_Elmt := First_Elmt (Components);
11179 while Present (Comp_Elmt) loop
11180 Comp_Id := Node (Comp_Elmt);
11181
11182 if Ekind (Comp_Id) = E_Component
11183 and then (No (Parent (Comp_Id))
11184 or else No (Expression (Parent (Comp_Id))))
11185 and then not Is_Fully_Initialized_Type (Etype (Comp_Id))
11186 then
11187 return False;
11188 end if;
11189
11190 Next_Elmt (Comp_Elmt);
11191 end loop;
11192
11193 return True;
11194
11195 elsif Is_Private_Type (Typ) then
11196 declare
11197 U : constant Entity_Id := Underlying_Type (Typ);
11198
11199 begin
11200 if No (U) then
11201 return False;
11202 else
11203 return Is_Fully_Initialized_Variant (U);
11204 end if;
11205 end;
11206
11207 else
11208 return False;
11209 end if;
11210 end Is_Fully_Initialized_Variant;
11211
11212 ---------------------
11213 -- Is_Ghost_Entity --
11214 ---------------------
11215
11216 function Is_Ghost_Entity (Id : Entity_Id) return Boolean is
11217 begin
11218 return Is_Checked_Ghost_Entity (Id) or else Is_Ignored_Ghost_Entity (Id);
11219 end Is_Ghost_Entity;
11220
11221 ----------------------------------
11222 -- Is_Ghost_Statement_Or_Pragma --
11223 ----------------------------------
11224
11225 function Is_Ghost_Statement_Or_Pragma (N : Node_Id) return Boolean is
11226 function Is_Ghost_Entity_Reference (N : Node_Id) return Boolean;
11227 -- Determine whether an arbitrary node denotes a reference to a Ghost
11228 -- entity.
11229
11230 -------------------------------
11231 -- Is_Ghost_Entity_Reference --
11232 -------------------------------
11233
11234 function Is_Ghost_Entity_Reference (N : Node_Id) return Boolean is
11235 Ref : Node_Id;
11236
11237 begin
11238 Ref := N;
11239
11240 -- When the reference extracts a subcomponent, recover the related
11241 -- object (SPARK RM 6.9(1)).
11242
11243 while Nkind_In (Ref, N_Explicit_Dereference,
11244 N_Indexed_Component,
11245 N_Selected_Component,
11246 N_Slice)
11247 loop
11248 Ref := Prefix (Ref);
11249 end loop;
11250
11251 return
11252 Is_Entity_Name (Ref)
11253 and then Present (Entity (Ref))
11254 and then Is_Ghost_Entity (Entity (Ref));
11255 end Is_Ghost_Entity_Reference;
11256
11257 -- Local variables
11258
11259 Arg : Node_Id;
11260 Stmt : Node_Id;
11261
11262 -- Start of processing for Is_Ghost_Statement_Or_Pragma
11263
11264 begin
11265 if Nkind (N) = N_Pragma then
11266
11267 -- A pragma is Ghost when it appears within a Ghost package or
11268 -- subprogram.
11269
11270 if Within_Ghost_Scope then
11271 return True;
11272 end if;
11273
11274 -- A pragma is Ghost when it mentions a Ghost entity
11275
11276 Arg := First (Pragma_Argument_Associations (N));
11277 while Present (Arg) loop
11278 if Is_Ghost_Entity_Reference (Get_Pragma_Arg (Arg)) then
11279 return True;
11280 end if;
11281
11282 Next (Arg);
11283 end loop;
11284 end if;
11285
11286 Stmt := N;
11287 while Present (Stmt) loop
11288
11289 -- A statement is Ghost when it appears within a Ghost package or
11290 -- subprogram.
11291
11292 if Is_Statement (Stmt) and then Within_Ghost_Scope then
11293 return True;
11294
11295 -- An assignment statement is Ghost when the target is a Ghost
11296 -- variable. A procedure call is Ghost when the invoked procedure
11297 -- is Ghost.
11298
11299 elsif Nkind_In (Stmt, N_Assignment_Statement,
11300 N_Procedure_Call_Statement)
11301 then
11302 return Is_Ghost_Entity_Reference (Name (Stmt));
11303
11304 -- Prevent the search from going too far
11305
11306 elsif Is_Body_Or_Package_Declaration (Stmt) then
11307 return False;
11308 end if;
11309
11310 Stmt := Parent (Stmt);
11311 end loop;
11312
11313 return False;
11314 end Is_Ghost_Statement_Or_Pragma;
11315
11316 ----------------------------
11317 -- Is_Inherited_Operation --
11318 ----------------------------
11319
11320 function Is_Inherited_Operation (E : Entity_Id) return Boolean is
11321 pragma Assert (Is_Overloadable (E));
11322 Kind : constant Node_Kind := Nkind (Parent (E));
11323 begin
11324 return Kind = N_Full_Type_Declaration
11325 or else Kind = N_Private_Extension_Declaration
11326 or else Kind = N_Subtype_Declaration
11327 or else (Ekind (E) = E_Enumeration_Literal
11328 and then Is_Derived_Type (Etype (E)));
11329 end Is_Inherited_Operation;
11330
11331 -------------------------------------
11332 -- Is_Inherited_Operation_For_Type --
11333 -------------------------------------
11334
11335 function Is_Inherited_Operation_For_Type
11336 (E : Entity_Id;
11337 Typ : Entity_Id) return Boolean
11338 is
11339 begin
11340 -- Check that the operation has been created by the type declaration
11341
11342 return Is_Inherited_Operation (E)
11343 and then Defining_Identifier (Parent (E)) = Typ;
11344 end Is_Inherited_Operation_For_Type;
11345
11346 -----------------
11347 -- Is_Iterator --
11348 -----------------
11349
11350 function Is_Iterator (Typ : Entity_Id) return Boolean is
11351 Ifaces_List : Elist_Id;
11352 Iface_Elmt : Elmt_Id;
11353 Iface : Entity_Id;
11354
11355 begin
11356 if Is_Class_Wide_Type (Typ)
11357 and then Nam_In (Chars (Etype (Typ)), Name_Forward_Iterator,
11358 Name_Reversible_Iterator)
11359 and then
11360 Is_Predefined_File_Name
11361 (Unit_File_Name (Get_Source_Unit (Etype (Typ))))
11362 then
11363 return True;
11364
11365 elsif not Is_Tagged_Type (Typ) or else not Is_Derived_Type (Typ) then
11366 return False;
11367
11368 elsif Present (Find_Value_Of_Aspect (Typ, Aspect_Iterable)) then
11369 return True;
11370
11371 else
11372 Collect_Interfaces (Typ, Ifaces_List);
11373
11374 Iface_Elmt := First_Elmt (Ifaces_List);
11375 while Present (Iface_Elmt) loop
11376 Iface := Node (Iface_Elmt);
11377 if Chars (Iface) = Name_Forward_Iterator
11378 and then
11379 Is_Predefined_File_Name
11380 (Unit_File_Name (Get_Source_Unit (Iface)))
11381 then
11382 return True;
11383 end if;
11384
11385 Next_Elmt (Iface_Elmt);
11386 end loop;
11387
11388 return False;
11389 end if;
11390 end Is_Iterator;
11391
11392 ------------
11393 -- Is_LHS --
11394 ------------
11395
11396 -- We seem to have a lot of overlapping functions that do similar things
11397 -- (testing for left hand sides or lvalues???).
11398
11399 function Is_LHS (N : Node_Id) return Is_LHS_Result is
11400 P : constant Node_Id := Parent (N);
11401
11402 begin
11403 -- Return True if we are the left hand side of an assignment statement
11404
11405 if Nkind (P) = N_Assignment_Statement then
11406 if Name (P) = N then
11407 return Yes;
11408 else
11409 return No;
11410 end if;
11411
11412 -- Case of prefix of indexed or selected component or slice
11413
11414 elsif Nkind_In (P, N_Indexed_Component, N_Selected_Component, N_Slice)
11415 and then N = Prefix (P)
11416 then
11417 -- Here we have the case where the parent P is N.Q or N(Q .. R).
11418 -- If P is an LHS, then N is also effectively an LHS, but there
11419 -- is an important exception. If N is of an access type, then
11420 -- what we really have is N.all.Q (or N.all(Q .. R)). In either
11421 -- case this makes N.all a left hand side but not N itself.
11422
11423 -- If we don't know the type yet, this is the case where we return
11424 -- Unknown, since the answer depends on the type which is unknown.
11425
11426 if No (Etype (N)) then
11427 return Unknown;
11428
11429 -- We have an Etype set, so we can check it
11430
11431 elsif Is_Access_Type (Etype (N)) then
11432 return No;
11433
11434 -- OK, not access type case, so just test whole expression
11435
11436 else
11437 return Is_LHS (P);
11438 end if;
11439
11440 -- All other cases are not left hand sides
11441
11442 else
11443 return No;
11444 end if;
11445 end Is_LHS;
11446
11447 -----------------------------
11448 -- Is_Library_Level_Entity --
11449 -----------------------------
11450
11451 function Is_Library_Level_Entity (E : Entity_Id) return Boolean is
11452 begin
11453 -- The following is a small optimization, and it also properly handles
11454 -- discriminals, which in task bodies might appear in expressions before
11455 -- the corresponding procedure has been created, and which therefore do
11456 -- not have an assigned scope.
11457
11458 if Is_Formal (E) then
11459 return False;
11460 end if;
11461
11462 -- Normal test is simply that the enclosing dynamic scope is Standard
11463
11464 return Enclosing_Dynamic_Scope (E) = Standard_Standard;
11465 end Is_Library_Level_Entity;
11466
11467 --------------------------------
11468 -- Is_Limited_Class_Wide_Type --
11469 --------------------------------
11470
11471 function Is_Limited_Class_Wide_Type (Typ : Entity_Id) return Boolean is
11472 begin
11473 return
11474 Is_Class_Wide_Type (Typ)
11475 and then (Is_Limited_Type (Typ) or else From_Limited_With (Typ));
11476 end Is_Limited_Class_Wide_Type;
11477
11478 ---------------------------------
11479 -- Is_Local_Variable_Reference --
11480 ---------------------------------
11481
11482 function Is_Local_Variable_Reference (Expr : Node_Id) return Boolean is
11483 begin
11484 if not Is_Entity_Name (Expr) then
11485 return False;
11486
11487 else
11488 declare
11489 Ent : constant Entity_Id := Entity (Expr);
11490 Sub : constant Entity_Id := Enclosing_Subprogram (Ent);
11491 begin
11492 if not Ekind_In (Ent, E_Variable, E_In_Out_Parameter) then
11493 return False;
11494 else
11495 return Present (Sub) and then Sub = Current_Subprogram;
11496 end if;
11497 end;
11498 end if;
11499 end Is_Local_Variable_Reference;
11500
11501 -------------------------
11502 -- Is_Object_Reference --
11503 -------------------------
11504
11505 function Is_Object_Reference (N : Node_Id) return Boolean is
11506
11507 function Is_Internally_Generated_Renaming (N : Node_Id) return Boolean;
11508 -- Determine whether N is the name of an internally-generated renaming
11509
11510 --------------------------------------
11511 -- Is_Internally_Generated_Renaming --
11512 --------------------------------------
11513
11514 function Is_Internally_Generated_Renaming (N : Node_Id) return Boolean is
11515 P : Node_Id;
11516
11517 begin
11518 P := N;
11519 while Present (P) loop
11520 if Nkind (P) = N_Object_Renaming_Declaration then
11521 return not Comes_From_Source (P);
11522 elsif Is_List_Member (P) then
11523 return False;
11524 end if;
11525
11526 P := Parent (P);
11527 end loop;
11528
11529 return False;
11530 end Is_Internally_Generated_Renaming;
11531
11532 -- Start of processing for Is_Object_Reference
11533
11534 begin
11535 if Is_Entity_Name (N) then
11536 return Present (Entity (N)) and then Is_Object (Entity (N));
11537
11538 else
11539 case Nkind (N) is
11540 when N_Indexed_Component | N_Slice =>
11541 return
11542 Is_Object_Reference (Prefix (N))
11543 or else Is_Access_Type (Etype (Prefix (N)));
11544
11545 -- In Ada 95, a function call is a constant object; a procedure
11546 -- call is not.
11547
11548 when N_Function_Call =>
11549 return Etype (N) /= Standard_Void_Type;
11550
11551 -- Attributes 'Input, 'Old and 'Result produce objects
11552
11553 when N_Attribute_Reference =>
11554 return
11555 Nam_In
11556 (Attribute_Name (N), Name_Input, Name_Old, Name_Result);
11557
11558 when N_Selected_Component =>
11559 return
11560 Is_Object_Reference (Selector_Name (N))
11561 and then
11562 (Is_Object_Reference (Prefix (N))
11563 or else Is_Access_Type (Etype (Prefix (N))));
11564
11565 when N_Explicit_Dereference =>
11566 return True;
11567
11568 -- A view conversion of a tagged object is an object reference
11569
11570 when N_Type_Conversion =>
11571 return Is_Tagged_Type (Etype (Subtype_Mark (N)))
11572 and then Is_Tagged_Type (Etype (Expression (N)))
11573 and then Is_Object_Reference (Expression (N));
11574
11575 -- An unchecked type conversion is considered to be an object if
11576 -- the operand is an object (this construction arises only as a
11577 -- result of expansion activities).
11578
11579 when N_Unchecked_Type_Conversion =>
11580 return True;
11581
11582 -- Allow string literals to act as objects as long as they appear
11583 -- in internally-generated renamings. The expansion of iterators
11584 -- may generate such renamings when the range involves a string
11585 -- literal.
11586
11587 when N_String_Literal =>
11588 return Is_Internally_Generated_Renaming (Parent (N));
11589
11590 -- AI05-0003: In Ada 2012 a qualified expression is a name.
11591 -- This allows disambiguation of function calls and the use
11592 -- of aggregates in more contexts.
11593
11594 when N_Qualified_Expression =>
11595 if Ada_Version < Ada_2012 then
11596 return False;
11597 else
11598 return Is_Object_Reference (Expression (N))
11599 or else Nkind (Expression (N)) = N_Aggregate;
11600 end if;
11601
11602 when others =>
11603 return False;
11604 end case;
11605 end if;
11606 end Is_Object_Reference;
11607
11608 -----------------------------------
11609 -- Is_OK_Variable_For_Out_Formal --
11610 -----------------------------------
11611
11612 function Is_OK_Variable_For_Out_Formal (AV : Node_Id) return Boolean is
11613 begin
11614 Note_Possible_Modification (AV, Sure => True);
11615
11616 -- We must reject parenthesized variable names. Comes_From_Source is
11617 -- checked because there are currently cases where the compiler violates
11618 -- this rule (e.g. passing a task object to its controlled Initialize
11619 -- routine). This should be properly documented in sinfo???
11620
11621 if Paren_Count (AV) > 0 and then Comes_From_Source (AV) then
11622 return False;
11623
11624 -- A variable is always allowed
11625
11626 elsif Is_Variable (AV) then
11627 return True;
11628
11629 -- Generalized indexing operations are rewritten as explicit
11630 -- dereferences, and it is only during resolution that we can
11631 -- check whether the context requires an access_to_variable type.
11632
11633 elsif Nkind (AV) = N_Explicit_Dereference
11634 and then Ada_Version >= Ada_2012
11635 and then Nkind (Original_Node (AV)) = N_Indexed_Component
11636 and then Present (Etype (Original_Node (AV)))
11637 and then Has_Implicit_Dereference (Etype (Original_Node (AV)))
11638 then
11639 return not Is_Access_Constant (Etype (Prefix (AV)));
11640
11641 -- Unchecked conversions are allowed only if they come from the
11642 -- generated code, which sometimes uses unchecked conversions for out
11643 -- parameters in cases where code generation is unaffected. We tell
11644 -- source unchecked conversions by seeing if they are rewrites of
11645 -- an original Unchecked_Conversion function call, or of an explicit
11646 -- conversion of a function call or an aggregate (as may happen in the
11647 -- expansion of a packed array aggregate).
11648
11649 elsif Nkind (AV) = N_Unchecked_Type_Conversion then
11650 if Nkind_In (Original_Node (AV), N_Function_Call, N_Aggregate) then
11651 return False;
11652
11653 elsif Comes_From_Source (AV)
11654 and then Nkind (Original_Node (Expression (AV))) = N_Function_Call
11655 then
11656 return False;
11657
11658 elsif Nkind (Original_Node (AV)) = N_Type_Conversion then
11659 return Is_OK_Variable_For_Out_Formal (Expression (AV));
11660
11661 else
11662 return True;
11663 end if;
11664
11665 -- Normal type conversions are allowed if argument is a variable
11666
11667 elsif Nkind (AV) = N_Type_Conversion then
11668 if Is_Variable (Expression (AV))
11669 and then Paren_Count (Expression (AV)) = 0
11670 then
11671 Note_Possible_Modification (Expression (AV), Sure => True);
11672 return True;
11673
11674 -- We also allow a non-parenthesized expression that raises
11675 -- constraint error if it rewrites what used to be a variable
11676
11677 elsif Raises_Constraint_Error (Expression (AV))
11678 and then Paren_Count (Expression (AV)) = 0
11679 and then Is_Variable (Original_Node (Expression (AV)))
11680 then
11681 return True;
11682
11683 -- Type conversion of something other than a variable
11684
11685 else
11686 return False;
11687 end if;
11688
11689 -- If this node is rewritten, then test the original form, if that is
11690 -- OK, then we consider the rewritten node OK (for example, if the
11691 -- original node is a conversion, then Is_Variable will not be true
11692 -- but we still want to allow the conversion if it converts a variable).
11693
11694 elsif Original_Node (AV) /= AV then
11695
11696 -- In Ada 2012, the explicit dereference may be a rewritten call to a
11697 -- Reference function.
11698
11699 if Ada_Version >= Ada_2012
11700 and then Nkind (Original_Node (AV)) = N_Function_Call
11701 and then
11702 Has_Implicit_Dereference (Etype (Name (Original_Node (AV))))
11703 then
11704 return True;
11705
11706 else
11707 return Is_OK_Variable_For_Out_Formal (Original_Node (AV));
11708 end if;
11709
11710 -- All other non-variables are rejected
11711
11712 else
11713 return False;
11714 end if;
11715 end Is_OK_Variable_For_Out_Formal;
11716
11717 -----------------------------------
11718 -- Is_Partially_Initialized_Type --
11719 -----------------------------------
11720
11721 function Is_Partially_Initialized_Type
11722 (Typ : Entity_Id;
11723 Include_Implicit : Boolean := True) return Boolean
11724 is
11725 begin
11726 if Is_Scalar_Type (Typ) then
11727 return False;
11728
11729 elsif Is_Access_Type (Typ) then
11730 return Include_Implicit;
11731
11732 elsif Is_Array_Type (Typ) then
11733
11734 -- If component type is partially initialized, so is array type
11735
11736 if Is_Partially_Initialized_Type
11737 (Component_Type (Typ), Include_Implicit)
11738 then
11739 return True;
11740
11741 -- Otherwise we are only partially initialized if we are fully
11742 -- initialized (this is the empty array case, no point in us
11743 -- duplicating that code here).
11744
11745 else
11746 return Is_Fully_Initialized_Type (Typ);
11747 end if;
11748
11749 elsif Is_Record_Type (Typ) then
11750
11751 -- A discriminated type is always partially initialized if in
11752 -- all mode
11753
11754 if Has_Discriminants (Typ) and then Include_Implicit then
11755 return True;
11756
11757 -- A tagged type is always partially initialized
11758
11759 elsif Is_Tagged_Type (Typ) then
11760 return True;
11761
11762 -- Case of non-discriminated record
11763
11764 else
11765 declare
11766 Ent : Entity_Id;
11767
11768 Component_Present : Boolean := False;
11769 -- Set True if at least one component is present. If no
11770 -- components are present, then record type is fully
11771 -- initialized (another odd case, like the null array).
11772
11773 begin
11774 -- Loop through components
11775
11776 Ent := First_Entity (Typ);
11777 while Present (Ent) loop
11778 if Ekind (Ent) = E_Component then
11779 Component_Present := True;
11780
11781 -- If a component has an initialization expression then
11782 -- the enclosing record type is partially initialized
11783
11784 if Present (Parent (Ent))
11785 and then Present (Expression (Parent (Ent)))
11786 then
11787 return True;
11788
11789 -- If a component is of a type which is itself partially
11790 -- initialized, then the enclosing record type is also.
11791
11792 elsif Is_Partially_Initialized_Type
11793 (Etype (Ent), Include_Implicit)
11794 then
11795 return True;
11796 end if;
11797 end if;
11798
11799 Next_Entity (Ent);
11800 end loop;
11801
11802 -- No initialized components found. If we found any components
11803 -- they were all uninitialized so the result is false.
11804
11805 if Component_Present then
11806 return False;
11807
11808 -- But if we found no components, then all the components are
11809 -- initialized so we consider the type to be initialized.
11810
11811 else
11812 return True;
11813 end if;
11814 end;
11815 end if;
11816
11817 -- Concurrent types are always fully initialized
11818
11819 elsif Is_Concurrent_Type (Typ) then
11820 return True;
11821
11822 -- For a private type, go to underlying type. If there is no underlying
11823 -- type then just assume this partially initialized. Not clear if this
11824 -- can happen in a non-error case, but no harm in testing for this.
11825
11826 elsif Is_Private_Type (Typ) then
11827 declare
11828 U : constant Entity_Id := Underlying_Type (Typ);
11829 begin
11830 if No (U) then
11831 return True;
11832 else
11833 return Is_Partially_Initialized_Type (U, Include_Implicit);
11834 end if;
11835 end;
11836
11837 -- For any other type (are there any?) assume partially initialized
11838
11839 else
11840 return True;
11841 end if;
11842 end Is_Partially_Initialized_Type;
11843
11844 ------------------------------------
11845 -- Is_Potentially_Persistent_Type --
11846 ------------------------------------
11847
11848 function Is_Potentially_Persistent_Type (T : Entity_Id) return Boolean is
11849 Comp : Entity_Id;
11850 Indx : Node_Id;
11851
11852 begin
11853 -- For private type, test corresponding full type
11854
11855 if Is_Private_Type (T) then
11856 return Is_Potentially_Persistent_Type (Full_View (T));
11857
11858 -- Scalar types are potentially persistent
11859
11860 elsif Is_Scalar_Type (T) then
11861 return True;
11862
11863 -- Record type is potentially persistent if not tagged and the types of
11864 -- all it components are potentially persistent, and no component has
11865 -- an initialization expression.
11866
11867 elsif Is_Record_Type (T)
11868 and then not Is_Tagged_Type (T)
11869 and then not Is_Partially_Initialized_Type (T)
11870 then
11871 Comp := First_Component (T);
11872 while Present (Comp) loop
11873 if not Is_Potentially_Persistent_Type (Etype (Comp)) then
11874 return False;
11875 else
11876 Next_Entity (Comp);
11877 end if;
11878 end loop;
11879
11880 return True;
11881
11882 -- Array type is potentially persistent if its component type is
11883 -- potentially persistent and if all its constraints are static.
11884
11885 elsif Is_Array_Type (T) then
11886 if not Is_Potentially_Persistent_Type (Component_Type (T)) then
11887 return False;
11888 end if;
11889
11890 Indx := First_Index (T);
11891 while Present (Indx) loop
11892 if not Is_OK_Static_Subtype (Etype (Indx)) then
11893 return False;
11894 else
11895 Next_Index (Indx);
11896 end if;
11897 end loop;
11898
11899 return True;
11900
11901 -- All other types are not potentially persistent
11902
11903 else
11904 return False;
11905 end if;
11906 end Is_Potentially_Persistent_Type;
11907
11908 --------------------------------
11909 -- Is_Potentially_Unevaluated --
11910 --------------------------------
11911
11912 function Is_Potentially_Unevaluated (N : Node_Id) return Boolean is
11913 Par : Node_Id;
11914 Expr : Node_Id;
11915
11916 begin
11917 Expr := N;
11918 Par := Parent (N);
11919
11920 -- A postcondition whose expression is a short-circuit is broken down
11921 -- into individual aspects for better exception reporting. The original
11922 -- short-circuit expression is rewritten as the second operand, and an
11923 -- occurrence of 'Old in that operand is potentially unevaluated.
11924 -- See Sem_ch13.adb for details of this transformation.
11925
11926 if Nkind (Original_Node (Par)) = N_And_Then then
11927 return True;
11928 end if;
11929
11930 while not Nkind_In (Par, N_If_Expression,
11931 N_Case_Expression,
11932 N_And_Then,
11933 N_Or_Else,
11934 N_In,
11935 N_Not_In)
11936 loop
11937 Expr := Par;
11938 Par := Parent (Par);
11939
11940 -- If the context is not an expression, or if is the result of
11941 -- expansion of an enclosing construct (such as another attribute)
11942 -- the predicate does not apply.
11943
11944 if Nkind (Par) not in N_Subexpr
11945 or else not Comes_From_Source (Par)
11946 then
11947 return False;
11948 end if;
11949 end loop;
11950
11951 if Nkind (Par) = N_If_Expression then
11952 return Is_Elsif (Par) or else Expr /= First (Expressions (Par));
11953
11954 elsif Nkind (Par) = N_Case_Expression then
11955 return Expr /= Expression (Par);
11956
11957 elsif Nkind_In (Par, N_And_Then, N_Or_Else) then
11958 return Expr = Right_Opnd (Par);
11959
11960 elsif Nkind_In (Par, N_In, N_Not_In) then
11961 return Expr /= Left_Opnd (Par);
11962
11963 else
11964 return False;
11965 end if;
11966 end Is_Potentially_Unevaluated;
11967
11968 ---------------------------------
11969 -- Is_Protected_Self_Reference --
11970 ---------------------------------
11971
11972 function Is_Protected_Self_Reference (N : Node_Id) return Boolean is
11973
11974 function In_Access_Definition (N : Node_Id) return Boolean;
11975 -- Returns true if N belongs to an access definition
11976
11977 --------------------------
11978 -- In_Access_Definition --
11979 --------------------------
11980
11981 function In_Access_Definition (N : Node_Id) return Boolean is
11982 P : Node_Id;
11983
11984 begin
11985 P := Parent (N);
11986 while Present (P) loop
11987 if Nkind (P) = N_Access_Definition then
11988 return True;
11989 end if;
11990
11991 P := Parent (P);
11992 end loop;
11993
11994 return False;
11995 end In_Access_Definition;
11996
11997 -- Start of processing for Is_Protected_Self_Reference
11998
11999 begin
12000 -- Verify that prefix is analyzed and has the proper form. Note that
12001 -- the attributes Elab_Spec, Elab_Body, Elab_Subp_Body and UET_Address,
12002 -- which also produce the address of an entity, do not analyze their
12003 -- prefix because they denote entities that are not necessarily visible.
12004 -- Neither of them can apply to a protected type.
12005
12006 return Ada_Version >= Ada_2005
12007 and then Is_Entity_Name (N)
12008 and then Present (Entity (N))
12009 and then Is_Protected_Type (Entity (N))
12010 and then In_Open_Scopes (Entity (N))
12011 and then not In_Access_Definition (N);
12012 end Is_Protected_Self_Reference;
12013
12014 -----------------------------
12015 -- Is_RCI_Pkg_Spec_Or_Body --
12016 -----------------------------
12017
12018 function Is_RCI_Pkg_Spec_Or_Body (Cunit : Node_Id) return Boolean is
12019
12020 function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean;
12021 -- Return True if the unit of Cunit is an RCI package declaration
12022
12023 ---------------------------
12024 -- Is_RCI_Pkg_Decl_Cunit --
12025 ---------------------------
12026
12027 function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean is
12028 The_Unit : constant Node_Id := Unit (Cunit);
12029
12030 begin
12031 if Nkind (The_Unit) /= N_Package_Declaration then
12032 return False;
12033 end if;
12034
12035 return Is_Remote_Call_Interface (Defining_Entity (The_Unit));
12036 end Is_RCI_Pkg_Decl_Cunit;
12037
12038 -- Start of processing for Is_RCI_Pkg_Spec_Or_Body
12039
12040 begin
12041 return Is_RCI_Pkg_Decl_Cunit (Cunit)
12042 or else
12043 (Nkind (Unit (Cunit)) = N_Package_Body
12044 and then Is_RCI_Pkg_Decl_Cunit (Library_Unit (Cunit)));
12045 end Is_RCI_Pkg_Spec_Or_Body;
12046
12047 -----------------------------------------
12048 -- Is_Remote_Access_To_Class_Wide_Type --
12049 -----------------------------------------
12050
12051 function Is_Remote_Access_To_Class_Wide_Type
12052 (E : Entity_Id) return Boolean
12053 is
12054 begin
12055 -- A remote access to class-wide type is a general access to object type
12056 -- declared in the visible part of a Remote_Types or Remote_Call_
12057 -- Interface unit.
12058
12059 return Ekind (E) = E_General_Access_Type
12060 and then (Is_Remote_Call_Interface (E) or else Is_Remote_Types (E));
12061 end Is_Remote_Access_To_Class_Wide_Type;
12062
12063 -----------------------------------------
12064 -- Is_Remote_Access_To_Subprogram_Type --
12065 -----------------------------------------
12066
12067 function Is_Remote_Access_To_Subprogram_Type
12068 (E : Entity_Id) return Boolean
12069 is
12070 begin
12071 return (Ekind (E) = E_Access_Subprogram_Type
12072 or else (Ekind (E) = E_Record_Type
12073 and then Present (Corresponding_Remote_Type (E))))
12074 and then (Is_Remote_Call_Interface (E) or else Is_Remote_Types (E));
12075 end Is_Remote_Access_To_Subprogram_Type;
12076
12077 --------------------
12078 -- Is_Remote_Call --
12079 --------------------
12080
12081 function Is_Remote_Call (N : Node_Id) return Boolean is
12082 begin
12083 if Nkind (N) not in N_Subprogram_Call then
12084
12085 -- An entry call cannot be remote
12086
12087 return False;
12088
12089 elsif Nkind (Name (N)) in N_Has_Entity
12090 and then Is_Remote_Call_Interface (Entity (Name (N)))
12091 then
12092 -- A subprogram declared in the spec of a RCI package is remote
12093
12094 return True;
12095
12096 elsif Nkind (Name (N)) = N_Explicit_Dereference
12097 and then Is_Remote_Access_To_Subprogram_Type
12098 (Etype (Prefix (Name (N))))
12099 then
12100 -- The dereference of a RAS is a remote call
12101
12102 return True;
12103
12104 elsif Present (Controlling_Argument (N))
12105 and then Is_Remote_Access_To_Class_Wide_Type
12106 (Etype (Controlling_Argument (N)))
12107 then
12108 -- Any primitive operation call with a controlling argument of
12109 -- a RACW type is a remote call.
12110
12111 return True;
12112 end if;
12113
12114 -- All other calls are local calls
12115
12116 return False;
12117 end Is_Remote_Call;
12118
12119 ----------------------
12120 -- Is_Renamed_Entry --
12121 ----------------------
12122
12123 function Is_Renamed_Entry (Proc_Nam : Entity_Id) return Boolean is
12124 Orig_Node : Node_Id := Empty;
12125 Subp_Decl : Node_Id := Parent (Parent (Proc_Nam));
12126
12127 function Is_Entry (Nam : Node_Id) return Boolean;
12128 -- Determine whether Nam is an entry. Traverse selectors if there are
12129 -- nested selected components.
12130
12131 --------------
12132 -- Is_Entry --
12133 --------------
12134
12135 function Is_Entry (Nam : Node_Id) return Boolean is
12136 begin
12137 if Nkind (Nam) = N_Selected_Component then
12138 return Is_Entry (Selector_Name (Nam));
12139 end if;
12140
12141 return Ekind (Entity (Nam)) = E_Entry;
12142 end Is_Entry;
12143
12144 -- Start of processing for Is_Renamed_Entry
12145
12146 begin
12147 if Present (Alias (Proc_Nam)) then
12148 Subp_Decl := Parent (Parent (Alias (Proc_Nam)));
12149 end if;
12150
12151 -- Look for a rewritten subprogram renaming declaration
12152
12153 if Nkind (Subp_Decl) = N_Subprogram_Declaration
12154 and then Present (Original_Node (Subp_Decl))
12155 then
12156 Orig_Node := Original_Node (Subp_Decl);
12157 end if;
12158
12159 -- The rewritten subprogram is actually an entry
12160
12161 if Present (Orig_Node)
12162 and then Nkind (Orig_Node) = N_Subprogram_Renaming_Declaration
12163 and then Is_Entry (Name (Orig_Node))
12164 then
12165 return True;
12166 end if;
12167
12168 return False;
12169 end Is_Renamed_Entry;
12170
12171 ----------------------------
12172 -- Is_Reversible_Iterator --
12173 ----------------------------
12174
12175 function Is_Reversible_Iterator (Typ : Entity_Id) return Boolean is
12176 Ifaces_List : Elist_Id;
12177 Iface_Elmt : Elmt_Id;
12178 Iface : Entity_Id;
12179
12180 begin
12181 if Is_Class_Wide_Type (Typ)
12182 and then Chars (Etype (Typ)) = Name_Reversible_Iterator
12183 and then Is_Predefined_File_Name
12184 (Unit_File_Name (Get_Source_Unit (Etype (Typ))))
12185 then
12186 return True;
12187
12188 elsif not Is_Tagged_Type (Typ) or else not Is_Derived_Type (Typ) then
12189 return False;
12190
12191 else
12192 Collect_Interfaces (Typ, Ifaces_List);
12193
12194 Iface_Elmt := First_Elmt (Ifaces_List);
12195 while Present (Iface_Elmt) loop
12196 Iface := Node (Iface_Elmt);
12197 if Chars (Iface) = Name_Reversible_Iterator
12198 and then
12199 Is_Predefined_File_Name
12200 (Unit_File_Name (Get_Source_Unit (Iface)))
12201 then
12202 return True;
12203 end if;
12204
12205 Next_Elmt (Iface_Elmt);
12206 end loop;
12207 end if;
12208
12209 return False;
12210 end Is_Reversible_Iterator;
12211
12212 ----------------------
12213 -- Is_Selector_Name --
12214 ----------------------
12215
12216 function Is_Selector_Name (N : Node_Id) return Boolean is
12217 begin
12218 if not Is_List_Member (N) then
12219 declare
12220 P : constant Node_Id := Parent (N);
12221 begin
12222 return Nkind_In (P, N_Expanded_Name,
12223 N_Generic_Association,
12224 N_Parameter_Association,
12225 N_Selected_Component)
12226 and then Selector_Name (P) = N;
12227 end;
12228
12229 else
12230 declare
12231 L : constant List_Id := List_Containing (N);
12232 P : constant Node_Id := Parent (L);
12233 begin
12234 return (Nkind (P) = N_Discriminant_Association
12235 and then Selector_Names (P) = L)
12236 or else
12237 (Nkind (P) = N_Component_Association
12238 and then Choices (P) = L);
12239 end;
12240 end if;
12241 end Is_Selector_Name;
12242
12243 -------------------------------------
12244 -- Is_SPARK_05_Initialization_Expr --
12245 -------------------------------------
12246
12247 function Is_SPARK_05_Initialization_Expr (N : Node_Id) return Boolean is
12248 Is_Ok : Boolean;
12249 Expr : Node_Id;
12250 Comp_Assn : Node_Id;
12251 Orig_N : constant Node_Id := Original_Node (N);
12252
12253 begin
12254 Is_Ok := True;
12255
12256 if not Comes_From_Source (Orig_N) then
12257 goto Done;
12258 end if;
12259
12260 pragma Assert (Nkind (Orig_N) in N_Subexpr);
12261
12262 case Nkind (Orig_N) is
12263 when N_Character_Literal |
12264 N_Integer_Literal |
12265 N_Real_Literal |
12266 N_String_Literal =>
12267 null;
12268
12269 when N_Identifier |
12270 N_Expanded_Name =>
12271 if Is_Entity_Name (Orig_N)
12272 and then Present (Entity (Orig_N)) -- needed in some cases
12273 then
12274 case Ekind (Entity (Orig_N)) is
12275 when E_Constant |
12276 E_Enumeration_Literal |
12277 E_Named_Integer |
12278 E_Named_Real =>
12279 null;
12280 when others =>
12281 if Is_Type (Entity (Orig_N)) then
12282 null;
12283 else
12284 Is_Ok := False;
12285 end if;
12286 end case;
12287 end if;
12288
12289 when N_Qualified_Expression |
12290 N_Type_Conversion =>
12291 Is_Ok := Is_SPARK_05_Initialization_Expr (Expression (Orig_N));
12292
12293 when N_Unary_Op =>
12294 Is_Ok := Is_SPARK_05_Initialization_Expr (Right_Opnd (Orig_N));
12295
12296 when N_Binary_Op |
12297 N_Short_Circuit |
12298 N_Membership_Test =>
12299 Is_Ok := Is_SPARK_05_Initialization_Expr (Left_Opnd (Orig_N))
12300 and then
12301 Is_SPARK_05_Initialization_Expr (Right_Opnd (Orig_N));
12302
12303 when N_Aggregate |
12304 N_Extension_Aggregate =>
12305 if Nkind (Orig_N) = N_Extension_Aggregate then
12306 Is_Ok :=
12307 Is_SPARK_05_Initialization_Expr (Ancestor_Part (Orig_N));
12308 end if;
12309
12310 Expr := First (Expressions (Orig_N));
12311 while Present (Expr) loop
12312 if not Is_SPARK_05_Initialization_Expr (Expr) then
12313 Is_Ok := False;
12314 goto Done;
12315 end if;
12316
12317 Next (Expr);
12318 end loop;
12319
12320 Comp_Assn := First (Component_Associations (Orig_N));
12321 while Present (Comp_Assn) loop
12322 Expr := Expression (Comp_Assn);
12323
12324 -- Note: test for Present here needed for box assocation
12325
12326 if Present (Expr)
12327 and then not Is_SPARK_05_Initialization_Expr (Expr)
12328 then
12329 Is_Ok := False;
12330 goto Done;
12331 end if;
12332
12333 Next (Comp_Assn);
12334 end loop;
12335
12336 when N_Attribute_Reference =>
12337 if Nkind (Prefix (Orig_N)) in N_Subexpr then
12338 Is_Ok := Is_SPARK_05_Initialization_Expr (Prefix (Orig_N));
12339 end if;
12340
12341 Expr := First (Expressions (Orig_N));
12342 while Present (Expr) loop
12343 if not Is_SPARK_05_Initialization_Expr (Expr) then
12344 Is_Ok := False;
12345 goto Done;
12346 end if;
12347
12348 Next (Expr);
12349 end loop;
12350
12351 -- Selected components might be expanded named not yet resolved, so
12352 -- default on the safe side. (Eg on sparklex.ads)
12353
12354 when N_Selected_Component =>
12355 null;
12356
12357 when others =>
12358 Is_Ok := False;
12359 end case;
12360
12361 <<Done>>
12362 return Is_Ok;
12363 end Is_SPARK_05_Initialization_Expr;
12364
12365 ----------------------------------
12366 -- Is_SPARK_05_Object_Reference --
12367 ----------------------------------
12368
12369 function Is_SPARK_05_Object_Reference (N : Node_Id) return Boolean is
12370 begin
12371 if Is_Entity_Name (N) then
12372 return Present (Entity (N))
12373 and then
12374 (Ekind_In (Entity (N), E_Constant, E_Variable)
12375 or else Ekind (Entity (N)) in Formal_Kind);
12376
12377 else
12378 case Nkind (N) is
12379 when N_Selected_Component =>
12380 return Is_SPARK_05_Object_Reference (Prefix (N));
12381
12382 when others =>
12383 return False;
12384 end case;
12385 end if;
12386 end Is_SPARK_05_Object_Reference;
12387
12388 -----------------------------
12389 -- Is_Specific_Tagged_Type --
12390 -----------------------------
12391
12392 function Is_Specific_Tagged_Type (Typ : Entity_Id) return Boolean is
12393 Full_Typ : Entity_Id;
12394
12395 begin
12396 -- Handle private types
12397
12398 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
12399 Full_Typ := Full_View (Typ);
12400 else
12401 Full_Typ := Typ;
12402 end if;
12403
12404 -- A specific tagged type is a non-class-wide tagged type
12405
12406 return Is_Tagged_Type (Full_Typ) and not Is_Class_Wide_Type (Full_Typ);
12407 end Is_Specific_Tagged_Type;
12408
12409 ------------------
12410 -- Is_Statement --
12411 ------------------
12412
12413 function Is_Statement (N : Node_Id) return Boolean is
12414 begin
12415 return
12416 Nkind (N) in N_Statement_Other_Than_Procedure_Call
12417 or else Nkind (N) = N_Procedure_Call_Statement;
12418 end Is_Statement;
12419
12420 -------------------------
12421 -- Is_Subject_To_Ghost --
12422 -------------------------
12423
12424 function Is_Subject_To_Ghost (N : Node_Id) return Boolean is
12425 function Enables_Ghostness (Arg : Node_Id) return Boolean;
12426 -- Determine whether aspect or pragma argument Arg enables "ghostness"
12427
12428 -----------------------
12429 -- Enables_Ghostness --
12430 -----------------------
12431
12432 function Enables_Ghostness (Arg : Node_Id) return Boolean is
12433 Expr : Node_Id;
12434
12435 begin
12436 Expr := Arg;
12437
12438 if Nkind (Expr) = N_Pragma_Argument_Association then
12439 Expr := Get_Pragma_Arg (Expr);
12440 end if;
12441
12442 -- Determine whether the expression of the aspect is static and
12443 -- denotes True.
12444
12445 if Present (Expr) then
12446 Preanalyze_And_Resolve (Expr);
12447
12448 return
12449 Is_OK_Static_Expression (Expr)
12450 and then Is_True (Expr_Value (Expr));
12451
12452 -- Otherwise Ghost defaults to True
12453
12454 else
12455 return True;
12456 end if;
12457 end Enables_Ghostness;
12458
12459 -- Local variables
12460
12461 Id : constant Entity_Id := Defining_Entity (N);
12462 Asp : Node_Id;
12463 Decl : Node_Id;
12464 Prev_Id : Entity_Id;
12465
12466 -- Start of processing for Is_Subject_To_Ghost
12467
12468 begin
12469 if Is_Ghost_Entity (Id) then
12470 return True;
12471
12472 -- The completion of a type or a constant is not fully analyzed when the
12473 -- reference to the Ghost entity is resolved. Because the completion is
12474 -- not marked as Ghost yet, inspect the partial view.
12475
12476 elsif Is_Record_Type (Id)
12477 or else Ekind (Id) = E_Constant
12478 or else (Nkind (N) = N_Object_Declaration
12479 and then Constant_Present (N))
12480 then
12481 Prev_Id := Incomplete_Or_Partial_View (Id);
12482
12483 if Present (Prev_Id) and then Is_Ghost_Entity (Prev_Id) then
12484 return True;
12485 end if;
12486 end if;
12487
12488 -- Examine the aspect specifications (if any) looking for aspect Ghost
12489
12490 if Permits_Aspect_Specifications (N) then
12491 Asp := First (Aspect_Specifications (N));
12492 while Present (Asp) loop
12493 if Chars (Identifier (Asp)) = Name_Ghost then
12494 return Enables_Ghostness (Expression (Asp));
12495 end if;
12496
12497 Next (Asp);
12498 end loop;
12499 end if;
12500
12501 Decl := Empty;
12502
12503 -- When the context is a [generic] package declaration, pragma Ghost
12504 -- resides in the visible declarations.
12505
12506 if Nkind_In (N, N_Generic_Package_Declaration,
12507 N_Package_Declaration)
12508 then
12509 Decl := First (Visible_Declarations (Specification (N)));
12510
12511 -- Otherwise pragma Ghost appears in the declarations following N
12512
12513 elsif Is_List_Member (N) then
12514 Decl := Next (N);
12515 end if;
12516
12517 while Present (Decl) loop
12518 if Nkind (Decl) = N_Pragma
12519 and then Pragma_Name (Decl) = Name_Ghost
12520 then
12521 return
12522 Enables_Ghostness (First (Pragma_Argument_Associations (Decl)));
12523
12524 -- A source construct ends the region where pragma Ghost may appear,
12525 -- stop the traversal.
12526
12527 elsif Comes_From_Source (Decl) then
12528 exit;
12529 end if;
12530
12531 Next (Decl);
12532 end loop;
12533
12534 return False;
12535 end Is_Subject_To_Ghost;
12536
12537 --------------------------------------------------
12538 -- Is_Subprogram_Stub_Without_Prior_Declaration --
12539 --------------------------------------------------
12540
12541 function Is_Subprogram_Stub_Without_Prior_Declaration
12542 (N : Node_Id) return Boolean
12543 is
12544 begin
12545 -- A subprogram stub without prior declaration serves as declaration for
12546 -- the actual subprogram body. As such, it has an attached defining
12547 -- entity of E_[Generic_]Function or E_[Generic_]Procedure.
12548
12549 return Nkind (N) = N_Subprogram_Body_Stub
12550 and then Ekind (Defining_Entity (N)) /= E_Subprogram_Body;
12551 end Is_Subprogram_Stub_Without_Prior_Declaration;
12552
12553 ---------------------------------
12554 -- Is_Synchronized_Tagged_Type --
12555 ---------------------------------
12556
12557 function Is_Synchronized_Tagged_Type (E : Entity_Id) return Boolean is
12558 Kind : constant Entity_Kind := Ekind (Base_Type (E));
12559
12560 begin
12561 -- A task or protected type derived from an interface is a tagged type.
12562 -- Such a tagged type is called a synchronized tagged type, as are
12563 -- synchronized interfaces and private extensions whose declaration
12564 -- includes the reserved word synchronized.
12565
12566 return (Is_Tagged_Type (E)
12567 and then (Kind = E_Task_Type
12568 or else
12569 Kind = E_Protected_Type))
12570 or else
12571 (Is_Interface (E)
12572 and then Is_Synchronized_Interface (E))
12573 or else
12574 (Ekind (E) = E_Record_Type_With_Private
12575 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
12576 and then (Synchronized_Present (Parent (E))
12577 or else Is_Synchronized_Interface (Etype (E))));
12578 end Is_Synchronized_Tagged_Type;
12579
12580 -----------------
12581 -- Is_Transfer --
12582 -----------------
12583
12584 function Is_Transfer (N : Node_Id) return Boolean is
12585 Kind : constant Node_Kind := Nkind (N);
12586
12587 begin
12588 if Kind = N_Simple_Return_Statement
12589 or else
12590 Kind = N_Extended_Return_Statement
12591 or else
12592 Kind = N_Goto_Statement
12593 or else
12594 Kind = N_Raise_Statement
12595 or else
12596 Kind = N_Requeue_Statement
12597 then
12598 return True;
12599
12600 elsif (Kind = N_Exit_Statement or else Kind in N_Raise_xxx_Error)
12601 and then No (Condition (N))
12602 then
12603 return True;
12604
12605 elsif Kind = N_Procedure_Call_Statement
12606 and then Is_Entity_Name (Name (N))
12607 and then Present (Entity (Name (N)))
12608 and then No_Return (Entity (Name (N)))
12609 then
12610 return True;
12611
12612 elsif Nkind (Original_Node (N)) = N_Raise_Statement then
12613 return True;
12614
12615 else
12616 return False;
12617 end if;
12618 end Is_Transfer;
12619
12620 -------------
12621 -- Is_True --
12622 -------------
12623
12624 function Is_True (U : Uint) return Boolean is
12625 begin
12626 return (U /= 0);
12627 end Is_True;
12628
12629 --------------------------------------
12630 -- Is_Unchecked_Conversion_Instance --
12631 --------------------------------------
12632
12633 function Is_Unchecked_Conversion_Instance (Id : Entity_Id) return Boolean is
12634 Gen_Par : Entity_Id;
12635
12636 begin
12637 -- Look for a function whose generic parent is the predefined intrinsic
12638 -- function Unchecked_Conversion.
12639
12640 if Ekind (Id) = E_Function then
12641 Gen_Par := Generic_Parent (Parent (Id));
12642
12643 return
12644 Present (Gen_Par)
12645 and then Chars (Gen_Par) = Name_Unchecked_Conversion
12646 and then Is_Intrinsic_Subprogram (Gen_Par)
12647 and then Is_Predefined_File_Name
12648 (Unit_File_Name (Get_Source_Unit (Gen_Par)));
12649 end if;
12650
12651 return False;
12652 end Is_Unchecked_Conversion_Instance;
12653
12654 -------------------------------
12655 -- Is_Universal_Numeric_Type --
12656 -------------------------------
12657
12658 function Is_Universal_Numeric_Type (T : Entity_Id) return Boolean is
12659 begin
12660 return T = Universal_Integer or else T = Universal_Real;
12661 end Is_Universal_Numeric_Type;
12662
12663 -------------------
12664 -- Is_Value_Type --
12665 -------------------
12666
12667 function Is_Value_Type (T : Entity_Id) return Boolean is
12668 begin
12669 return VM_Target = CLI_Target
12670 and then Nkind (T) in N_Has_Chars
12671 and then Chars (T) /= No_Name
12672 and then Get_Name_String (Chars (T)) = "valuetype";
12673 end Is_Value_Type;
12674
12675 ----------------------------
12676 -- Is_Variable_Size_Array --
12677 ----------------------------
12678
12679 function Is_Variable_Size_Array (E : Entity_Id) return Boolean is
12680 Idx : Node_Id;
12681
12682 begin
12683 pragma Assert (Is_Array_Type (E));
12684
12685 -- Check if some index is initialized with a non-constant value
12686
12687 Idx := First_Index (E);
12688 while Present (Idx) loop
12689 if Nkind (Idx) = N_Range then
12690 if not Is_Constant_Bound (Low_Bound (Idx))
12691 or else not Is_Constant_Bound (High_Bound (Idx))
12692 then
12693 return True;
12694 end if;
12695 end if;
12696
12697 Idx := Next_Index (Idx);
12698 end loop;
12699
12700 return False;
12701 end Is_Variable_Size_Array;
12702
12703 -----------------------------
12704 -- Is_Variable_Size_Record --
12705 -----------------------------
12706
12707 function Is_Variable_Size_Record (E : Entity_Id) return Boolean is
12708 Comp : Entity_Id;
12709 Comp_Typ : Entity_Id;
12710
12711 begin
12712 pragma Assert (Is_Record_Type (E));
12713
12714 Comp := First_Entity (E);
12715 while Present (Comp) loop
12716 Comp_Typ := Etype (Comp);
12717
12718 -- Recursive call if the record type has discriminants
12719
12720 if Is_Record_Type (Comp_Typ)
12721 and then Has_Discriminants (Comp_Typ)
12722 and then Is_Variable_Size_Record (Comp_Typ)
12723 then
12724 return True;
12725
12726 elsif Is_Array_Type (Comp_Typ)
12727 and then Is_Variable_Size_Array (Comp_Typ)
12728 then
12729 return True;
12730 end if;
12731
12732 Next_Entity (Comp);
12733 end loop;
12734
12735 return False;
12736 end Is_Variable_Size_Record;
12737
12738 -----------------
12739 -- Is_Variable --
12740 -----------------
12741
12742 function Is_Variable
12743 (N : Node_Id;
12744 Use_Original_Node : Boolean := True) return Boolean
12745 is
12746 Orig_Node : Node_Id;
12747
12748 function In_Protected_Function (E : Entity_Id) return Boolean;
12749 -- Within a protected function, the private components of the enclosing
12750 -- protected type are constants. A function nested within a (protected)
12751 -- procedure is not itself protected. Within the body of a protected
12752 -- function the current instance of the protected type is a constant.
12753
12754 function Is_Variable_Prefix (P : Node_Id) return Boolean;
12755 -- Prefixes can involve implicit dereferences, in which case we must
12756 -- test for the case of a reference of a constant access type, which can
12757 -- can never be a variable.
12758
12759 ---------------------------
12760 -- In_Protected_Function --
12761 ---------------------------
12762
12763 function In_Protected_Function (E : Entity_Id) return Boolean is
12764 Prot : Entity_Id;
12765 S : Entity_Id;
12766
12767 begin
12768 -- E is the current instance of a type
12769
12770 if Is_Type (E) then
12771 Prot := E;
12772
12773 -- E is an object
12774
12775 else
12776 Prot := Scope (E);
12777 end if;
12778
12779 if not Is_Protected_Type (Prot) then
12780 return False;
12781
12782 else
12783 S := Current_Scope;
12784 while Present (S) and then S /= Prot loop
12785 if Ekind (S) = E_Function and then Scope (S) = Prot then
12786 return True;
12787 end if;
12788
12789 S := Scope (S);
12790 end loop;
12791
12792 return False;
12793 end if;
12794 end In_Protected_Function;
12795
12796 ------------------------
12797 -- Is_Variable_Prefix --
12798 ------------------------
12799
12800 function Is_Variable_Prefix (P : Node_Id) return Boolean is
12801 begin
12802 if Is_Access_Type (Etype (P)) then
12803 return not Is_Access_Constant (Root_Type (Etype (P)));
12804
12805 -- For the case of an indexed component whose prefix has a packed
12806 -- array type, the prefix has been rewritten into a type conversion.
12807 -- Determine variable-ness from the converted expression.
12808
12809 elsif Nkind (P) = N_Type_Conversion
12810 and then not Comes_From_Source (P)
12811 and then Is_Array_Type (Etype (P))
12812 and then Is_Packed (Etype (P))
12813 then
12814 return Is_Variable (Expression (P));
12815
12816 else
12817 return Is_Variable (P);
12818 end if;
12819 end Is_Variable_Prefix;
12820
12821 -- Start of processing for Is_Variable
12822
12823 begin
12824 -- Check if we perform the test on the original node since this may be a
12825 -- test of syntactic categories which must not be disturbed by whatever
12826 -- rewriting might have occurred. For example, an aggregate, which is
12827 -- certainly NOT a variable, could be turned into a variable by
12828 -- expansion.
12829
12830 if Use_Original_Node then
12831 Orig_Node := Original_Node (N);
12832 else
12833 Orig_Node := N;
12834 end if;
12835
12836 -- Definitely OK if Assignment_OK is set. Since this is something that
12837 -- only gets set for expanded nodes, the test is on N, not Orig_Node.
12838
12839 if Nkind (N) in N_Subexpr and then Assignment_OK (N) then
12840 return True;
12841
12842 -- Normally we go to the original node, but there is one exception where
12843 -- we use the rewritten node, namely when it is an explicit dereference.
12844 -- The generated code may rewrite a prefix which is an access type with
12845 -- an explicit dereference. The dereference is a variable, even though
12846 -- the original node may not be (since it could be a constant of the
12847 -- access type).
12848
12849 -- In Ada 2005 we have a further case to consider: the prefix may be a
12850 -- function call given in prefix notation. The original node appears to
12851 -- be a selected component, but we need to examine the call.
12852
12853 elsif Nkind (N) = N_Explicit_Dereference
12854 and then Nkind (Orig_Node) /= N_Explicit_Dereference
12855 and then Present (Etype (Orig_Node))
12856 and then Is_Access_Type (Etype (Orig_Node))
12857 then
12858 -- Note that if the prefix is an explicit dereference that does not
12859 -- come from source, we must check for a rewritten function call in
12860 -- prefixed notation before other forms of rewriting, to prevent a
12861 -- compiler crash.
12862
12863 return
12864 (Nkind (Orig_Node) = N_Function_Call
12865 and then not Is_Access_Constant (Etype (Prefix (N))))
12866 or else
12867 Is_Variable_Prefix (Original_Node (Prefix (N)));
12868
12869 -- in Ada 2012, the dereference may have been added for a type with
12870 -- a declared implicit dereference aspect. Check that it is not an
12871 -- access to constant.
12872
12873 elsif Nkind (N) = N_Explicit_Dereference
12874 and then Present (Etype (Orig_Node))
12875 and then Ada_Version >= Ada_2012
12876 and then Has_Implicit_Dereference (Etype (Orig_Node))
12877 then
12878 return not Is_Access_Constant (Etype (Prefix (N)));
12879
12880 -- A function call is never a variable
12881
12882 elsif Nkind (N) = N_Function_Call then
12883 return False;
12884
12885 -- All remaining checks use the original node
12886
12887 elsif Is_Entity_Name (Orig_Node)
12888 and then Present (Entity (Orig_Node))
12889 then
12890 declare
12891 E : constant Entity_Id := Entity (Orig_Node);
12892 K : constant Entity_Kind := Ekind (E);
12893
12894 begin
12895 return (K = E_Variable
12896 and then Nkind (Parent (E)) /= N_Exception_Handler)
12897 or else (K = E_Component
12898 and then not In_Protected_Function (E))
12899 or else K = E_Out_Parameter
12900 or else K = E_In_Out_Parameter
12901 or else K = E_Generic_In_Out_Parameter
12902
12903 -- Current instance of type. If this is a protected type, check
12904 -- we are not within the body of one of its protected functions.
12905
12906 or else (Is_Type (E)
12907 and then In_Open_Scopes (E)
12908 and then not In_Protected_Function (E))
12909
12910 or else (Is_Incomplete_Or_Private_Type (E)
12911 and then In_Open_Scopes (Full_View (E)));
12912 end;
12913
12914 else
12915 case Nkind (Orig_Node) is
12916 when N_Indexed_Component | N_Slice =>
12917 return Is_Variable_Prefix (Prefix (Orig_Node));
12918
12919 when N_Selected_Component =>
12920 return (Is_Variable (Selector_Name (Orig_Node))
12921 and then Is_Variable_Prefix (Prefix (Orig_Node)))
12922 or else
12923 (Nkind (N) = N_Expanded_Name
12924 and then Scope (Entity (N)) = Entity (Prefix (N)));
12925
12926 -- For an explicit dereference, the type of the prefix cannot
12927 -- be an access to constant or an access to subprogram.
12928
12929 when N_Explicit_Dereference =>
12930 declare
12931 Typ : constant Entity_Id := Etype (Prefix (Orig_Node));
12932 begin
12933 return Is_Access_Type (Typ)
12934 and then not Is_Access_Constant (Root_Type (Typ))
12935 and then Ekind (Typ) /= E_Access_Subprogram_Type;
12936 end;
12937
12938 -- The type conversion is the case where we do not deal with the
12939 -- context dependent special case of an actual parameter. Thus
12940 -- the type conversion is only considered a variable for the
12941 -- purposes of this routine if the target type is tagged. However,
12942 -- a type conversion is considered to be a variable if it does not
12943 -- come from source (this deals for example with the conversions
12944 -- of expressions to their actual subtypes).
12945
12946 when N_Type_Conversion =>
12947 return Is_Variable (Expression (Orig_Node))
12948 and then
12949 (not Comes_From_Source (Orig_Node)
12950 or else
12951 (Is_Tagged_Type (Etype (Subtype_Mark (Orig_Node)))
12952 and then
12953 Is_Tagged_Type (Etype (Expression (Orig_Node)))));
12954
12955 -- GNAT allows an unchecked type conversion as a variable. This
12956 -- only affects the generation of internal expanded code, since
12957 -- calls to instantiations of Unchecked_Conversion are never
12958 -- considered variables (since they are function calls).
12959
12960 when N_Unchecked_Type_Conversion =>
12961 return Is_Variable (Expression (Orig_Node));
12962
12963 when others =>
12964 return False;
12965 end case;
12966 end if;
12967 end Is_Variable;
12968
12969 ---------------------------
12970 -- Is_Visibly_Controlled --
12971 ---------------------------
12972
12973 function Is_Visibly_Controlled (T : Entity_Id) return Boolean is
12974 Root : constant Entity_Id := Root_Type (T);
12975 begin
12976 return Chars (Scope (Root)) = Name_Finalization
12977 and then Chars (Scope (Scope (Root))) = Name_Ada
12978 and then Scope (Scope (Scope (Root))) = Standard_Standard;
12979 end Is_Visibly_Controlled;
12980
12981 ------------------------
12982 -- Is_Volatile_Object --
12983 ------------------------
12984
12985 function Is_Volatile_Object (N : Node_Id) return Boolean is
12986
12987 function Is_Volatile_Prefix (N : Node_Id) return Boolean;
12988 -- If prefix is an implicit dereference, examine designated type
12989
12990 function Object_Has_Volatile_Components (N : Node_Id) return Boolean;
12991 -- Determines if given object has volatile components
12992
12993 ------------------------
12994 -- Is_Volatile_Prefix --
12995 ------------------------
12996
12997 function Is_Volatile_Prefix (N : Node_Id) return Boolean is
12998 Typ : constant Entity_Id := Etype (N);
12999
13000 begin
13001 if Is_Access_Type (Typ) then
13002 declare
13003 Dtyp : constant Entity_Id := Designated_Type (Typ);
13004
13005 begin
13006 return Is_Volatile (Dtyp)
13007 or else Has_Volatile_Components (Dtyp);
13008 end;
13009
13010 else
13011 return Object_Has_Volatile_Components (N);
13012 end if;
13013 end Is_Volatile_Prefix;
13014
13015 ------------------------------------
13016 -- Object_Has_Volatile_Components --
13017 ------------------------------------
13018
13019 function Object_Has_Volatile_Components (N : Node_Id) return Boolean is
13020 Typ : constant Entity_Id := Etype (N);
13021
13022 begin
13023 if Is_Volatile (Typ)
13024 or else Has_Volatile_Components (Typ)
13025 then
13026 return True;
13027
13028 elsif Is_Entity_Name (N)
13029 and then (Has_Volatile_Components (Entity (N))
13030 or else Is_Volatile (Entity (N)))
13031 then
13032 return True;
13033
13034 elsif Nkind (N) = N_Indexed_Component
13035 or else Nkind (N) = N_Selected_Component
13036 then
13037 return Is_Volatile_Prefix (Prefix (N));
13038
13039 else
13040 return False;
13041 end if;
13042 end Object_Has_Volatile_Components;
13043
13044 -- Start of processing for Is_Volatile_Object
13045
13046 begin
13047 if Nkind (N) = N_Defining_Identifier then
13048 return Is_Volatile (N) or else Is_Volatile (Etype (N));
13049
13050 elsif Nkind (N) = N_Expanded_Name then
13051 return Is_Volatile_Object (Entity (N));
13052
13053 elsif Is_Volatile (Etype (N))
13054 or else (Is_Entity_Name (N) and then Is_Volatile (Entity (N)))
13055 then
13056 return True;
13057
13058 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component)
13059 and then Is_Volatile_Prefix (Prefix (N))
13060 then
13061 return True;
13062
13063 elsif Nkind (N) = N_Selected_Component
13064 and then Is_Volatile (Entity (Selector_Name (N)))
13065 then
13066 return True;
13067
13068 else
13069 return False;
13070 end if;
13071 end Is_Volatile_Object;
13072
13073 ---------------------------
13074 -- Itype_Has_Declaration --
13075 ---------------------------
13076
13077 function Itype_Has_Declaration (Id : Entity_Id) return Boolean is
13078 begin
13079 pragma Assert (Is_Itype (Id));
13080 return Present (Parent (Id))
13081 and then Nkind_In (Parent (Id), N_Full_Type_Declaration,
13082 N_Subtype_Declaration)
13083 and then Defining_Entity (Parent (Id)) = Id;
13084 end Itype_Has_Declaration;
13085
13086 -------------------------
13087 -- Kill_Current_Values --
13088 -------------------------
13089
13090 procedure Kill_Current_Values
13091 (Ent : Entity_Id;
13092 Last_Assignment_Only : Boolean := False)
13093 is
13094 begin
13095 if Is_Assignable (Ent) then
13096 Set_Last_Assignment (Ent, Empty);
13097 end if;
13098
13099 if Is_Object (Ent) then
13100 if not Last_Assignment_Only then
13101 Kill_Checks (Ent);
13102 Set_Current_Value (Ent, Empty);
13103
13104 if not Can_Never_Be_Null (Ent) then
13105 Set_Is_Known_Non_Null (Ent, False);
13106 end if;
13107
13108 Set_Is_Known_Null (Ent, False);
13109
13110 -- Reset Is_Known_Valid unless type is always valid, or if we have
13111 -- a loop parameter (loop parameters are always valid, since their
13112 -- bounds are defined by the bounds given in the loop header).
13113
13114 if not Is_Known_Valid (Etype (Ent))
13115 and then Ekind (Ent) /= E_Loop_Parameter
13116 then
13117 Set_Is_Known_Valid (Ent, False);
13118 end if;
13119 end if;
13120 end if;
13121 end Kill_Current_Values;
13122
13123 procedure Kill_Current_Values (Last_Assignment_Only : Boolean := False) is
13124 S : Entity_Id;
13125
13126 procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id);
13127 -- Clear current value for entity E and all entities chained to E
13128
13129 ------------------------------------------
13130 -- Kill_Current_Values_For_Entity_Chain --
13131 ------------------------------------------
13132
13133 procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id) is
13134 Ent : Entity_Id;
13135 begin
13136 Ent := E;
13137 while Present (Ent) loop
13138 Kill_Current_Values (Ent, Last_Assignment_Only);
13139 Next_Entity (Ent);
13140 end loop;
13141 end Kill_Current_Values_For_Entity_Chain;
13142
13143 -- Start of processing for Kill_Current_Values
13144
13145 begin
13146 -- Kill all saved checks, a special case of killing saved values
13147
13148 if not Last_Assignment_Only then
13149 Kill_All_Checks;
13150 end if;
13151
13152 -- Loop through relevant scopes, which includes the current scope and
13153 -- any parent scopes if the current scope is a block or a package.
13154
13155 S := Current_Scope;
13156 Scope_Loop : loop
13157
13158 -- Clear current values of all entities in current scope
13159
13160 Kill_Current_Values_For_Entity_Chain (First_Entity (S));
13161
13162 -- If scope is a package, also clear current values of all private
13163 -- entities in the scope.
13164
13165 if Is_Package_Or_Generic_Package (S)
13166 or else Is_Concurrent_Type (S)
13167 then
13168 Kill_Current_Values_For_Entity_Chain (First_Private_Entity (S));
13169 end if;
13170
13171 -- If this is a not a subprogram, deal with parents
13172
13173 if not Is_Subprogram (S) then
13174 S := Scope (S);
13175 exit Scope_Loop when S = Standard_Standard;
13176 else
13177 exit Scope_Loop;
13178 end if;
13179 end loop Scope_Loop;
13180 end Kill_Current_Values;
13181
13182 --------------------------
13183 -- Kill_Size_Check_Code --
13184 --------------------------
13185
13186 procedure Kill_Size_Check_Code (E : Entity_Id) is
13187 begin
13188 if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
13189 and then Present (Size_Check_Code (E))
13190 then
13191 Remove (Size_Check_Code (E));
13192 Set_Size_Check_Code (E, Empty);
13193 end if;
13194 end Kill_Size_Check_Code;
13195
13196 --------------------------
13197 -- Known_To_Be_Assigned --
13198 --------------------------
13199
13200 function Known_To_Be_Assigned (N : Node_Id) return Boolean is
13201 P : constant Node_Id := Parent (N);
13202
13203 begin
13204 case Nkind (P) is
13205
13206 -- Test left side of assignment
13207
13208 when N_Assignment_Statement =>
13209 return N = Name (P);
13210
13211 -- Function call arguments are never lvalues
13212
13213 when N_Function_Call =>
13214 return False;
13215
13216 -- Positional parameter for procedure or accept call
13217
13218 when N_Procedure_Call_Statement |
13219 N_Accept_Statement
13220 =>
13221 declare
13222 Proc : Entity_Id;
13223 Form : Entity_Id;
13224 Act : Node_Id;
13225
13226 begin
13227 Proc := Get_Subprogram_Entity (P);
13228
13229 if No (Proc) then
13230 return False;
13231 end if;
13232
13233 -- If we are not a list member, something is strange, so
13234 -- be conservative and return False.
13235
13236 if not Is_List_Member (N) then
13237 return False;
13238 end if;
13239
13240 -- We are going to find the right formal by stepping forward
13241 -- through the formals, as we step backwards in the actuals.
13242
13243 Form := First_Formal (Proc);
13244 Act := N;
13245 loop
13246 -- If no formal, something is weird, so be conservative
13247 -- and return False.
13248
13249 if No (Form) then
13250 return False;
13251 end if;
13252
13253 Prev (Act);
13254 exit when No (Act);
13255 Next_Formal (Form);
13256 end loop;
13257
13258 return Ekind (Form) /= E_In_Parameter;
13259 end;
13260
13261 -- Named parameter for procedure or accept call
13262
13263 when N_Parameter_Association =>
13264 declare
13265 Proc : Entity_Id;
13266 Form : Entity_Id;
13267
13268 begin
13269 Proc := Get_Subprogram_Entity (Parent (P));
13270
13271 if No (Proc) then
13272 return False;
13273 end if;
13274
13275 -- Loop through formals to find the one that matches
13276
13277 Form := First_Formal (Proc);
13278 loop
13279 -- If no matching formal, that's peculiar, some kind of
13280 -- previous error, so return False to be conservative.
13281 -- Actually this also happens in legal code in the case
13282 -- where P is a parameter association for an Extra_Formal???
13283
13284 if No (Form) then
13285 return False;
13286 end if;
13287
13288 -- Else test for match
13289
13290 if Chars (Form) = Chars (Selector_Name (P)) then
13291 return Ekind (Form) /= E_In_Parameter;
13292 end if;
13293
13294 Next_Formal (Form);
13295 end loop;
13296 end;
13297
13298 -- Test for appearing in a conversion that itself appears
13299 -- in an lvalue context, since this should be an lvalue.
13300
13301 when N_Type_Conversion =>
13302 return Known_To_Be_Assigned (P);
13303
13304 -- All other references are definitely not known to be modifications
13305
13306 when others =>
13307 return False;
13308
13309 end case;
13310 end Known_To_Be_Assigned;
13311
13312 ---------------------------
13313 -- Last_Source_Statement --
13314 ---------------------------
13315
13316 function Last_Source_Statement (HSS : Node_Id) return Node_Id is
13317 N : Node_Id;
13318
13319 begin
13320 N := Last (Statements (HSS));
13321 while Present (N) loop
13322 exit when Comes_From_Source (N);
13323 Prev (N);
13324 end loop;
13325
13326 return N;
13327 end Last_Source_Statement;
13328
13329 ----------------------------------
13330 -- Matching_Static_Array_Bounds --
13331 ----------------------------------
13332
13333 function Matching_Static_Array_Bounds
13334 (L_Typ : Node_Id;
13335 R_Typ : Node_Id) return Boolean
13336 is
13337 L_Ndims : constant Nat := Number_Dimensions (L_Typ);
13338 R_Ndims : constant Nat := Number_Dimensions (R_Typ);
13339
13340 L_Index : Node_Id;
13341 R_Index : Node_Id;
13342 L_Low : Node_Id;
13343 L_High : Node_Id;
13344 L_Len : Uint;
13345 R_Low : Node_Id;
13346 R_High : Node_Id;
13347 R_Len : Uint;
13348
13349 begin
13350 if L_Ndims /= R_Ndims then
13351 return False;
13352 end if;
13353
13354 -- Unconstrained types do not have static bounds
13355
13356 if not Is_Constrained (L_Typ) or else not Is_Constrained (R_Typ) then
13357 return False;
13358 end if;
13359
13360 -- First treat specially the first dimension, as the lower bound and
13361 -- length of string literals are not stored like those of arrays.
13362
13363 if Ekind (L_Typ) = E_String_Literal_Subtype then
13364 L_Low := String_Literal_Low_Bound (L_Typ);
13365 L_Len := String_Literal_Length (L_Typ);
13366 else
13367 L_Index := First_Index (L_Typ);
13368 Get_Index_Bounds (L_Index, L_Low, L_High);
13369
13370 if Is_OK_Static_Expression (L_Low)
13371 and then
13372 Is_OK_Static_Expression (L_High)
13373 then
13374 if Expr_Value (L_High) < Expr_Value (L_Low) then
13375 L_Len := Uint_0;
13376 else
13377 L_Len := (Expr_Value (L_High) - Expr_Value (L_Low)) + 1;
13378 end if;
13379 else
13380 return False;
13381 end if;
13382 end if;
13383
13384 if Ekind (R_Typ) = E_String_Literal_Subtype then
13385 R_Low := String_Literal_Low_Bound (R_Typ);
13386 R_Len := String_Literal_Length (R_Typ);
13387 else
13388 R_Index := First_Index (R_Typ);
13389 Get_Index_Bounds (R_Index, R_Low, R_High);
13390
13391 if Is_OK_Static_Expression (R_Low)
13392 and then
13393 Is_OK_Static_Expression (R_High)
13394 then
13395 if Expr_Value (R_High) < Expr_Value (R_Low) then
13396 R_Len := Uint_0;
13397 else
13398 R_Len := (Expr_Value (R_High) - Expr_Value (R_Low)) + 1;
13399 end if;
13400 else
13401 return False;
13402 end if;
13403 end if;
13404
13405 if (Is_OK_Static_Expression (L_Low)
13406 and then
13407 Is_OK_Static_Expression (R_Low))
13408 and then Expr_Value (L_Low) = Expr_Value (R_Low)
13409 and then L_Len = R_Len
13410 then
13411 null;
13412 else
13413 return False;
13414 end if;
13415
13416 -- Then treat all other dimensions
13417
13418 for Indx in 2 .. L_Ndims loop
13419 Next (L_Index);
13420 Next (R_Index);
13421
13422 Get_Index_Bounds (L_Index, L_Low, L_High);
13423 Get_Index_Bounds (R_Index, R_Low, R_High);
13424
13425 if (Is_OK_Static_Expression (L_Low) and then
13426 Is_OK_Static_Expression (L_High) and then
13427 Is_OK_Static_Expression (R_Low) and then
13428 Is_OK_Static_Expression (R_High))
13429 and then (Expr_Value (L_Low) = Expr_Value (R_Low)
13430 and then
13431 Expr_Value (L_High) = Expr_Value (R_High))
13432 then
13433 null;
13434 else
13435 return False;
13436 end if;
13437 end loop;
13438
13439 -- If we fall through the loop, all indexes matched
13440
13441 return True;
13442 end Matching_Static_Array_Bounds;
13443
13444 -------------------
13445 -- May_Be_Lvalue --
13446 -------------------
13447
13448 function May_Be_Lvalue (N : Node_Id) return Boolean is
13449 P : constant Node_Id := Parent (N);
13450
13451 begin
13452 case Nkind (P) is
13453
13454 -- Test left side of assignment
13455
13456 when N_Assignment_Statement =>
13457 return N = Name (P);
13458
13459 -- Test prefix of component or attribute. Note that the prefix of an
13460 -- explicit or implicit dereference cannot be an l-value.
13461
13462 when N_Attribute_Reference =>
13463 return N = Prefix (P)
13464 and then Name_Implies_Lvalue_Prefix (Attribute_Name (P));
13465
13466 -- For an expanded name, the name is an lvalue if the expanded name
13467 -- is an lvalue, but the prefix is never an lvalue, since it is just
13468 -- the scope where the name is found.
13469
13470 when N_Expanded_Name =>
13471 if N = Prefix (P) then
13472 return May_Be_Lvalue (P);
13473 else
13474 return False;
13475 end if;
13476
13477 -- For a selected component A.B, A is certainly an lvalue if A.B is.
13478 -- B is a little interesting, if we have A.B := 3, there is some
13479 -- discussion as to whether B is an lvalue or not, we choose to say
13480 -- it is. Note however that A is not an lvalue if it is of an access
13481 -- type since this is an implicit dereference.
13482
13483 when N_Selected_Component =>
13484 if N = Prefix (P)
13485 and then Present (Etype (N))
13486 and then Is_Access_Type (Etype (N))
13487 then
13488 return False;
13489 else
13490 return May_Be_Lvalue (P);
13491 end if;
13492
13493 -- For an indexed component or slice, the index or slice bounds is
13494 -- never an lvalue. The prefix is an lvalue if the indexed component
13495 -- or slice is an lvalue, except if it is an access type, where we
13496 -- have an implicit dereference.
13497
13498 when N_Indexed_Component | N_Slice =>
13499 if N /= Prefix (P)
13500 or else (Present (Etype (N)) and then Is_Access_Type (Etype (N)))
13501 then
13502 return False;
13503 else
13504 return May_Be_Lvalue (P);
13505 end if;
13506
13507 -- Prefix of a reference is an lvalue if the reference is an lvalue
13508
13509 when N_Reference =>
13510 return May_Be_Lvalue (P);
13511
13512 -- Prefix of explicit dereference is never an lvalue
13513
13514 when N_Explicit_Dereference =>
13515 return False;
13516
13517 -- Positional parameter for subprogram, entry, or accept call.
13518 -- In older versions of Ada function call arguments are never
13519 -- lvalues. In Ada 2012 functions can have in-out parameters.
13520
13521 when N_Subprogram_Call |
13522 N_Entry_Call_Statement |
13523 N_Accept_Statement
13524 =>
13525 if Nkind (P) = N_Function_Call and then Ada_Version < Ada_2012 then
13526 return False;
13527 end if;
13528
13529 -- The following mechanism is clumsy and fragile. A single flag
13530 -- set in Resolve_Actuals would be preferable ???
13531
13532 declare
13533 Proc : Entity_Id;
13534 Form : Entity_Id;
13535 Act : Node_Id;
13536
13537 begin
13538 Proc := Get_Subprogram_Entity (P);
13539
13540 if No (Proc) then
13541 return True;
13542 end if;
13543
13544 -- If we are not a list member, something is strange, so be
13545 -- conservative and return True.
13546
13547 if not Is_List_Member (N) then
13548 return True;
13549 end if;
13550
13551 -- We are going to find the right formal by stepping forward
13552 -- through the formals, as we step backwards in the actuals.
13553
13554 Form := First_Formal (Proc);
13555 Act := N;
13556 loop
13557 -- If no formal, something is weird, so be conservative and
13558 -- return True.
13559
13560 if No (Form) then
13561 return True;
13562 end if;
13563
13564 Prev (Act);
13565 exit when No (Act);
13566 Next_Formal (Form);
13567 end loop;
13568
13569 return Ekind (Form) /= E_In_Parameter;
13570 end;
13571
13572 -- Named parameter for procedure or accept call
13573
13574 when N_Parameter_Association =>
13575 declare
13576 Proc : Entity_Id;
13577 Form : Entity_Id;
13578
13579 begin
13580 Proc := Get_Subprogram_Entity (Parent (P));
13581
13582 if No (Proc) then
13583 return True;
13584 end if;
13585
13586 -- Loop through formals to find the one that matches
13587
13588 Form := First_Formal (Proc);
13589 loop
13590 -- If no matching formal, that's peculiar, some kind of
13591 -- previous error, so return True to be conservative.
13592 -- Actually happens with legal code for an unresolved call
13593 -- where we may get the wrong homonym???
13594
13595 if No (Form) then
13596 return True;
13597 end if;
13598
13599 -- Else test for match
13600
13601 if Chars (Form) = Chars (Selector_Name (P)) then
13602 return Ekind (Form) /= E_In_Parameter;
13603 end if;
13604
13605 Next_Formal (Form);
13606 end loop;
13607 end;
13608
13609 -- Test for appearing in a conversion that itself appears in an
13610 -- lvalue context, since this should be an lvalue.
13611
13612 when N_Type_Conversion =>
13613 return May_Be_Lvalue (P);
13614
13615 -- Test for appearance in object renaming declaration
13616
13617 when N_Object_Renaming_Declaration =>
13618 return True;
13619
13620 -- All other references are definitely not lvalues
13621
13622 when others =>
13623 return False;
13624
13625 end case;
13626 end May_Be_Lvalue;
13627
13628 -----------------------
13629 -- Mark_Coextensions --
13630 -----------------------
13631
13632 procedure Mark_Coextensions (Context_Nod : Node_Id; Root_Nod : Node_Id) is
13633 Is_Dynamic : Boolean;
13634 -- Indicates whether the context causes nested coextensions to be
13635 -- dynamic or static
13636
13637 function Mark_Allocator (N : Node_Id) return Traverse_Result;
13638 -- Recognize an allocator node and label it as a dynamic coextension
13639
13640 --------------------
13641 -- Mark_Allocator --
13642 --------------------
13643
13644 function Mark_Allocator (N : Node_Id) return Traverse_Result is
13645 begin
13646 if Nkind (N) = N_Allocator then
13647 if Is_Dynamic then
13648 Set_Is_Dynamic_Coextension (N);
13649
13650 -- If the allocator expression is potentially dynamic, it may
13651 -- be expanded out of order and require dynamic allocation
13652 -- anyway, so we treat the coextension itself as dynamic.
13653 -- Potential optimization ???
13654
13655 elsif Nkind (Expression (N)) = N_Qualified_Expression
13656 and then Nkind (Expression (Expression (N))) = N_Op_Concat
13657 then
13658 Set_Is_Dynamic_Coextension (N);
13659 else
13660 Set_Is_Static_Coextension (N);
13661 end if;
13662 end if;
13663
13664 return OK;
13665 end Mark_Allocator;
13666
13667 procedure Mark_Allocators is new Traverse_Proc (Mark_Allocator);
13668
13669 -- Start of processing Mark_Coextensions
13670
13671 begin
13672 case Nkind (Context_Nod) is
13673
13674 -- Comment here ???
13675
13676 when N_Assignment_Statement =>
13677 Is_Dynamic := Nkind (Expression (Context_Nod)) = N_Allocator;
13678
13679 -- An allocator that is a component of a returned aggregate
13680 -- must be dynamic.
13681
13682 when N_Simple_Return_Statement =>
13683 declare
13684 Expr : constant Node_Id := Expression (Context_Nod);
13685 begin
13686 Is_Dynamic :=
13687 Nkind (Expr) = N_Allocator
13688 or else
13689 (Nkind (Expr) = N_Qualified_Expression
13690 and then Nkind (Expression (Expr)) = N_Aggregate);
13691 end;
13692
13693 -- An alloctor within an object declaration in an extended return
13694 -- statement is of necessity dynamic.
13695
13696 when N_Object_Declaration =>
13697 Is_Dynamic := Nkind (Root_Nod) = N_Allocator
13698 or else
13699 Nkind (Parent (Context_Nod)) = N_Extended_Return_Statement;
13700
13701 -- This routine should not be called for constructs which may not
13702 -- contain coextensions.
13703
13704 when others =>
13705 raise Program_Error;
13706 end case;
13707
13708 Mark_Allocators (Root_Nod);
13709 end Mark_Coextensions;
13710
13711 ----------------------
13712 -- Needs_One_Actual --
13713 ----------------------
13714
13715 function Needs_One_Actual (E : Entity_Id) return Boolean is
13716 Formal : Entity_Id;
13717
13718 begin
13719 -- Ada 2005 or later, and formals present
13720
13721 if Ada_Version >= Ada_2005 and then Present (First_Formal (E)) then
13722 Formal := Next_Formal (First_Formal (E));
13723 while Present (Formal) loop
13724 if No (Default_Value (Formal)) then
13725 return False;
13726 end if;
13727
13728 Next_Formal (Formal);
13729 end loop;
13730
13731 return True;
13732
13733 -- Ada 83/95 or no formals
13734
13735 else
13736 return False;
13737 end if;
13738 end Needs_One_Actual;
13739
13740 ------------------------
13741 -- New_Copy_List_Tree --
13742 ------------------------
13743
13744 function New_Copy_List_Tree (List : List_Id) return List_Id is
13745 NL : List_Id;
13746 E : Node_Id;
13747
13748 begin
13749 if List = No_List then
13750 return No_List;
13751
13752 else
13753 NL := New_List;
13754 E := First (List);
13755
13756 while Present (E) loop
13757 Append (New_Copy_Tree (E), NL);
13758 E := Next (E);
13759 end loop;
13760
13761 return NL;
13762 end if;
13763 end New_Copy_List_Tree;
13764
13765 --------------------------------------------------
13766 -- New_Copy_Tree Auxiliary Data and Subprograms --
13767 --------------------------------------------------
13768
13769 use Atree.Unchecked_Access;
13770 use Atree_Private_Part;
13771
13772 -- Our approach here requires a two pass traversal of the tree. The
13773 -- first pass visits all nodes that eventually will be copied looking
13774 -- for defining Itypes. If any defining Itypes are found, then they are
13775 -- copied, and an entry is added to the replacement map. In the second
13776 -- phase, the tree is copied, using the replacement map to replace any
13777 -- Itype references within the copied tree.
13778
13779 -- The following hash tables are used if the Map supplied has more
13780 -- than hash threshold entries to speed up access to the map. If
13781 -- there are fewer entries, then the map is searched sequentially
13782 -- (because setting up a hash table for only a few entries takes
13783 -- more time than it saves.
13784
13785 function New_Copy_Hash (E : Entity_Id) return NCT_Header_Num;
13786 -- Hash function used for hash operations
13787
13788 -------------------
13789 -- New_Copy_Hash --
13790 -------------------
13791
13792 function New_Copy_Hash (E : Entity_Id) return NCT_Header_Num is
13793 begin
13794 return Nat (E) mod (NCT_Header_Num'Last + 1);
13795 end New_Copy_Hash;
13796
13797 ---------------
13798 -- NCT_Assoc --
13799 ---------------
13800
13801 -- The hash table NCT_Assoc associates old entities in the table
13802 -- with their corresponding new entities (i.e. the pairs of entries
13803 -- presented in the original Map argument are Key-Element pairs).
13804
13805 package NCT_Assoc is new Simple_HTable (
13806 Header_Num => NCT_Header_Num,
13807 Element => Entity_Id,
13808 No_Element => Empty,
13809 Key => Entity_Id,
13810 Hash => New_Copy_Hash,
13811 Equal => Types."=");
13812
13813 ---------------------
13814 -- NCT_Itype_Assoc --
13815 ---------------------
13816
13817 -- The hash table NCT_Itype_Assoc contains entries only for those
13818 -- old nodes which have a non-empty Associated_Node_For_Itype set.
13819 -- The key is the associated node, and the element is the new node
13820 -- itself (NOT the associated node for the new node).
13821
13822 package NCT_Itype_Assoc is new Simple_HTable (
13823 Header_Num => NCT_Header_Num,
13824 Element => Entity_Id,
13825 No_Element => Empty,
13826 Key => Entity_Id,
13827 Hash => New_Copy_Hash,
13828 Equal => Types."=");
13829
13830 -------------------
13831 -- New_Copy_Tree --
13832 -------------------
13833
13834 function New_Copy_Tree
13835 (Source : Node_Id;
13836 Map : Elist_Id := No_Elist;
13837 New_Sloc : Source_Ptr := No_Location;
13838 New_Scope : Entity_Id := Empty) return Node_Id
13839 is
13840 Actual_Map : Elist_Id := Map;
13841 -- This is the actual map for the copy. It is initialized with the
13842 -- given elements, and then enlarged as required for Itypes that are
13843 -- copied during the first phase of the copy operation. The visit
13844 -- procedures add elements to this map as Itypes are encountered.
13845 -- The reason we cannot use Map directly, is that it may well be
13846 -- (and normally is) initialized to No_Elist, and if we have mapped
13847 -- entities, we have to reset it to point to a real Elist.
13848
13849 function Assoc (N : Node_Or_Entity_Id) return Node_Id;
13850 -- Called during second phase to map entities into their corresponding
13851 -- copies using Actual_Map. If the argument is not an entity, or is not
13852 -- in Actual_Map, then it is returned unchanged.
13853
13854 procedure Build_NCT_Hash_Tables;
13855 -- Builds hash tables (number of elements >= threshold value)
13856
13857 function Copy_Elist_With_Replacement
13858 (Old_Elist : Elist_Id) return Elist_Id;
13859 -- Called during second phase to copy element list doing replacements
13860
13861 procedure Copy_Itype_With_Replacement (New_Itype : Entity_Id);
13862 -- Called during the second phase to process a copied Itype. The actual
13863 -- copy happened during the first phase (so that we could make the entry
13864 -- in the mapping), but we still have to deal with the descendents of
13865 -- the copied Itype and copy them where necessary.
13866
13867 function Copy_List_With_Replacement (Old_List : List_Id) return List_Id;
13868 -- Called during second phase to copy list doing replacements
13869
13870 function Copy_Node_With_Replacement (Old_Node : Node_Id) return Node_Id;
13871 -- Called during second phase to copy node doing replacements
13872
13873 procedure Visit_Elist (E : Elist_Id);
13874 -- Called during first phase to visit all elements of an Elist
13875
13876 procedure Visit_Field (F : Union_Id; N : Node_Id);
13877 -- Visit a single field, recursing to call Visit_Node or Visit_List
13878 -- if the field is a syntactic descendent of the current node (i.e.
13879 -- its parent is Node N).
13880
13881 procedure Visit_Itype (Old_Itype : Entity_Id);
13882 -- Called during first phase to visit subsidiary fields of a defining
13883 -- Itype, and also create a copy and make an entry in the replacement
13884 -- map for the new copy.
13885
13886 procedure Visit_List (L : List_Id);
13887 -- Called during first phase to visit all elements of a List
13888
13889 procedure Visit_Node (N : Node_Or_Entity_Id);
13890 -- Called during first phase to visit a node and all its subtrees
13891
13892 -----------
13893 -- Assoc --
13894 -----------
13895
13896 function Assoc (N : Node_Or_Entity_Id) return Node_Id is
13897 E : Elmt_Id;
13898 Ent : Entity_Id;
13899
13900 begin
13901 if not Has_Extension (N) or else No (Actual_Map) then
13902 return N;
13903
13904 elsif NCT_Hash_Tables_Used then
13905 Ent := NCT_Assoc.Get (Entity_Id (N));
13906
13907 if Present (Ent) then
13908 return Ent;
13909 else
13910 return N;
13911 end if;
13912
13913 -- No hash table used, do serial search
13914
13915 else
13916 E := First_Elmt (Actual_Map);
13917 while Present (E) loop
13918 if Node (E) = N then
13919 return Node (Next_Elmt (E));
13920 else
13921 E := Next_Elmt (Next_Elmt (E));
13922 end if;
13923 end loop;
13924 end if;
13925
13926 return N;
13927 end Assoc;
13928
13929 ---------------------------
13930 -- Build_NCT_Hash_Tables --
13931 ---------------------------
13932
13933 procedure Build_NCT_Hash_Tables is
13934 Elmt : Elmt_Id;
13935 Ent : Entity_Id;
13936 begin
13937 if NCT_Hash_Table_Setup then
13938 NCT_Assoc.Reset;
13939 NCT_Itype_Assoc.Reset;
13940 end if;
13941
13942 Elmt := First_Elmt (Actual_Map);
13943 while Present (Elmt) loop
13944 Ent := Node (Elmt);
13945
13946 -- Get new entity, and associate old and new
13947
13948 Next_Elmt (Elmt);
13949 NCT_Assoc.Set (Ent, Node (Elmt));
13950
13951 if Is_Type (Ent) then
13952 declare
13953 Anode : constant Entity_Id :=
13954 Associated_Node_For_Itype (Ent);
13955
13956 begin
13957 if Present (Anode) then
13958
13959 -- Enter a link between the associated node of the
13960 -- old Itype and the new Itype, for updating later
13961 -- when node is copied.
13962
13963 NCT_Itype_Assoc.Set (Anode, Node (Elmt));
13964 end if;
13965 end;
13966 end if;
13967
13968 Next_Elmt (Elmt);
13969 end loop;
13970
13971 NCT_Hash_Tables_Used := True;
13972 NCT_Hash_Table_Setup := True;
13973 end Build_NCT_Hash_Tables;
13974
13975 ---------------------------------
13976 -- Copy_Elist_With_Replacement --
13977 ---------------------------------
13978
13979 function Copy_Elist_With_Replacement
13980 (Old_Elist : Elist_Id) return Elist_Id
13981 is
13982 M : Elmt_Id;
13983 New_Elist : Elist_Id;
13984
13985 begin
13986 if No (Old_Elist) then
13987 return No_Elist;
13988
13989 else
13990 New_Elist := New_Elmt_List;
13991
13992 M := First_Elmt (Old_Elist);
13993 while Present (M) loop
13994 Append_Elmt (Copy_Node_With_Replacement (Node (M)), New_Elist);
13995 Next_Elmt (M);
13996 end loop;
13997 end if;
13998
13999 return New_Elist;
14000 end Copy_Elist_With_Replacement;
14001
14002 ---------------------------------
14003 -- Copy_Itype_With_Replacement --
14004 ---------------------------------
14005
14006 -- This routine exactly parallels its phase one analog Visit_Itype,
14007
14008 procedure Copy_Itype_With_Replacement (New_Itype : Entity_Id) is
14009 begin
14010 -- Translate Next_Entity, Scope and Etype fields, in case they
14011 -- reference entities that have been mapped into copies.
14012
14013 Set_Next_Entity (New_Itype, Assoc (Next_Entity (New_Itype)));
14014 Set_Etype (New_Itype, Assoc (Etype (New_Itype)));
14015
14016 if Present (New_Scope) then
14017 Set_Scope (New_Itype, New_Scope);
14018 else
14019 Set_Scope (New_Itype, Assoc (Scope (New_Itype)));
14020 end if;
14021
14022 -- Copy referenced fields
14023
14024 if Is_Discrete_Type (New_Itype) then
14025 Set_Scalar_Range (New_Itype,
14026 Copy_Node_With_Replacement (Scalar_Range (New_Itype)));
14027
14028 elsif Has_Discriminants (Base_Type (New_Itype)) then
14029 Set_Discriminant_Constraint (New_Itype,
14030 Copy_Elist_With_Replacement
14031 (Discriminant_Constraint (New_Itype)));
14032
14033 elsif Is_Array_Type (New_Itype) then
14034 if Present (First_Index (New_Itype)) then
14035 Set_First_Index (New_Itype,
14036 First (Copy_List_With_Replacement
14037 (List_Containing (First_Index (New_Itype)))));
14038 end if;
14039
14040 if Is_Packed (New_Itype) then
14041 Set_Packed_Array_Impl_Type (New_Itype,
14042 Copy_Node_With_Replacement
14043 (Packed_Array_Impl_Type (New_Itype)));
14044 end if;
14045 end if;
14046 end Copy_Itype_With_Replacement;
14047
14048 --------------------------------
14049 -- Copy_List_With_Replacement --
14050 --------------------------------
14051
14052 function Copy_List_With_Replacement
14053 (Old_List : List_Id) return List_Id
14054 is
14055 New_List : List_Id;
14056 E : Node_Id;
14057
14058 begin
14059 if Old_List = No_List then
14060 return No_List;
14061
14062 else
14063 New_List := Empty_List;
14064
14065 E := First (Old_List);
14066 while Present (E) loop
14067 Append (Copy_Node_With_Replacement (E), New_List);
14068 Next (E);
14069 end loop;
14070
14071 return New_List;
14072 end if;
14073 end Copy_List_With_Replacement;
14074
14075 --------------------------------
14076 -- Copy_Node_With_Replacement --
14077 --------------------------------
14078
14079 function Copy_Node_With_Replacement
14080 (Old_Node : Node_Id) return Node_Id
14081 is
14082 New_Node : Node_Id;
14083
14084 procedure Adjust_Named_Associations
14085 (Old_Node : Node_Id;
14086 New_Node : Node_Id);
14087 -- If a call node has named associations, these are chained through
14088 -- the First_Named_Actual, Next_Named_Actual links. These must be
14089 -- propagated separately to the new parameter list, because these
14090 -- are not syntactic fields.
14091
14092 function Copy_Field_With_Replacement
14093 (Field : Union_Id) return Union_Id;
14094 -- Given Field, which is a field of Old_Node, return a copy of it
14095 -- if it is a syntactic field (i.e. its parent is Node), setting
14096 -- the parent of the copy to poit to New_Node. Otherwise returns
14097 -- the field (possibly mapped if it is an entity).
14098
14099 -------------------------------
14100 -- Adjust_Named_Associations --
14101 -------------------------------
14102
14103 procedure Adjust_Named_Associations
14104 (Old_Node : Node_Id;
14105 New_Node : Node_Id)
14106 is
14107 Old_E : Node_Id;
14108 New_E : Node_Id;
14109
14110 Old_Next : Node_Id;
14111 New_Next : Node_Id;
14112
14113 begin
14114 Old_E := First (Parameter_Associations (Old_Node));
14115 New_E := First (Parameter_Associations (New_Node));
14116 while Present (Old_E) loop
14117 if Nkind (Old_E) = N_Parameter_Association
14118 and then Present (Next_Named_Actual (Old_E))
14119 then
14120 if First_Named_Actual (Old_Node)
14121 = Explicit_Actual_Parameter (Old_E)
14122 then
14123 Set_First_Named_Actual
14124 (New_Node, Explicit_Actual_Parameter (New_E));
14125 end if;
14126
14127 -- Now scan parameter list from the beginning,to locate
14128 -- next named actual, which can be out of order.
14129
14130 Old_Next := First (Parameter_Associations (Old_Node));
14131 New_Next := First (Parameter_Associations (New_Node));
14132
14133 while Nkind (Old_Next) /= N_Parameter_Association
14134 or else Explicit_Actual_Parameter (Old_Next)
14135 /= Next_Named_Actual (Old_E)
14136 loop
14137 Next (Old_Next);
14138 Next (New_Next);
14139 end loop;
14140
14141 Set_Next_Named_Actual
14142 (New_E, Explicit_Actual_Parameter (New_Next));
14143 end if;
14144
14145 Next (Old_E);
14146 Next (New_E);
14147 end loop;
14148 end Adjust_Named_Associations;
14149
14150 ---------------------------------
14151 -- Copy_Field_With_Replacement --
14152 ---------------------------------
14153
14154 function Copy_Field_With_Replacement
14155 (Field : Union_Id) return Union_Id
14156 is
14157 begin
14158 if Field = Union_Id (Empty) then
14159 return Field;
14160
14161 elsif Field in Node_Range then
14162 declare
14163 Old_N : constant Node_Id := Node_Id (Field);
14164 New_N : Node_Id;
14165
14166 begin
14167 -- If syntactic field, as indicated by the parent pointer
14168 -- being set, then copy the referenced node recursively.
14169
14170 if Parent (Old_N) = Old_Node then
14171 New_N := Copy_Node_With_Replacement (Old_N);
14172
14173 if New_N /= Old_N then
14174 Set_Parent (New_N, New_Node);
14175 end if;
14176
14177 -- For semantic fields, update possible entity reference
14178 -- from the replacement map.
14179
14180 else
14181 New_N := Assoc (Old_N);
14182 end if;
14183
14184 return Union_Id (New_N);
14185 end;
14186
14187 elsif Field in List_Range then
14188 declare
14189 Old_L : constant List_Id := List_Id (Field);
14190 New_L : List_Id;
14191
14192 begin
14193 -- If syntactic field, as indicated by the parent pointer,
14194 -- then recursively copy the entire referenced list.
14195
14196 if Parent (Old_L) = Old_Node then
14197 New_L := Copy_List_With_Replacement (Old_L);
14198 Set_Parent (New_L, New_Node);
14199
14200 -- For semantic list, just returned unchanged
14201
14202 else
14203 New_L := Old_L;
14204 end if;
14205
14206 return Union_Id (New_L);
14207 end;
14208
14209 -- Anything other than a list or a node is returned unchanged
14210
14211 else
14212 return Field;
14213 end if;
14214 end Copy_Field_With_Replacement;
14215
14216 -- Start of processing for Copy_Node_With_Replacement
14217
14218 begin
14219 if Old_Node <= Empty_Or_Error then
14220 return Old_Node;
14221
14222 elsif Has_Extension (Old_Node) then
14223 return Assoc (Old_Node);
14224
14225 else
14226 New_Node := New_Copy (Old_Node);
14227
14228 -- If the node we are copying is the associated node of a
14229 -- previously copied Itype, then adjust the associated node
14230 -- of the copy of that Itype accordingly.
14231
14232 if Present (Actual_Map) then
14233 declare
14234 E : Elmt_Id;
14235 Ent : Entity_Id;
14236
14237 begin
14238 -- Case of hash table used
14239
14240 if NCT_Hash_Tables_Used then
14241 Ent := NCT_Itype_Assoc.Get (Old_Node);
14242
14243 if Present (Ent) then
14244 Set_Associated_Node_For_Itype (Ent, New_Node);
14245 end if;
14246
14247 -- Case of no hash table used
14248
14249 else
14250 E := First_Elmt (Actual_Map);
14251 while Present (E) loop
14252 if Is_Itype (Node (E))
14253 and then
14254 Old_Node = Associated_Node_For_Itype (Node (E))
14255 then
14256 Set_Associated_Node_For_Itype
14257 (Node (Next_Elmt (E)), New_Node);
14258 end if;
14259
14260 E := Next_Elmt (Next_Elmt (E));
14261 end loop;
14262 end if;
14263 end;
14264 end if;
14265
14266 -- Recursively copy descendents
14267
14268 Set_Field1
14269 (New_Node, Copy_Field_With_Replacement (Field1 (New_Node)));
14270 Set_Field2
14271 (New_Node, Copy_Field_With_Replacement (Field2 (New_Node)));
14272 Set_Field3
14273 (New_Node, Copy_Field_With_Replacement (Field3 (New_Node)));
14274 Set_Field4
14275 (New_Node, Copy_Field_With_Replacement (Field4 (New_Node)));
14276 Set_Field5
14277 (New_Node, Copy_Field_With_Replacement (Field5 (New_Node)));
14278
14279 -- Adjust Sloc of new node if necessary
14280
14281 if New_Sloc /= No_Location then
14282 Set_Sloc (New_Node, New_Sloc);
14283
14284 -- If we adjust the Sloc, then we are essentially making
14285 -- a completely new node, so the Comes_From_Source flag
14286 -- should be reset to the proper default value.
14287
14288 Nodes.Table (New_Node).Comes_From_Source :=
14289 Default_Node.Comes_From_Source;
14290 end if;
14291
14292 -- If the node is call and has named associations,
14293 -- set the corresponding links in the copy.
14294
14295 if (Nkind (Old_Node) = N_Function_Call
14296 or else Nkind (Old_Node) = N_Entry_Call_Statement
14297 or else
14298 Nkind (Old_Node) = N_Procedure_Call_Statement)
14299 and then Present (First_Named_Actual (Old_Node))
14300 then
14301 Adjust_Named_Associations (Old_Node, New_Node);
14302 end if;
14303
14304 -- Reset First_Real_Statement for Handled_Sequence_Of_Statements.
14305 -- The replacement mechanism applies to entities, and is not used
14306 -- here. Eventually we may need a more general graph-copying
14307 -- routine. For now, do a sequential search to find desired node.
14308
14309 if Nkind (Old_Node) = N_Handled_Sequence_Of_Statements
14310 and then Present (First_Real_Statement (Old_Node))
14311 then
14312 declare
14313 Old_F : constant Node_Id := First_Real_Statement (Old_Node);
14314 N1, N2 : Node_Id;
14315
14316 begin
14317 N1 := First (Statements (Old_Node));
14318 N2 := First (Statements (New_Node));
14319
14320 while N1 /= Old_F loop
14321 Next (N1);
14322 Next (N2);
14323 end loop;
14324
14325 Set_First_Real_Statement (New_Node, N2);
14326 end;
14327 end if;
14328 end if;
14329
14330 -- All done, return copied node
14331
14332 return New_Node;
14333 end Copy_Node_With_Replacement;
14334
14335 -----------------
14336 -- Visit_Elist --
14337 -----------------
14338
14339 procedure Visit_Elist (E : Elist_Id) is
14340 Elmt : Elmt_Id;
14341 begin
14342 if Present (E) then
14343 Elmt := First_Elmt (E);
14344
14345 while Elmt /= No_Elmt loop
14346 Visit_Node (Node (Elmt));
14347 Next_Elmt (Elmt);
14348 end loop;
14349 end if;
14350 end Visit_Elist;
14351
14352 -----------------
14353 -- Visit_Field --
14354 -----------------
14355
14356 procedure Visit_Field (F : Union_Id; N : Node_Id) is
14357 begin
14358 if F = Union_Id (Empty) then
14359 return;
14360
14361 elsif F in Node_Range then
14362
14363 -- Copy node if it is syntactic, i.e. its parent pointer is
14364 -- set to point to the field that referenced it (certain
14365 -- Itypes will also meet this criterion, which is fine, since
14366 -- these are clearly Itypes that do need to be copied, since
14367 -- we are copying their parent.)
14368
14369 if Parent (Node_Id (F)) = N then
14370 Visit_Node (Node_Id (F));
14371 return;
14372
14373 -- Another case, if we are pointing to an Itype, then we want
14374 -- to copy it if its associated node is somewhere in the tree
14375 -- being copied.
14376
14377 -- Note: the exclusion of self-referential copies is just an
14378 -- optimization, since the search of the already copied list
14379 -- would catch it, but it is a common case (Etype pointing
14380 -- to itself for an Itype that is a base type).
14381
14382 elsif Has_Extension (Node_Id (F))
14383 and then Is_Itype (Entity_Id (F))
14384 and then Node_Id (F) /= N
14385 then
14386 declare
14387 P : Node_Id;
14388
14389 begin
14390 P := Associated_Node_For_Itype (Node_Id (F));
14391 while Present (P) loop
14392 if P = Source then
14393 Visit_Node (Node_Id (F));
14394 return;
14395 else
14396 P := Parent (P);
14397 end if;
14398 end loop;
14399
14400 -- An Itype whose parent is not being copied definitely
14401 -- should NOT be copied, since it does not belong in any
14402 -- sense to the copied subtree.
14403
14404 return;
14405 end;
14406 end if;
14407
14408 elsif F in List_Range and then Parent (List_Id (F)) = N then
14409 Visit_List (List_Id (F));
14410 return;
14411 end if;
14412 end Visit_Field;
14413
14414 -----------------
14415 -- Visit_Itype --
14416 -----------------
14417
14418 procedure Visit_Itype (Old_Itype : Entity_Id) is
14419 New_Itype : Entity_Id;
14420 E : Elmt_Id;
14421 Ent : Entity_Id;
14422
14423 begin
14424 -- Itypes that describe the designated type of access to subprograms
14425 -- have the structure of subprogram declarations, with signatures,
14426 -- etc. Either we duplicate the signatures completely, or choose to
14427 -- share such itypes, which is fine because their elaboration will
14428 -- have no side effects.
14429
14430 if Ekind (Old_Itype) = E_Subprogram_Type then
14431 return;
14432 end if;
14433
14434 New_Itype := New_Copy (Old_Itype);
14435
14436 -- The new Itype has all the attributes of the old one, and
14437 -- we just copy the contents of the entity. However, the back-end
14438 -- needs different names for debugging purposes, so we create a
14439 -- new internal name for it in all cases.
14440
14441 Set_Chars (New_Itype, New_Internal_Name ('T'));
14442
14443 -- If our associated node is an entity that has already been copied,
14444 -- then set the associated node of the copy to point to the right
14445 -- copy. If we have copied an Itype that is itself the associated
14446 -- node of some previously copied Itype, then we set the right
14447 -- pointer in the other direction.
14448
14449 if Present (Actual_Map) then
14450
14451 -- Case of hash tables used
14452
14453 if NCT_Hash_Tables_Used then
14454
14455 Ent := NCT_Assoc.Get (Associated_Node_For_Itype (Old_Itype));
14456
14457 if Present (Ent) then
14458 Set_Associated_Node_For_Itype (New_Itype, Ent);
14459 end if;
14460
14461 Ent := NCT_Itype_Assoc.Get (Old_Itype);
14462 if Present (Ent) then
14463 Set_Associated_Node_For_Itype (Ent, New_Itype);
14464
14465 -- If the hash table has no association for this Itype and
14466 -- its associated node, enter one now.
14467
14468 else
14469 NCT_Itype_Assoc.Set
14470 (Associated_Node_For_Itype (Old_Itype), New_Itype);
14471 end if;
14472
14473 -- Case of hash tables not used
14474
14475 else
14476 E := First_Elmt (Actual_Map);
14477 while Present (E) loop
14478 if Associated_Node_For_Itype (Old_Itype) = Node (E) then
14479 Set_Associated_Node_For_Itype
14480 (New_Itype, Node (Next_Elmt (E)));
14481 end if;
14482
14483 if Is_Type (Node (E))
14484 and then Old_Itype = Associated_Node_For_Itype (Node (E))
14485 then
14486 Set_Associated_Node_For_Itype
14487 (Node (Next_Elmt (E)), New_Itype);
14488 end if;
14489
14490 E := Next_Elmt (Next_Elmt (E));
14491 end loop;
14492 end if;
14493 end if;
14494
14495 if Present (Freeze_Node (New_Itype)) then
14496 Set_Is_Frozen (New_Itype, False);
14497 Set_Freeze_Node (New_Itype, Empty);
14498 end if;
14499
14500 -- Add new association to map
14501
14502 if No (Actual_Map) then
14503 Actual_Map := New_Elmt_List;
14504 end if;
14505
14506 Append_Elmt (Old_Itype, Actual_Map);
14507 Append_Elmt (New_Itype, Actual_Map);
14508
14509 if NCT_Hash_Tables_Used then
14510 NCT_Assoc.Set (Old_Itype, New_Itype);
14511
14512 else
14513 NCT_Table_Entries := NCT_Table_Entries + 1;
14514
14515 if NCT_Table_Entries > NCT_Hash_Threshold then
14516 Build_NCT_Hash_Tables;
14517 end if;
14518 end if;
14519
14520 -- If a record subtype is simply copied, the entity list will be
14521 -- shared. Thus cloned_Subtype must be set to indicate the sharing.
14522
14523 if Ekind_In (Old_Itype, E_Record_Subtype, E_Class_Wide_Subtype) then
14524 Set_Cloned_Subtype (New_Itype, Old_Itype);
14525 end if;
14526
14527 -- Visit descendents that eventually get copied
14528
14529 Visit_Field (Union_Id (Etype (Old_Itype)), Old_Itype);
14530
14531 if Is_Discrete_Type (Old_Itype) then
14532 Visit_Field (Union_Id (Scalar_Range (Old_Itype)), Old_Itype);
14533
14534 elsif Has_Discriminants (Base_Type (Old_Itype)) then
14535 -- ??? This should involve call to Visit_Field
14536 Visit_Elist (Discriminant_Constraint (Old_Itype));
14537
14538 elsif Is_Array_Type (Old_Itype) then
14539 if Present (First_Index (Old_Itype)) then
14540 Visit_Field (Union_Id (List_Containing
14541 (First_Index (Old_Itype))),
14542 Old_Itype);
14543 end if;
14544
14545 if Is_Packed (Old_Itype) then
14546 Visit_Field (Union_Id (Packed_Array_Impl_Type (Old_Itype)),
14547 Old_Itype);
14548 end if;
14549 end if;
14550 end Visit_Itype;
14551
14552 ----------------
14553 -- Visit_List --
14554 ----------------
14555
14556 procedure Visit_List (L : List_Id) is
14557 N : Node_Id;
14558 begin
14559 if L /= No_List then
14560 N := First (L);
14561
14562 while Present (N) loop
14563 Visit_Node (N);
14564 Next (N);
14565 end loop;
14566 end if;
14567 end Visit_List;
14568
14569 ----------------
14570 -- Visit_Node --
14571 ----------------
14572
14573 procedure Visit_Node (N : Node_Or_Entity_Id) is
14574
14575 -- Start of processing for Visit_Node
14576
14577 begin
14578 -- Handle case of an Itype, which must be copied
14579
14580 if Has_Extension (N) and then Is_Itype (N) then
14581
14582 -- Nothing to do if already in the list. This can happen with an
14583 -- Itype entity that appears more than once in the tree.
14584 -- Note that we do not want to visit descendents in this case.
14585
14586 -- Test for already in list when hash table is used
14587
14588 if NCT_Hash_Tables_Used then
14589 if Present (NCT_Assoc.Get (Entity_Id (N))) then
14590 return;
14591 end if;
14592
14593 -- Test for already in list when hash table not used
14594
14595 else
14596 declare
14597 E : Elmt_Id;
14598 begin
14599 if Present (Actual_Map) then
14600 E := First_Elmt (Actual_Map);
14601 while Present (E) loop
14602 if Node (E) = N then
14603 return;
14604 else
14605 E := Next_Elmt (Next_Elmt (E));
14606 end if;
14607 end loop;
14608 end if;
14609 end;
14610 end if;
14611
14612 Visit_Itype (N);
14613 end if;
14614
14615 -- Visit descendents
14616
14617 Visit_Field (Field1 (N), N);
14618 Visit_Field (Field2 (N), N);
14619 Visit_Field (Field3 (N), N);
14620 Visit_Field (Field4 (N), N);
14621 Visit_Field (Field5 (N), N);
14622 end Visit_Node;
14623
14624 -- Start of processing for New_Copy_Tree
14625
14626 begin
14627 Actual_Map := Map;
14628
14629 -- See if we should use hash table
14630
14631 if No (Actual_Map) then
14632 NCT_Hash_Tables_Used := False;
14633
14634 else
14635 declare
14636 Elmt : Elmt_Id;
14637
14638 begin
14639 NCT_Table_Entries := 0;
14640
14641 Elmt := First_Elmt (Actual_Map);
14642 while Present (Elmt) loop
14643 NCT_Table_Entries := NCT_Table_Entries + 1;
14644 Next_Elmt (Elmt);
14645 Next_Elmt (Elmt);
14646 end loop;
14647
14648 if NCT_Table_Entries > NCT_Hash_Threshold then
14649 Build_NCT_Hash_Tables;
14650 else
14651 NCT_Hash_Tables_Used := False;
14652 end if;
14653 end;
14654 end if;
14655
14656 -- Hash table set up if required, now start phase one by visiting
14657 -- top node (we will recursively visit the descendents).
14658
14659 Visit_Node (Source);
14660
14661 -- Now the second phase of the copy can start. First we process
14662 -- all the mapped entities, copying their descendents.
14663
14664 if Present (Actual_Map) then
14665 declare
14666 Elmt : Elmt_Id;
14667 New_Itype : Entity_Id;
14668 begin
14669 Elmt := First_Elmt (Actual_Map);
14670 while Present (Elmt) loop
14671 Next_Elmt (Elmt);
14672 New_Itype := Node (Elmt);
14673 Copy_Itype_With_Replacement (New_Itype);
14674 Next_Elmt (Elmt);
14675 end loop;
14676 end;
14677 end if;
14678
14679 -- Now we can copy the actual tree
14680
14681 return Copy_Node_With_Replacement (Source);
14682 end New_Copy_Tree;
14683
14684 -------------------------
14685 -- New_External_Entity --
14686 -------------------------
14687
14688 function New_External_Entity
14689 (Kind : Entity_Kind;
14690 Scope_Id : Entity_Id;
14691 Sloc_Value : Source_Ptr;
14692 Related_Id : Entity_Id;
14693 Suffix : Character;
14694 Suffix_Index : Nat := 0;
14695 Prefix : Character := ' ') return Entity_Id
14696 is
14697 N : constant Entity_Id :=
14698 Make_Defining_Identifier (Sloc_Value,
14699 New_External_Name
14700 (Chars (Related_Id), Suffix, Suffix_Index, Prefix));
14701
14702 begin
14703 Set_Ekind (N, Kind);
14704 Set_Is_Internal (N, True);
14705 Append_Entity (N, Scope_Id);
14706 Set_Public_Status (N);
14707
14708 if Kind in Type_Kind then
14709 Init_Size_Align (N);
14710 end if;
14711
14712 return N;
14713 end New_External_Entity;
14714
14715 -------------------------
14716 -- New_Internal_Entity --
14717 -------------------------
14718
14719 function New_Internal_Entity
14720 (Kind : Entity_Kind;
14721 Scope_Id : Entity_Id;
14722 Sloc_Value : Source_Ptr;
14723 Id_Char : Character) return Entity_Id
14724 is
14725 N : constant Entity_Id := Make_Temporary (Sloc_Value, Id_Char);
14726
14727 begin
14728 Set_Ekind (N, Kind);
14729 Set_Is_Internal (N, True);
14730 Append_Entity (N, Scope_Id);
14731
14732 if Kind in Type_Kind then
14733 Init_Size_Align (N);
14734 end if;
14735
14736 return N;
14737 end New_Internal_Entity;
14738
14739 -----------------
14740 -- Next_Actual --
14741 -----------------
14742
14743 function Next_Actual (Actual_Id : Node_Id) return Node_Id is
14744 N : Node_Id;
14745
14746 begin
14747 -- If we are pointing at a positional parameter, it is a member of a
14748 -- node list (the list of parameters), and the next parameter is the
14749 -- next node on the list, unless we hit a parameter association, then
14750 -- we shift to using the chain whose head is the First_Named_Actual in
14751 -- the parent, and then is threaded using the Next_Named_Actual of the
14752 -- Parameter_Association. All this fiddling is because the original node
14753 -- list is in the textual call order, and what we need is the
14754 -- declaration order.
14755
14756 if Is_List_Member (Actual_Id) then
14757 N := Next (Actual_Id);
14758
14759 if Nkind (N) = N_Parameter_Association then
14760 return First_Named_Actual (Parent (Actual_Id));
14761 else
14762 return N;
14763 end if;
14764
14765 else
14766 return Next_Named_Actual (Parent (Actual_Id));
14767 end if;
14768 end Next_Actual;
14769
14770 procedure Next_Actual (Actual_Id : in out Node_Id) is
14771 begin
14772 Actual_Id := Next_Actual (Actual_Id);
14773 end Next_Actual;
14774
14775 -----------------------
14776 -- Normalize_Actuals --
14777 -----------------------
14778
14779 -- Chain actuals according to formals of subprogram. If there are no named
14780 -- associations, the chain is simply the list of Parameter Associations,
14781 -- since the order is the same as the declaration order. If there are named
14782 -- associations, then the First_Named_Actual field in the N_Function_Call
14783 -- or N_Procedure_Call_Statement node points to the Parameter_Association
14784 -- node for the parameter that comes first in declaration order. The
14785 -- remaining named parameters are then chained in declaration order using
14786 -- Next_Named_Actual.
14787
14788 -- This routine also verifies that the number of actuals is compatible with
14789 -- the number and default values of formals, but performs no type checking
14790 -- (type checking is done by the caller).
14791
14792 -- If the matching succeeds, Success is set to True and the caller proceeds
14793 -- with type-checking. If the match is unsuccessful, then Success is set to
14794 -- False, and the caller attempts a different interpretation, if there is
14795 -- one.
14796
14797 -- If the flag Report is on, the call is not overloaded, and a failure to
14798 -- match can be reported here, rather than in the caller.
14799
14800 procedure Normalize_Actuals
14801 (N : Node_Id;
14802 S : Entity_Id;
14803 Report : Boolean;
14804 Success : out Boolean)
14805 is
14806 Actuals : constant List_Id := Parameter_Associations (N);
14807 Actual : Node_Id := Empty;
14808 Formal : Entity_Id;
14809 Last : Node_Id := Empty;
14810 First_Named : Node_Id := Empty;
14811 Found : Boolean;
14812
14813 Formals_To_Match : Integer := 0;
14814 Actuals_To_Match : Integer := 0;
14815
14816 procedure Chain (A : Node_Id);
14817 -- Add named actual at the proper place in the list, using the
14818 -- Next_Named_Actual link.
14819
14820 function Reporting return Boolean;
14821 -- Determines if an error is to be reported. To report an error, we
14822 -- need Report to be True, and also we do not report errors caused
14823 -- by calls to init procs that occur within other init procs. Such
14824 -- errors must always be cascaded errors, since if all the types are
14825 -- declared correctly, the compiler will certainly build decent calls.
14826
14827 -----------
14828 -- Chain --
14829 -----------
14830
14831 procedure Chain (A : Node_Id) is
14832 begin
14833 if No (Last) then
14834
14835 -- Call node points to first actual in list
14836
14837 Set_First_Named_Actual (N, Explicit_Actual_Parameter (A));
14838
14839 else
14840 Set_Next_Named_Actual (Last, Explicit_Actual_Parameter (A));
14841 end if;
14842
14843 Last := A;
14844 Set_Next_Named_Actual (Last, Empty);
14845 end Chain;
14846
14847 ---------------
14848 -- Reporting --
14849 ---------------
14850
14851 function Reporting return Boolean is
14852 begin
14853 if not Report then
14854 return False;
14855
14856 elsif not Within_Init_Proc then
14857 return True;
14858
14859 elsif Is_Init_Proc (Entity (Name (N))) then
14860 return False;
14861
14862 else
14863 return True;
14864 end if;
14865 end Reporting;
14866
14867 -- Start of processing for Normalize_Actuals
14868
14869 begin
14870 if Is_Access_Type (S) then
14871
14872 -- The name in the call is a function call that returns an access
14873 -- to subprogram. The designated type has the list of formals.
14874
14875 Formal := First_Formal (Designated_Type (S));
14876 else
14877 Formal := First_Formal (S);
14878 end if;
14879
14880 while Present (Formal) loop
14881 Formals_To_Match := Formals_To_Match + 1;
14882 Next_Formal (Formal);
14883 end loop;
14884
14885 -- Find if there is a named association, and verify that no positional
14886 -- associations appear after named ones.
14887
14888 if Present (Actuals) then
14889 Actual := First (Actuals);
14890 end if;
14891
14892 while Present (Actual)
14893 and then Nkind (Actual) /= N_Parameter_Association
14894 loop
14895 Actuals_To_Match := Actuals_To_Match + 1;
14896 Next (Actual);
14897 end loop;
14898
14899 if No (Actual) and Actuals_To_Match = Formals_To_Match then
14900
14901 -- Most common case: positional notation, no defaults
14902
14903 Success := True;
14904 return;
14905
14906 elsif Actuals_To_Match > Formals_To_Match then
14907
14908 -- Too many actuals: will not work
14909
14910 if Reporting then
14911 if Is_Entity_Name (Name (N)) then
14912 Error_Msg_N ("too many arguments in call to&", Name (N));
14913 else
14914 Error_Msg_N ("too many arguments in call", N);
14915 end if;
14916 end if;
14917
14918 Success := False;
14919 return;
14920 end if;
14921
14922 First_Named := Actual;
14923
14924 while Present (Actual) loop
14925 if Nkind (Actual) /= N_Parameter_Association then
14926 Error_Msg_N
14927 ("positional parameters not allowed after named ones", Actual);
14928 Success := False;
14929 return;
14930
14931 else
14932 Actuals_To_Match := Actuals_To_Match + 1;
14933 end if;
14934
14935 Next (Actual);
14936 end loop;
14937
14938 if Present (Actuals) then
14939 Actual := First (Actuals);
14940 end if;
14941
14942 Formal := First_Formal (S);
14943 while Present (Formal) loop
14944
14945 -- Match the formals in order. If the corresponding actual is
14946 -- positional, nothing to do. Else scan the list of named actuals
14947 -- to find the one with the right name.
14948
14949 if Present (Actual)
14950 and then Nkind (Actual) /= N_Parameter_Association
14951 then
14952 Next (Actual);
14953 Actuals_To_Match := Actuals_To_Match - 1;
14954 Formals_To_Match := Formals_To_Match - 1;
14955
14956 else
14957 -- For named parameters, search the list of actuals to find
14958 -- one that matches the next formal name.
14959
14960 Actual := First_Named;
14961 Found := False;
14962 while Present (Actual) loop
14963 if Chars (Selector_Name (Actual)) = Chars (Formal) then
14964 Found := True;
14965 Chain (Actual);
14966 Actuals_To_Match := Actuals_To_Match - 1;
14967 Formals_To_Match := Formals_To_Match - 1;
14968 exit;
14969 end if;
14970
14971 Next (Actual);
14972 end loop;
14973
14974 if not Found then
14975 if Ekind (Formal) /= E_In_Parameter
14976 or else No (Default_Value (Formal))
14977 then
14978 if Reporting then
14979 if (Comes_From_Source (S)
14980 or else Sloc (S) = Standard_Location)
14981 and then Is_Overloadable (S)
14982 then
14983 if No (Actuals)
14984 and then
14985 Nkind_In (Parent (N), N_Procedure_Call_Statement,
14986 N_Function_Call,
14987 N_Parameter_Association)
14988 and then Ekind (S) /= E_Function
14989 then
14990 Set_Etype (N, Etype (S));
14991
14992 else
14993 Error_Msg_Name_1 := Chars (S);
14994 Error_Msg_Sloc := Sloc (S);
14995 Error_Msg_NE
14996 ("missing argument for parameter & "
14997 & "in call to % declared #", N, Formal);
14998 end if;
14999
15000 elsif Is_Overloadable (S) then
15001 Error_Msg_Name_1 := Chars (S);
15002
15003 -- Point to type derivation that generated the
15004 -- operation.
15005
15006 Error_Msg_Sloc := Sloc (Parent (S));
15007
15008 Error_Msg_NE
15009 ("missing argument for parameter & "
15010 & "in call to % (inherited) #", N, Formal);
15011
15012 else
15013 Error_Msg_NE
15014 ("missing argument for parameter &", N, Formal);
15015 end if;
15016 end if;
15017
15018 Success := False;
15019 return;
15020
15021 else
15022 Formals_To_Match := Formals_To_Match - 1;
15023 end if;
15024 end if;
15025 end if;
15026
15027 Next_Formal (Formal);
15028 end loop;
15029
15030 if Formals_To_Match = 0 and then Actuals_To_Match = 0 then
15031 Success := True;
15032 return;
15033
15034 else
15035 if Reporting then
15036
15037 -- Find some superfluous named actual that did not get
15038 -- attached to the list of associations.
15039
15040 Actual := First (Actuals);
15041 while Present (Actual) loop
15042 if Nkind (Actual) = N_Parameter_Association
15043 and then Actual /= Last
15044 and then No (Next_Named_Actual (Actual))
15045 then
15046 Error_Msg_N ("unmatched actual & in call",
15047 Selector_Name (Actual));
15048 exit;
15049 end if;
15050
15051 Next (Actual);
15052 end loop;
15053 end if;
15054
15055 Success := False;
15056 return;
15057 end if;
15058 end Normalize_Actuals;
15059
15060 --------------------------------
15061 -- Note_Possible_Modification --
15062 --------------------------------
15063
15064 procedure Note_Possible_Modification (N : Node_Id; Sure : Boolean) is
15065 Modification_Comes_From_Source : constant Boolean :=
15066 Comes_From_Source (Parent (N));
15067
15068 Ent : Entity_Id;
15069 Exp : Node_Id;
15070
15071 begin
15072 -- Loop to find referenced entity, if there is one
15073
15074 Exp := N;
15075 loop
15076 Ent := Empty;
15077
15078 if Is_Entity_Name (Exp) then
15079 Ent := Entity (Exp);
15080
15081 -- If the entity is missing, it is an undeclared identifier,
15082 -- and there is nothing to annotate.
15083
15084 if No (Ent) then
15085 return;
15086 end if;
15087
15088 elsif Nkind (Exp) = N_Explicit_Dereference then
15089 declare
15090 P : constant Node_Id := Prefix (Exp);
15091
15092 begin
15093 -- In formal verification mode, keep track of all reads and
15094 -- writes through explicit dereferences.
15095
15096 if GNATprove_Mode then
15097 SPARK_Specific.Generate_Dereference (N, 'm');
15098 end if;
15099
15100 if Nkind (P) = N_Selected_Component
15101 and then Present (Entry_Formal (Entity (Selector_Name (P))))
15102 then
15103 -- Case of a reference to an entry formal
15104
15105 Ent := Entry_Formal (Entity (Selector_Name (P)));
15106
15107 elsif Nkind (P) = N_Identifier
15108 and then Nkind (Parent (Entity (P))) = N_Object_Declaration
15109 and then Present (Expression (Parent (Entity (P))))
15110 and then Nkind (Expression (Parent (Entity (P)))) =
15111 N_Reference
15112 then
15113 -- Case of a reference to a value on which side effects have
15114 -- been removed.
15115
15116 Exp := Prefix (Expression (Parent (Entity (P))));
15117 goto Continue;
15118
15119 else
15120 return;
15121 end if;
15122 end;
15123
15124 elsif Nkind_In (Exp, N_Type_Conversion,
15125 N_Unchecked_Type_Conversion)
15126 then
15127 Exp := Expression (Exp);
15128 goto Continue;
15129
15130 elsif Nkind_In (Exp, N_Slice,
15131 N_Indexed_Component,
15132 N_Selected_Component)
15133 then
15134 -- Special check, if the prefix is an access type, then return
15135 -- since we are modifying the thing pointed to, not the prefix.
15136 -- When we are expanding, most usually the prefix is replaced
15137 -- by an explicit dereference, and this test is not needed, but
15138 -- in some cases (notably -gnatc mode and generics) when we do
15139 -- not do full expansion, we need this special test.
15140
15141 if Is_Access_Type (Etype (Prefix (Exp))) then
15142 return;
15143
15144 -- Otherwise go to prefix and keep going
15145
15146 else
15147 Exp := Prefix (Exp);
15148 goto Continue;
15149 end if;
15150
15151 -- All other cases, not a modification
15152
15153 else
15154 return;
15155 end if;
15156
15157 -- Now look for entity being referenced
15158
15159 if Present (Ent) then
15160 if Is_Object (Ent) then
15161 if Comes_From_Source (Exp)
15162 or else Modification_Comes_From_Source
15163 then
15164 -- Give warning if pragma unmodified given and we are
15165 -- sure this is a modification.
15166
15167 if Has_Pragma_Unmodified (Ent) and then Sure then
15168 Error_Msg_NE ("??pragma Unmodified given for &!", N, Ent);
15169 end if;
15170
15171 Set_Never_Set_In_Source (Ent, False);
15172 end if;
15173
15174 Set_Is_True_Constant (Ent, False);
15175 Set_Current_Value (Ent, Empty);
15176 Set_Is_Known_Null (Ent, False);
15177
15178 if not Can_Never_Be_Null (Ent) then
15179 Set_Is_Known_Non_Null (Ent, False);
15180 end if;
15181
15182 -- Follow renaming chain
15183
15184 if (Ekind (Ent) = E_Variable or else Ekind (Ent) = E_Constant)
15185 and then Present (Renamed_Object (Ent))
15186 then
15187 Exp := Renamed_Object (Ent);
15188
15189 -- If the entity is the loop variable in an iteration over
15190 -- a container, retrieve container expression to indicate
15191 -- possible modificastion.
15192
15193 if Present (Related_Expression (Ent))
15194 and then Nkind (Parent (Related_Expression (Ent))) =
15195 N_Iterator_Specification
15196 then
15197 Exp := Original_Node (Related_Expression (Ent));
15198 end if;
15199
15200 goto Continue;
15201
15202 -- The expression may be the renaming of a subcomponent of an
15203 -- array or container. The assignment to the subcomponent is
15204 -- a modification of the container.
15205
15206 elsif Comes_From_Source (Original_Node (Exp))
15207 and then Nkind_In (Original_Node (Exp), N_Selected_Component,
15208 N_Indexed_Component)
15209 then
15210 Exp := Prefix (Original_Node (Exp));
15211 goto Continue;
15212 end if;
15213
15214 -- Generate a reference only if the assignment comes from
15215 -- source. This excludes, for example, calls to a dispatching
15216 -- assignment operation when the left-hand side is tagged. In
15217 -- GNATprove mode, we need those references also on generated
15218 -- code, as these are used to compute the local effects of
15219 -- subprograms.
15220
15221 if Modification_Comes_From_Source or GNATprove_Mode then
15222 Generate_Reference (Ent, Exp, 'm');
15223
15224 -- If the target of the assignment is the bound variable
15225 -- in an iterator, indicate that the corresponding array
15226 -- or container is also modified.
15227
15228 if Ada_Version >= Ada_2012
15229 and then Nkind (Parent (Ent)) = N_Iterator_Specification
15230 then
15231 declare
15232 Domain : constant Node_Id := Name (Parent (Ent));
15233
15234 begin
15235 -- TBD : in the full version of the construct, the
15236 -- domain of iteration can be given by an expression.
15237
15238 if Is_Entity_Name (Domain) then
15239 Generate_Reference (Entity (Domain), Exp, 'm');
15240 Set_Is_True_Constant (Entity (Domain), False);
15241 Set_Never_Set_In_Source (Entity (Domain), False);
15242 end if;
15243 end;
15244 end if;
15245 end if;
15246
15247 Check_Nested_Access (Ent);
15248 end if;
15249
15250 Kill_Checks (Ent);
15251
15252 -- If we are sure this is a modification from source, and we know
15253 -- this modifies a constant, then give an appropriate warning.
15254
15255 if Overlays_Constant (Ent)
15256 and then (Modification_Comes_From_Source and Sure)
15257 then
15258 declare
15259 A : constant Node_Id := Address_Clause (Ent);
15260 begin
15261 if Present (A) then
15262 declare
15263 Exp : constant Node_Id := Expression (A);
15264 begin
15265 if Nkind (Exp) = N_Attribute_Reference
15266 and then Attribute_Name (Exp) = Name_Address
15267 and then Is_Entity_Name (Prefix (Exp))
15268 then
15269 Error_Msg_Sloc := Sloc (A);
15270 Error_Msg_NE
15271 ("constant& may be modified via address "
15272 & "clause#??", N, Entity (Prefix (Exp)));
15273 end if;
15274 end;
15275 end if;
15276 end;
15277 end if;
15278
15279 return;
15280 end if;
15281
15282 <<Continue>>
15283 null;
15284 end loop;
15285 end Note_Possible_Modification;
15286
15287 -------------------------
15288 -- Object_Access_Level --
15289 -------------------------
15290
15291 -- Returns the static accessibility level of the view denoted by Obj. Note
15292 -- that the value returned is the result of a call to Scope_Depth. Only
15293 -- scope depths associated with dynamic scopes can actually be returned.
15294 -- Since only relative levels matter for accessibility checking, the fact
15295 -- that the distance between successive levels of accessibility is not
15296 -- always one is immaterial (invariant: if level(E2) is deeper than
15297 -- level(E1), then Scope_Depth(E1) < Scope_Depth(E2)).
15298
15299 function Object_Access_Level (Obj : Node_Id) return Uint is
15300 function Is_Interface_Conversion (N : Node_Id) return Boolean;
15301 -- Determine whether N is a construct of the form
15302 -- Some_Type (Operand._tag'Address)
15303 -- This construct appears in the context of dispatching calls.
15304
15305 function Reference_To (Obj : Node_Id) return Node_Id;
15306 -- An explicit dereference is created when removing side-effects from
15307 -- expressions for constraint checking purposes. In this case a local
15308 -- access type is created for it. The correct access level is that of
15309 -- the original source node. We detect this case by noting that the
15310 -- prefix of the dereference is created by an object declaration whose
15311 -- initial expression is a reference.
15312
15313 -----------------------------
15314 -- Is_Interface_Conversion --
15315 -----------------------------
15316
15317 function Is_Interface_Conversion (N : Node_Id) return Boolean is
15318 begin
15319 return Nkind (N) = N_Unchecked_Type_Conversion
15320 and then Nkind (Expression (N)) = N_Attribute_Reference
15321 and then Attribute_Name (Expression (N)) = Name_Address;
15322 end Is_Interface_Conversion;
15323
15324 ------------------
15325 -- Reference_To --
15326 ------------------
15327
15328 function Reference_To (Obj : Node_Id) return Node_Id is
15329 Pref : constant Node_Id := Prefix (Obj);
15330 begin
15331 if Is_Entity_Name (Pref)
15332 and then Nkind (Parent (Entity (Pref))) = N_Object_Declaration
15333 and then Present (Expression (Parent (Entity (Pref))))
15334 and then Nkind (Expression (Parent (Entity (Pref)))) = N_Reference
15335 then
15336 return (Prefix (Expression (Parent (Entity (Pref)))));
15337 else
15338 return Empty;
15339 end if;
15340 end Reference_To;
15341
15342 -- Local variables
15343
15344 E : Entity_Id;
15345
15346 -- Start of processing for Object_Access_Level
15347
15348 begin
15349 if Nkind (Obj) = N_Defining_Identifier
15350 or else Is_Entity_Name (Obj)
15351 then
15352 if Nkind (Obj) = N_Defining_Identifier then
15353 E := Obj;
15354 else
15355 E := Entity (Obj);
15356 end if;
15357
15358 if Is_Prival (E) then
15359 E := Prival_Link (E);
15360 end if;
15361
15362 -- If E is a type then it denotes a current instance. For this case
15363 -- we add one to the normal accessibility level of the type to ensure
15364 -- that current instances are treated as always being deeper than
15365 -- than the level of any visible named access type (see 3.10.2(21)).
15366
15367 if Is_Type (E) then
15368 return Type_Access_Level (E) + 1;
15369
15370 elsif Present (Renamed_Object (E)) then
15371 return Object_Access_Level (Renamed_Object (E));
15372
15373 -- Similarly, if E is a component of the current instance of a
15374 -- protected type, any instance of it is assumed to be at a deeper
15375 -- level than the type. For a protected object (whose type is an
15376 -- anonymous protected type) its components are at the same level
15377 -- as the type itself.
15378
15379 elsif not Is_Overloadable (E)
15380 and then Ekind (Scope (E)) = E_Protected_Type
15381 and then Comes_From_Source (Scope (E))
15382 then
15383 return Type_Access_Level (Scope (E)) + 1;
15384
15385 else
15386 -- Aliased formals take their access level from the point of call.
15387 -- This is smaller than the level of the subprogram itself.
15388
15389 if Is_Formal (E) and then Is_Aliased (E) then
15390 return Type_Access_Level (Etype (E));
15391
15392 else
15393 return Scope_Depth (Enclosing_Dynamic_Scope (E));
15394 end if;
15395 end if;
15396
15397 elsif Nkind (Obj) = N_Selected_Component then
15398 if Is_Access_Type (Etype (Prefix (Obj))) then
15399 return Type_Access_Level (Etype (Prefix (Obj)));
15400 else
15401 return Object_Access_Level (Prefix (Obj));
15402 end if;
15403
15404 elsif Nkind (Obj) = N_Indexed_Component then
15405 if Is_Access_Type (Etype (Prefix (Obj))) then
15406 return Type_Access_Level (Etype (Prefix (Obj)));
15407 else
15408 return Object_Access_Level (Prefix (Obj));
15409 end if;
15410
15411 elsif Nkind (Obj) = N_Explicit_Dereference then
15412
15413 -- If the prefix is a selected access discriminant then we make a
15414 -- recursive call on the prefix, which will in turn check the level
15415 -- of the prefix object of the selected discriminant.
15416
15417 -- In Ada 2012, if the discriminant has implicit dereference and
15418 -- the context is a selected component, treat this as an object of
15419 -- unknown scope (see below). This is necessary in compile-only mode;
15420 -- otherwise expansion will already have transformed the prefix into
15421 -- a temporary.
15422
15423 if Nkind (Prefix (Obj)) = N_Selected_Component
15424 and then Ekind (Etype (Prefix (Obj))) = E_Anonymous_Access_Type
15425 and then
15426 Ekind (Entity (Selector_Name (Prefix (Obj)))) = E_Discriminant
15427 and then
15428 (not Has_Implicit_Dereference
15429 (Entity (Selector_Name (Prefix (Obj))))
15430 or else Nkind (Parent (Obj)) /= N_Selected_Component)
15431 then
15432 return Object_Access_Level (Prefix (Obj));
15433
15434 -- Detect an interface conversion in the context of a dispatching
15435 -- call. Use the original form of the conversion to find the access
15436 -- level of the operand.
15437
15438 elsif Is_Interface (Etype (Obj))
15439 and then Is_Interface_Conversion (Prefix (Obj))
15440 and then Nkind (Original_Node (Obj)) = N_Type_Conversion
15441 then
15442 return Object_Access_Level (Original_Node (Obj));
15443
15444 elsif not Comes_From_Source (Obj) then
15445 declare
15446 Ref : constant Node_Id := Reference_To (Obj);
15447 begin
15448 if Present (Ref) then
15449 return Object_Access_Level (Ref);
15450 else
15451 return Type_Access_Level (Etype (Prefix (Obj)));
15452 end if;
15453 end;
15454
15455 else
15456 return Type_Access_Level (Etype (Prefix (Obj)));
15457 end if;
15458
15459 elsif Nkind_In (Obj, N_Type_Conversion, N_Unchecked_Type_Conversion) then
15460 return Object_Access_Level (Expression (Obj));
15461
15462 elsif Nkind (Obj) = N_Function_Call then
15463
15464 -- Function results are objects, so we get either the access level of
15465 -- the function or, in the case of an indirect call, the level of the
15466 -- access-to-subprogram type. (This code is used for Ada 95, but it
15467 -- looks wrong, because it seems that we should be checking the level
15468 -- of the call itself, even for Ada 95. However, using the Ada 2005
15469 -- version of the code causes regressions in several tests that are
15470 -- compiled with -gnat95. ???)
15471
15472 if Ada_Version < Ada_2005 then
15473 if Is_Entity_Name (Name (Obj)) then
15474 return Subprogram_Access_Level (Entity (Name (Obj)));
15475 else
15476 return Type_Access_Level (Etype (Prefix (Name (Obj))));
15477 end if;
15478
15479 -- For Ada 2005, the level of the result object of a function call is
15480 -- defined to be the level of the call's innermost enclosing master.
15481 -- We determine that by querying the depth of the innermost enclosing
15482 -- dynamic scope.
15483
15484 else
15485 Return_Master_Scope_Depth_Of_Call : declare
15486
15487 function Innermost_Master_Scope_Depth
15488 (N : Node_Id) return Uint;
15489 -- Returns the scope depth of the given node's innermost
15490 -- enclosing dynamic scope (effectively the accessibility
15491 -- level of the innermost enclosing master).
15492
15493 ----------------------------------
15494 -- Innermost_Master_Scope_Depth --
15495 ----------------------------------
15496
15497 function Innermost_Master_Scope_Depth
15498 (N : Node_Id) return Uint
15499 is
15500 Node_Par : Node_Id := Parent (N);
15501
15502 begin
15503 -- Locate the nearest enclosing node (by traversing Parents)
15504 -- that Defining_Entity can be applied to, and return the
15505 -- depth of that entity's nearest enclosing dynamic scope.
15506
15507 while Present (Node_Par) loop
15508 case Nkind (Node_Par) is
15509 when N_Component_Declaration |
15510 N_Entry_Declaration |
15511 N_Formal_Object_Declaration |
15512 N_Formal_Type_Declaration |
15513 N_Full_Type_Declaration |
15514 N_Incomplete_Type_Declaration |
15515 N_Loop_Parameter_Specification |
15516 N_Object_Declaration |
15517 N_Protected_Type_Declaration |
15518 N_Private_Extension_Declaration |
15519 N_Private_Type_Declaration |
15520 N_Subtype_Declaration |
15521 N_Function_Specification |
15522 N_Procedure_Specification |
15523 N_Task_Type_Declaration |
15524 N_Body_Stub |
15525 N_Generic_Instantiation |
15526 N_Proper_Body |
15527 N_Implicit_Label_Declaration |
15528 N_Package_Declaration |
15529 N_Single_Task_Declaration |
15530 N_Subprogram_Declaration |
15531 N_Generic_Declaration |
15532 N_Renaming_Declaration |
15533 N_Block_Statement |
15534 N_Formal_Subprogram_Declaration |
15535 N_Abstract_Subprogram_Declaration |
15536 N_Entry_Body |
15537 N_Exception_Declaration |
15538 N_Formal_Package_Declaration |
15539 N_Number_Declaration |
15540 N_Package_Specification |
15541 N_Parameter_Specification |
15542 N_Single_Protected_Declaration |
15543 N_Subunit =>
15544
15545 return Scope_Depth
15546 (Nearest_Dynamic_Scope
15547 (Defining_Entity (Node_Par)));
15548
15549 when others =>
15550 null;
15551 end case;
15552
15553 Node_Par := Parent (Node_Par);
15554 end loop;
15555
15556 pragma Assert (False);
15557
15558 -- Should never reach the following return
15559
15560 return Scope_Depth (Current_Scope) + 1;
15561 end Innermost_Master_Scope_Depth;
15562
15563 -- Start of processing for Return_Master_Scope_Depth_Of_Call
15564
15565 begin
15566 return Innermost_Master_Scope_Depth (Obj);
15567 end Return_Master_Scope_Depth_Of_Call;
15568 end if;
15569
15570 -- For convenience we handle qualified expressions, even though they
15571 -- aren't technically object names.
15572
15573 elsif Nkind (Obj) = N_Qualified_Expression then
15574 return Object_Access_Level (Expression (Obj));
15575
15576 -- Ditto for aggregates. They have the level of the temporary that
15577 -- will hold their value.
15578
15579 elsif Nkind (Obj) = N_Aggregate then
15580 return Object_Access_Level (Current_Scope);
15581
15582 -- Otherwise return the scope level of Standard. (If there are cases
15583 -- that fall through to this point they will be treated as having
15584 -- global accessibility for now. ???)
15585
15586 else
15587 return Scope_Depth (Standard_Standard);
15588 end if;
15589 end Object_Access_Level;
15590
15591 --------------------------
15592 -- Original_Aspect_Name --
15593 --------------------------
15594
15595 function Original_Aspect_Name (N : Node_Id) return Name_Id is
15596 Pras : Node_Id;
15597 Name : Name_Id;
15598
15599 begin
15600 pragma Assert (Nkind_In (N, N_Aspect_Specification, N_Pragma));
15601 Pras := N;
15602
15603 if Is_Rewrite_Substitution (Pras)
15604 and then Nkind (Original_Node (Pras)) = N_Pragma
15605 then
15606 Pras := Original_Node (Pras);
15607 end if;
15608
15609 -- Case where we came from aspect specication
15610
15611 if Nkind (Pras) = N_Pragma and then From_Aspect_Specification (Pras) then
15612 Pras := Corresponding_Aspect (Pras);
15613 end if;
15614
15615 -- Get name from aspect or pragma
15616
15617 if Nkind (Pras) = N_Pragma then
15618 Name := Pragma_Name (Pras);
15619 else
15620 Name := Chars (Identifier (Pras));
15621 end if;
15622
15623 -- Deal with 'Class
15624
15625 if Class_Present (Pras) then
15626 case Name is
15627
15628 -- Names that need converting to special _xxx form
15629
15630 when Name_Pre |
15631 Name_Pre_Class =>
15632 Name := Name_uPre;
15633
15634 when Name_Post |
15635 Name_Post_Class =>
15636 Name := Name_uPost;
15637
15638 when Name_Invariant =>
15639 Name := Name_uInvariant;
15640
15641 when Name_Type_Invariant |
15642 Name_Type_Invariant_Class =>
15643 Name := Name_uType_Invariant;
15644
15645 -- Nothing to do for other cases (e.g. a Check that derived
15646 -- from Pre_Class and has the flag set). Also we do nothing
15647 -- if the name is already in special _xxx form.
15648
15649 when others =>
15650 null;
15651 end case;
15652 end if;
15653
15654 return Name;
15655 end Original_Aspect_Name;
15656
15657 --------------------------------------
15658 -- Original_Corresponding_Operation --
15659 --------------------------------------
15660
15661 function Original_Corresponding_Operation (S : Entity_Id) return Entity_Id
15662 is
15663 Typ : constant Entity_Id := Find_Dispatching_Type (S);
15664
15665 begin
15666 -- If S is an inherited primitive S2 the original corresponding
15667 -- operation of S is the original corresponding operation of S2
15668
15669 if Present (Alias (S))
15670 and then Find_Dispatching_Type (Alias (S)) /= Typ
15671 then
15672 return Original_Corresponding_Operation (Alias (S));
15673
15674 -- If S overrides an inherited subprogram S2 the original corresponding
15675 -- operation of S is the original corresponding operation of S2
15676
15677 elsif Present (Overridden_Operation (S)) then
15678 return Original_Corresponding_Operation (Overridden_Operation (S));
15679
15680 -- otherwise it is S itself
15681
15682 else
15683 return S;
15684 end if;
15685 end Original_Corresponding_Operation;
15686
15687 ----------------------
15688 -- Policy_In_Effect --
15689 ----------------------
15690
15691 function Policy_In_Effect (Policy : Name_Id) return Name_Id is
15692 function Policy_In_List (List : Node_Id) return Name_Id;
15693 -- Determine the the mode of a policy in a N_Pragma list
15694
15695 --------------------
15696 -- Policy_In_List --
15697 --------------------
15698
15699 function Policy_In_List (List : Node_Id) return Name_Id is
15700 Arg : Node_Id;
15701 Expr : Node_Id;
15702 Prag : Node_Id;
15703
15704 begin
15705 Prag := List;
15706 while Present (Prag) loop
15707 Arg := First (Pragma_Argument_Associations (Prag));
15708 Expr := Get_Pragma_Arg (Arg);
15709
15710 -- The current Check_Policy pragma matches the requested policy,
15711 -- return the second argument which denotes the policy identifier.
15712
15713 if Chars (Expr) = Policy then
15714 return Chars (Get_Pragma_Arg (Next (Arg)));
15715 end if;
15716
15717 Prag := Next_Pragma (Prag);
15718 end loop;
15719
15720 return No_Name;
15721 end Policy_In_List;
15722
15723 -- Local variables
15724
15725 Kind : Name_Id;
15726
15727 -- Start of processing for Policy_In_Effect
15728
15729 begin
15730 if not Is_Valid_Assertion_Kind (Policy) then
15731 raise Program_Error;
15732 end if;
15733
15734 -- Inspect all policy pragmas that appear within scopes (if any)
15735
15736 Kind := Policy_In_List (Check_Policy_List);
15737
15738 -- Inspect all configuration policy pragmas (if any)
15739
15740 if Kind = No_Name then
15741 Kind := Policy_In_List (Check_Policy_List_Config);
15742 end if;
15743
15744 -- The context lacks policy pragmas, determine the mode based on whether
15745 -- assertions are enabled at the configuration level. This ensures that
15746 -- the policy is preserved when analyzing generics.
15747
15748 if Kind = No_Name then
15749 if Assertions_Enabled_Config then
15750 Kind := Name_Check;
15751 else
15752 Kind := Name_Ignore;
15753 end if;
15754 end if;
15755
15756 return Kind;
15757 end Policy_In_Effect;
15758
15759 ----------------------------------
15760 -- Predicate_Tests_On_Arguments --
15761 ----------------------------------
15762
15763 function Predicate_Tests_On_Arguments (Subp : Entity_Id) return Boolean is
15764 begin
15765 -- Always test predicates on indirect call
15766
15767 if Ekind (Subp) = E_Subprogram_Type then
15768 return True;
15769
15770 -- Do not test predicates on call to generated default Finalize, since
15771 -- we are not interested in whether something we are finalizing (and
15772 -- typically destroying) satisfies its predicates.
15773
15774 elsif Chars (Subp) = Name_Finalize
15775 and then not Comes_From_Source (Subp)
15776 then
15777 return False;
15778
15779 -- Do not test predicates on any internally generated routines
15780
15781 elsif Is_Internal_Name (Chars (Subp)) then
15782 return False;
15783
15784 -- Do not test predicates on call to Init_Proc, since if needed the
15785 -- predicate test will occur at some other point.
15786
15787 elsif Is_Init_Proc (Subp) then
15788 return False;
15789
15790 -- Do not test predicates on call to predicate function, since this
15791 -- would cause infinite recursion.
15792
15793 elsif Ekind (Subp) = E_Function
15794 and then (Is_Predicate_Function (Subp)
15795 or else
15796 Is_Predicate_Function_M (Subp))
15797 then
15798 return False;
15799
15800 -- For now, no other exceptions
15801
15802 else
15803 return True;
15804 end if;
15805 end Predicate_Tests_On_Arguments;
15806
15807 -----------------------
15808 -- Private_Component --
15809 -----------------------
15810
15811 function Private_Component (Type_Id : Entity_Id) return Entity_Id is
15812 Ancestor : constant Entity_Id := Base_Type (Type_Id);
15813
15814 function Trace_Components
15815 (T : Entity_Id;
15816 Check : Boolean) return Entity_Id;
15817 -- Recursive function that does the work, and checks against circular
15818 -- definition for each subcomponent type.
15819
15820 ----------------------
15821 -- Trace_Components --
15822 ----------------------
15823
15824 function Trace_Components
15825 (T : Entity_Id;
15826 Check : Boolean) return Entity_Id
15827 is
15828 Btype : constant Entity_Id := Base_Type (T);
15829 Component : Entity_Id;
15830 P : Entity_Id;
15831 Candidate : Entity_Id := Empty;
15832
15833 begin
15834 if Check and then Btype = Ancestor then
15835 Error_Msg_N ("circular type definition", Type_Id);
15836 return Any_Type;
15837 end if;
15838
15839 if Is_Private_Type (Btype) and then not Is_Generic_Type (Btype) then
15840 if Present (Full_View (Btype))
15841 and then Is_Record_Type (Full_View (Btype))
15842 and then not Is_Frozen (Btype)
15843 then
15844 -- To indicate that the ancestor depends on a private type, the
15845 -- current Btype is sufficient. However, to check for circular
15846 -- definition we must recurse on the full view.
15847
15848 Candidate := Trace_Components (Full_View (Btype), True);
15849
15850 if Candidate = Any_Type then
15851 return Any_Type;
15852 else
15853 return Btype;
15854 end if;
15855
15856 else
15857 return Btype;
15858 end if;
15859
15860 elsif Is_Array_Type (Btype) then
15861 return Trace_Components (Component_Type (Btype), True);
15862
15863 elsif Is_Record_Type (Btype) then
15864 Component := First_Entity (Btype);
15865 while Present (Component)
15866 and then Comes_From_Source (Component)
15867 loop
15868 -- Skip anonymous types generated by constrained components
15869
15870 if not Is_Type (Component) then
15871 P := Trace_Components (Etype (Component), True);
15872
15873 if Present (P) then
15874 if P = Any_Type then
15875 return P;
15876 else
15877 Candidate := P;
15878 end if;
15879 end if;
15880 end if;
15881
15882 Next_Entity (Component);
15883 end loop;
15884
15885 return Candidate;
15886
15887 else
15888 return Empty;
15889 end if;
15890 end Trace_Components;
15891
15892 -- Start of processing for Private_Component
15893
15894 begin
15895 return Trace_Components (Type_Id, False);
15896 end Private_Component;
15897
15898 ---------------------------
15899 -- Primitive_Names_Match --
15900 ---------------------------
15901
15902 function Primitive_Names_Match (E1, E2 : Entity_Id) return Boolean is
15903
15904 function Non_Internal_Name (E : Entity_Id) return Name_Id;
15905 -- Given an internal name, returns the corresponding non-internal name
15906
15907 ------------------------
15908 -- Non_Internal_Name --
15909 ------------------------
15910
15911 function Non_Internal_Name (E : Entity_Id) return Name_Id is
15912 begin
15913 Get_Name_String (Chars (E));
15914 Name_Len := Name_Len - 1;
15915 return Name_Find;
15916 end Non_Internal_Name;
15917
15918 -- Start of processing for Primitive_Names_Match
15919
15920 begin
15921 pragma Assert (Present (E1) and then Present (E2));
15922
15923 return Chars (E1) = Chars (E2)
15924 or else
15925 (not Is_Internal_Name (Chars (E1))
15926 and then Is_Internal_Name (Chars (E2))
15927 and then Non_Internal_Name (E2) = Chars (E1))
15928 or else
15929 (not Is_Internal_Name (Chars (E2))
15930 and then Is_Internal_Name (Chars (E1))
15931 and then Non_Internal_Name (E1) = Chars (E2))
15932 or else
15933 (Is_Predefined_Dispatching_Operation (E1)
15934 and then Is_Predefined_Dispatching_Operation (E2)
15935 and then Same_TSS (E1, E2))
15936 or else
15937 (Is_Init_Proc (E1) and then Is_Init_Proc (E2));
15938 end Primitive_Names_Match;
15939
15940 -----------------------
15941 -- Process_End_Label --
15942 -----------------------
15943
15944 procedure Process_End_Label
15945 (N : Node_Id;
15946 Typ : Character;
15947 Ent : Entity_Id)
15948 is
15949 Loc : Source_Ptr;
15950 Nam : Node_Id;
15951 Scop : Entity_Id;
15952
15953 Label_Ref : Boolean;
15954 -- Set True if reference to end label itself is required
15955
15956 Endl : Node_Id;
15957 -- Gets set to the operator symbol or identifier that references the
15958 -- entity Ent. For the child unit case, this is the identifier from the
15959 -- designator. For other cases, this is simply Endl.
15960
15961 procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id);
15962 -- N is an identifier node that appears as a parent unit reference in
15963 -- the case where Ent is a child unit. This procedure generates an
15964 -- appropriate cross-reference entry. E is the corresponding entity.
15965
15966 -------------------------
15967 -- Generate_Parent_Ref --
15968 -------------------------
15969
15970 procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id) is
15971 begin
15972 -- If names do not match, something weird, skip reference
15973
15974 if Chars (E) = Chars (N) then
15975
15976 -- Generate the reference. We do NOT consider this as a reference
15977 -- for unreferenced symbol purposes.
15978
15979 Generate_Reference (E, N, 'r', Set_Ref => False, Force => True);
15980
15981 if Style_Check then
15982 Style.Check_Identifier (N, E);
15983 end if;
15984 end if;
15985 end Generate_Parent_Ref;
15986
15987 -- Start of processing for Process_End_Label
15988
15989 begin
15990 -- If no node, ignore. This happens in some error situations, and
15991 -- also for some internally generated structures where no end label
15992 -- references are required in any case.
15993
15994 if No (N) then
15995 return;
15996 end if;
15997
15998 -- Nothing to do if no End_Label, happens for internally generated
15999 -- constructs where we don't want an end label reference anyway. Also
16000 -- nothing to do if Endl is a string literal, which means there was
16001 -- some prior error (bad operator symbol)
16002
16003 Endl := End_Label (N);
16004
16005 if No (Endl) or else Nkind (Endl) = N_String_Literal then
16006 return;
16007 end if;
16008
16009 -- Reference node is not in extended main source unit
16010
16011 if not In_Extended_Main_Source_Unit (N) then
16012
16013 -- Generally we do not collect references except for the extended
16014 -- main source unit. The one exception is the 'e' entry for a
16015 -- package spec, where it is useful for a client to have the
16016 -- ending information to define scopes.
16017
16018 if Typ /= 'e' then
16019 return;
16020
16021 else
16022 Label_Ref := False;
16023
16024 -- For this case, we can ignore any parent references, but we
16025 -- need the package name itself for the 'e' entry.
16026
16027 if Nkind (Endl) = N_Designator then
16028 Endl := Identifier (Endl);
16029 end if;
16030 end if;
16031
16032 -- Reference is in extended main source unit
16033
16034 else
16035 Label_Ref := True;
16036
16037 -- For designator, generate references for the parent entries
16038
16039 if Nkind (Endl) = N_Designator then
16040
16041 -- Generate references for the prefix if the END line comes from
16042 -- source (otherwise we do not need these references) We climb the
16043 -- scope stack to find the expected entities.
16044
16045 if Comes_From_Source (Endl) then
16046 Nam := Name (Endl);
16047 Scop := Current_Scope;
16048 while Nkind (Nam) = N_Selected_Component loop
16049 Scop := Scope (Scop);
16050 exit when No (Scop);
16051 Generate_Parent_Ref (Selector_Name (Nam), Scop);
16052 Nam := Prefix (Nam);
16053 end loop;
16054
16055 if Present (Scop) then
16056 Generate_Parent_Ref (Nam, Scope (Scop));
16057 end if;
16058 end if;
16059
16060 Endl := Identifier (Endl);
16061 end if;
16062 end if;
16063
16064 -- If the end label is not for the given entity, then either we have
16065 -- some previous error, or this is a generic instantiation for which
16066 -- we do not need to make a cross-reference in this case anyway. In
16067 -- either case we simply ignore the call.
16068
16069 if Chars (Ent) /= Chars (Endl) then
16070 return;
16071 end if;
16072
16073 -- If label was really there, then generate a normal reference and then
16074 -- adjust the location in the end label to point past the name (which
16075 -- should almost always be the semicolon).
16076
16077 Loc := Sloc (Endl);
16078
16079 if Comes_From_Source (Endl) then
16080
16081 -- If a label reference is required, then do the style check and
16082 -- generate an l-type cross-reference entry for the label
16083
16084 if Label_Ref then
16085 if Style_Check then
16086 Style.Check_Identifier (Endl, Ent);
16087 end if;
16088
16089 Generate_Reference (Ent, Endl, 'l', Set_Ref => False);
16090 end if;
16091
16092 -- Set the location to point past the label (normally this will
16093 -- mean the semicolon immediately following the label). This is
16094 -- done for the sake of the 'e' or 't' entry generated below.
16095
16096 Get_Decoded_Name_String (Chars (Endl));
16097 Set_Sloc (Endl, Sloc (Endl) + Source_Ptr (Name_Len));
16098
16099 else
16100 -- In SPARK mode, no missing label is allowed for packages and
16101 -- subprogram bodies. Detect those cases by testing whether
16102 -- Process_End_Label was called for a body (Typ = 't') or a package.
16103
16104 if Restriction_Check_Required (SPARK_05)
16105 and then (Typ = 't' or else Ekind (Ent) = E_Package)
16106 then
16107 Error_Msg_Node_1 := Endl;
16108 Check_SPARK_05_Restriction
16109 ("`END &` required", Endl, Force => True);
16110 end if;
16111 end if;
16112
16113 -- Now generate the e/t reference
16114
16115 Generate_Reference (Ent, Endl, Typ, Set_Ref => False, Force => True);
16116
16117 -- Restore Sloc, in case modified above, since we have an identifier
16118 -- and the normal Sloc should be left set in the tree.
16119
16120 Set_Sloc (Endl, Loc);
16121 end Process_End_Label;
16122
16123 ----------------
16124 -- Referenced --
16125 ----------------
16126
16127 function Referenced (Id : Entity_Id; Expr : Node_Id) return Boolean is
16128 Seen : Boolean := False;
16129
16130 function Is_Reference (N : Node_Id) return Traverse_Result;
16131 -- Determine whether node N denotes a reference to Id. If this is the
16132 -- case, set global flag Seen to True and stop the traversal.
16133
16134 ------------------
16135 -- Is_Reference --
16136 ------------------
16137
16138 function Is_Reference (N : Node_Id) return Traverse_Result is
16139 begin
16140 if Is_Entity_Name (N)
16141 and then Present (Entity (N))
16142 and then Entity (N) = Id
16143 then
16144 Seen := True;
16145 return Abandon;
16146 else
16147 return OK;
16148 end if;
16149 end Is_Reference;
16150
16151 procedure Inspect_Expression is new Traverse_Proc (Is_Reference);
16152
16153 -- Start of processing for Referenced
16154
16155 begin
16156 Inspect_Expression (Expr);
16157 return Seen;
16158 end Referenced;
16159
16160 ------------------------------------
16161 -- References_Generic_Formal_Type --
16162 ------------------------------------
16163
16164 function References_Generic_Formal_Type (N : Node_Id) return Boolean is
16165
16166 function Process (N : Node_Id) return Traverse_Result;
16167 -- Process one node in search for generic formal type
16168
16169 -------------
16170 -- Process --
16171 -------------
16172
16173 function Process (N : Node_Id) return Traverse_Result is
16174 begin
16175 if Nkind (N) in N_Has_Entity then
16176 declare
16177 E : constant Entity_Id := Entity (N);
16178 begin
16179 if Present (E) then
16180 if Is_Generic_Type (E) then
16181 return Abandon;
16182 elsif Present (Etype (E))
16183 and then Is_Generic_Type (Etype (E))
16184 then
16185 return Abandon;
16186 end if;
16187 end if;
16188 end;
16189 end if;
16190
16191 return Atree.OK;
16192 end Process;
16193
16194 function Traverse is new Traverse_Func (Process);
16195 -- Traverse tree to look for generic type
16196
16197 begin
16198 if Inside_A_Generic then
16199 return Traverse (N) = Abandon;
16200 else
16201 return False;
16202 end if;
16203 end References_Generic_Formal_Type;
16204
16205 --------------------
16206 -- Remove_Homonym --
16207 --------------------
16208
16209 procedure Remove_Homonym (E : Entity_Id) is
16210 Prev : Entity_Id := Empty;
16211 H : Entity_Id;
16212
16213 begin
16214 if E = Current_Entity (E) then
16215 if Present (Homonym (E)) then
16216 Set_Current_Entity (Homonym (E));
16217 else
16218 Set_Name_Entity_Id (Chars (E), Empty);
16219 end if;
16220
16221 else
16222 H := Current_Entity (E);
16223 while Present (H) and then H /= E loop
16224 Prev := H;
16225 H := Homonym (H);
16226 end loop;
16227
16228 -- If E is not on the homonym chain, nothing to do
16229
16230 if Present (H) then
16231 Set_Homonym (Prev, Homonym (E));
16232 end if;
16233 end if;
16234 end Remove_Homonym;
16235
16236 ---------------------
16237 -- Rep_To_Pos_Flag --
16238 ---------------------
16239
16240 function Rep_To_Pos_Flag (E : Entity_Id; Loc : Source_Ptr) return Node_Id is
16241 begin
16242 return New_Occurrence_Of
16243 (Boolean_Literals (not Range_Checks_Suppressed (E)), Loc);
16244 end Rep_To_Pos_Flag;
16245
16246 --------------------
16247 -- Require_Entity --
16248 --------------------
16249
16250 procedure Require_Entity (N : Node_Id) is
16251 begin
16252 if Is_Entity_Name (N) and then No (Entity (N)) then
16253 if Total_Errors_Detected /= 0 then
16254 Set_Entity (N, Any_Id);
16255 else
16256 raise Program_Error;
16257 end if;
16258 end if;
16259 end Require_Entity;
16260
16261 -------------------------------
16262 -- Requires_State_Refinement --
16263 -------------------------------
16264
16265 function Requires_State_Refinement
16266 (Spec_Id : Entity_Id;
16267 Body_Id : Entity_Id) return Boolean
16268 is
16269 function Mode_Is_Off (Prag : Node_Id) return Boolean;
16270 -- Given pragma SPARK_Mode, determine whether the mode is Off
16271
16272 -----------------
16273 -- Mode_Is_Off --
16274 -----------------
16275
16276 function Mode_Is_Off (Prag : Node_Id) return Boolean is
16277 Mode : Node_Id;
16278
16279 begin
16280 -- The default SPARK mode is On
16281
16282 if No (Prag) then
16283 return False;
16284 end if;
16285
16286 Mode := Get_Pragma_Arg (First (Pragma_Argument_Associations (Prag)));
16287
16288 -- Then the pragma lacks an argument, the default mode is On
16289
16290 if No (Mode) then
16291 return False;
16292 else
16293 return Chars (Mode) = Name_Off;
16294 end if;
16295 end Mode_Is_Off;
16296
16297 -- Start of processing for Requires_State_Refinement
16298
16299 begin
16300 -- A package that does not define at least one abstract state cannot
16301 -- possibly require refinement.
16302
16303 if No (Abstract_States (Spec_Id)) then
16304 return False;
16305
16306 -- The package instroduces a single null state which does not merit
16307 -- refinement.
16308
16309 elsif Has_Null_Abstract_State (Spec_Id) then
16310 return False;
16311
16312 -- Check whether the package body is subject to pragma SPARK_Mode. If
16313 -- it is and the mode is Off, the package body is considered to be in
16314 -- regular Ada and does not require refinement.
16315
16316 elsif Mode_Is_Off (SPARK_Pragma (Body_Id)) then
16317 return False;
16318
16319 -- The body's SPARK_Mode may be inherited from a similar pragma that
16320 -- appears in the private declarations of the spec. The pragma we are
16321 -- interested appears as the second entry in SPARK_Pragma.
16322
16323 elsif Present (SPARK_Pragma (Spec_Id))
16324 and then Mode_Is_Off (Next_Pragma (SPARK_Pragma (Spec_Id)))
16325 then
16326 return False;
16327
16328 -- The spec defines at least one abstract state and the body has no way
16329 -- of circumventing the refinement.
16330
16331 else
16332 return True;
16333 end if;
16334 end Requires_State_Refinement;
16335
16336 ------------------------------
16337 -- Requires_Transient_Scope --
16338 ------------------------------
16339
16340 -- A transient scope is required when variable-sized temporaries are
16341 -- allocated in the primary or secondary stack, or when finalization
16342 -- actions must be generated before the next instruction.
16343
16344 function Requires_Transient_Scope (Id : Entity_Id) return Boolean is
16345 Typ : constant Entity_Id := Underlying_Type (Id);
16346
16347 -- Start of processing for Requires_Transient_Scope
16348
16349 begin
16350 -- This is a private type which is not completed yet. This can only
16351 -- happen in a default expression (of a formal parameter or of a
16352 -- record component). Do not expand transient scope in this case
16353
16354 if No (Typ) then
16355 return False;
16356
16357 -- Do not expand transient scope for non-existent procedure return
16358
16359 elsif Typ = Standard_Void_Type then
16360 return False;
16361
16362 -- Elementary types do not require a transient scope
16363
16364 elsif Is_Elementary_Type (Typ) then
16365 return False;
16366
16367 -- Generally, indefinite subtypes require a transient scope, since the
16368 -- back end cannot generate temporaries, since this is not a valid type
16369 -- for declaring an object. It might be possible to relax this in the
16370 -- future, e.g. by declaring the maximum possible space for the type.
16371
16372 elsif Is_Indefinite_Subtype (Typ) then
16373 return True;
16374
16375 -- Functions returning tagged types may dispatch on result so their
16376 -- returned value is allocated on the secondary stack. Controlled
16377 -- type temporaries need finalization.
16378
16379 elsif Is_Tagged_Type (Typ)
16380 or else Has_Controlled_Component (Typ)
16381 then
16382 return not Is_Value_Type (Typ);
16383
16384 -- Record type
16385
16386 elsif Is_Record_Type (Typ) then
16387 declare
16388 Comp : Entity_Id;
16389 begin
16390 Comp := First_Entity (Typ);
16391 while Present (Comp) loop
16392 if Ekind (Comp) = E_Component
16393 and then Requires_Transient_Scope (Etype (Comp))
16394 then
16395 return True;
16396 else
16397 Next_Entity (Comp);
16398 end if;
16399 end loop;
16400 end;
16401
16402 return False;
16403
16404 -- String literal types never require transient scope
16405
16406 elsif Ekind (Typ) = E_String_Literal_Subtype then
16407 return False;
16408
16409 -- Array type. Note that we already know that this is a constrained
16410 -- array, since unconstrained arrays will fail the indefinite test.
16411
16412 elsif Is_Array_Type (Typ) then
16413
16414 -- If component type requires a transient scope, the array does too
16415
16416 if Requires_Transient_Scope (Component_Type (Typ)) then
16417 return True;
16418
16419 -- Otherwise, we only need a transient scope if the size depends on
16420 -- the value of one or more discriminants.
16421
16422 else
16423 return Size_Depends_On_Discriminant (Typ);
16424 end if;
16425
16426 -- All other cases do not require a transient scope
16427
16428 else
16429 return False;
16430 end if;
16431 end Requires_Transient_Scope;
16432
16433 --------------------------
16434 -- Reset_Analyzed_Flags --
16435 --------------------------
16436
16437 procedure Reset_Analyzed_Flags (N : Node_Id) is
16438
16439 function Clear_Analyzed (N : Node_Id) return Traverse_Result;
16440 -- Function used to reset Analyzed flags in tree. Note that we do
16441 -- not reset Analyzed flags in entities, since there is no need to
16442 -- reanalyze entities, and indeed, it is wrong to do so, since it
16443 -- can result in generating auxiliary stuff more than once.
16444
16445 --------------------
16446 -- Clear_Analyzed --
16447 --------------------
16448
16449 function Clear_Analyzed (N : Node_Id) return Traverse_Result is
16450 begin
16451 if not Has_Extension (N) then
16452 Set_Analyzed (N, False);
16453 end if;
16454
16455 return OK;
16456 end Clear_Analyzed;
16457
16458 procedure Reset_Analyzed is new Traverse_Proc (Clear_Analyzed);
16459
16460 -- Start of processing for Reset_Analyzed_Flags
16461
16462 begin
16463 Reset_Analyzed (N);
16464 end Reset_Analyzed_Flags;
16465
16466 ------------------------
16467 -- Restore_SPARK_Mode --
16468 ------------------------
16469
16470 procedure Restore_SPARK_Mode (Mode : SPARK_Mode_Type) is
16471 begin
16472 SPARK_Mode := Mode;
16473 end Restore_SPARK_Mode;
16474
16475 --------------------------------
16476 -- Returns_Unconstrained_Type --
16477 --------------------------------
16478
16479 function Returns_Unconstrained_Type (Subp : Entity_Id) return Boolean is
16480 begin
16481 return Ekind (Subp) = E_Function
16482 and then not Is_Scalar_Type (Etype (Subp))
16483 and then not Is_Access_Type (Etype (Subp))
16484 and then not Is_Constrained (Etype (Subp));
16485 end Returns_Unconstrained_Type;
16486
16487 ----------------------------
16488 -- Root_Type_Of_Full_View --
16489 ----------------------------
16490
16491 function Root_Type_Of_Full_View (T : Entity_Id) return Entity_Id is
16492 Rtyp : constant Entity_Id := Root_Type (T);
16493
16494 begin
16495 -- The root type of the full view may itself be a private type. Keep
16496 -- looking for the ultimate derivation parent.
16497
16498 if Is_Private_Type (Rtyp) and then Present (Full_View (Rtyp)) then
16499 return Root_Type_Of_Full_View (Full_View (Rtyp));
16500 else
16501 return Rtyp;
16502 end if;
16503 end Root_Type_Of_Full_View;
16504
16505 ---------------------------
16506 -- Safe_To_Capture_Value --
16507 ---------------------------
16508
16509 function Safe_To_Capture_Value
16510 (N : Node_Id;
16511 Ent : Entity_Id;
16512 Cond : Boolean := False) return Boolean
16513 is
16514 begin
16515 -- The only entities for which we track constant values are variables
16516 -- which are not renamings, constants, out parameters, and in out
16517 -- parameters, so check if we have this case.
16518
16519 -- Note: it may seem odd to track constant values for constants, but in
16520 -- fact this routine is used for other purposes than simply capturing
16521 -- the value. In particular, the setting of Known[_Non]_Null.
16522
16523 if (Ekind (Ent) = E_Variable and then No (Renamed_Object (Ent)))
16524 or else
16525 Ekind_In (Ent, E_Constant, E_Out_Parameter, E_In_Out_Parameter)
16526 then
16527 null;
16528
16529 -- For conditionals, we also allow loop parameters and all formals,
16530 -- including in parameters.
16531
16532 elsif Cond and then Ekind_In (Ent, E_Loop_Parameter, E_In_Parameter) then
16533 null;
16534
16535 -- For all other cases, not just unsafe, but impossible to capture
16536 -- Current_Value, since the above are the only entities which have
16537 -- Current_Value fields.
16538
16539 else
16540 return False;
16541 end if;
16542
16543 -- Skip if volatile or aliased, since funny things might be going on in
16544 -- these cases which we cannot necessarily track. Also skip any variable
16545 -- for which an address clause is given, or whose address is taken. Also
16546 -- never capture value of library level variables (an attempt to do so
16547 -- can occur in the case of package elaboration code).
16548
16549 if Treat_As_Volatile (Ent)
16550 or else Is_Aliased (Ent)
16551 or else Present (Address_Clause (Ent))
16552 or else Address_Taken (Ent)
16553 or else (Is_Library_Level_Entity (Ent)
16554 and then Ekind (Ent) = E_Variable)
16555 then
16556 return False;
16557 end if;
16558
16559 -- OK, all above conditions are met. We also require that the scope of
16560 -- the reference be the same as the scope of the entity, not counting
16561 -- packages and blocks and loops.
16562
16563 declare
16564 E_Scope : constant Entity_Id := Scope (Ent);
16565 R_Scope : Entity_Id;
16566
16567 begin
16568 R_Scope := Current_Scope;
16569 while R_Scope /= Standard_Standard loop
16570 exit when R_Scope = E_Scope;
16571
16572 if not Ekind_In (R_Scope, E_Package, E_Block, E_Loop) then
16573 return False;
16574 else
16575 R_Scope := Scope (R_Scope);
16576 end if;
16577 end loop;
16578 end;
16579
16580 -- We also require that the reference does not appear in a context
16581 -- where it is not sure to be executed (i.e. a conditional context
16582 -- or an exception handler). We skip this if Cond is True, since the
16583 -- capturing of values from conditional tests handles this ok.
16584
16585 if Cond then
16586 return True;
16587 end if;
16588
16589 declare
16590 Desc : Node_Id;
16591 P : Node_Id;
16592
16593 begin
16594 Desc := N;
16595
16596 -- Seems dubious that case expressions are not handled here ???
16597
16598 P := Parent (N);
16599 while Present (P) loop
16600 if Nkind (P) = N_If_Statement
16601 or else Nkind (P) = N_Case_Statement
16602 or else (Nkind (P) in N_Short_Circuit
16603 and then Desc = Right_Opnd (P))
16604 or else (Nkind (P) = N_If_Expression
16605 and then Desc /= First (Expressions (P)))
16606 or else Nkind (P) = N_Exception_Handler
16607 or else Nkind (P) = N_Selective_Accept
16608 or else Nkind (P) = N_Conditional_Entry_Call
16609 or else Nkind (P) = N_Timed_Entry_Call
16610 or else Nkind (P) = N_Asynchronous_Select
16611 then
16612 return False;
16613
16614 else
16615 Desc := P;
16616 P := Parent (P);
16617
16618 -- A special Ada 2012 case: the original node may be part
16619 -- of the else_actions of a conditional expression, in which
16620 -- case it might not have been expanded yet, and appears in
16621 -- a non-syntactic list of actions. In that case it is clearly
16622 -- not safe to save a value.
16623
16624 if No (P)
16625 and then Is_List_Member (Desc)
16626 and then No (Parent (List_Containing (Desc)))
16627 then
16628 return False;
16629 end if;
16630 end if;
16631 end loop;
16632 end;
16633
16634 -- OK, looks safe to set value
16635
16636 return True;
16637 end Safe_To_Capture_Value;
16638
16639 ---------------
16640 -- Same_Name --
16641 ---------------
16642
16643 function Same_Name (N1, N2 : Node_Id) return Boolean is
16644 K1 : constant Node_Kind := Nkind (N1);
16645 K2 : constant Node_Kind := Nkind (N2);
16646
16647 begin
16648 if (K1 = N_Identifier or else K1 = N_Defining_Identifier)
16649 and then (K2 = N_Identifier or else K2 = N_Defining_Identifier)
16650 then
16651 return Chars (N1) = Chars (N2);
16652
16653 elsif (K1 = N_Selected_Component or else K1 = N_Expanded_Name)
16654 and then (K2 = N_Selected_Component or else K2 = N_Expanded_Name)
16655 then
16656 return Same_Name (Selector_Name (N1), Selector_Name (N2))
16657 and then Same_Name (Prefix (N1), Prefix (N2));
16658
16659 else
16660 return False;
16661 end if;
16662 end Same_Name;
16663
16664 -----------------
16665 -- Same_Object --
16666 -----------------
16667
16668 function Same_Object (Node1, Node2 : Node_Id) return Boolean is
16669 N1 : constant Node_Id := Original_Node (Node1);
16670 N2 : constant Node_Id := Original_Node (Node2);
16671 -- We do the tests on original nodes, since we are most interested
16672 -- in the original source, not any expansion that got in the way.
16673
16674 K1 : constant Node_Kind := Nkind (N1);
16675 K2 : constant Node_Kind := Nkind (N2);
16676
16677 begin
16678 -- First case, both are entities with same entity
16679
16680 if K1 in N_Has_Entity and then K2 in N_Has_Entity then
16681 declare
16682 EN1 : constant Entity_Id := Entity (N1);
16683 EN2 : constant Entity_Id := Entity (N2);
16684 begin
16685 if Present (EN1) and then Present (EN2)
16686 and then (Ekind_In (EN1, E_Variable, E_Constant)
16687 or else Is_Formal (EN1))
16688 and then EN1 = EN2
16689 then
16690 return True;
16691 end if;
16692 end;
16693 end if;
16694
16695 -- Second case, selected component with same selector, same record
16696
16697 if K1 = N_Selected_Component
16698 and then K2 = N_Selected_Component
16699 and then Chars (Selector_Name (N1)) = Chars (Selector_Name (N2))
16700 then
16701 return Same_Object (Prefix (N1), Prefix (N2));
16702
16703 -- Third case, indexed component with same subscripts, same array
16704
16705 elsif K1 = N_Indexed_Component
16706 and then K2 = N_Indexed_Component
16707 and then Same_Object (Prefix (N1), Prefix (N2))
16708 then
16709 declare
16710 E1, E2 : Node_Id;
16711 begin
16712 E1 := First (Expressions (N1));
16713 E2 := First (Expressions (N2));
16714 while Present (E1) loop
16715 if not Same_Value (E1, E2) then
16716 return False;
16717 else
16718 Next (E1);
16719 Next (E2);
16720 end if;
16721 end loop;
16722
16723 return True;
16724 end;
16725
16726 -- Fourth case, slice of same array with same bounds
16727
16728 elsif K1 = N_Slice
16729 and then K2 = N_Slice
16730 and then Nkind (Discrete_Range (N1)) = N_Range
16731 and then Nkind (Discrete_Range (N2)) = N_Range
16732 and then Same_Value (Low_Bound (Discrete_Range (N1)),
16733 Low_Bound (Discrete_Range (N2)))
16734 and then Same_Value (High_Bound (Discrete_Range (N1)),
16735 High_Bound (Discrete_Range (N2)))
16736 then
16737 return Same_Name (Prefix (N1), Prefix (N2));
16738
16739 -- All other cases, not clearly the same object
16740
16741 else
16742 return False;
16743 end if;
16744 end Same_Object;
16745
16746 ---------------
16747 -- Same_Type --
16748 ---------------
16749
16750 function Same_Type (T1, T2 : Entity_Id) return Boolean is
16751 begin
16752 if T1 = T2 then
16753 return True;
16754
16755 elsif not Is_Constrained (T1)
16756 and then not Is_Constrained (T2)
16757 and then Base_Type (T1) = Base_Type (T2)
16758 then
16759 return True;
16760
16761 -- For now don't bother with case of identical constraints, to be
16762 -- fiddled with later on perhaps (this is only used for optimization
16763 -- purposes, so it is not critical to do a best possible job)
16764
16765 else
16766 return False;
16767 end if;
16768 end Same_Type;
16769
16770 ----------------
16771 -- Same_Value --
16772 ----------------
16773
16774 function Same_Value (Node1, Node2 : Node_Id) return Boolean is
16775 begin
16776 if Compile_Time_Known_Value (Node1)
16777 and then Compile_Time_Known_Value (Node2)
16778 and then Expr_Value (Node1) = Expr_Value (Node2)
16779 then
16780 return True;
16781 elsif Same_Object (Node1, Node2) then
16782 return True;
16783 else
16784 return False;
16785 end if;
16786 end Same_Value;
16787
16788 -----------------------------
16789 -- Save_SPARK_Mode_And_Set --
16790 -----------------------------
16791
16792 procedure Save_SPARK_Mode_And_Set
16793 (Context : Entity_Id;
16794 Mode : out SPARK_Mode_Type)
16795 is
16796 begin
16797 -- Save the current mode in effect
16798
16799 Mode := SPARK_Mode;
16800
16801 -- Do not consider illegal or partially decorated constructs
16802
16803 if Ekind (Context) = E_Void or else Error_Posted (Context) then
16804 null;
16805
16806 elsif Present (SPARK_Pragma (Context)) then
16807 SPARK_Mode := Get_SPARK_Mode_From_Pragma (SPARK_Pragma (Context));
16808 end if;
16809 end Save_SPARK_Mode_And_Set;
16810
16811 -------------------------
16812 -- Scalar_Part_Present --
16813 -------------------------
16814
16815 function Scalar_Part_Present (T : Entity_Id) return Boolean is
16816 C : Entity_Id;
16817
16818 begin
16819 if Is_Scalar_Type (T) then
16820 return True;
16821
16822 elsif Is_Array_Type (T) then
16823 return Scalar_Part_Present (Component_Type (T));
16824
16825 elsif Is_Record_Type (T) or else Has_Discriminants (T) then
16826 C := First_Component_Or_Discriminant (T);
16827 while Present (C) loop
16828 if Scalar_Part_Present (Etype (C)) then
16829 return True;
16830 else
16831 Next_Component_Or_Discriminant (C);
16832 end if;
16833 end loop;
16834 end if;
16835
16836 return False;
16837 end Scalar_Part_Present;
16838
16839 ------------------------
16840 -- Scope_Is_Transient --
16841 ------------------------
16842
16843 function Scope_Is_Transient return Boolean is
16844 begin
16845 return Scope_Stack.Table (Scope_Stack.Last).Is_Transient;
16846 end Scope_Is_Transient;
16847
16848 ------------------
16849 -- Scope_Within --
16850 ------------------
16851
16852 function Scope_Within (Scope1, Scope2 : Entity_Id) return Boolean is
16853 Scop : Entity_Id;
16854
16855 begin
16856 Scop := Scope1;
16857 while Scop /= Standard_Standard loop
16858 Scop := Scope (Scop);
16859
16860 if Scop = Scope2 then
16861 return True;
16862 end if;
16863 end loop;
16864
16865 return False;
16866 end Scope_Within;
16867
16868 --------------------------
16869 -- Scope_Within_Or_Same --
16870 --------------------------
16871
16872 function Scope_Within_Or_Same (Scope1, Scope2 : Entity_Id) return Boolean is
16873 Scop : Entity_Id;
16874
16875 begin
16876 Scop := Scope1;
16877 while Scop /= Standard_Standard loop
16878 if Scop = Scope2 then
16879 return True;
16880 else
16881 Scop := Scope (Scop);
16882 end if;
16883 end loop;
16884
16885 return False;
16886 end Scope_Within_Or_Same;
16887
16888 --------------------
16889 -- Set_Convention --
16890 --------------------
16891
16892 procedure Set_Convention (E : Entity_Id; Val : Snames.Convention_Id) is
16893 begin
16894 Basic_Set_Convention (E, Val);
16895
16896 if Is_Type (E)
16897 and then Is_Access_Subprogram_Type (Base_Type (E))
16898 and then Has_Foreign_Convention (E)
16899 then
16900 Set_Can_Use_Internal_Rep (E, False);
16901 end if;
16902
16903 -- If E is an object or component, and the type of E is an anonymous
16904 -- access type with no convention set, then also set the convention of
16905 -- the anonymous access type. We do not do this for anonymous protected
16906 -- types, since protected types always have the default convention.
16907
16908 if Present (Etype (E))
16909 and then (Is_Object (E)
16910 or else Ekind (E) = E_Component
16911
16912 -- Allow E_Void (happens for pragma Convention appearing
16913 -- in the middle of a record applying to a component)
16914
16915 or else Ekind (E) = E_Void)
16916 then
16917 declare
16918 Typ : constant Entity_Id := Etype (E);
16919
16920 begin
16921 if Ekind_In (Typ, E_Anonymous_Access_Type,
16922 E_Anonymous_Access_Subprogram_Type)
16923 and then not Has_Convention_Pragma (Typ)
16924 then
16925 Basic_Set_Convention (Typ, Val);
16926 Set_Has_Convention_Pragma (Typ);
16927
16928 -- And for the access subprogram type, deal similarly with the
16929 -- designated E_Subprogram_Type if it is also internal (which
16930 -- it always is?)
16931
16932 if Ekind (Typ) = E_Anonymous_Access_Subprogram_Type then
16933 declare
16934 Dtype : constant Entity_Id := Designated_Type (Typ);
16935 begin
16936 if Ekind (Dtype) = E_Subprogram_Type
16937 and then Is_Itype (Dtype)
16938 and then not Has_Convention_Pragma (Dtype)
16939 then
16940 Basic_Set_Convention (Dtype, Val);
16941 Set_Has_Convention_Pragma (Dtype);
16942 end if;
16943 end;
16944 end if;
16945 end if;
16946 end;
16947 end if;
16948 end Set_Convention;
16949
16950 ------------------------
16951 -- Set_Current_Entity --
16952 ------------------------
16953
16954 -- The given entity is to be set as the currently visible definition of its
16955 -- associated name (i.e. the Node_Id associated with its name). All we have
16956 -- to do is to get the name from the identifier, and then set the
16957 -- associated Node_Id to point to the given entity.
16958
16959 procedure Set_Current_Entity (E : Entity_Id) is
16960 begin
16961 Set_Name_Entity_Id (Chars (E), E);
16962 end Set_Current_Entity;
16963
16964 ---------------------------
16965 -- Set_Debug_Info_Needed --
16966 ---------------------------
16967
16968 procedure Set_Debug_Info_Needed (T : Entity_Id) is
16969
16970 procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id);
16971 pragma Inline (Set_Debug_Info_Needed_If_Not_Set);
16972 -- Used to set debug info in a related node if not set already
16973
16974 --------------------------------------
16975 -- Set_Debug_Info_Needed_If_Not_Set --
16976 --------------------------------------
16977
16978 procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id) is
16979 begin
16980 if Present (E) and then not Needs_Debug_Info (E) then
16981 Set_Debug_Info_Needed (E);
16982
16983 -- For a private type, indicate that the full view also needs
16984 -- debug information.
16985
16986 if Is_Type (E)
16987 and then Is_Private_Type (E)
16988 and then Present (Full_View (E))
16989 then
16990 Set_Debug_Info_Needed (Full_View (E));
16991 end if;
16992 end if;
16993 end Set_Debug_Info_Needed_If_Not_Set;
16994
16995 -- Start of processing for Set_Debug_Info_Needed
16996
16997 begin
16998 -- Nothing to do if argument is Empty or has Debug_Info_Off set, which
16999 -- indicates that Debug_Info_Needed is never required for the entity.
17000 -- Nothing to do if entity comes from a predefined file. Library files
17001 -- are compiled without debug information, but inlined bodies of these
17002 -- routines may appear in user code, and debug information on them ends
17003 -- up complicating debugging the user code.
17004
17005 if No (T)
17006 or else Debug_Info_Off (T)
17007 then
17008 return;
17009
17010 elsif In_Inlined_Body
17011 and then Is_Predefined_File_Name
17012 (Unit_File_Name (Get_Source_Unit (Sloc (T))))
17013 then
17014 Set_Needs_Debug_Info (T, False);
17015 end if;
17016
17017 -- Set flag in entity itself. Note that we will go through the following
17018 -- circuitry even if the flag is already set on T. That's intentional,
17019 -- it makes sure that the flag will be set in subsidiary entities.
17020
17021 Set_Needs_Debug_Info (T);
17022
17023 -- Set flag on subsidiary entities if not set already
17024
17025 if Is_Object (T) then
17026 Set_Debug_Info_Needed_If_Not_Set (Etype (T));
17027
17028 elsif Is_Type (T) then
17029 Set_Debug_Info_Needed_If_Not_Set (Etype (T));
17030
17031 if Is_Record_Type (T) then
17032 declare
17033 Ent : Entity_Id := First_Entity (T);
17034 begin
17035 while Present (Ent) loop
17036 Set_Debug_Info_Needed_If_Not_Set (Ent);
17037 Next_Entity (Ent);
17038 end loop;
17039 end;
17040
17041 -- For a class wide subtype, we also need debug information
17042 -- for the equivalent type.
17043
17044 if Ekind (T) = E_Class_Wide_Subtype then
17045 Set_Debug_Info_Needed_If_Not_Set (Equivalent_Type (T));
17046 end if;
17047
17048 elsif Is_Array_Type (T) then
17049 Set_Debug_Info_Needed_If_Not_Set (Component_Type (T));
17050
17051 declare
17052 Indx : Node_Id := First_Index (T);
17053 begin
17054 while Present (Indx) loop
17055 Set_Debug_Info_Needed_If_Not_Set (Etype (Indx));
17056 Indx := Next_Index (Indx);
17057 end loop;
17058 end;
17059
17060 -- For a packed array type, we also need debug information for
17061 -- the type used to represent the packed array. Conversely, we
17062 -- also need it for the former if we need it for the latter.
17063
17064 if Is_Packed (T) then
17065 Set_Debug_Info_Needed_If_Not_Set (Packed_Array_Impl_Type (T));
17066 end if;
17067
17068 if Is_Packed_Array_Impl_Type (T) then
17069 Set_Debug_Info_Needed_If_Not_Set (Original_Array_Type (T));
17070 end if;
17071
17072 elsif Is_Access_Type (T) then
17073 Set_Debug_Info_Needed_If_Not_Set (Directly_Designated_Type (T));
17074
17075 elsif Is_Private_Type (T) then
17076 Set_Debug_Info_Needed_If_Not_Set (Full_View (T));
17077
17078 elsif Is_Protected_Type (T) then
17079 Set_Debug_Info_Needed_If_Not_Set (Corresponding_Record_Type (T));
17080
17081 elsif Is_Scalar_Type (T) then
17082
17083 -- If the subrange bounds are materialized by dedicated constant
17084 -- objects, also include them in the debug info to make sure the
17085 -- debugger can properly use them.
17086
17087 if Present (Scalar_Range (T))
17088 and then Nkind (Scalar_Range (T)) = N_Range
17089 then
17090 declare
17091 Low_Bnd : constant Node_Id := Type_Low_Bound (T);
17092 High_Bnd : constant Node_Id := Type_High_Bound (T);
17093
17094 begin
17095 if Is_Entity_Name (Low_Bnd) then
17096 Set_Debug_Info_Needed_If_Not_Set (Entity (Low_Bnd));
17097 end if;
17098
17099 if Is_Entity_Name (High_Bnd) then
17100 Set_Debug_Info_Needed_If_Not_Set (Entity (High_Bnd));
17101 end if;
17102 end;
17103 end if;
17104 end if;
17105 end if;
17106 end Set_Debug_Info_Needed;
17107
17108 ----------------------------
17109 -- Set_Entity_With_Checks --
17110 ----------------------------
17111
17112 procedure Set_Entity_With_Checks (N : Node_Id; Val : Entity_Id) is
17113 Val_Actual : Entity_Id;
17114 Nod : Node_Id;
17115 Post_Node : Node_Id;
17116
17117 begin
17118 -- Unconditionally set the entity
17119
17120 Set_Entity (N, Val);
17121
17122 -- The node to post on is the selector in the case of an expanded name,
17123 -- and otherwise the node itself.
17124
17125 if Nkind (N) = N_Expanded_Name then
17126 Post_Node := Selector_Name (N);
17127 else
17128 Post_Node := N;
17129 end if;
17130
17131 -- Check for violation of No_Fixed_IO
17132
17133 if Restriction_Check_Required (No_Fixed_IO)
17134 and then
17135 ((RTU_Loaded (Ada_Text_IO)
17136 and then (Is_RTE (Val, RE_Decimal_IO)
17137 or else
17138 Is_RTE (Val, RE_Fixed_IO)))
17139
17140 or else
17141 (RTU_Loaded (Ada_Wide_Text_IO)
17142 and then (Is_RTE (Val, RO_WT_Decimal_IO)
17143 or else
17144 Is_RTE (Val, RO_WT_Fixed_IO)))
17145
17146 or else
17147 (RTU_Loaded (Ada_Wide_Wide_Text_IO)
17148 and then (Is_RTE (Val, RO_WW_Decimal_IO)
17149 or else
17150 Is_RTE (Val, RO_WW_Fixed_IO))))
17151
17152 -- A special extra check, don't complain about a reference from within
17153 -- the Ada.Interrupts package itself!
17154
17155 and then not In_Same_Extended_Unit (N, Val)
17156 then
17157 Check_Restriction (No_Fixed_IO, Post_Node);
17158 end if;
17159
17160 -- Remaining checks are only done on source nodes. Note that we test
17161 -- for violation of No_Fixed_IO even on non-source nodes, because the
17162 -- cases for checking violations of this restriction are instantiations
17163 -- where the reference in the instance has Comes_From_Source False.
17164
17165 if not Comes_From_Source (N) then
17166 return;
17167 end if;
17168
17169 -- Check for violation of No_Abort_Statements, which is triggered by
17170 -- call to Ada.Task_Identification.Abort_Task.
17171
17172 if Restriction_Check_Required (No_Abort_Statements)
17173 and then (Is_RTE (Val, RE_Abort_Task))
17174
17175 -- A special extra check, don't complain about a reference from within
17176 -- the Ada.Task_Identification package itself!
17177
17178 and then not In_Same_Extended_Unit (N, Val)
17179 then
17180 Check_Restriction (No_Abort_Statements, Post_Node);
17181 end if;
17182
17183 if Val = Standard_Long_Long_Integer then
17184 Check_Restriction (No_Long_Long_Integers, Post_Node);
17185 end if;
17186
17187 -- Check for violation of No_Dynamic_Attachment
17188
17189 if Restriction_Check_Required (No_Dynamic_Attachment)
17190 and then RTU_Loaded (Ada_Interrupts)
17191 and then (Is_RTE (Val, RE_Is_Reserved) or else
17192 Is_RTE (Val, RE_Is_Attached) or else
17193 Is_RTE (Val, RE_Current_Handler) or else
17194 Is_RTE (Val, RE_Attach_Handler) or else
17195 Is_RTE (Val, RE_Exchange_Handler) or else
17196 Is_RTE (Val, RE_Detach_Handler) or else
17197 Is_RTE (Val, RE_Reference))
17198
17199 -- A special extra check, don't complain about a reference from within
17200 -- the Ada.Interrupts package itself!
17201
17202 and then not In_Same_Extended_Unit (N, Val)
17203 then
17204 Check_Restriction (No_Dynamic_Attachment, Post_Node);
17205 end if;
17206
17207 -- Check for No_Implementation_Identifiers
17208
17209 if Restriction_Check_Required (No_Implementation_Identifiers) then
17210
17211 -- We have an implementation defined entity if it is marked as
17212 -- implementation defined, or is defined in a package marked as
17213 -- implementation defined. However, library packages themselves
17214 -- are excluded (we don't want to flag Interfaces itself, just
17215 -- the entities within it).
17216
17217 if (Is_Implementation_Defined (Val)
17218 or else
17219 (Present (Scope (Val))
17220 and then Is_Implementation_Defined (Scope (Val))))
17221 and then not (Ekind_In (Val, E_Package, E_Generic_Package)
17222 and then Is_Library_Level_Entity (Val))
17223 then
17224 Check_Restriction (No_Implementation_Identifiers, Post_Node);
17225 end if;
17226 end if;
17227
17228 -- Do the style check
17229
17230 if Style_Check
17231 and then not Suppress_Style_Checks (Val)
17232 and then not In_Instance
17233 then
17234 if Nkind (N) = N_Identifier then
17235 Nod := N;
17236 elsif Nkind (N) = N_Expanded_Name then
17237 Nod := Selector_Name (N);
17238 else
17239 return;
17240 end if;
17241
17242 -- A special situation arises for derived operations, where we want
17243 -- to do the check against the parent (since the Sloc of the derived
17244 -- operation points to the derived type declaration itself).
17245
17246 Val_Actual := Val;
17247 while not Comes_From_Source (Val_Actual)
17248 and then Nkind (Val_Actual) in N_Entity
17249 and then (Ekind (Val_Actual) = E_Enumeration_Literal
17250 or else Is_Subprogram_Or_Generic_Subprogram (Val_Actual))
17251 and then Present (Alias (Val_Actual))
17252 loop
17253 Val_Actual := Alias (Val_Actual);
17254 end loop;
17255
17256 -- Renaming declarations for generic actuals do not come from source,
17257 -- and have a different name from that of the entity they rename, so
17258 -- there is no style check to perform here.
17259
17260 if Chars (Nod) = Chars (Val_Actual) then
17261 Style.Check_Identifier (Nod, Val_Actual);
17262 end if;
17263 end if;
17264
17265 Set_Entity (N, Val);
17266 end Set_Entity_With_Checks;
17267
17268 -------------------------
17269 -- Set_Is_Ghost_Entity --
17270 -------------------------
17271
17272 procedure Set_Is_Ghost_Entity (Id : Entity_Id) is
17273 Policy : constant Name_Id := Policy_In_Effect (Name_Ghost);
17274
17275 begin
17276 if Policy = Name_Check then
17277 Set_Is_Checked_Ghost_Entity (Id);
17278
17279 elsif Policy = Name_Ignore then
17280 Set_Is_Ignored_Ghost_Entity (Id);
17281 end if;
17282 end Set_Is_Ghost_Entity;
17283
17284 ------------------------
17285 -- Set_Name_Entity_Id --
17286 ------------------------
17287
17288 procedure Set_Name_Entity_Id (Id : Name_Id; Val : Entity_Id) is
17289 begin
17290 Set_Name_Table_Int (Id, Int (Val));
17291 end Set_Name_Entity_Id;
17292
17293 ---------------------
17294 -- Set_Next_Actual --
17295 ---------------------
17296
17297 procedure Set_Next_Actual (Ass1_Id : Node_Id; Ass2_Id : Node_Id) is
17298 begin
17299 if Nkind (Parent (Ass1_Id)) = N_Parameter_Association then
17300 Set_First_Named_Actual (Parent (Ass1_Id), Ass2_Id);
17301 end if;
17302 end Set_Next_Actual;
17303
17304 ----------------------------------
17305 -- Set_Optimize_Alignment_Flags --
17306 ----------------------------------
17307
17308 procedure Set_Optimize_Alignment_Flags (E : Entity_Id) is
17309 begin
17310 if Optimize_Alignment = 'S' then
17311 Set_Optimize_Alignment_Space (E);
17312 elsif Optimize_Alignment = 'T' then
17313 Set_Optimize_Alignment_Time (E);
17314 end if;
17315 end Set_Optimize_Alignment_Flags;
17316
17317 -----------------------
17318 -- Set_Public_Status --
17319 -----------------------
17320
17321 procedure Set_Public_Status (Id : Entity_Id) is
17322 S : constant Entity_Id := Current_Scope;
17323
17324 function Within_HSS_Or_If (E : Entity_Id) return Boolean;
17325 -- Determines if E is defined within handled statement sequence or
17326 -- an if statement, returns True if so, False otherwise.
17327
17328 ----------------------
17329 -- Within_HSS_Or_If --
17330 ----------------------
17331
17332 function Within_HSS_Or_If (E : Entity_Id) return Boolean is
17333 N : Node_Id;
17334 begin
17335 N := Declaration_Node (E);
17336 loop
17337 N := Parent (N);
17338
17339 if No (N) then
17340 return False;
17341
17342 elsif Nkind_In (N, N_Handled_Sequence_Of_Statements,
17343 N_If_Statement)
17344 then
17345 return True;
17346 end if;
17347 end loop;
17348 end Within_HSS_Or_If;
17349
17350 -- Start of processing for Set_Public_Status
17351
17352 begin
17353 -- Everything in the scope of Standard is public
17354
17355 if S = Standard_Standard then
17356 Set_Is_Public (Id);
17357
17358 -- Entity is definitely not public if enclosing scope is not public
17359
17360 elsif not Is_Public (S) then
17361 return;
17362
17363 -- An object or function declaration that occurs in a handled sequence
17364 -- of statements or within an if statement is the declaration for a
17365 -- temporary object or local subprogram generated by the expander. It
17366 -- never needs to be made public and furthermore, making it public can
17367 -- cause back end problems.
17368
17369 elsif Nkind_In (Parent (Id), N_Object_Declaration,
17370 N_Function_Specification)
17371 and then Within_HSS_Or_If (Id)
17372 then
17373 return;
17374
17375 -- Entities in public packages or records are public
17376
17377 elsif Ekind (S) = E_Package or Is_Record_Type (S) then
17378 Set_Is_Public (Id);
17379
17380 -- The bounds of an entry family declaration can generate object
17381 -- declarations that are visible to the back-end, e.g. in the
17382 -- the declaration of a composite type that contains tasks.
17383
17384 elsif Is_Concurrent_Type (S)
17385 and then not Has_Completion (S)
17386 and then Nkind (Parent (Id)) = N_Object_Declaration
17387 then
17388 Set_Is_Public (Id);
17389 end if;
17390 end Set_Public_Status;
17391
17392 -----------------------------
17393 -- Set_Referenced_Modified --
17394 -----------------------------
17395
17396 procedure Set_Referenced_Modified (N : Node_Id; Out_Param : Boolean) is
17397 Pref : Node_Id;
17398
17399 begin
17400 -- Deal with indexed or selected component where prefix is modified
17401
17402 if Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
17403 Pref := Prefix (N);
17404
17405 -- If prefix is access type, then it is the designated object that is
17406 -- being modified, which means we have no entity to set the flag on.
17407
17408 if No (Etype (Pref)) or else Is_Access_Type (Etype (Pref)) then
17409 return;
17410
17411 -- Otherwise chase the prefix
17412
17413 else
17414 Set_Referenced_Modified (Pref, Out_Param);
17415 end if;
17416
17417 -- Otherwise see if we have an entity name (only other case to process)
17418
17419 elsif Is_Entity_Name (N) and then Present (Entity (N)) then
17420 Set_Referenced_As_LHS (Entity (N), not Out_Param);
17421 Set_Referenced_As_Out_Parameter (Entity (N), Out_Param);
17422 end if;
17423 end Set_Referenced_Modified;
17424
17425 ----------------------------
17426 -- Set_Scope_Is_Transient --
17427 ----------------------------
17428
17429 procedure Set_Scope_Is_Transient (V : Boolean := True) is
17430 begin
17431 Scope_Stack.Table (Scope_Stack.Last).Is_Transient := V;
17432 end Set_Scope_Is_Transient;
17433
17434 -------------------
17435 -- Set_Size_Info --
17436 -------------------
17437
17438 procedure Set_Size_Info (T1, T2 : Entity_Id) is
17439 begin
17440 -- We copy Esize, but not RM_Size, since in general RM_Size is
17441 -- subtype specific and does not get inherited by all subtypes.
17442
17443 Set_Esize (T1, Esize (T2));
17444 Set_Has_Biased_Representation (T1, Has_Biased_Representation (T2));
17445
17446 if Is_Discrete_Or_Fixed_Point_Type (T1)
17447 and then
17448 Is_Discrete_Or_Fixed_Point_Type (T2)
17449 then
17450 Set_Is_Unsigned_Type (T1, Is_Unsigned_Type (T2));
17451 end if;
17452
17453 Set_Alignment (T1, Alignment (T2));
17454 end Set_Size_Info;
17455
17456 --------------------
17457 -- Static_Boolean --
17458 --------------------
17459
17460 function Static_Boolean (N : Node_Id) return Uint is
17461 begin
17462 Analyze_And_Resolve (N, Standard_Boolean);
17463
17464 if N = Error
17465 or else Error_Posted (N)
17466 or else Etype (N) = Any_Type
17467 then
17468 return No_Uint;
17469 end if;
17470
17471 if Is_OK_Static_Expression (N) then
17472 if not Raises_Constraint_Error (N) then
17473 return Expr_Value (N);
17474 else
17475 return No_Uint;
17476 end if;
17477
17478 elsif Etype (N) = Any_Type then
17479 return No_Uint;
17480
17481 else
17482 Flag_Non_Static_Expr
17483 ("static boolean expression required here", N);
17484 return No_Uint;
17485 end if;
17486 end Static_Boolean;
17487
17488 --------------------
17489 -- Static_Integer --
17490 --------------------
17491
17492 function Static_Integer (N : Node_Id) return Uint is
17493 begin
17494 Analyze_And_Resolve (N, Any_Integer);
17495
17496 if N = Error
17497 or else Error_Posted (N)
17498 or else Etype (N) = Any_Type
17499 then
17500 return No_Uint;
17501 end if;
17502
17503 if Is_OK_Static_Expression (N) then
17504 if not Raises_Constraint_Error (N) then
17505 return Expr_Value (N);
17506 else
17507 return No_Uint;
17508 end if;
17509
17510 elsif Etype (N) = Any_Type then
17511 return No_Uint;
17512
17513 else
17514 Flag_Non_Static_Expr
17515 ("static integer expression required here", N);
17516 return No_Uint;
17517 end if;
17518 end Static_Integer;
17519
17520 --------------------------
17521 -- Statically_Different --
17522 --------------------------
17523
17524 function Statically_Different (E1, E2 : Node_Id) return Boolean is
17525 R1 : constant Node_Id := Get_Referenced_Object (E1);
17526 R2 : constant Node_Id := Get_Referenced_Object (E2);
17527 begin
17528 return Is_Entity_Name (R1)
17529 and then Is_Entity_Name (R2)
17530 and then Entity (R1) /= Entity (R2)
17531 and then not Is_Formal (Entity (R1))
17532 and then not Is_Formal (Entity (R2));
17533 end Statically_Different;
17534
17535 --------------------------------------
17536 -- Subject_To_Loop_Entry_Attributes --
17537 --------------------------------------
17538
17539 function Subject_To_Loop_Entry_Attributes (N : Node_Id) return Boolean is
17540 Stmt : Node_Id;
17541
17542 begin
17543 Stmt := N;
17544
17545 -- The expansion mechanism transform a loop subject to at least one
17546 -- 'Loop_Entry attribute into a conditional block. Infinite loops lack
17547 -- the conditional part.
17548
17549 if Nkind_In (Stmt, N_Block_Statement, N_If_Statement)
17550 and then Nkind (Original_Node (N)) = N_Loop_Statement
17551 then
17552 Stmt := Original_Node (N);
17553 end if;
17554
17555 return
17556 Nkind (Stmt) = N_Loop_Statement
17557 and then Present (Identifier (Stmt))
17558 and then Present (Entity (Identifier (Stmt)))
17559 and then Has_Loop_Entry_Attributes (Entity (Identifier (Stmt)));
17560 end Subject_To_Loop_Entry_Attributes;
17561
17562 -----------------------------
17563 -- Subprogram_Access_Level --
17564 -----------------------------
17565
17566 function Subprogram_Access_Level (Subp : Entity_Id) return Uint is
17567 begin
17568 if Present (Alias (Subp)) then
17569 return Subprogram_Access_Level (Alias (Subp));
17570 else
17571 return Scope_Depth (Enclosing_Dynamic_Scope (Subp));
17572 end if;
17573 end Subprogram_Access_Level;
17574
17575 -------------------------------
17576 -- Support_Atomic_Primitives --
17577 -------------------------------
17578
17579 function Support_Atomic_Primitives (Typ : Entity_Id) return Boolean is
17580 Size : Int;
17581
17582 begin
17583 -- Verify the alignment of Typ is known
17584
17585 if not Known_Alignment (Typ) then
17586 return False;
17587 end if;
17588
17589 if Known_Static_Esize (Typ) then
17590 Size := UI_To_Int (Esize (Typ));
17591
17592 -- If the Esize (Object_Size) is unknown at compile time, look at the
17593 -- RM_Size (Value_Size) which may have been set by an explicit rep item.
17594
17595 elsif Known_Static_RM_Size (Typ) then
17596 Size := UI_To_Int (RM_Size (Typ));
17597
17598 -- Otherwise, the size is considered to be unknown.
17599
17600 else
17601 return False;
17602 end if;
17603
17604 -- Check that the size of the component is 8, 16, 32 or 64 bits and that
17605 -- Typ is properly aligned.
17606
17607 case Size is
17608 when 8 | 16 | 32 | 64 =>
17609 return Size = UI_To_Int (Alignment (Typ)) * 8;
17610 when others =>
17611 return False;
17612 end case;
17613 end Support_Atomic_Primitives;
17614
17615 -----------------
17616 -- Trace_Scope --
17617 -----------------
17618
17619 procedure Trace_Scope (N : Node_Id; E : Entity_Id; Msg : String) is
17620 begin
17621 if Debug_Flag_W then
17622 for J in 0 .. Scope_Stack.Last loop
17623 Write_Str (" ");
17624 end loop;
17625
17626 Write_Str (Msg);
17627 Write_Name (Chars (E));
17628 Write_Str (" from ");
17629 Write_Location (Sloc (N));
17630 Write_Eol;
17631 end if;
17632 end Trace_Scope;
17633
17634 -----------------------
17635 -- Transfer_Entities --
17636 -----------------------
17637
17638 procedure Transfer_Entities (From : Entity_Id; To : Entity_Id) is
17639 procedure Set_Public_Status_Of (Id : Entity_Id);
17640 -- Set the Is_Public attribute of arbitrary entity Id by calling routine
17641 -- Set_Public_Status. If successfull and Id denotes a record type, set
17642 -- the Is_Public attribute of its fields.
17643
17644 --------------------------
17645 -- Set_Public_Status_Of --
17646 --------------------------
17647
17648 procedure Set_Public_Status_Of (Id : Entity_Id) is
17649 Field : Entity_Id;
17650
17651 begin
17652 if not Is_Public (Id) then
17653 Set_Public_Status (Id);
17654
17655 -- When the input entity is a public record type, ensure that all
17656 -- its internal fields are also exposed to the linker. The fields
17657 -- of a class-wide type are never made public.
17658
17659 if Is_Public (Id)
17660 and then Is_Record_Type (Id)
17661 and then not Is_Class_Wide_Type (Id)
17662 then
17663 Field := First_Entity (Id);
17664 while Present (Field) loop
17665 Set_Is_Public (Field);
17666 Next_Entity (Field);
17667 end loop;
17668 end if;
17669 end if;
17670 end Set_Public_Status_Of;
17671
17672 -- Local variables
17673
17674 Full_Id : Entity_Id;
17675 Id : Entity_Id;
17676
17677 -- Start of processing for Transfer_Entities
17678
17679 begin
17680 Id := First_Entity (From);
17681
17682 if Present (Id) then
17683
17684 -- Merge the entity chain of the source scope with that of the
17685 -- destination scope.
17686
17687 if Present (Last_Entity (To)) then
17688 Set_Next_Entity (Last_Entity (To), Id);
17689 else
17690 Set_First_Entity (To, Id);
17691 end if;
17692
17693 Set_Last_Entity (To, Last_Entity (From));
17694
17695 -- Inspect the entities of the source scope and update their Scope
17696 -- attribute.
17697
17698 while Present (Id) loop
17699 Set_Scope (Id, To);
17700 Set_Public_Status_Of (Id);
17701
17702 -- Handle an internally generated full view for a private type
17703
17704 if Is_Private_Type (Id)
17705 and then Present (Full_View (Id))
17706 and then Is_Itype (Full_View (Id))
17707 then
17708 Full_Id := Full_View (Id);
17709
17710 Set_Scope (Full_Id, To);
17711 Set_Public_Status_Of (Full_Id);
17712 end if;
17713
17714 Next_Entity (Id);
17715 end loop;
17716
17717 Set_First_Entity (From, Empty);
17718 Set_Last_Entity (From, Empty);
17719 end if;
17720 end Transfer_Entities;
17721
17722 -----------------------
17723 -- Type_Access_Level --
17724 -----------------------
17725
17726 function Type_Access_Level (Typ : Entity_Id) return Uint is
17727 Btyp : Entity_Id;
17728
17729 begin
17730 Btyp := Base_Type (Typ);
17731
17732 -- Ada 2005 (AI-230): For most cases of anonymous access types, we
17733 -- simply use the level where the type is declared. This is true for
17734 -- stand-alone object declarations, and for anonymous access types
17735 -- associated with components the level is the same as that of the
17736 -- enclosing composite type. However, special treatment is needed for
17737 -- the cases of access parameters, return objects of an anonymous access
17738 -- type, and, in Ada 95, access discriminants of limited types.
17739
17740 if Is_Access_Type (Btyp) then
17741 if Ekind (Btyp) = E_Anonymous_Access_Type then
17742
17743 -- If the type is a nonlocal anonymous access type (such as for
17744 -- an access parameter) we treat it as being declared at the
17745 -- library level to ensure that names such as X.all'access don't
17746 -- fail static accessibility checks.
17747
17748 if not Is_Local_Anonymous_Access (Typ) then
17749 return Scope_Depth (Standard_Standard);
17750
17751 -- If this is a return object, the accessibility level is that of
17752 -- the result subtype of the enclosing function. The test here is
17753 -- little complicated, because we have to account for extended
17754 -- return statements that have been rewritten as blocks, in which
17755 -- case we have to find and the Is_Return_Object attribute of the
17756 -- itype's associated object. It would be nice to find a way to
17757 -- simplify this test, but it doesn't seem worthwhile to add a new
17758 -- flag just for purposes of this test. ???
17759
17760 elsif Ekind (Scope (Btyp)) = E_Return_Statement
17761 or else
17762 (Is_Itype (Btyp)
17763 and then Nkind (Associated_Node_For_Itype (Btyp)) =
17764 N_Object_Declaration
17765 and then Is_Return_Object
17766 (Defining_Identifier
17767 (Associated_Node_For_Itype (Btyp))))
17768 then
17769 declare
17770 Scop : Entity_Id;
17771
17772 begin
17773 Scop := Scope (Scope (Btyp));
17774 while Present (Scop) loop
17775 exit when Ekind (Scop) = E_Function;
17776 Scop := Scope (Scop);
17777 end loop;
17778
17779 -- Treat the return object's type as having the level of the
17780 -- function's result subtype (as per RM05-6.5(5.3/2)).
17781
17782 return Type_Access_Level (Etype (Scop));
17783 end;
17784 end if;
17785 end if;
17786
17787 Btyp := Root_Type (Btyp);
17788
17789 -- The accessibility level of anonymous access types associated with
17790 -- discriminants is that of the current instance of the type, and
17791 -- that's deeper than the type itself (AARM 3.10.2 (12.3.21)).
17792
17793 -- AI-402: access discriminants have accessibility based on the
17794 -- object rather than the type in Ada 2005, so the above paragraph
17795 -- doesn't apply.
17796
17797 -- ??? Needs completion with rules from AI-416
17798
17799 if Ada_Version <= Ada_95
17800 and then Ekind (Typ) = E_Anonymous_Access_Type
17801 and then Present (Associated_Node_For_Itype (Typ))
17802 and then Nkind (Associated_Node_For_Itype (Typ)) =
17803 N_Discriminant_Specification
17804 then
17805 return Scope_Depth (Enclosing_Dynamic_Scope (Btyp)) + 1;
17806 end if;
17807 end if;
17808
17809 -- Return library level for a generic formal type. This is done because
17810 -- RM(10.3.2) says that "The statically deeper relationship does not
17811 -- apply to ... a descendant of a generic formal type". Rather than
17812 -- checking at each point where a static accessibility check is
17813 -- performed to see if we are dealing with a formal type, this rule is
17814 -- implemented by having Type_Access_Level and Deepest_Type_Access_Level
17815 -- return extreme values for a formal type; Deepest_Type_Access_Level
17816 -- returns Int'Last. By calling the appropriate function from among the
17817 -- two, we ensure that the static accessibility check will pass if we
17818 -- happen to run into a formal type. More specifically, we should call
17819 -- Deepest_Type_Access_Level instead of Type_Access_Level whenever the
17820 -- call occurs as part of a static accessibility check and the error
17821 -- case is the case where the type's level is too shallow (as opposed
17822 -- to too deep).
17823
17824 if Is_Generic_Type (Root_Type (Btyp)) then
17825 return Scope_Depth (Standard_Standard);
17826 end if;
17827
17828 return Scope_Depth (Enclosing_Dynamic_Scope (Btyp));
17829 end Type_Access_Level;
17830
17831 ------------------------------------
17832 -- Type_Without_Stream_Operation --
17833 ------------------------------------
17834
17835 function Type_Without_Stream_Operation
17836 (T : Entity_Id;
17837 Op : TSS_Name_Type := TSS_Null) return Entity_Id
17838 is
17839 BT : constant Entity_Id := Base_Type (T);
17840 Op_Missing : Boolean;
17841
17842 begin
17843 if not Restriction_Active (No_Default_Stream_Attributes) then
17844 return Empty;
17845 end if;
17846
17847 if Is_Elementary_Type (T) then
17848 if Op = TSS_Null then
17849 Op_Missing :=
17850 No (TSS (BT, TSS_Stream_Read))
17851 or else No (TSS (BT, TSS_Stream_Write));
17852
17853 else
17854 Op_Missing := No (TSS (BT, Op));
17855 end if;
17856
17857 if Op_Missing then
17858 return T;
17859 else
17860 return Empty;
17861 end if;
17862
17863 elsif Is_Array_Type (T) then
17864 return Type_Without_Stream_Operation (Component_Type (T), Op);
17865
17866 elsif Is_Record_Type (T) then
17867 declare
17868 Comp : Entity_Id;
17869 C_Typ : Entity_Id;
17870
17871 begin
17872 Comp := First_Component (T);
17873 while Present (Comp) loop
17874 C_Typ := Type_Without_Stream_Operation (Etype (Comp), Op);
17875
17876 if Present (C_Typ) then
17877 return C_Typ;
17878 end if;
17879
17880 Next_Component (Comp);
17881 end loop;
17882
17883 return Empty;
17884 end;
17885
17886 elsif Is_Private_Type (T) and then Present (Full_View (T)) then
17887 return Type_Without_Stream_Operation (Full_View (T), Op);
17888 else
17889 return Empty;
17890 end if;
17891 end Type_Without_Stream_Operation;
17892
17893 ----------------------------
17894 -- Unique_Defining_Entity --
17895 ----------------------------
17896
17897 function Unique_Defining_Entity (N : Node_Id) return Entity_Id is
17898 begin
17899 return Unique_Entity (Defining_Entity (N));
17900 end Unique_Defining_Entity;
17901
17902 -------------------
17903 -- Unique_Entity --
17904 -------------------
17905
17906 function Unique_Entity (E : Entity_Id) return Entity_Id is
17907 U : Entity_Id := E;
17908 P : Node_Id;
17909
17910 begin
17911 case Ekind (E) is
17912 when E_Constant =>
17913 if Present (Full_View (E)) then
17914 U := Full_View (E);
17915 end if;
17916
17917 when Type_Kind =>
17918 if Present (Full_View (E)) then
17919 U := Full_View (E);
17920 end if;
17921
17922 when E_Package_Body =>
17923 P := Parent (E);
17924
17925 if Nkind (P) = N_Defining_Program_Unit_Name then
17926 P := Parent (P);
17927 end if;
17928
17929 U := Corresponding_Spec (P);
17930
17931 when E_Subprogram_Body =>
17932 P := Parent (E);
17933
17934 if Nkind (P) = N_Defining_Program_Unit_Name then
17935 P := Parent (P);
17936 end if;
17937
17938 P := Parent (P);
17939
17940 if Nkind (P) = N_Subprogram_Body_Stub then
17941 if Present (Library_Unit (P)) then
17942
17943 -- Get to the function or procedure (generic) entity through
17944 -- the body entity.
17945
17946 U :=
17947 Unique_Entity (Defining_Entity (Get_Body_From_Stub (P)));
17948 end if;
17949 else
17950 U := Corresponding_Spec (P);
17951 end if;
17952
17953 when Formal_Kind =>
17954 if Present (Spec_Entity (E)) then
17955 U := Spec_Entity (E);
17956 end if;
17957
17958 when others =>
17959 null;
17960 end case;
17961
17962 return U;
17963 end Unique_Entity;
17964
17965 -----------------
17966 -- Unique_Name --
17967 -----------------
17968
17969 function Unique_Name (E : Entity_Id) return String is
17970
17971 -- Names of E_Subprogram_Body or E_Package_Body entities are not
17972 -- reliable, as they may not include the overloading suffix. Instead,
17973 -- when looking for the name of E or one of its enclosing scope, we get
17974 -- the name of the corresponding Unique_Entity.
17975
17976 function Get_Scoped_Name (E : Entity_Id) return String;
17977 -- Return the name of E prefixed by all the names of the scopes to which
17978 -- E belongs, except for Standard.
17979
17980 ---------------------
17981 -- Get_Scoped_Name --
17982 ---------------------
17983
17984 function Get_Scoped_Name (E : Entity_Id) return String is
17985 Name : constant String := Get_Name_String (Chars (E));
17986 begin
17987 if Has_Fully_Qualified_Name (E)
17988 or else Scope (E) = Standard_Standard
17989 then
17990 return Name;
17991 else
17992 return Get_Scoped_Name (Unique_Entity (Scope (E))) & "__" & Name;
17993 end if;
17994 end Get_Scoped_Name;
17995
17996 -- Start of processing for Unique_Name
17997
17998 begin
17999 if E = Standard_Standard then
18000 return Get_Name_String (Name_Standard);
18001
18002 elsif Scope (E) = Standard_Standard
18003 and then not (Ekind (E) = E_Package or else Is_Subprogram (E))
18004 then
18005 return Get_Name_String (Name_Standard) & "__" &
18006 Get_Name_String (Chars (E));
18007
18008 elsif Ekind (E) = E_Enumeration_Literal then
18009 return Unique_Name (Etype (E)) & "__" & Get_Name_String (Chars (E));
18010
18011 else
18012 return Get_Scoped_Name (Unique_Entity (E));
18013 end if;
18014 end Unique_Name;
18015
18016 ---------------------
18017 -- Unit_Is_Visible --
18018 ---------------------
18019
18020 function Unit_Is_Visible (U : Entity_Id) return Boolean is
18021 Curr : constant Node_Id := Cunit (Current_Sem_Unit);
18022 Curr_Entity : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
18023
18024 function Unit_In_Parent_Context (Par_Unit : Node_Id) return Boolean;
18025 -- For a child unit, check whether unit appears in a with_clause
18026 -- of a parent.
18027
18028 function Unit_In_Context (Comp_Unit : Node_Id) return Boolean;
18029 -- Scan the context clause of one compilation unit looking for a
18030 -- with_clause for the unit in question.
18031
18032 ----------------------------
18033 -- Unit_In_Parent_Context --
18034 ----------------------------
18035
18036 function Unit_In_Parent_Context (Par_Unit : Node_Id) return Boolean is
18037 begin
18038 if Unit_In_Context (Par_Unit) then
18039 return True;
18040
18041 elsif Is_Child_Unit (Defining_Entity (Unit (Par_Unit))) then
18042 return Unit_In_Parent_Context (Parent_Spec (Unit (Par_Unit)));
18043
18044 else
18045 return False;
18046 end if;
18047 end Unit_In_Parent_Context;
18048
18049 ---------------------
18050 -- Unit_In_Context --
18051 ---------------------
18052
18053 function Unit_In_Context (Comp_Unit : Node_Id) return Boolean is
18054 Clause : Node_Id;
18055
18056 begin
18057 Clause := First (Context_Items (Comp_Unit));
18058 while Present (Clause) loop
18059 if Nkind (Clause) = N_With_Clause then
18060 if Library_Unit (Clause) = U then
18061 return True;
18062
18063 -- The with_clause may denote a renaming of the unit we are
18064 -- looking for, eg. Text_IO which renames Ada.Text_IO.
18065
18066 elsif
18067 Renamed_Entity (Entity (Name (Clause))) =
18068 Defining_Entity (Unit (U))
18069 then
18070 return True;
18071 end if;
18072 end if;
18073
18074 Next (Clause);
18075 end loop;
18076
18077 return False;
18078 end Unit_In_Context;
18079
18080 -- Start of processing for Unit_Is_Visible
18081
18082 begin
18083 -- The currrent unit is directly visible
18084
18085 if Curr = U then
18086 return True;
18087
18088 elsif Unit_In_Context (Curr) then
18089 return True;
18090
18091 -- If the current unit is a body, check the context of the spec
18092
18093 elsif Nkind (Unit (Curr)) = N_Package_Body
18094 or else
18095 (Nkind (Unit (Curr)) = N_Subprogram_Body
18096 and then not Acts_As_Spec (Unit (Curr)))
18097 then
18098 if Unit_In_Context (Library_Unit (Curr)) then
18099 return True;
18100 end if;
18101 end if;
18102
18103 -- If the spec is a child unit, examine the parents
18104
18105 if Is_Child_Unit (Curr_Entity) then
18106 if Nkind (Unit (Curr)) in N_Unit_Body then
18107 return
18108 Unit_In_Parent_Context
18109 (Parent_Spec (Unit (Library_Unit (Curr))));
18110 else
18111 return Unit_In_Parent_Context (Parent_Spec (Unit (Curr)));
18112 end if;
18113
18114 else
18115 return False;
18116 end if;
18117 end Unit_Is_Visible;
18118
18119 ------------------------------
18120 -- Universal_Interpretation --
18121 ------------------------------
18122
18123 function Universal_Interpretation (Opnd : Node_Id) return Entity_Id is
18124 Index : Interp_Index;
18125 It : Interp;
18126
18127 begin
18128 -- The argument may be a formal parameter of an operator or subprogram
18129 -- with multiple interpretations, or else an expression for an actual.
18130
18131 if Nkind (Opnd) = N_Defining_Identifier
18132 or else not Is_Overloaded (Opnd)
18133 then
18134 if Etype (Opnd) = Universal_Integer
18135 or else Etype (Opnd) = Universal_Real
18136 then
18137 return Etype (Opnd);
18138 else
18139 return Empty;
18140 end if;
18141
18142 else
18143 Get_First_Interp (Opnd, Index, It);
18144 while Present (It.Typ) loop
18145 if It.Typ = Universal_Integer
18146 or else It.Typ = Universal_Real
18147 then
18148 return It.Typ;
18149 end if;
18150
18151 Get_Next_Interp (Index, It);
18152 end loop;
18153
18154 return Empty;
18155 end if;
18156 end Universal_Interpretation;
18157
18158 ---------------
18159 -- Unqualify --
18160 ---------------
18161
18162 function Unqualify (Expr : Node_Id) return Node_Id is
18163 begin
18164 -- Recurse to handle unlikely case of multiple levels of qualification
18165
18166 if Nkind (Expr) = N_Qualified_Expression then
18167 return Unqualify (Expression (Expr));
18168
18169 -- Normal case, not a qualified expression
18170
18171 else
18172 return Expr;
18173 end if;
18174 end Unqualify;
18175
18176 -----------------------
18177 -- Visible_Ancestors --
18178 -----------------------
18179
18180 function Visible_Ancestors (Typ : Entity_Id) return Elist_Id is
18181 List_1 : Elist_Id;
18182 List_2 : Elist_Id;
18183 Elmt : Elmt_Id;
18184
18185 begin
18186 pragma Assert (Is_Record_Type (Typ) and then Is_Tagged_Type (Typ));
18187
18188 -- Collect all the parents and progenitors of Typ. If the full-view of
18189 -- private parents and progenitors is available then it is used to
18190 -- generate the list of visible ancestors; otherwise their partial
18191 -- view is added to the resulting list.
18192
18193 Collect_Parents
18194 (T => Typ,
18195 List => List_1,
18196 Use_Full_View => True);
18197
18198 Collect_Interfaces
18199 (T => Typ,
18200 Ifaces_List => List_2,
18201 Exclude_Parents => True,
18202 Use_Full_View => True);
18203
18204 -- Join the two lists. Avoid duplications because an interface may
18205 -- simultaneously be parent and progenitor of a type.
18206
18207 Elmt := First_Elmt (List_2);
18208 while Present (Elmt) loop
18209 Append_Unique_Elmt (Node (Elmt), List_1);
18210 Next_Elmt (Elmt);
18211 end loop;
18212
18213 return List_1;
18214 end Visible_Ancestors;
18215
18216 ------------------------
18217 -- Within_Ghost_Scope --
18218 ------------------------
18219
18220 function Within_Ghost_Scope
18221 (Id : Entity_Id := Current_Scope) return Boolean
18222 is
18223 S : Entity_Id;
18224
18225 begin
18226 -- Climb the scope stack looking for a Ghost scope
18227
18228 S := Id;
18229 while Present (S) and then S /= Standard_Standard loop
18230 if Is_Ghost_Entity (S) then
18231 return True;
18232 end if;
18233
18234 S := Scope (S);
18235 end loop;
18236
18237 return False;
18238 end Within_Ghost_Scope;
18239
18240 ----------------------
18241 -- Within_Init_Proc --
18242 ----------------------
18243
18244 function Within_Init_Proc return Boolean is
18245 S : Entity_Id;
18246
18247 begin
18248 S := Current_Scope;
18249 while not Is_Overloadable (S) loop
18250 if S = Standard_Standard then
18251 return False;
18252 else
18253 S := Scope (S);
18254 end if;
18255 end loop;
18256
18257 return Is_Init_Proc (S);
18258 end Within_Init_Proc;
18259
18260 ------------------
18261 -- Within_Scope --
18262 ------------------
18263
18264 function Within_Scope (E : Entity_Id; S : Entity_Id) return Boolean is
18265 SE : Entity_Id;
18266 begin
18267 SE := Scope (E);
18268 loop
18269 if SE = S then
18270 return True;
18271 elsif SE = Standard_Standard then
18272 return False;
18273 else
18274 SE := Scope (SE);
18275 end if;
18276 end loop;
18277 end Within_Scope;
18278
18279 ----------------
18280 -- Wrong_Type --
18281 ----------------
18282
18283 procedure Wrong_Type (Expr : Node_Id; Expected_Type : Entity_Id) is
18284 Found_Type : constant Entity_Id := First_Subtype (Etype (Expr));
18285 Expec_Type : constant Entity_Id := First_Subtype (Expected_Type);
18286
18287 Matching_Field : Entity_Id;
18288 -- Entity to give a more precise suggestion on how to write a one-
18289 -- element positional aggregate.
18290
18291 function Has_One_Matching_Field return Boolean;
18292 -- Determines if Expec_Type is a record type with a single component or
18293 -- discriminant whose type matches the found type or is one dimensional
18294 -- array whose component type matches the found type. In the case of
18295 -- one discriminant, we ignore the variant parts. That's not accurate,
18296 -- but good enough for the warning.
18297
18298 ----------------------------
18299 -- Has_One_Matching_Field --
18300 ----------------------------
18301
18302 function Has_One_Matching_Field return Boolean is
18303 E : Entity_Id;
18304
18305 begin
18306 Matching_Field := Empty;
18307
18308 if Is_Array_Type (Expec_Type)
18309 and then Number_Dimensions (Expec_Type) = 1
18310 and then Covers (Etype (Component_Type (Expec_Type)), Found_Type)
18311 then
18312 -- Use type name if available. This excludes multidimensional
18313 -- arrays and anonymous arrays.
18314
18315 if Comes_From_Source (Expec_Type) then
18316 Matching_Field := Expec_Type;
18317
18318 -- For an assignment, use name of target
18319
18320 elsif Nkind (Parent (Expr)) = N_Assignment_Statement
18321 and then Is_Entity_Name (Name (Parent (Expr)))
18322 then
18323 Matching_Field := Entity (Name (Parent (Expr)));
18324 end if;
18325
18326 return True;
18327
18328 elsif not Is_Record_Type (Expec_Type) then
18329 return False;
18330
18331 else
18332 E := First_Entity (Expec_Type);
18333 loop
18334 if No (E) then
18335 return False;
18336
18337 elsif not Ekind_In (E, E_Discriminant, E_Component)
18338 or else Nam_In (Chars (E), Name_uTag, Name_uParent)
18339 then
18340 Next_Entity (E);
18341
18342 else
18343 exit;
18344 end if;
18345 end loop;
18346
18347 if not Covers (Etype (E), Found_Type) then
18348 return False;
18349
18350 elsif Present (Next_Entity (E))
18351 and then (Ekind (E) = E_Component
18352 or else Ekind (Next_Entity (E)) = E_Discriminant)
18353 then
18354 return False;
18355
18356 else
18357 Matching_Field := E;
18358 return True;
18359 end if;
18360 end if;
18361 end Has_One_Matching_Field;
18362
18363 -- Start of processing for Wrong_Type
18364
18365 begin
18366 -- Don't output message if either type is Any_Type, or if a message
18367 -- has already been posted for this node. We need to do the latter
18368 -- check explicitly (it is ordinarily done in Errout), because we
18369 -- are using ! to force the output of the error messages.
18370
18371 if Expec_Type = Any_Type
18372 or else Found_Type = Any_Type
18373 or else Error_Posted (Expr)
18374 then
18375 return;
18376
18377 -- If one of the types is a Taft-Amendment type and the other it its
18378 -- completion, it must be an illegal use of a TAT in the spec, for
18379 -- which an error was already emitted. Avoid cascaded errors.
18380
18381 elsif Is_Incomplete_Type (Expec_Type)
18382 and then Has_Completion_In_Body (Expec_Type)
18383 and then Full_View (Expec_Type) = Etype (Expr)
18384 then
18385 return;
18386
18387 elsif Is_Incomplete_Type (Etype (Expr))
18388 and then Has_Completion_In_Body (Etype (Expr))
18389 and then Full_View (Etype (Expr)) = Expec_Type
18390 then
18391 return;
18392
18393 -- In an instance, there is an ongoing problem with completion of
18394 -- type derived from private types. Their structure is what Gigi
18395 -- expects, but the Etype is the parent type rather than the
18396 -- derived private type itself. Do not flag error in this case. The
18397 -- private completion is an entity without a parent, like an Itype.
18398 -- Similarly, full and partial views may be incorrect in the instance.
18399 -- There is no simple way to insure that it is consistent ???
18400
18401 -- A similar view discrepancy can happen in an inlined body, for the
18402 -- same reason: inserted body may be outside of the original package
18403 -- and only partial views are visible at the point of insertion.
18404
18405 elsif In_Instance or else In_Inlined_Body then
18406 if Etype (Etype (Expr)) = Etype (Expected_Type)
18407 and then
18408 (Has_Private_Declaration (Expected_Type)
18409 or else Has_Private_Declaration (Etype (Expr)))
18410 and then No (Parent (Expected_Type))
18411 then
18412 return;
18413
18414 elsif Nkind (Parent (Expr)) = N_Qualified_Expression
18415 and then Entity (Subtype_Mark (Parent (Expr))) = Expected_Type
18416 then
18417 return;
18418
18419 elsif Is_Private_Type (Expected_Type)
18420 and then Present (Full_View (Expected_Type))
18421 and then Covers (Full_View (Expected_Type), Etype (Expr))
18422 then
18423 return;
18424 end if;
18425 end if;
18426
18427 -- An interesting special check. If the expression is parenthesized
18428 -- and its type corresponds to the type of the sole component of the
18429 -- expected record type, or to the component type of the expected one
18430 -- dimensional array type, then assume we have a bad aggregate attempt.
18431
18432 if Nkind (Expr) in N_Subexpr
18433 and then Paren_Count (Expr) /= 0
18434 and then Has_One_Matching_Field
18435 then
18436 Error_Msg_N ("positional aggregate cannot have one component", Expr);
18437 if Present (Matching_Field) then
18438 if Is_Array_Type (Expec_Type) then
18439 Error_Msg_NE
18440 ("\write instead `&''First ='> ...`", Expr, Matching_Field);
18441
18442 else
18443 Error_Msg_NE
18444 ("\write instead `& ='> ...`", Expr, Matching_Field);
18445 end if;
18446 end if;
18447
18448 -- Another special check, if we are looking for a pool-specific access
18449 -- type and we found an E_Access_Attribute_Type, then we have the case
18450 -- of an Access attribute being used in a context which needs a pool-
18451 -- specific type, which is never allowed. The one extra check we make
18452 -- is that the expected designated type covers the Found_Type.
18453
18454 elsif Is_Access_Type (Expec_Type)
18455 and then Ekind (Found_Type) = E_Access_Attribute_Type
18456 and then Ekind (Base_Type (Expec_Type)) /= E_General_Access_Type
18457 and then Ekind (Base_Type (Expec_Type)) /= E_Anonymous_Access_Type
18458 and then Covers
18459 (Designated_Type (Expec_Type), Designated_Type (Found_Type))
18460 then
18461 Error_Msg_N -- CODEFIX
18462 ("result must be general access type!", Expr);
18463 Error_Msg_NE -- CODEFIX
18464 ("add ALL to }!", Expr, Expec_Type);
18465
18466 -- Another special check, if the expected type is an integer type,
18467 -- but the expression is of type System.Address, and the parent is
18468 -- an addition or subtraction operation whose left operand is the
18469 -- expression in question and whose right operand is of an integral
18470 -- type, then this is an attempt at address arithmetic, so give
18471 -- appropriate message.
18472
18473 elsif Is_Integer_Type (Expec_Type)
18474 and then Is_RTE (Found_Type, RE_Address)
18475 and then Nkind_In (Parent (Expr), N_Op_Add, N_Op_Subtract)
18476 and then Expr = Left_Opnd (Parent (Expr))
18477 and then Is_Integer_Type (Etype (Right_Opnd (Parent (Expr))))
18478 then
18479 Error_Msg_N
18480 ("address arithmetic not predefined in package System",
18481 Parent (Expr));
18482 Error_Msg_N
18483 ("\possible missing with/use of System.Storage_Elements",
18484 Parent (Expr));
18485 return;
18486
18487 -- If the expected type is an anonymous access type, as for access
18488 -- parameters and discriminants, the error is on the designated types.
18489
18490 elsif Ekind (Expec_Type) = E_Anonymous_Access_Type then
18491 if Comes_From_Source (Expec_Type) then
18492 Error_Msg_NE ("expected}!", Expr, Expec_Type);
18493 else
18494 Error_Msg_NE
18495 ("expected an access type with designated}",
18496 Expr, Designated_Type (Expec_Type));
18497 end if;
18498
18499 if Is_Access_Type (Found_Type)
18500 and then not Comes_From_Source (Found_Type)
18501 then
18502 Error_Msg_NE
18503 ("\\found an access type with designated}!",
18504 Expr, Designated_Type (Found_Type));
18505 else
18506 if From_Limited_With (Found_Type) then
18507 Error_Msg_NE ("\\found incomplete}!", Expr, Found_Type);
18508 Error_Msg_Qual_Level := 99;
18509 Error_Msg_NE -- CODEFIX
18510 ("\\missing `WITH &;", Expr, Scope (Found_Type));
18511 Error_Msg_Qual_Level := 0;
18512 else
18513 Error_Msg_NE ("found}!", Expr, Found_Type);
18514 end if;
18515 end if;
18516
18517 -- Normal case of one type found, some other type expected
18518
18519 else
18520 -- If the names of the two types are the same, see if some number
18521 -- of levels of qualification will help. Don't try more than three
18522 -- levels, and if we get to standard, it's no use (and probably
18523 -- represents an error in the compiler) Also do not bother with
18524 -- internal scope names.
18525
18526 declare
18527 Expec_Scope : Entity_Id;
18528 Found_Scope : Entity_Id;
18529
18530 begin
18531 Expec_Scope := Expec_Type;
18532 Found_Scope := Found_Type;
18533
18534 for Levels in Int range 0 .. 3 loop
18535 if Chars (Expec_Scope) /= Chars (Found_Scope) then
18536 Error_Msg_Qual_Level := Levels;
18537 exit;
18538 end if;
18539
18540 Expec_Scope := Scope (Expec_Scope);
18541 Found_Scope := Scope (Found_Scope);
18542
18543 exit when Expec_Scope = Standard_Standard
18544 or else Found_Scope = Standard_Standard
18545 or else not Comes_From_Source (Expec_Scope)
18546 or else not Comes_From_Source (Found_Scope);
18547 end loop;
18548 end;
18549
18550 if Is_Record_Type (Expec_Type)
18551 and then Present (Corresponding_Remote_Type (Expec_Type))
18552 then
18553 Error_Msg_NE ("expected}!", Expr,
18554 Corresponding_Remote_Type (Expec_Type));
18555 else
18556 Error_Msg_NE ("expected}!", Expr, Expec_Type);
18557 end if;
18558
18559 if Is_Entity_Name (Expr)
18560 and then Is_Package_Or_Generic_Package (Entity (Expr))
18561 then
18562 Error_Msg_N ("\\found package name!", Expr);
18563
18564 elsif Is_Entity_Name (Expr)
18565 and then Ekind_In (Entity (Expr), E_Procedure, E_Generic_Procedure)
18566 then
18567 if Ekind (Expec_Type) = E_Access_Subprogram_Type then
18568 Error_Msg_N
18569 ("found procedure name, possibly missing Access attribute!",
18570 Expr);
18571 else
18572 Error_Msg_N
18573 ("\\found procedure name instead of function!", Expr);
18574 end if;
18575
18576 elsif Nkind (Expr) = N_Function_Call
18577 and then Ekind (Expec_Type) = E_Access_Subprogram_Type
18578 and then Etype (Designated_Type (Expec_Type)) = Etype (Expr)
18579 and then No (Parameter_Associations (Expr))
18580 then
18581 Error_Msg_N
18582 ("found function name, possibly missing Access attribute!",
18583 Expr);
18584
18585 -- Catch common error: a prefix or infix operator which is not
18586 -- directly visible because the type isn't.
18587
18588 elsif Nkind (Expr) in N_Op
18589 and then Is_Overloaded (Expr)
18590 and then not Is_Immediately_Visible (Expec_Type)
18591 and then not Is_Potentially_Use_Visible (Expec_Type)
18592 and then not In_Use (Expec_Type)
18593 and then Has_Compatible_Type (Right_Opnd (Expr), Expec_Type)
18594 then
18595 Error_Msg_N
18596 ("operator of the type is not directly visible!", Expr);
18597
18598 elsif Ekind (Found_Type) = E_Void
18599 and then Present (Parent (Found_Type))
18600 and then Nkind (Parent (Found_Type)) = N_Full_Type_Declaration
18601 then
18602 Error_Msg_NE ("\\found premature usage of}!", Expr, Found_Type);
18603
18604 else
18605 Error_Msg_NE ("\\found}!", Expr, Found_Type);
18606 end if;
18607
18608 -- A special check for cases like M1 and M2 = 0 where M1 and M2 are
18609 -- of the same modular type, and (M1 and M2) = 0 was intended.
18610
18611 if Expec_Type = Standard_Boolean
18612 and then Is_Modular_Integer_Type (Found_Type)
18613 and then Nkind_In (Parent (Expr), N_Op_And, N_Op_Or, N_Op_Xor)
18614 and then Nkind (Right_Opnd (Parent (Expr))) in N_Op_Compare
18615 then
18616 declare
18617 Op : constant Node_Id := Right_Opnd (Parent (Expr));
18618 L : constant Node_Id := Left_Opnd (Op);
18619 R : constant Node_Id := Right_Opnd (Op);
18620
18621 begin
18622 -- The case for the message is when the left operand of the
18623 -- comparison is the same modular type, or when it is an
18624 -- integer literal (or other universal integer expression),
18625 -- which would have been typed as the modular type if the
18626 -- parens had been there.
18627
18628 if (Etype (L) = Found_Type
18629 or else
18630 Etype (L) = Universal_Integer)
18631 and then Is_Integer_Type (Etype (R))
18632 then
18633 Error_Msg_N
18634 ("\\possible missing parens for modular operation", Expr);
18635 end if;
18636 end;
18637 end if;
18638
18639 -- Reset error message qualification indication
18640
18641 Error_Msg_Qual_Level := 0;
18642 end if;
18643 end Wrong_Type;
18644
18645 end Sem_Util;
This page took 0.966226 seconds and 5 git commands to generate.