]> 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-2015, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Treepr; -- ???For debugging code below
27
28 with Aspects; use Aspects;
29 with Atree; use Atree;
30 with Casing; use Casing;
31 with Checks; use Checks;
32 with Debug; use Debug;
33 with Elists; use Elists;
34 with Errout; use Errout;
35 with Exp_Ch11; use Exp_Ch11;
36 with Exp_Disp; use Exp_Disp;
37 with Exp_Util; use Exp_Util;
38 with Fname; use Fname;
39 with Freeze; use Freeze;
40 with Ghost; use Ghost;
41 with Lib; use Lib;
42 with Lib.Xref; use Lib.Xref;
43 with Namet.Sp; use Namet.Sp;
44 with Nlists; use Nlists;
45 with Nmake; use Nmake;
46 with Output; use Output;
47 with Restrict; use Restrict;
48 with Rident; use Rident;
49 with Rtsfind; use Rtsfind;
50 with Sem; use Sem;
51 with Sem_Aux; use Sem_Aux;
52 with Sem_Attr; use Sem_Attr;
53 with Sem_Ch6; use Sem_Ch6;
54 with Sem_Ch8; use Sem_Ch8;
55 with Sem_Ch13; use Sem_Ch13;
56 with Sem_Disp; use Sem_Disp;
57 with Sem_Eval; use Sem_Eval;
58 with Sem_Prag; use Sem_Prag;
59 with Sem_Res; use Sem_Res;
60 with Sem_Warn; use Sem_Warn;
61 with Sem_Type; use Sem_Type;
62 with Sinfo; use Sinfo;
63 with Sinput; use Sinput;
64 with Stand; use Stand;
65 with Style;
66 with Stringt; use Stringt;
67 with Targparm; use Targparm;
68 with Tbuild; use Tbuild;
69 with Ttypes; use Ttypes;
70 with Uname; use Uname;
71
72 with GNAT.HTable; use GNAT.HTable;
73
74 package body Sem_Util is
75
76 ----------------------------------------
77 -- Global Variables for New_Copy_Tree --
78 ----------------------------------------
79
80 -- These global variables are used by New_Copy_Tree. See description of the
81 -- body of this subprogram for details. Global variables can be safely used
82 -- by New_Copy_Tree, since there is no case of a recursive call from the
83 -- processing inside New_Copy_Tree.
84
85 NCT_Hash_Threshold : constant := 20;
86 -- If there are more than this number of pairs of entries in the map, then
87 -- Hash_Tables_Used will be set, and the hash tables will be initialized
88 -- and used for the searches.
89
90 NCT_Hash_Tables_Used : Boolean := False;
91 -- Set to True if hash tables are in use
92
93 NCT_Table_Entries : Nat := 0;
94 -- Count entries in table to see if threshold is reached
95
96 NCT_Hash_Table_Setup : Boolean := False;
97 -- Set to True if hash table contains data. We set this True if we setup
98 -- the hash table with data, and leave it set permanently from then on,
99 -- this is a signal that second and subsequent users of the hash table
100 -- must clear the old entries before reuse.
101
102 subtype NCT_Header_Num is Int range 0 .. 511;
103 -- Defines range of headers in hash tables (512 headers)
104
105 -----------------------
106 -- Local Subprograms --
107 -----------------------
108
109 function Build_Component_Subtype
110 (C : List_Id;
111 Loc : Source_Ptr;
112 T : Entity_Id) return Node_Id;
113 -- This function builds the subtype for Build_Actual_Subtype_Of_Component
114 -- and Build_Discriminal_Subtype_Of_Component. C is a list of constraints,
115 -- Loc is the source location, T is the original subtype.
116
117 function Has_Enabled_Property
118 (Item_Id : Entity_Id;
119 Property : Name_Id) return Boolean;
120 -- Subsidiary to routines Async_xxx_Enabled and Effective_xxx_Enabled.
121 -- Determine whether an abstract state or a variable denoted by entity
122 -- Item_Id has enabled property Property.
123
124 function Has_Null_Extension (T : Entity_Id) return Boolean;
125 -- T is a derived tagged type. Check whether the type extension is null.
126 -- If the parent type is fully initialized, T can be treated as such.
127
128 function Is_Fully_Initialized_Variant (Typ : Entity_Id) return Boolean;
129 -- Subsidiary to Is_Fully_Initialized_Type. For an unconstrained type
130 -- with discriminants whose default values are static, examine only the
131 -- components in the selected variant to determine whether all of them
132 -- have a default.
133
134 ------------------------------
135 -- Abstract_Interface_List --
136 ------------------------------
137
138 function Abstract_Interface_List (Typ : Entity_Id) return List_Id is
139 Nod : Node_Id;
140
141 begin
142 if Is_Concurrent_Type (Typ) then
143
144 -- If we are dealing with a synchronized subtype, go to the base
145 -- type, whose declaration has the interface list.
146
147 -- Shouldn't this be Declaration_Node???
148
149 Nod := Parent (Base_Type (Typ));
150
151 if Nkind (Nod) = N_Full_Type_Declaration then
152 return Empty_List;
153 end if;
154
155 elsif Ekind (Typ) = E_Record_Type_With_Private then
156 if Nkind (Parent (Typ)) = N_Full_Type_Declaration then
157 Nod := Type_Definition (Parent (Typ));
158
159 elsif Nkind (Parent (Typ)) = N_Private_Type_Declaration then
160 if Present (Full_View (Typ))
161 and then
162 Nkind (Parent (Full_View (Typ))) = N_Full_Type_Declaration
163 then
164 Nod := Type_Definition (Parent (Full_View (Typ)));
165
166 -- If the full-view is not available we cannot do anything else
167 -- here (the source has errors).
168
169 else
170 return Empty_List;
171 end if;
172
173 -- Support for generic formals with interfaces is still missing ???
174
175 elsif Nkind (Parent (Typ)) = N_Formal_Type_Declaration then
176 return Empty_List;
177
178 else
179 pragma Assert
180 (Nkind (Parent (Typ)) = N_Private_Extension_Declaration);
181 Nod := Parent (Typ);
182 end if;
183
184 elsif Ekind (Typ) = E_Record_Subtype then
185 Nod := Type_Definition (Parent (Etype (Typ)));
186
187 elsif Ekind (Typ) = E_Record_Subtype_With_Private then
188
189 -- Recurse, because parent may still be a private extension. Also
190 -- note that the full view of the subtype or the full view of its
191 -- base type may (both) be unavailable.
192
193 return Abstract_Interface_List (Etype (Typ));
194
195 else pragma Assert ((Ekind (Typ)) = E_Record_Type);
196 if Nkind (Parent (Typ)) = N_Formal_Type_Declaration then
197 Nod := Formal_Type_Definition (Parent (Typ));
198 else
199 Nod := Type_Definition (Parent (Typ));
200 end if;
201 end if;
202
203 return Interface_List (Nod);
204 end Abstract_Interface_List;
205
206 --------------------------------
207 -- Add_Access_Type_To_Process --
208 --------------------------------
209
210 procedure Add_Access_Type_To_Process (E : Entity_Id; A : Entity_Id) is
211 L : Elist_Id;
212
213 begin
214 Ensure_Freeze_Node (E);
215 L := Access_Types_To_Process (Freeze_Node (E));
216
217 if No (L) then
218 L := New_Elmt_List;
219 Set_Access_Types_To_Process (Freeze_Node (E), L);
220 end if;
221
222 Append_Elmt (A, L);
223 end Add_Access_Type_To_Process;
224
225 --------------------------
226 -- Add_Block_Identifier --
227 --------------------------
228
229 procedure Add_Block_Identifier (N : Node_Id; Id : out Entity_Id) is
230 Loc : constant Source_Ptr := Sloc (N);
231
232 begin
233 pragma Assert (Nkind (N) = N_Block_Statement);
234
235 -- The block already has a label, return its entity
236
237 if Present (Identifier (N)) then
238 Id := Entity (Identifier (N));
239
240 -- Create a new block label and set its attributes
241
242 else
243 Id := New_Internal_Entity (E_Block, Current_Scope, Loc, 'B');
244 Set_Etype (Id, Standard_Void_Type);
245 Set_Parent (Id, N);
246
247 Set_Identifier (N, New_Occurrence_Of (Id, Loc));
248 Set_Block_Node (Id, Identifier (N));
249 end if;
250 end Add_Block_Identifier;
251
252 ----------------------------
253 -- Add_Global_Declaration --
254 ----------------------------
255
256 procedure Add_Global_Declaration (N : Node_Id) is
257 Aux_Node : constant Node_Id := Aux_Decls_Node (Cunit (Current_Sem_Unit));
258
259 begin
260 if No (Declarations (Aux_Node)) then
261 Set_Declarations (Aux_Node, New_List);
262 end if;
263
264 Append_To (Declarations (Aux_Node), N);
265 Analyze (N);
266 end Add_Global_Declaration;
267
268 --------------------------------
269 -- Address_Integer_Convert_OK --
270 --------------------------------
271
272 function Address_Integer_Convert_OK (T1, T2 : Entity_Id) return Boolean is
273 begin
274 if Allow_Integer_Address
275 and then ((Is_Descendant_Of_Address (T1)
276 and then Is_Private_Type (T1)
277 and then Is_Integer_Type (T2))
278 or else
279 (Is_Descendant_Of_Address (T2)
280 and then Is_Private_Type (T2)
281 and then Is_Integer_Type (T1)))
282 then
283 return True;
284 else
285 return False;
286 end if;
287 end Address_Integer_Convert_OK;
288
289 -----------------
290 -- Addressable --
291 -----------------
292
293 -- For now, just 8/16/32/64. but analyze later if AAMP is special???
294
295 function Addressable (V : Uint) return Boolean is
296 begin
297 return V = Uint_8 or else
298 V = Uint_16 or else
299 V = Uint_32 or else
300 V = Uint_64;
301 end Addressable;
302
303 function Addressable (V : Int) return Boolean is
304 begin
305 return V = 8 or else
306 V = 16 or else
307 V = 32 or else
308 V = 64;
309 end Addressable;
310
311 ---------------------------------
312 -- Aggregate_Constraint_Checks --
313 ---------------------------------
314
315 procedure Aggregate_Constraint_Checks
316 (Exp : Node_Id;
317 Check_Typ : Entity_Id)
318 is
319 Exp_Typ : constant Entity_Id := Etype (Exp);
320
321 begin
322 if Raises_Constraint_Error (Exp) then
323 return;
324 end if;
325
326 -- Ada 2005 (AI-230): Generate a conversion to an anonymous access
327 -- component's type to force the appropriate accessibility checks.
328
329 -- Ada 2005 (AI-231): Generate conversion to the null-excluding
330 -- type to force the corresponding run-time check
331
332 if Is_Access_Type (Check_Typ)
333 and then ((Is_Local_Anonymous_Access (Check_Typ))
334 or else (Can_Never_Be_Null (Check_Typ)
335 and then not Can_Never_Be_Null (Exp_Typ)))
336 then
337 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
338 Analyze_And_Resolve (Exp, Check_Typ);
339 Check_Unset_Reference (Exp);
340 end if;
341
342 -- This is really expansion activity, so make sure that expansion is
343 -- on and is allowed. In GNATprove mode, we also want check flags to
344 -- be added in the tree, so that the formal verification can rely on
345 -- those to be present. In GNATprove mode for formal verification, some
346 -- treatment typically only done during expansion needs to be performed
347 -- on the tree, but it should not be applied inside generics. Otherwise,
348 -- this breaks the name resolution mechanism for generic instances.
349
350 if not Expander_Active
351 and (Inside_A_Generic or not Full_Analysis or not GNATprove_Mode)
352 then
353 return;
354 end if;
355
356 -- First check if we have to insert discriminant checks
357
358 if Has_Discriminants (Exp_Typ) then
359 Apply_Discriminant_Check (Exp, Check_Typ);
360
361 -- Next emit length checks for array aggregates
362
363 elsif Is_Array_Type (Exp_Typ) then
364 Apply_Length_Check (Exp, Check_Typ);
365
366 -- Finally emit scalar and string checks. If we are dealing with a
367 -- scalar literal we need to check by hand because the Etype of
368 -- literals is not necessarily correct.
369
370 elsif Is_Scalar_Type (Exp_Typ)
371 and then Compile_Time_Known_Value (Exp)
372 then
373 if Is_Out_Of_Range (Exp, Base_Type (Check_Typ)) then
374 Apply_Compile_Time_Constraint_Error
375 (Exp, "value not in range of}??", CE_Range_Check_Failed,
376 Ent => Base_Type (Check_Typ),
377 Typ => Base_Type (Check_Typ));
378
379 elsif Is_Out_Of_Range (Exp, Check_Typ) then
380 Apply_Compile_Time_Constraint_Error
381 (Exp, "value not in range of}??", CE_Range_Check_Failed,
382 Ent => Check_Typ,
383 Typ => Check_Typ);
384
385 elsif not Range_Checks_Suppressed (Check_Typ) then
386 Apply_Scalar_Range_Check (Exp, Check_Typ);
387 end if;
388
389 -- Verify that target type is also scalar, to prevent view anomalies
390 -- in instantiations.
391
392 elsif (Is_Scalar_Type (Exp_Typ)
393 or else Nkind (Exp) = N_String_Literal)
394 and then Is_Scalar_Type (Check_Typ)
395 and then Exp_Typ /= Check_Typ
396 then
397 if Is_Entity_Name (Exp)
398 and then Ekind (Entity (Exp)) = E_Constant
399 then
400 -- If expression is a constant, it is worthwhile checking whether
401 -- it is a bound of the type.
402
403 if (Is_Entity_Name (Type_Low_Bound (Check_Typ))
404 and then Entity (Exp) = Entity (Type_Low_Bound (Check_Typ)))
405 or else
406 (Is_Entity_Name (Type_High_Bound (Check_Typ))
407 and then Entity (Exp) = Entity (Type_High_Bound (Check_Typ)))
408 then
409 return;
410
411 else
412 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
413 Analyze_And_Resolve (Exp, Check_Typ);
414 Check_Unset_Reference (Exp);
415 end if;
416
417 -- Could use a comment on this case ???
418
419 else
420 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
421 Analyze_And_Resolve (Exp, Check_Typ);
422 Check_Unset_Reference (Exp);
423 end if;
424
425 end if;
426 end Aggregate_Constraint_Checks;
427
428 -----------------------
429 -- Alignment_In_Bits --
430 -----------------------
431
432 function Alignment_In_Bits (E : Entity_Id) return Uint is
433 begin
434 return Alignment (E) * System_Storage_Unit;
435 end Alignment_In_Bits;
436
437 --------------------------------------
438 -- All_Composite_Constraints_Static --
439 --------------------------------------
440
441 function All_Composite_Constraints_Static
442 (Constr : Node_Id) return Boolean
443 is
444 begin
445 if No (Constr) or else Error_Posted (Constr) then
446 return True;
447 end if;
448
449 case Nkind (Constr) is
450 when N_Subexpr =>
451 if Nkind (Constr) in N_Has_Entity
452 and then Present (Entity (Constr))
453 then
454 if Is_Type (Entity (Constr)) then
455 return
456 not Is_Discrete_Type (Entity (Constr))
457 or else Is_OK_Static_Subtype (Entity (Constr));
458 end if;
459
460 elsif Nkind (Constr) = N_Range then
461 return
462 Is_OK_Static_Expression (Low_Bound (Constr))
463 and then
464 Is_OK_Static_Expression (High_Bound (Constr));
465
466 elsif Nkind (Constr) = N_Attribute_Reference
467 and then Attribute_Name (Constr) = Name_Range
468 then
469 return
470 Is_OK_Static_Expression
471 (Type_Low_Bound (Etype (Prefix (Constr))))
472 and then
473 Is_OK_Static_Expression
474 (Type_High_Bound (Etype (Prefix (Constr))));
475 end if;
476
477 return
478 not Present (Etype (Constr)) -- previous error
479 or else not Is_Discrete_Type (Etype (Constr))
480 or else Is_OK_Static_Expression (Constr);
481
482 when N_Discriminant_Association =>
483 return All_Composite_Constraints_Static (Expression (Constr));
484
485 when N_Range_Constraint =>
486 return
487 All_Composite_Constraints_Static (Range_Expression (Constr));
488
489 when N_Index_Or_Discriminant_Constraint =>
490 declare
491 One_Cstr : Entity_Id;
492 begin
493 One_Cstr := First (Constraints (Constr));
494 while Present (One_Cstr) loop
495 if not All_Composite_Constraints_Static (One_Cstr) then
496 return False;
497 end if;
498
499 Next (One_Cstr);
500 end loop;
501 end;
502
503 return True;
504
505 when N_Subtype_Indication =>
506 return
507 All_Composite_Constraints_Static (Subtype_Mark (Constr))
508 and then
509 All_Composite_Constraints_Static (Constraint (Constr));
510
511 when others =>
512 raise Program_Error;
513 end case;
514 end All_Composite_Constraints_Static;
515
516 ---------------------------------
517 -- Append_Inherited_Subprogram --
518 ---------------------------------
519
520 procedure Append_Inherited_Subprogram (S : Entity_Id) is
521 Par : constant Entity_Id := Alias (S);
522 -- The parent subprogram
523
524 Scop : constant Entity_Id := Scope (Par);
525 -- The scope of definition of the parent subprogram
526
527 Typ : constant Entity_Id := Defining_Entity (Parent (S));
528 -- The derived type of which S is a primitive operation
529
530 Decl : Node_Id;
531 Next_E : Entity_Id;
532
533 begin
534 if Ekind (Current_Scope) = E_Package
535 and then In_Private_Part (Current_Scope)
536 and then Has_Private_Declaration (Typ)
537 and then Is_Tagged_Type (Typ)
538 and then Scop = Current_Scope
539 then
540 -- The inherited operation is available at the earliest place after
541 -- the derived type declaration ( RM 7.3.1 (6/1)). This is only
542 -- relevant for type extensions. If the parent operation appears
543 -- after the type extension, the operation is not visible.
544
545 Decl := First
546 (Visible_Declarations
547 (Package_Specification (Current_Scope)));
548 while Present (Decl) loop
549 if Nkind (Decl) = N_Private_Extension_Declaration
550 and then Defining_Entity (Decl) = Typ
551 then
552 if Sloc (Decl) > Sloc (Par) then
553 Next_E := Next_Entity (Par);
554 Set_Next_Entity (Par, S);
555 Set_Next_Entity (S, Next_E);
556 return;
557
558 else
559 exit;
560 end if;
561 end if;
562
563 Next (Decl);
564 end loop;
565 end if;
566
567 -- If partial view is not a type extension, or it appears before the
568 -- subprogram declaration, insert normally at end of entity list.
569
570 Append_Entity (S, Current_Scope);
571 end Append_Inherited_Subprogram;
572
573 -----------------------------------------
574 -- Apply_Compile_Time_Constraint_Error --
575 -----------------------------------------
576
577 procedure Apply_Compile_Time_Constraint_Error
578 (N : Node_Id;
579 Msg : String;
580 Reason : RT_Exception_Code;
581 Ent : Entity_Id := Empty;
582 Typ : Entity_Id := Empty;
583 Loc : Source_Ptr := No_Location;
584 Rep : Boolean := True;
585 Warn : Boolean := False)
586 is
587 Stat : constant Boolean := Is_Static_Expression (N);
588 R_Stat : constant Node_Id :=
589 Make_Raise_Constraint_Error (Sloc (N), Reason => Reason);
590 Rtyp : Entity_Id;
591
592 begin
593 if No (Typ) then
594 Rtyp := Etype (N);
595 else
596 Rtyp := Typ;
597 end if;
598
599 Discard_Node
600 (Compile_Time_Constraint_Error (N, Msg, Ent, Loc, Warn => Warn));
601
602 -- In GNATprove mode, do not replace the node with an exception raised.
603 -- In such a case, either the call to Compile_Time_Constraint_Error
604 -- issues an error which stops analysis, or it issues a warning in
605 -- a few cases where a suitable check flag is set for GNATprove to
606 -- generate a check message.
607
608 if not Rep or GNATprove_Mode then
609 return;
610 end if;
611
612 -- Now we replace the node by an N_Raise_Constraint_Error node
613 -- This does not need reanalyzing, so set it as analyzed now.
614
615 Rewrite (N, R_Stat);
616 Set_Analyzed (N, True);
617
618 Set_Etype (N, Rtyp);
619 Set_Raises_Constraint_Error (N);
620
621 -- Now deal with possible local raise handling
622
623 Possible_Local_Raise (N, Standard_Constraint_Error);
624
625 -- If the original expression was marked as static, the result is
626 -- still marked as static, but the Raises_Constraint_Error flag is
627 -- always set so that further static evaluation is not attempted.
628
629 if Stat then
630 Set_Is_Static_Expression (N);
631 end if;
632 end Apply_Compile_Time_Constraint_Error;
633
634 ---------------------------
635 -- Async_Readers_Enabled --
636 ---------------------------
637
638 function Async_Readers_Enabled (Id : Entity_Id) return Boolean is
639 begin
640 return Has_Enabled_Property (Id, Name_Async_Readers);
641 end Async_Readers_Enabled;
642
643 ---------------------------
644 -- Async_Writers_Enabled --
645 ---------------------------
646
647 function Async_Writers_Enabled (Id : Entity_Id) return Boolean is
648 begin
649 return Has_Enabled_Property (Id, Name_Async_Writers);
650 end Async_Writers_Enabled;
651
652 --------------------------------------
653 -- Available_Full_View_Of_Component --
654 --------------------------------------
655
656 function Available_Full_View_Of_Component (T : Entity_Id) return Boolean is
657 ST : constant Entity_Id := Scope (T);
658 SCT : constant Entity_Id := Scope (Component_Type (T));
659 begin
660 return In_Open_Scopes (ST)
661 and then In_Open_Scopes (SCT)
662 and then Scope_Depth (ST) >= Scope_Depth (SCT);
663 end Available_Full_View_Of_Component;
664
665 -------------------
666 -- Bad_Attribute --
667 -------------------
668
669 procedure Bad_Attribute
670 (N : Node_Id;
671 Nam : Name_Id;
672 Warn : Boolean := False)
673 is
674 begin
675 Error_Msg_Warn := Warn;
676 Error_Msg_N ("unrecognized attribute&<<", N);
677
678 -- Check for possible misspelling
679
680 Error_Msg_Name_1 := First_Attribute_Name;
681 while Error_Msg_Name_1 <= Last_Attribute_Name loop
682 if Is_Bad_Spelling_Of (Nam, Error_Msg_Name_1) then
683 Error_Msg_N -- CODEFIX
684 ("\possible misspelling of %<<", N);
685 exit;
686 end if;
687
688 Error_Msg_Name_1 := Error_Msg_Name_1 + 1;
689 end loop;
690 end Bad_Attribute;
691
692 --------------------------------
693 -- Bad_Predicated_Subtype_Use --
694 --------------------------------
695
696 procedure Bad_Predicated_Subtype_Use
697 (Msg : String;
698 N : Node_Id;
699 Typ : Entity_Id;
700 Suggest_Static : Boolean := False)
701 is
702 Gen : Entity_Id;
703
704 begin
705 -- Avoid cascaded errors
706
707 if Error_Posted (N) then
708 return;
709 end if;
710
711 if Inside_A_Generic then
712 Gen := Current_Scope;
713 while Present (Gen) and then Ekind (Gen) /= E_Generic_Package loop
714 Gen := Scope (Gen);
715 end loop;
716
717 if No (Gen) then
718 return;
719 end if;
720
721 if Is_Generic_Formal (Typ) and then Is_Discrete_Type (Typ) then
722 Set_No_Predicate_On_Actual (Typ);
723 end if;
724
725 elsif Has_Predicates (Typ) then
726 if Is_Generic_Actual_Type (Typ) then
727
728 -- The restriction on loop parameters is only that the type
729 -- should have no dynamic predicates.
730
731 if Nkind (Parent (N)) = N_Loop_Parameter_Specification
732 and then not Has_Dynamic_Predicate_Aspect (Typ)
733 and then Is_OK_Static_Subtype (Typ)
734 then
735 return;
736 end if;
737
738 Gen := Current_Scope;
739 while not Is_Generic_Instance (Gen) loop
740 Gen := Scope (Gen);
741 end loop;
742
743 pragma Assert (Present (Gen));
744
745 if Ekind (Gen) = E_Package and then In_Package_Body (Gen) then
746 Error_Msg_Warn := SPARK_Mode /= On;
747 Error_Msg_FE (Msg & "<<", N, Typ);
748 Error_Msg_F ("\Program_Error [<<", N);
749
750 Insert_Action (N,
751 Make_Raise_Program_Error (Sloc (N),
752 Reason => PE_Bad_Predicated_Generic_Type));
753
754 else
755 Error_Msg_FE (Msg & "<<", N, Typ);
756 end if;
757
758 else
759 Error_Msg_FE (Msg, N, Typ);
760 end if;
761
762 -- Emit an optional suggestion on how to remedy the error if the
763 -- context warrants it.
764
765 if Suggest_Static and then Has_Static_Predicate (Typ) then
766 Error_Msg_FE ("\predicate of & should be marked static", N, Typ);
767 end if;
768 end if;
769 end Bad_Predicated_Subtype_Use;
770
771 -----------------------------------------
772 -- Bad_Unordered_Enumeration_Reference --
773 -----------------------------------------
774
775 function Bad_Unordered_Enumeration_Reference
776 (N : Node_Id;
777 T : Entity_Id) return Boolean
778 is
779 begin
780 return Is_Enumeration_Type (T)
781 and then Warn_On_Unordered_Enumeration_Type
782 and then not Is_Generic_Type (T)
783 and then Comes_From_Source (N)
784 and then not Has_Pragma_Ordered (T)
785 and then not In_Same_Extended_Unit (N, T);
786 end Bad_Unordered_Enumeration_Reference;
787
788 --------------------------
789 -- Build_Actual_Subtype --
790 --------------------------
791
792 function Build_Actual_Subtype
793 (T : Entity_Id;
794 N : Node_Or_Entity_Id) return Node_Id
795 is
796 Loc : Source_Ptr;
797 -- Normally Sloc (N), but may point to corresponding body in some cases
798
799 Constraints : List_Id;
800 Decl : Node_Id;
801 Discr : Entity_Id;
802 Hi : Node_Id;
803 Lo : Node_Id;
804 Subt : Entity_Id;
805 Disc_Type : Entity_Id;
806 Obj : Node_Id;
807
808 begin
809 Loc := Sloc (N);
810
811 if Nkind (N) = N_Defining_Identifier then
812 Obj := New_Occurrence_Of (N, Loc);
813
814 -- If this is a formal parameter of a subprogram declaration, and
815 -- we are compiling the body, we want the declaration for the
816 -- actual subtype to carry the source position of the body, to
817 -- prevent anomalies in gdb when stepping through the code.
818
819 if Is_Formal (N) then
820 declare
821 Decl : constant Node_Id := Unit_Declaration_Node (Scope (N));
822 begin
823 if Nkind (Decl) = N_Subprogram_Declaration
824 and then Present (Corresponding_Body (Decl))
825 then
826 Loc := Sloc (Corresponding_Body (Decl));
827 end if;
828 end;
829 end if;
830
831 else
832 Obj := N;
833 end if;
834
835 if Is_Array_Type (T) then
836 Constraints := New_List;
837 for J in 1 .. Number_Dimensions (T) loop
838
839 -- Build an array subtype declaration with the nominal subtype and
840 -- the bounds of the actual. Add the declaration in front of the
841 -- local declarations for the subprogram, for analysis before any
842 -- reference to the formal in the body.
843
844 Lo :=
845 Make_Attribute_Reference (Loc,
846 Prefix =>
847 Duplicate_Subexpr_No_Checks (Obj, Name_Req => True),
848 Attribute_Name => Name_First,
849 Expressions => New_List (
850 Make_Integer_Literal (Loc, J)));
851
852 Hi :=
853 Make_Attribute_Reference (Loc,
854 Prefix =>
855 Duplicate_Subexpr_No_Checks (Obj, Name_Req => True),
856 Attribute_Name => Name_Last,
857 Expressions => New_List (
858 Make_Integer_Literal (Loc, J)));
859
860 Append (Make_Range (Loc, Lo, Hi), Constraints);
861 end loop;
862
863 -- If the type has unknown discriminants there is no constrained
864 -- subtype to build. This is never called for a formal or for a
865 -- lhs, so returning the type is ok ???
866
867 elsif Has_Unknown_Discriminants (T) then
868 return T;
869
870 else
871 Constraints := New_List;
872
873 -- Type T is a generic derived type, inherit the discriminants from
874 -- the parent type.
875
876 if Is_Private_Type (T)
877 and then No (Full_View (T))
878
879 -- T was flagged as an error if it was declared as a formal
880 -- derived type with known discriminants. In this case there
881 -- is no need to look at the parent type since T already carries
882 -- its own discriminants.
883
884 and then not Error_Posted (T)
885 then
886 Disc_Type := Etype (Base_Type (T));
887 else
888 Disc_Type := T;
889 end if;
890
891 Discr := First_Discriminant (Disc_Type);
892 while Present (Discr) loop
893 Append_To (Constraints,
894 Make_Selected_Component (Loc,
895 Prefix =>
896 Duplicate_Subexpr_No_Checks (Obj),
897 Selector_Name => New_Occurrence_Of (Discr, Loc)));
898 Next_Discriminant (Discr);
899 end loop;
900 end if;
901
902 Subt := Make_Temporary (Loc, 'S', Related_Node => N);
903 Set_Is_Internal (Subt);
904
905 Decl :=
906 Make_Subtype_Declaration (Loc,
907 Defining_Identifier => Subt,
908 Subtype_Indication =>
909 Make_Subtype_Indication (Loc,
910 Subtype_Mark => New_Occurrence_Of (T, Loc),
911 Constraint =>
912 Make_Index_Or_Discriminant_Constraint (Loc,
913 Constraints => Constraints)));
914
915 Mark_Rewrite_Insertion (Decl);
916 return Decl;
917 end Build_Actual_Subtype;
918
919 ---------------------------------------
920 -- Build_Actual_Subtype_Of_Component --
921 ---------------------------------------
922
923 function Build_Actual_Subtype_Of_Component
924 (T : Entity_Id;
925 N : Node_Id) return Node_Id
926 is
927 Loc : constant Source_Ptr := Sloc (N);
928 P : constant Node_Id := Prefix (N);
929 D : Elmt_Id;
930 Id : Node_Id;
931 Index_Typ : Entity_Id;
932
933 Desig_Typ : Entity_Id;
934 -- This is either a copy of T, or if T is an access type, then it is
935 -- the directly designated type of this access type.
936
937 function Build_Actual_Array_Constraint return List_Id;
938 -- If one or more of the bounds of the component depends on
939 -- discriminants, build actual constraint using the discriminants
940 -- of the prefix.
941
942 function Build_Actual_Record_Constraint return List_Id;
943 -- Similar to previous one, for discriminated components constrained
944 -- by the discriminant of the enclosing object.
945
946 -----------------------------------
947 -- Build_Actual_Array_Constraint --
948 -----------------------------------
949
950 function Build_Actual_Array_Constraint return List_Id is
951 Constraints : constant List_Id := New_List;
952 Indx : Node_Id;
953 Hi : Node_Id;
954 Lo : Node_Id;
955 Old_Hi : Node_Id;
956 Old_Lo : Node_Id;
957
958 begin
959 Indx := First_Index (Desig_Typ);
960 while Present (Indx) loop
961 Old_Lo := Type_Low_Bound (Etype (Indx));
962 Old_Hi := Type_High_Bound (Etype (Indx));
963
964 if Denotes_Discriminant (Old_Lo) then
965 Lo :=
966 Make_Selected_Component (Loc,
967 Prefix => New_Copy_Tree (P),
968 Selector_Name => New_Occurrence_Of (Entity (Old_Lo), Loc));
969
970 else
971 Lo := New_Copy_Tree (Old_Lo);
972
973 -- The new bound will be reanalyzed in the enclosing
974 -- declaration. For literal bounds that come from a type
975 -- declaration, the type of the context must be imposed, so
976 -- insure that analysis will take place. For non-universal
977 -- types this is not strictly necessary.
978
979 Set_Analyzed (Lo, False);
980 end if;
981
982 if Denotes_Discriminant (Old_Hi) then
983 Hi :=
984 Make_Selected_Component (Loc,
985 Prefix => New_Copy_Tree (P),
986 Selector_Name => New_Occurrence_Of (Entity (Old_Hi), Loc));
987
988 else
989 Hi := New_Copy_Tree (Old_Hi);
990 Set_Analyzed (Hi, False);
991 end if;
992
993 Append (Make_Range (Loc, Lo, Hi), Constraints);
994 Next_Index (Indx);
995 end loop;
996
997 return Constraints;
998 end Build_Actual_Array_Constraint;
999
1000 ------------------------------------
1001 -- Build_Actual_Record_Constraint --
1002 ------------------------------------
1003
1004 function Build_Actual_Record_Constraint return List_Id is
1005 Constraints : constant List_Id := New_List;
1006 D : Elmt_Id;
1007 D_Val : Node_Id;
1008
1009 begin
1010 D := First_Elmt (Discriminant_Constraint (Desig_Typ));
1011 while Present (D) loop
1012 if Denotes_Discriminant (Node (D)) then
1013 D_Val := Make_Selected_Component (Loc,
1014 Prefix => New_Copy_Tree (P),
1015 Selector_Name => New_Occurrence_Of (Entity (Node (D)), Loc));
1016
1017 else
1018 D_Val := New_Copy_Tree (Node (D));
1019 end if;
1020
1021 Append (D_Val, Constraints);
1022 Next_Elmt (D);
1023 end loop;
1024
1025 return Constraints;
1026 end Build_Actual_Record_Constraint;
1027
1028 -- Start of processing for Build_Actual_Subtype_Of_Component
1029
1030 begin
1031 -- Why the test for Spec_Expression mode here???
1032
1033 if In_Spec_Expression then
1034 return Empty;
1035
1036 -- More comments for the rest of this body would be good ???
1037
1038 elsif Nkind (N) = N_Explicit_Dereference then
1039 if Is_Composite_Type (T)
1040 and then not Is_Constrained (T)
1041 and then not (Is_Class_Wide_Type (T)
1042 and then Is_Constrained (Root_Type (T)))
1043 and then not Has_Unknown_Discriminants (T)
1044 then
1045 -- If the type of the dereference is already constrained, it is an
1046 -- actual subtype.
1047
1048 if Is_Array_Type (Etype (N))
1049 and then Is_Constrained (Etype (N))
1050 then
1051 return Empty;
1052 else
1053 Remove_Side_Effects (P);
1054 return Build_Actual_Subtype (T, N);
1055 end if;
1056 else
1057 return Empty;
1058 end if;
1059 end if;
1060
1061 if Ekind (T) = E_Access_Subtype then
1062 Desig_Typ := Designated_Type (T);
1063 else
1064 Desig_Typ := T;
1065 end if;
1066
1067 if Ekind (Desig_Typ) = E_Array_Subtype then
1068 Id := First_Index (Desig_Typ);
1069 while Present (Id) loop
1070 Index_Typ := Underlying_Type (Etype (Id));
1071
1072 if Denotes_Discriminant (Type_Low_Bound (Index_Typ))
1073 or else
1074 Denotes_Discriminant (Type_High_Bound (Index_Typ))
1075 then
1076 Remove_Side_Effects (P);
1077 return
1078 Build_Component_Subtype
1079 (Build_Actual_Array_Constraint, Loc, Base_Type (T));
1080 end if;
1081
1082 Next_Index (Id);
1083 end loop;
1084
1085 elsif Is_Composite_Type (Desig_Typ)
1086 and then Has_Discriminants (Desig_Typ)
1087 and then not Has_Unknown_Discriminants (Desig_Typ)
1088 then
1089 if Is_Private_Type (Desig_Typ)
1090 and then No (Discriminant_Constraint (Desig_Typ))
1091 then
1092 Desig_Typ := Full_View (Desig_Typ);
1093 end if;
1094
1095 D := First_Elmt (Discriminant_Constraint (Desig_Typ));
1096 while Present (D) loop
1097 if Denotes_Discriminant (Node (D)) then
1098 Remove_Side_Effects (P);
1099 return
1100 Build_Component_Subtype (
1101 Build_Actual_Record_Constraint, Loc, Base_Type (T));
1102 end if;
1103
1104 Next_Elmt (D);
1105 end loop;
1106 end if;
1107
1108 -- If none of the above, the actual and nominal subtypes are the same
1109
1110 return Empty;
1111 end Build_Actual_Subtype_Of_Component;
1112
1113 -----------------------------
1114 -- Build_Component_Subtype --
1115 -----------------------------
1116
1117 function Build_Component_Subtype
1118 (C : List_Id;
1119 Loc : Source_Ptr;
1120 T : Entity_Id) return Node_Id
1121 is
1122 Subt : Entity_Id;
1123 Decl : Node_Id;
1124
1125 begin
1126 -- Unchecked_Union components do not require component subtypes
1127
1128 if Is_Unchecked_Union (T) then
1129 return Empty;
1130 end if;
1131
1132 Subt := Make_Temporary (Loc, 'S');
1133 Set_Is_Internal (Subt);
1134
1135 Decl :=
1136 Make_Subtype_Declaration (Loc,
1137 Defining_Identifier => Subt,
1138 Subtype_Indication =>
1139 Make_Subtype_Indication (Loc,
1140 Subtype_Mark => New_Occurrence_Of (Base_Type (T), Loc),
1141 Constraint =>
1142 Make_Index_Or_Discriminant_Constraint (Loc,
1143 Constraints => C)));
1144
1145 Mark_Rewrite_Insertion (Decl);
1146 return Decl;
1147 end Build_Component_Subtype;
1148
1149 ----------------------------------
1150 -- Build_Default_Init_Cond_Call --
1151 ----------------------------------
1152
1153 function Build_Default_Init_Cond_Call
1154 (Loc : Source_Ptr;
1155 Obj_Id : Entity_Id;
1156 Typ : Entity_Id) return Node_Id
1157 is
1158 Proc_Id : constant Entity_Id := Default_Init_Cond_Procedure (Typ);
1159 Formal_Typ : constant Entity_Id := Etype (First_Formal (Proc_Id));
1160
1161 begin
1162 return
1163 Make_Procedure_Call_Statement (Loc,
1164 Name => New_Occurrence_Of (Proc_Id, Loc),
1165 Parameter_Associations => New_List (
1166 Make_Unchecked_Type_Conversion (Loc,
1167 Subtype_Mark => New_Occurrence_Of (Formal_Typ, Loc),
1168 Expression => New_Occurrence_Of (Obj_Id, Loc))));
1169 end Build_Default_Init_Cond_Call;
1170
1171 ----------------------------------------------
1172 -- Build_Default_Init_Cond_Procedure_Bodies --
1173 ----------------------------------------------
1174
1175 procedure Build_Default_Init_Cond_Procedure_Bodies (Priv_Decls : List_Id) is
1176 procedure Build_Default_Init_Cond_Procedure_Body (Typ : Entity_Id);
1177 -- If type Typ is subject to pragma Default_Initial_Condition, build the
1178 -- body of the procedure which verifies the assumption of the pragma at
1179 -- run time. The generated body is added after the type declaration.
1180
1181 --------------------------------------------
1182 -- Build_Default_Init_Cond_Procedure_Body --
1183 --------------------------------------------
1184
1185 procedure Build_Default_Init_Cond_Procedure_Body (Typ : Entity_Id) is
1186 Param_Id : Entity_Id;
1187 -- The entity of the sole formal parameter of the default initial
1188 -- condition procedure.
1189
1190 procedure Replace_Type_Reference (N : Node_Id);
1191 -- Replace a single reference to type Typ with a reference to formal
1192 -- parameter Param_Id.
1193
1194 ----------------------------
1195 -- Replace_Type_Reference --
1196 ----------------------------
1197
1198 procedure Replace_Type_Reference (N : Node_Id) is
1199 begin
1200 Rewrite (N, New_Occurrence_Of (Param_Id, Sloc (N)));
1201 end Replace_Type_Reference;
1202
1203 procedure Replace_Type_References is
1204 new Replace_Type_References_Generic (Replace_Type_Reference);
1205
1206 -- Local variables
1207
1208 Loc : constant Source_Ptr := Sloc (Typ);
1209 Prag : constant Node_Id :=
1210 Get_Pragma (Typ, Pragma_Default_Initial_Condition);
1211 Proc_Id : constant Entity_Id := Default_Init_Cond_Procedure (Typ);
1212 Spec_Decl : constant Node_Id := Unit_Declaration_Node (Proc_Id);
1213 Body_Decl : Node_Id;
1214 Expr : Node_Id;
1215 Stmt : Node_Id;
1216
1217 Save_Ghost_Mode : constant Ghost_Mode_Type := Ghost_Mode;
1218
1219 -- Start of processing for Build_Default_Init_Cond_Procedure_Body
1220
1221 begin
1222 -- The procedure should be generated only for [sub]types subject to
1223 -- pragma Default_Initial_Condition. Types that inherit the pragma do
1224 -- not get this specialized procedure.
1225
1226 pragma Assert (Has_Default_Init_Cond (Typ));
1227 pragma Assert (Present (Prag));
1228 pragma Assert (Present (Proc_Id));
1229
1230 -- Nothing to do if the body was already built
1231
1232 if Present (Corresponding_Body (Spec_Decl)) then
1233 return;
1234 end if;
1235
1236 -- The related type may be subject to pragma Ghost. Set the mode now
1237 -- to ensure that the analysis and expansion produce Ghost nodes.
1238
1239 Set_Ghost_Mode_From_Entity (Typ);
1240
1241 Param_Id := First_Formal (Proc_Id);
1242
1243 -- The pragma has an argument. Note that the argument is analyzed
1244 -- after all references to the current instance of the type are
1245 -- replaced.
1246
1247 if Present (Pragma_Argument_Associations (Prag)) then
1248 Expr :=
1249 Get_Pragma_Arg (First (Pragma_Argument_Associations (Prag)));
1250
1251 if Nkind (Expr) = N_Null then
1252 Stmt := Make_Null_Statement (Loc);
1253
1254 -- Preserve the original argument of the pragma by replicating it.
1255 -- Replace all references to the current instance of the type with
1256 -- references to the formal parameter.
1257
1258 else
1259 Expr := New_Copy_Tree (Expr);
1260 Replace_Type_References (Expr, Typ);
1261
1262 -- Generate:
1263 -- pragma Check (Default_Initial_Condition, <Expr>);
1264
1265 Stmt :=
1266 Make_Pragma (Loc,
1267 Pragma_Identifier =>
1268 Make_Identifier (Loc, Name_Check),
1269
1270 Pragma_Argument_Associations => New_List (
1271 Make_Pragma_Argument_Association (Loc,
1272 Expression =>
1273 Make_Identifier (Loc,
1274 Chars => Name_Default_Initial_Condition)),
1275 Make_Pragma_Argument_Association (Loc,
1276 Expression => Expr)));
1277 end if;
1278
1279 -- Otherwise the pragma appears without an argument
1280
1281 else
1282 Stmt := Make_Null_Statement (Loc);
1283 end if;
1284
1285 -- Generate:
1286 -- procedure <Typ>Default_Init_Cond (I : <Typ>) is
1287 -- begin
1288 -- <Stmt>;
1289 -- end <Typ>Default_Init_Cond;
1290
1291 Body_Decl :=
1292 Make_Subprogram_Body (Loc,
1293 Specification =>
1294 Copy_Separate_Tree (Specification (Spec_Decl)),
1295 Declarations => Empty_List,
1296 Handled_Statement_Sequence =>
1297 Make_Handled_Sequence_Of_Statements (Loc,
1298 Statements => New_List (Stmt)));
1299
1300 -- Link the spec and body of the default initial condition procedure
1301 -- to prevent the generation of a duplicate body.
1302
1303 Set_Corresponding_Body (Spec_Decl, Defining_Entity (Body_Decl));
1304 Set_Corresponding_Spec (Body_Decl, Proc_Id);
1305
1306 Insert_After_And_Analyze (Declaration_Node (Typ), Body_Decl);
1307 Ghost_Mode := Save_Ghost_Mode;
1308 end Build_Default_Init_Cond_Procedure_Body;
1309
1310 -- Local variables
1311
1312 Decl : Node_Id;
1313 Typ : Entity_Id;
1314
1315 -- Start of processing for Build_Default_Init_Cond_Procedure_Bodies
1316
1317 begin
1318 -- Inspect the private declarations looking for [sub]type declarations
1319
1320 Decl := First (Priv_Decls);
1321 while Present (Decl) loop
1322 if Nkind_In (Decl, N_Full_Type_Declaration,
1323 N_Subtype_Declaration)
1324 then
1325 Typ := Defining_Entity (Decl);
1326
1327 -- Guard against partially decorate types due to previous errors
1328
1329 if Is_Type (Typ) then
1330
1331 -- If the type is subject to pragma Default_Initial_Condition,
1332 -- generate the body of the internal procedure which verifies
1333 -- the assertion of the pragma at run time.
1334
1335 if Has_Default_Init_Cond (Typ) then
1336 Build_Default_Init_Cond_Procedure_Body (Typ);
1337
1338 -- A derived type inherits the default initial condition
1339 -- procedure from its parent type.
1340
1341 elsif Has_Inherited_Default_Init_Cond (Typ) then
1342 Inherit_Default_Init_Cond_Procedure (Typ);
1343 end if;
1344 end if;
1345 end if;
1346
1347 Next (Decl);
1348 end loop;
1349 end Build_Default_Init_Cond_Procedure_Bodies;
1350
1351 ---------------------------------------------------
1352 -- Build_Default_Init_Cond_Procedure_Declaration --
1353 ---------------------------------------------------
1354
1355 procedure Build_Default_Init_Cond_Procedure_Declaration (Typ : Entity_Id) is
1356 Loc : constant Source_Ptr := Sloc (Typ);
1357 Prag : constant Node_Id :=
1358 Get_Pragma (Typ, Pragma_Default_Initial_Condition);
1359
1360 Save_Ghost_Mode : constant Ghost_Mode_Type := Ghost_Mode;
1361
1362 Proc_Id : Entity_Id;
1363
1364 begin
1365 -- The procedure should be generated only for types subject to pragma
1366 -- Default_Initial_Condition. Types that inherit the pragma do not get
1367 -- this specialized procedure.
1368
1369 pragma Assert (Has_Default_Init_Cond (Typ));
1370 pragma Assert (Present (Prag));
1371
1372 -- Nothing to do if default initial condition procedure already built
1373
1374 if Present (Default_Init_Cond_Procedure (Typ)) then
1375 return;
1376 end if;
1377
1378 -- The related type may be subject to pragma Ghost. Set the mode now to
1379 -- ensure that the analysis and expansion produce Ghost nodes.
1380
1381 Set_Ghost_Mode_From_Entity (Typ);
1382
1383 Proc_Id :=
1384 Make_Defining_Identifier (Loc,
1385 Chars => New_External_Name (Chars (Typ), "Default_Init_Cond"));
1386
1387 -- Associate default initial condition procedure with the private type
1388
1389 Set_Ekind (Proc_Id, E_Procedure);
1390 Set_Is_Default_Init_Cond_Procedure (Proc_Id);
1391 Set_Default_Init_Cond_Procedure (Typ, Proc_Id);
1392
1393 -- Mark the default initial condition procedure explicitly as Ghost
1394 -- because it does not come from source.
1395
1396 if Ghost_Mode > None then
1397 Set_Is_Ghost_Entity (Proc_Id);
1398 end if;
1399
1400 -- Generate:
1401 -- procedure <Typ>Default_Init_Cond (Inn : <Typ>);
1402
1403 Insert_After_And_Analyze (Prag,
1404 Make_Subprogram_Declaration (Loc,
1405 Specification =>
1406 Make_Procedure_Specification (Loc,
1407 Defining_Unit_Name => Proc_Id,
1408 Parameter_Specifications => New_List (
1409 Make_Parameter_Specification (Loc,
1410 Defining_Identifier => Make_Temporary (Loc, 'I'),
1411 Parameter_Type => New_Occurrence_Of (Typ, Loc))))));
1412
1413 Ghost_Mode := Save_Ghost_Mode;
1414 end Build_Default_Init_Cond_Procedure_Declaration;
1415
1416 ---------------------------
1417 -- Build_Default_Subtype --
1418 ---------------------------
1419
1420 function Build_Default_Subtype
1421 (T : Entity_Id;
1422 N : Node_Id) return Entity_Id
1423 is
1424 Loc : constant Source_Ptr := Sloc (N);
1425 Disc : Entity_Id;
1426
1427 Bas : Entity_Id;
1428 -- The base type that is to be constrained by the defaults
1429
1430 begin
1431 if not Has_Discriminants (T) or else Is_Constrained (T) then
1432 return T;
1433 end if;
1434
1435 Bas := Base_Type (T);
1436
1437 -- If T is non-private but its base type is private, this is the
1438 -- completion of a subtype declaration whose parent type is private
1439 -- (see Complete_Private_Subtype in Sem_Ch3). The proper discriminants
1440 -- are to be found in the full view of the base. Check that the private
1441 -- status of T and its base differ.
1442
1443 if Is_Private_Type (Bas)
1444 and then not Is_Private_Type (T)
1445 and then Present (Full_View (Bas))
1446 then
1447 Bas := Full_View (Bas);
1448 end if;
1449
1450 Disc := First_Discriminant (T);
1451
1452 if No (Discriminant_Default_Value (Disc)) then
1453 return T;
1454 end if;
1455
1456 declare
1457 Act : constant Entity_Id := Make_Temporary (Loc, 'S');
1458 Constraints : constant List_Id := New_List;
1459 Decl : Node_Id;
1460
1461 begin
1462 while Present (Disc) loop
1463 Append_To (Constraints,
1464 New_Copy_Tree (Discriminant_Default_Value (Disc)));
1465 Next_Discriminant (Disc);
1466 end loop;
1467
1468 Decl :=
1469 Make_Subtype_Declaration (Loc,
1470 Defining_Identifier => Act,
1471 Subtype_Indication =>
1472 Make_Subtype_Indication (Loc,
1473 Subtype_Mark => New_Occurrence_Of (Bas, Loc),
1474 Constraint =>
1475 Make_Index_Or_Discriminant_Constraint (Loc,
1476 Constraints => Constraints)));
1477
1478 Insert_Action (N, Decl);
1479
1480 -- If the context is a component declaration the subtype declaration
1481 -- will be analyzed when the enclosing type is frozen, otherwise do
1482 -- it now.
1483
1484 if Ekind (Current_Scope) /= E_Record_Type then
1485 Analyze (Decl);
1486 end if;
1487
1488 return Act;
1489 end;
1490 end Build_Default_Subtype;
1491
1492 --------------------------------------------
1493 -- Build_Discriminal_Subtype_Of_Component --
1494 --------------------------------------------
1495
1496 function Build_Discriminal_Subtype_Of_Component
1497 (T : Entity_Id) return Node_Id
1498 is
1499 Loc : constant Source_Ptr := Sloc (T);
1500 D : Elmt_Id;
1501 Id : Node_Id;
1502
1503 function Build_Discriminal_Array_Constraint return List_Id;
1504 -- If one or more of the bounds of the component depends on
1505 -- discriminants, build actual constraint using the discriminants
1506 -- of the prefix.
1507
1508 function Build_Discriminal_Record_Constraint return List_Id;
1509 -- Similar to previous one, for discriminated components constrained by
1510 -- the discriminant of the enclosing object.
1511
1512 ----------------------------------------
1513 -- Build_Discriminal_Array_Constraint --
1514 ----------------------------------------
1515
1516 function Build_Discriminal_Array_Constraint return List_Id is
1517 Constraints : constant List_Id := New_List;
1518 Indx : Node_Id;
1519 Hi : Node_Id;
1520 Lo : Node_Id;
1521 Old_Hi : Node_Id;
1522 Old_Lo : Node_Id;
1523
1524 begin
1525 Indx := First_Index (T);
1526 while Present (Indx) loop
1527 Old_Lo := Type_Low_Bound (Etype (Indx));
1528 Old_Hi := Type_High_Bound (Etype (Indx));
1529
1530 if Denotes_Discriminant (Old_Lo) then
1531 Lo := New_Occurrence_Of (Discriminal (Entity (Old_Lo)), Loc);
1532
1533 else
1534 Lo := New_Copy_Tree (Old_Lo);
1535 end if;
1536
1537 if Denotes_Discriminant (Old_Hi) then
1538 Hi := New_Occurrence_Of (Discriminal (Entity (Old_Hi)), Loc);
1539
1540 else
1541 Hi := New_Copy_Tree (Old_Hi);
1542 end if;
1543
1544 Append (Make_Range (Loc, Lo, Hi), Constraints);
1545 Next_Index (Indx);
1546 end loop;
1547
1548 return Constraints;
1549 end Build_Discriminal_Array_Constraint;
1550
1551 -----------------------------------------
1552 -- Build_Discriminal_Record_Constraint --
1553 -----------------------------------------
1554
1555 function Build_Discriminal_Record_Constraint return List_Id is
1556 Constraints : constant List_Id := New_List;
1557 D : Elmt_Id;
1558 D_Val : Node_Id;
1559
1560 begin
1561 D := First_Elmt (Discriminant_Constraint (T));
1562 while Present (D) loop
1563 if Denotes_Discriminant (Node (D)) then
1564 D_Val :=
1565 New_Occurrence_Of (Discriminal (Entity (Node (D))), Loc);
1566 else
1567 D_Val := New_Copy_Tree (Node (D));
1568 end if;
1569
1570 Append (D_Val, Constraints);
1571 Next_Elmt (D);
1572 end loop;
1573
1574 return Constraints;
1575 end Build_Discriminal_Record_Constraint;
1576
1577 -- Start of processing for Build_Discriminal_Subtype_Of_Component
1578
1579 begin
1580 if Ekind (T) = E_Array_Subtype then
1581 Id := First_Index (T);
1582 while Present (Id) loop
1583 if Denotes_Discriminant (Type_Low_Bound (Etype (Id)))
1584 or else
1585 Denotes_Discriminant (Type_High_Bound (Etype (Id)))
1586 then
1587 return Build_Component_Subtype
1588 (Build_Discriminal_Array_Constraint, Loc, T);
1589 end if;
1590
1591 Next_Index (Id);
1592 end loop;
1593
1594 elsif Ekind (T) = E_Record_Subtype
1595 and then Has_Discriminants (T)
1596 and then not Has_Unknown_Discriminants (T)
1597 then
1598 D := First_Elmt (Discriminant_Constraint (T));
1599 while Present (D) loop
1600 if Denotes_Discriminant (Node (D)) then
1601 return Build_Component_Subtype
1602 (Build_Discriminal_Record_Constraint, Loc, T);
1603 end if;
1604
1605 Next_Elmt (D);
1606 end loop;
1607 end if;
1608
1609 -- If none of the above, the actual and nominal subtypes are the same
1610
1611 return Empty;
1612 end Build_Discriminal_Subtype_Of_Component;
1613
1614 ------------------------------
1615 -- Build_Elaboration_Entity --
1616 ------------------------------
1617
1618 procedure Build_Elaboration_Entity (N : Node_Id; Spec_Id : Entity_Id) is
1619 Loc : constant Source_Ptr := Sloc (N);
1620 Decl : Node_Id;
1621 Elab_Ent : Entity_Id;
1622
1623 procedure Set_Package_Name (Ent : Entity_Id);
1624 -- Given an entity, sets the fully qualified name of the entity in
1625 -- Name_Buffer, with components separated by double underscores. This
1626 -- is a recursive routine that climbs the scope chain to Standard.
1627
1628 ----------------------
1629 -- Set_Package_Name --
1630 ----------------------
1631
1632 procedure Set_Package_Name (Ent : Entity_Id) is
1633 begin
1634 if Scope (Ent) /= Standard_Standard then
1635 Set_Package_Name (Scope (Ent));
1636
1637 declare
1638 Nam : constant String := Get_Name_String (Chars (Ent));
1639 begin
1640 Name_Buffer (Name_Len + 1) := '_';
1641 Name_Buffer (Name_Len + 2) := '_';
1642 Name_Buffer (Name_Len + 3 .. Name_Len + Nam'Length + 2) := Nam;
1643 Name_Len := Name_Len + Nam'Length + 2;
1644 end;
1645
1646 else
1647 Get_Name_String (Chars (Ent));
1648 end if;
1649 end Set_Package_Name;
1650
1651 -- Start of processing for Build_Elaboration_Entity
1652
1653 begin
1654 -- Ignore call if already constructed
1655
1656 if Present (Elaboration_Entity (Spec_Id)) then
1657 return;
1658
1659 -- Ignore in ASIS mode, elaboration entity is not in source and plays
1660 -- no role in analysis.
1661
1662 elsif ASIS_Mode then
1663 return;
1664
1665 -- See if we need elaboration entity.
1666
1667 -- We always need an elaboration entity when preserving control flow, as
1668 -- we want to remain explicit about the unit's elaboration order.
1669
1670 elsif Opt.Suppress_Control_Flow_Optimizations then
1671 null;
1672
1673 -- We always need an elaboration entity for the dynamic elaboration
1674 -- model, since it is needed to properly generate the PE exception for
1675 -- access before elaboration.
1676
1677 elsif Dynamic_Elaboration_Checks then
1678 null;
1679
1680 -- For the static model, we don't need the elaboration counter if this
1681 -- unit is sure to have no elaboration code, since that means there
1682 -- is no elaboration unit to be called. Note that we can't just decide
1683 -- after the fact by looking to see whether there was elaboration code,
1684 -- because that's too late to make this decision.
1685
1686 elsif Restriction_Active (No_Elaboration_Code) then
1687 return;
1688
1689 -- Similarly, for the static model, we can skip the elaboration counter
1690 -- if we have the No_Multiple_Elaboration restriction, since for the
1691 -- static model, that's the only purpose of the counter (to avoid
1692 -- multiple elaboration).
1693
1694 elsif Restriction_Active (No_Multiple_Elaboration) then
1695 return;
1696 end if;
1697
1698 -- Here we need the elaboration entity
1699
1700 -- Construct name of elaboration entity as xxx_E, where xxx is the unit
1701 -- name with dots replaced by double underscore. We have to manually
1702 -- construct this name, since it will be elaborated in the outer scope,
1703 -- and thus will not have the unit name automatically prepended.
1704
1705 Set_Package_Name (Spec_Id);
1706 Add_Str_To_Name_Buffer ("_E");
1707
1708 -- Create elaboration counter
1709
1710 Elab_Ent := Make_Defining_Identifier (Loc, Chars => Name_Find);
1711 Set_Elaboration_Entity (Spec_Id, Elab_Ent);
1712
1713 Decl :=
1714 Make_Object_Declaration (Loc,
1715 Defining_Identifier => Elab_Ent,
1716 Object_Definition =>
1717 New_Occurrence_Of (Standard_Short_Integer, Loc),
1718 Expression => Make_Integer_Literal (Loc, Uint_0));
1719
1720 Push_Scope (Standard_Standard);
1721 Add_Global_Declaration (Decl);
1722 Pop_Scope;
1723
1724 -- Reset True_Constant indication, since we will indeed assign a value
1725 -- to the variable in the binder main. We also kill the Current_Value
1726 -- and Last_Assignment fields for the same reason.
1727
1728 Set_Is_True_Constant (Elab_Ent, False);
1729 Set_Current_Value (Elab_Ent, Empty);
1730 Set_Last_Assignment (Elab_Ent, Empty);
1731
1732 -- We do not want any further qualification of the name (if we did not
1733 -- do this, we would pick up the name of the generic package in the case
1734 -- of a library level generic instantiation).
1735
1736 Set_Has_Qualified_Name (Elab_Ent);
1737 Set_Has_Fully_Qualified_Name (Elab_Ent);
1738 end Build_Elaboration_Entity;
1739
1740 --------------------------------
1741 -- Build_Explicit_Dereference --
1742 --------------------------------
1743
1744 procedure Build_Explicit_Dereference
1745 (Expr : Node_Id;
1746 Disc : Entity_Id)
1747 is
1748 Loc : constant Source_Ptr := Sloc (Expr);
1749 I : Interp_Index;
1750 It : Interp;
1751
1752 begin
1753 -- An entity of a type with a reference aspect is overloaded with
1754 -- both interpretations: with and without the dereference. Now that
1755 -- the dereference is made explicit, set the type of the node properly,
1756 -- to prevent anomalies in the backend. Same if the expression is an
1757 -- overloaded function call whose return type has a reference aspect.
1758
1759 if Is_Entity_Name (Expr) then
1760 Set_Etype (Expr, Etype (Entity (Expr)));
1761
1762 -- The designated entity will not be examined again when resolving
1763 -- the dereference, so generate a reference to it now.
1764
1765 Generate_Reference (Entity (Expr), Expr);
1766
1767 elsif Nkind (Expr) = N_Function_Call then
1768
1769 -- If the name of the indexing function is overloaded, locate the one
1770 -- whose return type has an implicit dereference on the desired
1771 -- discriminant, and set entity and type of function call.
1772
1773 if Is_Overloaded (Name (Expr)) then
1774 Get_First_Interp (Name (Expr), I, It);
1775
1776 while Present (It.Nam) loop
1777 if Ekind ((It.Typ)) = E_Record_Type
1778 and then First_Entity ((It.Typ)) = Disc
1779 then
1780 Set_Entity (Name (Expr), It.Nam);
1781 Set_Etype (Name (Expr), Etype (It.Nam));
1782 exit;
1783 end if;
1784
1785 Get_Next_Interp (I, It);
1786 end loop;
1787 end if;
1788
1789 -- Set type of call from resolved function name.
1790
1791 Set_Etype (Expr, Etype (Name (Expr)));
1792 end if;
1793
1794 Set_Is_Overloaded (Expr, False);
1795
1796 -- The expression will often be a generalized indexing that yields a
1797 -- container element that is then dereferenced, in which case the
1798 -- generalized indexing call is also non-overloaded.
1799
1800 if Nkind (Expr) = N_Indexed_Component
1801 and then Present (Generalized_Indexing (Expr))
1802 then
1803 Set_Is_Overloaded (Generalized_Indexing (Expr), False);
1804 end if;
1805
1806 Rewrite (Expr,
1807 Make_Explicit_Dereference (Loc,
1808 Prefix =>
1809 Make_Selected_Component (Loc,
1810 Prefix => Relocate_Node (Expr),
1811 Selector_Name => New_Occurrence_Of (Disc, Loc))));
1812 Set_Etype (Prefix (Expr), Etype (Disc));
1813 Set_Etype (Expr, Designated_Type (Etype (Disc)));
1814 end Build_Explicit_Dereference;
1815
1816 -----------------------------------
1817 -- Cannot_Raise_Constraint_Error --
1818 -----------------------------------
1819
1820 function Cannot_Raise_Constraint_Error (Expr : Node_Id) return Boolean is
1821 begin
1822 if Compile_Time_Known_Value (Expr) then
1823 return True;
1824
1825 elsif Do_Range_Check (Expr) then
1826 return False;
1827
1828 elsif Raises_Constraint_Error (Expr) then
1829 return False;
1830
1831 else
1832 case Nkind (Expr) is
1833 when N_Identifier =>
1834 return True;
1835
1836 when N_Expanded_Name =>
1837 return True;
1838
1839 when N_Selected_Component =>
1840 return not Do_Discriminant_Check (Expr);
1841
1842 when N_Attribute_Reference =>
1843 if Do_Overflow_Check (Expr) then
1844 return False;
1845
1846 elsif No (Expressions (Expr)) then
1847 return True;
1848
1849 else
1850 declare
1851 N : Node_Id;
1852
1853 begin
1854 N := First (Expressions (Expr));
1855 while Present (N) loop
1856 if Cannot_Raise_Constraint_Error (N) then
1857 Next (N);
1858 else
1859 return False;
1860 end if;
1861 end loop;
1862
1863 return True;
1864 end;
1865 end if;
1866
1867 when N_Type_Conversion =>
1868 if Do_Overflow_Check (Expr)
1869 or else Do_Length_Check (Expr)
1870 or else Do_Tag_Check (Expr)
1871 then
1872 return False;
1873 else
1874 return Cannot_Raise_Constraint_Error (Expression (Expr));
1875 end if;
1876
1877 when N_Unchecked_Type_Conversion =>
1878 return Cannot_Raise_Constraint_Error (Expression (Expr));
1879
1880 when N_Unary_Op =>
1881 if Do_Overflow_Check (Expr) then
1882 return False;
1883 else
1884 return Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
1885 end if;
1886
1887 when N_Op_Divide |
1888 N_Op_Mod |
1889 N_Op_Rem
1890 =>
1891 if Do_Division_Check (Expr)
1892 or else
1893 Do_Overflow_Check (Expr)
1894 then
1895 return False;
1896 else
1897 return
1898 Cannot_Raise_Constraint_Error (Left_Opnd (Expr))
1899 and then
1900 Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
1901 end if;
1902
1903 when N_Op_Add |
1904 N_Op_And |
1905 N_Op_Concat |
1906 N_Op_Eq |
1907 N_Op_Expon |
1908 N_Op_Ge |
1909 N_Op_Gt |
1910 N_Op_Le |
1911 N_Op_Lt |
1912 N_Op_Multiply |
1913 N_Op_Ne |
1914 N_Op_Or |
1915 N_Op_Rotate_Left |
1916 N_Op_Rotate_Right |
1917 N_Op_Shift_Left |
1918 N_Op_Shift_Right |
1919 N_Op_Shift_Right_Arithmetic |
1920 N_Op_Subtract |
1921 N_Op_Xor
1922 =>
1923 if Do_Overflow_Check (Expr) then
1924 return False;
1925 else
1926 return
1927 Cannot_Raise_Constraint_Error (Left_Opnd (Expr))
1928 and then
1929 Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
1930 end if;
1931
1932 when others =>
1933 return False;
1934 end case;
1935 end if;
1936 end Cannot_Raise_Constraint_Error;
1937
1938 -----------------------------
1939 -- Check_Part_Of_Reference --
1940 -----------------------------
1941
1942 procedure Check_Part_Of_Reference (Var_Id : Entity_Id; Ref : Node_Id) is
1943 Conc_Typ : constant Entity_Id := Encapsulating_State (Var_Id);
1944 Decl : Node_Id;
1945 OK_Use : Boolean := False;
1946 Par : Node_Id;
1947 Prag_Nam : Name_Id;
1948 Spec_Id : Entity_Id;
1949
1950 begin
1951 -- Traverse the parent chain looking for a suitable context for the
1952 -- reference to the concurrent constituent.
1953
1954 Par := Parent (Ref);
1955 while Present (Par) loop
1956 if Nkind (Par) = N_Pragma then
1957 Prag_Nam := Pragma_Name (Par);
1958
1959 -- A concurrent constituent is allowed to appear in pragmas
1960 -- Initial_Condition and Initializes as this is part of the
1961 -- elaboration checks for the constituent (SPARK RM 9.3).
1962
1963 if Nam_In (Prag_Nam, Name_Initial_Condition, Name_Initializes) then
1964 OK_Use := True;
1965 exit;
1966
1967 -- When the reference appears within pragma Depends or Global,
1968 -- check whether the pragma applies to a single task type. Note
1969 -- that the pragma is not encapsulated by the type definition,
1970 -- but this is still a valid context.
1971
1972 elsif Nam_In (Prag_Nam, Name_Depends, Name_Global) then
1973 Decl := Find_Related_Declaration_Or_Body (Par);
1974
1975 if Nkind (Decl) = N_Object_Declaration
1976 and then Defining_Entity (Decl) = Conc_Typ
1977 then
1978 OK_Use := True;
1979 exit;
1980 end if;
1981 end if;
1982
1983 -- The reference appears somewhere in the definition of the single
1984 -- protected/task type (SPARK RM 9.3).
1985
1986 elsif Nkind_In (Par, N_Single_Protected_Declaration,
1987 N_Single_Task_Declaration)
1988 and then Defining_Entity (Par) = Conc_Typ
1989 then
1990 OK_Use := True;
1991 exit;
1992
1993 -- The reference appears within the expanded declaration or the body
1994 -- of the single protected/task type (SPARK RM 9.3).
1995
1996 elsif Nkind_In (Par, N_Protected_Body,
1997 N_Protected_Type_Declaration,
1998 N_Task_Body,
1999 N_Task_Type_Declaration)
2000 then
2001 Spec_Id := Unique_Defining_Entity (Par);
2002
2003 if Present (Anonymous_Object (Spec_Id))
2004 and then Anonymous_Object (Spec_Id) = Conc_Typ
2005 then
2006 OK_Use := True;
2007 exit;
2008 end if;
2009
2010 -- The reference has been relocated within an internally generated
2011 -- package or subprogram. Assume that the reference is legal as the
2012 -- real check was already performed in the original context of the
2013 -- reference.
2014
2015 elsif Nkind_In (Par, N_Package_Body,
2016 N_Package_Declaration,
2017 N_Subprogram_Body,
2018 N_Subprogram_Declaration)
2019 and then not Comes_From_Source (Par)
2020 then
2021 OK_Use := True;
2022 exit;
2023
2024 -- The reference has been relocated to an inlined body for GNATprove.
2025 -- Assume that the reference is legal as the real check was already
2026 -- performed in the original context of the reference.
2027
2028 elsif GNATprove_Mode
2029 and then Nkind (Par) = N_Subprogram_Body
2030 and then Chars (Defining_Entity (Par)) = Name_uParent
2031 then
2032 OK_Use := True;
2033 exit;
2034 end if;
2035
2036 Par := Parent (Par);
2037 end loop;
2038
2039 -- The reference is illegal as it appears outside the definition or
2040 -- body of the single protected/task type.
2041
2042 if not OK_Use then
2043 Error_Msg_NE
2044 ("reference to variable & cannot appear in this context",
2045 Ref, Var_Id);
2046 Error_Msg_Name_1 := Chars (Var_Id);
2047
2048 if Ekind (Conc_Typ) = E_Protected_Type then
2049 Error_Msg_NE
2050 ("\% is constituent of single protected type &", Ref, Conc_Typ);
2051 else
2052 Error_Msg_NE
2053 ("\% is constituent of single task type &", Ref, Conc_Typ);
2054 end if;
2055 end if;
2056 end Check_Part_Of_Reference;
2057
2058 -----------------------------------------
2059 -- Check_Dynamically_Tagged_Expression --
2060 -----------------------------------------
2061
2062 procedure Check_Dynamically_Tagged_Expression
2063 (Expr : Node_Id;
2064 Typ : Entity_Id;
2065 Related_Nod : Node_Id)
2066 is
2067 begin
2068 pragma Assert (Is_Tagged_Type (Typ));
2069
2070 -- In order to avoid spurious errors when analyzing the expanded code,
2071 -- this check is done only for nodes that come from source and for
2072 -- actuals of generic instantiations.
2073
2074 if (Comes_From_Source (Related_Nod)
2075 or else In_Generic_Actual (Expr))
2076 and then (Is_Class_Wide_Type (Etype (Expr))
2077 or else Is_Dynamically_Tagged (Expr))
2078 and then Is_Tagged_Type (Typ)
2079 and then not Is_Class_Wide_Type (Typ)
2080 then
2081 Error_Msg_N ("dynamically tagged expression not allowed!", Expr);
2082 end if;
2083 end Check_Dynamically_Tagged_Expression;
2084
2085 --------------------------
2086 -- Check_Fully_Declared --
2087 --------------------------
2088
2089 procedure Check_Fully_Declared (T : Entity_Id; N : Node_Id) is
2090 begin
2091 if Ekind (T) = E_Incomplete_Type then
2092
2093 -- Ada 2005 (AI-50217): If the type is available through a limited
2094 -- with_clause, verify that its full view has been analyzed.
2095
2096 if From_Limited_With (T)
2097 and then Present (Non_Limited_View (T))
2098 and then Ekind (Non_Limited_View (T)) /= E_Incomplete_Type
2099 then
2100 -- The non-limited view is fully declared
2101
2102 null;
2103
2104 else
2105 Error_Msg_NE
2106 ("premature usage of incomplete}", N, First_Subtype (T));
2107 end if;
2108
2109 -- Need comments for these tests ???
2110
2111 elsif Has_Private_Component (T)
2112 and then not Is_Generic_Type (Root_Type (T))
2113 and then not In_Spec_Expression
2114 then
2115 -- Special case: if T is the anonymous type created for a single
2116 -- task or protected object, use the name of the source object.
2117
2118 if Is_Concurrent_Type (T)
2119 and then not Comes_From_Source (T)
2120 and then Nkind (N) = N_Object_Declaration
2121 then
2122 Error_Msg_NE
2123 ("type of& has incomplete component",
2124 N, Defining_Identifier (N));
2125 else
2126 Error_Msg_NE
2127 ("premature usage of incomplete}",
2128 N, First_Subtype (T));
2129 end if;
2130 end if;
2131 end Check_Fully_Declared;
2132
2133 -------------------------------------------
2134 -- Check_Function_With_Address_Parameter --
2135 -------------------------------------------
2136
2137 procedure Check_Function_With_Address_Parameter (Subp_Id : Entity_Id) is
2138 F : Entity_Id;
2139 T : Entity_Id;
2140
2141 begin
2142 F := First_Formal (Subp_Id);
2143 while Present (F) loop
2144 T := Etype (F);
2145
2146 if Is_Private_Type (T) and then Present (Full_View (T)) then
2147 T := Full_View (T);
2148 end if;
2149
2150 if Is_Descendant_Of_Address (T) or else Is_Limited_Type (T) then
2151 Set_Is_Pure (Subp_Id, False);
2152 exit;
2153 end if;
2154
2155 Next_Formal (F);
2156 end loop;
2157 end Check_Function_With_Address_Parameter;
2158
2159 -------------------------------------
2160 -- Check_Function_Writable_Actuals --
2161 -------------------------------------
2162
2163 procedure Check_Function_Writable_Actuals (N : Node_Id) is
2164 Writable_Actuals_List : Elist_Id := No_Elist;
2165 Identifiers_List : Elist_Id := No_Elist;
2166 Aggr_Error_Node : Node_Id := Empty;
2167 Error_Node : Node_Id := Empty;
2168
2169 procedure Collect_Identifiers (N : Node_Id);
2170 -- In a single traversal of subtree N collect in Writable_Actuals_List
2171 -- all the actuals of functions with writable actuals, and in the list
2172 -- Identifiers_List collect all the identifiers that are not actuals of
2173 -- functions with writable actuals. If a writable actual is referenced
2174 -- twice as writable actual then Error_Node is set to reference its
2175 -- second occurrence, the error is reported, and the tree traversal
2176 -- is abandoned.
2177
2178 function Get_Function_Id (Call : Node_Id) return Entity_Id;
2179 -- Return the entity associated with the function call
2180
2181 procedure Preanalyze_Without_Errors (N : Node_Id);
2182 -- Preanalyze N without reporting errors. Very dubious, you can't just
2183 -- go analyzing things more than once???
2184
2185 -------------------------
2186 -- Collect_Identifiers --
2187 -------------------------
2188
2189 procedure Collect_Identifiers (N : Node_Id) is
2190
2191 function Check_Node (N : Node_Id) return Traverse_Result;
2192 -- Process a single node during the tree traversal to collect the
2193 -- writable actuals of functions and all the identifiers which are
2194 -- not writable actuals of functions.
2195
2196 function Contains (List : Elist_Id; N : Node_Id) return Boolean;
2197 -- Returns True if List has a node whose Entity is Entity (N)
2198
2199 -------------------------
2200 -- Check_Function_Call --
2201 -------------------------
2202
2203 function Check_Node (N : Node_Id) return Traverse_Result is
2204 Is_Writable_Actual : Boolean := False;
2205 Id : Entity_Id;
2206
2207 begin
2208 if Nkind (N) = N_Identifier then
2209
2210 -- No analysis possible if the entity is not decorated
2211
2212 if No (Entity (N)) then
2213 return Skip;
2214
2215 -- Don't collect identifiers of packages, called functions, etc
2216
2217 elsif Ekind_In (Entity (N), E_Package,
2218 E_Function,
2219 E_Procedure,
2220 E_Entry)
2221 then
2222 return Skip;
2223
2224 -- For rewritten nodes, continue the traversal in the original
2225 -- subtree. Needed to handle aggregates in original expressions
2226 -- extracted from the tree by Remove_Side_Effects.
2227
2228 elsif Is_Rewrite_Substitution (N) then
2229 Collect_Identifiers (Original_Node (N));
2230 return Skip;
2231
2232 -- For now we skip aggregate discriminants, since they require
2233 -- performing the analysis in two phases to identify conflicts:
2234 -- first one analyzing discriminants and second one analyzing
2235 -- the rest of components (since at run time, discriminants are
2236 -- evaluated prior to components): too much computation cost
2237 -- to identify a corner case???
2238
2239 elsif Nkind (Parent (N)) = N_Component_Association
2240 and then Nkind_In (Parent (Parent (N)),
2241 N_Aggregate,
2242 N_Extension_Aggregate)
2243 then
2244 declare
2245 Choice : constant Node_Id := First (Choices (Parent (N)));
2246
2247 begin
2248 if Ekind (Entity (N)) = E_Discriminant then
2249 return Skip;
2250
2251 elsif Expression (Parent (N)) = N
2252 and then Nkind (Choice) = N_Identifier
2253 and then Ekind (Entity (Choice)) = E_Discriminant
2254 then
2255 return Skip;
2256 end if;
2257 end;
2258
2259 -- Analyze if N is a writable actual of a function
2260
2261 elsif Nkind (Parent (N)) = N_Function_Call then
2262 declare
2263 Call : constant Node_Id := Parent (N);
2264 Actual : Node_Id;
2265 Formal : Node_Id;
2266
2267 begin
2268 Id := Get_Function_Id (Call);
2269
2270 -- In case of previous error, no check is possible
2271
2272 if No (Id) then
2273 return Abandon;
2274 end if;
2275
2276 if Ekind_In (Id, E_Function, E_Generic_Function)
2277 and then Has_Out_Or_In_Out_Parameter (Id)
2278 then
2279 Formal := First_Formal (Id);
2280 Actual := First_Actual (Call);
2281 while Present (Actual) and then Present (Formal) loop
2282 if Actual = N then
2283 if Ekind_In (Formal, E_Out_Parameter,
2284 E_In_Out_Parameter)
2285 then
2286 Is_Writable_Actual := True;
2287 end if;
2288
2289 exit;
2290 end if;
2291
2292 Next_Formal (Formal);
2293 Next_Actual (Actual);
2294 end loop;
2295 end if;
2296 end;
2297 end if;
2298
2299 if Is_Writable_Actual then
2300
2301 -- Skip checking the error in non-elementary types since
2302 -- RM 6.4.1(6.15/3) is restricted to elementary types, but
2303 -- store this actual in Writable_Actuals_List since it is
2304 -- needed to perform checks on other constructs that have
2305 -- arbitrary order of evaluation (for example, aggregates).
2306
2307 if not Is_Elementary_Type (Etype (N)) then
2308 if not Contains (Writable_Actuals_List, N) then
2309 Append_New_Elmt (N, To => Writable_Actuals_List);
2310 end if;
2311
2312 -- Second occurrence of an elementary type writable actual
2313
2314 elsif Contains (Writable_Actuals_List, N) then
2315
2316 -- Report the error on the second occurrence of the
2317 -- identifier. We cannot assume that N is the second
2318 -- occurrence (according to their location in the
2319 -- sources), since Traverse_Func walks through Field2
2320 -- last (see comment in the body of Traverse_Func).
2321
2322 declare
2323 Elmt : Elmt_Id;
2324
2325 begin
2326 Elmt := First_Elmt (Writable_Actuals_List);
2327 while Present (Elmt)
2328 and then Entity (Node (Elmt)) /= Entity (N)
2329 loop
2330 Next_Elmt (Elmt);
2331 end loop;
2332
2333 if Sloc (N) > Sloc (Node (Elmt)) then
2334 Error_Node := N;
2335 else
2336 Error_Node := Node (Elmt);
2337 end if;
2338
2339 Error_Msg_NE
2340 ("value may be affected by call to & "
2341 & "because order of evaluation is arbitrary",
2342 Error_Node, Id);
2343 return Abandon;
2344 end;
2345
2346 -- First occurrence of a elementary type writable actual
2347
2348 else
2349 Append_New_Elmt (N, To => Writable_Actuals_List);
2350 end if;
2351
2352 else
2353 if Identifiers_List = No_Elist then
2354 Identifiers_List := New_Elmt_List;
2355 end if;
2356
2357 Append_Unique_Elmt (N, Identifiers_List);
2358 end if;
2359 end if;
2360
2361 return OK;
2362 end Check_Node;
2363
2364 --------------
2365 -- Contains --
2366 --------------
2367
2368 function Contains
2369 (List : Elist_Id;
2370 N : Node_Id) return Boolean
2371 is
2372 pragma Assert (Nkind (N) in N_Has_Entity);
2373
2374 Elmt : Elmt_Id;
2375
2376 begin
2377 if List = No_Elist then
2378 return False;
2379 end if;
2380
2381 Elmt := First_Elmt (List);
2382 while Present (Elmt) loop
2383 if Entity (Node (Elmt)) = Entity (N) then
2384 return True;
2385 else
2386 Next_Elmt (Elmt);
2387 end if;
2388 end loop;
2389
2390 return False;
2391 end Contains;
2392
2393 ------------------
2394 -- Do_Traversal --
2395 ------------------
2396
2397 procedure Do_Traversal is new Traverse_Proc (Check_Node);
2398 -- The traversal procedure
2399
2400 -- Start of processing for Collect_Identifiers
2401
2402 begin
2403 if Present (Error_Node) then
2404 return;
2405 end if;
2406
2407 if Nkind (N) in N_Subexpr and then Is_OK_Static_Expression (N) then
2408 return;
2409 end if;
2410
2411 Do_Traversal (N);
2412 end Collect_Identifiers;
2413
2414 ---------------------
2415 -- Get_Function_Id --
2416 ---------------------
2417
2418 function Get_Function_Id (Call : Node_Id) return Entity_Id is
2419 Nam : constant Node_Id := Name (Call);
2420 Id : Entity_Id;
2421
2422 begin
2423 if Nkind (Nam) = N_Explicit_Dereference then
2424 Id := Etype (Nam);
2425 pragma Assert (Ekind (Id) = E_Subprogram_Type);
2426
2427 elsif Nkind (Nam) = N_Selected_Component then
2428 Id := Entity (Selector_Name (Nam));
2429
2430 elsif Nkind (Nam) = N_Indexed_Component then
2431 Id := Entity (Selector_Name (Prefix (Nam)));
2432
2433 else
2434 Id := Entity (Nam);
2435 end if;
2436
2437 return Id;
2438 end Get_Function_Id;
2439
2440 -------------------------------
2441 -- Preanalyze_Without_Errors --
2442 -------------------------------
2443
2444 procedure Preanalyze_Without_Errors (N : Node_Id) is
2445 Status : constant Boolean := Get_Ignore_Errors;
2446 begin
2447 Set_Ignore_Errors (True);
2448 Preanalyze (N);
2449 Set_Ignore_Errors (Status);
2450 end Preanalyze_Without_Errors;
2451
2452 -- Start of processing for Check_Function_Writable_Actuals
2453
2454 begin
2455 -- The check only applies to Ada 2012 code on which Check_Actuals has
2456 -- been set, and only to constructs that have multiple constituents
2457 -- whose order of evaluation is not specified by the language.
2458
2459 if Ada_Version < Ada_2012
2460 or else not Check_Actuals (N)
2461 or else (not (Nkind (N) in N_Op)
2462 and then not (Nkind (N) in N_Membership_Test)
2463 and then not Nkind_In (N, N_Range,
2464 N_Aggregate,
2465 N_Extension_Aggregate,
2466 N_Full_Type_Declaration,
2467 N_Function_Call,
2468 N_Procedure_Call_Statement,
2469 N_Entry_Call_Statement))
2470 or else (Nkind (N) = N_Full_Type_Declaration
2471 and then not Is_Record_Type (Defining_Identifier (N)))
2472
2473 -- In addition, this check only applies to source code, not to code
2474 -- generated by constraint checks.
2475
2476 or else not Comes_From_Source (N)
2477 then
2478 return;
2479 end if;
2480
2481 -- If a construct C has two or more direct constituents that are names
2482 -- or expressions whose evaluation may occur in an arbitrary order, at
2483 -- least one of which contains a function call with an in out or out
2484 -- parameter, then the construct is legal only if: for each name N that
2485 -- is passed as a parameter of mode in out or out to some inner function
2486 -- call C2 (not including the construct C itself), there is no other
2487 -- name anywhere within a direct constituent of the construct C other
2488 -- than the one containing C2, that is known to refer to the same
2489 -- object (RM 6.4.1(6.17/3)).
2490
2491 case Nkind (N) is
2492 when N_Range =>
2493 Collect_Identifiers (Low_Bound (N));
2494 Collect_Identifiers (High_Bound (N));
2495
2496 when N_Op | N_Membership_Test =>
2497 declare
2498 Expr : Node_Id;
2499
2500 begin
2501 Collect_Identifiers (Left_Opnd (N));
2502
2503 if Present (Right_Opnd (N)) then
2504 Collect_Identifiers (Right_Opnd (N));
2505 end if;
2506
2507 if Nkind_In (N, N_In, N_Not_In)
2508 and then Present (Alternatives (N))
2509 then
2510 Expr := First (Alternatives (N));
2511 while Present (Expr) loop
2512 Collect_Identifiers (Expr);
2513
2514 Next (Expr);
2515 end loop;
2516 end if;
2517 end;
2518
2519 when N_Full_Type_Declaration =>
2520 declare
2521 function Get_Record_Part (N : Node_Id) return Node_Id;
2522 -- Return the record part of this record type definition
2523
2524 function Get_Record_Part (N : Node_Id) return Node_Id is
2525 Type_Def : constant Node_Id := Type_Definition (N);
2526 begin
2527 if Nkind (Type_Def) = N_Derived_Type_Definition then
2528 return Record_Extension_Part (Type_Def);
2529 else
2530 return Type_Def;
2531 end if;
2532 end Get_Record_Part;
2533
2534 Comp : Node_Id;
2535 Def_Id : Entity_Id := Defining_Identifier (N);
2536 Rec : Node_Id := Get_Record_Part (N);
2537
2538 begin
2539 -- No need to perform any analysis if the record has no
2540 -- components
2541
2542 if No (Rec) or else No (Component_List (Rec)) then
2543 return;
2544 end if;
2545
2546 -- Collect the identifiers starting from the deepest
2547 -- derivation. Done to report the error in the deepest
2548 -- derivation.
2549
2550 loop
2551 if Present (Component_List (Rec)) then
2552 Comp := First (Component_Items (Component_List (Rec)));
2553 while Present (Comp) loop
2554 if Nkind (Comp) = N_Component_Declaration
2555 and then Present (Expression (Comp))
2556 then
2557 Collect_Identifiers (Expression (Comp));
2558 end if;
2559
2560 Next (Comp);
2561 end loop;
2562 end if;
2563
2564 exit when No (Underlying_Type (Etype (Def_Id)))
2565 or else Base_Type (Underlying_Type (Etype (Def_Id)))
2566 = Def_Id;
2567
2568 Def_Id := Base_Type (Underlying_Type (Etype (Def_Id)));
2569 Rec := Get_Record_Part (Parent (Def_Id));
2570 end loop;
2571 end;
2572
2573 when N_Subprogram_Call |
2574 N_Entry_Call_Statement =>
2575 declare
2576 Id : constant Entity_Id := Get_Function_Id (N);
2577 Formal : Node_Id;
2578 Actual : Node_Id;
2579
2580 begin
2581 Formal := First_Formal (Id);
2582 Actual := First_Actual (N);
2583 while Present (Actual) and then Present (Formal) loop
2584 if Ekind_In (Formal, E_Out_Parameter,
2585 E_In_Out_Parameter)
2586 then
2587 Collect_Identifiers (Actual);
2588 end if;
2589
2590 Next_Formal (Formal);
2591 Next_Actual (Actual);
2592 end loop;
2593 end;
2594
2595 when N_Aggregate |
2596 N_Extension_Aggregate =>
2597 declare
2598 Assoc : Node_Id;
2599 Choice : Node_Id;
2600 Comp_Expr : Node_Id;
2601
2602 begin
2603 -- Handle the N_Others_Choice of array aggregates with static
2604 -- bounds. There is no need to perform this analysis in
2605 -- aggregates without static bounds since we cannot evaluate
2606 -- if the N_Others_Choice covers several elements. There is
2607 -- no need to handle the N_Others choice of record aggregates
2608 -- since at this stage it has been already expanded by
2609 -- Resolve_Record_Aggregate.
2610
2611 if Is_Array_Type (Etype (N))
2612 and then Nkind (N) = N_Aggregate
2613 and then Present (Aggregate_Bounds (N))
2614 and then Compile_Time_Known_Bounds (Etype (N))
2615 and then Expr_Value (High_Bound (Aggregate_Bounds (N)))
2616 >
2617 Expr_Value (Low_Bound (Aggregate_Bounds (N)))
2618 then
2619 declare
2620 Count_Components : Uint := Uint_0;
2621 Num_Components : Uint;
2622 Others_Assoc : Node_Id;
2623 Others_Choice : Node_Id := Empty;
2624 Others_Box_Present : Boolean := False;
2625
2626 begin
2627 -- Count positional associations
2628
2629 if Present (Expressions (N)) then
2630 Comp_Expr := First (Expressions (N));
2631 while Present (Comp_Expr) loop
2632 Count_Components := Count_Components + 1;
2633 Next (Comp_Expr);
2634 end loop;
2635 end if;
2636
2637 -- Count the rest of elements and locate the N_Others
2638 -- choice (if any)
2639
2640 Assoc := First (Component_Associations (N));
2641 while Present (Assoc) loop
2642 Choice := First (Choices (Assoc));
2643 while Present (Choice) loop
2644 if Nkind (Choice) = N_Others_Choice then
2645 Others_Assoc := Assoc;
2646 Others_Choice := Choice;
2647 Others_Box_Present := Box_Present (Assoc);
2648
2649 -- Count several components
2650
2651 elsif Nkind_In (Choice, N_Range,
2652 N_Subtype_Indication)
2653 or else (Is_Entity_Name (Choice)
2654 and then Is_Type (Entity (Choice)))
2655 then
2656 declare
2657 L, H : Node_Id;
2658 begin
2659 Get_Index_Bounds (Choice, L, H);
2660 pragma Assert
2661 (Compile_Time_Known_Value (L)
2662 and then Compile_Time_Known_Value (H));
2663 Count_Components :=
2664 Count_Components
2665 + Expr_Value (H) - Expr_Value (L) + 1;
2666 end;
2667
2668 -- Count single component. No other case available
2669 -- since we are handling an aggregate with static
2670 -- bounds.
2671
2672 else
2673 pragma Assert (Is_OK_Static_Expression (Choice)
2674 or else Nkind (Choice) = N_Identifier
2675 or else Nkind (Choice) = N_Integer_Literal);
2676
2677 Count_Components := Count_Components + 1;
2678 end if;
2679
2680 Next (Choice);
2681 end loop;
2682
2683 Next (Assoc);
2684 end loop;
2685
2686 Num_Components :=
2687 Expr_Value (High_Bound (Aggregate_Bounds (N))) -
2688 Expr_Value (Low_Bound (Aggregate_Bounds (N))) + 1;
2689
2690 pragma Assert (Count_Components <= Num_Components);
2691
2692 -- Handle the N_Others choice if it covers several
2693 -- components
2694
2695 if Present (Others_Choice)
2696 and then (Num_Components - Count_Components) > 1
2697 then
2698 if not Others_Box_Present then
2699
2700 -- At this stage, if expansion is active, the
2701 -- expression of the others choice has not been
2702 -- analyzed. Hence we generate a duplicate and
2703 -- we analyze it silently to have available the
2704 -- minimum decoration required to collect the
2705 -- identifiers.
2706
2707 if not Expander_Active then
2708 Comp_Expr := Expression (Others_Assoc);
2709 else
2710 Comp_Expr :=
2711 New_Copy_Tree (Expression (Others_Assoc));
2712 Preanalyze_Without_Errors (Comp_Expr);
2713 end if;
2714
2715 Collect_Identifiers (Comp_Expr);
2716
2717 if Writable_Actuals_List /= No_Elist then
2718
2719 -- As suggested by Robert, at current stage we
2720 -- report occurrences of this case as warnings.
2721
2722 Error_Msg_N
2723 ("writable function parameter may affect "
2724 & "value in other component because order "
2725 & "of evaluation is unspecified??",
2726 Node (First_Elmt (Writable_Actuals_List)));
2727 end if;
2728 end if;
2729 end if;
2730 end;
2731
2732 -- For an array aggregate, a discrete_choice_list that has
2733 -- a nonstatic range is considered as two or more separate
2734 -- occurrences of the expression (RM 6.4.1(20/3)).
2735
2736 elsif Is_Array_Type (Etype (N))
2737 and then Nkind (N) = N_Aggregate
2738 and then Present (Aggregate_Bounds (N))
2739 and then not Compile_Time_Known_Bounds (Etype (N))
2740 then
2741 -- Collect identifiers found in the dynamic bounds
2742
2743 declare
2744 Count_Components : Natural := 0;
2745 Low, High : Node_Id;
2746
2747 begin
2748 Assoc := First (Component_Associations (N));
2749 while Present (Assoc) loop
2750 Choice := First (Choices (Assoc));
2751 while Present (Choice) loop
2752 if Nkind_In (Choice, N_Range,
2753 N_Subtype_Indication)
2754 or else (Is_Entity_Name (Choice)
2755 and then Is_Type (Entity (Choice)))
2756 then
2757 Get_Index_Bounds (Choice, Low, High);
2758
2759 if not Compile_Time_Known_Value (Low) then
2760 Collect_Identifiers (Low);
2761
2762 if No (Aggr_Error_Node) then
2763 Aggr_Error_Node := Low;
2764 end if;
2765 end if;
2766
2767 if not Compile_Time_Known_Value (High) then
2768 Collect_Identifiers (High);
2769
2770 if No (Aggr_Error_Node) then
2771 Aggr_Error_Node := High;
2772 end if;
2773 end if;
2774
2775 -- The RM rule is violated if there is more than
2776 -- a single choice in a component association.
2777
2778 else
2779 Count_Components := Count_Components + 1;
2780
2781 if No (Aggr_Error_Node)
2782 and then Count_Components > 1
2783 then
2784 Aggr_Error_Node := Choice;
2785 end if;
2786
2787 if not Compile_Time_Known_Value (Choice) then
2788 Collect_Identifiers (Choice);
2789 end if;
2790 end if;
2791
2792 Next (Choice);
2793 end loop;
2794
2795 Next (Assoc);
2796 end loop;
2797 end;
2798 end if;
2799
2800 -- Handle ancestor part of extension aggregates
2801
2802 if Nkind (N) = N_Extension_Aggregate then
2803 Collect_Identifiers (Ancestor_Part (N));
2804 end if;
2805
2806 -- Handle positional associations
2807
2808 if Present (Expressions (N)) then
2809 Comp_Expr := First (Expressions (N));
2810 while Present (Comp_Expr) loop
2811 if not Is_OK_Static_Expression (Comp_Expr) then
2812 Collect_Identifiers (Comp_Expr);
2813 end if;
2814
2815 Next (Comp_Expr);
2816 end loop;
2817 end if;
2818
2819 -- Handle discrete associations
2820
2821 if Present (Component_Associations (N)) then
2822 Assoc := First (Component_Associations (N));
2823 while Present (Assoc) loop
2824
2825 if not Box_Present (Assoc) then
2826 Choice := First (Choices (Assoc));
2827 while Present (Choice) loop
2828
2829 -- For now we skip discriminants since it requires
2830 -- performing the analysis in two phases: first one
2831 -- analyzing discriminants and second one analyzing
2832 -- the rest of components since discriminants are
2833 -- evaluated prior to components: too much extra
2834 -- work to detect a corner case???
2835
2836 if Nkind (Choice) in N_Has_Entity
2837 and then Present (Entity (Choice))
2838 and then Ekind (Entity (Choice)) = E_Discriminant
2839 then
2840 null;
2841
2842 elsif Box_Present (Assoc) then
2843 null;
2844
2845 else
2846 if not Analyzed (Expression (Assoc)) then
2847 Comp_Expr :=
2848 New_Copy_Tree (Expression (Assoc));
2849 Set_Parent (Comp_Expr, Parent (N));
2850 Preanalyze_Without_Errors (Comp_Expr);
2851 else
2852 Comp_Expr := Expression (Assoc);
2853 end if;
2854
2855 Collect_Identifiers (Comp_Expr);
2856 end if;
2857
2858 Next (Choice);
2859 end loop;
2860 end if;
2861
2862 Next (Assoc);
2863 end loop;
2864 end if;
2865 end;
2866
2867 when others =>
2868 return;
2869 end case;
2870
2871 -- No further action needed if we already reported an error
2872
2873 if Present (Error_Node) then
2874 return;
2875 end if;
2876
2877 -- Check violation of RM 6.20/3 in aggregates
2878
2879 if Present (Aggr_Error_Node)
2880 and then Writable_Actuals_List /= No_Elist
2881 then
2882 Error_Msg_N
2883 ("value may be affected by call in other component because they "
2884 & "are evaluated in unspecified order",
2885 Node (First_Elmt (Writable_Actuals_List)));
2886 return;
2887 end if;
2888
2889 -- Check if some writable argument of a function is referenced
2890
2891 if Writable_Actuals_List /= No_Elist
2892 and then Identifiers_List /= No_Elist
2893 then
2894 declare
2895 Elmt_1 : Elmt_Id;
2896 Elmt_2 : Elmt_Id;
2897
2898 begin
2899 Elmt_1 := First_Elmt (Writable_Actuals_List);
2900 while Present (Elmt_1) loop
2901 Elmt_2 := First_Elmt (Identifiers_List);
2902 while Present (Elmt_2) loop
2903 if Entity (Node (Elmt_1)) = Entity (Node (Elmt_2)) then
2904 case Nkind (Parent (Node (Elmt_2))) is
2905 when N_Aggregate |
2906 N_Component_Association |
2907 N_Component_Declaration =>
2908 Error_Msg_N
2909 ("value may be affected by call in other "
2910 & "component because they are evaluated "
2911 & "in unspecified order",
2912 Node (Elmt_2));
2913
2914 when N_In | N_Not_In =>
2915 Error_Msg_N
2916 ("value may be affected by call in other "
2917 & "alternative because they are evaluated "
2918 & "in unspecified order",
2919 Node (Elmt_2));
2920
2921 when others =>
2922 Error_Msg_N
2923 ("value of actual may be affected by call in "
2924 & "other actual because they are evaluated "
2925 & "in unspecified order",
2926 Node (Elmt_2));
2927 end case;
2928 end if;
2929
2930 Next_Elmt (Elmt_2);
2931 end loop;
2932
2933 Next_Elmt (Elmt_1);
2934 end loop;
2935 end;
2936 end if;
2937 end Check_Function_Writable_Actuals;
2938
2939 --------------------------------
2940 -- Check_Implicit_Dereference --
2941 --------------------------------
2942
2943 procedure Check_Implicit_Dereference (N : Node_Id; Typ : Entity_Id) is
2944 Disc : Entity_Id;
2945 Desig : Entity_Id;
2946 Nam : Node_Id;
2947
2948 begin
2949 if Nkind (N) = N_Indexed_Component
2950 and then Present (Generalized_Indexing (N))
2951 then
2952 Nam := Generalized_Indexing (N);
2953 else
2954 Nam := N;
2955 end if;
2956
2957 if Ada_Version < Ada_2012
2958 or else not Has_Implicit_Dereference (Base_Type (Typ))
2959 then
2960 return;
2961
2962 elsif not Comes_From_Source (N)
2963 and then Nkind (N) /= N_Indexed_Component
2964 then
2965 return;
2966
2967 elsif Is_Entity_Name (Nam) and then Is_Type (Entity (Nam)) then
2968 null;
2969
2970 else
2971 Disc := First_Discriminant (Typ);
2972 while Present (Disc) loop
2973 if Has_Implicit_Dereference (Disc) then
2974 Desig := Designated_Type (Etype (Disc));
2975 Add_One_Interp (Nam, Disc, Desig);
2976
2977 -- If the node is a generalized indexing, add interpretation
2978 -- to that node as well, for subsequent resolution.
2979
2980 if Nkind (N) = N_Indexed_Component then
2981 Add_One_Interp (N, Disc, Desig);
2982 end if;
2983
2984 -- If the operation comes from a generic unit and the context
2985 -- is a selected component, the selector name may be global
2986 -- and set in the instance already. Remove the entity to
2987 -- force resolution of the selected component, and the
2988 -- generation of an explicit dereference if needed.
2989
2990 if In_Instance
2991 and then Nkind (Parent (Nam)) = N_Selected_Component
2992 then
2993 Set_Entity (Selector_Name (Parent (Nam)), Empty);
2994 end if;
2995
2996 exit;
2997 end if;
2998
2999 Next_Discriminant (Disc);
3000 end loop;
3001 end if;
3002 end Check_Implicit_Dereference;
3003
3004 ----------------------------------
3005 -- Check_Internal_Protected_Use --
3006 ----------------------------------
3007
3008 procedure Check_Internal_Protected_Use (N : Node_Id; Nam : Entity_Id) is
3009 S : Entity_Id;
3010 Prot : Entity_Id;
3011
3012 begin
3013 S := Current_Scope;
3014 while Present (S) loop
3015 if S = Standard_Standard then
3016 return;
3017
3018 elsif Ekind (S) = E_Function
3019 and then Ekind (Scope (S)) = E_Protected_Type
3020 then
3021 Prot := Scope (S);
3022 exit;
3023 end if;
3024
3025 S := Scope (S);
3026 end loop;
3027
3028 if Scope (Nam) = Prot and then Ekind (Nam) /= E_Function then
3029
3030 -- An indirect function call (e.g. a callback within a protected
3031 -- function body) is not statically illegal. If the access type is
3032 -- anonymous and is the type of an access parameter, the scope of Nam
3033 -- will be the protected type, but it is not a protected operation.
3034
3035 if Ekind (Nam) = E_Subprogram_Type
3036 and then
3037 Nkind (Associated_Node_For_Itype (Nam)) = N_Function_Specification
3038 then
3039 null;
3040
3041 elsif Nkind (N) = N_Subprogram_Renaming_Declaration then
3042 Error_Msg_N
3043 ("within protected function cannot use protected "
3044 & "procedure in renaming or as generic actual", N);
3045
3046 elsif Nkind (N) = N_Attribute_Reference then
3047 Error_Msg_N
3048 ("within protected function cannot take access of "
3049 & " protected procedure", N);
3050
3051 else
3052 Error_Msg_N
3053 ("within protected function, protected object is constant", N);
3054 Error_Msg_N
3055 ("\cannot call operation that may modify it", N);
3056 end if;
3057 end if;
3058 end Check_Internal_Protected_Use;
3059
3060 ---------------------------------------
3061 -- Check_Later_Vs_Basic_Declarations --
3062 ---------------------------------------
3063
3064 procedure Check_Later_Vs_Basic_Declarations
3065 (Decls : List_Id;
3066 During_Parsing : Boolean)
3067 is
3068 Body_Sloc : Source_Ptr;
3069 Decl : Node_Id;
3070
3071 function Is_Later_Declarative_Item (Decl : Node_Id) return Boolean;
3072 -- Return whether Decl is considered as a declarative item.
3073 -- When During_Parsing is True, the semantics of Ada 83 is followed.
3074 -- When During_Parsing is False, the semantics of SPARK is followed.
3075
3076 -------------------------------
3077 -- Is_Later_Declarative_Item --
3078 -------------------------------
3079
3080 function Is_Later_Declarative_Item (Decl : Node_Id) return Boolean is
3081 begin
3082 if Nkind (Decl) in N_Later_Decl_Item then
3083 return True;
3084
3085 elsif Nkind (Decl) = N_Pragma then
3086 return True;
3087
3088 elsif During_Parsing then
3089 return False;
3090
3091 -- In SPARK, a package declaration is not considered as a later
3092 -- declarative item.
3093
3094 elsif Nkind (Decl) = N_Package_Declaration then
3095 return False;
3096
3097 -- In SPARK, a renaming is considered as a later declarative item
3098
3099 elsif Nkind (Decl) in N_Renaming_Declaration then
3100 return True;
3101
3102 else
3103 return False;
3104 end if;
3105 end Is_Later_Declarative_Item;
3106
3107 -- Start of processing for Check_Later_Vs_Basic_Declarations
3108
3109 begin
3110 Decl := First (Decls);
3111
3112 -- Loop through sequence of basic declarative items
3113
3114 Outer : while Present (Decl) loop
3115 if not Nkind_In (Decl, N_Subprogram_Body, N_Package_Body, N_Task_Body)
3116 and then Nkind (Decl) not in N_Body_Stub
3117 then
3118 Next (Decl);
3119
3120 -- Once a body is encountered, we only allow later declarative
3121 -- items. The inner loop checks the rest of the list.
3122
3123 else
3124 Body_Sloc := Sloc (Decl);
3125
3126 Inner : while Present (Decl) loop
3127 if not Is_Later_Declarative_Item (Decl) then
3128 if During_Parsing then
3129 if Ada_Version = Ada_83 then
3130 Error_Msg_Sloc := Body_Sloc;
3131 Error_Msg_N
3132 ("(Ada 83) decl cannot appear after body#", Decl);
3133 end if;
3134 else
3135 Error_Msg_Sloc := Body_Sloc;
3136 Check_SPARK_05_Restriction
3137 ("decl cannot appear after body#", Decl);
3138 end if;
3139 end if;
3140
3141 Next (Decl);
3142 end loop Inner;
3143 end if;
3144 end loop Outer;
3145 end Check_Later_Vs_Basic_Declarations;
3146
3147 ---------------------------
3148 -- Check_No_Hidden_State --
3149 ---------------------------
3150
3151 procedure Check_No_Hidden_State (Id : Entity_Id) is
3152 function Has_Null_Abstract_State (Pkg : Entity_Id) return Boolean;
3153 -- Determine whether the entity of a package denoted by Pkg has a null
3154 -- abstract state.
3155
3156 -----------------------------
3157 -- Has_Null_Abstract_State --
3158 -----------------------------
3159
3160 function Has_Null_Abstract_State (Pkg : Entity_Id) return Boolean is
3161 States : constant Elist_Id := Abstract_States (Pkg);
3162
3163 begin
3164 -- Check first available state of related package. A null abstract
3165 -- state always appears as the sole element of the state list.
3166
3167 return
3168 Present (States)
3169 and then Is_Null_State (Node (First_Elmt (States)));
3170 end Has_Null_Abstract_State;
3171
3172 -- Local variables
3173
3174 Context : Entity_Id := Empty;
3175 Not_Visible : Boolean := False;
3176 Scop : Entity_Id;
3177
3178 -- Start of processing for Check_No_Hidden_State
3179
3180 begin
3181 pragma Assert (Ekind_In (Id, E_Abstract_State, E_Variable));
3182
3183 -- Find the proper context where the object or state appears
3184
3185 Scop := Scope (Id);
3186 while Present (Scop) loop
3187 Context := Scop;
3188
3189 -- Keep track of the context's visibility
3190
3191 Not_Visible := Not_Visible or else In_Private_Part (Context);
3192
3193 -- Prevent the search from going too far
3194
3195 if Context = Standard_Standard then
3196 return;
3197
3198 -- Objects and states that appear immediately within a subprogram or
3199 -- inside a construct nested within a subprogram do not introduce a
3200 -- hidden state. They behave as local variable declarations.
3201
3202 elsif Is_Subprogram (Context) then
3203 return;
3204
3205 -- When examining a package body, use the entity of the spec as it
3206 -- carries the abstract state declarations.
3207
3208 elsif Ekind (Context) = E_Package_Body then
3209 Context := Spec_Entity (Context);
3210 end if;
3211
3212 -- Stop the traversal when a package subject to a null abstract state
3213 -- has been found.
3214
3215 if Ekind_In (Context, E_Generic_Package, E_Package)
3216 and then Has_Null_Abstract_State (Context)
3217 then
3218 exit;
3219 end if;
3220
3221 Scop := Scope (Scop);
3222 end loop;
3223
3224 -- At this point we know that there is at least one package with a null
3225 -- abstract state in visibility. Emit an error message unconditionally
3226 -- if the entity being processed is a state because the placement of the
3227 -- related package is irrelevant. This is not the case for objects as
3228 -- the intermediate context matters.
3229
3230 if Present (Context)
3231 and then (Ekind (Id) = E_Abstract_State or else Not_Visible)
3232 then
3233 Error_Msg_N ("cannot introduce hidden state &", Id);
3234 Error_Msg_NE ("\package & has null abstract state", Id, Context);
3235 end if;
3236 end Check_No_Hidden_State;
3237
3238 ----------------------------------------
3239 -- Check_Nonvolatile_Function_Profile --
3240 ----------------------------------------
3241
3242 procedure Check_Nonvolatile_Function_Profile (Func_Id : Entity_Id) is
3243 Formal : Entity_Id;
3244
3245 begin
3246 -- Inspect all formal parameters
3247
3248 Formal := First_Formal (Func_Id);
3249 while Present (Formal) loop
3250 if Is_Effectively_Volatile (Etype (Formal)) then
3251 Error_Msg_NE
3252 ("nonvolatile function & cannot have a volatile parameter",
3253 Formal, Func_Id);
3254 end if;
3255
3256 Next_Formal (Formal);
3257 end loop;
3258
3259 -- Inspect the return type
3260
3261 if Is_Effectively_Volatile (Etype (Func_Id)) then
3262 Error_Msg_NE
3263 ("nonvolatile function & cannot have a volatile return type",
3264 Result_Definition (Parent (Func_Id)), Func_Id);
3265 end if;
3266 end Check_Nonvolatile_Function_Profile;
3267
3268 ------------------------------------------
3269 -- Check_Potentially_Blocking_Operation --
3270 ------------------------------------------
3271
3272 procedure Check_Potentially_Blocking_Operation (N : Node_Id) is
3273 S : Entity_Id;
3274
3275 begin
3276 -- N is one of the potentially blocking operations listed in 9.5.1(8).
3277 -- When pragma Detect_Blocking is active, the run time will raise
3278 -- Program_Error. Here we only issue a warning, since we generally
3279 -- support the use of potentially blocking operations in the absence
3280 -- of the pragma.
3281
3282 -- Indirect blocking through a subprogram call cannot be diagnosed
3283 -- statically without interprocedural analysis, so we do not attempt
3284 -- to do it here.
3285
3286 S := Scope (Current_Scope);
3287 while Present (S) and then S /= Standard_Standard loop
3288 if Is_Protected_Type (S) then
3289 Error_Msg_N
3290 ("potentially blocking operation in protected operation??", N);
3291 return;
3292 end if;
3293
3294 S := Scope (S);
3295 end loop;
3296 end Check_Potentially_Blocking_Operation;
3297
3298 ---------------------------------
3299 -- Check_Result_And_Post_State --
3300 ---------------------------------
3301
3302 procedure Check_Result_And_Post_State (Subp_Id : Entity_Id) is
3303 procedure Check_Result_And_Post_State_In_Pragma
3304 (Prag : Node_Id;
3305 Result_Seen : in out Boolean);
3306 -- Determine whether pragma Prag mentions attribute 'Result and whether
3307 -- the pragma contains an expression that evaluates differently in pre-
3308 -- and post-state. Prag is a [refined] postcondition or a contract-cases
3309 -- pragma. Result_Seen is set when the pragma mentions attribute 'Result
3310
3311 function Has_In_Out_Parameter (Subp_Id : Entity_Id) return Boolean;
3312 -- Determine whether subprogram Subp_Id contains at least one IN OUT
3313 -- formal parameter.
3314
3315 -------------------------------------------
3316 -- Check_Result_And_Post_State_In_Pragma --
3317 -------------------------------------------
3318
3319 procedure Check_Result_And_Post_State_In_Pragma
3320 (Prag : Node_Id;
3321 Result_Seen : in out Boolean)
3322 is
3323 procedure Check_Expression (Expr : Node_Id);
3324 -- Perform the 'Result and post-state checks on a given expression
3325
3326 function Is_Function_Result (N : Node_Id) return Traverse_Result;
3327 -- Attempt to find attribute 'Result in a subtree denoted by N
3328
3329 function Is_Trivial_Boolean (N : Node_Id) return Boolean;
3330 -- Determine whether source node N denotes "True" or "False"
3331
3332 function Mentions_Post_State (N : Node_Id) return Boolean;
3333 -- Determine whether a subtree denoted by N mentions any construct
3334 -- that denotes a post-state.
3335
3336 procedure Check_Function_Result is
3337 new Traverse_Proc (Is_Function_Result);
3338
3339 ----------------------
3340 -- Check_Expression --
3341 ----------------------
3342
3343 procedure Check_Expression (Expr : Node_Id) is
3344 begin
3345 if not Is_Trivial_Boolean (Expr) then
3346 Check_Function_Result (Expr);
3347
3348 if not Mentions_Post_State (Expr) then
3349 if Pragma_Name (Prag) = Name_Contract_Cases then
3350 Error_Msg_NE
3351 ("contract case does not check the outcome of calling "
3352 & "&?T?", Expr, Subp_Id);
3353
3354 elsif Pragma_Name (Prag) = Name_Refined_Post then
3355 Error_Msg_NE
3356 ("refined postcondition does not check the outcome of "
3357 & "calling &?T?", Prag, Subp_Id);
3358
3359 else
3360 Error_Msg_NE
3361 ("postcondition does not check the outcome of calling "
3362 & "&?T?", Prag, Subp_Id);
3363 end if;
3364 end if;
3365 end if;
3366 end Check_Expression;
3367
3368 ------------------------
3369 -- Is_Function_Result --
3370 ------------------------
3371
3372 function Is_Function_Result (N : Node_Id) return Traverse_Result is
3373 begin
3374 if Is_Attribute_Result (N) then
3375 Result_Seen := True;
3376 return Abandon;
3377
3378 -- Continue the traversal
3379
3380 else
3381 return OK;
3382 end if;
3383 end Is_Function_Result;
3384
3385 ------------------------
3386 -- Is_Trivial_Boolean --
3387 ------------------------
3388
3389 function Is_Trivial_Boolean (N : Node_Id) return Boolean is
3390 begin
3391 return
3392 Comes_From_Source (N)
3393 and then Is_Entity_Name (N)
3394 and then (Entity (N) = Standard_True
3395 or else
3396 Entity (N) = Standard_False);
3397 end Is_Trivial_Boolean;
3398
3399 -------------------------
3400 -- Mentions_Post_State --
3401 -------------------------
3402
3403 function Mentions_Post_State (N : Node_Id) return Boolean is
3404 Post_State_Seen : Boolean := False;
3405
3406 function Is_Post_State (N : Node_Id) return Traverse_Result;
3407 -- Attempt to find a construct that denotes a post-state. If this
3408 -- is the case, set flag Post_State_Seen.
3409
3410 -------------------
3411 -- Is_Post_State --
3412 -------------------
3413
3414 function Is_Post_State (N : Node_Id) return Traverse_Result is
3415 Ent : Entity_Id;
3416
3417 begin
3418 if Nkind_In (N, N_Explicit_Dereference, N_Function_Call) then
3419 Post_State_Seen := True;
3420 return Abandon;
3421
3422 elsif Nkind_In (N, N_Expanded_Name, N_Identifier) then
3423 Ent := Entity (N);
3424
3425 -- The entity may be modifiable through an implicit
3426 -- dereference.
3427
3428 if No (Ent)
3429 or else Ekind (Ent) in Assignable_Kind
3430 or else (Is_Access_Type (Etype (Ent))
3431 and then Nkind (Parent (N)) =
3432 N_Selected_Component)
3433 then
3434 Post_State_Seen := True;
3435 return Abandon;
3436 end if;
3437
3438 elsif Nkind (N) = N_Attribute_Reference then
3439 if Attribute_Name (N) = Name_Old then
3440 return Skip;
3441
3442 elsif Attribute_Name (N) = Name_Result then
3443 Post_State_Seen := True;
3444 return Abandon;
3445 end if;
3446 end if;
3447
3448 return OK;
3449 end Is_Post_State;
3450
3451 procedure Find_Post_State is new Traverse_Proc (Is_Post_State);
3452
3453 -- Start of processing for Mentions_Post_State
3454
3455 begin
3456 Find_Post_State (N);
3457
3458 return Post_State_Seen;
3459 end Mentions_Post_State;
3460
3461 -- Local variables
3462
3463 Expr : constant Node_Id :=
3464 Get_Pragma_Arg
3465 (First (Pragma_Argument_Associations (Prag)));
3466 Nam : constant Name_Id := Pragma_Name (Prag);
3467 CCase : Node_Id;
3468
3469 -- Start of processing for Check_Result_And_Post_State_In_Pragma
3470
3471 begin
3472 -- Examine all consequences
3473
3474 if Nam = Name_Contract_Cases then
3475 CCase := First (Component_Associations (Expr));
3476 while Present (CCase) loop
3477 Check_Expression (Expression (CCase));
3478
3479 Next (CCase);
3480 end loop;
3481
3482 -- Examine the expression of a postcondition
3483
3484 else pragma Assert (Nam_In (Nam, Name_Postcondition,
3485 Name_Refined_Post));
3486 Check_Expression (Expr);
3487 end if;
3488 end Check_Result_And_Post_State_In_Pragma;
3489
3490 --------------------------
3491 -- Has_In_Out_Parameter --
3492 --------------------------
3493
3494 function Has_In_Out_Parameter (Subp_Id : Entity_Id) return Boolean is
3495 Formal : Entity_Id;
3496
3497 begin
3498 -- Traverse the formals looking for an IN OUT parameter
3499
3500 Formal := First_Formal (Subp_Id);
3501 while Present (Formal) loop
3502 if Ekind (Formal) = E_In_Out_Parameter then
3503 return True;
3504 end if;
3505
3506 Next_Formal (Formal);
3507 end loop;
3508
3509 return False;
3510 end Has_In_Out_Parameter;
3511
3512 -- Local variables
3513
3514 Items : constant Node_Id := Contract (Subp_Id);
3515 Subp_Decl : constant Node_Id := Unit_Declaration_Node (Subp_Id);
3516 Case_Prag : Node_Id := Empty;
3517 Post_Prag : Node_Id := Empty;
3518 Prag : Node_Id;
3519 Seen_In_Case : Boolean := False;
3520 Seen_In_Post : Boolean := False;
3521 Spec_Id : Entity_Id;
3522
3523 -- Start of processing for Check_Result_And_Post_State
3524
3525 begin
3526 -- The lack of attribute 'Result or a post-state is classified as a
3527 -- suspicious contract. Do not perform the check if the corresponding
3528 -- swich is not set.
3529
3530 if not Warn_On_Suspicious_Contract then
3531 return;
3532
3533 -- Nothing to do if there is no contract
3534
3535 elsif No (Items) then
3536 return;
3537 end if;
3538
3539 -- Retrieve the entity of the subprogram spec (if any)
3540
3541 if Nkind (Subp_Decl) = N_Subprogram_Body
3542 and then Present (Corresponding_Spec (Subp_Decl))
3543 then
3544 Spec_Id := Corresponding_Spec (Subp_Decl);
3545
3546 elsif Nkind (Subp_Decl) = N_Subprogram_Body_Stub
3547 and then Present (Corresponding_Spec_Of_Stub (Subp_Decl))
3548 then
3549 Spec_Id := Corresponding_Spec_Of_Stub (Subp_Decl);
3550
3551 else
3552 Spec_Id := Subp_Id;
3553 end if;
3554
3555 -- Examine all postconditions for attribute 'Result and a post-state
3556
3557 Prag := Pre_Post_Conditions (Items);
3558 while Present (Prag) loop
3559 if Nam_In (Pragma_Name (Prag), Name_Postcondition,
3560 Name_Refined_Post)
3561 and then not Error_Posted (Prag)
3562 then
3563 Post_Prag := Prag;
3564 Check_Result_And_Post_State_In_Pragma (Prag, Seen_In_Post);
3565 end if;
3566
3567 Prag := Next_Pragma (Prag);
3568 end loop;
3569
3570 -- Examine the contract cases of the subprogram for attribute 'Result
3571 -- and a post-state.
3572
3573 Prag := Contract_Test_Cases (Items);
3574 while Present (Prag) loop
3575 if Pragma_Name (Prag) = Name_Contract_Cases
3576 and then not Error_Posted (Prag)
3577 then
3578 Case_Prag := Prag;
3579 Check_Result_And_Post_State_In_Pragma (Prag, Seen_In_Case);
3580 end if;
3581
3582 Prag := Next_Pragma (Prag);
3583 end loop;
3584
3585 -- Do not emit any errors if the subprogram is not a function
3586
3587 if not Ekind_In (Spec_Id, E_Function, E_Generic_Function) then
3588 null;
3589
3590 -- Regardless of whether the function has postconditions or contract
3591 -- cases, or whether they mention attribute 'Result, an IN OUT formal
3592 -- parameter is always treated as a result.
3593
3594 elsif Has_In_Out_Parameter (Spec_Id) then
3595 null;
3596
3597 -- The function has both a postcondition and contract cases and they do
3598 -- not mention attribute 'Result.
3599
3600 elsif Present (Case_Prag)
3601 and then not Seen_In_Case
3602 and then Present (Post_Prag)
3603 and then not Seen_In_Post
3604 then
3605 Error_Msg_N
3606 ("neither postcondition nor contract cases mention function "
3607 & "result?T?", Post_Prag);
3608
3609 -- The function has contract cases only and they do not mention
3610 -- attribute 'Result.
3611
3612 elsif Present (Case_Prag) and then not Seen_In_Case then
3613 Error_Msg_N ("contract cases do not mention result?T?", Case_Prag);
3614
3615 -- The function has postconditions only and they do not mention
3616 -- attribute 'Result.
3617
3618 elsif Present (Post_Prag) and then not Seen_In_Post then
3619 Error_Msg_N
3620 ("postcondition does not mention function result?T?", Post_Prag);
3621 end if;
3622 end Check_Result_And_Post_State;
3623
3624 ------------------------------
3625 -- Check_Unprotected_Access --
3626 ------------------------------
3627
3628 procedure Check_Unprotected_Access
3629 (Context : Node_Id;
3630 Expr : Node_Id)
3631 is
3632 Cont_Encl_Typ : Entity_Id;
3633 Pref_Encl_Typ : Entity_Id;
3634
3635 function Enclosing_Protected_Type (Obj : Node_Id) return Entity_Id;
3636 -- Check whether Obj is a private component of a protected object.
3637 -- Return the protected type where the component resides, Empty
3638 -- otherwise.
3639
3640 function Is_Public_Operation return Boolean;
3641 -- Verify that the enclosing operation is callable from outside the
3642 -- protected object, to minimize false positives.
3643
3644 ------------------------------
3645 -- Enclosing_Protected_Type --
3646 ------------------------------
3647
3648 function Enclosing_Protected_Type (Obj : Node_Id) return Entity_Id is
3649 begin
3650 if Is_Entity_Name (Obj) then
3651 declare
3652 Ent : Entity_Id := Entity (Obj);
3653
3654 begin
3655 -- The object can be a renaming of a private component, use
3656 -- the original record component.
3657
3658 if Is_Prival (Ent) then
3659 Ent := Prival_Link (Ent);
3660 end if;
3661
3662 if Is_Protected_Type (Scope (Ent)) then
3663 return Scope (Ent);
3664 end if;
3665 end;
3666 end if;
3667
3668 -- For indexed and selected components, recursively check the prefix
3669
3670 if Nkind_In (Obj, N_Indexed_Component, N_Selected_Component) then
3671 return Enclosing_Protected_Type (Prefix (Obj));
3672
3673 -- The object does not denote a protected component
3674
3675 else
3676 return Empty;
3677 end if;
3678 end Enclosing_Protected_Type;
3679
3680 -------------------------
3681 -- Is_Public_Operation --
3682 -------------------------
3683
3684 function Is_Public_Operation return Boolean is
3685 S : Entity_Id;
3686 E : Entity_Id;
3687
3688 begin
3689 S := Current_Scope;
3690 while Present (S) and then S /= Pref_Encl_Typ loop
3691 if Scope (S) = Pref_Encl_Typ then
3692 E := First_Entity (Pref_Encl_Typ);
3693 while Present (E)
3694 and then E /= First_Private_Entity (Pref_Encl_Typ)
3695 loop
3696 if E = S then
3697 return True;
3698 end if;
3699
3700 Next_Entity (E);
3701 end loop;
3702 end if;
3703
3704 S := Scope (S);
3705 end loop;
3706
3707 return False;
3708 end Is_Public_Operation;
3709
3710 -- Start of processing for Check_Unprotected_Access
3711
3712 begin
3713 if Nkind (Expr) = N_Attribute_Reference
3714 and then Attribute_Name (Expr) = Name_Unchecked_Access
3715 then
3716 Cont_Encl_Typ := Enclosing_Protected_Type (Context);
3717 Pref_Encl_Typ := Enclosing_Protected_Type (Prefix (Expr));
3718
3719 -- Check whether we are trying to export a protected component to a
3720 -- context with an equal or lower access level.
3721
3722 if Present (Pref_Encl_Typ)
3723 and then No (Cont_Encl_Typ)
3724 and then Is_Public_Operation
3725 and then Scope_Depth (Pref_Encl_Typ) >=
3726 Object_Access_Level (Context)
3727 then
3728 Error_Msg_N
3729 ("??possible unprotected access to protected data", Expr);
3730 end if;
3731 end if;
3732 end Check_Unprotected_Access;
3733
3734 ------------------------------
3735 -- Check_Unused_Body_States --
3736 ------------------------------
3737
3738 procedure Check_Unused_Body_States (Body_Id : Entity_Id) is
3739 procedure Process_Refinement_Clause
3740 (Clause : Node_Id;
3741 States : Elist_Id);
3742 -- Inspect all constituents of refinement clause Clause and remove any
3743 -- matches from body state list States.
3744
3745 procedure Report_Unused_Body_States (States : Elist_Id);
3746 -- Emit errors for each abstract state or object found in list States
3747
3748 -------------------------------
3749 -- Process_Refinement_Clause --
3750 -------------------------------
3751
3752 procedure Process_Refinement_Clause
3753 (Clause : Node_Id;
3754 States : Elist_Id)
3755 is
3756 procedure Process_Constituent (Constit : Node_Id);
3757 -- Remove constituent Constit from body state list States
3758
3759 -------------------------
3760 -- Process_Constituent --
3761 -------------------------
3762
3763 procedure Process_Constituent (Constit : Node_Id) is
3764 Constit_Id : Entity_Id;
3765
3766 begin
3767 -- Guard against illegal constituents. Only abstract states and
3768 -- objects can appear on the right hand side of a refinement.
3769
3770 if Is_Entity_Name (Constit) then
3771 Constit_Id := Entity_Of (Constit);
3772
3773 if Present (Constit_Id)
3774 and then Ekind_In (Constit_Id, E_Abstract_State,
3775 E_Constant,
3776 E_Variable)
3777 then
3778 Remove (States, Constit_Id);
3779 end if;
3780 end if;
3781 end Process_Constituent;
3782
3783 -- Local variables
3784
3785 Constit : Node_Id;
3786
3787 -- Start of processing for Process_Refinement_Clause
3788
3789 begin
3790 if Nkind (Clause) = N_Component_Association then
3791 Constit := Expression (Clause);
3792
3793 -- Multiple constituents appear as an aggregate
3794
3795 if Nkind (Constit) = N_Aggregate then
3796 Constit := First (Expressions (Constit));
3797 while Present (Constit) loop
3798 Process_Constituent (Constit);
3799 Next (Constit);
3800 end loop;
3801
3802 -- Various forms of a single constituent
3803
3804 else
3805 Process_Constituent (Constit);
3806 end if;
3807 end if;
3808 end Process_Refinement_Clause;
3809
3810 -------------------------------
3811 -- Report_Unused_Body_States --
3812 -------------------------------
3813
3814 procedure Report_Unused_Body_States (States : Elist_Id) is
3815 Posted : Boolean := False;
3816 State_Elmt : Elmt_Id;
3817 State_Id : Entity_Id;
3818
3819 begin
3820 if Present (States) then
3821 State_Elmt := First_Elmt (States);
3822 while Present (State_Elmt) loop
3823 State_Id := Node (State_Elmt);
3824
3825 -- Constants are part of the hidden state of a package, but the
3826 -- compiler cannot determine whether they have variable input
3827 -- (SPARK RM 7.1.1(2)) and cannot classify them properly as a
3828 -- hidden state. Do not emit an error when a constant does not
3829 -- participate in a state refinement, even though it acts as a
3830 -- hidden state.
3831
3832 if Ekind (State_Id) = E_Constant then
3833 null;
3834
3835 -- Generate an error message of the form:
3836
3837 -- body of package ... has unused hidden states
3838 -- abstract state ... defined at ...
3839 -- variable ... defined at ...
3840
3841 else
3842 if not Posted then
3843 Posted := True;
3844 SPARK_Msg_N
3845 ("body of package & has unused hidden states", Body_Id);
3846 end if;
3847
3848 Error_Msg_Sloc := Sloc (State_Id);
3849
3850 if Ekind (State_Id) = E_Abstract_State then
3851 SPARK_Msg_NE
3852 ("\abstract state & defined #", Body_Id, State_Id);
3853
3854 else
3855 SPARK_Msg_NE ("\variable & defined #", Body_Id, State_Id);
3856 end if;
3857 end if;
3858
3859 Next_Elmt (State_Elmt);
3860 end loop;
3861 end if;
3862 end Report_Unused_Body_States;
3863
3864 -- Local variables
3865
3866 Prag : constant Node_Id := Get_Pragma (Body_Id, Pragma_Refined_State);
3867 Spec_Id : constant Entity_Id := Spec_Entity (Body_Id);
3868 Clause : Node_Id;
3869 States : Elist_Id;
3870
3871 -- Start of processing for Check_Unused_Body_States
3872
3873 begin
3874 -- Inspect the clauses of pragma Refined_State and determine whether all
3875 -- visible states declared within the package body participate in the
3876 -- refinement.
3877
3878 if Present (Prag) then
3879 Clause := Expression (Get_Argument (Prag, Spec_Id));
3880 States := Collect_Body_States (Body_Id);
3881
3882 -- Multiple non-null state refinements appear as an aggregate
3883
3884 if Nkind (Clause) = N_Aggregate then
3885 Clause := First (Component_Associations (Clause));
3886 while Present (Clause) loop
3887 Process_Refinement_Clause (Clause, States);
3888 Next (Clause);
3889 end loop;
3890
3891 -- Various forms of a single state refinement
3892
3893 else
3894 Process_Refinement_Clause (Clause, States);
3895 end if;
3896
3897 -- Ensure that all abstract states and objects declared in the
3898 -- package body state space are utilized as constituents.
3899
3900 Report_Unused_Body_States (States);
3901 end if;
3902 end Check_Unused_Body_States;
3903
3904 -------------------------
3905 -- Collect_Body_States --
3906 -------------------------
3907
3908 function Collect_Body_States (Body_Id : Entity_Id) return Elist_Id is
3909 function Is_Visible_Object (Obj_Id : Entity_Id) return Boolean;
3910 -- Determine whether object Obj_Id is a suitable visible state of a
3911 -- package body.
3912
3913 procedure Collect_Visible_States
3914 (Pack_Id : Entity_Id;
3915 States : in out Elist_Id);
3916 -- Gather the entities of all abstract states and objects declared in
3917 -- the visible state space of package Pack_Id.
3918
3919 ----------------------------
3920 -- Collect_Visible_States --
3921 ----------------------------
3922
3923 procedure Collect_Visible_States
3924 (Pack_Id : Entity_Id;
3925 States : in out Elist_Id)
3926 is
3927 Item_Id : Entity_Id;
3928
3929 begin
3930 -- Traverse the entity chain of the package and inspect all visible
3931 -- items.
3932
3933 Item_Id := First_Entity (Pack_Id);
3934 while Present (Item_Id) and then not In_Private_Part (Item_Id) loop
3935
3936 -- Do not consider internally generated items as those cannot be
3937 -- named and participate in refinement.
3938
3939 if not Comes_From_Source (Item_Id) then
3940 null;
3941
3942 elsif Ekind (Item_Id) = E_Abstract_State then
3943 Append_New_Elmt (Item_Id, States);
3944
3945 elsif Ekind_In (Item_Id, E_Constant, E_Variable)
3946 and then Is_Visible_Object (Item_Id)
3947 then
3948 Append_New_Elmt (Item_Id, States);
3949
3950 -- Recursively gather the visible states of a nested package
3951
3952 elsif Ekind (Item_Id) = E_Package then
3953 Collect_Visible_States (Item_Id, States);
3954 end if;
3955
3956 Next_Entity (Item_Id);
3957 end loop;
3958 end Collect_Visible_States;
3959
3960 -----------------------
3961 -- Is_Visible_Object --
3962 -----------------------
3963
3964 function Is_Visible_Object (Obj_Id : Entity_Id) return Boolean is
3965 begin
3966 -- Objects that map generic formals to their actuals are not visible
3967 -- from outside the generic instantiation.
3968
3969 if Present (Corresponding_Generic_Association
3970 (Declaration_Node (Obj_Id)))
3971 then
3972 return False;
3973
3974 -- Constituents of a single protected/task type act as components of
3975 -- the type and are not visible from outside the type.
3976
3977 elsif Ekind (Obj_Id) = E_Variable
3978 and then Present (Encapsulating_State (Obj_Id))
3979 and then Is_Single_Concurrent_Object (Encapsulating_State (Obj_Id))
3980 then
3981 return False;
3982
3983 else
3984 return True;
3985 end if;
3986 end Is_Visible_Object;
3987
3988 -- Local variables
3989
3990 Body_Decl : constant Node_Id := Unit_Declaration_Node (Body_Id);
3991 Decl : Node_Id;
3992 Item_Id : Entity_Id;
3993 States : Elist_Id := No_Elist;
3994
3995 -- Start of processing for Collect_Body_States
3996
3997 begin
3998 -- Inspect the declarations of the body looking for source objects,
3999 -- packages and package instantiations. Note that even though this
4000 -- processing is very similar to Collect_Visible_States, a package
4001 -- body does not have a First/Next_Entity list.
4002
4003 Decl := First (Declarations (Body_Decl));
4004 while Present (Decl) loop
4005
4006 -- Capture source objects as internally generated temporaries cannot
4007 -- be named and participate in refinement.
4008
4009 if Nkind (Decl) = N_Object_Declaration then
4010 Item_Id := Defining_Entity (Decl);
4011
4012 if Comes_From_Source (Item_Id)
4013 and then Is_Visible_Object (Item_Id)
4014 then
4015 Append_New_Elmt (Item_Id, States);
4016 end if;
4017
4018 -- Capture the visible abstract states and objects of a source
4019 -- package [instantiation].
4020
4021 elsif Nkind (Decl) = N_Package_Declaration then
4022 Item_Id := Defining_Entity (Decl);
4023
4024 if Comes_From_Source (Item_Id) then
4025 Collect_Visible_States (Item_Id, States);
4026 end if;
4027 end if;
4028
4029 Next (Decl);
4030 end loop;
4031
4032 return States;
4033 end Collect_Body_States;
4034
4035 ------------------------
4036 -- Collect_Interfaces --
4037 ------------------------
4038
4039 procedure Collect_Interfaces
4040 (T : Entity_Id;
4041 Ifaces_List : out Elist_Id;
4042 Exclude_Parents : Boolean := False;
4043 Use_Full_View : Boolean := True)
4044 is
4045 procedure Collect (Typ : Entity_Id);
4046 -- Subsidiary subprogram used to traverse the whole list
4047 -- of directly and indirectly implemented interfaces
4048
4049 -------------
4050 -- Collect --
4051 -------------
4052
4053 procedure Collect (Typ : Entity_Id) is
4054 Ancestor : Entity_Id;
4055 Full_T : Entity_Id;
4056 Id : Node_Id;
4057 Iface : Entity_Id;
4058
4059 begin
4060 Full_T := Typ;
4061
4062 -- Handle private types and subtypes
4063
4064 if Use_Full_View
4065 and then Is_Private_Type (Typ)
4066 and then Present (Full_View (Typ))
4067 then
4068 Full_T := Full_View (Typ);
4069
4070 if Ekind (Full_T) = E_Record_Subtype then
4071 Full_T := Full_View (Etype (Typ));
4072 end if;
4073 end if;
4074
4075 -- Include the ancestor if we are generating the whole list of
4076 -- abstract interfaces.
4077
4078 if Etype (Full_T) /= Typ
4079
4080 -- Protect the frontend against wrong sources. For example:
4081
4082 -- package P is
4083 -- type A is tagged null record;
4084 -- type B is new A with private;
4085 -- type C is new A with private;
4086 -- private
4087 -- type B is new C with null record;
4088 -- type C is new B with null record;
4089 -- end P;
4090
4091 and then Etype (Full_T) /= T
4092 then
4093 Ancestor := Etype (Full_T);
4094 Collect (Ancestor);
4095
4096 if Is_Interface (Ancestor) and then not Exclude_Parents then
4097 Append_Unique_Elmt (Ancestor, Ifaces_List);
4098 end if;
4099 end if;
4100
4101 -- Traverse the graph of ancestor interfaces
4102
4103 if Is_Non_Empty_List (Abstract_Interface_List (Full_T)) then
4104 Id := First (Abstract_Interface_List (Full_T));
4105 while Present (Id) loop
4106 Iface := Etype (Id);
4107
4108 -- Protect against wrong uses. For example:
4109 -- type I is interface;
4110 -- type O is tagged null record;
4111 -- type Wrong is new I and O with null record; -- ERROR
4112
4113 if Is_Interface (Iface) then
4114 if Exclude_Parents
4115 and then Etype (T) /= T
4116 and then Interface_Present_In_Ancestor (Etype (T), Iface)
4117 then
4118 null;
4119 else
4120 Collect (Iface);
4121 Append_Unique_Elmt (Iface, Ifaces_List);
4122 end if;
4123 end if;
4124
4125 Next (Id);
4126 end loop;
4127 end if;
4128 end Collect;
4129
4130 -- Start of processing for Collect_Interfaces
4131
4132 begin
4133 pragma Assert (Is_Tagged_Type (T) or else Is_Concurrent_Type (T));
4134 Ifaces_List := New_Elmt_List;
4135 Collect (T);
4136 end Collect_Interfaces;
4137
4138 ----------------------------------
4139 -- Collect_Interface_Components --
4140 ----------------------------------
4141
4142 procedure Collect_Interface_Components
4143 (Tagged_Type : Entity_Id;
4144 Components_List : out Elist_Id)
4145 is
4146 procedure Collect (Typ : Entity_Id);
4147 -- Subsidiary subprogram used to climb to the parents
4148
4149 -------------
4150 -- Collect --
4151 -------------
4152
4153 procedure Collect (Typ : Entity_Id) is
4154 Tag_Comp : Entity_Id;
4155 Parent_Typ : Entity_Id;
4156
4157 begin
4158 -- Handle private types
4159
4160 if Present (Full_View (Etype (Typ))) then
4161 Parent_Typ := Full_View (Etype (Typ));
4162 else
4163 Parent_Typ := Etype (Typ);
4164 end if;
4165
4166 if Parent_Typ /= Typ
4167
4168 -- Protect the frontend against wrong sources. For example:
4169
4170 -- package P is
4171 -- type A is tagged null record;
4172 -- type B is new A with private;
4173 -- type C is new A with private;
4174 -- private
4175 -- type B is new C with null record;
4176 -- type C is new B with null record;
4177 -- end P;
4178
4179 and then Parent_Typ /= Tagged_Type
4180 then
4181 Collect (Parent_Typ);
4182 end if;
4183
4184 -- Collect the components containing tags of secondary dispatch
4185 -- tables.
4186
4187 Tag_Comp := Next_Tag_Component (First_Tag_Component (Typ));
4188 while Present (Tag_Comp) loop
4189 pragma Assert (Present (Related_Type (Tag_Comp)));
4190 Append_Elmt (Tag_Comp, Components_List);
4191
4192 Tag_Comp := Next_Tag_Component (Tag_Comp);
4193 end loop;
4194 end Collect;
4195
4196 -- Start of processing for Collect_Interface_Components
4197
4198 begin
4199 pragma Assert (Ekind (Tagged_Type) = E_Record_Type
4200 and then Is_Tagged_Type (Tagged_Type));
4201
4202 Components_List := New_Elmt_List;
4203 Collect (Tagged_Type);
4204 end Collect_Interface_Components;
4205
4206 -----------------------------
4207 -- Collect_Interfaces_Info --
4208 -----------------------------
4209
4210 procedure Collect_Interfaces_Info
4211 (T : Entity_Id;
4212 Ifaces_List : out Elist_Id;
4213 Components_List : out Elist_Id;
4214 Tags_List : out Elist_Id)
4215 is
4216 Comps_List : Elist_Id;
4217 Comp_Elmt : Elmt_Id;
4218 Comp_Iface : Entity_Id;
4219 Iface_Elmt : Elmt_Id;
4220 Iface : Entity_Id;
4221
4222 function Search_Tag (Iface : Entity_Id) return Entity_Id;
4223 -- Search for the secondary tag associated with the interface type
4224 -- Iface that is implemented by T.
4225
4226 ----------------
4227 -- Search_Tag --
4228 ----------------
4229
4230 function Search_Tag (Iface : Entity_Id) return Entity_Id is
4231 ADT : Elmt_Id;
4232 begin
4233 if not Is_CPP_Class (T) then
4234 ADT := Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (T))));
4235 else
4236 ADT := Next_Elmt (First_Elmt (Access_Disp_Table (T)));
4237 end if;
4238
4239 while Present (ADT)
4240 and then Is_Tag (Node (ADT))
4241 and then Related_Type (Node (ADT)) /= Iface
4242 loop
4243 -- Skip secondary dispatch table referencing thunks to user
4244 -- defined primitives covered by this interface.
4245
4246 pragma Assert (Has_Suffix (Node (ADT), 'P'));
4247 Next_Elmt (ADT);
4248
4249 -- Skip secondary dispatch tables of Ada types
4250
4251 if not Is_CPP_Class (T) then
4252
4253 -- Skip secondary dispatch table referencing thunks to
4254 -- predefined primitives.
4255
4256 pragma Assert (Has_Suffix (Node (ADT), 'Y'));
4257 Next_Elmt (ADT);
4258
4259 -- Skip secondary dispatch table referencing user-defined
4260 -- primitives covered by this interface.
4261
4262 pragma Assert (Has_Suffix (Node (ADT), 'D'));
4263 Next_Elmt (ADT);
4264
4265 -- Skip secondary dispatch table referencing predefined
4266 -- primitives.
4267
4268 pragma Assert (Has_Suffix (Node (ADT), 'Z'));
4269 Next_Elmt (ADT);
4270 end if;
4271 end loop;
4272
4273 pragma Assert (Is_Tag (Node (ADT)));
4274 return Node (ADT);
4275 end Search_Tag;
4276
4277 -- Start of processing for Collect_Interfaces_Info
4278
4279 begin
4280 Collect_Interfaces (T, Ifaces_List);
4281 Collect_Interface_Components (T, Comps_List);
4282
4283 -- Search for the record component and tag associated with each
4284 -- interface type of T.
4285
4286 Components_List := New_Elmt_List;
4287 Tags_List := New_Elmt_List;
4288
4289 Iface_Elmt := First_Elmt (Ifaces_List);
4290 while Present (Iface_Elmt) loop
4291 Iface := Node (Iface_Elmt);
4292
4293 -- Associate the primary tag component and the primary dispatch table
4294 -- with all the interfaces that are parents of T
4295
4296 if Is_Ancestor (Iface, T, Use_Full_View => True) then
4297 Append_Elmt (First_Tag_Component (T), Components_List);
4298 Append_Elmt (Node (First_Elmt (Access_Disp_Table (T))), Tags_List);
4299
4300 -- Otherwise search for the tag component and secondary dispatch
4301 -- table of Iface
4302
4303 else
4304 Comp_Elmt := First_Elmt (Comps_List);
4305 while Present (Comp_Elmt) loop
4306 Comp_Iface := Related_Type (Node (Comp_Elmt));
4307
4308 if Comp_Iface = Iface
4309 or else Is_Ancestor (Iface, Comp_Iface, Use_Full_View => True)
4310 then
4311 Append_Elmt (Node (Comp_Elmt), Components_List);
4312 Append_Elmt (Search_Tag (Comp_Iface), Tags_List);
4313 exit;
4314 end if;
4315
4316 Next_Elmt (Comp_Elmt);
4317 end loop;
4318 pragma Assert (Present (Comp_Elmt));
4319 end if;
4320
4321 Next_Elmt (Iface_Elmt);
4322 end loop;
4323 end Collect_Interfaces_Info;
4324
4325 ---------------------
4326 -- Collect_Parents --
4327 ---------------------
4328
4329 procedure Collect_Parents
4330 (T : Entity_Id;
4331 List : out Elist_Id;
4332 Use_Full_View : Boolean := True)
4333 is
4334 Current_Typ : Entity_Id := T;
4335 Parent_Typ : Entity_Id;
4336
4337 begin
4338 List := New_Elmt_List;
4339
4340 -- No action if the if the type has no parents
4341
4342 if T = Etype (T) then
4343 return;
4344 end if;
4345
4346 loop
4347 Parent_Typ := Etype (Current_Typ);
4348
4349 if Is_Private_Type (Parent_Typ)
4350 and then Present (Full_View (Parent_Typ))
4351 and then Use_Full_View
4352 then
4353 Parent_Typ := Full_View (Base_Type (Parent_Typ));
4354 end if;
4355
4356 Append_Elmt (Parent_Typ, List);
4357
4358 exit when Parent_Typ = Current_Typ;
4359 Current_Typ := Parent_Typ;
4360 end loop;
4361 end Collect_Parents;
4362
4363 ----------------------------------
4364 -- Collect_Primitive_Operations --
4365 ----------------------------------
4366
4367 function Collect_Primitive_Operations (T : Entity_Id) return Elist_Id is
4368 B_Type : constant Entity_Id := Base_Type (T);
4369 B_Decl : constant Node_Id := Original_Node (Parent (B_Type));
4370 B_Scope : Entity_Id := Scope (B_Type);
4371 Op_List : Elist_Id;
4372 Formal : Entity_Id;
4373 Is_Prim : Boolean;
4374 Is_Type_In_Pkg : Boolean;
4375 Formal_Derived : Boolean := False;
4376 Id : Entity_Id;
4377
4378 function Match (E : Entity_Id) return Boolean;
4379 -- True if E's base type is B_Type, or E is of an anonymous access type
4380 -- and the base type of its designated type is B_Type.
4381
4382 -----------
4383 -- Match --
4384 -----------
4385
4386 function Match (E : Entity_Id) return Boolean is
4387 Etyp : Entity_Id := Etype (E);
4388
4389 begin
4390 if Ekind (Etyp) = E_Anonymous_Access_Type then
4391 Etyp := Designated_Type (Etyp);
4392 end if;
4393
4394 -- In Ada 2012 a primitive operation may have a formal of an
4395 -- incomplete view of the parent type.
4396
4397 return Base_Type (Etyp) = B_Type
4398 or else
4399 (Ada_Version >= Ada_2012
4400 and then Ekind (Etyp) = E_Incomplete_Type
4401 and then Full_View (Etyp) = B_Type);
4402 end Match;
4403
4404 -- Start of processing for Collect_Primitive_Operations
4405
4406 begin
4407 -- For tagged types, the primitive operations are collected as they
4408 -- are declared, and held in an explicit list which is simply returned.
4409
4410 if Is_Tagged_Type (B_Type) then
4411 return Primitive_Operations (B_Type);
4412
4413 -- An untagged generic type that is a derived type inherits the
4414 -- primitive operations of its parent type. Other formal types only
4415 -- have predefined operators, which are not explicitly represented.
4416
4417 elsif Is_Generic_Type (B_Type) then
4418 if Nkind (B_Decl) = N_Formal_Type_Declaration
4419 and then Nkind (Formal_Type_Definition (B_Decl)) =
4420 N_Formal_Derived_Type_Definition
4421 then
4422 Formal_Derived := True;
4423 else
4424 return New_Elmt_List;
4425 end if;
4426 end if;
4427
4428 Op_List := New_Elmt_List;
4429
4430 if B_Scope = Standard_Standard then
4431 if B_Type = Standard_String then
4432 Append_Elmt (Standard_Op_Concat, Op_List);
4433
4434 elsif B_Type = Standard_Wide_String then
4435 Append_Elmt (Standard_Op_Concatw, Op_List);
4436
4437 else
4438 null;
4439 end if;
4440
4441 -- Locate the primitive subprograms of the type
4442
4443 else
4444 -- The primitive operations appear after the base type, except
4445 -- if the derivation happens within the private part of B_Scope
4446 -- and the type is a private type, in which case both the type
4447 -- and some primitive operations may appear before the base
4448 -- type, and the list of candidates starts after the type.
4449
4450 if In_Open_Scopes (B_Scope)
4451 and then Scope (T) = B_Scope
4452 and then In_Private_Part (B_Scope)
4453 then
4454 Id := Next_Entity (T);
4455
4456 -- In Ada 2012, If the type has an incomplete partial view, there
4457 -- may be primitive operations declared before the full view, so
4458 -- we need to start scanning from the incomplete view, which is
4459 -- earlier on the entity chain.
4460
4461 elsif Nkind (Parent (B_Type)) = N_Full_Type_Declaration
4462 and then Present (Incomplete_View (Parent (B_Type)))
4463 then
4464 Id := Defining_Entity (Incomplete_View (Parent (B_Type)));
4465
4466 -- If T is a derived from a type with an incomplete view declared
4467 -- elsewhere, that incomplete view is irrelevant, we want the
4468 -- operations in the scope of T.
4469
4470 if Scope (Id) /= Scope (B_Type) then
4471 Id := Next_Entity (B_Type);
4472 end if;
4473
4474 else
4475 Id := Next_Entity (B_Type);
4476 end if;
4477
4478 -- Set flag if this is a type in a package spec
4479
4480 Is_Type_In_Pkg :=
4481 Is_Package_Or_Generic_Package (B_Scope)
4482 and then
4483 Nkind (Parent (Declaration_Node (First_Subtype (T)))) /=
4484 N_Package_Body;
4485
4486 while Present (Id) loop
4487
4488 -- Test whether the result type or any of the parameter types of
4489 -- each subprogram following the type match that type when the
4490 -- type is declared in a package spec, is a derived type, or the
4491 -- subprogram is marked as primitive. (The Is_Primitive test is
4492 -- needed to find primitives of nonderived types in declarative
4493 -- parts that happen to override the predefined "=" operator.)
4494
4495 -- Note that generic formal subprograms are not considered to be
4496 -- primitive operations and thus are never inherited.
4497
4498 if Is_Overloadable (Id)
4499 and then (Is_Type_In_Pkg
4500 or else Is_Derived_Type (B_Type)
4501 or else Is_Primitive (Id))
4502 and then Nkind (Parent (Parent (Id)))
4503 not in N_Formal_Subprogram_Declaration
4504 then
4505 Is_Prim := False;
4506
4507 if Match (Id) then
4508 Is_Prim := True;
4509
4510 else
4511 Formal := First_Formal (Id);
4512 while Present (Formal) loop
4513 if Match (Formal) then
4514 Is_Prim := True;
4515 exit;
4516 end if;
4517
4518 Next_Formal (Formal);
4519 end loop;
4520 end if;
4521
4522 -- For a formal derived type, the only primitives are the ones
4523 -- inherited from the parent type. Operations appearing in the
4524 -- package declaration are not primitive for it.
4525
4526 if Is_Prim
4527 and then (not Formal_Derived or else Present (Alias (Id)))
4528 then
4529 -- In the special case of an equality operator aliased to
4530 -- an overriding dispatching equality belonging to the same
4531 -- type, we don't include it in the list of primitives.
4532 -- This avoids inheriting multiple equality operators when
4533 -- deriving from untagged private types whose full type is
4534 -- tagged, which can otherwise cause ambiguities. Note that
4535 -- this should only happen for this kind of untagged parent
4536 -- type, since normally dispatching operations are inherited
4537 -- using the type's Primitive_Operations list.
4538
4539 if Chars (Id) = Name_Op_Eq
4540 and then Is_Dispatching_Operation (Id)
4541 and then Present (Alias (Id))
4542 and then Present (Overridden_Operation (Alias (Id)))
4543 and then Base_Type (Etype (First_Entity (Id))) =
4544 Base_Type (Etype (First_Entity (Alias (Id))))
4545 then
4546 null;
4547
4548 -- Include the subprogram in the list of primitives
4549
4550 else
4551 Append_Elmt (Id, Op_List);
4552 end if;
4553 end if;
4554 end if;
4555
4556 Next_Entity (Id);
4557
4558 -- For a type declared in System, some of its operations may
4559 -- appear in the target-specific extension to System.
4560
4561 if No (Id)
4562 and then B_Scope = RTU_Entity (System)
4563 and then Present_System_Aux
4564 then
4565 B_Scope := System_Aux_Id;
4566 Id := First_Entity (System_Aux_Id);
4567 end if;
4568 end loop;
4569 end if;
4570
4571 return Op_List;
4572 end Collect_Primitive_Operations;
4573
4574 -----------------------------------
4575 -- Compile_Time_Constraint_Error --
4576 -----------------------------------
4577
4578 function Compile_Time_Constraint_Error
4579 (N : Node_Id;
4580 Msg : String;
4581 Ent : Entity_Id := Empty;
4582 Loc : Source_Ptr := No_Location;
4583 Warn : Boolean := False) return Node_Id
4584 is
4585 Msgc : String (1 .. Msg'Length + 3);
4586 -- Copy of message, with room for possible ?? or << and ! at end
4587
4588 Msgl : Natural;
4589 Wmsg : Boolean;
4590 Eloc : Source_Ptr;
4591
4592 -- Start of processing for Compile_Time_Constraint_Error
4593
4594 begin
4595 -- If this is a warning, convert it into an error if we are in code
4596 -- subject to SPARK_Mode being set On, unless Warn is True to force a
4597 -- warning. The rationale is that a compile-time constraint error should
4598 -- lead to an error instead of a warning when SPARK_Mode is On, but in
4599 -- a few cases we prefer to issue a warning and generate both a suitable
4600 -- run-time error in GNAT and a suitable check message in GNATprove.
4601 -- Those cases are those that likely correspond to deactivated SPARK
4602 -- code, so that this kind of code can be compiled and analyzed instead
4603 -- of being rejected.
4604
4605 Error_Msg_Warn := Warn or SPARK_Mode /= On;
4606
4607 -- A static constraint error in an instance body is not a fatal error.
4608 -- we choose to inhibit the message altogether, because there is no
4609 -- obvious node (for now) on which to post it. On the other hand the
4610 -- offending node must be replaced with a constraint_error in any case.
4611
4612 -- No messages are generated if we already posted an error on this node
4613
4614 if not Error_Posted (N) then
4615 if Loc /= No_Location then
4616 Eloc := Loc;
4617 else
4618 Eloc := Sloc (N);
4619 end if;
4620
4621 -- Copy message to Msgc, converting any ? in the message into
4622 -- < instead, so that we have an error in GNATprove mode.
4623
4624 Msgl := Msg'Length;
4625
4626 for J in 1 .. Msgl loop
4627 if Msg (J) = '?' and then (J = 1 or else Msg (J) /= ''') then
4628 Msgc (J) := '<';
4629 else
4630 Msgc (J) := Msg (J);
4631 end if;
4632 end loop;
4633
4634 -- Message is a warning, even in Ada 95 case
4635
4636 if Msg (Msg'Last) = '?' or else Msg (Msg'Last) = '<' then
4637 Wmsg := True;
4638
4639 -- In Ada 83, all messages are warnings. In the private part and
4640 -- the body of an instance, constraint_checks are only warnings.
4641 -- We also make this a warning if the Warn parameter is set.
4642
4643 elsif Warn
4644 or else (Ada_Version = Ada_83 and then Comes_From_Source (N))
4645 then
4646 Msgl := Msgl + 1;
4647 Msgc (Msgl) := '<';
4648 Msgl := Msgl + 1;
4649 Msgc (Msgl) := '<';
4650 Wmsg := True;
4651
4652 elsif In_Instance_Not_Visible then
4653 Msgl := Msgl + 1;
4654 Msgc (Msgl) := '<';
4655 Msgl := Msgl + 1;
4656 Msgc (Msgl) := '<';
4657 Wmsg := True;
4658
4659 -- Otherwise we have a real error message (Ada 95 static case)
4660 -- and we make this an unconditional message. Note that in the
4661 -- warning case we do not make the message unconditional, it seems
4662 -- quite reasonable to delete messages like this (about exceptions
4663 -- that will be raised) in dead code.
4664
4665 else
4666 Wmsg := False;
4667 Msgl := Msgl + 1;
4668 Msgc (Msgl) := '!';
4669 end if;
4670
4671 -- One more test, skip the warning if the related expression is
4672 -- statically unevaluated, since we don't want to warn about what
4673 -- will happen when something is evaluated if it never will be
4674 -- evaluated.
4675
4676 if not Is_Statically_Unevaluated (N) then
4677 if Present (Ent) then
4678 Error_Msg_NEL (Msgc (1 .. Msgl), N, Ent, Eloc);
4679 else
4680 Error_Msg_NEL (Msgc (1 .. Msgl), N, Etype (N), Eloc);
4681 end if;
4682
4683 if Wmsg then
4684
4685 -- Check whether the context is an Init_Proc
4686
4687 if Inside_Init_Proc then
4688 declare
4689 Conc_Typ : constant Entity_Id :=
4690 Corresponding_Concurrent_Type
4691 (Entity (Parameter_Type (First
4692 (Parameter_Specifications
4693 (Parent (Current_Scope))))));
4694
4695 begin
4696 -- Don't complain if the corresponding concurrent type
4697 -- doesn't come from source (i.e. a single task/protected
4698 -- object).
4699
4700 if Present (Conc_Typ)
4701 and then not Comes_From_Source (Conc_Typ)
4702 then
4703 Error_Msg_NEL
4704 ("\& [<<", N, Standard_Constraint_Error, Eloc);
4705
4706 else
4707 if GNATprove_Mode then
4708 Error_Msg_NEL
4709 ("\& would have been raised for objects of this "
4710 & "type", N, Standard_Constraint_Error, Eloc);
4711 else
4712 Error_Msg_NEL
4713 ("\& will be raised for objects of this type??",
4714 N, Standard_Constraint_Error, Eloc);
4715 end if;
4716 end if;
4717 end;
4718
4719 else
4720 Error_Msg_NEL ("\& [<<", N, Standard_Constraint_Error, Eloc);
4721 end if;
4722
4723 else
4724 Error_Msg ("\static expression fails Constraint_Check", Eloc);
4725 Set_Error_Posted (N);
4726 end if;
4727 end if;
4728 end if;
4729
4730 return N;
4731 end Compile_Time_Constraint_Error;
4732
4733 -----------------------
4734 -- Conditional_Delay --
4735 -----------------------
4736
4737 procedure Conditional_Delay (New_Ent, Old_Ent : Entity_Id) is
4738 begin
4739 if Has_Delayed_Freeze (Old_Ent) and then not Is_Frozen (Old_Ent) then
4740 Set_Has_Delayed_Freeze (New_Ent);
4741 end if;
4742 end Conditional_Delay;
4743
4744 ----------------------------
4745 -- Contains_Refined_State --
4746 ----------------------------
4747
4748 function Contains_Refined_State (Prag : Node_Id) return Boolean is
4749 function Has_State_In_Dependency (List : Node_Id) return Boolean;
4750 -- Determine whether a dependency list mentions a state with a visible
4751 -- refinement.
4752
4753 function Has_State_In_Global (List : Node_Id) return Boolean;
4754 -- Determine whether a global list mentions a state with a visible
4755 -- refinement.
4756
4757 function Is_Refined_State (Item : Node_Id) return Boolean;
4758 -- Determine whether Item is a reference to an abstract state with a
4759 -- visible refinement.
4760
4761 -----------------------------
4762 -- Has_State_In_Dependency --
4763 -----------------------------
4764
4765 function Has_State_In_Dependency (List : Node_Id) return Boolean is
4766 Clause : Node_Id;
4767 Output : Node_Id;
4768
4769 begin
4770 -- A null dependency list does not mention any states
4771
4772 if Nkind (List) = N_Null then
4773 return False;
4774
4775 -- Dependency clauses appear as component associations of an
4776 -- aggregate.
4777
4778 elsif Nkind (List) = N_Aggregate
4779 and then Present (Component_Associations (List))
4780 then
4781 Clause := First (Component_Associations (List));
4782 while Present (Clause) loop
4783
4784 -- Inspect the outputs of a dependency clause
4785
4786 Output := First (Choices (Clause));
4787 while Present (Output) loop
4788 if Is_Refined_State (Output) then
4789 return True;
4790 end if;
4791
4792 Next (Output);
4793 end loop;
4794
4795 -- Inspect the outputs of a dependency clause
4796
4797 if Is_Refined_State (Expression (Clause)) then
4798 return True;
4799 end if;
4800
4801 Next (Clause);
4802 end loop;
4803
4804 -- If we get here, then none of the dependency clauses mention a
4805 -- state with visible refinement.
4806
4807 return False;
4808
4809 -- An illegal pragma managed to sneak in
4810
4811 else
4812 raise Program_Error;
4813 end if;
4814 end Has_State_In_Dependency;
4815
4816 -------------------------
4817 -- Has_State_In_Global --
4818 -------------------------
4819
4820 function Has_State_In_Global (List : Node_Id) return Boolean is
4821 Item : Node_Id;
4822
4823 begin
4824 -- A null global list does not mention any states
4825
4826 if Nkind (List) = N_Null then
4827 return False;
4828
4829 -- Simple global list or moded global list declaration
4830
4831 elsif Nkind (List) = N_Aggregate then
4832
4833 -- The declaration of a simple global list appear as a collection
4834 -- of expressions.
4835
4836 if Present (Expressions (List)) then
4837 Item := First (Expressions (List));
4838 while Present (Item) loop
4839 if Is_Refined_State (Item) then
4840 return True;
4841 end if;
4842
4843 Next (Item);
4844 end loop;
4845
4846 -- The declaration of a moded global list appears as a collection
4847 -- of component associations where individual choices denote
4848 -- modes.
4849
4850 else
4851 Item := First (Component_Associations (List));
4852 while Present (Item) loop
4853 if Has_State_In_Global (Expression (Item)) then
4854 return True;
4855 end if;
4856
4857 Next (Item);
4858 end loop;
4859 end if;
4860
4861 -- If we get here, then the simple/moded global list did not
4862 -- mention any states with a visible refinement.
4863
4864 return False;
4865
4866 -- Single global item declaration
4867
4868 elsif Is_Entity_Name (List) then
4869 return Is_Refined_State (List);
4870
4871 -- An illegal pragma managed to sneak in
4872
4873 else
4874 raise Program_Error;
4875 end if;
4876 end Has_State_In_Global;
4877
4878 ----------------------
4879 -- Is_Refined_State --
4880 ----------------------
4881
4882 function Is_Refined_State (Item : Node_Id) return Boolean is
4883 Elmt : Node_Id;
4884 Item_Id : Entity_Id;
4885
4886 begin
4887 if Nkind (Item) = N_Null then
4888 return False;
4889
4890 -- States cannot be subject to attribute 'Result. This case arises
4891 -- in dependency relations.
4892
4893 elsif Nkind (Item) = N_Attribute_Reference
4894 and then Attribute_Name (Item) = Name_Result
4895 then
4896 return False;
4897
4898 -- Multiple items appear as an aggregate. This case arises in
4899 -- dependency relations.
4900
4901 elsif Nkind (Item) = N_Aggregate
4902 and then Present (Expressions (Item))
4903 then
4904 Elmt := First (Expressions (Item));
4905 while Present (Elmt) loop
4906 if Is_Refined_State (Elmt) then
4907 return True;
4908 end if;
4909
4910 Next (Elmt);
4911 end loop;
4912
4913 -- If we get here, then none of the inputs or outputs reference a
4914 -- state with visible refinement.
4915
4916 return False;
4917
4918 -- Single item
4919
4920 else
4921 Item_Id := Entity_Of (Item);
4922
4923 return
4924 Present (Item_Id)
4925 and then Ekind (Item_Id) = E_Abstract_State
4926 and then Has_Visible_Refinement (Item_Id);
4927 end if;
4928 end Is_Refined_State;
4929
4930 -- Local variables
4931
4932 Arg : constant Node_Id :=
4933 Get_Pragma_Arg (First (Pragma_Argument_Associations (Prag)));
4934 Nam : constant Name_Id := Pragma_Name (Prag);
4935
4936 -- Start of processing for Contains_Refined_State
4937
4938 begin
4939 if Nam = Name_Depends then
4940 return Has_State_In_Dependency (Arg);
4941
4942 else pragma Assert (Nam = Name_Global);
4943 return Has_State_In_Global (Arg);
4944 end if;
4945 end Contains_Refined_State;
4946
4947 -------------------------
4948 -- Copy_Component_List --
4949 -------------------------
4950
4951 function Copy_Component_List
4952 (R_Typ : Entity_Id;
4953 Loc : Source_Ptr) return List_Id
4954 is
4955 Comp : Node_Id;
4956 Comps : constant List_Id := New_List;
4957
4958 begin
4959 Comp := First_Component (Underlying_Type (R_Typ));
4960 while Present (Comp) loop
4961 if Comes_From_Source (Comp) then
4962 declare
4963 Comp_Decl : constant Node_Id := Declaration_Node (Comp);
4964 begin
4965 Append_To (Comps,
4966 Make_Component_Declaration (Loc,
4967 Defining_Identifier =>
4968 Make_Defining_Identifier (Loc, Chars (Comp)),
4969 Component_Definition =>
4970 New_Copy_Tree
4971 (Component_Definition (Comp_Decl), New_Sloc => Loc)));
4972 end;
4973 end if;
4974
4975 Next_Component (Comp);
4976 end loop;
4977
4978 return Comps;
4979 end Copy_Component_List;
4980
4981 -------------------------
4982 -- Copy_Parameter_List --
4983 -------------------------
4984
4985 function Copy_Parameter_List (Subp_Id : Entity_Id) return List_Id is
4986 Loc : constant Source_Ptr := Sloc (Subp_Id);
4987 Plist : List_Id;
4988 Formal : Entity_Id;
4989
4990 begin
4991 if No (First_Formal (Subp_Id)) then
4992 return No_List;
4993 else
4994 Plist := New_List;
4995 Formal := First_Formal (Subp_Id);
4996 while Present (Formal) loop
4997 Append_To (Plist,
4998 Make_Parameter_Specification (Loc,
4999 Defining_Identifier =>
5000 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)),
5001 In_Present => In_Present (Parent (Formal)),
5002 Out_Present => Out_Present (Parent (Formal)),
5003 Parameter_Type =>
5004 New_Occurrence_Of (Etype (Formal), Loc),
5005 Expression =>
5006 New_Copy_Tree (Expression (Parent (Formal)))));
5007
5008 Next_Formal (Formal);
5009 end loop;
5010 end if;
5011
5012 return Plist;
5013 end Copy_Parameter_List;
5014
5015 --------------------------
5016 -- Copy_Subprogram_Spec --
5017 --------------------------
5018
5019 function Copy_Subprogram_Spec (Spec : Node_Id) return Node_Id is
5020 Def_Id : Node_Id;
5021 Formal_Spec : Node_Id;
5022 Result : Node_Id;
5023
5024 begin
5025 -- The structure of the original tree must be replicated without any
5026 -- alterations. Use New_Copy_Tree for this purpose.
5027
5028 Result := New_Copy_Tree (Spec);
5029
5030 -- Create a new entity for the defining unit name
5031
5032 Def_Id := Defining_Unit_Name (Result);
5033 Set_Defining_Unit_Name (Result,
5034 Make_Defining_Identifier (Sloc (Def_Id), Chars (Def_Id)));
5035
5036 -- Create new entities for the formal parameters
5037
5038 if Present (Parameter_Specifications (Result)) then
5039 Formal_Spec := First (Parameter_Specifications (Result));
5040 while Present (Formal_Spec) loop
5041 Def_Id := Defining_Identifier (Formal_Spec);
5042 Set_Defining_Identifier (Formal_Spec,
5043 Make_Defining_Identifier (Sloc (Def_Id), Chars (Def_Id)));
5044
5045 Next (Formal_Spec);
5046 end loop;
5047 end if;
5048
5049 return Result;
5050 end Copy_Subprogram_Spec;
5051
5052 --------------------------------
5053 -- Corresponding_Generic_Type --
5054 --------------------------------
5055
5056 function Corresponding_Generic_Type (T : Entity_Id) return Entity_Id is
5057 Inst : Entity_Id;
5058 Gen : Entity_Id;
5059 Typ : Entity_Id;
5060
5061 begin
5062 if not Is_Generic_Actual_Type (T) then
5063 return Any_Type;
5064
5065 -- If the actual is the actual of an enclosing instance, resolution
5066 -- was correct in the generic.
5067
5068 elsif Nkind (Parent (T)) = N_Subtype_Declaration
5069 and then Is_Entity_Name (Subtype_Indication (Parent (T)))
5070 and then
5071 Is_Generic_Actual_Type (Entity (Subtype_Indication (Parent (T))))
5072 then
5073 return Any_Type;
5074
5075 else
5076 Inst := Scope (T);
5077
5078 if Is_Wrapper_Package (Inst) then
5079 Inst := Related_Instance (Inst);
5080 end if;
5081
5082 Gen :=
5083 Generic_Parent
5084 (Specification (Unit_Declaration_Node (Inst)));
5085
5086 -- Generic actual has the same name as the corresponding formal
5087
5088 Typ := First_Entity (Gen);
5089 while Present (Typ) loop
5090 if Chars (Typ) = Chars (T) then
5091 return Typ;
5092 end if;
5093
5094 Next_Entity (Typ);
5095 end loop;
5096
5097 return Any_Type;
5098 end if;
5099 end Corresponding_Generic_Type;
5100
5101 --------------------
5102 -- Current_Entity --
5103 --------------------
5104
5105 -- The currently visible definition for a given identifier is the
5106 -- one most chained at the start of the visibility chain, i.e. the
5107 -- one that is referenced by the Node_Id value of the name of the
5108 -- given identifier.
5109
5110 function Current_Entity (N : Node_Id) return Entity_Id is
5111 begin
5112 return Get_Name_Entity_Id (Chars (N));
5113 end Current_Entity;
5114
5115 -----------------------------
5116 -- Current_Entity_In_Scope --
5117 -----------------------------
5118
5119 function Current_Entity_In_Scope (N : Node_Id) return Entity_Id is
5120 E : Entity_Id;
5121 CS : constant Entity_Id := Current_Scope;
5122
5123 Transient_Case : constant Boolean := Scope_Is_Transient;
5124
5125 begin
5126 E := Get_Name_Entity_Id (Chars (N));
5127 while Present (E)
5128 and then Scope (E) /= CS
5129 and then (not Transient_Case or else Scope (E) /= Scope (CS))
5130 loop
5131 E := Homonym (E);
5132 end loop;
5133
5134 return E;
5135 end Current_Entity_In_Scope;
5136
5137 -------------------
5138 -- Current_Scope --
5139 -------------------
5140
5141 function Current_Scope return Entity_Id is
5142 begin
5143 if Scope_Stack.Last = -1 then
5144 return Standard_Standard;
5145 else
5146 declare
5147 C : constant Entity_Id :=
5148 Scope_Stack.Table (Scope_Stack.Last).Entity;
5149 begin
5150 if Present (C) then
5151 return C;
5152 else
5153 return Standard_Standard;
5154 end if;
5155 end;
5156 end if;
5157 end Current_Scope;
5158
5159 ----------------------------
5160 -- Current_Scope_No_Loops --
5161 ----------------------------
5162
5163 function Current_Scope_No_Loops return Entity_Id is
5164 S : Entity_Id;
5165
5166 begin
5167 -- Examine the scope stack starting from the current scope and skip any
5168 -- internally generated loops.
5169
5170 S := Current_Scope;
5171 while Present (S) and then S /= Standard_Standard loop
5172 if Ekind (S) = E_Loop and then not Comes_From_Source (S) then
5173 S := Scope (S);
5174 else
5175 exit;
5176 end if;
5177 end loop;
5178
5179 return S;
5180 end Current_Scope_No_Loops;
5181
5182 ------------------------
5183 -- Current_Subprogram --
5184 ------------------------
5185
5186 function Current_Subprogram return Entity_Id is
5187 Scop : constant Entity_Id := Current_Scope;
5188 begin
5189 if Is_Subprogram_Or_Generic_Subprogram (Scop) then
5190 return Scop;
5191 else
5192 return Enclosing_Subprogram (Scop);
5193 end if;
5194 end Current_Subprogram;
5195
5196 ----------------------------------
5197 -- Deepest_Type_Access_Level --
5198 ----------------------------------
5199
5200 function Deepest_Type_Access_Level (Typ : Entity_Id) return Uint is
5201 begin
5202 if Ekind (Typ) = E_Anonymous_Access_Type
5203 and then not Is_Local_Anonymous_Access (Typ)
5204 and then Nkind (Associated_Node_For_Itype (Typ)) = N_Object_Declaration
5205 then
5206 -- Typ is the type of an Ada 2012 stand-alone object of an anonymous
5207 -- access type.
5208
5209 return
5210 Scope_Depth (Enclosing_Dynamic_Scope
5211 (Defining_Identifier
5212 (Associated_Node_For_Itype (Typ))));
5213
5214 -- For generic formal type, return Int'Last (infinite).
5215 -- See comment preceding Is_Generic_Type call in Type_Access_Level.
5216
5217 elsif Is_Generic_Type (Root_Type (Typ)) then
5218 return UI_From_Int (Int'Last);
5219
5220 else
5221 return Type_Access_Level (Typ);
5222 end if;
5223 end Deepest_Type_Access_Level;
5224
5225 ---------------------
5226 -- Defining_Entity --
5227 ---------------------
5228
5229 function Defining_Entity
5230 (N : Node_Id;
5231 Empty_On_Errors : Boolean := False) return Entity_Id
5232 is
5233 Err : Entity_Id := Empty;
5234
5235 begin
5236 case Nkind (N) is
5237 when N_Abstract_Subprogram_Declaration |
5238 N_Expression_Function |
5239 N_Formal_Subprogram_Declaration |
5240 N_Generic_Package_Declaration |
5241 N_Generic_Subprogram_Declaration |
5242 N_Package_Declaration |
5243 N_Subprogram_Body |
5244 N_Subprogram_Body_Stub |
5245 N_Subprogram_Declaration |
5246 N_Subprogram_Renaming_Declaration
5247 =>
5248 return Defining_Entity (Specification (N));
5249
5250 when N_Component_Declaration |
5251 N_Defining_Program_Unit_Name |
5252 N_Discriminant_Specification |
5253 N_Entry_Body |
5254 N_Entry_Declaration |
5255 N_Entry_Index_Specification |
5256 N_Exception_Declaration |
5257 N_Exception_Renaming_Declaration |
5258 N_Formal_Object_Declaration |
5259 N_Formal_Package_Declaration |
5260 N_Formal_Type_Declaration |
5261 N_Full_Type_Declaration |
5262 N_Implicit_Label_Declaration |
5263 N_Incomplete_Type_Declaration |
5264 N_Loop_Parameter_Specification |
5265 N_Number_Declaration |
5266 N_Object_Declaration |
5267 N_Object_Renaming_Declaration |
5268 N_Package_Body_Stub |
5269 N_Parameter_Specification |
5270 N_Private_Extension_Declaration |
5271 N_Private_Type_Declaration |
5272 N_Protected_Body |
5273 N_Protected_Body_Stub |
5274 N_Protected_Type_Declaration |
5275 N_Single_Protected_Declaration |
5276 N_Single_Task_Declaration |
5277 N_Subtype_Declaration |
5278 N_Task_Body |
5279 N_Task_Body_Stub |
5280 N_Task_Type_Declaration
5281 =>
5282 return Defining_Identifier (N);
5283
5284 when N_Subunit =>
5285 return Defining_Entity (Proper_Body (N));
5286
5287 when N_Function_Instantiation |
5288 N_Function_Specification |
5289 N_Generic_Function_Renaming_Declaration |
5290 N_Generic_Package_Renaming_Declaration |
5291 N_Generic_Procedure_Renaming_Declaration |
5292 N_Package_Body |
5293 N_Package_Instantiation |
5294 N_Package_Renaming_Declaration |
5295 N_Package_Specification |
5296 N_Procedure_Instantiation |
5297 N_Procedure_Specification
5298 =>
5299 declare
5300 Nam : constant Node_Id := Defining_Unit_Name (N);
5301
5302 begin
5303 if Nkind (Nam) in N_Entity then
5304 return Nam;
5305
5306 -- For Error, make up a name and attach to declaration so we
5307 -- can continue semantic analysis.
5308
5309 elsif Nam = Error then
5310 if Empty_On_Errors then
5311 return Empty;
5312 else
5313 Err := Make_Temporary (Sloc (N), 'T');
5314 Set_Defining_Unit_Name (N, Err);
5315
5316 return Err;
5317 end if;
5318
5319 -- If not an entity, get defining identifier
5320
5321 else
5322 return Defining_Identifier (Nam);
5323 end if;
5324 end;
5325
5326 when N_Block_Statement |
5327 N_Loop_Statement =>
5328 return Entity (Identifier (N));
5329
5330 when others =>
5331 if Empty_On_Errors then
5332 return Empty;
5333 else
5334 raise Program_Error;
5335 end if;
5336
5337 end case;
5338 end Defining_Entity;
5339
5340 --------------------------
5341 -- Denotes_Discriminant --
5342 --------------------------
5343
5344 function Denotes_Discriminant
5345 (N : Node_Id;
5346 Check_Concurrent : Boolean := False) return Boolean
5347 is
5348 E : Entity_Id;
5349
5350 begin
5351 if not Is_Entity_Name (N) or else No (Entity (N)) then
5352 return False;
5353 else
5354 E := Entity (N);
5355 end if;
5356
5357 -- If we are checking for a protected type, the discriminant may have
5358 -- been rewritten as the corresponding discriminal of the original type
5359 -- or of the corresponding concurrent record, depending on whether we
5360 -- are in the spec or body of the protected type.
5361
5362 return Ekind (E) = E_Discriminant
5363 or else
5364 (Check_Concurrent
5365 and then Ekind (E) = E_In_Parameter
5366 and then Present (Discriminal_Link (E))
5367 and then
5368 (Is_Concurrent_Type (Scope (Discriminal_Link (E)))
5369 or else
5370 Is_Concurrent_Record_Type (Scope (Discriminal_Link (E)))));
5371 end Denotes_Discriminant;
5372
5373 -------------------------
5374 -- Denotes_Same_Object --
5375 -------------------------
5376
5377 function Denotes_Same_Object (A1, A2 : Node_Id) return Boolean is
5378 Obj1 : Node_Id := A1;
5379 Obj2 : Node_Id := A2;
5380
5381 function Has_Prefix (N : Node_Id) return Boolean;
5382 -- Return True if N has attribute Prefix
5383
5384 function Is_Renaming (N : Node_Id) return Boolean;
5385 -- Return true if N names a renaming entity
5386
5387 function Is_Valid_Renaming (N : Node_Id) return Boolean;
5388 -- For renamings, return False if the prefix of any dereference within
5389 -- the renamed object_name is a variable, or any expression within the
5390 -- renamed object_name contains references to variables or calls on
5391 -- nonstatic functions; otherwise return True (RM 6.4.1(6.10/3))
5392
5393 ----------------
5394 -- Has_Prefix --
5395 ----------------
5396
5397 function Has_Prefix (N : Node_Id) return Boolean is
5398 begin
5399 return
5400 Nkind_In (N,
5401 N_Attribute_Reference,
5402 N_Expanded_Name,
5403 N_Explicit_Dereference,
5404 N_Indexed_Component,
5405 N_Reference,
5406 N_Selected_Component,
5407 N_Slice);
5408 end Has_Prefix;
5409
5410 -----------------
5411 -- Is_Renaming --
5412 -----------------
5413
5414 function Is_Renaming (N : Node_Id) return Boolean is
5415 begin
5416 return Is_Entity_Name (N)
5417 and then Present (Renamed_Entity (Entity (N)));
5418 end Is_Renaming;
5419
5420 -----------------------
5421 -- Is_Valid_Renaming --
5422 -----------------------
5423
5424 function Is_Valid_Renaming (N : Node_Id) return Boolean is
5425
5426 function Check_Renaming (N : Node_Id) return Boolean;
5427 -- Recursive function used to traverse all the prefixes of N
5428
5429 function Check_Renaming (N : Node_Id) return Boolean is
5430 begin
5431 if Is_Renaming (N)
5432 and then not Check_Renaming (Renamed_Entity (Entity (N)))
5433 then
5434 return False;
5435 end if;
5436
5437 if Nkind (N) = N_Indexed_Component then
5438 declare
5439 Indx : Node_Id;
5440
5441 begin
5442 Indx := First (Expressions (N));
5443 while Present (Indx) loop
5444 if not Is_OK_Static_Expression (Indx) then
5445 return False;
5446 end if;
5447
5448 Next_Index (Indx);
5449 end loop;
5450 end;
5451 end if;
5452
5453 if Has_Prefix (N) then
5454 declare
5455 P : constant Node_Id := Prefix (N);
5456
5457 begin
5458 if Nkind (N) = N_Explicit_Dereference
5459 and then Is_Variable (P)
5460 then
5461 return False;
5462
5463 elsif Is_Entity_Name (P)
5464 and then Ekind (Entity (P)) = E_Function
5465 then
5466 return False;
5467
5468 elsif Nkind (P) = N_Function_Call then
5469 return False;
5470 end if;
5471
5472 -- Recursion to continue traversing the prefix of the
5473 -- renaming expression
5474
5475 return Check_Renaming (P);
5476 end;
5477 end if;
5478
5479 return True;
5480 end Check_Renaming;
5481
5482 -- Start of processing for Is_Valid_Renaming
5483
5484 begin
5485 return Check_Renaming (N);
5486 end Is_Valid_Renaming;
5487
5488 -- Start of processing for Denotes_Same_Object
5489
5490 begin
5491 -- Both names statically denote the same stand-alone object or parameter
5492 -- (RM 6.4.1(6.5/3))
5493
5494 if Is_Entity_Name (Obj1)
5495 and then Is_Entity_Name (Obj2)
5496 and then Entity (Obj1) = Entity (Obj2)
5497 then
5498 return True;
5499 end if;
5500
5501 -- For renamings, the prefix of any dereference within the renamed
5502 -- object_name is not a variable, and any expression within the
5503 -- renamed object_name contains no references to variables nor
5504 -- calls on nonstatic functions (RM 6.4.1(6.10/3)).
5505
5506 if Is_Renaming (Obj1) then
5507 if Is_Valid_Renaming (Obj1) then
5508 Obj1 := Renamed_Entity (Entity (Obj1));
5509 else
5510 return False;
5511 end if;
5512 end if;
5513
5514 if Is_Renaming (Obj2) then
5515 if Is_Valid_Renaming (Obj2) then
5516 Obj2 := Renamed_Entity (Entity (Obj2));
5517 else
5518 return False;
5519 end if;
5520 end if;
5521
5522 -- No match if not same node kind (such cases are handled by
5523 -- Denotes_Same_Prefix)
5524
5525 if Nkind (Obj1) /= Nkind (Obj2) then
5526 return False;
5527
5528 -- After handling valid renamings, one of the two names statically
5529 -- denoted a renaming declaration whose renamed object_name is known
5530 -- to denote the same object as the other (RM 6.4.1(6.10/3))
5531
5532 elsif Is_Entity_Name (Obj1) then
5533 if Is_Entity_Name (Obj2) then
5534 return Entity (Obj1) = Entity (Obj2);
5535 else
5536 return False;
5537 end if;
5538
5539 -- Both names are selected_components, their prefixes are known to
5540 -- denote the same object, and their selector_names denote the same
5541 -- component (RM 6.4.1(6.6/3)).
5542
5543 elsif Nkind (Obj1) = N_Selected_Component then
5544 return Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2))
5545 and then
5546 Entity (Selector_Name (Obj1)) = Entity (Selector_Name (Obj2));
5547
5548 -- Both names are dereferences and the dereferenced names are known to
5549 -- denote the same object (RM 6.4.1(6.7/3))
5550
5551 elsif Nkind (Obj1) = N_Explicit_Dereference then
5552 return Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2));
5553
5554 -- Both names are indexed_components, their prefixes are known to denote
5555 -- the same object, and each of the pairs of corresponding index values
5556 -- are either both static expressions with the same static value or both
5557 -- names that are known to denote the same object (RM 6.4.1(6.8/3))
5558
5559 elsif Nkind (Obj1) = N_Indexed_Component then
5560 if not Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2)) then
5561 return False;
5562 else
5563 declare
5564 Indx1 : Node_Id;
5565 Indx2 : Node_Id;
5566
5567 begin
5568 Indx1 := First (Expressions (Obj1));
5569 Indx2 := First (Expressions (Obj2));
5570 while Present (Indx1) loop
5571
5572 -- Indexes must denote the same static value or same object
5573
5574 if Is_OK_Static_Expression (Indx1) then
5575 if not Is_OK_Static_Expression (Indx2) then
5576 return False;
5577
5578 elsif Expr_Value (Indx1) /= Expr_Value (Indx2) then
5579 return False;
5580 end if;
5581
5582 elsif not Denotes_Same_Object (Indx1, Indx2) then
5583 return False;
5584 end if;
5585
5586 Next (Indx1);
5587 Next (Indx2);
5588 end loop;
5589
5590 return True;
5591 end;
5592 end if;
5593
5594 -- Both names are slices, their prefixes are known to denote the same
5595 -- object, and the two slices have statically matching index constraints
5596 -- (RM 6.4.1(6.9/3))
5597
5598 elsif Nkind (Obj1) = N_Slice
5599 and then Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2))
5600 then
5601 declare
5602 Lo1, Lo2, Hi1, Hi2 : Node_Id;
5603
5604 begin
5605 Get_Index_Bounds (Etype (Obj1), Lo1, Hi1);
5606 Get_Index_Bounds (Etype (Obj2), Lo2, Hi2);
5607
5608 -- Check whether bounds are statically identical. There is no
5609 -- attempt to detect partial overlap of slices.
5610
5611 return Denotes_Same_Object (Lo1, Lo2)
5612 and then
5613 Denotes_Same_Object (Hi1, Hi2);
5614 end;
5615
5616 -- In the recursion, literals appear as indexes
5617
5618 elsif Nkind (Obj1) = N_Integer_Literal
5619 and then
5620 Nkind (Obj2) = N_Integer_Literal
5621 then
5622 return Intval (Obj1) = Intval (Obj2);
5623
5624 else
5625 return False;
5626 end if;
5627 end Denotes_Same_Object;
5628
5629 -------------------------
5630 -- Denotes_Same_Prefix --
5631 -------------------------
5632
5633 function Denotes_Same_Prefix (A1, A2 : Node_Id) return Boolean is
5634 begin
5635 if Is_Entity_Name (A1) then
5636 if Nkind_In (A2, N_Selected_Component, N_Indexed_Component)
5637 and then not Is_Access_Type (Etype (A1))
5638 then
5639 return Denotes_Same_Object (A1, Prefix (A2))
5640 or else Denotes_Same_Prefix (A1, Prefix (A2));
5641 else
5642 return False;
5643 end if;
5644
5645 elsif Is_Entity_Name (A2) then
5646 return Denotes_Same_Prefix (A1 => A2, A2 => A1);
5647
5648 elsif Nkind_In (A1, N_Selected_Component, N_Indexed_Component, N_Slice)
5649 and then
5650 Nkind_In (A2, N_Selected_Component, N_Indexed_Component, N_Slice)
5651 then
5652 declare
5653 Root1, Root2 : Node_Id;
5654 Depth1, Depth2 : Nat := 0;
5655
5656 begin
5657 Root1 := Prefix (A1);
5658 while not Is_Entity_Name (Root1) loop
5659 if not Nkind_In
5660 (Root1, N_Selected_Component, N_Indexed_Component)
5661 then
5662 return False;
5663 else
5664 Root1 := Prefix (Root1);
5665 end if;
5666
5667 Depth1 := Depth1 + 1;
5668 end loop;
5669
5670 Root2 := Prefix (A2);
5671 while not Is_Entity_Name (Root2) loop
5672 if not Nkind_In (Root2, N_Selected_Component,
5673 N_Indexed_Component)
5674 then
5675 return False;
5676 else
5677 Root2 := Prefix (Root2);
5678 end if;
5679
5680 Depth2 := Depth2 + 1;
5681 end loop;
5682
5683 -- If both have the same depth and they do not denote the same
5684 -- object, they are disjoint and no warning is needed.
5685
5686 if Depth1 = Depth2 then
5687 return False;
5688
5689 elsif Depth1 > Depth2 then
5690 Root1 := Prefix (A1);
5691 for J in 1 .. Depth1 - Depth2 - 1 loop
5692 Root1 := Prefix (Root1);
5693 end loop;
5694
5695 return Denotes_Same_Object (Root1, A2);
5696
5697 else
5698 Root2 := Prefix (A2);
5699 for J in 1 .. Depth2 - Depth1 - 1 loop
5700 Root2 := Prefix (Root2);
5701 end loop;
5702
5703 return Denotes_Same_Object (A1, Root2);
5704 end if;
5705 end;
5706
5707 else
5708 return False;
5709 end if;
5710 end Denotes_Same_Prefix;
5711
5712 ----------------------
5713 -- Denotes_Variable --
5714 ----------------------
5715
5716 function Denotes_Variable (N : Node_Id) return Boolean is
5717 begin
5718 return Is_Variable (N) and then Paren_Count (N) = 0;
5719 end Denotes_Variable;
5720
5721 -----------------------------
5722 -- Depends_On_Discriminant --
5723 -----------------------------
5724
5725 function Depends_On_Discriminant (N : Node_Id) return Boolean is
5726 L : Node_Id;
5727 H : Node_Id;
5728
5729 begin
5730 Get_Index_Bounds (N, L, H);
5731 return Denotes_Discriminant (L) or else Denotes_Discriminant (H);
5732 end Depends_On_Discriminant;
5733
5734 -------------------------
5735 -- Designate_Same_Unit --
5736 -------------------------
5737
5738 function Designate_Same_Unit
5739 (Name1 : Node_Id;
5740 Name2 : Node_Id) return Boolean
5741 is
5742 K1 : constant Node_Kind := Nkind (Name1);
5743 K2 : constant Node_Kind := Nkind (Name2);
5744
5745 function Prefix_Node (N : Node_Id) return Node_Id;
5746 -- Returns the parent unit name node of a defining program unit name
5747 -- or the prefix if N is a selected component or an expanded name.
5748
5749 function Select_Node (N : Node_Id) return Node_Id;
5750 -- Returns the defining identifier node of a defining program unit
5751 -- name or the selector node if N is a selected component or an
5752 -- expanded name.
5753
5754 -----------------
5755 -- Prefix_Node --
5756 -----------------
5757
5758 function Prefix_Node (N : Node_Id) return Node_Id is
5759 begin
5760 if Nkind (N) = N_Defining_Program_Unit_Name then
5761 return Name (N);
5762 else
5763 return Prefix (N);
5764 end if;
5765 end Prefix_Node;
5766
5767 -----------------
5768 -- Select_Node --
5769 -----------------
5770
5771 function Select_Node (N : Node_Id) return Node_Id is
5772 begin
5773 if Nkind (N) = N_Defining_Program_Unit_Name then
5774 return Defining_Identifier (N);
5775 else
5776 return Selector_Name (N);
5777 end if;
5778 end Select_Node;
5779
5780 -- Start of processing for Designate_Same_Unit
5781
5782 begin
5783 if Nkind_In (K1, N_Identifier, N_Defining_Identifier)
5784 and then
5785 Nkind_In (K2, N_Identifier, N_Defining_Identifier)
5786 then
5787 return Chars (Name1) = Chars (Name2);
5788
5789 elsif Nkind_In (K1, N_Expanded_Name,
5790 N_Selected_Component,
5791 N_Defining_Program_Unit_Name)
5792 and then
5793 Nkind_In (K2, N_Expanded_Name,
5794 N_Selected_Component,
5795 N_Defining_Program_Unit_Name)
5796 then
5797 return
5798 (Chars (Select_Node (Name1)) = Chars (Select_Node (Name2)))
5799 and then
5800 Designate_Same_Unit (Prefix_Node (Name1), Prefix_Node (Name2));
5801
5802 else
5803 return False;
5804 end if;
5805 end Designate_Same_Unit;
5806
5807 ------------------------------------------
5808 -- function Dynamic_Accessibility_Level --
5809 ------------------------------------------
5810
5811 function Dynamic_Accessibility_Level (Expr : Node_Id) return Node_Id is
5812 E : Entity_Id;
5813 Loc : constant Source_Ptr := Sloc (Expr);
5814
5815 function Make_Level_Literal (Level : Uint) return Node_Id;
5816 -- Construct an integer literal representing an accessibility level
5817 -- with its type set to Natural.
5818
5819 ------------------------
5820 -- Make_Level_Literal --
5821 ------------------------
5822
5823 function Make_Level_Literal (Level : Uint) return Node_Id is
5824 Result : constant Node_Id := Make_Integer_Literal (Loc, Level);
5825 begin
5826 Set_Etype (Result, Standard_Natural);
5827 return Result;
5828 end Make_Level_Literal;
5829
5830 -- Start of processing for Dynamic_Accessibility_Level
5831
5832 begin
5833 if Is_Entity_Name (Expr) then
5834 E := Entity (Expr);
5835
5836 if Present (Renamed_Object (E)) then
5837 return Dynamic_Accessibility_Level (Renamed_Object (E));
5838 end if;
5839
5840 if Is_Formal (E) or else Ekind_In (E, E_Variable, E_Constant) then
5841 if Present (Extra_Accessibility (E)) then
5842 return New_Occurrence_Of (Extra_Accessibility (E), Loc);
5843 end if;
5844 end if;
5845 end if;
5846
5847 -- Unimplemented: Ptr.all'Access, where Ptr has Extra_Accessibility ???
5848
5849 case Nkind (Expr) is
5850
5851 -- For access discriminant, the level of the enclosing object
5852
5853 when N_Selected_Component =>
5854 if Ekind (Entity (Selector_Name (Expr))) = E_Discriminant
5855 and then Ekind (Etype (Entity (Selector_Name (Expr)))) =
5856 E_Anonymous_Access_Type
5857 then
5858 return Make_Level_Literal (Object_Access_Level (Expr));
5859 end if;
5860
5861 when N_Attribute_Reference =>
5862 case Get_Attribute_Id (Attribute_Name (Expr)) is
5863
5864 -- For X'Access, the level of the prefix X
5865
5866 when Attribute_Access =>
5867 return Make_Level_Literal
5868 (Object_Access_Level (Prefix (Expr)));
5869
5870 -- Treat the unchecked attributes as library-level
5871
5872 when Attribute_Unchecked_Access |
5873 Attribute_Unrestricted_Access =>
5874 return Make_Level_Literal (Scope_Depth (Standard_Standard));
5875
5876 -- No other access-valued attributes
5877
5878 when others =>
5879 raise Program_Error;
5880 end case;
5881
5882 when N_Allocator =>
5883
5884 -- Unimplemented: depends on context. As an actual parameter where
5885 -- formal type is anonymous, use
5886 -- Scope_Depth (Current_Scope) + 1.
5887 -- For other cases, see 3.10.2(14/3) and following. ???
5888
5889 null;
5890
5891 when N_Type_Conversion =>
5892 if not Is_Local_Anonymous_Access (Etype (Expr)) then
5893
5894 -- Handle type conversions introduced for a rename of an
5895 -- Ada 2012 stand-alone object of an anonymous access type.
5896
5897 return Dynamic_Accessibility_Level (Expression (Expr));
5898 end if;
5899
5900 when others =>
5901 null;
5902 end case;
5903
5904 return Make_Level_Literal (Type_Access_Level (Etype (Expr)));
5905 end Dynamic_Accessibility_Level;
5906
5907 -----------------------------------
5908 -- Effective_Extra_Accessibility --
5909 -----------------------------------
5910
5911 function Effective_Extra_Accessibility (Id : Entity_Id) return Entity_Id is
5912 begin
5913 if Present (Renamed_Object (Id))
5914 and then Is_Entity_Name (Renamed_Object (Id))
5915 then
5916 return Effective_Extra_Accessibility (Entity (Renamed_Object (Id)));
5917 else
5918 return Extra_Accessibility (Id);
5919 end if;
5920 end Effective_Extra_Accessibility;
5921
5922 -----------------------------
5923 -- Effective_Reads_Enabled --
5924 -----------------------------
5925
5926 function Effective_Reads_Enabled (Id : Entity_Id) return Boolean is
5927 begin
5928 return Has_Enabled_Property (Id, Name_Effective_Reads);
5929 end Effective_Reads_Enabled;
5930
5931 ------------------------------
5932 -- Effective_Writes_Enabled --
5933 ------------------------------
5934
5935 function Effective_Writes_Enabled (Id : Entity_Id) return Boolean is
5936 begin
5937 return Has_Enabled_Property (Id, Name_Effective_Writes);
5938 end Effective_Writes_Enabled;
5939
5940 ------------------------------
5941 -- Enclosing_Comp_Unit_Node --
5942 ------------------------------
5943
5944 function Enclosing_Comp_Unit_Node (N : Node_Id) return Node_Id is
5945 Current_Node : Node_Id;
5946
5947 begin
5948 Current_Node := N;
5949 while Present (Current_Node)
5950 and then Nkind (Current_Node) /= N_Compilation_Unit
5951 loop
5952 Current_Node := Parent (Current_Node);
5953 end loop;
5954
5955 if Nkind (Current_Node) /= N_Compilation_Unit then
5956 return Empty;
5957 else
5958 return Current_Node;
5959 end if;
5960 end Enclosing_Comp_Unit_Node;
5961
5962 --------------------------
5963 -- Enclosing_CPP_Parent --
5964 --------------------------
5965
5966 function Enclosing_CPP_Parent (Typ : Entity_Id) return Entity_Id is
5967 Parent_Typ : Entity_Id := Typ;
5968
5969 begin
5970 while not Is_CPP_Class (Parent_Typ)
5971 and then Etype (Parent_Typ) /= Parent_Typ
5972 loop
5973 Parent_Typ := Etype (Parent_Typ);
5974
5975 if Is_Private_Type (Parent_Typ) then
5976 Parent_Typ := Full_View (Base_Type (Parent_Typ));
5977 end if;
5978 end loop;
5979
5980 pragma Assert (Is_CPP_Class (Parent_Typ));
5981 return Parent_Typ;
5982 end Enclosing_CPP_Parent;
5983
5984 ---------------------------
5985 -- Enclosing_Declaration --
5986 ---------------------------
5987
5988 function Enclosing_Declaration (N : Node_Id) return Node_Id is
5989 Decl : Node_Id := N;
5990
5991 begin
5992 while Present (Decl)
5993 and then not (Nkind (Decl) in N_Declaration
5994 or else
5995 Nkind (Decl) in N_Later_Decl_Item)
5996 loop
5997 Decl := Parent (Decl);
5998 end loop;
5999
6000 return Decl;
6001 end Enclosing_Declaration;
6002
6003 ----------------------------
6004 -- Enclosing_Generic_Body --
6005 ----------------------------
6006
6007 function Enclosing_Generic_Body
6008 (N : Node_Id) return Node_Id
6009 is
6010 P : Node_Id;
6011 Decl : Node_Id;
6012 Spec : Node_Id;
6013
6014 begin
6015 P := Parent (N);
6016 while Present (P) loop
6017 if Nkind (P) = N_Package_Body
6018 or else Nkind (P) = N_Subprogram_Body
6019 then
6020 Spec := Corresponding_Spec (P);
6021
6022 if Present (Spec) then
6023 Decl := Unit_Declaration_Node (Spec);
6024
6025 if Nkind (Decl) = N_Generic_Package_Declaration
6026 or else Nkind (Decl) = N_Generic_Subprogram_Declaration
6027 then
6028 return P;
6029 end if;
6030 end if;
6031 end if;
6032
6033 P := Parent (P);
6034 end loop;
6035
6036 return Empty;
6037 end Enclosing_Generic_Body;
6038
6039 ----------------------------
6040 -- Enclosing_Generic_Unit --
6041 ----------------------------
6042
6043 function Enclosing_Generic_Unit
6044 (N : Node_Id) return Node_Id
6045 is
6046 P : Node_Id;
6047 Decl : Node_Id;
6048 Spec : Node_Id;
6049
6050 begin
6051 P := Parent (N);
6052 while Present (P) loop
6053 if Nkind (P) = N_Generic_Package_Declaration
6054 or else Nkind (P) = N_Generic_Subprogram_Declaration
6055 then
6056 return P;
6057
6058 elsif Nkind (P) = N_Package_Body
6059 or else Nkind (P) = N_Subprogram_Body
6060 then
6061 Spec := Corresponding_Spec (P);
6062
6063 if Present (Spec) then
6064 Decl := Unit_Declaration_Node (Spec);
6065
6066 if Nkind (Decl) = N_Generic_Package_Declaration
6067 or else Nkind (Decl) = N_Generic_Subprogram_Declaration
6068 then
6069 return Decl;
6070 end if;
6071 end if;
6072 end if;
6073
6074 P := Parent (P);
6075 end loop;
6076
6077 return Empty;
6078 end Enclosing_Generic_Unit;
6079
6080 -------------------------------
6081 -- Enclosing_Lib_Unit_Entity --
6082 -------------------------------
6083
6084 function Enclosing_Lib_Unit_Entity
6085 (E : Entity_Id := Current_Scope) return Entity_Id
6086 is
6087 Unit_Entity : Entity_Id;
6088
6089 begin
6090 -- Look for enclosing library unit entity by following scope links.
6091 -- Equivalent to, but faster than indexing through the scope stack.
6092
6093 Unit_Entity := E;
6094 while (Present (Scope (Unit_Entity))
6095 and then Scope (Unit_Entity) /= Standard_Standard)
6096 and not Is_Child_Unit (Unit_Entity)
6097 loop
6098 Unit_Entity := Scope (Unit_Entity);
6099 end loop;
6100
6101 return Unit_Entity;
6102 end Enclosing_Lib_Unit_Entity;
6103
6104 -----------------------------
6105 -- Enclosing_Lib_Unit_Node --
6106 -----------------------------
6107
6108 function Enclosing_Lib_Unit_Node (N : Node_Id) return Node_Id is
6109 Encl_Unit : Node_Id;
6110
6111 begin
6112 Encl_Unit := Enclosing_Comp_Unit_Node (N);
6113 while Present (Encl_Unit)
6114 and then Nkind (Unit (Encl_Unit)) = N_Subunit
6115 loop
6116 Encl_Unit := Library_Unit (Encl_Unit);
6117 end loop;
6118
6119 return Encl_Unit;
6120 end Enclosing_Lib_Unit_Node;
6121
6122 -----------------------
6123 -- Enclosing_Package --
6124 -----------------------
6125
6126 function Enclosing_Package (E : Entity_Id) return Entity_Id is
6127 Dynamic_Scope : constant Entity_Id := Enclosing_Dynamic_Scope (E);
6128
6129 begin
6130 if Dynamic_Scope = Standard_Standard then
6131 return Standard_Standard;
6132
6133 elsif Dynamic_Scope = Empty then
6134 return Empty;
6135
6136 elsif Ekind_In (Dynamic_Scope, E_Package, E_Package_Body,
6137 E_Generic_Package)
6138 then
6139 return Dynamic_Scope;
6140
6141 else
6142 return Enclosing_Package (Dynamic_Scope);
6143 end if;
6144 end Enclosing_Package;
6145
6146 -------------------------------------
6147 -- Enclosing_Package_Or_Subprogram --
6148 -------------------------------------
6149
6150 function Enclosing_Package_Or_Subprogram (E : Entity_Id) return Entity_Id is
6151 S : Entity_Id;
6152
6153 begin
6154 S := Scope (E);
6155 while Present (S) loop
6156 if Is_Package_Or_Generic_Package (S)
6157 or else Ekind (S) = E_Package_Body
6158 then
6159 return S;
6160
6161 elsif Is_Subprogram_Or_Generic_Subprogram (S)
6162 or else Ekind (S) = E_Subprogram_Body
6163 then
6164 return S;
6165
6166 else
6167 S := Scope (S);
6168 end if;
6169 end loop;
6170
6171 return Empty;
6172 end Enclosing_Package_Or_Subprogram;
6173
6174 --------------------------
6175 -- Enclosing_Subprogram --
6176 --------------------------
6177
6178 function Enclosing_Subprogram (E : Entity_Id) return Entity_Id is
6179 Dynamic_Scope : constant Entity_Id := Enclosing_Dynamic_Scope (E);
6180
6181 begin
6182 if Dynamic_Scope = Standard_Standard then
6183 return Empty;
6184
6185 elsif Dynamic_Scope = Empty then
6186 return Empty;
6187
6188 elsif Ekind (Dynamic_Scope) = E_Subprogram_Body then
6189 return Corresponding_Spec (Parent (Parent (Dynamic_Scope)));
6190
6191 elsif Ekind (Dynamic_Scope) = E_Block
6192 or else Ekind (Dynamic_Scope) = E_Return_Statement
6193 then
6194 return Enclosing_Subprogram (Dynamic_Scope);
6195
6196 elsif Ekind (Dynamic_Scope) = E_Task_Type then
6197 return Get_Task_Body_Procedure (Dynamic_Scope);
6198
6199 elsif Ekind (Dynamic_Scope) = E_Limited_Private_Type
6200 and then Present (Full_View (Dynamic_Scope))
6201 and then Ekind (Full_View (Dynamic_Scope)) = E_Task_Type
6202 then
6203 return Get_Task_Body_Procedure (Full_View (Dynamic_Scope));
6204
6205 -- No body is generated if the protected operation is eliminated
6206
6207 elsif Convention (Dynamic_Scope) = Convention_Protected
6208 and then not Is_Eliminated (Dynamic_Scope)
6209 and then Present (Protected_Body_Subprogram (Dynamic_Scope))
6210 then
6211 return Protected_Body_Subprogram (Dynamic_Scope);
6212
6213 else
6214 return Dynamic_Scope;
6215 end if;
6216 end Enclosing_Subprogram;
6217
6218 ------------------------
6219 -- Ensure_Freeze_Node --
6220 ------------------------
6221
6222 procedure Ensure_Freeze_Node (E : Entity_Id) is
6223 FN : Node_Id;
6224 begin
6225 if No (Freeze_Node (E)) then
6226 FN := Make_Freeze_Entity (Sloc (E));
6227 Set_Has_Delayed_Freeze (E);
6228 Set_Freeze_Node (E, FN);
6229 Set_Access_Types_To_Process (FN, No_Elist);
6230 Set_TSS_Elist (FN, No_Elist);
6231 Set_Entity (FN, E);
6232 end if;
6233 end Ensure_Freeze_Node;
6234
6235 ----------------
6236 -- Enter_Name --
6237 ----------------
6238
6239 procedure Enter_Name (Def_Id : Entity_Id) is
6240 C : constant Entity_Id := Current_Entity (Def_Id);
6241 E : constant Entity_Id := Current_Entity_In_Scope (Def_Id);
6242 S : constant Entity_Id := Current_Scope;
6243
6244 begin
6245 Generate_Definition (Def_Id);
6246
6247 -- Add new name to current scope declarations. Check for duplicate
6248 -- declaration, which may or may not be a genuine error.
6249
6250 if Present (E) then
6251
6252 -- Case of previous entity entered because of a missing declaration
6253 -- or else a bad subtype indication. Best is to use the new entity,
6254 -- and make the previous one invisible.
6255
6256 if Etype (E) = Any_Type then
6257 Set_Is_Immediately_Visible (E, False);
6258
6259 -- Case of renaming declaration constructed for package instances.
6260 -- if there is an explicit declaration with the same identifier,
6261 -- the renaming is not immediately visible any longer, but remains
6262 -- visible through selected component notation.
6263
6264 elsif Nkind (Parent (E)) = N_Package_Renaming_Declaration
6265 and then not Comes_From_Source (E)
6266 then
6267 Set_Is_Immediately_Visible (E, False);
6268
6269 -- The new entity may be the package renaming, which has the same
6270 -- same name as a generic formal which has been seen already.
6271
6272 elsif Nkind (Parent (Def_Id)) = N_Package_Renaming_Declaration
6273 and then not Comes_From_Source (Def_Id)
6274 then
6275 Set_Is_Immediately_Visible (E, False);
6276
6277 -- For a fat pointer corresponding to a remote access to subprogram,
6278 -- we use the same identifier as the RAS type, so that the proper
6279 -- name appears in the stub. This type is only retrieved through
6280 -- the RAS type and never by visibility, and is not added to the
6281 -- visibility list (see below).
6282
6283 elsif Nkind (Parent (Def_Id)) = N_Full_Type_Declaration
6284 and then Ekind (Def_Id) = E_Record_Type
6285 and then Present (Corresponding_Remote_Type (Def_Id))
6286 then
6287 null;
6288
6289 -- Case of an implicit operation or derived literal. The new entity
6290 -- hides the implicit one, which is removed from all visibility,
6291 -- i.e. the entity list of its scope, and homonym chain of its name.
6292
6293 elsif (Is_Overloadable (E) and then Is_Inherited_Operation (E))
6294 or else Is_Internal (E)
6295 then
6296 declare
6297 Prev : Entity_Id;
6298 Prev_Vis : Entity_Id;
6299 Decl : constant Node_Id := Parent (E);
6300
6301 begin
6302 -- If E is an implicit declaration, it cannot be the first
6303 -- entity in the scope.
6304
6305 Prev := First_Entity (Current_Scope);
6306 while Present (Prev) and then Next_Entity (Prev) /= E loop
6307 Next_Entity (Prev);
6308 end loop;
6309
6310 if No (Prev) then
6311
6312 -- If E is not on the entity chain of the current scope,
6313 -- it is an implicit declaration in the generic formal
6314 -- part of a generic subprogram. When analyzing the body,
6315 -- the generic formals are visible but not on the entity
6316 -- chain of the subprogram. The new entity will become
6317 -- the visible one in the body.
6318
6319 pragma Assert
6320 (Nkind (Parent (Decl)) = N_Generic_Subprogram_Declaration);
6321 null;
6322
6323 else
6324 Set_Next_Entity (Prev, Next_Entity (E));
6325
6326 if No (Next_Entity (Prev)) then
6327 Set_Last_Entity (Current_Scope, Prev);
6328 end if;
6329
6330 if E = Current_Entity (E) then
6331 Prev_Vis := Empty;
6332
6333 else
6334 Prev_Vis := Current_Entity (E);
6335 while Homonym (Prev_Vis) /= E loop
6336 Prev_Vis := Homonym (Prev_Vis);
6337 end loop;
6338 end if;
6339
6340 if Present (Prev_Vis) then
6341
6342 -- Skip E in the visibility chain
6343
6344 Set_Homonym (Prev_Vis, Homonym (E));
6345
6346 else
6347 Set_Name_Entity_Id (Chars (E), Homonym (E));
6348 end if;
6349 end if;
6350 end;
6351
6352 -- This section of code could use a comment ???
6353
6354 elsif Present (Etype (E))
6355 and then Is_Concurrent_Type (Etype (E))
6356 and then E = Def_Id
6357 then
6358 return;
6359
6360 -- If the homograph is a protected component renaming, it should not
6361 -- be hiding the current entity. Such renamings are treated as weak
6362 -- declarations.
6363
6364 elsif Is_Prival (E) then
6365 Set_Is_Immediately_Visible (E, False);
6366
6367 -- In this case the current entity is a protected component renaming.
6368 -- Perform minimal decoration by setting the scope and return since
6369 -- the prival should not be hiding other visible entities.
6370
6371 elsif Is_Prival (Def_Id) then
6372 Set_Scope (Def_Id, Current_Scope);
6373 return;
6374
6375 -- Analogous to privals, the discriminal generated for an entry index
6376 -- parameter acts as a weak declaration. Perform minimal decoration
6377 -- to avoid bogus errors.
6378
6379 elsif Is_Discriminal (Def_Id)
6380 and then Ekind (Discriminal_Link (Def_Id)) = E_Entry_Index_Parameter
6381 then
6382 Set_Scope (Def_Id, Current_Scope);
6383 return;
6384
6385 -- In the body or private part of an instance, a type extension may
6386 -- introduce a component with the same name as that of an actual. The
6387 -- legality rule is not enforced, but the semantics of the full type
6388 -- with two components of same name are not clear at this point???
6389
6390 elsif In_Instance_Not_Visible then
6391 null;
6392
6393 -- When compiling a package body, some child units may have become
6394 -- visible. They cannot conflict with local entities that hide them.
6395
6396 elsif Is_Child_Unit (E)
6397 and then In_Open_Scopes (Scope (E))
6398 and then not Is_Immediately_Visible (E)
6399 then
6400 null;
6401
6402 -- Conversely, with front-end inlining we may compile the parent body
6403 -- first, and a child unit subsequently. The context is now the
6404 -- parent spec, and body entities are not visible.
6405
6406 elsif Is_Child_Unit (Def_Id)
6407 and then Is_Package_Body_Entity (E)
6408 and then not In_Package_Body (Current_Scope)
6409 then
6410 null;
6411
6412 -- Case of genuine duplicate declaration
6413
6414 else
6415 Error_Msg_Sloc := Sloc (E);
6416
6417 -- If the previous declaration is an incomplete type declaration
6418 -- this may be an attempt to complete it with a private type. The
6419 -- following avoids confusing cascaded errors.
6420
6421 if Nkind (Parent (E)) = N_Incomplete_Type_Declaration
6422 and then Nkind (Parent (Def_Id)) = N_Private_Type_Declaration
6423 then
6424 Error_Msg_N
6425 ("incomplete type cannot be completed with a private " &
6426 "declaration", Parent (Def_Id));
6427 Set_Is_Immediately_Visible (E, False);
6428 Set_Full_View (E, Def_Id);
6429
6430 -- An inherited component of a record conflicts with a new
6431 -- discriminant. The discriminant is inserted first in the scope,
6432 -- but the error should be posted on it, not on the component.
6433
6434 elsif Ekind (E) = E_Discriminant
6435 and then Present (Scope (Def_Id))
6436 and then Scope (Def_Id) /= Current_Scope
6437 then
6438 Error_Msg_Sloc := Sloc (Def_Id);
6439 Error_Msg_N ("& conflicts with declaration#", E);
6440 return;
6441
6442 -- If the name of the unit appears in its own context clause, a
6443 -- dummy package with the name has already been created, and the
6444 -- error emitted. Try to continue quietly.
6445
6446 elsif Error_Posted (E)
6447 and then Sloc (E) = No_Location
6448 and then Nkind (Parent (E)) = N_Package_Specification
6449 and then Current_Scope = Standard_Standard
6450 then
6451 Set_Scope (Def_Id, Current_Scope);
6452 return;
6453
6454 else
6455 Error_Msg_N ("& conflicts with declaration#", Def_Id);
6456
6457 -- Avoid cascaded messages with duplicate components in
6458 -- derived types.
6459
6460 if Ekind_In (E, E_Component, E_Discriminant) then
6461 return;
6462 end if;
6463 end if;
6464
6465 if Nkind (Parent (Parent (Def_Id))) =
6466 N_Generic_Subprogram_Declaration
6467 and then Def_Id =
6468 Defining_Entity (Specification (Parent (Parent (Def_Id))))
6469 then
6470 Error_Msg_N ("\generic units cannot be overloaded", Def_Id);
6471 end if;
6472
6473 -- If entity is in standard, then we are in trouble, because it
6474 -- means that we have a library package with a duplicated name.
6475 -- That's hard to recover from, so abort.
6476
6477 if S = Standard_Standard then
6478 raise Unrecoverable_Error;
6479
6480 -- Otherwise we continue with the declaration. Having two
6481 -- identical declarations should not cause us too much trouble.
6482
6483 else
6484 null;
6485 end if;
6486 end if;
6487 end if;
6488
6489 -- If we fall through, declaration is OK, at least OK enough to continue
6490
6491 -- If Def_Id is a discriminant or a record component we are in the midst
6492 -- of inheriting components in a derived record definition. Preserve
6493 -- their Ekind and Etype.
6494
6495 if Ekind_In (Def_Id, E_Discriminant, E_Component) then
6496 null;
6497
6498 -- If a type is already set, leave it alone (happens when a type
6499 -- declaration is reanalyzed following a call to the optimizer).
6500
6501 elsif Present (Etype (Def_Id)) then
6502 null;
6503
6504 -- Otherwise, the kind E_Void insures that premature uses of the entity
6505 -- will be detected. Any_Type insures that no cascaded errors will occur
6506
6507 else
6508 Set_Ekind (Def_Id, E_Void);
6509 Set_Etype (Def_Id, Any_Type);
6510 end if;
6511
6512 -- Inherited discriminants and components in derived record types are
6513 -- immediately visible. Itypes are not.
6514
6515 -- Unless the Itype is for a record type with a corresponding remote
6516 -- type (what is that about, it was not commented ???)
6517
6518 if Ekind_In (Def_Id, E_Discriminant, E_Component)
6519 or else
6520 ((not Is_Record_Type (Def_Id)
6521 or else No (Corresponding_Remote_Type (Def_Id)))
6522 and then not Is_Itype (Def_Id))
6523 then
6524 Set_Is_Immediately_Visible (Def_Id);
6525 Set_Current_Entity (Def_Id);
6526 end if;
6527
6528 Set_Homonym (Def_Id, C);
6529 Append_Entity (Def_Id, S);
6530 Set_Public_Status (Def_Id);
6531
6532 -- Declaring a homonym is not allowed in SPARK ...
6533
6534 if Present (C) and then Restriction_Check_Required (SPARK_05) then
6535 declare
6536 Enclosing_Subp : constant Node_Id := Enclosing_Subprogram (Def_Id);
6537 Enclosing_Pack : constant Node_Id := Enclosing_Package (Def_Id);
6538 Other_Scope : constant Node_Id := Enclosing_Dynamic_Scope (C);
6539
6540 begin
6541 -- ... unless the new declaration is in a subprogram, and the
6542 -- visible declaration is a variable declaration or a parameter
6543 -- specification outside that subprogram.
6544
6545 if Present (Enclosing_Subp)
6546 and then Nkind_In (Parent (C), N_Object_Declaration,
6547 N_Parameter_Specification)
6548 and then not Scope_Within_Or_Same (Other_Scope, Enclosing_Subp)
6549 then
6550 null;
6551
6552 -- ... or the new declaration is in a package, and the visible
6553 -- declaration occurs outside that package.
6554
6555 elsif Present (Enclosing_Pack)
6556 and then not Scope_Within_Or_Same (Other_Scope, Enclosing_Pack)
6557 then
6558 null;
6559
6560 -- ... or the new declaration is a component declaration in a
6561 -- record type definition.
6562
6563 elsif Nkind (Parent (Def_Id)) = N_Component_Declaration then
6564 null;
6565
6566 -- Don't issue error for non-source entities
6567
6568 elsif Comes_From_Source (Def_Id)
6569 and then Comes_From_Source (C)
6570 then
6571 Error_Msg_Sloc := Sloc (C);
6572 Check_SPARK_05_Restriction
6573 ("redeclaration of identifier &#", Def_Id);
6574 end if;
6575 end;
6576 end if;
6577
6578 -- Warn if new entity hides an old one
6579
6580 if Warn_On_Hiding and then Present (C)
6581
6582 -- Don't warn for record components since they always have a well
6583 -- defined scope which does not confuse other uses. Note that in
6584 -- some cases, Ekind has not been set yet.
6585
6586 and then Ekind (C) /= E_Component
6587 and then Ekind (C) /= E_Discriminant
6588 and then Nkind (Parent (C)) /= N_Component_Declaration
6589 and then Ekind (Def_Id) /= E_Component
6590 and then Ekind (Def_Id) /= E_Discriminant
6591 and then Nkind (Parent (Def_Id)) /= N_Component_Declaration
6592
6593 -- Don't warn for one character variables. It is too common to use
6594 -- such variables as locals and will just cause too many false hits.
6595
6596 and then Length_Of_Name (Chars (C)) /= 1
6597
6598 -- Don't warn for non-source entities
6599
6600 and then Comes_From_Source (C)
6601 and then Comes_From_Source (Def_Id)
6602
6603 -- Don't warn unless entity in question is in extended main source
6604
6605 and then In_Extended_Main_Source_Unit (Def_Id)
6606
6607 -- Finally, the hidden entity must be either immediately visible or
6608 -- use visible (i.e. from a used package).
6609
6610 and then
6611 (Is_Immediately_Visible (C)
6612 or else
6613 Is_Potentially_Use_Visible (C))
6614 then
6615 Error_Msg_Sloc := Sloc (C);
6616 Error_Msg_N ("declaration hides &#?h?", Def_Id);
6617 end if;
6618 end Enter_Name;
6619
6620 ---------------
6621 -- Entity_Of --
6622 ---------------
6623
6624 function Entity_Of (N : Node_Id) return Entity_Id is
6625 Id : Entity_Id;
6626
6627 begin
6628 Id := Empty;
6629
6630 if Is_Entity_Name (N) then
6631 Id := Entity (N);
6632
6633 -- Follow a possible chain of renamings to reach the root renamed
6634 -- object.
6635
6636 while Present (Id)
6637 and then Is_Object (Id)
6638 and then Present (Renamed_Object (Id))
6639 loop
6640 if Is_Entity_Name (Renamed_Object (Id)) then
6641 Id := Entity (Renamed_Object (Id));
6642 else
6643 Id := Empty;
6644 exit;
6645 end if;
6646 end loop;
6647 end if;
6648
6649 return Id;
6650 end Entity_Of;
6651
6652 --------------------------
6653 -- Explain_Limited_Type --
6654 --------------------------
6655
6656 procedure Explain_Limited_Type (T : Entity_Id; N : Node_Id) is
6657 C : Entity_Id;
6658
6659 begin
6660 -- For array, component type must be limited
6661
6662 if Is_Array_Type (T) then
6663 Error_Msg_Node_2 := T;
6664 Error_Msg_NE
6665 ("\component type& of type& is limited", N, Component_Type (T));
6666 Explain_Limited_Type (Component_Type (T), N);
6667
6668 elsif Is_Record_Type (T) then
6669
6670 -- No need for extra messages if explicit limited record
6671
6672 if Is_Limited_Record (Base_Type (T)) then
6673 return;
6674 end if;
6675
6676 -- Otherwise find a limited component. Check only components that
6677 -- come from source, or inherited components that appear in the
6678 -- source of the ancestor.
6679
6680 C := First_Component (T);
6681 while Present (C) loop
6682 if Is_Limited_Type (Etype (C))
6683 and then
6684 (Comes_From_Source (C)
6685 or else
6686 (Present (Original_Record_Component (C))
6687 and then
6688 Comes_From_Source (Original_Record_Component (C))))
6689 then
6690 Error_Msg_Node_2 := T;
6691 Error_Msg_NE ("\component& of type& has limited type", N, C);
6692 Explain_Limited_Type (Etype (C), N);
6693 return;
6694 end if;
6695
6696 Next_Component (C);
6697 end loop;
6698
6699 -- The type may be declared explicitly limited, even if no component
6700 -- of it is limited, in which case we fall out of the loop.
6701 return;
6702 end if;
6703 end Explain_Limited_Type;
6704
6705 -------------------------------
6706 -- Extensions_Visible_Status --
6707 -------------------------------
6708
6709 function Extensions_Visible_Status
6710 (Id : Entity_Id) return Extensions_Visible_Mode
6711 is
6712 Arg : Node_Id;
6713 Decl : Node_Id;
6714 Expr : Node_Id;
6715 Prag : Node_Id;
6716 Subp : Entity_Id;
6717
6718 begin
6719 -- When a formal parameter is subject to Extensions_Visible, the pragma
6720 -- is stored in the contract of related subprogram.
6721
6722 if Is_Formal (Id) then
6723 Subp := Scope (Id);
6724
6725 elsif Is_Subprogram_Or_Generic_Subprogram (Id) then
6726 Subp := Id;
6727
6728 -- No other construct carries this pragma
6729
6730 else
6731 return Extensions_Visible_None;
6732 end if;
6733
6734 Prag := Get_Pragma (Subp, Pragma_Extensions_Visible);
6735
6736 -- In certain cases analysis may request the Extensions_Visible status
6737 -- of an expression function before the pragma has been analyzed yet.
6738 -- Inspect the declarative items after the expression function looking
6739 -- for the pragma (if any).
6740
6741 if No (Prag) and then Is_Expression_Function (Subp) then
6742 Decl := Next (Unit_Declaration_Node (Subp));
6743 while Present (Decl) loop
6744 if Nkind (Decl) = N_Pragma
6745 and then Pragma_Name (Decl) = Name_Extensions_Visible
6746 then
6747 Prag := Decl;
6748 exit;
6749
6750 -- A source construct ends the region where Extensions_Visible may
6751 -- appear, stop the traversal. An expanded expression function is
6752 -- no longer a source construct, but it must still be recognized.
6753
6754 elsif Comes_From_Source (Decl)
6755 or else
6756 (Nkind_In (Decl, N_Subprogram_Body,
6757 N_Subprogram_Declaration)
6758 and then Is_Expression_Function (Defining_Entity (Decl)))
6759 then
6760 exit;
6761 end if;
6762
6763 Next (Decl);
6764 end loop;
6765 end if;
6766
6767 -- Extract the value from the Boolean expression (if any)
6768
6769 if Present (Prag) then
6770 Arg := First (Pragma_Argument_Associations (Prag));
6771
6772 if Present (Arg) then
6773 Expr := Get_Pragma_Arg (Arg);
6774
6775 -- When the associated subprogram is an expression function, the
6776 -- argument of the pragma may not have been analyzed.
6777
6778 if not Analyzed (Expr) then
6779 Preanalyze_And_Resolve (Expr, Standard_Boolean);
6780 end if;
6781
6782 -- Guard against cascading errors when the argument of pragma
6783 -- Extensions_Visible is not a valid static Boolean expression.
6784
6785 if Error_Posted (Expr) then
6786 return Extensions_Visible_None;
6787
6788 elsif Is_True (Expr_Value (Expr)) then
6789 return Extensions_Visible_True;
6790
6791 else
6792 return Extensions_Visible_False;
6793 end if;
6794
6795 -- Otherwise the aspect or pragma defaults to True
6796
6797 else
6798 return Extensions_Visible_True;
6799 end if;
6800
6801 -- Otherwise aspect or pragma Extensions_Visible is not inherited or
6802 -- directly specified. In SPARK code, its value defaults to "False".
6803
6804 elsif SPARK_Mode = On then
6805 return Extensions_Visible_False;
6806
6807 -- In non-SPARK code, aspect or pragma Extensions_Visible defaults to
6808 -- "True".
6809
6810 else
6811 return Extensions_Visible_True;
6812 end if;
6813 end Extensions_Visible_Status;
6814
6815 -----------------
6816 -- Find_Actual --
6817 -----------------
6818
6819 procedure Find_Actual
6820 (N : Node_Id;
6821 Formal : out Entity_Id;
6822 Call : out Node_Id)
6823 is
6824 Context : constant Node_Id := Parent (N);
6825 Actual : Node_Id;
6826 Call_Nam : Node_Id;
6827
6828 begin
6829 if Nkind_In (Context, N_Indexed_Component, N_Selected_Component)
6830 and then N = Prefix (Context)
6831 then
6832 Find_Actual (Context, Formal, Call);
6833 return;
6834
6835 elsif Nkind (Context) = N_Parameter_Association
6836 and then N = Explicit_Actual_Parameter (Context)
6837 then
6838 Call := Parent (Context);
6839
6840 elsif Nkind_In (Context, N_Entry_Call_Statement,
6841 N_Function_Call,
6842 N_Procedure_Call_Statement)
6843 then
6844 Call := Context;
6845
6846 else
6847 Formal := Empty;
6848 Call := Empty;
6849 return;
6850 end if;
6851
6852 -- If we have a call to a subprogram look for the parameter. Note that
6853 -- we exclude overloaded calls, since we don't know enough to be sure
6854 -- of giving the right answer in this case.
6855
6856 if Nkind_In (Call, N_Entry_Call_Statement,
6857 N_Function_Call,
6858 N_Procedure_Call_Statement)
6859 then
6860 Call_Nam := Name (Call);
6861
6862 -- A call to a protected or task entry appears as a selected
6863 -- component rather than an expanded name.
6864
6865 if Nkind (Call_Nam) = N_Selected_Component then
6866 Call_Nam := Selector_Name (Call_Nam);
6867 end if;
6868
6869 if Is_Entity_Name (Call_Nam)
6870 and then Present (Entity (Call_Nam))
6871 and then Is_Overloadable (Entity (Call_Nam))
6872 and then not Is_Overloaded (Call_Nam)
6873 then
6874 -- If node is name in call it is not an actual
6875
6876 if N = Call_Nam then
6877 Formal := Empty;
6878 Call := Empty;
6879 return;
6880 end if;
6881
6882 -- Fall here if we are definitely a parameter
6883
6884 Actual := First_Actual (Call);
6885 Formal := First_Formal (Entity (Call_Nam));
6886 while Present (Formal) and then Present (Actual) loop
6887 if Actual = N then
6888 return;
6889
6890 -- An actual that is the prefix in a prefixed call may have
6891 -- been rewritten in the call, after the deferred reference
6892 -- was collected. Check if sloc and kinds and names match.
6893
6894 elsif Sloc (Actual) = Sloc (N)
6895 and then Nkind (Actual) = N_Identifier
6896 and then Nkind (Actual) = Nkind (N)
6897 and then Chars (Actual) = Chars (N)
6898 then
6899 return;
6900
6901 else
6902 Actual := Next_Actual (Actual);
6903 Formal := Next_Formal (Formal);
6904 end if;
6905 end loop;
6906 end if;
6907 end if;
6908
6909 -- Fall through here if we did not find matching actual
6910
6911 Formal := Empty;
6912 Call := Empty;
6913 end Find_Actual;
6914
6915 ---------------------------
6916 -- Find_Body_Discriminal --
6917 ---------------------------
6918
6919 function Find_Body_Discriminal
6920 (Spec_Discriminant : Entity_Id) return Entity_Id
6921 is
6922 Tsk : Entity_Id;
6923 Disc : Entity_Id;
6924
6925 begin
6926 -- If expansion is suppressed, then the scope can be the concurrent type
6927 -- itself rather than a corresponding concurrent record type.
6928
6929 if Is_Concurrent_Type (Scope (Spec_Discriminant)) then
6930 Tsk := Scope (Spec_Discriminant);
6931
6932 else
6933 pragma Assert (Is_Concurrent_Record_Type (Scope (Spec_Discriminant)));
6934
6935 Tsk := Corresponding_Concurrent_Type (Scope (Spec_Discriminant));
6936 end if;
6937
6938 -- Find discriminant of original concurrent type, and use its current
6939 -- discriminal, which is the renaming within the task/protected body.
6940
6941 Disc := First_Discriminant (Tsk);
6942 while Present (Disc) loop
6943 if Chars (Disc) = Chars (Spec_Discriminant) then
6944 return Discriminal (Disc);
6945 end if;
6946
6947 Next_Discriminant (Disc);
6948 end loop;
6949
6950 -- That loop should always succeed in finding a matching entry and
6951 -- returning. Fatal error if not.
6952
6953 raise Program_Error;
6954 end Find_Body_Discriminal;
6955
6956 -------------------------------------
6957 -- Find_Corresponding_Discriminant --
6958 -------------------------------------
6959
6960 function Find_Corresponding_Discriminant
6961 (Id : Node_Id;
6962 Typ : Entity_Id) return Entity_Id
6963 is
6964 Par_Disc : Entity_Id;
6965 Old_Disc : Entity_Id;
6966 New_Disc : Entity_Id;
6967
6968 begin
6969 Par_Disc := Original_Record_Component (Original_Discriminant (Id));
6970
6971 -- The original type may currently be private, and the discriminant
6972 -- only appear on its full view.
6973
6974 if Is_Private_Type (Scope (Par_Disc))
6975 and then not Has_Discriminants (Scope (Par_Disc))
6976 and then Present (Full_View (Scope (Par_Disc)))
6977 then
6978 Old_Disc := First_Discriminant (Full_View (Scope (Par_Disc)));
6979 else
6980 Old_Disc := First_Discriminant (Scope (Par_Disc));
6981 end if;
6982
6983 if Is_Class_Wide_Type (Typ) then
6984 New_Disc := First_Discriminant (Root_Type (Typ));
6985 else
6986 New_Disc := First_Discriminant (Typ);
6987 end if;
6988
6989 while Present (Old_Disc) and then Present (New_Disc) loop
6990 if Old_Disc = Par_Disc then
6991 return New_Disc;
6992 end if;
6993
6994 Next_Discriminant (Old_Disc);
6995 Next_Discriminant (New_Disc);
6996 end loop;
6997
6998 -- Should always find it
6999
7000 raise Program_Error;
7001 end Find_Corresponding_Discriminant;
7002
7003 ----------------------------------
7004 -- Find_Enclosing_Iterator_Loop --
7005 ----------------------------------
7006
7007 function Find_Enclosing_Iterator_Loop (Id : Entity_Id) return Entity_Id is
7008 Constr : Node_Id;
7009 S : Entity_Id;
7010
7011 begin
7012 -- Traverse the scope chain looking for an iterator loop. Such loops are
7013 -- usually transformed into blocks, hence the use of Original_Node.
7014
7015 S := Id;
7016 while Present (S) and then S /= Standard_Standard loop
7017 if Ekind (S) = E_Loop
7018 and then Nkind (Parent (S)) = N_Implicit_Label_Declaration
7019 then
7020 Constr := Original_Node (Label_Construct (Parent (S)));
7021
7022 if Nkind (Constr) = N_Loop_Statement
7023 and then Present (Iteration_Scheme (Constr))
7024 and then Nkind (Iterator_Specification
7025 (Iteration_Scheme (Constr))) =
7026 N_Iterator_Specification
7027 then
7028 return S;
7029 end if;
7030 end if;
7031
7032 S := Scope (S);
7033 end loop;
7034
7035 return Empty;
7036 end Find_Enclosing_Iterator_Loop;
7037
7038 ------------------------------------
7039 -- Find_Loop_In_Conditional_Block --
7040 ------------------------------------
7041
7042 function Find_Loop_In_Conditional_Block (N : Node_Id) return Node_Id is
7043 Stmt : Node_Id;
7044
7045 begin
7046 Stmt := N;
7047
7048 if Nkind (Stmt) = N_If_Statement then
7049 Stmt := First (Then_Statements (Stmt));
7050 end if;
7051
7052 pragma Assert (Nkind (Stmt) = N_Block_Statement);
7053
7054 -- Inspect the statements of the conditional block. In general the loop
7055 -- should be the first statement in the statement sequence of the block,
7056 -- but the finalization machinery may have introduced extra object
7057 -- declarations.
7058
7059 Stmt := First (Statements (Handled_Statement_Sequence (Stmt)));
7060 while Present (Stmt) loop
7061 if Nkind (Stmt) = N_Loop_Statement then
7062 return Stmt;
7063 end if;
7064
7065 Next (Stmt);
7066 end loop;
7067
7068 -- The expansion of attribute 'Loop_Entry produced a malformed block
7069
7070 raise Program_Error;
7071 end Find_Loop_In_Conditional_Block;
7072
7073 --------------------------
7074 -- Find_Overlaid_Entity --
7075 --------------------------
7076
7077 procedure Find_Overlaid_Entity
7078 (N : Node_Id;
7079 Ent : out Entity_Id;
7080 Off : out Boolean)
7081 is
7082 Expr : Node_Id;
7083
7084 begin
7085 -- We are looking for one of the two following forms:
7086
7087 -- for X'Address use Y'Address
7088
7089 -- or
7090
7091 -- Const : constant Address := expr;
7092 -- ...
7093 -- for X'Address use Const;
7094
7095 -- In the second case, the expr is either Y'Address, or recursively a
7096 -- constant that eventually references Y'Address.
7097
7098 Ent := Empty;
7099 Off := False;
7100
7101 if Nkind (N) = N_Attribute_Definition_Clause
7102 and then Chars (N) = Name_Address
7103 then
7104 Expr := Expression (N);
7105
7106 -- This loop checks the form of the expression for Y'Address,
7107 -- using recursion to deal with intermediate constants.
7108
7109 loop
7110 -- Check for Y'Address
7111
7112 if Nkind (Expr) = N_Attribute_Reference
7113 and then Attribute_Name (Expr) = Name_Address
7114 then
7115 Expr := Prefix (Expr);
7116 exit;
7117
7118 -- Check for Const where Const is a constant entity
7119
7120 elsif Is_Entity_Name (Expr)
7121 and then Ekind (Entity (Expr)) = E_Constant
7122 then
7123 Expr := Constant_Value (Entity (Expr));
7124
7125 -- Anything else does not need checking
7126
7127 else
7128 return;
7129 end if;
7130 end loop;
7131
7132 -- This loop checks the form of the prefix for an entity, using
7133 -- recursion to deal with intermediate components.
7134
7135 loop
7136 -- Check for Y where Y is an entity
7137
7138 if Is_Entity_Name (Expr) then
7139 Ent := Entity (Expr);
7140 return;
7141
7142 -- Check for components
7143
7144 elsif
7145 Nkind_In (Expr, N_Selected_Component, N_Indexed_Component)
7146 then
7147 Expr := Prefix (Expr);
7148 Off := True;
7149
7150 -- Anything else does not need checking
7151
7152 else
7153 return;
7154 end if;
7155 end loop;
7156 end if;
7157 end Find_Overlaid_Entity;
7158
7159 -------------------------
7160 -- Find_Parameter_Type --
7161 -------------------------
7162
7163 function Find_Parameter_Type (Param : Node_Id) return Entity_Id is
7164 begin
7165 if Nkind (Param) /= N_Parameter_Specification then
7166 return Empty;
7167
7168 -- For an access parameter, obtain the type from the formal entity
7169 -- itself, because access to subprogram nodes do not carry a type.
7170 -- Shouldn't we always use the formal entity ???
7171
7172 elsif Nkind (Parameter_Type (Param)) = N_Access_Definition then
7173 return Etype (Defining_Identifier (Param));
7174
7175 else
7176 return Etype (Parameter_Type (Param));
7177 end if;
7178 end Find_Parameter_Type;
7179
7180 -----------------------------------
7181 -- Find_Placement_In_State_Space --
7182 -----------------------------------
7183
7184 procedure Find_Placement_In_State_Space
7185 (Item_Id : Entity_Id;
7186 Placement : out State_Space_Kind;
7187 Pack_Id : out Entity_Id)
7188 is
7189 Context : Entity_Id;
7190
7191 begin
7192 -- Assume that the item does not appear in the state space of a package
7193
7194 Placement := Not_In_Package;
7195 Pack_Id := Empty;
7196
7197 -- Climb the scope stack and examine the enclosing context
7198
7199 Context := Scope (Item_Id);
7200 while Present (Context) and then Context /= Standard_Standard loop
7201 if Ekind (Context) = E_Package then
7202 Pack_Id := Context;
7203
7204 -- A package body is a cut off point for the traversal as the item
7205 -- cannot be visible to the outside from this point on. Note that
7206 -- this test must be done first as a body is also classified as a
7207 -- private part.
7208
7209 if In_Package_Body (Context) then
7210 Placement := Body_State_Space;
7211 return;
7212
7213 -- The private part of a package is a cut off point for the
7214 -- traversal as the item cannot be visible to the outside from
7215 -- this point on.
7216
7217 elsif In_Private_Part (Context) then
7218 Placement := Private_State_Space;
7219 return;
7220
7221 -- When the item appears in the visible state space of a package,
7222 -- continue to climb the scope stack as this may not be the final
7223 -- state space.
7224
7225 else
7226 Placement := Visible_State_Space;
7227
7228 -- The visible state space of a child unit acts as the proper
7229 -- placement of an item.
7230
7231 if Is_Child_Unit (Context) then
7232 return;
7233 end if;
7234 end if;
7235
7236 -- The item or its enclosing package appear in a construct that has
7237 -- no state space.
7238
7239 else
7240 Placement := Not_In_Package;
7241 return;
7242 end if;
7243
7244 Context := Scope (Context);
7245 end loop;
7246 end Find_Placement_In_State_Space;
7247
7248 ------------------------
7249 -- Find_Specific_Type --
7250 ------------------------
7251
7252 function Find_Specific_Type (CW : Entity_Id) return Entity_Id is
7253 Typ : Entity_Id := Root_Type (CW);
7254
7255 begin
7256 if Ekind (Typ) = E_Incomplete_Type then
7257 if From_Limited_With (Typ) then
7258 Typ := Non_Limited_View (Typ);
7259 else
7260 Typ := Full_View (Typ);
7261 end if;
7262 end if;
7263
7264 if Is_Private_Type (Typ)
7265 and then not Is_Tagged_Type (Typ)
7266 and then Present (Full_View (Typ))
7267 then
7268 return Full_View (Typ);
7269 else
7270 return Typ;
7271 end if;
7272 end Find_Specific_Type;
7273
7274 -----------------------------
7275 -- Find_Static_Alternative --
7276 -----------------------------
7277
7278 function Find_Static_Alternative (N : Node_Id) return Node_Id is
7279 Expr : constant Node_Id := Expression (N);
7280 Val : constant Uint := Expr_Value (Expr);
7281 Alt : Node_Id;
7282 Choice : Node_Id;
7283
7284 begin
7285 Alt := First (Alternatives (N));
7286
7287 Search : loop
7288 if Nkind (Alt) /= N_Pragma then
7289 Choice := First (Discrete_Choices (Alt));
7290 while Present (Choice) loop
7291
7292 -- Others choice, always matches
7293
7294 if Nkind (Choice) = N_Others_Choice then
7295 exit Search;
7296
7297 -- Range, check if value is in the range
7298
7299 elsif Nkind (Choice) = N_Range then
7300 exit Search when
7301 Val >= Expr_Value (Low_Bound (Choice))
7302 and then
7303 Val <= Expr_Value (High_Bound (Choice));
7304
7305 -- Choice is a subtype name. Note that we know it must
7306 -- be a static subtype, since otherwise it would have
7307 -- been diagnosed as illegal.
7308
7309 elsif Is_Entity_Name (Choice)
7310 and then Is_Type (Entity (Choice))
7311 then
7312 exit Search when Is_In_Range (Expr, Etype (Choice),
7313 Assume_Valid => False);
7314
7315 -- Choice is a subtype indication
7316
7317 elsif Nkind (Choice) = N_Subtype_Indication then
7318 declare
7319 C : constant Node_Id := Constraint (Choice);
7320 R : constant Node_Id := Range_Expression (C);
7321
7322 begin
7323 exit Search when
7324 Val >= Expr_Value (Low_Bound (R))
7325 and then
7326 Val <= Expr_Value (High_Bound (R));
7327 end;
7328
7329 -- Choice is a simple expression
7330
7331 else
7332 exit Search when Val = Expr_Value (Choice);
7333 end if;
7334
7335 Next (Choice);
7336 end loop;
7337 end if;
7338
7339 Next (Alt);
7340 pragma Assert (Present (Alt));
7341 end loop Search;
7342
7343 -- The above loop *must* terminate by finding a match, since
7344 -- we know the case statement is valid, and the value of the
7345 -- expression is known at compile time. When we fall out of
7346 -- the loop, Alt points to the alternative that we know will
7347 -- be selected at run time.
7348
7349 return Alt;
7350 end Find_Static_Alternative;
7351
7352 ------------------
7353 -- First_Actual --
7354 ------------------
7355
7356 function First_Actual (Node : Node_Id) return Node_Id is
7357 N : Node_Id;
7358
7359 begin
7360 if No (Parameter_Associations (Node)) then
7361 return Empty;
7362 end if;
7363
7364 N := First (Parameter_Associations (Node));
7365
7366 if Nkind (N) = N_Parameter_Association then
7367 return First_Named_Actual (Node);
7368 else
7369 return N;
7370 end if;
7371 end First_Actual;
7372
7373 -------------
7374 -- Fix_Msg --
7375 -------------
7376
7377 function Fix_Msg (Id : Entity_Id; Msg : String) return String is
7378 Is_Task : constant Boolean :=
7379 Ekind_In (Id, E_Task_Body, E_Task_Type)
7380 or else Is_Single_Task_Object (Id);
7381 Msg_Last : constant Natural := Msg'Last;
7382 Msg_Index : Natural;
7383 Res : String (Msg'Range) := (others => ' ');
7384 Res_Index : Natural;
7385
7386 begin
7387 -- Copy all characters from the input message Msg to result Res with
7388 -- suitable replacements.
7389
7390 Msg_Index := Msg'First;
7391 Res_Index := Res'First;
7392 while Msg_Index <= Msg_Last loop
7393
7394 -- Replace "subprogram" with a different word
7395
7396 if Msg_Index <= Msg_Last - 10
7397 and then Msg (Msg_Index .. Msg_Index + 9) = "subprogram"
7398 then
7399 if Ekind_In (Id, E_Entry, E_Entry_Family) then
7400 Res (Res_Index .. Res_Index + 4) := "entry";
7401 Res_Index := Res_Index + 5;
7402
7403 elsif Is_Task then
7404 Res (Res_Index .. Res_Index + 8) := "task type";
7405 Res_Index := Res_Index + 9;
7406
7407 else
7408 Res (Res_Index .. Res_Index + 9) := "subprogram";
7409 Res_Index := Res_Index + 10;
7410 end if;
7411
7412 Msg_Index := Msg_Index + 10;
7413
7414 -- Replace "protected" with a different word
7415
7416 elsif Msg_Index <= Msg_Last - 9
7417 and then Msg (Msg_Index .. Msg_Index + 8) = "protected"
7418 and then Is_Task
7419 then
7420 Res (Res_Index .. Res_Index + 3) := "task";
7421 Res_Index := Res_Index + 4;
7422 Msg_Index := Msg_Index + 9;
7423
7424 -- Otherwise copy the character
7425
7426 else
7427 Res (Res_Index) := Msg (Msg_Index);
7428 Msg_Index := Msg_Index + 1;
7429 Res_Index := Res_Index + 1;
7430 end if;
7431 end loop;
7432
7433 return Res (Res'First .. Res_Index - 1);
7434 end Fix_Msg;
7435
7436 -----------------------
7437 -- Gather_Components --
7438 -----------------------
7439
7440 procedure Gather_Components
7441 (Typ : Entity_Id;
7442 Comp_List : Node_Id;
7443 Governed_By : List_Id;
7444 Into : Elist_Id;
7445 Report_Errors : out Boolean)
7446 is
7447 Assoc : Node_Id;
7448 Variant : Node_Id;
7449 Discrete_Choice : Node_Id;
7450 Comp_Item : Node_Id;
7451
7452 Discrim : Entity_Id;
7453 Discrim_Name : Node_Id;
7454 Discrim_Value : Node_Id;
7455
7456 begin
7457 Report_Errors := False;
7458
7459 if No (Comp_List) or else Null_Present (Comp_List) then
7460 return;
7461
7462 elsif Present (Component_Items (Comp_List)) then
7463 Comp_Item := First (Component_Items (Comp_List));
7464
7465 else
7466 Comp_Item := Empty;
7467 end if;
7468
7469 while Present (Comp_Item) loop
7470
7471 -- Skip the tag of a tagged record, the interface tags, as well
7472 -- as all items that are not user components (anonymous types,
7473 -- rep clauses, Parent field, controller field).
7474
7475 if Nkind (Comp_Item) = N_Component_Declaration then
7476 declare
7477 Comp : constant Entity_Id := Defining_Identifier (Comp_Item);
7478 begin
7479 if not Is_Tag (Comp) and then Chars (Comp) /= Name_uParent then
7480 Append_Elmt (Comp, Into);
7481 end if;
7482 end;
7483 end if;
7484
7485 Next (Comp_Item);
7486 end loop;
7487
7488 if No (Variant_Part (Comp_List)) then
7489 return;
7490 else
7491 Discrim_Name := Name (Variant_Part (Comp_List));
7492 Variant := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
7493 end if;
7494
7495 -- Look for the discriminant that governs this variant part.
7496 -- The discriminant *must* be in the Governed_By List
7497
7498 Assoc := First (Governed_By);
7499 Find_Constraint : loop
7500 Discrim := First (Choices (Assoc));
7501 exit Find_Constraint when Chars (Discrim_Name) = Chars (Discrim)
7502 or else (Present (Corresponding_Discriminant (Entity (Discrim)))
7503 and then
7504 Chars (Corresponding_Discriminant (Entity (Discrim))) =
7505 Chars (Discrim_Name))
7506 or else Chars (Original_Record_Component (Entity (Discrim)))
7507 = Chars (Discrim_Name);
7508
7509 if No (Next (Assoc)) then
7510 if not Is_Constrained (Typ)
7511 and then Is_Derived_Type (Typ)
7512 and then Present (Stored_Constraint (Typ))
7513 then
7514 -- If the type is a tagged type with inherited discriminants,
7515 -- use the stored constraint on the parent in order to find
7516 -- the values of discriminants that are otherwise hidden by an
7517 -- explicit constraint. Renamed discriminants are handled in
7518 -- the code above.
7519
7520 -- If several parent discriminants are renamed by a single
7521 -- discriminant of the derived type, the call to obtain the
7522 -- Corresponding_Discriminant field only retrieves the last
7523 -- of them. We recover the constraint on the others from the
7524 -- Stored_Constraint as well.
7525
7526 declare
7527 D : Entity_Id;
7528 C : Elmt_Id;
7529
7530 begin
7531 D := First_Discriminant (Etype (Typ));
7532 C := First_Elmt (Stored_Constraint (Typ));
7533 while Present (D) and then Present (C) loop
7534 if Chars (Discrim_Name) = Chars (D) then
7535 if Is_Entity_Name (Node (C))
7536 and then Entity (Node (C)) = Entity (Discrim)
7537 then
7538 -- D is renamed by Discrim, whose value is given in
7539 -- Assoc.
7540
7541 null;
7542
7543 else
7544 Assoc :=
7545 Make_Component_Association (Sloc (Typ),
7546 New_List
7547 (New_Occurrence_Of (D, Sloc (Typ))),
7548 Duplicate_Subexpr_No_Checks (Node (C)));
7549 end if;
7550 exit Find_Constraint;
7551 end if;
7552
7553 Next_Discriminant (D);
7554 Next_Elmt (C);
7555 end loop;
7556 end;
7557 end if;
7558 end if;
7559
7560 if No (Next (Assoc)) then
7561 Error_Msg_NE (" missing value for discriminant&",
7562 First (Governed_By), Discrim_Name);
7563 Report_Errors := True;
7564 return;
7565 end if;
7566
7567 Next (Assoc);
7568 end loop Find_Constraint;
7569
7570 Discrim_Value := Expression (Assoc);
7571
7572 if not Is_OK_Static_Expression (Discrim_Value) then
7573
7574 -- If the variant part is governed by a discriminant of the type
7575 -- this is an error. If the variant part and the discriminant are
7576 -- inherited from an ancestor this is legal (AI05-120) unless the
7577 -- components are being gathered for an aggregate, in which case
7578 -- the caller must check Report_Errors.
7579
7580 if Scope (Original_Record_Component
7581 ((Entity (First (Choices (Assoc)))))) = Typ
7582 then
7583 Error_Msg_FE
7584 ("value for discriminant & must be static!",
7585 Discrim_Value, Discrim);
7586 Why_Not_Static (Discrim_Value);
7587 end if;
7588
7589 Report_Errors := True;
7590 return;
7591 end if;
7592
7593 Search_For_Discriminant_Value : declare
7594 Low : Node_Id;
7595 High : Node_Id;
7596
7597 UI_High : Uint;
7598 UI_Low : Uint;
7599 UI_Discrim_Value : constant Uint := Expr_Value (Discrim_Value);
7600
7601 begin
7602 Find_Discrete_Value : while Present (Variant) loop
7603 Discrete_Choice := First (Discrete_Choices (Variant));
7604 while Present (Discrete_Choice) loop
7605 exit Find_Discrete_Value when
7606 Nkind (Discrete_Choice) = N_Others_Choice;
7607
7608 Get_Index_Bounds (Discrete_Choice, Low, High);
7609
7610 UI_Low := Expr_Value (Low);
7611 UI_High := Expr_Value (High);
7612
7613 exit Find_Discrete_Value when
7614 UI_Low <= UI_Discrim_Value
7615 and then
7616 UI_High >= UI_Discrim_Value;
7617
7618 Next (Discrete_Choice);
7619 end loop;
7620
7621 Next_Non_Pragma (Variant);
7622 end loop Find_Discrete_Value;
7623 end Search_For_Discriminant_Value;
7624
7625 if No (Variant) then
7626 Error_Msg_NE
7627 ("value of discriminant & is out of range", Discrim_Value, Discrim);
7628 Report_Errors := True;
7629 return;
7630 end if;
7631
7632 -- If we have found the corresponding choice, recursively add its
7633 -- components to the Into list. The nested components are part of
7634 -- the same record type.
7635
7636 Gather_Components
7637 (Typ, Component_List (Variant), Governed_By, Into, Report_Errors);
7638 end Gather_Components;
7639
7640 ------------------------
7641 -- Get_Actual_Subtype --
7642 ------------------------
7643
7644 function Get_Actual_Subtype (N : Node_Id) return Entity_Id is
7645 Typ : constant Entity_Id := Etype (N);
7646 Utyp : Entity_Id := Underlying_Type (Typ);
7647 Decl : Node_Id;
7648 Atyp : Entity_Id;
7649
7650 begin
7651 if No (Utyp) then
7652 Utyp := Typ;
7653 end if;
7654
7655 -- If what we have is an identifier that references a subprogram
7656 -- formal, or a variable or constant object, then we get the actual
7657 -- subtype from the referenced entity if one has been built.
7658
7659 if Nkind (N) = N_Identifier
7660 and then
7661 (Is_Formal (Entity (N))
7662 or else Ekind (Entity (N)) = E_Constant
7663 or else Ekind (Entity (N)) = E_Variable)
7664 and then Present (Actual_Subtype (Entity (N)))
7665 then
7666 return Actual_Subtype (Entity (N));
7667
7668 -- Actual subtype of unchecked union is always itself. We never need
7669 -- the "real" actual subtype. If we did, we couldn't get it anyway
7670 -- because the discriminant is not available. The restrictions on
7671 -- Unchecked_Union are designed to make sure that this is OK.
7672
7673 elsif Is_Unchecked_Union (Base_Type (Utyp)) then
7674 return Typ;
7675
7676 -- Here for the unconstrained case, we must find actual subtype
7677 -- No actual subtype is available, so we must build it on the fly.
7678
7679 -- Checking the type, not the underlying type, for constrainedness
7680 -- seems to be necessary. Maybe all the tests should be on the type???
7681
7682 elsif (not Is_Constrained (Typ))
7683 and then (Is_Array_Type (Utyp)
7684 or else (Is_Record_Type (Utyp)
7685 and then Has_Discriminants (Utyp)))
7686 and then not Has_Unknown_Discriminants (Utyp)
7687 and then not (Ekind (Utyp) = E_String_Literal_Subtype)
7688 then
7689 -- Nothing to do if in spec expression (why not???)
7690
7691 if In_Spec_Expression then
7692 return Typ;
7693
7694 elsif Is_Private_Type (Typ) and then not Has_Discriminants (Typ) then
7695
7696 -- If the type has no discriminants, there is no subtype to
7697 -- build, even if the underlying type is discriminated.
7698
7699 return Typ;
7700
7701 -- Else build the actual subtype
7702
7703 else
7704 Decl := Build_Actual_Subtype (Typ, N);
7705 Atyp := Defining_Identifier (Decl);
7706
7707 -- If Build_Actual_Subtype generated a new declaration then use it
7708
7709 if Atyp /= Typ then
7710
7711 -- The actual subtype is an Itype, so analyze the declaration,
7712 -- but do not attach it to the tree, to get the type defined.
7713
7714 Set_Parent (Decl, N);
7715 Set_Is_Itype (Atyp);
7716 Analyze (Decl, Suppress => All_Checks);
7717 Set_Associated_Node_For_Itype (Atyp, N);
7718 Set_Has_Delayed_Freeze (Atyp, False);
7719
7720 -- We need to freeze the actual subtype immediately. This is
7721 -- needed, because otherwise this Itype will not get frozen
7722 -- at all, and it is always safe to freeze on creation because
7723 -- any associated types must be frozen at this point.
7724
7725 Freeze_Itype (Atyp, N);
7726 return Atyp;
7727
7728 -- Otherwise we did not build a declaration, so return original
7729
7730 else
7731 return Typ;
7732 end if;
7733 end if;
7734
7735 -- For all remaining cases, the actual subtype is the same as
7736 -- the nominal type.
7737
7738 else
7739 return Typ;
7740 end if;
7741 end Get_Actual_Subtype;
7742
7743 -------------------------------------
7744 -- Get_Actual_Subtype_If_Available --
7745 -------------------------------------
7746
7747 function Get_Actual_Subtype_If_Available (N : Node_Id) return Entity_Id is
7748 Typ : constant Entity_Id := Etype (N);
7749
7750 begin
7751 -- If what we have is an identifier that references a subprogram
7752 -- formal, or a variable or constant object, then we get the actual
7753 -- subtype from the referenced entity if one has been built.
7754
7755 if Nkind (N) = N_Identifier
7756 and then
7757 (Is_Formal (Entity (N))
7758 or else Ekind (Entity (N)) = E_Constant
7759 or else Ekind (Entity (N)) = E_Variable)
7760 and then Present (Actual_Subtype (Entity (N)))
7761 then
7762 return Actual_Subtype (Entity (N));
7763
7764 -- Otherwise the Etype of N is returned unchanged
7765
7766 else
7767 return Typ;
7768 end if;
7769 end Get_Actual_Subtype_If_Available;
7770
7771 ------------------------
7772 -- Get_Body_From_Stub --
7773 ------------------------
7774
7775 function Get_Body_From_Stub (N : Node_Id) return Node_Id is
7776 begin
7777 return Proper_Body (Unit (Library_Unit (N)));
7778 end Get_Body_From_Stub;
7779
7780 ---------------------
7781 -- Get_Cursor_Type --
7782 ---------------------
7783
7784 function Get_Cursor_Type
7785 (Aspect : Node_Id;
7786 Typ : Entity_Id) return Entity_Id
7787 is
7788 Assoc : Node_Id;
7789 Func : Entity_Id;
7790 First_Op : Entity_Id;
7791 Cursor : Entity_Id;
7792
7793 begin
7794 -- If error already detected, return
7795
7796 if Error_Posted (Aspect) then
7797 return Any_Type;
7798 end if;
7799
7800 -- The cursor type for an Iterable aspect is the return type of a
7801 -- non-overloaded First primitive operation. Locate association for
7802 -- First.
7803
7804 Assoc := First (Component_Associations (Expression (Aspect)));
7805 First_Op := Any_Id;
7806 while Present (Assoc) loop
7807 if Chars (First (Choices (Assoc))) = Name_First then
7808 First_Op := Expression (Assoc);
7809 exit;
7810 end if;
7811
7812 Next (Assoc);
7813 end loop;
7814
7815 if First_Op = Any_Id then
7816 Error_Msg_N ("aspect Iterable must specify First operation", Aspect);
7817 return Any_Type;
7818 end if;
7819
7820 Cursor := Any_Type;
7821
7822 -- Locate function with desired name and profile in scope of type
7823 -- In the rare case where the type is an integer type, a base type
7824 -- is created for it, check that the base type of the first formal
7825 -- of First matches the base type of the domain.
7826
7827 Func := First_Entity (Scope (Typ));
7828 while Present (Func) loop
7829 if Chars (Func) = Chars (First_Op)
7830 and then Ekind (Func) = E_Function
7831 and then Present (First_Formal (Func))
7832 and then Base_Type (Etype (First_Formal (Func))) = Base_Type (Typ)
7833 and then No (Next_Formal (First_Formal (Func)))
7834 then
7835 if Cursor /= Any_Type then
7836 Error_Msg_N
7837 ("Operation First for iterable type must be unique", Aspect);
7838 return Any_Type;
7839 else
7840 Cursor := Etype (Func);
7841 end if;
7842 end if;
7843
7844 Next_Entity (Func);
7845 end loop;
7846
7847 -- If not found, no way to resolve remaining primitives.
7848
7849 if Cursor = Any_Type then
7850 Error_Msg_N
7851 ("No legal primitive operation First for Iterable type", Aspect);
7852 end if;
7853
7854 return Cursor;
7855 end Get_Cursor_Type;
7856
7857 function Get_Cursor_Type (Typ : Entity_Id) return Entity_Id is
7858 begin
7859 return Etype (Get_Iterable_Type_Primitive (Typ, Name_First));
7860 end Get_Cursor_Type;
7861
7862 -------------------------------
7863 -- Get_Default_External_Name --
7864 -------------------------------
7865
7866 function Get_Default_External_Name (E : Node_Or_Entity_Id) return Node_Id is
7867 begin
7868 Get_Decoded_Name_String (Chars (E));
7869
7870 if Opt.External_Name_Imp_Casing = Uppercase then
7871 Set_Casing (All_Upper_Case);
7872 else
7873 Set_Casing (All_Lower_Case);
7874 end if;
7875
7876 return
7877 Make_String_Literal (Sloc (E),
7878 Strval => String_From_Name_Buffer);
7879 end Get_Default_External_Name;
7880
7881 --------------------------
7882 -- Get_Enclosing_Object --
7883 --------------------------
7884
7885 function Get_Enclosing_Object (N : Node_Id) return Entity_Id is
7886 begin
7887 if Is_Entity_Name (N) then
7888 return Entity (N);
7889 else
7890 case Nkind (N) is
7891 when N_Indexed_Component |
7892 N_Slice |
7893 N_Selected_Component =>
7894
7895 -- If not generating code, a dereference may be left implicit.
7896 -- In thoses cases, return Empty.
7897
7898 if Is_Access_Type (Etype (Prefix (N))) then
7899 return Empty;
7900 else
7901 return Get_Enclosing_Object (Prefix (N));
7902 end if;
7903
7904 when N_Type_Conversion =>
7905 return Get_Enclosing_Object (Expression (N));
7906
7907 when others =>
7908 return Empty;
7909 end case;
7910 end if;
7911 end Get_Enclosing_Object;
7912
7913 ---------------------------
7914 -- Get_Enum_Lit_From_Pos --
7915 ---------------------------
7916
7917 function Get_Enum_Lit_From_Pos
7918 (T : Entity_Id;
7919 Pos : Uint;
7920 Loc : Source_Ptr) return Node_Id
7921 is
7922 Btyp : Entity_Id := Base_Type (T);
7923 Lit : Node_Id;
7924
7925 begin
7926 -- In the case where the literal is of type Character, Wide_Character
7927 -- or Wide_Wide_Character or of a type derived from them, there needs
7928 -- to be some special handling since there is no explicit chain of
7929 -- literals to search. Instead, an N_Character_Literal node is created
7930 -- with the appropriate Char_Code and Chars fields.
7931
7932 if Is_Standard_Character_Type (T) then
7933 Set_Character_Literal_Name (UI_To_CC (Pos));
7934 return
7935 Make_Character_Literal (Loc,
7936 Chars => Name_Find,
7937 Char_Literal_Value => Pos);
7938
7939 -- For all other cases, we have a complete table of literals, and
7940 -- we simply iterate through the chain of literal until the one
7941 -- with the desired position value is found.
7942
7943 else
7944 if Is_Private_Type (Btyp) and then Present (Full_View (Btyp)) then
7945 Btyp := Full_View (Btyp);
7946 end if;
7947
7948 Lit := First_Literal (Btyp);
7949 for J in 1 .. UI_To_Int (Pos) loop
7950 Next_Literal (Lit);
7951 end loop;
7952
7953 return New_Occurrence_Of (Lit, Loc);
7954 end if;
7955 end Get_Enum_Lit_From_Pos;
7956
7957 ------------------------
7958 -- Get_Generic_Entity --
7959 ------------------------
7960
7961 function Get_Generic_Entity (N : Node_Id) return Entity_Id is
7962 Ent : constant Entity_Id := Entity (Name (N));
7963 begin
7964 if Present (Renamed_Object (Ent)) then
7965 return Renamed_Object (Ent);
7966 else
7967 return Ent;
7968 end if;
7969 end Get_Generic_Entity;
7970
7971 -------------------------------------
7972 -- Get_Incomplete_View_Of_Ancestor --
7973 -------------------------------------
7974
7975 function Get_Incomplete_View_Of_Ancestor (E : Entity_Id) return Entity_Id is
7976 Cur_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
7977 Par_Scope : Entity_Id;
7978 Par_Type : Entity_Id;
7979
7980 begin
7981 -- The incomplete view of an ancestor is only relevant for private
7982 -- derived types in child units.
7983
7984 if not Is_Derived_Type (E)
7985 or else not Is_Child_Unit (Cur_Unit)
7986 then
7987 return Empty;
7988
7989 else
7990 Par_Scope := Scope (Cur_Unit);
7991 if No (Par_Scope) then
7992 return Empty;
7993 end if;
7994
7995 Par_Type := Etype (Base_Type (E));
7996
7997 -- Traverse list of ancestor types until we find one declared in
7998 -- a parent or grandparent unit (two levels seem sufficient).
7999
8000 while Present (Par_Type) loop
8001 if Scope (Par_Type) = Par_Scope
8002 or else Scope (Par_Type) = Scope (Par_Scope)
8003 then
8004 return Par_Type;
8005
8006 elsif not Is_Derived_Type (Par_Type) then
8007 return Empty;
8008
8009 else
8010 Par_Type := Etype (Base_Type (Par_Type));
8011 end if;
8012 end loop;
8013
8014 -- If none found, there is no relevant ancestor type.
8015
8016 return Empty;
8017 end if;
8018 end Get_Incomplete_View_Of_Ancestor;
8019
8020 ----------------------
8021 -- Get_Index_Bounds --
8022 ----------------------
8023
8024 procedure Get_Index_Bounds (N : Node_Id; L, H : out Node_Id) is
8025 Kind : constant Node_Kind := Nkind (N);
8026 R : Node_Id;
8027
8028 begin
8029 if Kind = N_Range then
8030 L := Low_Bound (N);
8031 H := High_Bound (N);
8032
8033 elsif Kind = N_Subtype_Indication then
8034 R := Range_Expression (Constraint (N));
8035
8036 if R = Error then
8037 L := Error;
8038 H := Error;
8039 return;
8040
8041 else
8042 L := Low_Bound (Range_Expression (Constraint (N)));
8043 H := High_Bound (Range_Expression (Constraint (N)));
8044 end if;
8045
8046 elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
8047 if Error_Posted (Scalar_Range (Entity (N))) then
8048 L := Error;
8049 H := Error;
8050
8051 elsif Nkind (Scalar_Range (Entity (N))) = N_Subtype_Indication then
8052 Get_Index_Bounds (Scalar_Range (Entity (N)), L, H);
8053
8054 else
8055 L := Low_Bound (Scalar_Range (Entity (N)));
8056 H := High_Bound (Scalar_Range (Entity (N)));
8057 end if;
8058
8059 else
8060 -- N is an expression, indicating a range with one value
8061
8062 L := N;
8063 H := N;
8064 end if;
8065 end Get_Index_Bounds;
8066
8067 ---------------------------------
8068 -- Get_Iterable_Type_Primitive --
8069 ---------------------------------
8070
8071 function Get_Iterable_Type_Primitive
8072 (Typ : Entity_Id;
8073 Nam : Name_Id) return Entity_Id
8074 is
8075 Funcs : constant Node_Id := Find_Value_Of_Aspect (Typ, Aspect_Iterable);
8076 Assoc : Node_Id;
8077
8078 begin
8079 if No (Funcs) then
8080 return Empty;
8081
8082 else
8083 Assoc := First (Component_Associations (Funcs));
8084 while Present (Assoc) loop
8085 if Chars (First (Choices (Assoc))) = Nam then
8086 return Entity (Expression (Assoc));
8087 end if;
8088
8089 Assoc := Next (Assoc);
8090 end loop;
8091
8092 return Empty;
8093 end if;
8094 end Get_Iterable_Type_Primitive;
8095
8096 ----------------------------------
8097 -- Get_Library_Unit_Name_string --
8098 ----------------------------------
8099
8100 procedure Get_Library_Unit_Name_String (Decl_Node : Node_Id) is
8101 Unit_Name_Id : constant Unit_Name_Type := Get_Unit_Name (Decl_Node);
8102
8103 begin
8104 Get_Unit_Name_String (Unit_Name_Id);
8105
8106 -- Remove seven last character (" (spec)" or " (body)")
8107
8108 Name_Len := Name_Len - 7;
8109 pragma Assert (Name_Buffer (Name_Len + 1) = ' ');
8110 end Get_Library_Unit_Name_String;
8111
8112 ------------------------
8113 -- Get_Name_Entity_Id --
8114 ------------------------
8115
8116 function Get_Name_Entity_Id (Id : Name_Id) return Entity_Id is
8117 begin
8118 return Entity_Id (Get_Name_Table_Int (Id));
8119 end Get_Name_Entity_Id;
8120
8121 ------------------------------
8122 -- Get_Name_From_CTC_Pragma --
8123 ------------------------------
8124
8125 function Get_Name_From_CTC_Pragma (N : Node_Id) return String_Id is
8126 Arg : constant Node_Id :=
8127 Get_Pragma_Arg (First (Pragma_Argument_Associations (N)));
8128 begin
8129 return Strval (Expr_Value_S (Arg));
8130 end Get_Name_From_CTC_Pragma;
8131
8132 -----------------------
8133 -- Get_Parent_Entity --
8134 -----------------------
8135
8136 function Get_Parent_Entity (Unit : Node_Id) return Entity_Id is
8137 begin
8138 if Nkind (Unit) = N_Package_Body
8139 and then Nkind (Original_Node (Unit)) = N_Package_Instantiation
8140 then
8141 return Defining_Entity
8142 (Specification (Instance_Spec (Original_Node (Unit))));
8143 elsif Nkind (Unit) = N_Package_Instantiation then
8144 return Defining_Entity (Specification (Instance_Spec (Unit)));
8145 else
8146 return Defining_Entity (Unit);
8147 end if;
8148 end Get_Parent_Entity;
8149
8150 -------------------
8151 -- Get_Pragma_Id --
8152 -------------------
8153
8154 function Get_Pragma_Id (N : Node_Id) return Pragma_Id is
8155 begin
8156 return Get_Pragma_Id (Pragma_Name (N));
8157 end Get_Pragma_Id;
8158
8159 -----------------------
8160 -- Get_Reason_String --
8161 -----------------------
8162
8163 procedure Get_Reason_String (N : Node_Id) is
8164 begin
8165 if Nkind (N) = N_String_Literal then
8166 Store_String_Chars (Strval (N));
8167
8168 elsif Nkind (N) = N_Op_Concat then
8169 Get_Reason_String (Left_Opnd (N));
8170 Get_Reason_String (Right_Opnd (N));
8171
8172 -- If not of required form, error
8173
8174 else
8175 Error_Msg_N
8176 ("Reason for pragma Warnings has wrong form", N);
8177 Error_Msg_N
8178 ("\must be string literal or concatenation of string literals", N);
8179 return;
8180 end if;
8181 end Get_Reason_String;
8182
8183 --------------------------------
8184 -- Get_Reference_Discriminant --
8185 --------------------------------
8186
8187 function Get_Reference_Discriminant (Typ : Entity_Id) return Entity_Id is
8188 D : Entity_Id;
8189
8190 begin
8191 D := First_Discriminant (Typ);
8192 while Present (D) loop
8193 if Has_Implicit_Dereference (D) then
8194 return D;
8195 end if;
8196 Next_Discriminant (D);
8197 end loop;
8198
8199 return Empty;
8200 end Get_Reference_Discriminant;
8201
8202 ---------------------------
8203 -- Get_Referenced_Object --
8204 ---------------------------
8205
8206 function Get_Referenced_Object (N : Node_Id) return Node_Id is
8207 R : Node_Id;
8208
8209 begin
8210 R := N;
8211 while Is_Entity_Name (R)
8212 and then Present (Renamed_Object (Entity (R)))
8213 loop
8214 R := Renamed_Object (Entity (R));
8215 end loop;
8216
8217 return R;
8218 end Get_Referenced_Object;
8219
8220 ------------------------
8221 -- Get_Renamed_Entity --
8222 ------------------------
8223
8224 function Get_Renamed_Entity (E : Entity_Id) return Entity_Id is
8225 R : Entity_Id;
8226
8227 begin
8228 R := E;
8229 while Present (Renamed_Entity (R)) loop
8230 R := Renamed_Entity (R);
8231 end loop;
8232
8233 return R;
8234 end Get_Renamed_Entity;
8235
8236 -----------------------
8237 -- Get_Return_Object --
8238 -----------------------
8239
8240 function Get_Return_Object (N : Node_Id) return Entity_Id is
8241 Decl : Node_Id;
8242
8243 begin
8244 Decl := First (Return_Object_Declarations (N));
8245 while Present (Decl) loop
8246 exit when Nkind (Decl) = N_Object_Declaration
8247 and then Is_Return_Object (Defining_Identifier (Decl));
8248 Next (Decl);
8249 end loop;
8250
8251 pragma Assert (Present (Decl));
8252 return Defining_Identifier (Decl);
8253 end Get_Return_Object;
8254
8255 ---------------------------
8256 -- Get_Subprogram_Entity --
8257 ---------------------------
8258
8259 function Get_Subprogram_Entity (Nod : Node_Id) return Entity_Id is
8260 Subp : Node_Id;
8261 Subp_Id : Entity_Id;
8262
8263 begin
8264 if Nkind (Nod) = N_Accept_Statement then
8265 Subp := Entry_Direct_Name (Nod);
8266
8267 elsif Nkind (Nod) = N_Slice then
8268 Subp := Prefix (Nod);
8269
8270 else
8271 Subp := Name (Nod);
8272 end if;
8273
8274 -- Strip the subprogram call
8275
8276 loop
8277 if Nkind_In (Subp, N_Explicit_Dereference,
8278 N_Indexed_Component,
8279 N_Selected_Component)
8280 then
8281 Subp := Prefix (Subp);
8282
8283 elsif Nkind_In (Subp, N_Type_Conversion,
8284 N_Unchecked_Type_Conversion)
8285 then
8286 Subp := Expression (Subp);
8287
8288 else
8289 exit;
8290 end if;
8291 end loop;
8292
8293 -- Extract the entity of the subprogram call
8294
8295 if Is_Entity_Name (Subp) then
8296 Subp_Id := Entity (Subp);
8297
8298 if Ekind (Subp_Id) = E_Access_Subprogram_Type then
8299 Subp_Id := Directly_Designated_Type (Subp_Id);
8300 end if;
8301
8302 if Is_Subprogram (Subp_Id) then
8303 return Subp_Id;
8304 else
8305 return Empty;
8306 end if;
8307
8308 -- The search did not find a construct that denotes a subprogram
8309
8310 else
8311 return Empty;
8312 end if;
8313 end Get_Subprogram_Entity;
8314
8315 -----------------------------
8316 -- Get_Task_Body_Procedure --
8317 -----------------------------
8318
8319 function Get_Task_Body_Procedure (E : Entity_Id) return Node_Id is
8320 begin
8321 -- Note: A task type may be the completion of a private type with
8322 -- discriminants. When performing elaboration checks on a task
8323 -- declaration, the current view of the type may be the private one,
8324 -- and the procedure that holds the body of the task is held in its
8325 -- underlying type.
8326
8327 -- This is an odd function, why not have Task_Body_Procedure do
8328 -- the following digging???
8329
8330 return Task_Body_Procedure (Underlying_Type (Root_Type (E)));
8331 end Get_Task_Body_Procedure;
8332
8333 -------------------------
8334 -- Get_User_Defined_Eq --
8335 -------------------------
8336
8337 function Get_User_Defined_Eq (E : Entity_Id) return Entity_Id is
8338 Prim : Elmt_Id;
8339 Op : Entity_Id;
8340
8341 begin
8342 Prim := First_Elmt (Collect_Primitive_Operations (E));
8343 while Present (Prim) loop
8344 Op := Node (Prim);
8345
8346 if Chars (Op) = Name_Op_Eq
8347 and then Etype (Op) = Standard_Boolean
8348 and then Etype (First_Formal (Op)) = E
8349 and then Etype (Next_Formal (First_Formal (Op))) = E
8350 then
8351 return Op;
8352 end if;
8353
8354 Next_Elmt (Prim);
8355 end loop;
8356
8357 return Empty;
8358 end Get_User_Defined_Eq;
8359
8360 -----------------------
8361 -- Has_Access_Values --
8362 -----------------------
8363
8364 function Has_Access_Values (T : Entity_Id) return Boolean is
8365 Typ : constant Entity_Id := Underlying_Type (T);
8366
8367 begin
8368 -- Case of a private type which is not completed yet. This can only
8369 -- happen in the case of a generic format type appearing directly, or
8370 -- as a component of the type to which this function is being applied
8371 -- at the top level. Return False in this case, since we certainly do
8372 -- not know that the type contains access types.
8373
8374 if No (Typ) then
8375 return False;
8376
8377 elsif Is_Access_Type (Typ) then
8378 return True;
8379
8380 elsif Is_Array_Type (Typ) then
8381 return Has_Access_Values (Component_Type (Typ));
8382
8383 elsif Is_Record_Type (Typ) then
8384 declare
8385 Comp : Entity_Id;
8386
8387 begin
8388 -- Loop to Check components
8389
8390 Comp := First_Component_Or_Discriminant (Typ);
8391 while Present (Comp) loop
8392
8393 -- Check for access component, tag field does not count, even
8394 -- though it is implemented internally using an access type.
8395
8396 if Has_Access_Values (Etype (Comp))
8397 and then Chars (Comp) /= Name_uTag
8398 then
8399 return True;
8400 end if;
8401
8402 Next_Component_Or_Discriminant (Comp);
8403 end loop;
8404 end;
8405
8406 return False;
8407
8408 else
8409 return False;
8410 end if;
8411 end Has_Access_Values;
8412
8413 ------------------------------
8414 -- Has_Compatible_Alignment --
8415 ------------------------------
8416
8417 function Has_Compatible_Alignment
8418 (Obj : Entity_Id;
8419 Expr : Node_Id;
8420 Layout_Done : Boolean) return Alignment_Result
8421 is
8422 function Has_Compatible_Alignment_Internal
8423 (Obj : Entity_Id;
8424 Expr : Node_Id;
8425 Layout_Done : Boolean;
8426 Default : Alignment_Result) return Alignment_Result;
8427 -- This is the internal recursive function that actually does the work.
8428 -- There is one additional parameter, which says what the result should
8429 -- be if no alignment information is found, and there is no definite
8430 -- indication of compatible alignments. At the outer level, this is set
8431 -- to Unknown, but for internal recursive calls in the case where types
8432 -- are known to be correct, it is set to Known_Compatible.
8433
8434 ---------------------------------------
8435 -- Has_Compatible_Alignment_Internal --
8436 ---------------------------------------
8437
8438 function Has_Compatible_Alignment_Internal
8439 (Obj : Entity_Id;
8440 Expr : Node_Id;
8441 Layout_Done : Boolean;
8442 Default : Alignment_Result) return Alignment_Result
8443 is
8444 Result : Alignment_Result := Known_Compatible;
8445 -- Holds the current status of the result. Note that once a value of
8446 -- Known_Incompatible is set, it is sticky and does not get changed
8447 -- to Unknown (the value in Result only gets worse as we go along,
8448 -- never better).
8449
8450 Offs : Uint := No_Uint;
8451 -- Set to a factor of the offset from the base object when Expr is a
8452 -- selected or indexed component, based on Component_Bit_Offset and
8453 -- Component_Size respectively. A negative value is used to represent
8454 -- a value which is not known at compile time.
8455
8456 procedure Check_Prefix;
8457 -- Checks the prefix recursively in the case where the expression
8458 -- is an indexed or selected component.
8459
8460 procedure Set_Result (R : Alignment_Result);
8461 -- If R represents a worse outcome (unknown instead of known
8462 -- compatible, or known incompatible), then set Result to R.
8463
8464 ------------------
8465 -- Check_Prefix --
8466 ------------------
8467
8468 procedure Check_Prefix is
8469 begin
8470 -- The subtlety here is that in doing a recursive call to check
8471 -- the prefix, we have to decide what to do in the case where we
8472 -- don't find any specific indication of an alignment problem.
8473
8474 -- At the outer level, we normally set Unknown as the result in
8475 -- this case, since we can only set Known_Compatible if we really
8476 -- know that the alignment value is OK, but for the recursive
8477 -- call, in the case where the types match, and we have not
8478 -- specified a peculiar alignment for the object, we are only
8479 -- concerned about suspicious rep clauses, the default case does
8480 -- not affect us, since the compiler will, in the absence of such
8481 -- rep clauses, ensure that the alignment is correct.
8482
8483 if Default = Known_Compatible
8484 or else
8485 (Etype (Obj) = Etype (Expr)
8486 and then (Unknown_Alignment (Obj)
8487 or else
8488 Alignment (Obj) = Alignment (Etype (Obj))))
8489 then
8490 Set_Result
8491 (Has_Compatible_Alignment_Internal
8492 (Obj, Prefix (Expr), Layout_Done, Known_Compatible));
8493
8494 -- In all other cases, we need a full check on the prefix
8495
8496 else
8497 Set_Result
8498 (Has_Compatible_Alignment_Internal
8499 (Obj, Prefix (Expr), Layout_Done, Unknown));
8500 end if;
8501 end Check_Prefix;
8502
8503 ----------------
8504 -- Set_Result --
8505 ----------------
8506
8507 procedure Set_Result (R : Alignment_Result) is
8508 begin
8509 if R > Result then
8510 Result := R;
8511 end if;
8512 end Set_Result;
8513
8514 -- Start of processing for Has_Compatible_Alignment_Internal
8515
8516 begin
8517 -- If Expr is a selected component, we must make sure there is no
8518 -- potentially troublesome component clause and that the record is
8519 -- not packed if the layout is not done.
8520
8521 if Nkind (Expr) = N_Selected_Component then
8522
8523 -- Packing generates unknown alignment if layout is not done
8524
8525 if Is_Packed (Etype (Prefix (Expr))) and then not Layout_Done then
8526 Set_Result (Unknown);
8527 end if;
8528
8529 -- Check prefix and component offset
8530
8531 Check_Prefix;
8532 Offs := Component_Bit_Offset (Entity (Selector_Name (Expr)));
8533
8534 -- If Expr is an indexed component, we must make sure there is no
8535 -- potentially troublesome Component_Size clause and that the array
8536 -- is not bit-packed if the layout is not done.
8537
8538 elsif Nkind (Expr) = N_Indexed_Component then
8539 declare
8540 Typ : constant Entity_Id := Etype (Prefix (Expr));
8541 Ind : constant Node_Id := First_Index (Typ);
8542
8543 begin
8544 -- Packing generates unknown alignment if layout is not done
8545
8546 if Is_Bit_Packed_Array (Typ) and then not Layout_Done then
8547 Set_Result (Unknown);
8548 end if;
8549
8550 -- Check prefix and component offset
8551
8552 Check_Prefix;
8553 Offs := Component_Size (Typ);
8554
8555 -- Small optimization: compute the full offset when possible
8556
8557 if Offs /= No_Uint
8558 and then Offs > Uint_0
8559 and then Present (Ind)
8560 and then Nkind (Ind) = N_Range
8561 and then Compile_Time_Known_Value (Low_Bound (Ind))
8562 and then Compile_Time_Known_Value (First (Expressions (Expr)))
8563 then
8564 Offs := Offs * (Expr_Value (First (Expressions (Expr)))
8565 - Expr_Value (Low_Bound ((Ind))));
8566 end if;
8567 end;
8568 end if;
8569
8570 -- If we have a null offset, the result is entirely determined by
8571 -- the base object and has already been computed recursively.
8572
8573 if Offs = Uint_0 then
8574 null;
8575
8576 -- Case where we know the alignment of the object
8577
8578 elsif Known_Alignment (Obj) then
8579 declare
8580 ObjA : constant Uint := Alignment (Obj);
8581 ExpA : Uint := No_Uint;
8582 SizA : Uint := No_Uint;
8583
8584 begin
8585 -- If alignment of Obj is 1, then we are always OK
8586
8587 if ObjA = 1 then
8588 Set_Result (Known_Compatible);
8589
8590 -- Alignment of Obj is greater than 1, so we need to check
8591
8592 else
8593 -- If we have an offset, see if it is compatible
8594
8595 if Offs /= No_Uint and Offs > Uint_0 then
8596 if Offs mod (System_Storage_Unit * ObjA) /= 0 then
8597 Set_Result (Known_Incompatible);
8598 end if;
8599
8600 -- See if Expr is an object with known alignment
8601
8602 elsif Is_Entity_Name (Expr)
8603 and then Known_Alignment (Entity (Expr))
8604 then
8605 ExpA := Alignment (Entity (Expr));
8606
8607 -- Otherwise, we can use the alignment of the type of
8608 -- Expr given that we already checked for
8609 -- discombobulating rep clauses for the cases of indexed
8610 -- and selected components above.
8611
8612 elsif Known_Alignment (Etype (Expr)) then
8613 ExpA := Alignment (Etype (Expr));
8614
8615 -- Otherwise the alignment is unknown
8616
8617 else
8618 Set_Result (Default);
8619 end if;
8620
8621 -- If we got an alignment, see if it is acceptable
8622
8623 if ExpA /= No_Uint and then ExpA < ObjA then
8624 Set_Result (Known_Incompatible);
8625 end if;
8626
8627 -- If Expr is not a piece of a larger object, see if size
8628 -- is given. If so, check that it is not too small for the
8629 -- required alignment.
8630
8631 if Offs /= No_Uint then
8632 null;
8633
8634 -- See if Expr is an object with known size
8635
8636 elsif Is_Entity_Name (Expr)
8637 and then Known_Static_Esize (Entity (Expr))
8638 then
8639 SizA := Esize (Entity (Expr));
8640
8641 -- Otherwise, we check the object size of the Expr type
8642
8643 elsif Known_Static_Esize (Etype (Expr)) then
8644 SizA := Esize (Etype (Expr));
8645 end if;
8646
8647 -- If we got a size, see if it is a multiple of the Obj
8648 -- alignment, if not, then the alignment cannot be
8649 -- acceptable, since the size is always a multiple of the
8650 -- alignment.
8651
8652 if SizA /= No_Uint then
8653 if SizA mod (ObjA * Ttypes.System_Storage_Unit) /= 0 then
8654 Set_Result (Known_Incompatible);
8655 end if;
8656 end if;
8657 end if;
8658 end;
8659
8660 -- If we do not know required alignment, any non-zero offset is a
8661 -- potential problem (but certainly may be OK, so result is unknown).
8662
8663 elsif Offs /= No_Uint then
8664 Set_Result (Unknown);
8665
8666 -- If we can't find the result by direct comparison of alignment
8667 -- values, then there is still one case that we can determine known
8668 -- result, and that is when we can determine that the types are the
8669 -- same, and no alignments are specified. Then we known that the
8670 -- alignments are compatible, even if we don't know the alignment
8671 -- value in the front end.
8672
8673 elsif Etype (Obj) = Etype (Expr) then
8674
8675 -- Types are the same, but we have to check for possible size
8676 -- and alignments on the Expr object that may make the alignment
8677 -- different, even though the types are the same.
8678
8679 if Is_Entity_Name (Expr) then
8680
8681 -- First check alignment of the Expr object. Any alignment less
8682 -- than Maximum_Alignment is worrisome since this is the case
8683 -- where we do not know the alignment of Obj.
8684
8685 if Known_Alignment (Entity (Expr))
8686 and then UI_To_Int (Alignment (Entity (Expr))) <
8687 Ttypes.Maximum_Alignment
8688 then
8689 Set_Result (Unknown);
8690
8691 -- Now check size of Expr object. Any size that is not an
8692 -- even multiple of Maximum_Alignment is also worrisome
8693 -- since it may cause the alignment of the object to be less
8694 -- than the alignment of the type.
8695
8696 elsif Known_Static_Esize (Entity (Expr))
8697 and then
8698 (UI_To_Int (Esize (Entity (Expr))) mod
8699 (Ttypes.Maximum_Alignment * Ttypes.System_Storage_Unit))
8700 /= 0
8701 then
8702 Set_Result (Unknown);
8703
8704 -- Otherwise same type is decisive
8705
8706 else
8707 Set_Result (Known_Compatible);
8708 end if;
8709 end if;
8710
8711 -- Another case to deal with is when there is an explicit size or
8712 -- alignment clause when the types are not the same. If so, then the
8713 -- result is Unknown. We don't need to do this test if the Default is
8714 -- Unknown, since that result will be set in any case.
8715
8716 elsif Default /= Unknown
8717 and then (Has_Size_Clause (Etype (Expr))
8718 or else
8719 Has_Alignment_Clause (Etype (Expr)))
8720 then
8721 Set_Result (Unknown);
8722
8723 -- If no indication found, set default
8724
8725 else
8726 Set_Result (Default);
8727 end if;
8728
8729 -- Return worst result found
8730
8731 return Result;
8732 end Has_Compatible_Alignment_Internal;
8733
8734 -- Start of processing for Has_Compatible_Alignment
8735
8736 begin
8737 -- If Obj has no specified alignment, then set alignment from the type
8738 -- alignment. Perhaps we should always do this, but for sure we should
8739 -- do it when there is an address clause since we can do more if the
8740 -- alignment is known.
8741
8742 if Unknown_Alignment (Obj) then
8743 Set_Alignment (Obj, Alignment (Etype (Obj)));
8744 end if;
8745
8746 -- Now do the internal call that does all the work
8747
8748 return
8749 Has_Compatible_Alignment_Internal (Obj, Expr, Layout_Done, Unknown);
8750 end Has_Compatible_Alignment;
8751
8752 ----------------------
8753 -- Has_Declarations --
8754 ----------------------
8755
8756 function Has_Declarations (N : Node_Id) return Boolean is
8757 begin
8758 return Nkind_In (Nkind (N), N_Accept_Statement,
8759 N_Block_Statement,
8760 N_Compilation_Unit_Aux,
8761 N_Entry_Body,
8762 N_Package_Body,
8763 N_Protected_Body,
8764 N_Subprogram_Body,
8765 N_Task_Body,
8766 N_Package_Specification);
8767 end Has_Declarations;
8768
8769 ---------------------------------
8770 -- Has_Defaulted_Discriminants --
8771 ---------------------------------
8772
8773 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean is
8774 begin
8775 return Has_Discriminants (Typ)
8776 and then Present (First_Discriminant (Typ))
8777 and then Present (Discriminant_Default_Value
8778 (First_Discriminant (Typ)));
8779 end Has_Defaulted_Discriminants;
8780
8781 -------------------
8782 -- Has_Denormals --
8783 -------------------
8784
8785 function Has_Denormals (E : Entity_Id) return Boolean is
8786 begin
8787 return Is_Floating_Point_Type (E) and then Denorm_On_Target;
8788 end Has_Denormals;
8789
8790 -------------------------------------------
8791 -- Has_Discriminant_Dependent_Constraint --
8792 -------------------------------------------
8793
8794 function Has_Discriminant_Dependent_Constraint
8795 (Comp : Entity_Id) return Boolean
8796 is
8797 Comp_Decl : constant Node_Id := Parent (Comp);
8798 Subt_Indic : Node_Id;
8799 Constr : Node_Id;
8800 Assn : Node_Id;
8801
8802 begin
8803 -- Discriminants can't depend on discriminants
8804
8805 if Ekind (Comp) = E_Discriminant then
8806 return False;
8807
8808 else
8809 Subt_Indic := Subtype_Indication (Component_Definition (Comp_Decl));
8810
8811 if Nkind (Subt_Indic) = N_Subtype_Indication then
8812 Constr := Constraint (Subt_Indic);
8813
8814 if Nkind (Constr) = N_Index_Or_Discriminant_Constraint then
8815 Assn := First (Constraints (Constr));
8816 while Present (Assn) loop
8817 case Nkind (Assn) is
8818 when N_Subtype_Indication |
8819 N_Range |
8820 N_Identifier
8821 =>
8822 if Depends_On_Discriminant (Assn) then
8823 return True;
8824 end if;
8825
8826 when N_Discriminant_Association =>
8827 if Depends_On_Discriminant (Expression (Assn)) then
8828 return True;
8829 end if;
8830
8831 when others =>
8832 null;
8833 end case;
8834
8835 Next (Assn);
8836 end loop;
8837 end if;
8838 end if;
8839 end if;
8840
8841 return False;
8842 end Has_Discriminant_Dependent_Constraint;
8843
8844 --------------------------------------
8845 -- Has_Effectively_Volatile_Profile --
8846 --------------------------------------
8847
8848 function Has_Effectively_Volatile_Profile
8849 (Subp_Id : Entity_Id) return Boolean
8850 is
8851 Formal : Entity_Id;
8852
8853 begin
8854 -- Inspect the formal parameters looking for an effectively volatile
8855 -- type.
8856
8857 Formal := First_Formal (Subp_Id);
8858 while Present (Formal) loop
8859 if Is_Effectively_Volatile (Etype (Formal)) then
8860 return True;
8861 end if;
8862
8863 Next_Formal (Formal);
8864 end loop;
8865
8866 -- Inspect the return type of functions
8867
8868 if Ekind_In (Subp_Id, E_Function, E_Generic_Function)
8869 and then Is_Effectively_Volatile (Etype (Subp_Id))
8870 then
8871 return True;
8872 end if;
8873
8874 return False;
8875 end Has_Effectively_Volatile_Profile;
8876
8877 --------------------------
8878 -- Has_Enabled_Property --
8879 --------------------------
8880
8881 function Has_Enabled_Property
8882 (Item_Id : Entity_Id;
8883 Property : Name_Id) return Boolean
8884 is
8885 function State_Has_Enabled_Property return Boolean;
8886 -- Determine whether a state denoted by Item_Id has the property enabled
8887
8888 function Variable_Has_Enabled_Property return Boolean;
8889 -- Determine whether a variable denoted by Item_Id has the property
8890 -- enabled.
8891
8892 --------------------------------
8893 -- State_Has_Enabled_Property --
8894 --------------------------------
8895
8896 function State_Has_Enabled_Property return Boolean is
8897 Decl : constant Node_Id := Parent (Item_Id);
8898 Opt : Node_Id;
8899 Opt_Nam : Node_Id;
8900 Prop : Node_Id;
8901 Prop_Nam : Node_Id;
8902 Props : Node_Id;
8903
8904 begin
8905 -- The declaration of an external abstract state appears as an
8906 -- extension aggregate. If this is not the case, properties can never
8907 -- be set.
8908
8909 if Nkind (Decl) /= N_Extension_Aggregate then
8910 return False;
8911 end if;
8912
8913 -- When External appears as a simple option, it automatically enables
8914 -- all properties.
8915
8916 Opt := First (Expressions (Decl));
8917 while Present (Opt) loop
8918 if Nkind (Opt) = N_Identifier
8919 and then Chars (Opt) = Name_External
8920 then
8921 return True;
8922 end if;
8923
8924 Next (Opt);
8925 end loop;
8926
8927 -- When External specifies particular properties, inspect those and
8928 -- find the desired one (if any).
8929
8930 Opt := First (Component_Associations (Decl));
8931 while Present (Opt) loop
8932 Opt_Nam := First (Choices (Opt));
8933
8934 if Nkind (Opt_Nam) = N_Identifier
8935 and then Chars (Opt_Nam) = Name_External
8936 then
8937 Props := Expression (Opt);
8938
8939 -- Multiple properties appear as an aggregate
8940
8941 if Nkind (Props) = N_Aggregate then
8942
8943 -- Simple property form
8944
8945 Prop := First (Expressions (Props));
8946 while Present (Prop) loop
8947 if Chars (Prop) = Property then
8948 return True;
8949 end if;
8950
8951 Next (Prop);
8952 end loop;
8953
8954 -- Property with expression form
8955
8956 Prop := First (Component_Associations (Props));
8957 while Present (Prop) loop
8958 Prop_Nam := First (Choices (Prop));
8959
8960 -- The property can be represented in two ways:
8961 -- others => <value>
8962 -- <property> => <value>
8963
8964 if Nkind (Prop_Nam) = N_Others_Choice
8965 or else (Nkind (Prop_Nam) = N_Identifier
8966 and then Chars (Prop_Nam) = Property)
8967 then
8968 return Is_True (Expr_Value (Expression (Prop)));
8969 end if;
8970
8971 Next (Prop);
8972 end loop;
8973
8974 -- Single property
8975
8976 else
8977 return Chars (Props) = Property;
8978 end if;
8979 end if;
8980
8981 Next (Opt);
8982 end loop;
8983
8984 return False;
8985 end State_Has_Enabled_Property;
8986
8987 -----------------------------------
8988 -- Variable_Has_Enabled_Property --
8989 -----------------------------------
8990
8991 function Variable_Has_Enabled_Property return Boolean is
8992 function Is_Enabled (Prag : Node_Id) return Boolean;
8993 -- Determine whether property pragma Prag (if present) denotes an
8994 -- enabled property.
8995
8996 ----------------
8997 -- Is_Enabled --
8998 ----------------
8999
9000 function Is_Enabled (Prag : Node_Id) return Boolean is
9001 Arg1 : Node_Id;
9002
9003 begin
9004 if Present (Prag) then
9005 Arg1 := First (Pragma_Argument_Associations (Prag));
9006
9007 -- The pragma has an optional Boolean expression, the related
9008 -- property is enabled only when the expression evaluates to
9009 -- True.
9010
9011 if Present (Arg1) then
9012 return Is_True (Expr_Value (Get_Pragma_Arg (Arg1)));
9013
9014 -- Otherwise the lack of expression enables the property by
9015 -- default.
9016
9017 else
9018 return True;
9019 end if;
9020
9021 -- The property was never set in the first place
9022
9023 else
9024 return False;
9025 end if;
9026 end Is_Enabled;
9027
9028 -- Local variables
9029
9030 AR : constant Node_Id :=
9031 Get_Pragma (Item_Id, Pragma_Async_Readers);
9032 AW : constant Node_Id :=
9033 Get_Pragma (Item_Id, Pragma_Async_Writers);
9034 ER : constant Node_Id :=
9035 Get_Pragma (Item_Id, Pragma_Effective_Reads);
9036 EW : constant Node_Id :=
9037 Get_Pragma (Item_Id, Pragma_Effective_Writes);
9038
9039 -- Start of processing for Variable_Has_Enabled_Property
9040
9041 begin
9042 -- A non-effectively volatile object can never possess external
9043 -- properties.
9044
9045 if not Is_Effectively_Volatile (Item_Id) then
9046 return False;
9047
9048 -- External properties related to variables come in two flavors -
9049 -- explicit and implicit. The explicit case is characterized by the
9050 -- presence of a property pragma with an optional Boolean flag. The
9051 -- property is enabled when the flag evaluates to True or the flag is
9052 -- missing altogether.
9053
9054 elsif Property = Name_Async_Readers and then Is_Enabled (AR) then
9055 return True;
9056
9057 elsif Property = Name_Async_Writers and then Is_Enabled (AW) then
9058 return True;
9059
9060 elsif Property = Name_Effective_Reads and then Is_Enabled (ER) then
9061 return True;
9062
9063 elsif Property = Name_Effective_Writes and then Is_Enabled (EW) then
9064 return True;
9065
9066 -- The implicit case lacks all property pragmas
9067
9068 elsif No (AR) and then No (AW) and then No (ER) and then No (EW) then
9069 return True;
9070
9071 else
9072 return False;
9073 end if;
9074 end Variable_Has_Enabled_Property;
9075
9076 -- Start of processing for Has_Enabled_Property
9077
9078 begin
9079 -- Abstract states and variables have a flexible scheme of specifying
9080 -- external properties.
9081
9082 if Ekind (Item_Id) = E_Abstract_State then
9083 return State_Has_Enabled_Property;
9084
9085 elsif Ekind (Item_Id) = E_Variable then
9086 return Variable_Has_Enabled_Property;
9087
9088 -- Otherwise a property is enabled when the related item is effectively
9089 -- volatile.
9090
9091 else
9092 return Is_Effectively_Volatile (Item_Id);
9093 end if;
9094 end Has_Enabled_Property;
9095
9096 -------------------------------------
9097 -- Has_Full_Default_Initialization --
9098 -------------------------------------
9099
9100 function Has_Full_Default_Initialization (Typ : Entity_Id) return Boolean is
9101 Arg : Node_Id;
9102 Comp : Entity_Id;
9103 Prag : Node_Id;
9104
9105 begin
9106 -- A private type and its full view is fully default initialized when it
9107 -- is subject to pragma Default_Initial_Condition without an argument or
9108 -- with a non-null argument. Since any type may act as the full view of
9109 -- a private type, this check must be performed prior to the specialized
9110 -- tests below.
9111
9112 if Has_Default_Init_Cond (Typ)
9113 or else Has_Inherited_Default_Init_Cond (Typ)
9114 then
9115 Prag := Get_Pragma (Typ, Pragma_Default_Initial_Condition);
9116
9117 -- Pragma Default_Initial_Condition must be present if one of the
9118 -- related entity flags is set.
9119
9120 pragma Assert (Present (Prag));
9121 Arg := First (Pragma_Argument_Associations (Prag));
9122
9123 -- A non-null argument guarantees full default initialization
9124
9125 if Present (Arg) then
9126 return Nkind (Arg) /= N_Null;
9127
9128 -- Otherwise the missing argument defaults the pragma to "True" which
9129 -- is considered a non-null argument (see above).
9130
9131 else
9132 return True;
9133 end if;
9134 end if;
9135
9136 -- A scalar type is fully default initialized if it is subject to aspect
9137 -- Default_Value.
9138
9139 if Is_Scalar_Type (Typ) then
9140 return Has_Default_Aspect (Typ);
9141
9142 -- An array type is fully default initialized if its element type is
9143 -- scalar and the array type carries aspect Default_Component_Value or
9144 -- the element type is fully default initialized.
9145
9146 elsif Is_Array_Type (Typ) then
9147 return
9148 Has_Default_Aspect (Typ)
9149 or else Has_Full_Default_Initialization (Component_Type (Typ));
9150
9151 -- A protected type, record type or type extension is fully default
9152 -- initialized if all its components either carry an initialization
9153 -- expression or have a type that is fully default initialized. The
9154 -- parent type of a type extension must be fully default initialized.
9155
9156 elsif Is_Record_Type (Typ) or else Is_Protected_Type (Typ) then
9157
9158 -- Inspect all entities defined in the scope of the type, looking for
9159 -- uninitialized components.
9160
9161 Comp := First_Entity (Typ);
9162 while Present (Comp) loop
9163 if Ekind (Comp) = E_Component
9164 and then Comes_From_Source (Comp)
9165 and then No (Expression (Parent (Comp)))
9166 and then not Has_Full_Default_Initialization (Etype (Comp))
9167 then
9168 return False;
9169 end if;
9170
9171 Next_Entity (Comp);
9172 end loop;
9173
9174 -- Ensure that the parent type of a type extension is fully default
9175 -- initialized.
9176
9177 if Etype (Typ) /= Typ
9178 and then not Has_Full_Default_Initialization (Etype (Typ))
9179 then
9180 return False;
9181 end if;
9182
9183 -- If we get here, then all components and parent portion are fully
9184 -- default initialized.
9185
9186 return True;
9187
9188 -- A task type is fully default initialized by default
9189
9190 elsif Is_Task_Type (Typ) then
9191 return True;
9192
9193 -- Otherwise the type is not fully default initialized
9194
9195 else
9196 return False;
9197 end if;
9198 end Has_Full_Default_Initialization;
9199
9200 --------------------
9201 -- Has_Infinities --
9202 --------------------
9203
9204 function Has_Infinities (E : Entity_Id) return Boolean is
9205 begin
9206 return
9207 Is_Floating_Point_Type (E)
9208 and then Nkind (Scalar_Range (E)) = N_Range
9209 and then Includes_Infinities (Scalar_Range (E));
9210 end Has_Infinities;
9211
9212 --------------------
9213 -- Has_Interfaces --
9214 --------------------
9215
9216 function Has_Interfaces
9217 (T : Entity_Id;
9218 Use_Full_View : Boolean := True) return Boolean
9219 is
9220 Typ : Entity_Id := Base_Type (T);
9221
9222 begin
9223 -- Handle concurrent types
9224
9225 if Is_Concurrent_Type (Typ) then
9226 Typ := Corresponding_Record_Type (Typ);
9227 end if;
9228
9229 if not Present (Typ)
9230 or else not Is_Record_Type (Typ)
9231 or else not Is_Tagged_Type (Typ)
9232 then
9233 return False;
9234 end if;
9235
9236 -- Handle private types
9237
9238 if Use_Full_View and then Present (Full_View (Typ)) then
9239 Typ := Full_View (Typ);
9240 end if;
9241
9242 -- Handle concurrent record types
9243
9244 if Is_Concurrent_Record_Type (Typ)
9245 and then Is_Non_Empty_List (Abstract_Interface_List (Typ))
9246 then
9247 return True;
9248 end if;
9249
9250 loop
9251 if Is_Interface (Typ)
9252 or else
9253 (Is_Record_Type (Typ)
9254 and then Present (Interfaces (Typ))
9255 and then not Is_Empty_Elmt_List (Interfaces (Typ)))
9256 then
9257 return True;
9258 end if;
9259
9260 exit when Etype (Typ) = Typ
9261
9262 -- Handle private types
9263
9264 or else (Present (Full_View (Etype (Typ)))
9265 and then Full_View (Etype (Typ)) = Typ)
9266
9267 -- Protect frontend against wrong sources with cyclic derivations
9268
9269 or else Etype (Typ) = T;
9270
9271 -- Climb to the ancestor type handling private types
9272
9273 if Present (Full_View (Etype (Typ))) then
9274 Typ := Full_View (Etype (Typ));
9275 else
9276 Typ := Etype (Typ);
9277 end if;
9278 end loop;
9279
9280 return False;
9281 end Has_Interfaces;
9282
9283 ---------------------------------
9284 -- Has_No_Obvious_Side_Effects --
9285 ---------------------------------
9286
9287 function Has_No_Obvious_Side_Effects (N : Node_Id) return Boolean is
9288 begin
9289 -- For now, just handle literals, constants, and non-volatile
9290 -- variables and expressions combining these with operators or
9291 -- short circuit forms.
9292
9293 if Nkind (N) in N_Numeric_Or_String_Literal then
9294 return True;
9295
9296 elsif Nkind (N) = N_Character_Literal then
9297 return True;
9298
9299 elsif Nkind (N) in N_Unary_Op then
9300 return Has_No_Obvious_Side_Effects (Right_Opnd (N));
9301
9302 elsif Nkind (N) in N_Binary_Op or else Nkind (N) in N_Short_Circuit then
9303 return Has_No_Obvious_Side_Effects (Left_Opnd (N))
9304 and then
9305 Has_No_Obvious_Side_Effects (Right_Opnd (N));
9306
9307 elsif Nkind (N) = N_Expression_With_Actions
9308 and then Is_Empty_List (Actions (N))
9309 then
9310 return Has_No_Obvious_Side_Effects (Expression (N));
9311
9312 elsif Nkind (N) in N_Has_Entity then
9313 return Present (Entity (N))
9314 and then Ekind_In (Entity (N), E_Variable,
9315 E_Constant,
9316 E_Enumeration_Literal,
9317 E_In_Parameter,
9318 E_Out_Parameter,
9319 E_In_Out_Parameter)
9320 and then not Is_Volatile (Entity (N));
9321
9322 else
9323 return False;
9324 end if;
9325 end Has_No_Obvious_Side_Effects;
9326
9327 -----------------------------
9328 -- Has_Non_Null_Refinement --
9329 -----------------------------
9330
9331 function Has_Non_Null_Refinement (Id : Entity_Id) return Boolean is
9332 begin
9333 pragma Assert (Ekind (Id) = E_Abstract_State);
9334
9335 -- For a refinement to be non-null, the first constituent must be
9336 -- anything other than null.
9337
9338 if Present (Refinement_Constituents (Id)) then
9339 return
9340 Nkind (Node (First_Elmt (Refinement_Constituents (Id)))) /= N_Null;
9341 end if;
9342
9343 return False;
9344 end Has_Non_Null_Refinement;
9345
9346 ------------------------
9347 -- Has_Null_Exclusion --
9348 ------------------------
9349
9350 function Has_Null_Exclusion (N : Node_Id) return Boolean is
9351 begin
9352 case Nkind (N) is
9353 when N_Access_Definition |
9354 N_Access_Function_Definition |
9355 N_Access_Procedure_Definition |
9356 N_Access_To_Object_Definition |
9357 N_Allocator |
9358 N_Derived_Type_Definition |
9359 N_Function_Specification |
9360 N_Subtype_Declaration =>
9361 return Null_Exclusion_Present (N);
9362
9363 when N_Component_Definition |
9364 N_Formal_Object_Declaration |
9365 N_Object_Renaming_Declaration =>
9366 if Present (Subtype_Mark (N)) then
9367 return Null_Exclusion_Present (N);
9368 else pragma Assert (Present (Access_Definition (N)));
9369 return Null_Exclusion_Present (Access_Definition (N));
9370 end if;
9371
9372 when N_Discriminant_Specification =>
9373 if Nkind (Discriminant_Type (N)) = N_Access_Definition then
9374 return Null_Exclusion_Present (Discriminant_Type (N));
9375 else
9376 return Null_Exclusion_Present (N);
9377 end if;
9378
9379 when N_Object_Declaration =>
9380 if Nkind (Object_Definition (N)) = N_Access_Definition then
9381 return Null_Exclusion_Present (Object_Definition (N));
9382 else
9383 return Null_Exclusion_Present (N);
9384 end if;
9385
9386 when N_Parameter_Specification =>
9387 if Nkind (Parameter_Type (N)) = N_Access_Definition then
9388 return Null_Exclusion_Present (Parameter_Type (N));
9389 else
9390 return Null_Exclusion_Present (N);
9391 end if;
9392
9393 when others =>
9394 return False;
9395
9396 end case;
9397 end Has_Null_Exclusion;
9398
9399 ------------------------
9400 -- Has_Null_Extension --
9401 ------------------------
9402
9403 function Has_Null_Extension (T : Entity_Id) return Boolean is
9404 B : constant Entity_Id := Base_Type (T);
9405 Comps : Node_Id;
9406 Ext : Node_Id;
9407
9408 begin
9409 if Nkind (Parent (B)) = N_Full_Type_Declaration
9410 and then Present (Record_Extension_Part (Type_Definition (Parent (B))))
9411 then
9412 Ext := Record_Extension_Part (Type_Definition (Parent (B)));
9413
9414 if Present (Ext) then
9415 if Null_Present (Ext) then
9416 return True;
9417 else
9418 Comps := Component_List (Ext);
9419
9420 -- The null component list is rewritten during analysis to
9421 -- include the parent component. Any other component indicates
9422 -- that the extension was not originally null.
9423
9424 return Null_Present (Comps)
9425 or else No (Next (First (Component_Items (Comps))));
9426 end if;
9427 else
9428 return False;
9429 end if;
9430
9431 else
9432 return False;
9433 end if;
9434 end Has_Null_Extension;
9435
9436 -------------------------
9437 -- Has_Null_Refinement --
9438 -------------------------
9439
9440 function Has_Null_Refinement (Id : Entity_Id) return Boolean is
9441 begin
9442 pragma Assert (Ekind (Id) = E_Abstract_State);
9443
9444 -- For a refinement to be null, the state's sole constituent must be a
9445 -- null.
9446
9447 if Present (Refinement_Constituents (Id)) then
9448 return
9449 Nkind (Node (First_Elmt (Refinement_Constituents (Id)))) = N_Null;
9450 end if;
9451
9452 return False;
9453 end Has_Null_Refinement;
9454
9455 -------------------------------
9456 -- Has_Overriding_Initialize --
9457 -------------------------------
9458
9459 function Has_Overriding_Initialize (T : Entity_Id) return Boolean is
9460 BT : constant Entity_Id := Base_Type (T);
9461 P : Elmt_Id;
9462
9463 begin
9464 if Is_Controlled (BT) then
9465 if Is_RTU (Scope (BT), Ada_Finalization) then
9466 return False;
9467
9468 elsif Present (Primitive_Operations (BT)) then
9469 P := First_Elmt (Primitive_Operations (BT));
9470 while Present (P) loop
9471 declare
9472 Init : constant Entity_Id := Node (P);
9473 Formal : constant Entity_Id := First_Formal (Init);
9474 begin
9475 if Ekind (Init) = E_Procedure
9476 and then Chars (Init) = Name_Initialize
9477 and then Comes_From_Source (Init)
9478 and then Present (Formal)
9479 and then Etype (Formal) = BT
9480 and then No (Next_Formal (Formal))
9481 and then (Ada_Version < Ada_2012
9482 or else not Null_Present (Parent (Init)))
9483 then
9484 return True;
9485 end if;
9486 end;
9487
9488 Next_Elmt (P);
9489 end loop;
9490 end if;
9491
9492 -- Here if type itself does not have a non-null Initialize operation:
9493 -- check immediate ancestor.
9494
9495 if Is_Derived_Type (BT)
9496 and then Has_Overriding_Initialize (Etype (BT))
9497 then
9498 return True;
9499 end if;
9500 end if;
9501
9502 return False;
9503 end Has_Overriding_Initialize;
9504
9505 --------------------------------------
9506 -- Has_Preelaborable_Initialization --
9507 --------------------------------------
9508
9509 function Has_Preelaborable_Initialization (E : Entity_Id) return Boolean is
9510 Has_PE : Boolean;
9511
9512 procedure Check_Components (E : Entity_Id);
9513 -- Check component/discriminant chain, sets Has_PE False if a component
9514 -- or discriminant does not meet the preelaborable initialization rules.
9515
9516 ----------------------
9517 -- Check_Components --
9518 ----------------------
9519
9520 procedure Check_Components (E : Entity_Id) is
9521 Ent : Entity_Id;
9522 Exp : Node_Id;
9523
9524 function Is_Preelaborable_Expression (N : Node_Id) return Boolean;
9525 -- Returns True if and only if the expression denoted by N does not
9526 -- violate restrictions on preelaborable constructs (RM-10.2.1(5-9)).
9527
9528 ---------------------------------
9529 -- Is_Preelaborable_Expression --
9530 ---------------------------------
9531
9532 function Is_Preelaborable_Expression (N : Node_Id) return Boolean is
9533 Exp : Node_Id;
9534 Assn : Node_Id;
9535 Choice : Node_Id;
9536 Comp_Type : Entity_Id;
9537 Is_Array_Aggr : Boolean;
9538
9539 begin
9540 if Is_OK_Static_Expression (N) then
9541 return True;
9542
9543 elsif Nkind (N) = N_Null then
9544 return True;
9545
9546 -- Attributes are allowed in general, even if their prefix is a
9547 -- formal type. (It seems that certain attributes known not to be
9548 -- static might not be allowed, but there are no rules to prevent
9549 -- them.)
9550
9551 elsif Nkind (N) = N_Attribute_Reference then
9552 return True;
9553
9554 -- The name of a discriminant evaluated within its parent type is
9555 -- defined to be preelaborable (10.2.1(8)). Note that we test for
9556 -- names that denote discriminals as well as discriminants to
9557 -- catch references occurring within init procs.
9558
9559 elsif Is_Entity_Name (N)
9560 and then
9561 (Ekind (Entity (N)) = E_Discriminant
9562 or else (Ekind_In (Entity (N), E_Constant, E_In_Parameter)
9563 and then Present (Discriminal_Link (Entity (N)))))
9564 then
9565 return True;
9566
9567 elsif Nkind (N) = N_Qualified_Expression then
9568 return Is_Preelaborable_Expression (Expression (N));
9569
9570 -- For aggregates we have to check that each of the associations
9571 -- is preelaborable.
9572
9573 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
9574 Is_Array_Aggr := Is_Array_Type (Etype (N));
9575
9576 if Is_Array_Aggr then
9577 Comp_Type := Component_Type (Etype (N));
9578 end if;
9579
9580 -- Check the ancestor part of extension aggregates, which must
9581 -- be either the name of a type that has preelaborable init or
9582 -- an expression that is preelaborable.
9583
9584 if Nkind (N) = N_Extension_Aggregate then
9585 declare
9586 Anc_Part : constant Node_Id := Ancestor_Part (N);
9587
9588 begin
9589 if Is_Entity_Name (Anc_Part)
9590 and then Is_Type (Entity (Anc_Part))
9591 then
9592 if not Has_Preelaborable_Initialization
9593 (Entity (Anc_Part))
9594 then
9595 return False;
9596 end if;
9597
9598 elsif not Is_Preelaborable_Expression (Anc_Part) then
9599 return False;
9600 end if;
9601 end;
9602 end if;
9603
9604 -- Check positional associations
9605
9606 Exp := First (Expressions (N));
9607 while Present (Exp) loop
9608 if not Is_Preelaborable_Expression (Exp) then
9609 return False;
9610 end if;
9611
9612 Next (Exp);
9613 end loop;
9614
9615 -- Check named associations
9616
9617 Assn := First (Component_Associations (N));
9618 while Present (Assn) loop
9619 Choice := First (Choices (Assn));
9620 while Present (Choice) loop
9621 if Is_Array_Aggr then
9622 if Nkind (Choice) = N_Others_Choice then
9623 null;
9624
9625 elsif Nkind (Choice) = N_Range then
9626 if not Is_OK_Static_Range (Choice) then
9627 return False;
9628 end if;
9629
9630 elsif not Is_OK_Static_Expression (Choice) then
9631 return False;
9632 end if;
9633
9634 else
9635 Comp_Type := Etype (Choice);
9636 end if;
9637
9638 Next (Choice);
9639 end loop;
9640
9641 -- If the association has a <> at this point, then we have
9642 -- to check whether the component's type has preelaborable
9643 -- initialization. Note that this only occurs when the
9644 -- association's corresponding component does not have a
9645 -- default expression, the latter case having already been
9646 -- expanded as an expression for the association.
9647
9648 if Box_Present (Assn) then
9649 if not Has_Preelaborable_Initialization (Comp_Type) then
9650 return False;
9651 end if;
9652
9653 -- In the expression case we check whether the expression
9654 -- is preelaborable.
9655
9656 elsif
9657 not Is_Preelaborable_Expression (Expression (Assn))
9658 then
9659 return False;
9660 end if;
9661
9662 Next (Assn);
9663 end loop;
9664
9665 -- If we get here then aggregate as a whole is preelaborable
9666
9667 return True;
9668
9669 -- All other cases are not preelaborable
9670
9671 else
9672 return False;
9673 end if;
9674 end Is_Preelaborable_Expression;
9675
9676 -- Start of processing for Check_Components
9677
9678 begin
9679 -- Loop through entities of record or protected type
9680
9681 Ent := E;
9682 while Present (Ent) loop
9683
9684 -- We are interested only in components and discriminants
9685
9686 Exp := Empty;
9687
9688 case Ekind (Ent) is
9689 when E_Component =>
9690
9691 -- Get default expression if any. If there is no declaration
9692 -- node, it means we have an internal entity. The parent and
9693 -- tag fields are examples of such entities. For such cases,
9694 -- we just test the type of the entity.
9695
9696 if Present (Declaration_Node (Ent)) then
9697 Exp := Expression (Declaration_Node (Ent));
9698 end if;
9699
9700 when E_Discriminant =>
9701
9702 -- Note: for a renamed discriminant, the Declaration_Node
9703 -- may point to the one from the ancestor, and have a
9704 -- different expression, so use the proper attribute to
9705 -- retrieve the expression from the derived constraint.
9706
9707 Exp := Discriminant_Default_Value (Ent);
9708
9709 when others =>
9710 goto Check_Next_Entity;
9711 end case;
9712
9713 -- A component has PI if it has no default expression and the
9714 -- component type has PI.
9715
9716 if No (Exp) then
9717 if not Has_Preelaborable_Initialization (Etype (Ent)) then
9718 Has_PE := False;
9719 exit;
9720 end if;
9721
9722 -- Require the default expression to be preelaborable
9723
9724 elsif not Is_Preelaborable_Expression (Exp) then
9725 Has_PE := False;
9726 exit;
9727 end if;
9728
9729 <<Check_Next_Entity>>
9730 Next_Entity (Ent);
9731 end loop;
9732 end Check_Components;
9733
9734 -- Start of processing for Has_Preelaborable_Initialization
9735
9736 begin
9737 -- Immediate return if already marked as known preelaborable init. This
9738 -- covers types for which this function has already been called once
9739 -- and returned True (in which case the result is cached), and also
9740 -- types to which a pragma Preelaborable_Initialization applies.
9741
9742 if Known_To_Have_Preelab_Init (E) then
9743 return True;
9744 end if;
9745
9746 -- If the type is a subtype representing a generic actual type, then
9747 -- test whether its base type has preelaborable initialization since
9748 -- the subtype representing the actual does not inherit this attribute
9749 -- from the actual or formal. (but maybe it should???)
9750
9751 if Is_Generic_Actual_Type (E) then
9752 return Has_Preelaborable_Initialization (Base_Type (E));
9753 end if;
9754
9755 -- All elementary types have preelaborable initialization
9756
9757 if Is_Elementary_Type (E) then
9758 Has_PE := True;
9759
9760 -- Array types have PI if the component type has PI
9761
9762 elsif Is_Array_Type (E) then
9763 Has_PE := Has_Preelaborable_Initialization (Component_Type (E));
9764
9765 -- A derived type has preelaborable initialization if its parent type
9766 -- has preelaborable initialization and (in the case of a derived record
9767 -- extension) if the non-inherited components all have preelaborable
9768 -- initialization. However, a user-defined controlled type with an
9769 -- overriding Initialize procedure does not have preelaborable
9770 -- initialization.
9771
9772 elsif Is_Derived_Type (E) then
9773
9774 -- If the derived type is a private extension then it doesn't have
9775 -- preelaborable initialization.
9776
9777 if Ekind (Base_Type (E)) = E_Record_Type_With_Private then
9778 return False;
9779 end if;
9780
9781 -- First check whether ancestor type has preelaborable initialization
9782
9783 Has_PE := Has_Preelaborable_Initialization (Etype (Base_Type (E)));
9784
9785 -- If OK, check extension components (if any)
9786
9787 if Has_PE and then Is_Record_Type (E) then
9788 Check_Components (First_Entity (E));
9789 end if;
9790
9791 -- Check specifically for 10.2.1(11.4/2) exception: a controlled type
9792 -- with a user defined Initialize procedure does not have PI. If
9793 -- the type is untagged, the control primitives come from a component
9794 -- that has already been checked.
9795
9796 if Has_PE
9797 and then Is_Controlled (E)
9798 and then Is_Tagged_Type (E)
9799 and then Has_Overriding_Initialize (E)
9800 then
9801 Has_PE := False;
9802 end if;
9803
9804 -- Private types not derived from a type having preelaborable init and
9805 -- that are not marked with pragma Preelaborable_Initialization do not
9806 -- have preelaborable initialization.
9807
9808 elsif Is_Private_Type (E) then
9809 return False;
9810
9811 -- Record type has PI if it is non private and all components have PI
9812
9813 elsif Is_Record_Type (E) then
9814 Has_PE := True;
9815 Check_Components (First_Entity (E));
9816
9817 -- Protected types must not have entries, and components must meet
9818 -- same set of rules as for record components.
9819
9820 elsif Is_Protected_Type (E) then
9821 if Has_Entries (E) then
9822 Has_PE := False;
9823 else
9824 Has_PE := True;
9825 Check_Components (First_Entity (E));
9826 Check_Components (First_Private_Entity (E));
9827 end if;
9828
9829 -- Type System.Address always has preelaborable initialization
9830
9831 elsif Is_RTE (E, RE_Address) then
9832 Has_PE := True;
9833
9834 -- In all other cases, type does not have preelaborable initialization
9835
9836 else
9837 return False;
9838 end if;
9839
9840 -- If type has preelaborable initialization, cache result
9841
9842 if Has_PE then
9843 Set_Known_To_Have_Preelab_Init (E);
9844 end if;
9845
9846 return Has_PE;
9847 end Has_Preelaborable_Initialization;
9848
9849 ---------------------------
9850 -- Has_Private_Component --
9851 ---------------------------
9852
9853 function Has_Private_Component (Type_Id : Entity_Id) return Boolean is
9854 Btype : Entity_Id := Base_Type (Type_Id);
9855 Component : Entity_Id;
9856
9857 begin
9858 if Error_Posted (Type_Id)
9859 or else Error_Posted (Btype)
9860 then
9861 return False;
9862 end if;
9863
9864 if Is_Class_Wide_Type (Btype) then
9865 Btype := Root_Type (Btype);
9866 end if;
9867
9868 if Is_Private_Type (Btype) then
9869 declare
9870 UT : constant Entity_Id := Underlying_Type (Btype);
9871 begin
9872 if No (UT) then
9873 if No (Full_View (Btype)) then
9874 return not Is_Generic_Type (Btype)
9875 and then
9876 not Is_Generic_Type (Root_Type (Btype));
9877 else
9878 return not Is_Generic_Type (Root_Type (Full_View (Btype)));
9879 end if;
9880 else
9881 return not Is_Frozen (UT) and then Has_Private_Component (UT);
9882 end if;
9883 end;
9884
9885 elsif Is_Array_Type (Btype) then
9886 return Has_Private_Component (Component_Type (Btype));
9887
9888 elsif Is_Record_Type (Btype) then
9889 Component := First_Component (Btype);
9890 while Present (Component) loop
9891 if Has_Private_Component (Etype (Component)) then
9892 return True;
9893 end if;
9894
9895 Next_Component (Component);
9896 end loop;
9897
9898 return False;
9899
9900 elsif Is_Protected_Type (Btype)
9901 and then Present (Corresponding_Record_Type (Btype))
9902 then
9903 return Has_Private_Component (Corresponding_Record_Type (Btype));
9904
9905 else
9906 return False;
9907 end if;
9908 end Has_Private_Component;
9909
9910 ----------------------
9911 -- Has_Signed_Zeros --
9912 ----------------------
9913
9914 function Has_Signed_Zeros (E : Entity_Id) return Boolean is
9915 begin
9916 return Is_Floating_Point_Type (E) and then Signed_Zeros_On_Target;
9917 end Has_Signed_Zeros;
9918
9919 ------------------------------
9920 -- Has_Significant_Contract --
9921 ------------------------------
9922
9923 function Has_Significant_Contract (Subp_Id : Entity_Id) return Boolean is
9924 Subp_Nam : constant Name_Id := Chars (Subp_Id);
9925
9926 begin
9927 -- _Finalizer procedure
9928
9929 if Subp_Nam = Name_uFinalizer then
9930 return False;
9931
9932 -- _Postconditions procedure
9933
9934 elsif Subp_Nam = Name_uPostconditions then
9935 return False;
9936
9937 -- Predicate function
9938
9939 elsif Ekind (Subp_Id) = E_Function
9940 and then Is_Predicate_Function (Subp_Id)
9941 then
9942 return False;
9943
9944 -- TSS subprogram
9945
9946 elsif Get_TSS_Name (Subp_Id) /= TSS_Null then
9947 return False;
9948
9949 else
9950 return True;
9951 end if;
9952 end Has_Significant_Contract;
9953
9954 -----------------------------
9955 -- Has_Static_Array_Bounds --
9956 -----------------------------
9957
9958 function Has_Static_Array_Bounds (Typ : Node_Id) return Boolean is
9959 Ndims : constant Nat := Number_Dimensions (Typ);
9960
9961 Index : Node_Id;
9962 Low : Node_Id;
9963 High : Node_Id;
9964
9965 begin
9966 -- Unconstrained types do not have static bounds
9967
9968 if not Is_Constrained (Typ) then
9969 return False;
9970 end if;
9971
9972 -- First treat string literals specially, as the lower bound and length
9973 -- of string literals are not stored like those of arrays.
9974
9975 -- A string literal always has static bounds
9976
9977 if Ekind (Typ) = E_String_Literal_Subtype then
9978 return True;
9979 end if;
9980
9981 -- Treat all dimensions in turn
9982
9983 Index := First_Index (Typ);
9984 for Indx in 1 .. Ndims loop
9985
9986 -- In case of an illegal index which is not a discrete type, return
9987 -- that the type is not static.
9988
9989 if not Is_Discrete_Type (Etype (Index))
9990 or else Etype (Index) = Any_Type
9991 then
9992 return False;
9993 end if;
9994
9995 Get_Index_Bounds (Index, Low, High);
9996
9997 if Error_Posted (Low) or else Error_Posted (High) then
9998 return False;
9999 end if;
10000
10001 if Is_OK_Static_Expression (Low)
10002 and then
10003 Is_OK_Static_Expression (High)
10004 then
10005 null;
10006 else
10007 return False;
10008 end if;
10009
10010 Next (Index);
10011 end loop;
10012
10013 -- If we fall through the loop, all indexes matched
10014
10015 return True;
10016 end Has_Static_Array_Bounds;
10017
10018 ----------------
10019 -- Has_Stream --
10020 ----------------
10021
10022 function Has_Stream (T : Entity_Id) return Boolean is
10023 E : Entity_Id;
10024
10025 begin
10026 if No (T) then
10027 return False;
10028
10029 elsif Is_RTE (Root_Type (T), RE_Root_Stream_Type) then
10030 return True;
10031
10032 elsif Is_Array_Type (T) then
10033 return Has_Stream (Component_Type (T));
10034
10035 elsif Is_Record_Type (T) then
10036 E := First_Component (T);
10037 while Present (E) loop
10038 if Has_Stream (Etype (E)) then
10039 return True;
10040 else
10041 Next_Component (E);
10042 end if;
10043 end loop;
10044
10045 return False;
10046
10047 elsif Is_Private_Type (T) then
10048 return Has_Stream (Underlying_Type (T));
10049
10050 else
10051 return False;
10052 end if;
10053 end Has_Stream;
10054
10055 ----------------
10056 -- Has_Suffix --
10057 ----------------
10058
10059 function Has_Suffix (E : Entity_Id; Suffix : Character) return Boolean is
10060 begin
10061 Get_Name_String (Chars (E));
10062 return Name_Buffer (Name_Len) = Suffix;
10063 end Has_Suffix;
10064
10065 ----------------
10066 -- Add_Suffix --
10067 ----------------
10068
10069 function Add_Suffix (E : Entity_Id; Suffix : Character) return Name_Id is
10070 begin
10071 Get_Name_String (Chars (E));
10072 Add_Char_To_Name_Buffer (Suffix);
10073 return Name_Find;
10074 end Add_Suffix;
10075
10076 -------------------
10077 -- Remove_Suffix --
10078 -------------------
10079
10080 function Remove_Suffix (E : Entity_Id; Suffix : Character) return Name_Id is
10081 begin
10082 pragma Assert (Has_Suffix (E, Suffix));
10083 Get_Name_String (Chars (E));
10084 Name_Len := Name_Len - 1;
10085 return Name_Find;
10086 end Remove_Suffix;
10087
10088 --------------------------
10089 -- Has_Tagged_Component --
10090 --------------------------
10091
10092 function Has_Tagged_Component (Typ : Entity_Id) return Boolean is
10093 Comp : Entity_Id;
10094
10095 begin
10096 if Is_Private_Type (Typ) and then Present (Underlying_Type (Typ)) then
10097 return Has_Tagged_Component (Underlying_Type (Typ));
10098
10099 elsif Is_Array_Type (Typ) then
10100 return Has_Tagged_Component (Component_Type (Typ));
10101
10102 elsif Is_Tagged_Type (Typ) then
10103 return True;
10104
10105 elsif Is_Record_Type (Typ) then
10106 Comp := First_Component (Typ);
10107 while Present (Comp) loop
10108 if Has_Tagged_Component (Etype (Comp)) then
10109 return True;
10110 end if;
10111
10112 Next_Component (Comp);
10113 end loop;
10114
10115 return False;
10116
10117 else
10118 return False;
10119 end if;
10120 end Has_Tagged_Component;
10121
10122 -----------------------------
10123 -- Has_Undefined_Reference --
10124 -----------------------------
10125
10126 function Has_Undefined_Reference (Expr : Node_Id) return Boolean is
10127 Has_Undef_Ref : Boolean := False;
10128 -- Flag set when expression Expr contains at least one undefined
10129 -- reference.
10130
10131 function Is_Undefined_Reference (N : Node_Id) return Traverse_Result;
10132 -- Determine whether N denotes a reference and if it does, whether it is
10133 -- undefined.
10134
10135 ----------------------------
10136 -- Is_Undefined_Reference --
10137 ----------------------------
10138
10139 function Is_Undefined_Reference (N : Node_Id) return Traverse_Result is
10140 begin
10141 if Is_Entity_Name (N)
10142 and then Present (Entity (N))
10143 and then Entity (N) = Any_Id
10144 then
10145 Has_Undef_Ref := True;
10146 return Abandon;
10147 end if;
10148
10149 return OK;
10150 end Is_Undefined_Reference;
10151
10152 procedure Find_Undefined_References is
10153 new Traverse_Proc (Is_Undefined_Reference);
10154
10155 -- Start of processing for Has_Undefined_Reference
10156
10157 begin
10158 Find_Undefined_References (Expr);
10159
10160 return Has_Undef_Ref;
10161 end Has_Undefined_Reference;
10162
10163 ----------------------------
10164 -- Has_Volatile_Component --
10165 ----------------------------
10166
10167 function Has_Volatile_Component (Typ : Entity_Id) return Boolean is
10168 Comp : Entity_Id;
10169
10170 begin
10171 if Has_Volatile_Components (Typ) then
10172 return True;
10173
10174 elsif Is_Array_Type (Typ) then
10175 return Is_Volatile (Component_Type (Typ));
10176
10177 elsif Is_Record_Type (Typ) then
10178 Comp := First_Component (Typ);
10179 while Present (Comp) loop
10180 if Is_Volatile_Object (Comp) then
10181 return True;
10182 end if;
10183
10184 Comp := Next_Component (Comp);
10185 end loop;
10186 end if;
10187
10188 return False;
10189 end Has_Volatile_Component;
10190
10191 -------------------------
10192 -- Implementation_Kind --
10193 -------------------------
10194
10195 function Implementation_Kind (Subp : Entity_Id) return Name_Id is
10196 Impl_Prag : constant Node_Id := Get_Rep_Pragma (Subp, Name_Implemented);
10197 Arg : Node_Id;
10198 begin
10199 pragma Assert (Present (Impl_Prag));
10200 Arg := Last (Pragma_Argument_Associations (Impl_Prag));
10201 return Chars (Get_Pragma_Arg (Arg));
10202 end Implementation_Kind;
10203
10204 --------------------------
10205 -- Implements_Interface --
10206 --------------------------
10207
10208 function Implements_Interface
10209 (Typ_Ent : Entity_Id;
10210 Iface_Ent : Entity_Id;
10211 Exclude_Parents : Boolean := False) return Boolean
10212 is
10213 Ifaces_List : Elist_Id;
10214 Elmt : Elmt_Id;
10215 Iface : Entity_Id := Base_Type (Iface_Ent);
10216 Typ : Entity_Id := Base_Type (Typ_Ent);
10217
10218 begin
10219 if Is_Class_Wide_Type (Typ) then
10220 Typ := Root_Type (Typ);
10221 end if;
10222
10223 if not Has_Interfaces (Typ) then
10224 return False;
10225 end if;
10226
10227 if Is_Class_Wide_Type (Iface) then
10228 Iface := Root_Type (Iface);
10229 end if;
10230
10231 Collect_Interfaces (Typ, Ifaces_List);
10232
10233 Elmt := First_Elmt (Ifaces_List);
10234 while Present (Elmt) loop
10235 if Is_Ancestor (Node (Elmt), Typ, Use_Full_View => True)
10236 and then Exclude_Parents
10237 then
10238 null;
10239
10240 elsif Node (Elmt) = Iface then
10241 return True;
10242 end if;
10243
10244 Next_Elmt (Elmt);
10245 end loop;
10246
10247 return False;
10248 end Implements_Interface;
10249
10250 ------------------------------------
10251 -- In_Assertion_Expression_Pragma --
10252 ------------------------------------
10253
10254 function In_Assertion_Expression_Pragma (N : Node_Id) return Boolean is
10255 Par : Node_Id;
10256 Prag : Node_Id := Empty;
10257
10258 begin
10259 -- Climb the parent chain looking for an enclosing pragma
10260
10261 Par := N;
10262 while Present (Par) loop
10263 if Nkind (Par) = N_Pragma then
10264 Prag := Par;
10265 exit;
10266
10267 -- Precondition-like pragmas are expanded into if statements, check
10268 -- the original node instead.
10269
10270 elsif Nkind (Original_Node (Par)) = N_Pragma then
10271 Prag := Original_Node (Par);
10272 exit;
10273
10274 -- The expansion of attribute 'Old generates a constant to capture
10275 -- the result of the prefix. If the parent traversal reaches
10276 -- one of these constants, then the node technically came from a
10277 -- postcondition-like pragma. Note that the Ekind is not tested here
10278 -- because N may be the expression of an object declaration which is
10279 -- currently being analyzed. Such objects carry Ekind of E_Void.
10280
10281 elsif Nkind (Par) = N_Object_Declaration
10282 and then Constant_Present (Par)
10283 and then Stores_Attribute_Old_Prefix (Defining_Entity (Par))
10284 then
10285 return True;
10286
10287 -- Prevent the search from going too far
10288
10289 elsif Is_Body_Or_Package_Declaration (Par) then
10290 return False;
10291 end if;
10292
10293 Par := Parent (Par);
10294 end loop;
10295
10296 return
10297 Present (Prag)
10298 and then Assertion_Expression_Pragma (Get_Pragma_Id (Prag));
10299 end In_Assertion_Expression_Pragma;
10300
10301 -----------------
10302 -- In_Instance --
10303 -----------------
10304
10305 function In_Instance return Boolean is
10306 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
10307 S : Entity_Id;
10308
10309 begin
10310 S := Current_Scope;
10311 while Present (S) and then S /= Standard_Standard loop
10312 if Ekind_In (S, E_Function, E_Package, E_Procedure)
10313 and then Is_Generic_Instance (S)
10314 then
10315 -- A child instance is always compiled in the context of a parent
10316 -- instance. Nevertheless, the actuals are not analyzed in an
10317 -- instance context. We detect this case by examining the current
10318 -- compilation unit, which must be a child instance, and checking
10319 -- that it is not currently on the scope stack.
10320
10321 if Is_Child_Unit (Curr_Unit)
10322 and then Nkind (Unit (Cunit (Current_Sem_Unit))) =
10323 N_Package_Instantiation
10324 and then not In_Open_Scopes (Curr_Unit)
10325 then
10326 return False;
10327 else
10328 return True;
10329 end if;
10330 end if;
10331
10332 S := Scope (S);
10333 end loop;
10334
10335 return False;
10336 end In_Instance;
10337
10338 ----------------------
10339 -- In_Instance_Body --
10340 ----------------------
10341
10342 function In_Instance_Body return Boolean is
10343 S : Entity_Id;
10344
10345 begin
10346 S := Current_Scope;
10347 while Present (S) and then S /= Standard_Standard loop
10348 if Ekind_In (S, E_Function, E_Procedure)
10349 and then Is_Generic_Instance (S)
10350 then
10351 return True;
10352
10353 elsif Ekind (S) = E_Package
10354 and then In_Package_Body (S)
10355 and then Is_Generic_Instance (S)
10356 then
10357 return True;
10358 end if;
10359
10360 S := Scope (S);
10361 end loop;
10362
10363 return False;
10364 end In_Instance_Body;
10365
10366 -----------------------------
10367 -- In_Instance_Not_Visible --
10368 -----------------------------
10369
10370 function In_Instance_Not_Visible return Boolean is
10371 S : Entity_Id;
10372
10373 begin
10374 S := Current_Scope;
10375 while Present (S) and then S /= Standard_Standard loop
10376 if Ekind_In (S, E_Function, E_Procedure)
10377 and then Is_Generic_Instance (S)
10378 then
10379 return True;
10380
10381 elsif Ekind (S) = E_Package
10382 and then (In_Package_Body (S) or else In_Private_Part (S))
10383 and then Is_Generic_Instance (S)
10384 then
10385 return True;
10386 end if;
10387
10388 S := Scope (S);
10389 end loop;
10390
10391 return False;
10392 end In_Instance_Not_Visible;
10393
10394 ------------------------------
10395 -- In_Instance_Visible_Part --
10396 ------------------------------
10397
10398 function In_Instance_Visible_Part return Boolean is
10399 S : Entity_Id;
10400
10401 begin
10402 S := Current_Scope;
10403 while Present (S) and then S /= Standard_Standard loop
10404 if Ekind (S) = E_Package
10405 and then Is_Generic_Instance (S)
10406 and then not In_Package_Body (S)
10407 and then not In_Private_Part (S)
10408 then
10409 return True;
10410 end if;
10411
10412 S := Scope (S);
10413 end loop;
10414
10415 return False;
10416 end In_Instance_Visible_Part;
10417
10418 ---------------------
10419 -- In_Package_Body --
10420 ---------------------
10421
10422 function In_Package_Body return Boolean is
10423 S : Entity_Id;
10424
10425 begin
10426 S := Current_Scope;
10427 while Present (S) and then S /= Standard_Standard loop
10428 if Ekind (S) = E_Package and then In_Package_Body (S) then
10429 return True;
10430 else
10431 S := Scope (S);
10432 end if;
10433 end loop;
10434
10435 return False;
10436 end In_Package_Body;
10437
10438 --------------------------------
10439 -- In_Parameter_Specification --
10440 --------------------------------
10441
10442 function In_Parameter_Specification (N : Node_Id) return Boolean is
10443 PN : Node_Id;
10444
10445 begin
10446 PN := Parent (N);
10447 while Present (PN) loop
10448 if Nkind (PN) = N_Parameter_Specification then
10449 return True;
10450 end if;
10451
10452 PN := Parent (PN);
10453 end loop;
10454
10455 return False;
10456 end In_Parameter_Specification;
10457
10458 --------------------------
10459 -- In_Pragma_Expression --
10460 --------------------------
10461
10462 function In_Pragma_Expression (N : Node_Id; Nam : Name_Id) return Boolean is
10463 P : Node_Id;
10464 begin
10465 P := Parent (N);
10466 loop
10467 if No (P) then
10468 return False;
10469 elsif Nkind (P) = N_Pragma and then Pragma_Name (P) = Nam then
10470 return True;
10471 else
10472 P := Parent (P);
10473 end if;
10474 end loop;
10475 end In_Pragma_Expression;
10476
10477 -------------------------------------
10478 -- In_Reverse_Storage_Order_Object --
10479 -------------------------------------
10480
10481 function In_Reverse_Storage_Order_Object (N : Node_Id) return Boolean is
10482 Pref : Node_Id;
10483 Btyp : Entity_Id := Empty;
10484
10485 begin
10486 -- Climb up indexed components
10487
10488 Pref := N;
10489 loop
10490 case Nkind (Pref) is
10491 when N_Selected_Component =>
10492 Pref := Prefix (Pref);
10493 exit;
10494
10495 when N_Indexed_Component =>
10496 Pref := Prefix (Pref);
10497
10498 when others =>
10499 Pref := Empty;
10500 exit;
10501 end case;
10502 end loop;
10503
10504 if Present (Pref) then
10505 Btyp := Base_Type (Etype (Pref));
10506 end if;
10507
10508 return Present (Btyp)
10509 and then (Is_Record_Type (Btyp) or else Is_Array_Type (Btyp))
10510 and then Reverse_Storage_Order (Btyp);
10511 end In_Reverse_Storage_Order_Object;
10512
10513 --------------------------------------
10514 -- In_Subprogram_Or_Concurrent_Unit --
10515 --------------------------------------
10516
10517 function In_Subprogram_Or_Concurrent_Unit return Boolean is
10518 E : Entity_Id;
10519 K : Entity_Kind;
10520
10521 begin
10522 -- Use scope chain to check successively outer scopes
10523
10524 E := Current_Scope;
10525 loop
10526 K := Ekind (E);
10527
10528 if K in Subprogram_Kind
10529 or else K in Concurrent_Kind
10530 or else K in Generic_Subprogram_Kind
10531 then
10532 return True;
10533
10534 elsif E = Standard_Standard then
10535 return False;
10536 end if;
10537
10538 E := Scope (E);
10539 end loop;
10540 end In_Subprogram_Or_Concurrent_Unit;
10541
10542 ---------------------
10543 -- In_Visible_Part --
10544 ---------------------
10545
10546 function In_Visible_Part (Scope_Id : Entity_Id) return Boolean is
10547 begin
10548 return Is_Package_Or_Generic_Package (Scope_Id)
10549 and then In_Open_Scopes (Scope_Id)
10550 and then not In_Package_Body (Scope_Id)
10551 and then not In_Private_Part (Scope_Id);
10552 end In_Visible_Part;
10553
10554 --------------------------------
10555 -- Incomplete_Or_Partial_View --
10556 --------------------------------
10557
10558 function Incomplete_Or_Partial_View (Id : Entity_Id) return Entity_Id is
10559 function Inspect_Decls
10560 (Decls : List_Id;
10561 Taft : Boolean := False) return Entity_Id;
10562 -- Check whether a declarative region contains the incomplete or partial
10563 -- view of Id.
10564
10565 -------------------
10566 -- Inspect_Decls --
10567 -------------------
10568
10569 function Inspect_Decls
10570 (Decls : List_Id;
10571 Taft : Boolean := False) return Entity_Id
10572 is
10573 Decl : Node_Id;
10574 Match : Node_Id;
10575
10576 begin
10577 Decl := First (Decls);
10578 while Present (Decl) loop
10579 Match := Empty;
10580
10581 if Taft then
10582 if Nkind (Decl) = N_Incomplete_Type_Declaration then
10583 Match := Defining_Identifier (Decl);
10584 end if;
10585
10586 else
10587 if Nkind_In (Decl, N_Private_Extension_Declaration,
10588 N_Private_Type_Declaration)
10589 then
10590 Match := Defining_Identifier (Decl);
10591 end if;
10592 end if;
10593
10594 if Present (Match)
10595 and then Present (Full_View (Match))
10596 and then Full_View (Match) = Id
10597 then
10598 return Match;
10599 end if;
10600
10601 Next (Decl);
10602 end loop;
10603
10604 return Empty;
10605 end Inspect_Decls;
10606
10607 -- Local variables
10608
10609 Prev : Entity_Id;
10610
10611 -- Start of processing for Incomplete_Or_Partial_View
10612
10613 begin
10614 -- Deferred constant or incomplete type case
10615
10616 Prev := Current_Entity_In_Scope (Id);
10617
10618 if Present (Prev)
10619 and then (Is_Incomplete_Type (Prev) or else Ekind (Prev) = E_Constant)
10620 and then Present (Full_View (Prev))
10621 and then Full_View (Prev) = Id
10622 then
10623 return Prev;
10624 end if;
10625
10626 -- Private or Taft amendment type case
10627
10628 declare
10629 Pkg : constant Entity_Id := Scope (Id);
10630 Pkg_Decl : Node_Id := Pkg;
10631
10632 begin
10633 if Present (Pkg) and then Ekind (Pkg) = E_Package then
10634 while Nkind (Pkg_Decl) /= N_Package_Specification loop
10635 Pkg_Decl := Parent (Pkg_Decl);
10636 end loop;
10637
10638 -- It is knows that Typ has a private view, look for it in the
10639 -- visible declarations of the enclosing scope. A special case
10640 -- of this is when the two views have been exchanged - the full
10641 -- appears earlier than the private.
10642
10643 if Has_Private_Declaration (Id) then
10644 Prev := Inspect_Decls (Visible_Declarations (Pkg_Decl));
10645
10646 -- Exchanged view case, look in the private declarations
10647
10648 if No (Prev) then
10649 Prev := Inspect_Decls (Private_Declarations (Pkg_Decl));
10650 end if;
10651
10652 return Prev;
10653
10654 -- Otherwise if this is the package body, then Typ is a potential
10655 -- Taft amendment type. The incomplete view should be located in
10656 -- the private declarations of the enclosing scope.
10657
10658 elsif In_Package_Body (Pkg) then
10659 return Inspect_Decls (Private_Declarations (Pkg_Decl), True);
10660 end if;
10661 end if;
10662 end;
10663
10664 -- The type has no incomplete or private view
10665
10666 return Empty;
10667 end Incomplete_Or_Partial_View;
10668
10669 -----------------------------------------
10670 -- Inherit_Default_Init_Cond_Procedure --
10671 -----------------------------------------
10672
10673 procedure Inherit_Default_Init_Cond_Procedure (Typ : Entity_Id) is
10674 Par_Typ : constant Entity_Id := Etype (Typ);
10675
10676 begin
10677 -- A derived type inherits the default initial condition procedure of
10678 -- its parent type.
10679
10680 if No (Default_Init_Cond_Procedure (Typ)) then
10681 Set_Default_Init_Cond_Procedure
10682 (Typ, Default_Init_Cond_Procedure (Par_Typ));
10683 end if;
10684 end Inherit_Default_Init_Cond_Procedure;
10685
10686 ----------------------------
10687 -- Inherit_Rep_Item_Chain --
10688 ----------------------------
10689
10690 procedure Inherit_Rep_Item_Chain (Typ : Entity_Id; From_Typ : Entity_Id) is
10691 From_Item : constant Node_Id := First_Rep_Item (From_Typ);
10692 Item : Node_Id := Empty;
10693 Last_Item : Node_Id := Empty;
10694
10695 begin
10696 -- Reach the end of the destination type's chain (if any) and capture
10697 -- the last item.
10698
10699 Item := First_Rep_Item (Typ);
10700 while Present (Item) loop
10701
10702 -- Do not inherit a chain that has been inherited already
10703
10704 if Item = From_Item then
10705 return;
10706 end if;
10707
10708 Last_Item := Item;
10709 Item := Next_Rep_Item (Item);
10710 end loop;
10711
10712 Item := First_Rep_Item (From_Typ);
10713
10714 -- Additional check when both parent and current type have rep.
10715 -- items, to prevent circularities when the derivation completes
10716 -- a private declaration and inherits from both views of the parent.
10717 -- There may be a remaining problem with the proper ordering of
10718 -- attribute specifications and aspects on the chains of the four
10719 -- entities involved. ???
10720
10721 if Present (Item) and then Present (From_Item) then
10722 while Present (Item) loop
10723 if Item = First_Rep_Item (Typ) then
10724 return;
10725 end if;
10726
10727 Item := Next_Rep_Item (Item);
10728 end loop;
10729 end if;
10730
10731 -- When the destination type has a rep item chain, the chain of the
10732 -- source type is appended to it.
10733
10734 if Present (Last_Item) then
10735 Set_Next_Rep_Item (Last_Item, From_Item);
10736
10737 -- Otherwise the destination type directly inherits the rep item chain
10738 -- of the source type (if any).
10739
10740 else
10741 Set_First_Rep_Item (Typ, From_Item);
10742 end if;
10743 end Inherit_Rep_Item_Chain;
10744
10745 ---------------------------------
10746 -- Insert_Explicit_Dereference --
10747 ---------------------------------
10748
10749 procedure Insert_Explicit_Dereference (N : Node_Id) is
10750 New_Prefix : constant Node_Id := Relocate_Node (N);
10751 Ent : Entity_Id := Empty;
10752 Pref : Node_Id;
10753 I : Interp_Index;
10754 It : Interp;
10755 T : Entity_Id;
10756
10757 begin
10758 Save_Interps (N, New_Prefix);
10759
10760 Rewrite (N,
10761 Make_Explicit_Dereference (Sloc (Parent (N)),
10762 Prefix => New_Prefix));
10763
10764 Set_Etype (N, Designated_Type (Etype (New_Prefix)));
10765
10766 if Is_Overloaded (New_Prefix) then
10767
10768 -- The dereference is also overloaded, and its interpretations are
10769 -- the designated types of the interpretations of the original node.
10770
10771 Set_Etype (N, Any_Type);
10772
10773 Get_First_Interp (New_Prefix, I, It);
10774 while Present (It.Nam) loop
10775 T := It.Typ;
10776
10777 if Is_Access_Type (T) then
10778 Add_One_Interp (N, Designated_Type (T), Designated_Type (T));
10779 end if;
10780
10781 Get_Next_Interp (I, It);
10782 end loop;
10783
10784 End_Interp_List;
10785
10786 else
10787 -- Prefix is unambiguous: mark the original prefix (which might
10788 -- Come_From_Source) as a reference, since the new (relocated) one
10789 -- won't be taken into account.
10790
10791 if Is_Entity_Name (New_Prefix) then
10792 Ent := Entity (New_Prefix);
10793 Pref := New_Prefix;
10794
10795 -- For a retrieval of a subcomponent of some composite object,
10796 -- retrieve the ultimate entity if there is one.
10797
10798 elsif Nkind_In (New_Prefix, N_Selected_Component,
10799 N_Indexed_Component)
10800 then
10801 Pref := Prefix (New_Prefix);
10802 while Present (Pref)
10803 and then Nkind_In (Pref, N_Selected_Component,
10804 N_Indexed_Component)
10805 loop
10806 Pref := Prefix (Pref);
10807 end loop;
10808
10809 if Present (Pref) and then Is_Entity_Name (Pref) then
10810 Ent := Entity (Pref);
10811 end if;
10812 end if;
10813
10814 -- Place the reference on the entity node
10815
10816 if Present (Ent) then
10817 Generate_Reference (Ent, Pref);
10818 end if;
10819 end if;
10820 end Insert_Explicit_Dereference;
10821
10822 ------------------------------------------
10823 -- Inspect_Deferred_Constant_Completion --
10824 ------------------------------------------
10825
10826 procedure Inspect_Deferred_Constant_Completion (Decls : List_Id) is
10827 Decl : Node_Id;
10828
10829 begin
10830 Decl := First (Decls);
10831 while Present (Decl) loop
10832
10833 -- Deferred constant signature
10834
10835 if Nkind (Decl) = N_Object_Declaration
10836 and then Constant_Present (Decl)
10837 and then No (Expression (Decl))
10838
10839 -- No need to check internally generated constants
10840
10841 and then Comes_From_Source (Decl)
10842
10843 -- The constant is not completed. A full object declaration or a
10844 -- pragma Import complete a deferred constant.
10845
10846 and then not Has_Completion (Defining_Identifier (Decl))
10847 then
10848 Error_Msg_N
10849 ("constant declaration requires initialization expression",
10850 Defining_Identifier (Decl));
10851 end if;
10852
10853 Decl := Next (Decl);
10854 end loop;
10855 end Inspect_Deferred_Constant_Completion;
10856
10857 -----------------------------
10858 -- Install_Generic_Formals --
10859 -----------------------------
10860
10861 procedure Install_Generic_Formals (Subp_Id : Entity_Id) is
10862 E : Entity_Id;
10863
10864 begin
10865 pragma Assert (Is_Generic_Subprogram (Subp_Id));
10866
10867 E := First_Entity (Subp_Id);
10868 while Present (E) loop
10869 Install_Entity (E);
10870 Next_Entity (E);
10871 end loop;
10872 end Install_Generic_Formals;
10873
10874 -----------------------------
10875 -- Is_Actual_Out_Parameter --
10876 -----------------------------
10877
10878 function Is_Actual_Out_Parameter (N : Node_Id) return Boolean is
10879 Formal : Entity_Id;
10880 Call : Node_Id;
10881 begin
10882 Find_Actual (N, Formal, Call);
10883 return Present (Formal) and then Ekind (Formal) = E_Out_Parameter;
10884 end Is_Actual_Out_Parameter;
10885
10886 -------------------------
10887 -- Is_Actual_Parameter --
10888 -------------------------
10889
10890 function Is_Actual_Parameter (N : Node_Id) return Boolean is
10891 PK : constant Node_Kind := Nkind (Parent (N));
10892
10893 begin
10894 case PK is
10895 when N_Parameter_Association =>
10896 return N = Explicit_Actual_Parameter (Parent (N));
10897
10898 when N_Subprogram_Call =>
10899 return Is_List_Member (N)
10900 and then
10901 List_Containing (N) = Parameter_Associations (Parent (N));
10902
10903 when others =>
10904 return False;
10905 end case;
10906 end Is_Actual_Parameter;
10907
10908 --------------------------------
10909 -- Is_Actual_Tagged_Parameter --
10910 --------------------------------
10911
10912 function Is_Actual_Tagged_Parameter (N : Node_Id) return Boolean is
10913 Formal : Entity_Id;
10914 Call : Node_Id;
10915 begin
10916 Find_Actual (N, Formal, Call);
10917 return Present (Formal) and then Is_Tagged_Type (Etype (Formal));
10918 end Is_Actual_Tagged_Parameter;
10919
10920 ---------------------
10921 -- Is_Aliased_View --
10922 ---------------------
10923
10924 function Is_Aliased_View (Obj : Node_Id) return Boolean is
10925 E : Entity_Id;
10926
10927 begin
10928 if Is_Entity_Name (Obj) then
10929 E := Entity (Obj);
10930
10931 return
10932 (Is_Object (E)
10933 and then
10934 (Is_Aliased (E)
10935 or else (Present (Renamed_Object (E))
10936 and then Is_Aliased_View (Renamed_Object (E)))))
10937
10938 or else ((Is_Formal (E)
10939 or else Ekind_In (E, E_Generic_In_Out_Parameter,
10940 E_Generic_In_Parameter))
10941 and then Is_Tagged_Type (Etype (E)))
10942
10943 or else (Is_Concurrent_Type (E) and then In_Open_Scopes (E))
10944
10945 -- Current instance of type, either directly or as rewritten
10946 -- reference to the current object.
10947
10948 or else (Is_Entity_Name (Original_Node (Obj))
10949 and then Present (Entity (Original_Node (Obj)))
10950 and then Is_Type (Entity (Original_Node (Obj))))
10951
10952 or else (Is_Type (E) and then E = Current_Scope)
10953
10954 or else (Is_Incomplete_Or_Private_Type (E)
10955 and then Full_View (E) = Current_Scope)
10956
10957 -- Ada 2012 AI05-0053: the return object of an extended return
10958 -- statement is aliased if its type is immutably limited.
10959
10960 or else (Is_Return_Object (E)
10961 and then Is_Limited_View (Etype (E)));
10962
10963 elsif Nkind (Obj) = N_Selected_Component then
10964 return Is_Aliased (Entity (Selector_Name (Obj)));
10965
10966 elsif Nkind (Obj) = N_Indexed_Component then
10967 return Has_Aliased_Components (Etype (Prefix (Obj)))
10968 or else
10969 (Is_Access_Type (Etype (Prefix (Obj)))
10970 and then Has_Aliased_Components
10971 (Designated_Type (Etype (Prefix (Obj)))));
10972
10973 elsif Nkind_In (Obj, N_Unchecked_Type_Conversion, N_Type_Conversion) then
10974 return Is_Tagged_Type (Etype (Obj))
10975 and then Is_Aliased_View (Expression (Obj));
10976
10977 elsif Nkind (Obj) = N_Explicit_Dereference then
10978 return Nkind (Original_Node (Obj)) /= N_Function_Call;
10979
10980 else
10981 return False;
10982 end if;
10983 end Is_Aliased_View;
10984
10985 -------------------------
10986 -- Is_Ancestor_Package --
10987 -------------------------
10988
10989 function Is_Ancestor_Package
10990 (E1 : Entity_Id;
10991 E2 : Entity_Id) return Boolean
10992 is
10993 Par : Entity_Id;
10994
10995 begin
10996 Par := E2;
10997 while Present (Par) and then Par /= Standard_Standard loop
10998 if Par = E1 then
10999 return True;
11000 end if;
11001
11002 Par := Scope (Par);
11003 end loop;
11004
11005 return False;
11006 end Is_Ancestor_Package;
11007
11008 ----------------------
11009 -- Is_Atomic_Object --
11010 ----------------------
11011
11012 function Is_Atomic_Object (N : Node_Id) return Boolean is
11013
11014 function Object_Has_Atomic_Components (N : Node_Id) return Boolean;
11015 -- Determines if given object has atomic components
11016
11017 function Is_Atomic_Prefix (N : Node_Id) return Boolean;
11018 -- If prefix is an implicit dereference, examine designated type
11019
11020 ----------------------
11021 -- Is_Atomic_Prefix --
11022 ----------------------
11023
11024 function Is_Atomic_Prefix (N : Node_Id) return Boolean is
11025 begin
11026 if Is_Access_Type (Etype (N)) then
11027 return
11028 Has_Atomic_Components (Designated_Type (Etype (N)));
11029 else
11030 return Object_Has_Atomic_Components (N);
11031 end if;
11032 end Is_Atomic_Prefix;
11033
11034 ----------------------------------
11035 -- Object_Has_Atomic_Components --
11036 ----------------------------------
11037
11038 function Object_Has_Atomic_Components (N : Node_Id) return Boolean is
11039 begin
11040 if Has_Atomic_Components (Etype (N))
11041 or else Is_Atomic (Etype (N))
11042 then
11043 return True;
11044
11045 elsif Is_Entity_Name (N)
11046 and then (Has_Atomic_Components (Entity (N))
11047 or else Is_Atomic (Entity (N)))
11048 then
11049 return True;
11050
11051 elsif Nkind (N) = N_Selected_Component
11052 and then Is_Atomic (Entity (Selector_Name (N)))
11053 then
11054 return True;
11055
11056 elsif Nkind (N) = N_Indexed_Component
11057 or else Nkind (N) = N_Selected_Component
11058 then
11059 return Is_Atomic_Prefix (Prefix (N));
11060
11061 else
11062 return False;
11063 end if;
11064 end Object_Has_Atomic_Components;
11065
11066 -- Start of processing for Is_Atomic_Object
11067
11068 begin
11069 -- Predicate is not relevant to subprograms
11070
11071 if Is_Entity_Name (N) and then Is_Overloadable (Entity (N)) then
11072 return False;
11073
11074 elsif Is_Atomic (Etype (N))
11075 or else (Is_Entity_Name (N) and then Is_Atomic (Entity (N)))
11076 then
11077 return True;
11078
11079 elsif Nkind (N) = N_Selected_Component
11080 and then Is_Atomic (Entity (Selector_Name (N)))
11081 then
11082 return True;
11083
11084 elsif Nkind (N) = N_Indexed_Component
11085 or else Nkind (N) = N_Selected_Component
11086 then
11087 return Is_Atomic_Prefix (Prefix (N));
11088
11089 else
11090 return False;
11091 end if;
11092 end Is_Atomic_Object;
11093
11094 -----------------------------
11095 -- Is_Atomic_Or_VFA_Object --
11096 -----------------------------
11097
11098 function Is_Atomic_Or_VFA_Object (N : Node_Id) return Boolean is
11099 begin
11100 return Is_Atomic_Object (N)
11101 or else (Is_Object_Reference (N)
11102 and then Is_Entity_Name (N)
11103 and then (Is_Volatile_Full_Access (Entity (N))
11104 or else
11105 Is_Volatile_Full_Access (Etype (Entity (N)))));
11106 end Is_Atomic_Or_VFA_Object;
11107
11108 -------------------------
11109 -- Is_Attribute_Result --
11110 -------------------------
11111
11112 function Is_Attribute_Result (N : Node_Id) return Boolean is
11113 begin
11114 return Nkind (N) = N_Attribute_Reference
11115 and then Attribute_Name (N) = Name_Result;
11116 end Is_Attribute_Result;
11117
11118 -------------------------
11119 -- Is_Attribute_Update --
11120 -------------------------
11121
11122 function Is_Attribute_Update (N : Node_Id) return Boolean is
11123 begin
11124 return Nkind (N) = N_Attribute_Reference
11125 and then Attribute_Name (N) = Name_Update;
11126 end Is_Attribute_Update;
11127
11128 ------------------------------------
11129 -- Is_Body_Or_Package_Declaration --
11130 ------------------------------------
11131
11132 function Is_Body_Or_Package_Declaration (N : Node_Id) return Boolean is
11133 begin
11134 return Nkind_In (N, N_Entry_Body,
11135 N_Package_Body,
11136 N_Package_Declaration,
11137 N_Protected_Body,
11138 N_Subprogram_Body,
11139 N_Task_Body);
11140 end Is_Body_Or_Package_Declaration;
11141
11142 -----------------------
11143 -- Is_Bounded_String --
11144 -----------------------
11145
11146 function Is_Bounded_String (T : Entity_Id) return Boolean is
11147 Under : constant Entity_Id := Underlying_Type (Root_Type (T));
11148
11149 begin
11150 -- Check whether T is ultimately derived from Ada.Strings.Superbounded.
11151 -- Super_String, or one of the [Wide_]Wide_ versions. This will
11152 -- be True for all the Bounded_String types in instances of the
11153 -- Generic_Bounded_Length generics, and for types derived from those.
11154
11155 return Present (Under)
11156 and then (Is_RTE (Root_Type (Under), RO_SU_Super_String) or else
11157 Is_RTE (Root_Type (Under), RO_WI_Super_String) or else
11158 Is_RTE (Root_Type (Under), RO_WW_Super_String));
11159 end Is_Bounded_String;
11160
11161 -------------------------
11162 -- Is_Child_Or_Sibling --
11163 -------------------------
11164
11165 function Is_Child_Or_Sibling
11166 (Pack_1 : Entity_Id;
11167 Pack_2 : Entity_Id) return Boolean
11168 is
11169 function Distance_From_Standard (Pack : Entity_Id) return Nat;
11170 -- Given an arbitrary package, return the number of "climbs" necessary
11171 -- to reach scope Standard_Standard.
11172
11173 procedure Equalize_Depths
11174 (Pack : in out Entity_Id;
11175 Depth : in out Nat;
11176 Depth_To_Reach : Nat);
11177 -- Given an arbitrary package, its depth and a target depth to reach,
11178 -- climb the scope chain until the said depth is reached. The pointer
11179 -- to the package and its depth a modified during the climb.
11180
11181 ----------------------------
11182 -- Distance_From_Standard --
11183 ----------------------------
11184
11185 function Distance_From_Standard (Pack : Entity_Id) return Nat is
11186 Dist : Nat;
11187 Scop : Entity_Id;
11188
11189 begin
11190 Dist := 0;
11191 Scop := Pack;
11192 while Present (Scop) and then Scop /= Standard_Standard loop
11193 Dist := Dist + 1;
11194 Scop := Scope (Scop);
11195 end loop;
11196
11197 return Dist;
11198 end Distance_From_Standard;
11199
11200 ---------------------
11201 -- Equalize_Depths --
11202 ---------------------
11203
11204 procedure Equalize_Depths
11205 (Pack : in out Entity_Id;
11206 Depth : in out Nat;
11207 Depth_To_Reach : Nat)
11208 is
11209 begin
11210 -- The package must be at a greater or equal depth
11211
11212 if Depth < Depth_To_Reach then
11213 raise Program_Error;
11214 end if;
11215
11216 -- Climb the scope chain until the desired depth is reached
11217
11218 while Present (Pack) and then Depth /= Depth_To_Reach loop
11219 Pack := Scope (Pack);
11220 Depth := Depth - 1;
11221 end loop;
11222 end Equalize_Depths;
11223
11224 -- Local variables
11225
11226 P_1 : Entity_Id := Pack_1;
11227 P_1_Child : Boolean := False;
11228 P_1_Depth : Nat := Distance_From_Standard (P_1);
11229 P_2 : Entity_Id := Pack_2;
11230 P_2_Child : Boolean := False;
11231 P_2_Depth : Nat := Distance_From_Standard (P_2);
11232
11233 -- Start of processing for Is_Child_Or_Sibling
11234
11235 begin
11236 pragma Assert
11237 (Ekind (Pack_1) = E_Package and then Ekind (Pack_2) = E_Package);
11238
11239 -- Both packages denote the same entity, therefore they cannot be
11240 -- children or siblings.
11241
11242 if P_1 = P_2 then
11243 return False;
11244
11245 -- One of the packages is at a deeper level than the other. Note that
11246 -- both may still come from differen hierarchies.
11247
11248 -- (root) P_2
11249 -- / \ :
11250 -- X P_2 or X
11251 -- : :
11252 -- P_1 P_1
11253
11254 elsif P_1_Depth > P_2_Depth then
11255 Equalize_Depths
11256 (Pack => P_1,
11257 Depth => P_1_Depth,
11258 Depth_To_Reach => P_2_Depth);
11259 P_1_Child := True;
11260
11261 -- (root) P_1
11262 -- / \ :
11263 -- P_1 X or X
11264 -- : :
11265 -- P_2 P_2
11266
11267 elsif P_2_Depth > P_1_Depth then
11268 Equalize_Depths
11269 (Pack => P_2,
11270 Depth => P_2_Depth,
11271 Depth_To_Reach => P_1_Depth);
11272 P_2_Child := True;
11273 end if;
11274
11275 -- At this stage the package pointers have been elevated to the same
11276 -- depth. If the related entities are the same, then one package is a
11277 -- potential child of the other:
11278
11279 -- P_1
11280 -- :
11281 -- X became P_1 P_2 or vica versa
11282 -- :
11283 -- P_2
11284
11285 if P_1 = P_2 then
11286 if P_1_Child then
11287 return Is_Child_Unit (Pack_1);
11288
11289 else pragma Assert (P_2_Child);
11290 return Is_Child_Unit (Pack_2);
11291 end if;
11292
11293 -- The packages may come from the same package chain or from entirely
11294 -- different hierarcies. To determine this, climb the scope stack until
11295 -- a common root is found.
11296
11297 -- (root) (root 1) (root 2)
11298 -- / \ | |
11299 -- P_1 P_2 P_1 P_2
11300
11301 else
11302 while Present (P_1) and then Present (P_2) loop
11303
11304 -- The two packages may be siblings
11305
11306 if P_1 = P_2 then
11307 return Is_Child_Unit (Pack_1) and then Is_Child_Unit (Pack_2);
11308 end if;
11309
11310 P_1 := Scope (P_1);
11311 P_2 := Scope (P_2);
11312 end loop;
11313 end if;
11314
11315 return False;
11316 end Is_Child_Or_Sibling;
11317
11318 -----------------------------
11319 -- Is_Concurrent_Interface --
11320 -----------------------------
11321
11322 function Is_Concurrent_Interface (T : Entity_Id) return Boolean is
11323 begin
11324 return Is_Interface (T)
11325 and then
11326 (Is_Protected_Interface (T)
11327 or else Is_Synchronized_Interface (T)
11328 or else Is_Task_Interface (T));
11329 end Is_Concurrent_Interface;
11330
11331 -----------------------
11332 -- Is_Constant_Bound --
11333 -----------------------
11334
11335 function Is_Constant_Bound (Exp : Node_Id) return Boolean is
11336 begin
11337 if Compile_Time_Known_Value (Exp) then
11338 return True;
11339
11340 elsif Is_Entity_Name (Exp) and then Present (Entity (Exp)) then
11341 return Is_Constant_Object (Entity (Exp))
11342 or else Ekind (Entity (Exp)) = E_Enumeration_Literal;
11343
11344 elsif Nkind (Exp) in N_Binary_Op then
11345 return Is_Constant_Bound (Left_Opnd (Exp))
11346 and then Is_Constant_Bound (Right_Opnd (Exp))
11347 and then Scope (Entity (Exp)) = Standard_Standard;
11348
11349 else
11350 return False;
11351 end if;
11352 end Is_Constant_Bound;
11353
11354 ---------------------------
11355 -- Is_Container_Element --
11356 ---------------------------
11357
11358 function Is_Container_Element (Exp : Node_Id) return Boolean is
11359 Loc : constant Source_Ptr := Sloc (Exp);
11360 Pref : constant Node_Id := Prefix (Exp);
11361
11362 Call : Node_Id;
11363 -- Call to an indexing aspect
11364
11365 Cont_Typ : Entity_Id;
11366 -- The type of the container being accessed
11367
11368 Elem_Typ : Entity_Id;
11369 -- Its element type
11370
11371 Indexing : Entity_Id;
11372 Is_Const : Boolean;
11373 -- Indicates that constant indexing is used, and the element is thus
11374 -- a constant.
11375
11376 Ref_Typ : Entity_Id;
11377 -- The reference type returned by the indexing operation
11378
11379 begin
11380 -- If C is a container, in a context that imposes the element type of
11381 -- that container, the indexing notation C (X) is rewritten as:
11382
11383 -- Indexing (C, X).Discr.all
11384
11385 -- where Indexing is one of the indexing aspects of the container.
11386 -- If the context does not require a reference, the construct can be
11387 -- rewritten as
11388
11389 -- Element (C, X)
11390
11391 -- First, verify that the construct has the proper form
11392
11393 if not Expander_Active then
11394 return False;
11395
11396 elsif Nkind (Pref) /= N_Selected_Component then
11397 return False;
11398
11399 elsif Nkind (Prefix (Pref)) /= N_Function_Call then
11400 return False;
11401
11402 else
11403 Call := Prefix (Pref);
11404 Ref_Typ := Etype (Call);
11405 end if;
11406
11407 if not Has_Implicit_Dereference (Ref_Typ)
11408 or else No (First (Parameter_Associations (Call)))
11409 or else not Is_Entity_Name (Name (Call))
11410 then
11411 return False;
11412 end if;
11413
11414 -- Retrieve type of container object, and its iterator aspects
11415
11416 Cont_Typ := Etype (First (Parameter_Associations (Call)));
11417 Indexing := Find_Value_Of_Aspect (Cont_Typ, Aspect_Constant_Indexing);
11418 Is_Const := False;
11419
11420 if No (Indexing) then
11421
11422 -- Container should have at least one indexing operation
11423
11424 return False;
11425
11426 elsif Entity (Name (Call)) /= Entity (Indexing) then
11427
11428 -- This may be a variable indexing operation
11429
11430 Indexing := Find_Value_Of_Aspect (Cont_Typ, Aspect_Variable_Indexing);
11431
11432 if No (Indexing)
11433 or else Entity (Name (Call)) /= Entity (Indexing)
11434 then
11435 return False;
11436 end if;
11437
11438 else
11439 Is_Const := True;
11440 end if;
11441
11442 Elem_Typ := Find_Value_Of_Aspect (Cont_Typ, Aspect_Iterator_Element);
11443
11444 if No (Elem_Typ) or else Entity (Elem_Typ) /= Etype (Exp) then
11445 return False;
11446 end if;
11447
11448 -- Check that the expression is not the target of an assignment, in
11449 -- which case the rewriting is not possible.
11450
11451 if not Is_Const then
11452 declare
11453 Par : Node_Id;
11454
11455 begin
11456 Par := Exp;
11457 while Present (Par)
11458 loop
11459 if Nkind (Parent (Par)) = N_Assignment_Statement
11460 and then Par = Name (Parent (Par))
11461 then
11462 return False;
11463
11464 -- A renaming produces a reference, and the transformation
11465 -- does not apply.
11466
11467 elsif Nkind (Parent (Par)) = N_Object_Renaming_Declaration then
11468 return False;
11469
11470 elsif Nkind_In
11471 (Nkind (Parent (Par)), N_Function_Call,
11472 N_Procedure_Call_Statement,
11473 N_Entry_Call_Statement)
11474 then
11475 -- Check that the element is not part of an actual for an
11476 -- in-out parameter.
11477
11478 declare
11479 F : Entity_Id;
11480 A : Node_Id;
11481
11482 begin
11483 F := First_Formal (Entity (Name (Parent (Par))));
11484 A := First (Parameter_Associations (Parent (Par)));
11485 while Present (F) loop
11486 if A = Par and then Ekind (F) /= E_In_Parameter then
11487 return False;
11488 end if;
11489
11490 Next_Formal (F);
11491 Next (A);
11492 end loop;
11493 end;
11494
11495 -- E_In_Parameter in a call: element is not modified.
11496
11497 exit;
11498 end if;
11499
11500 Par := Parent (Par);
11501 end loop;
11502 end;
11503 end if;
11504
11505 -- The expression has the proper form and the context requires the
11506 -- element type. Retrieve the Element function of the container and
11507 -- rewrite the construct as a call to it.
11508
11509 declare
11510 Op : Elmt_Id;
11511
11512 begin
11513 Op := First_Elmt (Primitive_Operations (Cont_Typ));
11514 while Present (Op) loop
11515 exit when Chars (Node (Op)) = Name_Element;
11516 Next_Elmt (Op);
11517 end loop;
11518
11519 if No (Op) then
11520 return False;
11521
11522 else
11523 Rewrite (Exp,
11524 Make_Function_Call (Loc,
11525 Name => New_Occurrence_Of (Node (Op), Loc),
11526 Parameter_Associations => Parameter_Associations (Call)));
11527 Analyze_And_Resolve (Exp, Entity (Elem_Typ));
11528 return True;
11529 end if;
11530 end;
11531 end Is_Container_Element;
11532
11533 ----------------------------
11534 -- Is_Contract_Annotation --
11535 ----------------------------
11536
11537 function Is_Contract_Annotation (Item : Node_Id) return Boolean is
11538 begin
11539 return Is_Package_Contract_Annotation (Item)
11540 or else
11541 Is_Subprogram_Contract_Annotation (Item);
11542 end Is_Contract_Annotation;
11543
11544 --------------------------------------
11545 -- Is_Controlling_Limited_Procedure --
11546 --------------------------------------
11547
11548 function Is_Controlling_Limited_Procedure
11549 (Proc_Nam : Entity_Id) return Boolean
11550 is
11551 Param_Typ : Entity_Id := Empty;
11552
11553 begin
11554 if Ekind (Proc_Nam) = E_Procedure
11555 and then Present (Parameter_Specifications (Parent (Proc_Nam)))
11556 then
11557 Param_Typ := Etype (Parameter_Type (First (
11558 Parameter_Specifications (Parent (Proc_Nam)))));
11559
11560 -- In this case where an Itype was created, the procedure call has been
11561 -- rewritten.
11562
11563 elsif Present (Associated_Node_For_Itype (Proc_Nam))
11564 and then Present (Original_Node (Associated_Node_For_Itype (Proc_Nam)))
11565 and then
11566 Present (Parameter_Associations
11567 (Associated_Node_For_Itype (Proc_Nam)))
11568 then
11569 Param_Typ :=
11570 Etype (First (Parameter_Associations
11571 (Associated_Node_For_Itype (Proc_Nam))));
11572 end if;
11573
11574 if Present (Param_Typ) then
11575 return
11576 Is_Interface (Param_Typ)
11577 and then Is_Limited_Record (Param_Typ);
11578 end if;
11579
11580 return False;
11581 end Is_Controlling_Limited_Procedure;
11582
11583 -----------------------------
11584 -- Is_CPP_Constructor_Call --
11585 -----------------------------
11586
11587 function Is_CPP_Constructor_Call (N : Node_Id) return Boolean is
11588 begin
11589 return Nkind (N) = N_Function_Call
11590 and then Is_CPP_Class (Etype (Etype (N)))
11591 and then Is_Constructor (Entity (Name (N)))
11592 and then Is_Imported (Entity (Name (N)));
11593 end Is_CPP_Constructor_Call;
11594
11595 -------------------------
11596 -- Is_Current_Instance --
11597 -------------------------
11598
11599 function Is_Current_Instance (N : Node_Id) return Boolean is
11600 Typ : constant Entity_Id := Entity (N);
11601 P : Node_Id;
11602
11603 begin
11604 -- Simplest case: entity is a concurrent type and we are currently
11605 -- inside the body. This will eventually be expanded into a
11606 -- call to Self (for tasks) or _object (for protected objects).
11607
11608 if Is_Concurrent_Type (Typ) and then In_Open_Scopes (Typ) then
11609 return True;
11610
11611 else
11612 -- Check whether the context is a (sub)type declaration for the
11613 -- type entity.
11614
11615 P := Parent (N);
11616 while Present (P) loop
11617 if Nkind_In (P, N_Full_Type_Declaration,
11618 N_Private_Type_Declaration,
11619 N_Subtype_Declaration)
11620 and then Comes_From_Source (P)
11621 and then Defining_Entity (P) = Typ
11622 then
11623 return True;
11624
11625 -- A subtype name may appear in an aspect specification for a
11626 -- Predicate_Failure aspect, for which we do not construct a
11627 -- wrapper procedure. The subtype will be replaced by the
11628 -- expression being tested when the corresponding predicate
11629 -- check is expanded.
11630
11631 elsif Nkind (P) = N_Aspect_Specification
11632 and then Nkind (Parent (P)) = N_Subtype_Declaration
11633 then
11634 return True;
11635
11636 elsif Nkind (P) = N_Pragma
11637 and then
11638 Get_Pragma_Id (Pragma_Name (P)) = Pragma_Predicate_Failure
11639 then
11640 return True;
11641 end if;
11642
11643 P := Parent (P);
11644 end loop;
11645 end if;
11646
11647 -- In any other context this is not a current occurrence
11648
11649 return False;
11650 end Is_Current_Instance;
11651
11652 --------------------
11653 -- Is_Declaration --
11654 --------------------
11655
11656 function Is_Declaration (N : Node_Id) return Boolean is
11657 begin
11658 case Nkind (N) is
11659 when N_Abstract_Subprogram_Declaration |
11660 N_Exception_Declaration |
11661 N_Exception_Renaming_Declaration |
11662 N_Full_Type_Declaration |
11663 N_Generic_Function_Renaming_Declaration |
11664 N_Generic_Package_Declaration |
11665 N_Generic_Package_Renaming_Declaration |
11666 N_Generic_Procedure_Renaming_Declaration |
11667 N_Generic_Subprogram_Declaration |
11668 N_Number_Declaration |
11669 N_Object_Declaration |
11670 N_Object_Renaming_Declaration |
11671 N_Package_Declaration |
11672 N_Package_Renaming_Declaration |
11673 N_Private_Extension_Declaration |
11674 N_Private_Type_Declaration |
11675 N_Subprogram_Declaration |
11676 N_Subprogram_Renaming_Declaration |
11677 N_Subtype_Declaration =>
11678 return True;
11679
11680 when others =>
11681 return False;
11682 end case;
11683 end Is_Declaration;
11684
11685 --------------------------------
11686 -- Is_Declared_Within_Variant --
11687 --------------------------------
11688
11689 function Is_Declared_Within_Variant (Comp : Entity_Id) return Boolean is
11690 Comp_Decl : constant Node_Id := Parent (Comp);
11691 Comp_List : constant Node_Id := Parent (Comp_Decl);
11692 begin
11693 return Nkind (Parent (Comp_List)) = N_Variant;
11694 end Is_Declared_Within_Variant;
11695
11696 ----------------------------------------------
11697 -- Is_Dependent_Component_Of_Mutable_Object --
11698 ----------------------------------------------
11699
11700 function Is_Dependent_Component_Of_Mutable_Object
11701 (Object : Node_Id) return Boolean
11702 is
11703 P : Node_Id;
11704 Prefix_Type : Entity_Id;
11705 P_Aliased : Boolean := False;
11706 Comp : Entity_Id;
11707
11708 Deref : Node_Id := Object;
11709 -- Dereference node, in something like X.all.Y(2)
11710
11711 -- Start of processing for Is_Dependent_Component_Of_Mutable_Object
11712
11713 begin
11714 -- Find the dereference node if any
11715
11716 while Nkind_In (Deref, N_Indexed_Component,
11717 N_Selected_Component,
11718 N_Slice)
11719 loop
11720 Deref := Prefix (Deref);
11721 end loop;
11722
11723 -- Ada 2005: If we have a component or slice of a dereference,
11724 -- something like X.all.Y (2), and the type of X is access-to-constant,
11725 -- Is_Variable will return False, because it is indeed a constant
11726 -- view. But it might be a view of a variable object, so we want the
11727 -- following condition to be True in that case.
11728
11729 if Is_Variable (Object)
11730 or else (Ada_Version >= Ada_2005
11731 and then Nkind (Deref) = N_Explicit_Dereference)
11732 then
11733 if Nkind (Object) = N_Selected_Component then
11734 P := Prefix (Object);
11735 Prefix_Type := Etype (P);
11736
11737 if Is_Entity_Name (P) then
11738 if Ekind (Entity (P)) = E_Generic_In_Out_Parameter then
11739 Prefix_Type := Base_Type (Prefix_Type);
11740 end if;
11741
11742 if Is_Aliased (Entity (P)) then
11743 P_Aliased := True;
11744 end if;
11745
11746 -- A discriminant check on a selected component may be expanded
11747 -- into a dereference when removing side-effects. Recover the
11748 -- original node and its type, which may be unconstrained.
11749
11750 elsif Nkind (P) = N_Explicit_Dereference
11751 and then not (Comes_From_Source (P))
11752 then
11753 P := Original_Node (P);
11754 Prefix_Type := Etype (P);
11755
11756 else
11757 -- Check for prefix being an aliased component???
11758
11759 null;
11760
11761 end if;
11762
11763 -- A heap object is constrained by its initial value
11764
11765 -- Ada 2005 (AI-363): Always assume the object could be mutable in
11766 -- the dereferenced case, since the access value might denote an
11767 -- unconstrained aliased object, whereas in Ada 95 the designated
11768 -- object is guaranteed to be constrained. A worst-case assumption
11769 -- has to apply in Ada 2005 because we can't tell at compile
11770 -- time whether the object is "constrained by its initial value"
11771 -- (despite the fact that 3.10.2(26/2) and 8.5.1(5/2) are semantic
11772 -- rules (these rules are acknowledged to need fixing).
11773
11774 if Ada_Version < Ada_2005 then
11775 if Is_Access_Type (Prefix_Type)
11776 or else Nkind (P) = N_Explicit_Dereference
11777 then
11778 return False;
11779 end if;
11780
11781 else pragma Assert (Ada_Version >= Ada_2005);
11782 if Is_Access_Type (Prefix_Type) then
11783
11784 -- If the access type is pool-specific, and there is no
11785 -- constrained partial view of the designated type, then the
11786 -- designated object is known to be constrained.
11787
11788 if Ekind (Prefix_Type) = E_Access_Type
11789 and then not Object_Type_Has_Constrained_Partial_View
11790 (Typ => Designated_Type (Prefix_Type),
11791 Scop => Current_Scope)
11792 then
11793 return False;
11794
11795 -- Otherwise (general access type, or there is a constrained
11796 -- partial view of the designated type), we need to check
11797 -- based on the designated type.
11798
11799 else
11800 Prefix_Type := Designated_Type (Prefix_Type);
11801 end if;
11802 end if;
11803 end if;
11804
11805 Comp :=
11806 Original_Record_Component (Entity (Selector_Name (Object)));
11807
11808 -- As per AI-0017, the renaming is illegal in a generic body, even
11809 -- if the subtype is indefinite.
11810
11811 -- Ada 2005 (AI-363): In Ada 2005 an aliased object can be mutable
11812
11813 if not Is_Constrained (Prefix_Type)
11814 and then (Is_Definite_Subtype (Prefix_Type)
11815 or else
11816 (Is_Generic_Type (Prefix_Type)
11817 and then Ekind (Current_Scope) = E_Generic_Package
11818 and then In_Package_Body (Current_Scope)))
11819
11820 and then (Is_Declared_Within_Variant (Comp)
11821 or else Has_Discriminant_Dependent_Constraint (Comp))
11822 and then (not P_Aliased or else Ada_Version >= Ada_2005)
11823 then
11824 return True;
11825
11826 -- If the prefix is of an access type at this point, then we want
11827 -- to return False, rather than calling this function recursively
11828 -- on the access object (which itself might be a discriminant-
11829 -- dependent component of some other object, but that isn't
11830 -- relevant to checking the object passed to us). This avoids
11831 -- issuing wrong errors when compiling with -gnatc, where there
11832 -- can be implicit dereferences that have not been expanded.
11833
11834 elsif Is_Access_Type (Etype (Prefix (Object))) then
11835 return False;
11836
11837 else
11838 return
11839 Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
11840 end if;
11841
11842 elsif Nkind (Object) = N_Indexed_Component
11843 or else Nkind (Object) = N_Slice
11844 then
11845 return Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
11846
11847 -- A type conversion that Is_Variable is a view conversion:
11848 -- go back to the denoted object.
11849
11850 elsif Nkind (Object) = N_Type_Conversion then
11851 return
11852 Is_Dependent_Component_Of_Mutable_Object (Expression (Object));
11853 end if;
11854 end if;
11855
11856 return False;
11857 end Is_Dependent_Component_Of_Mutable_Object;
11858
11859 ---------------------
11860 -- Is_Dereferenced --
11861 ---------------------
11862
11863 function Is_Dereferenced (N : Node_Id) return Boolean is
11864 P : constant Node_Id := Parent (N);
11865 begin
11866 return Nkind_In (P, N_Selected_Component,
11867 N_Explicit_Dereference,
11868 N_Indexed_Component,
11869 N_Slice)
11870 and then Prefix (P) = N;
11871 end Is_Dereferenced;
11872
11873 ----------------------
11874 -- Is_Descendant_Of --
11875 ----------------------
11876
11877 function Is_Descendant_Of (T1 : Entity_Id; T2 : Entity_Id) return Boolean is
11878 T : Entity_Id;
11879 Etyp : Entity_Id;
11880
11881 begin
11882 pragma Assert (Nkind (T1) in N_Entity);
11883 pragma Assert (Nkind (T2) in N_Entity);
11884
11885 T := Base_Type (T1);
11886
11887 -- Immediate return if the types match
11888
11889 if T = T2 then
11890 return True;
11891
11892 -- Comment needed here ???
11893
11894 elsif Ekind (T) = E_Class_Wide_Type then
11895 return Etype (T) = T2;
11896
11897 -- All other cases
11898
11899 else
11900 loop
11901 Etyp := Etype (T);
11902
11903 -- Done if we found the type we are looking for
11904
11905 if Etyp = T2 then
11906 return True;
11907
11908 -- Done if no more derivations to check
11909
11910 elsif T = T1
11911 or else T = Etyp
11912 then
11913 return False;
11914
11915 -- Following test catches error cases resulting from prev errors
11916
11917 elsif No (Etyp) then
11918 return False;
11919
11920 elsif Is_Private_Type (T) and then Etyp = Full_View (T) then
11921 return False;
11922
11923 elsif Is_Private_Type (Etyp) and then Full_View (Etyp) = T then
11924 return False;
11925 end if;
11926
11927 T := Base_Type (Etyp);
11928 end loop;
11929 end if;
11930 end Is_Descendant_Of;
11931
11932 ----------------------------------------
11933 -- Is_Descendant_Of_Suspension_Object --
11934 ----------------------------------------
11935
11936 function Is_Descendant_Of_Suspension_Object
11937 (Typ : Entity_Id) return Boolean
11938 is
11939 Cur_Typ : Entity_Id;
11940 Par_Typ : Entity_Id;
11941
11942 begin
11943 -- Climb the type derivation chain checking each parent type against
11944 -- Suspension_Object.
11945
11946 Cur_Typ := Base_Type (Typ);
11947 while Present (Cur_Typ) loop
11948 Par_Typ := Etype (Cur_Typ);
11949
11950 -- The current type is a match
11951
11952 if Is_Suspension_Object (Cur_Typ) then
11953 return True;
11954
11955 -- Stop the traversal once the root of the derivation chain has been
11956 -- reached. In that case the current type is its own base type.
11957
11958 elsif Cur_Typ = Par_Typ then
11959 exit;
11960 end if;
11961
11962 Cur_Typ := Base_Type (Par_Typ);
11963 end loop;
11964
11965 return False;
11966 end Is_Descendant_Of_Suspension_Object;
11967
11968 ---------------------------------------------
11969 -- Is_Double_Precision_Floating_Point_Type --
11970 ---------------------------------------------
11971
11972 function Is_Double_Precision_Floating_Point_Type
11973 (E : Entity_Id) return Boolean is
11974 begin
11975 return Is_Floating_Point_Type (E)
11976 and then Machine_Radix_Value (E) = Uint_2
11977 and then Machine_Mantissa_Value (E) = UI_From_Int (53)
11978 and then Machine_Emax_Value (E) = Uint_2 ** Uint_10
11979 and then Machine_Emin_Value (E) = Uint_3 - (Uint_2 ** Uint_10);
11980 end Is_Double_Precision_Floating_Point_Type;
11981
11982 -----------------------------
11983 -- Is_Effectively_Volatile --
11984 -----------------------------
11985
11986 function Is_Effectively_Volatile (Id : Entity_Id) return Boolean is
11987 begin
11988 if Is_Type (Id) then
11989
11990 -- An arbitrary type is effectively volatile when it is subject to
11991 -- pragma Atomic or Volatile.
11992
11993 if Is_Volatile (Id) then
11994 return True;
11995
11996 -- An array type is effectively volatile when it is subject to pragma
11997 -- Atomic_Components or Volatile_Components or its compolent type is
11998 -- effectively volatile.
11999
12000 elsif Is_Array_Type (Id) then
12001 return
12002 Has_Volatile_Components (Id)
12003 or else
12004 Is_Effectively_Volatile (Component_Type (Base_Type (Id)));
12005
12006 -- A protected type is always volatile
12007
12008 elsif Is_Protected_Type (Id) then
12009 return True;
12010
12011 -- A descendant of Ada.Synchronous_Task_Control.Suspension_Object is
12012 -- automatically volatile.
12013
12014 elsif Is_Descendant_Of_Suspension_Object (Id) then
12015 return True;
12016
12017 -- Otherwise the type is not effectively volatile
12018
12019 else
12020 return False;
12021 end if;
12022
12023 -- Otherwise Id denotes an object
12024
12025 else
12026 return
12027 Is_Volatile (Id)
12028 or else Has_Volatile_Components (Id)
12029 or else Is_Effectively_Volatile (Etype (Id));
12030 end if;
12031 end Is_Effectively_Volatile;
12032
12033 ------------------------------------
12034 -- Is_Effectively_Volatile_Object --
12035 ------------------------------------
12036
12037 function Is_Effectively_Volatile_Object (N : Node_Id) return Boolean is
12038 begin
12039 if Is_Entity_Name (N) then
12040 return Is_Effectively_Volatile (Entity (N));
12041
12042 elsif Nkind (N) = N_Expanded_Name then
12043 return Is_Effectively_Volatile (Entity (N));
12044
12045 elsif Nkind (N) = N_Indexed_Component then
12046 return Is_Effectively_Volatile_Object (Prefix (N));
12047
12048 elsif Nkind (N) = N_Selected_Component then
12049 return
12050 Is_Effectively_Volatile_Object (Prefix (N))
12051 or else
12052 Is_Effectively_Volatile_Object (Selector_Name (N));
12053
12054 else
12055 return False;
12056 end if;
12057 end Is_Effectively_Volatile_Object;
12058
12059 -------------------
12060 -- Is_Entry_Body --
12061 -------------------
12062
12063 function Is_Entry_Body (Id : Entity_Id) return Boolean is
12064 begin
12065 return
12066 Ekind_In (Id, E_Entry, E_Entry_Family)
12067 and then Nkind (Unit_Declaration_Node (Id)) = N_Entry_Body;
12068 end Is_Entry_Body;
12069
12070 --------------------------
12071 -- Is_Entry_Declaration --
12072 --------------------------
12073
12074 function Is_Entry_Declaration (Id : Entity_Id) return Boolean is
12075 begin
12076 return
12077 Ekind_In (Id, E_Entry, E_Entry_Family)
12078 and then Nkind (Unit_Declaration_Node (Id)) = N_Entry_Declaration;
12079 end Is_Entry_Declaration;
12080
12081 ----------------------------
12082 -- Is_Expression_Function --
12083 ----------------------------
12084
12085 function Is_Expression_Function (Subp : Entity_Id) return Boolean is
12086 begin
12087 if Ekind_In (Subp, E_Function, E_Subprogram_Body) then
12088 return
12089 Nkind (Original_Node (Unit_Declaration_Node (Subp))) =
12090 N_Expression_Function;
12091 else
12092 return False;
12093 end if;
12094 end Is_Expression_Function;
12095
12096 ------------------------------------------
12097 -- Is_Expression_Function_Or_Completion --
12098 ------------------------------------------
12099
12100 function Is_Expression_Function_Or_Completion
12101 (Subp : Entity_Id) return Boolean
12102 is
12103 Subp_Decl : Node_Id;
12104
12105 begin
12106 if Ekind (Subp) = E_Function then
12107 Subp_Decl := Unit_Declaration_Node (Subp);
12108
12109 -- The function declaration is either an expression function or is
12110 -- completed by an expression function body.
12111
12112 return
12113 Is_Expression_Function (Subp)
12114 or else (Nkind (Subp_Decl) = N_Subprogram_Declaration
12115 and then Present (Corresponding_Body (Subp_Decl))
12116 and then Is_Expression_Function
12117 (Corresponding_Body (Subp_Decl)));
12118
12119 elsif Ekind (Subp) = E_Subprogram_Body then
12120 return Is_Expression_Function (Subp);
12121
12122 else
12123 return False;
12124 end if;
12125 end Is_Expression_Function_Or_Completion;
12126
12127 -----------------------
12128 -- Is_EVF_Expression --
12129 -----------------------
12130
12131 function Is_EVF_Expression (N : Node_Id) return Boolean is
12132 Orig_N : constant Node_Id := Original_Node (N);
12133 Alt : Node_Id;
12134 Expr : Node_Id;
12135 Id : Entity_Id;
12136
12137 begin
12138 -- Detect a reference to a formal parameter of a specific tagged type
12139 -- whose related subprogram is subject to pragma Expresions_Visible with
12140 -- value "False".
12141
12142 if Is_Entity_Name (N) and then Present (Entity (N)) then
12143 Id := Entity (N);
12144
12145 return
12146 Is_Formal (Id)
12147 and then Is_Specific_Tagged_Type (Etype (Id))
12148 and then Extensions_Visible_Status (Id) =
12149 Extensions_Visible_False;
12150
12151 -- A case expression is an EVF expression when it contains at least one
12152 -- EVF dependent_expression. Note that a case expression may have been
12153 -- expanded, hence the use of Original_Node.
12154
12155 elsif Nkind (Orig_N) = N_Case_Expression then
12156 Alt := First (Alternatives (Orig_N));
12157 while Present (Alt) loop
12158 if Is_EVF_Expression (Expression (Alt)) then
12159 return True;
12160 end if;
12161
12162 Next (Alt);
12163 end loop;
12164
12165 -- An if expression is an EVF expression when it contains at least one
12166 -- EVF dependent_expression. Note that an if expression may have been
12167 -- expanded, hence the use of Original_Node.
12168
12169 elsif Nkind (Orig_N) = N_If_Expression then
12170 Expr := Next (First (Expressions (Orig_N)));
12171 while Present (Expr) loop
12172 if Is_EVF_Expression (Expr) then
12173 return True;
12174 end if;
12175
12176 Next (Expr);
12177 end loop;
12178
12179 -- A qualified expression or a type conversion is an EVF expression when
12180 -- its operand is an EVF expression.
12181
12182 elsif Nkind_In (N, N_Qualified_Expression,
12183 N_Unchecked_Type_Conversion,
12184 N_Type_Conversion)
12185 then
12186 return Is_EVF_Expression (Expression (N));
12187
12188 -- Attributes 'Loop_Entry, 'Old, and 'Update are EVF expressions when
12189 -- their prefix denotes an EVF expression.
12190
12191 elsif Nkind (N) = N_Attribute_Reference
12192 and then Nam_In (Attribute_Name (N), Name_Loop_Entry,
12193 Name_Old,
12194 Name_Update)
12195 then
12196 return Is_EVF_Expression (Prefix (N));
12197 end if;
12198
12199 return False;
12200 end Is_EVF_Expression;
12201
12202 --------------
12203 -- Is_False --
12204 --------------
12205
12206 function Is_False (U : Uint) return Boolean is
12207 begin
12208 return (U = 0);
12209 end Is_False;
12210
12211 ---------------------------
12212 -- Is_Fixed_Model_Number --
12213 ---------------------------
12214
12215 function Is_Fixed_Model_Number (U : Ureal; T : Entity_Id) return Boolean is
12216 S : constant Ureal := Small_Value (T);
12217 M : Urealp.Save_Mark;
12218 R : Boolean;
12219 begin
12220 M := Urealp.Mark;
12221 R := (U = UR_Trunc (U / S) * S);
12222 Urealp.Release (M);
12223 return R;
12224 end Is_Fixed_Model_Number;
12225
12226 -------------------------------
12227 -- Is_Fully_Initialized_Type --
12228 -------------------------------
12229
12230 function Is_Fully_Initialized_Type (Typ : Entity_Id) return Boolean is
12231 begin
12232 -- Scalar types
12233
12234 if Is_Scalar_Type (Typ) then
12235
12236 -- A scalar type with an aspect Default_Value is fully initialized
12237
12238 -- Note: Iniitalize/Normalize_Scalars also ensure full initialization
12239 -- of a scalar type, but we don't take that into account here, since
12240 -- we don't want these to affect warnings.
12241
12242 return Has_Default_Aspect (Typ);
12243
12244 elsif Is_Access_Type (Typ) then
12245 return True;
12246
12247 elsif Is_Array_Type (Typ) then
12248 if Is_Fully_Initialized_Type (Component_Type (Typ))
12249 or else (Ada_Version >= Ada_2012 and then Has_Default_Aspect (Typ))
12250 then
12251 return True;
12252 end if;
12253
12254 -- An interesting case, if we have a constrained type one of whose
12255 -- bounds is known to be null, then there are no elements to be
12256 -- initialized, so all the elements are initialized.
12257
12258 if Is_Constrained (Typ) then
12259 declare
12260 Indx : Node_Id;
12261 Indx_Typ : Entity_Id;
12262 Lbd, Hbd : Node_Id;
12263
12264 begin
12265 Indx := First_Index (Typ);
12266 while Present (Indx) loop
12267 if Etype (Indx) = Any_Type then
12268 return False;
12269
12270 -- If index is a range, use directly
12271
12272 elsif Nkind (Indx) = N_Range then
12273 Lbd := Low_Bound (Indx);
12274 Hbd := High_Bound (Indx);
12275
12276 else
12277 Indx_Typ := Etype (Indx);
12278
12279 if Is_Private_Type (Indx_Typ) then
12280 Indx_Typ := Full_View (Indx_Typ);
12281 end if;
12282
12283 if No (Indx_Typ) or else Etype (Indx_Typ) = Any_Type then
12284 return False;
12285 else
12286 Lbd := Type_Low_Bound (Indx_Typ);
12287 Hbd := Type_High_Bound (Indx_Typ);
12288 end if;
12289 end if;
12290
12291 if Compile_Time_Known_Value (Lbd)
12292 and then
12293 Compile_Time_Known_Value (Hbd)
12294 then
12295 if Expr_Value (Hbd) < Expr_Value (Lbd) then
12296 return True;
12297 end if;
12298 end if;
12299
12300 Next_Index (Indx);
12301 end loop;
12302 end;
12303 end if;
12304
12305 -- If no null indexes, then type is not fully initialized
12306
12307 return False;
12308
12309 -- Record types
12310
12311 elsif Is_Record_Type (Typ) then
12312 if Has_Discriminants (Typ)
12313 and then
12314 Present (Discriminant_Default_Value (First_Discriminant (Typ)))
12315 and then Is_Fully_Initialized_Variant (Typ)
12316 then
12317 return True;
12318 end if;
12319
12320 -- We consider bounded string types to be fully initialized, because
12321 -- otherwise we get false alarms when the Data component is not
12322 -- default-initialized.
12323
12324 if Is_Bounded_String (Typ) then
12325 return True;
12326 end if;
12327
12328 -- Controlled records are considered to be fully initialized if
12329 -- there is a user defined Initialize routine. This may not be
12330 -- entirely correct, but as the spec notes, we are guessing here
12331 -- what is best from the point of view of issuing warnings.
12332
12333 if Is_Controlled (Typ) then
12334 declare
12335 Utyp : constant Entity_Id := Underlying_Type (Typ);
12336
12337 begin
12338 if Present (Utyp) then
12339 declare
12340 Init : constant Entity_Id :=
12341 (Find_Optional_Prim_Op
12342 (Underlying_Type (Typ), Name_Initialize));
12343
12344 begin
12345 if Present (Init)
12346 and then Comes_From_Source (Init)
12347 and then not
12348 Is_Predefined_File_Name
12349 (File_Name (Get_Source_File_Index (Sloc (Init))))
12350 then
12351 return True;
12352
12353 elsif Has_Null_Extension (Typ)
12354 and then
12355 Is_Fully_Initialized_Type
12356 (Etype (Base_Type (Typ)))
12357 then
12358 return True;
12359 end if;
12360 end;
12361 end if;
12362 end;
12363 end if;
12364
12365 -- Otherwise see if all record components are initialized
12366
12367 declare
12368 Ent : Entity_Id;
12369
12370 begin
12371 Ent := First_Entity (Typ);
12372 while Present (Ent) loop
12373 if Ekind (Ent) = E_Component
12374 and then (No (Parent (Ent))
12375 or else No (Expression (Parent (Ent))))
12376 and then not Is_Fully_Initialized_Type (Etype (Ent))
12377
12378 -- Special VM case for tag components, which need to be
12379 -- defined in this case, but are never initialized as VMs
12380 -- are using other dispatching mechanisms. Ignore this
12381 -- uninitialized case. Note that this applies both to the
12382 -- uTag entry and the main vtable pointer (CPP_Class case).
12383
12384 and then (Tagged_Type_Expansion or else not Is_Tag (Ent))
12385 then
12386 return False;
12387 end if;
12388
12389 Next_Entity (Ent);
12390 end loop;
12391 end;
12392
12393 -- No uninitialized components, so type is fully initialized.
12394 -- Note that this catches the case of no components as well.
12395
12396 return True;
12397
12398 elsif Is_Concurrent_Type (Typ) then
12399 return True;
12400
12401 elsif Is_Private_Type (Typ) then
12402 declare
12403 U : constant Entity_Id := Underlying_Type (Typ);
12404
12405 begin
12406 if No (U) then
12407 return False;
12408 else
12409 return Is_Fully_Initialized_Type (U);
12410 end if;
12411 end;
12412
12413 else
12414 return False;
12415 end if;
12416 end Is_Fully_Initialized_Type;
12417
12418 ----------------------------------
12419 -- Is_Fully_Initialized_Variant --
12420 ----------------------------------
12421
12422 function Is_Fully_Initialized_Variant (Typ : Entity_Id) return Boolean is
12423 Loc : constant Source_Ptr := Sloc (Typ);
12424 Constraints : constant List_Id := New_List;
12425 Components : constant Elist_Id := New_Elmt_List;
12426 Comp_Elmt : Elmt_Id;
12427 Comp_Id : Node_Id;
12428 Comp_List : Node_Id;
12429 Discr : Entity_Id;
12430 Discr_Val : Node_Id;
12431
12432 Report_Errors : Boolean;
12433 pragma Warnings (Off, Report_Errors);
12434
12435 begin
12436 if Serious_Errors_Detected > 0 then
12437 return False;
12438 end if;
12439
12440 if Is_Record_Type (Typ)
12441 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
12442 and then Nkind (Type_Definition (Parent (Typ))) = N_Record_Definition
12443 then
12444 Comp_List := Component_List (Type_Definition (Parent (Typ)));
12445
12446 Discr := First_Discriminant (Typ);
12447 while Present (Discr) loop
12448 if Nkind (Parent (Discr)) = N_Discriminant_Specification then
12449 Discr_Val := Expression (Parent (Discr));
12450
12451 if Present (Discr_Val)
12452 and then Is_OK_Static_Expression (Discr_Val)
12453 then
12454 Append_To (Constraints,
12455 Make_Component_Association (Loc,
12456 Choices => New_List (New_Occurrence_Of (Discr, Loc)),
12457 Expression => New_Copy (Discr_Val)));
12458 else
12459 return False;
12460 end if;
12461 else
12462 return False;
12463 end if;
12464
12465 Next_Discriminant (Discr);
12466 end loop;
12467
12468 Gather_Components
12469 (Typ => Typ,
12470 Comp_List => Comp_List,
12471 Governed_By => Constraints,
12472 Into => Components,
12473 Report_Errors => Report_Errors);
12474
12475 -- Check that each component present is fully initialized
12476
12477 Comp_Elmt := First_Elmt (Components);
12478 while Present (Comp_Elmt) loop
12479 Comp_Id := Node (Comp_Elmt);
12480
12481 if Ekind (Comp_Id) = E_Component
12482 and then (No (Parent (Comp_Id))
12483 or else No (Expression (Parent (Comp_Id))))
12484 and then not Is_Fully_Initialized_Type (Etype (Comp_Id))
12485 then
12486 return False;
12487 end if;
12488
12489 Next_Elmt (Comp_Elmt);
12490 end loop;
12491
12492 return True;
12493
12494 elsif Is_Private_Type (Typ) then
12495 declare
12496 U : constant Entity_Id := Underlying_Type (Typ);
12497
12498 begin
12499 if No (U) then
12500 return False;
12501 else
12502 return Is_Fully_Initialized_Variant (U);
12503 end if;
12504 end;
12505
12506 else
12507 return False;
12508 end if;
12509 end Is_Fully_Initialized_Variant;
12510
12511 ------------------------------------
12512 -- Is_Generic_Declaration_Or_Body --
12513 ------------------------------------
12514
12515 function Is_Generic_Declaration_Or_Body (Decl : Node_Id) return Boolean is
12516 Spec_Decl : Node_Id;
12517
12518 begin
12519 -- Package/subprogram body
12520
12521 if Nkind_In (Decl, N_Package_Body, N_Subprogram_Body)
12522 and then Present (Corresponding_Spec (Decl))
12523 then
12524 Spec_Decl := Unit_Declaration_Node (Corresponding_Spec (Decl));
12525
12526 -- Package/subprogram body stub
12527
12528 elsif Nkind_In (Decl, N_Package_Body_Stub, N_Subprogram_Body_Stub)
12529 and then Present (Corresponding_Spec_Of_Stub (Decl))
12530 then
12531 Spec_Decl :=
12532 Unit_Declaration_Node (Corresponding_Spec_Of_Stub (Decl));
12533
12534 -- All other cases
12535
12536 else
12537 Spec_Decl := Decl;
12538 end if;
12539
12540 -- Rather than inspecting the defining entity of the spec declaration,
12541 -- look at its Nkind. This takes care of the case where the analysis of
12542 -- a generic body modifies the Ekind of its spec to allow for recursive
12543 -- calls.
12544
12545 return
12546 Nkind_In (Spec_Decl, N_Generic_Package_Declaration,
12547 N_Generic_Subprogram_Declaration);
12548 end Is_Generic_Declaration_Or_Body;
12549
12550 ----------------------------
12551 -- Is_Inherited_Operation --
12552 ----------------------------
12553
12554 function Is_Inherited_Operation (E : Entity_Id) return Boolean is
12555 pragma Assert (Is_Overloadable (E));
12556 Kind : constant Node_Kind := Nkind (Parent (E));
12557 begin
12558 return Kind = N_Full_Type_Declaration
12559 or else Kind = N_Private_Extension_Declaration
12560 or else Kind = N_Subtype_Declaration
12561 or else (Ekind (E) = E_Enumeration_Literal
12562 and then Is_Derived_Type (Etype (E)));
12563 end Is_Inherited_Operation;
12564
12565 -------------------------------------
12566 -- Is_Inherited_Operation_For_Type --
12567 -------------------------------------
12568
12569 function Is_Inherited_Operation_For_Type
12570 (E : Entity_Id;
12571 Typ : Entity_Id) return Boolean
12572 is
12573 begin
12574 -- Check that the operation has been created by the type declaration
12575
12576 return Is_Inherited_Operation (E)
12577 and then Defining_Identifier (Parent (E)) = Typ;
12578 end Is_Inherited_Operation_For_Type;
12579
12580 -----------------
12581 -- Is_Iterator --
12582 -----------------
12583
12584 function Is_Iterator (Typ : Entity_Id) return Boolean is
12585 function Denotes_Iterator (Iter_Typ : Entity_Id) return Boolean;
12586 -- Determine whether type Iter_Typ is a predefined forward or reversible
12587 -- iterator.
12588
12589 ----------------------
12590 -- Denotes_Iterator --
12591 ----------------------
12592
12593 function Denotes_Iterator (Iter_Typ : Entity_Id) return Boolean is
12594 begin
12595 return
12596 Nam_In (Chars (Iter_Typ), Name_Forward_Iterator,
12597 Name_Reversible_Iterator)
12598 and then Is_Predefined_File_Name
12599 (Unit_File_Name (Get_Source_Unit (Iter_Typ)));
12600 end Denotes_Iterator;
12601
12602 -- Local variables
12603
12604 Iface_Elmt : Elmt_Id;
12605 Ifaces : Elist_Id;
12606
12607 -- Start of processing for Is_Iterator
12608
12609 begin
12610 -- The type may be a subtype of a descendant of the proper instance of
12611 -- the predefined interface type, so we must use the root type of the
12612 -- given type. The same is done for Is_Reversible_Iterator.
12613
12614 if Is_Class_Wide_Type (Typ)
12615 and then Denotes_Iterator (Root_Type (Typ))
12616 then
12617 return True;
12618
12619 elsif not Is_Tagged_Type (Typ) or else not Is_Derived_Type (Typ) then
12620 return False;
12621
12622 elsif Present (Find_Value_Of_Aspect (Typ, Aspect_Iterable)) then
12623 return True;
12624
12625 else
12626 Collect_Interfaces (Typ, Ifaces);
12627
12628 Iface_Elmt := First_Elmt (Ifaces);
12629 while Present (Iface_Elmt) loop
12630 if Denotes_Iterator (Node (Iface_Elmt)) then
12631 return True;
12632 end if;
12633
12634 Next_Elmt (Iface_Elmt);
12635 end loop;
12636
12637 return False;
12638 end if;
12639 end Is_Iterator;
12640
12641 ----------------------------
12642 -- Is_Iterator_Over_Array --
12643 ----------------------------
12644
12645 function Is_Iterator_Over_Array (N : Node_Id) return Boolean is
12646 Container : constant Node_Id := Name (N);
12647 Container_Typ : constant Entity_Id := Base_Type (Etype (Container));
12648 begin
12649 return Is_Array_Type (Container_Typ);
12650 end Is_Iterator_Over_Array;
12651
12652 ------------
12653 -- Is_LHS --
12654 ------------
12655
12656 -- We seem to have a lot of overlapping functions that do similar things
12657 -- (testing for left hand sides or lvalues???).
12658
12659 function Is_LHS (N : Node_Id) return Is_LHS_Result is
12660 P : constant Node_Id := Parent (N);
12661
12662 begin
12663 -- Return True if we are the left hand side of an assignment statement
12664
12665 if Nkind (P) = N_Assignment_Statement then
12666 if Name (P) = N then
12667 return Yes;
12668 else
12669 return No;
12670 end if;
12671
12672 -- Case of prefix of indexed or selected component or slice
12673
12674 elsif Nkind_In (P, N_Indexed_Component, N_Selected_Component, N_Slice)
12675 and then N = Prefix (P)
12676 then
12677 -- Here we have the case where the parent P is N.Q or N(Q .. R).
12678 -- If P is an LHS, then N is also effectively an LHS, but there
12679 -- is an important exception. If N is of an access type, then
12680 -- what we really have is N.all.Q (or N.all(Q .. R)). In either
12681 -- case this makes N.all a left hand side but not N itself.
12682
12683 -- If we don't know the type yet, this is the case where we return
12684 -- Unknown, since the answer depends on the type which is unknown.
12685
12686 if No (Etype (N)) then
12687 return Unknown;
12688
12689 -- We have an Etype set, so we can check it
12690
12691 elsif Is_Access_Type (Etype (N)) then
12692 return No;
12693
12694 -- OK, not access type case, so just test whole expression
12695
12696 else
12697 return Is_LHS (P);
12698 end if;
12699
12700 -- All other cases are not left hand sides
12701
12702 else
12703 return No;
12704 end if;
12705 end Is_LHS;
12706
12707 -----------------------------
12708 -- Is_Library_Level_Entity --
12709 -----------------------------
12710
12711 function Is_Library_Level_Entity (E : Entity_Id) return Boolean is
12712 begin
12713 -- The following is a small optimization, and it also properly handles
12714 -- discriminals, which in task bodies might appear in expressions before
12715 -- the corresponding procedure has been created, and which therefore do
12716 -- not have an assigned scope.
12717
12718 if Is_Formal (E) then
12719 return False;
12720 end if;
12721
12722 -- Normal test is simply that the enclosing dynamic scope is Standard
12723
12724 return Enclosing_Dynamic_Scope (E) = Standard_Standard;
12725 end Is_Library_Level_Entity;
12726
12727 --------------------------------
12728 -- Is_Limited_Class_Wide_Type --
12729 --------------------------------
12730
12731 function Is_Limited_Class_Wide_Type (Typ : Entity_Id) return Boolean is
12732 begin
12733 return
12734 Is_Class_Wide_Type (Typ)
12735 and then (Is_Limited_Type (Typ) or else From_Limited_With (Typ));
12736 end Is_Limited_Class_Wide_Type;
12737
12738 ---------------------------------
12739 -- Is_Local_Variable_Reference --
12740 ---------------------------------
12741
12742 function Is_Local_Variable_Reference (Expr : Node_Id) return Boolean is
12743 begin
12744 if not Is_Entity_Name (Expr) then
12745 return False;
12746
12747 else
12748 declare
12749 Ent : constant Entity_Id := Entity (Expr);
12750 Sub : constant Entity_Id := Enclosing_Subprogram (Ent);
12751 begin
12752 if not Ekind_In (Ent, E_Variable, E_In_Out_Parameter) then
12753 return False;
12754 else
12755 return Present (Sub) and then Sub = Current_Subprogram;
12756 end if;
12757 end;
12758 end if;
12759 end Is_Local_Variable_Reference;
12760
12761 -----------------------------------------------
12762 -- Is_Nontrivial_Default_Init_Cond_Procedure --
12763 -----------------------------------------------
12764
12765 function Is_Nontrivial_Default_Init_Cond_Procedure
12766 (Id : Entity_Id) return Boolean
12767 is
12768 Body_Decl : Node_Id;
12769 Stmt : Node_Id;
12770
12771 begin
12772 if Ekind (Id) = E_Procedure
12773 and then Is_Default_Init_Cond_Procedure (Id)
12774 then
12775 Body_Decl :=
12776 Unit_Declaration_Node
12777 (Corresponding_Body (Unit_Declaration_Node (Id)));
12778
12779 -- The body of the Default_Initial_Condition procedure must contain
12780 -- at least one statement, otherwise the generation of the subprogram
12781 -- body failed.
12782
12783 pragma Assert (Present (Handled_Statement_Sequence (Body_Decl)));
12784
12785 -- To qualify as nontrivial, the first statement of the procedure
12786 -- must be a check in the form of an if statement. If the original
12787 -- Default_Initial_Condition expression was folded, then the first
12788 -- statement is not a check.
12789
12790 Stmt := First (Statements (Handled_Statement_Sequence (Body_Decl)));
12791
12792 return
12793 Nkind (Stmt) = N_If_Statement
12794 and then Nkind (Original_Node (Stmt)) = N_Pragma;
12795 end if;
12796
12797 return False;
12798 end Is_Nontrivial_Default_Init_Cond_Procedure;
12799
12800 -------------------------
12801 -- Is_Object_Reference --
12802 -------------------------
12803
12804 function Is_Object_Reference (N : Node_Id) return Boolean is
12805 function Is_Internally_Generated_Renaming (N : Node_Id) return Boolean;
12806 -- Determine whether N is the name of an internally-generated renaming
12807
12808 --------------------------------------
12809 -- Is_Internally_Generated_Renaming --
12810 --------------------------------------
12811
12812 function Is_Internally_Generated_Renaming (N : Node_Id) return Boolean is
12813 P : Node_Id;
12814
12815 begin
12816 P := N;
12817 while Present (P) loop
12818 if Nkind (P) = N_Object_Renaming_Declaration then
12819 return not Comes_From_Source (P);
12820 elsif Is_List_Member (P) then
12821 return False;
12822 end if;
12823
12824 P := Parent (P);
12825 end loop;
12826
12827 return False;
12828 end Is_Internally_Generated_Renaming;
12829
12830 -- Start of processing for Is_Object_Reference
12831
12832 begin
12833 if Is_Entity_Name (N) then
12834 return Present (Entity (N)) and then Is_Object (Entity (N));
12835
12836 else
12837 case Nkind (N) is
12838 when N_Indexed_Component | N_Slice =>
12839 return
12840 Is_Object_Reference (Prefix (N))
12841 or else Is_Access_Type (Etype (Prefix (N)));
12842
12843 -- In Ada 95, a function call is a constant object; a procedure
12844 -- call is not.
12845
12846 when N_Function_Call =>
12847 return Etype (N) /= Standard_Void_Type;
12848
12849 -- Attributes 'Input, 'Loop_Entry, 'Old and 'Result produce
12850 -- objects.
12851
12852 when N_Attribute_Reference =>
12853 return
12854 Nam_In (Attribute_Name (N), Name_Input,
12855 Name_Loop_Entry,
12856 Name_Old,
12857 Name_Result);
12858
12859 when N_Selected_Component =>
12860 return
12861 Is_Object_Reference (Selector_Name (N))
12862 and then
12863 (Is_Object_Reference (Prefix (N))
12864 or else Is_Access_Type (Etype (Prefix (N))));
12865
12866 when N_Explicit_Dereference =>
12867 return True;
12868
12869 -- A view conversion of a tagged object is an object reference
12870
12871 when N_Type_Conversion =>
12872 return Is_Tagged_Type (Etype (Subtype_Mark (N)))
12873 and then Is_Tagged_Type (Etype (Expression (N)))
12874 and then Is_Object_Reference (Expression (N));
12875
12876 -- An unchecked type conversion is considered to be an object if
12877 -- the operand is an object (this construction arises only as a
12878 -- result of expansion activities).
12879
12880 when N_Unchecked_Type_Conversion =>
12881 return True;
12882
12883 -- Allow string literals to act as objects as long as they appear
12884 -- in internally-generated renamings. The expansion of iterators
12885 -- may generate such renamings when the range involves a string
12886 -- literal.
12887
12888 when N_String_Literal =>
12889 return Is_Internally_Generated_Renaming (Parent (N));
12890
12891 -- AI05-0003: In Ada 2012 a qualified expression is a name.
12892 -- This allows disambiguation of function calls and the use
12893 -- of aggregates in more contexts.
12894
12895 when N_Qualified_Expression =>
12896 if Ada_Version < Ada_2012 then
12897 return False;
12898 else
12899 return Is_Object_Reference (Expression (N))
12900 or else Nkind (Expression (N)) = N_Aggregate;
12901 end if;
12902
12903 when others =>
12904 return False;
12905 end case;
12906 end if;
12907 end Is_Object_Reference;
12908
12909 -----------------------------------
12910 -- Is_OK_Variable_For_Out_Formal --
12911 -----------------------------------
12912
12913 function Is_OK_Variable_For_Out_Formal (AV : Node_Id) return Boolean is
12914 begin
12915 Note_Possible_Modification (AV, Sure => True);
12916
12917 -- We must reject parenthesized variable names. Comes_From_Source is
12918 -- checked because there are currently cases where the compiler violates
12919 -- this rule (e.g. passing a task object to its controlled Initialize
12920 -- routine). This should be properly documented in sinfo???
12921
12922 if Paren_Count (AV) > 0 and then Comes_From_Source (AV) then
12923 return False;
12924
12925 -- A variable is always allowed
12926
12927 elsif Is_Variable (AV) then
12928 return True;
12929
12930 -- Generalized indexing operations are rewritten as explicit
12931 -- dereferences, and it is only during resolution that we can
12932 -- check whether the context requires an access_to_variable type.
12933
12934 elsif Nkind (AV) = N_Explicit_Dereference
12935 and then Ada_Version >= Ada_2012
12936 and then Nkind (Original_Node (AV)) = N_Indexed_Component
12937 and then Present (Etype (Original_Node (AV)))
12938 and then Has_Implicit_Dereference (Etype (Original_Node (AV)))
12939 then
12940 return not Is_Access_Constant (Etype (Prefix (AV)));
12941
12942 -- Unchecked conversions are allowed only if they come from the
12943 -- generated code, which sometimes uses unchecked conversions for out
12944 -- parameters in cases where code generation is unaffected. We tell
12945 -- source unchecked conversions by seeing if they are rewrites of
12946 -- an original Unchecked_Conversion function call, or of an explicit
12947 -- conversion of a function call or an aggregate (as may happen in the
12948 -- expansion of a packed array aggregate).
12949
12950 elsif Nkind (AV) = N_Unchecked_Type_Conversion then
12951 if Nkind_In (Original_Node (AV), N_Function_Call, N_Aggregate) then
12952 return False;
12953
12954 elsif Comes_From_Source (AV)
12955 and then Nkind (Original_Node (Expression (AV))) = N_Function_Call
12956 then
12957 return False;
12958
12959 elsif Nkind (Original_Node (AV)) = N_Type_Conversion then
12960 return Is_OK_Variable_For_Out_Formal (Expression (AV));
12961
12962 else
12963 return True;
12964 end if;
12965
12966 -- Normal type conversions are allowed if argument is a variable
12967
12968 elsif Nkind (AV) = N_Type_Conversion then
12969 if Is_Variable (Expression (AV))
12970 and then Paren_Count (Expression (AV)) = 0
12971 then
12972 Note_Possible_Modification (Expression (AV), Sure => True);
12973 return True;
12974
12975 -- We also allow a non-parenthesized expression that raises
12976 -- constraint error if it rewrites what used to be a variable
12977
12978 elsif Raises_Constraint_Error (Expression (AV))
12979 and then Paren_Count (Expression (AV)) = 0
12980 and then Is_Variable (Original_Node (Expression (AV)))
12981 then
12982 return True;
12983
12984 -- Type conversion of something other than a variable
12985
12986 else
12987 return False;
12988 end if;
12989
12990 -- If this node is rewritten, then test the original form, if that is
12991 -- OK, then we consider the rewritten node OK (for example, if the
12992 -- original node is a conversion, then Is_Variable will not be true
12993 -- but we still want to allow the conversion if it converts a variable).
12994
12995 elsif Original_Node (AV) /= AV then
12996
12997 -- In Ada 2012, the explicit dereference may be a rewritten call to a
12998 -- Reference function.
12999
13000 if Ada_Version >= Ada_2012
13001 and then Nkind (Original_Node (AV)) = N_Function_Call
13002 and then
13003 Has_Implicit_Dereference (Etype (Name (Original_Node (AV))))
13004 then
13005
13006 -- Check that this is not a constant reference.
13007
13008 return not Is_Access_Constant (Etype (Prefix (AV)));
13009
13010 elsif Has_Implicit_Dereference (Etype (Original_Node (AV))) then
13011 return
13012 not Is_Access_Constant (Etype
13013 (Get_Reference_Discriminant (Etype (Original_Node (AV)))));
13014
13015 else
13016 return Is_OK_Variable_For_Out_Formal (Original_Node (AV));
13017 end if;
13018
13019 -- All other non-variables are rejected
13020
13021 else
13022 return False;
13023 end if;
13024 end Is_OK_Variable_For_Out_Formal;
13025
13026 ----------------------------
13027 -- Is_OK_Volatile_Context --
13028 ----------------------------
13029
13030 function Is_OK_Volatile_Context
13031 (Context : Node_Id;
13032 Obj_Ref : Node_Id) return Boolean
13033 is
13034 function Is_Protected_Operation_Call (Nod : Node_Id) return Boolean;
13035 -- Determine whether an arbitrary node denotes a call to a protected
13036 -- entry, function or procedure in prefixed form where the prefix is
13037 -- Obj_Ref.
13038
13039 function Within_Check (Nod : Node_Id) return Boolean;
13040 -- Determine whether an arbitrary node appears in a check node
13041
13042 function Within_Subprogram_Call (Nod : Node_Id) return Boolean;
13043 -- Determine whether an arbitrary node appears in a procedure call
13044
13045 function Within_Volatile_Function (Id : Entity_Id) return Boolean;
13046 -- Determine whether an arbitrary entity appears in a volatile function
13047
13048 ---------------------------------
13049 -- Is_Protected_Operation_Call --
13050 ---------------------------------
13051
13052 function Is_Protected_Operation_Call (Nod : Node_Id) return Boolean is
13053 Pref : Node_Id;
13054 Subp : Node_Id;
13055
13056 begin
13057 -- A call to a protected operations retains its selected component
13058 -- form as opposed to other prefixed calls that are transformed in
13059 -- expanded names.
13060
13061 if Nkind (Nod) = N_Selected_Component then
13062 Pref := Prefix (Nod);
13063 Subp := Selector_Name (Nod);
13064
13065 return
13066 Pref = Obj_Ref
13067 and then Present (Etype (Pref))
13068 and then Is_Protected_Type (Etype (Pref))
13069 and then Is_Entity_Name (Subp)
13070 and then Present (Entity (Subp))
13071 and then Ekind_In (Entity (Subp), E_Entry,
13072 E_Entry_Family,
13073 E_Function,
13074 E_Procedure);
13075 else
13076 return False;
13077 end if;
13078 end Is_Protected_Operation_Call;
13079
13080 ------------------
13081 -- Within_Check --
13082 ------------------
13083
13084 function Within_Check (Nod : Node_Id) return Boolean is
13085 Par : Node_Id;
13086
13087 begin
13088 -- Climb the parent chain looking for a check node
13089
13090 Par := Nod;
13091 while Present (Par) loop
13092 if Nkind (Par) in N_Raise_xxx_Error then
13093 return True;
13094
13095 -- Prevent the search from going too far
13096
13097 elsif Is_Body_Or_Package_Declaration (Par) then
13098 exit;
13099 end if;
13100
13101 Par := Parent (Par);
13102 end loop;
13103
13104 return False;
13105 end Within_Check;
13106
13107 ----------------------------
13108 -- Within_Subprogram_Call --
13109 ----------------------------
13110
13111 function Within_Subprogram_Call (Nod : Node_Id) return Boolean is
13112 Par : Node_Id;
13113
13114 begin
13115 -- Climb the parent chain looking for a function or procedure call
13116
13117 Par := Nod;
13118 while Present (Par) loop
13119 if Nkind_In (Par, N_Entry_Call_Statement,
13120 N_Function_Call,
13121 N_Procedure_Call_Statement)
13122 then
13123 return True;
13124
13125 -- Prevent the search from going too far
13126
13127 elsif Is_Body_Or_Package_Declaration (Par) then
13128 exit;
13129 end if;
13130
13131 Par := Parent (Par);
13132 end loop;
13133
13134 return False;
13135 end Within_Subprogram_Call;
13136
13137 ------------------------------
13138 -- Within_Volatile_Function --
13139 ------------------------------
13140
13141 function Within_Volatile_Function (Id : Entity_Id) return Boolean is
13142 Func_Id : Entity_Id;
13143
13144 begin
13145 -- Traverse the scope stack looking for a [generic] function
13146
13147 Func_Id := Id;
13148 while Present (Func_Id) and then Func_Id /= Standard_Standard loop
13149 if Ekind_In (Func_Id, E_Function, E_Generic_Function) then
13150 return Is_Volatile_Function (Func_Id);
13151 end if;
13152
13153 Func_Id := Scope (Func_Id);
13154 end loop;
13155
13156 return False;
13157 end Within_Volatile_Function;
13158
13159 -- Local variables
13160
13161 Obj_Id : Entity_Id;
13162
13163 -- Start of processing for Is_OK_Volatile_Context
13164
13165 begin
13166 -- The volatile object appears on either side of an assignment
13167
13168 if Nkind (Context) = N_Assignment_Statement then
13169 return True;
13170
13171 -- The volatile object is part of the initialization expression of
13172 -- another object.
13173
13174 elsif Nkind (Context) = N_Object_Declaration
13175 and then Present (Expression (Context))
13176 and then Expression (Context) = Obj_Ref
13177 then
13178 Obj_Id := Defining_Entity (Context);
13179
13180 -- The volatile object acts as the initialization expression of an
13181 -- extended return statement. This is valid context as long as the
13182 -- function is volatile.
13183
13184 if Is_Return_Object (Obj_Id) then
13185 return Within_Volatile_Function (Obj_Id);
13186
13187 -- Otherwise this is a normal object initialization
13188
13189 else
13190 return True;
13191 end if;
13192
13193 -- The volatile object acts as the name of a renaming declaration
13194
13195 elsif Nkind (Context) = N_Object_Renaming_Declaration
13196 and then Name (Context) = Obj_Ref
13197 then
13198 return True;
13199
13200 -- The volatile object appears as an actual parameter in a call to an
13201 -- instance of Unchecked_Conversion whose result is renamed.
13202
13203 elsif Nkind (Context) = N_Function_Call
13204 and then Is_Entity_Name (Name (Context))
13205 and then Is_Unchecked_Conversion_Instance (Entity (Name (Context)))
13206 and then Nkind (Parent (Context)) = N_Object_Renaming_Declaration
13207 then
13208 return True;
13209
13210 -- The volatile object is actually the prefix in a protected entry,
13211 -- function, or procedure call.
13212
13213 elsif Is_Protected_Operation_Call (Context) then
13214 return True;
13215
13216 -- The volatile object appears as the expression of a simple return
13217 -- statement that applies to a volatile function.
13218
13219 elsif Nkind (Context) = N_Simple_Return_Statement
13220 and then Expression (Context) = Obj_Ref
13221 then
13222 return
13223 Within_Volatile_Function (Return_Statement_Entity (Context));
13224
13225 -- The volatile object appears as the prefix of a name occurring in a
13226 -- non-interfering context.
13227
13228 elsif Nkind_In (Context, N_Attribute_Reference,
13229 N_Explicit_Dereference,
13230 N_Indexed_Component,
13231 N_Selected_Component,
13232 N_Slice)
13233 and then Prefix (Context) = Obj_Ref
13234 and then Is_OK_Volatile_Context
13235 (Context => Parent (Context),
13236 Obj_Ref => Context)
13237 then
13238 return True;
13239
13240 -- The volatile object appears as the expression of a type conversion
13241 -- occurring in a non-interfering context.
13242
13243 elsif Nkind_In (Context, N_Type_Conversion,
13244 N_Unchecked_Type_Conversion)
13245 and then Expression (Context) = Obj_Ref
13246 and then Is_OK_Volatile_Context
13247 (Context => Parent (Context),
13248 Obj_Ref => Context)
13249 then
13250 return True;
13251
13252 -- Allow references to volatile objects in various checks. This is
13253 -- not a direct SPARK 2014 requirement.
13254
13255 elsif Within_Check (Context) then
13256 return True;
13257
13258 -- Assume that references to effectively volatile objects that appear
13259 -- as actual parameters in a subprogram call are always legal. A full
13260 -- legality check is done when the actuals are resolved.
13261
13262 elsif Within_Subprogram_Call (Context) then
13263 return True;
13264
13265 -- Otherwise the context is not suitable for an effectively volatile
13266 -- object.
13267
13268 else
13269 return False;
13270 end if;
13271 end Is_OK_Volatile_Context;
13272
13273 ------------------------------------
13274 -- Is_Package_Contract_Annotation --
13275 ------------------------------------
13276
13277 function Is_Package_Contract_Annotation (Item : Node_Id) return Boolean is
13278 Nam : Name_Id;
13279
13280 begin
13281 if Nkind (Item) = N_Aspect_Specification then
13282 Nam := Chars (Identifier (Item));
13283
13284 else pragma Assert (Nkind (Item) = N_Pragma);
13285 Nam := Pragma_Name (Item);
13286 end if;
13287
13288 return Nam = Name_Abstract_State
13289 or else Nam = Name_Initial_Condition
13290 or else Nam = Name_Initializes
13291 or else Nam = Name_Refined_State;
13292 end Is_Package_Contract_Annotation;
13293
13294 -----------------------------------
13295 -- Is_Partially_Initialized_Type --
13296 -----------------------------------
13297
13298 function Is_Partially_Initialized_Type
13299 (Typ : Entity_Id;
13300 Include_Implicit : Boolean := True) return Boolean
13301 is
13302 begin
13303 if Is_Scalar_Type (Typ) then
13304 return False;
13305
13306 elsif Is_Access_Type (Typ) then
13307 return Include_Implicit;
13308
13309 elsif Is_Array_Type (Typ) then
13310
13311 -- If component type is partially initialized, so is array type
13312
13313 if Is_Partially_Initialized_Type
13314 (Component_Type (Typ), Include_Implicit)
13315 then
13316 return True;
13317
13318 -- Otherwise we are only partially initialized if we are fully
13319 -- initialized (this is the empty array case, no point in us
13320 -- duplicating that code here).
13321
13322 else
13323 return Is_Fully_Initialized_Type (Typ);
13324 end if;
13325
13326 elsif Is_Record_Type (Typ) then
13327
13328 -- A discriminated type is always partially initialized if in
13329 -- all mode
13330
13331 if Has_Discriminants (Typ) and then Include_Implicit then
13332 return True;
13333
13334 -- A tagged type is always partially initialized
13335
13336 elsif Is_Tagged_Type (Typ) then
13337 return True;
13338
13339 -- Case of non-discriminated record
13340
13341 else
13342 declare
13343 Ent : Entity_Id;
13344
13345 Component_Present : Boolean := False;
13346 -- Set True if at least one component is present. If no
13347 -- components are present, then record type is fully
13348 -- initialized (another odd case, like the null array).
13349
13350 begin
13351 -- Loop through components
13352
13353 Ent := First_Entity (Typ);
13354 while Present (Ent) loop
13355 if Ekind (Ent) = E_Component then
13356 Component_Present := True;
13357
13358 -- If a component has an initialization expression then
13359 -- the enclosing record type is partially initialized
13360
13361 if Present (Parent (Ent))
13362 and then Present (Expression (Parent (Ent)))
13363 then
13364 return True;
13365
13366 -- If a component is of a type which is itself partially
13367 -- initialized, then the enclosing record type is also.
13368
13369 elsif Is_Partially_Initialized_Type
13370 (Etype (Ent), Include_Implicit)
13371 then
13372 return True;
13373 end if;
13374 end if;
13375
13376 Next_Entity (Ent);
13377 end loop;
13378
13379 -- No initialized components found. If we found any components
13380 -- they were all uninitialized so the result is false.
13381
13382 if Component_Present then
13383 return False;
13384
13385 -- But if we found no components, then all the components are
13386 -- initialized so we consider the type to be initialized.
13387
13388 else
13389 return True;
13390 end if;
13391 end;
13392 end if;
13393
13394 -- Concurrent types are always fully initialized
13395
13396 elsif Is_Concurrent_Type (Typ) then
13397 return True;
13398
13399 -- For a private type, go to underlying type. If there is no underlying
13400 -- type then just assume this partially initialized. Not clear if this
13401 -- can happen in a non-error case, but no harm in testing for this.
13402
13403 elsif Is_Private_Type (Typ) then
13404 declare
13405 U : constant Entity_Id := Underlying_Type (Typ);
13406 begin
13407 if No (U) then
13408 return True;
13409 else
13410 return Is_Partially_Initialized_Type (U, Include_Implicit);
13411 end if;
13412 end;
13413
13414 -- For any other type (are there any?) assume partially initialized
13415
13416 else
13417 return True;
13418 end if;
13419 end Is_Partially_Initialized_Type;
13420
13421 ------------------------------------
13422 -- Is_Potentially_Persistent_Type --
13423 ------------------------------------
13424
13425 function Is_Potentially_Persistent_Type (T : Entity_Id) return Boolean is
13426 Comp : Entity_Id;
13427 Indx : Node_Id;
13428
13429 begin
13430 -- For private type, test corresponding full type
13431
13432 if Is_Private_Type (T) then
13433 return Is_Potentially_Persistent_Type (Full_View (T));
13434
13435 -- Scalar types are potentially persistent
13436
13437 elsif Is_Scalar_Type (T) then
13438 return True;
13439
13440 -- Record type is potentially persistent if not tagged and the types of
13441 -- all it components are potentially persistent, and no component has
13442 -- an initialization expression.
13443
13444 elsif Is_Record_Type (T)
13445 and then not Is_Tagged_Type (T)
13446 and then not Is_Partially_Initialized_Type (T)
13447 then
13448 Comp := First_Component (T);
13449 while Present (Comp) loop
13450 if not Is_Potentially_Persistent_Type (Etype (Comp)) then
13451 return False;
13452 else
13453 Next_Entity (Comp);
13454 end if;
13455 end loop;
13456
13457 return True;
13458
13459 -- Array type is potentially persistent if its component type is
13460 -- potentially persistent and if all its constraints are static.
13461
13462 elsif Is_Array_Type (T) then
13463 if not Is_Potentially_Persistent_Type (Component_Type (T)) then
13464 return False;
13465 end if;
13466
13467 Indx := First_Index (T);
13468 while Present (Indx) loop
13469 if not Is_OK_Static_Subtype (Etype (Indx)) then
13470 return False;
13471 else
13472 Next_Index (Indx);
13473 end if;
13474 end loop;
13475
13476 return True;
13477
13478 -- All other types are not potentially persistent
13479
13480 else
13481 return False;
13482 end if;
13483 end Is_Potentially_Persistent_Type;
13484
13485 --------------------------------
13486 -- Is_Potentially_Unevaluated --
13487 --------------------------------
13488
13489 function Is_Potentially_Unevaluated (N : Node_Id) return Boolean is
13490 Par : Node_Id;
13491 Expr : Node_Id;
13492
13493 begin
13494 Expr := N;
13495 Par := Parent (N);
13496
13497 -- A postcondition whose expression is a short-circuit is broken down
13498 -- into individual aspects for better exception reporting. The original
13499 -- short-circuit expression is rewritten as the second operand, and an
13500 -- occurrence of 'Old in that operand is potentially unevaluated.
13501 -- See Sem_ch13.adb for details of this transformation.
13502
13503 if Nkind (Original_Node (Par)) = N_And_Then then
13504 return True;
13505 end if;
13506
13507 while not Nkind_In (Par, N_If_Expression,
13508 N_Case_Expression,
13509 N_And_Then,
13510 N_Or_Else,
13511 N_In,
13512 N_Not_In)
13513 loop
13514 Expr := Par;
13515 Par := Parent (Par);
13516
13517 -- If the context is not an expression, or if is the result of
13518 -- expansion of an enclosing construct (such as another attribute)
13519 -- the predicate does not apply.
13520
13521 if Nkind (Par) not in N_Subexpr
13522 or else not Comes_From_Source (Par)
13523 then
13524 return False;
13525 end if;
13526 end loop;
13527
13528 if Nkind (Par) = N_If_Expression then
13529 return Is_Elsif (Par) or else Expr /= First (Expressions (Par));
13530
13531 elsif Nkind (Par) = N_Case_Expression then
13532 return Expr /= Expression (Par);
13533
13534 elsif Nkind_In (Par, N_And_Then, N_Or_Else) then
13535 return Expr = Right_Opnd (Par);
13536
13537 elsif Nkind_In (Par, N_In, N_Not_In) then
13538 return Expr /= Left_Opnd (Par);
13539
13540 else
13541 return False;
13542 end if;
13543 end Is_Potentially_Unevaluated;
13544
13545 ---------------------------------
13546 -- Is_Protected_Self_Reference --
13547 ---------------------------------
13548
13549 function Is_Protected_Self_Reference (N : Node_Id) return Boolean is
13550
13551 function In_Access_Definition (N : Node_Id) return Boolean;
13552 -- Returns true if N belongs to an access definition
13553
13554 --------------------------
13555 -- In_Access_Definition --
13556 --------------------------
13557
13558 function In_Access_Definition (N : Node_Id) return Boolean is
13559 P : Node_Id;
13560
13561 begin
13562 P := Parent (N);
13563 while Present (P) loop
13564 if Nkind (P) = N_Access_Definition then
13565 return True;
13566 end if;
13567
13568 P := Parent (P);
13569 end loop;
13570
13571 return False;
13572 end In_Access_Definition;
13573
13574 -- Start of processing for Is_Protected_Self_Reference
13575
13576 begin
13577 -- Verify that prefix is analyzed and has the proper form. Note that
13578 -- the attributes Elab_Spec, Elab_Body and Elab_Subp_Body which also
13579 -- produce the address of an entity, do not analyze their prefix
13580 -- because they denote entities that are not necessarily visible.
13581 -- Neither of them can apply to a protected type.
13582
13583 return Ada_Version >= Ada_2005
13584 and then Is_Entity_Name (N)
13585 and then Present (Entity (N))
13586 and then Is_Protected_Type (Entity (N))
13587 and then In_Open_Scopes (Entity (N))
13588 and then not In_Access_Definition (N);
13589 end Is_Protected_Self_Reference;
13590
13591 -----------------------------
13592 -- Is_RCI_Pkg_Spec_Or_Body --
13593 -----------------------------
13594
13595 function Is_RCI_Pkg_Spec_Or_Body (Cunit : Node_Id) return Boolean is
13596
13597 function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean;
13598 -- Return True if the unit of Cunit is an RCI package declaration
13599
13600 ---------------------------
13601 -- Is_RCI_Pkg_Decl_Cunit --
13602 ---------------------------
13603
13604 function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean is
13605 The_Unit : constant Node_Id := Unit (Cunit);
13606
13607 begin
13608 if Nkind (The_Unit) /= N_Package_Declaration then
13609 return False;
13610 end if;
13611
13612 return Is_Remote_Call_Interface (Defining_Entity (The_Unit));
13613 end Is_RCI_Pkg_Decl_Cunit;
13614
13615 -- Start of processing for Is_RCI_Pkg_Spec_Or_Body
13616
13617 begin
13618 return Is_RCI_Pkg_Decl_Cunit (Cunit)
13619 or else
13620 (Nkind (Unit (Cunit)) = N_Package_Body
13621 and then Is_RCI_Pkg_Decl_Cunit (Library_Unit (Cunit)));
13622 end Is_RCI_Pkg_Spec_Or_Body;
13623
13624 -----------------------------------------
13625 -- Is_Remote_Access_To_Class_Wide_Type --
13626 -----------------------------------------
13627
13628 function Is_Remote_Access_To_Class_Wide_Type
13629 (E : Entity_Id) return Boolean
13630 is
13631 begin
13632 -- A remote access to class-wide type is a general access to object type
13633 -- declared in the visible part of a Remote_Types or Remote_Call_
13634 -- Interface unit.
13635
13636 return Ekind (E) = E_General_Access_Type
13637 and then (Is_Remote_Call_Interface (E) or else Is_Remote_Types (E));
13638 end Is_Remote_Access_To_Class_Wide_Type;
13639
13640 -----------------------------------------
13641 -- Is_Remote_Access_To_Subprogram_Type --
13642 -----------------------------------------
13643
13644 function Is_Remote_Access_To_Subprogram_Type
13645 (E : Entity_Id) return Boolean
13646 is
13647 begin
13648 return (Ekind (E) = E_Access_Subprogram_Type
13649 or else (Ekind (E) = E_Record_Type
13650 and then Present (Corresponding_Remote_Type (E))))
13651 and then (Is_Remote_Call_Interface (E) or else Is_Remote_Types (E));
13652 end Is_Remote_Access_To_Subprogram_Type;
13653
13654 --------------------
13655 -- Is_Remote_Call --
13656 --------------------
13657
13658 function Is_Remote_Call (N : Node_Id) return Boolean is
13659 begin
13660 if Nkind (N) not in N_Subprogram_Call then
13661
13662 -- An entry call cannot be remote
13663
13664 return False;
13665
13666 elsif Nkind (Name (N)) in N_Has_Entity
13667 and then Is_Remote_Call_Interface (Entity (Name (N)))
13668 then
13669 -- A subprogram declared in the spec of a RCI package is remote
13670
13671 return True;
13672
13673 elsif Nkind (Name (N)) = N_Explicit_Dereference
13674 and then Is_Remote_Access_To_Subprogram_Type
13675 (Etype (Prefix (Name (N))))
13676 then
13677 -- The dereference of a RAS is a remote call
13678
13679 return True;
13680
13681 elsif Present (Controlling_Argument (N))
13682 and then Is_Remote_Access_To_Class_Wide_Type
13683 (Etype (Controlling_Argument (N)))
13684 then
13685 -- Any primitive operation call with a controlling argument of
13686 -- a RACW type is a remote call.
13687
13688 return True;
13689 end if;
13690
13691 -- All other calls are local calls
13692
13693 return False;
13694 end Is_Remote_Call;
13695
13696 ----------------------
13697 -- Is_Renamed_Entry --
13698 ----------------------
13699
13700 function Is_Renamed_Entry (Proc_Nam : Entity_Id) return Boolean is
13701 Orig_Node : Node_Id := Empty;
13702 Subp_Decl : Node_Id := Parent (Parent (Proc_Nam));
13703
13704 function Is_Entry (Nam : Node_Id) return Boolean;
13705 -- Determine whether Nam is an entry. Traverse selectors if there are
13706 -- nested selected components.
13707
13708 --------------
13709 -- Is_Entry --
13710 --------------
13711
13712 function Is_Entry (Nam : Node_Id) return Boolean is
13713 begin
13714 if Nkind (Nam) = N_Selected_Component then
13715 return Is_Entry (Selector_Name (Nam));
13716 end if;
13717
13718 return Ekind (Entity (Nam)) = E_Entry;
13719 end Is_Entry;
13720
13721 -- Start of processing for Is_Renamed_Entry
13722
13723 begin
13724 if Present (Alias (Proc_Nam)) then
13725 Subp_Decl := Parent (Parent (Alias (Proc_Nam)));
13726 end if;
13727
13728 -- Look for a rewritten subprogram renaming declaration
13729
13730 if Nkind (Subp_Decl) = N_Subprogram_Declaration
13731 and then Present (Original_Node (Subp_Decl))
13732 then
13733 Orig_Node := Original_Node (Subp_Decl);
13734 end if;
13735
13736 -- The rewritten subprogram is actually an entry
13737
13738 if Present (Orig_Node)
13739 and then Nkind (Orig_Node) = N_Subprogram_Renaming_Declaration
13740 and then Is_Entry (Name (Orig_Node))
13741 then
13742 return True;
13743 end if;
13744
13745 return False;
13746 end Is_Renamed_Entry;
13747
13748 -----------------------------
13749 -- Is_Renaming_Declaration --
13750 -----------------------------
13751
13752 function Is_Renaming_Declaration (N : Node_Id) return Boolean is
13753 begin
13754 case Nkind (N) is
13755 when N_Exception_Renaming_Declaration |
13756 N_Generic_Function_Renaming_Declaration |
13757 N_Generic_Package_Renaming_Declaration |
13758 N_Generic_Procedure_Renaming_Declaration |
13759 N_Object_Renaming_Declaration |
13760 N_Package_Renaming_Declaration |
13761 N_Subprogram_Renaming_Declaration =>
13762 return True;
13763
13764 when others =>
13765 return False;
13766 end case;
13767 end Is_Renaming_Declaration;
13768
13769 ----------------------------
13770 -- Is_Reversible_Iterator --
13771 ----------------------------
13772
13773 function Is_Reversible_Iterator (Typ : Entity_Id) return Boolean is
13774 Ifaces_List : Elist_Id;
13775 Iface_Elmt : Elmt_Id;
13776 Iface : Entity_Id;
13777
13778 begin
13779 if Is_Class_Wide_Type (Typ)
13780 and then Chars (Root_Type (Typ)) = Name_Reversible_Iterator
13781 and then Is_Predefined_File_Name
13782 (Unit_File_Name (Get_Source_Unit (Root_Type (Typ))))
13783 then
13784 return True;
13785
13786 elsif not Is_Tagged_Type (Typ) or else not Is_Derived_Type (Typ) then
13787 return False;
13788
13789 else
13790 Collect_Interfaces (Typ, Ifaces_List);
13791
13792 Iface_Elmt := First_Elmt (Ifaces_List);
13793 while Present (Iface_Elmt) loop
13794 Iface := Node (Iface_Elmt);
13795 if Chars (Iface) = Name_Reversible_Iterator
13796 and then
13797 Is_Predefined_File_Name
13798 (Unit_File_Name (Get_Source_Unit (Iface)))
13799 then
13800 return True;
13801 end if;
13802
13803 Next_Elmt (Iface_Elmt);
13804 end loop;
13805 end if;
13806
13807 return False;
13808 end Is_Reversible_Iterator;
13809
13810 ----------------------
13811 -- Is_Selector_Name --
13812 ----------------------
13813
13814 function Is_Selector_Name (N : Node_Id) return Boolean is
13815 begin
13816 if not Is_List_Member (N) then
13817 declare
13818 P : constant Node_Id := Parent (N);
13819 begin
13820 return Nkind_In (P, N_Expanded_Name,
13821 N_Generic_Association,
13822 N_Parameter_Association,
13823 N_Selected_Component)
13824 and then Selector_Name (P) = N;
13825 end;
13826
13827 else
13828 declare
13829 L : constant List_Id := List_Containing (N);
13830 P : constant Node_Id := Parent (L);
13831 begin
13832 return (Nkind (P) = N_Discriminant_Association
13833 and then Selector_Names (P) = L)
13834 or else
13835 (Nkind (P) = N_Component_Association
13836 and then Choices (P) = L);
13837 end;
13838 end if;
13839 end Is_Selector_Name;
13840
13841 ---------------------------------
13842 -- Is_Single_Concurrent_Object --
13843 ---------------------------------
13844
13845 function Is_Single_Concurrent_Object (Id : Entity_Id) return Boolean is
13846 begin
13847 return
13848 Is_Single_Protected_Object (Id) or else Is_Single_Task_Object (Id);
13849 end Is_Single_Concurrent_Object;
13850
13851 -------------------------------
13852 -- Is_Single_Concurrent_Type --
13853 -------------------------------
13854
13855 function Is_Single_Concurrent_Type (Id : Entity_Id) return Boolean is
13856 begin
13857 return
13858 Ekind_In (Id, E_Protected_Type, E_Task_Type)
13859 and then Is_Single_Concurrent_Type_Declaration
13860 (Declaration_Node (Id));
13861 end Is_Single_Concurrent_Type;
13862
13863 -------------------------------------------
13864 -- Is_Single_Concurrent_Type_Declaration --
13865 -------------------------------------------
13866
13867 function Is_Single_Concurrent_Type_Declaration
13868 (N : Node_Id) return Boolean
13869 is
13870 begin
13871 return Nkind_In (Original_Node (N), N_Single_Protected_Declaration,
13872 N_Single_Task_Declaration);
13873 end Is_Single_Concurrent_Type_Declaration;
13874
13875 ---------------------------------------------
13876 -- Is_Single_Precision_Floating_Point_Type --
13877 ---------------------------------------------
13878
13879 function Is_Single_Precision_Floating_Point_Type
13880 (E : Entity_Id) return Boolean is
13881 begin
13882 return Is_Floating_Point_Type (E)
13883 and then Machine_Radix_Value (E) = Uint_2
13884 and then Machine_Mantissa_Value (E) = Uint_24
13885 and then Machine_Emax_Value (E) = Uint_2 ** Uint_7
13886 and then Machine_Emin_Value (E) = Uint_3 - (Uint_2 ** Uint_7);
13887 end Is_Single_Precision_Floating_Point_Type;
13888
13889 --------------------------------
13890 -- Is_Single_Protected_Object --
13891 --------------------------------
13892
13893 function Is_Single_Protected_Object (Id : Entity_Id) return Boolean is
13894 begin
13895 return
13896 Ekind (Id) = E_Variable
13897 and then Ekind (Etype (Id)) = E_Protected_Type
13898 and then Is_Single_Concurrent_Type (Etype (Id));
13899 end Is_Single_Protected_Object;
13900
13901 ---------------------------
13902 -- Is_Single_Task_Object --
13903 ---------------------------
13904
13905 function Is_Single_Task_Object (Id : Entity_Id) return Boolean is
13906 begin
13907 return
13908 Ekind (Id) = E_Variable
13909 and then Ekind (Etype (Id)) = E_Task_Type
13910 and then Is_Single_Concurrent_Type (Etype (Id));
13911 end Is_Single_Task_Object;
13912
13913 -------------------------------------
13914 -- Is_SPARK_05_Initialization_Expr --
13915 -------------------------------------
13916
13917 function Is_SPARK_05_Initialization_Expr (N : Node_Id) return Boolean is
13918 Is_Ok : Boolean;
13919 Expr : Node_Id;
13920 Comp_Assn : Node_Id;
13921 Orig_N : constant Node_Id := Original_Node (N);
13922
13923 begin
13924 Is_Ok := True;
13925
13926 if not Comes_From_Source (Orig_N) then
13927 goto Done;
13928 end if;
13929
13930 pragma Assert (Nkind (Orig_N) in N_Subexpr);
13931
13932 case Nkind (Orig_N) is
13933 when N_Character_Literal |
13934 N_Integer_Literal |
13935 N_Real_Literal |
13936 N_String_Literal =>
13937 null;
13938
13939 when N_Identifier |
13940 N_Expanded_Name =>
13941 if Is_Entity_Name (Orig_N)
13942 and then Present (Entity (Orig_N)) -- needed in some cases
13943 then
13944 case Ekind (Entity (Orig_N)) is
13945 when E_Constant |
13946 E_Enumeration_Literal |
13947 E_Named_Integer |
13948 E_Named_Real =>
13949 null;
13950 when others =>
13951 if Is_Type (Entity (Orig_N)) then
13952 null;
13953 else
13954 Is_Ok := False;
13955 end if;
13956 end case;
13957 end if;
13958
13959 when N_Qualified_Expression |
13960 N_Type_Conversion =>
13961 Is_Ok := Is_SPARK_05_Initialization_Expr (Expression (Orig_N));
13962
13963 when N_Unary_Op =>
13964 Is_Ok := Is_SPARK_05_Initialization_Expr (Right_Opnd (Orig_N));
13965
13966 when N_Binary_Op |
13967 N_Short_Circuit |
13968 N_Membership_Test =>
13969 Is_Ok := Is_SPARK_05_Initialization_Expr (Left_Opnd (Orig_N))
13970 and then
13971 Is_SPARK_05_Initialization_Expr (Right_Opnd (Orig_N));
13972
13973 when N_Aggregate |
13974 N_Extension_Aggregate =>
13975 if Nkind (Orig_N) = N_Extension_Aggregate then
13976 Is_Ok :=
13977 Is_SPARK_05_Initialization_Expr (Ancestor_Part (Orig_N));
13978 end if;
13979
13980 Expr := First (Expressions (Orig_N));
13981 while Present (Expr) loop
13982 if not Is_SPARK_05_Initialization_Expr (Expr) then
13983 Is_Ok := False;
13984 goto Done;
13985 end if;
13986
13987 Next (Expr);
13988 end loop;
13989
13990 Comp_Assn := First (Component_Associations (Orig_N));
13991 while Present (Comp_Assn) loop
13992 Expr := Expression (Comp_Assn);
13993
13994 -- Note: test for Present here needed for box assocation
13995
13996 if Present (Expr)
13997 and then not Is_SPARK_05_Initialization_Expr (Expr)
13998 then
13999 Is_Ok := False;
14000 goto Done;
14001 end if;
14002
14003 Next (Comp_Assn);
14004 end loop;
14005
14006 when N_Attribute_Reference =>
14007 if Nkind (Prefix (Orig_N)) in N_Subexpr then
14008 Is_Ok := Is_SPARK_05_Initialization_Expr (Prefix (Orig_N));
14009 end if;
14010
14011 Expr := First (Expressions (Orig_N));
14012 while Present (Expr) loop
14013 if not Is_SPARK_05_Initialization_Expr (Expr) then
14014 Is_Ok := False;
14015 goto Done;
14016 end if;
14017
14018 Next (Expr);
14019 end loop;
14020
14021 -- Selected components might be expanded named not yet resolved, so
14022 -- default on the safe side. (Eg on sparklex.ads)
14023
14024 when N_Selected_Component =>
14025 null;
14026
14027 when others =>
14028 Is_Ok := False;
14029 end case;
14030
14031 <<Done>>
14032 return Is_Ok;
14033 end Is_SPARK_05_Initialization_Expr;
14034
14035 ----------------------------------
14036 -- Is_SPARK_05_Object_Reference --
14037 ----------------------------------
14038
14039 function Is_SPARK_05_Object_Reference (N : Node_Id) return Boolean is
14040 begin
14041 if Is_Entity_Name (N) then
14042 return Present (Entity (N))
14043 and then
14044 (Ekind_In (Entity (N), E_Constant, E_Variable)
14045 or else Ekind (Entity (N)) in Formal_Kind);
14046
14047 else
14048 case Nkind (N) is
14049 when N_Selected_Component =>
14050 return Is_SPARK_05_Object_Reference (Prefix (N));
14051
14052 when others =>
14053 return False;
14054 end case;
14055 end if;
14056 end Is_SPARK_05_Object_Reference;
14057
14058 -----------------------------
14059 -- Is_Specific_Tagged_Type --
14060 -----------------------------
14061
14062 function Is_Specific_Tagged_Type (Typ : Entity_Id) return Boolean is
14063 Full_Typ : Entity_Id;
14064
14065 begin
14066 -- Handle private types
14067
14068 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
14069 Full_Typ := Full_View (Typ);
14070 else
14071 Full_Typ := Typ;
14072 end if;
14073
14074 -- A specific tagged type is a non-class-wide tagged type
14075
14076 return Is_Tagged_Type (Full_Typ) and not Is_Class_Wide_Type (Full_Typ);
14077 end Is_Specific_Tagged_Type;
14078
14079 ------------------
14080 -- Is_Statement --
14081 ------------------
14082
14083 function Is_Statement (N : Node_Id) return Boolean is
14084 begin
14085 return
14086 Nkind (N) in N_Statement_Other_Than_Procedure_Call
14087 or else Nkind (N) = N_Procedure_Call_Statement;
14088 end Is_Statement;
14089
14090 ---------------------------------------
14091 -- Is_Subprogram_Contract_Annotation --
14092 ---------------------------------------
14093
14094 function Is_Subprogram_Contract_Annotation
14095 (Item : Node_Id) return Boolean
14096 is
14097 Nam : Name_Id;
14098
14099 begin
14100 if Nkind (Item) = N_Aspect_Specification then
14101 Nam := Chars (Identifier (Item));
14102
14103 else pragma Assert (Nkind (Item) = N_Pragma);
14104 Nam := Pragma_Name (Item);
14105 end if;
14106
14107 return Nam = Name_Contract_Cases
14108 or else Nam = Name_Depends
14109 or else Nam = Name_Extensions_Visible
14110 or else Nam = Name_Global
14111 or else Nam = Name_Post
14112 or else Nam = Name_Post_Class
14113 or else Nam = Name_Postcondition
14114 or else Nam = Name_Pre
14115 or else Nam = Name_Pre_Class
14116 or else Nam = Name_Precondition
14117 or else Nam = Name_Refined_Depends
14118 or else Nam = Name_Refined_Global
14119 or else Nam = Name_Refined_Post
14120 or else Nam = Name_Test_Case;
14121 end Is_Subprogram_Contract_Annotation;
14122
14123 --------------------------------------------------
14124 -- Is_Subprogram_Stub_Without_Prior_Declaration --
14125 --------------------------------------------------
14126
14127 function Is_Subprogram_Stub_Without_Prior_Declaration
14128 (N : Node_Id) return Boolean
14129 is
14130 begin
14131 -- A subprogram stub without prior declaration serves as declaration for
14132 -- the actual subprogram body. As such, it has an attached defining
14133 -- entity of E_[Generic_]Function or E_[Generic_]Procedure.
14134
14135 return Nkind (N) = N_Subprogram_Body_Stub
14136 and then Ekind (Defining_Entity (N)) /= E_Subprogram_Body;
14137 end Is_Subprogram_Stub_Without_Prior_Declaration;
14138
14139 --------------------------
14140 -- Is_Suspension_Object --
14141 --------------------------
14142
14143 function Is_Suspension_Object (Id : Entity_Id) return Boolean is
14144 begin
14145 -- This approach does an exact name match rather than to rely on
14146 -- RTSfind. Routine Is_Effectively_Volatile is used by clients of the
14147 -- front end at point where all auxiliary tables are locked and any
14148 -- modifications to them are treated as violations. Do not tamper with
14149 -- the tables, instead examine the Chars fields of all the scopes of Id.
14150
14151 return
14152 Chars (Id) = Name_Suspension_Object
14153 and then Present (Scope (Id))
14154 and then Chars (Scope (Id)) = Name_Synchronous_Task_Control
14155 and then Present (Scope (Scope (Id)))
14156 and then Chars (Scope (Scope (Id))) = Name_Ada
14157 and then Present (Scope (Scope (Scope (Id))))
14158 and then Scope (Scope (Scope (Id))) = Standard_Standard;
14159 end Is_Suspension_Object;
14160
14161 ----------------------------
14162 -- Is_Synchronized_Object --
14163 ----------------------------
14164
14165 function Is_Synchronized_Object (Id : Entity_Id) return Boolean is
14166 Prag : Node_Id;
14167
14168 begin
14169 if Is_Object (Id) then
14170
14171 -- The object is synchronized if it is of a type that yields a
14172 -- synchronized object.
14173
14174 if Yields_Synchronized_Object (Etype (Id)) then
14175 return True;
14176
14177 -- The object is synchronized if it is atomic and Async_Writers is
14178 -- enabled.
14179
14180 elsif Is_Atomic (Id) and then Async_Writers_Enabled (Id) then
14181 return True;
14182
14183 -- A constant is a synchronized object by default
14184
14185 elsif Ekind (Id) = E_Constant then
14186 return True;
14187
14188 -- A variable is a synchronized object if it is subject to pragma
14189 -- Constant_After_Elaboration.
14190
14191 elsif Ekind (Id) = E_Variable then
14192 Prag := Get_Pragma (Id, Pragma_Constant_After_Elaboration);
14193
14194 return Present (Prag) and then Is_Enabled_Pragma (Prag);
14195 end if;
14196 end if;
14197
14198 -- Otherwise the input is not an object or it does not qualify as a
14199 -- synchronized object.
14200
14201 return False;
14202 end Is_Synchronized_Object;
14203
14204 ---------------------------------
14205 -- Is_Synchronized_Tagged_Type --
14206 ---------------------------------
14207
14208 function Is_Synchronized_Tagged_Type (E : Entity_Id) return Boolean is
14209 Kind : constant Entity_Kind := Ekind (Base_Type (E));
14210
14211 begin
14212 -- A task or protected type derived from an interface is a tagged type.
14213 -- Such a tagged type is called a synchronized tagged type, as are
14214 -- synchronized interfaces and private extensions whose declaration
14215 -- includes the reserved word synchronized.
14216
14217 return (Is_Tagged_Type (E)
14218 and then (Kind = E_Task_Type
14219 or else
14220 Kind = E_Protected_Type))
14221 or else
14222 (Is_Interface (E)
14223 and then Is_Synchronized_Interface (E))
14224 or else
14225 (Ekind (E) = E_Record_Type_With_Private
14226 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
14227 and then (Synchronized_Present (Parent (E))
14228 or else Is_Synchronized_Interface (Etype (E))));
14229 end Is_Synchronized_Tagged_Type;
14230
14231 -----------------
14232 -- Is_Transfer --
14233 -----------------
14234
14235 function Is_Transfer (N : Node_Id) return Boolean is
14236 Kind : constant Node_Kind := Nkind (N);
14237
14238 begin
14239 if Kind = N_Simple_Return_Statement
14240 or else
14241 Kind = N_Extended_Return_Statement
14242 or else
14243 Kind = N_Goto_Statement
14244 or else
14245 Kind = N_Raise_Statement
14246 or else
14247 Kind = N_Requeue_Statement
14248 then
14249 return True;
14250
14251 elsif (Kind = N_Exit_Statement or else Kind in N_Raise_xxx_Error)
14252 and then No (Condition (N))
14253 then
14254 return True;
14255
14256 elsif Kind = N_Procedure_Call_Statement
14257 and then Is_Entity_Name (Name (N))
14258 and then Present (Entity (Name (N)))
14259 and then No_Return (Entity (Name (N)))
14260 then
14261 return True;
14262
14263 elsif Nkind (Original_Node (N)) = N_Raise_Statement then
14264 return True;
14265
14266 else
14267 return False;
14268 end if;
14269 end Is_Transfer;
14270
14271 -------------
14272 -- Is_True --
14273 -------------
14274
14275 function Is_True (U : Uint) return Boolean is
14276 begin
14277 return (U /= 0);
14278 end Is_True;
14279
14280 --------------------------------------
14281 -- Is_Unchecked_Conversion_Instance --
14282 --------------------------------------
14283
14284 function Is_Unchecked_Conversion_Instance (Id : Entity_Id) return Boolean is
14285 Par : Node_Id;
14286 Gen_Par : Entity_Id;
14287
14288 begin
14289 -- Look for a function whose generic parent is the predefined intrinsic
14290 -- function Unchecked_Conversion.
14291
14292 if Ekind (Id) = E_Function then
14293 Par := Parent (Id);
14294
14295 if Nkind (Par) /= N_Function_Specification then
14296 return False;
14297 end if;
14298
14299 Gen_Par := Generic_Parent (Par);
14300
14301 return
14302 Present (Gen_Par)
14303 and then Chars (Gen_Par) = Name_Unchecked_Conversion
14304 and then Is_Intrinsic_Subprogram (Gen_Par)
14305 and then Is_Predefined_File_Name
14306 (Unit_File_Name (Get_Source_Unit (Gen_Par)));
14307 end if;
14308
14309 return False;
14310 end Is_Unchecked_Conversion_Instance;
14311
14312 -------------------------------
14313 -- Is_Universal_Numeric_Type --
14314 -------------------------------
14315
14316 function Is_Universal_Numeric_Type (T : Entity_Id) return Boolean is
14317 begin
14318 return T = Universal_Integer or else T = Universal_Real;
14319 end Is_Universal_Numeric_Type;
14320
14321 ----------------------------
14322 -- Is_Variable_Size_Array --
14323 ----------------------------
14324
14325 function Is_Variable_Size_Array (E : Entity_Id) return Boolean is
14326 Idx : Node_Id;
14327
14328 begin
14329 pragma Assert (Is_Array_Type (E));
14330
14331 -- Check if some index is initialized with a non-constant value
14332
14333 Idx := First_Index (E);
14334 while Present (Idx) loop
14335 if Nkind (Idx) = N_Range then
14336 if not Is_Constant_Bound (Low_Bound (Idx))
14337 or else not Is_Constant_Bound (High_Bound (Idx))
14338 then
14339 return True;
14340 end if;
14341 end if;
14342
14343 Idx := Next_Index (Idx);
14344 end loop;
14345
14346 return False;
14347 end Is_Variable_Size_Array;
14348
14349 -----------------------------
14350 -- Is_Variable_Size_Record --
14351 -----------------------------
14352
14353 function Is_Variable_Size_Record (E : Entity_Id) return Boolean is
14354 Comp : Entity_Id;
14355 Comp_Typ : Entity_Id;
14356
14357 begin
14358 pragma Assert (Is_Record_Type (E));
14359
14360 Comp := First_Entity (E);
14361 while Present (Comp) loop
14362 Comp_Typ := Etype (Comp);
14363
14364 -- Recursive call if the record type has discriminants
14365
14366 if Is_Record_Type (Comp_Typ)
14367 and then Has_Discriminants (Comp_Typ)
14368 and then Is_Variable_Size_Record (Comp_Typ)
14369 then
14370 return True;
14371
14372 elsif Is_Array_Type (Comp_Typ)
14373 and then Is_Variable_Size_Array (Comp_Typ)
14374 then
14375 return True;
14376 end if;
14377
14378 Next_Entity (Comp);
14379 end loop;
14380
14381 return False;
14382 end Is_Variable_Size_Record;
14383
14384 -----------------
14385 -- Is_Variable --
14386 -----------------
14387
14388 function Is_Variable
14389 (N : Node_Id;
14390 Use_Original_Node : Boolean := True) return Boolean
14391 is
14392 Orig_Node : Node_Id;
14393
14394 function In_Protected_Function (E : Entity_Id) return Boolean;
14395 -- Within a protected function, the private components of the enclosing
14396 -- protected type are constants. A function nested within a (protected)
14397 -- procedure is not itself protected. Within the body of a protected
14398 -- function the current instance of the protected type is a constant.
14399
14400 function Is_Variable_Prefix (P : Node_Id) return Boolean;
14401 -- Prefixes can involve implicit dereferences, in which case we must
14402 -- test for the case of a reference of a constant access type, which can
14403 -- can never be a variable.
14404
14405 ---------------------------
14406 -- In_Protected_Function --
14407 ---------------------------
14408
14409 function In_Protected_Function (E : Entity_Id) return Boolean is
14410 Prot : Entity_Id;
14411 S : Entity_Id;
14412
14413 begin
14414 -- E is the current instance of a type
14415
14416 if Is_Type (E) then
14417 Prot := E;
14418
14419 -- E is an object
14420
14421 else
14422 Prot := Scope (E);
14423 end if;
14424
14425 if not Is_Protected_Type (Prot) then
14426 return False;
14427
14428 else
14429 S := Current_Scope;
14430 while Present (S) and then S /= Prot loop
14431 if Ekind (S) = E_Function and then Scope (S) = Prot then
14432 return True;
14433 end if;
14434
14435 S := Scope (S);
14436 end loop;
14437
14438 return False;
14439 end if;
14440 end In_Protected_Function;
14441
14442 ------------------------
14443 -- Is_Variable_Prefix --
14444 ------------------------
14445
14446 function Is_Variable_Prefix (P : Node_Id) return Boolean is
14447 begin
14448 if Is_Access_Type (Etype (P)) then
14449 return not Is_Access_Constant (Root_Type (Etype (P)));
14450
14451 -- For the case of an indexed component whose prefix has a packed
14452 -- array type, the prefix has been rewritten into a type conversion.
14453 -- Determine variable-ness from the converted expression.
14454
14455 elsif Nkind (P) = N_Type_Conversion
14456 and then not Comes_From_Source (P)
14457 and then Is_Array_Type (Etype (P))
14458 and then Is_Packed (Etype (P))
14459 then
14460 return Is_Variable (Expression (P));
14461
14462 else
14463 return Is_Variable (P);
14464 end if;
14465 end Is_Variable_Prefix;
14466
14467 -- Start of processing for Is_Variable
14468
14469 begin
14470 -- Special check, allow x'Deref(expr) as a variable
14471
14472 if Nkind (N) = N_Attribute_Reference
14473 and then Attribute_Name (N) = Name_Deref
14474 then
14475 return True;
14476 end if;
14477
14478 -- Check if we perform the test on the original node since this may be a
14479 -- test of syntactic categories which must not be disturbed by whatever
14480 -- rewriting might have occurred. For example, an aggregate, which is
14481 -- certainly NOT a variable, could be turned into a variable by
14482 -- expansion.
14483
14484 if Use_Original_Node then
14485 Orig_Node := Original_Node (N);
14486 else
14487 Orig_Node := N;
14488 end if;
14489
14490 -- Definitely OK if Assignment_OK is set. Since this is something that
14491 -- only gets set for expanded nodes, the test is on N, not Orig_Node.
14492
14493 if Nkind (N) in N_Subexpr and then Assignment_OK (N) then
14494 return True;
14495
14496 -- Normally we go to the original node, but there is one exception where
14497 -- we use the rewritten node, namely when it is an explicit dereference.
14498 -- The generated code may rewrite a prefix which is an access type with
14499 -- an explicit dereference. The dereference is a variable, even though
14500 -- the original node may not be (since it could be a constant of the
14501 -- access type).
14502
14503 -- In Ada 2005 we have a further case to consider: the prefix may be a
14504 -- function call given in prefix notation. The original node appears to
14505 -- be a selected component, but we need to examine the call.
14506
14507 elsif Nkind (N) = N_Explicit_Dereference
14508 and then Nkind (Orig_Node) /= N_Explicit_Dereference
14509 and then Present (Etype (Orig_Node))
14510 and then Is_Access_Type (Etype (Orig_Node))
14511 then
14512 -- Note that if the prefix is an explicit dereference that does not
14513 -- come from source, we must check for a rewritten function call in
14514 -- prefixed notation before other forms of rewriting, to prevent a
14515 -- compiler crash.
14516
14517 return
14518 (Nkind (Orig_Node) = N_Function_Call
14519 and then not Is_Access_Constant (Etype (Prefix (N))))
14520 or else
14521 Is_Variable_Prefix (Original_Node (Prefix (N)));
14522
14523 -- in Ada 2012, the dereference may have been added for a type with
14524 -- a declared implicit dereference aspect. Check that it is not an
14525 -- access to constant.
14526
14527 elsif Nkind (N) = N_Explicit_Dereference
14528 and then Present (Etype (Orig_Node))
14529 and then Ada_Version >= Ada_2012
14530 and then Has_Implicit_Dereference (Etype (Orig_Node))
14531 then
14532 return not Is_Access_Constant (Etype (Prefix (N)));
14533
14534 -- A function call is never a variable
14535
14536 elsif Nkind (N) = N_Function_Call then
14537 return False;
14538
14539 -- All remaining checks use the original node
14540
14541 elsif Is_Entity_Name (Orig_Node)
14542 and then Present (Entity (Orig_Node))
14543 then
14544 declare
14545 E : constant Entity_Id := Entity (Orig_Node);
14546 K : constant Entity_Kind := Ekind (E);
14547
14548 begin
14549 return (K = E_Variable
14550 and then Nkind (Parent (E)) /= N_Exception_Handler)
14551 or else (K = E_Component
14552 and then not In_Protected_Function (E))
14553 or else K = E_Out_Parameter
14554 or else K = E_In_Out_Parameter
14555 or else K = E_Generic_In_Out_Parameter
14556
14557 -- Current instance of type. If this is a protected type, check
14558 -- we are not within the body of one of its protected functions.
14559
14560 or else (Is_Type (E)
14561 and then In_Open_Scopes (E)
14562 and then not In_Protected_Function (E))
14563
14564 or else (Is_Incomplete_Or_Private_Type (E)
14565 and then In_Open_Scopes (Full_View (E)));
14566 end;
14567
14568 else
14569 case Nkind (Orig_Node) is
14570 when N_Indexed_Component | N_Slice =>
14571 return Is_Variable_Prefix (Prefix (Orig_Node));
14572
14573 when N_Selected_Component =>
14574 return (Is_Variable (Selector_Name (Orig_Node))
14575 and then Is_Variable_Prefix (Prefix (Orig_Node)))
14576 or else
14577 (Nkind (N) = N_Expanded_Name
14578 and then Scope (Entity (N)) = Entity (Prefix (N)));
14579
14580 -- For an explicit dereference, the type of the prefix cannot
14581 -- be an access to constant or an access to subprogram.
14582
14583 when N_Explicit_Dereference =>
14584 declare
14585 Typ : constant Entity_Id := Etype (Prefix (Orig_Node));
14586 begin
14587 return Is_Access_Type (Typ)
14588 and then not Is_Access_Constant (Root_Type (Typ))
14589 and then Ekind (Typ) /= E_Access_Subprogram_Type;
14590 end;
14591
14592 -- The type conversion is the case where we do not deal with the
14593 -- context dependent special case of an actual parameter. Thus
14594 -- the type conversion is only considered a variable for the
14595 -- purposes of this routine if the target type is tagged. However,
14596 -- a type conversion is considered to be a variable if it does not
14597 -- come from source (this deals for example with the conversions
14598 -- of expressions to their actual subtypes).
14599
14600 when N_Type_Conversion =>
14601 return Is_Variable (Expression (Orig_Node))
14602 and then
14603 (not Comes_From_Source (Orig_Node)
14604 or else
14605 (Is_Tagged_Type (Etype (Subtype_Mark (Orig_Node)))
14606 and then
14607 Is_Tagged_Type (Etype (Expression (Orig_Node)))));
14608
14609 -- GNAT allows an unchecked type conversion as a variable. This
14610 -- only affects the generation of internal expanded code, since
14611 -- calls to instantiations of Unchecked_Conversion are never
14612 -- considered variables (since they are function calls).
14613
14614 when N_Unchecked_Type_Conversion =>
14615 return Is_Variable (Expression (Orig_Node));
14616
14617 when others =>
14618 return False;
14619 end case;
14620 end if;
14621 end Is_Variable;
14622
14623 ---------------------------
14624 -- Is_Visibly_Controlled --
14625 ---------------------------
14626
14627 function Is_Visibly_Controlled (T : Entity_Id) return Boolean is
14628 Root : constant Entity_Id := Root_Type (T);
14629 begin
14630 return Chars (Scope (Root)) = Name_Finalization
14631 and then Chars (Scope (Scope (Root))) = Name_Ada
14632 and then Scope (Scope (Scope (Root))) = Standard_Standard;
14633 end Is_Visibly_Controlled;
14634
14635 --------------------------
14636 -- Is_Volatile_Function --
14637 --------------------------
14638
14639 function Is_Volatile_Function (Func_Id : Entity_Id) return Boolean is
14640 begin
14641 -- The caller must ensure that Func_Id denotes a function
14642
14643 pragma Assert (Ekind_In (Func_Id, E_Function, E_Generic_Function));
14644
14645 -- A protected function is automatically volatile
14646
14647 if Is_Primitive (Func_Id)
14648 and then Present (First_Formal (Func_Id))
14649 and then Is_Protected_Type (Etype (First_Formal (Func_Id)))
14650 and then Etype (First_Formal (Func_Id)) = Scope (Func_Id)
14651 then
14652 return True;
14653
14654 -- An instance of Ada.Unchecked_Conversion is a volatile function if
14655 -- either the source or the target are effectively volatile.
14656
14657 elsif Is_Unchecked_Conversion_Instance (Func_Id)
14658 and then Has_Effectively_Volatile_Profile (Func_Id)
14659 then
14660 return True;
14661
14662 -- Otherwise the function is treated as volatile if it is subject to
14663 -- enabled pragma Volatile_Function.
14664
14665 else
14666 return
14667 Is_Enabled_Pragma (Get_Pragma (Func_Id, Pragma_Volatile_Function));
14668 end if;
14669 end Is_Volatile_Function;
14670
14671 ------------------------
14672 -- Is_Volatile_Object --
14673 ------------------------
14674
14675 function Is_Volatile_Object (N : Node_Id) return Boolean is
14676
14677 function Is_Volatile_Prefix (N : Node_Id) return Boolean;
14678 -- If prefix is an implicit dereference, examine designated type
14679
14680 function Object_Has_Volatile_Components (N : Node_Id) return Boolean;
14681 -- Determines if given object has volatile components
14682
14683 ------------------------
14684 -- Is_Volatile_Prefix --
14685 ------------------------
14686
14687 function Is_Volatile_Prefix (N : Node_Id) return Boolean is
14688 Typ : constant Entity_Id := Etype (N);
14689
14690 begin
14691 if Is_Access_Type (Typ) then
14692 declare
14693 Dtyp : constant Entity_Id := Designated_Type (Typ);
14694
14695 begin
14696 return Is_Volatile (Dtyp)
14697 or else Has_Volatile_Components (Dtyp);
14698 end;
14699
14700 else
14701 return Object_Has_Volatile_Components (N);
14702 end if;
14703 end Is_Volatile_Prefix;
14704
14705 ------------------------------------
14706 -- Object_Has_Volatile_Components --
14707 ------------------------------------
14708
14709 function Object_Has_Volatile_Components (N : Node_Id) return Boolean is
14710 Typ : constant Entity_Id := Etype (N);
14711
14712 begin
14713 if Is_Volatile (Typ)
14714 or else Has_Volatile_Components (Typ)
14715 then
14716 return True;
14717
14718 elsif Is_Entity_Name (N)
14719 and then (Has_Volatile_Components (Entity (N))
14720 or else Is_Volatile (Entity (N)))
14721 then
14722 return True;
14723
14724 elsif Nkind (N) = N_Indexed_Component
14725 or else Nkind (N) = N_Selected_Component
14726 then
14727 return Is_Volatile_Prefix (Prefix (N));
14728
14729 else
14730 return False;
14731 end if;
14732 end Object_Has_Volatile_Components;
14733
14734 -- Start of processing for Is_Volatile_Object
14735
14736 begin
14737 if Nkind (N) = N_Defining_Identifier then
14738 return Is_Volatile (N) or else Is_Volatile (Etype (N));
14739
14740 elsif Nkind (N) = N_Expanded_Name then
14741 return Is_Volatile_Object (Entity (N));
14742
14743 elsif Is_Volatile (Etype (N))
14744 or else (Is_Entity_Name (N) and then Is_Volatile (Entity (N)))
14745 then
14746 return True;
14747
14748 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component)
14749 and then Is_Volatile_Prefix (Prefix (N))
14750 then
14751 return True;
14752
14753 elsif Nkind (N) = N_Selected_Component
14754 and then Is_Volatile (Entity (Selector_Name (N)))
14755 then
14756 return True;
14757
14758 else
14759 return False;
14760 end if;
14761 end Is_Volatile_Object;
14762
14763 ---------------------------
14764 -- Itype_Has_Declaration --
14765 ---------------------------
14766
14767 function Itype_Has_Declaration (Id : Entity_Id) return Boolean is
14768 begin
14769 pragma Assert (Is_Itype (Id));
14770 return Present (Parent (Id))
14771 and then Nkind_In (Parent (Id), N_Full_Type_Declaration,
14772 N_Subtype_Declaration)
14773 and then Defining_Entity (Parent (Id)) = Id;
14774 end Itype_Has_Declaration;
14775
14776 -------------------------
14777 -- Kill_Current_Values --
14778 -------------------------
14779
14780 procedure Kill_Current_Values
14781 (Ent : Entity_Id;
14782 Last_Assignment_Only : Boolean := False)
14783 is
14784 begin
14785 if Is_Assignable (Ent) then
14786 Set_Last_Assignment (Ent, Empty);
14787 end if;
14788
14789 if Is_Object (Ent) then
14790 if not Last_Assignment_Only then
14791 Kill_Checks (Ent);
14792 Set_Current_Value (Ent, Empty);
14793
14794 -- Do not reset the Is_Known_[Non_]Null and Is_Known_Valid flags
14795 -- for a constant. Once the constant is elaborated, its value is
14796 -- not changed, therefore the associated flags that describe the
14797 -- value should not be modified either.
14798
14799 if Ekind (Ent) = E_Constant then
14800 null;
14801
14802 -- Non-constant entities
14803
14804 else
14805 if not Can_Never_Be_Null (Ent) then
14806 Set_Is_Known_Non_Null (Ent, False);
14807 end if;
14808
14809 Set_Is_Known_Null (Ent, False);
14810
14811 -- Reset the Is_Known_Valid flag unless the type is always
14812 -- valid. This does not apply to a loop parameter because its
14813 -- bounds are defined by the loop header and therefore always
14814 -- valid.
14815
14816 if not Is_Known_Valid (Etype (Ent))
14817 and then Ekind (Ent) /= E_Loop_Parameter
14818 then
14819 Set_Is_Known_Valid (Ent, False);
14820 end if;
14821 end if;
14822 end if;
14823 end if;
14824 end Kill_Current_Values;
14825
14826 procedure Kill_Current_Values (Last_Assignment_Only : Boolean := False) is
14827 S : Entity_Id;
14828
14829 procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id);
14830 -- Clear current value for entity E and all entities chained to E
14831
14832 ------------------------------------------
14833 -- Kill_Current_Values_For_Entity_Chain --
14834 ------------------------------------------
14835
14836 procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id) is
14837 Ent : Entity_Id;
14838 begin
14839 Ent := E;
14840 while Present (Ent) loop
14841 Kill_Current_Values (Ent, Last_Assignment_Only);
14842 Next_Entity (Ent);
14843 end loop;
14844 end Kill_Current_Values_For_Entity_Chain;
14845
14846 -- Start of processing for Kill_Current_Values
14847
14848 begin
14849 -- Kill all saved checks, a special case of killing saved values
14850
14851 if not Last_Assignment_Only then
14852 Kill_All_Checks;
14853 end if;
14854
14855 -- Loop through relevant scopes, which includes the current scope and
14856 -- any parent scopes if the current scope is a block or a package.
14857
14858 S := Current_Scope;
14859 Scope_Loop : loop
14860
14861 -- Clear current values of all entities in current scope
14862
14863 Kill_Current_Values_For_Entity_Chain (First_Entity (S));
14864
14865 -- If scope is a package, also clear current values of all private
14866 -- entities in the scope.
14867
14868 if Is_Package_Or_Generic_Package (S)
14869 or else Is_Concurrent_Type (S)
14870 then
14871 Kill_Current_Values_For_Entity_Chain (First_Private_Entity (S));
14872 end if;
14873
14874 -- If this is a not a subprogram, deal with parents
14875
14876 if not Is_Subprogram (S) then
14877 S := Scope (S);
14878 exit Scope_Loop when S = Standard_Standard;
14879 else
14880 exit Scope_Loop;
14881 end if;
14882 end loop Scope_Loop;
14883 end Kill_Current_Values;
14884
14885 --------------------------
14886 -- Kill_Size_Check_Code --
14887 --------------------------
14888
14889 procedure Kill_Size_Check_Code (E : Entity_Id) is
14890 begin
14891 if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
14892 and then Present (Size_Check_Code (E))
14893 then
14894 Remove (Size_Check_Code (E));
14895 Set_Size_Check_Code (E, Empty);
14896 end if;
14897 end Kill_Size_Check_Code;
14898
14899 --------------------------
14900 -- Known_To_Be_Assigned --
14901 --------------------------
14902
14903 function Known_To_Be_Assigned (N : Node_Id) return Boolean is
14904 P : constant Node_Id := Parent (N);
14905
14906 begin
14907 case Nkind (P) is
14908
14909 -- Test left side of assignment
14910
14911 when N_Assignment_Statement =>
14912 return N = Name (P);
14913
14914 -- Function call arguments are never lvalues
14915
14916 when N_Function_Call =>
14917 return False;
14918
14919 -- Positional parameter for procedure or accept call
14920
14921 when N_Procedure_Call_Statement |
14922 N_Accept_Statement
14923 =>
14924 declare
14925 Proc : Entity_Id;
14926 Form : Entity_Id;
14927 Act : Node_Id;
14928
14929 begin
14930 Proc := Get_Subprogram_Entity (P);
14931
14932 if No (Proc) then
14933 return False;
14934 end if;
14935
14936 -- If we are not a list member, something is strange, so
14937 -- be conservative and return False.
14938
14939 if not Is_List_Member (N) then
14940 return False;
14941 end if;
14942
14943 -- We are going to find the right formal by stepping forward
14944 -- through the formals, as we step backwards in the actuals.
14945
14946 Form := First_Formal (Proc);
14947 Act := N;
14948 loop
14949 -- If no formal, something is weird, so be conservative
14950 -- and return False.
14951
14952 if No (Form) then
14953 return False;
14954 end if;
14955
14956 Prev (Act);
14957 exit when No (Act);
14958 Next_Formal (Form);
14959 end loop;
14960
14961 return Ekind (Form) /= E_In_Parameter;
14962 end;
14963
14964 -- Named parameter for procedure or accept call
14965
14966 when N_Parameter_Association =>
14967 declare
14968 Proc : Entity_Id;
14969 Form : Entity_Id;
14970
14971 begin
14972 Proc := Get_Subprogram_Entity (Parent (P));
14973
14974 if No (Proc) then
14975 return False;
14976 end if;
14977
14978 -- Loop through formals to find the one that matches
14979
14980 Form := First_Formal (Proc);
14981 loop
14982 -- If no matching formal, that's peculiar, some kind of
14983 -- previous error, so return False to be conservative.
14984 -- Actually this also happens in legal code in the case
14985 -- where P is a parameter association for an Extra_Formal???
14986
14987 if No (Form) then
14988 return False;
14989 end if;
14990
14991 -- Else test for match
14992
14993 if Chars (Form) = Chars (Selector_Name (P)) then
14994 return Ekind (Form) /= E_In_Parameter;
14995 end if;
14996
14997 Next_Formal (Form);
14998 end loop;
14999 end;
15000
15001 -- Test for appearing in a conversion that itself appears
15002 -- in an lvalue context, since this should be an lvalue.
15003
15004 when N_Type_Conversion =>
15005 return Known_To_Be_Assigned (P);
15006
15007 -- All other references are definitely not known to be modifications
15008
15009 when others =>
15010 return False;
15011
15012 end case;
15013 end Known_To_Be_Assigned;
15014
15015 ---------------------------
15016 -- Last_Source_Statement --
15017 ---------------------------
15018
15019 function Last_Source_Statement (HSS : Node_Id) return Node_Id is
15020 N : Node_Id;
15021
15022 begin
15023 N := Last (Statements (HSS));
15024 while Present (N) loop
15025 exit when Comes_From_Source (N);
15026 Prev (N);
15027 end loop;
15028
15029 return N;
15030 end Last_Source_Statement;
15031
15032 ----------------------------------
15033 -- Matching_Static_Array_Bounds --
15034 ----------------------------------
15035
15036 function Matching_Static_Array_Bounds
15037 (L_Typ : Node_Id;
15038 R_Typ : Node_Id) return Boolean
15039 is
15040 L_Ndims : constant Nat := Number_Dimensions (L_Typ);
15041 R_Ndims : constant Nat := Number_Dimensions (R_Typ);
15042
15043 L_Index : Node_Id;
15044 R_Index : Node_Id;
15045 L_Low : Node_Id;
15046 L_High : Node_Id;
15047 L_Len : Uint;
15048 R_Low : Node_Id;
15049 R_High : Node_Id;
15050 R_Len : Uint;
15051
15052 begin
15053 if L_Ndims /= R_Ndims then
15054 return False;
15055 end if;
15056
15057 -- Unconstrained types do not have static bounds
15058
15059 if not Is_Constrained (L_Typ) or else not Is_Constrained (R_Typ) then
15060 return False;
15061 end if;
15062
15063 -- First treat specially the first dimension, as the lower bound and
15064 -- length of string literals are not stored like those of arrays.
15065
15066 if Ekind (L_Typ) = E_String_Literal_Subtype then
15067 L_Low := String_Literal_Low_Bound (L_Typ);
15068 L_Len := String_Literal_Length (L_Typ);
15069 else
15070 L_Index := First_Index (L_Typ);
15071 Get_Index_Bounds (L_Index, L_Low, L_High);
15072
15073 if Is_OK_Static_Expression (L_Low)
15074 and then
15075 Is_OK_Static_Expression (L_High)
15076 then
15077 if Expr_Value (L_High) < Expr_Value (L_Low) then
15078 L_Len := Uint_0;
15079 else
15080 L_Len := (Expr_Value (L_High) - Expr_Value (L_Low)) + 1;
15081 end if;
15082 else
15083 return False;
15084 end if;
15085 end if;
15086
15087 if Ekind (R_Typ) = E_String_Literal_Subtype then
15088 R_Low := String_Literal_Low_Bound (R_Typ);
15089 R_Len := String_Literal_Length (R_Typ);
15090 else
15091 R_Index := First_Index (R_Typ);
15092 Get_Index_Bounds (R_Index, R_Low, R_High);
15093
15094 if Is_OK_Static_Expression (R_Low)
15095 and then
15096 Is_OK_Static_Expression (R_High)
15097 then
15098 if Expr_Value (R_High) < Expr_Value (R_Low) then
15099 R_Len := Uint_0;
15100 else
15101 R_Len := (Expr_Value (R_High) - Expr_Value (R_Low)) + 1;
15102 end if;
15103 else
15104 return False;
15105 end if;
15106 end if;
15107
15108 if (Is_OK_Static_Expression (L_Low)
15109 and then
15110 Is_OK_Static_Expression (R_Low))
15111 and then Expr_Value (L_Low) = Expr_Value (R_Low)
15112 and then L_Len = R_Len
15113 then
15114 null;
15115 else
15116 return False;
15117 end if;
15118
15119 -- Then treat all other dimensions
15120
15121 for Indx in 2 .. L_Ndims loop
15122 Next (L_Index);
15123 Next (R_Index);
15124
15125 Get_Index_Bounds (L_Index, L_Low, L_High);
15126 Get_Index_Bounds (R_Index, R_Low, R_High);
15127
15128 if (Is_OK_Static_Expression (L_Low) and then
15129 Is_OK_Static_Expression (L_High) and then
15130 Is_OK_Static_Expression (R_Low) and then
15131 Is_OK_Static_Expression (R_High))
15132 and then (Expr_Value (L_Low) = Expr_Value (R_Low)
15133 and then
15134 Expr_Value (L_High) = Expr_Value (R_High))
15135 then
15136 null;
15137 else
15138 return False;
15139 end if;
15140 end loop;
15141
15142 -- If we fall through the loop, all indexes matched
15143
15144 return True;
15145 end Matching_Static_Array_Bounds;
15146
15147 -------------------
15148 -- May_Be_Lvalue --
15149 -------------------
15150
15151 function May_Be_Lvalue (N : Node_Id) return Boolean is
15152 P : constant Node_Id := Parent (N);
15153
15154 begin
15155 case Nkind (P) is
15156
15157 -- Test left side of assignment
15158
15159 when N_Assignment_Statement =>
15160 return N = Name (P);
15161
15162 -- Test prefix of component or attribute. Note that the prefix of an
15163 -- explicit or implicit dereference cannot be an l-value.
15164
15165 when N_Attribute_Reference =>
15166 return N = Prefix (P)
15167 and then Name_Implies_Lvalue_Prefix (Attribute_Name (P));
15168
15169 -- For an expanded name, the name is an lvalue if the expanded name
15170 -- is an lvalue, but the prefix is never an lvalue, since it is just
15171 -- the scope where the name is found.
15172
15173 when N_Expanded_Name =>
15174 if N = Prefix (P) then
15175 return May_Be_Lvalue (P);
15176 else
15177 return False;
15178 end if;
15179
15180 -- For a selected component A.B, A is certainly an lvalue if A.B is.
15181 -- B is a little interesting, if we have A.B := 3, there is some
15182 -- discussion as to whether B is an lvalue or not, we choose to say
15183 -- it is. Note however that A is not an lvalue if it is of an access
15184 -- type since this is an implicit dereference.
15185
15186 when N_Selected_Component =>
15187 if N = Prefix (P)
15188 and then Present (Etype (N))
15189 and then Is_Access_Type (Etype (N))
15190 then
15191 return False;
15192 else
15193 return May_Be_Lvalue (P);
15194 end if;
15195
15196 -- For an indexed component or slice, the index or slice bounds is
15197 -- never an lvalue. The prefix is an lvalue if the indexed component
15198 -- or slice is an lvalue, except if it is an access type, where we
15199 -- have an implicit dereference.
15200
15201 when N_Indexed_Component | N_Slice =>
15202 if N /= Prefix (P)
15203 or else (Present (Etype (N)) and then Is_Access_Type (Etype (N)))
15204 then
15205 return False;
15206 else
15207 return May_Be_Lvalue (P);
15208 end if;
15209
15210 -- Prefix of a reference is an lvalue if the reference is an lvalue
15211
15212 when N_Reference =>
15213 return May_Be_Lvalue (P);
15214
15215 -- Prefix of explicit dereference is never an lvalue
15216
15217 when N_Explicit_Dereference =>
15218 return False;
15219
15220 -- Positional parameter for subprogram, entry, or accept call.
15221 -- In older versions of Ada function call arguments are never
15222 -- lvalues. In Ada 2012 functions can have in-out parameters.
15223
15224 when N_Subprogram_Call |
15225 N_Entry_Call_Statement |
15226 N_Accept_Statement
15227 =>
15228 if Nkind (P) = N_Function_Call and then Ada_Version < Ada_2012 then
15229 return False;
15230 end if;
15231
15232 -- The following mechanism is clumsy and fragile. A single flag
15233 -- set in Resolve_Actuals would be preferable ???
15234
15235 declare
15236 Proc : Entity_Id;
15237 Form : Entity_Id;
15238 Act : Node_Id;
15239
15240 begin
15241 Proc := Get_Subprogram_Entity (P);
15242
15243 if No (Proc) then
15244 return True;
15245 end if;
15246
15247 -- If we are not a list member, something is strange, so be
15248 -- conservative and return True.
15249
15250 if not Is_List_Member (N) then
15251 return True;
15252 end if;
15253
15254 -- We are going to find the right formal by stepping forward
15255 -- through the formals, as we step backwards in the actuals.
15256
15257 Form := First_Formal (Proc);
15258 Act := N;
15259 loop
15260 -- If no formal, something is weird, so be conservative and
15261 -- return True.
15262
15263 if No (Form) then
15264 return True;
15265 end if;
15266
15267 Prev (Act);
15268 exit when No (Act);
15269 Next_Formal (Form);
15270 end loop;
15271
15272 return Ekind (Form) /= E_In_Parameter;
15273 end;
15274
15275 -- Named parameter for procedure or accept call
15276
15277 when N_Parameter_Association =>
15278 declare
15279 Proc : Entity_Id;
15280 Form : Entity_Id;
15281
15282 begin
15283 Proc := Get_Subprogram_Entity (Parent (P));
15284
15285 if No (Proc) then
15286 return True;
15287 end if;
15288
15289 -- Loop through formals to find the one that matches
15290
15291 Form := First_Formal (Proc);
15292 loop
15293 -- If no matching formal, that's peculiar, some kind of
15294 -- previous error, so return True to be conservative.
15295 -- Actually happens with legal code for an unresolved call
15296 -- where we may get the wrong homonym???
15297
15298 if No (Form) then
15299 return True;
15300 end if;
15301
15302 -- Else test for match
15303
15304 if Chars (Form) = Chars (Selector_Name (P)) then
15305 return Ekind (Form) /= E_In_Parameter;
15306 end if;
15307
15308 Next_Formal (Form);
15309 end loop;
15310 end;
15311
15312 -- Test for appearing in a conversion that itself appears in an
15313 -- lvalue context, since this should be an lvalue.
15314
15315 when N_Type_Conversion =>
15316 return May_Be_Lvalue (P);
15317
15318 -- Test for appearance in object renaming declaration
15319
15320 when N_Object_Renaming_Declaration =>
15321 return True;
15322
15323 -- All other references are definitely not lvalues
15324
15325 when others =>
15326 return False;
15327
15328 end case;
15329 end May_Be_Lvalue;
15330
15331 -----------------------
15332 -- Mark_Coextensions --
15333 -----------------------
15334
15335 procedure Mark_Coextensions (Context_Nod : Node_Id; Root_Nod : Node_Id) is
15336 Is_Dynamic : Boolean;
15337 -- Indicates whether the context causes nested coextensions to be
15338 -- dynamic or static
15339
15340 function Mark_Allocator (N : Node_Id) return Traverse_Result;
15341 -- Recognize an allocator node and label it as a dynamic coextension
15342
15343 --------------------
15344 -- Mark_Allocator --
15345 --------------------
15346
15347 function Mark_Allocator (N : Node_Id) return Traverse_Result is
15348 begin
15349 if Nkind (N) = N_Allocator then
15350 if Is_Dynamic then
15351 Set_Is_Dynamic_Coextension (N);
15352
15353 -- If the allocator expression is potentially dynamic, it may
15354 -- be expanded out of order and require dynamic allocation
15355 -- anyway, so we treat the coextension itself as dynamic.
15356 -- Potential optimization ???
15357
15358 elsif Nkind (Expression (N)) = N_Qualified_Expression
15359 and then Nkind (Expression (Expression (N))) = N_Op_Concat
15360 then
15361 Set_Is_Dynamic_Coextension (N);
15362 else
15363 Set_Is_Static_Coextension (N);
15364 end if;
15365 end if;
15366
15367 return OK;
15368 end Mark_Allocator;
15369
15370 procedure Mark_Allocators is new Traverse_Proc (Mark_Allocator);
15371
15372 -- Start of processing for Mark_Coextensions
15373
15374 begin
15375 -- An allocator that appears on the right-hand side of an assignment is
15376 -- treated as a potentially dynamic coextension when the right-hand side
15377 -- is an allocator or a qualified expression.
15378
15379 -- Obj := new ...'(new Coextension ...);
15380
15381 if Nkind (Context_Nod) = N_Assignment_Statement then
15382 Is_Dynamic :=
15383 Nkind_In (Expression (Context_Nod), N_Allocator,
15384 N_Qualified_Expression);
15385
15386 -- An allocator that appears within the expression of a simple return
15387 -- statement is treated as a potentially dynamic coextension when the
15388 -- expression is either aggregate, allocator, or qualified expression.
15389
15390 -- return (new Coextension ...);
15391 -- return new ...'(new Coextension ...);
15392
15393 elsif Nkind (Context_Nod) = N_Simple_Return_Statement then
15394 Is_Dynamic :=
15395 Nkind_In (Expression (Context_Nod), N_Aggregate,
15396 N_Allocator,
15397 N_Qualified_Expression);
15398
15399 -- An alloctor that appears within the initialization expression of an
15400 -- object declaration is considered a potentially dynamic coextension
15401 -- when the initialization expression is an allocator or a qualified
15402 -- expression.
15403
15404 -- Obj : ... := new ...'(new Coextension ...);
15405
15406 -- A similar case arises when the object declaration is part of an
15407 -- extended return statement.
15408
15409 -- return Obj : ... := new ...'(new Coextension ...);
15410 -- return Obj : ... := (new Coextension ...);
15411
15412 elsif Nkind (Context_Nod) = N_Object_Declaration then
15413 Is_Dynamic :=
15414 Nkind_In (Root_Nod, N_Allocator, N_Qualified_Expression)
15415 or else
15416 Nkind (Parent (Context_Nod)) = N_Extended_Return_Statement;
15417
15418 -- This routine should not be called with constructs that cannot contain
15419 -- coextensions.
15420
15421 else
15422 raise Program_Error;
15423 end if;
15424
15425 Mark_Allocators (Root_Nod);
15426 end Mark_Coextensions;
15427
15428 ----------------------
15429 -- Needs_One_Actual --
15430 ----------------------
15431
15432 function Needs_One_Actual (E : Entity_Id) return Boolean is
15433 Formal : Entity_Id;
15434
15435 begin
15436 -- Ada 2005 or later, and formals present
15437
15438 if Ada_Version >= Ada_2005 and then Present (First_Formal (E)) then
15439 Formal := Next_Formal (First_Formal (E));
15440 while Present (Formal) loop
15441 if No (Default_Value (Formal)) then
15442 return False;
15443 end if;
15444
15445 Next_Formal (Formal);
15446 end loop;
15447
15448 return True;
15449
15450 -- Ada 83/95 or no formals
15451
15452 else
15453 return False;
15454 end if;
15455 end Needs_One_Actual;
15456
15457 ------------------------
15458 -- New_Copy_List_Tree --
15459 ------------------------
15460
15461 function New_Copy_List_Tree (List : List_Id) return List_Id is
15462 NL : List_Id;
15463 E : Node_Id;
15464
15465 begin
15466 if List = No_List then
15467 return No_List;
15468
15469 else
15470 NL := New_List;
15471 E := First (List);
15472
15473 while Present (E) loop
15474 Append (New_Copy_Tree (E), NL);
15475 E := Next (E);
15476 end loop;
15477
15478 return NL;
15479 end if;
15480 end New_Copy_List_Tree;
15481
15482 --------------------------------------------------
15483 -- New_Copy_Tree Auxiliary Data and Subprograms --
15484 --------------------------------------------------
15485
15486 use Atree.Unchecked_Access;
15487 use Atree_Private_Part;
15488
15489 -- Our approach here requires a two pass traversal of the tree. The
15490 -- first pass visits all nodes that eventually will be copied looking
15491 -- for defining Itypes. If any defining Itypes are found, then they are
15492 -- copied, and an entry is added to the replacement map. In the second
15493 -- phase, the tree is copied, using the replacement map to replace any
15494 -- Itype references within the copied tree.
15495
15496 -- The following hash tables are used if the Map supplied has more
15497 -- than hash threshold entries to speed up access to the map. If
15498 -- there are fewer entries, then the map is searched sequentially
15499 -- (because setting up a hash table for only a few entries takes
15500 -- more time than it saves.
15501
15502 function New_Copy_Hash (E : Entity_Id) return NCT_Header_Num;
15503 -- Hash function used for hash operations
15504
15505 -------------------
15506 -- New_Copy_Hash --
15507 -------------------
15508
15509 function New_Copy_Hash (E : Entity_Id) return NCT_Header_Num is
15510 begin
15511 return Nat (E) mod (NCT_Header_Num'Last + 1);
15512 end New_Copy_Hash;
15513
15514 ---------------
15515 -- NCT_Assoc --
15516 ---------------
15517
15518 -- The hash table NCT_Assoc associates old entities in the table
15519 -- with their corresponding new entities (i.e. the pairs of entries
15520 -- presented in the original Map argument are Key-Element pairs).
15521
15522 package NCT_Assoc is new Simple_HTable (
15523 Header_Num => NCT_Header_Num,
15524 Element => Entity_Id,
15525 No_Element => Empty,
15526 Key => Entity_Id,
15527 Hash => New_Copy_Hash,
15528 Equal => Types."=");
15529
15530 ---------------------
15531 -- NCT_Itype_Assoc --
15532 ---------------------
15533
15534 -- The hash table NCT_Itype_Assoc contains entries only for those
15535 -- old nodes which have a non-empty Associated_Node_For_Itype set.
15536 -- The key is the associated node, and the element is the new node
15537 -- itself (NOT the associated node for the new node).
15538
15539 package NCT_Itype_Assoc is new Simple_HTable (
15540 Header_Num => NCT_Header_Num,
15541 Element => Entity_Id,
15542 No_Element => Empty,
15543 Key => Entity_Id,
15544 Hash => New_Copy_Hash,
15545 Equal => Types."=");
15546
15547 -------------------
15548 -- New_Copy_Tree --
15549 -------------------
15550
15551 function New_Copy_Tree
15552 (Source : Node_Id;
15553 Map : Elist_Id := No_Elist;
15554 New_Sloc : Source_Ptr := No_Location;
15555 New_Scope : Entity_Id := Empty) return Node_Id
15556 is
15557 Actual_Map : Elist_Id := Map;
15558 -- This is the actual map for the copy. It is initialized with the
15559 -- given elements, and then enlarged as required for Itypes that are
15560 -- copied during the first phase of the copy operation. The visit
15561 -- procedures add elements to this map as Itypes are encountered.
15562 -- The reason we cannot use Map directly, is that it may well be
15563 -- (and normally is) initialized to No_Elist, and if we have mapped
15564 -- entities, we have to reset it to point to a real Elist.
15565
15566 function Assoc (N : Node_Or_Entity_Id) return Node_Id;
15567 -- Called during second phase to map entities into their corresponding
15568 -- copies using Actual_Map. If the argument is not an entity, or is not
15569 -- in Actual_Map, then it is returned unchanged.
15570
15571 procedure Build_NCT_Hash_Tables;
15572 -- Builds hash tables (number of elements >= threshold value)
15573
15574 function Copy_Elist_With_Replacement
15575 (Old_Elist : Elist_Id) return Elist_Id;
15576 -- Called during second phase to copy element list doing replacements
15577
15578 procedure Copy_Itype_With_Replacement (New_Itype : Entity_Id);
15579 -- Called during the second phase to process a copied Itype. The actual
15580 -- copy happened during the first phase (so that we could make the entry
15581 -- in the mapping), but we still have to deal with the descendants of
15582 -- the copied Itype and copy them where necessary.
15583
15584 function Copy_List_With_Replacement (Old_List : List_Id) return List_Id;
15585 -- Called during second phase to copy list doing replacements
15586
15587 function Copy_Node_With_Replacement (Old_Node : Node_Id) return Node_Id;
15588 -- Called during second phase to copy node doing replacements
15589
15590 procedure Visit_Elist (E : Elist_Id);
15591 -- Called during first phase to visit all elements of an Elist
15592
15593 procedure Visit_Field (F : Union_Id; N : Node_Id);
15594 -- Visit a single field, recursing to call Visit_Node or Visit_List
15595 -- if the field is a syntactic descendant of the current node (i.e.
15596 -- its parent is Node N).
15597
15598 procedure Visit_Itype (Old_Itype : Entity_Id);
15599 -- Called during first phase to visit subsidiary fields of a defining
15600 -- Itype, and also create a copy and make an entry in the replacement
15601 -- map for the new copy.
15602
15603 procedure Visit_List (L : List_Id);
15604 -- Called during first phase to visit all elements of a List
15605
15606 procedure Visit_Node (N : Node_Or_Entity_Id);
15607 -- Called during first phase to visit a node and all its subtrees
15608
15609 -----------
15610 -- Assoc --
15611 -----------
15612
15613 function Assoc (N : Node_Or_Entity_Id) return Node_Id is
15614 E : Elmt_Id;
15615 Ent : Entity_Id;
15616
15617 begin
15618 if not Has_Extension (N) or else No (Actual_Map) then
15619 return N;
15620
15621 elsif NCT_Hash_Tables_Used then
15622 Ent := NCT_Assoc.Get (Entity_Id (N));
15623
15624 if Present (Ent) then
15625 return Ent;
15626 else
15627 return N;
15628 end if;
15629
15630 -- No hash table used, do serial search
15631
15632 else
15633 E := First_Elmt (Actual_Map);
15634 while Present (E) loop
15635 if Node (E) = N then
15636 return Node (Next_Elmt (E));
15637 else
15638 E := Next_Elmt (Next_Elmt (E));
15639 end if;
15640 end loop;
15641 end if;
15642
15643 return N;
15644 end Assoc;
15645
15646 ---------------------------
15647 -- Build_NCT_Hash_Tables --
15648 ---------------------------
15649
15650 procedure Build_NCT_Hash_Tables is
15651 Elmt : Elmt_Id;
15652 Ent : Entity_Id;
15653 begin
15654 if NCT_Hash_Table_Setup then
15655 NCT_Assoc.Reset;
15656 NCT_Itype_Assoc.Reset;
15657 end if;
15658
15659 Elmt := First_Elmt (Actual_Map);
15660 while Present (Elmt) loop
15661 Ent := Node (Elmt);
15662
15663 -- Get new entity, and associate old and new
15664
15665 Next_Elmt (Elmt);
15666 NCT_Assoc.Set (Ent, Node (Elmt));
15667
15668 if Is_Type (Ent) then
15669 declare
15670 Anode : constant Entity_Id :=
15671 Associated_Node_For_Itype (Ent);
15672
15673 begin
15674 if Present (Anode) then
15675
15676 -- Enter a link between the associated node of the
15677 -- old Itype and the new Itype, for updating later
15678 -- when node is copied.
15679
15680 NCT_Itype_Assoc.Set (Anode, Node (Elmt));
15681 end if;
15682 end;
15683 end if;
15684
15685 Next_Elmt (Elmt);
15686 end loop;
15687
15688 NCT_Hash_Tables_Used := True;
15689 NCT_Hash_Table_Setup := True;
15690 end Build_NCT_Hash_Tables;
15691
15692 ---------------------------------
15693 -- Copy_Elist_With_Replacement --
15694 ---------------------------------
15695
15696 function Copy_Elist_With_Replacement
15697 (Old_Elist : Elist_Id) return Elist_Id
15698 is
15699 M : Elmt_Id;
15700 New_Elist : Elist_Id;
15701
15702 begin
15703 if No (Old_Elist) then
15704 return No_Elist;
15705
15706 else
15707 New_Elist := New_Elmt_List;
15708
15709 M := First_Elmt (Old_Elist);
15710 while Present (M) loop
15711 Append_Elmt (Copy_Node_With_Replacement (Node (M)), New_Elist);
15712 Next_Elmt (M);
15713 end loop;
15714 end if;
15715
15716 return New_Elist;
15717 end Copy_Elist_With_Replacement;
15718
15719 ---------------------------------
15720 -- Copy_Itype_With_Replacement --
15721 ---------------------------------
15722
15723 -- This routine exactly parallels its phase one analog Visit_Itype,
15724
15725 procedure Copy_Itype_With_Replacement (New_Itype : Entity_Id) is
15726 begin
15727 -- Translate Next_Entity, Scope and Etype fields, in case they
15728 -- reference entities that have been mapped into copies.
15729
15730 Set_Next_Entity (New_Itype, Assoc (Next_Entity (New_Itype)));
15731 Set_Etype (New_Itype, Assoc (Etype (New_Itype)));
15732
15733 if Present (New_Scope) then
15734 Set_Scope (New_Itype, New_Scope);
15735 else
15736 Set_Scope (New_Itype, Assoc (Scope (New_Itype)));
15737 end if;
15738
15739 -- Copy referenced fields
15740
15741 if Is_Discrete_Type (New_Itype) then
15742 Set_Scalar_Range (New_Itype,
15743 Copy_Node_With_Replacement (Scalar_Range (New_Itype)));
15744
15745 elsif Has_Discriminants (Base_Type (New_Itype)) then
15746 Set_Discriminant_Constraint (New_Itype,
15747 Copy_Elist_With_Replacement
15748 (Discriminant_Constraint (New_Itype)));
15749
15750 elsif Is_Array_Type (New_Itype) then
15751 if Present (First_Index (New_Itype)) then
15752 Set_First_Index (New_Itype,
15753 First (Copy_List_With_Replacement
15754 (List_Containing (First_Index (New_Itype)))));
15755 end if;
15756
15757 if Is_Packed (New_Itype) then
15758 Set_Packed_Array_Impl_Type (New_Itype,
15759 Copy_Node_With_Replacement
15760 (Packed_Array_Impl_Type (New_Itype)));
15761 end if;
15762 end if;
15763 end Copy_Itype_With_Replacement;
15764
15765 --------------------------------
15766 -- Copy_List_With_Replacement --
15767 --------------------------------
15768
15769 function Copy_List_With_Replacement
15770 (Old_List : List_Id) return List_Id
15771 is
15772 New_List : List_Id;
15773 E : Node_Id;
15774
15775 begin
15776 if Old_List = No_List then
15777 return No_List;
15778
15779 else
15780 New_List := Empty_List;
15781
15782 E := First (Old_List);
15783 while Present (E) loop
15784 Append (Copy_Node_With_Replacement (E), New_List);
15785 Next (E);
15786 end loop;
15787
15788 return New_List;
15789 end if;
15790 end Copy_List_With_Replacement;
15791
15792 --------------------------------
15793 -- Copy_Node_With_Replacement --
15794 --------------------------------
15795
15796 function Copy_Node_With_Replacement
15797 (Old_Node : Node_Id) return Node_Id
15798 is
15799 New_Node : Node_Id;
15800
15801 procedure Adjust_Named_Associations
15802 (Old_Node : Node_Id;
15803 New_Node : Node_Id);
15804 -- If a call node has named associations, these are chained through
15805 -- the First_Named_Actual, Next_Named_Actual links. These must be
15806 -- propagated separately to the new parameter list, because these
15807 -- are not syntactic fields.
15808
15809 function Copy_Field_With_Replacement
15810 (Field : Union_Id) return Union_Id;
15811 -- Given Field, which is a field of Old_Node, return a copy of it
15812 -- if it is a syntactic field (i.e. its parent is Node), setting
15813 -- the parent of the copy to poit to New_Node. Otherwise returns
15814 -- the field (possibly mapped if it is an entity).
15815
15816 -------------------------------
15817 -- Adjust_Named_Associations --
15818 -------------------------------
15819
15820 procedure Adjust_Named_Associations
15821 (Old_Node : Node_Id;
15822 New_Node : Node_Id)
15823 is
15824 Old_E : Node_Id;
15825 New_E : Node_Id;
15826
15827 Old_Next : Node_Id;
15828 New_Next : Node_Id;
15829
15830 begin
15831 Old_E := First (Parameter_Associations (Old_Node));
15832 New_E := First (Parameter_Associations (New_Node));
15833 while Present (Old_E) loop
15834 if Nkind (Old_E) = N_Parameter_Association
15835 and then Present (Next_Named_Actual (Old_E))
15836 then
15837 if First_Named_Actual (Old_Node)
15838 = Explicit_Actual_Parameter (Old_E)
15839 then
15840 Set_First_Named_Actual
15841 (New_Node, Explicit_Actual_Parameter (New_E));
15842 end if;
15843
15844 -- Now scan parameter list from the beginning,to locate
15845 -- next named actual, which can be out of order.
15846
15847 Old_Next := First (Parameter_Associations (Old_Node));
15848 New_Next := First (Parameter_Associations (New_Node));
15849
15850 while Nkind (Old_Next) /= N_Parameter_Association
15851 or else Explicit_Actual_Parameter (Old_Next) /=
15852 Next_Named_Actual (Old_E)
15853 loop
15854 Next (Old_Next);
15855 Next (New_Next);
15856 end loop;
15857
15858 Set_Next_Named_Actual
15859 (New_E, Explicit_Actual_Parameter (New_Next));
15860 end if;
15861
15862 Next (Old_E);
15863 Next (New_E);
15864 end loop;
15865 end Adjust_Named_Associations;
15866
15867 ---------------------------------
15868 -- Copy_Field_With_Replacement --
15869 ---------------------------------
15870
15871 function Copy_Field_With_Replacement
15872 (Field : Union_Id) return Union_Id
15873 is
15874 begin
15875 if Field = Union_Id (Empty) then
15876 return Field;
15877
15878 elsif Field in Node_Range then
15879 declare
15880 Old_N : constant Node_Id := Node_Id (Field);
15881 New_N : Node_Id;
15882
15883 begin
15884 -- If syntactic field, as indicated by the parent pointer
15885 -- being set, then copy the referenced node recursively.
15886
15887 if Parent (Old_N) = Old_Node then
15888 New_N := Copy_Node_With_Replacement (Old_N);
15889
15890 if New_N /= Old_N then
15891 Set_Parent (New_N, New_Node);
15892 end if;
15893
15894 -- For semantic fields, update possible entity reference
15895 -- from the replacement map.
15896
15897 else
15898 New_N := Assoc (Old_N);
15899 end if;
15900
15901 return Union_Id (New_N);
15902 end;
15903
15904 elsif Field in List_Range then
15905 declare
15906 Old_L : constant List_Id := List_Id (Field);
15907 New_L : List_Id;
15908
15909 begin
15910 -- If syntactic field, as indicated by the parent pointer,
15911 -- then recursively copy the entire referenced list.
15912
15913 if Parent (Old_L) = Old_Node then
15914 New_L := Copy_List_With_Replacement (Old_L);
15915 Set_Parent (New_L, New_Node);
15916
15917 -- For semantic list, just returned unchanged
15918
15919 else
15920 New_L := Old_L;
15921 end if;
15922
15923 return Union_Id (New_L);
15924 end;
15925
15926 -- Anything other than a list or a node is returned unchanged
15927
15928 else
15929 return Field;
15930 end if;
15931 end Copy_Field_With_Replacement;
15932
15933 -- Start of processing for Copy_Node_With_Replacement
15934
15935 begin
15936 if Old_Node <= Empty_Or_Error then
15937 return Old_Node;
15938
15939 elsif Has_Extension (Old_Node) then
15940 return Assoc (Old_Node);
15941
15942 else
15943 New_Node := New_Copy (Old_Node);
15944
15945 -- If the node we are copying is the associated node of a
15946 -- previously copied Itype, then adjust the associated node
15947 -- of the copy of that Itype accordingly.
15948
15949 if Present (Actual_Map) then
15950 declare
15951 E : Elmt_Id;
15952 Ent : Entity_Id;
15953
15954 begin
15955 -- Case of hash table used
15956
15957 if NCT_Hash_Tables_Used then
15958 Ent := NCT_Itype_Assoc.Get (Old_Node);
15959
15960 if Present (Ent) then
15961 Set_Associated_Node_For_Itype (Ent, New_Node);
15962 end if;
15963
15964 -- Case of no hash table used
15965
15966 else
15967 E := First_Elmt (Actual_Map);
15968 while Present (E) loop
15969 if Is_Itype (Node (E))
15970 and then
15971 Old_Node = Associated_Node_For_Itype (Node (E))
15972 then
15973 Set_Associated_Node_For_Itype
15974 (Node (Next_Elmt (E)), New_Node);
15975 end if;
15976
15977 E := Next_Elmt (Next_Elmt (E));
15978 end loop;
15979 end if;
15980 end;
15981 end if;
15982
15983 -- Recursively copy descendants
15984
15985 Set_Field1
15986 (New_Node, Copy_Field_With_Replacement (Field1 (New_Node)));
15987 Set_Field2
15988 (New_Node, Copy_Field_With_Replacement (Field2 (New_Node)));
15989 Set_Field3
15990 (New_Node, Copy_Field_With_Replacement (Field3 (New_Node)));
15991 Set_Field4
15992 (New_Node, Copy_Field_With_Replacement (Field4 (New_Node)));
15993 Set_Field5
15994 (New_Node, Copy_Field_With_Replacement (Field5 (New_Node)));
15995
15996 -- Adjust Sloc of new node if necessary
15997
15998 if New_Sloc /= No_Location then
15999 Set_Sloc (New_Node, New_Sloc);
16000
16001 -- If we adjust the Sloc, then we are essentially making a
16002 -- completely new node, so the Comes_From_Source flag should
16003 -- be reset to the proper default value.
16004
16005 Set_Comes_From_Source
16006 (New_Node, Default_Node.Comes_From_Source);
16007 end if;
16008
16009 -- If the node is a call and has named associations, set the
16010 -- corresponding links in the copy.
16011
16012 if Nkind_In (Old_Node, N_Entry_Call_Statement,
16013 N_Function_Call,
16014 N_Procedure_Call_Statement)
16015 and then Present (First_Named_Actual (Old_Node))
16016 then
16017 Adjust_Named_Associations (Old_Node, New_Node);
16018 end if;
16019
16020 -- Reset First_Real_Statement for Handled_Sequence_Of_Statements.
16021 -- The replacement mechanism applies to entities, and is not used
16022 -- here. Eventually we may need a more general graph-copying
16023 -- routine. For now, do a sequential search to find desired node.
16024
16025 if Nkind (Old_Node) = N_Handled_Sequence_Of_Statements
16026 and then Present (First_Real_Statement (Old_Node))
16027 then
16028 declare
16029 Old_F : constant Node_Id := First_Real_Statement (Old_Node);
16030 N1, N2 : Node_Id;
16031
16032 begin
16033 N1 := First (Statements (Old_Node));
16034 N2 := First (Statements (New_Node));
16035
16036 while N1 /= Old_F loop
16037 Next (N1);
16038 Next (N2);
16039 end loop;
16040
16041 Set_First_Real_Statement (New_Node, N2);
16042 end;
16043 end if;
16044 end if;
16045
16046 -- All done, return copied node
16047
16048 return New_Node;
16049 end Copy_Node_With_Replacement;
16050
16051 -----------------
16052 -- Visit_Elist --
16053 -----------------
16054
16055 procedure Visit_Elist (E : Elist_Id) is
16056 Elmt : Elmt_Id;
16057 begin
16058 if Present (E) then
16059 Elmt := First_Elmt (E);
16060
16061 while Elmt /= No_Elmt loop
16062 Visit_Node (Node (Elmt));
16063 Next_Elmt (Elmt);
16064 end loop;
16065 end if;
16066 end Visit_Elist;
16067
16068 -----------------
16069 -- Visit_Field --
16070 -----------------
16071
16072 procedure Visit_Field (F : Union_Id; N : Node_Id) is
16073 begin
16074 if F = Union_Id (Empty) then
16075 return;
16076
16077 elsif F in Node_Range then
16078
16079 -- Copy node if it is syntactic, i.e. its parent pointer is
16080 -- set to point to the field that referenced it (certain
16081 -- Itypes will also meet this criterion, which is fine, since
16082 -- these are clearly Itypes that do need to be copied, since
16083 -- we are copying their parent.)
16084
16085 if Parent (Node_Id (F)) = N then
16086 Visit_Node (Node_Id (F));
16087 return;
16088
16089 -- Another case, if we are pointing to an Itype, then we want
16090 -- to copy it if its associated node is somewhere in the tree
16091 -- being copied.
16092
16093 -- Note: the exclusion of self-referential copies is just an
16094 -- optimization, since the search of the already copied list
16095 -- would catch it, but it is a common case (Etype pointing
16096 -- to itself for an Itype that is a base type).
16097
16098 elsif Has_Extension (Node_Id (F))
16099 and then Is_Itype (Entity_Id (F))
16100 and then Node_Id (F) /= N
16101 then
16102 declare
16103 P : Node_Id;
16104
16105 begin
16106 P := Associated_Node_For_Itype (Node_Id (F));
16107 while Present (P) loop
16108 if P = Source then
16109 Visit_Node (Node_Id (F));
16110 return;
16111 else
16112 P := Parent (P);
16113 end if;
16114 end loop;
16115
16116 -- An Itype whose parent is not being copied definitely
16117 -- should NOT be copied, since it does not belong in any
16118 -- sense to the copied subtree.
16119
16120 return;
16121 end;
16122 end if;
16123
16124 elsif F in List_Range and then Parent (List_Id (F)) = N then
16125 Visit_List (List_Id (F));
16126 return;
16127 end if;
16128 end Visit_Field;
16129
16130 -----------------
16131 -- Visit_Itype --
16132 -----------------
16133
16134 procedure Visit_Itype (Old_Itype : Entity_Id) is
16135 New_Itype : Entity_Id;
16136 E : Elmt_Id;
16137 Ent : Entity_Id;
16138
16139 begin
16140 -- Itypes that describe the designated type of access to subprograms
16141 -- have the structure of subprogram declarations, with signatures,
16142 -- etc. Either we duplicate the signatures completely, or choose to
16143 -- share such itypes, which is fine because their elaboration will
16144 -- have no side effects.
16145
16146 if Ekind (Old_Itype) = E_Subprogram_Type then
16147 return;
16148 end if;
16149
16150 New_Itype := New_Copy (Old_Itype);
16151
16152 -- The new Itype has all the attributes of the old one, and
16153 -- we just copy the contents of the entity. However, the back-end
16154 -- needs different names for debugging purposes, so we create a
16155 -- new internal name for it in all cases.
16156
16157 Set_Chars (New_Itype, New_Internal_Name ('T'));
16158
16159 -- If our associated node is an entity that has already been copied,
16160 -- then set the associated node of the copy to point to the right
16161 -- copy. If we have copied an Itype that is itself the associated
16162 -- node of some previously copied Itype, then we set the right
16163 -- pointer in the other direction.
16164
16165 if Present (Actual_Map) then
16166
16167 -- Case of hash tables used
16168
16169 if NCT_Hash_Tables_Used then
16170
16171 Ent := NCT_Assoc.Get (Associated_Node_For_Itype (Old_Itype));
16172
16173 if Present (Ent) then
16174 Set_Associated_Node_For_Itype (New_Itype, Ent);
16175 end if;
16176
16177 Ent := NCT_Itype_Assoc.Get (Old_Itype);
16178 if Present (Ent) then
16179 Set_Associated_Node_For_Itype (Ent, New_Itype);
16180
16181 -- If the hash table has no association for this Itype and
16182 -- its associated node, enter one now.
16183
16184 else
16185 NCT_Itype_Assoc.Set
16186 (Associated_Node_For_Itype (Old_Itype), New_Itype);
16187 end if;
16188
16189 -- Case of hash tables not used
16190
16191 else
16192 E := First_Elmt (Actual_Map);
16193 while Present (E) loop
16194 if Associated_Node_For_Itype (Old_Itype) = Node (E) then
16195 Set_Associated_Node_For_Itype
16196 (New_Itype, Node (Next_Elmt (E)));
16197 end if;
16198
16199 if Is_Type (Node (E))
16200 and then Old_Itype = Associated_Node_For_Itype (Node (E))
16201 then
16202 Set_Associated_Node_For_Itype
16203 (Node (Next_Elmt (E)), New_Itype);
16204 end if;
16205
16206 E := Next_Elmt (Next_Elmt (E));
16207 end loop;
16208 end if;
16209 end if;
16210
16211 if Present (Freeze_Node (New_Itype)) then
16212 Set_Is_Frozen (New_Itype, False);
16213 Set_Freeze_Node (New_Itype, Empty);
16214 end if;
16215
16216 -- Add new association to map
16217
16218 if No (Actual_Map) then
16219 Actual_Map := New_Elmt_List;
16220 end if;
16221
16222 Append_Elmt (Old_Itype, Actual_Map);
16223 Append_Elmt (New_Itype, Actual_Map);
16224
16225 if NCT_Hash_Tables_Used then
16226 NCT_Assoc.Set (Old_Itype, New_Itype);
16227
16228 else
16229 NCT_Table_Entries := NCT_Table_Entries + 1;
16230
16231 if NCT_Table_Entries > NCT_Hash_Threshold then
16232 Build_NCT_Hash_Tables;
16233 end if;
16234 end if;
16235
16236 -- If a record subtype is simply copied, the entity list will be
16237 -- shared. Thus cloned_Subtype must be set to indicate the sharing.
16238
16239 if Ekind_In (Old_Itype, E_Record_Subtype, E_Class_Wide_Subtype) then
16240 Set_Cloned_Subtype (New_Itype, Old_Itype);
16241 end if;
16242
16243 -- Visit descendants that eventually get copied
16244
16245 Visit_Field (Union_Id (Etype (Old_Itype)), Old_Itype);
16246
16247 if Is_Discrete_Type (Old_Itype) then
16248 Visit_Field (Union_Id (Scalar_Range (Old_Itype)), Old_Itype);
16249
16250 elsif Has_Discriminants (Base_Type (Old_Itype)) then
16251 -- ??? This should involve call to Visit_Field
16252 Visit_Elist (Discriminant_Constraint (Old_Itype));
16253
16254 elsif Is_Array_Type (Old_Itype) then
16255 if Present (First_Index (Old_Itype)) then
16256 Visit_Field (Union_Id (List_Containing
16257 (First_Index (Old_Itype))),
16258 Old_Itype);
16259 end if;
16260
16261 if Is_Packed (Old_Itype) then
16262 Visit_Field (Union_Id (Packed_Array_Impl_Type (Old_Itype)),
16263 Old_Itype);
16264 end if;
16265 end if;
16266 end Visit_Itype;
16267
16268 ----------------
16269 -- Visit_List --
16270 ----------------
16271
16272 procedure Visit_List (L : List_Id) is
16273 N : Node_Id;
16274 begin
16275 if L /= No_List then
16276 N := First (L);
16277
16278 while Present (N) loop
16279 Visit_Node (N);
16280 Next (N);
16281 end loop;
16282 end if;
16283 end Visit_List;
16284
16285 ----------------
16286 -- Visit_Node --
16287 ----------------
16288
16289 procedure Visit_Node (N : Node_Or_Entity_Id) is
16290
16291 -- Start of processing for Visit_Node
16292
16293 begin
16294 -- Handle case of an Itype, which must be copied
16295
16296 if Has_Extension (N) and then Is_Itype (N) then
16297
16298 -- Nothing to do if already in the list. This can happen with an
16299 -- Itype entity that appears more than once in the tree.
16300 -- Note that we do not want to visit descendants in this case.
16301
16302 -- Test for already in list when hash table is used
16303
16304 if NCT_Hash_Tables_Used then
16305 if Present (NCT_Assoc.Get (Entity_Id (N))) then
16306 return;
16307 end if;
16308
16309 -- Test for already in list when hash table not used
16310
16311 else
16312 declare
16313 E : Elmt_Id;
16314 begin
16315 if Present (Actual_Map) then
16316 E := First_Elmt (Actual_Map);
16317 while Present (E) loop
16318 if Node (E) = N then
16319 return;
16320 else
16321 E := Next_Elmt (Next_Elmt (E));
16322 end if;
16323 end loop;
16324 end if;
16325 end;
16326 end if;
16327
16328 Visit_Itype (N);
16329 end if;
16330
16331 -- Visit descendants
16332
16333 Visit_Field (Field1 (N), N);
16334 Visit_Field (Field2 (N), N);
16335 Visit_Field (Field3 (N), N);
16336 Visit_Field (Field4 (N), N);
16337 Visit_Field (Field5 (N), N);
16338 end Visit_Node;
16339
16340 -- Start of processing for New_Copy_Tree
16341
16342 begin
16343 Actual_Map := Map;
16344
16345 -- See if we should use hash table
16346
16347 if No (Actual_Map) then
16348 NCT_Hash_Tables_Used := False;
16349
16350 else
16351 declare
16352 Elmt : Elmt_Id;
16353
16354 begin
16355 NCT_Table_Entries := 0;
16356
16357 Elmt := First_Elmt (Actual_Map);
16358 while Present (Elmt) loop
16359 NCT_Table_Entries := NCT_Table_Entries + 1;
16360 Next_Elmt (Elmt);
16361 Next_Elmt (Elmt);
16362 end loop;
16363
16364 if NCT_Table_Entries > NCT_Hash_Threshold then
16365 Build_NCT_Hash_Tables;
16366 else
16367 NCT_Hash_Tables_Used := False;
16368 end if;
16369 end;
16370 end if;
16371
16372 -- Hash table set up if required, now start phase one by visiting
16373 -- top node (we will recursively visit the descendants).
16374
16375 Visit_Node (Source);
16376
16377 -- Now the second phase of the copy can start. First we process
16378 -- all the mapped entities, copying their descendants.
16379
16380 if Present (Actual_Map) then
16381 declare
16382 Elmt : Elmt_Id;
16383 New_Itype : Entity_Id;
16384 begin
16385 Elmt := First_Elmt (Actual_Map);
16386 while Present (Elmt) loop
16387 Next_Elmt (Elmt);
16388 New_Itype := Node (Elmt);
16389
16390 if Is_Itype (New_Itype) then
16391 Copy_Itype_With_Replacement (New_Itype);
16392 end if;
16393 Next_Elmt (Elmt);
16394 end loop;
16395 end;
16396 end if;
16397
16398 -- Now we can copy the actual tree
16399
16400 return Copy_Node_With_Replacement (Source);
16401 end New_Copy_Tree;
16402
16403 -------------------------
16404 -- New_External_Entity --
16405 -------------------------
16406
16407 function New_External_Entity
16408 (Kind : Entity_Kind;
16409 Scope_Id : Entity_Id;
16410 Sloc_Value : Source_Ptr;
16411 Related_Id : Entity_Id;
16412 Suffix : Character;
16413 Suffix_Index : Nat := 0;
16414 Prefix : Character := ' ') return Entity_Id
16415 is
16416 N : constant Entity_Id :=
16417 Make_Defining_Identifier (Sloc_Value,
16418 New_External_Name
16419 (Chars (Related_Id), Suffix, Suffix_Index, Prefix));
16420
16421 begin
16422 Set_Ekind (N, Kind);
16423 Set_Is_Internal (N, True);
16424 Append_Entity (N, Scope_Id);
16425 Set_Public_Status (N);
16426
16427 if Kind in Type_Kind then
16428 Init_Size_Align (N);
16429 end if;
16430
16431 return N;
16432 end New_External_Entity;
16433
16434 -------------------------
16435 -- New_Internal_Entity --
16436 -------------------------
16437
16438 function New_Internal_Entity
16439 (Kind : Entity_Kind;
16440 Scope_Id : Entity_Id;
16441 Sloc_Value : Source_Ptr;
16442 Id_Char : Character) return Entity_Id
16443 is
16444 N : constant Entity_Id := Make_Temporary (Sloc_Value, Id_Char);
16445
16446 begin
16447 Set_Ekind (N, Kind);
16448 Set_Is_Internal (N, True);
16449 Append_Entity (N, Scope_Id);
16450
16451 if Kind in Type_Kind then
16452 Init_Size_Align (N);
16453 end if;
16454
16455 return N;
16456 end New_Internal_Entity;
16457
16458 -----------------
16459 -- Next_Actual --
16460 -----------------
16461
16462 function Next_Actual (Actual_Id : Node_Id) return Node_Id is
16463 N : Node_Id;
16464
16465 begin
16466 -- If we are pointing at a positional parameter, it is a member of a
16467 -- node list (the list of parameters), and the next parameter is the
16468 -- next node on the list, unless we hit a parameter association, then
16469 -- we shift to using the chain whose head is the First_Named_Actual in
16470 -- the parent, and then is threaded using the Next_Named_Actual of the
16471 -- Parameter_Association. All this fiddling is because the original node
16472 -- list is in the textual call order, and what we need is the
16473 -- declaration order.
16474
16475 if Is_List_Member (Actual_Id) then
16476 N := Next (Actual_Id);
16477
16478 if Nkind (N) = N_Parameter_Association then
16479 return First_Named_Actual (Parent (Actual_Id));
16480 else
16481 return N;
16482 end if;
16483
16484 else
16485 return Next_Named_Actual (Parent (Actual_Id));
16486 end if;
16487 end Next_Actual;
16488
16489 procedure Next_Actual (Actual_Id : in out Node_Id) is
16490 begin
16491 Actual_Id := Next_Actual (Actual_Id);
16492 end Next_Actual;
16493
16494 -----------------------
16495 -- Normalize_Actuals --
16496 -----------------------
16497
16498 -- Chain actuals according to formals of subprogram. If there are no named
16499 -- associations, the chain is simply the list of Parameter Associations,
16500 -- since the order is the same as the declaration order. If there are named
16501 -- associations, then the First_Named_Actual field in the N_Function_Call
16502 -- or N_Procedure_Call_Statement node points to the Parameter_Association
16503 -- node for the parameter that comes first in declaration order. The
16504 -- remaining named parameters are then chained in declaration order using
16505 -- Next_Named_Actual.
16506
16507 -- This routine also verifies that the number of actuals is compatible with
16508 -- the number and default values of formals, but performs no type checking
16509 -- (type checking is done by the caller).
16510
16511 -- If the matching succeeds, Success is set to True and the caller proceeds
16512 -- with type-checking. If the match is unsuccessful, then Success is set to
16513 -- False, and the caller attempts a different interpretation, if there is
16514 -- one.
16515
16516 -- If the flag Report is on, the call is not overloaded, and a failure to
16517 -- match can be reported here, rather than in the caller.
16518
16519 procedure Normalize_Actuals
16520 (N : Node_Id;
16521 S : Entity_Id;
16522 Report : Boolean;
16523 Success : out Boolean)
16524 is
16525 Actuals : constant List_Id := Parameter_Associations (N);
16526 Actual : Node_Id := Empty;
16527 Formal : Entity_Id;
16528 Last : Node_Id := Empty;
16529 First_Named : Node_Id := Empty;
16530 Found : Boolean;
16531
16532 Formals_To_Match : Integer := 0;
16533 Actuals_To_Match : Integer := 0;
16534
16535 procedure Chain (A : Node_Id);
16536 -- Add named actual at the proper place in the list, using the
16537 -- Next_Named_Actual link.
16538
16539 function Reporting return Boolean;
16540 -- Determines if an error is to be reported. To report an error, we
16541 -- need Report to be True, and also we do not report errors caused
16542 -- by calls to init procs that occur within other init procs. Such
16543 -- errors must always be cascaded errors, since if all the types are
16544 -- declared correctly, the compiler will certainly build decent calls.
16545
16546 -----------
16547 -- Chain --
16548 -----------
16549
16550 procedure Chain (A : Node_Id) is
16551 begin
16552 if No (Last) then
16553
16554 -- Call node points to first actual in list
16555
16556 Set_First_Named_Actual (N, Explicit_Actual_Parameter (A));
16557
16558 else
16559 Set_Next_Named_Actual (Last, Explicit_Actual_Parameter (A));
16560 end if;
16561
16562 Last := A;
16563 Set_Next_Named_Actual (Last, Empty);
16564 end Chain;
16565
16566 ---------------
16567 -- Reporting --
16568 ---------------
16569
16570 function Reporting return Boolean is
16571 begin
16572 if not Report then
16573 return False;
16574
16575 elsif not Within_Init_Proc then
16576 return True;
16577
16578 elsif Is_Init_Proc (Entity (Name (N))) then
16579 return False;
16580
16581 else
16582 return True;
16583 end if;
16584 end Reporting;
16585
16586 -- Start of processing for Normalize_Actuals
16587
16588 begin
16589 if Is_Access_Type (S) then
16590
16591 -- The name in the call is a function call that returns an access
16592 -- to subprogram. The designated type has the list of formals.
16593
16594 Formal := First_Formal (Designated_Type (S));
16595 else
16596 Formal := First_Formal (S);
16597 end if;
16598
16599 while Present (Formal) loop
16600 Formals_To_Match := Formals_To_Match + 1;
16601 Next_Formal (Formal);
16602 end loop;
16603
16604 -- Find if there is a named association, and verify that no positional
16605 -- associations appear after named ones.
16606
16607 if Present (Actuals) then
16608 Actual := First (Actuals);
16609 end if;
16610
16611 while Present (Actual)
16612 and then Nkind (Actual) /= N_Parameter_Association
16613 loop
16614 Actuals_To_Match := Actuals_To_Match + 1;
16615 Next (Actual);
16616 end loop;
16617
16618 if No (Actual) and Actuals_To_Match = Formals_To_Match then
16619
16620 -- Most common case: positional notation, no defaults
16621
16622 Success := True;
16623 return;
16624
16625 elsif Actuals_To_Match > Formals_To_Match then
16626
16627 -- Too many actuals: will not work
16628
16629 if Reporting then
16630 if Is_Entity_Name (Name (N)) then
16631 Error_Msg_N ("too many arguments in call to&", Name (N));
16632 else
16633 Error_Msg_N ("too many arguments in call", N);
16634 end if;
16635 end if;
16636
16637 Success := False;
16638 return;
16639 end if;
16640
16641 First_Named := Actual;
16642
16643 while Present (Actual) loop
16644 if Nkind (Actual) /= N_Parameter_Association then
16645 Error_Msg_N
16646 ("positional parameters not allowed after named ones", Actual);
16647 Success := False;
16648 return;
16649
16650 else
16651 Actuals_To_Match := Actuals_To_Match + 1;
16652 end if;
16653
16654 Next (Actual);
16655 end loop;
16656
16657 if Present (Actuals) then
16658 Actual := First (Actuals);
16659 end if;
16660
16661 Formal := First_Formal (S);
16662 while Present (Formal) loop
16663
16664 -- Match the formals in order. If the corresponding actual is
16665 -- positional, nothing to do. Else scan the list of named actuals
16666 -- to find the one with the right name.
16667
16668 if Present (Actual)
16669 and then Nkind (Actual) /= N_Parameter_Association
16670 then
16671 Next (Actual);
16672 Actuals_To_Match := Actuals_To_Match - 1;
16673 Formals_To_Match := Formals_To_Match - 1;
16674
16675 else
16676 -- For named parameters, search the list of actuals to find
16677 -- one that matches the next formal name.
16678
16679 Actual := First_Named;
16680 Found := False;
16681 while Present (Actual) loop
16682 if Chars (Selector_Name (Actual)) = Chars (Formal) then
16683 Found := True;
16684 Chain (Actual);
16685 Actuals_To_Match := Actuals_To_Match - 1;
16686 Formals_To_Match := Formals_To_Match - 1;
16687 exit;
16688 end if;
16689
16690 Next (Actual);
16691 end loop;
16692
16693 if not Found then
16694 if Ekind (Formal) /= E_In_Parameter
16695 or else No (Default_Value (Formal))
16696 then
16697 if Reporting then
16698 if (Comes_From_Source (S)
16699 or else Sloc (S) = Standard_Location)
16700 and then Is_Overloadable (S)
16701 then
16702 if No (Actuals)
16703 and then
16704 Nkind_In (Parent (N), N_Procedure_Call_Statement,
16705 N_Function_Call,
16706 N_Parameter_Association)
16707 and then Ekind (S) /= E_Function
16708 then
16709 Set_Etype (N, Etype (S));
16710
16711 else
16712 Error_Msg_Name_1 := Chars (S);
16713 Error_Msg_Sloc := Sloc (S);
16714 Error_Msg_NE
16715 ("missing argument for parameter & "
16716 & "in call to % declared #", N, Formal);
16717 end if;
16718
16719 elsif Is_Overloadable (S) then
16720 Error_Msg_Name_1 := Chars (S);
16721
16722 -- Point to type derivation that generated the
16723 -- operation.
16724
16725 Error_Msg_Sloc := Sloc (Parent (S));
16726
16727 Error_Msg_NE
16728 ("missing argument for parameter & "
16729 & "in call to % (inherited) #", N, Formal);
16730
16731 else
16732 Error_Msg_NE
16733 ("missing argument for parameter &", N, Formal);
16734 end if;
16735 end if;
16736
16737 Success := False;
16738 return;
16739
16740 else
16741 Formals_To_Match := Formals_To_Match - 1;
16742 end if;
16743 end if;
16744 end if;
16745
16746 Next_Formal (Formal);
16747 end loop;
16748
16749 if Formals_To_Match = 0 and then Actuals_To_Match = 0 then
16750 Success := True;
16751 return;
16752
16753 else
16754 if Reporting then
16755
16756 -- Find some superfluous named actual that did not get
16757 -- attached to the list of associations.
16758
16759 Actual := First (Actuals);
16760 while Present (Actual) loop
16761 if Nkind (Actual) = N_Parameter_Association
16762 and then Actual /= Last
16763 and then No (Next_Named_Actual (Actual))
16764 then
16765 Error_Msg_N ("unmatched actual & in call",
16766 Selector_Name (Actual));
16767 exit;
16768 end if;
16769
16770 Next (Actual);
16771 end loop;
16772 end if;
16773
16774 Success := False;
16775 return;
16776 end if;
16777 end Normalize_Actuals;
16778
16779 --------------------------------
16780 -- Note_Possible_Modification --
16781 --------------------------------
16782
16783 procedure Note_Possible_Modification (N : Node_Id; Sure : Boolean) is
16784 Modification_Comes_From_Source : constant Boolean :=
16785 Comes_From_Source (Parent (N));
16786
16787 Ent : Entity_Id;
16788 Exp : Node_Id;
16789
16790 begin
16791 -- Loop to find referenced entity, if there is one
16792
16793 Exp := N;
16794 loop
16795 Ent := Empty;
16796
16797 if Is_Entity_Name (Exp) then
16798 Ent := Entity (Exp);
16799
16800 -- If the entity is missing, it is an undeclared identifier,
16801 -- and there is nothing to annotate.
16802
16803 if No (Ent) then
16804 return;
16805 end if;
16806
16807 elsif Nkind (Exp) = N_Explicit_Dereference then
16808 declare
16809 P : constant Node_Id := Prefix (Exp);
16810
16811 begin
16812 -- In formal verification mode, keep track of all reads and
16813 -- writes through explicit dereferences.
16814
16815 if GNATprove_Mode then
16816 SPARK_Specific.Generate_Dereference (N, 'm');
16817 end if;
16818
16819 if Nkind (P) = N_Selected_Component
16820 and then Present (Entry_Formal (Entity (Selector_Name (P))))
16821 then
16822 -- Case of a reference to an entry formal
16823
16824 Ent := Entry_Formal (Entity (Selector_Name (P)));
16825
16826 elsif Nkind (P) = N_Identifier
16827 and then Nkind (Parent (Entity (P))) = N_Object_Declaration
16828 and then Present (Expression (Parent (Entity (P))))
16829 and then Nkind (Expression (Parent (Entity (P)))) =
16830 N_Reference
16831 then
16832 -- Case of a reference to a value on which side effects have
16833 -- been removed.
16834
16835 Exp := Prefix (Expression (Parent (Entity (P))));
16836 goto Continue;
16837
16838 else
16839 return;
16840 end if;
16841 end;
16842
16843 elsif Nkind_In (Exp, N_Type_Conversion,
16844 N_Unchecked_Type_Conversion)
16845 then
16846 Exp := Expression (Exp);
16847 goto Continue;
16848
16849 elsif Nkind_In (Exp, N_Slice,
16850 N_Indexed_Component,
16851 N_Selected_Component)
16852 then
16853 -- Special check, if the prefix is an access type, then return
16854 -- since we are modifying the thing pointed to, not the prefix.
16855 -- When we are expanding, most usually the prefix is replaced
16856 -- by an explicit dereference, and this test is not needed, but
16857 -- in some cases (notably -gnatc mode and generics) when we do
16858 -- not do full expansion, we need this special test.
16859
16860 if Is_Access_Type (Etype (Prefix (Exp))) then
16861 return;
16862
16863 -- Otherwise go to prefix and keep going
16864
16865 else
16866 Exp := Prefix (Exp);
16867 goto Continue;
16868 end if;
16869
16870 -- All other cases, not a modification
16871
16872 else
16873 return;
16874 end if;
16875
16876 -- Now look for entity being referenced
16877
16878 if Present (Ent) then
16879 if Is_Object (Ent) then
16880 if Comes_From_Source (Exp)
16881 or else Modification_Comes_From_Source
16882 then
16883 -- Give warning if pragma unmodified given and we are
16884 -- sure this is a modification.
16885
16886 if Has_Pragma_Unmodified (Ent) and then Sure then
16887 Error_Msg_NE ("??pragma Unmodified given for &!", N, Ent);
16888 end if;
16889
16890 Set_Never_Set_In_Source (Ent, False);
16891 end if;
16892
16893 Set_Is_True_Constant (Ent, False);
16894 Set_Current_Value (Ent, Empty);
16895 Set_Is_Known_Null (Ent, False);
16896
16897 if not Can_Never_Be_Null (Ent) then
16898 Set_Is_Known_Non_Null (Ent, False);
16899 end if;
16900
16901 -- Follow renaming chain
16902
16903 if (Ekind (Ent) = E_Variable or else Ekind (Ent) = E_Constant)
16904 and then Present (Renamed_Object (Ent))
16905 then
16906 Exp := Renamed_Object (Ent);
16907
16908 -- If the entity is the loop variable in an iteration over
16909 -- a container, retrieve container expression to indicate
16910 -- possible modification.
16911
16912 if Present (Related_Expression (Ent))
16913 and then Nkind (Parent (Related_Expression (Ent))) =
16914 N_Iterator_Specification
16915 then
16916 Exp := Original_Node (Related_Expression (Ent));
16917 end if;
16918
16919 goto Continue;
16920
16921 -- The expression may be the renaming of a subcomponent of an
16922 -- array or container. The assignment to the subcomponent is
16923 -- a modification of the container.
16924
16925 elsif Comes_From_Source (Original_Node (Exp))
16926 and then Nkind_In (Original_Node (Exp), N_Selected_Component,
16927 N_Indexed_Component)
16928 then
16929 Exp := Prefix (Original_Node (Exp));
16930 goto Continue;
16931 end if;
16932
16933 -- Generate a reference only if the assignment comes from
16934 -- source. This excludes, for example, calls to a dispatching
16935 -- assignment operation when the left-hand side is tagged. In
16936 -- GNATprove mode, we need those references also on generated
16937 -- code, as these are used to compute the local effects of
16938 -- subprograms.
16939
16940 if Modification_Comes_From_Source or GNATprove_Mode then
16941 Generate_Reference (Ent, Exp, 'm');
16942
16943 -- If the target of the assignment is the bound variable
16944 -- in an iterator, indicate that the corresponding array
16945 -- or container is also modified.
16946
16947 if Ada_Version >= Ada_2012
16948 and then Nkind (Parent (Ent)) = N_Iterator_Specification
16949 then
16950 declare
16951 Domain : constant Node_Id := Name (Parent (Ent));
16952
16953 begin
16954 -- TBD : in the full version of the construct, the
16955 -- domain of iteration can be given by an expression.
16956
16957 if Is_Entity_Name (Domain) then
16958 Generate_Reference (Entity (Domain), Exp, 'm');
16959 Set_Is_True_Constant (Entity (Domain), False);
16960 Set_Never_Set_In_Source (Entity (Domain), False);
16961 end if;
16962 end;
16963 end if;
16964 end if;
16965 end if;
16966
16967 Kill_Checks (Ent);
16968
16969 -- If we are sure this is a modification from source, and we know
16970 -- this modifies a constant, then give an appropriate warning.
16971
16972 if Sure
16973 and then Modification_Comes_From_Source
16974 and then Overlays_Constant (Ent)
16975 and then Address_Clause_Overlay_Warnings
16976 then
16977 declare
16978 Addr : constant Node_Id := Address_Clause (Ent);
16979 O_Ent : Entity_Id;
16980 Off : Boolean;
16981
16982 begin
16983 Find_Overlaid_Entity (Addr, O_Ent, Off);
16984
16985 Error_Msg_Sloc := Sloc (Addr);
16986 Error_Msg_NE
16987 ("??constant& may be modified via address clause#",
16988 N, O_Ent);
16989 end;
16990 end if;
16991
16992 return;
16993 end if;
16994
16995 <<Continue>>
16996 null;
16997 end loop;
16998 end Note_Possible_Modification;
16999
17000 -------------------------
17001 -- Object_Access_Level --
17002 -------------------------
17003
17004 -- Returns the static accessibility level of the view denoted by Obj. Note
17005 -- that the value returned is the result of a call to Scope_Depth. Only
17006 -- scope depths associated with dynamic scopes can actually be returned.
17007 -- Since only relative levels matter for accessibility checking, the fact
17008 -- that the distance between successive levels of accessibility is not
17009 -- always one is immaterial (invariant: if level(E2) is deeper than
17010 -- level(E1), then Scope_Depth(E1) < Scope_Depth(E2)).
17011
17012 function Object_Access_Level (Obj : Node_Id) return Uint is
17013 function Is_Interface_Conversion (N : Node_Id) return Boolean;
17014 -- Determine whether N is a construct of the form
17015 -- Some_Type (Operand._tag'Address)
17016 -- This construct appears in the context of dispatching calls.
17017
17018 function Reference_To (Obj : Node_Id) return Node_Id;
17019 -- An explicit dereference is created when removing side-effects from
17020 -- expressions for constraint checking purposes. In this case a local
17021 -- access type is created for it. The correct access level is that of
17022 -- the original source node. We detect this case by noting that the
17023 -- prefix of the dereference is created by an object declaration whose
17024 -- initial expression is a reference.
17025
17026 -----------------------------
17027 -- Is_Interface_Conversion --
17028 -----------------------------
17029
17030 function Is_Interface_Conversion (N : Node_Id) return Boolean is
17031 begin
17032 return Nkind (N) = N_Unchecked_Type_Conversion
17033 and then Nkind (Expression (N)) = N_Attribute_Reference
17034 and then Attribute_Name (Expression (N)) = Name_Address;
17035 end Is_Interface_Conversion;
17036
17037 ------------------
17038 -- Reference_To --
17039 ------------------
17040
17041 function Reference_To (Obj : Node_Id) return Node_Id is
17042 Pref : constant Node_Id := Prefix (Obj);
17043 begin
17044 if Is_Entity_Name (Pref)
17045 and then Nkind (Parent (Entity (Pref))) = N_Object_Declaration
17046 and then Present (Expression (Parent (Entity (Pref))))
17047 and then Nkind (Expression (Parent (Entity (Pref)))) = N_Reference
17048 then
17049 return (Prefix (Expression (Parent (Entity (Pref)))));
17050 else
17051 return Empty;
17052 end if;
17053 end Reference_To;
17054
17055 -- Local variables
17056
17057 E : Entity_Id;
17058
17059 -- Start of processing for Object_Access_Level
17060
17061 begin
17062 if Nkind (Obj) = N_Defining_Identifier
17063 or else Is_Entity_Name (Obj)
17064 then
17065 if Nkind (Obj) = N_Defining_Identifier then
17066 E := Obj;
17067 else
17068 E := Entity (Obj);
17069 end if;
17070
17071 if Is_Prival (E) then
17072 E := Prival_Link (E);
17073 end if;
17074
17075 -- If E is a type then it denotes a current instance. For this case
17076 -- we add one to the normal accessibility level of the type to ensure
17077 -- that current instances are treated as always being deeper than
17078 -- than the level of any visible named access type (see 3.10.2(21)).
17079
17080 if Is_Type (E) then
17081 return Type_Access_Level (E) + 1;
17082
17083 elsif Present (Renamed_Object (E)) then
17084 return Object_Access_Level (Renamed_Object (E));
17085
17086 -- Similarly, if E is a component of the current instance of a
17087 -- protected type, any instance of it is assumed to be at a deeper
17088 -- level than the type. For a protected object (whose type is an
17089 -- anonymous protected type) its components are at the same level
17090 -- as the type itself.
17091
17092 elsif not Is_Overloadable (E)
17093 and then Ekind (Scope (E)) = E_Protected_Type
17094 and then Comes_From_Source (Scope (E))
17095 then
17096 return Type_Access_Level (Scope (E)) + 1;
17097
17098 else
17099 -- Aliased formals of functions take their access level from the
17100 -- point of call, i.e. require a dynamic check. For static check
17101 -- purposes, this is smaller than the level of the subprogram
17102 -- itself. For procedures the aliased makes no difference.
17103
17104 if Is_Formal (E)
17105 and then Is_Aliased (E)
17106 and then Ekind (Scope (E)) = E_Function
17107 then
17108 return Type_Access_Level (Etype (E));
17109
17110 else
17111 return Scope_Depth (Enclosing_Dynamic_Scope (E));
17112 end if;
17113 end if;
17114
17115 elsif Nkind (Obj) = N_Selected_Component then
17116 if Is_Access_Type (Etype (Prefix (Obj))) then
17117 return Type_Access_Level (Etype (Prefix (Obj)));
17118 else
17119 return Object_Access_Level (Prefix (Obj));
17120 end if;
17121
17122 elsif Nkind (Obj) = N_Indexed_Component then
17123 if Is_Access_Type (Etype (Prefix (Obj))) then
17124 return Type_Access_Level (Etype (Prefix (Obj)));
17125 else
17126 return Object_Access_Level (Prefix (Obj));
17127 end if;
17128
17129 elsif Nkind (Obj) = N_Explicit_Dereference then
17130
17131 -- If the prefix is a selected access discriminant then we make a
17132 -- recursive call on the prefix, which will in turn check the level
17133 -- of the prefix object of the selected discriminant.
17134
17135 -- In Ada 2012, if the discriminant has implicit dereference and
17136 -- the context is a selected component, treat this as an object of
17137 -- unknown scope (see below). This is necessary in compile-only mode;
17138 -- otherwise expansion will already have transformed the prefix into
17139 -- a temporary.
17140
17141 if Nkind (Prefix (Obj)) = N_Selected_Component
17142 and then Ekind (Etype (Prefix (Obj))) = E_Anonymous_Access_Type
17143 and then
17144 Ekind (Entity (Selector_Name (Prefix (Obj)))) = E_Discriminant
17145 and then
17146 (not Has_Implicit_Dereference
17147 (Entity (Selector_Name (Prefix (Obj))))
17148 or else Nkind (Parent (Obj)) /= N_Selected_Component)
17149 then
17150 return Object_Access_Level (Prefix (Obj));
17151
17152 -- Detect an interface conversion in the context of a dispatching
17153 -- call. Use the original form of the conversion to find the access
17154 -- level of the operand.
17155
17156 elsif Is_Interface (Etype (Obj))
17157 and then Is_Interface_Conversion (Prefix (Obj))
17158 and then Nkind (Original_Node (Obj)) = N_Type_Conversion
17159 then
17160 return Object_Access_Level (Original_Node (Obj));
17161
17162 elsif not Comes_From_Source (Obj) then
17163 declare
17164 Ref : constant Node_Id := Reference_To (Obj);
17165 begin
17166 if Present (Ref) then
17167 return Object_Access_Level (Ref);
17168 else
17169 return Type_Access_Level (Etype (Prefix (Obj)));
17170 end if;
17171 end;
17172
17173 else
17174 return Type_Access_Level (Etype (Prefix (Obj)));
17175 end if;
17176
17177 elsif Nkind_In (Obj, N_Type_Conversion, N_Unchecked_Type_Conversion) then
17178 return Object_Access_Level (Expression (Obj));
17179
17180 elsif Nkind (Obj) = N_Function_Call then
17181
17182 -- Function results are objects, so we get either the access level of
17183 -- the function or, in the case of an indirect call, the level of the
17184 -- access-to-subprogram type. (This code is used for Ada 95, but it
17185 -- looks wrong, because it seems that we should be checking the level
17186 -- of the call itself, even for Ada 95. However, using the Ada 2005
17187 -- version of the code causes regressions in several tests that are
17188 -- compiled with -gnat95. ???)
17189
17190 if Ada_Version < Ada_2005 then
17191 if Is_Entity_Name (Name (Obj)) then
17192 return Subprogram_Access_Level (Entity (Name (Obj)));
17193 else
17194 return Type_Access_Level (Etype (Prefix (Name (Obj))));
17195 end if;
17196
17197 -- For Ada 2005, the level of the result object of a function call is
17198 -- defined to be the level of the call's innermost enclosing master.
17199 -- We determine that by querying the depth of the innermost enclosing
17200 -- dynamic scope.
17201
17202 else
17203 Return_Master_Scope_Depth_Of_Call : declare
17204
17205 function Innermost_Master_Scope_Depth
17206 (N : Node_Id) return Uint;
17207 -- Returns the scope depth of the given node's innermost
17208 -- enclosing dynamic scope (effectively the accessibility
17209 -- level of the innermost enclosing master).
17210
17211 ----------------------------------
17212 -- Innermost_Master_Scope_Depth --
17213 ----------------------------------
17214
17215 function Innermost_Master_Scope_Depth
17216 (N : Node_Id) return Uint
17217 is
17218 Node_Par : Node_Id := Parent (N);
17219
17220 begin
17221 -- Locate the nearest enclosing node (by traversing Parents)
17222 -- that Defining_Entity can be applied to, and return the
17223 -- depth of that entity's nearest enclosing dynamic scope.
17224
17225 while Present (Node_Par) loop
17226 case Nkind (Node_Par) is
17227 when N_Component_Declaration |
17228 N_Entry_Declaration |
17229 N_Formal_Object_Declaration |
17230 N_Formal_Type_Declaration |
17231 N_Full_Type_Declaration |
17232 N_Incomplete_Type_Declaration |
17233 N_Loop_Parameter_Specification |
17234 N_Object_Declaration |
17235 N_Protected_Type_Declaration |
17236 N_Private_Extension_Declaration |
17237 N_Private_Type_Declaration |
17238 N_Subtype_Declaration |
17239 N_Function_Specification |
17240 N_Procedure_Specification |
17241 N_Task_Type_Declaration |
17242 N_Body_Stub |
17243 N_Generic_Instantiation |
17244 N_Proper_Body |
17245 N_Implicit_Label_Declaration |
17246 N_Package_Declaration |
17247 N_Single_Task_Declaration |
17248 N_Subprogram_Declaration |
17249 N_Generic_Declaration |
17250 N_Renaming_Declaration |
17251 N_Block_Statement |
17252 N_Formal_Subprogram_Declaration |
17253 N_Abstract_Subprogram_Declaration |
17254 N_Entry_Body |
17255 N_Exception_Declaration |
17256 N_Formal_Package_Declaration |
17257 N_Number_Declaration |
17258 N_Package_Specification |
17259 N_Parameter_Specification |
17260 N_Single_Protected_Declaration |
17261 N_Subunit =>
17262
17263 return Scope_Depth
17264 (Nearest_Dynamic_Scope
17265 (Defining_Entity (Node_Par)));
17266
17267 when others =>
17268 null;
17269 end case;
17270
17271 Node_Par := Parent (Node_Par);
17272 end loop;
17273
17274 pragma Assert (False);
17275
17276 -- Should never reach the following return
17277
17278 return Scope_Depth (Current_Scope) + 1;
17279 end Innermost_Master_Scope_Depth;
17280
17281 -- Start of processing for Return_Master_Scope_Depth_Of_Call
17282
17283 begin
17284 return Innermost_Master_Scope_Depth (Obj);
17285 end Return_Master_Scope_Depth_Of_Call;
17286 end if;
17287
17288 -- For convenience we handle qualified expressions, even though they
17289 -- aren't technically object names.
17290
17291 elsif Nkind (Obj) = N_Qualified_Expression then
17292 return Object_Access_Level (Expression (Obj));
17293
17294 -- Ditto for aggregates. They have the level of the temporary that
17295 -- will hold their value.
17296
17297 elsif Nkind (Obj) = N_Aggregate then
17298 return Object_Access_Level (Current_Scope);
17299
17300 -- Otherwise return the scope level of Standard. (If there are cases
17301 -- that fall through to this point they will be treated as having
17302 -- global accessibility for now. ???)
17303
17304 else
17305 return Scope_Depth (Standard_Standard);
17306 end if;
17307 end Object_Access_Level;
17308
17309 ---------------------------------
17310 -- Original_Aspect_Pragma_Name --
17311 ---------------------------------
17312
17313 function Original_Aspect_Pragma_Name (N : Node_Id) return Name_Id is
17314 Item : Node_Id;
17315 Item_Nam : Name_Id;
17316
17317 begin
17318 pragma Assert (Nkind_In (N, N_Aspect_Specification, N_Pragma));
17319
17320 Item := N;
17321
17322 -- The pragma was generated to emulate an aspect, use the original
17323 -- aspect specification.
17324
17325 if Nkind (Item) = N_Pragma and then From_Aspect_Specification (Item) then
17326 Item := Corresponding_Aspect (Item);
17327 end if;
17328
17329 -- Retrieve the name of the aspect/pragma. Note that Pre, Pre_Class,
17330 -- Post and Post_Class rewrite their pragma identifier to preserve the
17331 -- original name.
17332 -- ??? this is kludgey
17333
17334 if Nkind (Item) = N_Pragma then
17335 Item_Nam := Chars (Original_Node (Pragma_Identifier (Item)));
17336
17337 else
17338 pragma Assert (Nkind (Item) = N_Aspect_Specification);
17339 Item_Nam := Chars (Identifier (Item));
17340 end if;
17341
17342 -- Deal with 'Class by converting the name to its _XXX form
17343
17344 if Class_Present (Item) then
17345 if Item_Nam = Name_Invariant then
17346 Item_Nam := Name_uInvariant;
17347
17348 elsif Item_Nam = Name_Post then
17349 Item_Nam := Name_uPost;
17350
17351 elsif Item_Nam = Name_Pre then
17352 Item_Nam := Name_uPre;
17353
17354 elsif Nam_In (Item_Nam, Name_Type_Invariant,
17355 Name_Type_Invariant_Class)
17356 then
17357 Item_Nam := Name_uType_Invariant;
17358
17359 -- Nothing to do for other cases (e.g. a Check that derived from
17360 -- Pre_Class and has the flag set). Also we do nothing if the name
17361 -- is already in special _xxx form.
17362
17363 end if;
17364 end if;
17365
17366 return Item_Nam;
17367 end Original_Aspect_Pragma_Name;
17368
17369 --------------------------------------
17370 -- Original_Corresponding_Operation --
17371 --------------------------------------
17372
17373 function Original_Corresponding_Operation (S : Entity_Id) return Entity_Id
17374 is
17375 Typ : constant Entity_Id := Find_Dispatching_Type (S);
17376
17377 begin
17378 -- If S is an inherited primitive S2 the original corresponding
17379 -- operation of S is the original corresponding operation of S2
17380
17381 if Present (Alias (S))
17382 and then Find_Dispatching_Type (Alias (S)) /= Typ
17383 then
17384 return Original_Corresponding_Operation (Alias (S));
17385
17386 -- If S overrides an inherited subprogram S2 the original corresponding
17387 -- operation of S is the original corresponding operation of S2
17388
17389 elsif Present (Overridden_Operation (S)) then
17390 return Original_Corresponding_Operation (Overridden_Operation (S));
17391
17392 -- otherwise it is S itself
17393
17394 else
17395 return S;
17396 end if;
17397 end Original_Corresponding_Operation;
17398
17399 ----------------------
17400 -- Policy_In_Effect --
17401 ----------------------
17402
17403 function Policy_In_Effect (Policy : Name_Id) return Name_Id is
17404 function Policy_In_List (List : Node_Id) return Name_Id;
17405 -- Determine the mode of a policy in a N_Pragma list
17406
17407 --------------------
17408 -- Policy_In_List --
17409 --------------------
17410
17411 function Policy_In_List (List : Node_Id) return Name_Id is
17412 Arg1 : Node_Id;
17413 Arg2 : Node_Id;
17414 Prag : Node_Id;
17415
17416 begin
17417 Prag := List;
17418 while Present (Prag) loop
17419 Arg1 := First (Pragma_Argument_Associations (Prag));
17420 Arg2 := Next (Arg1);
17421
17422 Arg1 := Get_Pragma_Arg (Arg1);
17423 Arg2 := Get_Pragma_Arg (Arg2);
17424
17425 -- The current Check_Policy pragma matches the requested policy or
17426 -- appears in the single argument form (Assertion, policy_id).
17427
17428 if Nam_In (Chars (Arg1), Name_Assertion, Policy) then
17429 return Chars (Arg2);
17430 end if;
17431
17432 Prag := Next_Pragma (Prag);
17433 end loop;
17434
17435 return No_Name;
17436 end Policy_In_List;
17437
17438 -- Local variables
17439
17440 Kind : Name_Id;
17441
17442 -- Start of processing for Policy_In_Effect
17443
17444 begin
17445 if not Is_Valid_Assertion_Kind (Policy) then
17446 raise Program_Error;
17447 end if;
17448
17449 -- Inspect all policy pragmas that appear within scopes (if any)
17450
17451 Kind := Policy_In_List (Check_Policy_List);
17452
17453 -- Inspect all configuration policy pragmas (if any)
17454
17455 if Kind = No_Name then
17456 Kind := Policy_In_List (Check_Policy_List_Config);
17457 end if;
17458
17459 -- The context lacks policy pragmas, determine the mode based on whether
17460 -- assertions are enabled at the configuration level. This ensures that
17461 -- the policy is preserved when analyzing generics.
17462
17463 if Kind = No_Name then
17464 if Assertions_Enabled_Config then
17465 Kind := Name_Check;
17466 else
17467 Kind := Name_Ignore;
17468 end if;
17469 end if;
17470
17471 return Kind;
17472 end Policy_In_Effect;
17473
17474 ----------------------------------
17475 -- Predicate_Tests_On_Arguments --
17476 ----------------------------------
17477
17478 function Predicate_Tests_On_Arguments (Subp : Entity_Id) return Boolean is
17479 begin
17480 -- Always test predicates on indirect call
17481
17482 if Ekind (Subp) = E_Subprogram_Type then
17483 return True;
17484
17485 -- Do not test predicates on call to generated default Finalize, since
17486 -- we are not interested in whether something we are finalizing (and
17487 -- typically destroying) satisfies its predicates.
17488
17489 elsif Chars (Subp) = Name_Finalize
17490 and then not Comes_From_Source (Subp)
17491 then
17492 return False;
17493
17494 -- Do not test predicates on any internally generated routines
17495
17496 elsif Is_Internal_Name (Chars (Subp)) then
17497 return False;
17498
17499 -- Do not test predicates on call to Init_Proc, since if needed the
17500 -- predicate test will occur at some other point.
17501
17502 elsif Is_Init_Proc (Subp) then
17503 return False;
17504
17505 -- Do not test predicates on call to predicate function, since this
17506 -- would cause infinite recursion.
17507
17508 elsif Ekind (Subp) = E_Function
17509 and then (Is_Predicate_Function (Subp)
17510 or else
17511 Is_Predicate_Function_M (Subp))
17512 then
17513 return False;
17514
17515 -- For now, no other exceptions
17516
17517 else
17518 return True;
17519 end if;
17520 end Predicate_Tests_On_Arguments;
17521
17522 -----------------------
17523 -- Private_Component --
17524 -----------------------
17525
17526 function Private_Component (Type_Id : Entity_Id) return Entity_Id is
17527 Ancestor : constant Entity_Id := Base_Type (Type_Id);
17528
17529 function Trace_Components
17530 (T : Entity_Id;
17531 Check : Boolean) return Entity_Id;
17532 -- Recursive function that does the work, and checks against circular
17533 -- definition for each subcomponent type.
17534
17535 ----------------------
17536 -- Trace_Components --
17537 ----------------------
17538
17539 function Trace_Components
17540 (T : Entity_Id;
17541 Check : Boolean) return Entity_Id
17542 is
17543 Btype : constant Entity_Id := Base_Type (T);
17544 Component : Entity_Id;
17545 P : Entity_Id;
17546 Candidate : Entity_Id := Empty;
17547
17548 begin
17549 if Check and then Btype = Ancestor then
17550 Error_Msg_N ("circular type definition", Type_Id);
17551 return Any_Type;
17552 end if;
17553
17554 if Is_Private_Type (Btype) and then not Is_Generic_Type (Btype) then
17555 if Present (Full_View (Btype))
17556 and then Is_Record_Type (Full_View (Btype))
17557 and then not Is_Frozen (Btype)
17558 then
17559 -- To indicate that the ancestor depends on a private type, the
17560 -- current Btype is sufficient. However, to check for circular
17561 -- definition we must recurse on the full view.
17562
17563 Candidate := Trace_Components (Full_View (Btype), True);
17564
17565 if Candidate = Any_Type then
17566 return Any_Type;
17567 else
17568 return Btype;
17569 end if;
17570
17571 else
17572 return Btype;
17573 end if;
17574
17575 elsif Is_Array_Type (Btype) then
17576 return Trace_Components (Component_Type (Btype), True);
17577
17578 elsif Is_Record_Type (Btype) then
17579 Component := First_Entity (Btype);
17580 while Present (Component)
17581 and then Comes_From_Source (Component)
17582 loop
17583 -- Skip anonymous types generated by constrained components
17584
17585 if not Is_Type (Component) then
17586 P := Trace_Components (Etype (Component), True);
17587
17588 if Present (P) then
17589 if P = Any_Type then
17590 return P;
17591 else
17592 Candidate := P;
17593 end if;
17594 end if;
17595 end if;
17596
17597 Next_Entity (Component);
17598 end loop;
17599
17600 return Candidate;
17601
17602 else
17603 return Empty;
17604 end if;
17605 end Trace_Components;
17606
17607 -- Start of processing for Private_Component
17608
17609 begin
17610 return Trace_Components (Type_Id, False);
17611 end Private_Component;
17612
17613 ---------------------------
17614 -- Primitive_Names_Match --
17615 ---------------------------
17616
17617 function Primitive_Names_Match (E1, E2 : Entity_Id) return Boolean is
17618
17619 function Non_Internal_Name (E : Entity_Id) return Name_Id;
17620 -- Given an internal name, returns the corresponding non-internal name
17621
17622 ------------------------
17623 -- Non_Internal_Name --
17624 ------------------------
17625
17626 function Non_Internal_Name (E : Entity_Id) return Name_Id is
17627 begin
17628 Get_Name_String (Chars (E));
17629 Name_Len := Name_Len - 1;
17630 return Name_Find;
17631 end Non_Internal_Name;
17632
17633 -- Start of processing for Primitive_Names_Match
17634
17635 begin
17636 pragma Assert (Present (E1) and then Present (E2));
17637
17638 return Chars (E1) = Chars (E2)
17639 or else
17640 (not Is_Internal_Name (Chars (E1))
17641 and then Is_Internal_Name (Chars (E2))
17642 and then Non_Internal_Name (E2) = Chars (E1))
17643 or else
17644 (not Is_Internal_Name (Chars (E2))
17645 and then Is_Internal_Name (Chars (E1))
17646 and then Non_Internal_Name (E1) = Chars (E2))
17647 or else
17648 (Is_Predefined_Dispatching_Operation (E1)
17649 and then Is_Predefined_Dispatching_Operation (E2)
17650 and then Same_TSS (E1, E2))
17651 or else
17652 (Is_Init_Proc (E1) and then Is_Init_Proc (E2));
17653 end Primitive_Names_Match;
17654
17655 -----------------------
17656 -- Process_End_Label --
17657 -----------------------
17658
17659 procedure Process_End_Label
17660 (N : Node_Id;
17661 Typ : Character;
17662 Ent : Entity_Id)
17663 is
17664 Loc : Source_Ptr;
17665 Nam : Node_Id;
17666 Scop : Entity_Id;
17667
17668 Label_Ref : Boolean;
17669 -- Set True if reference to end label itself is required
17670
17671 Endl : Node_Id;
17672 -- Gets set to the operator symbol or identifier that references the
17673 -- entity Ent. For the child unit case, this is the identifier from the
17674 -- designator. For other cases, this is simply Endl.
17675
17676 procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id);
17677 -- N is an identifier node that appears as a parent unit reference in
17678 -- the case where Ent is a child unit. This procedure generates an
17679 -- appropriate cross-reference entry. E is the corresponding entity.
17680
17681 -------------------------
17682 -- Generate_Parent_Ref --
17683 -------------------------
17684
17685 procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id) is
17686 begin
17687 -- If names do not match, something weird, skip reference
17688
17689 if Chars (E) = Chars (N) then
17690
17691 -- Generate the reference. We do NOT consider this as a reference
17692 -- for unreferenced symbol purposes.
17693
17694 Generate_Reference (E, N, 'r', Set_Ref => False, Force => True);
17695
17696 if Style_Check then
17697 Style.Check_Identifier (N, E);
17698 end if;
17699 end if;
17700 end Generate_Parent_Ref;
17701
17702 -- Start of processing for Process_End_Label
17703
17704 begin
17705 -- If no node, ignore. This happens in some error situations, and
17706 -- also for some internally generated structures where no end label
17707 -- references are required in any case.
17708
17709 if No (N) then
17710 return;
17711 end if;
17712
17713 -- Nothing to do if no End_Label, happens for internally generated
17714 -- constructs where we don't want an end label reference anyway. Also
17715 -- nothing to do if Endl is a string literal, which means there was
17716 -- some prior error (bad operator symbol)
17717
17718 Endl := End_Label (N);
17719
17720 if No (Endl) or else Nkind (Endl) = N_String_Literal then
17721 return;
17722 end if;
17723
17724 -- Reference node is not in extended main source unit
17725
17726 if not In_Extended_Main_Source_Unit (N) then
17727
17728 -- Generally we do not collect references except for the extended
17729 -- main source unit. The one exception is the 'e' entry for a
17730 -- package spec, where it is useful for a client to have the
17731 -- ending information to define scopes.
17732
17733 if Typ /= 'e' then
17734 return;
17735
17736 else
17737 Label_Ref := False;
17738
17739 -- For this case, we can ignore any parent references, but we
17740 -- need the package name itself for the 'e' entry.
17741
17742 if Nkind (Endl) = N_Designator then
17743 Endl := Identifier (Endl);
17744 end if;
17745 end if;
17746
17747 -- Reference is in extended main source unit
17748
17749 else
17750 Label_Ref := True;
17751
17752 -- For designator, generate references for the parent entries
17753
17754 if Nkind (Endl) = N_Designator then
17755
17756 -- Generate references for the prefix if the END line comes from
17757 -- source (otherwise we do not need these references) We climb the
17758 -- scope stack to find the expected entities.
17759
17760 if Comes_From_Source (Endl) then
17761 Nam := Name (Endl);
17762 Scop := Current_Scope;
17763 while Nkind (Nam) = N_Selected_Component loop
17764 Scop := Scope (Scop);
17765 exit when No (Scop);
17766 Generate_Parent_Ref (Selector_Name (Nam), Scop);
17767 Nam := Prefix (Nam);
17768 end loop;
17769
17770 if Present (Scop) then
17771 Generate_Parent_Ref (Nam, Scope (Scop));
17772 end if;
17773 end if;
17774
17775 Endl := Identifier (Endl);
17776 end if;
17777 end if;
17778
17779 -- If the end label is not for the given entity, then either we have
17780 -- some previous error, or this is a generic instantiation for which
17781 -- we do not need to make a cross-reference in this case anyway. In
17782 -- either case we simply ignore the call.
17783
17784 if Chars (Ent) /= Chars (Endl) then
17785 return;
17786 end if;
17787
17788 -- If label was really there, then generate a normal reference and then
17789 -- adjust the location in the end label to point past the name (which
17790 -- should almost always be the semicolon).
17791
17792 Loc := Sloc (Endl);
17793
17794 if Comes_From_Source (Endl) then
17795
17796 -- If a label reference is required, then do the style check and
17797 -- generate an l-type cross-reference entry for the label
17798
17799 if Label_Ref then
17800 if Style_Check then
17801 Style.Check_Identifier (Endl, Ent);
17802 end if;
17803
17804 Generate_Reference (Ent, Endl, 'l', Set_Ref => False);
17805 end if;
17806
17807 -- Set the location to point past the label (normally this will
17808 -- mean the semicolon immediately following the label). This is
17809 -- done for the sake of the 'e' or 't' entry generated below.
17810
17811 Get_Decoded_Name_String (Chars (Endl));
17812 Set_Sloc (Endl, Sloc (Endl) + Source_Ptr (Name_Len));
17813
17814 else
17815 -- In SPARK mode, no missing label is allowed for packages and
17816 -- subprogram bodies. Detect those cases by testing whether
17817 -- Process_End_Label was called for a body (Typ = 't') or a package.
17818
17819 if Restriction_Check_Required (SPARK_05)
17820 and then (Typ = 't' or else Ekind (Ent) = E_Package)
17821 then
17822 Error_Msg_Node_1 := Endl;
17823 Check_SPARK_05_Restriction
17824 ("`END &` required", Endl, Force => True);
17825 end if;
17826 end if;
17827
17828 -- Now generate the e/t reference
17829
17830 Generate_Reference (Ent, Endl, Typ, Set_Ref => False, Force => True);
17831
17832 -- Restore Sloc, in case modified above, since we have an identifier
17833 -- and the normal Sloc should be left set in the tree.
17834
17835 Set_Sloc (Endl, Loc);
17836 end Process_End_Label;
17837
17838 ---------------------------------------
17839 -- Record_Possible_Part_Of_Reference --
17840 ---------------------------------------
17841
17842 procedure Record_Possible_Part_Of_Reference
17843 (Var_Id : Entity_Id;
17844 Ref : Node_Id)
17845 is
17846 Encap : constant Entity_Id := Encapsulating_State (Var_Id);
17847 Refs : Elist_Id;
17848
17849 begin
17850 -- The variable is a constituent of a single protected/task type. Such
17851 -- a variable acts as a component of the type and must appear within a
17852 -- specific region (SPARK RM 9.3). Instead of recording the reference,
17853 -- verify its legality now.
17854
17855 if Present (Encap) and then Is_Single_Concurrent_Object (Encap) then
17856 Check_Part_Of_Reference (Var_Id, Ref);
17857
17858 -- The variable is subject to pragma Part_Of and may eventually become a
17859 -- constituent of a single protected/task type. Record the reference to
17860 -- verify its placement when the contract of the variable is analyzed.
17861
17862 elsif Present (Get_Pragma (Var_Id, Pragma_Part_Of)) then
17863 Refs := Part_Of_References (Var_Id);
17864
17865 if No (Refs) then
17866 Refs := New_Elmt_List;
17867 Set_Part_Of_References (Var_Id, Refs);
17868 end if;
17869
17870 Append_Elmt (Ref, Refs);
17871 end if;
17872 end Record_Possible_Part_Of_Reference;
17873
17874 ----------------
17875 -- Referenced --
17876 ----------------
17877
17878 function Referenced (Id : Entity_Id; Expr : Node_Id) return Boolean is
17879 Seen : Boolean := False;
17880
17881 function Is_Reference (N : Node_Id) return Traverse_Result;
17882 -- Determine whether node N denotes a reference to Id. If this is the
17883 -- case, set global flag Seen to True and stop the traversal.
17884
17885 ------------------
17886 -- Is_Reference --
17887 ------------------
17888
17889 function Is_Reference (N : Node_Id) return Traverse_Result is
17890 begin
17891 if Is_Entity_Name (N)
17892 and then Present (Entity (N))
17893 and then Entity (N) = Id
17894 then
17895 Seen := True;
17896 return Abandon;
17897 else
17898 return OK;
17899 end if;
17900 end Is_Reference;
17901
17902 procedure Inspect_Expression is new Traverse_Proc (Is_Reference);
17903
17904 -- Start of processing for Referenced
17905
17906 begin
17907 Inspect_Expression (Expr);
17908 return Seen;
17909 end Referenced;
17910
17911 ------------------------------------
17912 -- References_Generic_Formal_Type --
17913 ------------------------------------
17914
17915 function References_Generic_Formal_Type (N : Node_Id) return Boolean is
17916
17917 function Process (N : Node_Id) return Traverse_Result;
17918 -- Process one node in search for generic formal type
17919
17920 -------------
17921 -- Process --
17922 -------------
17923
17924 function Process (N : Node_Id) return Traverse_Result is
17925 begin
17926 if Nkind (N) in N_Has_Entity then
17927 declare
17928 E : constant Entity_Id := Entity (N);
17929 begin
17930 if Present (E) then
17931 if Is_Generic_Type (E) then
17932 return Abandon;
17933 elsif Present (Etype (E))
17934 and then Is_Generic_Type (Etype (E))
17935 then
17936 return Abandon;
17937 end if;
17938 end if;
17939 end;
17940 end if;
17941
17942 return Atree.OK;
17943 end Process;
17944
17945 function Traverse is new Traverse_Func (Process);
17946 -- Traverse tree to look for generic type
17947
17948 begin
17949 if Inside_A_Generic then
17950 return Traverse (N) = Abandon;
17951 else
17952 return False;
17953 end if;
17954 end References_Generic_Formal_Type;
17955
17956 --------------------
17957 -- Remove_Homonym --
17958 --------------------
17959
17960 procedure Remove_Homonym (E : Entity_Id) is
17961 Prev : Entity_Id := Empty;
17962 H : Entity_Id;
17963
17964 begin
17965 if E = Current_Entity (E) then
17966 if Present (Homonym (E)) then
17967 Set_Current_Entity (Homonym (E));
17968 else
17969 Set_Name_Entity_Id (Chars (E), Empty);
17970 end if;
17971
17972 else
17973 H := Current_Entity (E);
17974 while Present (H) and then H /= E loop
17975 Prev := H;
17976 H := Homonym (H);
17977 end loop;
17978
17979 -- If E is not on the homonym chain, nothing to do
17980
17981 if Present (H) then
17982 Set_Homonym (Prev, Homonym (E));
17983 end if;
17984 end if;
17985 end Remove_Homonym;
17986
17987 ------------------------------
17988 -- Remove_Overloaded_Entity --
17989 ------------------------------
17990
17991 procedure Remove_Overloaded_Entity (Id : Entity_Id) is
17992 procedure Remove_Primitive_Of (Typ : Entity_Id);
17993 -- Remove primitive subprogram Id from the list of primitives that
17994 -- belong to type Typ.
17995
17996 -------------------------
17997 -- Remove_Primitive_Of --
17998 -------------------------
17999
18000 procedure Remove_Primitive_Of (Typ : Entity_Id) is
18001 Prims : Elist_Id;
18002
18003 begin
18004 if Is_Tagged_Type (Typ) then
18005 Prims := Direct_Primitive_Operations (Typ);
18006
18007 if Present (Prims) then
18008 Remove (Prims, Id);
18009 end if;
18010 end if;
18011 end Remove_Primitive_Of;
18012
18013 -- Local variables
18014
18015 Scop : constant Entity_Id := Scope (Id);
18016 Formal : Entity_Id;
18017 Prev_Id : Entity_Id;
18018
18019 -- Start of processing for Remove_Overloaded_Entity
18020
18021 begin
18022 -- Remove the entity from the homonym chain. When the entity is the
18023 -- head of the chain, associate the entry in the name table with its
18024 -- homonym effectively making it the new head of the chain.
18025
18026 if Current_Entity (Id) = Id then
18027 Set_Name_Entity_Id (Chars (Id), Homonym (Id));
18028
18029 -- Otherwise link the previous and next homonyms
18030
18031 else
18032 Prev_Id := Current_Entity (Id);
18033 while Present (Prev_Id) and then Homonym (Prev_Id) /= Id loop
18034 Prev_Id := Homonym (Prev_Id);
18035 end loop;
18036
18037 Set_Homonym (Prev_Id, Homonym (Id));
18038 end if;
18039
18040 -- Remove the entity from the scope entity chain. When the entity is
18041 -- the head of the chain, set the next entity as the new head of the
18042 -- chain.
18043
18044 if First_Entity (Scop) = Id then
18045 Prev_Id := Empty;
18046 Set_First_Entity (Scop, Next_Entity (Id));
18047
18048 -- Otherwise the entity is either in the middle of the chain or it acts
18049 -- as its tail. Traverse and link the previous and next entities.
18050
18051 else
18052 Prev_Id := First_Entity (Scop);
18053 while Present (Prev_Id) and then Next_Entity (Prev_Id) /= Id loop
18054 Next_Entity (Prev_Id);
18055 end loop;
18056
18057 Set_Next_Entity (Prev_Id, Next_Entity (Id));
18058 end if;
18059
18060 -- Handle the case where the entity acts as the tail of the scope entity
18061 -- chain.
18062
18063 if Last_Entity (Scop) = Id then
18064 Set_Last_Entity (Scop, Prev_Id);
18065 end if;
18066
18067 -- The entity denotes a primitive subprogram. Remove it from the list of
18068 -- primitives of the associated controlling type.
18069
18070 if Ekind_In (Id, E_Function, E_Procedure) and then Is_Primitive (Id) then
18071 Formal := First_Formal (Id);
18072 while Present (Formal) loop
18073 if Is_Controlling_Formal (Formal) then
18074 Remove_Primitive_Of (Etype (Formal));
18075 exit;
18076 end if;
18077
18078 Next_Formal (Formal);
18079 end loop;
18080
18081 if Ekind (Id) = E_Function and then Has_Controlling_Result (Id) then
18082 Remove_Primitive_Of (Etype (Id));
18083 end if;
18084 end if;
18085 end Remove_Overloaded_Entity;
18086
18087 ---------------------
18088 -- Rep_To_Pos_Flag --
18089 ---------------------
18090
18091 function Rep_To_Pos_Flag (E : Entity_Id; Loc : Source_Ptr) return Node_Id is
18092 begin
18093 return New_Occurrence_Of
18094 (Boolean_Literals (not Range_Checks_Suppressed (E)), Loc);
18095 end Rep_To_Pos_Flag;
18096
18097 --------------------
18098 -- Require_Entity --
18099 --------------------
18100
18101 procedure Require_Entity (N : Node_Id) is
18102 begin
18103 if Is_Entity_Name (N) and then No (Entity (N)) then
18104 if Total_Errors_Detected /= 0 then
18105 Set_Entity (N, Any_Id);
18106 else
18107 raise Program_Error;
18108 end if;
18109 end if;
18110 end Require_Entity;
18111
18112 -------------------------------
18113 -- Requires_State_Refinement --
18114 -------------------------------
18115
18116 function Requires_State_Refinement
18117 (Spec_Id : Entity_Id;
18118 Body_Id : Entity_Id) return Boolean
18119 is
18120 function Mode_Is_Off (Prag : Node_Id) return Boolean;
18121 -- Given pragma SPARK_Mode, determine whether the mode is Off
18122
18123 -----------------
18124 -- Mode_Is_Off --
18125 -----------------
18126
18127 function Mode_Is_Off (Prag : Node_Id) return Boolean is
18128 Mode : Node_Id;
18129
18130 begin
18131 -- The default SPARK mode is On
18132
18133 if No (Prag) then
18134 return False;
18135 end if;
18136
18137 Mode := Get_Pragma_Arg (First (Pragma_Argument_Associations (Prag)));
18138
18139 -- Then the pragma lacks an argument, the default mode is On
18140
18141 if No (Mode) then
18142 return False;
18143 else
18144 return Chars (Mode) = Name_Off;
18145 end if;
18146 end Mode_Is_Off;
18147
18148 -- Start of processing for Requires_State_Refinement
18149
18150 begin
18151 -- A package that does not define at least one abstract state cannot
18152 -- possibly require refinement.
18153
18154 if No (Abstract_States (Spec_Id)) then
18155 return False;
18156
18157 -- The package instroduces a single null state which does not merit
18158 -- refinement.
18159
18160 elsif Has_Null_Abstract_State (Spec_Id) then
18161 return False;
18162
18163 -- Check whether the package body is subject to pragma SPARK_Mode. If
18164 -- it is and the mode is Off, the package body is considered to be in
18165 -- regular Ada and does not require refinement.
18166
18167 elsif Mode_Is_Off (SPARK_Pragma (Body_Id)) then
18168 return False;
18169
18170 -- The body's SPARK_Mode may be inherited from a similar pragma that
18171 -- appears in the private declarations of the spec. The pragma we are
18172 -- interested appears as the second entry in SPARK_Pragma.
18173
18174 elsif Present (SPARK_Pragma (Spec_Id))
18175 and then Mode_Is_Off (Next_Pragma (SPARK_Pragma (Spec_Id)))
18176 then
18177 return False;
18178
18179 -- The spec defines at least one abstract state and the body has no way
18180 -- of circumventing the refinement.
18181
18182 else
18183 return True;
18184 end if;
18185 end Requires_State_Refinement;
18186
18187 ------------------------------
18188 -- Requires_Transient_Scope --
18189 ------------------------------
18190
18191 -- A transient scope is required when variable-sized temporaries are
18192 -- allocated on the secondary stack, or when finalization actions must be
18193 -- generated before the next instruction.
18194
18195 function Old_Requires_Transient_Scope (Id : Entity_Id) return Boolean;
18196 function New_Requires_Transient_Scope (Id : Entity_Id) return Boolean;
18197 -- ???We retain the old and new algorithms for Requires_Transient_Scope for
18198 -- the time being. New_Requires_Transient_Scope is used by default; the
18199 -- debug switch -gnatdQ can be used to do Old_Requires_Transient_Scope
18200 -- instead. The intent is to use this temporarily to measure before/after
18201 -- efficiency. Note: when this temporary code is removed, the documentation
18202 -- of dQ in debug.adb should be removed.
18203
18204 procedure Results_Differ (Id : Entity_Id);
18205 -- ???Debugging code. Called when the Old_ and New_ results differ. Will be
18206 -- removed when New_Requires_Transient_Scope becomes
18207 -- Requires_Transient_Scope and Old_Requires_Transient_Scope is eliminated.
18208
18209 procedure Results_Differ (Id : Entity_Id) is
18210 begin
18211 if False then -- False to disable; True for debugging
18212 Treepr.Print_Tree_Node (Id);
18213
18214 if Old_Requires_Transient_Scope (Id) =
18215 New_Requires_Transient_Scope (Id)
18216 then
18217 raise Program_Error;
18218 end if;
18219 end if;
18220 end Results_Differ;
18221
18222 function Requires_Transient_Scope (Id : Entity_Id) return Boolean is
18223 Old_Result : constant Boolean := Old_Requires_Transient_Scope (Id);
18224
18225 begin
18226 if Debug_Flag_QQ then
18227 return Old_Result;
18228 end if;
18229
18230 declare
18231 New_Result : constant Boolean := New_Requires_Transient_Scope (Id);
18232
18233 begin
18234 -- Assert that we're not putting things on the secondary stack if we
18235 -- didn't before; we are trying to AVOID secondary stack when
18236 -- possible.
18237
18238 if not Old_Result then
18239 pragma Assert (not New_Result);
18240 null;
18241 end if;
18242
18243 if New_Result /= Old_Result then
18244 Results_Differ (Id);
18245 end if;
18246
18247 return New_Result;
18248 end;
18249 end Requires_Transient_Scope;
18250
18251 ----------------------------------
18252 -- Old_Requires_Transient_Scope --
18253 ----------------------------------
18254
18255 function Old_Requires_Transient_Scope (Id : Entity_Id) return Boolean is
18256 Typ : constant Entity_Id := Underlying_Type (Id);
18257
18258 begin
18259 -- This is a private type which is not completed yet. This can only
18260 -- happen in a default expression (of a formal parameter or of a
18261 -- record component). Do not expand transient scope in this case.
18262
18263 if No (Typ) then
18264 return False;
18265
18266 -- Do not expand transient scope for non-existent procedure return
18267
18268 elsif Typ = Standard_Void_Type then
18269 return False;
18270
18271 -- Elementary types do not require a transient scope
18272
18273 elsif Is_Elementary_Type (Typ) then
18274 return False;
18275
18276 -- Generally, indefinite subtypes require a transient scope, since the
18277 -- back end cannot generate temporaries, since this is not a valid type
18278 -- for declaring an object. It might be possible to relax this in the
18279 -- future, e.g. by declaring the maximum possible space for the type.
18280
18281 elsif not Is_Definite_Subtype (Typ) then
18282 return True;
18283
18284 -- Functions returning tagged types may dispatch on result so their
18285 -- returned value is allocated on the secondary stack. Controlled
18286 -- type temporaries need finalization.
18287
18288 elsif Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
18289 return True;
18290
18291 -- Record type
18292
18293 elsif Is_Record_Type (Typ) then
18294 declare
18295 Comp : Entity_Id;
18296
18297 begin
18298 Comp := First_Entity (Typ);
18299 while Present (Comp) loop
18300 if Ekind (Comp) = E_Component then
18301
18302 -- ???It's not clear we need a full recursive call to
18303 -- Old_Requires_Transient_Scope here. Note that the
18304 -- following can't happen.
18305
18306 pragma Assert (Is_Definite_Subtype (Etype (Comp)));
18307 pragma Assert (not Has_Controlled_Component (Etype (Comp)));
18308
18309 if Old_Requires_Transient_Scope (Etype (Comp)) then
18310 return True;
18311 end if;
18312 end if;
18313
18314 Next_Entity (Comp);
18315 end loop;
18316 end;
18317
18318 return False;
18319
18320 -- String literal types never require transient scope
18321
18322 elsif Ekind (Typ) = E_String_Literal_Subtype then
18323 return False;
18324
18325 -- Array type. Note that we already know that this is a constrained
18326 -- array, since unconstrained arrays will fail the indefinite test.
18327
18328 elsif Is_Array_Type (Typ) then
18329
18330 -- If component type requires a transient scope, the array does too
18331
18332 if Old_Requires_Transient_Scope (Component_Type (Typ)) then
18333 return True;
18334
18335 -- Otherwise, we only need a transient scope if the size depends on
18336 -- the value of one or more discriminants.
18337
18338 else
18339 return Size_Depends_On_Discriminant (Typ);
18340 end if;
18341
18342 -- All other cases do not require a transient scope
18343
18344 else
18345 pragma Assert (Is_Protected_Type (Typ) or else Is_Task_Type (Typ));
18346 return False;
18347 end if;
18348 end Old_Requires_Transient_Scope;
18349
18350 ----------------------------------
18351 -- New_Requires_Transient_Scope --
18352 ----------------------------------
18353
18354 function New_Requires_Transient_Scope (Id : Entity_Id) return Boolean is
18355
18356 function Caller_Known_Size_Record (Typ : Entity_Id) return Boolean;
18357 -- This is called for untagged records and protected types, with
18358 -- nondefaulted discriminants. Returns True if the size of function
18359 -- results is known at the call site, False otherwise. Returns False
18360 -- if there is a variant part that depends on the discriminants of
18361 -- this type, or if there is an array constrained by the discriminants
18362 -- of this type. ???Currently, this is overly conservative (the array
18363 -- could be nested inside some other record that is constrained by
18364 -- nondiscriminants). That is, the recursive calls are too conservative.
18365
18366 function Large_Max_Size_Mutable (Typ : Entity_Id) return Boolean;
18367 -- Returns True if Typ is a nonlimited record with defaulted
18368 -- discriminants whose max size makes it unsuitable for allocating on
18369 -- the primary stack.
18370
18371 ------------------------------
18372 -- Caller_Known_Size_Record --
18373 ------------------------------
18374
18375 function Caller_Known_Size_Record (Typ : Entity_Id) return Boolean is
18376 pragma Assert (Typ = Underlying_Type (Typ));
18377
18378 begin
18379 if Has_Variant_Part (Typ) and then not Is_Definite_Subtype (Typ) then
18380 return False;
18381 end if;
18382
18383 declare
18384 Comp : Entity_Id;
18385
18386 begin
18387 Comp := First_Entity (Typ);
18388 while Present (Comp) loop
18389
18390 -- Only look at E_Component entities. No need to look at
18391 -- E_Discriminant entities, and we must ignore internal
18392 -- subtypes generated for constrained components.
18393
18394 if Ekind (Comp) = E_Component then
18395 declare
18396 Comp_Type : constant Entity_Id :=
18397 Underlying_Type (Etype (Comp));
18398
18399 begin
18400 if Is_Record_Type (Comp_Type)
18401 or else
18402 Is_Protected_Type (Comp_Type)
18403 then
18404 if not Caller_Known_Size_Record (Comp_Type) then
18405 return False;
18406 end if;
18407
18408 elsif Is_Array_Type (Comp_Type) then
18409 if Size_Depends_On_Discriminant (Comp_Type) then
18410 return False;
18411 end if;
18412 end if;
18413 end;
18414 end if;
18415
18416 Next_Entity (Comp);
18417 end loop;
18418 end;
18419
18420 return True;
18421 end Caller_Known_Size_Record;
18422
18423 ------------------------------
18424 -- Large_Max_Size_Mutable --
18425 ------------------------------
18426
18427 function Large_Max_Size_Mutable (Typ : Entity_Id) return Boolean is
18428 pragma Assert (Typ = Underlying_Type (Typ));
18429
18430 function Is_Large_Discrete_Type (T : Entity_Id) return Boolean;
18431 -- Returns true if the discrete type T has a large range
18432
18433 ----------------------------
18434 -- Is_Large_Discrete_Type --
18435 ----------------------------
18436
18437 function Is_Large_Discrete_Type (T : Entity_Id) return Boolean is
18438 Threshold : constant Int := 16;
18439 -- Arbitrary threshold above which we consider it "large". We want
18440 -- a fairly large threshold, because these large types really
18441 -- shouldn't have default discriminants in the first place, in
18442 -- most cases.
18443
18444 begin
18445 return UI_To_Int (RM_Size (T)) > Threshold;
18446 end Is_Large_Discrete_Type;
18447
18448 begin
18449 if Is_Record_Type (Typ)
18450 and then not Is_Limited_View (Typ)
18451 and then Has_Defaulted_Discriminants (Typ)
18452 then
18453 -- Loop through the components, looking for an array whose upper
18454 -- bound(s) depends on discriminants, where both the subtype of
18455 -- the discriminant and the index subtype are too large.
18456
18457 declare
18458 Comp : Entity_Id;
18459
18460 begin
18461 Comp := First_Entity (Typ);
18462 while Present (Comp) loop
18463 if Ekind (Comp) = E_Component then
18464 declare
18465 Comp_Type : constant Entity_Id :=
18466 Underlying_Type (Etype (Comp));
18467 Indx : Node_Id;
18468 Ityp : Entity_Id;
18469 Hi : Node_Id;
18470
18471 begin
18472 if Is_Array_Type (Comp_Type) then
18473 Indx := First_Index (Comp_Type);
18474
18475 while Present (Indx) loop
18476 Ityp := Etype (Indx);
18477 Hi := Type_High_Bound (Ityp);
18478
18479 if Nkind (Hi) = N_Identifier
18480 and then Ekind (Entity (Hi)) = E_Discriminant
18481 and then Is_Large_Discrete_Type (Ityp)
18482 and then Is_Large_Discrete_Type
18483 (Etype (Entity (Hi)))
18484 then
18485 return True;
18486 end if;
18487
18488 Next_Index (Indx);
18489 end loop;
18490 end if;
18491 end;
18492 end if;
18493
18494 Next_Entity (Comp);
18495 end loop;
18496 end;
18497 end if;
18498
18499 return False;
18500 end Large_Max_Size_Mutable;
18501
18502 -- Local declarations
18503
18504 Typ : constant Entity_Id := Underlying_Type (Id);
18505
18506 -- Start of processing for New_Requires_Transient_Scope
18507
18508 begin
18509 -- This is a private type which is not completed yet. This can only
18510 -- happen in a default expression (of a formal parameter or of a
18511 -- record component). Do not expand transient scope in this case.
18512
18513 if No (Typ) then
18514 return False;
18515
18516 -- Do not expand transient scope for non-existent procedure return or
18517 -- string literal types.
18518
18519 elsif Typ = Standard_Void_Type
18520 or else Ekind (Typ) = E_String_Literal_Subtype
18521 then
18522 return False;
18523
18524 -- If Typ is a generic formal incomplete type, then we want to look at
18525 -- the actual type.
18526
18527 elsif Ekind (Typ) = E_Record_Subtype
18528 and then Present (Cloned_Subtype (Typ))
18529 then
18530 return New_Requires_Transient_Scope (Cloned_Subtype (Typ));
18531
18532 -- Functions returning specific tagged types may dispatch on result, so
18533 -- their returned value is allocated on the secondary stack, even in the
18534 -- definite case. We must treat nondispatching functions the same way,
18535 -- because access-to-function types can point at both, so the calling
18536 -- conventions must be compatible. Is_Tagged_Type includes controlled
18537 -- types and class-wide types. Controlled type temporaries need
18538 -- finalization.
18539
18540 -- ???It's not clear why we need to return noncontrolled types with
18541 -- controlled components on the secondary stack.
18542
18543 elsif Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
18544 return True;
18545
18546 -- Untagged definite subtypes are known size. This includes all
18547 -- elementary [sub]types. Tasks are known size even if they have
18548 -- discriminants. So we return False here, with one exception:
18549 -- For a type like:
18550 -- type T (Last : Natural := 0) is
18551 -- X : String (1 .. Last);
18552 -- end record;
18553 -- we return True. That's because for "P(F(...));", where F returns T,
18554 -- we don't know the size of the result at the call site, so if we
18555 -- allocated it on the primary stack, we would have to allocate the
18556 -- maximum size, which is way too big.
18557
18558 elsif Is_Definite_Subtype (Typ) or else Is_Task_Type (Typ) then
18559 return Large_Max_Size_Mutable (Typ);
18560
18561 -- Indefinite (discriminated) untagged record or protected type
18562
18563 elsif Is_Record_Type (Typ) or else Is_Protected_Type (Typ) then
18564 return not Caller_Known_Size_Record (Typ);
18565
18566 -- Unconstrained array
18567
18568 else
18569 pragma Assert (Is_Array_Type (Typ) and not Is_Definite_Subtype (Typ));
18570 return True;
18571 end if;
18572 end New_Requires_Transient_Scope;
18573
18574 --------------------------
18575 -- Reset_Analyzed_Flags --
18576 --------------------------
18577
18578 procedure Reset_Analyzed_Flags (N : Node_Id) is
18579
18580 function Clear_Analyzed (N : Node_Id) return Traverse_Result;
18581 -- Function used to reset Analyzed flags in tree. Note that we do
18582 -- not reset Analyzed flags in entities, since there is no need to
18583 -- reanalyze entities, and indeed, it is wrong to do so, since it
18584 -- can result in generating auxiliary stuff more than once.
18585
18586 --------------------
18587 -- Clear_Analyzed --
18588 --------------------
18589
18590 function Clear_Analyzed (N : Node_Id) return Traverse_Result is
18591 begin
18592 if not Has_Extension (N) then
18593 Set_Analyzed (N, False);
18594 end if;
18595
18596 return OK;
18597 end Clear_Analyzed;
18598
18599 procedure Reset_Analyzed is new Traverse_Proc (Clear_Analyzed);
18600
18601 -- Start of processing for Reset_Analyzed_Flags
18602
18603 begin
18604 Reset_Analyzed (N);
18605 end Reset_Analyzed_Flags;
18606
18607 ------------------------
18608 -- Restore_SPARK_Mode --
18609 ------------------------
18610
18611 procedure Restore_SPARK_Mode (Mode : SPARK_Mode_Type) is
18612 begin
18613 SPARK_Mode := Mode;
18614 end Restore_SPARK_Mode;
18615
18616 --------------------------------
18617 -- Returns_Unconstrained_Type --
18618 --------------------------------
18619
18620 function Returns_Unconstrained_Type (Subp : Entity_Id) return Boolean is
18621 begin
18622 return Ekind (Subp) = E_Function
18623 and then not Is_Scalar_Type (Etype (Subp))
18624 and then not Is_Access_Type (Etype (Subp))
18625 and then not Is_Constrained (Etype (Subp));
18626 end Returns_Unconstrained_Type;
18627
18628 ----------------------------
18629 -- Root_Type_Of_Full_View --
18630 ----------------------------
18631
18632 function Root_Type_Of_Full_View (T : Entity_Id) return Entity_Id is
18633 Rtyp : constant Entity_Id := Root_Type (T);
18634
18635 begin
18636 -- The root type of the full view may itself be a private type. Keep
18637 -- looking for the ultimate derivation parent.
18638
18639 if Is_Private_Type (Rtyp) and then Present (Full_View (Rtyp)) then
18640 return Root_Type_Of_Full_View (Full_View (Rtyp));
18641 else
18642 return Rtyp;
18643 end if;
18644 end Root_Type_Of_Full_View;
18645
18646 ---------------------------
18647 -- Safe_To_Capture_Value --
18648 ---------------------------
18649
18650 function Safe_To_Capture_Value
18651 (N : Node_Id;
18652 Ent : Entity_Id;
18653 Cond : Boolean := False) return Boolean
18654 is
18655 begin
18656 -- The only entities for which we track constant values are variables
18657 -- which are not renamings, constants, out parameters, and in out
18658 -- parameters, so check if we have this case.
18659
18660 -- Note: it may seem odd to track constant values for constants, but in
18661 -- fact this routine is used for other purposes than simply capturing
18662 -- the value. In particular, the setting of Known[_Non]_Null.
18663
18664 if (Ekind (Ent) = E_Variable and then No (Renamed_Object (Ent)))
18665 or else
18666 Ekind_In (Ent, E_Constant, E_Out_Parameter, E_In_Out_Parameter)
18667 then
18668 null;
18669
18670 -- For conditionals, we also allow loop parameters and all formals,
18671 -- including in parameters.
18672
18673 elsif Cond and then Ekind_In (Ent, E_Loop_Parameter, E_In_Parameter) then
18674 null;
18675
18676 -- For all other cases, not just unsafe, but impossible to capture
18677 -- Current_Value, since the above are the only entities which have
18678 -- Current_Value fields.
18679
18680 else
18681 return False;
18682 end if;
18683
18684 -- Skip if volatile or aliased, since funny things might be going on in
18685 -- these cases which we cannot necessarily track. Also skip any variable
18686 -- for which an address clause is given, or whose address is taken. Also
18687 -- never capture value of library level variables (an attempt to do so
18688 -- can occur in the case of package elaboration code).
18689
18690 if Treat_As_Volatile (Ent)
18691 or else Is_Aliased (Ent)
18692 or else Present (Address_Clause (Ent))
18693 or else Address_Taken (Ent)
18694 or else (Is_Library_Level_Entity (Ent)
18695 and then Ekind (Ent) = E_Variable)
18696 then
18697 return False;
18698 end if;
18699
18700 -- OK, all above conditions are met. We also require that the scope of
18701 -- the reference be the same as the scope of the entity, not counting
18702 -- packages and blocks and loops.
18703
18704 declare
18705 E_Scope : constant Entity_Id := Scope (Ent);
18706 R_Scope : Entity_Id;
18707
18708 begin
18709 R_Scope := Current_Scope;
18710 while R_Scope /= Standard_Standard loop
18711 exit when R_Scope = E_Scope;
18712
18713 if not Ekind_In (R_Scope, E_Package, E_Block, E_Loop) then
18714 return False;
18715 else
18716 R_Scope := Scope (R_Scope);
18717 end if;
18718 end loop;
18719 end;
18720
18721 -- We also require that the reference does not appear in a context
18722 -- where it is not sure to be executed (i.e. a conditional context
18723 -- or an exception handler). We skip this if Cond is True, since the
18724 -- capturing of values from conditional tests handles this ok.
18725
18726 if Cond then
18727 return True;
18728 end if;
18729
18730 declare
18731 Desc : Node_Id;
18732 P : Node_Id;
18733
18734 begin
18735 Desc := N;
18736
18737 -- Seems dubious that case expressions are not handled here ???
18738
18739 P := Parent (N);
18740 while Present (P) loop
18741 if Nkind (P) = N_If_Statement
18742 or else Nkind (P) = N_Case_Statement
18743 or else (Nkind (P) in N_Short_Circuit
18744 and then Desc = Right_Opnd (P))
18745 or else (Nkind (P) = N_If_Expression
18746 and then Desc /= First (Expressions (P)))
18747 or else Nkind (P) = N_Exception_Handler
18748 or else Nkind (P) = N_Selective_Accept
18749 or else Nkind (P) = N_Conditional_Entry_Call
18750 or else Nkind (P) = N_Timed_Entry_Call
18751 or else Nkind (P) = N_Asynchronous_Select
18752 then
18753 return False;
18754
18755 else
18756 Desc := P;
18757 P := Parent (P);
18758
18759 -- A special Ada 2012 case: the original node may be part
18760 -- of the else_actions of a conditional expression, in which
18761 -- case it might not have been expanded yet, and appears in
18762 -- a non-syntactic list of actions. In that case it is clearly
18763 -- not safe to save a value.
18764
18765 if No (P)
18766 and then Is_List_Member (Desc)
18767 and then No (Parent (List_Containing (Desc)))
18768 then
18769 return False;
18770 end if;
18771 end if;
18772 end loop;
18773 end;
18774
18775 -- OK, looks safe to set value
18776
18777 return True;
18778 end Safe_To_Capture_Value;
18779
18780 ---------------
18781 -- Same_Name --
18782 ---------------
18783
18784 function Same_Name (N1, N2 : Node_Id) return Boolean is
18785 K1 : constant Node_Kind := Nkind (N1);
18786 K2 : constant Node_Kind := Nkind (N2);
18787
18788 begin
18789 if (K1 = N_Identifier or else K1 = N_Defining_Identifier)
18790 and then (K2 = N_Identifier or else K2 = N_Defining_Identifier)
18791 then
18792 return Chars (N1) = Chars (N2);
18793
18794 elsif (K1 = N_Selected_Component or else K1 = N_Expanded_Name)
18795 and then (K2 = N_Selected_Component or else K2 = N_Expanded_Name)
18796 then
18797 return Same_Name (Selector_Name (N1), Selector_Name (N2))
18798 and then Same_Name (Prefix (N1), Prefix (N2));
18799
18800 else
18801 return False;
18802 end if;
18803 end Same_Name;
18804
18805 -----------------
18806 -- Same_Object --
18807 -----------------
18808
18809 function Same_Object (Node1, Node2 : Node_Id) return Boolean is
18810 N1 : constant Node_Id := Original_Node (Node1);
18811 N2 : constant Node_Id := Original_Node (Node2);
18812 -- We do the tests on original nodes, since we are most interested
18813 -- in the original source, not any expansion that got in the way.
18814
18815 K1 : constant Node_Kind := Nkind (N1);
18816 K2 : constant Node_Kind := Nkind (N2);
18817
18818 begin
18819 -- First case, both are entities with same entity
18820
18821 if K1 in N_Has_Entity and then K2 in N_Has_Entity then
18822 declare
18823 EN1 : constant Entity_Id := Entity (N1);
18824 EN2 : constant Entity_Id := Entity (N2);
18825 begin
18826 if Present (EN1) and then Present (EN2)
18827 and then (Ekind_In (EN1, E_Variable, E_Constant)
18828 or else Is_Formal (EN1))
18829 and then EN1 = EN2
18830 then
18831 return True;
18832 end if;
18833 end;
18834 end if;
18835
18836 -- Second case, selected component with same selector, same record
18837
18838 if K1 = N_Selected_Component
18839 and then K2 = N_Selected_Component
18840 and then Chars (Selector_Name (N1)) = Chars (Selector_Name (N2))
18841 then
18842 return Same_Object (Prefix (N1), Prefix (N2));
18843
18844 -- Third case, indexed component with same subscripts, same array
18845
18846 elsif K1 = N_Indexed_Component
18847 and then K2 = N_Indexed_Component
18848 and then Same_Object (Prefix (N1), Prefix (N2))
18849 then
18850 declare
18851 E1, E2 : Node_Id;
18852 begin
18853 E1 := First (Expressions (N1));
18854 E2 := First (Expressions (N2));
18855 while Present (E1) loop
18856 if not Same_Value (E1, E2) then
18857 return False;
18858 else
18859 Next (E1);
18860 Next (E2);
18861 end if;
18862 end loop;
18863
18864 return True;
18865 end;
18866
18867 -- Fourth case, slice of same array with same bounds
18868
18869 elsif K1 = N_Slice
18870 and then K2 = N_Slice
18871 and then Nkind (Discrete_Range (N1)) = N_Range
18872 and then Nkind (Discrete_Range (N2)) = N_Range
18873 and then Same_Value (Low_Bound (Discrete_Range (N1)),
18874 Low_Bound (Discrete_Range (N2)))
18875 and then Same_Value (High_Bound (Discrete_Range (N1)),
18876 High_Bound (Discrete_Range (N2)))
18877 then
18878 return Same_Name (Prefix (N1), Prefix (N2));
18879
18880 -- All other cases, not clearly the same object
18881
18882 else
18883 return False;
18884 end if;
18885 end Same_Object;
18886
18887 ---------------
18888 -- Same_Type --
18889 ---------------
18890
18891 function Same_Type (T1, T2 : Entity_Id) return Boolean is
18892 begin
18893 if T1 = T2 then
18894 return True;
18895
18896 elsif not Is_Constrained (T1)
18897 and then not Is_Constrained (T2)
18898 and then Base_Type (T1) = Base_Type (T2)
18899 then
18900 return True;
18901
18902 -- For now don't bother with case of identical constraints, to be
18903 -- fiddled with later on perhaps (this is only used for optimization
18904 -- purposes, so it is not critical to do a best possible job)
18905
18906 else
18907 return False;
18908 end if;
18909 end Same_Type;
18910
18911 ----------------
18912 -- Same_Value --
18913 ----------------
18914
18915 function Same_Value (Node1, Node2 : Node_Id) return Boolean is
18916 begin
18917 if Compile_Time_Known_Value (Node1)
18918 and then Compile_Time_Known_Value (Node2)
18919 and then Expr_Value (Node1) = Expr_Value (Node2)
18920 then
18921 return True;
18922 elsif Same_Object (Node1, Node2) then
18923 return True;
18924 else
18925 return False;
18926 end if;
18927 end Same_Value;
18928
18929 -----------------------------
18930 -- Save_SPARK_Mode_And_Set --
18931 -----------------------------
18932
18933 procedure Save_SPARK_Mode_And_Set
18934 (Context : Entity_Id;
18935 Mode : out SPARK_Mode_Type)
18936 is
18937 begin
18938 -- Save the current mode in effect
18939
18940 Mode := SPARK_Mode;
18941
18942 -- Do not consider illegal or partially decorated constructs
18943
18944 if Ekind (Context) = E_Void or else Error_Posted (Context) then
18945 null;
18946
18947 elsif Present (SPARK_Pragma (Context)) then
18948 SPARK_Mode := Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Context));
18949 end if;
18950 end Save_SPARK_Mode_And_Set;
18951
18952 -------------------------
18953 -- Scalar_Part_Present --
18954 -------------------------
18955
18956 function Scalar_Part_Present (T : Entity_Id) return Boolean is
18957 C : Entity_Id;
18958
18959 begin
18960 if Is_Scalar_Type (T) then
18961 return True;
18962
18963 elsif Is_Array_Type (T) then
18964 return Scalar_Part_Present (Component_Type (T));
18965
18966 elsif Is_Record_Type (T) or else Has_Discriminants (T) then
18967 C := First_Component_Or_Discriminant (T);
18968 while Present (C) loop
18969 if Scalar_Part_Present (Etype (C)) then
18970 return True;
18971 else
18972 Next_Component_Or_Discriminant (C);
18973 end if;
18974 end loop;
18975 end if;
18976
18977 return False;
18978 end Scalar_Part_Present;
18979
18980 ------------------------
18981 -- Scope_Is_Transient --
18982 ------------------------
18983
18984 function Scope_Is_Transient return Boolean is
18985 begin
18986 return Scope_Stack.Table (Scope_Stack.Last).Is_Transient;
18987 end Scope_Is_Transient;
18988
18989 ------------------
18990 -- Scope_Within --
18991 ------------------
18992
18993 function Scope_Within (Scope1, Scope2 : Entity_Id) return Boolean is
18994 Scop : Entity_Id;
18995
18996 begin
18997 Scop := Scope1;
18998 while Scop /= Standard_Standard loop
18999 Scop := Scope (Scop);
19000
19001 if Scop = Scope2 then
19002 return True;
19003 end if;
19004 end loop;
19005
19006 return False;
19007 end Scope_Within;
19008
19009 --------------------------
19010 -- Scope_Within_Or_Same --
19011 --------------------------
19012
19013 function Scope_Within_Or_Same (Scope1, Scope2 : Entity_Id) return Boolean is
19014 Scop : Entity_Id;
19015
19016 begin
19017 Scop := Scope1;
19018 while Scop /= Standard_Standard loop
19019 if Scop = Scope2 then
19020 return True;
19021 else
19022 Scop := Scope (Scop);
19023 end if;
19024 end loop;
19025
19026 return False;
19027 end Scope_Within_Or_Same;
19028
19029 --------------------
19030 -- Set_Convention --
19031 --------------------
19032
19033 procedure Set_Convention (E : Entity_Id; Val : Snames.Convention_Id) is
19034 begin
19035 Basic_Set_Convention (E, Val);
19036
19037 if Is_Type (E)
19038 and then Is_Access_Subprogram_Type (Base_Type (E))
19039 and then Has_Foreign_Convention (E)
19040 then
19041
19042 -- A pragma Convention in an instance may apply to the subtype
19043 -- created for a formal, in which case we have already verified
19044 -- that conventions of actual and formal match and there is nothing
19045 -- to flag on the subtype.
19046
19047 if In_Instance then
19048 null;
19049 else
19050 Set_Can_Use_Internal_Rep (E, False);
19051 end if;
19052 end if;
19053
19054 -- If E is an object or component, and the type of E is an anonymous
19055 -- access type with no convention set, then also set the convention of
19056 -- the anonymous access type. We do not do this for anonymous protected
19057 -- types, since protected types always have the default convention.
19058
19059 if Present (Etype (E))
19060 and then (Is_Object (E)
19061 or else Ekind (E) = E_Component
19062
19063 -- Allow E_Void (happens for pragma Convention appearing
19064 -- in the middle of a record applying to a component)
19065
19066 or else Ekind (E) = E_Void)
19067 then
19068 declare
19069 Typ : constant Entity_Id := Etype (E);
19070
19071 begin
19072 if Ekind_In (Typ, E_Anonymous_Access_Type,
19073 E_Anonymous_Access_Subprogram_Type)
19074 and then not Has_Convention_Pragma (Typ)
19075 then
19076 Basic_Set_Convention (Typ, Val);
19077 Set_Has_Convention_Pragma (Typ);
19078
19079 -- And for the access subprogram type, deal similarly with the
19080 -- designated E_Subprogram_Type if it is also internal (which
19081 -- it always is?)
19082
19083 if Ekind (Typ) = E_Anonymous_Access_Subprogram_Type then
19084 declare
19085 Dtype : constant Entity_Id := Designated_Type (Typ);
19086 begin
19087 if Ekind (Dtype) = E_Subprogram_Type
19088 and then Is_Itype (Dtype)
19089 and then not Has_Convention_Pragma (Dtype)
19090 then
19091 Basic_Set_Convention (Dtype, Val);
19092 Set_Has_Convention_Pragma (Dtype);
19093 end if;
19094 end;
19095 end if;
19096 end if;
19097 end;
19098 end if;
19099 end Set_Convention;
19100
19101 ------------------------
19102 -- Set_Current_Entity --
19103 ------------------------
19104
19105 -- The given entity is to be set as the currently visible definition of its
19106 -- associated name (i.e. the Node_Id associated with its name). All we have
19107 -- to do is to get the name from the identifier, and then set the
19108 -- associated Node_Id to point to the given entity.
19109
19110 procedure Set_Current_Entity (E : Entity_Id) is
19111 begin
19112 Set_Name_Entity_Id (Chars (E), E);
19113 end Set_Current_Entity;
19114
19115 ---------------------------
19116 -- Set_Debug_Info_Needed --
19117 ---------------------------
19118
19119 procedure Set_Debug_Info_Needed (T : Entity_Id) is
19120
19121 procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id);
19122 pragma Inline (Set_Debug_Info_Needed_If_Not_Set);
19123 -- Used to set debug info in a related node if not set already
19124
19125 --------------------------------------
19126 -- Set_Debug_Info_Needed_If_Not_Set --
19127 --------------------------------------
19128
19129 procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id) is
19130 begin
19131 if Present (E) and then not Needs_Debug_Info (E) then
19132 Set_Debug_Info_Needed (E);
19133
19134 -- For a private type, indicate that the full view also needs
19135 -- debug information.
19136
19137 if Is_Type (E)
19138 and then Is_Private_Type (E)
19139 and then Present (Full_View (E))
19140 then
19141 Set_Debug_Info_Needed (Full_View (E));
19142 end if;
19143 end if;
19144 end Set_Debug_Info_Needed_If_Not_Set;
19145
19146 -- Start of processing for Set_Debug_Info_Needed
19147
19148 begin
19149 -- Nothing to do if argument is Empty or has Debug_Info_Off set, which
19150 -- indicates that Debug_Info_Needed is never required for the entity.
19151 -- Nothing to do if entity comes from a predefined file. Library files
19152 -- are compiled without debug information, but inlined bodies of these
19153 -- routines may appear in user code, and debug information on them ends
19154 -- up complicating debugging the user code.
19155
19156 if No (T)
19157 or else Debug_Info_Off (T)
19158 then
19159 return;
19160
19161 elsif In_Inlined_Body
19162 and then Is_Predefined_File_Name
19163 (Unit_File_Name (Get_Source_Unit (Sloc (T))))
19164 then
19165 Set_Needs_Debug_Info (T, False);
19166 end if;
19167
19168 -- Set flag in entity itself. Note that we will go through the following
19169 -- circuitry even if the flag is already set on T. That's intentional,
19170 -- it makes sure that the flag will be set in subsidiary entities.
19171
19172 Set_Needs_Debug_Info (T);
19173
19174 -- Set flag on subsidiary entities if not set already
19175
19176 if Is_Object (T) then
19177 Set_Debug_Info_Needed_If_Not_Set (Etype (T));
19178
19179 elsif Is_Type (T) then
19180 Set_Debug_Info_Needed_If_Not_Set (Etype (T));
19181
19182 if Is_Record_Type (T) then
19183 declare
19184 Ent : Entity_Id := First_Entity (T);
19185 begin
19186 while Present (Ent) loop
19187 Set_Debug_Info_Needed_If_Not_Set (Ent);
19188 Next_Entity (Ent);
19189 end loop;
19190 end;
19191
19192 -- For a class wide subtype, we also need debug information
19193 -- for the equivalent type.
19194
19195 if Ekind (T) = E_Class_Wide_Subtype then
19196 Set_Debug_Info_Needed_If_Not_Set (Equivalent_Type (T));
19197 end if;
19198
19199 elsif Is_Array_Type (T) then
19200 Set_Debug_Info_Needed_If_Not_Set (Component_Type (T));
19201
19202 declare
19203 Indx : Node_Id := First_Index (T);
19204 begin
19205 while Present (Indx) loop
19206 Set_Debug_Info_Needed_If_Not_Set (Etype (Indx));
19207 Indx := Next_Index (Indx);
19208 end loop;
19209 end;
19210
19211 -- For a packed array type, we also need debug information for
19212 -- the type used to represent the packed array. Conversely, we
19213 -- also need it for the former if we need it for the latter.
19214
19215 if Is_Packed (T) then
19216 Set_Debug_Info_Needed_If_Not_Set (Packed_Array_Impl_Type (T));
19217 end if;
19218
19219 if Is_Packed_Array_Impl_Type (T) then
19220 Set_Debug_Info_Needed_If_Not_Set (Original_Array_Type (T));
19221 end if;
19222
19223 elsif Is_Access_Type (T) then
19224 Set_Debug_Info_Needed_If_Not_Set (Directly_Designated_Type (T));
19225
19226 elsif Is_Private_Type (T) then
19227 declare
19228 FV : constant Entity_Id := Full_View (T);
19229
19230 begin
19231 Set_Debug_Info_Needed_If_Not_Set (FV);
19232
19233 -- If the full view is itself a derived private type, we need
19234 -- debug information on its underlying type.
19235
19236 if Present (FV)
19237 and then Is_Private_Type (FV)
19238 and then Present (Underlying_Full_View (FV))
19239 then
19240 Set_Needs_Debug_Info (Underlying_Full_View (FV));
19241 end if;
19242 end;
19243
19244 elsif Is_Protected_Type (T) then
19245 Set_Debug_Info_Needed_If_Not_Set (Corresponding_Record_Type (T));
19246
19247 elsif Is_Scalar_Type (T) then
19248
19249 -- If the subrange bounds are materialized by dedicated constant
19250 -- objects, also include them in the debug info to make sure the
19251 -- debugger can properly use them.
19252
19253 if Present (Scalar_Range (T))
19254 and then Nkind (Scalar_Range (T)) = N_Range
19255 then
19256 declare
19257 Low_Bnd : constant Node_Id := Type_Low_Bound (T);
19258 High_Bnd : constant Node_Id := Type_High_Bound (T);
19259
19260 begin
19261 if Is_Entity_Name (Low_Bnd) then
19262 Set_Debug_Info_Needed_If_Not_Set (Entity (Low_Bnd));
19263 end if;
19264
19265 if Is_Entity_Name (High_Bnd) then
19266 Set_Debug_Info_Needed_If_Not_Set (Entity (High_Bnd));
19267 end if;
19268 end;
19269 end if;
19270 end if;
19271 end if;
19272 end Set_Debug_Info_Needed;
19273
19274 ----------------------------
19275 -- Set_Entity_With_Checks --
19276 ----------------------------
19277
19278 procedure Set_Entity_With_Checks (N : Node_Id; Val : Entity_Id) is
19279 Val_Actual : Entity_Id;
19280 Nod : Node_Id;
19281 Post_Node : Node_Id;
19282
19283 begin
19284 -- Unconditionally set the entity
19285
19286 Set_Entity (N, Val);
19287
19288 -- The node to post on is the selector in the case of an expanded name,
19289 -- and otherwise the node itself.
19290
19291 if Nkind (N) = N_Expanded_Name then
19292 Post_Node := Selector_Name (N);
19293 else
19294 Post_Node := N;
19295 end if;
19296
19297 -- Check for violation of No_Fixed_IO
19298
19299 if Restriction_Check_Required (No_Fixed_IO)
19300 and then
19301 ((RTU_Loaded (Ada_Text_IO)
19302 and then (Is_RTE (Val, RE_Decimal_IO)
19303 or else
19304 Is_RTE (Val, RE_Fixed_IO)))
19305
19306 or else
19307 (RTU_Loaded (Ada_Wide_Text_IO)
19308 and then (Is_RTE (Val, RO_WT_Decimal_IO)
19309 or else
19310 Is_RTE (Val, RO_WT_Fixed_IO)))
19311
19312 or else
19313 (RTU_Loaded (Ada_Wide_Wide_Text_IO)
19314 and then (Is_RTE (Val, RO_WW_Decimal_IO)
19315 or else
19316 Is_RTE (Val, RO_WW_Fixed_IO))))
19317
19318 -- A special extra check, don't complain about a reference from within
19319 -- the Ada.Interrupts package itself!
19320
19321 and then not In_Same_Extended_Unit (N, Val)
19322 then
19323 Check_Restriction (No_Fixed_IO, Post_Node);
19324 end if;
19325
19326 -- Remaining checks are only done on source nodes. Note that we test
19327 -- for violation of No_Fixed_IO even on non-source nodes, because the
19328 -- cases for checking violations of this restriction are instantiations
19329 -- where the reference in the instance has Comes_From_Source False.
19330
19331 if not Comes_From_Source (N) then
19332 return;
19333 end if;
19334
19335 -- Check for violation of No_Abort_Statements, which is triggered by
19336 -- call to Ada.Task_Identification.Abort_Task.
19337
19338 if Restriction_Check_Required (No_Abort_Statements)
19339 and then (Is_RTE (Val, RE_Abort_Task))
19340
19341 -- A special extra check, don't complain about a reference from within
19342 -- the Ada.Task_Identification package itself!
19343
19344 and then not In_Same_Extended_Unit (N, Val)
19345 then
19346 Check_Restriction (No_Abort_Statements, Post_Node);
19347 end if;
19348
19349 if Val = Standard_Long_Long_Integer then
19350 Check_Restriction (No_Long_Long_Integers, Post_Node);
19351 end if;
19352
19353 -- Check for violation of No_Dynamic_Attachment
19354
19355 if Restriction_Check_Required (No_Dynamic_Attachment)
19356 and then RTU_Loaded (Ada_Interrupts)
19357 and then (Is_RTE (Val, RE_Is_Reserved) or else
19358 Is_RTE (Val, RE_Is_Attached) or else
19359 Is_RTE (Val, RE_Current_Handler) or else
19360 Is_RTE (Val, RE_Attach_Handler) or else
19361 Is_RTE (Val, RE_Exchange_Handler) or else
19362 Is_RTE (Val, RE_Detach_Handler) or else
19363 Is_RTE (Val, RE_Reference))
19364
19365 -- A special extra check, don't complain about a reference from within
19366 -- the Ada.Interrupts package itself!
19367
19368 and then not In_Same_Extended_Unit (N, Val)
19369 then
19370 Check_Restriction (No_Dynamic_Attachment, Post_Node);
19371 end if;
19372
19373 -- Check for No_Implementation_Identifiers
19374
19375 if Restriction_Check_Required (No_Implementation_Identifiers) then
19376
19377 -- We have an implementation defined entity if it is marked as
19378 -- implementation defined, or is defined in a package marked as
19379 -- implementation defined. However, library packages themselves
19380 -- are excluded (we don't want to flag Interfaces itself, just
19381 -- the entities within it).
19382
19383 if (Is_Implementation_Defined (Val)
19384 or else
19385 (Present (Scope (Val))
19386 and then Is_Implementation_Defined (Scope (Val))))
19387 and then not (Ekind_In (Val, E_Package, E_Generic_Package)
19388 and then Is_Library_Level_Entity (Val))
19389 then
19390 Check_Restriction (No_Implementation_Identifiers, Post_Node);
19391 end if;
19392 end if;
19393
19394 -- Do the style check
19395
19396 if Style_Check
19397 and then not Suppress_Style_Checks (Val)
19398 and then not In_Instance
19399 then
19400 if Nkind (N) = N_Identifier then
19401 Nod := N;
19402 elsif Nkind (N) = N_Expanded_Name then
19403 Nod := Selector_Name (N);
19404 else
19405 return;
19406 end if;
19407
19408 -- A special situation arises for derived operations, where we want
19409 -- to do the check against the parent (since the Sloc of the derived
19410 -- operation points to the derived type declaration itself).
19411
19412 Val_Actual := Val;
19413 while not Comes_From_Source (Val_Actual)
19414 and then Nkind (Val_Actual) in N_Entity
19415 and then (Ekind (Val_Actual) = E_Enumeration_Literal
19416 or else Is_Subprogram_Or_Generic_Subprogram (Val_Actual))
19417 and then Present (Alias (Val_Actual))
19418 loop
19419 Val_Actual := Alias (Val_Actual);
19420 end loop;
19421
19422 -- Renaming declarations for generic actuals do not come from source,
19423 -- and have a different name from that of the entity they rename, so
19424 -- there is no style check to perform here.
19425
19426 if Chars (Nod) = Chars (Val_Actual) then
19427 Style.Check_Identifier (Nod, Val_Actual);
19428 end if;
19429 end if;
19430
19431 Set_Entity (N, Val);
19432 end Set_Entity_With_Checks;
19433
19434 ------------------------
19435 -- Set_Name_Entity_Id --
19436 ------------------------
19437
19438 procedure Set_Name_Entity_Id (Id : Name_Id; Val : Entity_Id) is
19439 begin
19440 Set_Name_Table_Int (Id, Int (Val));
19441 end Set_Name_Entity_Id;
19442
19443 ---------------------
19444 -- Set_Next_Actual --
19445 ---------------------
19446
19447 procedure Set_Next_Actual (Ass1_Id : Node_Id; Ass2_Id : Node_Id) is
19448 begin
19449 if Nkind (Parent (Ass1_Id)) = N_Parameter_Association then
19450 Set_First_Named_Actual (Parent (Ass1_Id), Ass2_Id);
19451 end if;
19452 end Set_Next_Actual;
19453
19454 ----------------------------------
19455 -- Set_Optimize_Alignment_Flags --
19456 ----------------------------------
19457
19458 procedure Set_Optimize_Alignment_Flags (E : Entity_Id) is
19459 begin
19460 if Optimize_Alignment = 'S' then
19461 Set_Optimize_Alignment_Space (E);
19462 elsif Optimize_Alignment = 'T' then
19463 Set_Optimize_Alignment_Time (E);
19464 end if;
19465 end Set_Optimize_Alignment_Flags;
19466
19467 -----------------------
19468 -- Set_Public_Status --
19469 -----------------------
19470
19471 procedure Set_Public_Status (Id : Entity_Id) is
19472 S : constant Entity_Id := Current_Scope;
19473
19474 function Within_HSS_Or_If (E : Entity_Id) return Boolean;
19475 -- Determines if E is defined within handled statement sequence or
19476 -- an if statement, returns True if so, False otherwise.
19477
19478 ----------------------
19479 -- Within_HSS_Or_If --
19480 ----------------------
19481
19482 function Within_HSS_Or_If (E : Entity_Id) return Boolean is
19483 N : Node_Id;
19484 begin
19485 N := Declaration_Node (E);
19486 loop
19487 N := Parent (N);
19488
19489 if No (N) then
19490 return False;
19491
19492 elsif Nkind_In (N, N_Handled_Sequence_Of_Statements,
19493 N_If_Statement)
19494 then
19495 return True;
19496 end if;
19497 end loop;
19498 end Within_HSS_Or_If;
19499
19500 -- Start of processing for Set_Public_Status
19501
19502 begin
19503 -- Everything in the scope of Standard is public
19504
19505 if S = Standard_Standard then
19506 Set_Is_Public (Id);
19507
19508 -- Entity is definitely not public if enclosing scope is not public
19509
19510 elsif not Is_Public (S) then
19511 return;
19512
19513 -- An object or function declaration that occurs in a handled sequence
19514 -- of statements or within an if statement is the declaration for a
19515 -- temporary object or local subprogram generated by the expander. It
19516 -- never needs to be made public and furthermore, making it public can
19517 -- cause back end problems.
19518
19519 elsif Nkind_In (Parent (Id), N_Object_Declaration,
19520 N_Function_Specification)
19521 and then Within_HSS_Or_If (Id)
19522 then
19523 return;
19524
19525 -- Entities in public packages or records are public
19526
19527 elsif Ekind (S) = E_Package or Is_Record_Type (S) then
19528 Set_Is_Public (Id);
19529
19530 -- The bounds of an entry family declaration can generate object
19531 -- declarations that are visible to the back-end, e.g. in the
19532 -- the declaration of a composite type that contains tasks.
19533
19534 elsif Is_Concurrent_Type (S)
19535 and then not Has_Completion (S)
19536 and then Nkind (Parent (Id)) = N_Object_Declaration
19537 then
19538 Set_Is_Public (Id);
19539 end if;
19540 end Set_Public_Status;
19541
19542 -----------------------------
19543 -- Set_Referenced_Modified --
19544 -----------------------------
19545
19546 procedure Set_Referenced_Modified (N : Node_Id; Out_Param : Boolean) is
19547 Pref : Node_Id;
19548
19549 begin
19550 -- Deal with indexed or selected component where prefix is modified
19551
19552 if Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
19553 Pref := Prefix (N);
19554
19555 -- If prefix is access type, then it is the designated object that is
19556 -- being modified, which means we have no entity to set the flag on.
19557
19558 if No (Etype (Pref)) or else Is_Access_Type (Etype (Pref)) then
19559 return;
19560
19561 -- Otherwise chase the prefix
19562
19563 else
19564 Set_Referenced_Modified (Pref, Out_Param);
19565 end if;
19566
19567 -- Otherwise see if we have an entity name (only other case to process)
19568
19569 elsif Is_Entity_Name (N) and then Present (Entity (N)) then
19570 Set_Referenced_As_LHS (Entity (N), not Out_Param);
19571 Set_Referenced_As_Out_Parameter (Entity (N), Out_Param);
19572 end if;
19573 end Set_Referenced_Modified;
19574
19575 ----------------------------
19576 -- Set_Scope_Is_Transient --
19577 ----------------------------
19578
19579 procedure Set_Scope_Is_Transient (V : Boolean := True) is
19580 begin
19581 Scope_Stack.Table (Scope_Stack.Last).Is_Transient := V;
19582 end Set_Scope_Is_Transient;
19583
19584 -------------------
19585 -- Set_Size_Info --
19586 -------------------
19587
19588 procedure Set_Size_Info (T1, T2 : Entity_Id) is
19589 begin
19590 -- We copy Esize, but not RM_Size, since in general RM_Size is
19591 -- subtype specific and does not get inherited by all subtypes.
19592
19593 Set_Esize (T1, Esize (T2));
19594 Set_Has_Biased_Representation (T1, Has_Biased_Representation (T2));
19595
19596 if Is_Discrete_Or_Fixed_Point_Type (T1)
19597 and then
19598 Is_Discrete_Or_Fixed_Point_Type (T2)
19599 then
19600 Set_Is_Unsigned_Type (T1, Is_Unsigned_Type (T2));
19601 end if;
19602
19603 Set_Alignment (T1, Alignment (T2));
19604 end Set_Size_Info;
19605
19606 --------------------
19607 -- Static_Boolean --
19608 --------------------
19609
19610 function Static_Boolean (N : Node_Id) return Uint is
19611 begin
19612 Analyze_And_Resolve (N, Standard_Boolean);
19613
19614 if N = Error
19615 or else Error_Posted (N)
19616 or else Etype (N) = Any_Type
19617 then
19618 return No_Uint;
19619 end if;
19620
19621 if Is_OK_Static_Expression (N) then
19622 if not Raises_Constraint_Error (N) then
19623 return Expr_Value (N);
19624 else
19625 return No_Uint;
19626 end if;
19627
19628 elsif Etype (N) = Any_Type then
19629 return No_Uint;
19630
19631 else
19632 Flag_Non_Static_Expr
19633 ("static boolean expression required here", N);
19634 return No_Uint;
19635 end if;
19636 end Static_Boolean;
19637
19638 --------------------
19639 -- Static_Integer --
19640 --------------------
19641
19642 function Static_Integer (N : Node_Id) return Uint is
19643 begin
19644 Analyze_And_Resolve (N, Any_Integer);
19645
19646 if N = Error
19647 or else Error_Posted (N)
19648 or else Etype (N) = Any_Type
19649 then
19650 return No_Uint;
19651 end if;
19652
19653 if Is_OK_Static_Expression (N) then
19654 if not Raises_Constraint_Error (N) then
19655 return Expr_Value (N);
19656 else
19657 return No_Uint;
19658 end if;
19659
19660 elsif Etype (N) = Any_Type then
19661 return No_Uint;
19662
19663 else
19664 Flag_Non_Static_Expr
19665 ("static integer expression required here", N);
19666 return No_Uint;
19667 end if;
19668 end Static_Integer;
19669
19670 --------------------------
19671 -- Statically_Different --
19672 --------------------------
19673
19674 function Statically_Different (E1, E2 : Node_Id) return Boolean is
19675 R1 : constant Node_Id := Get_Referenced_Object (E1);
19676 R2 : constant Node_Id := Get_Referenced_Object (E2);
19677 begin
19678 return Is_Entity_Name (R1)
19679 and then Is_Entity_Name (R2)
19680 and then Entity (R1) /= Entity (R2)
19681 and then not Is_Formal (Entity (R1))
19682 and then not Is_Formal (Entity (R2));
19683 end Statically_Different;
19684
19685 --------------------------------------
19686 -- Subject_To_Loop_Entry_Attributes --
19687 --------------------------------------
19688
19689 function Subject_To_Loop_Entry_Attributes (N : Node_Id) return Boolean is
19690 Stmt : Node_Id;
19691
19692 begin
19693 Stmt := N;
19694
19695 -- The expansion mechanism transform a loop subject to at least one
19696 -- 'Loop_Entry attribute into a conditional block. Infinite loops lack
19697 -- the conditional part.
19698
19699 if Nkind_In (Stmt, N_Block_Statement, N_If_Statement)
19700 and then Nkind (Original_Node (N)) = N_Loop_Statement
19701 then
19702 Stmt := Original_Node (N);
19703 end if;
19704
19705 return
19706 Nkind (Stmt) = N_Loop_Statement
19707 and then Present (Identifier (Stmt))
19708 and then Present (Entity (Identifier (Stmt)))
19709 and then Has_Loop_Entry_Attributes (Entity (Identifier (Stmt)));
19710 end Subject_To_Loop_Entry_Attributes;
19711
19712 -----------------------------
19713 -- Subprogram_Access_Level --
19714 -----------------------------
19715
19716 function Subprogram_Access_Level (Subp : Entity_Id) return Uint is
19717 begin
19718 if Present (Alias (Subp)) then
19719 return Subprogram_Access_Level (Alias (Subp));
19720 else
19721 return Scope_Depth (Enclosing_Dynamic_Scope (Subp));
19722 end if;
19723 end Subprogram_Access_Level;
19724
19725 -------------------------------
19726 -- Support_Atomic_Primitives --
19727 -------------------------------
19728
19729 function Support_Atomic_Primitives (Typ : Entity_Id) return Boolean is
19730 Size : Int;
19731
19732 begin
19733 -- Verify the alignment of Typ is known
19734
19735 if not Known_Alignment (Typ) then
19736 return False;
19737 end if;
19738
19739 if Known_Static_Esize (Typ) then
19740 Size := UI_To_Int (Esize (Typ));
19741
19742 -- If the Esize (Object_Size) is unknown at compile time, look at the
19743 -- RM_Size (Value_Size) which may have been set by an explicit rep item.
19744
19745 elsif Known_Static_RM_Size (Typ) then
19746 Size := UI_To_Int (RM_Size (Typ));
19747
19748 -- Otherwise, the size is considered to be unknown.
19749
19750 else
19751 return False;
19752 end if;
19753
19754 -- Check that the size of the component is 8, 16, 32 or 64 bits and that
19755 -- Typ is properly aligned.
19756
19757 case Size is
19758 when 8 | 16 | 32 | 64 =>
19759 return Size = UI_To_Int (Alignment (Typ)) * 8;
19760 when others =>
19761 return False;
19762 end case;
19763 end Support_Atomic_Primitives;
19764
19765 -----------------
19766 -- Trace_Scope --
19767 -----------------
19768
19769 procedure Trace_Scope (N : Node_Id; E : Entity_Id; Msg : String) is
19770 begin
19771 if Debug_Flag_W then
19772 for J in 0 .. Scope_Stack.Last loop
19773 Write_Str (" ");
19774 end loop;
19775
19776 Write_Str (Msg);
19777 Write_Name (Chars (E));
19778 Write_Str (" from ");
19779 Write_Location (Sloc (N));
19780 Write_Eol;
19781 end if;
19782 end Trace_Scope;
19783
19784 -----------------------
19785 -- Transfer_Entities --
19786 -----------------------
19787
19788 procedure Transfer_Entities (From : Entity_Id; To : Entity_Id) is
19789 procedure Set_Public_Status_Of (Id : Entity_Id);
19790 -- Set the Is_Public attribute of arbitrary entity Id by calling routine
19791 -- Set_Public_Status. If successfull and Id denotes a record type, set
19792 -- the Is_Public attribute of its fields.
19793
19794 --------------------------
19795 -- Set_Public_Status_Of --
19796 --------------------------
19797
19798 procedure Set_Public_Status_Of (Id : Entity_Id) is
19799 Field : Entity_Id;
19800
19801 begin
19802 if not Is_Public (Id) then
19803 Set_Public_Status (Id);
19804
19805 -- When the input entity is a public record type, ensure that all
19806 -- its internal fields are also exposed to the linker. The fields
19807 -- of a class-wide type are never made public.
19808
19809 if Is_Public (Id)
19810 and then Is_Record_Type (Id)
19811 and then not Is_Class_Wide_Type (Id)
19812 then
19813 Field := First_Entity (Id);
19814 while Present (Field) loop
19815 Set_Is_Public (Field);
19816 Next_Entity (Field);
19817 end loop;
19818 end if;
19819 end if;
19820 end Set_Public_Status_Of;
19821
19822 -- Local variables
19823
19824 Full_Id : Entity_Id;
19825 Id : Entity_Id;
19826
19827 -- Start of processing for Transfer_Entities
19828
19829 begin
19830 Id := First_Entity (From);
19831
19832 if Present (Id) then
19833
19834 -- Merge the entity chain of the source scope with that of the
19835 -- destination scope.
19836
19837 if Present (Last_Entity (To)) then
19838 Set_Next_Entity (Last_Entity (To), Id);
19839 else
19840 Set_First_Entity (To, Id);
19841 end if;
19842
19843 Set_Last_Entity (To, Last_Entity (From));
19844
19845 -- Inspect the entities of the source scope and update their Scope
19846 -- attribute.
19847
19848 while Present (Id) loop
19849 Set_Scope (Id, To);
19850 Set_Public_Status_Of (Id);
19851
19852 -- Handle an internally generated full view for a private type
19853
19854 if Is_Private_Type (Id)
19855 and then Present (Full_View (Id))
19856 and then Is_Itype (Full_View (Id))
19857 then
19858 Full_Id := Full_View (Id);
19859
19860 Set_Scope (Full_Id, To);
19861 Set_Public_Status_Of (Full_Id);
19862 end if;
19863
19864 Next_Entity (Id);
19865 end loop;
19866
19867 Set_First_Entity (From, Empty);
19868 Set_Last_Entity (From, Empty);
19869 end if;
19870 end Transfer_Entities;
19871
19872 -----------------------
19873 -- Type_Access_Level --
19874 -----------------------
19875
19876 function Type_Access_Level (Typ : Entity_Id) return Uint is
19877 Btyp : Entity_Id;
19878
19879 begin
19880 Btyp := Base_Type (Typ);
19881
19882 -- Ada 2005 (AI-230): For most cases of anonymous access types, we
19883 -- simply use the level where the type is declared. This is true for
19884 -- stand-alone object declarations, and for anonymous access types
19885 -- associated with components the level is the same as that of the
19886 -- enclosing composite type. However, special treatment is needed for
19887 -- the cases of access parameters, return objects of an anonymous access
19888 -- type, and, in Ada 95, access discriminants of limited types.
19889
19890 if Is_Access_Type (Btyp) then
19891 if Ekind (Btyp) = E_Anonymous_Access_Type then
19892
19893 -- If the type is a nonlocal anonymous access type (such as for
19894 -- an access parameter) we treat it as being declared at the
19895 -- library level to ensure that names such as X.all'access don't
19896 -- fail static accessibility checks.
19897
19898 if not Is_Local_Anonymous_Access (Typ) then
19899 return Scope_Depth (Standard_Standard);
19900
19901 -- If this is a return object, the accessibility level is that of
19902 -- the result subtype of the enclosing function. The test here is
19903 -- little complicated, because we have to account for extended
19904 -- return statements that have been rewritten as blocks, in which
19905 -- case we have to find and the Is_Return_Object attribute of the
19906 -- itype's associated object. It would be nice to find a way to
19907 -- simplify this test, but it doesn't seem worthwhile to add a new
19908 -- flag just for purposes of this test. ???
19909
19910 elsif Ekind (Scope (Btyp)) = E_Return_Statement
19911 or else
19912 (Is_Itype (Btyp)
19913 and then Nkind (Associated_Node_For_Itype (Btyp)) =
19914 N_Object_Declaration
19915 and then Is_Return_Object
19916 (Defining_Identifier
19917 (Associated_Node_For_Itype (Btyp))))
19918 then
19919 declare
19920 Scop : Entity_Id;
19921
19922 begin
19923 Scop := Scope (Scope (Btyp));
19924 while Present (Scop) loop
19925 exit when Ekind (Scop) = E_Function;
19926 Scop := Scope (Scop);
19927 end loop;
19928
19929 -- Treat the return object's type as having the level of the
19930 -- function's result subtype (as per RM05-6.5(5.3/2)).
19931
19932 return Type_Access_Level (Etype (Scop));
19933 end;
19934 end if;
19935 end if;
19936
19937 Btyp := Root_Type (Btyp);
19938
19939 -- The accessibility level of anonymous access types associated with
19940 -- discriminants is that of the current instance of the type, and
19941 -- that's deeper than the type itself (AARM 3.10.2 (12.3.21)).
19942
19943 -- AI-402: access discriminants have accessibility based on the
19944 -- object rather than the type in Ada 2005, so the above paragraph
19945 -- doesn't apply.
19946
19947 -- ??? Needs completion with rules from AI-416
19948
19949 if Ada_Version <= Ada_95
19950 and then Ekind (Typ) = E_Anonymous_Access_Type
19951 and then Present (Associated_Node_For_Itype (Typ))
19952 and then Nkind (Associated_Node_For_Itype (Typ)) =
19953 N_Discriminant_Specification
19954 then
19955 return Scope_Depth (Enclosing_Dynamic_Scope (Btyp)) + 1;
19956 end if;
19957 end if;
19958
19959 -- Return library level for a generic formal type. This is done because
19960 -- RM(10.3.2) says that "The statically deeper relationship does not
19961 -- apply to ... a descendant of a generic formal type". Rather than
19962 -- checking at each point where a static accessibility check is
19963 -- performed to see if we are dealing with a formal type, this rule is
19964 -- implemented by having Type_Access_Level and Deepest_Type_Access_Level
19965 -- return extreme values for a formal type; Deepest_Type_Access_Level
19966 -- returns Int'Last. By calling the appropriate function from among the
19967 -- two, we ensure that the static accessibility check will pass if we
19968 -- happen to run into a formal type. More specifically, we should call
19969 -- Deepest_Type_Access_Level instead of Type_Access_Level whenever the
19970 -- call occurs as part of a static accessibility check and the error
19971 -- case is the case where the type's level is too shallow (as opposed
19972 -- to too deep).
19973
19974 if Is_Generic_Type (Root_Type (Btyp)) then
19975 return Scope_Depth (Standard_Standard);
19976 end if;
19977
19978 return Scope_Depth (Enclosing_Dynamic_Scope (Btyp));
19979 end Type_Access_Level;
19980
19981 ------------------------------------
19982 -- Type_Without_Stream_Operation --
19983 ------------------------------------
19984
19985 function Type_Without_Stream_Operation
19986 (T : Entity_Id;
19987 Op : TSS_Name_Type := TSS_Null) return Entity_Id
19988 is
19989 BT : constant Entity_Id := Base_Type (T);
19990 Op_Missing : Boolean;
19991
19992 begin
19993 if not Restriction_Active (No_Default_Stream_Attributes) then
19994 return Empty;
19995 end if;
19996
19997 if Is_Elementary_Type (T) then
19998 if Op = TSS_Null then
19999 Op_Missing :=
20000 No (TSS (BT, TSS_Stream_Read))
20001 or else No (TSS (BT, TSS_Stream_Write));
20002
20003 else
20004 Op_Missing := No (TSS (BT, Op));
20005 end if;
20006
20007 if Op_Missing then
20008 return T;
20009 else
20010 return Empty;
20011 end if;
20012
20013 elsif Is_Array_Type (T) then
20014 return Type_Without_Stream_Operation (Component_Type (T), Op);
20015
20016 elsif Is_Record_Type (T) then
20017 declare
20018 Comp : Entity_Id;
20019 C_Typ : Entity_Id;
20020
20021 begin
20022 Comp := First_Component (T);
20023 while Present (Comp) loop
20024 C_Typ := Type_Without_Stream_Operation (Etype (Comp), Op);
20025
20026 if Present (C_Typ) then
20027 return C_Typ;
20028 end if;
20029
20030 Next_Component (Comp);
20031 end loop;
20032
20033 return Empty;
20034 end;
20035
20036 elsif Is_Private_Type (T) and then Present (Full_View (T)) then
20037 return Type_Without_Stream_Operation (Full_View (T), Op);
20038 else
20039 return Empty;
20040 end if;
20041 end Type_Without_Stream_Operation;
20042
20043 ----------------------------
20044 -- Unique_Defining_Entity --
20045 ----------------------------
20046
20047 function Unique_Defining_Entity (N : Node_Id) return Entity_Id is
20048 begin
20049 return Unique_Entity (Defining_Entity (N));
20050 end Unique_Defining_Entity;
20051
20052 -------------------
20053 -- Unique_Entity --
20054 -------------------
20055
20056 function Unique_Entity (E : Entity_Id) return Entity_Id is
20057 U : Entity_Id := E;
20058 P : Node_Id;
20059
20060 begin
20061 case Ekind (E) is
20062 when E_Constant =>
20063 if Present (Full_View (E)) then
20064 U := Full_View (E);
20065 end if;
20066
20067 when Entry_Kind =>
20068 if Nkind (Parent (E)) = N_Entry_Body then
20069 declare
20070 Prot_Item : Entity_Id;
20071 begin
20072 -- Traverse the entity list of the protected type and locate
20073 -- an entry declaration which matches the entry body.
20074
20075 Prot_Item := First_Entity (Scope (E));
20076 while Present (Prot_Item) loop
20077 if Ekind (Prot_Item) = E_Entry
20078 and then Corresponding_Body (Parent (Prot_Item)) = E
20079 then
20080 U := Prot_Item;
20081 exit;
20082 end if;
20083
20084 Next_Entity (Prot_Item);
20085 end loop;
20086 end;
20087 end if;
20088
20089 when Formal_Kind =>
20090 if Present (Spec_Entity (E)) then
20091 U := Spec_Entity (E);
20092 end if;
20093
20094 when E_Package_Body =>
20095 P := Parent (E);
20096
20097 if Nkind (P) = N_Defining_Program_Unit_Name then
20098 P := Parent (P);
20099 end if;
20100
20101 if Nkind (P) = N_Package_Body
20102 and then Present (Corresponding_Spec (P))
20103 then
20104 U := Corresponding_Spec (P);
20105
20106 elsif Nkind (P) = N_Package_Body_Stub
20107 and then Present (Corresponding_Spec_Of_Stub (P))
20108 then
20109 U := Corresponding_Spec_Of_Stub (P);
20110 end if;
20111
20112 when E_Protected_Body =>
20113 P := Parent (E);
20114
20115 if Nkind (P) = N_Protected_Body
20116 and then Present (Corresponding_Spec (P))
20117 then
20118 U := Corresponding_Spec (P);
20119
20120 elsif Nkind (P) = N_Protected_Body_Stub
20121 and then Present (Corresponding_Spec_Of_Stub (P))
20122 then
20123 U := Corresponding_Spec_Of_Stub (P);
20124 end if;
20125
20126 when E_Subprogram_Body =>
20127 P := Parent (E);
20128
20129 if Nkind (P) = N_Defining_Program_Unit_Name then
20130 P := Parent (P);
20131 end if;
20132
20133 P := Parent (P);
20134
20135 if Nkind (P) = N_Subprogram_Body
20136 and then Present (Corresponding_Spec (P))
20137 then
20138 U := Corresponding_Spec (P);
20139
20140 elsif Nkind (P) = N_Subprogram_Body_Stub
20141 and then Present (Corresponding_Spec_Of_Stub (P))
20142 then
20143 U := Corresponding_Spec_Of_Stub (P);
20144 end if;
20145
20146 when E_Task_Body =>
20147 P := Parent (E);
20148
20149 if Nkind (P) = N_Task_Body
20150 and then Present (Corresponding_Spec (P))
20151 then
20152 U := Corresponding_Spec (P);
20153
20154 elsif Nkind (P) = N_Task_Body_Stub
20155 and then Present (Corresponding_Spec_Of_Stub (P))
20156 then
20157 U := Corresponding_Spec_Of_Stub (P);
20158 end if;
20159
20160 when Type_Kind =>
20161 if Present (Full_View (E)) then
20162 U := Full_View (E);
20163 end if;
20164
20165 when others =>
20166 null;
20167 end case;
20168
20169 return U;
20170 end Unique_Entity;
20171
20172 -----------------
20173 -- Unique_Name --
20174 -----------------
20175
20176 function Unique_Name (E : Entity_Id) return String is
20177
20178 -- Names of E_Subprogram_Body or E_Package_Body entities are not
20179 -- reliable, as they may not include the overloading suffix. Instead,
20180 -- when looking for the name of E or one of its enclosing scope, we get
20181 -- the name of the corresponding Unique_Entity.
20182
20183 function Get_Scoped_Name (E : Entity_Id) return String;
20184 -- Return the name of E prefixed by all the names of the scopes to which
20185 -- E belongs, except for Standard.
20186
20187 ---------------------
20188 -- Get_Scoped_Name --
20189 ---------------------
20190
20191 function Get_Scoped_Name (E : Entity_Id) return String is
20192 Name : constant String := Get_Name_String (Chars (E));
20193 begin
20194 if Has_Fully_Qualified_Name (E)
20195 or else Scope (E) = Standard_Standard
20196 then
20197 return Name;
20198 else
20199 return Get_Scoped_Name (Unique_Entity (Scope (E))) & "__" & Name;
20200 end if;
20201 end Get_Scoped_Name;
20202
20203 -- Start of processing for Unique_Name
20204
20205 begin
20206 if E = Standard_Standard then
20207 return Get_Name_String (Name_Standard);
20208
20209 elsif Scope (E) = Standard_Standard
20210 and then not (Ekind (E) = E_Package or else Is_Subprogram (E))
20211 then
20212 return Get_Name_String (Name_Standard) & "__" &
20213 Get_Name_String (Chars (E));
20214
20215 elsif Ekind (E) = E_Enumeration_Literal then
20216 return Unique_Name (Etype (E)) & "__" & Get_Name_String (Chars (E));
20217
20218 else
20219 return Get_Scoped_Name (Unique_Entity (E));
20220 end if;
20221 end Unique_Name;
20222
20223 ---------------------
20224 -- Unit_Is_Visible --
20225 ---------------------
20226
20227 function Unit_Is_Visible (U : Entity_Id) return Boolean is
20228 Curr : constant Node_Id := Cunit (Current_Sem_Unit);
20229 Curr_Entity : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
20230
20231 function Unit_In_Parent_Context (Par_Unit : Node_Id) return Boolean;
20232 -- For a child unit, check whether unit appears in a with_clause
20233 -- of a parent.
20234
20235 function Unit_In_Context (Comp_Unit : Node_Id) return Boolean;
20236 -- Scan the context clause of one compilation unit looking for a
20237 -- with_clause for the unit in question.
20238
20239 ----------------------------
20240 -- Unit_In_Parent_Context --
20241 ----------------------------
20242
20243 function Unit_In_Parent_Context (Par_Unit : Node_Id) return Boolean is
20244 begin
20245 if Unit_In_Context (Par_Unit) then
20246 return True;
20247
20248 elsif Is_Child_Unit (Defining_Entity (Unit (Par_Unit))) then
20249 return Unit_In_Parent_Context (Parent_Spec (Unit (Par_Unit)));
20250
20251 else
20252 return False;
20253 end if;
20254 end Unit_In_Parent_Context;
20255
20256 ---------------------
20257 -- Unit_In_Context --
20258 ---------------------
20259
20260 function Unit_In_Context (Comp_Unit : Node_Id) return Boolean is
20261 Clause : Node_Id;
20262
20263 begin
20264 Clause := First (Context_Items (Comp_Unit));
20265 while Present (Clause) loop
20266 if Nkind (Clause) = N_With_Clause then
20267 if Library_Unit (Clause) = U then
20268 return True;
20269
20270 -- The with_clause may denote a renaming of the unit we are
20271 -- looking for, eg. Text_IO which renames Ada.Text_IO.
20272
20273 elsif
20274 Renamed_Entity (Entity (Name (Clause))) =
20275 Defining_Entity (Unit (U))
20276 then
20277 return True;
20278 end if;
20279 end if;
20280
20281 Next (Clause);
20282 end loop;
20283
20284 return False;
20285 end Unit_In_Context;
20286
20287 -- Start of processing for Unit_Is_Visible
20288
20289 begin
20290 -- The currrent unit is directly visible
20291
20292 if Curr = U then
20293 return True;
20294
20295 elsif Unit_In_Context (Curr) then
20296 return True;
20297
20298 -- If the current unit is a body, check the context of the spec
20299
20300 elsif Nkind (Unit (Curr)) = N_Package_Body
20301 or else
20302 (Nkind (Unit (Curr)) = N_Subprogram_Body
20303 and then not Acts_As_Spec (Unit (Curr)))
20304 then
20305 if Unit_In_Context (Library_Unit (Curr)) then
20306 return True;
20307 end if;
20308 end if;
20309
20310 -- If the spec is a child unit, examine the parents
20311
20312 if Is_Child_Unit (Curr_Entity) then
20313 if Nkind (Unit (Curr)) in N_Unit_Body then
20314 return
20315 Unit_In_Parent_Context
20316 (Parent_Spec (Unit (Library_Unit (Curr))));
20317 else
20318 return Unit_In_Parent_Context (Parent_Spec (Unit (Curr)));
20319 end if;
20320
20321 else
20322 return False;
20323 end if;
20324 end Unit_Is_Visible;
20325
20326 ------------------------------
20327 -- Universal_Interpretation --
20328 ------------------------------
20329
20330 function Universal_Interpretation (Opnd : Node_Id) return Entity_Id is
20331 Index : Interp_Index;
20332 It : Interp;
20333
20334 begin
20335 -- The argument may be a formal parameter of an operator or subprogram
20336 -- with multiple interpretations, or else an expression for an actual.
20337
20338 if Nkind (Opnd) = N_Defining_Identifier
20339 or else not Is_Overloaded (Opnd)
20340 then
20341 if Etype (Opnd) = Universal_Integer
20342 or else Etype (Opnd) = Universal_Real
20343 then
20344 return Etype (Opnd);
20345 else
20346 return Empty;
20347 end if;
20348
20349 else
20350 Get_First_Interp (Opnd, Index, It);
20351 while Present (It.Typ) loop
20352 if It.Typ = Universal_Integer
20353 or else It.Typ = Universal_Real
20354 then
20355 return It.Typ;
20356 end if;
20357
20358 Get_Next_Interp (Index, It);
20359 end loop;
20360
20361 return Empty;
20362 end if;
20363 end Universal_Interpretation;
20364
20365 ---------------
20366 -- Unqualify --
20367 ---------------
20368
20369 function Unqualify (Expr : Node_Id) return Node_Id is
20370 begin
20371 -- Recurse to handle unlikely case of multiple levels of qualification
20372
20373 if Nkind (Expr) = N_Qualified_Expression then
20374 return Unqualify (Expression (Expr));
20375
20376 -- Normal case, not a qualified expression
20377
20378 else
20379 return Expr;
20380 end if;
20381 end Unqualify;
20382
20383 -----------------------
20384 -- Visible_Ancestors --
20385 -----------------------
20386
20387 function Visible_Ancestors (Typ : Entity_Id) return Elist_Id is
20388 List_1 : Elist_Id;
20389 List_2 : Elist_Id;
20390 Elmt : Elmt_Id;
20391
20392 begin
20393 pragma Assert (Is_Record_Type (Typ) and then Is_Tagged_Type (Typ));
20394
20395 -- Collect all the parents and progenitors of Typ. If the full-view of
20396 -- private parents and progenitors is available then it is used to
20397 -- generate the list of visible ancestors; otherwise their partial
20398 -- view is added to the resulting list.
20399
20400 Collect_Parents
20401 (T => Typ,
20402 List => List_1,
20403 Use_Full_View => True);
20404
20405 Collect_Interfaces
20406 (T => Typ,
20407 Ifaces_List => List_2,
20408 Exclude_Parents => True,
20409 Use_Full_View => True);
20410
20411 -- Join the two lists. Avoid duplications because an interface may
20412 -- simultaneously be parent and progenitor of a type.
20413
20414 Elmt := First_Elmt (List_2);
20415 while Present (Elmt) loop
20416 Append_Unique_Elmt (Node (Elmt), List_1);
20417 Next_Elmt (Elmt);
20418 end loop;
20419
20420 return List_1;
20421 end Visible_Ancestors;
20422
20423 ----------------------
20424 -- Within_Init_Proc --
20425 ----------------------
20426
20427 function Within_Init_Proc return Boolean is
20428 S : Entity_Id;
20429
20430 begin
20431 S := Current_Scope;
20432 while not Is_Overloadable (S) loop
20433 if S = Standard_Standard then
20434 return False;
20435 else
20436 S := Scope (S);
20437 end if;
20438 end loop;
20439
20440 return Is_Init_Proc (S);
20441 end Within_Init_Proc;
20442
20443 ------------------
20444 -- Within_Scope --
20445 ------------------
20446
20447 function Within_Scope (E : Entity_Id; S : Entity_Id) return Boolean is
20448 begin
20449 return Scope_Within_Or_Same (Scope (E), S);
20450 end Within_Scope;
20451
20452 ----------------
20453 -- Wrong_Type --
20454 ----------------
20455
20456 procedure Wrong_Type (Expr : Node_Id; Expected_Type : Entity_Id) is
20457 Found_Type : constant Entity_Id := First_Subtype (Etype (Expr));
20458 Expec_Type : constant Entity_Id := First_Subtype (Expected_Type);
20459
20460 Matching_Field : Entity_Id;
20461 -- Entity to give a more precise suggestion on how to write a one-
20462 -- element positional aggregate.
20463
20464 function Has_One_Matching_Field return Boolean;
20465 -- Determines if Expec_Type is a record type with a single component or
20466 -- discriminant whose type matches the found type or is one dimensional
20467 -- array whose component type matches the found type. In the case of
20468 -- one discriminant, we ignore the variant parts. That's not accurate,
20469 -- but good enough for the warning.
20470
20471 ----------------------------
20472 -- Has_One_Matching_Field --
20473 ----------------------------
20474
20475 function Has_One_Matching_Field return Boolean is
20476 E : Entity_Id;
20477
20478 begin
20479 Matching_Field := Empty;
20480
20481 if Is_Array_Type (Expec_Type)
20482 and then Number_Dimensions (Expec_Type) = 1
20483 and then Covers (Etype (Component_Type (Expec_Type)), Found_Type)
20484 then
20485 -- Use type name if available. This excludes multidimensional
20486 -- arrays and anonymous arrays.
20487
20488 if Comes_From_Source (Expec_Type) then
20489 Matching_Field := Expec_Type;
20490
20491 -- For an assignment, use name of target
20492
20493 elsif Nkind (Parent (Expr)) = N_Assignment_Statement
20494 and then Is_Entity_Name (Name (Parent (Expr)))
20495 then
20496 Matching_Field := Entity (Name (Parent (Expr)));
20497 end if;
20498
20499 return True;
20500
20501 elsif not Is_Record_Type (Expec_Type) then
20502 return False;
20503
20504 else
20505 E := First_Entity (Expec_Type);
20506 loop
20507 if No (E) then
20508 return False;
20509
20510 elsif not Ekind_In (E, E_Discriminant, E_Component)
20511 or else Nam_In (Chars (E), Name_uTag, Name_uParent)
20512 then
20513 Next_Entity (E);
20514
20515 else
20516 exit;
20517 end if;
20518 end loop;
20519
20520 if not Covers (Etype (E), Found_Type) then
20521 return False;
20522
20523 elsif Present (Next_Entity (E))
20524 and then (Ekind (E) = E_Component
20525 or else Ekind (Next_Entity (E)) = E_Discriminant)
20526 then
20527 return False;
20528
20529 else
20530 Matching_Field := E;
20531 return True;
20532 end if;
20533 end if;
20534 end Has_One_Matching_Field;
20535
20536 -- Start of processing for Wrong_Type
20537
20538 begin
20539 -- Don't output message if either type is Any_Type, or if a message
20540 -- has already been posted for this node. We need to do the latter
20541 -- check explicitly (it is ordinarily done in Errout), because we
20542 -- are using ! to force the output of the error messages.
20543
20544 if Expec_Type = Any_Type
20545 or else Found_Type = Any_Type
20546 or else Error_Posted (Expr)
20547 then
20548 return;
20549
20550 -- If one of the types is a Taft-Amendment type and the other it its
20551 -- completion, it must be an illegal use of a TAT in the spec, for
20552 -- which an error was already emitted. Avoid cascaded errors.
20553
20554 elsif Is_Incomplete_Type (Expec_Type)
20555 and then Has_Completion_In_Body (Expec_Type)
20556 and then Full_View (Expec_Type) = Etype (Expr)
20557 then
20558 return;
20559
20560 elsif Is_Incomplete_Type (Etype (Expr))
20561 and then Has_Completion_In_Body (Etype (Expr))
20562 and then Full_View (Etype (Expr)) = Expec_Type
20563 then
20564 return;
20565
20566 -- In an instance, there is an ongoing problem with completion of
20567 -- type derived from private types. Their structure is what Gigi
20568 -- expects, but the Etype is the parent type rather than the
20569 -- derived private type itself. Do not flag error in this case. The
20570 -- private completion is an entity without a parent, like an Itype.
20571 -- Similarly, full and partial views may be incorrect in the instance.
20572 -- There is no simple way to insure that it is consistent ???
20573
20574 -- A similar view discrepancy can happen in an inlined body, for the
20575 -- same reason: inserted body may be outside of the original package
20576 -- and only partial views are visible at the point of insertion.
20577
20578 elsif In_Instance or else In_Inlined_Body then
20579 if Etype (Etype (Expr)) = Etype (Expected_Type)
20580 and then
20581 (Has_Private_Declaration (Expected_Type)
20582 or else Has_Private_Declaration (Etype (Expr)))
20583 and then No (Parent (Expected_Type))
20584 then
20585 return;
20586
20587 elsif Nkind (Parent (Expr)) = N_Qualified_Expression
20588 and then Entity (Subtype_Mark (Parent (Expr))) = Expected_Type
20589 then
20590 return;
20591
20592 elsif Is_Private_Type (Expected_Type)
20593 and then Present (Full_View (Expected_Type))
20594 and then Covers (Full_View (Expected_Type), Etype (Expr))
20595 then
20596 return;
20597
20598 -- Conversely, type of expression may be the private one
20599
20600 elsif Is_Private_Type (Base_Type (Etype (Expr)))
20601 and then Full_View (Base_Type (Etype (Expr))) = Expected_Type
20602 then
20603 return;
20604 end if;
20605 end if;
20606
20607 -- An interesting special check. If the expression is parenthesized
20608 -- and its type corresponds to the type of the sole component of the
20609 -- expected record type, or to the component type of the expected one
20610 -- dimensional array type, then assume we have a bad aggregate attempt.
20611
20612 if Nkind (Expr) in N_Subexpr
20613 and then Paren_Count (Expr) /= 0
20614 and then Has_One_Matching_Field
20615 then
20616 Error_Msg_N ("positional aggregate cannot have one component", Expr);
20617
20618 if Present (Matching_Field) then
20619 if Is_Array_Type (Expec_Type) then
20620 Error_Msg_NE
20621 ("\write instead `&''First ='> ...`", Expr, Matching_Field);
20622 else
20623 Error_Msg_NE
20624 ("\write instead `& ='> ...`", Expr, Matching_Field);
20625 end if;
20626 end if;
20627
20628 -- Another special check, if we are looking for a pool-specific access
20629 -- type and we found an E_Access_Attribute_Type, then we have the case
20630 -- of an Access attribute being used in a context which needs a pool-
20631 -- specific type, which is never allowed. The one extra check we make
20632 -- is that the expected designated type covers the Found_Type.
20633
20634 elsif Is_Access_Type (Expec_Type)
20635 and then Ekind (Found_Type) = E_Access_Attribute_Type
20636 and then Ekind (Base_Type (Expec_Type)) /= E_General_Access_Type
20637 and then Ekind (Base_Type (Expec_Type)) /= E_Anonymous_Access_Type
20638 and then Covers
20639 (Designated_Type (Expec_Type), Designated_Type (Found_Type))
20640 then
20641 Error_Msg_N -- CODEFIX
20642 ("result must be general access type!", Expr);
20643 Error_Msg_NE -- CODEFIX
20644 ("add ALL to }!", Expr, Expec_Type);
20645
20646 -- Another special check, if the expected type is an integer type,
20647 -- but the expression is of type System.Address, and the parent is
20648 -- an addition or subtraction operation whose left operand is the
20649 -- expression in question and whose right operand is of an integral
20650 -- type, then this is an attempt at address arithmetic, so give
20651 -- appropriate message.
20652
20653 elsif Is_Integer_Type (Expec_Type)
20654 and then Is_RTE (Found_Type, RE_Address)
20655 and then Nkind_In (Parent (Expr), N_Op_Add, N_Op_Subtract)
20656 and then Expr = Left_Opnd (Parent (Expr))
20657 and then Is_Integer_Type (Etype (Right_Opnd (Parent (Expr))))
20658 then
20659 Error_Msg_N
20660 ("address arithmetic not predefined in package System",
20661 Parent (Expr));
20662 Error_Msg_N
20663 ("\possible missing with/use of System.Storage_Elements",
20664 Parent (Expr));
20665 return;
20666
20667 -- If the expected type is an anonymous access type, as for access
20668 -- parameters and discriminants, the error is on the designated types.
20669
20670 elsif Ekind (Expec_Type) = E_Anonymous_Access_Type then
20671 if Comes_From_Source (Expec_Type) then
20672 Error_Msg_NE ("expected}!", Expr, Expec_Type);
20673 else
20674 Error_Msg_NE
20675 ("expected an access type with designated}",
20676 Expr, Designated_Type (Expec_Type));
20677 end if;
20678
20679 if Is_Access_Type (Found_Type)
20680 and then not Comes_From_Source (Found_Type)
20681 then
20682 Error_Msg_NE
20683 ("\\found an access type with designated}!",
20684 Expr, Designated_Type (Found_Type));
20685 else
20686 if From_Limited_With (Found_Type) then
20687 Error_Msg_NE ("\\found incomplete}!", Expr, Found_Type);
20688 Error_Msg_Qual_Level := 99;
20689 Error_Msg_NE -- CODEFIX
20690 ("\\missing `WITH &;", Expr, Scope (Found_Type));
20691 Error_Msg_Qual_Level := 0;
20692 else
20693 Error_Msg_NE ("found}!", Expr, Found_Type);
20694 end if;
20695 end if;
20696
20697 -- Normal case of one type found, some other type expected
20698
20699 else
20700 -- If the names of the two types are the same, see if some number
20701 -- of levels of qualification will help. Don't try more than three
20702 -- levels, and if we get to standard, it's no use (and probably
20703 -- represents an error in the compiler) Also do not bother with
20704 -- internal scope names.
20705
20706 declare
20707 Expec_Scope : Entity_Id;
20708 Found_Scope : Entity_Id;
20709
20710 begin
20711 Expec_Scope := Expec_Type;
20712 Found_Scope := Found_Type;
20713
20714 for Levels in Nat range 0 .. 3 loop
20715 if Chars (Expec_Scope) /= Chars (Found_Scope) then
20716 Error_Msg_Qual_Level := Levels;
20717 exit;
20718 end if;
20719
20720 Expec_Scope := Scope (Expec_Scope);
20721 Found_Scope := Scope (Found_Scope);
20722
20723 exit when Expec_Scope = Standard_Standard
20724 or else Found_Scope = Standard_Standard
20725 or else not Comes_From_Source (Expec_Scope)
20726 or else not Comes_From_Source (Found_Scope);
20727 end loop;
20728 end;
20729
20730 if Is_Record_Type (Expec_Type)
20731 and then Present (Corresponding_Remote_Type (Expec_Type))
20732 then
20733 Error_Msg_NE ("expected}!", Expr,
20734 Corresponding_Remote_Type (Expec_Type));
20735 else
20736 Error_Msg_NE ("expected}!", Expr, Expec_Type);
20737 end if;
20738
20739 if Is_Entity_Name (Expr)
20740 and then Is_Package_Or_Generic_Package (Entity (Expr))
20741 then
20742 Error_Msg_N ("\\found package name!", Expr);
20743
20744 elsif Is_Entity_Name (Expr)
20745 and then Ekind_In (Entity (Expr), E_Procedure, E_Generic_Procedure)
20746 then
20747 if Ekind (Expec_Type) = E_Access_Subprogram_Type then
20748 Error_Msg_N
20749 ("found procedure name, possibly missing Access attribute!",
20750 Expr);
20751 else
20752 Error_Msg_N
20753 ("\\found procedure name instead of function!", Expr);
20754 end if;
20755
20756 elsif Nkind (Expr) = N_Function_Call
20757 and then Ekind (Expec_Type) = E_Access_Subprogram_Type
20758 and then Etype (Designated_Type (Expec_Type)) = Etype (Expr)
20759 and then No (Parameter_Associations (Expr))
20760 then
20761 Error_Msg_N
20762 ("found function name, possibly missing Access attribute!",
20763 Expr);
20764
20765 -- Catch common error: a prefix or infix operator which is not
20766 -- directly visible because the type isn't.
20767
20768 elsif Nkind (Expr) in N_Op
20769 and then Is_Overloaded (Expr)
20770 and then not Is_Immediately_Visible (Expec_Type)
20771 and then not Is_Potentially_Use_Visible (Expec_Type)
20772 and then not In_Use (Expec_Type)
20773 and then Has_Compatible_Type (Right_Opnd (Expr), Expec_Type)
20774 then
20775 Error_Msg_N
20776 ("operator of the type is not directly visible!", Expr);
20777
20778 elsif Ekind (Found_Type) = E_Void
20779 and then Present (Parent (Found_Type))
20780 and then Nkind (Parent (Found_Type)) = N_Full_Type_Declaration
20781 then
20782 Error_Msg_NE ("\\found premature usage of}!", Expr, Found_Type);
20783
20784 else
20785 Error_Msg_NE ("\\found}!", Expr, Found_Type);
20786 end if;
20787
20788 -- A special check for cases like M1 and M2 = 0 where M1 and M2 are
20789 -- of the same modular type, and (M1 and M2) = 0 was intended.
20790
20791 if Expec_Type = Standard_Boolean
20792 and then Is_Modular_Integer_Type (Found_Type)
20793 and then Nkind_In (Parent (Expr), N_Op_And, N_Op_Or, N_Op_Xor)
20794 and then Nkind (Right_Opnd (Parent (Expr))) in N_Op_Compare
20795 then
20796 declare
20797 Op : constant Node_Id := Right_Opnd (Parent (Expr));
20798 L : constant Node_Id := Left_Opnd (Op);
20799 R : constant Node_Id := Right_Opnd (Op);
20800
20801 begin
20802 -- The case for the message is when the left operand of the
20803 -- comparison is the same modular type, or when it is an
20804 -- integer literal (or other universal integer expression),
20805 -- which would have been typed as the modular type if the
20806 -- parens had been there.
20807
20808 if (Etype (L) = Found_Type
20809 or else
20810 Etype (L) = Universal_Integer)
20811 and then Is_Integer_Type (Etype (R))
20812 then
20813 Error_Msg_N
20814 ("\\possible missing parens for modular operation", Expr);
20815 end if;
20816 end;
20817 end if;
20818
20819 -- Reset error message qualification indication
20820
20821 Error_Msg_Qual_Level := 0;
20822 end if;
20823 end Wrong_Type;
20824
20825 --------------------------------
20826 -- Yields_Synchronized_Object --
20827 --------------------------------
20828
20829 function Yields_Synchronized_Object (Typ : Entity_Id) return Boolean is
20830 Has_Sync_Comp : Boolean := False;
20831 Id : Entity_Id;
20832
20833 begin
20834 -- An array type yields a synchronized object if its component type
20835 -- yields a synchronized object.
20836
20837 if Is_Array_Type (Typ) then
20838 return Yields_Synchronized_Object (Component_Type (Typ));
20839
20840 -- A descendant of type Ada.Synchronous_Task_Control.Suspension_Object
20841 -- yields a synchronized object by default.
20842
20843 elsif Is_Descendant_Of_Suspension_Object (Typ) then
20844 return True;
20845
20846 -- A protected type yields a synchronized object by default
20847
20848 elsif Is_Protected_Type (Typ) then
20849 return True;
20850
20851 -- A record type or type extension yields a synchronized object when its
20852 -- discriminants (if any) lack default values and all components are of
20853 -- a type that yelds a synchronized object.
20854
20855 elsif Is_Record_Type (Typ) then
20856
20857 -- Inspect all entities defined in the scope of the type, looking for
20858 -- components of a type that does not yeld a synchronized object or
20859 -- for discriminants with default values.
20860
20861 Id := First_Entity (Typ);
20862 while Present (Id) loop
20863 if Comes_From_Source (Id) then
20864 if Ekind (Id) = E_Component then
20865 if Yields_Synchronized_Object (Etype (Id)) then
20866 Has_Sync_Comp := True;
20867
20868 -- The component does not yield a synchronized object
20869
20870 else
20871 return False;
20872 end if;
20873
20874 elsif Ekind (Id) = E_Discriminant
20875 and then Present (Expression (Parent (Id)))
20876 then
20877 return False;
20878 end if;
20879 end if;
20880
20881 Next_Entity (Id);
20882 end loop;
20883
20884 -- Ensure that the parent type of a type extension yields a
20885 -- synchronized object.
20886
20887 if Etype (Typ) /= Typ
20888 and then not Yields_Synchronized_Object (Etype (Typ))
20889 then
20890 return False;
20891 end if;
20892
20893 -- If we get here, then all discriminants lack default values and all
20894 -- components are of a type that yields a synchronized object.
20895
20896 return Has_Sync_Comp;
20897
20898 -- A synchronized interface type yields a synchronized object by default
20899
20900 elsif Is_Synchronized_Interface (Typ) then
20901 return True;
20902
20903 -- A task type yelds a synchronized object by default
20904
20905 elsif Is_Task_Type (Typ) then
20906 return True;
20907
20908 -- Otherwise the type does not yield a synchronized object
20909
20910 else
20911 return False;
20912 end if;
20913 end Yields_Synchronized_Object;
20914
20915 end Sem_Util;
This page took 0.9436 seconds and 6 git commands to generate.