]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/sem_util.adb
[multiple changes]
[thirdparty/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-2016, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
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
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 type to
330 -- force the corresponding run-time check
331
332 if Is_Access_Type (Check_Typ)
333 and then Is_Local_Anonymous_Access (Check_Typ)
334 then
335 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
336 Analyze_And_Resolve (Exp, Check_Typ);
337 Check_Unset_Reference (Exp);
338 end if;
339
340 -- What follows is really expansion activity, so check that expansion
341 -- is on and is allowed. In GNATprove mode, we also want check flags to
342 -- be added in the tree, so that the formal verification can rely on
343 -- those to be present. In GNATprove mode for formal verification, some
344 -- treatment typically only done during expansion needs to be performed
345 -- on the tree, but it should not be applied inside generics. Otherwise,
346 -- this breaks the name resolution mechanism for generic instances.
347
348 if not Expander_Active
349 and (Inside_A_Generic or not Full_Analysis or not GNATprove_Mode)
350 then
351 return;
352 end if;
353
354 if Is_Access_Type (Check_Typ)
355 and then Can_Never_Be_Null (Check_Typ)
356 and then not Can_Never_Be_Null (Exp_Typ)
357 then
358 Install_Null_Excluding_Check (Exp);
359 end if;
360
361 -- First check if we have to insert discriminant checks
362
363 if Has_Discriminants (Exp_Typ) then
364 Apply_Discriminant_Check (Exp, Check_Typ);
365
366 -- Next emit length checks for array aggregates
367
368 elsif Is_Array_Type (Exp_Typ) then
369 Apply_Length_Check (Exp, Check_Typ);
370
371 -- Finally emit scalar and string checks. If we are dealing with a
372 -- scalar literal we need to check by hand because the Etype of
373 -- literals is not necessarily correct.
374
375 elsif Is_Scalar_Type (Exp_Typ)
376 and then Compile_Time_Known_Value (Exp)
377 then
378 if Is_Out_Of_Range (Exp, Base_Type (Check_Typ)) then
379 Apply_Compile_Time_Constraint_Error
380 (Exp, "value not in range of}??", CE_Range_Check_Failed,
381 Ent => Base_Type (Check_Typ),
382 Typ => Base_Type (Check_Typ));
383
384 elsif Is_Out_Of_Range (Exp, Check_Typ) then
385 Apply_Compile_Time_Constraint_Error
386 (Exp, "value not in range of}??", CE_Range_Check_Failed,
387 Ent => Check_Typ,
388 Typ => Check_Typ);
389
390 elsif not Range_Checks_Suppressed (Check_Typ) then
391 Apply_Scalar_Range_Check (Exp, Check_Typ);
392 end if;
393
394 -- Verify that target type is also scalar, to prevent view anomalies
395 -- in instantiations.
396
397 elsif (Is_Scalar_Type (Exp_Typ)
398 or else Nkind (Exp) = N_String_Literal)
399 and then Is_Scalar_Type (Check_Typ)
400 and then Exp_Typ /= Check_Typ
401 then
402 if Is_Entity_Name (Exp)
403 and then Ekind (Entity (Exp)) = E_Constant
404 then
405 -- If expression is a constant, it is worthwhile checking whether
406 -- it is a bound of the type.
407
408 if (Is_Entity_Name (Type_Low_Bound (Check_Typ))
409 and then Entity (Exp) = Entity (Type_Low_Bound (Check_Typ)))
410 or else
411 (Is_Entity_Name (Type_High_Bound (Check_Typ))
412 and then Entity (Exp) = Entity (Type_High_Bound (Check_Typ)))
413 then
414 return;
415
416 else
417 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
418 Analyze_And_Resolve (Exp, Check_Typ);
419 Check_Unset_Reference (Exp);
420 end if;
421
422 -- Could use a comment on this case ???
423
424 else
425 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
426 Analyze_And_Resolve (Exp, Check_Typ);
427 Check_Unset_Reference (Exp);
428 end if;
429
430 end if;
431 end Aggregate_Constraint_Checks;
432
433 -----------------------
434 -- Alignment_In_Bits --
435 -----------------------
436
437 function Alignment_In_Bits (E : Entity_Id) return Uint is
438 begin
439 return Alignment (E) * System_Storage_Unit;
440 end Alignment_In_Bits;
441
442 --------------------------------------
443 -- All_Composite_Constraints_Static --
444 --------------------------------------
445
446 function All_Composite_Constraints_Static
447 (Constr : Node_Id) return Boolean
448 is
449 begin
450 if No (Constr) or else Error_Posted (Constr) then
451 return True;
452 end if;
453
454 case Nkind (Constr) is
455 when N_Subexpr =>
456 if Nkind (Constr) in N_Has_Entity
457 and then Present (Entity (Constr))
458 then
459 if Is_Type (Entity (Constr)) then
460 return
461 not Is_Discrete_Type (Entity (Constr))
462 or else Is_OK_Static_Subtype (Entity (Constr));
463 end if;
464
465 elsif Nkind (Constr) = N_Range then
466 return
467 Is_OK_Static_Expression (Low_Bound (Constr))
468 and then
469 Is_OK_Static_Expression (High_Bound (Constr));
470
471 elsif Nkind (Constr) = N_Attribute_Reference
472 and then Attribute_Name (Constr) = Name_Range
473 then
474 return
475 Is_OK_Static_Expression
476 (Type_Low_Bound (Etype (Prefix (Constr))))
477 and then
478 Is_OK_Static_Expression
479 (Type_High_Bound (Etype (Prefix (Constr))));
480 end if;
481
482 return
483 not Present (Etype (Constr)) -- previous error
484 or else not Is_Discrete_Type (Etype (Constr))
485 or else Is_OK_Static_Expression (Constr);
486
487 when N_Discriminant_Association =>
488 return All_Composite_Constraints_Static (Expression (Constr));
489
490 when N_Range_Constraint =>
491 return
492 All_Composite_Constraints_Static (Range_Expression (Constr));
493
494 when N_Index_Or_Discriminant_Constraint =>
495 declare
496 One_Cstr : Entity_Id;
497 begin
498 One_Cstr := First (Constraints (Constr));
499 while Present (One_Cstr) loop
500 if not All_Composite_Constraints_Static (One_Cstr) then
501 return False;
502 end if;
503
504 Next (One_Cstr);
505 end loop;
506 end;
507
508 return True;
509
510 when N_Subtype_Indication =>
511 return
512 All_Composite_Constraints_Static (Subtype_Mark (Constr))
513 and then
514 All_Composite_Constraints_Static (Constraint (Constr));
515
516 when others =>
517 raise Program_Error;
518 end case;
519 end All_Composite_Constraints_Static;
520
521 ---------------------------------
522 -- Append_Inherited_Subprogram --
523 ---------------------------------
524
525 procedure Append_Inherited_Subprogram (S : Entity_Id) is
526 Par : constant Entity_Id := Alias (S);
527 -- The parent subprogram
528
529 Scop : constant Entity_Id := Scope (Par);
530 -- The scope of definition of the parent subprogram
531
532 Typ : constant Entity_Id := Defining_Entity (Parent (S));
533 -- The derived type of which S is a primitive operation
534
535 Decl : Node_Id;
536 Next_E : Entity_Id;
537
538 begin
539 if Ekind (Current_Scope) = E_Package
540 and then In_Private_Part (Current_Scope)
541 and then Has_Private_Declaration (Typ)
542 and then Is_Tagged_Type (Typ)
543 and then Scop = Current_Scope
544 then
545 -- The inherited operation is available at the earliest place after
546 -- the derived type declaration ( RM 7.3.1 (6/1)). This is only
547 -- relevant for type extensions. If the parent operation appears
548 -- after the type extension, the operation is not visible.
549
550 Decl := First
551 (Visible_Declarations
552 (Package_Specification (Current_Scope)));
553 while Present (Decl) loop
554 if Nkind (Decl) = N_Private_Extension_Declaration
555 and then Defining_Entity (Decl) = Typ
556 then
557 if Sloc (Decl) > Sloc (Par) then
558 Next_E := Next_Entity (Par);
559 Set_Next_Entity (Par, S);
560 Set_Next_Entity (S, Next_E);
561 return;
562
563 else
564 exit;
565 end if;
566 end if;
567
568 Next (Decl);
569 end loop;
570 end if;
571
572 -- If partial view is not a type extension, or it appears before the
573 -- subprogram declaration, insert normally at end of entity list.
574
575 Append_Entity (S, Current_Scope);
576 end Append_Inherited_Subprogram;
577
578 -----------------------------------------
579 -- Apply_Compile_Time_Constraint_Error --
580 -----------------------------------------
581
582 procedure Apply_Compile_Time_Constraint_Error
583 (N : Node_Id;
584 Msg : String;
585 Reason : RT_Exception_Code;
586 Ent : Entity_Id := Empty;
587 Typ : Entity_Id := Empty;
588 Loc : Source_Ptr := No_Location;
589 Rep : Boolean := True;
590 Warn : Boolean := False)
591 is
592 Stat : constant Boolean := Is_Static_Expression (N);
593 R_Stat : constant Node_Id :=
594 Make_Raise_Constraint_Error (Sloc (N), Reason => Reason);
595 Rtyp : Entity_Id;
596
597 begin
598 if No (Typ) then
599 Rtyp := Etype (N);
600 else
601 Rtyp := Typ;
602 end if;
603
604 Discard_Node
605 (Compile_Time_Constraint_Error (N, Msg, Ent, Loc, Warn => Warn));
606
607 -- In GNATprove mode, do not replace the node with an exception raised.
608 -- In such a case, either the call to Compile_Time_Constraint_Error
609 -- issues an error which stops analysis, or it issues a warning in
610 -- a few cases where a suitable check flag is set for GNATprove to
611 -- generate a check message.
612
613 if not Rep or GNATprove_Mode then
614 return;
615 end if;
616
617 -- Now we replace the node by an N_Raise_Constraint_Error node
618 -- This does not need reanalyzing, so set it as analyzed now.
619
620 Rewrite (N, R_Stat);
621 Set_Analyzed (N, True);
622
623 Set_Etype (N, Rtyp);
624 Set_Raises_Constraint_Error (N);
625
626 -- Now deal with possible local raise handling
627
628 Possible_Local_Raise (N, Standard_Constraint_Error);
629
630 -- If the original expression was marked as static, the result is
631 -- still marked as static, but the Raises_Constraint_Error flag is
632 -- always set so that further static evaluation is not attempted.
633
634 if Stat then
635 Set_Is_Static_Expression (N);
636 end if;
637 end Apply_Compile_Time_Constraint_Error;
638
639 ---------------------------
640 -- Async_Readers_Enabled --
641 ---------------------------
642
643 function Async_Readers_Enabled (Id : Entity_Id) return Boolean is
644 begin
645 return Has_Enabled_Property (Id, Name_Async_Readers);
646 end Async_Readers_Enabled;
647
648 ---------------------------
649 -- Async_Writers_Enabled --
650 ---------------------------
651
652 function Async_Writers_Enabled (Id : Entity_Id) return Boolean is
653 begin
654 return Has_Enabled_Property (Id, Name_Async_Writers);
655 end Async_Writers_Enabled;
656
657 --------------------------------------
658 -- Available_Full_View_Of_Component --
659 --------------------------------------
660
661 function Available_Full_View_Of_Component (T : Entity_Id) return Boolean is
662 ST : constant Entity_Id := Scope (T);
663 SCT : constant Entity_Id := Scope (Component_Type (T));
664 begin
665 return In_Open_Scopes (ST)
666 and then In_Open_Scopes (SCT)
667 and then Scope_Depth (ST) >= Scope_Depth (SCT);
668 end Available_Full_View_Of_Component;
669
670 -------------------
671 -- Bad_Attribute --
672 -------------------
673
674 procedure Bad_Attribute
675 (N : Node_Id;
676 Nam : Name_Id;
677 Warn : Boolean := False)
678 is
679 begin
680 Error_Msg_Warn := Warn;
681 Error_Msg_N ("unrecognized attribute&<<", N);
682
683 -- Check for possible misspelling
684
685 Error_Msg_Name_1 := First_Attribute_Name;
686 while Error_Msg_Name_1 <= Last_Attribute_Name loop
687 if Is_Bad_Spelling_Of (Nam, Error_Msg_Name_1) then
688 Error_Msg_N -- CODEFIX
689 ("\possible misspelling of %<<", N);
690 exit;
691 end if;
692
693 Error_Msg_Name_1 := Error_Msg_Name_1 + 1;
694 end loop;
695 end Bad_Attribute;
696
697 --------------------------------
698 -- Bad_Predicated_Subtype_Use --
699 --------------------------------
700
701 procedure Bad_Predicated_Subtype_Use
702 (Msg : String;
703 N : Node_Id;
704 Typ : Entity_Id;
705 Suggest_Static : Boolean := False)
706 is
707 Gen : Entity_Id;
708
709 begin
710 -- Avoid cascaded errors
711
712 if Error_Posted (N) then
713 return;
714 end if;
715
716 if Inside_A_Generic then
717 Gen := Current_Scope;
718 while Present (Gen) and then Ekind (Gen) /= E_Generic_Package loop
719 Gen := Scope (Gen);
720 end loop;
721
722 if No (Gen) then
723 return;
724 end if;
725
726 if Is_Generic_Formal (Typ) and then Is_Discrete_Type (Typ) then
727 Set_No_Predicate_On_Actual (Typ);
728 end if;
729
730 elsif Has_Predicates (Typ) then
731 if Is_Generic_Actual_Type (Typ) then
732
733 -- The restriction on loop parameters is only that the type
734 -- should have no dynamic predicates.
735
736 if Nkind (Parent (N)) = N_Loop_Parameter_Specification
737 and then not Has_Dynamic_Predicate_Aspect (Typ)
738 and then Is_OK_Static_Subtype (Typ)
739 then
740 return;
741 end if;
742
743 Gen := Current_Scope;
744 while not Is_Generic_Instance (Gen) loop
745 Gen := Scope (Gen);
746 end loop;
747
748 pragma Assert (Present (Gen));
749
750 if Ekind (Gen) = E_Package and then In_Package_Body (Gen) then
751 Error_Msg_Warn := SPARK_Mode /= On;
752 Error_Msg_FE (Msg & "<<", N, Typ);
753 Error_Msg_F ("\Program_Error [<<", N);
754
755 Insert_Action (N,
756 Make_Raise_Program_Error (Sloc (N),
757 Reason => PE_Bad_Predicated_Generic_Type));
758
759 else
760 Error_Msg_FE (Msg & "<<", N, Typ);
761 end if;
762
763 else
764 Error_Msg_FE (Msg, N, Typ);
765 end if;
766
767 -- Emit an optional suggestion on how to remedy the error if the
768 -- context warrants it.
769
770 if Suggest_Static and then Has_Static_Predicate (Typ) then
771 Error_Msg_FE ("\predicate of & should be marked static", N, Typ);
772 end if;
773 end if;
774 end Bad_Predicated_Subtype_Use;
775
776 -----------------------------------------
777 -- Bad_Unordered_Enumeration_Reference --
778 -----------------------------------------
779
780 function Bad_Unordered_Enumeration_Reference
781 (N : Node_Id;
782 T : Entity_Id) return Boolean
783 is
784 begin
785 return Is_Enumeration_Type (T)
786 and then Warn_On_Unordered_Enumeration_Type
787 and then not Is_Generic_Type (T)
788 and then Comes_From_Source (N)
789 and then not Has_Pragma_Ordered (T)
790 and then not In_Same_Extended_Unit (N, T);
791 end Bad_Unordered_Enumeration_Reference;
792
793 --------------------------
794 -- Build_Actual_Subtype --
795 --------------------------
796
797 function Build_Actual_Subtype
798 (T : Entity_Id;
799 N : Node_Or_Entity_Id) return Node_Id
800 is
801 Loc : Source_Ptr;
802 -- Normally Sloc (N), but may point to corresponding body in some cases
803
804 Constraints : List_Id;
805 Decl : Node_Id;
806 Discr : Entity_Id;
807 Hi : Node_Id;
808 Lo : Node_Id;
809 Subt : Entity_Id;
810 Disc_Type : Entity_Id;
811 Obj : Node_Id;
812
813 begin
814 Loc := Sloc (N);
815
816 if Nkind (N) = N_Defining_Identifier then
817 Obj := New_Occurrence_Of (N, Loc);
818
819 -- If this is a formal parameter of a subprogram declaration, and
820 -- we are compiling the body, we want the declaration for the
821 -- actual subtype to carry the source position of the body, to
822 -- prevent anomalies in gdb when stepping through the code.
823
824 if Is_Formal (N) then
825 declare
826 Decl : constant Node_Id := Unit_Declaration_Node (Scope (N));
827 begin
828 if Nkind (Decl) = N_Subprogram_Declaration
829 and then Present (Corresponding_Body (Decl))
830 then
831 Loc := Sloc (Corresponding_Body (Decl));
832 end if;
833 end;
834 end if;
835
836 else
837 Obj := N;
838 end if;
839
840 if Is_Array_Type (T) then
841 Constraints := New_List;
842 for J in 1 .. Number_Dimensions (T) loop
843
844 -- Build an array subtype declaration with the nominal subtype and
845 -- the bounds of the actual. Add the declaration in front of the
846 -- local declarations for the subprogram, for analysis before any
847 -- reference to the formal in the body.
848
849 Lo :=
850 Make_Attribute_Reference (Loc,
851 Prefix =>
852 Duplicate_Subexpr_No_Checks (Obj, Name_Req => True),
853 Attribute_Name => Name_First,
854 Expressions => New_List (
855 Make_Integer_Literal (Loc, J)));
856
857 Hi :=
858 Make_Attribute_Reference (Loc,
859 Prefix =>
860 Duplicate_Subexpr_No_Checks (Obj, Name_Req => True),
861 Attribute_Name => Name_Last,
862 Expressions => New_List (
863 Make_Integer_Literal (Loc, J)));
864
865 Append (Make_Range (Loc, Lo, Hi), Constraints);
866 end loop;
867
868 -- If the type has unknown discriminants there is no constrained
869 -- subtype to build. This is never called for a formal or for a
870 -- lhs, so returning the type is ok ???
871
872 elsif Has_Unknown_Discriminants (T) then
873 return T;
874
875 else
876 Constraints := New_List;
877
878 -- Type T is a generic derived type, inherit the discriminants from
879 -- the parent type.
880
881 if Is_Private_Type (T)
882 and then No (Full_View (T))
883
884 -- T was flagged as an error if it was declared as a formal
885 -- derived type with known discriminants. In this case there
886 -- is no need to look at the parent type since T already carries
887 -- its own discriminants.
888
889 and then not Error_Posted (T)
890 then
891 Disc_Type := Etype (Base_Type (T));
892 else
893 Disc_Type := T;
894 end if;
895
896 Discr := First_Discriminant (Disc_Type);
897 while Present (Discr) loop
898 Append_To (Constraints,
899 Make_Selected_Component (Loc,
900 Prefix =>
901 Duplicate_Subexpr_No_Checks (Obj),
902 Selector_Name => New_Occurrence_Of (Discr, Loc)));
903 Next_Discriminant (Discr);
904 end loop;
905 end if;
906
907 Subt := Make_Temporary (Loc, 'S', Related_Node => N);
908 Set_Is_Internal (Subt);
909
910 Decl :=
911 Make_Subtype_Declaration (Loc,
912 Defining_Identifier => Subt,
913 Subtype_Indication =>
914 Make_Subtype_Indication (Loc,
915 Subtype_Mark => New_Occurrence_Of (T, Loc),
916 Constraint =>
917 Make_Index_Or_Discriminant_Constraint (Loc,
918 Constraints => Constraints)));
919
920 Mark_Rewrite_Insertion (Decl);
921 return Decl;
922 end Build_Actual_Subtype;
923
924 ---------------------------------------
925 -- Build_Actual_Subtype_Of_Component --
926 ---------------------------------------
927
928 function Build_Actual_Subtype_Of_Component
929 (T : Entity_Id;
930 N : Node_Id) return Node_Id
931 is
932 Loc : constant Source_Ptr := Sloc (N);
933 P : constant Node_Id := Prefix (N);
934 D : Elmt_Id;
935 Id : Node_Id;
936 Index_Typ : Entity_Id;
937
938 Desig_Typ : Entity_Id;
939 -- This is either a copy of T, or if T is an access type, then it is
940 -- the directly designated type of this access type.
941
942 function Build_Actual_Array_Constraint return List_Id;
943 -- If one or more of the bounds of the component depends on
944 -- discriminants, build actual constraint using the discriminants
945 -- of the prefix.
946
947 function Build_Actual_Record_Constraint return List_Id;
948 -- Similar to previous one, for discriminated components constrained
949 -- by the discriminant of the enclosing object.
950
951 -----------------------------------
952 -- Build_Actual_Array_Constraint --
953 -----------------------------------
954
955 function Build_Actual_Array_Constraint return List_Id is
956 Constraints : constant List_Id := New_List;
957 Indx : Node_Id;
958 Hi : Node_Id;
959 Lo : Node_Id;
960 Old_Hi : Node_Id;
961 Old_Lo : Node_Id;
962
963 begin
964 Indx := First_Index (Desig_Typ);
965 while Present (Indx) loop
966 Old_Lo := Type_Low_Bound (Etype (Indx));
967 Old_Hi := Type_High_Bound (Etype (Indx));
968
969 if Denotes_Discriminant (Old_Lo) then
970 Lo :=
971 Make_Selected_Component (Loc,
972 Prefix => New_Copy_Tree (P),
973 Selector_Name => New_Occurrence_Of (Entity (Old_Lo), Loc));
974
975 else
976 Lo := New_Copy_Tree (Old_Lo);
977
978 -- The new bound will be reanalyzed in the enclosing
979 -- declaration. For literal bounds that come from a type
980 -- declaration, the type of the context must be imposed, so
981 -- insure that analysis will take place. For non-universal
982 -- types this is not strictly necessary.
983
984 Set_Analyzed (Lo, False);
985 end if;
986
987 if Denotes_Discriminant (Old_Hi) then
988 Hi :=
989 Make_Selected_Component (Loc,
990 Prefix => New_Copy_Tree (P),
991 Selector_Name => New_Occurrence_Of (Entity (Old_Hi), Loc));
992
993 else
994 Hi := New_Copy_Tree (Old_Hi);
995 Set_Analyzed (Hi, False);
996 end if;
997
998 Append (Make_Range (Loc, Lo, Hi), Constraints);
999 Next_Index (Indx);
1000 end loop;
1001
1002 return Constraints;
1003 end Build_Actual_Array_Constraint;
1004
1005 ------------------------------------
1006 -- Build_Actual_Record_Constraint --
1007 ------------------------------------
1008
1009 function Build_Actual_Record_Constraint return List_Id is
1010 Constraints : constant List_Id := New_List;
1011 D : Elmt_Id;
1012 D_Val : Node_Id;
1013
1014 begin
1015 D := First_Elmt (Discriminant_Constraint (Desig_Typ));
1016 while Present (D) loop
1017 if Denotes_Discriminant (Node (D)) then
1018 D_Val := Make_Selected_Component (Loc,
1019 Prefix => New_Copy_Tree (P),
1020 Selector_Name => New_Occurrence_Of (Entity (Node (D)), Loc));
1021
1022 else
1023 D_Val := New_Copy_Tree (Node (D));
1024 end if;
1025
1026 Append (D_Val, Constraints);
1027 Next_Elmt (D);
1028 end loop;
1029
1030 return Constraints;
1031 end Build_Actual_Record_Constraint;
1032
1033 -- Start of processing for Build_Actual_Subtype_Of_Component
1034
1035 begin
1036 -- Why the test for Spec_Expression mode here???
1037
1038 if In_Spec_Expression then
1039 return Empty;
1040
1041 -- More comments for the rest of this body would be good ???
1042
1043 elsif Nkind (N) = N_Explicit_Dereference then
1044 if Is_Composite_Type (T)
1045 and then not Is_Constrained (T)
1046 and then not (Is_Class_Wide_Type (T)
1047 and then Is_Constrained (Root_Type (T)))
1048 and then not Has_Unknown_Discriminants (T)
1049 then
1050 -- If the type of the dereference is already constrained, it is an
1051 -- actual subtype.
1052
1053 if Is_Array_Type (Etype (N))
1054 and then Is_Constrained (Etype (N))
1055 then
1056 return Empty;
1057 else
1058 Remove_Side_Effects (P);
1059 return Build_Actual_Subtype (T, N);
1060 end if;
1061 else
1062 return Empty;
1063 end if;
1064 end if;
1065
1066 if Ekind (T) = E_Access_Subtype then
1067 Desig_Typ := Designated_Type (T);
1068 else
1069 Desig_Typ := T;
1070 end if;
1071
1072 if Ekind (Desig_Typ) = E_Array_Subtype then
1073 Id := First_Index (Desig_Typ);
1074 while Present (Id) loop
1075 Index_Typ := Underlying_Type (Etype (Id));
1076
1077 if Denotes_Discriminant (Type_Low_Bound (Index_Typ))
1078 or else
1079 Denotes_Discriminant (Type_High_Bound (Index_Typ))
1080 then
1081 Remove_Side_Effects (P);
1082 return
1083 Build_Component_Subtype
1084 (Build_Actual_Array_Constraint, Loc, Base_Type (T));
1085 end if;
1086
1087 Next_Index (Id);
1088 end loop;
1089
1090 elsif Is_Composite_Type (Desig_Typ)
1091 and then Has_Discriminants (Desig_Typ)
1092 and then not Has_Unknown_Discriminants (Desig_Typ)
1093 then
1094 if Is_Private_Type (Desig_Typ)
1095 and then No (Discriminant_Constraint (Desig_Typ))
1096 then
1097 Desig_Typ := Full_View (Desig_Typ);
1098 end if;
1099
1100 D := First_Elmt (Discriminant_Constraint (Desig_Typ));
1101 while Present (D) loop
1102 if Denotes_Discriminant (Node (D)) then
1103 Remove_Side_Effects (P);
1104 return
1105 Build_Component_Subtype (
1106 Build_Actual_Record_Constraint, Loc, Base_Type (T));
1107 end if;
1108
1109 Next_Elmt (D);
1110 end loop;
1111 end if;
1112
1113 -- If none of the above, the actual and nominal subtypes are the same
1114
1115 return Empty;
1116 end Build_Actual_Subtype_Of_Component;
1117
1118 -----------------------------
1119 -- Build_Component_Subtype --
1120 -----------------------------
1121
1122 function Build_Component_Subtype
1123 (C : List_Id;
1124 Loc : Source_Ptr;
1125 T : Entity_Id) return Node_Id
1126 is
1127 Subt : Entity_Id;
1128 Decl : Node_Id;
1129
1130 begin
1131 -- Unchecked_Union components do not require component subtypes
1132
1133 if Is_Unchecked_Union (T) then
1134 return Empty;
1135 end if;
1136
1137 Subt := Make_Temporary (Loc, 'S');
1138 Set_Is_Internal (Subt);
1139
1140 Decl :=
1141 Make_Subtype_Declaration (Loc,
1142 Defining_Identifier => Subt,
1143 Subtype_Indication =>
1144 Make_Subtype_Indication (Loc,
1145 Subtype_Mark => New_Occurrence_Of (Base_Type (T), Loc),
1146 Constraint =>
1147 Make_Index_Or_Discriminant_Constraint (Loc,
1148 Constraints => C)));
1149
1150 Mark_Rewrite_Insertion (Decl);
1151 return Decl;
1152 end Build_Component_Subtype;
1153
1154 ----------------------------------
1155 -- Build_Default_Init_Cond_Call --
1156 ----------------------------------
1157
1158 function Build_Default_Init_Cond_Call
1159 (Loc : Source_Ptr;
1160 Obj_Id : Entity_Id;
1161 Typ : Entity_Id) return Node_Id
1162 is
1163 Proc_Id : constant Entity_Id := Default_Init_Cond_Procedure (Typ);
1164 Formal_Typ : constant Entity_Id := Etype (First_Formal (Proc_Id));
1165
1166 begin
1167 return
1168 Make_Procedure_Call_Statement (Loc,
1169 Name => New_Occurrence_Of (Proc_Id, Loc),
1170 Parameter_Associations => New_List (
1171 Make_Unchecked_Type_Conversion (Loc,
1172 Subtype_Mark => New_Occurrence_Of (Formal_Typ, Loc),
1173 Expression => New_Occurrence_Of (Obj_Id, Loc))));
1174 end Build_Default_Init_Cond_Call;
1175
1176 ----------------------------------------------
1177 -- Build_Default_Init_Cond_Procedure_Bodies --
1178 ----------------------------------------------
1179
1180 procedure Build_Default_Init_Cond_Procedure_Bodies (Priv_Decls : List_Id) is
1181 procedure Build_Default_Init_Cond_Procedure_Body (Typ : Entity_Id);
1182 -- If type Typ is subject to pragma Default_Initial_Condition, build the
1183 -- body of the procedure which verifies the assumption of the pragma at
1184 -- run time. The generated body is added after the type declaration.
1185
1186 --------------------------------------------
1187 -- Build_Default_Init_Cond_Procedure_Body --
1188 --------------------------------------------
1189
1190 procedure Build_Default_Init_Cond_Procedure_Body (Typ : Entity_Id) is
1191 Param_Id : Entity_Id;
1192 -- The entity of the sole formal parameter of the default initial
1193 -- condition procedure.
1194
1195 procedure Replace_Type_Reference (N : Node_Id);
1196 -- Replace a single reference to type Typ with a reference to formal
1197 -- parameter Param_Id.
1198
1199 ----------------------------
1200 -- Replace_Type_Reference --
1201 ----------------------------
1202
1203 procedure Replace_Type_Reference (N : Node_Id) is
1204 begin
1205 Rewrite (N, New_Occurrence_Of (Param_Id, Sloc (N)));
1206 end Replace_Type_Reference;
1207
1208 procedure Replace_Type_References is
1209 new Replace_Type_References_Generic (Replace_Type_Reference);
1210
1211 -- Local variables
1212
1213 Loc : constant Source_Ptr := Sloc (Typ);
1214 Prag : constant Node_Id :=
1215 Get_Pragma (Typ, Pragma_Default_Initial_Condition);
1216 Proc_Id : constant Entity_Id := Default_Init_Cond_Procedure (Typ);
1217 Body_Decl : Node_Id;
1218 Expr : Node_Id;
1219 Spec_Decl : Node_Id;
1220 Stmt : Node_Id;
1221
1222 Save_Ghost_Mode : constant Ghost_Mode_Type := Ghost_Mode;
1223
1224 -- Start of processing for Build_Default_Init_Cond_Procedure_Body
1225
1226 begin
1227 -- The procedure should be generated only for [sub]types subject to
1228 -- pragma Default_Initial_Condition. Types that inherit the pragma do
1229 -- not get this specialized procedure.
1230
1231 pragma Assert (Has_Default_Init_Cond (Typ));
1232 pragma Assert (Present (Prag));
1233
1234 -- Nothing to do if the spec was not built. This occurs when the
1235 -- expression of the Default_Initial_Condition is missing or is
1236 -- null.
1237
1238 if No (Proc_Id) then
1239 return;
1240
1241 -- Nothing to do if the body was already built
1242
1243 elsif Present (Corresponding_Body (Unit_Declaration_Node (Proc_Id)))
1244 then
1245 return;
1246 end if;
1247
1248 -- The related type may be subject to pragma Ghost. Set the mode now
1249 -- to ensure that the analysis and expansion produce Ghost nodes.
1250
1251 Set_Ghost_Mode_From_Entity (Typ);
1252
1253 Param_Id := First_Formal (Proc_Id);
1254
1255 -- The pragma has an argument. Note that the argument is analyzed
1256 -- after all references to the current instance of the type are
1257 -- replaced.
1258
1259 if Present (Pragma_Argument_Associations (Prag)) then
1260 Expr :=
1261 Get_Pragma_Arg (First (Pragma_Argument_Associations (Prag)));
1262
1263 if Nkind (Expr) = N_Null then
1264 Stmt := Make_Null_Statement (Loc);
1265
1266 -- Preserve the original argument of the pragma by replicating it.
1267 -- Replace all references to the current instance of the type with
1268 -- references to the formal parameter.
1269
1270 else
1271 Expr := New_Copy_Tree (Expr);
1272 Replace_Type_References (Expr, Typ);
1273
1274 -- Generate:
1275 -- pragma Check (Default_Initial_Condition, <Expr>);
1276
1277 Stmt :=
1278 Make_Pragma (Loc,
1279 Pragma_Identifier =>
1280 Make_Identifier (Loc, Name_Check),
1281
1282 Pragma_Argument_Associations => New_List (
1283 Make_Pragma_Argument_Association (Loc,
1284 Expression =>
1285 Make_Identifier (Loc,
1286 Chars => Name_Default_Initial_Condition)),
1287 Make_Pragma_Argument_Association (Loc,
1288 Expression => Expr)));
1289 end if;
1290
1291 -- Otherwise the pragma appears without an argument
1292
1293 else
1294 Stmt := Make_Null_Statement (Loc);
1295 end if;
1296
1297 -- Generate:
1298 -- procedure <Typ>Default_Init_Cond (I : <Typ>) is
1299 -- begin
1300 -- <Stmt>;
1301 -- end <Typ>Default_Init_Cond;
1302
1303 Spec_Decl := Unit_Declaration_Node (Proc_Id);
1304 Body_Decl :=
1305 Make_Subprogram_Body (Loc,
1306 Specification =>
1307 Copy_Separate_Tree (Specification (Spec_Decl)),
1308 Declarations => Empty_List,
1309 Handled_Statement_Sequence =>
1310 Make_Handled_Sequence_Of_Statements (Loc,
1311 Statements => New_List (Stmt)));
1312
1313 -- Link the spec and body of the default initial condition procedure
1314 -- to prevent the generation of a duplicate body.
1315
1316 Set_Corresponding_Body (Spec_Decl, Defining_Entity (Body_Decl));
1317 Set_Corresponding_Spec (Body_Decl, Proc_Id);
1318
1319 Insert_After_And_Analyze (Declaration_Node (Typ), Body_Decl);
1320 Ghost_Mode := Save_Ghost_Mode;
1321 end Build_Default_Init_Cond_Procedure_Body;
1322
1323 -- Local variables
1324
1325 Decl : Node_Id;
1326 Typ : Entity_Id;
1327
1328 -- Start of processing for Build_Default_Init_Cond_Procedure_Bodies
1329
1330 begin
1331 -- Inspect the private declarations looking for [sub]type declarations
1332
1333 Decl := First (Priv_Decls);
1334 while Present (Decl) loop
1335 if Nkind_In (Decl, N_Full_Type_Declaration,
1336 N_Subtype_Declaration)
1337 then
1338 Typ := Defining_Entity (Decl);
1339
1340 -- Guard against partially decorate types due to previous errors
1341
1342 if Is_Type (Typ) then
1343
1344 -- If the type is subject to pragma Default_Initial_Condition,
1345 -- generate the body of the internal procedure which verifies
1346 -- the assertion of the pragma at run time.
1347
1348 if Has_Default_Init_Cond (Typ) then
1349 Build_Default_Init_Cond_Procedure_Body (Typ);
1350
1351 -- A derived type inherits the default initial condition
1352 -- procedure from its parent type.
1353
1354 elsif Has_Inherited_Default_Init_Cond (Typ) then
1355 Inherit_Default_Init_Cond_Procedure (Typ);
1356 end if;
1357 end if;
1358 end if;
1359
1360 Next (Decl);
1361 end loop;
1362 end Build_Default_Init_Cond_Procedure_Bodies;
1363
1364 ---------------------------------------------------
1365 -- Build_Default_Init_Cond_Procedure_Declaration --
1366 ---------------------------------------------------
1367
1368 procedure Build_Default_Init_Cond_Procedure_Declaration (Typ : Entity_Id) is
1369 Loc : constant Source_Ptr := Sloc (Typ);
1370 Prag : constant Node_Id :=
1371 Get_Pragma (Typ, Pragma_Default_Initial_Condition);
1372
1373 Save_Ghost_Mode : constant Ghost_Mode_Type := Ghost_Mode;
1374
1375 Args : List_Id;
1376 Proc_Id : Entity_Id;
1377
1378 begin
1379 -- The procedure should be generated only for types subject to pragma
1380 -- Default_Initial_Condition. Types that inherit the pragma do not get
1381 -- this specialized procedure.
1382
1383 pragma Assert (Has_Default_Init_Cond (Typ));
1384 pragma Assert (Present (Prag));
1385
1386 Args := Pragma_Argument_Associations (Prag);
1387
1388 -- Nothing to do if default initial condition procedure already built
1389
1390 if Present (Default_Init_Cond_Procedure (Typ)) then
1391 return;
1392
1393 -- Nothing to do if the default initial condition appears without an
1394 -- expression.
1395
1396 elsif No (Args) then
1397 return;
1398
1399 -- Nothing to do if the expression of the default initial condition is
1400 -- null.
1401
1402 elsif Nkind (Get_Pragma_Arg (First (Args))) = N_Null then
1403 return;
1404 end if;
1405
1406 -- The related type may be subject to pragma Ghost. Set the mode now to
1407 -- ensure that the analysis and expansion produce Ghost nodes.
1408
1409 Set_Ghost_Mode_From_Entity (Typ);
1410
1411 Proc_Id :=
1412 Make_Defining_Identifier (Loc,
1413 Chars => New_External_Name (Chars (Typ), "Default_Init_Cond"));
1414
1415 -- Associate default initial condition procedure with the private type
1416
1417 Set_Ekind (Proc_Id, E_Procedure);
1418 Set_Is_Default_Init_Cond_Procedure (Proc_Id);
1419 Set_Default_Init_Cond_Procedure (Typ, Proc_Id);
1420
1421 -- Mark the default initial condition procedure explicitly as Ghost
1422 -- because it does not come from source.
1423
1424 if Ghost_Mode > None then
1425 Set_Is_Ghost_Entity (Proc_Id);
1426 end if;
1427
1428 -- Generate:
1429 -- procedure <Typ>Default_Init_Cond (Inn : <Typ>);
1430
1431 Insert_After_And_Analyze (Prag,
1432 Make_Subprogram_Declaration (Loc,
1433 Specification =>
1434 Make_Procedure_Specification (Loc,
1435 Defining_Unit_Name => Proc_Id,
1436 Parameter_Specifications => New_List (
1437 Make_Parameter_Specification (Loc,
1438 Defining_Identifier => Make_Temporary (Loc, 'I'),
1439 Parameter_Type => New_Occurrence_Of (Typ, Loc))))));
1440
1441 Ghost_Mode := Save_Ghost_Mode;
1442 end Build_Default_Init_Cond_Procedure_Declaration;
1443
1444 ---------------------------
1445 -- Build_Default_Subtype --
1446 ---------------------------
1447
1448 function Build_Default_Subtype
1449 (T : Entity_Id;
1450 N : Node_Id) return Entity_Id
1451 is
1452 Loc : constant Source_Ptr := Sloc (N);
1453 Disc : Entity_Id;
1454
1455 Bas : Entity_Id;
1456 -- The base type that is to be constrained by the defaults
1457
1458 begin
1459 if not Has_Discriminants (T) or else Is_Constrained (T) then
1460 return T;
1461 end if;
1462
1463 Bas := Base_Type (T);
1464
1465 -- If T is non-private but its base type is private, this is the
1466 -- completion of a subtype declaration whose parent type is private
1467 -- (see Complete_Private_Subtype in Sem_Ch3). The proper discriminants
1468 -- are to be found in the full view of the base. Check that the private
1469 -- status of T and its base differ.
1470
1471 if Is_Private_Type (Bas)
1472 and then not Is_Private_Type (T)
1473 and then Present (Full_View (Bas))
1474 then
1475 Bas := Full_View (Bas);
1476 end if;
1477
1478 Disc := First_Discriminant (T);
1479
1480 if No (Discriminant_Default_Value (Disc)) then
1481 return T;
1482 end if;
1483
1484 declare
1485 Act : constant Entity_Id := Make_Temporary (Loc, 'S');
1486 Constraints : constant List_Id := New_List;
1487 Decl : Node_Id;
1488
1489 begin
1490 while Present (Disc) loop
1491 Append_To (Constraints,
1492 New_Copy_Tree (Discriminant_Default_Value (Disc)));
1493 Next_Discriminant (Disc);
1494 end loop;
1495
1496 Decl :=
1497 Make_Subtype_Declaration (Loc,
1498 Defining_Identifier => Act,
1499 Subtype_Indication =>
1500 Make_Subtype_Indication (Loc,
1501 Subtype_Mark => New_Occurrence_Of (Bas, Loc),
1502 Constraint =>
1503 Make_Index_Or_Discriminant_Constraint (Loc,
1504 Constraints => Constraints)));
1505
1506 Insert_Action (N, Decl);
1507
1508 -- If the context is a component declaration the subtype declaration
1509 -- will be analyzed when the enclosing type is frozen, otherwise do
1510 -- it now.
1511
1512 if Ekind (Current_Scope) /= E_Record_Type then
1513 Analyze (Decl);
1514 end if;
1515
1516 return Act;
1517 end;
1518 end Build_Default_Subtype;
1519
1520 --------------------------------------------
1521 -- Build_Discriminal_Subtype_Of_Component --
1522 --------------------------------------------
1523
1524 function Build_Discriminal_Subtype_Of_Component
1525 (T : Entity_Id) return Node_Id
1526 is
1527 Loc : constant Source_Ptr := Sloc (T);
1528 D : Elmt_Id;
1529 Id : Node_Id;
1530
1531 function Build_Discriminal_Array_Constraint return List_Id;
1532 -- If one or more of the bounds of the component depends on
1533 -- discriminants, build actual constraint using the discriminants
1534 -- of the prefix.
1535
1536 function Build_Discriminal_Record_Constraint return List_Id;
1537 -- Similar to previous one, for discriminated components constrained by
1538 -- the discriminant of the enclosing object.
1539
1540 ----------------------------------------
1541 -- Build_Discriminal_Array_Constraint --
1542 ----------------------------------------
1543
1544 function Build_Discriminal_Array_Constraint return List_Id is
1545 Constraints : constant List_Id := New_List;
1546 Indx : Node_Id;
1547 Hi : Node_Id;
1548 Lo : Node_Id;
1549 Old_Hi : Node_Id;
1550 Old_Lo : Node_Id;
1551
1552 begin
1553 Indx := First_Index (T);
1554 while Present (Indx) loop
1555 Old_Lo := Type_Low_Bound (Etype (Indx));
1556 Old_Hi := Type_High_Bound (Etype (Indx));
1557
1558 if Denotes_Discriminant (Old_Lo) then
1559 Lo := New_Occurrence_Of (Discriminal (Entity (Old_Lo)), Loc);
1560
1561 else
1562 Lo := New_Copy_Tree (Old_Lo);
1563 end if;
1564
1565 if Denotes_Discriminant (Old_Hi) then
1566 Hi := New_Occurrence_Of (Discriminal (Entity (Old_Hi)), Loc);
1567
1568 else
1569 Hi := New_Copy_Tree (Old_Hi);
1570 end if;
1571
1572 Append (Make_Range (Loc, Lo, Hi), Constraints);
1573 Next_Index (Indx);
1574 end loop;
1575
1576 return Constraints;
1577 end Build_Discriminal_Array_Constraint;
1578
1579 -----------------------------------------
1580 -- Build_Discriminal_Record_Constraint --
1581 -----------------------------------------
1582
1583 function Build_Discriminal_Record_Constraint return List_Id is
1584 Constraints : constant List_Id := New_List;
1585 D : Elmt_Id;
1586 D_Val : Node_Id;
1587
1588 begin
1589 D := First_Elmt (Discriminant_Constraint (T));
1590 while Present (D) loop
1591 if Denotes_Discriminant (Node (D)) then
1592 D_Val :=
1593 New_Occurrence_Of (Discriminal (Entity (Node (D))), Loc);
1594 else
1595 D_Val := New_Copy_Tree (Node (D));
1596 end if;
1597
1598 Append (D_Val, Constraints);
1599 Next_Elmt (D);
1600 end loop;
1601
1602 return Constraints;
1603 end Build_Discriminal_Record_Constraint;
1604
1605 -- Start of processing for Build_Discriminal_Subtype_Of_Component
1606
1607 begin
1608 if Ekind (T) = E_Array_Subtype then
1609 Id := First_Index (T);
1610 while Present (Id) loop
1611 if Denotes_Discriminant (Type_Low_Bound (Etype (Id)))
1612 or else
1613 Denotes_Discriminant (Type_High_Bound (Etype (Id)))
1614 then
1615 return Build_Component_Subtype
1616 (Build_Discriminal_Array_Constraint, Loc, T);
1617 end if;
1618
1619 Next_Index (Id);
1620 end loop;
1621
1622 elsif Ekind (T) = E_Record_Subtype
1623 and then Has_Discriminants (T)
1624 and then not Has_Unknown_Discriminants (T)
1625 then
1626 D := First_Elmt (Discriminant_Constraint (T));
1627 while Present (D) loop
1628 if Denotes_Discriminant (Node (D)) then
1629 return Build_Component_Subtype
1630 (Build_Discriminal_Record_Constraint, Loc, T);
1631 end if;
1632
1633 Next_Elmt (D);
1634 end loop;
1635 end if;
1636
1637 -- If none of the above, the actual and nominal subtypes are the same
1638
1639 return Empty;
1640 end Build_Discriminal_Subtype_Of_Component;
1641
1642 ------------------------------
1643 -- Build_Elaboration_Entity --
1644 ------------------------------
1645
1646 procedure Build_Elaboration_Entity (N : Node_Id; Spec_Id : Entity_Id) is
1647 Loc : constant Source_Ptr := Sloc (N);
1648 Decl : Node_Id;
1649 Elab_Ent : Entity_Id;
1650
1651 procedure Set_Package_Name (Ent : Entity_Id);
1652 -- Given an entity, sets the fully qualified name of the entity in
1653 -- Name_Buffer, with components separated by double underscores. This
1654 -- is a recursive routine that climbs the scope chain to Standard.
1655
1656 ----------------------
1657 -- Set_Package_Name --
1658 ----------------------
1659
1660 procedure Set_Package_Name (Ent : Entity_Id) is
1661 begin
1662 if Scope (Ent) /= Standard_Standard then
1663 Set_Package_Name (Scope (Ent));
1664
1665 declare
1666 Nam : constant String := Get_Name_String (Chars (Ent));
1667 begin
1668 Name_Buffer (Name_Len + 1) := '_';
1669 Name_Buffer (Name_Len + 2) := '_';
1670 Name_Buffer (Name_Len + 3 .. Name_Len + Nam'Length + 2) := Nam;
1671 Name_Len := Name_Len + Nam'Length + 2;
1672 end;
1673
1674 else
1675 Get_Name_String (Chars (Ent));
1676 end if;
1677 end Set_Package_Name;
1678
1679 -- Start of processing for Build_Elaboration_Entity
1680
1681 begin
1682 -- Ignore call if already constructed
1683
1684 if Present (Elaboration_Entity (Spec_Id)) then
1685 return;
1686
1687 -- Ignore in ASIS mode, elaboration entity is not in source and plays
1688 -- no role in analysis.
1689
1690 elsif ASIS_Mode then
1691 return;
1692
1693 -- See if we need elaboration entity.
1694
1695 -- We always need an elaboration entity when preserving control flow, as
1696 -- we want to remain explicit about the unit's elaboration order.
1697
1698 elsif Opt.Suppress_Control_Flow_Optimizations then
1699 null;
1700
1701 -- We always need an elaboration entity for the dynamic elaboration
1702 -- model, since it is needed to properly generate the PE exception for
1703 -- access before elaboration.
1704
1705 elsif Dynamic_Elaboration_Checks then
1706 null;
1707
1708 -- For the static model, we don't need the elaboration counter if this
1709 -- unit is sure to have no elaboration code, since that means there
1710 -- is no elaboration unit to be called. Note that we can't just decide
1711 -- after the fact by looking to see whether there was elaboration code,
1712 -- because that's too late to make this decision.
1713
1714 elsif Restriction_Active (No_Elaboration_Code) then
1715 return;
1716
1717 -- Similarly, for the static model, we can skip the elaboration counter
1718 -- if we have the No_Multiple_Elaboration restriction, since for the
1719 -- static model, that's the only purpose of the counter (to avoid
1720 -- multiple elaboration).
1721
1722 elsif Restriction_Active (No_Multiple_Elaboration) then
1723 return;
1724 end if;
1725
1726 -- Here we need the elaboration entity
1727
1728 -- Construct name of elaboration entity as xxx_E, where xxx is the unit
1729 -- name with dots replaced by double underscore. We have to manually
1730 -- construct this name, since it will be elaborated in the outer scope,
1731 -- and thus will not have the unit name automatically prepended.
1732
1733 Set_Package_Name (Spec_Id);
1734 Add_Str_To_Name_Buffer ("_E");
1735
1736 -- Create elaboration counter
1737
1738 Elab_Ent := Make_Defining_Identifier (Loc, Chars => Name_Find);
1739 Set_Elaboration_Entity (Spec_Id, Elab_Ent);
1740
1741 Decl :=
1742 Make_Object_Declaration (Loc,
1743 Defining_Identifier => Elab_Ent,
1744 Object_Definition =>
1745 New_Occurrence_Of (Standard_Short_Integer, Loc),
1746 Expression => Make_Integer_Literal (Loc, Uint_0));
1747
1748 Push_Scope (Standard_Standard);
1749 Add_Global_Declaration (Decl);
1750 Pop_Scope;
1751
1752 -- Reset True_Constant indication, since we will indeed assign a value
1753 -- to the variable in the binder main. We also kill the Current_Value
1754 -- and Last_Assignment fields for the same reason.
1755
1756 Set_Is_True_Constant (Elab_Ent, False);
1757 Set_Current_Value (Elab_Ent, Empty);
1758 Set_Last_Assignment (Elab_Ent, Empty);
1759
1760 -- We do not want any further qualification of the name (if we did not
1761 -- do this, we would pick up the name of the generic package in the case
1762 -- of a library level generic instantiation).
1763
1764 Set_Has_Qualified_Name (Elab_Ent);
1765 Set_Has_Fully_Qualified_Name (Elab_Ent);
1766 end Build_Elaboration_Entity;
1767
1768 --------------------------------
1769 -- Build_Explicit_Dereference --
1770 --------------------------------
1771
1772 procedure Build_Explicit_Dereference
1773 (Expr : Node_Id;
1774 Disc : Entity_Id)
1775 is
1776 Loc : constant Source_Ptr := Sloc (Expr);
1777 I : Interp_Index;
1778 It : Interp;
1779
1780 begin
1781 -- An entity of a type with a reference aspect is overloaded with
1782 -- both interpretations: with and without the dereference. Now that
1783 -- the dereference is made explicit, set the type of the node properly,
1784 -- to prevent anomalies in the backend. Same if the expression is an
1785 -- overloaded function call whose return type has a reference aspect.
1786
1787 if Is_Entity_Name (Expr) then
1788 Set_Etype (Expr, Etype (Entity (Expr)));
1789
1790 -- The designated entity will not be examined again when resolving
1791 -- the dereference, so generate a reference to it now.
1792
1793 Generate_Reference (Entity (Expr), Expr);
1794
1795 elsif Nkind (Expr) = N_Function_Call then
1796
1797 -- If the name of the indexing function is overloaded, locate the one
1798 -- whose return type has an implicit dereference on the desired
1799 -- discriminant, and set entity and type of function call.
1800
1801 if Is_Overloaded (Name (Expr)) then
1802 Get_First_Interp (Name (Expr), I, It);
1803
1804 while Present (It.Nam) loop
1805 if Ekind ((It.Typ)) = E_Record_Type
1806 and then First_Entity ((It.Typ)) = Disc
1807 then
1808 Set_Entity (Name (Expr), It.Nam);
1809 Set_Etype (Name (Expr), Etype (It.Nam));
1810 exit;
1811 end if;
1812
1813 Get_Next_Interp (I, It);
1814 end loop;
1815 end if;
1816
1817 -- Set type of call from resolved function name.
1818
1819 Set_Etype (Expr, Etype (Name (Expr)));
1820 end if;
1821
1822 Set_Is_Overloaded (Expr, False);
1823
1824 -- The expression will often be a generalized indexing that yields a
1825 -- container element that is then dereferenced, in which case the
1826 -- generalized indexing call is also non-overloaded.
1827
1828 if Nkind (Expr) = N_Indexed_Component
1829 and then Present (Generalized_Indexing (Expr))
1830 then
1831 Set_Is_Overloaded (Generalized_Indexing (Expr), False);
1832 end if;
1833
1834 Rewrite (Expr,
1835 Make_Explicit_Dereference (Loc,
1836 Prefix =>
1837 Make_Selected_Component (Loc,
1838 Prefix => Relocate_Node (Expr),
1839 Selector_Name => New_Occurrence_Of (Disc, Loc))));
1840 Set_Etype (Prefix (Expr), Etype (Disc));
1841 Set_Etype (Expr, Designated_Type (Etype (Disc)));
1842 end Build_Explicit_Dereference;
1843
1844 -----------------------------------
1845 -- Cannot_Raise_Constraint_Error --
1846 -----------------------------------
1847
1848 function Cannot_Raise_Constraint_Error (Expr : Node_Id) return Boolean is
1849 begin
1850 if Compile_Time_Known_Value (Expr) then
1851 return True;
1852
1853 elsif Do_Range_Check (Expr) then
1854 return False;
1855
1856 elsif Raises_Constraint_Error (Expr) then
1857 return False;
1858
1859 else
1860 case Nkind (Expr) is
1861 when N_Identifier =>
1862 return True;
1863
1864 when N_Expanded_Name =>
1865 return True;
1866
1867 when N_Selected_Component =>
1868 return not Do_Discriminant_Check (Expr);
1869
1870 when N_Attribute_Reference =>
1871 if Do_Overflow_Check (Expr) then
1872 return False;
1873
1874 elsif No (Expressions (Expr)) then
1875 return True;
1876
1877 else
1878 declare
1879 N : Node_Id;
1880
1881 begin
1882 N := First (Expressions (Expr));
1883 while Present (N) loop
1884 if Cannot_Raise_Constraint_Error (N) then
1885 Next (N);
1886 else
1887 return False;
1888 end if;
1889 end loop;
1890
1891 return True;
1892 end;
1893 end if;
1894
1895 when N_Type_Conversion =>
1896 if Do_Overflow_Check (Expr)
1897 or else Do_Length_Check (Expr)
1898 or else Do_Tag_Check (Expr)
1899 then
1900 return False;
1901 else
1902 return Cannot_Raise_Constraint_Error (Expression (Expr));
1903 end if;
1904
1905 when N_Unchecked_Type_Conversion =>
1906 return Cannot_Raise_Constraint_Error (Expression (Expr));
1907
1908 when N_Unary_Op =>
1909 if Do_Overflow_Check (Expr) then
1910 return False;
1911 else
1912 return Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
1913 end if;
1914
1915 when N_Op_Divide |
1916 N_Op_Mod |
1917 N_Op_Rem
1918 =>
1919 if Do_Division_Check (Expr)
1920 or else
1921 Do_Overflow_Check (Expr)
1922 then
1923 return False;
1924 else
1925 return
1926 Cannot_Raise_Constraint_Error (Left_Opnd (Expr))
1927 and then
1928 Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
1929 end if;
1930
1931 when N_Op_Add |
1932 N_Op_And |
1933 N_Op_Concat |
1934 N_Op_Eq |
1935 N_Op_Expon |
1936 N_Op_Ge |
1937 N_Op_Gt |
1938 N_Op_Le |
1939 N_Op_Lt |
1940 N_Op_Multiply |
1941 N_Op_Ne |
1942 N_Op_Or |
1943 N_Op_Rotate_Left |
1944 N_Op_Rotate_Right |
1945 N_Op_Shift_Left |
1946 N_Op_Shift_Right |
1947 N_Op_Shift_Right_Arithmetic |
1948 N_Op_Subtract |
1949 N_Op_Xor
1950 =>
1951 if Do_Overflow_Check (Expr) then
1952 return False;
1953 else
1954 return
1955 Cannot_Raise_Constraint_Error (Left_Opnd (Expr))
1956 and then
1957 Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
1958 end if;
1959
1960 when others =>
1961 return False;
1962 end case;
1963 end if;
1964 end Cannot_Raise_Constraint_Error;
1965
1966 -----------------------------
1967 -- Check_Part_Of_Reference --
1968 -----------------------------
1969
1970 procedure Check_Part_Of_Reference (Var_Id : Entity_Id; Ref : Node_Id) is
1971 Conc_Typ : constant Entity_Id := Encapsulating_State (Var_Id);
1972 Decl : Node_Id;
1973 OK_Use : Boolean := False;
1974 Par : Node_Id;
1975 Prag_Nam : Name_Id;
1976 Spec_Id : Entity_Id;
1977
1978 begin
1979 -- Traverse the parent chain looking for a suitable context for the
1980 -- reference to the concurrent constituent.
1981
1982 Par := Parent (Ref);
1983 while Present (Par) loop
1984 if Nkind (Par) = N_Pragma then
1985 Prag_Nam := Pragma_Name (Par);
1986
1987 -- A concurrent constituent is allowed to appear in pragmas
1988 -- Initial_Condition and Initializes as this is part of the
1989 -- elaboration checks for the constituent (SPARK RM 9.3).
1990
1991 if Nam_In (Prag_Nam, Name_Initial_Condition, Name_Initializes) then
1992 OK_Use := True;
1993 exit;
1994
1995 -- When the reference appears within pragma Depends or Global,
1996 -- check whether the pragma applies to a single task type. Note
1997 -- that the pragma is not encapsulated by the type definition,
1998 -- but this is still a valid context.
1999
2000 elsif Nam_In (Prag_Nam, Name_Depends, Name_Global) then
2001 Decl := Find_Related_Declaration_Or_Body (Par);
2002
2003 if Nkind (Decl) = N_Object_Declaration
2004 and then Defining_Entity (Decl) = Conc_Typ
2005 then
2006 OK_Use := True;
2007 exit;
2008 end if;
2009 end if;
2010
2011 -- The reference appears somewhere in the definition of the single
2012 -- protected/task type (SPARK RM 9.3).
2013
2014 elsif Nkind_In (Par, N_Single_Protected_Declaration,
2015 N_Single_Task_Declaration)
2016 and then Defining_Entity (Par) = Conc_Typ
2017 then
2018 OK_Use := True;
2019 exit;
2020
2021 -- The reference appears within the expanded declaration or the body
2022 -- of the single protected/task type (SPARK RM 9.3).
2023
2024 elsif Nkind_In (Par, N_Protected_Body,
2025 N_Protected_Type_Declaration,
2026 N_Task_Body,
2027 N_Task_Type_Declaration)
2028 then
2029 Spec_Id := Unique_Defining_Entity (Par);
2030
2031 if Present (Anonymous_Object (Spec_Id))
2032 and then Anonymous_Object (Spec_Id) = Conc_Typ
2033 then
2034 OK_Use := True;
2035 exit;
2036 end if;
2037
2038 -- The reference has been relocated within an internally generated
2039 -- package or subprogram. Assume that the reference is legal as the
2040 -- real check was already performed in the original context of the
2041 -- reference.
2042
2043 elsif Nkind_In (Par, N_Package_Body,
2044 N_Package_Declaration,
2045 N_Subprogram_Body,
2046 N_Subprogram_Declaration)
2047 and then not Comes_From_Source (Par)
2048 then
2049 OK_Use := True;
2050 exit;
2051
2052 -- The reference has been relocated to an inlined body for GNATprove.
2053 -- Assume that the reference is legal as the real check was already
2054 -- performed in the original context of the reference.
2055
2056 elsif GNATprove_Mode
2057 and then Nkind (Par) = N_Subprogram_Body
2058 and then Chars (Defining_Entity (Par)) = Name_uParent
2059 then
2060 OK_Use := True;
2061 exit;
2062 end if;
2063
2064 Par := Parent (Par);
2065 end loop;
2066
2067 -- The reference is illegal as it appears outside the definition or
2068 -- body of the single protected/task type.
2069
2070 if not OK_Use then
2071 Error_Msg_NE
2072 ("reference to variable & cannot appear in this context",
2073 Ref, Var_Id);
2074 Error_Msg_Name_1 := Chars (Var_Id);
2075
2076 if Ekind (Conc_Typ) = E_Protected_Type then
2077 Error_Msg_NE
2078 ("\% is constituent of single protected type &", Ref, Conc_Typ);
2079 else
2080 Error_Msg_NE
2081 ("\% is constituent of single task type &", Ref, Conc_Typ);
2082 end if;
2083 end if;
2084 end Check_Part_Of_Reference;
2085
2086 -----------------------------------------
2087 -- Check_Dynamically_Tagged_Expression --
2088 -----------------------------------------
2089
2090 procedure Check_Dynamically_Tagged_Expression
2091 (Expr : Node_Id;
2092 Typ : Entity_Id;
2093 Related_Nod : Node_Id)
2094 is
2095 begin
2096 pragma Assert (Is_Tagged_Type (Typ));
2097
2098 -- In order to avoid spurious errors when analyzing the expanded code,
2099 -- this check is done only for nodes that come from source and for
2100 -- actuals of generic instantiations.
2101
2102 if (Comes_From_Source (Related_Nod)
2103 or else In_Generic_Actual (Expr))
2104 and then (Is_Class_Wide_Type (Etype (Expr))
2105 or else Is_Dynamically_Tagged (Expr))
2106 and then Is_Tagged_Type (Typ)
2107 and then not Is_Class_Wide_Type (Typ)
2108 then
2109 Error_Msg_N ("dynamically tagged expression not allowed!", Expr);
2110 end if;
2111 end Check_Dynamically_Tagged_Expression;
2112
2113 --------------------------
2114 -- Check_Fully_Declared --
2115 --------------------------
2116
2117 procedure Check_Fully_Declared (T : Entity_Id; N : Node_Id) is
2118 begin
2119 if Ekind (T) = E_Incomplete_Type then
2120
2121 -- Ada 2005 (AI-50217): If the type is available through a limited
2122 -- with_clause, verify that its full view has been analyzed.
2123
2124 if From_Limited_With (T)
2125 and then Present (Non_Limited_View (T))
2126 and then Ekind (Non_Limited_View (T)) /= E_Incomplete_Type
2127 then
2128 -- The non-limited view is fully declared
2129
2130 null;
2131
2132 else
2133 Error_Msg_NE
2134 ("premature usage of incomplete}", N, First_Subtype (T));
2135 end if;
2136
2137 -- Need comments for these tests ???
2138
2139 elsif Has_Private_Component (T)
2140 and then not Is_Generic_Type (Root_Type (T))
2141 and then not In_Spec_Expression
2142 then
2143 -- Special case: if T is the anonymous type created for a single
2144 -- task or protected object, use the name of the source object.
2145
2146 if Is_Concurrent_Type (T)
2147 and then not Comes_From_Source (T)
2148 and then Nkind (N) = N_Object_Declaration
2149 then
2150 Error_Msg_NE
2151 ("type of& has incomplete component",
2152 N, Defining_Identifier (N));
2153 else
2154 Error_Msg_NE
2155 ("premature usage of incomplete}",
2156 N, First_Subtype (T));
2157 end if;
2158 end if;
2159 end Check_Fully_Declared;
2160
2161 -------------------------------------------
2162 -- Check_Function_With_Address_Parameter --
2163 -------------------------------------------
2164
2165 procedure Check_Function_With_Address_Parameter (Subp_Id : Entity_Id) is
2166 F : Entity_Id;
2167 T : Entity_Id;
2168
2169 begin
2170 F := First_Formal (Subp_Id);
2171 while Present (F) loop
2172 T := Etype (F);
2173
2174 if Is_Private_Type (T) and then Present (Full_View (T)) then
2175 T := Full_View (T);
2176 end if;
2177
2178 if Is_Descendant_Of_Address (T) or else Is_Limited_Type (T) then
2179 Set_Is_Pure (Subp_Id, False);
2180 exit;
2181 end if;
2182
2183 Next_Formal (F);
2184 end loop;
2185 end Check_Function_With_Address_Parameter;
2186
2187 -------------------------------------
2188 -- Check_Function_Writable_Actuals --
2189 -------------------------------------
2190
2191 procedure Check_Function_Writable_Actuals (N : Node_Id) is
2192 Writable_Actuals_List : Elist_Id := No_Elist;
2193 Identifiers_List : Elist_Id := No_Elist;
2194 Aggr_Error_Node : Node_Id := Empty;
2195 Error_Node : Node_Id := Empty;
2196
2197 procedure Collect_Identifiers (N : Node_Id);
2198 -- In a single traversal of subtree N collect in Writable_Actuals_List
2199 -- all the actuals of functions with writable actuals, and in the list
2200 -- Identifiers_List collect all the identifiers that are not actuals of
2201 -- functions with writable actuals. If a writable actual is referenced
2202 -- twice as writable actual then Error_Node is set to reference its
2203 -- second occurrence, the error is reported, and the tree traversal
2204 -- is abandoned.
2205
2206 function Get_Function_Id (Call : Node_Id) return Entity_Id;
2207 -- Return the entity associated with the function call
2208
2209 procedure Preanalyze_Without_Errors (N : Node_Id);
2210 -- Preanalyze N without reporting errors. Very dubious, you can't just
2211 -- go analyzing things more than once???
2212
2213 -------------------------
2214 -- Collect_Identifiers --
2215 -------------------------
2216
2217 procedure Collect_Identifiers (N : Node_Id) is
2218
2219 function Check_Node (N : Node_Id) return Traverse_Result;
2220 -- Process a single node during the tree traversal to collect the
2221 -- writable actuals of functions and all the identifiers which are
2222 -- not writable actuals of functions.
2223
2224 function Contains (List : Elist_Id; N : Node_Id) return Boolean;
2225 -- Returns True if List has a node whose Entity is Entity (N)
2226
2227 -------------------------
2228 -- Check_Function_Call --
2229 -------------------------
2230
2231 function Check_Node (N : Node_Id) return Traverse_Result is
2232 Is_Writable_Actual : Boolean := False;
2233 Id : Entity_Id;
2234
2235 begin
2236 if Nkind (N) = N_Identifier then
2237
2238 -- No analysis possible if the entity is not decorated
2239
2240 if No (Entity (N)) then
2241 return Skip;
2242
2243 -- Don't collect identifiers of packages, called functions, etc
2244
2245 elsif Ekind_In (Entity (N), E_Package,
2246 E_Function,
2247 E_Procedure,
2248 E_Entry)
2249 then
2250 return Skip;
2251
2252 -- For rewritten nodes, continue the traversal in the original
2253 -- subtree. Needed to handle aggregates in original expressions
2254 -- extracted from the tree by Remove_Side_Effects.
2255
2256 elsif Is_Rewrite_Substitution (N) then
2257 Collect_Identifiers (Original_Node (N));
2258 return Skip;
2259
2260 -- For now we skip aggregate discriminants, since they require
2261 -- performing the analysis in two phases to identify conflicts:
2262 -- first one analyzing discriminants and second one analyzing
2263 -- the rest of components (since at run time, discriminants are
2264 -- evaluated prior to components): too much computation cost
2265 -- to identify a corner case???
2266
2267 elsif Nkind (Parent (N)) = N_Component_Association
2268 and then Nkind_In (Parent (Parent (N)),
2269 N_Aggregate,
2270 N_Extension_Aggregate)
2271 then
2272 declare
2273 Choice : constant Node_Id := First (Choices (Parent (N)));
2274
2275 begin
2276 if Ekind (Entity (N)) = E_Discriminant then
2277 return Skip;
2278
2279 elsif Expression (Parent (N)) = N
2280 and then Nkind (Choice) = N_Identifier
2281 and then Ekind (Entity (Choice)) = E_Discriminant
2282 then
2283 return Skip;
2284 end if;
2285 end;
2286
2287 -- Analyze if N is a writable actual of a function
2288
2289 elsif Nkind (Parent (N)) = N_Function_Call then
2290 declare
2291 Call : constant Node_Id := Parent (N);
2292 Actual : Node_Id;
2293 Formal : Node_Id;
2294
2295 begin
2296 Id := Get_Function_Id (Call);
2297
2298 -- In case of previous error, no check is possible
2299
2300 if No (Id) then
2301 return Abandon;
2302 end if;
2303
2304 if Ekind_In (Id, E_Function, E_Generic_Function)
2305 and then Has_Out_Or_In_Out_Parameter (Id)
2306 then
2307 Formal := First_Formal (Id);
2308 Actual := First_Actual (Call);
2309 while Present (Actual) and then Present (Formal) loop
2310 if Actual = N then
2311 if Ekind_In (Formal, E_Out_Parameter,
2312 E_In_Out_Parameter)
2313 then
2314 Is_Writable_Actual := True;
2315 end if;
2316
2317 exit;
2318 end if;
2319
2320 Next_Formal (Formal);
2321 Next_Actual (Actual);
2322 end loop;
2323 end if;
2324 end;
2325 end if;
2326
2327 if Is_Writable_Actual then
2328
2329 -- Skip checking the error in non-elementary types since
2330 -- RM 6.4.1(6.15/3) is restricted to elementary types, but
2331 -- store this actual in Writable_Actuals_List since it is
2332 -- needed to perform checks on other constructs that have
2333 -- arbitrary order of evaluation (for example, aggregates).
2334
2335 if not Is_Elementary_Type (Etype (N)) then
2336 if not Contains (Writable_Actuals_List, N) then
2337 Append_New_Elmt (N, To => Writable_Actuals_List);
2338 end if;
2339
2340 -- Second occurrence of an elementary type writable actual
2341
2342 elsif Contains (Writable_Actuals_List, N) then
2343
2344 -- Report the error on the second occurrence of the
2345 -- identifier. We cannot assume that N is the second
2346 -- occurrence (according to their location in the
2347 -- sources), since Traverse_Func walks through Field2
2348 -- last (see comment in the body of Traverse_Func).
2349
2350 declare
2351 Elmt : Elmt_Id;
2352
2353 begin
2354 Elmt := First_Elmt (Writable_Actuals_List);
2355 while Present (Elmt)
2356 and then Entity (Node (Elmt)) /= Entity (N)
2357 loop
2358 Next_Elmt (Elmt);
2359 end loop;
2360
2361 if Sloc (N) > Sloc (Node (Elmt)) then
2362 Error_Node := N;
2363 else
2364 Error_Node := Node (Elmt);
2365 end if;
2366
2367 Error_Msg_NE
2368 ("value may be affected by call to & "
2369 & "because order of evaluation is arbitrary",
2370 Error_Node, Id);
2371 return Abandon;
2372 end;
2373
2374 -- First occurrence of a elementary type writable actual
2375
2376 else
2377 Append_New_Elmt (N, To => Writable_Actuals_List);
2378 end if;
2379
2380 else
2381 if Identifiers_List = No_Elist then
2382 Identifiers_List := New_Elmt_List;
2383 end if;
2384
2385 Append_Unique_Elmt (N, Identifiers_List);
2386 end if;
2387 end if;
2388
2389 return OK;
2390 end Check_Node;
2391
2392 --------------
2393 -- Contains --
2394 --------------
2395
2396 function Contains
2397 (List : Elist_Id;
2398 N : Node_Id) return Boolean
2399 is
2400 pragma Assert (Nkind (N) in N_Has_Entity);
2401
2402 Elmt : Elmt_Id;
2403
2404 begin
2405 if List = No_Elist then
2406 return False;
2407 end if;
2408
2409 Elmt := First_Elmt (List);
2410 while Present (Elmt) loop
2411 if Entity (Node (Elmt)) = Entity (N) then
2412 return True;
2413 else
2414 Next_Elmt (Elmt);
2415 end if;
2416 end loop;
2417
2418 return False;
2419 end Contains;
2420
2421 ------------------
2422 -- Do_Traversal --
2423 ------------------
2424
2425 procedure Do_Traversal is new Traverse_Proc (Check_Node);
2426 -- The traversal procedure
2427
2428 -- Start of processing for Collect_Identifiers
2429
2430 begin
2431 if Present (Error_Node) then
2432 return;
2433 end if;
2434
2435 if Nkind (N) in N_Subexpr and then Is_OK_Static_Expression (N) then
2436 return;
2437 end if;
2438
2439 Do_Traversal (N);
2440 end Collect_Identifiers;
2441
2442 ---------------------
2443 -- Get_Function_Id --
2444 ---------------------
2445
2446 function Get_Function_Id (Call : Node_Id) return Entity_Id is
2447 Nam : constant Node_Id := Name (Call);
2448 Id : Entity_Id;
2449
2450 begin
2451 if Nkind (Nam) = N_Explicit_Dereference then
2452 Id := Etype (Nam);
2453 pragma Assert (Ekind (Id) = E_Subprogram_Type);
2454
2455 elsif Nkind (Nam) = N_Selected_Component then
2456 Id := Entity (Selector_Name (Nam));
2457
2458 elsif Nkind (Nam) = N_Indexed_Component then
2459 Id := Entity (Selector_Name (Prefix (Nam)));
2460
2461 else
2462 Id := Entity (Nam);
2463 end if;
2464
2465 return Id;
2466 end Get_Function_Id;
2467
2468 -------------------------------
2469 -- Preanalyze_Without_Errors --
2470 -------------------------------
2471
2472 procedure Preanalyze_Without_Errors (N : Node_Id) is
2473 Status : constant Boolean := Get_Ignore_Errors;
2474 begin
2475 Set_Ignore_Errors (True);
2476 Preanalyze (N);
2477 Set_Ignore_Errors (Status);
2478 end Preanalyze_Without_Errors;
2479
2480 -- Start of processing for Check_Function_Writable_Actuals
2481
2482 begin
2483 -- The check only applies to Ada 2012 code on which Check_Actuals has
2484 -- been set, and only to constructs that have multiple constituents
2485 -- whose order of evaluation is not specified by the language.
2486
2487 if Ada_Version < Ada_2012
2488 or else not Check_Actuals (N)
2489 or else (not (Nkind (N) in N_Op)
2490 and then not (Nkind (N) in N_Membership_Test)
2491 and then not Nkind_In (N, N_Range,
2492 N_Aggregate,
2493 N_Extension_Aggregate,
2494 N_Full_Type_Declaration,
2495 N_Function_Call,
2496 N_Procedure_Call_Statement,
2497 N_Entry_Call_Statement))
2498 or else (Nkind (N) = N_Full_Type_Declaration
2499 and then not Is_Record_Type (Defining_Identifier (N)))
2500
2501 -- In addition, this check only applies to source code, not to code
2502 -- generated by constraint checks.
2503
2504 or else not Comes_From_Source (N)
2505 then
2506 return;
2507 end if;
2508
2509 -- If a construct C has two or more direct constituents that are names
2510 -- or expressions whose evaluation may occur in an arbitrary order, at
2511 -- least one of which contains a function call with an in out or out
2512 -- parameter, then the construct is legal only if: for each name N that
2513 -- is passed as a parameter of mode in out or out to some inner function
2514 -- call C2 (not including the construct C itself), there is no other
2515 -- name anywhere within a direct constituent of the construct C other
2516 -- than the one containing C2, that is known to refer to the same
2517 -- object (RM 6.4.1(6.17/3)).
2518
2519 case Nkind (N) is
2520 when N_Range =>
2521 Collect_Identifiers (Low_Bound (N));
2522 Collect_Identifiers (High_Bound (N));
2523
2524 when N_Op | N_Membership_Test =>
2525 declare
2526 Expr : Node_Id;
2527
2528 begin
2529 Collect_Identifiers (Left_Opnd (N));
2530
2531 if Present (Right_Opnd (N)) then
2532 Collect_Identifiers (Right_Opnd (N));
2533 end if;
2534
2535 if Nkind_In (N, N_In, N_Not_In)
2536 and then Present (Alternatives (N))
2537 then
2538 Expr := First (Alternatives (N));
2539 while Present (Expr) loop
2540 Collect_Identifiers (Expr);
2541
2542 Next (Expr);
2543 end loop;
2544 end if;
2545 end;
2546
2547 when N_Full_Type_Declaration =>
2548 declare
2549 function Get_Record_Part (N : Node_Id) return Node_Id;
2550 -- Return the record part of this record type definition
2551
2552 function Get_Record_Part (N : Node_Id) return Node_Id is
2553 Type_Def : constant Node_Id := Type_Definition (N);
2554 begin
2555 if Nkind (Type_Def) = N_Derived_Type_Definition then
2556 return Record_Extension_Part (Type_Def);
2557 else
2558 return Type_Def;
2559 end if;
2560 end Get_Record_Part;
2561
2562 Comp : Node_Id;
2563 Def_Id : Entity_Id := Defining_Identifier (N);
2564 Rec : Node_Id := Get_Record_Part (N);
2565
2566 begin
2567 -- No need to perform any analysis if the record has no
2568 -- components
2569
2570 if No (Rec) or else No (Component_List (Rec)) then
2571 return;
2572 end if;
2573
2574 -- Collect the identifiers starting from the deepest
2575 -- derivation. Done to report the error in the deepest
2576 -- derivation.
2577
2578 loop
2579 if Present (Component_List (Rec)) then
2580 Comp := First (Component_Items (Component_List (Rec)));
2581 while Present (Comp) loop
2582 if Nkind (Comp) = N_Component_Declaration
2583 and then Present (Expression (Comp))
2584 then
2585 Collect_Identifiers (Expression (Comp));
2586 end if;
2587
2588 Next (Comp);
2589 end loop;
2590 end if;
2591
2592 exit when No (Underlying_Type (Etype (Def_Id)))
2593 or else Base_Type (Underlying_Type (Etype (Def_Id)))
2594 = Def_Id;
2595
2596 Def_Id := Base_Type (Underlying_Type (Etype (Def_Id)));
2597 Rec := Get_Record_Part (Parent (Def_Id));
2598 end loop;
2599 end;
2600
2601 when N_Subprogram_Call |
2602 N_Entry_Call_Statement =>
2603 declare
2604 Id : constant Entity_Id := Get_Function_Id (N);
2605 Formal : Node_Id;
2606 Actual : Node_Id;
2607
2608 begin
2609 Formal := First_Formal (Id);
2610 Actual := First_Actual (N);
2611 while Present (Actual) and then Present (Formal) loop
2612 if Ekind_In (Formal, E_Out_Parameter,
2613 E_In_Out_Parameter)
2614 then
2615 Collect_Identifiers (Actual);
2616 end if;
2617
2618 Next_Formal (Formal);
2619 Next_Actual (Actual);
2620 end loop;
2621 end;
2622
2623 when N_Aggregate |
2624 N_Extension_Aggregate =>
2625 declare
2626 Assoc : Node_Id;
2627 Choice : Node_Id;
2628 Comp_Expr : Node_Id;
2629
2630 begin
2631 -- Handle the N_Others_Choice of array aggregates with static
2632 -- bounds. There is no need to perform this analysis in
2633 -- aggregates without static bounds since we cannot evaluate
2634 -- if the N_Others_Choice covers several elements. There is
2635 -- no need to handle the N_Others choice of record aggregates
2636 -- since at this stage it has been already expanded by
2637 -- Resolve_Record_Aggregate.
2638
2639 if Is_Array_Type (Etype (N))
2640 and then Nkind (N) = N_Aggregate
2641 and then Present (Aggregate_Bounds (N))
2642 and then Compile_Time_Known_Bounds (Etype (N))
2643 and then Expr_Value (High_Bound (Aggregate_Bounds (N)))
2644 >
2645 Expr_Value (Low_Bound (Aggregate_Bounds (N)))
2646 then
2647 declare
2648 Count_Components : Uint := Uint_0;
2649 Num_Components : Uint;
2650 Others_Assoc : Node_Id;
2651 Others_Choice : Node_Id := Empty;
2652 Others_Box_Present : Boolean := False;
2653
2654 begin
2655 -- Count positional associations
2656
2657 if Present (Expressions (N)) then
2658 Comp_Expr := First (Expressions (N));
2659 while Present (Comp_Expr) loop
2660 Count_Components := Count_Components + 1;
2661 Next (Comp_Expr);
2662 end loop;
2663 end if;
2664
2665 -- Count the rest of elements and locate the N_Others
2666 -- choice (if any)
2667
2668 Assoc := First (Component_Associations (N));
2669 while Present (Assoc) loop
2670 Choice := First (Choices (Assoc));
2671 while Present (Choice) loop
2672 if Nkind (Choice) = N_Others_Choice then
2673 Others_Assoc := Assoc;
2674 Others_Choice := Choice;
2675 Others_Box_Present := Box_Present (Assoc);
2676
2677 -- Count several components
2678
2679 elsif Nkind_In (Choice, N_Range,
2680 N_Subtype_Indication)
2681 or else (Is_Entity_Name (Choice)
2682 and then Is_Type (Entity (Choice)))
2683 then
2684 declare
2685 L, H : Node_Id;
2686 begin
2687 Get_Index_Bounds (Choice, L, H);
2688 pragma Assert
2689 (Compile_Time_Known_Value (L)
2690 and then Compile_Time_Known_Value (H));
2691 Count_Components :=
2692 Count_Components
2693 + Expr_Value (H) - Expr_Value (L) + 1;
2694 end;
2695
2696 -- Count single component. No other case available
2697 -- since we are handling an aggregate with static
2698 -- bounds.
2699
2700 else
2701 pragma Assert (Is_OK_Static_Expression (Choice)
2702 or else Nkind (Choice) = N_Identifier
2703 or else Nkind (Choice) = N_Integer_Literal);
2704
2705 Count_Components := Count_Components + 1;
2706 end if;
2707
2708 Next (Choice);
2709 end loop;
2710
2711 Next (Assoc);
2712 end loop;
2713
2714 Num_Components :=
2715 Expr_Value (High_Bound (Aggregate_Bounds (N))) -
2716 Expr_Value (Low_Bound (Aggregate_Bounds (N))) + 1;
2717
2718 pragma Assert (Count_Components <= Num_Components);
2719
2720 -- Handle the N_Others choice if it covers several
2721 -- components
2722
2723 if Present (Others_Choice)
2724 and then (Num_Components - Count_Components) > 1
2725 then
2726 if not Others_Box_Present then
2727
2728 -- At this stage, if expansion is active, the
2729 -- expression of the others choice has not been
2730 -- analyzed. Hence we generate a duplicate and
2731 -- we analyze it silently to have available the
2732 -- minimum decoration required to collect the
2733 -- identifiers.
2734
2735 if not Expander_Active then
2736 Comp_Expr := Expression (Others_Assoc);
2737 else
2738 Comp_Expr :=
2739 New_Copy_Tree (Expression (Others_Assoc));
2740 Preanalyze_Without_Errors (Comp_Expr);
2741 end if;
2742
2743 Collect_Identifiers (Comp_Expr);
2744
2745 if Writable_Actuals_List /= No_Elist then
2746
2747 -- As suggested by Robert, at current stage we
2748 -- report occurrences of this case as warnings.
2749
2750 Error_Msg_N
2751 ("writable function parameter may affect "
2752 & "value in other component because order "
2753 & "of evaluation is unspecified??",
2754 Node (First_Elmt (Writable_Actuals_List)));
2755 end if;
2756 end if;
2757 end if;
2758 end;
2759
2760 -- For an array aggregate, a discrete_choice_list that has
2761 -- a nonstatic range is considered as two or more separate
2762 -- occurrences of the expression (RM 6.4.1(20/3)).
2763
2764 elsif Is_Array_Type (Etype (N))
2765 and then Nkind (N) = N_Aggregate
2766 and then Present (Aggregate_Bounds (N))
2767 and then not Compile_Time_Known_Bounds (Etype (N))
2768 then
2769 -- Collect identifiers found in the dynamic bounds
2770
2771 declare
2772 Count_Components : Natural := 0;
2773 Low, High : Node_Id;
2774
2775 begin
2776 Assoc := First (Component_Associations (N));
2777 while Present (Assoc) loop
2778 Choice := First (Choices (Assoc));
2779 while Present (Choice) loop
2780 if Nkind_In (Choice, N_Range,
2781 N_Subtype_Indication)
2782 or else (Is_Entity_Name (Choice)
2783 and then Is_Type (Entity (Choice)))
2784 then
2785 Get_Index_Bounds (Choice, Low, High);
2786
2787 if not Compile_Time_Known_Value (Low) then
2788 Collect_Identifiers (Low);
2789
2790 if No (Aggr_Error_Node) then
2791 Aggr_Error_Node := Low;
2792 end if;
2793 end if;
2794
2795 if not Compile_Time_Known_Value (High) then
2796 Collect_Identifiers (High);
2797
2798 if No (Aggr_Error_Node) then
2799 Aggr_Error_Node := High;
2800 end if;
2801 end if;
2802
2803 -- The RM rule is violated if there is more than
2804 -- a single choice in a component association.
2805
2806 else
2807 Count_Components := Count_Components + 1;
2808
2809 if No (Aggr_Error_Node)
2810 and then Count_Components > 1
2811 then
2812 Aggr_Error_Node := Choice;
2813 end if;
2814
2815 if not Compile_Time_Known_Value (Choice) then
2816 Collect_Identifiers (Choice);
2817 end if;
2818 end if;
2819
2820 Next (Choice);
2821 end loop;
2822
2823 Next (Assoc);
2824 end loop;
2825 end;
2826 end if;
2827
2828 -- Handle ancestor part of extension aggregates
2829
2830 if Nkind (N) = N_Extension_Aggregate then
2831 Collect_Identifiers (Ancestor_Part (N));
2832 end if;
2833
2834 -- Handle positional associations
2835
2836 if Present (Expressions (N)) then
2837 Comp_Expr := First (Expressions (N));
2838 while Present (Comp_Expr) loop
2839 if not Is_OK_Static_Expression (Comp_Expr) then
2840 Collect_Identifiers (Comp_Expr);
2841 end if;
2842
2843 Next (Comp_Expr);
2844 end loop;
2845 end if;
2846
2847 -- Handle discrete associations
2848
2849 if Present (Component_Associations (N)) then
2850 Assoc := First (Component_Associations (N));
2851 while Present (Assoc) loop
2852
2853 if not Box_Present (Assoc) then
2854 Choice := First (Choices (Assoc));
2855 while Present (Choice) loop
2856
2857 -- For now we skip discriminants since it requires
2858 -- performing the analysis in two phases: first one
2859 -- analyzing discriminants and second one analyzing
2860 -- the rest of components since discriminants are
2861 -- evaluated prior to components: too much extra
2862 -- work to detect a corner case???
2863
2864 if Nkind (Choice) in N_Has_Entity
2865 and then Present (Entity (Choice))
2866 and then Ekind (Entity (Choice)) = E_Discriminant
2867 then
2868 null;
2869
2870 elsif Box_Present (Assoc) then
2871 null;
2872
2873 else
2874 if not Analyzed (Expression (Assoc)) then
2875 Comp_Expr :=
2876 New_Copy_Tree (Expression (Assoc));
2877 Set_Parent (Comp_Expr, Parent (N));
2878 Preanalyze_Without_Errors (Comp_Expr);
2879 else
2880 Comp_Expr := Expression (Assoc);
2881 end if;
2882
2883 Collect_Identifiers (Comp_Expr);
2884 end if;
2885
2886 Next (Choice);
2887 end loop;
2888 end if;
2889
2890 Next (Assoc);
2891 end loop;
2892 end if;
2893 end;
2894
2895 when others =>
2896 return;
2897 end case;
2898
2899 -- No further action needed if we already reported an error
2900
2901 if Present (Error_Node) then
2902 return;
2903 end if;
2904
2905 -- Check violation of RM 6.20/3 in aggregates
2906
2907 if Present (Aggr_Error_Node)
2908 and then Writable_Actuals_List /= No_Elist
2909 then
2910 Error_Msg_N
2911 ("value may be affected by call in other component because they "
2912 & "are evaluated in unspecified order",
2913 Node (First_Elmt (Writable_Actuals_List)));
2914 return;
2915 end if;
2916
2917 -- Check if some writable argument of a function is referenced
2918
2919 if Writable_Actuals_List /= No_Elist
2920 and then Identifiers_List /= No_Elist
2921 then
2922 declare
2923 Elmt_1 : Elmt_Id;
2924 Elmt_2 : Elmt_Id;
2925
2926 begin
2927 Elmt_1 := First_Elmt (Writable_Actuals_List);
2928 while Present (Elmt_1) loop
2929 Elmt_2 := First_Elmt (Identifiers_List);
2930 while Present (Elmt_2) loop
2931 if Entity (Node (Elmt_1)) = Entity (Node (Elmt_2)) then
2932 case Nkind (Parent (Node (Elmt_2))) is
2933 when N_Aggregate |
2934 N_Component_Association |
2935 N_Component_Declaration =>
2936 Error_Msg_N
2937 ("value may be affected by call in other "
2938 & "component because they are evaluated "
2939 & "in unspecified order",
2940 Node (Elmt_2));
2941
2942 when N_In | N_Not_In =>
2943 Error_Msg_N
2944 ("value may be affected by call in other "
2945 & "alternative because they are evaluated "
2946 & "in unspecified order",
2947 Node (Elmt_2));
2948
2949 when others =>
2950 Error_Msg_N
2951 ("value of actual may be affected by call in "
2952 & "other actual because they are evaluated "
2953 & "in unspecified order",
2954 Node (Elmt_2));
2955 end case;
2956 end if;
2957
2958 Next_Elmt (Elmt_2);
2959 end loop;
2960
2961 Next_Elmt (Elmt_1);
2962 end loop;
2963 end;
2964 end if;
2965 end Check_Function_Writable_Actuals;
2966
2967 --------------------------------
2968 -- Check_Implicit_Dereference --
2969 --------------------------------
2970
2971 procedure Check_Implicit_Dereference (N : Node_Id; Typ : Entity_Id) is
2972 Disc : Entity_Id;
2973 Desig : Entity_Id;
2974 Nam : Node_Id;
2975
2976 begin
2977 if Nkind (N) = N_Indexed_Component
2978 and then Present (Generalized_Indexing (N))
2979 then
2980 Nam := Generalized_Indexing (N);
2981 else
2982 Nam := N;
2983 end if;
2984
2985 if Ada_Version < Ada_2012
2986 or else not Has_Implicit_Dereference (Base_Type (Typ))
2987 then
2988 return;
2989
2990 elsif not Comes_From_Source (N)
2991 and then Nkind (N) /= N_Indexed_Component
2992 then
2993 return;
2994
2995 elsif Is_Entity_Name (Nam) and then Is_Type (Entity (Nam)) then
2996 null;
2997
2998 else
2999 Disc := First_Discriminant (Typ);
3000 while Present (Disc) loop
3001 if Has_Implicit_Dereference (Disc) then
3002 Desig := Designated_Type (Etype (Disc));
3003 Add_One_Interp (Nam, Disc, Desig);
3004
3005 -- If the node is a generalized indexing, add interpretation
3006 -- to that node as well, for subsequent resolution.
3007
3008 if Nkind (N) = N_Indexed_Component then
3009 Add_One_Interp (N, Disc, Desig);
3010 end if;
3011
3012 -- If the operation comes from a generic unit and the context
3013 -- is a selected component, the selector name may be global
3014 -- and set in the instance already. Remove the entity to
3015 -- force resolution of the selected component, and the
3016 -- generation of an explicit dereference if needed.
3017
3018 if In_Instance
3019 and then Nkind (Parent (Nam)) = N_Selected_Component
3020 then
3021 Set_Entity (Selector_Name (Parent (Nam)), Empty);
3022 end if;
3023
3024 exit;
3025 end if;
3026
3027 Next_Discriminant (Disc);
3028 end loop;
3029 end if;
3030 end Check_Implicit_Dereference;
3031
3032 ----------------------------------
3033 -- Check_Internal_Protected_Use --
3034 ----------------------------------
3035
3036 procedure Check_Internal_Protected_Use (N : Node_Id; Nam : Entity_Id) is
3037 S : Entity_Id;
3038 Prot : Entity_Id;
3039
3040 begin
3041 S := Current_Scope;
3042 while Present (S) loop
3043 if S = Standard_Standard then
3044 return;
3045
3046 elsif Ekind (S) = E_Function
3047 and then Ekind (Scope (S)) = E_Protected_Type
3048 then
3049 Prot := Scope (S);
3050 exit;
3051 end if;
3052
3053 S := Scope (S);
3054 end loop;
3055
3056 if Scope (Nam) = Prot and then Ekind (Nam) /= E_Function then
3057
3058 -- An indirect function call (e.g. a callback within a protected
3059 -- function body) is not statically illegal. If the access type is
3060 -- anonymous and is the type of an access parameter, the scope of Nam
3061 -- will be the protected type, but it is not a protected operation.
3062
3063 if Ekind (Nam) = E_Subprogram_Type
3064 and then
3065 Nkind (Associated_Node_For_Itype (Nam)) = N_Function_Specification
3066 then
3067 null;
3068
3069 elsif Nkind (N) = N_Subprogram_Renaming_Declaration then
3070 Error_Msg_N
3071 ("within protected function cannot use protected "
3072 & "procedure in renaming or as generic actual", N);
3073
3074 elsif Nkind (N) = N_Attribute_Reference then
3075 Error_Msg_N
3076 ("within protected function cannot take access of "
3077 & " protected procedure", N);
3078
3079 else
3080 Error_Msg_N
3081 ("within protected function, protected object is constant", N);
3082 Error_Msg_N
3083 ("\cannot call operation that may modify it", N);
3084 end if;
3085 end if;
3086 end Check_Internal_Protected_Use;
3087
3088 ---------------------------------------
3089 -- Check_Later_Vs_Basic_Declarations --
3090 ---------------------------------------
3091
3092 procedure Check_Later_Vs_Basic_Declarations
3093 (Decls : List_Id;
3094 During_Parsing : Boolean)
3095 is
3096 Body_Sloc : Source_Ptr;
3097 Decl : Node_Id;
3098
3099 function Is_Later_Declarative_Item (Decl : Node_Id) return Boolean;
3100 -- Return whether Decl is considered as a declarative item.
3101 -- When During_Parsing is True, the semantics of Ada 83 is followed.
3102 -- When During_Parsing is False, the semantics of SPARK is followed.
3103
3104 -------------------------------
3105 -- Is_Later_Declarative_Item --
3106 -------------------------------
3107
3108 function Is_Later_Declarative_Item (Decl : Node_Id) return Boolean is
3109 begin
3110 if Nkind (Decl) in N_Later_Decl_Item then
3111 return True;
3112
3113 elsif Nkind (Decl) = N_Pragma then
3114 return True;
3115
3116 elsif During_Parsing then
3117 return False;
3118
3119 -- In SPARK, a package declaration is not considered as a later
3120 -- declarative item.
3121
3122 elsif Nkind (Decl) = N_Package_Declaration then
3123 return False;
3124
3125 -- In SPARK, a renaming is considered as a later declarative item
3126
3127 elsif Nkind (Decl) in N_Renaming_Declaration then
3128 return True;
3129
3130 else
3131 return False;
3132 end if;
3133 end Is_Later_Declarative_Item;
3134
3135 -- Start of processing for Check_Later_Vs_Basic_Declarations
3136
3137 begin
3138 Decl := First (Decls);
3139
3140 -- Loop through sequence of basic declarative items
3141
3142 Outer : while Present (Decl) loop
3143 if not Nkind_In (Decl, N_Subprogram_Body, N_Package_Body, N_Task_Body)
3144 and then Nkind (Decl) not in N_Body_Stub
3145 then
3146 Next (Decl);
3147
3148 -- Once a body is encountered, we only allow later declarative
3149 -- items. The inner loop checks the rest of the list.
3150
3151 else
3152 Body_Sloc := Sloc (Decl);
3153
3154 Inner : while Present (Decl) loop
3155 if not Is_Later_Declarative_Item (Decl) then
3156 if During_Parsing then
3157 if Ada_Version = Ada_83 then
3158 Error_Msg_Sloc := Body_Sloc;
3159 Error_Msg_N
3160 ("(Ada 83) decl cannot appear after body#", Decl);
3161 end if;
3162 else
3163 Error_Msg_Sloc := Body_Sloc;
3164 Check_SPARK_05_Restriction
3165 ("decl cannot appear after body#", Decl);
3166 end if;
3167 end if;
3168
3169 Next (Decl);
3170 end loop Inner;
3171 end if;
3172 end loop Outer;
3173 end Check_Later_Vs_Basic_Declarations;
3174
3175 ---------------------------
3176 -- Check_No_Hidden_State --
3177 ---------------------------
3178
3179 procedure Check_No_Hidden_State (Id : Entity_Id) is
3180 function Has_Null_Abstract_State (Pkg : Entity_Id) return Boolean;
3181 -- Determine whether the entity of a package denoted by Pkg has a null
3182 -- abstract state.
3183
3184 -----------------------------
3185 -- Has_Null_Abstract_State --
3186 -----------------------------
3187
3188 function Has_Null_Abstract_State (Pkg : Entity_Id) return Boolean is
3189 States : constant Elist_Id := Abstract_States (Pkg);
3190
3191 begin
3192 -- Check first available state of related package. A null abstract
3193 -- state always appears as the sole element of the state list.
3194
3195 return
3196 Present (States)
3197 and then Is_Null_State (Node (First_Elmt (States)));
3198 end Has_Null_Abstract_State;
3199
3200 -- Local variables
3201
3202 Context : Entity_Id := Empty;
3203 Not_Visible : Boolean := False;
3204 Scop : Entity_Id;
3205
3206 -- Start of processing for Check_No_Hidden_State
3207
3208 begin
3209 pragma Assert (Ekind_In (Id, E_Abstract_State, E_Variable));
3210
3211 -- Find the proper context where the object or state appears
3212
3213 Scop := Scope (Id);
3214 while Present (Scop) loop
3215 Context := Scop;
3216
3217 -- Keep track of the context's visibility
3218
3219 Not_Visible := Not_Visible or else In_Private_Part (Context);
3220
3221 -- Prevent the search from going too far
3222
3223 if Context = Standard_Standard then
3224 return;
3225
3226 -- Objects and states that appear immediately within a subprogram or
3227 -- inside a construct nested within a subprogram do not introduce a
3228 -- hidden state. They behave as local variable declarations.
3229
3230 elsif Is_Subprogram (Context) then
3231 return;
3232
3233 -- When examining a package body, use the entity of the spec as it
3234 -- carries the abstract state declarations.
3235
3236 elsif Ekind (Context) = E_Package_Body then
3237 Context := Spec_Entity (Context);
3238 end if;
3239
3240 -- Stop the traversal when a package subject to a null abstract state
3241 -- has been found.
3242
3243 if Ekind_In (Context, E_Generic_Package, E_Package)
3244 and then Has_Null_Abstract_State (Context)
3245 then
3246 exit;
3247 end if;
3248
3249 Scop := Scope (Scop);
3250 end loop;
3251
3252 -- At this point we know that there is at least one package with a null
3253 -- abstract state in visibility. Emit an error message unconditionally
3254 -- if the entity being processed is a state because the placement of the
3255 -- related package is irrelevant. This is not the case for objects as
3256 -- the intermediate context matters.
3257
3258 if Present (Context)
3259 and then (Ekind (Id) = E_Abstract_State or else Not_Visible)
3260 then
3261 Error_Msg_N ("cannot introduce hidden state &", Id);
3262 Error_Msg_NE ("\package & has null abstract state", Id, Context);
3263 end if;
3264 end Check_No_Hidden_State;
3265
3266 ----------------------------------------
3267 -- Check_Nonvolatile_Function_Profile --
3268 ----------------------------------------
3269
3270 procedure Check_Nonvolatile_Function_Profile (Func_Id : Entity_Id) is
3271 Formal : Entity_Id;
3272
3273 begin
3274 -- Inspect all formal parameters
3275
3276 Formal := First_Formal (Func_Id);
3277 while Present (Formal) loop
3278 if Is_Effectively_Volatile (Etype (Formal)) then
3279 Error_Msg_NE
3280 ("nonvolatile function & cannot have a volatile parameter",
3281 Formal, Func_Id);
3282 end if;
3283
3284 Next_Formal (Formal);
3285 end loop;
3286
3287 -- Inspect the return type
3288
3289 if Is_Effectively_Volatile (Etype (Func_Id)) then
3290 Error_Msg_NE
3291 ("nonvolatile function & cannot have a volatile return type",
3292 Result_Definition (Parent (Func_Id)), Func_Id);
3293 end if;
3294 end Check_Nonvolatile_Function_Profile;
3295
3296 ------------------------------------------
3297 -- Check_Potentially_Blocking_Operation --
3298 ------------------------------------------
3299
3300 procedure Check_Potentially_Blocking_Operation (N : Node_Id) is
3301 S : Entity_Id;
3302
3303 begin
3304 -- N is one of the potentially blocking operations listed in 9.5.1(8).
3305 -- When pragma Detect_Blocking is active, the run time will raise
3306 -- Program_Error. Here we only issue a warning, since we generally
3307 -- support the use of potentially blocking operations in the absence
3308 -- of the pragma.
3309
3310 -- Indirect blocking through a subprogram call cannot be diagnosed
3311 -- statically without interprocedural analysis, so we do not attempt
3312 -- to do it here.
3313
3314 S := Scope (Current_Scope);
3315 while Present (S) and then S /= Standard_Standard loop
3316 if Is_Protected_Type (S) then
3317 Error_Msg_N
3318 ("potentially blocking operation in protected operation??", N);
3319 return;
3320 end if;
3321
3322 S := Scope (S);
3323 end loop;
3324 end Check_Potentially_Blocking_Operation;
3325
3326 ---------------------------------
3327 -- Check_Result_And_Post_State --
3328 ---------------------------------
3329
3330 procedure Check_Result_And_Post_State (Subp_Id : Entity_Id) is
3331 procedure Check_Result_And_Post_State_In_Pragma
3332 (Prag : Node_Id;
3333 Result_Seen : in out Boolean);
3334 -- Determine whether pragma Prag mentions attribute 'Result and whether
3335 -- the pragma contains an expression that evaluates differently in pre-
3336 -- and post-state. Prag is a [refined] postcondition or a contract-cases
3337 -- pragma. Result_Seen is set when the pragma mentions attribute 'Result
3338
3339 function Has_In_Out_Parameter (Subp_Id : Entity_Id) return Boolean;
3340 -- Determine whether subprogram Subp_Id contains at least one IN OUT
3341 -- formal parameter.
3342
3343 -------------------------------------------
3344 -- Check_Result_And_Post_State_In_Pragma --
3345 -------------------------------------------
3346
3347 procedure Check_Result_And_Post_State_In_Pragma
3348 (Prag : Node_Id;
3349 Result_Seen : in out Boolean)
3350 is
3351 procedure Check_Expression (Expr : Node_Id);
3352 -- Perform the 'Result and post-state checks on a given expression
3353
3354 function Is_Function_Result (N : Node_Id) return Traverse_Result;
3355 -- Attempt to find attribute 'Result in a subtree denoted by N
3356
3357 function Is_Trivial_Boolean (N : Node_Id) return Boolean;
3358 -- Determine whether source node N denotes "True" or "False"
3359
3360 function Mentions_Post_State (N : Node_Id) return Boolean;
3361 -- Determine whether a subtree denoted by N mentions any construct
3362 -- that denotes a post-state.
3363
3364 procedure Check_Function_Result is
3365 new Traverse_Proc (Is_Function_Result);
3366
3367 ----------------------
3368 -- Check_Expression --
3369 ----------------------
3370
3371 procedure Check_Expression (Expr : Node_Id) is
3372 begin
3373 if not Is_Trivial_Boolean (Expr) then
3374 Check_Function_Result (Expr);
3375
3376 if not Mentions_Post_State (Expr) then
3377 if Pragma_Name (Prag) = Name_Contract_Cases then
3378 Error_Msg_NE
3379 ("contract case does not check the outcome of calling "
3380 & "&?T?", Expr, Subp_Id);
3381
3382 elsif Pragma_Name (Prag) = Name_Refined_Post then
3383 Error_Msg_NE
3384 ("refined postcondition does not check the outcome of "
3385 & "calling &?T?", Prag, Subp_Id);
3386
3387 else
3388 Error_Msg_NE
3389 ("postcondition does not check the outcome of calling "
3390 & "&?T?", Prag, Subp_Id);
3391 end if;
3392 end if;
3393 end if;
3394 end Check_Expression;
3395
3396 ------------------------
3397 -- Is_Function_Result --
3398 ------------------------
3399
3400 function Is_Function_Result (N : Node_Id) return Traverse_Result is
3401 begin
3402 if Is_Attribute_Result (N) then
3403 Result_Seen := True;
3404 return Abandon;
3405
3406 -- Continue the traversal
3407
3408 else
3409 return OK;
3410 end if;
3411 end Is_Function_Result;
3412
3413 ------------------------
3414 -- Is_Trivial_Boolean --
3415 ------------------------
3416
3417 function Is_Trivial_Boolean (N : Node_Id) return Boolean is
3418 begin
3419 return
3420 Comes_From_Source (N)
3421 and then Is_Entity_Name (N)
3422 and then (Entity (N) = Standard_True
3423 or else
3424 Entity (N) = Standard_False);
3425 end Is_Trivial_Boolean;
3426
3427 -------------------------
3428 -- Mentions_Post_State --
3429 -------------------------
3430
3431 function Mentions_Post_State (N : Node_Id) return Boolean is
3432 Post_State_Seen : Boolean := False;
3433
3434 function Is_Post_State (N : Node_Id) return Traverse_Result;
3435 -- Attempt to find a construct that denotes a post-state. If this
3436 -- is the case, set flag Post_State_Seen.
3437
3438 -------------------
3439 -- Is_Post_State --
3440 -------------------
3441
3442 function Is_Post_State (N : Node_Id) return Traverse_Result is
3443 Ent : Entity_Id;
3444
3445 begin
3446 if Nkind_In (N, N_Explicit_Dereference, N_Function_Call) then
3447 Post_State_Seen := True;
3448 return Abandon;
3449
3450 elsif Nkind_In (N, N_Expanded_Name, N_Identifier) then
3451 Ent := Entity (N);
3452
3453 -- The entity may be modifiable through an implicit
3454 -- dereference.
3455
3456 if No (Ent)
3457 or else Ekind (Ent) in Assignable_Kind
3458 or else (Is_Access_Type (Etype (Ent))
3459 and then Nkind (Parent (N)) =
3460 N_Selected_Component)
3461 then
3462 Post_State_Seen := True;
3463 return Abandon;
3464 end if;
3465
3466 elsif Nkind (N) = N_Attribute_Reference then
3467 if Attribute_Name (N) = Name_Old then
3468 return Skip;
3469
3470 elsif Attribute_Name (N) = Name_Result then
3471 Post_State_Seen := True;
3472 return Abandon;
3473 end if;
3474 end if;
3475
3476 return OK;
3477 end Is_Post_State;
3478
3479 procedure Find_Post_State is new Traverse_Proc (Is_Post_State);
3480
3481 -- Start of processing for Mentions_Post_State
3482
3483 begin
3484 Find_Post_State (N);
3485
3486 return Post_State_Seen;
3487 end Mentions_Post_State;
3488
3489 -- Local variables
3490
3491 Expr : constant Node_Id :=
3492 Get_Pragma_Arg
3493 (First (Pragma_Argument_Associations (Prag)));
3494 Nam : constant Name_Id := Pragma_Name (Prag);
3495 CCase : Node_Id;
3496
3497 -- Start of processing for Check_Result_And_Post_State_In_Pragma
3498
3499 begin
3500 -- Examine all consequences
3501
3502 if Nam = Name_Contract_Cases then
3503 CCase := First (Component_Associations (Expr));
3504 while Present (CCase) loop
3505 Check_Expression (Expression (CCase));
3506
3507 Next (CCase);
3508 end loop;
3509
3510 -- Examine the expression of a postcondition
3511
3512 else pragma Assert (Nam_In (Nam, Name_Postcondition,
3513 Name_Refined_Post));
3514 Check_Expression (Expr);
3515 end if;
3516 end Check_Result_And_Post_State_In_Pragma;
3517
3518 --------------------------
3519 -- Has_In_Out_Parameter --
3520 --------------------------
3521
3522 function Has_In_Out_Parameter (Subp_Id : Entity_Id) return Boolean is
3523 Formal : Entity_Id;
3524
3525 begin
3526 -- Traverse the formals looking for an IN OUT parameter
3527
3528 Formal := First_Formal (Subp_Id);
3529 while Present (Formal) loop
3530 if Ekind (Formal) = E_In_Out_Parameter then
3531 return True;
3532 end if;
3533
3534 Next_Formal (Formal);
3535 end loop;
3536
3537 return False;
3538 end Has_In_Out_Parameter;
3539
3540 -- Local variables
3541
3542 Items : constant Node_Id := Contract (Subp_Id);
3543 Subp_Decl : constant Node_Id := Unit_Declaration_Node (Subp_Id);
3544 Case_Prag : Node_Id := Empty;
3545 Post_Prag : Node_Id := Empty;
3546 Prag : Node_Id;
3547 Seen_In_Case : Boolean := False;
3548 Seen_In_Post : Boolean := False;
3549 Spec_Id : Entity_Id;
3550
3551 -- Start of processing for Check_Result_And_Post_State
3552
3553 begin
3554 -- The lack of attribute 'Result or a post-state is classified as a
3555 -- suspicious contract. Do not perform the check if the corresponding
3556 -- swich is not set.
3557
3558 if not Warn_On_Suspicious_Contract then
3559 return;
3560
3561 -- Nothing to do if there is no contract
3562
3563 elsif No (Items) then
3564 return;
3565 end if;
3566
3567 -- Retrieve the entity of the subprogram spec (if any)
3568
3569 if Nkind (Subp_Decl) = N_Subprogram_Body
3570 and then Present (Corresponding_Spec (Subp_Decl))
3571 then
3572 Spec_Id := Corresponding_Spec (Subp_Decl);
3573
3574 elsif Nkind (Subp_Decl) = N_Subprogram_Body_Stub
3575 and then Present (Corresponding_Spec_Of_Stub (Subp_Decl))
3576 then
3577 Spec_Id := Corresponding_Spec_Of_Stub (Subp_Decl);
3578
3579 else
3580 Spec_Id := Subp_Id;
3581 end if;
3582
3583 -- Examine all postconditions for attribute 'Result and a post-state
3584
3585 Prag := Pre_Post_Conditions (Items);
3586 while Present (Prag) loop
3587 if Nam_In (Pragma_Name (Prag), Name_Postcondition,
3588 Name_Refined_Post)
3589 and then not Error_Posted (Prag)
3590 then
3591 Post_Prag := Prag;
3592 Check_Result_And_Post_State_In_Pragma (Prag, Seen_In_Post);
3593 end if;
3594
3595 Prag := Next_Pragma (Prag);
3596 end loop;
3597
3598 -- Examine the contract cases of the subprogram for attribute 'Result
3599 -- and a post-state.
3600
3601 Prag := Contract_Test_Cases (Items);
3602 while Present (Prag) loop
3603 if Pragma_Name (Prag) = Name_Contract_Cases
3604 and then not Error_Posted (Prag)
3605 then
3606 Case_Prag := Prag;
3607 Check_Result_And_Post_State_In_Pragma (Prag, Seen_In_Case);
3608 end if;
3609
3610 Prag := Next_Pragma (Prag);
3611 end loop;
3612
3613 -- Do not emit any errors if the subprogram is not a function
3614
3615 if not Ekind_In (Spec_Id, E_Function, E_Generic_Function) then
3616 null;
3617
3618 -- Regardless of whether the function has postconditions or contract
3619 -- cases, or whether they mention attribute 'Result, an IN OUT formal
3620 -- parameter is always treated as a result.
3621
3622 elsif Has_In_Out_Parameter (Spec_Id) then
3623 null;
3624
3625 -- The function has both a postcondition and contract cases and they do
3626 -- not mention attribute 'Result.
3627
3628 elsif Present (Case_Prag)
3629 and then not Seen_In_Case
3630 and then Present (Post_Prag)
3631 and then not Seen_In_Post
3632 then
3633 Error_Msg_N
3634 ("neither postcondition nor contract cases mention function "
3635 & "result?T?", Post_Prag);
3636
3637 -- The function has contract cases only and they do not mention
3638 -- attribute 'Result.
3639
3640 elsif Present (Case_Prag) and then not Seen_In_Case then
3641 Error_Msg_N ("contract cases do not mention result?T?", Case_Prag);
3642
3643 -- The function has postconditions only and they do not mention
3644 -- attribute 'Result.
3645
3646 elsif Present (Post_Prag) and then not Seen_In_Post then
3647 Error_Msg_N
3648 ("postcondition does not mention function result?T?", Post_Prag);
3649 end if;
3650 end Check_Result_And_Post_State;
3651
3652 -----------------------------
3653 -- Check_State_Refinements --
3654 -----------------------------
3655
3656 procedure Check_State_Refinements
3657 (Context : Node_Id;
3658 Is_Main_Unit : Boolean := False)
3659 is
3660 procedure Check_Package (Pack : Node_Id);
3661 -- Verify that all abstract states of a [generic] package denoted by its
3662 -- declarative node Pack have proper refinement. Recursively verify the
3663 -- visible and private declarations of the [generic] package for other
3664 -- nested packages.
3665
3666 procedure Check_Packages_In (Decls : List_Id);
3667 -- Seek out [generic] package declarations within declarative list Decls
3668 -- and verify the status of their abstract state refinement.
3669
3670 function SPARK_Mode_Is_Off (N : Node_Id) return Boolean;
3671 -- Determine whether construct N is subject to pragma SPARK_Mode Off
3672
3673 -------------------
3674 -- Check_Package --
3675 -------------------
3676
3677 procedure Check_Package (Pack : Node_Id) is
3678 Body_Id : constant Entity_Id := Corresponding_Body (Pack);
3679 Spec : constant Node_Id := Specification (Pack);
3680 States : constant Elist_Id :=
3681 Abstract_States (Defining_Entity (Pack));
3682
3683 State_Elmt : Elmt_Id;
3684 State_Id : Entity_Id;
3685
3686 begin
3687 -- Do not verify proper state refinement when the package is subject
3688 -- to pragma SPARK_Mode Off because this disables the requirement for
3689 -- state refinement.
3690
3691 if SPARK_Mode_Is_Off (Pack) then
3692 null;
3693
3694 -- State refinement can only occur in a completing packge body. Do
3695 -- not verify proper state refinement when the body is subject to
3696 -- pragma SPARK_Mode Off because this disables the requirement for
3697 -- state refinement.
3698
3699 elsif Present (Body_Id)
3700 and then SPARK_Mode_Is_Off (Unit_Declaration_Node (Body_Id))
3701 then
3702 null;
3703
3704 -- Do not verify proper state refinement when the package is an
3705 -- instance as this check was already performed in the generic.
3706
3707 elsif Present (Generic_Parent (Spec)) then
3708 null;
3709
3710 -- Otherwise examine the contents of the package
3711
3712 else
3713 if Present (States) then
3714 State_Elmt := First_Elmt (States);
3715 while Present (State_Elmt) loop
3716 State_Id := Node (State_Elmt);
3717
3718 -- Emit an error when a non-null state lacks any form of
3719 -- refinement.
3720
3721 if not Is_Null_State (State_Id)
3722 and then not Has_Null_Refinement (State_Id)
3723 and then not Has_Non_Null_Refinement (State_Id)
3724 then
3725 Error_Msg_N ("state & requires refinement", State_Id);
3726 end if;
3727
3728 Next_Elmt (State_Elmt);
3729 end loop;
3730 end if;
3731
3732 Check_Packages_In (Visible_Declarations (Spec));
3733 Check_Packages_In (Private_Declarations (Spec));
3734 end if;
3735 end Check_Package;
3736
3737 -----------------------
3738 -- Check_Packages_In --
3739 -----------------------
3740
3741 procedure Check_Packages_In (Decls : List_Id) is
3742 Decl : Node_Id;
3743
3744 begin
3745 if Present (Decls) then
3746 Decl := First (Decls);
3747 while Present (Decl) loop
3748 if Nkind_In (Decl, N_Generic_Package_Declaration,
3749 N_Package_Declaration)
3750 then
3751 Check_Package (Decl);
3752 end if;
3753
3754 Next (Decl);
3755 end loop;
3756 end if;
3757 end Check_Packages_In;
3758
3759 -----------------------
3760 -- SPARK_Mode_Is_Off --
3761 -----------------------
3762
3763 function SPARK_Mode_Is_Off (N : Node_Id) return Boolean is
3764 Prag : constant Node_Id := SPARK_Pragma (Defining_Entity (N));
3765
3766 begin
3767 return
3768 Present (Prag) and then Get_SPARK_Mode_From_Annotation (Prag) = Off;
3769 end SPARK_Mode_Is_Off;
3770
3771 -- Start of processing for Check_State_Refinements
3772
3773 begin
3774 -- A block may declare a nested package
3775
3776 if Nkind (Context) = N_Block_Statement then
3777 Check_Packages_In (Declarations (Context));
3778
3779 -- An entry, protected, subprogram, or task body may declare a nested
3780 -- package.
3781
3782 elsif Nkind_In (Context, N_Entry_Body,
3783 N_Protected_Body,
3784 N_Subprogram_Body,
3785 N_Task_Body)
3786 then
3787 -- Do not verify proper state refinement when the body is subject to
3788 -- pragma SPARK_Mode Off because this disables the requirement for
3789 -- state refinement.
3790
3791 if not SPARK_Mode_Is_Off (Context) then
3792 Check_Packages_In (Declarations (Context));
3793 end if;
3794
3795 -- A package body may declare a nested package
3796
3797 elsif Nkind (Context) = N_Package_Body then
3798 Check_Package (Unit_Declaration_Node (Corresponding_Spec (Context)));
3799
3800 -- Do not verify proper state refinement when the body is subject to
3801 -- pragma SPARK_Mode Off because this disables the requirement for
3802 -- state refinement.
3803
3804 if not SPARK_Mode_Is_Off (Context) then
3805 Check_Packages_In (Declarations (Context));
3806 end if;
3807
3808 -- A library level [generic] package may declare a nested package
3809
3810 elsif Nkind_In (Context, N_Generic_Package_Declaration,
3811 N_Package_Declaration)
3812 and then Is_Main_Unit
3813 then
3814 Check_Package (Context);
3815 end if;
3816 end Check_State_Refinements;
3817
3818 ------------------------------
3819 -- Check_Unprotected_Access --
3820 ------------------------------
3821
3822 procedure Check_Unprotected_Access
3823 (Context : Node_Id;
3824 Expr : Node_Id)
3825 is
3826 Cont_Encl_Typ : Entity_Id;
3827 Pref_Encl_Typ : Entity_Id;
3828
3829 function Enclosing_Protected_Type (Obj : Node_Id) return Entity_Id;
3830 -- Check whether Obj is a private component of a protected object.
3831 -- Return the protected type where the component resides, Empty
3832 -- otherwise.
3833
3834 function Is_Public_Operation return Boolean;
3835 -- Verify that the enclosing operation is callable from outside the
3836 -- protected object, to minimize false positives.
3837
3838 ------------------------------
3839 -- Enclosing_Protected_Type --
3840 ------------------------------
3841
3842 function Enclosing_Protected_Type (Obj : Node_Id) return Entity_Id is
3843 begin
3844 if Is_Entity_Name (Obj) then
3845 declare
3846 Ent : Entity_Id := Entity (Obj);
3847
3848 begin
3849 -- The object can be a renaming of a private component, use
3850 -- the original record component.
3851
3852 if Is_Prival (Ent) then
3853 Ent := Prival_Link (Ent);
3854 end if;
3855
3856 if Is_Protected_Type (Scope (Ent)) then
3857 return Scope (Ent);
3858 end if;
3859 end;
3860 end if;
3861
3862 -- For indexed and selected components, recursively check the prefix
3863
3864 if Nkind_In (Obj, N_Indexed_Component, N_Selected_Component) then
3865 return Enclosing_Protected_Type (Prefix (Obj));
3866
3867 -- The object does not denote a protected component
3868
3869 else
3870 return Empty;
3871 end if;
3872 end Enclosing_Protected_Type;
3873
3874 -------------------------
3875 -- Is_Public_Operation --
3876 -------------------------
3877
3878 function Is_Public_Operation return Boolean is
3879 S : Entity_Id;
3880 E : Entity_Id;
3881
3882 begin
3883 S := Current_Scope;
3884 while Present (S) and then S /= Pref_Encl_Typ loop
3885 if Scope (S) = Pref_Encl_Typ then
3886 E := First_Entity (Pref_Encl_Typ);
3887 while Present (E)
3888 and then E /= First_Private_Entity (Pref_Encl_Typ)
3889 loop
3890 if E = S then
3891 return True;
3892 end if;
3893
3894 Next_Entity (E);
3895 end loop;
3896 end if;
3897
3898 S := Scope (S);
3899 end loop;
3900
3901 return False;
3902 end Is_Public_Operation;
3903
3904 -- Start of processing for Check_Unprotected_Access
3905
3906 begin
3907 if Nkind (Expr) = N_Attribute_Reference
3908 and then Attribute_Name (Expr) = Name_Unchecked_Access
3909 then
3910 Cont_Encl_Typ := Enclosing_Protected_Type (Context);
3911 Pref_Encl_Typ := Enclosing_Protected_Type (Prefix (Expr));
3912
3913 -- Check whether we are trying to export a protected component to a
3914 -- context with an equal or lower access level.
3915
3916 if Present (Pref_Encl_Typ)
3917 and then No (Cont_Encl_Typ)
3918 and then Is_Public_Operation
3919 and then Scope_Depth (Pref_Encl_Typ) >=
3920 Object_Access_Level (Context)
3921 then
3922 Error_Msg_N
3923 ("??possible unprotected access to protected data", Expr);
3924 end if;
3925 end if;
3926 end Check_Unprotected_Access;
3927
3928 ------------------------------
3929 -- Check_Unused_Body_States --
3930 ------------------------------
3931
3932 procedure Check_Unused_Body_States (Body_Id : Entity_Id) is
3933 procedure Process_Refinement_Clause
3934 (Clause : Node_Id;
3935 States : Elist_Id);
3936 -- Inspect all constituents of refinement clause Clause and remove any
3937 -- matches from body state list States.
3938
3939 procedure Report_Unused_Body_States (States : Elist_Id);
3940 -- Emit errors for each abstract state or object found in list States
3941
3942 -------------------------------
3943 -- Process_Refinement_Clause --
3944 -------------------------------
3945
3946 procedure Process_Refinement_Clause
3947 (Clause : Node_Id;
3948 States : Elist_Id)
3949 is
3950 procedure Process_Constituent (Constit : Node_Id);
3951 -- Remove constituent Constit from body state list States
3952
3953 -------------------------
3954 -- Process_Constituent --
3955 -------------------------
3956
3957 procedure Process_Constituent (Constit : Node_Id) is
3958 Constit_Id : Entity_Id;
3959
3960 begin
3961 -- Guard against illegal constituents. Only abstract states and
3962 -- objects can appear on the right hand side of a refinement.
3963
3964 if Is_Entity_Name (Constit) then
3965 Constit_Id := Entity_Of (Constit);
3966
3967 if Present (Constit_Id)
3968 and then Ekind_In (Constit_Id, E_Abstract_State,
3969 E_Constant,
3970 E_Variable)
3971 then
3972 Remove (States, Constit_Id);
3973 end if;
3974 end if;
3975 end Process_Constituent;
3976
3977 -- Local variables
3978
3979 Constit : Node_Id;
3980
3981 -- Start of processing for Process_Refinement_Clause
3982
3983 begin
3984 if Nkind (Clause) = N_Component_Association then
3985 Constit := Expression (Clause);
3986
3987 -- Multiple constituents appear as an aggregate
3988
3989 if Nkind (Constit) = N_Aggregate then
3990 Constit := First (Expressions (Constit));
3991 while Present (Constit) loop
3992 Process_Constituent (Constit);
3993 Next (Constit);
3994 end loop;
3995
3996 -- Various forms of a single constituent
3997
3998 else
3999 Process_Constituent (Constit);
4000 end if;
4001 end if;
4002 end Process_Refinement_Clause;
4003
4004 -------------------------------
4005 -- Report_Unused_Body_States --
4006 -------------------------------
4007
4008 procedure Report_Unused_Body_States (States : Elist_Id) is
4009 Posted : Boolean := False;
4010 State_Elmt : Elmt_Id;
4011 State_Id : Entity_Id;
4012
4013 begin
4014 if Present (States) then
4015 State_Elmt := First_Elmt (States);
4016 while Present (State_Elmt) loop
4017 State_Id := Node (State_Elmt);
4018
4019 -- Constants are part of the hidden state of a package, but the
4020 -- compiler cannot determine whether they have variable input
4021 -- (SPARK RM 7.1.1(2)) and cannot classify them properly as a
4022 -- hidden state. Do not emit an error when a constant does not
4023 -- participate in a state refinement, even though it acts as a
4024 -- hidden state.
4025
4026 if Ekind (State_Id) = E_Constant then
4027 null;
4028
4029 -- Generate an error message of the form:
4030
4031 -- body of package ... has unused hidden states
4032 -- abstract state ... defined at ...
4033 -- variable ... defined at ...
4034
4035 else
4036 if not Posted then
4037 Posted := True;
4038 SPARK_Msg_N
4039 ("body of package & has unused hidden states", Body_Id);
4040 end if;
4041
4042 Error_Msg_Sloc := Sloc (State_Id);
4043
4044 if Ekind (State_Id) = E_Abstract_State then
4045 SPARK_Msg_NE
4046 ("\abstract state & defined #", Body_Id, State_Id);
4047
4048 else
4049 SPARK_Msg_NE ("\variable & defined #", Body_Id, State_Id);
4050 end if;
4051 end if;
4052
4053 Next_Elmt (State_Elmt);
4054 end loop;
4055 end if;
4056 end Report_Unused_Body_States;
4057
4058 -- Local variables
4059
4060 Prag : constant Node_Id := Get_Pragma (Body_Id, Pragma_Refined_State);
4061 Spec_Id : constant Entity_Id := Spec_Entity (Body_Id);
4062 Clause : Node_Id;
4063 States : Elist_Id;
4064
4065 -- Start of processing for Check_Unused_Body_States
4066
4067 begin
4068 -- Inspect the clauses of pragma Refined_State and determine whether all
4069 -- visible states declared within the package body participate in the
4070 -- refinement.
4071
4072 if Present (Prag) then
4073 Clause := Expression (Get_Argument (Prag, Spec_Id));
4074 States := Collect_Body_States (Body_Id);
4075
4076 -- Multiple non-null state refinements appear as an aggregate
4077
4078 if Nkind (Clause) = N_Aggregate then
4079 Clause := First (Component_Associations (Clause));
4080 while Present (Clause) loop
4081 Process_Refinement_Clause (Clause, States);
4082 Next (Clause);
4083 end loop;
4084
4085 -- Various forms of a single state refinement
4086
4087 else
4088 Process_Refinement_Clause (Clause, States);
4089 end if;
4090
4091 -- Ensure that all abstract states and objects declared in the
4092 -- package body state space are utilized as constituents.
4093
4094 Report_Unused_Body_States (States);
4095 end if;
4096 end Check_Unused_Body_States;
4097
4098 -------------------------
4099 -- Collect_Body_States --
4100 -------------------------
4101
4102 function Collect_Body_States (Body_Id : Entity_Id) return Elist_Id is
4103 function Is_Visible_Object (Obj_Id : Entity_Id) return Boolean;
4104 -- Determine whether object Obj_Id is a suitable visible state of a
4105 -- package body.
4106
4107 procedure Collect_Visible_States
4108 (Pack_Id : Entity_Id;
4109 States : in out Elist_Id);
4110 -- Gather the entities of all abstract states and objects declared in
4111 -- the visible state space of package Pack_Id.
4112
4113 ----------------------------
4114 -- Collect_Visible_States --
4115 ----------------------------
4116
4117 procedure Collect_Visible_States
4118 (Pack_Id : Entity_Id;
4119 States : in out Elist_Id)
4120 is
4121 Item_Id : Entity_Id;
4122
4123 begin
4124 -- Traverse the entity chain of the package and inspect all visible
4125 -- items.
4126
4127 Item_Id := First_Entity (Pack_Id);
4128 while Present (Item_Id) and then not In_Private_Part (Item_Id) loop
4129
4130 -- Do not consider internally generated items as those cannot be
4131 -- named and participate in refinement.
4132
4133 if not Comes_From_Source (Item_Id) then
4134 null;
4135
4136 elsif Ekind (Item_Id) = E_Abstract_State then
4137 Append_New_Elmt (Item_Id, States);
4138
4139 elsif Ekind_In (Item_Id, E_Constant, E_Variable)
4140 and then Is_Visible_Object (Item_Id)
4141 then
4142 Append_New_Elmt (Item_Id, States);
4143
4144 -- Recursively gather the visible states of a nested package
4145
4146 elsif Ekind (Item_Id) = E_Package then
4147 Collect_Visible_States (Item_Id, States);
4148 end if;
4149
4150 Next_Entity (Item_Id);
4151 end loop;
4152 end Collect_Visible_States;
4153
4154 -----------------------
4155 -- Is_Visible_Object --
4156 -----------------------
4157
4158 function Is_Visible_Object (Obj_Id : Entity_Id) return Boolean is
4159 begin
4160 -- Objects that map generic formals to their actuals are not visible
4161 -- from outside the generic instantiation.
4162
4163 if Present (Corresponding_Generic_Association
4164 (Declaration_Node (Obj_Id)))
4165 then
4166 return False;
4167
4168 -- Constituents of a single protected/task type act as components of
4169 -- the type and are not visible from outside the type.
4170
4171 elsif Ekind (Obj_Id) = E_Variable
4172 and then Present (Encapsulating_State (Obj_Id))
4173 and then Is_Single_Concurrent_Object (Encapsulating_State (Obj_Id))
4174 then
4175 return False;
4176
4177 else
4178 return True;
4179 end if;
4180 end Is_Visible_Object;
4181
4182 -- Local variables
4183
4184 Body_Decl : constant Node_Id := Unit_Declaration_Node (Body_Id);
4185 Decl : Node_Id;
4186 Item_Id : Entity_Id;
4187 States : Elist_Id := No_Elist;
4188
4189 -- Start of processing for Collect_Body_States
4190
4191 begin
4192 -- Inspect the declarations of the body looking for source objects,
4193 -- packages and package instantiations. Note that even though this
4194 -- processing is very similar to Collect_Visible_States, a package
4195 -- body does not have a First/Next_Entity list.
4196
4197 Decl := First (Declarations (Body_Decl));
4198 while Present (Decl) loop
4199
4200 -- Capture source objects as internally generated temporaries cannot
4201 -- be named and participate in refinement.
4202
4203 if Nkind (Decl) = N_Object_Declaration then
4204 Item_Id := Defining_Entity (Decl);
4205
4206 if Comes_From_Source (Item_Id)
4207 and then Is_Visible_Object (Item_Id)
4208 then
4209 Append_New_Elmt (Item_Id, States);
4210 end if;
4211
4212 -- Capture the visible abstract states and objects of a source
4213 -- package [instantiation].
4214
4215 elsif Nkind (Decl) = N_Package_Declaration then
4216 Item_Id := Defining_Entity (Decl);
4217
4218 if Comes_From_Source (Item_Id) then
4219 Collect_Visible_States (Item_Id, States);
4220 end if;
4221 end if;
4222
4223 Next (Decl);
4224 end loop;
4225
4226 return States;
4227 end Collect_Body_States;
4228
4229 ------------------------
4230 -- Collect_Interfaces --
4231 ------------------------
4232
4233 procedure Collect_Interfaces
4234 (T : Entity_Id;
4235 Ifaces_List : out Elist_Id;
4236 Exclude_Parents : Boolean := False;
4237 Use_Full_View : Boolean := True)
4238 is
4239 procedure Collect (Typ : Entity_Id);
4240 -- Subsidiary subprogram used to traverse the whole list
4241 -- of directly and indirectly implemented interfaces
4242
4243 -------------
4244 -- Collect --
4245 -------------
4246
4247 procedure Collect (Typ : Entity_Id) is
4248 Ancestor : Entity_Id;
4249 Full_T : Entity_Id;
4250 Id : Node_Id;
4251 Iface : Entity_Id;
4252
4253 begin
4254 Full_T := Typ;
4255
4256 -- Handle private types and subtypes
4257
4258 if Use_Full_View
4259 and then Is_Private_Type (Typ)
4260 and then Present (Full_View (Typ))
4261 then
4262 Full_T := Full_View (Typ);
4263
4264 if Ekind (Full_T) = E_Record_Subtype then
4265 Full_T := Etype (Typ);
4266
4267 if Present (Full_View (Full_T)) then
4268 Full_T := Full_View (Full_T);
4269 end if;
4270 end if;
4271 end if;
4272
4273 -- Include the ancestor if we are generating the whole list of
4274 -- abstract interfaces.
4275
4276 if Etype (Full_T) /= Typ
4277
4278 -- Protect the frontend against wrong sources. For example:
4279
4280 -- package P is
4281 -- type A is tagged null record;
4282 -- type B is new A with private;
4283 -- type C is new A with private;
4284 -- private
4285 -- type B is new C with null record;
4286 -- type C is new B with null record;
4287 -- end P;
4288
4289 and then Etype (Full_T) /= T
4290 then
4291 Ancestor := Etype (Full_T);
4292 Collect (Ancestor);
4293
4294 if Is_Interface (Ancestor) and then not Exclude_Parents then
4295 Append_Unique_Elmt (Ancestor, Ifaces_List);
4296 end if;
4297 end if;
4298
4299 -- Traverse the graph of ancestor interfaces
4300
4301 if Is_Non_Empty_List (Abstract_Interface_List (Full_T)) then
4302 Id := First (Abstract_Interface_List (Full_T));
4303 while Present (Id) loop
4304 Iface := Etype (Id);
4305
4306 -- Protect against wrong uses. For example:
4307 -- type I is interface;
4308 -- type O is tagged null record;
4309 -- type Wrong is new I and O with null record; -- ERROR
4310
4311 if Is_Interface (Iface) then
4312 if Exclude_Parents
4313 and then Etype (T) /= T
4314 and then Interface_Present_In_Ancestor (Etype (T), Iface)
4315 then
4316 null;
4317 else
4318 Collect (Iface);
4319 Append_Unique_Elmt (Iface, Ifaces_List);
4320 end if;
4321 end if;
4322
4323 Next (Id);
4324 end loop;
4325 end if;
4326 end Collect;
4327
4328 -- Start of processing for Collect_Interfaces
4329
4330 begin
4331 pragma Assert (Is_Tagged_Type (T) or else Is_Concurrent_Type (T));
4332 Ifaces_List := New_Elmt_List;
4333 Collect (T);
4334 end Collect_Interfaces;
4335
4336 ----------------------------------
4337 -- Collect_Interface_Components --
4338 ----------------------------------
4339
4340 procedure Collect_Interface_Components
4341 (Tagged_Type : Entity_Id;
4342 Components_List : out Elist_Id)
4343 is
4344 procedure Collect (Typ : Entity_Id);
4345 -- Subsidiary subprogram used to climb to the parents
4346
4347 -------------
4348 -- Collect --
4349 -------------
4350
4351 procedure Collect (Typ : Entity_Id) is
4352 Tag_Comp : Entity_Id;
4353 Parent_Typ : Entity_Id;
4354
4355 begin
4356 -- Handle private types
4357
4358 if Present (Full_View (Etype (Typ))) then
4359 Parent_Typ := Full_View (Etype (Typ));
4360 else
4361 Parent_Typ := Etype (Typ);
4362 end if;
4363
4364 if Parent_Typ /= Typ
4365
4366 -- Protect the frontend against wrong sources. For example:
4367
4368 -- package P is
4369 -- type A is tagged null record;
4370 -- type B is new A with private;
4371 -- type C is new A with private;
4372 -- private
4373 -- type B is new C with null record;
4374 -- type C is new B with null record;
4375 -- end P;
4376
4377 and then Parent_Typ /= Tagged_Type
4378 then
4379 Collect (Parent_Typ);
4380 end if;
4381
4382 -- Collect the components containing tags of secondary dispatch
4383 -- tables.
4384
4385 Tag_Comp := Next_Tag_Component (First_Tag_Component (Typ));
4386 while Present (Tag_Comp) loop
4387 pragma Assert (Present (Related_Type (Tag_Comp)));
4388 Append_Elmt (Tag_Comp, Components_List);
4389
4390 Tag_Comp := Next_Tag_Component (Tag_Comp);
4391 end loop;
4392 end Collect;
4393
4394 -- Start of processing for Collect_Interface_Components
4395
4396 begin
4397 pragma Assert (Ekind (Tagged_Type) = E_Record_Type
4398 and then Is_Tagged_Type (Tagged_Type));
4399
4400 Components_List := New_Elmt_List;
4401 Collect (Tagged_Type);
4402 end Collect_Interface_Components;
4403
4404 -----------------------------
4405 -- Collect_Interfaces_Info --
4406 -----------------------------
4407
4408 procedure Collect_Interfaces_Info
4409 (T : Entity_Id;
4410 Ifaces_List : out Elist_Id;
4411 Components_List : out Elist_Id;
4412 Tags_List : out Elist_Id)
4413 is
4414 Comps_List : Elist_Id;
4415 Comp_Elmt : Elmt_Id;
4416 Comp_Iface : Entity_Id;
4417 Iface_Elmt : Elmt_Id;
4418 Iface : Entity_Id;
4419
4420 function Search_Tag (Iface : Entity_Id) return Entity_Id;
4421 -- Search for the secondary tag associated with the interface type
4422 -- Iface that is implemented by T.
4423
4424 ----------------
4425 -- Search_Tag --
4426 ----------------
4427
4428 function Search_Tag (Iface : Entity_Id) return Entity_Id is
4429 ADT : Elmt_Id;
4430 begin
4431 if not Is_CPP_Class (T) then
4432 ADT := Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (T))));
4433 else
4434 ADT := Next_Elmt (First_Elmt (Access_Disp_Table (T)));
4435 end if;
4436
4437 while Present (ADT)
4438 and then Is_Tag (Node (ADT))
4439 and then Related_Type (Node (ADT)) /= Iface
4440 loop
4441 -- Skip secondary dispatch table referencing thunks to user
4442 -- defined primitives covered by this interface.
4443
4444 pragma Assert (Has_Suffix (Node (ADT), 'P'));
4445 Next_Elmt (ADT);
4446
4447 -- Skip secondary dispatch tables of Ada types
4448
4449 if not Is_CPP_Class (T) then
4450
4451 -- Skip secondary dispatch table referencing thunks to
4452 -- predefined primitives.
4453
4454 pragma Assert (Has_Suffix (Node (ADT), 'Y'));
4455 Next_Elmt (ADT);
4456
4457 -- Skip secondary dispatch table referencing user-defined
4458 -- primitives covered by this interface.
4459
4460 pragma Assert (Has_Suffix (Node (ADT), 'D'));
4461 Next_Elmt (ADT);
4462
4463 -- Skip secondary dispatch table referencing predefined
4464 -- primitives.
4465
4466 pragma Assert (Has_Suffix (Node (ADT), 'Z'));
4467 Next_Elmt (ADT);
4468 end if;
4469 end loop;
4470
4471 pragma Assert (Is_Tag (Node (ADT)));
4472 return Node (ADT);
4473 end Search_Tag;
4474
4475 -- Start of processing for Collect_Interfaces_Info
4476
4477 begin
4478 Collect_Interfaces (T, Ifaces_List);
4479 Collect_Interface_Components (T, Comps_List);
4480
4481 -- Search for the record component and tag associated with each
4482 -- interface type of T.
4483
4484 Components_List := New_Elmt_List;
4485 Tags_List := New_Elmt_List;
4486
4487 Iface_Elmt := First_Elmt (Ifaces_List);
4488 while Present (Iface_Elmt) loop
4489 Iface := Node (Iface_Elmt);
4490
4491 -- Associate the primary tag component and the primary dispatch table
4492 -- with all the interfaces that are parents of T
4493
4494 if Is_Ancestor (Iface, T, Use_Full_View => True) then
4495 Append_Elmt (First_Tag_Component (T), Components_List);
4496 Append_Elmt (Node (First_Elmt (Access_Disp_Table (T))), Tags_List);
4497
4498 -- Otherwise search for the tag component and secondary dispatch
4499 -- table of Iface
4500
4501 else
4502 Comp_Elmt := First_Elmt (Comps_List);
4503 while Present (Comp_Elmt) loop
4504 Comp_Iface := Related_Type (Node (Comp_Elmt));
4505
4506 if Comp_Iface = Iface
4507 or else Is_Ancestor (Iface, Comp_Iface, Use_Full_View => True)
4508 then
4509 Append_Elmt (Node (Comp_Elmt), Components_List);
4510 Append_Elmt (Search_Tag (Comp_Iface), Tags_List);
4511 exit;
4512 end if;
4513
4514 Next_Elmt (Comp_Elmt);
4515 end loop;
4516 pragma Assert (Present (Comp_Elmt));
4517 end if;
4518
4519 Next_Elmt (Iface_Elmt);
4520 end loop;
4521 end Collect_Interfaces_Info;
4522
4523 ---------------------
4524 -- Collect_Parents --
4525 ---------------------
4526
4527 procedure Collect_Parents
4528 (T : Entity_Id;
4529 List : out Elist_Id;
4530 Use_Full_View : Boolean := True)
4531 is
4532 Current_Typ : Entity_Id := T;
4533 Parent_Typ : Entity_Id;
4534
4535 begin
4536 List := New_Elmt_List;
4537
4538 -- No action if the if the type has no parents
4539
4540 if T = Etype (T) then
4541 return;
4542 end if;
4543
4544 loop
4545 Parent_Typ := Etype (Current_Typ);
4546
4547 if Is_Private_Type (Parent_Typ)
4548 and then Present (Full_View (Parent_Typ))
4549 and then Use_Full_View
4550 then
4551 Parent_Typ := Full_View (Base_Type (Parent_Typ));
4552 end if;
4553
4554 Append_Elmt (Parent_Typ, List);
4555
4556 exit when Parent_Typ = Current_Typ;
4557 Current_Typ := Parent_Typ;
4558 end loop;
4559 end Collect_Parents;
4560
4561 ----------------------------------
4562 -- Collect_Primitive_Operations --
4563 ----------------------------------
4564
4565 function Collect_Primitive_Operations (T : Entity_Id) return Elist_Id is
4566 B_Type : constant Entity_Id := Base_Type (T);
4567 B_Decl : constant Node_Id := Original_Node (Parent (B_Type));
4568 B_Scope : Entity_Id := Scope (B_Type);
4569 Op_List : Elist_Id;
4570 Formal : Entity_Id;
4571 Is_Prim : Boolean;
4572 Is_Type_In_Pkg : Boolean;
4573 Formal_Derived : Boolean := False;
4574 Id : Entity_Id;
4575
4576 function Match (E : Entity_Id) return Boolean;
4577 -- True if E's base type is B_Type, or E is of an anonymous access type
4578 -- and the base type of its designated type is B_Type.
4579
4580 -----------
4581 -- Match --
4582 -----------
4583
4584 function Match (E : Entity_Id) return Boolean is
4585 Etyp : Entity_Id := Etype (E);
4586
4587 begin
4588 if Ekind (Etyp) = E_Anonymous_Access_Type then
4589 Etyp := Designated_Type (Etyp);
4590 end if;
4591
4592 -- In Ada 2012 a primitive operation may have a formal of an
4593 -- incomplete view of the parent type.
4594
4595 return Base_Type (Etyp) = B_Type
4596 or else
4597 (Ada_Version >= Ada_2012
4598 and then Ekind (Etyp) = E_Incomplete_Type
4599 and then Full_View (Etyp) = B_Type);
4600 end Match;
4601
4602 -- Start of processing for Collect_Primitive_Operations
4603
4604 begin
4605 -- For tagged types, the primitive operations are collected as they
4606 -- are declared, and held in an explicit list which is simply returned.
4607
4608 if Is_Tagged_Type (B_Type) then
4609 return Primitive_Operations (B_Type);
4610
4611 -- An untagged generic type that is a derived type inherits the
4612 -- primitive operations of its parent type. Other formal types only
4613 -- have predefined operators, which are not explicitly represented.
4614
4615 elsif Is_Generic_Type (B_Type) then
4616 if Nkind (B_Decl) = N_Formal_Type_Declaration
4617 and then Nkind (Formal_Type_Definition (B_Decl)) =
4618 N_Formal_Derived_Type_Definition
4619 then
4620 Formal_Derived := True;
4621 else
4622 return New_Elmt_List;
4623 end if;
4624 end if;
4625
4626 Op_List := New_Elmt_List;
4627
4628 if B_Scope = Standard_Standard then
4629 if B_Type = Standard_String then
4630 Append_Elmt (Standard_Op_Concat, Op_List);
4631
4632 elsif B_Type = Standard_Wide_String then
4633 Append_Elmt (Standard_Op_Concatw, Op_List);
4634
4635 else
4636 null;
4637 end if;
4638
4639 -- Locate the primitive subprograms of the type
4640
4641 else
4642 -- The primitive operations appear after the base type, except
4643 -- if the derivation happens within the private part of B_Scope
4644 -- and the type is a private type, in which case both the type
4645 -- and some primitive operations may appear before the base
4646 -- type, and the list of candidates starts after the type.
4647
4648 if In_Open_Scopes (B_Scope)
4649 and then Scope (T) = B_Scope
4650 and then In_Private_Part (B_Scope)
4651 then
4652 Id := Next_Entity (T);
4653
4654 -- In Ada 2012, If the type has an incomplete partial view, there
4655 -- may be primitive operations declared before the full view, so
4656 -- we need to start scanning from the incomplete view, which is
4657 -- earlier on the entity chain.
4658
4659 elsif Nkind (Parent (B_Type)) = N_Full_Type_Declaration
4660 and then Present (Incomplete_View (Parent (B_Type)))
4661 then
4662 Id := Defining_Entity (Incomplete_View (Parent (B_Type)));
4663
4664 -- If T is a derived from a type with an incomplete view declared
4665 -- elsewhere, that incomplete view is irrelevant, we want the
4666 -- operations in the scope of T.
4667
4668 if Scope (Id) /= Scope (B_Type) then
4669 Id := Next_Entity (B_Type);
4670 end if;
4671
4672 else
4673 Id := Next_Entity (B_Type);
4674 end if;
4675
4676 -- Set flag if this is a type in a package spec
4677
4678 Is_Type_In_Pkg :=
4679 Is_Package_Or_Generic_Package (B_Scope)
4680 and then
4681 Nkind (Parent (Declaration_Node (First_Subtype (T)))) /=
4682 N_Package_Body;
4683
4684 while Present (Id) loop
4685
4686 -- Test whether the result type or any of the parameter types of
4687 -- each subprogram following the type match that type when the
4688 -- type is declared in a package spec, is a derived type, or the
4689 -- subprogram is marked as primitive. (The Is_Primitive test is
4690 -- needed to find primitives of nonderived types in declarative
4691 -- parts that happen to override the predefined "=" operator.)
4692
4693 -- Note that generic formal subprograms are not considered to be
4694 -- primitive operations and thus are never inherited.
4695
4696 if Is_Overloadable (Id)
4697 and then (Is_Type_In_Pkg
4698 or else Is_Derived_Type (B_Type)
4699 or else Is_Primitive (Id))
4700 and then Nkind (Parent (Parent (Id)))
4701 not in N_Formal_Subprogram_Declaration
4702 then
4703 Is_Prim := False;
4704
4705 if Match (Id) then
4706 Is_Prim := True;
4707
4708 else
4709 Formal := First_Formal (Id);
4710 while Present (Formal) loop
4711 if Match (Formal) then
4712 Is_Prim := True;
4713 exit;
4714 end if;
4715
4716 Next_Formal (Formal);
4717 end loop;
4718 end if;
4719
4720 -- For a formal derived type, the only primitives are the ones
4721 -- inherited from the parent type. Operations appearing in the
4722 -- package declaration are not primitive for it.
4723
4724 if Is_Prim
4725 and then (not Formal_Derived or else Present (Alias (Id)))
4726 then
4727 -- In the special case of an equality operator aliased to
4728 -- an overriding dispatching equality belonging to the same
4729 -- type, we don't include it in the list of primitives.
4730 -- This avoids inheriting multiple equality operators when
4731 -- deriving from untagged private types whose full type is
4732 -- tagged, which can otherwise cause ambiguities. Note that
4733 -- this should only happen for this kind of untagged parent
4734 -- type, since normally dispatching operations are inherited
4735 -- using the type's Primitive_Operations list.
4736
4737 if Chars (Id) = Name_Op_Eq
4738 and then Is_Dispatching_Operation (Id)
4739 and then Present (Alias (Id))
4740 and then Present (Overridden_Operation (Alias (Id)))
4741 and then Base_Type (Etype (First_Entity (Id))) =
4742 Base_Type (Etype (First_Entity (Alias (Id))))
4743 then
4744 null;
4745
4746 -- Include the subprogram in the list of primitives
4747
4748 else
4749 Append_Elmt (Id, Op_List);
4750 end if;
4751 end if;
4752 end if;
4753
4754 Next_Entity (Id);
4755
4756 -- For a type declared in System, some of its operations may
4757 -- appear in the target-specific extension to System.
4758
4759 if No (Id)
4760 and then B_Scope = RTU_Entity (System)
4761 and then Present_System_Aux
4762 then
4763 B_Scope := System_Aux_Id;
4764 Id := First_Entity (System_Aux_Id);
4765 end if;
4766 end loop;
4767 end if;
4768
4769 return Op_List;
4770 end Collect_Primitive_Operations;
4771
4772 -----------------------------------
4773 -- Compile_Time_Constraint_Error --
4774 -----------------------------------
4775
4776 function Compile_Time_Constraint_Error
4777 (N : Node_Id;
4778 Msg : String;
4779 Ent : Entity_Id := Empty;
4780 Loc : Source_Ptr := No_Location;
4781 Warn : Boolean := False) return Node_Id
4782 is
4783 Msgc : String (1 .. Msg'Length + 3);
4784 -- Copy of message, with room for possible ?? or << and ! at end
4785
4786 Msgl : Natural;
4787 Wmsg : Boolean;
4788 Eloc : Source_Ptr;
4789
4790 -- Start of processing for Compile_Time_Constraint_Error
4791
4792 begin
4793 -- If this is a warning, convert it into an error if we are in code
4794 -- subject to SPARK_Mode being set On, unless Warn is True to force a
4795 -- warning. The rationale is that a compile-time constraint error should
4796 -- lead to an error instead of a warning when SPARK_Mode is On, but in
4797 -- a few cases we prefer to issue a warning and generate both a suitable
4798 -- run-time error in GNAT and a suitable check message in GNATprove.
4799 -- Those cases are those that likely correspond to deactivated SPARK
4800 -- code, so that this kind of code can be compiled and analyzed instead
4801 -- of being rejected.
4802
4803 Error_Msg_Warn := Warn or SPARK_Mode /= On;
4804
4805 -- A static constraint error in an instance body is not a fatal error.
4806 -- we choose to inhibit the message altogether, because there is no
4807 -- obvious node (for now) on which to post it. On the other hand the
4808 -- offending node must be replaced with a constraint_error in any case.
4809
4810 -- No messages are generated if we already posted an error on this node
4811
4812 if not Error_Posted (N) then
4813 if Loc /= No_Location then
4814 Eloc := Loc;
4815 else
4816 Eloc := Sloc (N);
4817 end if;
4818
4819 -- Copy message to Msgc, converting any ? in the message into
4820 -- < instead, so that we have an error in GNATprove mode.
4821
4822 Msgl := Msg'Length;
4823
4824 for J in 1 .. Msgl loop
4825 if Msg (J) = '?' and then (J = 1 or else Msg (J) /= ''') then
4826 Msgc (J) := '<';
4827 else
4828 Msgc (J) := Msg (J);
4829 end if;
4830 end loop;
4831
4832 -- Message is a warning, even in Ada 95 case
4833
4834 if Msg (Msg'Last) = '?' or else Msg (Msg'Last) = '<' then
4835 Wmsg := True;
4836
4837 -- In Ada 83, all messages are warnings. In the private part and
4838 -- the body of an instance, constraint_checks are only warnings.
4839 -- We also make this a warning if the Warn parameter is set.
4840
4841 elsif Warn
4842 or else (Ada_Version = Ada_83 and then Comes_From_Source (N))
4843 then
4844 Msgl := Msgl + 1;
4845 Msgc (Msgl) := '<';
4846 Msgl := Msgl + 1;
4847 Msgc (Msgl) := '<';
4848 Wmsg := True;
4849
4850 elsif In_Instance_Not_Visible then
4851 Msgl := Msgl + 1;
4852 Msgc (Msgl) := '<';
4853 Msgl := Msgl + 1;
4854 Msgc (Msgl) := '<';
4855 Wmsg := True;
4856
4857 -- Otherwise we have a real error message (Ada 95 static case)
4858 -- and we make this an unconditional message. Note that in the
4859 -- warning case we do not make the message unconditional, it seems
4860 -- quite reasonable to delete messages like this (about exceptions
4861 -- that will be raised) in dead code.
4862
4863 else
4864 Wmsg := False;
4865 Msgl := Msgl + 1;
4866 Msgc (Msgl) := '!';
4867 end if;
4868
4869 -- One more test, skip the warning if the related expression is
4870 -- statically unevaluated, since we don't want to warn about what
4871 -- will happen when something is evaluated if it never will be
4872 -- evaluated.
4873
4874 if not Is_Statically_Unevaluated (N) then
4875 if Present (Ent) then
4876 Error_Msg_NEL (Msgc (1 .. Msgl), N, Ent, Eloc);
4877 else
4878 Error_Msg_NEL (Msgc (1 .. Msgl), N, Etype (N), Eloc);
4879 end if;
4880
4881 if Wmsg then
4882
4883 -- Check whether the context is an Init_Proc
4884
4885 if Inside_Init_Proc then
4886 declare
4887 Conc_Typ : constant Entity_Id :=
4888 Corresponding_Concurrent_Type
4889 (Entity (Parameter_Type (First
4890 (Parameter_Specifications
4891 (Parent (Current_Scope))))));
4892
4893 begin
4894 -- Don't complain if the corresponding concurrent type
4895 -- doesn't come from source (i.e. a single task/protected
4896 -- object).
4897
4898 if Present (Conc_Typ)
4899 and then not Comes_From_Source (Conc_Typ)
4900 then
4901 Error_Msg_NEL
4902 ("\& [<<", N, Standard_Constraint_Error, Eloc);
4903
4904 else
4905 if GNATprove_Mode then
4906 Error_Msg_NEL
4907 ("\& would have been raised for objects of this "
4908 & "type", N, Standard_Constraint_Error, Eloc);
4909 else
4910 Error_Msg_NEL
4911 ("\& will be raised for objects of this type??",
4912 N, Standard_Constraint_Error, Eloc);
4913 end if;
4914 end if;
4915 end;
4916
4917 else
4918 Error_Msg_NEL ("\& [<<", N, Standard_Constraint_Error, Eloc);
4919 end if;
4920
4921 else
4922 Error_Msg ("\static expression fails Constraint_Check", Eloc);
4923 Set_Error_Posted (N);
4924 end if;
4925 end if;
4926 end if;
4927
4928 return N;
4929 end Compile_Time_Constraint_Error;
4930
4931 -----------------------
4932 -- Conditional_Delay --
4933 -----------------------
4934
4935 procedure Conditional_Delay (New_Ent, Old_Ent : Entity_Id) is
4936 begin
4937 if Has_Delayed_Freeze (Old_Ent) and then not Is_Frozen (Old_Ent) then
4938 Set_Has_Delayed_Freeze (New_Ent);
4939 end if;
4940 end Conditional_Delay;
4941
4942 ----------------------------
4943 -- Contains_Refined_State --
4944 ----------------------------
4945
4946 function Contains_Refined_State (Prag : Node_Id) return Boolean is
4947 function Has_State_In_Dependency (List : Node_Id) return Boolean;
4948 -- Determine whether a dependency list mentions a state with a visible
4949 -- refinement.
4950
4951 function Has_State_In_Global (List : Node_Id) return Boolean;
4952 -- Determine whether a global list mentions a state with a visible
4953 -- refinement.
4954
4955 function Is_Refined_State (Item : Node_Id) return Boolean;
4956 -- Determine whether Item is a reference to an abstract state with a
4957 -- visible refinement.
4958
4959 -----------------------------
4960 -- Has_State_In_Dependency --
4961 -----------------------------
4962
4963 function Has_State_In_Dependency (List : Node_Id) return Boolean is
4964 Clause : Node_Id;
4965 Output : Node_Id;
4966
4967 begin
4968 -- A null dependency list does not mention any states
4969
4970 if Nkind (List) = N_Null then
4971 return False;
4972
4973 -- Dependency clauses appear as component associations of an
4974 -- aggregate.
4975
4976 elsif Nkind (List) = N_Aggregate
4977 and then Present (Component_Associations (List))
4978 then
4979 Clause := First (Component_Associations (List));
4980 while Present (Clause) loop
4981
4982 -- Inspect the outputs of a dependency clause
4983
4984 Output := First (Choices (Clause));
4985 while Present (Output) loop
4986 if Is_Refined_State (Output) then
4987 return True;
4988 end if;
4989
4990 Next (Output);
4991 end loop;
4992
4993 -- Inspect the outputs of a dependency clause
4994
4995 if Is_Refined_State (Expression (Clause)) then
4996 return True;
4997 end if;
4998
4999 Next (Clause);
5000 end loop;
5001
5002 -- If we get here, then none of the dependency clauses mention a
5003 -- state with visible refinement.
5004
5005 return False;
5006
5007 -- An illegal pragma managed to sneak in
5008
5009 else
5010 raise Program_Error;
5011 end if;
5012 end Has_State_In_Dependency;
5013
5014 -------------------------
5015 -- Has_State_In_Global --
5016 -------------------------
5017
5018 function Has_State_In_Global (List : Node_Id) return Boolean is
5019 Item : Node_Id;
5020
5021 begin
5022 -- A null global list does not mention any states
5023
5024 if Nkind (List) = N_Null then
5025 return False;
5026
5027 -- Simple global list or moded global list declaration
5028
5029 elsif Nkind (List) = N_Aggregate then
5030
5031 -- The declaration of a simple global list appear as a collection
5032 -- of expressions.
5033
5034 if Present (Expressions (List)) then
5035 Item := First (Expressions (List));
5036 while Present (Item) loop
5037 if Is_Refined_State (Item) then
5038 return True;
5039 end if;
5040
5041 Next (Item);
5042 end loop;
5043
5044 -- The declaration of a moded global list appears as a collection
5045 -- of component associations where individual choices denote
5046 -- modes.
5047
5048 else
5049 Item := First (Component_Associations (List));
5050 while Present (Item) loop
5051 if Has_State_In_Global (Expression (Item)) then
5052 return True;
5053 end if;
5054
5055 Next (Item);
5056 end loop;
5057 end if;
5058
5059 -- If we get here, then the simple/moded global list did not
5060 -- mention any states with a visible refinement.
5061
5062 return False;
5063
5064 -- Single global item declaration
5065
5066 elsif Is_Entity_Name (List) then
5067 return Is_Refined_State (List);
5068
5069 -- An illegal pragma managed to sneak in
5070
5071 else
5072 raise Program_Error;
5073 end if;
5074 end Has_State_In_Global;
5075
5076 ----------------------
5077 -- Is_Refined_State --
5078 ----------------------
5079
5080 function Is_Refined_State (Item : Node_Id) return Boolean is
5081 Elmt : Node_Id;
5082 Item_Id : Entity_Id;
5083
5084 begin
5085 if Nkind (Item) = N_Null then
5086 return False;
5087
5088 -- States cannot be subject to attribute 'Result. This case arises
5089 -- in dependency relations.
5090
5091 elsif Nkind (Item) = N_Attribute_Reference
5092 and then Attribute_Name (Item) = Name_Result
5093 then
5094 return False;
5095
5096 -- Multiple items appear as an aggregate. This case arises in
5097 -- dependency relations.
5098
5099 elsif Nkind (Item) = N_Aggregate
5100 and then Present (Expressions (Item))
5101 then
5102 Elmt := First (Expressions (Item));
5103 while Present (Elmt) loop
5104 if Is_Refined_State (Elmt) then
5105 return True;
5106 end if;
5107
5108 Next (Elmt);
5109 end loop;
5110
5111 -- If we get here, then none of the inputs or outputs reference a
5112 -- state with visible refinement.
5113
5114 return False;
5115
5116 -- Single item
5117
5118 else
5119 Item_Id := Entity_Of (Item);
5120
5121 return
5122 Present (Item_Id)
5123 and then Ekind (Item_Id) = E_Abstract_State
5124 and then Has_Visible_Refinement (Item_Id);
5125 end if;
5126 end Is_Refined_State;
5127
5128 -- Local variables
5129
5130 Arg : constant Node_Id :=
5131 Get_Pragma_Arg (First (Pragma_Argument_Associations (Prag)));
5132 Nam : constant Name_Id := Pragma_Name (Prag);
5133
5134 -- Start of processing for Contains_Refined_State
5135
5136 begin
5137 if Nam = Name_Depends then
5138 return Has_State_In_Dependency (Arg);
5139
5140 else pragma Assert (Nam = Name_Global);
5141 return Has_State_In_Global (Arg);
5142 end if;
5143 end Contains_Refined_State;
5144
5145 -------------------------
5146 -- Copy_Component_List --
5147 -------------------------
5148
5149 function Copy_Component_List
5150 (R_Typ : Entity_Id;
5151 Loc : Source_Ptr) return List_Id
5152 is
5153 Comp : Node_Id;
5154 Comps : constant List_Id := New_List;
5155
5156 begin
5157 Comp := First_Component (Underlying_Type (R_Typ));
5158 while Present (Comp) loop
5159 if Comes_From_Source (Comp) then
5160 declare
5161 Comp_Decl : constant Node_Id := Declaration_Node (Comp);
5162 begin
5163 Append_To (Comps,
5164 Make_Component_Declaration (Loc,
5165 Defining_Identifier =>
5166 Make_Defining_Identifier (Loc, Chars (Comp)),
5167 Component_Definition =>
5168 New_Copy_Tree
5169 (Component_Definition (Comp_Decl), New_Sloc => Loc)));
5170 end;
5171 end if;
5172
5173 Next_Component (Comp);
5174 end loop;
5175
5176 return Comps;
5177 end Copy_Component_List;
5178
5179 -------------------------
5180 -- Copy_Parameter_List --
5181 -------------------------
5182
5183 function Copy_Parameter_List (Subp_Id : Entity_Id) return List_Id is
5184 Loc : constant Source_Ptr := Sloc (Subp_Id);
5185 Plist : List_Id;
5186 Formal : Entity_Id;
5187
5188 begin
5189 if No (First_Formal (Subp_Id)) then
5190 return No_List;
5191 else
5192 Plist := New_List;
5193 Formal := First_Formal (Subp_Id);
5194 while Present (Formal) loop
5195 Append_To (Plist,
5196 Make_Parameter_Specification (Loc,
5197 Defining_Identifier =>
5198 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)),
5199 In_Present => In_Present (Parent (Formal)),
5200 Out_Present => Out_Present (Parent (Formal)),
5201 Parameter_Type =>
5202 New_Occurrence_Of (Etype (Formal), Loc),
5203 Expression =>
5204 New_Copy_Tree (Expression (Parent (Formal)))));
5205
5206 Next_Formal (Formal);
5207 end loop;
5208 end if;
5209
5210 return Plist;
5211 end Copy_Parameter_List;
5212
5213 --------------------------
5214 -- Copy_Subprogram_Spec --
5215 --------------------------
5216
5217 function Copy_Subprogram_Spec (Spec : Node_Id) return Node_Id is
5218 Def_Id : Node_Id;
5219 Formal_Spec : Node_Id;
5220 Result : Node_Id;
5221
5222 begin
5223 -- The structure of the original tree must be replicated without any
5224 -- alterations. Use New_Copy_Tree for this purpose.
5225
5226 Result := New_Copy_Tree (Spec);
5227
5228 -- Create a new entity for the defining unit name
5229
5230 Def_Id := Defining_Unit_Name (Result);
5231 Set_Defining_Unit_Name (Result,
5232 Make_Defining_Identifier (Sloc (Def_Id), Chars (Def_Id)));
5233
5234 -- Create new entities for the formal parameters
5235
5236 if Present (Parameter_Specifications (Result)) then
5237 Formal_Spec := First (Parameter_Specifications (Result));
5238 while Present (Formal_Spec) loop
5239 Def_Id := Defining_Identifier (Formal_Spec);
5240 Set_Defining_Identifier (Formal_Spec,
5241 Make_Defining_Identifier (Sloc (Def_Id), Chars (Def_Id)));
5242
5243 Next (Formal_Spec);
5244 end loop;
5245 end if;
5246
5247 return Result;
5248 end Copy_Subprogram_Spec;
5249
5250 --------------------------------
5251 -- Corresponding_Generic_Type --
5252 --------------------------------
5253
5254 function Corresponding_Generic_Type (T : Entity_Id) return Entity_Id is
5255 Inst : Entity_Id;
5256 Gen : Entity_Id;
5257 Typ : Entity_Id;
5258
5259 begin
5260 if not Is_Generic_Actual_Type (T) then
5261 return Any_Type;
5262
5263 -- If the actual is the actual of an enclosing instance, resolution
5264 -- was correct in the generic.
5265
5266 elsif Nkind (Parent (T)) = N_Subtype_Declaration
5267 and then Is_Entity_Name (Subtype_Indication (Parent (T)))
5268 and then
5269 Is_Generic_Actual_Type (Entity (Subtype_Indication (Parent (T))))
5270 then
5271 return Any_Type;
5272
5273 else
5274 Inst := Scope (T);
5275
5276 if Is_Wrapper_Package (Inst) then
5277 Inst := Related_Instance (Inst);
5278 end if;
5279
5280 Gen :=
5281 Generic_Parent
5282 (Specification (Unit_Declaration_Node (Inst)));
5283
5284 -- Generic actual has the same name as the corresponding formal
5285
5286 Typ := First_Entity (Gen);
5287 while Present (Typ) loop
5288 if Chars (Typ) = Chars (T) then
5289 return Typ;
5290 end if;
5291
5292 Next_Entity (Typ);
5293 end loop;
5294
5295 return Any_Type;
5296 end if;
5297 end Corresponding_Generic_Type;
5298
5299 --------------------
5300 -- Current_Entity --
5301 --------------------
5302
5303 -- The currently visible definition for a given identifier is the
5304 -- one most chained at the start of the visibility chain, i.e. the
5305 -- one that is referenced by the Node_Id value of the name of the
5306 -- given identifier.
5307
5308 function Current_Entity (N : Node_Id) return Entity_Id is
5309 begin
5310 return Get_Name_Entity_Id (Chars (N));
5311 end Current_Entity;
5312
5313 -----------------------------
5314 -- Current_Entity_In_Scope --
5315 -----------------------------
5316
5317 function Current_Entity_In_Scope (N : Node_Id) return Entity_Id is
5318 E : Entity_Id;
5319 CS : constant Entity_Id := Current_Scope;
5320
5321 Transient_Case : constant Boolean := Scope_Is_Transient;
5322
5323 begin
5324 E := Get_Name_Entity_Id (Chars (N));
5325 while Present (E)
5326 and then Scope (E) /= CS
5327 and then (not Transient_Case or else Scope (E) /= Scope (CS))
5328 loop
5329 E := Homonym (E);
5330 end loop;
5331
5332 return E;
5333 end Current_Entity_In_Scope;
5334
5335 -------------------
5336 -- Current_Scope --
5337 -------------------
5338
5339 function Current_Scope return Entity_Id is
5340 begin
5341 if Scope_Stack.Last = -1 then
5342 return Standard_Standard;
5343 else
5344 declare
5345 C : constant Entity_Id :=
5346 Scope_Stack.Table (Scope_Stack.Last).Entity;
5347 begin
5348 if Present (C) then
5349 return C;
5350 else
5351 return Standard_Standard;
5352 end if;
5353 end;
5354 end if;
5355 end Current_Scope;
5356
5357 ----------------------------
5358 -- Current_Scope_No_Loops --
5359 ----------------------------
5360
5361 function Current_Scope_No_Loops return Entity_Id is
5362 S : Entity_Id;
5363
5364 begin
5365 -- Examine the scope stack starting from the current scope and skip any
5366 -- internally generated loops.
5367
5368 S := Current_Scope;
5369 while Present (S) and then S /= Standard_Standard loop
5370 if Ekind (S) = E_Loop and then not Comes_From_Source (S) then
5371 S := Scope (S);
5372 else
5373 exit;
5374 end if;
5375 end loop;
5376
5377 return S;
5378 end Current_Scope_No_Loops;
5379
5380 ------------------------
5381 -- Current_Subprogram --
5382 ------------------------
5383
5384 function Current_Subprogram return Entity_Id is
5385 Scop : constant Entity_Id := Current_Scope;
5386 begin
5387 if Is_Subprogram_Or_Generic_Subprogram (Scop) then
5388 return Scop;
5389 else
5390 return Enclosing_Subprogram (Scop);
5391 end if;
5392 end Current_Subprogram;
5393
5394 ----------------------------------
5395 -- Deepest_Type_Access_Level --
5396 ----------------------------------
5397
5398 function Deepest_Type_Access_Level (Typ : Entity_Id) return Uint is
5399 begin
5400 if Ekind (Typ) = E_Anonymous_Access_Type
5401 and then not Is_Local_Anonymous_Access (Typ)
5402 and then Nkind (Associated_Node_For_Itype (Typ)) = N_Object_Declaration
5403 then
5404 -- Typ is the type of an Ada 2012 stand-alone object of an anonymous
5405 -- access type.
5406
5407 return
5408 Scope_Depth (Enclosing_Dynamic_Scope
5409 (Defining_Identifier
5410 (Associated_Node_For_Itype (Typ))));
5411
5412 -- For generic formal type, return Int'Last (infinite).
5413 -- See comment preceding Is_Generic_Type call in Type_Access_Level.
5414
5415 elsif Is_Generic_Type (Root_Type (Typ)) then
5416 return UI_From_Int (Int'Last);
5417
5418 else
5419 return Type_Access_Level (Typ);
5420 end if;
5421 end Deepest_Type_Access_Level;
5422
5423 ---------------------
5424 -- Defining_Entity --
5425 ---------------------
5426
5427 function Defining_Entity
5428 (N : Node_Id;
5429 Empty_On_Errors : Boolean := False) return Entity_Id
5430 is
5431 Err : Entity_Id := Empty;
5432
5433 begin
5434 case Nkind (N) is
5435 when N_Abstract_Subprogram_Declaration |
5436 N_Expression_Function |
5437 N_Formal_Subprogram_Declaration |
5438 N_Generic_Package_Declaration |
5439 N_Generic_Subprogram_Declaration |
5440 N_Package_Declaration |
5441 N_Subprogram_Body |
5442 N_Subprogram_Body_Stub |
5443 N_Subprogram_Declaration |
5444 N_Subprogram_Renaming_Declaration
5445 =>
5446 return Defining_Entity (Specification (N));
5447
5448 when N_Component_Declaration |
5449 N_Defining_Program_Unit_Name |
5450 N_Discriminant_Specification |
5451 N_Entry_Body |
5452 N_Entry_Declaration |
5453 N_Entry_Index_Specification |
5454 N_Exception_Declaration |
5455 N_Exception_Renaming_Declaration |
5456 N_Formal_Object_Declaration |
5457 N_Formal_Package_Declaration |
5458 N_Formal_Type_Declaration |
5459 N_Full_Type_Declaration |
5460 N_Implicit_Label_Declaration |
5461 N_Incomplete_Type_Declaration |
5462 N_Loop_Parameter_Specification |
5463 N_Number_Declaration |
5464 N_Object_Declaration |
5465 N_Object_Renaming_Declaration |
5466 N_Package_Body_Stub |
5467 N_Parameter_Specification |
5468 N_Private_Extension_Declaration |
5469 N_Private_Type_Declaration |
5470 N_Protected_Body |
5471 N_Protected_Body_Stub |
5472 N_Protected_Type_Declaration |
5473 N_Single_Protected_Declaration |
5474 N_Single_Task_Declaration |
5475 N_Subtype_Declaration |
5476 N_Task_Body |
5477 N_Task_Body_Stub |
5478 N_Task_Type_Declaration
5479 =>
5480 return Defining_Identifier (N);
5481
5482 when N_Subunit =>
5483 return Defining_Entity (Proper_Body (N));
5484
5485 when N_Function_Instantiation |
5486 N_Function_Specification |
5487 N_Generic_Function_Renaming_Declaration |
5488 N_Generic_Package_Renaming_Declaration |
5489 N_Generic_Procedure_Renaming_Declaration |
5490 N_Package_Body |
5491 N_Package_Instantiation |
5492 N_Package_Renaming_Declaration |
5493 N_Package_Specification |
5494 N_Procedure_Instantiation |
5495 N_Procedure_Specification
5496 =>
5497 declare
5498 Nam : constant Node_Id := Defining_Unit_Name (N);
5499
5500 begin
5501 if Nkind (Nam) in N_Entity then
5502 return Nam;
5503
5504 -- For Error, make up a name and attach to declaration so we
5505 -- can continue semantic analysis.
5506
5507 elsif Nam = Error then
5508 if Empty_On_Errors then
5509 return Empty;
5510 else
5511 Err := Make_Temporary (Sloc (N), 'T');
5512 Set_Defining_Unit_Name (N, Err);
5513
5514 return Err;
5515 end if;
5516
5517 -- If not an entity, get defining identifier
5518
5519 else
5520 return Defining_Identifier (Nam);
5521 end if;
5522 end;
5523
5524 when N_Block_Statement |
5525 N_Loop_Statement =>
5526 return Entity (Identifier (N));
5527
5528 when others =>
5529 if Empty_On_Errors then
5530 return Empty;
5531 else
5532 raise Program_Error;
5533 end if;
5534
5535 end case;
5536 end Defining_Entity;
5537
5538 --------------------------
5539 -- Denotes_Discriminant --
5540 --------------------------
5541
5542 function Denotes_Discriminant
5543 (N : Node_Id;
5544 Check_Concurrent : Boolean := False) return Boolean
5545 is
5546 E : Entity_Id;
5547
5548 begin
5549 if not Is_Entity_Name (N) or else No (Entity (N)) then
5550 return False;
5551 else
5552 E := Entity (N);
5553 end if;
5554
5555 -- If we are checking for a protected type, the discriminant may have
5556 -- been rewritten as the corresponding discriminal of the original type
5557 -- or of the corresponding concurrent record, depending on whether we
5558 -- are in the spec or body of the protected type.
5559
5560 return Ekind (E) = E_Discriminant
5561 or else
5562 (Check_Concurrent
5563 and then Ekind (E) = E_In_Parameter
5564 and then Present (Discriminal_Link (E))
5565 and then
5566 (Is_Concurrent_Type (Scope (Discriminal_Link (E)))
5567 or else
5568 Is_Concurrent_Record_Type (Scope (Discriminal_Link (E)))));
5569 end Denotes_Discriminant;
5570
5571 -------------------------
5572 -- Denotes_Same_Object --
5573 -------------------------
5574
5575 function Denotes_Same_Object (A1, A2 : Node_Id) return Boolean is
5576 Obj1 : Node_Id := A1;
5577 Obj2 : Node_Id := A2;
5578
5579 function Has_Prefix (N : Node_Id) return Boolean;
5580 -- Return True if N has attribute Prefix
5581
5582 function Is_Renaming (N : Node_Id) return Boolean;
5583 -- Return true if N names a renaming entity
5584
5585 function Is_Valid_Renaming (N : Node_Id) return Boolean;
5586 -- For renamings, return False if the prefix of any dereference within
5587 -- the renamed object_name is a variable, or any expression within the
5588 -- renamed object_name contains references to variables or calls on
5589 -- nonstatic functions; otherwise return True (RM 6.4.1(6.10/3))
5590
5591 ----------------
5592 -- Has_Prefix --
5593 ----------------
5594
5595 function Has_Prefix (N : Node_Id) return Boolean is
5596 begin
5597 return
5598 Nkind_In (N,
5599 N_Attribute_Reference,
5600 N_Expanded_Name,
5601 N_Explicit_Dereference,
5602 N_Indexed_Component,
5603 N_Reference,
5604 N_Selected_Component,
5605 N_Slice);
5606 end Has_Prefix;
5607
5608 -----------------
5609 -- Is_Renaming --
5610 -----------------
5611
5612 function Is_Renaming (N : Node_Id) return Boolean is
5613 begin
5614 return Is_Entity_Name (N)
5615 and then Present (Renamed_Entity (Entity (N)));
5616 end Is_Renaming;
5617
5618 -----------------------
5619 -- Is_Valid_Renaming --
5620 -----------------------
5621
5622 function Is_Valid_Renaming (N : Node_Id) return Boolean is
5623
5624 function Check_Renaming (N : Node_Id) return Boolean;
5625 -- Recursive function used to traverse all the prefixes of N
5626
5627 function Check_Renaming (N : Node_Id) return Boolean is
5628 begin
5629 if Is_Renaming (N)
5630 and then not Check_Renaming (Renamed_Entity (Entity (N)))
5631 then
5632 return False;
5633 end if;
5634
5635 if Nkind (N) = N_Indexed_Component then
5636 declare
5637 Indx : Node_Id;
5638
5639 begin
5640 Indx := First (Expressions (N));
5641 while Present (Indx) loop
5642 if not Is_OK_Static_Expression (Indx) then
5643 return False;
5644 end if;
5645
5646 Next_Index (Indx);
5647 end loop;
5648 end;
5649 end if;
5650
5651 if Has_Prefix (N) then
5652 declare
5653 P : constant Node_Id := Prefix (N);
5654
5655 begin
5656 if Nkind (N) = N_Explicit_Dereference
5657 and then Is_Variable (P)
5658 then
5659 return False;
5660
5661 elsif Is_Entity_Name (P)
5662 and then Ekind (Entity (P)) = E_Function
5663 then
5664 return False;
5665
5666 elsif Nkind (P) = N_Function_Call then
5667 return False;
5668 end if;
5669
5670 -- Recursion to continue traversing the prefix of the
5671 -- renaming expression
5672
5673 return Check_Renaming (P);
5674 end;
5675 end if;
5676
5677 return True;
5678 end Check_Renaming;
5679
5680 -- Start of processing for Is_Valid_Renaming
5681
5682 begin
5683 return Check_Renaming (N);
5684 end Is_Valid_Renaming;
5685
5686 -- Start of processing for Denotes_Same_Object
5687
5688 begin
5689 -- Both names statically denote the same stand-alone object or parameter
5690 -- (RM 6.4.1(6.5/3))
5691
5692 if Is_Entity_Name (Obj1)
5693 and then Is_Entity_Name (Obj2)
5694 and then Entity (Obj1) = Entity (Obj2)
5695 then
5696 return True;
5697 end if;
5698
5699 -- For renamings, the prefix of any dereference within the renamed
5700 -- object_name is not a variable, and any expression within the
5701 -- renamed object_name contains no references to variables nor
5702 -- calls on nonstatic functions (RM 6.4.1(6.10/3)).
5703
5704 if Is_Renaming (Obj1) then
5705 if Is_Valid_Renaming (Obj1) then
5706 Obj1 := Renamed_Entity (Entity (Obj1));
5707 else
5708 return False;
5709 end if;
5710 end if;
5711
5712 if Is_Renaming (Obj2) then
5713 if Is_Valid_Renaming (Obj2) then
5714 Obj2 := Renamed_Entity (Entity (Obj2));
5715 else
5716 return False;
5717 end if;
5718 end if;
5719
5720 -- No match if not same node kind (such cases are handled by
5721 -- Denotes_Same_Prefix)
5722
5723 if Nkind (Obj1) /= Nkind (Obj2) then
5724 return False;
5725
5726 -- After handling valid renamings, one of the two names statically
5727 -- denoted a renaming declaration whose renamed object_name is known
5728 -- to denote the same object as the other (RM 6.4.1(6.10/3))
5729
5730 elsif Is_Entity_Name (Obj1) then
5731 if Is_Entity_Name (Obj2) then
5732 return Entity (Obj1) = Entity (Obj2);
5733 else
5734 return False;
5735 end if;
5736
5737 -- Both names are selected_components, their prefixes are known to
5738 -- denote the same object, and their selector_names denote the same
5739 -- component (RM 6.4.1(6.6/3)).
5740
5741 elsif Nkind (Obj1) = N_Selected_Component then
5742 return Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2))
5743 and then
5744 Entity (Selector_Name (Obj1)) = Entity (Selector_Name (Obj2));
5745
5746 -- Both names are dereferences and the dereferenced names are known to
5747 -- denote the same object (RM 6.4.1(6.7/3))
5748
5749 elsif Nkind (Obj1) = N_Explicit_Dereference then
5750 return Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2));
5751
5752 -- Both names are indexed_components, their prefixes are known to denote
5753 -- the same object, and each of the pairs of corresponding index values
5754 -- are either both static expressions with the same static value or both
5755 -- names that are known to denote the same object (RM 6.4.1(6.8/3))
5756
5757 elsif Nkind (Obj1) = N_Indexed_Component then
5758 if not Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2)) then
5759 return False;
5760 else
5761 declare
5762 Indx1 : Node_Id;
5763 Indx2 : Node_Id;
5764
5765 begin
5766 Indx1 := First (Expressions (Obj1));
5767 Indx2 := First (Expressions (Obj2));
5768 while Present (Indx1) loop
5769
5770 -- Indexes must denote the same static value or same object
5771
5772 if Is_OK_Static_Expression (Indx1) then
5773 if not Is_OK_Static_Expression (Indx2) then
5774 return False;
5775
5776 elsif Expr_Value (Indx1) /= Expr_Value (Indx2) then
5777 return False;
5778 end if;
5779
5780 elsif not Denotes_Same_Object (Indx1, Indx2) then
5781 return False;
5782 end if;
5783
5784 Next (Indx1);
5785 Next (Indx2);
5786 end loop;
5787
5788 return True;
5789 end;
5790 end if;
5791
5792 -- Both names are slices, their prefixes are known to denote the same
5793 -- object, and the two slices have statically matching index constraints
5794 -- (RM 6.4.1(6.9/3))
5795
5796 elsif Nkind (Obj1) = N_Slice
5797 and then Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2))
5798 then
5799 declare
5800 Lo1, Lo2, Hi1, Hi2 : Node_Id;
5801
5802 begin
5803 Get_Index_Bounds (Etype (Obj1), Lo1, Hi1);
5804 Get_Index_Bounds (Etype (Obj2), Lo2, Hi2);
5805
5806 -- Check whether bounds are statically identical. There is no
5807 -- attempt to detect partial overlap of slices.
5808
5809 return Denotes_Same_Object (Lo1, Lo2)
5810 and then
5811 Denotes_Same_Object (Hi1, Hi2);
5812 end;
5813
5814 -- In the recursion, literals appear as indexes
5815
5816 elsif Nkind (Obj1) = N_Integer_Literal
5817 and then
5818 Nkind (Obj2) = N_Integer_Literal
5819 then
5820 return Intval (Obj1) = Intval (Obj2);
5821
5822 else
5823 return False;
5824 end if;
5825 end Denotes_Same_Object;
5826
5827 -------------------------
5828 -- Denotes_Same_Prefix --
5829 -------------------------
5830
5831 function Denotes_Same_Prefix (A1, A2 : Node_Id) return Boolean is
5832 begin
5833 if Is_Entity_Name (A1) then
5834 if Nkind_In (A2, N_Selected_Component, N_Indexed_Component)
5835 and then not Is_Access_Type (Etype (A1))
5836 then
5837 return Denotes_Same_Object (A1, Prefix (A2))
5838 or else Denotes_Same_Prefix (A1, Prefix (A2));
5839 else
5840 return False;
5841 end if;
5842
5843 elsif Is_Entity_Name (A2) then
5844 return Denotes_Same_Prefix (A1 => A2, A2 => A1);
5845
5846 elsif Nkind_In (A1, N_Selected_Component, N_Indexed_Component, N_Slice)
5847 and then
5848 Nkind_In (A2, N_Selected_Component, N_Indexed_Component, N_Slice)
5849 then
5850 declare
5851 Root1, Root2 : Node_Id;
5852 Depth1, Depth2 : Nat := 0;
5853
5854 begin
5855 Root1 := Prefix (A1);
5856 while not Is_Entity_Name (Root1) loop
5857 if not Nkind_In
5858 (Root1, N_Selected_Component, N_Indexed_Component)
5859 then
5860 return False;
5861 else
5862 Root1 := Prefix (Root1);
5863 end if;
5864
5865 Depth1 := Depth1 + 1;
5866 end loop;
5867
5868 Root2 := Prefix (A2);
5869 while not Is_Entity_Name (Root2) loop
5870 if not Nkind_In (Root2, N_Selected_Component,
5871 N_Indexed_Component)
5872 then
5873 return False;
5874 else
5875 Root2 := Prefix (Root2);
5876 end if;
5877
5878 Depth2 := Depth2 + 1;
5879 end loop;
5880
5881 -- If both have the same depth and they do not denote the same
5882 -- object, they are disjoint and no warning is needed.
5883
5884 if Depth1 = Depth2 then
5885 return False;
5886
5887 elsif Depth1 > Depth2 then
5888 Root1 := Prefix (A1);
5889 for J in 1 .. Depth1 - Depth2 - 1 loop
5890 Root1 := Prefix (Root1);
5891 end loop;
5892
5893 return Denotes_Same_Object (Root1, A2);
5894
5895 else
5896 Root2 := Prefix (A2);
5897 for J in 1 .. Depth2 - Depth1 - 1 loop
5898 Root2 := Prefix (Root2);
5899 end loop;
5900
5901 return Denotes_Same_Object (A1, Root2);
5902 end if;
5903 end;
5904
5905 else
5906 return False;
5907 end if;
5908 end Denotes_Same_Prefix;
5909
5910 ----------------------
5911 -- Denotes_Variable --
5912 ----------------------
5913
5914 function Denotes_Variable (N : Node_Id) return Boolean is
5915 begin
5916 return Is_Variable (N) and then Paren_Count (N) = 0;
5917 end Denotes_Variable;
5918
5919 -----------------------------
5920 -- Depends_On_Discriminant --
5921 -----------------------------
5922
5923 function Depends_On_Discriminant (N : Node_Id) return Boolean is
5924 L : Node_Id;
5925 H : Node_Id;
5926
5927 begin
5928 Get_Index_Bounds (N, L, H);
5929 return Denotes_Discriminant (L) or else Denotes_Discriminant (H);
5930 end Depends_On_Discriminant;
5931
5932 -------------------------
5933 -- Designate_Same_Unit --
5934 -------------------------
5935
5936 function Designate_Same_Unit
5937 (Name1 : Node_Id;
5938 Name2 : Node_Id) return Boolean
5939 is
5940 K1 : constant Node_Kind := Nkind (Name1);
5941 K2 : constant Node_Kind := Nkind (Name2);
5942
5943 function Prefix_Node (N : Node_Id) return Node_Id;
5944 -- Returns the parent unit name node of a defining program unit name
5945 -- or the prefix if N is a selected component or an expanded name.
5946
5947 function Select_Node (N : Node_Id) return Node_Id;
5948 -- Returns the defining identifier node of a defining program unit
5949 -- name or the selector node if N is a selected component or an
5950 -- expanded name.
5951
5952 -----------------
5953 -- Prefix_Node --
5954 -----------------
5955
5956 function Prefix_Node (N : Node_Id) return Node_Id is
5957 begin
5958 if Nkind (N) = N_Defining_Program_Unit_Name then
5959 return Name (N);
5960 else
5961 return Prefix (N);
5962 end if;
5963 end Prefix_Node;
5964
5965 -----------------
5966 -- Select_Node --
5967 -----------------
5968
5969 function Select_Node (N : Node_Id) return Node_Id is
5970 begin
5971 if Nkind (N) = N_Defining_Program_Unit_Name then
5972 return Defining_Identifier (N);
5973 else
5974 return Selector_Name (N);
5975 end if;
5976 end Select_Node;
5977
5978 -- Start of processing for Designate_Same_Unit
5979
5980 begin
5981 if Nkind_In (K1, N_Identifier, N_Defining_Identifier)
5982 and then
5983 Nkind_In (K2, N_Identifier, N_Defining_Identifier)
5984 then
5985 return Chars (Name1) = Chars (Name2);
5986
5987 elsif Nkind_In (K1, N_Expanded_Name,
5988 N_Selected_Component,
5989 N_Defining_Program_Unit_Name)
5990 and then
5991 Nkind_In (K2, N_Expanded_Name,
5992 N_Selected_Component,
5993 N_Defining_Program_Unit_Name)
5994 then
5995 return
5996 (Chars (Select_Node (Name1)) = Chars (Select_Node (Name2)))
5997 and then
5998 Designate_Same_Unit (Prefix_Node (Name1), Prefix_Node (Name2));
5999
6000 else
6001 return False;
6002 end if;
6003 end Designate_Same_Unit;
6004
6005 ------------------------------------------
6006 -- function Dynamic_Accessibility_Level --
6007 ------------------------------------------
6008
6009 function Dynamic_Accessibility_Level (Expr : Node_Id) return Node_Id is
6010 E : Entity_Id;
6011 Loc : constant Source_Ptr := Sloc (Expr);
6012
6013 function Make_Level_Literal (Level : Uint) return Node_Id;
6014 -- Construct an integer literal representing an accessibility level
6015 -- with its type set to Natural.
6016
6017 ------------------------
6018 -- Make_Level_Literal --
6019 ------------------------
6020
6021 function Make_Level_Literal (Level : Uint) return Node_Id is
6022 Result : constant Node_Id := Make_Integer_Literal (Loc, Level);
6023 begin
6024 Set_Etype (Result, Standard_Natural);
6025 return Result;
6026 end Make_Level_Literal;
6027
6028 -- Start of processing for Dynamic_Accessibility_Level
6029
6030 begin
6031 if Is_Entity_Name (Expr) then
6032 E := Entity (Expr);
6033
6034 if Present (Renamed_Object (E)) then
6035 return Dynamic_Accessibility_Level (Renamed_Object (E));
6036 end if;
6037
6038 if Is_Formal (E) or else Ekind_In (E, E_Variable, E_Constant) then
6039 if Present (Extra_Accessibility (E)) then
6040 return New_Occurrence_Of (Extra_Accessibility (E), Loc);
6041 end if;
6042 end if;
6043 end if;
6044
6045 -- Unimplemented: Ptr.all'Access, where Ptr has Extra_Accessibility ???
6046
6047 case Nkind (Expr) is
6048
6049 -- For access discriminant, the level of the enclosing object
6050
6051 when N_Selected_Component =>
6052 if Ekind (Entity (Selector_Name (Expr))) = E_Discriminant
6053 and then Ekind (Etype (Entity (Selector_Name (Expr)))) =
6054 E_Anonymous_Access_Type
6055 then
6056 return Make_Level_Literal (Object_Access_Level (Expr));
6057 end if;
6058
6059 when N_Attribute_Reference =>
6060 case Get_Attribute_Id (Attribute_Name (Expr)) is
6061
6062 -- For X'Access, the level of the prefix X
6063
6064 when Attribute_Access =>
6065 return Make_Level_Literal
6066 (Object_Access_Level (Prefix (Expr)));
6067
6068 -- Treat the unchecked attributes as library-level
6069
6070 when Attribute_Unchecked_Access |
6071 Attribute_Unrestricted_Access =>
6072 return Make_Level_Literal (Scope_Depth (Standard_Standard));
6073
6074 -- No other access-valued attributes
6075
6076 when others =>
6077 raise Program_Error;
6078 end case;
6079
6080 when N_Allocator =>
6081
6082 -- Unimplemented: depends on context. As an actual parameter where
6083 -- formal type is anonymous, use
6084 -- Scope_Depth (Current_Scope) + 1.
6085 -- For other cases, see 3.10.2(14/3) and following. ???
6086
6087 null;
6088
6089 when N_Type_Conversion =>
6090 if not Is_Local_Anonymous_Access (Etype (Expr)) then
6091
6092 -- Handle type conversions introduced for a rename of an
6093 -- Ada 2012 stand-alone object of an anonymous access type.
6094
6095 return Dynamic_Accessibility_Level (Expression (Expr));
6096 end if;
6097
6098 when others =>
6099 null;
6100 end case;
6101
6102 return Make_Level_Literal (Type_Access_Level (Etype (Expr)));
6103 end Dynamic_Accessibility_Level;
6104
6105 -----------------------------------
6106 -- Effective_Extra_Accessibility --
6107 -----------------------------------
6108
6109 function Effective_Extra_Accessibility (Id : Entity_Id) return Entity_Id is
6110 begin
6111 if Present (Renamed_Object (Id))
6112 and then Is_Entity_Name (Renamed_Object (Id))
6113 then
6114 return Effective_Extra_Accessibility (Entity (Renamed_Object (Id)));
6115 else
6116 return Extra_Accessibility (Id);
6117 end if;
6118 end Effective_Extra_Accessibility;
6119
6120 -----------------------------
6121 -- Effective_Reads_Enabled --
6122 -----------------------------
6123
6124 function Effective_Reads_Enabled (Id : Entity_Id) return Boolean is
6125 begin
6126 return Has_Enabled_Property (Id, Name_Effective_Reads);
6127 end Effective_Reads_Enabled;
6128
6129 ------------------------------
6130 -- Effective_Writes_Enabled --
6131 ------------------------------
6132
6133 function Effective_Writes_Enabled (Id : Entity_Id) return Boolean is
6134 begin
6135 return Has_Enabled_Property (Id, Name_Effective_Writes);
6136 end Effective_Writes_Enabled;
6137
6138 ------------------------------
6139 -- Enclosing_Comp_Unit_Node --
6140 ------------------------------
6141
6142 function Enclosing_Comp_Unit_Node (N : Node_Id) return Node_Id is
6143 Current_Node : Node_Id;
6144
6145 begin
6146 Current_Node := N;
6147 while Present (Current_Node)
6148 and then Nkind (Current_Node) /= N_Compilation_Unit
6149 loop
6150 Current_Node := Parent (Current_Node);
6151 end loop;
6152
6153 if Nkind (Current_Node) /= N_Compilation_Unit then
6154 return Empty;
6155 else
6156 return Current_Node;
6157 end if;
6158 end Enclosing_Comp_Unit_Node;
6159
6160 --------------------------
6161 -- Enclosing_CPP_Parent --
6162 --------------------------
6163
6164 function Enclosing_CPP_Parent (Typ : Entity_Id) return Entity_Id is
6165 Parent_Typ : Entity_Id := Typ;
6166
6167 begin
6168 while not Is_CPP_Class (Parent_Typ)
6169 and then Etype (Parent_Typ) /= Parent_Typ
6170 loop
6171 Parent_Typ := Etype (Parent_Typ);
6172
6173 if Is_Private_Type (Parent_Typ) then
6174 Parent_Typ := Full_View (Base_Type (Parent_Typ));
6175 end if;
6176 end loop;
6177
6178 pragma Assert (Is_CPP_Class (Parent_Typ));
6179 return Parent_Typ;
6180 end Enclosing_CPP_Parent;
6181
6182 ---------------------------
6183 -- Enclosing_Declaration --
6184 ---------------------------
6185
6186 function Enclosing_Declaration (N : Node_Id) return Node_Id is
6187 Decl : Node_Id := N;
6188
6189 begin
6190 while Present (Decl)
6191 and then not (Nkind (Decl) in N_Declaration
6192 or else
6193 Nkind (Decl) in N_Later_Decl_Item)
6194 loop
6195 Decl := Parent (Decl);
6196 end loop;
6197
6198 return Decl;
6199 end Enclosing_Declaration;
6200
6201 ----------------------------
6202 -- Enclosing_Generic_Body --
6203 ----------------------------
6204
6205 function Enclosing_Generic_Body
6206 (N : Node_Id) return Node_Id
6207 is
6208 P : Node_Id;
6209 Decl : Node_Id;
6210 Spec : Node_Id;
6211
6212 begin
6213 P := Parent (N);
6214 while Present (P) loop
6215 if Nkind (P) = N_Package_Body
6216 or else Nkind (P) = N_Subprogram_Body
6217 then
6218 Spec := Corresponding_Spec (P);
6219
6220 if Present (Spec) then
6221 Decl := Unit_Declaration_Node (Spec);
6222
6223 if Nkind (Decl) = N_Generic_Package_Declaration
6224 or else Nkind (Decl) = N_Generic_Subprogram_Declaration
6225 then
6226 return P;
6227 end if;
6228 end if;
6229 end if;
6230
6231 P := Parent (P);
6232 end loop;
6233
6234 return Empty;
6235 end Enclosing_Generic_Body;
6236
6237 ----------------------------
6238 -- Enclosing_Generic_Unit --
6239 ----------------------------
6240
6241 function Enclosing_Generic_Unit
6242 (N : Node_Id) return Node_Id
6243 is
6244 P : Node_Id;
6245 Decl : Node_Id;
6246 Spec : Node_Id;
6247
6248 begin
6249 P := Parent (N);
6250 while Present (P) loop
6251 if Nkind (P) = N_Generic_Package_Declaration
6252 or else Nkind (P) = N_Generic_Subprogram_Declaration
6253 then
6254 return P;
6255
6256 elsif Nkind (P) = N_Package_Body
6257 or else Nkind (P) = N_Subprogram_Body
6258 then
6259 Spec := Corresponding_Spec (P);
6260
6261 if Present (Spec) then
6262 Decl := Unit_Declaration_Node (Spec);
6263
6264 if Nkind (Decl) = N_Generic_Package_Declaration
6265 or else Nkind (Decl) = N_Generic_Subprogram_Declaration
6266 then
6267 return Decl;
6268 end if;
6269 end if;
6270 end if;
6271
6272 P := Parent (P);
6273 end loop;
6274
6275 return Empty;
6276 end Enclosing_Generic_Unit;
6277
6278 -------------------------------
6279 -- Enclosing_Lib_Unit_Entity --
6280 -------------------------------
6281
6282 function Enclosing_Lib_Unit_Entity
6283 (E : Entity_Id := Current_Scope) return Entity_Id
6284 is
6285 Unit_Entity : Entity_Id;
6286
6287 begin
6288 -- Look for enclosing library unit entity by following scope links.
6289 -- Equivalent to, but faster than indexing through the scope stack.
6290
6291 Unit_Entity := E;
6292 while (Present (Scope (Unit_Entity))
6293 and then Scope (Unit_Entity) /= Standard_Standard)
6294 and not Is_Child_Unit (Unit_Entity)
6295 loop
6296 Unit_Entity := Scope (Unit_Entity);
6297 end loop;
6298
6299 return Unit_Entity;
6300 end Enclosing_Lib_Unit_Entity;
6301
6302 -----------------------------
6303 -- Enclosing_Lib_Unit_Node --
6304 -----------------------------
6305
6306 function Enclosing_Lib_Unit_Node (N : Node_Id) return Node_Id is
6307 Encl_Unit : Node_Id;
6308
6309 begin
6310 Encl_Unit := Enclosing_Comp_Unit_Node (N);
6311 while Present (Encl_Unit)
6312 and then Nkind (Unit (Encl_Unit)) = N_Subunit
6313 loop
6314 Encl_Unit := Library_Unit (Encl_Unit);
6315 end loop;
6316
6317 return Encl_Unit;
6318 end Enclosing_Lib_Unit_Node;
6319
6320 -----------------------
6321 -- Enclosing_Package --
6322 -----------------------
6323
6324 function Enclosing_Package (E : Entity_Id) return Entity_Id is
6325 Dynamic_Scope : constant Entity_Id := Enclosing_Dynamic_Scope (E);
6326
6327 begin
6328 if Dynamic_Scope = Standard_Standard then
6329 return Standard_Standard;
6330
6331 elsif Dynamic_Scope = Empty then
6332 return Empty;
6333
6334 elsif Ekind_In (Dynamic_Scope, E_Package, E_Package_Body,
6335 E_Generic_Package)
6336 then
6337 return Dynamic_Scope;
6338
6339 else
6340 return Enclosing_Package (Dynamic_Scope);
6341 end if;
6342 end Enclosing_Package;
6343
6344 -------------------------------------
6345 -- Enclosing_Package_Or_Subprogram --
6346 -------------------------------------
6347
6348 function Enclosing_Package_Or_Subprogram (E : Entity_Id) return Entity_Id is
6349 S : Entity_Id;
6350
6351 begin
6352 S := Scope (E);
6353 while Present (S) loop
6354 if Is_Package_Or_Generic_Package (S)
6355 or else Ekind (S) = E_Package_Body
6356 then
6357 return S;
6358
6359 elsif Is_Subprogram_Or_Generic_Subprogram (S)
6360 or else Ekind (S) = E_Subprogram_Body
6361 then
6362 return S;
6363
6364 else
6365 S := Scope (S);
6366 end if;
6367 end loop;
6368
6369 return Empty;
6370 end Enclosing_Package_Or_Subprogram;
6371
6372 --------------------------
6373 -- Enclosing_Subprogram --
6374 --------------------------
6375
6376 function Enclosing_Subprogram (E : Entity_Id) return Entity_Id is
6377 Dynamic_Scope : constant Entity_Id := Enclosing_Dynamic_Scope (E);
6378
6379 begin
6380 if Dynamic_Scope = Standard_Standard then
6381 return Empty;
6382
6383 elsif Dynamic_Scope = Empty then
6384 return Empty;
6385
6386 elsif Ekind (Dynamic_Scope) = E_Subprogram_Body then
6387 return Corresponding_Spec (Parent (Parent (Dynamic_Scope)));
6388
6389 elsif Ekind (Dynamic_Scope) = E_Block
6390 or else Ekind (Dynamic_Scope) = E_Return_Statement
6391 then
6392 return Enclosing_Subprogram (Dynamic_Scope);
6393
6394 elsif Ekind (Dynamic_Scope) = E_Task_Type then
6395 return Get_Task_Body_Procedure (Dynamic_Scope);
6396
6397 elsif Ekind (Dynamic_Scope) = E_Limited_Private_Type
6398 and then Present (Full_View (Dynamic_Scope))
6399 and then Ekind (Full_View (Dynamic_Scope)) = E_Task_Type
6400 then
6401 return Get_Task_Body_Procedure (Full_View (Dynamic_Scope));
6402
6403 -- No body is generated if the protected operation is eliminated
6404
6405 elsif Convention (Dynamic_Scope) = Convention_Protected
6406 and then not Is_Eliminated (Dynamic_Scope)
6407 and then Present (Protected_Body_Subprogram (Dynamic_Scope))
6408 then
6409 return Protected_Body_Subprogram (Dynamic_Scope);
6410
6411 else
6412 return Dynamic_Scope;
6413 end if;
6414 end Enclosing_Subprogram;
6415
6416 ------------------------
6417 -- Ensure_Freeze_Node --
6418 ------------------------
6419
6420 procedure Ensure_Freeze_Node (E : Entity_Id) is
6421 FN : Node_Id;
6422 begin
6423 if No (Freeze_Node (E)) then
6424 FN := Make_Freeze_Entity (Sloc (E));
6425 Set_Has_Delayed_Freeze (E);
6426 Set_Freeze_Node (E, FN);
6427 Set_Access_Types_To_Process (FN, No_Elist);
6428 Set_TSS_Elist (FN, No_Elist);
6429 Set_Entity (FN, E);
6430 end if;
6431 end Ensure_Freeze_Node;
6432
6433 ----------------
6434 -- Enter_Name --
6435 ----------------
6436
6437 procedure Enter_Name (Def_Id : Entity_Id) is
6438 C : constant Entity_Id := Current_Entity (Def_Id);
6439 E : constant Entity_Id := Current_Entity_In_Scope (Def_Id);
6440 S : constant Entity_Id := Current_Scope;
6441
6442 begin
6443 Generate_Definition (Def_Id);
6444
6445 -- Add new name to current scope declarations. Check for duplicate
6446 -- declaration, which may or may not be a genuine error.
6447
6448 if Present (E) then
6449
6450 -- Case of previous entity entered because of a missing declaration
6451 -- or else a bad subtype indication. Best is to use the new entity,
6452 -- and make the previous one invisible.
6453
6454 if Etype (E) = Any_Type then
6455 Set_Is_Immediately_Visible (E, False);
6456
6457 -- Case of renaming declaration constructed for package instances.
6458 -- if there is an explicit declaration with the same identifier,
6459 -- the renaming is not immediately visible any longer, but remains
6460 -- visible through selected component notation.
6461
6462 elsif Nkind (Parent (E)) = N_Package_Renaming_Declaration
6463 and then not Comes_From_Source (E)
6464 then
6465 Set_Is_Immediately_Visible (E, False);
6466
6467 -- The new entity may be the package renaming, which has the same
6468 -- same name as a generic formal which has been seen already.
6469
6470 elsif Nkind (Parent (Def_Id)) = N_Package_Renaming_Declaration
6471 and then not Comes_From_Source (Def_Id)
6472 then
6473 Set_Is_Immediately_Visible (E, False);
6474
6475 -- For a fat pointer corresponding to a remote access to subprogram,
6476 -- we use the same identifier as the RAS type, so that the proper
6477 -- name appears in the stub. This type is only retrieved through
6478 -- the RAS type and never by visibility, and is not added to the
6479 -- visibility list (see below).
6480
6481 elsif Nkind (Parent (Def_Id)) = N_Full_Type_Declaration
6482 and then Ekind (Def_Id) = E_Record_Type
6483 and then Present (Corresponding_Remote_Type (Def_Id))
6484 then
6485 null;
6486
6487 -- Case of an implicit operation or derived literal. The new entity
6488 -- hides the implicit one, which is removed from all visibility,
6489 -- i.e. the entity list of its scope, and homonym chain of its name.
6490
6491 elsif (Is_Overloadable (E) and then Is_Inherited_Operation (E))
6492 or else Is_Internal (E)
6493 then
6494 declare
6495 Decl : constant Node_Id := Parent (E);
6496 Prev : Entity_Id;
6497 Prev_Vis : Entity_Id;
6498
6499 begin
6500 -- If E is an implicit declaration, it cannot be the first
6501 -- entity in the scope.
6502
6503 Prev := First_Entity (Current_Scope);
6504 while Present (Prev) and then Next_Entity (Prev) /= E loop
6505 Next_Entity (Prev);
6506 end loop;
6507
6508 if No (Prev) then
6509
6510 -- If E is not on the entity chain of the current scope,
6511 -- it is an implicit declaration in the generic formal
6512 -- part of a generic subprogram. When analyzing the body,
6513 -- the generic formals are visible but not on the entity
6514 -- chain of the subprogram. The new entity will become
6515 -- the visible one in the body.
6516
6517 pragma Assert
6518 (Nkind (Parent (Decl)) = N_Generic_Subprogram_Declaration);
6519 null;
6520
6521 else
6522 Set_Next_Entity (Prev, Next_Entity (E));
6523
6524 if No (Next_Entity (Prev)) then
6525 Set_Last_Entity (Current_Scope, Prev);
6526 end if;
6527
6528 if E = Current_Entity (E) then
6529 Prev_Vis := Empty;
6530
6531 else
6532 Prev_Vis := Current_Entity (E);
6533 while Homonym (Prev_Vis) /= E loop
6534 Prev_Vis := Homonym (Prev_Vis);
6535 end loop;
6536 end if;
6537
6538 if Present (Prev_Vis) then
6539
6540 -- Skip E in the visibility chain
6541
6542 Set_Homonym (Prev_Vis, Homonym (E));
6543
6544 else
6545 Set_Name_Entity_Id (Chars (E), Homonym (E));
6546 end if;
6547 end if;
6548 end;
6549
6550 -- This section of code could use a comment ???
6551
6552 elsif Present (Etype (E))
6553 and then Is_Concurrent_Type (Etype (E))
6554 and then E = Def_Id
6555 then
6556 return;
6557
6558 -- If the homograph is a protected component renaming, it should not
6559 -- be hiding the current entity. Such renamings are treated as weak
6560 -- declarations.
6561
6562 elsif Is_Prival (E) then
6563 Set_Is_Immediately_Visible (E, False);
6564
6565 -- In this case the current entity is a protected component renaming.
6566 -- Perform minimal decoration by setting the scope and return since
6567 -- the prival should not be hiding other visible entities.
6568
6569 elsif Is_Prival (Def_Id) then
6570 Set_Scope (Def_Id, Current_Scope);
6571 return;
6572
6573 -- Analogous to privals, the discriminal generated for an entry index
6574 -- parameter acts as a weak declaration. Perform minimal decoration
6575 -- to avoid bogus errors.
6576
6577 elsif Is_Discriminal (Def_Id)
6578 and then Ekind (Discriminal_Link (Def_Id)) = E_Entry_Index_Parameter
6579 then
6580 Set_Scope (Def_Id, Current_Scope);
6581 return;
6582
6583 -- In the body or private part of an instance, a type extension may
6584 -- introduce a component with the same name as that of an actual. The
6585 -- legality rule is not enforced, but the semantics of the full type
6586 -- with two components of same name are not clear at this point???
6587
6588 elsif In_Instance_Not_Visible then
6589 null;
6590
6591 -- When compiling a package body, some child units may have become
6592 -- visible. They cannot conflict with local entities that hide them.
6593
6594 elsif Is_Child_Unit (E)
6595 and then In_Open_Scopes (Scope (E))
6596 and then not Is_Immediately_Visible (E)
6597 then
6598 null;
6599
6600 -- Conversely, with front-end inlining we may compile the parent body
6601 -- first, and a child unit subsequently. The context is now the
6602 -- parent spec, and body entities are not visible.
6603
6604 elsif Is_Child_Unit (Def_Id)
6605 and then Is_Package_Body_Entity (E)
6606 and then not In_Package_Body (Current_Scope)
6607 then
6608 null;
6609
6610 -- Case of genuine duplicate declaration
6611
6612 else
6613 Error_Msg_Sloc := Sloc (E);
6614
6615 -- If the previous declaration is an incomplete type declaration
6616 -- this may be an attempt to complete it with a private type. The
6617 -- following avoids confusing cascaded errors.
6618
6619 if Nkind (Parent (E)) = N_Incomplete_Type_Declaration
6620 and then Nkind (Parent (Def_Id)) = N_Private_Type_Declaration
6621 then
6622 Error_Msg_N
6623 ("incomplete type cannot be completed with a private " &
6624 "declaration", Parent (Def_Id));
6625 Set_Is_Immediately_Visible (E, False);
6626 Set_Full_View (E, Def_Id);
6627
6628 -- An inherited component of a record conflicts with a new
6629 -- discriminant. The discriminant is inserted first in the scope,
6630 -- but the error should be posted on it, not on the component.
6631
6632 elsif Ekind (E) = E_Discriminant
6633 and then Present (Scope (Def_Id))
6634 and then Scope (Def_Id) /= Current_Scope
6635 then
6636 Error_Msg_Sloc := Sloc (Def_Id);
6637 Error_Msg_N ("& conflicts with declaration#", E);
6638 return;
6639
6640 -- If the name of the unit appears in its own context clause, a
6641 -- dummy package with the name has already been created, and the
6642 -- error emitted. Try to continue quietly.
6643
6644 elsif Error_Posted (E)
6645 and then Sloc (E) = No_Location
6646 and then Nkind (Parent (E)) = N_Package_Specification
6647 and then Current_Scope = Standard_Standard
6648 then
6649 Set_Scope (Def_Id, Current_Scope);
6650 return;
6651
6652 else
6653 Error_Msg_N ("& conflicts with declaration#", Def_Id);
6654
6655 -- Avoid cascaded messages with duplicate components in
6656 -- derived types.
6657
6658 if Ekind_In (E, E_Component, E_Discriminant) then
6659 return;
6660 end if;
6661 end if;
6662
6663 if Nkind (Parent (Parent (Def_Id))) =
6664 N_Generic_Subprogram_Declaration
6665 and then Def_Id =
6666 Defining_Entity (Specification (Parent (Parent (Def_Id))))
6667 then
6668 Error_Msg_N ("\generic units cannot be overloaded", Def_Id);
6669 end if;
6670
6671 -- If entity is in standard, then we are in trouble, because it
6672 -- means that we have a library package with a duplicated name.
6673 -- That's hard to recover from, so abort.
6674
6675 if S = Standard_Standard then
6676 raise Unrecoverable_Error;
6677
6678 -- Otherwise we continue with the declaration. Having two
6679 -- identical declarations should not cause us too much trouble.
6680
6681 else
6682 null;
6683 end if;
6684 end if;
6685 end if;
6686
6687 -- If we fall through, declaration is OK, at least OK enough to continue
6688
6689 -- If Def_Id is a discriminant or a record component we are in the midst
6690 -- of inheriting components in a derived record definition. Preserve
6691 -- their Ekind and Etype.
6692
6693 if Ekind_In (Def_Id, E_Discriminant, E_Component) then
6694 null;
6695
6696 -- If a type is already set, leave it alone (happens when a type
6697 -- declaration is reanalyzed following a call to the optimizer).
6698
6699 elsif Present (Etype (Def_Id)) then
6700 null;
6701
6702 -- Otherwise, the kind E_Void insures that premature uses of the entity
6703 -- will be detected. Any_Type insures that no cascaded errors will occur
6704
6705 else
6706 Set_Ekind (Def_Id, E_Void);
6707 Set_Etype (Def_Id, Any_Type);
6708 end if;
6709
6710 -- Inherited discriminants and components in derived record types are
6711 -- immediately visible. Itypes are not.
6712
6713 -- Unless the Itype is for a record type with a corresponding remote
6714 -- type (what is that about, it was not commented ???)
6715
6716 if Ekind_In (Def_Id, E_Discriminant, E_Component)
6717 or else
6718 ((not Is_Record_Type (Def_Id)
6719 or else No (Corresponding_Remote_Type (Def_Id)))
6720 and then not Is_Itype (Def_Id))
6721 then
6722 Set_Is_Immediately_Visible (Def_Id);
6723 Set_Current_Entity (Def_Id);
6724 end if;
6725
6726 Set_Homonym (Def_Id, C);
6727 Append_Entity (Def_Id, S);
6728 Set_Public_Status (Def_Id);
6729
6730 -- Declaring a homonym is not allowed in SPARK ...
6731
6732 if Present (C) and then Restriction_Check_Required (SPARK_05) then
6733 declare
6734 Enclosing_Subp : constant Node_Id := Enclosing_Subprogram (Def_Id);
6735 Enclosing_Pack : constant Node_Id := Enclosing_Package (Def_Id);
6736 Other_Scope : constant Node_Id := Enclosing_Dynamic_Scope (C);
6737
6738 begin
6739 -- ... unless the new declaration is in a subprogram, and the
6740 -- visible declaration is a variable declaration or a parameter
6741 -- specification outside that subprogram.
6742
6743 if Present (Enclosing_Subp)
6744 and then Nkind_In (Parent (C), N_Object_Declaration,
6745 N_Parameter_Specification)
6746 and then not Scope_Within_Or_Same (Other_Scope, Enclosing_Subp)
6747 then
6748 null;
6749
6750 -- ... or the new declaration is in a package, and the visible
6751 -- declaration occurs outside that package.
6752
6753 elsif Present (Enclosing_Pack)
6754 and then not Scope_Within_Or_Same (Other_Scope, Enclosing_Pack)
6755 then
6756 null;
6757
6758 -- ... or the new declaration is a component declaration in a
6759 -- record type definition.
6760
6761 elsif Nkind (Parent (Def_Id)) = N_Component_Declaration then
6762 null;
6763
6764 -- Don't issue error for non-source entities
6765
6766 elsif Comes_From_Source (Def_Id)
6767 and then Comes_From_Source (C)
6768 then
6769 Error_Msg_Sloc := Sloc (C);
6770 Check_SPARK_05_Restriction
6771 ("redeclaration of identifier &#", Def_Id);
6772 end if;
6773 end;
6774 end if;
6775
6776 -- Warn if new entity hides an old one
6777
6778 if Warn_On_Hiding and then Present (C)
6779
6780 -- Don't warn for record components since they always have a well
6781 -- defined scope which does not confuse other uses. Note that in
6782 -- some cases, Ekind has not been set yet.
6783
6784 and then Ekind (C) /= E_Component
6785 and then Ekind (C) /= E_Discriminant
6786 and then Nkind (Parent (C)) /= N_Component_Declaration
6787 and then Ekind (Def_Id) /= E_Component
6788 and then Ekind (Def_Id) /= E_Discriminant
6789 and then Nkind (Parent (Def_Id)) /= N_Component_Declaration
6790
6791 -- Don't warn for one character variables. It is too common to use
6792 -- such variables as locals and will just cause too many false hits.
6793
6794 and then Length_Of_Name (Chars (C)) /= 1
6795
6796 -- Don't warn for non-source entities
6797
6798 and then Comes_From_Source (C)
6799 and then Comes_From_Source (Def_Id)
6800
6801 -- Don't warn unless entity in question is in extended main source
6802
6803 and then In_Extended_Main_Source_Unit (Def_Id)
6804
6805 -- Finally, the hidden entity must be either immediately visible or
6806 -- use visible (i.e. from a used package).
6807
6808 and then
6809 (Is_Immediately_Visible (C)
6810 or else
6811 Is_Potentially_Use_Visible (C))
6812 then
6813 Error_Msg_Sloc := Sloc (C);
6814 Error_Msg_N ("declaration hides &#?h?", Def_Id);
6815 end if;
6816 end Enter_Name;
6817
6818 ---------------
6819 -- Entity_Of --
6820 ---------------
6821
6822 function Entity_Of (N : Node_Id) return Entity_Id is
6823 Id : Entity_Id;
6824
6825 begin
6826 Id := Empty;
6827
6828 if Is_Entity_Name (N) then
6829 Id := Entity (N);
6830
6831 -- Follow a possible chain of renamings to reach the root renamed
6832 -- object.
6833
6834 while Present (Id)
6835 and then Is_Object (Id)
6836 and then Present (Renamed_Object (Id))
6837 loop
6838 if Is_Entity_Name (Renamed_Object (Id)) then
6839 Id := Entity (Renamed_Object (Id));
6840 else
6841 Id := Empty;
6842 exit;
6843 end if;
6844 end loop;
6845 end if;
6846
6847 return Id;
6848 end Entity_Of;
6849
6850 --------------------------
6851 -- Explain_Limited_Type --
6852 --------------------------
6853
6854 procedure Explain_Limited_Type (T : Entity_Id; N : Node_Id) is
6855 C : Entity_Id;
6856
6857 begin
6858 -- For array, component type must be limited
6859
6860 if Is_Array_Type (T) then
6861 Error_Msg_Node_2 := T;
6862 Error_Msg_NE
6863 ("\component type& of type& is limited", N, Component_Type (T));
6864 Explain_Limited_Type (Component_Type (T), N);
6865
6866 elsif Is_Record_Type (T) then
6867
6868 -- No need for extra messages if explicit limited record
6869
6870 if Is_Limited_Record (Base_Type (T)) then
6871 return;
6872 end if;
6873
6874 -- Otherwise find a limited component. Check only components that
6875 -- come from source, or inherited components that appear in the
6876 -- source of the ancestor.
6877
6878 C := First_Component (T);
6879 while Present (C) loop
6880 if Is_Limited_Type (Etype (C))
6881 and then
6882 (Comes_From_Source (C)
6883 or else
6884 (Present (Original_Record_Component (C))
6885 and then
6886 Comes_From_Source (Original_Record_Component (C))))
6887 then
6888 Error_Msg_Node_2 := T;
6889 Error_Msg_NE ("\component& of type& has limited type", N, C);
6890 Explain_Limited_Type (Etype (C), N);
6891 return;
6892 end if;
6893
6894 Next_Component (C);
6895 end loop;
6896
6897 -- The type may be declared explicitly limited, even if no component
6898 -- of it is limited, in which case we fall out of the loop.
6899 return;
6900 end if;
6901 end Explain_Limited_Type;
6902
6903 -------------------------------
6904 -- Extensions_Visible_Status --
6905 -------------------------------
6906
6907 function Extensions_Visible_Status
6908 (Id : Entity_Id) return Extensions_Visible_Mode
6909 is
6910 Arg : Node_Id;
6911 Decl : Node_Id;
6912 Expr : Node_Id;
6913 Prag : Node_Id;
6914 Subp : Entity_Id;
6915
6916 begin
6917 -- When a formal parameter is subject to Extensions_Visible, the pragma
6918 -- is stored in the contract of related subprogram.
6919
6920 if Is_Formal (Id) then
6921 Subp := Scope (Id);
6922
6923 elsif Is_Subprogram_Or_Generic_Subprogram (Id) then
6924 Subp := Id;
6925
6926 -- No other construct carries this pragma
6927
6928 else
6929 return Extensions_Visible_None;
6930 end if;
6931
6932 Prag := Get_Pragma (Subp, Pragma_Extensions_Visible);
6933
6934 -- In certain cases analysis may request the Extensions_Visible status
6935 -- of an expression function before the pragma has been analyzed yet.
6936 -- Inspect the declarative items after the expression function looking
6937 -- for the pragma (if any).
6938
6939 if No (Prag) and then Is_Expression_Function (Subp) then
6940 Decl := Next (Unit_Declaration_Node (Subp));
6941 while Present (Decl) loop
6942 if Nkind (Decl) = N_Pragma
6943 and then Pragma_Name (Decl) = Name_Extensions_Visible
6944 then
6945 Prag := Decl;
6946 exit;
6947
6948 -- A source construct ends the region where Extensions_Visible may
6949 -- appear, stop the traversal. An expanded expression function is
6950 -- no longer a source construct, but it must still be recognized.
6951
6952 elsif Comes_From_Source (Decl)
6953 or else
6954 (Nkind_In (Decl, N_Subprogram_Body,
6955 N_Subprogram_Declaration)
6956 and then Is_Expression_Function (Defining_Entity (Decl)))
6957 then
6958 exit;
6959 end if;
6960
6961 Next (Decl);
6962 end loop;
6963 end if;
6964
6965 -- Extract the value from the Boolean expression (if any)
6966
6967 if Present (Prag) then
6968 Arg := First (Pragma_Argument_Associations (Prag));
6969
6970 if Present (Arg) then
6971 Expr := Get_Pragma_Arg (Arg);
6972
6973 -- When the associated subprogram is an expression function, the
6974 -- argument of the pragma may not have been analyzed.
6975
6976 if not Analyzed (Expr) then
6977 Preanalyze_And_Resolve (Expr, Standard_Boolean);
6978 end if;
6979
6980 -- Guard against cascading errors when the argument of pragma
6981 -- Extensions_Visible is not a valid static Boolean expression.
6982
6983 if Error_Posted (Expr) then
6984 return Extensions_Visible_None;
6985
6986 elsif Is_True (Expr_Value (Expr)) then
6987 return Extensions_Visible_True;
6988
6989 else
6990 return Extensions_Visible_False;
6991 end if;
6992
6993 -- Otherwise the aspect or pragma defaults to True
6994
6995 else
6996 return Extensions_Visible_True;
6997 end if;
6998
6999 -- Otherwise aspect or pragma Extensions_Visible is not inherited or
7000 -- directly specified. In SPARK code, its value defaults to "False".
7001
7002 elsif SPARK_Mode = On then
7003 return Extensions_Visible_False;
7004
7005 -- In non-SPARK code, aspect or pragma Extensions_Visible defaults to
7006 -- "True".
7007
7008 else
7009 return Extensions_Visible_True;
7010 end if;
7011 end Extensions_Visible_Status;
7012
7013 -----------------
7014 -- Find_Actual --
7015 -----------------
7016
7017 procedure Find_Actual
7018 (N : Node_Id;
7019 Formal : out Entity_Id;
7020 Call : out Node_Id)
7021 is
7022 Context : constant Node_Id := Parent (N);
7023 Actual : Node_Id;
7024 Call_Nam : Node_Id;
7025
7026 begin
7027 if Nkind_In (Context, N_Indexed_Component, N_Selected_Component)
7028 and then N = Prefix (Context)
7029 then
7030 Find_Actual (Context, Formal, Call);
7031 return;
7032
7033 elsif Nkind (Context) = N_Parameter_Association
7034 and then N = Explicit_Actual_Parameter (Context)
7035 then
7036 Call := Parent (Context);
7037
7038 elsif Nkind_In (Context, N_Entry_Call_Statement,
7039 N_Function_Call,
7040 N_Procedure_Call_Statement)
7041 then
7042 Call := Context;
7043
7044 else
7045 Formal := Empty;
7046 Call := Empty;
7047 return;
7048 end if;
7049
7050 -- If we have a call to a subprogram look for the parameter. Note that
7051 -- we exclude overloaded calls, since we don't know enough to be sure
7052 -- of giving the right answer in this case.
7053
7054 if Nkind_In (Call, N_Entry_Call_Statement,
7055 N_Function_Call,
7056 N_Procedure_Call_Statement)
7057 then
7058 Call_Nam := Name (Call);
7059
7060 -- A call to a protected or task entry appears as a selected
7061 -- component rather than an expanded name.
7062
7063 if Nkind (Call_Nam) = N_Selected_Component then
7064 Call_Nam := Selector_Name (Call_Nam);
7065 end if;
7066
7067 if Is_Entity_Name (Call_Nam)
7068 and then Present (Entity (Call_Nam))
7069 and then Is_Overloadable (Entity (Call_Nam))
7070 and then not Is_Overloaded (Call_Nam)
7071 then
7072 -- If node is name in call it is not an actual
7073
7074 if N = Call_Nam then
7075 Formal := Empty;
7076 Call := Empty;
7077 return;
7078 end if;
7079
7080 -- Fall here if we are definitely a parameter
7081
7082 Actual := First_Actual (Call);
7083 Formal := First_Formal (Entity (Call_Nam));
7084 while Present (Formal) and then Present (Actual) loop
7085 if Actual = N then
7086 return;
7087
7088 -- An actual that is the prefix in a prefixed call may have
7089 -- been rewritten in the call, after the deferred reference
7090 -- was collected. Check if sloc and kinds and names match.
7091
7092 elsif Sloc (Actual) = Sloc (N)
7093 and then Nkind (Actual) = N_Identifier
7094 and then Nkind (Actual) = Nkind (N)
7095 and then Chars (Actual) = Chars (N)
7096 then
7097 return;
7098
7099 else
7100 Actual := Next_Actual (Actual);
7101 Formal := Next_Formal (Formal);
7102 end if;
7103 end loop;
7104 end if;
7105 end if;
7106
7107 -- Fall through here if we did not find matching actual
7108
7109 Formal := Empty;
7110 Call := Empty;
7111 end Find_Actual;
7112
7113 ---------------------------
7114 -- Find_Body_Discriminal --
7115 ---------------------------
7116
7117 function Find_Body_Discriminal
7118 (Spec_Discriminant : Entity_Id) return Entity_Id
7119 is
7120 Tsk : Entity_Id;
7121 Disc : Entity_Id;
7122
7123 begin
7124 -- If expansion is suppressed, then the scope can be the concurrent type
7125 -- itself rather than a corresponding concurrent record type.
7126
7127 if Is_Concurrent_Type (Scope (Spec_Discriminant)) then
7128 Tsk := Scope (Spec_Discriminant);
7129
7130 else
7131 pragma Assert (Is_Concurrent_Record_Type (Scope (Spec_Discriminant)));
7132
7133 Tsk := Corresponding_Concurrent_Type (Scope (Spec_Discriminant));
7134 end if;
7135
7136 -- Find discriminant of original concurrent type, and use its current
7137 -- discriminal, which is the renaming within the task/protected body.
7138
7139 Disc := First_Discriminant (Tsk);
7140 while Present (Disc) loop
7141 if Chars (Disc) = Chars (Spec_Discriminant) then
7142 return Discriminal (Disc);
7143 end if;
7144
7145 Next_Discriminant (Disc);
7146 end loop;
7147
7148 -- That loop should always succeed in finding a matching entry and
7149 -- returning. Fatal error if not.
7150
7151 raise Program_Error;
7152 end Find_Body_Discriminal;
7153
7154 -------------------------------------
7155 -- Find_Corresponding_Discriminant --
7156 -------------------------------------
7157
7158 function Find_Corresponding_Discriminant
7159 (Id : Node_Id;
7160 Typ : Entity_Id) return Entity_Id
7161 is
7162 Par_Disc : Entity_Id;
7163 Old_Disc : Entity_Id;
7164 New_Disc : Entity_Id;
7165
7166 begin
7167 Par_Disc := Original_Record_Component (Original_Discriminant (Id));
7168
7169 -- The original type may currently be private, and the discriminant
7170 -- only appear on its full view.
7171
7172 if Is_Private_Type (Scope (Par_Disc))
7173 and then not Has_Discriminants (Scope (Par_Disc))
7174 and then Present (Full_View (Scope (Par_Disc)))
7175 then
7176 Old_Disc := First_Discriminant (Full_View (Scope (Par_Disc)));
7177 else
7178 Old_Disc := First_Discriminant (Scope (Par_Disc));
7179 end if;
7180
7181 if Is_Class_Wide_Type (Typ) then
7182 New_Disc := First_Discriminant (Root_Type (Typ));
7183 else
7184 New_Disc := First_Discriminant (Typ);
7185 end if;
7186
7187 while Present (Old_Disc) and then Present (New_Disc) loop
7188 if Old_Disc = Par_Disc then
7189 return New_Disc;
7190 end if;
7191
7192 Next_Discriminant (Old_Disc);
7193 Next_Discriminant (New_Disc);
7194 end loop;
7195
7196 -- Should always find it
7197
7198 raise Program_Error;
7199 end Find_Corresponding_Discriminant;
7200
7201 ----------------------------------
7202 -- Find_Enclosing_Iterator_Loop --
7203 ----------------------------------
7204
7205 function Find_Enclosing_Iterator_Loop (Id : Entity_Id) return Entity_Id is
7206 Constr : Node_Id;
7207 S : Entity_Id;
7208
7209 begin
7210 -- Traverse the scope chain looking for an iterator loop. Such loops are
7211 -- usually transformed into blocks, hence the use of Original_Node.
7212
7213 S := Id;
7214 while Present (S) and then S /= Standard_Standard loop
7215 if Ekind (S) = E_Loop
7216 and then Nkind (Parent (S)) = N_Implicit_Label_Declaration
7217 then
7218 Constr := Original_Node (Label_Construct (Parent (S)));
7219
7220 if Nkind (Constr) = N_Loop_Statement
7221 and then Present (Iteration_Scheme (Constr))
7222 and then Nkind (Iterator_Specification
7223 (Iteration_Scheme (Constr))) =
7224 N_Iterator_Specification
7225 then
7226 return S;
7227 end if;
7228 end if;
7229
7230 S := Scope (S);
7231 end loop;
7232
7233 return Empty;
7234 end Find_Enclosing_Iterator_Loop;
7235
7236 ------------------------------------
7237 -- Find_Loop_In_Conditional_Block --
7238 ------------------------------------
7239
7240 function Find_Loop_In_Conditional_Block (N : Node_Id) return Node_Id is
7241 Stmt : Node_Id;
7242
7243 begin
7244 Stmt := N;
7245
7246 if Nkind (Stmt) = N_If_Statement then
7247 Stmt := First (Then_Statements (Stmt));
7248 end if;
7249
7250 pragma Assert (Nkind (Stmt) = N_Block_Statement);
7251
7252 -- Inspect the statements of the conditional block. In general the loop
7253 -- should be the first statement in the statement sequence of the block,
7254 -- but the finalization machinery may have introduced extra object
7255 -- declarations.
7256
7257 Stmt := First (Statements (Handled_Statement_Sequence (Stmt)));
7258 while Present (Stmt) loop
7259 if Nkind (Stmt) = N_Loop_Statement then
7260 return Stmt;
7261 end if;
7262
7263 Next (Stmt);
7264 end loop;
7265
7266 -- The expansion of attribute 'Loop_Entry produced a malformed block
7267
7268 raise Program_Error;
7269 end Find_Loop_In_Conditional_Block;
7270
7271 --------------------------
7272 -- Find_Overlaid_Entity --
7273 --------------------------
7274
7275 procedure Find_Overlaid_Entity
7276 (N : Node_Id;
7277 Ent : out Entity_Id;
7278 Off : out Boolean)
7279 is
7280 Expr : Node_Id;
7281
7282 begin
7283 -- We are looking for one of the two following forms:
7284
7285 -- for X'Address use Y'Address
7286
7287 -- or
7288
7289 -- Const : constant Address := expr;
7290 -- ...
7291 -- for X'Address use Const;
7292
7293 -- In the second case, the expr is either Y'Address, or recursively a
7294 -- constant that eventually references Y'Address.
7295
7296 Ent := Empty;
7297 Off := False;
7298
7299 if Nkind (N) = N_Attribute_Definition_Clause
7300 and then Chars (N) = Name_Address
7301 then
7302 Expr := Expression (N);
7303
7304 -- This loop checks the form of the expression for Y'Address,
7305 -- using recursion to deal with intermediate constants.
7306
7307 loop
7308 -- Check for Y'Address
7309
7310 if Nkind (Expr) = N_Attribute_Reference
7311 and then Attribute_Name (Expr) = Name_Address
7312 then
7313 Expr := Prefix (Expr);
7314 exit;
7315
7316 -- Check for Const where Const is a constant entity
7317
7318 elsif Is_Entity_Name (Expr)
7319 and then Ekind (Entity (Expr)) = E_Constant
7320 then
7321 Expr := Constant_Value (Entity (Expr));
7322
7323 -- Anything else does not need checking
7324
7325 else
7326 return;
7327 end if;
7328 end loop;
7329
7330 -- This loop checks the form of the prefix for an entity, using
7331 -- recursion to deal with intermediate components.
7332
7333 loop
7334 -- Check for Y where Y is an entity
7335
7336 if Is_Entity_Name (Expr) then
7337 Ent := Entity (Expr);
7338 return;
7339
7340 -- Check for components
7341
7342 elsif
7343 Nkind_In (Expr, N_Selected_Component, N_Indexed_Component)
7344 then
7345 Expr := Prefix (Expr);
7346 Off := True;
7347
7348 -- Anything else does not need checking
7349
7350 else
7351 return;
7352 end if;
7353 end loop;
7354 end if;
7355 end Find_Overlaid_Entity;
7356
7357 -------------------------
7358 -- Find_Parameter_Type --
7359 -------------------------
7360
7361 function Find_Parameter_Type (Param : Node_Id) return Entity_Id is
7362 begin
7363 if Nkind (Param) /= N_Parameter_Specification then
7364 return Empty;
7365
7366 -- For an access parameter, obtain the type from the formal entity
7367 -- itself, because access to subprogram nodes do not carry a type.
7368 -- Shouldn't we always use the formal entity ???
7369
7370 elsif Nkind (Parameter_Type (Param)) = N_Access_Definition then
7371 return Etype (Defining_Identifier (Param));
7372
7373 else
7374 return Etype (Parameter_Type (Param));
7375 end if;
7376 end Find_Parameter_Type;
7377
7378 -----------------------------------
7379 -- Find_Placement_In_State_Space --
7380 -----------------------------------
7381
7382 procedure Find_Placement_In_State_Space
7383 (Item_Id : Entity_Id;
7384 Placement : out State_Space_Kind;
7385 Pack_Id : out Entity_Id)
7386 is
7387 Context : Entity_Id;
7388
7389 begin
7390 -- Assume that the item does not appear in the state space of a package
7391
7392 Placement := Not_In_Package;
7393 Pack_Id := Empty;
7394
7395 -- Climb the scope stack and examine the enclosing context
7396
7397 Context := Scope (Item_Id);
7398 while Present (Context) and then Context /= Standard_Standard loop
7399 if Ekind (Context) = E_Package then
7400 Pack_Id := Context;
7401
7402 -- A package body is a cut off point for the traversal as the item
7403 -- cannot be visible to the outside from this point on. Note that
7404 -- this test must be done first as a body is also classified as a
7405 -- private part.
7406
7407 if In_Package_Body (Context) then
7408 Placement := Body_State_Space;
7409 return;
7410
7411 -- The private part of a package is a cut off point for the
7412 -- traversal as the item cannot be visible to the outside from
7413 -- this point on.
7414
7415 elsif In_Private_Part (Context) then
7416 Placement := Private_State_Space;
7417 return;
7418
7419 -- When the item appears in the visible state space of a package,
7420 -- continue to climb the scope stack as this may not be the final
7421 -- state space.
7422
7423 else
7424 Placement := Visible_State_Space;
7425
7426 -- The visible state space of a child unit acts as the proper
7427 -- placement of an item.
7428
7429 if Is_Child_Unit (Context) then
7430 return;
7431 end if;
7432 end if;
7433
7434 -- The item or its enclosing package appear in a construct that has
7435 -- no state space.
7436
7437 else
7438 Placement := Not_In_Package;
7439 return;
7440 end if;
7441
7442 Context := Scope (Context);
7443 end loop;
7444 end Find_Placement_In_State_Space;
7445
7446 ------------------------
7447 -- Find_Specific_Type --
7448 ------------------------
7449
7450 function Find_Specific_Type (CW : Entity_Id) return Entity_Id is
7451 Typ : Entity_Id := Root_Type (CW);
7452
7453 begin
7454 if Ekind (Typ) = E_Incomplete_Type then
7455 if From_Limited_With (Typ) then
7456 Typ := Non_Limited_View (Typ);
7457 else
7458 Typ := Full_View (Typ);
7459 end if;
7460 end if;
7461
7462 if Is_Private_Type (Typ)
7463 and then not Is_Tagged_Type (Typ)
7464 and then Present (Full_View (Typ))
7465 then
7466 return Full_View (Typ);
7467 else
7468 return Typ;
7469 end if;
7470 end Find_Specific_Type;
7471
7472 -----------------------------
7473 -- Find_Static_Alternative --
7474 -----------------------------
7475
7476 function Find_Static_Alternative (N : Node_Id) return Node_Id is
7477 Expr : constant Node_Id := Expression (N);
7478 Val : constant Uint := Expr_Value (Expr);
7479 Alt : Node_Id;
7480 Choice : Node_Id;
7481
7482 begin
7483 Alt := First (Alternatives (N));
7484
7485 Search : loop
7486 if Nkind (Alt) /= N_Pragma then
7487 Choice := First (Discrete_Choices (Alt));
7488 while Present (Choice) loop
7489
7490 -- Others choice, always matches
7491
7492 if Nkind (Choice) = N_Others_Choice then
7493 exit Search;
7494
7495 -- Range, check if value is in the range
7496
7497 elsif Nkind (Choice) = N_Range then
7498 exit Search when
7499 Val >= Expr_Value (Low_Bound (Choice))
7500 and then
7501 Val <= Expr_Value (High_Bound (Choice));
7502
7503 -- Choice is a subtype name. Note that we know it must
7504 -- be a static subtype, since otherwise it would have
7505 -- been diagnosed as illegal.
7506
7507 elsif Is_Entity_Name (Choice)
7508 and then Is_Type (Entity (Choice))
7509 then
7510 exit Search when Is_In_Range (Expr, Etype (Choice),
7511 Assume_Valid => False);
7512
7513 -- Choice is a subtype indication
7514
7515 elsif Nkind (Choice) = N_Subtype_Indication then
7516 declare
7517 C : constant Node_Id := Constraint (Choice);
7518 R : constant Node_Id := Range_Expression (C);
7519
7520 begin
7521 exit Search when
7522 Val >= Expr_Value (Low_Bound (R))
7523 and then
7524 Val <= Expr_Value (High_Bound (R));
7525 end;
7526
7527 -- Choice is a simple expression
7528
7529 else
7530 exit Search when Val = Expr_Value (Choice);
7531 end if;
7532
7533 Next (Choice);
7534 end loop;
7535 end if;
7536
7537 Next (Alt);
7538 pragma Assert (Present (Alt));
7539 end loop Search;
7540
7541 -- The above loop *must* terminate by finding a match, since
7542 -- we know the case statement is valid, and the value of the
7543 -- expression is known at compile time. When we fall out of
7544 -- the loop, Alt points to the alternative that we know will
7545 -- be selected at run time.
7546
7547 return Alt;
7548 end Find_Static_Alternative;
7549
7550 ------------------
7551 -- First_Actual --
7552 ------------------
7553
7554 function First_Actual (Node : Node_Id) return Node_Id is
7555 N : Node_Id;
7556
7557 begin
7558 if No (Parameter_Associations (Node)) then
7559 return Empty;
7560 end if;
7561
7562 N := First (Parameter_Associations (Node));
7563
7564 if Nkind (N) = N_Parameter_Association then
7565 return First_Named_Actual (Node);
7566 else
7567 return N;
7568 end if;
7569 end First_Actual;
7570
7571 -------------
7572 -- Fix_Msg --
7573 -------------
7574
7575 function Fix_Msg (Id : Entity_Id; Msg : String) return String is
7576 Is_Task : constant Boolean :=
7577 Ekind_In (Id, E_Task_Body, E_Task_Type)
7578 or else Is_Single_Task_Object (Id);
7579 Msg_Last : constant Natural := Msg'Last;
7580 Msg_Index : Natural;
7581 Res : String (Msg'Range) := (others => ' ');
7582 Res_Index : Natural;
7583
7584 begin
7585 -- Copy all characters from the input message Msg to result Res with
7586 -- suitable replacements.
7587
7588 Msg_Index := Msg'First;
7589 Res_Index := Res'First;
7590 while Msg_Index <= Msg_Last loop
7591
7592 -- Replace "subprogram" with a different word
7593
7594 if Msg_Index <= Msg_Last - 10
7595 and then Msg (Msg_Index .. Msg_Index + 9) = "subprogram"
7596 then
7597 if Ekind_In (Id, E_Entry, E_Entry_Family) then
7598 Res (Res_Index .. Res_Index + 4) := "entry";
7599 Res_Index := Res_Index + 5;
7600
7601 elsif Is_Task then
7602 Res (Res_Index .. Res_Index + 8) := "task type";
7603 Res_Index := Res_Index + 9;
7604
7605 else
7606 Res (Res_Index .. Res_Index + 9) := "subprogram";
7607 Res_Index := Res_Index + 10;
7608 end if;
7609
7610 Msg_Index := Msg_Index + 10;
7611
7612 -- Replace "protected" with a different word
7613
7614 elsif Msg_Index <= Msg_Last - 9
7615 and then Msg (Msg_Index .. Msg_Index + 8) = "protected"
7616 and then Is_Task
7617 then
7618 Res (Res_Index .. Res_Index + 3) := "task";
7619 Res_Index := Res_Index + 4;
7620 Msg_Index := Msg_Index + 9;
7621
7622 -- Otherwise copy the character
7623
7624 else
7625 Res (Res_Index) := Msg (Msg_Index);
7626 Msg_Index := Msg_Index + 1;
7627 Res_Index := Res_Index + 1;
7628 end if;
7629 end loop;
7630
7631 return Res (Res'First .. Res_Index - 1);
7632 end Fix_Msg;
7633
7634 -----------------------
7635 -- Gather_Components --
7636 -----------------------
7637
7638 procedure Gather_Components
7639 (Typ : Entity_Id;
7640 Comp_List : Node_Id;
7641 Governed_By : List_Id;
7642 Into : Elist_Id;
7643 Report_Errors : out Boolean)
7644 is
7645 Assoc : Node_Id;
7646 Variant : Node_Id;
7647 Discrete_Choice : Node_Id;
7648 Comp_Item : Node_Id;
7649
7650 Discrim : Entity_Id;
7651 Discrim_Name : Node_Id;
7652 Discrim_Value : Node_Id;
7653
7654 begin
7655 Report_Errors := False;
7656
7657 if No (Comp_List) or else Null_Present (Comp_List) then
7658 return;
7659
7660 elsif Present (Component_Items (Comp_List)) then
7661 Comp_Item := First (Component_Items (Comp_List));
7662
7663 else
7664 Comp_Item := Empty;
7665 end if;
7666
7667 while Present (Comp_Item) loop
7668
7669 -- Skip the tag of a tagged record, the interface tags, as well
7670 -- as all items that are not user components (anonymous types,
7671 -- rep clauses, Parent field, controller field).
7672
7673 if Nkind (Comp_Item) = N_Component_Declaration then
7674 declare
7675 Comp : constant Entity_Id := Defining_Identifier (Comp_Item);
7676 begin
7677 if not Is_Tag (Comp) and then Chars (Comp) /= Name_uParent then
7678 Append_Elmt (Comp, Into);
7679 end if;
7680 end;
7681 end if;
7682
7683 Next (Comp_Item);
7684 end loop;
7685
7686 if No (Variant_Part (Comp_List)) then
7687 return;
7688 else
7689 Discrim_Name := Name (Variant_Part (Comp_List));
7690 Variant := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
7691 end if;
7692
7693 -- Look for the discriminant that governs this variant part.
7694 -- The discriminant *must* be in the Governed_By List
7695
7696 Assoc := First (Governed_By);
7697 Find_Constraint : loop
7698 Discrim := First (Choices (Assoc));
7699 exit Find_Constraint when Chars (Discrim_Name) = Chars (Discrim)
7700 or else (Present (Corresponding_Discriminant (Entity (Discrim)))
7701 and then
7702 Chars (Corresponding_Discriminant (Entity (Discrim))) =
7703 Chars (Discrim_Name))
7704 or else Chars (Original_Record_Component (Entity (Discrim)))
7705 = Chars (Discrim_Name);
7706
7707 if No (Next (Assoc)) then
7708 if not Is_Constrained (Typ)
7709 and then Is_Derived_Type (Typ)
7710 and then Present (Stored_Constraint (Typ))
7711 then
7712 -- If the type is a tagged type with inherited discriminants,
7713 -- use the stored constraint on the parent in order to find
7714 -- the values of discriminants that are otherwise hidden by an
7715 -- explicit constraint. Renamed discriminants are handled in
7716 -- the code above.
7717
7718 -- If several parent discriminants are renamed by a single
7719 -- discriminant of the derived type, the call to obtain the
7720 -- Corresponding_Discriminant field only retrieves the last
7721 -- of them. We recover the constraint on the others from the
7722 -- Stored_Constraint as well.
7723
7724 declare
7725 D : Entity_Id;
7726 C : Elmt_Id;
7727
7728 begin
7729 D := First_Discriminant (Etype (Typ));
7730 C := First_Elmt (Stored_Constraint (Typ));
7731 while Present (D) and then Present (C) loop
7732 if Chars (Discrim_Name) = Chars (D) then
7733 if Is_Entity_Name (Node (C))
7734 and then Entity (Node (C)) = Entity (Discrim)
7735 then
7736 -- D is renamed by Discrim, whose value is given in
7737 -- Assoc.
7738
7739 null;
7740
7741 else
7742 Assoc :=
7743 Make_Component_Association (Sloc (Typ),
7744 New_List
7745 (New_Occurrence_Of (D, Sloc (Typ))),
7746 Duplicate_Subexpr_No_Checks (Node (C)));
7747 end if;
7748 exit Find_Constraint;
7749 end if;
7750
7751 Next_Discriminant (D);
7752 Next_Elmt (C);
7753 end loop;
7754 end;
7755 end if;
7756 end if;
7757
7758 if No (Next (Assoc)) then
7759 Error_Msg_NE (" missing value for discriminant&",
7760 First (Governed_By), Discrim_Name);
7761 Report_Errors := True;
7762 return;
7763 end if;
7764
7765 Next (Assoc);
7766 end loop Find_Constraint;
7767
7768 Discrim_Value := Expression (Assoc);
7769
7770 if not Is_OK_Static_Expression (Discrim_Value) then
7771
7772 -- If the variant part is governed by a discriminant of the type
7773 -- this is an error. If the variant part and the discriminant are
7774 -- inherited from an ancestor this is legal (AI05-120) unless the
7775 -- components are being gathered for an aggregate, in which case
7776 -- the caller must check Report_Errors.
7777
7778 if Scope (Original_Record_Component
7779 ((Entity (First (Choices (Assoc)))))) = Typ
7780 then
7781 Error_Msg_FE
7782 ("value for discriminant & must be static!",
7783 Discrim_Value, Discrim);
7784 Why_Not_Static (Discrim_Value);
7785 end if;
7786
7787 Report_Errors := True;
7788 return;
7789 end if;
7790
7791 Search_For_Discriminant_Value : declare
7792 Low : Node_Id;
7793 High : Node_Id;
7794
7795 UI_High : Uint;
7796 UI_Low : Uint;
7797 UI_Discrim_Value : constant Uint := Expr_Value (Discrim_Value);
7798
7799 begin
7800 Find_Discrete_Value : while Present (Variant) loop
7801 Discrete_Choice := First (Discrete_Choices (Variant));
7802 while Present (Discrete_Choice) loop
7803 exit Find_Discrete_Value when
7804 Nkind (Discrete_Choice) = N_Others_Choice;
7805
7806 Get_Index_Bounds (Discrete_Choice, Low, High);
7807
7808 UI_Low := Expr_Value (Low);
7809 UI_High := Expr_Value (High);
7810
7811 exit Find_Discrete_Value when
7812 UI_Low <= UI_Discrim_Value
7813 and then
7814 UI_High >= UI_Discrim_Value;
7815
7816 Next (Discrete_Choice);
7817 end loop;
7818
7819 Next_Non_Pragma (Variant);
7820 end loop Find_Discrete_Value;
7821 end Search_For_Discriminant_Value;
7822
7823 if No (Variant) then
7824 Error_Msg_NE
7825 ("value of discriminant & is out of range", Discrim_Value, Discrim);
7826 Report_Errors := True;
7827 return;
7828 end if;
7829
7830 -- If we have found the corresponding choice, recursively add its
7831 -- components to the Into list. The nested components are part of
7832 -- the same record type.
7833
7834 Gather_Components
7835 (Typ, Component_List (Variant), Governed_By, Into, Report_Errors);
7836 end Gather_Components;
7837
7838 ------------------------
7839 -- Get_Actual_Subtype --
7840 ------------------------
7841
7842 function Get_Actual_Subtype (N : Node_Id) return Entity_Id is
7843 Typ : constant Entity_Id := Etype (N);
7844 Utyp : Entity_Id := Underlying_Type (Typ);
7845 Decl : Node_Id;
7846 Atyp : Entity_Id;
7847
7848 begin
7849 if No (Utyp) then
7850 Utyp := Typ;
7851 end if;
7852
7853 -- If what we have is an identifier that references a subprogram
7854 -- formal, or a variable or constant object, then we get the actual
7855 -- subtype from the referenced entity if one has been built.
7856
7857 if Nkind (N) = N_Identifier
7858 and then
7859 (Is_Formal (Entity (N))
7860 or else Ekind (Entity (N)) = E_Constant
7861 or else Ekind (Entity (N)) = E_Variable)
7862 and then Present (Actual_Subtype (Entity (N)))
7863 then
7864 return Actual_Subtype (Entity (N));
7865
7866 -- Actual subtype of unchecked union is always itself. We never need
7867 -- the "real" actual subtype. If we did, we couldn't get it anyway
7868 -- because the discriminant is not available. The restrictions on
7869 -- Unchecked_Union are designed to make sure that this is OK.
7870
7871 elsif Is_Unchecked_Union (Base_Type (Utyp)) then
7872 return Typ;
7873
7874 -- Here for the unconstrained case, we must find actual subtype
7875 -- No actual subtype is available, so we must build it on the fly.
7876
7877 -- Checking the type, not the underlying type, for constrainedness
7878 -- seems to be necessary. Maybe all the tests should be on the type???
7879
7880 elsif (not Is_Constrained (Typ))
7881 and then (Is_Array_Type (Utyp)
7882 or else (Is_Record_Type (Utyp)
7883 and then Has_Discriminants (Utyp)))
7884 and then not Has_Unknown_Discriminants (Utyp)
7885 and then not (Ekind (Utyp) = E_String_Literal_Subtype)
7886 then
7887 -- Nothing to do if in spec expression (why not???)
7888
7889 if In_Spec_Expression then
7890 return Typ;
7891
7892 elsif Is_Private_Type (Typ) and then not Has_Discriminants (Typ) then
7893
7894 -- If the type has no discriminants, there is no subtype to
7895 -- build, even if the underlying type is discriminated.
7896
7897 return Typ;
7898
7899 -- Else build the actual subtype
7900
7901 else
7902 Decl := Build_Actual_Subtype (Typ, N);
7903 Atyp := Defining_Identifier (Decl);
7904
7905 -- If Build_Actual_Subtype generated a new declaration then use it
7906
7907 if Atyp /= Typ then
7908
7909 -- The actual subtype is an Itype, so analyze the declaration,
7910 -- but do not attach it to the tree, to get the type defined.
7911
7912 Set_Parent (Decl, N);
7913 Set_Is_Itype (Atyp);
7914 Analyze (Decl, Suppress => All_Checks);
7915 Set_Associated_Node_For_Itype (Atyp, N);
7916 Set_Has_Delayed_Freeze (Atyp, False);
7917
7918 -- We need to freeze the actual subtype immediately. This is
7919 -- needed, because otherwise this Itype will not get frozen
7920 -- at all, and it is always safe to freeze on creation because
7921 -- any associated types must be frozen at this point.
7922
7923 Freeze_Itype (Atyp, N);
7924 return Atyp;
7925
7926 -- Otherwise we did not build a declaration, so return original
7927
7928 else
7929 return Typ;
7930 end if;
7931 end if;
7932
7933 -- For all remaining cases, the actual subtype is the same as
7934 -- the nominal type.
7935
7936 else
7937 return Typ;
7938 end if;
7939 end Get_Actual_Subtype;
7940
7941 -------------------------------------
7942 -- Get_Actual_Subtype_If_Available --
7943 -------------------------------------
7944
7945 function Get_Actual_Subtype_If_Available (N : Node_Id) return Entity_Id is
7946 Typ : constant Entity_Id := Etype (N);
7947
7948 begin
7949 -- If what we have is an identifier that references a subprogram
7950 -- formal, or a variable or constant object, then we get the actual
7951 -- subtype from the referenced entity if one has been built.
7952
7953 if Nkind (N) = N_Identifier
7954 and then
7955 (Is_Formal (Entity (N))
7956 or else Ekind (Entity (N)) = E_Constant
7957 or else Ekind (Entity (N)) = E_Variable)
7958 and then Present (Actual_Subtype (Entity (N)))
7959 then
7960 return Actual_Subtype (Entity (N));
7961
7962 -- Otherwise the Etype of N is returned unchanged
7963
7964 else
7965 return Typ;
7966 end if;
7967 end Get_Actual_Subtype_If_Available;
7968
7969 ------------------------
7970 -- Get_Body_From_Stub --
7971 ------------------------
7972
7973 function Get_Body_From_Stub (N : Node_Id) return Node_Id is
7974 begin
7975 return Proper_Body (Unit (Library_Unit (N)));
7976 end Get_Body_From_Stub;
7977
7978 ---------------------
7979 -- Get_Cursor_Type --
7980 ---------------------
7981
7982 function Get_Cursor_Type
7983 (Aspect : Node_Id;
7984 Typ : Entity_Id) return Entity_Id
7985 is
7986 Assoc : Node_Id;
7987 Func : Entity_Id;
7988 First_Op : Entity_Id;
7989 Cursor : Entity_Id;
7990
7991 begin
7992 -- If error already detected, return
7993
7994 if Error_Posted (Aspect) then
7995 return Any_Type;
7996 end if;
7997
7998 -- The cursor type for an Iterable aspect is the return type of a
7999 -- non-overloaded First primitive operation. Locate association for
8000 -- First.
8001
8002 Assoc := First (Component_Associations (Expression (Aspect)));
8003 First_Op := Any_Id;
8004 while Present (Assoc) loop
8005 if Chars (First (Choices (Assoc))) = Name_First then
8006 First_Op := Expression (Assoc);
8007 exit;
8008 end if;
8009
8010 Next (Assoc);
8011 end loop;
8012
8013 if First_Op = Any_Id then
8014 Error_Msg_N ("aspect Iterable must specify First operation", Aspect);
8015 return Any_Type;
8016 end if;
8017
8018 Cursor := Any_Type;
8019
8020 -- Locate function with desired name and profile in scope of type
8021 -- In the rare case where the type is an integer type, a base type
8022 -- is created for it, check that the base type of the first formal
8023 -- of First matches the base type of the domain.
8024
8025 Func := First_Entity (Scope (Typ));
8026 while Present (Func) loop
8027 if Chars (Func) = Chars (First_Op)
8028 and then Ekind (Func) = E_Function
8029 and then Present (First_Formal (Func))
8030 and then Base_Type (Etype (First_Formal (Func))) = Base_Type (Typ)
8031 and then No (Next_Formal (First_Formal (Func)))
8032 then
8033 if Cursor /= Any_Type then
8034 Error_Msg_N
8035 ("Operation First for iterable type must be unique", Aspect);
8036 return Any_Type;
8037 else
8038 Cursor := Etype (Func);
8039 end if;
8040 end if;
8041
8042 Next_Entity (Func);
8043 end loop;
8044
8045 -- If not found, no way to resolve remaining primitives.
8046
8047 if Cursor = Any_Type then
8048 Error_Msg_N
8049 ("No legal primitive operation First for Iterable type", Aspect);
8050 end if;
8051
8052 return Cursor;
8053 end Get_Cursor_Type;
8054
8055 function Get_Cursor_Type (Typ : Entity_Id) return Entity_Id is
8056 begin
8057 return Etype (Get_Iterable_Type_Primitive (Typ, Name_First));
8058 end Get_Cursor_Type;
8059
8060 -------------------------------
8061 -- Get_Default_External_Name --
8062 -------------------------------
8063
8064 function Get_Default_External_Name (E : Node_Or_Entity_Id) return Node_Id is
8065 begin
8066 Get_Decoded_Name_String (Chars (E));
8067
8068 if Opt.External_Name_Imp_Casing = Uppercase then
8069 Set_Casing (All_Upper_Case);
8070 else
8071 Set_Casing (All_Lower_Case);
8072 end if;
8073
8074 return
8075 Make_String_Literal (Sloc (E),
8076 Strval => String_From_Name_Buffer);
8077 end Get_Default_External_Name;
8078
8079 --------------------------
8080 -- Get_Enclosing_Object --
8081 --------------------------
8082
8083 function Get_Enclosing_Object (N : Node_Id) return Entity_Id is
8084 begin
8085 if Is_Entity_Name (N) then
8086 return Entity (N);
8087 else
8088 case Nkind (N) is
8089 when N_Indexed_Component |
8090 N_Slice |
8091 N_Selected_Component =>
8092
8093 -- If not generating code, a dereference may be left implicit.
8094 -- In thoses cases, return Empty.
8095
8096 if Is_Access_Type (Etype (Prefix (N))) then
8097 return Empty;
8098 else
8099 return Get_Enclosing_Object (Prefix (N));
8100 end if;
8101
8102 when N_Type_Conversion =>
8103 return Get_Enclosing_Object (Expression (N));
8104
8105 when others =>
8106 return Empty;
8107 end case;
8108 end if;
8109 end Get_Enclosing_Object;
8110
8111 ---------------------------
8112 -- Get_Enum_Lit_From_Pos --
8113 ---------------------------
8114
8115 function Get_Enum_Lit_From_Pos
8116 (T : Entity_Id;
8117 Pos : Uint;
8118 Loc : Source_Ptr) return Node_Id
8119 is
8120 Btyp : Entity_Id := Base_Type (T);
8121 Lit : Node_Id;
8122
8123 begin
8124 -- In the case where the literal is of type Character, Wide_Character
8125 -- or Wide_Wide_Character or of a type derived from them, there needs
8126 -- to be some special handling since there is no explicit chain of
8127 -- literals to search. Instead, an N_Character_Literal node is created
8128 -- with the appropriate Char_Code and Chars fields.
8129
8130 if Is_Standard_Character_Type (T) then
8131 Set_Character_Literal_Name (UI_To_CC (Pos));
8132 return
8133 Make_Character_Literal (Loc,
8134 Chars => Name_Find,
8135 Char_Literal_Value => Pos);
8136
8137 -- For all other cases, we have a complete table of literals, and
8138 -- we simply iterate through the chain of literal until the one
8139 -- with the desired position value is found.
8140
8141 else
8142 if Is_Private_Type (Btyp) and then Present (Full_View (Btyp)) then
8143 Btyp := Full_View (Btyp);
8144 end if;
8145
8146 Lit := First_Literal (Btyp);
8147 for J in 1 .. UI_To_Int (Pos) loop
8148 Next_Literal (Lit);
8149 end loop;
8150
8151 return New_Occurrence_Of (Lit, Loc);
8152 end if;
8153 end Get_Enum_Lit_From_Pos;
8154
8155 ------------------------
8156 -- Get_Generic_Entity --
8157 ------------------------
8158
8159 function Get_Generic_Entity (N : Node_Id) return Entity_Id is
8160 Ent : constant Entity_Id := Entity (Name (N));
8161 begin
8162 if Present (Renamed_Object (Ent)) then
8163 return Renamed_Object (Ent);
8164 else
8165 return Ent;
8166 end if;
8167 end Get_Generic_Entity;
8168
8169 -------------------------------------
8170 -- Get_Incomplete_View_Of_Ancestor --
8171 -------------------------------------
8172
8173 function Get_Incomplete_View_Of_Ancestor (E : Entity_Id) return Entity_Id is
8174 Cur_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
8175 Par_Scope : Entity_Id;
8176 Par_Type : Entity_Id;
8177
8178 begin
8179 -- The incomplete view of an ancestor is only relevant for private
8180 -- derived types in child units.
8181
8182 if not Is_Derived_Type (E)
8183 or else not Is_Child_Unit (Cur_Unit)
8184 then
8185 return Empty;
8186
8187 else
8188 Par_Scope := Scope (Cur_Unit);
8189 if No (Par_Scope) then
8190 return Empty;
8191 end if;
8192
8193 Par_Type := Etype (Base_Type (E));
8194
8195 -- Traverse list of ancestor types until we find one declared in
8196 -- a parent or grandparent unit (two levels seem sufficient).
8197
8198 while Present (Par_Type) loop
8199 if Scope (Par_Type) = Par_Scope
8200 or else Scope (Par_Type) = Scope (Par_Scope)
8201 then
8202 return Par_Type;
8203
8204 elsif not Is_Derived_Type (Par_Type) then
8205 return Empty;
8206
8207 else
8208 Par_Type := Etype (Base_Type (Par_Type));
8209 end if;
8210 end loop;
8211
8212 -- If none found, there is no relevant ancestor type.
8213
8214 return Empty;
8215 end if;
8216 end Get_Incomplete_View_Of_Ancestor;
8217
8218 ----------------------
8219 -- Get_Index_Bounds --
8220 ----------------------
8221
8222 procedure Get_Index_Bounds (N : Node_Id; L, H : out Node_Id) is
8223 Kind : constant Node_Kind := Nkind (N);
8224 R : Node_Id;
8225
8226 begin
8227 if Kind = N_Range then
8228 L := Low_Bound (N);
8229 H := High_Bound (N);
8230
8231 elsif Kind = N_Subtype_Indication then
8232 R := Range_Expression (Constraint (N));
8233
8234 if R = Error then
8235 L := Error;
8236 H := Error;
8237 return;
8238
8239 else
8240 L := Low_Bound (Range_Expression (Constraint (N)));
8241 H := High_Bound (Range_Expression (Constraint (N)));
8242 end if;
8243
8244 elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
8245 if Error_Posted (Scalar_Range (Entity (N))) then
8246 L := Error;
8247 H := Error;
8248
8249 elsif Nkind (Scalar_Range (Entity (N))) = N_Subtype_Indication then
8250 Get_Index_Bounds (Scalar_Range (Entity (N)), L, H);
8251
8252 else
8253 L := Low_Bound (Scalar_Range (Entity (N)));
8254 H := High_Bound (Scalar_Range (Entity (N)));
8255 end if;
8256
8257 else
8258 -- N is an expression, indicating a range with one value
8259
8260 L := N;
8261 H := N;
8262 end if;
8263 end Get_Index_Bounds;
8264
8265 ---------------------------------
8266 -- Get_Iterable_Type_Primitive --
8267 ---------------------------------
8268
8269 function Get_Iterable_Type_Primitive
8270 (Typ : Entity_Id;
8271 Nam : Name_Id) return Entity_Id
8272 is
8273 Funcs : constant Node_Id := Find_Value_Of_Aspect (Typ, Aspect_Iterable);
8274 Assoc : Node_Id;
8275
8276 begin
8277 if No (Funcs) then
8278 return Empty;
8279
8280 else
8281 Assoc := First (Component_Associations (Funcs));
8282 while Present (Assoc) loop
8283 if Chars (First (Choices (Assoc))) = Nam then
8284 return Entity (Expression (Assoc));
8285 end if;
8286
8287 Assoc := Next (Assoc);
8288 end loop;
8289
8290 return Empty;
8291 end if;
8292 end Get_Iterable_Type_Primitive;
8293
8294 ----------------------------------
8295 -- Get_Library_Unit_Name_string --
8296 ----------------------------------
8297
8298 procedure Get_Library_Unit_Name_String (Decl_Node : Node_Id) is
8299 Unit_Name_Id : constant Unit_Name_Type := Get_Unit_Name (Decl_Node);
8300
8301 begin
8302 Get_Unit_Name_String (Unit_Name_Id);
8303
8304 -- Remove seven last character (" (spec)" or " (body)")
8305
8306 Name_Len := Name_Len - 7;
8307 pragma Assert (Name_Buffer (Name_Len + 1) = ' ');
8308 end Get_Library_Unit_Name_String;
8309
8310 ------------------------
8311 -- Get_Name_Entity_Id --
8312 ------------------------
8313
8314 function Get_Name_Entity_Id (Id : Name_Id) return Entity_Id is
8315 begin
8316 return Entity_Id (Get_Name_Table_Int (Id));
8317 end Get_Name_Entity_Id;
8318
8319 ------------------------------
8320 -- Get_Name_From_CTC_Pragma --
8321 ------------------------------
8322
8323 function Get_Name_From_CTC_Pragma (N : Node_Id) return String_Id is
8324 Arg : constant Node_Id :=
8325 Get_Pragma_Arg (First (Pragma_Argument_Associations (N)));
8326 begin
8327 return Strval (Expr_Value_S (Arg));
8328 end Get_Name_From_CTC_Pragma;
8329
8330 -----------------------
8331 -- Get_Parent_Entity --
8332 -----------------------
8333
8334 function Get_Parent_Entity (Unit : Node_Id) return Entity_Id is
8335 begin
8336 if Nkind (Unit) = N_Package_Body
8337 and then Nkind (Original_Node (Unit)) = N_Package_Instantiation
8338 then
8339 return Defining_Entity
8340 (Specification (Instance_Spec (Original_Node (Unit))));
8341 elsif Nkind (Unit) = N_Package_Instantiation then
8342 return Defining_Entity (Specification (Instance_Spec (Unit)));
8343 else
8344 return Defining_Entity (Unit);
8345 end if;
8346 end Get_Parent_Entity;
8347
8348 -------------------
8349 -- Get_Pragma_Id --
8350 -------------------
8351
8352 function Get_Pragma_Id (N : Node_Id) return Pragma_Id is
8353 begin
8354 return Get_Pragma_Id (Pragma_Name (N));
8355 end Get_Pragma_Id;
8356
8357 ------------------------
8358 -- Get_Qualified_Name --
8359 ------------------------
8360
8361 function Get_Qualified_Name
8362 (Id : Entity_Id;
8363 Suffix : Entity_Id := Empty) return Name_Id
8364 is
8365 Suffix_Nam : Name_Id := No_Name;
8366
8367 begin
8368 if Present (Suffix) then
8369 Suffix_Nam := Chars (Suffix);
8370 end if;
8371
8372 return Get_Qualified_Name (Chars (Id), Suffix_Nam, Scope (Id));
8373 end Get_Qualified_Name;
8374
8375 function Get_Qualified_Name
8376 (Nam : Name_Id;
8377 Suffix : Name_Id := No_Name;
8378 Scop : Entity_Id := Current_Scope) return Name_Id
8379 is
8380 procedure Add_Scope (S : Entity_Id);
8381 -- Add the fully qualified form of scope S to the name buffer. The
8382 -- format is:
8383 -- s-1__s__
8384
8385 ---------------
8386 -- Add_Scope --
8387 ---------------
8388
8389 procedure Add_Scope (S : Entity_Id) is
8390 begin
8391 if S = Empty then
8392 null;
8393
8394 elsif S = Standard_Standard then
8395 null;
8396
8397 else
8398 Add_Scope (Scope (S));
8399 Get_Name_String_And_Append (Chars (S));
8400 Add_Str_To_Name_Buffer ("__");
8401 end if;
8402 end Add_Scope;
8403
8404 -- Start of processing for Get_Qualified_Name
8405
8406 begin
8407 Name_Len := 0;
8408 Add_Scope (Scop);
8409
8410 -- Append the base name after all scopes have been chained
8411
8412 Get_Name_String_And_Append (Nam);
8413
8414 -- Append the suffix (if present)
8415
8416 if Suffix /= No_Name then
8417 Add_Str_To_Name_Buffer ("__");
8418 Get_Name_String_And_Append (Suffix);
8419 end if;
8420
8421 return Name_Find;
8422 end Get_Qualified_Name;
8423
8424 -----------------------
8425 -- Get_Reason_String --
8426 -----------------------
8427
8428 procedure Get_Reason_String (N : Node_Id) is
8429 begin
8430 if Nkind (N) = N_String_Literal then
8431 Store_String_Chars (Strval (N));
8432
8433 elsif Nkind (N) = N_Op_Concat then
8434 Get_Reason_String (Left_Opnd (N));
8435 Get_Reason_String (Right_Opnd (N));
8436
8437 -- If not of required form, error
8438
8439 else
8440 Error_Msg_N
8441 ("Reason for pragma Warnings has wrong form", N);
8442 Error_Msg_N
8443 ("\must be string literal or concatenation of string literals", N);
8444 return;
8445 end if;
8446 end Get_Reason_String;
8447
8448 --------------------------------
8449 -- Get_Reference_Discriminant --
8450 --------------------------------
8451
8452 function Get_Reference_Discriminant (Typ : Entity_Id) return Entity_Id is
8453 D : Entity_Id;
8454
8455 begin
8456 D := First_Discriminant (Typ);
8457 while Present (D) loop
8458 if Has_Implicit_Dereference (D) then
8459 return D;
8460 end if;
8461 Next_Discriminant (D);
8462 end loop;
8463
8464 return Empty;
8465 end Get_Reference_Discriminant;
8466
8467 ---------------------------
8468 -- Get_Referenced_Object --
8469 ---------------------------
8470
8471 function Get_Referenced_Object (N : Node_Id) return Node_Id is
8472 R : Node_Id;
8473
8474 begin
8475 R := N;
8476 while Is_Entity_Name (R)
8477 and then Present (Renamed_Object (Entity (R)))
8478 loop
8479 R := Renamed_Object (Entity (R));
8480 end loop;
8481
8482 return R;
8483 end Get_Referenced_Object;
8484
8485 ------------------------
8486 -- Get_Renamed_Entity --
8487 ------------------------
8488
8489 function Get_Renamed_Entity (E : Entity_Id) return Entity_Id is
8490 R : Entity_Id;
8491
8492 begin
8493 R := E;
8494 while Present (Renamed_Entity (R)) loop
8495 R := Renamed_Entity (R);
8496 end loop;
8497
8498 return R;
8499 end Get_Renamed_Entity;
8500
8501 -----------------------
8502 -- Get_Return_Object --
8503 -----------------------
8504
8505 function Get_Return_Object (N : Node_Id) return Entity_Id is
8506 Decl : Node_Id;
8507
8508 begin
8509 Decl := First (Return_Object_Declarations (N));
8510 while Present (Decl) loop
8511 exit when Nkind (Decl) = N_Object_Declaration
8512 and then Is_Return_Object (Defining_Identifier (Decl));
8513 Next (Decl);
8514 end loop;
8515
8516 pragma Assert (Present (Decl));
8517 return Defining_Identifier (Decl);
8518 end Get_Return_Object;
8519
8520 ---------------------------
8521 -- Get_Subprogram_Entity --
8522 ---------------------------
8523
8524 function Get_Subprogram_Entity (Nod : Node_Id) return Entity_Id is
8525 Subp : Node_Id;
8526 Subp_Id : Entity_Id;
8527
8528 begin
8529 if Nkind (Nod) = N_Accept_Statement then
8530 Subp := Entry_Direct_Name (Nod);
8531
8532 elsif Nkind (Nod) = N_Slice then
8533 Subp := Prefix (Nod);
8534
8535 else
8536 Subp := Name (Nod);
8537 end if;
8538
8539 -- Strip the subprogram call
8540
8541 loop
8542 if Nkind_In (Subp, N_Explicit_Dereference,
8543 N_Indexed_Component,
8544 N_Selected_Component)
8545 then
8546 Subp := Prefix (Subp);
8547
8548 elsif Nkind_In (Subp, N_Type_Conversion,
8549 N_Unchecked_Type_Conversion)
8550 then
8551 Subp := Expression (Subp);
8552
8553 else
8554 exit;
8555 end if;
8556 end loop;
8557
8558 -- Extract the entity of the subprogram call
8559
8560 if Is_Entity_Name (Subp) then
8561 Subp_Id := Entity (Subp);
8562
8563 if Ekind (Subp_Id) = E_Access_Subprogram_Type then
8564 Subp_Id := Directly_Designated_Type (Subp_Id);
8565 end if;
8566
8567 if Is_Subprogram (Subp_Id) then
8568 return Subp_Id;
8569 else
8570 return Empty;
8571 end if;
8572
8573 -- The search did not find a construct that denotes a subprogram
8574
8575 else
8576 return Empty;
8577 end if;
8578 end Get_Subprogram_Entity;
8579
8580 -----------------------------
8581 -- Get_Task_Body_Procedure --
8582 -----------------------------
8583
8584 function Get_Task_Body_Procedure (E : Entity_Id) return Node_Id is
8585 begin
8586 -- Note: A task type may be the completion of a private type with
8587 -- discriminants. When performing elaboration checks on a task
8588 -- declaration, the current view of the type may be the private one,
8589 -- and the procedure that holds the body of the task is held in its
8590 -- underlying type.
8591
8592 -- This is an odd function, why not have Task_Body_Procedure do
8593 -- the following digging???
8594
8595 return Task_Body_Procedure (Underlying_Type (Root_Type (E)));
8596 end Get_Task_Body_Procedure;
8597
8598 -------------------------
8599 -- Get_User_Defined_Eq --
8600 -------------------------
8601
8602 function Get_User_Defined_Eq (E : Entity_Id) return Entity_Id is
8603 Prim : Elmt_Id;
8604 Op : Entity_Id;
8605
8606 begin
8607 Prim := First_Elmt (Collect_Primitive_Operations (E));
8608 while Present (Prim) loop
8609 Op := Node (Prim);
8610
8611 if Chars (Op) = Name_Op_Eq
8612 and then Etype (Op) = Standard_Boolean
8613 and then Etype (First_Formal (Op)) = E
8614 and then Etype (Next_Formal (First_Formal (Op))) = E
8615 then
8616 return Op;
8617 end if;
8618
8619 Next_Elmt (Prim);
8620 end loop;
8621
8622 return Empty;
8623 end Get_User_Defined_Eq;
8624
8625 ---------------
8626 -- Get_Views --
8627 ---------------
8628
8629 procedure Get_Views
8630 (Typ : Entity_Id;
8631 Priv_Typ : out Entity_Id;
8632 Full_Typ : out Entity_Id;
8633 Full_Base : out Entity_Id;
8634 CRec_Typ : out Entity_Id)
8635 is
8636 begin
8637 -- Assume that none of the views can be recovered
8638
8639 Priv_Typ := Empty;
8640 Full_Typ := Empty;
8641 Full_Base := Empty;
8642 CRec_Typ := Empty;
8643
8644 -- The input type is private
8645
8646 if Is_Private_Type (Typ) then
8647 Priv_Typ := Typ;
8648 Full_Typ := Full_View (Priv_Typ);
8649
8650 if Present (Full_Typ) then
8651 Full_Base := Base_Type (Full_Typ);
8652
8653 if Ekind_In (Full_Typ, E_Protected_Type, E_Task_Type) then
8654 CRec_Typ := Corresponding_Record_Type (Full_Typ);
8655 end if;
8656 end if;
8657
8658 -- The input type is the corresponding record type of a protected or a
8659 -- task type.
8660
8661 elsif Ekind (Typ) = E_Record_Type
8662 and then Is_Concurrent_Record_Type (Typ)
8663 then
8664 CRec_Typ := Typ;
8665 Full_Typ := Corresponding_Concurrent_Type (CRec_Typ);
8666 Full_Base := Base_Type (Full_Typ);
8667 Priv_Typ := Incomplete_Or_Partial_View (Full_Typ);
8668
8669 -- Otherwise the input type could be the full view of a private type
8670
8671 else
8672 Full_Typ := Typ;
8673 Full_Base := Base_Type (Full_Typ);
8674
8675 if Ekind_In (Full_Typ, E_Protected_Type, E_Task_Type) then
8676 CRec_Typ := Corresponding_Record_Type (Full_Typ);
8677 end if;
8678
8679 -- The type is the full view of a private type, obtain the partial
8680 -- view.
8681
8682 if Has_Private_Declaration (Full_Typ)
8683 and then not Is_Private_Type (Full_Typ)
8684 then
8685 Priv_Typ := Incomplete_Or_Partial_View (Full_Typ);
8686
8687 -- The full view of a private type should always have a partial
8688 -- view.
8689
8690 pragma Assert (Present (Priv_Typ));
8691 end if;
8692 end if;
8693 end Get_Views;
8694
8695 -----------------------
8696 -- Has_Access_Values --
8697 -----------------------
8698
8699 function Has_Access_Values (T : Entity_Id) return Boolean is
8700 Typ : constant Entity_Id := Underlying_Type (T);
8701
8702 begin
8703 -- Case of a private type which is not completed yet. This can only
8704 -- happen in the case of a generic format type appearing directly, or
8705 -- as a component of the type to which this function is being applied
8706 -- at the top level. Return False in this case, since we certainly do
8707 -- not know that the type contains access types.
8708
8709 if No (Typ) then
8710 return False;
8711
8712 elsif Is_Access_Type (Typ) then
8713 return True;
8714
8715 elsif Is_Array_Type (Typ) then
8716 return Has_Access_Values (Component_Type (Typ));
8717
8718 elsif Is_Record_Type (Typ) then
8719 declare
8720 Comp : Entity_Id;
8721
8722 begin
8723 -- Loop to Check components
8724
8725 Comp := First_Component_Or_Discriminant (Typ);
8726 while Present (Comp) loop
8727
8728 -- Check for access component, tag field does not count, even
8729 -- though it is implemented internally using an access type.
8730
8731 if Has_Access_Values (Etype (Comp))
8732 and then Chars (Comp) /= Name_uTag
8733 then
8734 return True;
8735 end if;
8736
8737 Next_Component_Or_Discriminant (Comp);
8738 end loop;
8739 end;
8740
8741 return False;
8742
8743 else
8744 return False;
8745 end if;
8746 end Has_Access_Values;
8747
8748 ------------------------------
8749 -- Has_Compatible_Alignment --
8750 ------------------------------
8751
8752 function Has_Compatible_Alignment
8753 (Obj : Entity_Id;
8754 Expr : Node_Id;
8755 Layout_Done : Boolean) return Alignment_Result
8756 is
8757 function Has_Compatible_Alignment_Internal
8758 (Obj : Entity_Id;
8759 Expr : Node_Id;
8760 Layout_Done : Boolean;
8761 Default : Alignment_Result) return Alignment_Result;
8762 -- This is the internal recursive function that actually does the work.
8763 -- There is one additional parameter, which says what the result should
8764 -- be if no alignment information is found, and there is no definite
8765 -- indication of compatible alignments. At the outer level, this is set
8766 -- to Unknown, but for internal recursive calls in the case where types
8767 -- are known to be correct, it is set to Known_Compatible.
8768
8769 ---------------------------------------
8770 -- Has_Compatible_Alignment_Internal --
8771 ---------------------------------------
8772
8773 function Has_Compatible_Alignment_Internal
8774 (Obj : Entity_Id;
8775 Expr : Node_Id;
8776 Layout_Done : Boolean;
8777 Default : Alignment_Result) return Alignment_Result
8778 is
8779 Result : Alignment_Result := Known_Compatible;
8780 -- Holds the current status of the result. Note that once a value of
8781 -- Known_Incompatible is set, it is sticky and does not get changed
8782 -- to Unknown (the value in Result only gets worse as we go along,
8783 -- never better).
8784
8785 Offs : Uint := No_Uint;
8786 -- Set to a factor of the offset from the base object when Expr is a
8787 -- selected or indexed component, based on Component_Bit_Offset and
8788 -- Component_Size respectively. A negative value is used to represent
8789 -- a value which is not known at compile time.
8790
8791 procedure Check_Prefix;
8792 -- Checks the prefix recursively in the case where the expression
8793 -- is an indexed or selected component.
8794
8795 procedure Set_Result (R : Alignment_Result);
8796 -- If R represents a worse outcome (unknown instead of known
8797 -- compatible, or known incompatible), then set Result to R.
8798
8799 ------------------
8800 -- Check_Prefix --
8801 ------------------
8802
8803 procedure Check_Prefix is
8804 begin
8805 -- The subtlety here is that in doing a recursive call to check
8806 -- the prefix, we have to decide what to do in the case where we
8807 -- don't find any specific indication of an alignment problem.
8808
8809 -- At the outer level, we normally set Unknown as the result in
8810 -- this case, since we can only set Known_Compatible if we really
8811 -- know that the alignment value is OK, but for the recursive
8812 -- call, in the case where the types match, and we have not
8813 -- specified a peculiar alignment for the object, we are only
8814 -- concerned about suspicious rep clauses, the default case does
8815 -- not affect us, since the compiler will, in the absence of such
8816 -- rep clauses, ensure that the alignment is correct.
8817
8818 if Default = Known_Compatible
8819 or else
8820 (Etype (Obj) = Etype (Expr)
8821 and then (Unknown_Alignment (Obj)
8822 or else
8823 Alignment (Obj) = Alignment (Etype (Obj))))
8824 then
8825 Set_Result
8826 (Has_Compatible_Alignment_Internal
8827 (Obj, Prefix (Expr), Layout_Done, Known_Compatible));
8828
8829 -- In all other cases, we need a full check on the prefix
8830
8831 else
8832 Set_Result
8833 (Has_Compatible_Alignment_Internal
8834 (Obj, Prefix (Expr), Layout_Done, Unknown));
8835 end if;
8836 end Check_Prefix;
8837
8838 ----------------
8839 -- Set_Result --
8840 ----------------
8841
8842 procedure Set_Result (R : Alignment_Result) is
8843 begin
8844 if R > Result then
8845 Result := R;
8846 end if;
8847 end Set_Result;
8848
8849 -- Start of processing for Has_Compatible_Alignment_Internal
8850
8851 begin
8852 -- If Expr is a selected component, we must make sure there is no
8853 -- potentially troublesome component clause and that the record is
8854 -- not packed if the layout is not done.
8855
8856 if Nkind (Expr) = N_Selected_Component then
8857
8858 -- Packing generates unknown alignment if layout is not done
8859
8860 if Is_Packed (Etype (Prefix (Expr))) and then not Layout_Done then
8861 Set_Result (Unknown);
8862 end if;
8863
8864 -- Check prefix and component offset
8865
8866 Check_Prefix;
8867 Offs := Component_Bit_Offset (Entity (Selector_Name (Expr)));
8868
8869 -- If Expr is an indexed component, we must make sure there is no
8870 -- potentially troublesome Component_Size clause and that the array
8871 -- is not bit-packed if the layout is not done.
8872
8873 elsif Nkind (Expr) = N_Indexed_Component then
8874 declare
8875 Typ : constant Entity_Id := Etype (Prefix (Expr));
8876
8877 begin
8878 -- Packing generates unknown alignment if layout is not done
8879
8880 if Is_Bit_Packed_Array (Typ) and then not Layout_Done then
8881 Set_Result (Unknown);
8882 end if;
8883
8884 -- Check prefix and component offset (or at least size)
8885
8886 Check_Prefix;
8887 Offs := Indexed_Component_Bit_Offset (Expr);
8888 if Offs = No_Uint then
8889 Offs := Component_Size (Typ);
8890 end if;
8891 end;
8892 end if;
8893
8894 -- If we have a null offset, the result is entirely determined by
8895 -- the base object and has already been computed recursively.
8896
8897 if Offs = Uint_0 then
8898 null;
8899
8900 -- Case where we know the alignment of the object
8901
8902 elsif Known_Alignment (Obj) then
8903 declare
8904 ObjA : constant Uint := Alignment (Obj);
8905 ExpA : Uint := No_Uint;
8906 SizA : Uint := No_Uint;
8907
8908 begin
8909 -- If alignment of Obj is 1, then we are always OK
8910
8911 if ObjA = 1 then
8912 Set_Result (Known_Compatible);
8913
8914 -- Alignment of Obj is greater than 1, so we need to check
8915
8916 else
8917 -- If we have an offset, see if it is compatible
8918
8919 if Offs /= No_Uint and Offs > Uint_0 then
8920 if Offs mod (System_Storage_Unit * ObjA) /= 0 then
8921 Set_Result (Known_Incompatible);
8922 end if;
8923
8924 -- See if Expr is an object with known alignment
8925
8926 elsif Is_Entity_Name (Expr)
8927 and then Known_Alignment (Entity (Expr))
8928 then
8929 ExpA := Alignment (Entity (Expr));
8930
8931 -- Otherwise, we can use the alignment of the type of
8932 -- Expr given that we already checked for
8933 -- discombobulating rep clauses for the cases of indexed
8934 -- and selected components above.
8935
8936 elsif Known_Alignment (Etype (Expr)) then
8937 ExpA := Alignment (Etype (Expr));
8938
8939 -- Otherwise the alignment is unknown
8940
8941 else
8942 Set_Result (Default);
8943 end if;
8944
8945 -- If we got an alignment, see if it is acceptable
8946
8947 if ExpA /= No_Uint and then ExpA < ObjA then
8948 Set_Result (Known_Incompatible);
8949 end if;
8950
8951 -- If Expr is not a piece of a larger object, see if size
8952 -- is given. If so, check that it is not too small for the
8953 -- required alignment.
8954
8955 if Offs /= No_Uint then
8956 null;
8957
8958 -- See if Expr is an object with known size
8959
8960 elsif Is_Entity_Name (Expr)
8961 and then Known_Static_Esize (Entity (Expr))
8962 then
8963 SizA := Esize (Entity (Expr));
8964
8965 -- Otherwise, we check the object size of the Expr type
8966
8967 elsif Known_Static_Esize (Etype (Expr)) then
8968 SizA := Esize (Etype (Expr));
8969 end if;
8970
8971 -- If we got a size, see if it is a multiple of the Obj
8972 -- alignment, if not, then the alignment cannot be
8973 -- acceptable, since the size is always a multiple of the
8974 -- alignment.
8975
8976 if SizA /= No_Uint then
8977 if SizA mod (ObjA * Ttypes.System_Storage_Unit) /= 0 then
8978 Set_Result (Known_Incompatible);
8979 end if;
8980 end if;
8981 end if;
8982 end;
8983
8984 -- If we do not know required alignment, any non-zero offset is a
8985 -- potential problem (but certainly may be OK, so result is unknown).
8986
8987 elsif Offs /= No_Uint then
8988 Set_Result (Unknown);
8989
8990 -- If we can't find the result by direct comparison of alignment
8991 -- values, then there is still one case that we can determine known
8992 -- result, and that is when we can determine that the types are the
8993 -- same, and no alignments are specified. Then we known that the
8994 -- alignments are compatible, even if we don't know the alignment
8995 -- value in the front end.
8996
8997 elsif Etype (Obj) = Etype (Expr) then
8998
8999 -- Types are the same, but we have to check for possible size
9000 -- and alignments on the Expr object that may make the alignment
9001 -- different, even though the types are the same.
9002
9003 if Is_Entity_Name (Expr) then
9004
9005 -- First check alignment of the Expr object. Any alignment less
9006 -- than Maximum_Alignment is worrisome since this is the case
9007 -- where we do not know the alignment of Obj.
9008
9009 if Known_Alignment (Entity (Expr))
9010 and then UI_To_Int (Alignment (Entity (Expr))) <
9011 Ttypes.Maximum_Alignment
9012 then
9013 Set_Result (Unknown);
9014
9015 -- Now check size of Expr object. Any size that is not an
9016 -- even multiple of Maximum_Alignment is also worrisome
9017 -- since it may cause the alignment of the object to be less
9018 -- than the alignment of the type.
9019
9020 elsif Known_Static_Esize (Entity (Expr))
9021 and then
9022 (UI_To_Int (Esize (Entity (Expr))) mod
9023 (Ttypes.Maximum_Alignment * Ttypes.System_Storage_Unit))
9024 /= 0
9025 then
9026 Set_Result (Unknown);
9027
9028 -- Otherwise same type is decisive
9029
9030 else
9031 Set_Result (Known_Compatible);
9032 end if;
9033 end if;
9034
9035 -- Another case to deal with is when there is an explicit size or
9036 -- alignment clause when the types are not the same. If so, then the
9037 -- result is Unknown. We don't need to do this test if the Default is
9038 -- Unknown, since that result will be set in any case.
9039
9040 elsif Default /= Unknown
9041 and then (Has_Size_Clause (Etype (Expr))
9042 or else
9043 Has_Alignment_Clause (Etype (Expr)))
9044 then
9045 Set_Result (Unknown);
9046
9047 -- If no indication found, set default
9048
9049 else
9050 Set_Result (Default);
9051 end if;
9052
9053 -- Return worst result found
9054
9055 return Result;
9056 end Has_Compatible_Alignment_Internal;
9057
9058 -- Start of processing for Has_Compatible_Alignment
9059
9060 begin
9061 -- If Obj has no specified alignment, then set alignment from the type
9062 -- alignment. Perhaps we should always do this, but for sure we should
9063 -- do it when there is an address clause since we can do more if the
9064 -- alignment is known.
9065
9066 if Unknown_Alignment (Obj) then
9067 Set_Alignment (Obj, Alignment (Etype (Obj)));
9068 end if;
9069
9070 -- Now do the internal call that does all the work
9071
9072 return
9073 Has_Compatible_Alignment_Internal (Obj, Expr, Layout_Done, Unknown);
9074 end Has_Compatible_Alignment;
9075
9076 ----------------------
9077 -- Has_Declarations --
9078 ----------------------
9079
9080 function Has_Declarations (N : Node_Id) return Boolean is
9081 begin
9082 return Nkind_In (Nkind (N), N_Accept_Statement,
9083 N_Block_Statement,
9084 N_Compilation_Unit_Aux,
9085 N_Entry_Body,
9086 N_Package_Body,
9087 N_Protected_Body,
9088 N_Subprogram_Body,
9089 N_Task_Body,
9090 N_Package_Specification);
9091 end Has_Declarations;
9092
9093 ---------------------------------
9094 -- Has_Defaulted_Discriminants --
9095 ---------------------------------
9096
9097 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean is
9098 begin
9099 return Has_Discriminants (Typ)
9100 and then Present (First_Discriminant (Typ))
9101 and then Present (Discriminant_Default_Value
9102 (First_Discriminant (Typ)));
9103 end Has_Defaulted_Discriminants;
9104
9105 -------------------
9106 -- Has_Denormals --
9107 -------------------
9108
9109 function Has_Denormals (E : Entity_Id) return Boolean is
9110 begin
9111 return Is_Floating_Point_Type (E) and then Denorm_On_Target;
9112 end Has_Denormals;
9113
9114 -------------------------------------------
9115 -- Has_Discriminant_Dependent_Constraint --
9116 -------------------------------------------
9117
9118 function Has_Discriminant_Dependent_Constraint
9119 (Comp : Entity_Id) return Boolean
9120 is
9121 Comp_Decl : constant Node_Id := Parent (Comp);
9122 Subt_Indic : Node_Id;
9123 Constr : Node_Id;
9124 Assn : Node_Id;
9125
9126 begin
9127 -- Discriminants can't depend on discriminants
9128
9129 if Ekind (Comp) = E_Discriminant then
9130 return False;
9131
9132 else
9133 Subt_Indic := Subtype_Indication (Component_Definition (Comp_Decl));
9134
9135 if Nkind (Subt_Indic) = N_Subtype_Indication then
9136 Constr := Constraint (Subt_Indic);
9137
9138 if Nkind (Constr) = N_Index_Or_Discriminant_Constraint then
9139 Assn := First (Constraints (Constr));
9140 while Present (Assn) loop
9141 case Nkind (Assn) is
9142 when N_Subtype_Indication |
9143 N_Range |
9144 N_Identifier
9145 =>
9146 if Depends_On_Discriminant (Assn) then
9147 return True;
9148 end if;
9149
9150 when N_Discriminant_Association =>
9151 if Depends_On_Discriminant (Expression (Assn)) then
9152 return True;
9153 end if;
9154
9155 when others =>
9156 null;
9157 end case;
9158
9159 Next (Assn);
9160 end loop;
9161 end if;
9162 end if;
9163 end if;
9164
9165 return False;
9166 end Has_Discriminant_Dependent_Constraint;
9167
9168 --------------------------------------
9169 -- Has_Effectively_Volatile_Profile --
9170 --------------------------------------
9171
9172 function Has_Effectively_Volatile_Profile
9173 (Subp_Id : Entity_Id) return Boolean
9174 is
9175 Formal : Entity_Id;
9176
9177 begin
9178 -- Inspect the formal parameters looking for an effectively volatile
9179 -- type.
9180
9181 Formal := First_Formal (Subp_Id);
9182 while Present (Formal) loop
9183 if Is_Effectively_Volatile (Etype (Formal)) then
9184 return True;
9185 end if;
9186
9187 Next_Formal (Formal);
9188 end loop;
9189
9190 -- Inspect the return type of functions
9191
9192 if Ekind_In (Subp_Id, E_Function, E_Generic_Function)
9193 and then Is_Effectively_Volatile (Etype (Subp_Id))
9194 then
9195 return True;
9196 end if;
9197
9198 return False;
9199 end Has_Effectively_Volatile_Profile;
9200
9201 --------------------------
9202 -- Has_Enabled_Property --
9203 --------------------------
9204
9205 function Has_Enabled_Property
9206 (Item_Id : Entity_Id;
9207 Property : Name_Id) return Boolean
9208 is
9209 function State_Has_Enabled_Property return Boolean;
9210 -- Determine whether a state denoted by Item_Id has the property enabled
9211
9212 function Variable_Has_Enabled_Property return Boolean;
9213 -- Determine whether a variable denoted by Item_Id has the property
9214 -- enabled.
9215
9216 --------------------------------
9217 -- State_Has_Enabled_Property --
9218 --------------------------------
9219
9220 function State_Has_Enabled_Property return Boolean is
9221 Decl : constant Node_Id := Parent (Item_Id);
9222 Opt : Node_Id;
9223 Opt_Nam : Node_Id;
9224 Prop : Node_Id;
9225 Prop_Nam : Node_Id;
9226 Props : Node_Id;
9227
9228 begin
9229 -- The declaration of an external abstract state appears as an
9230 -- extension aggregate. If this is not the case, properties can never
9231 -- be set.
9232
9233 if Nkind (Decl) /= N_Extension_Aggregate then
9234 return False;
9235 end if;
9236
9237 -- When External appears as a simple option, it automatically enables
9238 -- all properties.
9239
9240 Opt := First (Expressions (Decl));
9241 while Present (Opt) loop
9242 if Nkind (Opt) = N_Identifier
9243 and then Chars (Opt) = Name_External
9244 then
9245 return True;
9246 end if;
9247
9248 Next (Opt);
9249 end loop;
9250
9251 -- When External specifies particular properties, inspect those and
9252 -- find the desired one (if any).
9253
9254 Opt := First (Component_Associations (Decl));
9255 while Present (Opt) loop
9256 Opt_Nam := First (Choices (Opt));
9257
9258 if Nkind (Opt_Nam) = N_Identifier
9259 and then Chars (Opt_Nam) = Name_External
9260 then
9261 Props := Expression (Opt);
9262
9263 -- Multiple properties appear as an aggregate
9264
9265 if Nkind (Props) = N_Aggregate then
9266
9267 -- Simple property form
9268
9269 Prop := First (Expressions (Props));
9270 while Present (Prop) loop
9271 if Chars (Prop) = Property then
9272 return True;
9273 end if;
9274
9275 Next (Prop);
9276 end loop;
9277
9278 -- Property with expression form
9279
9280 Prop := First (Component_Associations (Props));
9281 while Present (Prop) loop
9282 Prop_Nam := First (Choices (Prop));
9283
9284 -- The property can be represented in two ways:
9285 -- others => <value>
9286 -- <property> => <value>
9287
9288 if Nkind (Prop_Nam) = N_Others_Choice
9289 or else (Nkind (Prop_Nam) = N_Identifier
9290 and then Chars (Prop_Nam) = Property)
9291 then
9292 return Is_True (Expr_Value (Expression (Prop)));
9293 end if;
9294
9295 Next (Prop);
9296 end loop;
9297
9298 -- Single property
9299
9300 else
9301 return Chars (Props) = Property;
9302 end if;
9303 end if;
9304
9305 Next (Opt);
9306 end loop;
9307
9308 return False;
9309 end State_Has_Enabled_Property;
9310
9311 -----------------------------------
9312 -- Variable_Has_Enabled_Property --
9313 -----------------------------------
9314
9315 function Variable_Has_Enabled_Property return Boolean is
9316 function Is_Enabled (Prag : Node_Id) return Boolean;
9317 -- Determine whether property pragma Prag (if present) denotes an
9318 -- enabled property.
9319
9320 ----------------
9321 -- Is_Enabled --
9322 ----------------
9323
9324 function Is_Enabled (Prag : Node_Id) return Boolean is
9325 Arg1 : Node_Id;
9326
9327 begin
9328 if Present (Prag) then
9329 Arg1 := First (Pragma_Argument_Associations (Prag));
9330
9331 -- The pragma has an optional Boolean expression, the related
9332 -- property is enabled only when the expression evaluates to
9333 -- True.
9334
9335 if Present (Arg1) then
9336 return Is_True (Expr_Value (Get_Pragma_Arg (Arg1)));
9337
9338 -- Otherwise the lack of expression enables the property by
9339 -- default.
9340
9341 else
9342 return True;
9343 end if;
9344
9345 -- The property was never set in the first place
9346
9347 else
9348 return False;
9349 end if;
9350 end Is_Enabled;
9351
9352 -- Local variables
9353
9354 AR : constant Node_Id :=
9355 Get_Pragma (Item_Id, Pragma_Async_Readers);
9356 AW : constant Node_Id :=
9357 Get_Pragma (Item_Id, Pragma_Async_Writers);
9358 ER : constant Node_Id :=
9359 Get_Pragma (Item_Id, Pragma_Effective_Reads);
9360 EW : constant Node_Id :=
9361 Get_Pragma (Item_Id, Pragma_Effective_Writes);
9362
9363 -- Start of processing for Variable_Has_Enabled_Property
9364
9365 begin
9366 -- A non-effectively volatile object can never possess external
9367 -- properties.
9368
9369 if not Is_Effectively_Volatile (Item_Id) then
9370 return False;
9371
9372 -- External properties related to variables come in two flavors -
9373 -- explicit and implicit. The explicit case is characterized by the
9374 -- presence of a property pragma with an optional Boolean flag. The
9375 -- property is enabled when the flag evaluates to True or the flag is
9376 -- missing altogether.
9377
9378 elsif Property = Name_Async_Readers and then Is_Enabled (AR) then
9379 return True;
9380
9381 elsif Property = Name_Async_Writers and then Is_Enabled (AW) then
9382 return True;
9383
9384 elsif Property = Name_Effective_Reads and then Is_Enabled (ER) then
9385 return True;
9386
9387 elsif Property = Name_Effective_Writes and then Is_Enabled (EW) then
9388 return True;
9389
9390 -- The implicit case lacks all property pragmas
9391
9392 elsif No (AR) and then No (AW) and then No (ER) and then No (EW) then
9393 return True;
9394
9395 else
9396 return False;
9397 end if;
9398 end Variable_Has_Enabled_Property;
9399
9400 -- Start of processing for Has_Enabled_Property
9401
9402 begin
9403 -- Abstract states and variables have a flexible scheme of specifying
9404 -- external properties.
9405
9406 if Ekind (Item_Id) = E_Abstract_State then
9407 return State_Has_Enabled_Property;
9408
9409 elsif Ekind (Item_Id) = E_Variable then
9410 return Variable_Has_Enabled_Property;
9411
9412 -- Otherwise a property is enabled when the related item is effectively
9413 -- volatile.
9414
9415 else
9416 return Is_Effectively_Volatile (Item_Id);
9417 end if;
9418 end Has_Enabled_Property;
9419
9420 -------------------------------------
9421 -- Has_Full_Default_Initialization --
9422 -------------------------------------
9423
9424 function Has_Full_Default_Initialization (Typ : Entity_Id) return Boolean is
9425 Arg : Node_Id;
9426 Comp : Entity_Id;
9427 Prag : Node_Id;
9428
9429 begin
9430 -- A private type and its full view is fully default initialized when it
9431 -- is subject to pragma Default_Initial_Condition without an argument or
9432 -- with a non-null argument. Since any type may act as the full view of
9433 -- a private type, this check must be performed prior to the specialized
9434 -- tests below.
9435
9436 if Has_Default_Init_Cond (Typ)
9437 or else Has_Inherited_Default_Init_Cond (Typ)
9438 then
9439 Prag := Get_Pragma (Typ, Pragma_Default_Initial_Condition);
9440
9441 -- Pragma Default_Initial_Condition must be present if one of the
9442 -- related entity flags is set.
9443
9444 pragma Assert (Present (Prag));
9445 Arg := First (Pragma_Argument_Associations (Prag));
9446
9447 -- A non-null argument guarantees full default initialization
9448
9449 if Present (Arg) then
9450 return Nkind (Arg) /= N_Null;
9451
9452 -- Otherwise the missing argument defaults the pragma to "True" which
9453 -- is considered a non-null argument (see above).
9454
9455 else
9456 return True;
9457 end if;
9458 end if;
9459
9460 -- A scalar type is fully default initialized if it is subject to aspect
9461 -- Default_Value.
9462
9463 if Is_Scalar_Type (Typ) then
9464 return Has_Default_Aspect (Typ);
9465
9466 -- An array type is fully default initialized if its element type is
9467 -- scalar and the array type carries aspect Default_Component_Value or
9468 -- the element type is fully default initialized.
9469
9470 elsif Is_Array_Type (Typ) then
9471 return
9472 Has_Default_Aspect (Typ)
9473 or else Has_Full_Default_Initialization (Component_Type (Typ));
9474
9475 -- A protected type, record type, or type extension is fully default
9476 -- initialized if all its components either carry an initialization
9477 -- expression or have a type that is fully default initialized. The
9478 -- parent type of a type extension must be fully default initialized.
9479
9480 elsif Is_Record_Type (Typ) or else Is_Protected_Type (Typ) then
9481
9482 -- Inspect all entities defined in the scope of the type, looking for
9483 -- uninitialized components.
9484
9485 Comp := First_Entity (Typ);
9486 while Present (Comp) loop
9487 if Ekind (Comp) = E_Component
9488 and then Comes_From_Source (Comp)
9489 and then No (Expression (Parent (Comp)))
9490 and then not Has_Full_Default_Initialization (Etype (Comp))
9491 then
9492 return False;
9493 end if;
9494
9495 Next_Entity (Comp);
9496 end loop;
9497
9498 -- Ensure that the parent type of a type extension is fully default
9499 -- initialized.
9500
9501 if Etype (Typ) /= Typ
9502 and then not Has_Full_Default_Initialization (Etype (Typ))
9503 then
9504 return False;
9505 end if;
9506
9507 -- If we get here, then all components and parent portion are fully
9508 -- default initialized.
9509
9510 return True;
9511
9512 -- A task type is fully default initialized by default
9513
9514 elsif Is_Task_Type (Typ) then
9515 return True;
9516
9517 -- Otherwise the type is not fully default initialized
9518
9519 else
9520 return False;
9521 end if;
9522 end Has_Full_Default_Initialization;
9523
9524 --------------------
9525 -- Has_Infinities --
9526 --------------------
9527
9528 function Has_Infinities (E : Entity_Id) return Boolean is
9529 begin
9530 return
9531 Is_Floating_Point_Type (E)
9532 and then Nkind (Scalar_Range (E)) = N_Range
9533 and then Includes_Infinities (Scalar_Range (E));
9534 end Has_Infinities;
9535
9536 --------------------
9537 -- Has_Interfaces --
9538 --------------------
9539
9540 function Has_Interfaces
9541 (T : Entity_Id;
9542 Use_Full_View : Boolean := True) return Boolean
9543 is
9544 Typ : Entity_Id := Base_Type (T);
9545
9546 begin
9547 -- Handle concurrent types
9548
9549 if Is_Concurrent_Type (Typ) then
9550 Typ := Corresponding_Record_Type (Typ);
9551 end if;
9552
9553 if not Present (Typ)
9554 or else not Is_Record_Type (Typ)
9555 or else not Is_Tagged_Type (Typ)
9556 then
9557 return False;
9558 end if;
9559
9560 -- Handle private types
9561
9562 if Use_Full_View and then Present (Full_View (Typ)) then
9563 Typ := Full_View (Typ);
9564 end if;
9565
9566 -- Handle concurrent record types
9567
9568 if Is_Concurrent_Record_Type (Typ)
9569 and then Is_Non_Empty_List (Abstract_Interface_List (Typ))
9570 then
9571 return True;
9572 end if;
9573
9574 loop
9575 if Is_Interface (Typ)
9576 or else
9577 (Is_Record_Type (Typ)
9578 and then Present (Interfaces (Typ))
9579 and then not Is_Empty_Elmt_List (Interfaces (Typ)))
9580 then
9581 return True;
9582 end if;
9583
9584 exit when Etype (Typ) = Typ
9585
9586 -- Handle private types
9587
9588 or else (Present (Full_View (Etype (Typ)))
9589 and then Full_View (Etype (Typ)) = Typ)
9590
9591 -- Protect frontend against wrong sources with cyclic derivations
9592
9593 or else Etype (Typ) = T;
9594
9595 -- Climb to the ancestor type handling private types
9596
9597 if Present (Full_View (Etype (Typ))) then
9598 Typ := Full_View (Etype (Typ));
9599 else
9600 Typ := Etype (Typ);
9601 end if;
9602 end loop;
9603
9604 return False;
9605 end Has_Interfaces;
9606
9607 ---------------------------------
9608 -- Has_No_Obvious_Side_Effects --
9609 ---------------------------------
9610
9611 function Has_No_Obvious_Side_Effects (N : Node_Id) return Boolean is
9612 begin
9613 -- For now, just handle literals, constants, and non-volatile
9614 -- variables and expressions combining these with operators or
9615 -- short circuit forms.
9616
9617 if Nkind (N) in N_Numeric_Or_String_Literal then
9618 return True;
9619
9620 elsif Nkind (N) = N_Character_Literal then
9621 return True;
9622
9623 elsif Nkind (N) in N_Unary_Op then
9624 return Has_No_Obvious_Side_Effects (Right_Opnd (N));
9625
9626 elsif Nkind (N) in N_Binary_Op or else Nkind (N) in N_Short_Circuit then
9627 return Has_No_Obvious_Side_Effects (Left_Opnd (N))
9628 and then
9629 Has_No_Obvious_Side_Effects (Right_Opnd (N));
9630
9631 elsif Nkind (N) = N_Expression_With_Actions
9632 and then Is_Empty_List (Actions (N))
9633 then
9634 return Has_No_Obvious_Side_Effects (Expression (N));
9635
9636 elsif Nkind (N) in N_Has_Entity then
9637 return Present (Entity (N))
9638 and then Ekind_In (Entity (N), E_Variable,
9639 E_Constant,
9640 E_Enumeration_Literal,
9641 E_In_Parameter,
9642 E_Out_Parameter,
9643 E_In_Out_Parameter)
9644 and then not Is_Volatile (Entity (N));
9645
9646 else
9647 return False;
9648 end if;
9649 end Has_No_Obvious_Side_Effects;
9650
9651 -----------------------------
9652 -- Has_Non_Null_Refinement --
9653 -----------------------------
9654
9655 function Has_Non_Null_Refinement (Id : Entity_Id) return Boolean is
9656 Constits : Elist_Id;
9657
9658 begin
9659 pragma Assert (Ekind (Id) = E_Abstract_State);
9660 Constits := Refinement_Constituents (Id);
9661
9662 -- For a refinement to be non-null, the first constituent must be
9663 -- anything other than null.
9664
9665 return
9666 Present (Constits)
9667 and then Nkind (Node (First_Elmt (Constits))) /= N_Null;
9668 end Has_Non_Null_Refinement;
9669
9670 -------------------
9671 -- Has_Null_Body --
9672 -------------------
9673
9674 function Has_Null_Body (Proc_Id : Entity_Id) return Boolean is
9675 Body_Id : Entity_Id;
9676 Decl : Node_Id;
9677 Spec : Node_Id;
9678 Stmt1 : Node_Id;
9679 Stmt2 : Node_Id;
9680
9681 begin
9682 Spec := Parent (Proc_Id);
9683 Decl := Parent (Spec);
9684
9685 -- Retrieve the entity of the procedure body (e.g. invariant proc).
9686
9687 if Nkind (Spec) = N_Procedure_Specification
9688 and then Nkind (Decl) = N_Subprogram_Declaration
9689 then
9690 Body_Id := Corresponding_Body (Decl);
9691
9692 -- The body acts as a spec
9693
9694 else
9695 Body_Id := Proc_Id;
9696 end if;
9697
9698 -- The body will be generated later
9699
9700 if No (Body_Id) then
9701 return False;
9702 end if;
9703
9704 Spec := Parent (Body_Id);
9705 Decl := Parent (Spec);
9706
9707 pragma Assert
9708 (Nkind (Spec) = N_Procedure_Specification
9709 and then Nkind (Decl) = N_Subprogram_Body);
9710
9711 Stmt1 := First (Statements (Handled_Statement_Sequence (Decl)));
9712
9713 -- Look for a null statement followed by an optional return
9714 -- statement.
9715
9716 if Nkind (Stmt1) = N_Null_Statement then
9717 Stmt2 := Next (Stmt1);
9718
9719 if Present (Stmt2) then
9720 return Nkind (Stmt2) = N_Simple_Return_Statement;
9721 else
9722 return True;
9723 end if;
9724 end if;
9725
9726 return False;
9727 end Has_Null_Body;
9728
9729 ------------------------
9730 -- Has_Null_Exclusion --
9731 ------------------------
9732
9733 function Has_Null_Exclusion (N : Node_Id) return Boolean is
9734 begin
9735 case Nkind (N) is
9736 when N_Access_Definition |
9737 N_Access_Function_Definition |
9738 N_Access_Procedure_Definition |
9739 N_Access_To_Object_Definition |
9740 N_Allocator |
9741 N_Derived_Type_Definition |
9742 N_Function_Specification |
9743 N_Subtype_Declaration =>
9744 return Null_Exclusion_Present (N);
9745
9746 when N_Component_Definition |
9747 N_Formal_Object_Declaration |
9748 N_Object_Renaming_Declaration =>
9749 if Present (Subtype_Mark (N)) then
9750 return Null_Exclusion_Present (N);
9751 else pragma Assert (Present (Access_Definition (N)));
9752 return Null_Exclusion_Present (Access_Definition (N));
9753 end if;
9754
9755 when N_Discriminant_Specification =>
9756 if Nkind (Discriminant_Type (N)) = N_Access_Definition then
9757 return Null_Exclusion_Present (Discriminant_Type (N));
9758 else
9759 return Null_Exclusion_Present (N);
9760 end if;
9761
9762 when N_Object_Declaration =>
9763 if Nkind (Object_Definition (N)) = N_Access_Definition then
9764 return Null_Exclusion_Present (Object_Definition (N));
9765 else
9766 return Null_Exclusion_Present (N);
9767 end if;
9768
9769 when N_Parameter_Specification =>
9770 if Nkind (Parameter_Type (N)) = N_Access_Definition then
9771 return Null_Exclusion_Present (Parameter_Type (N));
9772 else
9773 return Null_Exclusion_Present (N);
9774 end if;
9775
9776 when others =>
9777 return False;
9778
9779 end case;
9780 end Has_Null_Exclusion;
9781
9782 ------------------------
9783 -- Has_Null_Extension --
9784 ------------------------
9785
9786 function Has_Null_Extension (T : Entity_Id) return Boolean is
9787 B : constant Entity_Id := Base_Type (T);
9788 Comps : Node_Id;
9789 Ext : Node_Id;
9790
9791 begin
9792 if Nkind (Parent (B)) = N_Full_Type_Declaration
9793 and then Present (Record_Extension_Part (Type_Definition (Parent (B))))
9794 then
9795 Ext := Record_Extension_Part (Type_Definition (Parent (B)));
9796
9797 if Present (Ext) then
9798 if Null_Present (Ext) then
9799 return True;
9800 else
9801 Comps := Component_List (Ext);
9802
9803 -- The null component list is rewritten during analysis to
9804 -- include the parent component. Any other component indicates
9805 -- that the extension was not originally null.
9806
9807 return Null_Present (Comps)
9808 or else No (Next (First (Component_Items (Comps))));
9809 end if;
9810 else
9811 return False;
9812 end if;
9813
9814 else
9815 return False;
9816 end if;
9817 end Has_Null_Extension;
9818
9819 -------------------------
9820 -- Has_Null_Refinement --
9821 -------------------------
9822
9823 function Has_Null_Refinement (Id : Entity_Id) return Boolean is
9824 Constits : Elist_Id;
9825
9826 begin
9827 pragma Assert (Ekind (Id) = E_Abstract_State);
9828 Constits := Refinement_Constituents (Id);
9829
9830 -- For a refinement to be null, the state's sole constituent must be a
9831 -- null.
9832
9833 return
9834 Present (Constits)
9835 and then Nkind (Node (First_Elmt (Constits))) = N_Null;
9836 end Has_Null_Refinement;
9837
9838 -------------------------------
9839 -- Has_Overriding_Initialize --
9840 -------------------------------
9841
9842 function Has_Overriding_Initialize (T : Entity_Id) return Boolean is
9843 BT : constant Entity_Id := Base_Type (T);
9844 P : Elmt_Id;
9845
9846 begin
9847 if Is_Controlled (BT) then
9848 if Is_RTU (Scope (BT), Ada_Finalization) then
9849 return False;
9850
9851 elsif Present (Primitive_Operations (BT)) then
9852 P := First_Elmt (Primitive_Operations (BT));
9853 while Present (P) loop
9854 declare
9855 Init : constant Entity_Id := Node (P);
9856 Formal : constant Entity_Id := First_Formal (Init);
9857 begin
9858 if Ekind (Init) = E_Procedure
9859 and then Chars (Init) = Name_Initialize
9860 and then Comes_From_Source (Init)
9861 and then Present (Formal)
9862 and then Etype (Formal) = BT
9863 and then No (Next_Formal (Formal))
9864 and then (Ada_Version < Ada_2012
9865 or else not Null_Present (Parent (Init)))
9866 then
9867 return True;
9868 end if;
9869 end;
9870
9871 Next_Elmt (P);
9872 end loop;
9873 end if;
9874
9875 -- Here if type itself does not have a non-null Initialize operation:
9876 -- check immediate ancestor.
9877
9878 if Is_Derived_Type (BT)
9879 and then Has_Overriding_Initialize (Etype (BT))
9880 then
9881 return True;
9882 end if;
9883 end if;
9884
9885 return False;
9886 end Has_Overriding_Initialize;
9887
9888 --------------------------------------
9889 -- Has_Preelaborable_Initialization --
9890 --------------------------------------
9891
9892 function Has_Preelaborable_Initialization (E : Entity_Id) return Boolean is
9893 Has_PE : Boolean;
9894
9895 procedure Check_Components (E : Entity_Id);
9896 -- Check component/discriminant chain, sets Has_PE False if a component
9897 -- or discriminant does not meet the preelaborable initialization rules.
9898
9899 ----------------------
9900 -- Check_Components --
9901 ----------------------
9902
9903 procedure Check_Components (E : Entity_Id) is
9904 Ent : Entity_Id;
9905 Exp : Node_Id;
9906
9907 function Is_Preelaborable_Expression (N : Node_Id) return Boolean;
9908 -- Returns True if and only if the expression denoted by N does not
9909 -- violate restrictions on preelaborable constructs (RM-10.2.1(5-9)).
9910
9911 ---------------------------------
9912 -- Is_Preelaborable_Expression --
9913 ---------------------------------
9914
9915 function Is_Preelaborable_Expression (N : Node_Id) return Boolean is
9916 Exp : Node_Id;
9917 Assn : Node_Id;
9918 Choice : Node_Id;
9919 Comp_Type : Entity_Id;
9920 Is_Array_Aggr : Boolean;
9921
9922 begin
9923 if Is_OK_Static_Expression (N) then
9924 return True;
9925
9926 elsif Nkind (N) = N_Null then
9927 return True;
9928
9929 -- Attributes are allowed in general, even if their prefix is a
9930 -- formal type. (It seems that certain attributes known not to be
9931 -- static might not be allowed, but there are no rules to prevent
9932 -- them.)
9933
9934 elsif Nkind (N) = N_Attribute_Reference then
9935 return True;
9936
9937 -- The name of a discriminant evaluated within its parent type is
9938 -- defined to be preelaborable (10.2.1(8)). Note that we test for
9939 -- names that denote discriminals as well as discriminants to
9940 -- catch references occurring within init procs.
9941
9942 elsif Is_Entity_Name (N)
9943 and then
9944 (Ekind (Entity (N)) = E_Discriminant
9945 or else (Ekind_In (Entity (N), E_Constant, E_In_Parameter)
9946 and then Present (Discriminal_Link (Entity (N)))))
9947 then
9948 return True;
9949
9950 elsif Nkind (N) = N_Qualified_Expression then
9951 return Is_Preelaborable_Expression (Expression (N));
9952
9953 -- For aggregates we have to check that each of the associations
9954 -- is preelaborable.
9955
9956 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
9957 Is_Array_Aggr := Is_Array_Type (Etype (N));
9958
9959 if Is_Array_Aggr then
9960 Comp_Type := Component_Type (Etype (N));
9961 end if;
9962
9963 -- Check the ancestor part of extension aggregates, which must
9964 -- be either the name of a type that has preelaborable init or
9965 -- an expression that is preelaborable.
9966
9967 if Nkind (N) = N_Extension_Aggregate then
9968 declare
9969 Anc_Part : constant Node_Id := Ancestor_Part (N);
9970
9971 begin
9972 if Is_Entity_Name (Anc_Part)
9973 and then Is_Type (Entity (Anc_Part))
9974 then
9975 if not Has_Preelaborable_Initialization
9976 (Entity (Anc_Part))
9977 then
9978 return False;
9979 end if;
9980
9981 elsif not Is_Preelaborable_Expression (Anc_Part) then
9982 return False;
9983 end if;
9984 end;
9985 end if;
9986
9987 -- Check positional associations
9988
9989 Exp := First (Expressions (N));
9990 while Present (Exp) loop
9991 if not Is_Preelaborable_Expression (Exp) then
9992 return False;
9993 end if;
9994
9995 Next (Exp);
9996 end loop;
9997
9998 -- Check named associations
9999
10000 Assn := First (Component_Associations (N));
10001 while Present (Assn) loop
10002 Choice := First (Choices (Assn));
10003 while Present (Choice) loop
10004 if Is_Array_Aggr then
10005 if Nkind (Choice) = N_Others_Choice then
10006 null;
10007
10008 elsif Nkind (Choice) = N_Range then
10009 if not Is_OK_Static_Range (Choice) then
10010 return False;
10011 end if;
10012
10013 elsif not Is_OK_Static_Expression (Choice) then
10014 return False;
10015 end if;
10016
10017 else
10018 Comp_Type := Etype (Choice);
10019 end if;
10020
10021 Next (Choice);
10022 end loop;
10023
10024 -- If the association has a <> at this point, then we have
10025 -- to check whether the component's type has preelaborable
10026 -- initialization. Note that this only occurs when the
10027 -- association's corresponding component does not have a
10028 -- default expression, the latter case having already been
10029 -- expanded as an expression for the association.
10030
10031 if Box_Present (Assn) then
10032 if not Has_Preelaborable_Initialization (Comp_Type) then
10033 return False;
10034 end if;
10035
10036 -- In the expression case we check whether the expression
10037 -- is preelaborable.
10038
10039 elsif
10040 not Is_Preelaborable_Expression (Expression (Assn))
10041 then
10042 return False;
10043 end if;
10044
10045 Next (Assn);
10046 end loop;
10047
10048 -- If we get here then aggregate as a whole is preelaborable
10049
10050 return True;
10051
10052 -- All other cases are not preelaborable
10053
10054 else
10055 return False;
10056 end if;
10057 end Is_Preelaborable_Expression;
10058
10059 -- Start of processing for Check_Components
10060
10061 begin
10062 -- Loop through entities of record or protected type
10063
10064 Ent := E;
10065 while Present (Ent) loop
10066
10067 -- We are interested only in components and discriminants
10068
10069 Exp := Empty;
10070
10071 case Ekind (Ent) is
10072 when E_Component =>
10073
10074 -- Get default expression if any. If there is no declaration
10075 -- node, it means we have an internal entity. The parent and
10076 -- tag fields are examples of such entities. For such cases,
10077 -- we just test the type of the entity.
10078
10079 if Present (Declaration_Node (Ent)) then
10080 Exp := Expression (Declaration_Node (Ent));
10081 end if;
10082
10083 when E_Discriminant =>
10084
10085 -- Note: for a renamed discriminant, the Declaration_Node
10086 -- may point to the one from the ancestor, and have a
10087 -- different expression, so use the proper attribute to
10088 -- retrieve the expression from the derived constraint.
10089
10090 Exp := Discriminant_Default_Value (Ent);
10091
10092 when others =>
10093 goto Check_Next_Entity;
10094 end case;
10095
10096 -- A component has PI if it has no default expression and the
10097 -- component type has PI.
10098
10099 if No (Exp) then
10100 if not Has_Preelaborable_Initialization (Etype (Ent)) then
10101 Has_PE := False;
10102 exit;
10103 end if;
10104
10105 -- Require the default expression to be preelaborable
10106
10107 elsif not Is_Preelaborable_Expression (Exp) then
10108 Has_PE := False;
10109 exit;
10110 end if;
10111
10112 <<Check_Next_Entity>>
10113 Next_Entity (Ent);
10114 end loop;
10115 end Check_Components;
10116
10117 -- Start of processing for Has_Preelaborable_Initialization
10118
10119 begin
10120 -- Immediate return if already marked as known preelaborable init. This
10121 -- covers types for which this function has already been called once
10122 -- and returned True (in which case the result is cached), and also
10123 -- types to which a pragma Preelaborable_Initialization applies.
10124
10125 if Known_To_Have_Preelab_Init (E) then
10126 return True;
10127 end if;
10128
10129 -- If the type is a subtype representing a generic actual type, then
10130 -- test whether its base type has preelaborable initialization since
10131 -- the subtype representing the actual does not inherit this attribute
10132 -- from the actual or formal. (but maybe it should???)
10133
10134 if Is_Generic_Actual_Type (E) then
10135 return Has_Preelaborable_Initialization (Base_Type (E));
10136 end if;
10137
10138 -- All elementary types have preelaborable initialization
10139
10140 if Is_Elementary_Type (E) then
10141 Has_PE := True;
10142
10143 -- Array types have PI if the component type has PI
10144
10145 elsif Is_Array_Type (E) then
10146 Has_PE := Has_Preelaborable_Initialization (Component_Type (E));
10147
10148 -- A derived type has preelaborable initialization if its parent type
10149 -- has preelaborable initialization and (in the case of a derived record
10150 -- extension) if the non-inherited components all have preelaborable
10151 -- initialization. However, a user-defined controlled type with an
10152 -- overriding Initialize procedure does not have preelaborable
10153 -- initialization.
10154
10155 elsif Is_Derived_Type (E) then
10156
10157 -- If the derived type is a private extension then it doesn't have
10158 -- preelaborable initialization.
10159
10160 if Ekind (Base_Type (E)) = E_Record_Type_With_Private then
10161 return False;
10162 end if;
10163
10164 -- First check whether ancestor type has preelaborable initialization
10165
10166 Has_PE := Has_Preelaborable_Initialization (Etype (Base_Type (E)));
10167
10168 -- If OK, check extension components (if any)
10169
10170 if Has_PE and then Is_Record_Type (E) then
10171 Check_Components (First_Entity (E));
10172 end if;
10173
10174 -- Check specifically for 10.2.1(11.4/2) exception: a controlled type
10175 -- with a user defined Initialize procedure does not have PI. If
10176 -- the type is untagged, the control primitives come from a component
10177 -- that has already been checked.
10178
10179 if Has_PE
10180 and then Is_Controlled (E)
10181 and then Is_Tagged_Type (E)
10182 and then Has_Overriding_Initialize (E)
10183 then
10184 Has_PE := False;
10185 end if;
10186
10187 -- Private types not derived from a type having preelaborable init and
10188 -- that are not marked with pragma Preelaborable_Initialization do not
10189 -- have preelaborable initialization.
10190
10191 elsif Is_Private_Type (E) then
10192 return False;
10193
10194 -- Record type has PI if it is non private and all components have PI
10195
10196 elsif Is_Record_Type (E) then
10197 Has_PE := True;
10198 Check_Components (First_Entity (E));
10199
10200 -- Protected types must not have entries, and components must meet
10201 -- same set of rules as for record components.
10202
10203 elsif Is_Protected_Type (E) then
10204 if Has_Entries (E) then
10205 Has_PE := False;
10206 else
10207 Has_PE := True;
10208 Check_Components (First_Entity (E));
10209 Check_Components (First_Private_Entity (E));
10210 end if;
10211
10212 -- Type System.Address always has preelaborable initialization
10213
10214 elsif Is_RTE (E, RE_Address) then
10215 Has_PE := True;
10216
10217 -- In all other cases, type does not have preelaborable initialization
10218
10219 else
10220 return False;
10221 end if;
10222
10223 -- If type has preelaborable initialization, cache result
10224
10225 if Has_PE then
10226 Set_Known_To_Have_Preelab_Init (E);
10227 end if;
10228
10229 return Has_PE;
10230 end Has_Preelaborable_Initialization;
10231
10232 ---------------------------
10233 -- Has_Private_Component --
10234 ---------------------------
10235
10236 function Has_Private_Component (Type_Id : Entity_Id) return Boolean is
10237 Btype : Entity_Id := Base_Type (Type_Id);
10238 Component : Entity_Id;
10239
10240 begin
10241 if Error_Posted (Type_Id)
10242 or else Error_Posted (Btype)
10243 then
10244 return False;
10245 end if;
10246
10247 if Is_Class_Wide_Type (Btype) then
10248 Btype := Root_Type (Btype);
10249 end if;
10250
10251 if Is_Private_Type (Btype) then
10252 declare
10253 UT : constant Entity_Id := Underlying_Type (Btype);
10254 begin
10255 if No (UT) then
10256 if No (Full_View (Btype)) then
10257 return not Is_Generic_Type (Btype)
10258 and then
10259 not Is_Generic_Type (Root_Type (Btype));
10260 else
10261 return not Is_Generic_Type (Root_Type (Full_View (Btype)));
10262 end if;
10263 else
10264 return not Is_Frozen (UT) and then Has_Private_Component (UT);
10265 end if;
10266 end;
10267
10268 elsif Is_Array_Type (Btype) then
10269 return Has_Private_Component (Component_Type (Btype));
10270
10271 elsif Is_Record_Type (Btype) then
10272 Component := First_Component (Btype);
10273 while Present (Component) loop
10274 if Has_Private_Component (Etype (Component)) then
10275 return True;
10276 end if;
10277
10278 Next_Component (Component);
10279 end loop;
10280
10281 return False;
10282
10283 elsif Is_Protected_Type (Btype)
10284 and then Present (Corresponding_Record_Type (Btype))
10285 then
10286 return Has_Private_Component (Corresponding_Record_Type (Btype));
10287
10288 else
10289 return False;
10290 end if;
10291 end Has_Private_Component;
10292
10293 ----------------------
10294 -- Has_Signed_Zeros --
10295 ----------------------
10296
10297 function Has_Signed_Zeros (E : Entity_Id) return Boolean is
10298 begin
10299 return Is_Floating_Point_Type (E) and then Signed_Zeros_On_Target;
10300 end Has_Signed_Zeros;
10301
10302 ------------------------------
10303 -- Has_Significant_Contract --
10304 ------------------------------
10305
10306 function Has_Significant_Contract (Subp_Id : Entity_Id) return Boolean is
10307 Subp_Nam : constant Name_Id := Chars (Subp_Id);
10308
10309 begin
10310 -- _Finalizer procedure
10311
10312 if Subp_Nam = Name_uFinalizer then
10313 return False;
10314
10315 -- _Postconditions procedure
10316
10317 elsif Subp_Nam = Name_uPostconditions then
10318 return False;
10319
10320 -- Predicate function
10321
10322 elsif Ekind (Subp_Id) = E_Function
10323 and then Is_Predicate_Function (Subp_Id)
10324 then
10325 return False;
10326
10327 -- TSS subprogram
10328
10329 elsif Get_TSS_Name (Subp_Id) /= TSS_Null then
10330 return False;
10331
10332 else
10333 return True;
10334 end if;
10335 end Has_Significant_Contract;
10336
10337 -----------------------------
10338 -- Has_Static_Array_Bounds --
10339 -----------------------------
10340
10341 function Has_Static_Array_Bounds (Typ : Node_Id) return Boolean is
10342 Ndims : constant Nat := Number_Dimensions (Typ);
10343
10344 Index : Node_Id;
10345 Low : Node_Id;
10346 High : Node_Id;
10347
10348 begin
10349 -- Unconstrained types do not have static bounds
10350
10351 if not Is_Constrained (Typ) then
10352 return False;
10353 end if;
10354
10355 -- First treat string literals specially, as the lower bound and length
10356 -- of string literals are not stored like those of arrays.
10357
10358 -- A string literal always has static bounds
10359
10360 if Ekind (Typ) = E_String_Literal_Subtype then
10361 return True;
10362 end if;
10363
10364 -- Treat all dimensions in turn
10365
10366 Index := First_Index (Typ);
10367 for Indx in 1 .. Ndims loop
10368
10369 -- In case of an illegal index which is not a discrete type, return
10370 -- that the type is not static.
10371
10372 if not Is_Discrete_Type (Etype (Index))
10373 or else Etype (Index) = Any_Type
10374 then
10375 return False;
10376 end if;
10377
10378 Get_Index_Bounds (Index, Low, High);
10379
10380 if Error_Posted (Low) or else Error_Posted (High) then
10381 return False;
10382 end if;
10383
10384 if Is_OK_Static_Expression (Low)
10385 and then
10386 Is_OK_Static_Expression (High)
10387 then
10388 null;
10389 else
10390 return False;
10391 end if;
10392
10393 Next (Index);
10394 end loop;
10395
10396 -- If we fall through the loop, all indexes matched
10397
10398 return True;
10399 end Has_Static_Array_Bounds;
10400
10401 ----------------
10402 -- Has_Stream --
10403 ----------------
10404
10405 function Has_Stream (T : Entity_Id) return Boolean is
10406 E : Entity_Id;
10407
10408 begin
10409 if No (T) then
10410 return False;
10411
10412 elsif Is_RTE (Root_Type (T), RE_Root_Stream_Type) then
10413 return True;
10414
10415 elsif Is_Array_Type (T) then
10416 return Has_Stream (Component_Type (T));
10417
10418 elsif Is_Record_Type (T) then
10419 E := First_Component (T);
10420 while Present (E) loop
10421 if Has_Stream (Etype (E)) then
10422 return True;
10423 else
10424 Next_Component (E);
10425 end if;
10426 end loop;
10427
10428 return False;
10429
10430 elsif Is_Private_Type (T) then
10431 return Has_Stream (Underlying_Type (T));
10432
10433 else
10434 return False;
10435 end if;
10436 end Has_Stream;
10437
10438 ----------------
10439 -- Has_Suffix --
10440 ----------------
10441
10442 function Has_Suffix (E : Entity_Id; Suffix : Character) return Boolean is
10443 begin
10444 Get_Name_String (Chars (E));
10445 return Name_Buffer (Name_Len) = Suffix;
10446 end Has_Suffix;
10447
10448 ----------------
10449 -- Add_Suffix --
10450 ----------------
10451
10452 function Add_Suffix (E : Entity_Id; Suffix : Character) return Name_Id is
10453 begin
10454 Get_Name_String (Chars (E));
10455 Add_Char_To_Name_Buffer (Suffix);
10456 return Name_Find;
10457 end Add_Suffix;
10458
10459 -------------------
10460 -- Remove_Suffix --
10461 -------------------
10462
10463 function Remove_Suffix (E : Entity_Id; Suffix : Character) return Name_Id is
10464 begin
10465 pragma Assert (Has_Suffix (E, Suffix));
10466 Get_Name_String (Chars (E));
10467 Name_Len := Name_Len - 1;
10468 return Name_Find;
10469 end Remove_Suffix;
10470
10471 ----------------------------------
10472 -- Replace_Null_By_Null_Address --
10473 ----------------------------------
10474
10475 procedure Replace_Null_By_Null_Address (N : Node_Id) is
10476 procedure Replace_Null_Operand (Op : Node_Id; Other_Op : Node_Id);
10477 -- Replace operand Op with a reference to Null_Address when the operand
10478 -- denotes a null Address. Other_Op denotes the other operand.
10479
10480 --------------------------
10481 -- Replace_Null_Operand --
10482 --------------------------
10483
10484 procedure Replace_Null_Operand (Op : Node_Id; Other_Op : Node_Id) is
10485 begin
10486 -- Check the type of the complementary operand since the N_Null node
10487 -- has not been decorated yet.
10488
10489 if Nkind (Op) = N_Null
10490 and then Is_Descendant_Of_Address (Etype (Other_Op))
10491 then
10492 Rewrite (Op, New_Occurrence_Of (RTE (RE_Null_Address), Sloc (Op)));
10493 end if;
10494 end Replace_Null_Operand;
10495
10496 -- Start of processing for Replace_Null_By_Null_Address
10497
10498 begin
10499 pragma Assert (Relaxed_RM_Semantics);
10500 pragma Assert (Nkind_In (N, N_Null,
10501 N_Op_Eq,
10502 N_Op_Ge,
10503 N_Op_Gt,
10504 N_Op_Le,
10505 N_Op_Lt,
10506 N_Op_Ne));
10507
10508 if Nkind (N) = N_Null then
10509 Rewrite (N, New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
10510
10511 else
10512 declare
10513 L : constant Node_Id := Left_Opnd (N);
10514 R : constant Node_Id := Right_Opnd (N);
10515
10516 begin
10517 Replace_Null_Operand (L, Other_Op => R);
10518 Replace_Null_Operand (R, Other_Op => L);
10519 end;
10520 end if;
10521 end Replace_Null_By_Null_Address;
10522
10523 --------------------------
10524 -- Has_Tagged_Component --
10525 --------------------------
10526
10527 function Has_Tagged_Component (Typ : Entity_Id) return Boolean is
10528 Comp : Entity_Id;
10529
10530 begin
10531 if Is_Private_Type (Typ) and then Present (Underlying_Type (Typ)) then
10532 return Has_Tagged_Component (Underlying_Type (Typ));
10533
10534 elsif Is_Array_Type (Typ) then
10535 return Has_Tagged_Component (Component_Type (Typ));
10536
10537 elsif Is_Tagged_Type (Typ) then
10538 return True;
10539
10540 elsif Is_Record_Type (Typ) then
10541 Comp := First_Component (Typ);
10542 while Present (Comp) loop
10543 if Has_Tagged_Component (Etype (Comp)) then
10544 return True;
10545 end if;
10546
10547 Next_Component (Comp);
10548 end loop;
10549
10550 return False;
10551
10552 else
10553 return False;
10554 end if;
10555 end Has_Tagged_Component;
10556
10557 -----------------------------
10558 -- Has_Undefined_Reference --
10559 -----------------------------
10560
10561 function Has_Undefined_Reference (Expr : Node_Id) return Boolean is
10562 Has_Undef_Ref : Boolean := False;
10563 -- Flag set when expression Expr contains at least one undefined
10564 -- reference.
10565
10566 function Is_Undefined_Reference (N : Node_Id) return Traverse_Result;
10567 -- Determine whether N denotes a reference and if it does, whether it is
10568 -- undefined.
10569
10570 ----------------------------
10571 -- Is_Undefined_Reference --
10572 ----------------------------
10573
10574 function Is_Undefined_Reference (N : Node_Id) return Traverse_Result is
10575 begin
10576 if Is_Entity_Name (N)
10577 and then Present (Entity (N))
10578 and then Entity (N) = Any_Id
10579 then
10580 Has_Undef_Ref := True;
10581 return Abandon;
10582 end if;
10583
10584 return OK;
10585 end Is_Undefined_Reference;
10586
10587 procedure Find_Undefined_References is
10588 new Traverse_Proc (Is_Undefined_Reference);
10589
10590 -- Start of processing for Has_Undefined_Reference
10591
10592 begin
10593 Find_Undefined_References (Expr);
10594
10595 return Has_Undef_Ref;
10596 end Has_Undefined_Reference;
10597
10598 ----------------------------
10599 -- Has_Volatile_Component --
10600 ----------------------------
10601
10602 function Has_Volatile_Component (Typ : Entity_Id) return Boolean is
10603 Comp : Entity_Id;
10604
10605 begin
10606 if Has_Volatile_Components (Typ) then
10607 return True;
10608
10609 elsif Is_Array_Type (Typ) then
10610 return Is_Volatile (Component_Type (Typ));
10611
10612 elsif Is_Record_Type (Typ) then
10613 Comp := First_Component (Typ);
10614 while Present (Comp) loop
10615 if Is_Volatile_Object (Comp) then
10616 return True;
10617 end if;
10618
10619 Comp := Next_Component (Comp);
10620 end loop;
10621 end if;
10622
10623 return False;
10624 end Has_Volatile_Component;
10625
10626 -------------------------
10627 -- Implementation_Kind --
10628 -------------------------
10629
10630 function Implementation_Kind (Subp : Entity_Id) return Name_Id is
10631 Impl_Prag : constant Node_Id := Get_Rep_Pragma (Subp, Name_Implemented);
10632 Arg : Node_Id;
10633 begin
10634 pragma Assert (Present (Impl_Prag));
10635 Arg := Last (Pragma_Argument_Associations (Impl_Prag));
10636 return Chars (Get_Pragma_Arg (Arg));
10637 end Implementation_Kind;
10638
10639 --------------------------
10640 -- Implements_Interface --
10641 --------------------------
10642
10643 function Implements_Interface
10644 (Typ_Ent : Entity_Id;
10645 Iface_Ent : Entity_Id;
10646 Exclude_Parents : Boolean := False) return Boolean
10647 is
10648 Ifaces_List : Elist_Id;
10649 Elmt : Elmt_Id;
10650 Iface : Entity_Id := Base_Type (Iface_Ent);
10651 Typ : Entity_Id := Base_Type (Typ_Ent);
10652
10653 begin
10654 if Is_Class_Wide_Type (Typ) then
10655 Typ := Root_Type (Typ);
10656 end if;
10657
10658 if not Has_Interfaces (Typ) then
10659 return False;
10660 end if;
10661
10662 if Is_Class_Wide_Type (Iface) then
10663 Iface := Root_Type (Iface);
10664 end if;
10665
10666 Collect_Interfaces (Typ, Ifaces_List);
10667
10668 Elmt := First_Elmt (Ifaces_List);
10669 while Present (Elmt) loop
10670 if Is_Ancestor (Node (Elmt), Typ, Use_Full_View => True)
10671 and then Exclude_Parents
10672 then
10673 null;
10674
10675 elsif Node (Elmt) = Iface then
10676 return True;
10677 end if;
10678
10679 Next_Elmt (Elmt);
10680 end loop;
10681
10682 return False;
10683 end Implements_Interface;
10684
10685 ------------------------------------
10686 -- In_Assertion_Expression_Pragma --
10687 ------------------------------------
10688
10689 function In_Assertion_Expression_Pragma (N : Node_Id) return Boolean is
10690 Par : Node_Id;
10691 Prag : Node_Id := Empty;
10692
10693 begin
10694 -- Climb the parent chain looking for an enclosing pragma
10695
10696 Par := N;
10697 while Present (Par) loop
10698 if Nkind (Par) = N_Pragma then
10699 Prag := Par;
10700 exit;
10701
10702 -- Precondition-like pragmas are expanded into if statements, check
10703 -- the original node instead.
10704
10705 elsif Nkind (Original_Node (Par)) = N_Pragma then
10706 Prag := Original_Node (Par);
10707 exit;
10708
10709 -- The expansion of attribute 'Old generates a constant to capture
10710 -- the result of the prefix. If the parent traversal reaches
10711 -- one of these constants, then the node technically came from a
10712 -- postcondition-like pragma. Note that the Ekind is not tested here
10713 -- because N may be the expression of an object declaration which is
10714 -- currently being analyzed. Such objects carry Ekind of E_Void.
10715
10716 elsif Nkind (Par) = N_Object_Declaration
10717 and then Constant_Present (Par)
10718 and then Stores_Attribute_Old_Prefix (Defining_Entity (Par))
10719 then
10720 return True;
10721
10722 -- Prevent the search from going too far
10723
10724 elsif Is_Body_Or_Package_Declaration (Par) then
10725 return False;
10726 end if;
10727
10728 Par := Parent (Par);
10729 end loop;
10730
10731 return
10732 Present (Prag)
10733 and then Assertion_Expression_Pragma (Get_Pragma_Id (Prag));
10734 end In_Assertion_Expression_Pragma;
10735
10736 -----------------
10737 -- In_Instance --
10738 -----------------
10739
10740 function In_Instance return Boolean is
10741 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
10742 S : Entity_Id;
10743
10744 begin
10745 S := Current_Scope;
10746 while Present (S) and then S /= Standard_Standard loop
10747 if Ekind_In (S, E_Function, E_Package, E_Procedure)
10748 and then Is_Generic_Instance (S)
10749 then
10750 -- A child instance is always compiled in the context of a parent
10751 -- instance. Nevertheless, the actuals are not analyzed in an
10752 -- instance context. We detect this case by examining the current
10753 -- compilation unit, which must be a child instance, and checking
10754 -- that it is not currently on the scope stack.
10755
10756 if Is_Child_Unit (Curr_Unit)
10757 and then Nkind (Unit (Cunit (Current_Sem_Unit))) =
10758 N_Package_Instantiation
10759 and then not In_Open_Scopes (Curr_Unit)
10760 then
10761 return False;
10762 else
10763 return True;
10764 end if;
10765 end if;
10766
10767 S := Scope (S);
10768 end loop;
10769
10770 return False;
10771 end In_Instance;
10772
10773 ----------------------
10774 -- In_Instance_Body --
10775 ----------------------
10776
10777 function In_Instance_Body return Boolean is
10778 S : Entity_Id;
10779
10780 begin
10781 S := Current_Scope;
10782 while Present (S) and then S /= Standard_Standard loop
10783 if Ekind_In (S, E_Function, E_Procedure)
10784 and then Is_Generic_Instance (S)
10785 then
10786 return True;
10787
10788 elsif Ekind (S) = E_Package
10789 and then In_Package_Body (S)
10790 and then Is_Generic_Instance (S)
10791 then
10792 return True;
10793 end if;
10794
10795 S := Scope (S);
10796 end loop;
10797
10798 return False;
10799 end In_Instance_Body;
10800
10801 -----------------------------
10802 -- In_Instance_Not_Visible --
10803 -----------------------------
10804
10805 function In_Instance_Not_Visible return Boolean is
10806 S : Entity_Id;
10807
10808 begin
10809 S := Current_Scope;
10810 while Present (S) and then S /= Standard_Standard loop
10811 if Ekind_In (S, E_Function, E_Procedure)
10812 and then Is_Generic_Instance (S)
10813 then
10814 return True;
10815
10816 elsif Ekind (S) = E_Package
10817 and then (In_Package_Body (S) or else In_Private_Part (S))
10818 and then Is_Generic_Instance (S)
10819 then
10820 return True;
10821 end if;
10822
10823 S := Scope (S);
10824 end loop;
10825
10826 return False;
10827 end In_Instance_Not_Visible;
10828
10829 ------------------------------
10830 -- In_Instance_Visible_Part --
10831 ------------------------------
10832
10833 function In_Instance_Visible_Part return Boolean is
10834 S : Entity_Id;
10835
10836 begin
10837 S := Current_Scope;
10838 while Present (S) and then S /= Standard_Standard loop
10839 if Ekind (S) = E_Package
10840 and then Is_Generic_Instance (S)
10841 and then not In_Package_Body (S)
10842 and then not In_Private_Part (S)
10843 then
10844 return True;
10845 end if;
10846
10847 S := Scope (S);
10848 end loop;
10849
10850 return False;
10851 end In_Instance_Visible_Part;
10852
10853 ---------------------
10854 -- In_Package_Body --
10855 ---------------------
10856
10857 function In_Package_Body return Boolean is
10858 S : Entity_Id;
10859
10860 begin
10861 S := Current_Scope;
10862 while Present (S) and then S /= Standard_Standard loop
10863 if Ekind (S) = E_Package and then In_Package_Body (S) then
10864 return True;
10865 else
10866 S := Scope (S);
10867 end if;
10868 end loop;
10869
10870 return False;
10871 end In_Package_Body;
10872
10873 --------------------------------
10874 -- In_Parameter_Specification --
10875 --------------------------------
10876
10877 function In_Parameter_Specification (N : Node_Id) return Boolean is
10878 PN : Node_Id;
10879
10880 begin
10881 PN := Parent (N);
10882 while Present (PN) loop
10883 if Nkind (PN) = N_Parameter_Specification then
10884 return True;
10885 end if;
10886
10887 PN := Parent (PN);
10888 end loop;
10889
10890 return False;
10891 end In_Parameter_Specification;
10892
10893 --------------------------
10894 -- In_Pragma_Expression --
10895 --------------------------
10896
10897 function In_Pragma_Expression (N : Node_Id; Nam : Name_Id) return Boolean is
10898 P : Node_Id;
10899 begin
10900 P := Parent (N);
10901 loop
10902 if No (P) then
10903 return False;
10904 elsif Nkind (P) = N_Pragma and then Pragma_Name (P) = Nam then
10905 return True;
10906 else
10907 P := Parent (P);
10908 end if;
10909 end loop;
10910 end In_Pragma_Expression;
10911
10912 ---------------------------
10913 -- In_Pre_Post_Condition --
10914 ---------------------------
10915
10916 function In_Pre_Post_Condition (N : Node_Id) return Boolean is
10917 Par : Node_Id;
10918 Prag : Node_Id := Empty;
10919 Prag_Id : Pragma_Id;
10920
10921 begin
10922 -- Climb the parent chain looking for an enclosing pragma
10923
10924 Par := N;
10925 while Present (Par) loop
10926 if Nkind (Par) = N_Pragma then
10927 Prag := Par;
10928 exit;
10929
10930 -- Prevent the search from going too far
10931
10932 elsif Is_Body_Or_Package_Declaration (Par) then
10933 exit;
10934 end if;
10935
10936 Par := Parent (Par);
10937 end loop;
10938
10939 if Present (Prag) then
10940 Prag_Id := Get_Pragma_Id (Prag);
10941
10942 return
10943 Prag_Id = Pragma_Post
10944 or else Prag_Id = Pragma_Post_Class
10945 or else Prag_Id = Pragma_Postcondition
10946 or else Prag_Id = Pragma_Pre
10947 or else Prag_Id = Pragma_Pre_Class
10948 or else Prag_Id = Pragma_Precondition;
10949
10950 -- Otherwise the node is not enclosed by a pre/postcondition pragma
10951
10952 else
10953 return False;
10954 end if;
10955 end In_Pre_Post_Condition;
10956
10957 -------------------------------------
10958 -- In_Reverse_Storage_Order_Object --
10959 -------------------------------------
10960
10961 function In_Reverse_Storage_Order_Object (N : Node_Id) return Boolean is
10962 Pref : Node_Id;
10963 Btyp : Entity_Id := Empty;
10964
10965 begin
10966 -- Climb up indexed components
10967
10968 Pref := N;
10969 loop
10970 case Nkind (Pref) is
10971 when N_Selected_Component =>
10972 Pref := Prefix (Pref);
10973 exit;
10974
10975 when N_Indexed_Component =>
10976 Pref := Prefix (Pref);
10977
10978 when others =>
10979 Pref := Empty;
10980 exit;
10981 end case;
10982 end loop;
10983
10984 if Present (Pref) then
10985 Btyp := Base_Type (Etype (Pref));
10986 end if;
10987
10988 return Present (Btyp)
10989 and then (Is_Record_Type (Btyp) or else Is_Array_Type (Btyp))
10990 and then Reverse_Storage_Order (Btyp);
10991 end In_Reverse_Storage_Order_Object;
10992
10993 --------------------------------------
10994 -- In_Subprogram_Or_Concurrent_Unit --
10995 --------------------------------------
10996
10997 function In_Subprogram_Or_Concurrent_Unit return Boolean is
10998 E : Entity_Id;
10999 K : Entity_Kind;
11000
11001 begin
11002 -- Use scope chain to check successively outer scopes
11003
11004 E := Current_Scope;
11005 loop
11006 K := Ekind (E);
11007
11008 if K in Subprogram_Kind
11009 or else K in Concurrent_Kind
11010 or else K in Generic_Subprogram_Kind
11011 then
11012 return True;
11013
11014 elsif E = Standard_Standard then
11015 return False;
11016 end if;
11017
11018 E := Scope (E);
11019 end loop;
11020 end In_Subprogram_Or_Concurrent_Unit;
11021
11022 ---------------------
11023 -- In_Visible_Part --
11024 ---------------------
11025
11026 function In_Visible_Part (Scope_Id : Entity_Id) return Boolean is
11027 begin
11028 return Is_Package_Or_Generic_Package (Scope_Id)
11029 and then In_Open_Scopes (Scope_Id)
11030 and then not In_Package_Body (Scope_Id)
11031 and then not In_Private_Part (Scope_Id);
11032 end In_Visible_Part;
11033
11034 --------------------------------
11035 -- Incomplete_Or_Partial_View --
11036 --------------------------------
11037
11038 function Incomplete_Or_Partial_View (Id : Entity_Id) return Entity_Id is
11039 function Inspect_Decls
11040 (Decls : List_Id;
11041 Taft : Boolean := False) return Entity_Id;
11042 -- Check whether a declarative region contains the incomplete or partial
11043 -- view of Id.
11044
11045 -------------------
11046 -- Inspect_Decls --
11047 -------------------
11048
11049 function Inspect_Decls
11050 (Decls : List_Id;
11051 Taft : Boolean := False) return Entity_Id
11052 is
11053 Decl : Node_Id;
11054 Match : Node_Id;
11055
11056 begin
11057 Decl := First (Decls);
11058 while Present (Decl) loop
11059 Match := Empty;
11060
11061 -- The partial view of a Taft-amendment type is an incomplete
11062 -- type.
11063
11064 if Taft then
11065 if Nkind (Decl) = N_Incomplete_Type_Declaration then
11066 Match := Defining_Identifier (Decl);
11067 end if;
11068
11069 -- Otherwise look for a private type whose full view matches the
11070 -- input type. Note that this checks full_type_declaration nodes
11071 -- to account for derivations from a private type where the type
11072 -- declaration hold the partial view and the full view is an
11073 -- itype.
11074
11075 elsif Nkind_In (Decl, N_Full_Type_Declaration,
11076 N_Private_Extension_Declaration,
11077 N_Private_Type_Declaration)
11078 then
11079 Match := Defining_Identifier (Decl);
11080 end if;
11081
11082 -- Guard against unanalyzed entities
11083
11084 if Present (Match)
11085 and then Is_Type (Match)
11086 and then Present (Full_View (Match))
11087 and then Full_View (Match) = Id
11088 then
11089 return Match;
11090 end if;
11091
11092 Next (Decl);
11093 end loop;
11094
11095 return Empty;
11096 end Inspect_Decls;
11097
11098 -- Local variables
11099
11100 Prev : Entity_Id;
11101
11102 -- Start of processing for Incomplete_Or_Partial_View
11103
11104 begin
11105 -- Deferred constant or incomplete type case
11106
11107 Prev := Current_Entity_In_Scope (Id);
11108
11109 if Present (Prev)
11110 and then (Is_Incomplete_Type (Prev) or else Ekind (Prev) = E_Constant)
11111 and then Present (Full_View (Prev))
11112 and then Full_View (Prev) = Id
11113 then
11114 return Prev;
11115 end if;
11116
11117 -- Private or Taft amendment type case
11118
11119 declare
11120 Pkg : constant Entity_Id := Scope (Id);
11121 Pkg_Decl : Node_Id := Pkg;
11122
11123 begin
11124 if Present (Pkg)
11125 and then Ekind_In (Pkg, E_Generic_Package, E_Package)
11126 then
11127 while Nkind (Pkg_Decl) /= N_Package_Specification loop
11128 Pkg_Decl := Parent (Pkg_Decl);
11129 end loop;
11130
11131 -- It is knows that Typ has a private view, look for it in the
11132 -- visible declarations of the enclosing scope. A special case
11133 -- of this is when the two views have been exchanged - the full
11134 -- appears earlier than the private.
11135
11136 if Has_Private_Declaration (Id) then
11137 Prev := Inspect_Decls (Visible_Declarations (Pkg_Decl));
11138
11139 -- Exchanged view case, look in the private declarations
11140
11141 if No (Prev) then
11142 Prev := Inspect_Decls (Private_Declarations (Pkg_Decl));
11143 end if;
11144
11145 return Prev;
11146
11147 -- Otherwise if this is the package body, then Typ is a potential
11148 -- Taft amendment type. The incomplete view should be located in
11149 -- the private declarations of the enclosing scope.
11150
11151 elsif In_Package_Body (Pkg) then
11152 return Inspect_Decls (Private_Declarations (Pkg_Decl), True);
11153 end if;
11154 end if;
11155 end;
11156
11157 -- The type has no incomplete or private view
11158
11159 return Empty;
11160 end Incomplete_Or_Partial_View;
11161
11162 ----------------------------------
11163 -- Indexed_Component_Bit_Offset --
11164 ----------------------------------
11165
11166 function Indexed_Component_Bit_Offset (N : Node_Id) return Uint is
11167 Exp : constant Node_Id := First (Expressions (N));
11168 Typ : constant Entity_Id := Etype (Prefix (N));
11169 Off : constant Uint := Component_Size (Typ);
11170 Ind : Node_Id;
11171
11172 begin
11173 -- Return early if the component size is not known or variable
11174
11175 if Off = No_Uint or else Off < Uint_0 then
11176 return No_Uint;
11177 end if;
11178
11179 -- Deal with the degenerate case of an empty component
11180
11181 if Off = Uint_0 then
11182 return Off;
11183 end if;
11184
11185 -- Check that both the index value and the low bound are known
11186
11187 if not Compile_Time_Known_Value (Exp) then
11188 return No_Uint;
11189 end if;
11190
11191 Ind := First_Index (Typ);
11192 if No (Ind) then
11193 return No_Uint;
11194 end if;
11195
11196 if Nkind (Ind) = N_Subtype_Indication then
11197 Ind := Constraint (Ind);
11198
11199 if Nkind (Ind) = N_Range_Constraint then
11200 Ind := Range_Expression (Ind);
11201 end if;
11202 end if;
11203
11204 if Nkind (Ind) /= N_Range
11205 or else not Compile_Time_Known_Value (Low_Bound (Ind))
11206 then
11207 return No_Uint;
11208 end if;
11209
11210 -- Return the scaled offset
11211
11212 return Off * (Expr_Value (Exp) - Expr_Value (Low_Bound ((Ind))));
11213 end Indexed_Component_Bit_Offset;
11214
11215 -----------------------------------------
11216 -- Inherit_Default_Init_Cond_Procedure --
11217 -----------------------------------------
11218
11219 procedure Inherit_Default_Init_Cond_Procedure (Typ : Entity_Id) is
11220 Par_Typ : constant Entity_Id := Etype (Typ);
11221
11222 begin
11223 -- A derived type inherits the default initial condition procedure of
11224 -- its parent type.
11225
11226 if No (Default_Init_Cond_Procedure (Typ)) then
11227 Set_Default_Init_Cond_Procedure
11228 (Typ, Default_Init_Cond_Procedure (Par_Typ));
11229 end if;
11230 end Inherit_Default_Init_Cond_Procedure;
11231
11232 ----------------------------
11233 -- Inherit_Rep_Item_Chain --
11234 ----------------------------
11235
11236 procedure Inherit_Rep_Item_Chain (Typ : Entity_Id; From_Typ : Entity_Id) is
11237 Item : Node_Id;
11238 Next_Item : Node_Id;
11239
11240 begin
11241 -- There are several inheritance scenarios to consider depending on
11242 -- whether both types have rep item chains and whether the destination
11243 -- type already inherits part of the source type's rep item chain.
11244
11245 -- 1) The source type lacks a rep item chain
11246 -- From_Typ ---> Empty
11247 --
11248 -- Typ --------> Item (or Empty)
11249
11250 -- In this case inheritance cannot take place because there are no items
11251 -- to inherit.
11252
11253 -- 2) The destination type lacks a rep item chain
11254 -- From_Typ ---> Item ---> ...
11255 --
11256 -- Typ --------> Empty
11257
11258 -- Inheritance takes place by setting the First_Rep_Item of the
11259 -- destination type to the First_Rep_Item of the source type.
11260 -- From_Typ ---> Item ---> ...
11261 -- ^
11262 -- Typ -----------+
11263
11264 -- 3.1) Both source and destination types have at least one rep item.
11265 -- The destination type does NOT inherit a rep item from the source
11266 -- type.
11267 -- From_Typ ---> Item ---> Item
11268 --
11269 -- Typ --------> Item ---> Item
11270
11271 -- Inheritance takes place by setting the Next_Rep_Item of the last item
11272 -- of the destination type to the First_Rep_Item of the source type.
11273 -- From_Typ -------------------> Item ---> Item
11274 -- ^
11275 -- Typ --------> Item ---> Item --+
11276
11277 -- 3.2) Both source and destination types have at least one rep item.
11278 -- The destination type DOES inherit part of the rep item chain of the
11279 -- source type.
11280 -- From_Typ ---> Item ---> Item ---> Item
11281 -- ^
11282 -- Typ --------> Item ------+
11283
11284 -- This rare case arises when the full view of a private extension must
11285 -- inherit the rep item chain from the full view of its parent type and
11286 -- the full view of the parent type contains extra rep items. Currently
11287 -- only invariants may lead to such form of inheritance.
11288
11289 -- type From_Typ is tagged private
11290 -- with Type_Invariant'Class => Item_2;
11291
11292 -- type Typ is new From_Typ with private
11293 -- with Type_Invariant => Item_4;
11294
11295 -- At this point the rep item chains contain the following items
11296
11297 -- From_Typ -----------> Item_2 ---> Item_3
11298 -- ^
11299 -- Typ --------> Item_4 --+
11300
11301 -- The full views of both types may introduce extra invariants
11302
11303 -- type From_Typ is tagged null record
11304 -- with Type_Invariant => Item_1;
11305
11306 -- type Typ is new From_Typ with null record;
11307
11308 -- The full view of Typ would have to inherit any new rep items added to
11309 -- the full view of From_Typ.
11310
11311 -- From_Typ -----------> Item_1 ---> Item_2 ---> Item_3
11312 -- ^
11313 -- Typ --------> Item_4 --+
11314
11315 -- To achieve this form of inheritance, the destination type must first
11316 -- sever the link between its own rep chain and that of the source type,
11317 -- then inheritance 3.1 takes place.
11318
11319 -- Case 1: The source type lacks a rep item chain
11320
11321 if No (First_Rep_Item (From_Typ)) then
11322 return;
11323
11324 -- Case 2: The destination type lacks a rep item chain
11325
11326 elsif No (First_Rep_Item (Typ)) then
11327 Set_First_Rep_Item (Typ, First_Rep_Item (From_Typ));
11328
11329 -- Case 3: Both the source and destination types have at least one rep
11330 -- item. Traverse the rep item chain of the destination type to find the
11331 -- last rep item.
11332
11333 else
11334 Item := Empty;
11335 Next_Item := First_Rep_Item (Typ);
11336 while Present (Next_Item) loop
11337
11338 -- Detect a link between the destination type's rep chain and that
11339 -- of the source type. There are two possibilities:
11340
11341 -- Variant 1
11342 -- Next_Item
11343 -- V
11344 -- From_Typ ---> Item_1 --->
11345 -- ^
11346 -- Typ -----------+
11347 --
11348 -- Item is Empty
11349
11350 -- Variant 2
11351 -- Next_Item
11352 -- V
11353 -- From_Typ ---> Item_1 ---> Item_2 --->
11354 -- ^
11355 -- Typ --------> Item_3 ------+
11356 -- ^
11357 -- Item
11358
11359 if Has_Rep_Item (From_Typ, Next_Item) then
11360 exit;
11361 end if;
11362
11363 Item := Next_Item;
11364 Next_Item := Next_Rep_Item (Next_Item);
11365 end loop;
11366
11367 -- Inherit the source type's rep item chain
11368
11369 if Present (Item) then
11370 Set_Next_Rep_Item (Item, First_Rep_Item (From_Typ));
11371 else
11372 Set_First_Rep_Item (Typ, First_Rep_Item (From_Typ));
11373 end if;
11374 end if;
11375 end Inherit_Rep_Item_Chain;
11376
11377 ---------------------------------
11378 -- Insert_Explicit_Dereference --
11379 ---------------------------------
11380
11381 procedure Insert_Explicit_Dereference (N : Node_Id) is
11382 New_Prefix : constant Node_Id := Relocate_Node (N);
11383 Ent : Entity_Id := Empty;
11384 Pref : Node_Id;
11385 I : Interp_Index;
11386 It : Interp;
11387 T : Entity_Id;
11388
11389 begin
11390 Save_Interps (N, New_Prefix);
11391
11392 Rewrite (N,
11393 Make_Explicit_Dereference (Sloc (Parent (N)),
11394 Prefix => New_Prefix));
11395
11396 Set_Etype (N, Designated_Type (Etype (New_Prefix)));
11397
11398 if Is_Overloaded (New_Prefix) then
11399
11400 -- The dereference is also overloaded, and its interpretations are
11401 -- the designated types of the interpretations of the original node.
11402
11403 Set_Etype (N, Any_Type);
11404
11405 Get_First_Interp (New_Prefix, I, It);
11406 while Present (It.Nam) loop
11407 T := It.Typ;
11408
11409 if Is_Access_Type (T) then
11410 Add_One_Interp (N, Designated_Type (T), Designated_Type (T));
11411 end if;
11412
11413 Get_Next_Interp (I, It);
11414 end loop;
11415
11416 End_Interp_List;
11417
11418 else
11419 -- Prefix is unambiguous: mark the original prefix (which might
11420 -- Come_From_Source) as a reference, since the new (relocated) one
11421 -- won't be taken into account.
11422
11423 if Is_Entity_Name (New_Prefix) then
11424 Ent := Entity (New_Prefix);
11425 Pref := New_Prefix;
11426
11427 -- For a retrieval of a subcomponent of some composite object,
11428 -- retrieve the ultimate entity if there is one.
11429
11430 elsif Nkind_In (New_Prefix, N_Selected_Component,
11431 N_Indexed_Component)
11432 then
11433 Pref := Prefix (New_Prefix);
11434 while Present (Pref)
11435 and then Nkind_In (Pref, N_Selected_Component,
11436 N_Indexed_Component)
11437 loop
11438 Pref := Prefix (Pref);
11439 end loop;
11440
11441 if Present (Pref) and then Is_Entity_Name (Pref) then
11442 Ent := Entity (Pref);
11443 end if;
11444 end if;
11445
11446 -- Place the reference on the entity node
11447
11448 if Present (Ent) then
11449 Generate_Reference (Ent, Pref);
11450 end if;
11451 end if;
11452 end Insert_Explicit_Dereference;
11453
11454 ------------------------------------------
11455 -- Inspect_Deferred_Constant_Completion --
11456 ------------------------------------------
11457
11458 procedure Inspect_Deferred_Constant_Completion (Decls : List_Id) is
11459 Decl : Node_Id;
11460
11461 begin
11462 Decl := First (Decls);
11463 while Present (Decl) loop
11464
11465 -- Deferred constant signature
11466
11467 if Nkind (Decl) = N_Object_Declaration
11468 and then Constant_Present (Decl)
11469 and then No (Expression (Decl))
11470
11471 -- No need to check internally generated constants
11472
11473 and then Comes_From_Source (Decl)
11474
11475 -- The constant is not completed. A full object declaration or a
11476 -- pragma Import complete a deferred constant.
11477
11478 and then not Has_Completion (Defining_Identifier (Decl))
11479 then
11480 Error_Msg_N
11481 ("constant declaration requires initialization expression",
11482 Defining_Identifier (Decl));
11483 end if;
11484
11485 Decl := Next (Decl);
11486 end loop;
11487 end Inspect_Deferred_Constant_Completion;
11488
11489 -----------------------------
11490 -- Install_Generic_Formals --
11491 -----------------------------
11492
11493 procedure Install_Generic_Formals (Subp_Id : Entity_Id) is
11494 E : Entity_Id;
11495
11496 begin
11497 pragma Assert (Is_Generic_Subprogram (Subp_Id));
11498
11499 E := First_Entity (Subp_Id);
11500 while Present (E) loop
11501 Install_Entity (E);
11502 Next_Entity (E);
11503 end loop;
11504 end Install_Generic_Formals;
11505
11506 -----------------------------
11507 -- Is_Actual_Out_Parameter --
11508 -----------------------------
11509
11510 function Is_Actual_Out_Parameter (N : Node_Id) return Boolean is
11511 Formal : Entity_Id;
11512 Call : Node_Id;
11513 begin
11514 Find_Actual (N, Formal, Call);
11515 return Present (Formal) and then Ekind (Formal) = E_Out_Parameter;
11516 end Is_Actual_Out_Parameter;
11517
11518 -------------------------
11519 -- Is_Actual_Parameter --
11520 -------------------------
11521
11522 function Is_Actual_Parameter (N : Node_Id) return Boolean is
11523 PK : constant Node_Kind := Nkind (Parent (N));
11524
11525 begin
11526 case PK is
11527 when N_Parameter_Association =>
11528 return N = Explicit_Actual_Parameter (Parent (N));
11529
11530 when N_Subprogram_Call =>
11531 return Is_List_Member (N)
11532 and then
11533 List_Containing (N) = Parameter_Associations (Parent (N));
11534
11535 when others =>
11536 return False;
11537 end case;
11538 end Is_Actual_Parameter;
11539
11540 --------------------------------
11541 -- Is_Actual_Tagged_Parameter --
11542 --------------------------------
11543
11544 function Is_Actual_Tagged_Parameter (N : Node_Id) return Boolean is
11545 Formal : Entity_Id;
11546 Call : Node_Id;
11547 begin
11548 Find_Actual (N, Formal, Call);
11549 return Present (Formal) and then Is_Tagged_Type (Etype (Formal));
11550 end Is_Actual_Tagged_Parameter;
11551
11552 ---------------------
11553 -- Is_Aliased_View --
11554 ---------------------
11555
11556 function Is_Aliased_View (Obj : Node_Id) return Boolean is
11557 E : Entity_Id;
11558
11559 begin
11560 if Is_Entity_Name (Obj) then
11561 E := Entity (Obj);
11562
11563 return
11564 (Is_Object (E)
11565 and then
11566 (Is_Aliased (E)
11567 or else (Present (Renamed_Object (E))
11568 and then Is_Aliased_View (Renamed_Object (E)))))
11569
11570 or else ((Is_Formal (E)
11571 or else Ekind_In (E, E_Generic_In_Out_Parameter,
11572 E_Generic_In_Parameter))
11573 and then Is_Tagged_Type (Etype (E)))
11574
11575 or else (Is_Concurrent_Type (E) and then In_Open_Scopes (E))
11576
11577 -- Current instance of type, either directly or as rewritten
11578 -- reference to the current object.
11579
11580 or else (Is_Entity_Name (Original_Node (Obj))
11581 and then Present (Entity (Original_Node (Obj)))
11582 and then Is_Type (Entity (Original_Node (Obj))))
11583
11584 or else (Is_Type (E) and then E = Current_Scope)
11585
11586 or else (Is_Incomplete_Or_Private_Type (E)
11587 and then Full_View (E) = Current_Scope)
11588
11589 -- Ada 2012 AI05-0053: the return object of an extended return
11590 -- statement is aliased if its type is immutably limited.
11591
11592 or else (Is_Return_Object (E)
11593 and then Is_Limited_View (Etype (E)));
11594
11595 elsif Nkind (Obj) = N_Selected_Component then
11596 return Is_Aliased (Entity (Selector_Name (Obj)));
11597
11598 elsif Nkind (Obj) = N_Indexed_Component then
11599 return Has_Aliased_Components (Etype (Prefix (Obj)))
11600 or else
11601 (Is_Access_Type (Etype (Prefix (Obj)))
11602 and then Has_Aliased_Components
11603 (Designated_Type (Etype (Prefix (Obj)))));
11604
11605 elsif Nkind_In (Obj, N_Unchecked_Type_Conversion, N_Type_Conversion) then
11606 return Is_Tagged_Type (Etype (Obj))
11607 and then Is_Aliased_View (Expression (Obj));
11608
11609 elsif Nkind (Obj) = N_Explicit_Dereference then
11610 return Nkind (Original_Node (Obj)) /= N_Function_Call;
11611
11612 else
11613 return False;
11614 end if;
11615 end Is_Aliased_View;
11616
11617 -------------------------
11618 -- Is_Ancestor_Package --
11619 -------------------------
11620
11621 function Is_Ancestor_Package
11622 (E1 : Entity_Id;
11623 E2 : Entity_Id) return Boolean
11624 is
11625 Par : Entity_Id;
11626
11627 begin
11628 Par := E2;
11629 while Present (Par) and then Par /= Standard_Standard loop
11630 if Par = E1 then
11631 return True;
11632 end if;
11633
11634 Par := Scope (Par);
11635 end loop;
11636
11637 return False;
11638 end Is_Ancestor_Package;
11639
11640 ----------------------
11641 -- Is_Atomic_Object --
11642 ----------------------
11643
11644 function Is_Atomic_Object (N : Node_Id) return Boolean is
11645
11646 function Object_Has_Atomic_Components (N : Node_Id) return Boolean;
11647 -- Determines if given object has atomic components
11648
11649 function Is_Atomic_Prefix (N : Node_Id) return Boolean;
11650 -- If prefix is an implicit dereference, examine designated type
11651
11652 ----------------------
11653 -- Is_Atomic_Prefix --
11654 ----------------------
11655
11656 function Is_Atomic_Prefix (N : Node_Id) return Boolean is
11657 begin
11658 if Is_Access_Type (Etype (N)) then
11659 return
11660 Has_Atomic_Components (Designated_Type (Etype (N)));
11661 else
11662 return Object_Has_Atomic_Components (N);
11663 end if;
11664 end Is_Atomic_Prefix;
11665
11666 ----------------------------------
11667 -- Object_Has_Atomic_Components --
11668 ----------------------------------
11669
11670 function Object_Has_Atomic_Components (N : Node_Id) return Boolean is
11671 begin
11672 if Has_Atomic_Components (Etype (N))
11673 or else Is_Atomic (Etype (N))
11674 then
11675 return True;
11676
11677 elsif Is_Entity_Name (N)
11678 and then (Has_Atomic_Components (Entity (N))
11679 or else Is_Atomic (Entity (N)))
11680 then
11681 return True;
11682
11683 elsif Nkind (N) = N_Selected_Component
11684 and then Is_Atomic (Entity (Selector_Name (N)))
11685 then
11686 return True;
11687
11688 elsif Nkind (N) = N_Indexed_Component
11689 or else Nkind (N) = N_Selected_Component
11690 then
11691 return Is_Atomic_Prefix (Prefix (N));
11692
11693 else
11694 return False;
11695 end if;
11696 end Object_Has_Atomic_Components;
11697
11698 -- Start of processing for Is_Atomic_Object
11699
11700 begin
11701 -- Predicate is not relevant to subprograms
11702
11703 if Is_Entity_Name (N) and then Is_Overloadable (Entity (N)) then
11704 return False;
11705
11706 elsif Is_Atomic (Etype (N))
11707 or else (Is_Entity_Name (N) and then Is_Atomic (Entity (N)))
11708 then
11709 return True;
11710
11711 elsif Nkind (N) = N_Selected_Component
11712 and then Is_Atomic (Entity (Selector_Name (N)))
11713 then
11714 return True;
11715
11716 elsif Nkind (N) = N_Indexed_Component
11717 or else Nkind (N) = N_Selected_Component
11718 then
11719 return Is_Atomic_Prefix (Prefix (N));
11720
11721 else
11722 return False;
11723 end if;
11724 end Is_Atomic_Object;
11725
11726 -----------------------------
11727 -- Is_Atomic_Or_VFA_Object --
11728 -----------------------------
11729
11730 function Is_Atomic_Or_VFA_Object (N : Node_Id) return Boolean is
11731 begin
11732 return Is_Atomic_Object (N)
11733 or else (Is_Object_Reference (N)
11734 and then Is_Entity_Name (N)
11735 and then (Is_Volatile_Full_Access (Entity (N))
11736 or else
11737 Is_Volatile_Full_Access (Etype (Entity (N)))));
11738 end Is_Atomic_Or_VFA_Object;
11739
11740 -------------------------
11741 -- Is_Attribute_Result --
11742 -------------------------
11743
11744 function Is_Attribute_Result (N : Node_Id) return Boolean is
11745 begin
11746 return Nkind (N) = N_Attribute_Reference
11747 and then Attribute_Name (N) = Name_Result;
11748 end Is_Attribute_Result;
11749
11750 -------------------------
11751 -- Is_Attribute_Update --
11752 -------------------------
11753
11754 function Is_Attribute_Update (N : Node_Id) return Boolean is
11755 begin
11756 return Nkind (N) = N_Attribute_Reference
11757 and then Attribute_Name (N) = Name_Update;
11758 end Is_Attribute_Update;
11759
11760 ------------------------------------
11761 -- Is_Body_Or_Package_Declaration --
11762 ------------------------------------
11763
11764 function Is_Body_Or_Package_Declaration (N : Node_Id) return Boolean is
11765 begin
11766 return Nkind_In (N, N_Entry_Body,
11767 N_Package_Body,
11768 N_Package_Declaration,
11769 N_Protected_Body,
11770 N_Subprogram_Body,
11771 N_Task_Body);
11772 end Is_Body_Or_Package_Declaration;
11773
11774 -----------------------
11775 -- Is_Bounded_String --
11776 -----------------------
11777
11778 function Is_Bounded_String (T : Entity_Id) return Boolean is
11779 Under : constant Entity_Id := Underlying_Type (Root_Type (T));
11780
11781 begin
11782 -- Check whether T is ultimately derived from Ada.Strings.Superbounded.
11783 -- Super_String, or one of the [Wide_]Wide_ versions. This will
11784 -- be True for all the Bounded_String types in instances of the
11785 -- Generic_Bounded_Length generics, and for types derived from those.
11786
11787 return Present (Under)
11788 and then (Is_RTE (Root_Type (Under), RO_SU_Super_String) or else
11789 Is_RTE (Root_Type (Under), RO_WI_Super_String) or else
11790 Is_RTE (Root_Type (Under), RO_WW_Super_String));
11791 end Is_Bounded_String;
11792
11793 -------------------------
11794 -- Is_Child_Or_Sibling --
11795 -------------------------
11796
11797 function Is_Child_Or_Sibling
11798 (Pack_1 : Entity_Id;
11799 Pack_2 : Entity_Id) return Boolean
11800 is
11801 function Distance_From_Standard (Pack : Entity_Id) return Nat;
11802 -- Given an arbitrary package, return the number of "climbs" necessary
11803 -- to reach scope Standard_Standard.
11804
11805 procedure Equalize_Depths
11806 (Pack : in out Entity_Id;
11807 Depth : in out Nat;
11808 Depth_To_Reach : Nat);
11809 -- Given an arbitrary package, its depth and a target depth to reach,
11810 -- climb the scope chain until the said depth is reached. The pointer
11811 -- to the package and its depth a modified during the climb.
11812
11813 ----------------------------
11814 -- Distance_From_Standard --
11815 ----------------------------
11816
11817 function Distance_From_Standard (Pack : Entity_Id) return Nat is
11818 Dist : Nat;
11819 Scop : Entity_Id;
11820
11821 begin
11822 Dist := 0;
11823 Scop := Pack;
11824 while Present (Scop) and then Scop /= Standard_Standard loop
11825 Dist := Dist + 1;
11826 Scop := Scope (Scop);
11827 end loop;
11828
11829 return Dist;
11830 end Distance_From_Standard;
11831
11832 ---------------------
11833 -- Equalize_Depths --
11834 ---------------------
11835
11836 procedure Equalize_Depths
11837 (Pack : in out Entity_Id;
11838 Depth : in out Nat;
11839 Depth_To_Reach : Nat)
11840 is
11841 begin
11842 -- The package must be at a greater or equal depth
11843
11844 if Depth < Depth_To_Reach then
11845 raise Program_Error;
11846 end if;
11847
11848 -- Climb the scope chain until the desired depth is reached
11849
11850 while Present (Pack) and then Depth /= Depth_To_Reach loop
11851 Pack := Scope (Pack);
11852 Depth := Depth - 1;
11853 end loop;
11854 end Equalize_Depths;
11855
11856 -- Local variables
11857
11858 P_1 : Entity_Id := Pack_1;
11859 P_1_Child : Boolean := False;
11860 P_1_Depth : Nat := Distance_From_Standard (P_1);
11861 P_2 : Entity_Id := Pack_2;
11862 P_2_Child : Boolean := False;
11863 P_2_Depth : Nat := Distance_From_Standard (P_2);
11864
11865 -- Start of processing for Is_Child_Or_Sibling
11866
11867 begin
11868 pragma Assert
11869 (Ekind (Pack_1) = E_Package and then Ekind (Pack_2) = E_Package);
11870
11871 -- Both packages denote the same entity, therefore they cannot be
11872 -- children or siblings.
11873
11874 if P_1 = P_2 then
11875 return False;
11876
11877 -- One of the packages is at a deeper level than the other. Note that
11878 -- both may still come from differen hierarchies.
11879
11880 -- (root) P_2
11881 -- / \ :
11882 -- X P_2 or X
11883 -- : :
11884 -- P_1 P_1
11885
11886 elsif P_1_Depth > P_2_Depth then
11887 Equalize_Depths
11888 (Pack => P_1,
11889 Depth => P_1_Depth,
11890 Depth_To_Reach => P_2_Depth);
11891 P_1_Child := True;
11892
11893 -- (root) P_1
11894 -- / \ :
11895 -- P_1 X or X
11896 -- : :
11897 -- P_2 P_2
11898
11899 elsif P_2_Depth > P_1_Depth then
11900 Equalize_Depths
11901 (Pack => P_2,
11902 Depth => P_2_Depth,
11903 Depth_To_Reach => P_1_Depth);
11904 P_2_Child := True;
11905 end if;
11906
11907 -- At this stage the package pointers have been elevated to the same
11908 -- depth. If the related entities are the same, then one package is a
11909 -- potential child of the other:
11910
11911 -- P_1
11912 -- :
11913 -- X became P_1 P_2 or vica versa
11914 -- :
11915 -- P_2
11916
11917 if P_1 = P_2 then
11918 if P_1_Child then
11919 return Is_Child_Unit (Pack_1);
11920
11921 else pragma Assert (P_2_Child);
11922 return Is_Child_Unit (Pack_2);
11923 end if;
11924
11925 -- The packages may come from the same package chain or from entirely
11926 -- different hierarcies. To determine this, climb the scope stack until
11927 -- a common root is found.
11928
11929 -- (root) (root 1) (root 2)
11930 -- / \ | |
11931 -- P_1 P_2 P_1 P_2
11932
11933 else
11934 while Present (P_1) and then Present (P_2) loop
11935
11936 -- The two packages may be siblings
11937
11938 if P_1 = P_2 then
11939 return Is_Child_Unit (Pack_1) and then Is_Child_Unit (Pack_2);
11940 end if;
11941
11942 P_1 := Scope (P_1);
11943 P_2 := Scope (P_2);
11944 end loop;
11945 end if;
11946
11947 return False;
11948 end Is_Child_Or_Sibling;
11949
11950 -----------------------------
11951 -- Is_Concurrent_Interface --
11952 -----------------------------
11953
11954 function Is_Concurrent_Interface (T : Entity_Id) return Boolean is
11955 begin
11956 return Is_Interface (T)
11957 and then
11958 (Is_Protected_Interface (T)
11959 or else Is_Synchronized_Interface (T)
11960 or else Is_Task_Interface (T));
11961 end Is_Concurrent_Interface;
11962
11963 -----------------------
11964 -- Is_Constant_Bound --
11965 -----------------------
11966
11967 function Is_Constant_Bound (Exp : Node_Id) return Boolean is
11968 begin
11969 if Compile_Time_Known_Value (Exp) then
11970 return True;
11971
11972 elsif Is_Entity_Name (Exp) and then Present (Entity (Exp)) then
11973 return Is_Constant_Object (Entity (Exp))
11974 or else Ekind (Entity (Exp)) = E_Enumeration_Literal;
11975
11976 elsif Nkind (Exp) in N_Binary_Op then
11977 return Is_Constant_Bound (Left_Opnd (Exp))
11978 and then Is_Constant_Bound (Right_Opnd (Exp))
11979 and then Scope (Entity (Exp)) = Standard_Standard;
11980
11981 else
11982 return False;
11983 end if;
11984 end Is_Constant_Bound;
11985
11986 ---------------------------
11987 -- Is_Container_Element --
11988 ---------------------------
11989
11990 function Is_Container_Element (Exp : Node_Id) return Boolean is
11991 Loc : constant Source_Ptr := Sloc (Exp);
11992 Pref : constant Node_Id := Prefix (Exp);
11993
11994 Call : Node_Id;
11995 -- Call to an indexing aspect
11996
11997 Cont_Typ : Entity_Id;
11998 -- The type of the container being accessed
11999
12000 Elem_Typ : Entity_Id;
12001 -- Its element type
12002
12003 Indexing : Entity_Id;
12004 Is_Const : Boolean;
12005 -- Indicates that constant indexing is used, and the element is thus
12006 -- a constant.
12007
12008 Ref_Typ : Entity_Id;
12009 -- The reference type returned by the indexing operation
12010
12011 begin
12012 -- If C is a container, in a context that imposes the element type of
12013 -- that container, the indexing notation C (X) is rewritten as:
12014
12015 -- Indexing (C, X).Discr.all
12016
12017 -- where Indexing is one of the indexing aspects of the container.
12018 -- If the context does not require a reference, the construct can be
12019 -- rewritten as
12020
12021 -- Element (C, X)
12022
12023 -- First, verify that the construct has the proper form
12024
12025 if not Expander_Active then
12026 return False;
12027
12028 elsif Nkind (Pref) /= N_Selected_Component then
12029 return False;
12030
12031 elsif Nkind (Prefix (Pref)) /= N_Function_Call then
12032 return False;
12033
12034 else
12035 Call := Prefix (Pref);
12036 Ref_Typ := Etype (Call);
12037 end if;
12038
12039 if not Has_Implicit_Dereference (Ref_Typ)
12040 or else No (First (Parameter_Associations (Call)))
12041 or else not Is_Entity_Name (Name (Call))
12042 then
12043 return False;
12044 end if;
12045
12046 -- Retrieve type of container object, and its iterator aspects
12047
12048 Cont_Typ := Etype (First (Parameter_Associations (Call)));
12049 Indexing := Find_Value_Of_Aspect (Cont_Typ, Aspect_Constant_Indexing);
12050 Is_Const := False;
12051
12052 if No (Indexing) then
12053
12054 -- Container should have at least one indexing operation
12055
12056 return False;
12057
12058 elsif Entity (Name (Call)) /= Entity (Indexing) then
12059
12060 -- This may be a variable indexing operation
12061
12062 Indexing := Find_Value_Of_Aspect (Cont_Typ, Aspect_Variable_Indexing);
12063
12064 if No (Indexing)
12065 or else Entity (Name (Call)) /= Entity (Indexing)
12066 then
12067 return False;
12068 end if;
12069
12070 else
12071 Is_Const := True;
12072 end if;
12073
12074 Elem_Typ := Find_Value_Of_Aspect (Cont_Typ, Aspect_Iterator_Element);
12075
12076 if No (Elem_Typ) or else Entity (Elem_Typ) /= Etype (Exp) then
12077 return False;
12078 end if;
12079
12080 -- Check that the expression is not the target of an assignment, in
12081 -- which case the rewriting is not possible.
12082
12083 if not Is_Const then
12084 declare
12085 Par : Node_Id;
12086
12087 begin
12088 Par := Exp;
12089 while Present (Par)
12090 loop
12091 if Nkind (Parent (Par)) = N_Assignment_Statement
12092 and then Par = Name (Parent (Par))
12093 then
12094 return False;
12095
12096 -- A renaming produces a reference, and the transformation
12097 -- does not apply.
12098
12099 elsif Nkind (Parent (Par)) = N_Object_Renaming_Declaration then
12100 return False;
12101
12102 elsif Nkind_In
12103 (Nkind (Parent (Par)), N_Function_Call,
12104 N_Procedure_Call_Statement,
12105 N_Entry_Call_Statement)
12106 then
12107 -- Check that the element is not part of an actual for an
12108 -- in-out parameter.
12109
12110 declare
12111 F : Entity_Id;
12112 A : Node_Id;
12113
12114 begin
12115 F := First_Formal (Entity (Name (Parent (Par))));
12116 A := First (Parameter_Associations (Parent (Par)));
12117 while Present (F) loop
12118 if A = Par and then Ekind (F) /= E_In_Parameter then
12119 return False;
12120 end if;
12121
12122 Next_Formal (F);
12123 Next (A);
12124 end loop;
12125 end;
12126
12127 -- E_In_Parameter in a call: element is not modified.
12128
12129 exit;
12130 end if;
12131
12132 Par := Parent (Par);
12133 end loop;
12134 end;
12135 end if;
12136
12137 -- The expression has the proper form and the context requires the
12138 -- element type. Retrieve the Element function of the container and
12139 -- rewrite the construct as a call to it.
12140
12141 declare
12142 Op : Elmt_Id;
12143
12144 begin
12145 Op := First_Elmt (Primitive_Operations (Cont_Typ));
12146 while Present (Op) loop
12147 exit when Chars (Node (Op)) = Name_Element;
12148 Next_Elmt (Op);
12149 end loop;
12150
12151 if No (Op) then
12152 return False;
12153
12154 else
12155 Rewrite (Exp,
12156 Make_Function_Call (Loc,
12157 Name => New_Occurrence_Of (Node (Op), Loc),
12158 Parameter_Associations => Parameter_Associations (Call)));
12159 Analyze_And_Resolve (Exp, Entity (Elem_Typ));
12160 return True;
12161 end if;
12162 end;
12163 end Is_Container_Element;
12164
12165 ----------------------------
12166 -- Is_Contract_Annotation --
12167 ----------------------------
12168
12169 function Is_Contract_Annotation (Item : Node_Id) return Boolean is
12170 begin
12171 return Is_Package_Contract_Annotation (Item)
12172 or else
12173 Is_Subprogram_Contract_Annotation (Item);
12174 end Is_Contract_Annotation;
12175
12176 --------------------------------------
12177 -- Is_Controlling_Limited_Procedure --
12178 --------------------------------------
12179
12180 function Is_Controlling_Limited_Procedure
12181 (Proc_Nam : Entity_Id) return Boolean
12182 is
12183 Param_Typ : Entity_Id := Empty;
12184
12185 begin
12186 if Ekind (Proc_Nam) = E_Procedure
12187 and then Present (Parameter_Specifications (Parent (Proc_Nam)))
12188 then
12189 Param_Typ := Etype (Parameter_Type (First (
12190 Parameter_Specifications (Parent (Proc_Nam)))));
12191
12192 -- In this case where an Itype was created, the procedure call has been
12193 -- rewritten.
12194
12195 elsif Present (Associated_Node_For_Itype (Proc_Nam))
12196 and then Present (Original_Node (Associated_Node_For_Itype (Proc_Nam)))
12197 and then
12198 Present (Parameter_Associations
12199 (Associated_Node_For_Itype (Proc_Nam)))
12200 then
12201 Param_Typ :=
12202 Etype (First (Parameter_Associations
12203 (Associated_Node_For_Itype (Proc_Nam))));
12204 end if;
12205
12206 if Present (Param_Typ) then
12207 return
12208 Is_Interface (Param_Typ)
12209 and then Is_Limited_Record (Param_Typ);
12210 end if;
12211
12212 return False;
12213 end Is_Controlling_Limited_Procedure;
12214
12215 -----------------------------
12216 -- Is_CPP_Constructor_Call --
12217 -----------------------------
12218
12219 function Is_CPP_Constructor_Call (N : Node_Id) return Boolean is
12220 begin
12221 return Nkind (N) = N_Function_Call
12222 and then Is_CPP_Class (Etype (Etype (N)))
12223 and then Is_Constructor (Entity (Name (N)))
12224 and then Is_Imported (Entity (Name (N)));
12225 end Is_CPP_Constructor_Call;
12226
12227 -------------------------
12228 -- Is_Current_Instance --
12229 -------------------------
12230
12231 function Is_Current_Instance (N : Node_Id) return Boolean is
12232 Typ : constant Entity_Id := Entity (N);
12233 P : Node_Id;
12234
12235 begin
12236 -- Simplest case: entity is a concurrent type and we are currently
12237 -- inside the body. This will eventually be expanded into a
12238 -- call to Self (for tasks) or _object (for protected objects).
12239
12240 if Is_Concurrent_Type (Typ) and then In_Open_Scopes (Typ) then
12241 return True;
12242
12243 else
12244 -- Check whether the context is a (sub)type declaration for the
12245 -- type entity.
12246
12247 P := Parent (N);
12248 while Present (P) loop
12249 if Nkind_In (P, N_Full_Type_Declaration,
12250 N_Private_Type_Declaration,
12251 N_Subtype_Declaration)
12252 and then Comes_From_Source (P)
12253 and then Defining_Entity (P) = Typ
12254 then
12255 return True;
12256
12257 -- A subtype name may appear in an aspect specification for a
12258 -- Predicate_Failure aspect, for which we do not construct a
12259 -- wrapper procedure. The subtype will be replaced by the
12260 -- expression being tested when the corresponding predicate
12261 -- check is expanded.
12262
12263 elsif Nkind (P) = N_Aspect_Specification
12264 and then Nkind (Parent (P)) = N_Subtype_Declaration
12265 then
12266 return True;
12267
12268 elsif Nkind (P) = N_Pragma
12269 and then
12270 Get_Pragma_Id (Pragma_Name (P)) = Pragma_Predicate_Failure
12271 then
12272 return True;
12273 end if;
12274
12275 P := Parent (P);
12276 end loop;
12277 end if;
12278
12279 -- In any other context this is not a current occurrence
12280
12281 return False;
12282 end Is_Current_Instance;
12283
12284 --------------------
12285 -- Is_Declaration --
12286 --------------------
12287
12288 function Is_Declaration (N : Node_Id) return Boolean is
12289 begin
12290 case Nkind (N) is
12291 when N_Abstract_Subprogram_Declaration |
12292 N_Exception_Declaration |
12293 N_Exception_Renaming_Declaration |
12294 N_Full_Type_Declaration |
12295 N_Generic_Function_Renaming_Declaration |
12296 N_Generic_Package_Declaration |
12297 N_Generic_Package_Renaming_Declaration |
12298 N_Generic_Procedure_Renaming_Declaration |
12299 N_Generic_Subprogram_Declaration |
12300 N_Number_Declaration |
12301 N_Object_Declaration |
12302 N_Object_Renaming_Declaration |
12303 N_Package_Declaration |
12304 N_Package_Renaming_Declaration |
12305 N_Private_Extension_Declaration |
12306 N_Private_Type_Declaration |
12307 N_Subprogram_Declaration |
12308 N_Subprogram_Renaming_Declaration |
12309 N_Subtype_Declaration =>
12310 return True;
12311
12312 when others =>
12313 return False;
12314 end case;
12315 end Is_Declaration;
12316
12317 --------------------------------
12318 -- Is_Declared_Within_Variant --
12319 --------------------------------
12320
12321 function Is_Declared_Within_Variant (Comp : Entity_Id) return Boolean is
12322 Comp_Decl : constant Node_Id := Parent (Comp);
12323 Comp_List : constant Node_Id := Parent (Comp_Decl);
12324 begin
12325 return Nkind (Parent (Comp_List)) = N_Variant;
12326 end Is_Declared_Within_Variant;
12327
12328 ----------------------------------------------
12329 -- Is_Dependent_Component_Of_Mutable_Object --
12330 ----------------------------------------------
12331
12332 function Is_Dependent_Component_Of_Mutable_Object
12333 (Object : Node_Id) return Boolean
12334 is
12335 P : Node_Id;
12336 Prefix_Type : Entity_Id;
12337 P_Aliased : Boolean := False;
12338 Comp : Entity_Id;
12339
12340 Deref : Node_Id := Object;
12341 -- Dereference node, in something like X.all.Y(2)
12342
12343 -- Start of processing for Is_Dependent_Component_Of_Mutable_Object
12344
12345 begin
12346 -- Find the dereference node if any
12347
12348 while Nkind_In (Deref, N_Indexed_Component,
12349 N_Selected_Component,
12350 N_Slice)
12351 loop
12352 Deref := Prefix (Deref);
12353 end loop;
12354
12355 -- Ada 2005: If we have a component or slice of a dereference,
12356 -- something like X.all.Y (2), and the type of X is access-to-constant,
12357 -- Is_Variable will return False, because it is indeed a constant
12358 -- view. But it might be a view of a variable object, so we want the
12359 -- following condition to be True in that case.
12360
12361 if Is_Variable (Object)
12362 or else (Ada_Version >= Ada_2005
12363 and then Nkind (Deref) = N_Explicit_Dereference)
12364 then
12365 if Nkind (Object) = N_Selected_Component then
12366 P := Prefix (Object);
12367 Prefix_Type := Etype (P);
12368
12369 if Is_Entity_Name (P) then
12370 if Ekind (Entity (P)) = E_Generic_In_Out_Parameter then
12371 Prefix_Type := Base_Type (Prefix_Type);
12372 end if;
12373
12374 if Is_Aliased (Entity (P)) then
12375 P_Aliased := True;
12376 end if;
12377
12378 -- A discriminant check on a selected component may be expanded
12379 -- into a dereference when removing side-effects. Recover the
12380 -- original node and its type, which may be unconstrained.
12381
12382 elsif Nkind (P) = N_Explicit_Dereference
12383 and then not (Comes_From_Source (P))
12384 then
12385 P := Original_Node (P);
12386 Prefix_Type := Etype (P);
12387
12388 else
12389 -- Check for prefix being an aliased component???
12390
12391 null;
12392
12393 end if;
12394
12395 -- A heap object is constrained by its initial value
12396
12397 -- Ada 2005 (AI-363): Always assume the object could be mutable in
12398 -- the dereferenced case, since the access value might denote an
12399 -- unconstrained aliased object, whereas in Ada 95 the designated
12400 -- object is guaranteed to be constrained. A worst-case assumption
12401 -- has to apply in Ada 2005 because we can't tell at compile
12402 -- time whether the object is "constrained by its initial value"
12403 -- (despite the fact that 3.10.2(26/2) and 8.5.1(5/2) are semantic
12404 -- rules (these rules are acknowledged to need fixing).
12405
12406 if Ada_Version < Ada_2005 then
12407 if Is_Access_Type (Prefix_Type)
12408 or else Nkind (P) = N_Explicit_Dereference
12409 then
12410 return False;
12411 end if;
12412
12413 else pragma Assert (Ada_Version >= Ada_2005);
12414 if Is_Access_Type (Prefix_Type) then
12415
12416 -- If the access type is pool-specific, and there is no
12417 -- constrained partial view of the designated type, then the
12418 -- designated object is known to be constrained.
12419
12420 if Ekind (Prefix_Type) = E_Access_Type
12421 and then not Object_Type_Has_Constrained_Partial_View
12422 (Typ => Designated_Type (Prefix_Type),
12423 Scop => Current_Scope)
12424 then
12425 return False;
12426
12427 -- Otherwise (general access type, or there is a constrained
12428 -- partial view of the designated type), we need to check
12429 -- based on the designated type.
12430
12431 else
12432 Prefix_Type := Designated_Type (Prefix_Type);
12433 end if;
12434 end if;
12435 end if;
12436
12437 Comp :=
12438 Original_Record_Component (Entity (Selector_Name (Object)));
12439
12440 -- As per AI-0017, the renaming is illegal in a generic body, even
12441 -- if the subtype is indefinite.
12442
12443 -- Ada 2005 (AI-363): In Ada 2005 an aliased object can be mutable
12444
12445 if not Is_Constrained (Prefix_Type)
12446 and then (Is_Definite_Subtype (Prefix_Type)
12447 or else
12448 (Is_Generic_Type (Prefix_Type)
12449 and then Ekind (Current_Scope) = E_Generic_Package
12450 and then In_Package_Body (Current_Scope)))
12451
12452 and then (Is_Declared_Within_Variant (Comp)
12453 or else Has_Discriminant_Dependent_Constraint (Comp))
12454 and then (not P_Aliased or else Ada_Version >= Ada_2005)
12455 then
12456 return True;
12457
12458 -- If the prefix is of an access type at this point, then we want
12459 -- to return False, rather than calling this function recursively
12460 -- on the access object (which itself might be a discriminant-
12461 -- dependent component of some other object, but that isn't
12462 -- relevant to checking the object passed to us). This avoids
12463 -- issuing wrong errors when compiling with -gnatc, where there
12464 -- can be implicit dereferences that have not been expanded.
12465
12466 elsif Is_Access_Type (Etype (Prefix (Object))) then
12467 return False;
12468
12469 else
12470 return
12471 Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
12472 end if;
12473
12474 elsif Nkind (Object) = N_Indexed_Component
12475 or else Nkind (Object) = N_Slice
12476 then
12477 return Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
12478
12479 -- A type conversion that Is_Variable is a view conversion:
12480 -- go back to the denoted object.
12481
12482 elsif Nkind (Object) = N_Type_Conversion then
12483 return
12484 Is_Dependent_Component_Of_Mutable_Object (Expression (Object));
12485 end if;
12486 end if;
12487
12488 return False;
12489 end Is_Dependent_Component_Of_Mutable_Object;
12490
12491 ---------------------
12492 -- Is_Dereferenced --
12493 ---------------------
12494
12495 function Is_Dereferenced (N : Node_Id) return Boolean is
12496 P : constant Node_Id := Parent (N);
12497 begin
12498 return Nkind_In (P, N_Selected_Component,
12499 N_Explicit_Dereference,
12500 N_Indexed_Component,
12501 N_Slice)
12502 and then Prefix (P) = N;
12503 end Is_Dereferenced;
12504
12505 ----------------------
12506 -- Is_Descendant_Of --
12507 ----------------------
12508
12509 function Is_Descendant_Of (T1 : Entity_Id; T2 : Entity_Id) return Boolean is
12510 T : Entity_Id;
12511 Etyp : Entity_Id;
12512
12513 begin
12514 pragma Assert (Nkind (T1) in N_Entity);
12515 pragma Assert (Nkind (T2) in N_Entity);
12516
12517 T := Base_Type (T1);
12518
12519 -- Immediate return if the types match
12520
12521 if T = T2 then
12522 return True;
12523
12524 -- Comment needed here ???
12525
12526 elsif Ekind (T) = E_Class_Wide_Type then
12527 return Etype (T) = T2;
12528
12529 -- All other cases
12530
12531 else
12532 loop
12533 Etyp := Etype (T);
12534
12535 -- Done if we found the type we are looking for
12536
12537 if Etyp = T2 then
12538 return True;
12539
12540 -- Done if no more derivations to check
12541
12542 elsif T = T1
12543 or else T = Etyp
12544 then
12545 return False;
12546
12547 -- Following test catches error cases resulting from prev errors
12548
12549 elsif No (Etyp) then
12550 return False;
12551
12552 elsif Is_Private_Type (T) and then Etyp = Full_View (T) then
12553 return False;
12554
12555 elsif Is_Private_Type (Etyp) and then Full_View (Etyp) = T then
12556 return False;
12557 end if;
12558
12559 T := Base_Type (Etyp);
12560 end loop;
12561 end if;
12562 end Is_Descendant_Of;
12563
12564 ----------------------------------------
12565 -- Is_Descendant_Of_Suspension_Object --
12566 ----------------------------------------
12567
12568 function Is_Descendant_Of_Suspension_Object
12569 (Typ : Entity_Id) return Boolean
12570 is
12571 Cur_Typ : Entity_Id;
12572 Par_Typ : Entity_Id;
12573
12574 begin
12575 -- Climb the type derivation chain checking each parent type against
12576 -- Suspension_Object.
12577
12578 Cur_Typ := Base_Type (Typ);
12579 while Present (Cur_Typ) loop
12580 Par_Typ := Etype (Cur_Typ);
12581
12582 -- The current type is a match
12583
12584 if Is_Suspension_Object (Cur_Typ) then
12585 return True;
12586
12587 -- Stop the traversal once the root of the derivation chain has been
12588 -- reached. In that case the current type is its own base type.
12589
12590 elsif Cur_Typ = Par_Typ then
12591 exit;
12592 end if;
12593
12594 Cur_Typ := Base_Type (Par_Typ);
12595 end loop;
12596
12597 return False;
12598 end Is_Descendant_Of_Suspension_Object;
12599
12600 ---------------------------------------------
12601 -- Is_Double_Precision_Floating_Point_Type --
12602 ---------------------------------------------
12603
12604 function Is_Double_Precision_Floating_Point_Type
12605 (E : Entity_Id) return Boolean is
12606 begin
12607 return Is_Floating_Point_Type (E)
12608 and then Machine_Radix_Value (E) = Uint_2
12609 and then Machine_Mantissa_Value (E) = UI_From_Int (53)
12610 and then Machine_Emax_Value (E) = Uint_2 ** Uint_10
12611 and then Machine_Emin_Value (E) = Uint_3 - (Uint_2 ** Uint_10);
12612 end Is_Double_Precision_Floating_Point_Type;
12613
12614 -----------------------------
12615 -- Is_Effectively_Volatile --
12616 -----------------------------
12617
12618 function Is_Effectively_Volatile (Id : Entity_Id) return Boolean is
12619 begin
12620 if Is_Type (Id) then
12621
12622 -- An arbitrary type is effectively volatile when it is subject to
12623 -- pragma Atomic or Volatile.
12624
12625 if Is_Volatile (Id) then
12626 return True;
12627
12628 -- An array type is effectively volatile when it is subject to pragma
12629 -- Atomic_Components or Volatile_Components or its compolent type is
12630 -- effectively volatile.
12631
12632 elsif Is_Array_Type (Id) then
12633 return
12634 Has_Volatile_Components (Id)
12635 or else
12636 Is_Effectively_Volatile (Component_Type (Base_Type (Id)));
12637
12638 -- A protected type is always volatile
12639
12640 elsif Is_Protected_Type (Id) then
12641 return True;
12642
12643 -- A descendant of Ada.Synchronous_Task_Control.Suspension_Object is
12644 -- automatically volatile.
12645
12646 elsif Is_Descendant_Of_Suspension_Object (Id) then
12647 return True;
12648
12649 -- Otherwise the type is not effectively volatile
12650
12651 else
12652 return False;
12653 end if;
12654
12655 -- Otherwise Id denotes an object
12656
12657 else
12658 return
12659 Is_Volatile (Id)
12660 or else Has_Volatile_Components (Id)
12661 or else Is_Effectively_Volatile (Etype (Id));
12662 end if;
12663 end Is_Effectively_Volatile;
12664
12665 ------------------------------------
12666 -- Is_Effectively_Volatile_Object --
12667 ------------------------------------
12668
12669 function Is_Effectively_Volatile_Object (N : Node_Id) return Boolean is
12670 begin
12671 if Is_Entity_Name (N) then
12672 return Is_Effectively_Volatile (Entity (N));
12673
12674 elsif Nkind (N) = N_Indexed_Component then
12675 return Is_Effectively_Volatile_Object (Prefix (N));
12676
12677 elsif Nkind (N) = N_Selected_Component then
12678 return
12679 Is_Effectively_Volatile_Object (Prefix (N))
12680 or else
12681 Is_Effectively_Volatile_Object (Selector_Name (N));
12682
12683 else
12684 return False;
12685 end if;
12686 end Is_Effectively_Volatile_Object;
12687
12688 -------------------
12689 -- Is_Entry_Body --
12690 -------------------
12691
12692 function Is_Entry_Body (Id : Entity_Id) return Boolean is
12693 begin
12694 return
12695 Ekind_In (Id, E_Entry, E_Entry_Family)
12696 and then Nkind (Unit_Declaration_Node (Id)) = N_Entry_Body;
12697 end Is_Entry_Body;
12698
12699 --------------------------
12700 -- Is_Entry_Declaration --
12701 --------------------------
12702
12703 function Is_Entry_Declaration (Id : Entity_Id) return Boolean is
12704 begin
12705 return
12706 Ekind_In (Id, E_Entry, E_Entry_Family)
12707 and then Nkind (Unit_Declaration_Node (Id)) = N_Entry_Declaration;
12708 end Is_Entry_Declaration;
12709
12710 ------------------------------------
12711 -- Is_Expanded_Priority_Attribute --
12712 ------------------------------------
12713
12714 function Is_Expanded_Priority_Attribute (E : Entity_Id) return Boolean is
12715 begin
12716 return
12717 Nkind (E) = N_Function_Call
12718 and then not Configurable_Run_Time_Mode
12719 and then (Entity (Name (E)) = RTE (RE_Get_Ceiling)
12720 or else Entity (Name (E)) = RTE (RO_PE_Get_Ceiling));
12721 end Is_Expanded_Priority_Attribute;
12722
12723 ----------------------------
12724 -- Is_Expression_Function --
12725 ----------------------------
12726
12727 function Is_Expression_Function (Subp : Entity_Id) return Boolean is
12728 begin
12729 if Ekind_In (Subp, E_Function, E_Subprogram_Body) then
12730 return
12731 Nkind (Original_Node (Unit_Declaration_Node (Subp))) =
12732 N_Expression_Function;
12733 else
12734 return False;
12735 end if;
12736 end Is_Expression_Function;
12737
12738 ------------------------------------------
12739 -- Is_Expression_Function_Or_Completion --
12740 ------------------------------------------
12741
12742 function Is_Expression_Function_Or_Completion
12743 (Subp : Entity_Id) return Boolean
12744 is
12745 Subp_Decl : Node_Id;
12746
12747 begin
12748 if Ekind (Subp) = E_Function then
12749 Subp_Decl := Unit_Declaration_Node (Subp);
12750
12751 -- The function declaration is either an expression function or is
12752 -- completed by an expression function body.
12753
12754 return
12755 Is_Expression_Function (Subp)
12756 or else (Nkind (Subp_Decl) = N_Subprogram_Declaration
12757 and then Present (Corresponding_Body (Subp_Decl))
12758 and then Is_Expression_Function
12759 (Corresponding_Body (Subp_Decl)));
12760
12761 elsif Ekind (Subp) = E_Subprogram_Body then
12762 return Is_Expression_Function (Subp);
12763
12764 else
12765 return False;
12766 end if;
12767 end Is_Expression_Function_Or_Completion;
12768
12769 -----------------------
12770 -- Is_EVF_Expression --
12771 -----------------------
12772
12773 function Is_EVF_Expression (N : Node_Id) return Boolean is
12774 Orig_N : constant Node_Id := Original_Node (N);
12775 Alt : Node_Id;
12776 Expr : Node_Id;
12777 Id : Entity_Id;
12778
12779 begin
12780 -- Detect a reference to a formal parameter of a specific tagged type
12781 -- whose related subprogram is subject to pragma Expresions_Visible with
12782 -- value "False".
12783
12784 if Is_Entity_Name (N) and then Present (Entity (N)) then
12785 Id := Entity (N);
12786
12787 return
12788 Is_Formal (Id)
12789 and then Is_Specific_Tagged_Type (Etype (Id))
12790 and then Extensions_Visible_Status (Id) =
12791 Extensions_Visible_False;
12792
12793 -- A case expression is an EVF expression when it contains at least one
12794 -- EVF dependent_expression. Note that a case expression may have been
12795 -- expanded, hence the use of Original_Node.
12796
12797 elsif Nkind (Orig_N) = N_Case_Expression then
12798 Alt := First (Alternatives (Orig_N));
12799 while Present (Alt) loop
12800 if Is_EVF_Expression (Expression (Alt)) then
12801 return True;
12802 end if;
12803
12804 Next (Alt);
12805 end loop;
12806
12807 -- An if expression is an EVF expression when it contains at least one
12808 -- EVF dependent_expression. Note that an if expression may have been
12809 -- expanded, hence the use of Original_Node.
12810
12811 elsif Nkind (Orig_N) = N_If_Expression then
12812 Expr := Next (First (Expressions (Orig_N)));
12813 while Present (Expr) loop
12814 if Is_EVF_Expression (Expr) then
12815 return True;
12816 end if;
12817
12818 Next (Expr);
12819 end loop;
12820
12821 -- A qualified expression or a type conversion is an EVF expression when
12822 -- its operand is an EVF expression.
12823
12824 elsif Nkind_In (N, N_Qualified_Expression,
12825 N_Unchecked_Type_Conversion,
12826 N_Type_Conversion)
12827 then
12828 return Is_EVF_Expression (Expression (N));
12829
12830 -- Attributes 'Loop_Entry, 'Old, and 'Update are EVF expressions when
12831 -- their prefix denotes an EVF expression.
12832
12833 elsif Nkind (N) = N_Attribute_Reference
12834 and then Nam_In (Attribute_Name (N), Name_Loop_Entry,
12835 Name_Old,
12836 Name_Update)
12837 then
12838 return Is_EVF_Expression (Prefix (N));
12839 end if;
12840
12841 return False;
12842 end Is_EVF_Expression;
12843
12844 --------------
12845 -- Is_False --
12846 --------------
12847
12848 function Is_False (U : Uint) return Boolean is
12849 begin
12850 return (U = 0);
12851 end Is_False;
12852
12853 ---------------------------
12854 -- Is_Fixed_Model_Number --
12855 ---------------------------
12856
12857 function Is_Fixed_Model_Number (U : Ureal; T : Entity_Id) return Boolean is
12858 S : constant Ureal := Small_Value (T);
12859 M : Urealp.Save_Mark;
12860 R : Boolean;
12861 begin
12862 M := Urealp.Mark;
12863 R := (U = UR_Trunc (U / S) * S);
12864 Urealp.Release (M);
12865 return R;
12866 end Is_Fixed_Model_Number;
12867
12868 -------------------------------
12869 -- Is_Fully_Initialized_Type --
12870 -------------------------------
12871
12872 function Is_Fully_Initialized_Type (Typ : Entity_Id) return Boolean is
12873 begin
12874 -- Scalar types
12875
12876 if Is_Scalar_Type (Typ) then
12877
12878 -- A scalar type with an aspect Default_Value is fully initialized
12879
12880 -- Note: Iniitalize/Normalize_Scalars also ensure full initialization
12881 -- of a scalar type, but we don't take that into account here, since
12882 -- we don't want these to affect warnings.
12883
12884 return Has_Default_Aspect (Typ);
12885
12886 elsif Is_Access_Type (Typ) then
12887 return True;
12888
12889 elsif Is_Array_Type (Typ) then
12890 if Is_Fully_Initialized_Type (Component_Type (Typ))
12891 or else (Ada_Version >= Ada_2012 and then Has_Default_Aspect (Typ))
12892 then
12893 return True;
12894 end if;
12895
12896 -- An interesting case, if we have a constrained type one of whose
12897 -- bounds is known to be null, then there are no elements to be
12898 -- initialized, so all the elements are initialized.
12899
12900 if Is_Constrained (Typ) then
12901 declare
12902 Indx : Node_Id;
12903 Indx_Typ : Entity_Id;
12904 Lbd, Hbd : Node_Id;
12905
12906 begin
12907 Indx := First_Index (Typ);
12908 while Present (Indx) loop
12909 if Etype (Indx) = Any_Type then
12910 return False;
12911
12912 -- If index is a range, use directly
12913
12914 elsif Nkind (Indx) = N_Range then
12915 Lbd := Low_Bound (Indx);
12916 Hbd := High_Bound (Indx);
12917
12918 else
12919 Indx_Typ := Etype (Indx);
12920
12921 if Is_Private_Type (Indx_Typ) then
12922 Indx_Typ := Full_View (Indx_Typ);
12923 end if;
12924
12925 if No (Indx_Typ) or else Etype (Indx_Typ) = Any_Type then
12926 return False;
12927 else
12928 Lbd := Type_Low_Bound (Indx_Typ);
12929 Hbd := Type_High_Bound (Indx_Typ);
12930 end if;
12931 end if;
12932
12933 if Compile_Time_Known_Value (Lbd)
12934 and then
12935 Compile_Time_Known_Value (Hbd)
12936 then
12937 if Expr_Value (Hbd) < Expr_Value (Lbd) then
12938 return True;
12939 end if;
12940 end if;
12941
12942 Next_Index (Indx);
12943 end loop;
12944 end;
12945 end if;
12946
12947 -- If no null indexes, then type is not fully initialized
12948
12949 return False;
12950
12951 -- Record types
12952
12953 elsif Is_Record_Type (Typ) then
12954 if Has_Discriminants (Typ)
12955 and then
12956 Present (Discriminant_Default_Value (First_Discriminant (Typ)))
12957 and then Is_Fully_Initialized_Variant (Typ)
12958 then
12959 return True;
12960 end if;
12961
12962 -- We consider bounded string types to be fully initialized, because
12963 -- otherwise we get false alarms when the Data component is not
12964 -- default-initialized.
12965
12966 if Is_Bounded_String (Typ) then
12967 return True;
12968 end if;
12969
12970 -- Controlled records are considered to be fully initialized if
12971 -- there is a user defined Initialize routine. This may not be
12972 -- entirely correct, but as the spec notes, we are guessing here
12973 -- what is best from the point of view of issuing warnings.
12974
12975 if Is_Controlled (Typ) then
12976 declare
12977 Utyp : constant Entity_Id := Underlying_Type (Typ);
12978
12979 begin
12980 if Present (Utyp) then
12981 declare
12982 Init : constant Entity_Id :=
12983 (Find_Optional_Prim_Op
12984 (Underlying_Type (Typ), Name_Initialize));
12985
12986 begin
12987 if Present (Init)
12988 and then Comes_From_Source (Init)
12989 and then not
12990 Is_Predefined_File_Name
12991 (File_Name (Get_Source_File_Index (Sloc (Init))))
12992 then
12993 return True;
12994
12995 elsif Has_Null_Extension (Typ)
12996 and then
12997 Is_Fully_Initialized_Type
12998 (Etype (Base_Type (Typ)))
12999 then
13000 return True;
13001 end if;
13002 end;
13003 end if;
13004 end;
13005 end if;
13006
13007 -- Otherwise see if all record components are initialized
13008
13009 declare
13010 Ent : Entity_Id;
13011
13012 begin
13013 Ent := First_Entity (Typ);
13014 while Present (Ent) loop
13015 if Ekind (Ent) = E_Component
13016 and then (No (Parent (Ent))
13017 or else No (Expression (Parent (Ent))))
13018 and then not Is_Fully_Initialized_Type (Etype (Ent))
13019
13020 -- Special VM case for tag components, which need to be
13021 -- defined in this case, but are never initialized as VMs
13022 -- are using other dispatching mechanisms. Ignore this
13023 -- uninitialized case. Note that this applies both to the
13024 -- uTag entry and the main vtable pointer (CPP_Class case).
13025
13026 and then (Tagged_Type_Expansion or else not Is_Tag (Ent))
13027 then
13028 return False;
13029 end if;
13030
13031 Next_Entity (Ent);
13032 end loop;
13033 end;
13034
13035 -- No uninitialized components, so type is fully initialized.
13036 -- Note that this catches the case of no components as well.
13037
13038 return True;
13039
13040 elsif Is_Concurrent_Type (Typ) then
13041 return True;
13042
13043 elsif Is_Private_Type (Typ) then
13044 declare
13045 U : constant Entity_Id := Underlying_Type (Typ);
13046
13047 begin
13048 if No (U) then
13049 return False;
13050 else
13051 return Is_Fully_Initialized_Type (U);
13052 end if;
13053 end;
13054
13055 else
13056 return False;
13057 end if;
13058 end Is_Fully_Initialized_Type;
13059
13060 ----------------------------------
13061 -- Is_Fully_Initialized_Variant --
13062 ----------------------------------
13063
13064 function Is_Fully_Initialized_Variant (Typ : Entity_Id) return Boolean is
13065 Loc : constant Source_Ptr := Sloc (Typ);
13066 Constraints : constant List_Id := New_List;
13067 Components : constant Elist_Id := New_Elmt_List;
13068 Comp_Elmt : Elmt_Id;
13069 Comp_Id : Node_Id;
13070 Comp_List : Node_Id;
13071 Discr : Entity_Id;
13072 Discr_Val : Node_Id;
13073
13074 Report_Errors : Boolean;
13075 pragma Warnings (Off, Report_Errors);
13076
13077 begin
13078 if Serious_Errors_Detected > 0 then
13079 return False;
13080 end if;
13081
13082 if Is_Record_Type (Typ)
13083 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
13084 and then Nkind (Type_Definition (Parent (Typ))) = N_Record_Definition
13085 then
13086 Comp_List := Component_List (Type_Definition (Parent (Typ)));
13087
13088 Discr := First_Discriminant (Typ);
13089 while Present (Discr) loop
13090 if Nkind (Parent (Discr)) = N_Discriminant_Specification then
13091 Discr_Val := Expression (Parent (Discr));
13092
13093 if Present (Discr_Val)
13094 and then Is_OK_Static_Expression (Discr_Val)
13095 then
13096 Append_To (Constraints,
13097 Make_Component_Association (Loc,
13098 Choices => New_List (New_Occurrence_Of (Discr, Loc)),
13099 Expression => New_Copy (Discr_Val)));
13100 else
13101 return False;
13102 end if;
13103 else
13104 return False;
13105 end if;
13106
13107 Next_Discriminant (Discr);
13108 end loop;
13109
13110 Gather_Components
13111 (Typ => Typ,
13112 Comp_List => Comp_List,
13113 Governed_By => Constraints,
13114 Into => Components,
13115 Report_Errors => Report_Errors);
13116
13117 -- Check that each component present is fully initialized
13118
13119 Comp_Elmt := First_Elmt (Components);
13120 while Present (Comp_Elmt) loop
13121 Comp_Id := Node (Comp_Elmt);
13122
13123 if Ekind (Comp_Id) = E_Component
13124 and then (No (Parent (Comp_Id))
13125 or else No (Expression (Parent (Comp_Id))))
13126 and then not Is_Fully_Initialized_Type (Etype (Comp_Id))
13127 then
13128 return False;
13129 end if;
13130
13131 Next_Elmt (Comp_Elmt);
13132 end loop;
13133
13134 return True;
13135
13136 elsif Is_Private_Type (Typ) then
13137 declare
13138 U : constant Entity_Id := Underlying_Type (Typ);
13139
13140 begin
13141 if No (U) then
13142 return False;
13143 else
13144 return Is_Fully_Initialized_Variant (U);
13145 end if;
13146 end;
13147
13148 else
13149 return False;
13150 end if;
13151 end Is_Fully_Initialized_Variant;
13152
13153 ------------------------------------
13154 -- Is_Generic_Declaration_Or_Body --
13155 ------------------------------------
13156
13157 function Is_Generic_Declaration_Or_Body (Decl : Node_Id) return Boolean is
13158 Spec_Decl : Node_Id;
13159
13160 begin
13161 -- Package/subprogram body
13162
13163 if Nkind_In (Decl, N_Package_Body, N_Subprogram_Body)
13164 and then Present (Corresponding_Spec (Decl))
13165 then
13166 Spec_Decl := Unit_Declaration_Node (Corresponding_Spec (Decl));
13167
13168 -- Package/subprogram body stub
13169
13170 elsif Nkind_In (Decl, N_Package_Body_Stub, N_Subprogram_Body_Stub)
13171 and then Present (Corresponding_Spec_Of_Stub (Decl))
13172 then
13173 Spec_Decl :=
13174 Unit_Declaration_Node (Corresponding_Spec_Of_Stub (Decl));
13175
13176 -- All other cases
13177
13178 else
13179 Spec_Decl := Decl;
13180 end if;
13181
13182 -- Rather than inspecting the defining entity of the spec declaration,
13183 -- look at its Nkind. This takes care of the case where the analysis of
13184 -- a generic body modifies the Ekind of its spec to allow for recursive
13185 -- calls.
13186
13187 return
13188 Nkind_In (Spec_Decl, N_Generic_Package_Declaration,
13189 N_Generic_Subprogram_Declaration);
13190 end Is_Generic_Declaration_Or_Body;
13191
13192 ----------------------------
13193 -- Is_Inherited_Operation --
13194 ----------------------------
13195
13196 function Is_Inherited_Operation (E : Entity_Id) return Boolean is
13197 pragma Assert (Is_Overloadable (E));
13198 Kind : constant Node_Kind := Nkind (Parent (E));
13199 begin
13200 return Kind = N_Full_Type_Declaration
13201 or else Kind = N_Private_Extension_Declaration
13202 or else Kind = N_Subtype_Declaration
13203 or else (Ekind (E) = E_Enumeration_Literal
13204 and then Is_Derived_Type (Etype (E)));
13205 end Is_Inherited_Operation;
13206
13207 -------------------------------------
13208 -- Is_Inherited_Operation_For_Type --
13209 -------------------------------------
13210
13211 function Is_Inherited_Operation_For_Type
13212 (E : Entity_Id;
13213 Typ : Entity_Id) return Boolean
13214 is
13215 begin
13216 -- Check that the operation has been created by the type declaration
13217
13218 return Is_Inherited_Operation (E)
13219 and then Defining_Identifier (Parent (E)) = Typ;
13220 end Is_Inherited_Operation_For_Type;
13221
13222 -----------------
13223 -- Is_Iterator --
13224 -----------------
13225
13226 function Is_Iterator (Typ : Entity_Id) return Boolean is
13227 function Denotes_Iterator (Iter_Typ : Entity_Id) return Boolean;
13228 -- Determine whether type Iter_Typ is a predefined forward or reversible
13229 -- iterator.
13230
13231 ----------------------
13232 -- Denotes_Iterator --
13233 ----------------------
13234
13235 function Denotes_Iterator (Iter_Typ : Entity_Id) return Boolean is
13236 begin
13237 -- Check that the name matches, and that the ultimate ancestor is in
13238 -- a predefined unit, i.e the one that declares iterator interfaces.
13239
13240 return
13241 Nam_In (Chars (Iter_Typ), Name_Forward_Iterator,
13242 Name_Reversible_Iterator)
13243 and then Is_Predefined_File_Name
13244 (Unit_File_Name (Get_Source_Unit (Root_Type (Iter_Typ))));
13245 end Denotes_Iterator;
13246
13247 -- Local variables
13248
13249 Iface_Elmt : Elmt_Id;
13250 Ifaces : Elist_Id;
13251
13252 -- Start of processing for Is_Iterator
13253
13254 begin
13255 -- The type may be a subtype of a descendant of the proper instance of
13256 -- the predefined interface type, so we must use the root type of the
13257 -- given type. The same is done for Is_Reversible_Iterator.
13258
13259 if Is_Class_Wide_Type (Typ)
13260 and then Denotes_Iterator (Root_Type (Typ))
13261 then
13262 return True;
13263
13264 elsif not Is_Tagged_Type (Typ) or else not Is_Derived_Type (Typ) then
13265 return False;
13266
13267 elsif Present (Find_Value_Of_Aspect (Typ, Aspect_Iterable)) then
13268 return True;
13269
13270 else
13271 Collect_Interfaces (Typ, Ifaces);
13272
13273 Iface_Elmt := First_Elmt (Ifaces);
13274 while Present (Iface_Elmt) loop
13275 if Denotes_Iterator (Node (Iface_Elmt)) then
13276 return True;
13277 end if;
13278
13279 Next_Elmt (Iface_Elmt);
13280 end loop;
13281
13282 return False;
13283 end if;
13284 end Is_Iterator;
13285
13286 ----------------------------
13287 -- Is_Iterator_Over_Array --
13288 ----------------------------
13289
13290 function Is_Iterator_Over_Array (N : Node_Id) return Boolean is
13291 Container : constant Node_Id := Name (N);
13292 Container_Typ : constant Entity_Id := Base_Type (Etype (Container));
13293 begin
13294 return Is_Array_Type (Container_Typ);
13295 end Is_Iterator_Over_Array;
13296
13297 ------------
13298 -- Is_LHS --
13299 ------------
13300
13301 -- We seem to have a lot of overlapping functions that do similar things
13302 -- (testing for left hand sides or lvalues???).
13303
13304 function Is_LHS (N : Node_Id) return Is_LHS_Result is
13305 P : constant Node_Id := Parent (N);
13306
13307 begin
13308 -- Return True if we are the left hand side of an assignment statement
13309
13310 if Nkind (P) = N_Assignment_Statement then
13311 if Name (P) = N then
13312 return Yes;
13313 else
13314 return No;
13315 end if;
13316
13317 -- Case of prefix of indexed or selected component or slice
13318
13319 elsif Nkind_In (P, N_Indexed_Component, N_Selected_Component, N_Slice)
13320 and then N = Prefix (P)
13321 then
13322 -- Here we have the case where the parent P is N.Q or N(Q .. R).
13323 -- If P is an LHS, then N is also effectively an LHS, but there
13324 -- is an important exception. If N is of an access type, then
13325 -- what we really have is N.all.Q (or N.all(Q .. R)). In either
13326 -- case this makes N.all a left hand side but not N itself.
13327
13328 -- If we don't know the type yet, this is the case where we return
13329 -- Unknown, since the answer depends on the type which is unknown.
13330
13331 if No (Etype (N)) then
13332 return Unknown;
13333
13334 -- We have an Etype set, so we can check it
13335
13336 elsif Is_Access_Type (Etype (N)) then
13337 return No;
13338
13339 -- OK, not access type case, so just test whole expression
13340
13341 else
13342 return Is_LHS (P);
13343 end if;
13344
13345 -- All other cases are not left hand sides
13346
13347 else
13348 return No;
13349 end if;
13350 end Is_LHS;
13351
13352 -----------------------------
13353 -- Is_Library_Level_Entity --
13354 -----------------------------
13355
13356 function Is_Library_Level_Entity (E : Entity_Id) return Boolean is
13357 begin
13358 -- The following is a small optimization, and it also properly handles
13359 -- discriminals, which in task bodies might appear in expressions before
13360 -- the corresponding procedure has been created, and which therefore do
13361 -- not have an assigned scope.
13362
13363 if Is_Formal (E) then
13364 return False;
13365 end if;
13366
13367 -- Normal test is simply that the enclosing dynamic scope is Standard
13368
13369 return Enclosing_Dynamic_Scope (E) = Standard_Standard;
13370 end Is_Library_Level_Entity;
13371
13372 --------------------------------
13373 -- Is_Limited_Class_Wide_Type --
13374 --------------------------------
13375
13376 function Is_Limited_Class_Wide_Type (Typ : Entity_Id) return Boolean is
13377 begin
13378 return
13379 Is_Class_Wide_Type (Typ)
13380 and then (Is_Limited_Type (Typ) or else From_Limited_With (Typ));
13381 end Is_Limited_Class_Wide_Type;
13382
13383 ---------------------------------
13384 -- Is_Local_Variable_Reference --
13385 ---------------------------------
13386
13387 function Is_Local_Variable_Reference (Expr : Node_Id) return Boolean is
13388 begin
13389 if not Is_Entity_Name (Expr) then
13390 return False;
13391
13392 else
13393 declare
13394 Ent : constant Entity_Id := Entity (Expr);
13395 Sub : constant Entity_Id := Enclosing_Subprogram (Ent);
13396 begin
13397 if not Ekind_In (Ent, E_Variable, E_In_Out_Parameter) then
13398 return False;
13399 else
13400 return Present (Sub) and then Sub = Current_Subprogram;
13401 end if;
13402 end;
13403 end if;
13404 end Is_Local_Variable_Reference;
13405
13406 -----------------------------------------------
13407 -- Is_Nontrivial_Default_Init_Cond_Procedure --
13408 -----------------------------------------------
13409
13410 function Is_Nontrivial_Default_Init_Cond_Procedure
13411 (Id : Entity_Id) return Boolean
13412 is
13413 Body_Decl : Node_Id;
13414 Stmt : Node_Id;
13415
13416 begin
13417 if Ekind (Id) = E_Procedure
13418 and then Is_Default_Init_Cond_Procedure (Id)
13419 then
13420 Body_Decl :=
13421 Unit_Declaration_Node
13422 (Corresponding_Body (Unit_Declaration_Node (Id)));
13423
13424 -- The body of the Default_Initial_Condition procedure must contain
13425 -- at least one statement, otherwise the generation of the subprogram
13426 -- body failed.
13427
13428 pragma Assert (Present (Handled_Statement_Sequence (Body_Decl)));
13429
13430 -- To qualify as nontrivial, the first statement of the procedure
13431 -- must be a check in the form of an if statement. If the original
13432 -- Default_Initial_Condition expression was folded, then the first
13433 -- statement is not a check.
13434
13435 Stmt := First (Statements (Handled_Statement_Sequence (Body_Decl)));
13436
13437 return
13438 Nkind (Stmt) = N_If_Statement
13439 and then Nkind (Original_Node (Stmt)) = N_Pragma;
13440 end if;
13441
13442 return False;
13443 end Is_Nontrivial_Default_Init_Cond_Procedure;
13444
13445 -------------------------
13446 -- Is_Null_Record_Type --
13447 -------------------------
13448
13449 function Is_Null_Record_Type (T : Entity_Id) return Boolean is
13450 Decl : constant Node_Id := Parent (T);
13451 begin
13452 return Nkind (Decl) = N_Full_Type_Declaration
13453 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
13454 and then
13455 (No (Component_List (Type_Definition (Decl)))
13456 or else Null_Present (Component_List (Type_Definition (Decl))));
13457 end Is_Null_Record_Type;
13458
13459 -------------------------
13460 -- Is_Object_Reference --
13461 -------------------------
13462
13463 function Is_Object_Reference (N : Node_Id) return Boolean is
13464 function Is_Internally_Generated_Renaming (N : Node_Id) return Boolean;
13465 -- Determine whether N is the name of an internally-generated renaming
13466
13467 --------------------------------------
13468 -- Is_Internally_Generated_Renaming --
13469 --------------------------------------
13470
13471 function Is_Internally_Generated_Renaming (N : Node_Id) return Boolean is
13472 P : Node_Id;
13473
13474 begin
13475 P := N;
13476 while Present (P) loop
13477 if Nkind (P) = N_Object_Renaming_Declaration then
13478 return not Comes_From_Source (P);
13479 elsif Is_List_Member (P) then
13480 return False;
13481 end if;
13482
13483 P := Parent (P);
13484 end loop;
13485
13486 return False;
13487 end Is_Internally_Generated_Renaming;
13488
13489 -- Start of processing for Is_Object_Reference
13490
13491 begin
13492 if Is_Entity_Name (N) then
13493 return Present (Entity (N)) and then Is_Object (Entity (N));
13494
13495 else
13496 case Nkind (N) is
13497 when N_Indexed_Component | N_Slice =>
13498 return
13499 Is_Object_Reference (Prefix (N))
13500 or else Is_Access_Type (Etype (Prefix (N)));
13501
13502 -- In Ada 95, a function call is a constant object; a procedure
13503 -- call is not.
13504
13505 when N_Function_Call =>
13506 return Etype (N) /= Standard_Void_Type;
13507
13508 -- Attributes 'Input, 'Loop_Entry, 'Old, and 'Result produce
13509 -- objects.
13510
13511 when N_Attribute_Reference =>
13512 return
13513 Nam_In (Attribute_Name (N), Name_Input,
13514 Name_Loop_Entry,
13515 Name_Old,
13516 Name_Result);
13517
13518 when N_Selected_Component =>
13519 return
13520 Is_Object_Reference (Selector_Name (N))
13521 and then
13522 (Is_Object_Reference (Prefix (N))
13523 or else Is_Access_Type (Etype (Prefix (N))));
13524
13525 when N_Explicit_Dereference =>
13526 return True;
13527
13528 -- A view conversion of a tagged object is an object reference
13529
13530 when N_Type_Conversion =>
13531 return Is_Tagged_Type (Etype (Subtype_Mark (N)))
13532 and then Is_Tagged_Type (Etype (Expression (N)))
13533 and then Is_Object_Reference (Expression (N));
13534
13535 -- An unchecked type conversion is considered to be an object if
13536 -- the operand is an object (this construction arises only as a
13537 -- result of expansion activities).
13538
13539 when N_Unchecked_Type_Conversion =>
13540 return True;
13541
13542 -- Allow string literals to act as objects as long as they appear
13543 -- in internally-generated renamings. The expansion of iterators
13544 -- may generate such renamings when the range involves a string
13545 -- literal.
13546
13547 when N_String_Literal =>
13548 return Is_Internally_Generated_Renaming (Parent (N));
13549
13550 -- AI05-0003: In Ada 2012 a qualified expression is a name.
13551 -- This allows disambiguation of function calls and the use
13552 -- of aggregates in more contexts.
13553
13554 when N_Qualified_Expression =>
13555 if Ada_Version < Ada_2012 then
13556 return False;
13557 else
13558 return Is_Object_Reference (Expression (N))
13559 or else Nkind (Expression (N)) = N_Aggregate;
13560 end if;
13561
13562 when others =>
13563 return False;
13564 end case;
13565 end if;
13566 end Is_Object_Reference;
13567
13568 -----------------------------------
13569 -- Is_OK_Variable_For_Out_Formal --
13570 -----------------------------------
13571
13572 function Is_OK_Variable_For_Out_Formal (AV : Node_Id) return Boolean is
13573 begin
13574 Note_Possible_Modification (AV, Sure => True);
13575
13576 -- We must reject parenthesized variable names. Comes_From_Source is
13577 -- checked because there are currently cases where the compiler violates
13578 -- this rule (e.g. passing a task object to its controlled Initialize
13579 -- routine). This should be properly documented in sinfo???
13580
13581 if Paren_Count (AV) > 0 and then Comes_From_Source (AV) then
13582 return False;
13583
13584 -- A variable is always allowed
13585
13586 elsif Is_Variable (AV) then
13587 return True;
13588
13589 -- Generalized indexing operations are rewritten as explicit
13590 -- dereferences, and it is only during resolution that we can
13591 -- check whether the context requires an access_to_variable type.
13592
13593 elsif Nkind (AV) = N_Explicit_Dereference
13594 and then Ada_Version >= Ada_2012
13595 and then Nkind (Original_Node (AV)) = N_Indexed_Component
13596 and then Present (Etype (Original_Node (AV)))
13597 and then Has_Implicit_Dereference (Etype (Original_Node (AV)))
13598 then
13599 return not Is_Access_Constant (Etype (Prefix (AV)));
13600
13601 -- Unchecked conversions are allowed only if they come from the
13602 -- generated code, which sometimes uses unchecked conversions for out
13603 -- parameters in cases where code generation is unaffected. We tell
13604 -- source unchecked conversions by seeing if they are rewrites of
13605 -- an original Unchecked_Conversion function call, or of an explicit
13606 -- conversion of a function call or an aggregate (as may happen in the
13607 -- expansion of a packed array aggregate).
13608
13609 elsif Nkind (AV) = N_Unchecked_Type_Conversion then
13610 if Nkind_In (Original_Node (AV), N_Function_Call, N_Aggregate) then
13611 return False;
13612
13613 elsif Comes_From_Source (AV)
13614 and then Nkind (Original_Node (Expression (AV))) = N_Function_Call
13615 then
13616 return False;
13617
13618 elsif Nkind (Original_Node (AV)) = N_Type_Conversion then
13619 return Is_OK_Variable_For_Out_Formal (Expression (AV));
13620
13621 else
13622 return True;
13623 end if;
13624
13625 -- Normal type conversions are allowed if argument is a variable
13626
13627 elsif Nkind (AV) = N_Type_Conversion then
13628 if Is_Variable (Expression (AV))
13629 and then Paren_Count (Expression (AV)) = 0
13630 then
13631 Note_Possible_Modification (Expression (AV), Sure => True);
13632 return True;
13633
13634 -- We also allow a non-parenthesized expression that raises
13635 -- constraint error if it rewrites what used to be a variable
13636
13637 elsif Raises_Constraint_Error (Expression (AV))
13638 and then Paren_Count (Expression (AV)) = 0
13639 and then Is_Variable (Original_Node (Expression (AV)))
13640 then
13641 return True;
13642
13643 -- Type conversion of something other than a variable
13644
13645 else
13646 return False;
13647 end if;
13648
13649 -- If this node is rewritten, then test the original form, if that is
13650 -- OK, then we consider the rewritten node OK (for example, if the
13651 -- original node is a conversion, then Is_Variable will not be true
13652 -- but we still want to allow the conversion if it converts a variable).
13653
13654 elsif Original_Node (AV) /= AV then
13655
13656 -- In Ada 2012, the explicit dereference may be a rewritten call to a
13657 -- Reference function.
13658
13659 if Ada_Version >= Ada_2012
13660 and then Nkind (Original_Node (AV)) = N_Function_Call
13661 and then
13662 Has_Implicit_Dereference (Etype (Name (Original_Node (AV))))
13663 then
13664
13665 -- Check that this is not a constant reference.
13666
13667 return not Is_Access_Constant (Etype (Prefix (AV)));
13668
13669 elsif Has_Implicit_Dereference (Etype (Original_Node (AV))) then
13670 return
13671 not Is_Access_Constant (Etype
13672 (Get_Reference_Discriminant (Etype (Original_Node (AV)))));
13673
13674 else
13675 return Is_OK_Variable_For_Out_Formal (Original_Node (AV));
13676 end if;
13677
13678 -- All other non-variables are rejected
13679
13680 else
13681 return False;
13682 end if;
13683 end Is_OK_Variable_For_Out_Formal;
13684
13685 ----------------------------
13686 -- Is_OK_Volatile_Context --
13687 ----------------------------
13688
13689 function Is_OK_Volatile_Context
13690 (Context : Node_Id;
13691 Obj_Ref : Node_Id) return Boolean
13692 is
13693 function Is_Protected_Operation_Call (Nod : Node_Id) return Boolean;
13694 -- Determine whether an arbitrary node denotes a call to a protected
13695 -- entry, function, or procedure in prefixed form where the prefix is
13696 -- Obj_Ref.
13697
13698 function Within_Check (Nod : Node_Id) return Boolean;
13699 -- Determine whether an arbitrary node appears in a check node
13700
13701 function Within_Subprogram_Call (Nod : Node_Id) return Boolean;
13702 -- Determine whether an arbitrary node appears in an entry, function, or
13703 -- procedure call.
13704
13705 function Within_Volatile_Function (Id : Entity_Id) return Boolean;
13706 -- Determine whether an arbitrary entity appears in a volatile function
13707
13708 ---------------------------------
13709 -- Is_Protected_Operation_Call --
13710 ---------------------------------
13711
13712 function Is_Protected_Operation_Call (Nod : Node_Id) return Boolean is
13713 Pref : Node_Id;
13714 Subp : Node_Id;
13715
13716 begin
13717 -- A call to a protected operations retains its selected component
13718 -- form as opposed to other prefixed calls that are transformed in
13719 -- expanded names.
13720
13721 if Nkind (Nod) = N_Selected_Component then
13722 Pref := Prefix (Nod);
13723 Subp := Selector_Name (Nod);
13724
13725 return
13726 Pref = Obj_Ref
13727 and then Present (Etype (Pref))
13728 and then Is_Protected_Type (Etype (Pref))
13729 and then Is_Entity_Name (Subp)
13730 and then Present (Entity (Subp))
13731 and then Ekind_In (Entity (Subp), E_Entry,
13732 E_Entry_Family,
13733 E_Function,
13734 E_Procedure);
13735 else
13736 return False;
13737 end if;
13738 end Is_Protected_Operation_Call;
13739
13740 ------------------
13741 -- Within_Check --
13742 ------------------
13743
13744 function Within_Check (Nod : Node_Id) return Boolean is
13745 Par : Node_Id;
13746
13747 begin
13748 -- Climb the parent chain looking for a check node
13749
13750 Par := Nod;
13751 while Present (Par) loop
13752 if Nkind (Par) in N_Raise_xxx_Error then
13753 return True;
13754
13755 -- Prevent the search from going too far
13756
13757 elsif Is_Body_Or_Package_Declaration (Par) then
13758 exit;
13759 end if;
13760
13761 Par := Parent (Par);
13762 end loop;
13763
13764 return False;
13765 end Within_Check;
13766
13767 ----------------------------
13768 -- Within_Subprogram_Call --
13769 ----------------------------
13770
13771 function Within_Subprogram_Call (Nod : Node_Id) return Boolean is
13772 Par : Node_Id;
13773
13774 begin
13775 -- Climb the parent chain looking for a function or procedure call
13776
13777 Par := Nod;
13778 while Present (Par) loop
13779 if Nkind_In (Par, N_Entry_Call_Statement,
13780 N_Function_Call,
13781 N_Procedure_Call_Statement)
13782 then
13783 return True;
13784
13785 -- Prevent the search from going too far
13786
13787 elsif Is_Body_Or_Package_Declaration (Par) then
13788 exit;
13789 end if;
13790
13791 Par := Parent (Par);
13792 end loop;
13793
13794 return False;
13795 end Within_Subprogram_Call;
13796
13797 ------------------------------
13798 -- Within_Volatile_Function --
13799 ------------------------------
13800
13801 function Within_Volatile_Function (Id : Entity_Id) return Boolean is
13802 Func_Id : Entity_Id;
13803
13804 begin
13805 -- Traverse the scope stack looking for a [generic] function
13806
13807 Func_Id := Id;
13808 while Present (Func_Id) and then Func_Id /= Standard_Standard loop
13809 if Ekind_In (Func_Id, E_Function, E_Generic_Function) then
13810 return Is_Volatile_Function (Func_Id);
13811 end if;
13812
13813 Func_Id := Scope (Func_Id);
13814 end loop;
13815
13816 return False;
13817 end Within_Volatile_Function;
13818
13819 -- Local variables
13820
13821 Obj_Id : Entity_Id;
13822
13823 -- Start of processing for Is_OK_Volatile_Context
13824
13825 begin
13826 -- The volatile object appears on either side of an assignment
13827
13828 if Nkind (Context) = N_Assignment_Statement then
13829 return True;
13830
13831 -- The volatile object is part of the initialization expression of
13832 -- another object.
13833
13834 elsif Nkind (Context) = N_Object_Declaration
13835 and then Present (Expression (Context))
13836 and then Expression (Context) = Obj_Ref
13837 then
13838 Obj_Id := Defining_Entity (Context);
13839
13840 -- The volatile object acts as the initialization expression of an
13841 -- extended return statement. This is valid context as long as the
13842 -- function is volatile.
13843
13844 if Is_Return_Object (Obj_Id) then
13845 return Within_Volatile_Function (Obj_Id);
13846
13847 -- Otherwise this is a normal object initialization
13848
13849 else
13850 return True;
13851 end if;
13852
13853 -- The volatile object acts as the name of a renaming declaration
13854
13855 elsif Nkind (Context) = N_Object_Renaming_Declaration
13856 and then Name (Context) = Obj_Ref
13857 then
13858 return True;
13859
13860 -- The volatile object appears as an actual parameter in a call to an
13861 -- instance of Unchecked_Conversion whose result is renamed.
13862
13863 elsif Nkind (Context) = N_Function_Call
13864 and then Is_Entity_Name (Name (Context))
13865 and then Is_Unchecked_Conversion_Instance (Entity (Name (Context)))
13866 and then Nkind (Parent (Context)) = N_Object_Renaming_Declaration
13867 then
13868 return True;
13869
13870 -- The volatile object is actually the prefix in a protected entry,
13871 -- function, or procedure call.
13872
13873 elsif Is_Protected_Operation_Call (Context) then
13874 return True;
13875
13876 -- The volatile object appears as the expression of a simple return
13877 -- statement that applies to a volatile function.
13878
13879 elsif Nkind (Context) = N_Simple_Return_Statement
13880 and then Expression (Context) = Obj_Ref
13881 then
13882 return
13883 Within_Volatile_Function (Return_Statement_Entity (Context));
13884
13885 -- The volatile object appears as the prefix of a name occurring in a
13886 -- non-interfering context.
13887
13888 elsif Nkind_In (Context, N_Attribute_Reference,
13889 N_Explicit_Dereference,
13890 N_Indexed_Component,
13891 N_Selected_Component,
13892 N_Slice)
13893 and then Prefix (Context) = Obj_Ref
13894 and then Is_OK_Volatile_Context
13895 (Context => Parent (Context),
13896 Obj_Ref => Context)
13897 then
13898 return True;
13899
13900 -- The volatile object appears as the prefix of attributes Address,
13901 -- Alignment, Component_Size, First_Bit, Last_Bit, Position, Size,
13902 -- Storage_Size.
13903
13904 elsif Nkind (Context) = N_Attribute_Reference
13905 and then Prefix (Context) = Obj_Ref
13906 and then Nam_In (Attribute_Name (Context), Name_Address,
13907 Name_Alignment,
13908 Name_Component_Size,
13909 Name_First_Bit,
13910 Name_Last_Bit,
13911 Name_Position,
13912 Name_Size,
13913 Name_Storage_Size)
13914 then
13915 return True;
13916
13917 -- The volatile object appears as the expression of a type conversion
13918 -- occurring in a non-interfering context.
13919
13920 elsif Nkind_In (Context, N_Type_Conversion,
13921 N_Unchecked_Type_Conversion)
13922 and then Expression (Context) = Obj_Ref
13923 and then Is_OK_Volatile_Context
13924 (Context => Parent (Context),
13925 Obj_Ref => Context)
13926 then
13927 return True;
13928
13929 -- Allow references to volatile objects in various checks. This is not a
13930 -- direct SPARK 2014 requirement.
13931
13932 elsif Within_Check (Context) then
13933 return True;
13934
13935 -- Assume that references to effectively volatile objects that appear
13936 -- as actual parameters in a subprogram call are always legal. A full
13937 -- legality check is done when the actuals are resolved (see routine
13938 -- Resolve_Actuals).
13939
13940 elsif Within_Subprogram_Call (Context) then
13941 return True;
13942
13943 -- Otherwise the context is not suitable for an effectively volatile
13944 -- object.
13945
13946 else
13947 return False;
13948 end if;
13949 end Is_OK_Volatile_Context;
13950
13951 ------------------------------------
13952 -- Is_Package_Contract_Annotation --
13953 ------------------------------------
13954
13955 function Is_Package_Contract_Annotation (Item : Node_Id) return Boolean is
13956 Nam : Name_Id;
13957
13958 begin
13959 if Nkind (Item) = N_Aspect_Specification then
13960 Nam := Chars (Identifier (Item));
13961
13962 else pragma Assert (Nkind (Item) = N_Pragma);
13963 Nam := Pragma_Name (Item);
13964 end if;
13965
13966 return Nam = Name_Abstract_State
13967 or else Nam = Name_Initial_Condition
13968 or else Nam = Name_Initializes
13969 or else Nam = Name_Refined_State;
13970 end Is_Package_Contract_Annotation;
13971
13972 -----------------------------------
13973 -- Is_Partially_Initialized_Type --
13974 -----------------------------------
13975
13976 function Is_Partially_Initialized_Type
13977 (Typ : Entity_Id;
13978 Include_Implicit : Boolean := True) return Boolean
13979 is
13980 begin
13981 if Is_Scalar_Type (Typ) then
13982 return False;
13983
13984 elsif Is_Access_Type (Typ) then
13985 return Include_Implicit;
13986
13987 elsif Is_Array_Type (Typ) then
13988
13989 -- If component type is partially initialized, so is array type
13990
13991 if Is_Partially_Initialized_Type
13992 (Component_Type (Typ), Include_Implicit)
13993 then
13994 return True;
13995
13996 -- Otherwise we are only partially initialized if we are fully
13997 -- initialized (this is the empty array case, no point in us
13998 -- duplicating that code here).
13999
14000 else
14001 return Is_Fully_Initialized_Type (Typ);
14002 end if;
14003
14004 elsif Is_Record_Type (Typ) then
14005
14006 -- A discriminated type is always partially initialized if in
14007 -- all mode
14008
14009 if Has_Discriminants (Typ) and then Include_Implicit then
14010 return True;
14011
14012 -- A tagged type is always partially initialized
14013
14014 elsif Is_Tagged_Type (Typ) then
14015 return True;
14016
14017 -- Case of non-discriminated record
14018
14019 else
14020 declare
14021 Ent : Entity_Id;
14022
14023 Component_Present : Boolean := False;
14024 -- Set True if at least one component is present. If no
14025 -- components are present, then record type is fully
14026 -- initialized (another odd case, like the null array).
14027
14028 begin
14029 -- Loop through components
14030
14031 Ent := First_Entity (Typ);
14032 while Present (Ent) loop
14033 if Ekind (Ent) = E_Component then
14034 Component_Present := True;
14035
14036 -- If a component has an initialization expression then
14037 -- the enclosing record type is partially initialized
14038
14039 if Present (Parent (Ent))
14040 and then Present (Expression (Parent (Ent)))
14041 then
14042 return True;
14043
14044 -- If a component is of a type which is itself partially
14045 -- initialized, then the enclosing record type is also.
14046
14047 elsif Is_Partially_Initialized_Type
14048 (Etype (Ent), Include_Implicit)
14049 then
14050 return True;
14051 end if;
14052 end if;
14053
14054 Next_Entity (Ent);
14055 end loop;
14056
14057 -- No initialized components found. If we found any components
14058 -- they were all uninitialized so the result is false.
14059
14060 if Component_Present then
14061 return False;
14062
14063 -- But if we found no components, then all the components are
14064 -- initialized so we consider the type to be initialized.
14065
14066 else
14067 return True;
14068 end if;
14069 end;
14070 end if;
14071
14072 -- Concurrent types are always fully initialized
14073
14074 elsif Is_Concurrent_Type (Typ) then
14075 return True;
14076
14077 -- For a private type, go to underlying type. If there is no underlying
14078 -- type then just assume this partially initialized. Not clear if this
14079 -- can happen in a non-error case, but no harm in testing for this.
14080
14081 elsif Is_Private_Type (Typ) then
14082 declare
14083 U : constant Entity_Id := Underlying_Type (Typ);
14084 begin
14085 if No (U) then
14086 return True;
14087 else
14088 return Is_Partially_Initialized_Type (U, Include_Implicit);
14089 end if;
14090 end;
14091
14092 -- For any other type (are there any?) assume partially initialized
14093
14094 else
14095 return True;
14096 end if;
14097 end Is_Partially_Initialized_Type;
14098
14099 ------------------------------------
14100 -- Is_Potentially_Persistent_Type --
14101 ------------------------------------
14102
14103 function Is_Potentially_Persistent_Type (T : Entity_Id) return Boolean is
14104 Comp : Entity_Id;
14105 Indx : Node_Id;
14106
14107 begin
14108 -- For private type, test corresponding full type
14109
14110 if Is_Private_Type (T) then
14111 return Is_Potentially_Persistent_Type (Full_View (T));
14112
14113 -- Scalar types are potentially persistent
14114
14115 elsif Is_Scalar_Type (T) then
14116 return True;
14117
14118 -- Record type is potentially persistent if not tagged and the types of
14119 -- all it components are potentially persistent, and no component has
14120 -- an initialization expression.
14121
14122 elsif Is_Record_Type (T)
14123 and then not Is_Tagged_Type (T)
14124 and then not Is_Partially_Initialized_Type (T)
14125 then
14126 Comp := First_Component (T);
14127 while Present (Comp) loop
14128 if not Is_Potentially_Persistent_Type (Etype (Comp)) then
14129 return False;
14130 else
14131 Next_Entity (Comp);
14132 end if;
14133 end loop;
14134
14135 return True;
14136
14137 -- Array type is potentially persistent if its component type is
14138 -- potentially persistent and if all its constraints are static.
14139
14140 elsif Is_Array_Type (T) then
14141 if not Is_Potentially_Persistent_Type (Component_Type (T)) then
14142 return False;
14143 end if;
14144
14145 Indx := First_Index (T);
14146 while Present (Indx) loop
14147 if not Is_OK_Static_Subtype (Etype (Indx)) then
14148 return False;
14149 else
14150 Next_Index (Indx);
14151 end if;
14152 end loop;
14153
14154 return True;
14155
14156 -- All other types are not potentially persistent
14157
14158 else
14159 return False;
14160 end if;
14161 end Is_Potentially_Persistent_Type;
14162
14163 --------------------------------
14164 -- Is_Potentially_Unevaluated --
14165 --------------------------------
14166
14167 function Is_Potentially_Unevaluated (N : Node_Id) return Boolean is
14168 Par : Node_Id;
14169 Expr : Node_Id;
14170
14171 begin
14172 Expr := N;
14173 Par := Parent (N);
14174
14175 -- A postcondition whose expression is a short-circuit is broken down
14176 -- into individual aspects for better exception reporting. The original
14177 -- short-circuit expression is rewritten as the second operand, and an
14178 -- occurrence of 'Old in that operand is potentially unevaluated.
14179 -- See Sem_ch13.adb for details of this transformation.
14180
14181 if Nkind (Original_Node (Par)) = N_And_Then then
14182 return True;
14183 end if;
14184
14185 while not Nkind_In (Par, N_If_Expression,
14186 N_Case_Expression,
14187 N_And_Then,
14188 N_Or_Else,
14189 N_In,
14190 N_Not_In)
14191 loop
14192 Expr := Par;
14193 Par := Parent (Par);
14194
14195 -- If the context is not an expression, or if is the result of
14196 -- expansion of an enclosing construct (such as another attribute)
14197 -- the predicate does not apply.
14198
14199 if Nkind (Par) not in N_Subexpr
14200 or else not Comes_From_Source (Par)
14201 then
14202 return False;
14203 end if;
14204 end loop;
14205
14206 if Nkind (Par) = N_If_Expression then
14207 return Is_Elsif (Par) or else Expr /= First (Expressions (Par));
14208
14209 elsif Nkind (Par) = N_Case_Expression then
14210 return Expr /= Expression (Par);
14211
14212 elsif Nkind_In (Par, N_And_Then, N_Or_Else) then
14213 return Expr = Right_Opnd (Par);
14214
14215 elsif Nkind_In (Par, N_In, N_Not_In) then
14216 return Expr /= Left_Opnd (Par);
14217
14218 else
14219 return False;
14220 end if;
14221 end Is_Potentially_Unevaluated;
14222
14223 ---------------------------------
14224 -- Is_Protected_Self_Reference --
14225 ---------------------------------
14226
14227 function Is_Protected_Self_Reference (N : Node_Id) return Boolean is
14228
14229 function In_Access_Definition (N : Node_Id) return Boolean;
14230 -- Returns true if N belongs to an access definition
14231
14232 --------------------------
14233 -- In_Access_Definition --
14234 --------------------------
14235
14236 function In_Access_Definition (N : Node_Id) return Boolean is
14237 P : Node_Id;
14238
14239 begin
14240 P := Parent (N);
14241 while Present (P) loop
14242 if Nkind (P) = N_Access_Definition then
14243 return True;
14244 end if;
14245
14246 P := Parent (P);
14247 end loop;
14248
14249 return False;
14250 end In_Access_Definition;
14251
14252 -- Start of processing for Is_Protected_Self_Reference
14253
14254 begin
14255 -- Verify that prefix is analyzed and has the proper form. Note that
14256 -- the attributes Elab_Spec, Elab_Body, and Elab_Subp_Body, which also
14257 -- produce the address of an entity, do not analyze their prefix
14258 -- because they denote entities that are not necessarily visible.
14259 -- Neither of them can apply to a protected type.
14260
14261 return Ada_Version >= Ada_2005
14262 and then Is_Entity_Name (N)
14263 and then Present (Entity (N))
14264 and then Is_Protected_Type (Entity (N))
14265 and then In_Open_Scopes (Entity (N))
14266 and then not In_Access_Definition (N);
14267 end Is_Protected_Self_Reference;
14268
14269 -----------------------------
14270 -- Is_RCI_Pkg_Spec_Or_Body --
14271 -----------------------------
14272
14273 function Is_RCI_Pkg_Spec_Or_Body (Cunit : Node_Id) return Boolean is
14274
14275 function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean;
14276 -- Return True if the unit of Cunit is an RCI package declaration
14277
14278 ---------------------------
14279 -- Is_RCI_Pkg_Decl_Cunit --
14280 ---------------------------
14281
14282 function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean is
14283 The_Unit : constant Node_Id := Unit (Cunit);
14284
14285 begin
14286 if Nkind (The_Unit) /= N_Package_Declaration then
14287 return False;
14288 end if;
14289
14290 return Is_Remote_Call_Interface (Defining_Entity (The_Unit));
14291 end Is_RCI_Pkg_Decl_Cunit;
14292
14293 -- Start of processing for Is_RCI_Pkg_Spec_Or_Body
14294
14295 begin
14296 return Is_RCI_Pkg_Decl_Cunit (Cunit)
14297 or else
14298 (Nkind (Unit (Cunit)) = N_Package_Body
14299 and then Is_RCI_Pkg_Decl_Cunit (Library_Unit (Cunit)));
14300 end Is_RCI_Pkg_Spec_Or_Body;
14301
14302 -----------------------------------------
14303 -- Is_Remote_Access_To_Class_Wide_Type --
14304 -----------------------------------------
14305
14306 function Is_Remote_Access_To_Class_Wide_Type
14307 (E : Entity_Id) return Boolean
14308 is
14309 begin
14310 -- A remote access to class-wide type is a general access to object type
14311 -- declared in the visible part of a Remote_Types or Remote_Call_
14312 -- Interface unit.
14313
14314 return Ekind (E) = E_General_Access_Type
14315 and then (Is_Remote_Call_Interface (E) or else Is_Remote_Types (E));
14316 end Is_Remote_Access_To_Class_Wide_Type;
14317
14318 -----------------------------------------
14319 -- Is_Remote_Access_To_Subprogram_Type --
14320 -----------------------------------------
14321
14322 function Is_Remote_Access_To_Subprogram_Type
14323 (E : Entity_Id) return Boolean
14324 is
14325 begin
14326 return (Ekind (E) = E_Access_Subprogram_Type
14327 or else (Ekind (E) = E_Record_Type
14328 and then Present (Corresponding_Remote_Type (E))))
14329 and then (Is_Remote_Call_Interface (E) or else Is_Remote_Types (E));
14330 end Is_Remote_Access_To_Subprogram_Type;
14331
14332 --------------------
14333 -- Is_Remote_Call --
14334 --------------------
14335
14336 function Is_Remote_Call (N : Node_Id) return Boolean is
14337 begin
14338 if Nkind (N) not in N_Subprogram_Call then
14339
14340 -- An entry call cannot be remote
14341
14342 return False;
14343
14344 elsif Nkind (Name (N)) in N_Has_Entity
14345 and then Is_Remote_Call_Interface (Entity (Name (N)))
14346 then
14347 -- A subprogram declared in the spec of a RCI package is remote
14348
14349 return True;
14350
14351 elsif Nkind (Name (N)) = N_Explicit_Dereference
14352 and then Is_Remote_Access_To_Subprogram_Type
14353 (Etype (Prefix (Name (N))))
14354 then
14355 -- The dereference of a RAS is a remote call
14356
14357 return True;
14358
14359 elsif Present (Controlling_Argument (N))
14360 and then Is_Remote_Access_To_Class_Wide_Type
14361 (Etype (Controlling_Argument (N)))
14362 then
14363 -- Any primitive operation call with a controlling argument of
14364 -- a RACW type is a remote call.
14365
14366 return True;
14367 end if;
14368
14369 -- All other calls are local calls
14370
14371 return False;
14372 end Is_Remote_Call;
14373
14374 ----------------------
14375 -- Is_Renamed_Entry --
14376 ----------------------
14377
14378 function Is_Renamed_Entry (Proc_Nam : Entity_Id) return Boolean is
14379 Orig_Node : Node_Id := Empty;
14380 Subp_Decl : Node_Id := Parent (Parent (Proc_Nam));
14381
14382 function Is_Entry (Nam : Node_Id) return Boolean;
14383 -- Determine whether Nam is an entry. Traverse selectors if there are
14384 -- nested selected components.
14385
14386 --------------
14387 -- Is_Entry --
14388 --------------
14389
14390 function Is_Entry (Nam : Node_Id) return Boolean is
14391 begin
14392 if Nkind (Nam) = N_Selected_Component then
14393 return Is_Entry (Selector_Name (Nam));
14394 end if;
14395
14396 return Ekind (Entity (Nam)) = E_Entry;
14397 end Is_Entry;
14398
14399 -- Start of processing for Is_Renamed_Entry
14400
14401 begin
14402 if Present (Alias (Proc_Nam)) then
14403 Subp_Decl := Parent (Parent (Alias (Proc_Nam)));
14404 end if;
14405
14406 -- Look for a rewritten subprogram renaming declaration
14407
14408 if Nkind (Subp_Decl) = N_Subprogram_Declaration
14409 and then Present (Original_Node (Subp_Decl))
14410 then
14411 Orig_Node := Original_Node (Subp_Decl);
14412 end if;
14413
14414 -- The rewritten subprogram is actually an entry
14415
14416 if Present (Orig_Node)
14417 and then Nkind (Orig_Node) = N_Subprogram_Renaming_Declaration
14418 and then Is_Entry (Name (Orig_Node))
14419 then
14420 return True;
14421 end if;
14422
14423 return False;
14424 end Is_Renamed_Entry;
14425
14426 -----------------------------
14427 -- Is_Renaming_Declaration --
14428 -----------------------------
14429
14430 function Is_Renaming_Declaration (N : Node_Id) return Boolean is
14431 begin
14432 case Nkind (N) is
14433 when N_Exception_Renaming_Declaration |
14434 N_Generic_Function_Renaming_Declaration |
14435 N_Generic_Package_Renaming_Declaration |
14436 N_Generic_Procedure_Renaming_Declaration |
14437 N_Object_Renaming_Declaration |
14438 N_Package_Renaming_Declaration |
14439 N_Subprogram_Renaming_Declaration =>
14440 return True;
14441
14442 when others =>
14443 return False;
14444 end case;
14445 end Is_Renaming_Declaration;
14446
14447 ----------------------------
14448 -- Is_Reversible_Iterator --
14449 ----------------------------
14450
14451 function Is_Reversible_Iterator (Typ : Entity_Id) return Boolean is
14452 Ifaces_List : Elist_Id;
14453 Iface_Elmt : Elmt_Id;
14454 Iface : Entity_Id;
14455
14456 begin
14457 if Is_Class_Wide_Type (Typ)
14458 and then Chars (Root_Type (Typ)) = Name_Reversible_Iterator
14459 and then Is_Predefined_File_Name
14460 (Unit_File_Name (Get_Source_Unit (Root_Type (Typ))))
14461 then
14462 return True;
14463
14464 elsif not Is_Tagged_Type (Typ) or else not Is_Derived_Type (Typ) then
14465 return False;
14466
14467 else
14468 Collect_Interfaces (Typ, Ifaces_List);
14469
14470 Iface_Elmt := First_Elmt (Ifaces_List);
14471 while Present (Iface_Elmt) loop
14472 Iface := Node (Iface_Elmt);
14473 if Chars (Iface) = Name_Reversible_Iterator
14474 and then
14475 Is_Predefined_File_Name
14476 (Unit_File_Name (Get_Source_Unit (Iface)))
14477 then
14478 return True;
14479 end if;
14480
14481 Next_Elmt (Iface_Elmt);
14482 end loop;
14483 end if;
14484
14485 return False;
14486 end Is_Reversible_Iterator;
14487
14488 ----------------------
14489 -- Is_Selector_Name --
14490 ----------------------
14491
14492 function Is_Selector_Name (N : Node_Id) return Boolean is
14493 begin
14494 if not Is_List_Member (N) then
14495 declare
14496 P : constant Node_Id := Parent (N);
14497 begin
14498 return Nkind_In (P, N_Expanded_Name,
14499 N_Generic_Association,
14500 N_Parameter_Association,
14501 N_Selected_Component)
14502 and then Selector_Name (P) = N;
14503 end;
14504
14505 else
14506 declare
14507 L : constant List_Id := List_Containing (N);
14508 P : constant Node_Id := Parent (L);
14509 begin
14510 return (Nkind (P) = N_Discriminant_Association
14511 and then Selector_Names (P) = L)
14512 or else
14513 (Nkind (P) = N_Component_Association
14514 and then Choices (P) = L);
14515 end;
14516 end if;
14517 end Is_Selector_Name;
14518
14519 ---------------------------------
14520 -- Is_Single_Concurrent_Object --
14521 ---------------------------------
14522
14523 function Is_Single_Concurrent_Object (Id : Entity_Id) return Boolean is
14524 begin
14525 return
14526 Is_Single_Protected_Object (Id) or else Is_Single_Task_Object (Id);
14527 end Is_Single_Concurrent_Object;
14528
14529 -------------------------------
14530 -- Is_Single_Concurrent_Type --
14531 -------------------------------
14532
14533 function Is_Single_Concurrent_Type (Id : Entity_Id) return Boolean is
14534 begin
14535 return
14536 Ekind_In (Id, E_Protected_Type, E_Task_Type)
14537 and then Is_Single_Concurrent_Type_Declaration
14538 (Declaration_Node (Id));
14539 end Is_Single_Concurrent_Type;
14540
14541 -------------------------------------------
14542 -- Is_Single_Concurrent_Type_Declaration --
14543 -------------------------------------------
14544
14545 function Is_Single_Concurrent_Type_Declaration
14546 (N : Node_Id) return Boolean
14547 is
14548 begin
14549 return Nkind_In (Original_Node (N), N_Single_Protected_Declaration,
14550 N_Single_Task_Declaration);
14551 end Is_Single_Concurrent_Type_Declaration;
14552
14553 ---------------------------------------------
14554 -- Is_Single_Precision_Floating_Point_Type --
14555 ---------------------------------------------
14556
14557 function Is_Single_Precision_Floating_Point_Type
14558 (E : Entity_Id) return Boolean is
14559 begin
14560 return Is_Floating_Point_Type (E)
14561 and then Machine_Radix_Value (E) = Uint_2
14562 and then Machine_Mantissa_Value (E) = Uint_24
14563 and then Machine_Emax_Value (E) = Uint_2 ** Uint_7
14564 and then Machine_Emin_Value (E) = Uint_3 - (Uint_2 ** Uint_7);
14565 end Is_Single_Precision_Floating_Point_Type;
14566
14567 --------------------------------
14568 -- Is_Single_Protected_Object --
14569 --------------------------------
14570
14571 function Is_Single_Protected_Object (Id : Entity_Id) return Boolean is
14572 begin
14573 return
14574 Ekind (Id) = E_Variable
14575 and then Ekind (Etype (Id)) = E_Protected_Type
14576 and then Is_Single_Concurrent_Type (Etype (Id));
14577 end Is_Single_Protected_Object;
14578
14579 ---------------------------
14580 -- Is_Single_Task_Object --
14581 ---------------------------
14582
14583 function Is_Single_Task_Object (Id : Entity_Id) return Boolean is
14584 begin
14585 return
14586 Ekind (Id) = E_Variable
14587 and then Ekind (Etype (Id)) = E_Task_Type
14588 and then Is_Single_Concurrent_Type (Etype (Id));
14589 end Is_Single_Task_Object;
14590
14591 -------------------------------------
14592 -- Is_SPARK_05_Initialization_Expr --
14593 -------------------------------------
14594
14595 function Is_SPARK_05_Initialization_Expr (N : Node_Id) return Boolean is
14596 Is_Ok : Boolean;
14597 Expr : Node_Id;
14598 Comp_Assn : Node_Id;
14599 Orig_N : constant Node_Id := Original_Node (N);
14600
14601 begin
14602 Is_Ok := True;
14603
14604 if not Comes_From_Source (Orig_N) then
14605 goto Done;
14606 end if;
14607
14608 pragma Assert (Nkind (Orig_N) in N_Subexpr);
14609
14610 case Nkind (Orig_N) is
14611 when N_Character_Literal |
14612 N_Integer_Literal |
14613 N_Real_Literal |
14614 N_String_Literal =>
14615 null;
14616
14617 when N_Identifier |
14618 N_Expanded_Name =>
14619 if Is_Entity_Name (Orig_N)
14620 and then Present (Entity (Orig_N)) -- needed in some cases
14621 then
14622 case Ekind (Entity (Orig_N)) is
14623 when E_Constant |
14624 E_Enumeration_Literal |
14625 E_Named_Integer |
14626 E_Named_Real =>
14627 null;
14628 when others =>
14629 if Is_Type (Entity (Orig_N)) then
14630 null;
14631 else
14632 Is_Ok := False;
14633 end if;
14634 end case;
14635 end if;
14636
14637 when N_Qualified_Expression |
14638 N_Type_Conversion =>
14639 Is_Ok := Is_SPARK_05_Initialization_Expr (Expression (Orig_N));
14640
14641 when N_Unary_Op =>
14642 Is_Ok := Is_SPARK_05_Initialization_Expr (Right_Opnd (Orig_N));
14643
14644 when N_Binary_Op |
14645 N_Short_Circuit |
14646 N_Membership_Test =>
14647 Is_Ok := Is_SPARK_05_Initialization_Expr (Left_Opnd (Orig_N))
14648 and then
14649 Is_SPARK_05_Initialization_Expr (Right_Opnd (Orig_N));
14650
14651 when N_Aggregate |
14652 N_Extension_Aggregate =>
14653 if Nkind (Orig_N) = N_Extension_Aggregate then
14654 Is_Ok :=
14655 Is_SPARK_05_Initialization_Expr (Ancestor_Part (Orig_N));
14656 end if;
14657
14658 Expr := First (Expressions (Orig_N));
14659 while Present (Expr) loop
14660 if not Is_SPARK_05_Initialization_Expr (Expr) then
14661 Is_Ok := False;
14662 goto Done;
14663 end if;
14664
14665 Next (Expr);
14666 end loop;
14667
14668 Comp_Assn := First (Component_Associations (Orig_N));
14669 while Present (Comp_Assn) loop
14670 Expr := Expression (Comp_Assn);
14671
14672 -- Note: test for Present here needed for box assocation
14673
14674 if Present (Expr)
14675 and then not Is_SPARK_05_Initialization_Expr (Expr)
14676 then
14677 Is_Ok := False;
14678 goto Done;
14679 end if;
14680
14681 Next (Comp_Assn);
14682 end loop;
14683
14684 when N_Attribute_Reference =>
14685 if Nkind (Prefix (Orig_N)) in N_Subexpr then
14686 Is_Ok := Is_SPARK_05_Initialization_Expr (Prefix (Orig_N));
14687 end if;
14688
14689 Expr := First (Expressions (Orig_N));
14690 while Present (Expr) loop
14691 if not Is_SPARK_05_Initialization_Expr (Expr) then
14692 Is_Ok := False;
14693 goto Done;
14694 end if;
14695
14696 Next (Expr);
14697 end loop;
14698
14699 -- Selected components might be expanded named not yet resolved, so
14700 -- default on the safe side. (Eg on sparklex.ads)
14701
14702 when N_Selected_Component =>
14703 null;
14704
14705 when others =>
14706 Is_Ok := False;
14707 end case;
14708
14709 <<Done>>
14710 return Is_Ok;
14711 end Is_SPARK_05_Initialization_Expr;
14712
14713 ----------------------------------
14714 -- Is_SPARK_05_Object_Reference --
14715 ----------------------------------
14716
14717 function Is_SPARK_05_Object_Reference (N : Node_Id) return Boolean is
14718 begin
14719 if Is_Entity_Name (N) then
14720 return Present (Entity (N))
14721 and then
14722 (Ekind_In (Entity (N), E_Constant, E_Variable)
14723 or else Ekind (Entity (N)) in Formal_Kind);
14724
14725 else
14726 case Nkind (N) is
14727 when N_Selected_Component =>
14728 return Is_SPARK_05_Object_Reference (Prefix (N));
14729
14730 when others =>
14731 return False;
14732 end case;
14733 end if;
14734 end Is_SPARK_05_Object_Reference;
14735
14736 -----------------------------
14737 -- Is_Specific_Tagged_Type --
14738 -----------------------------
14739
14740 function Is_Specific_Tagged_Type (Typ : Entity_Id) return Boolean is
14741 Full_Typ : Entity_Id;
14742
14743 begin
14744 -- Handle private types
14745
14746 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
14747 Full_Typ := Full_View (Typ);
14748 else
14749 Full_Typ := Typ;
14750 end if;
14751
14752 -- A specific tagged type is a non-class-wide tagged type
14753
14754 return Is_Tagged_Type (Full_Typ) and not Is_Class_Wide_Type (Full_Typ);
14755 end Is_Specific_Tagged_Type;
14756
14757 ------------------
14758 -- Is_Statement --
14759 ------------------
14760
14761 function Is_Statement (N : Node_Id) return Boolean is
14762 begin
14763 return
14764 Nkind (N) in N_Statement_Other_Than_Procedure_Call
14765 or else Nkind (N) = N_Procedure_Call_Statement;
14766 end Is_Statement;
14767
14768 ---------------------------------------
14769 -- Is_Subprogram_Contract_Annotation --
14770 ---------------------------------------
14771
14772 function Is_Subprogram_Contract_Annotation
14773 (Item : Node_Id) return Boolean
14774 is
14775 Nam : Name_Id;
14776
14777 begin
14778 if Nkind (Item) = N_Aspect_Specification then
14779 Nam := Chars (Identifier (Item));
14780
14781 else pragma Assert (Nkind (Item) = N_Pragma);
14782 Nam := Pragma_Name (Item);
14783 end if;
14784
14785 return Nam = Name_Contract_Cases
14786 or else Nam = Name_Depends
14787 or else Nam = Name_Extensions_Visible
14788 or else Nam = Name_Global
14789 or else Nam = Name_Post
14790 or else Nam = Name_Post_Class
14791 or else Nam = Name_Postcondition
14792 or else Nam = Name_Pre
14793 or else Nam = Name_Pre_Class
14794 or else Nam = Name_Precondition
14795 or else Nam = Name_Refined_Depends
14796 or else Nam = Name_Refined_Global
14797 or else Nam = Name_Refined_Post
14798 or else Nam = Name_Test_Case;
14799 end Is_Subprogram_Contract_Annotation;
14800
14801 --------------------------------------------------
14802 -- Is_Subprogram_Stub_Without_Prior_Declaration --
14803 --------------------------------------------------
14804
14805 function Is_Subprogram_Stub_Without_Prior_Declaration
14806 (N : Node_Id) return Boolean
14807 is
14808 begin
14809 -- A subprogram stub without prior declaration serves as declaration for
14810 -- the actual subprogram body. As such, it has an attached defining
14811 -- entity of E_[Generic_]Function or E_[Generic_]Procedure.
14812
14813 return Nkind (N) = N_Subprogram_Body_Stub
14814 and then Ekind (Defining_Entity (N)) /= E_Subprogram_Body;
14815 end Is_Subprogram_Stub_Without_Prior_Declaration;
14816
14817 --------------------------
14818 -- Is_Suspension_Object --
14819 --------------------------
14820
14821 function Is_Suspension_Object (Id : Entity_Id) return Boolean is
14822 begin
14823 -- This approach does an exact name match rather than to rely on
14824 -- RTSfind. Routine Is_Effectively_Volatile is used by clients of the
14825 -- front end at point where all auxiliary tables are locked and any
14826 -- modifications to them are treated as violations. Do not tamper with
14827 -- the tables, instead examine the Chars fields of all the scopes of Id.
14828
14829 return
14830 Chars (Id) = Name_Suspension_Object
14831 and then Present (Scope (Id))
14832 and then Chars (Scope (Id)) = Name_Synchronous_Task_Control
14833 and then Present (Scope (Scope (Id)))
14834 and then Chars (Scope (Scope (Id))) = Name_Ada
14835 and then Present (Scope (Scope (Scope (Id))))
14836 and then Scope (Scope (Scope (Id))) = Standard_Standard;
14837 end Is_Suspension_Object;
14838
14839 ----------------------------
14840 -- Is_Synchronized_Object --
14841 ----------------------------
14842
14843 function Is_Synchronized_Object (Id : Entity_Id) return Boolean is
14844 Prag : Node_Id;
14845
14846 begin
14847 if Is_Object (Id) then
14848
14849 -- The object is synchronized if it is of a type that yields a
14850 -- synchronized object.
14851
14852 if Yields_Synchronized_Object (Etype (Id)) then
14853 return True;
14854
14855 -- The object is synchronized if it is atomic and Async_Writers is
14856 -- enabled.
14857
14858 elsif Is_Atomic (Id) and then Async_Writers_Enabled (Id) then
14859 return True;
14860
14861 -- A constant is a synchronized object by default
14862
14863 elsif Ekind (Id) = E_Constant then
14864 return True;
14865
14866 -- A variable is a synchronized object if it is subject to pragma
14867 -- Constant_After_Elaboration.
14868
14869 elsif Ekind (Id) = E_Variable then
14870 Prag := Get_Pragma (Id, Pragma_Constant_After_Elaboration);
14871
14872 return Present (Prag) and then Is_Enabled_Pragma (Prag);
14873 end if;
14874 end if;
14875
14876 -- Otherwise the input is not an object or it does not qualify as a
14877 -- synchronized object.
14878
14879 return False;
14880 end Is_Synchronized_Object;
14881
14882 ---------------------------------
14883 -- Is_Synchronized_Tagged_Type --
14884 ---------------------------------
14885
14886 function Is_Synchronized_Tagged_Type (E : Entity_Id) return Boolean is
14887 Kind : constant Entity_Kind := Ekind (Base_Type (E));
14888
14889 begin
14890 -- A task or protected type derived from an interface is a tagged type.
14891 -- Such a tagged type is called a synchronized tagged type, as are
14892 -- synchronized interfaces and private extensions whose declaration
14893 -- includes the reserved word synchronized.
14894
14895 return (Is_Tagged_Type (E)
14896 and then (Kind = E_Task_Type
14897 or else
14898 Kind = E_Protected_Type))
14899 or else
14900 (Is_Interface (E)
14901 and then Is_Synchronized_Interface (E))
14902 or else
14903 (Ekind (E) = E_Record_Type_With_Private
14904 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
14905 and then (Synchronized_Present (Parent (E))
14906 or else Is_Synchronized_Interface (Etype (E))));
14907 end Is_Synchronized_Tagged_Type;
14908
14909 -----------------
14910 -- Is_Transfer --
14911 -----------------
14912
14913 function Is_Transfer (N : Node_Id) return Boolean is
14914 Kind : constant Node_Kind := Nkind (N);
14915
14916 begin
14917 if Kind = N_Simple_Return_Statement
14918 or else
14919 Kind = N_Extended_Return_Statement
14920 or else
14921 Kind = N_Goto_Statement
14922 or else
14923 Kind = N_Raise_Statement
14924 or else
14925 Kind = N_Requeue_Statement
14926 then
14927 return True;
14928
14929 elsif (Kind = N_Exit_Statement or else Kind in N_Raise_xxx_Error)
14930 and then No (Condition (N))
14931 then
14932 return True;
14933
14934 elsif Kind = N_Procedure_Call_Statement
14935 and then Is_Entity_Name (Name (N))
14936 and then Present (Entity (Name (N)))
14937 and then No_Return (Entity (Name (N)))
14938 then
14939 return True;
14940
14941 elsif Nkind (Original_Node (N)) = N_Raise_Statement then
14942 return True;
14943
14944 else
14945 return False;
14946 end if;
14947 end Is_Transfer;
14948
14949 -------------
14950 -- Is_True --
14951 -------------
14952
14953 function Is_True (U : Uint) return Boolean is
14954 begin
14955 return (U /= 0);
14956 end Is_True;
14957
14958 --------------------------------------
14959 -- Is_Unchecked_Conversion_Instance --
14960 --------------------------------------
14961
14962 function Is_Unchecked_Conversion_Instance (Id : Entity_Id) return Boolean is
14963 Par : Node_Id;
14964
14965 begin
14966 -- Look for a function whose generic parent is the predefined intrinsic
14967 -- function Unchecked_Conversion, or for one that renames such an
14968 -- instance.
14969
14970 if Ekind (Id) = E_Function then
14971 Par := Parent (Id);
14972
14973 if Nkind (Par) = N_Function_Specification then
14974 Par := Generic_Parent (Par);
14975
14976 if Present (Par) then
14977 return
14978 Chars (Par) = Name_Unchecked_Conversion
14979 and then Is_Intrinsic_Subprogram (Par)
14980 and then Is_Predefined_File_Name
14981 (Unit_File_Name (Get_Source_Unit (Par)));
14982 else
14983 return
14984 Present (Alias (Id))
14985 and then Is_Unchecked_Conversion_Instance (Alias (Id));
14986 end if;
14987 end if;
14988 end if;
14989
14990 return False;
14991 end Is_Unchecked_Conversion_Instance;
14992
14993 -------------------------------
14994 -- Is_Universal_Numeric_Type --
14995 -------------------------------
14996
14997 function Is_Universal_Numeric_Type (T : Entity_Id) return Boolean is
14998 begin
14999 return T = Universal_Integer or else T = Universal_Real;
15000 end Is_Universal_Numeric_Type;
15001
15002 ----------------------------
15003 -- Is_Variable_Size_Array --
15004 ----------------------------
15005
15006 function Is_Variable_Size_Array (E : Entity_Id) return Boolean is
15007 Idx : Node_Id;
15008
15009 begin
15010 pragma Assert (Is_Array_Type (E));
15011
15012 -- Check if some index is initialized with a non-constant value
15013
15014 Idx := First_Index (E);
15015 while Present (Idx) loop
15016 if Nkind (Idx) = N_Range then
15017 if not Is_Constant_Bound (Low_Bound (Idx))
15018 or else not Is_Constant_Bound (High_Bound (Idx))
15019 then
15020 return True;
15021 end if;
15022 end if;
15023
15024 Idx := Next_Index (Idx);
15025 end loop;
15026
15027 return False;
15028 end Is_Variable_Size_Array;
15029
15030 -----------------------------
15031 -- Is_Variable_Size_Record --
15032 -----------------------------
15033
15034 function Is_Variable_Size_Record (E : Entity_Id) return Boolean is
15035 Comp : Entity_Id;
15036 Comp_Typ : Entity_Id;
15037
15038 begin
15039 pragma Assert (Is_Record_Type (E));
15040
15041 Comp := First_Entity (E);
15042 while Present (Comp) loop
15043 Comp_Typ := Etype (Comp);
15044
15045 -- Recursive call if the record type has discriminants
15046
15047 if Is_Record_Type (Comp_Typ)
15048 and then Has_Discriminants (Comp_Typ)
15049 and then Is_Variable_Size_Record (Comp_Typ)
15050 then
15051 return True;
15052
15053 elsif Is_Array_Type (Comp_Typ)
15054 and then Is_Variable_Size_Array (Comp_Typ)
15055 then
15056 return True;
15057 end if;
15058
15059 Next_Entity (Comp);
15060 end loop;
15061
15062 return False;
15063 end Is_Variable_Size_Record;
15064
15065 -----------------
15066 -- Is_Variable --
15067 -----------------
15068
15069 function Is_Variable
15070 (N : Node_Id;
15071 Use_Original_Node : Boolean := True) return Boolean
15072 is
15073 Orig_Node : Node_Id;
15074
15075 function In_Protected_Function (E : Entity_Id) return Boolean;
15076 -- Within a protected function, the private components of the enclosing
15077 -- protected type are constants. A function nested within a (protected)
15078 -- procedure is not itself protected. Within the body of a protected
15079 -- function the current instance of the protected type is a constant.
15080
15081 function Is_Variable_Prefix (P : Node_Id) return Boolean;
15082 -- Prefixes can involve implicit dereferences, in which case we must
15083 -- test for the case of a reference of a constant access type, which can
15084 -- can never be a variable.
15085
15086 ---------------------------
15087 -- In_Protected_Function --
15088 ---------------------------
15089
15090 function In_Protected_Function (E : Entity_Id) return Boolean is
15091 Prot : Entity_Id;
15092 S : Entity_Id;
15093
15094 begin
15095 -- E is the current instance of a type
15096
15097 if Is_Type (E) then
15098 Prot := E;
15099
15100 -- E is an object
15101
15102 else
15103 Prot := Scope (E);
15104 end if;
15105
15106 if not Is_Protected_Type (Prot) then
15107 return False;
15108
15109 else
15110 S := Current_Scope;
15111 while Present (S) and then S /= Prot loop
15112 if Ekind (S) = E_Function and then Scope (S) = Prot then
15113 return True;
15114 end if;
15115
15116 S := Scope (S);
15117 end loop;
15118
15119 return False;
15120 end if;
15121 end In_Protected_Function;
15122
15123 ------------------------
15124 -- Is_Variable_Prefix --
15125 ------------------------
15126
15127 function Is_Variable_Prefix (P : Node_Id) return Boolean is
15128 begin
15129 if Is_Access_Type (Etype (P)) then
15130 return not Is_Access_Constant (Root_Type (Etype (P)));
15131
15132 -- For the case of an indexed component whose prefix has a packed
15133 -- array type, the prefix has been rewritten into a type conversion.
15134 -- Determine variable-ness from the converted expression.
15135
15136 elsif Nkind (P) = N_Type_Conversion
15137 and then not Comes_From_Source (P)
15138 and then Is_Array_Type (Etype (P))
15139 and then Is_Packed (Etype (P))
15140 then
15141 return Is_Variable (Expression (P));
15142
15143 else
15144 return Is_Variable (P);
15145 end if;
15146 end Is_Variable_Prefix;
15147
15148 -- Start of processing for Is_Variable
15149
15150 begin
15151 -- Special check, allow x'Deref(expr) as a variable
15152
15153 if Nkind (N) = N_Attribute_Reference
15154 and then Attribute_Name (N) = Name_Deref
15155 then
15156 return True;
15157 end if;
15158
15159 -- Check if we perform the test on the original node since this may be a
15160 -- test of syntactic categories which must not be disturbed by whatever
15161 -- rewriting might have occurred. For example, an aggregate, which is
15162 -- certainly NOT a variable, could be turned into a variable by
15163 -- expansion.
15164
15165 if Use_Original_Node then
15166 Orig_Node := Original_Node (N);
15167 else
15168 Orig_Node := N;
15169 end if;
15170
15171 -- Definitely OK if Assignment_OK is set. Since this is something that
15172 -- only gets set for expanded nodes, the test is on N, not Orig_Node.
15173
15174 if Nkind (N) in N_Subexpr and then Assignment_OK (N) then
15175 return True;
15176
15177 -- Normally we go to the original node, but there is one exception where
15178 -- we use the rewritten node, namely when it is an explicit dereference.
15179 -- The generated code may rewrite a prefix which is an access type with
15180 -- an explicit dereference. The dereference is a variable, even though
15181 -- the original node may not be (since it could be a constant of the
15182 -- access type).
15183
15184 -- In Ada 2005 we have a further case to consider: the prefix may be a
15185 -- function call given in prefix notation. The original node appears to
15186 -- be a selected component, but we need to examine the call.
15187
15188 elsif Nkind (N) = N_Explicit_Dereference
15189 and then Nkind (Orig_Node) /= N_Explicit_Dereference
15190 and then Present (Etype (Orig_Node))
15191 and then Is_Access_Type (Etype (Orig_Node))
15192 then
15193 -- Note that if the prefix is an explicit dereference that does not
15194 -- come from source, we must check for a rewritten function call in
15195 -- prefixed notation before other forms of rewriting, to prevent a
15196 -- compiler crash.
15197
15198 return
15199 (Nkind (Orig_Node) = N_Function_Call
15200 and then not Is_Access_Constant (Etype (Prefix (N))))
15201 or else
15202 Is_Variable_Prefix (Original_Node (Prefix (N)));
15203
15204 -- in Ada 2012, the dereference may have been added for a type with
15205 -- a declared implicit dereference aspect. Check that it is not an
15206 -- access to constant.
15207
15208 elsif Nkind (N) = N_Explicit_Dereference
15209 and then Present (Etype (Orig_Node))
15210 and then Ada_Version >= Ada_2012
15211 and then Has_Implicit_Dereference (Etype (Orig_Node))
15212 then
15213 return not Is_Access_Constant (Etype (Prefix (N)));
15214
15215 -- A function call is never a variable
15216
15217 elsif Nkind (N) = N_Function_Call then
15218 return False;
15219
15220 -- All remaining checks use the original node
15221
15222 elsif Is_Entity_Name (Orig_Node)
15223 and then Present (Entity (Orig_Node))
15224 then
15225 declare
15226 E : constant Entity_Id := Entity (Orig_Node);
15227 K : constant Entity_Kind := Ekind (E);
15228
15229 begin
15230 return (K = E_Variable
15231 and then Nkind (Parent (E)) /= N_Exception_Handler)
15232 or else (K = E_Component
15233 and then not In_Protected_Function (E))
15234 or else K = E_Out_Parameter
15235 or else K = E_In_Out_Parameter
15236 or else K = E_Generic_In_Out_Parameter
15237
15238 -- Current instance of type. If this is a protected type, check
15239 -- we are not within the body of one of its protected functions.
15240
15241 or else (Is_Type (E)
15242 and then In_Open_Scopes (E)
15243 and then not In_Protected_Function (E))
15244
15245 or else (Is_Incomplete_Or_Private_Type (E)
15246 and then In_Open_Scopes (Full_View (E)));
15247 end;
15248
15249 else
15250 case Nkind (Orig_Node) is
15251 when N_Indexed_Component | N_Slice =>
15252 return Is_Variable_Prefix (Prefix (Orig_Node));
15253
15254 when N_Selected_Component =>
15255 return (Is_Variable (Selector_Name (Orig_Node))
15256 and then Is_Variable_Prefix (Prefix (Orig_Node)))
15257 or else
15258 (Nkind (N) = N_Expanded_Name
15259 and then Scope (Entity (N)) = Entity (Prefix (N)));
15260
15261 -- For an explicit dereference, the type of the prefix cannot
15262 -- be an access to constant or an access to subprogram.
15263
15264 when N_Explicit_Dereference =>
15265 declare
15266 Typ : constant Entity_Id := Etype (Prefix (Orig_Node));
15267 begin
15268 return Is_Access_Type (Typ)
15269 and then not Is_Access_Constant (Root_Type (Typ))
15270 and then Ekind (Typ) /= E_Access_Subprogram_Type;
15271 end;
15272
15273 -- The type conversion is the case where we do not deal with the
15274 -- context dependent special case of an actual parameter. Thus
15275 -- the type conversion is only considered a variable for the
15276 -- purposes of this routine if the target type is tagged. However,
15277 -- a type conversion is considered to be a variable if it does not
15278 -- come from source (this deals for example with the conversions
15279 -- of expressions to their actual subtypes).
15280
15281 when N_Type_Conversion =>
15282 return Is_Variable (Expression (Orig_Node))
15283 and then
15284 (not Comes_From_Source (Orig_Node)
15285 or else
15286 (Is_Tagged_Type (Etype (Subtype_Mark (Orig_Node)))
15287 and then
15288 Is_Tagged_Type (Etype (Expression (Orig_Node)))));
15289
15290 -- GNAT allows an unchecked type conversion as a variable. This
15291 -- only affects the generation of internal expanded code, since
15292 -- calls to instantiations of Unchecked_Conversion are never
15293 -- considered variables (since they are function calls).
15294
15295 when N_Unchecked_Type_Conversion =>
15296 return Is_Variable (Expression (Orig_Node));
15297
15298 when others =>
15299 return False;
15300 end case;
15301 end if;
15302 end Is_Variable;
15303
15304 ---------------------------
15305 -- Is_Visibly_Controlled --
15306 ---------------------------
15307
15308 function Is_Visibly_Controlled (T : Entity_Id) return Boolean is
15309 Root : constant Entity_Id := Root_Type (T);
15310 begin
15311 return Chars (Scope (Root)) = Name_Finalization
15312 and then Chars (Scope (Scope (Root))) = Name_Ada
15313 and then Scope (Scope (Scope (Root))) = Standard_Standard;
15314 end Is_Visibly_Controlled;
15315
15316 --------------------------
15317 -- Is_Volatile_Function --
15318 --------------------------
15319
15320 function Is_Volatile_Function (Func_Id : Entity_Id) return Boolean is
15321 begin
15322 pragma Assert (Ekind_In (Func_Id, E_Function, E_Generic_Function));
15323
15324 -- A function declared within a protected type is volatile
15325
15326 if Is_Protected_Type (Scope (Func_Id)) then
15327 return True;
15328
15329 -- An instance of Ada.Unchecked_Conversion is a volatile function if
15330 -- either the source or the target are effectively volatile.
15331
15332 elsif Is_Unchecked_Conversion_Instance (Func_Id)
15333 and then Has_Effectively_Volatile_Profile (Func_Id)
15334 then
15335 return True;
15336
15337 -- Otherwise the function is treated as volatile if it is subject to
15338 -- enabled pragma Volatile_Function.
15339
15340 else
15341 return
15342 Is_Enabled_Pragma (Get_Pragma (Func_Id, Pragma_Volatile_Function));
15343 end if;
15344 end Is_Volatile_Function;
15345
15346 ------------------------
15347 -- Is_Volatile_Object --
15348 ------------------------
15349
15350 function Is_Volatile_Object (N : Node_Id) return Boolean is
15351
15352 function Is_Volatile_Prefix (N : Node_Id) return Boolean;
15353 -- If prefix is an implicit dereference, examine designated type
15354
15355 function Object_Has_Volatile_Components (N : Node_Id) return Boolean;
15356 -- Determines if given object has volatile components
15357
15358 ------------------------
15359 -- Is_Volatile_Prefix --
15360 ------------------------
15361
15362 function Is_Volatile_Prefix (N : Node_Id) return Boolean is
15363 Typ : constant Entity_Id := Etype (N);
15364
15365 begin
15366 if Is_Access_Type (Typ) then
15367 declare
15368 Dtyp : constant Entity_Id := Designated_Type (Typ);
15369
15370 begin
15371 return Is_Volatile (Dtyp)
15372 or else Has_Volatile_Components (Dtyp);
15373 end;
15374
15375 else
15376 return Object_Has_Volatile_Components (N);
15377 end if;
15378 end Is_Volatile_Prefix;
15379
15380 ------------------------------------
15381 -- Object_Has_Volatile_Components --
15382 ------------------------------------
15383
15384 function Object_Has_Volatile_Components (N : Node_Id) return Boolean is
15385 Typ : constant Entity_Id := Etype (N);
15386
15387 begin
15388 if Is_Volatile (Typ)
15389 or else Has_Volatile_Components (Typ)
15390 then
15391 return True;
15392
15393 elsif Is_Entity_Name (N)
15394 and then (Has_Volatile_Components (Entity (N))
15395 or else Is_Volatile (Entity (N)))
15396 then
15397 return True;
15398
15399 elsif Nkind (N) = N_Indexed_Component
15400 or else Nkind (N) = N_Selected_Component
15401 then
15402 return Is_Volatile_Prefix (Prefix (N));
15403
15404 else
15405 return False;
15406 end if;
15407 end Object_Has_Volatile_Components;
15408
15409 -- Start of processing for Is_Volatile_Object
15410
15411 begin
15412 if Nkind (N) = N_Defining_Identifier then
15413 return Is_Volatile (N) or else Is_Volatile (Etype (N));
15414
15415 elsif Nkind (N) = N_Expanded_Name then
15416 return Is_Volatile_Object (Entity (N));
15417
15418 elsif Is_Volatile (Etype (N))
15419 or else (Is_Entity_Name (N) and then Is_Volatile (Entity (N)))
15420 then
15421 return True;
15422
15423 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component)
15424 and then Is_Volatile_Prefix (Prefix (N))
15425 then
15426 return True;
15427
15428 elsif Nkind (N) = N_Selected_Component
15429 and then Is_Volatile (Entity (Selector_Name (N)))
15430 then
15431 return True;
15432
15433 else
15434 return False;
15435 end if;
15436 end Is_Volatile_Object;
15437
15438 ---------------------------
15439 -- Itype_Has_Declaration --
15440 ---------------------------
15441
15442 function Itype_Has_Declaration (Id : Entity_Id) return Boolean is
15443 begin
15444 pragma Assert (Is_Itype (Id));
15445 return Present (Parent (Id))
15446 and then Nkind_In (Parent (Id), N_Full_Type_Declaration,
15447 N_Subtype_Declaration)
15448 and then Defining_Entity (Parent (Id)) = Id;
15449 end Itype_Has_Declaration;
15450
15451 -------------------------
15452 -- Kill_Current_Values --
15453 -------------------------
15454
15455 procedure Kill_Current_Values
15456 (Ent : Entity_Id;
15457 Last_Assignment_Only : Boolean := False)
15458 is
15459 begin
15460 if Is_Assignable (Ent) then
15461 Set_Last_Assignment (Ent, Empty);
15462 end if;
15463
15464 if Is_Object (Ent) then
15465 if not Last_Assignment_Only then
15466 Kill_Checks (Ent);
15467 Set_Current_Value (Ent, Empty);
15468
15469 -- Do not reset the Is_Known_[Non_]Null and Is_Known_Valid flags
15470 -- for a constant. Once the constant is elaborated, its value is
15471 -- not changed, therefore the associated flags that describe the
15472 -- value should not be modified either.
15473
15474 if Ekind (Ent) = E_Constant then
15475 null;
15476
15477 -- Non-constant entities
15478
15479 else
15480 if not Can_Never_Be_Null (Ent) then
15481 Set_Is_Known_Non_Null (Ent, False);
15482 end if;
15483
15484 Set_Is_Known_Null (Ent, False);
15485
15486 -- Reset the Is_Known_Valid flag unless the type is always
15487 -- valid. This does not apply to a loop parameter because its
15488 -- bounds are defined by the loop header and therefore always
15489 -- valid.
15490
15491 if not Is_Known_Valid (Etype (Ent))
15492 and then Ekind (Ent) /= E_Loop_Parameter
15493 then
15494 Set_Is_Known_Valid (Ent, False);
15495 end if;
15496 end if;
15497 end if;
15498 end if;
15499 end Kill_Current_Values;
15500
15501 procedure Kill_Current_Values (Last_Assignment_Only : Boolean := False) is
15502 S : Entity_Id;
15503
15504 procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id);
15505 -- Clear current value for entity E and all entities chained to E
15506
15507 ------------------------------------------
15508 -- Kill_Current_Values_For_Entity_Chain --
15509 ------------------------------------------
15510
15511 procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id) is
15512 Ent : Entity_Id;
15513 begin
15514 Ent := E;
15515 while Present (Ent) loop
15516 Kill_Current_Values (Ent, Last_Assignment_Only);
15517 Next_Entity (Ent);
15518 end loop;
15519 end Kill_Current_Values_For_Entity_Chain;
15520
15521 -- Start of processing for Kill_Current_Values
15522
15523 begin
15524 -- Kill all saved checks, a special case of killing saved values
15525
15526 if not Last_Assignment_Only then
15527 Kill_All_Checks;
15528 end if;
15529
15530 -- Loop through relevant scopes, which includes the current scope and
15531 -- any parent scopes if the current scope is a block or a package.
15532
15533 S := Current_Scope;
15534 Scope_Loop : loop
15535
15536 -- Clear current values of all entities in current scope
15537
15538 Kill_Current_Values_For_Entity_Chain (First_Entity (S));
15539
15540 -- If scope is a package, also clear current values of all private
15541 -- entities in the scope.
15542
15543 if Is_Package_Or_Generic_Package (S)
15544 or else Is_Concurrent_Type (S)
15545 then
15546 Kill_Current_Values_For_Entity_Chain (First_Private_Entity (S));
15547 end if;
15548
15549 -- If this is a not a subprogram, deal with parents
15550
15551 if not Is_Subprogram (S) then
15552 S := Scope (S);
15553 exit Scope_Loop when S = Standard_Standard;
15554 else
15555 exit Scope_Loop;
15556 end if;
15557 end loop Scope_Loop;
15558 end Kill_Current_Values;
15559
15560 --------------------------
15561 -- Kill_Size_Check_Code --
15562 --------------------------
15563
15564 procedure Kill_Size_Check_Code (E : Entity_Id) is
15565 begin
15566 if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
15567 and then Present (Size_Check_Code (E))
15568 then
15569 Remove (Size_Check_Code (E));
15570 Set_Size_Check_Code (E, Empty);
15571 end if;
15572 end Kill_Size_Check_Code;
15573
15574 --------------------------
15575 -- Known_To_Be_Assigned --
15576 --------------------------
15577
15578 function Known_To_Be_Assigned (N : Node_Id) return Boolean is
15579 P : constant Node_Id := Parent (N);
15580
15581 begin
15582 case Nkind (P) is
15583
15584 -- Test left side of assignment
15585
15586 when N_Assignment_Statement =>
15587 return N = Name (P);
15588
15589 -- Function call arguments are never lvalues
15590
15591 when N_Function_Call =>
15592 return False;
15593
15594 -- Positional parameter for procedure or accept call
15595
15596 when N_Procedure_Call_Statement |
15597 N_Accept_Statement
15598 =>
15599 declare
15600 Proc : Entity_Id;
15601 Form : Entity_Id;
15602 Act : Node_Id;
15603
15604 begin
15605 Proc := Get_Subprogram_Entity (P);
15606
15607 if No (Proc) then
15608 return False;
15609 end if;
15610
15611 -- If we are not a list member, something is strange, so
15612 -- be conservative and return False.
15613
15614 if not Is_List_Member (N) then
15615 return False;
15616 end if;
15617
15618 -- We are going to find the right formal by stepping forward
15619 -- through the formals, as we step backwards in the actuals.
15620
15621 Form := First_Formal (Proc);
15622 Act := N;
15623 loop
15624 -- If no formal, something is weird, so be conservative
15625 -- and return False.
15626
15627 if No (Form) then
15628 return False;
15629 end if;
15630
15631 Prev (Act);
15632 exit when No (Act);
15633 Next_Formal (Form);
15634 end loop;
15635
15636 return Ekind (Form) /= E_In_Parameter;
15637 end;
15638
15639 -- Named parameter for procedure or accept call
15640
15641 when N_Parameter_Association =>
15642 declare
15643 Proc : Entity_Id;
15644 Form : Entity_Id;
15645
15646 begin
15647 Proc := Get_Subprogram_Entity (Parent (P));
15648
15649 if No (Proc) then
15650 return False;
15651 end if;
15652
15653 -- Loop through formals to find the one that matches
15654
15655 Form := First_Formal (Proc);
15656 loop
15657 -- If no matching formal, that's peculiar, some kind of
15658 -- previous error, so return False to be conservative.
15659 -- Actually this also happens in legal code in the case
15660 -- where P is a parameter association for an Extra_Formal???
15661
15662 if No (Form) then
15663 return False;
15664 end if;
15665
15666 -- Else test for match
15667
15668 if Chars (Form) = Chars (Selector_Name (P)) then
15669 return Ekind (Form) /= E_In_Parameter;
15670 end if;
15671
15672 Next_Formal (Form);
15673 end loop;
15674 end;
15675
15676 -- Test for appearing in a conversion that itself appears
15677 -- in an lvalue context, since this should be an lvalue.
15678
15679 when N_Type_Conversion =>
15680 return Known_To_Be_Assigned (P);
15681
15682 -- All other references are definitely not known to be modifications
15683
15684 when others =>
15685 return False;
15686
15687 end case;
15688 end Known_To_Be_Assigned;
15689
15690 ---------------------------
15691 -- Last_Source_Statement --
15692 ---------------------------
15693
15694 function Last_Source_Statement (HSS : Node_Id) return Node_Id is
15695 N : Node_Id;
15696
15697 begin
15698 N := Last (Statements (HSS));
15699 while Present (N) loop
15700 exit when Comes_From_Source (N);
15701 Prev (N);
15702 end loop;
15703
15704 return N;
15705 end Last_Source_Statement;
15706
15707 ----------------------------------
15708 -- Matching_Static_Array_Bounds --
15709 ----------------------------------
15710
15711 function Matching_Static_Array_Bounds
15712 (L_Typ : Node_Id;
15713 R_Typ : Node_Id) return Boolean
15714 is
15715 L_Ndims : constant Nat := Number_Dimensions (L_Typ);
15716 R_Ndims : constant Nat := Number_Dimensions (R_Typ);
15717
15718 L_Index : Node_Id;
15719 R_Index : Node_Id;
15720 L_Low : Node_Id;
15721 L_High : Node_Id;
15722 L_Len : Uint;
15723 R_Low : Node_Id;
15724 R_High : Node_Id;
15725 R_Len : Uint;
15726
15727 begin
15728 if L_Ndims /= R_Ndims then
15729 return False;
15730 end if;
15731
15732 -- Unconstrained types do not have static bounds
15733
15734 if not Is_Constrained (L_Typ) or else not Is_Constrained (R_Typ) then
15735 return False;
15736 end if;
15737
15738 -- First treat specially the first dimension, as the lower bound and
15739 -- length of string literals are not stored like those of arrays.
15740
15741 if Ekind (L_Typ) = E_String_Literal_Subtype then
15742 L_Low := String_Literal_Low_Bound (L_Typ);
15743 L_Len := String_Literal_Length (L_Typ);
15744 else
15745 L_Index := First_Index (L_Typ);
15746 Get_Index_Bounds (L_Index, L_Low, L_High);
15747
15748 if Is_OK_Static_Expression (L_Low)
15749 and then
15750 Is_OK_Static_Expression (L_High)
15751 then
15752 if Expr_Value (L_High) < Expr_Value (L_Low) then
15753 L_Len := Uint_0;
15754 else
15755 L_Len := (Expr_Value (L_High) - Expr_Value (L_Low)) + 1;
15756 end if;
15757 else
15758 return False;
15759 end if;
15760 end if;
15761
15762 if Ekind (R_Typ) = E_String_Literal_Subtype then
15763 R_Low := String_Literal_Low_Bound (R_Typ);
15764 R_Len := String_Literal_Length (R_Typ);
15765 else
15766 R_Index := First_Index (R_Typ);
15767 Get_Index_Bounds (R_Index, R_Low, R_High);
15768
15769 if Is_OK_Static_Expression (R_Low)
15770 and then
15771 Is_OK_Static_Expression (R_High)
15772 then
15773 if Expr_Value (R_High) < Expr_Value (R_Low) then
15774 R_Len := Uint_0;
15775 else
15776 R_Len := (Expr_Value (R_High) - Expr_Value (R_Low)) + 1;
15777 end if;
15778 else
15779 return False;
15780 end if;
15781 end if;
15782
15783 if (Is_OK_Static_Expression (L_Low)
15784 and then
15785 Is_OK_Static_Expression (R_Low))
15786 and then Expr_Value (L_Low) = Expr_Value (R_Low)
15787 and then L_Len = R_Len
15788 then
15789 null;
15790 else
15791 return False;
15792 end if;
15793
15794 -- Then treat all other dimensions
15795
15796 for Indx in 2 .. L_Ndims loop
15797 Next (L_Index);
15798 Next (R_Index);
15799
15800 Get_Index_Bounds (L_Index, L_Low, L_High);
15801 Get_Index_Bounds (R_Index, R_Low, R_High);
15802
15803 if (Is_OK_Static_Expression (L_Low) and then
15804 Is_OK_Static_Expression (L_High) and then
15805 Is_OK_Static_Expression (R_Low) and then
15806 Is_OK_Static_Expression (R_High))
15807 and then (Expr_Value (L_Low) = Expr_Value (R_Low)
15808 and then
15809 Expr_Value (L_High) = Expr_Value (R_High))
15810 then
15811 null;
15812 else
15813 return False;
15814 end if;
15815 end loop;
15816
15817 -- If we fall through the loop, all indexes matched
15818
15819 return True;
15820 end Matching_Static_Array_Bounds;
15821
15822 -------------------
15823 -- May_Be_Lvalue --
15824 -------------------
15825
15826 function May_Be_Lvalue (N : Node_Id) return Boolean is
15827 P : constant Node_Id := Parent (N);
15828
15829 begin
15830 case Nkind (P) is
15831
15832 -- Test left side of assignment
15833
15834 when N_Assignment_Statement =>
15835 return N = Name (P);
15836
15837 -- Test prefix of component or attribute. Note that the prefix of an
15838 -- explicit or implicit dereference cannot be an l-value. In the case
15839 -- of a 'Read attribute, the reference can be an actual in the
15840 -- argument list of the attribute.
15841
15842 when N_Attribute_Reference =>
15843 return (N = Prefix (P)
15844 and then Name_Implies_Lvalue_Prefix (Attribute_Name (P)))
15845 or else
15846 Attribute_Name (P) = Name_Read;
15847
15848 -- For an expanded name, the name is an lvalue if the expanded name
15849 -- is an lvalue, but the prefix is never an lvalue, since it is just
15850 -- the scope where the name is found.
15851
15852 when N_Expanded_Name =>
15853 if N = Prefix (P) then
15854 return May_Be_Lvalue (P);
15855 else
15856 return False;
15857 end if;
15858
15859 -- For a selected component A.B, A is certainly an lvalue if A.B is.
15860 -- B is a little interesting, if we have A.B := 3, there is some
15861 -- discussion as to whether B is an lvalue or not, we choose to say
15862 -- it is. Note however that A is not an lvalue if it is of an access
15863 -- type since this is an implicit dereference.
15864
15865 when N_Selected_Component =>
15866 if N = Prefix (P)
15867 and then Present (Etype (N))
15868 and then Is_Access_Type (Etype (N))
15869 then
15870 return False;
15871 else
15872 return May_Be_Lvalue (P);
15873 end if;
15874
15875 -- For an indexed component or slice, the index or slice bounds is
15876 -- never an lvalue. The prefix is an lvalue if the indexed component
15877 -- or slice is an lvalue, except if it is an access type, where we
15878 -- have an implicit dereference.
15879
15880 when N_Indexed_Component | N_Slice =>
15881 if N /= Prefix (P)
15882 or else (Present (Etype (N)) and then Is_Access_Type (Etype (N)))
15883 then
15884 return False;
15885 else
15886 return May_Be_Lvalue (P);
15887 end if;
15888
15889 -- Prefix of a reference is an lvalue if the reference is an lvalue
15890
15891 when N_Reference =>
15892 return May_Be_Lvalue (P);
15893
15894 -- Prefix of explicit dereference is never an lvalue
15895
15896 when N_Explicit_Dereference =>
15897 return False;
15898
15899 -- Positional parameter for subprogram, entry, or accept call.
15900 -- In older versions of Ada function call arguments are never
15901 -- lvalues. In Ada 2012 functions can have in-out parameters.
15902
15903 when N_Subprogram_Call |
15904 N_Entry_Call_Statement |
15905 N_Accept_Statement
15906 =>
15907 if Nkind (P) = N_Function_Call and then Ada_Version < Ada_2012 then
15908 return False;
15909 end if;
15910
15911 -- The following mechanism is clumsy and fragile. A single flag
15912 -- set in Resolve_Actuals would be preferable ???
15913
15914 declare
15915 Proc : Entity_Id;
15916 Form : Entity_Id;
15917 Act : Node_Id;
15918
15919 begin
15920 Proc := Get_Subprogram_Entity (P);
15921
15922 if No (Proc) then
15923 return True;
15924 end if;
15925
15926 -- If we are not a list member, something is strange, so be
15927 -- conservative and return True.
15928
15929 if not Is_List_Member (N) then
15930 return True;
15931 end if;
15932
15933 -- We are going to find the right formal by stepping forward
15934 -- through the formals, as we step backwards in the actuals.
15935
15936 Form := First_Formal (Proc);
15937 Act := N;
15938 loop
15939 -- If no formal, something is weird, so be conservative and
15940 -- return True.
15941
15942 if No (Form) then
15943 return True;
15944 end if;
15945
15946 Prev (Act);
15947 exit when No (Act);
15948 Next_Formal (Form);
15949 end loop;
15950
15951 return Ekind (Form) /= E_In_Parameter;
15952 end;
15953
15954 -- Named parameter for procedure or accept call
15955
15956 when N_Parameter_Association =>
15957 declare
15958 Proc : Entity_Id;
15959 Form : Entity_Id;
15960
15961 begin
15962 Proc := Get_Subprogram_Entity (Parent (P));
15963
15964 if No (Proc) then
15965 return True;
15966 end if;
15967
15968 -- Loop through formals to find the one that matches
15969
15970 Form := First_Formal (Proc);
15971 loop
15972 -- If no matching formal, that's peculiar, some kind of
15973 -- previous error, so return True to be conservative.
15974 -- Actually happens with legal code for an unresolved call
15975 -- where we may get the wrong homonym???
15976
15977 if No (Form) then
15978 return True;
15979 end if;
15980
15981 -- Else test for match
15982
15983 if Chars (Form) = Chars (Selector_Name (P)) then
15984 return Ekind (Form) /= E_In_Parameter;
15985 end if;
15986
15987 Next_Formal (Form);
15988 end loop;
15989 end;
15990
15991 -- Test for appearing in a conversion that itself appears in an
15992 -- lvalue context, since this should be an lvalue.
15993
15994 when N_Type_Conversion =>
15995 return May_Be_Lvalue (P);
15996
15997 -- Test for appearance in object renaming declaration
15998
15999 when N_Object_Renaming_Declaration =>
16000 return True;
16001
16002 -- All other references are definitely not lvalues
16003
16004 when others =>
16005 return False;
16006
16007 end case;
16008 end May_Be_Lvalue;
16009
16010 -----------------------
16011 -- Mark_Coextensions --
16012 -----------------------
16013
16014 procedure Mark_Coextensions (Context_Nod : Node_Id; Root_Nod : Node_Id) is
16015 Is_Dynamic : Boolean;
16016 -- Indicates whether the context causes nested coextensions to be
16017 -- dynamic or static
16018
16019 function Mark_Allocator (N : Node_Id) return Traverse_Result;
16020 -- Recognize an allocator node and label it as a dynamic coextension
16021
16022 --------------------
16023 -- Mark_Allocator --
16024 --------------------
16025
16026 function Mark_Allocator (N : Node_Id) return Traverse_Result is
16027 begin
16028 if Nkind (N) = N_Allocator then
16029 if Is_Dynamic then
16030 Set_Is_Dynamic_Coextension (N);
16031
16032 -- If the allocator expression is potentially dynamic, it may
16033 -- be expanded out of order and require dynamic allocation
16034 -- anyway, so we treat the coextension itself as dynamic.
16035 -- Potential optimization ???
16036
16037 elsif Nkind (Expression (N)) = N_Qualified_Expression
16038 and then Nkind (Expression (Expression (N))) = N_Op_Concat
16039 then
16040 Set_Is_Dynamic_Coextension (N);
16041 else
16042 Set_Is_Static_Coextension (N);
16043 end if;
16044 end if;
16045
16046 return OK;
16047 end Mark_Allocator;
16048
16049 procedure Mark_Allocators is new Traverse_Proc (Mark_Allocator);
16050
16051 -- Start of processing for Mark_Coextensions
16052
16053 begin
16054 -- An allocator that appears on the right-hand side of an assignment is
16055 -- treated as a potentially dynamic coextension when the right-hand side
16056 -- is an allocator or a qualified expression.
16057
16058 -- Obj := new ...'(new Coextension ...);
16059
16060 if Nkind (Context_Nod) = N_Assignment_Statement then
16061 Is_Dynamic :=
16062 Nkind_In (Expression (Context_Nod), N_Allocator,
16063 N_Qualified_Expression);
16064
16065 -- An allocator that appears within the expression of a simple return
16066 -- statement is treated as a potentially dynamic coextension when the
16067 -- expression is either aggregate, allocator, or qualified expression.
16068
16069 -- return (new Coextension ...);
16070 -- return new ...'(new Coextension ...);
16071
16072 elsif Nkind (Context_Nod) = N_Simple_Return_Statement then
16073 Is_Dynamic :=
16074 Nkind_In (Expression (Context_Nod), N_Aggregate,
16075 N_Allocator,
16076 N_Qualified_Expression);
16077
16078 -- An alloctor that appears within the initialization expression of an
16079 -- object declaration is considered a potentially dynamic coextension
16080 -- when the initialization expression is an allocator or a qualified
16081 -- expression.
16082
16083 -- Obj : ... := new ...'(new Coextension ...);
16084
16085 -- A similar case arises when the object declaration is part of an
16086 -- extended return statement.
16087
16088 -- return Obj : ... := new ...'(new Coextension ...);
16089 -- return Obj : ... := (new Coextension ...);
16090
16091 elsif Nkind (Context_Nod) = N_Object_Declaration then
16092 Is_Dynamic :=
16093 Nkind_In (Root_Nod, N_Allocator, N_Qualified_Expression)
16094 or else
16095 Nkind (Parent (Context_Nod)) = N_Extended_Return_Statement;
16096
16097 -- This routine should not be called with constructs that cannot contain
16098 -- coextensions.
16099
16100 else
16101 raise Program_Error;
16102 end if;
16103
16104 Mark_Allocators (Root_Nod);
16105 end Mark_Coextensions;
16106
16107 ----------------------
16108 -- Needs_One_Actual --
16109 ----------------------
16110
16111 function Needs_One_Actual (E : Entity_Id) return Boolean is
16112 Formal : Entity_Id;
16113
16114 begin
16115 -- Ada 2005 or later, and formals present
16116
16117 if Ada_Version >= Ada_2005 and then Present (First_Formal (E)) then
16118 Formal := Next_Formal (First_Formal (E));
16119 while Present (Formal) loop
16120 if No (Default_Value (Formal)) then
16121 return False;
16122 end if;
16123
16124 Next_Formal (Formal);
16125 end loop;
16126
16127 return True;
16128
16129 -- Ada 83/95 or no formals
16130
16131 else
16132 return False;
16133 end if;
16134 end Needs_One_Actual;
16135
16136 ------------------------
16137 -- New_Copy_List_Tree --
16138 ------------------------
16139
16140 function New_Copy_List_Tree (List : List_Id) return List_Id is
16141 NL : List_Id;
16142 E : Node_Id;
16143
16144 begin
16145 if List = No_List then
16146 return No_List;
16147
16148 else
16149 NL := New_List;
16150 E := First (List);
16151
16152 while Present (E) loop
16153 Append (New_Copy_Tree (E), NL);
16154 E := Next (E);
16155 end loop;
16156
16157 return NL;
16158 end if;
16159 end New_Copy_List_Tree;
16160
16161 --------------------------------------------------
16162 -- New_Copy_Tree Auxiliary Data and Subprograms --
16163 --------------------------------------------------
16164
16165 use Atree.Unchecked_Access;
16166 use Atree_Private_Part;
16167
16168 -- Our approach here requires a two pass traversal of the tree. The
16169 -- first pass visits all nodes that eventually will be copied looking
16170 -- for defining Itypes. If any defining Itypes are found, then they are
16171 -- copied, and an entry is added to the replacement map. In the second
16172 -- phase, the tree is copied, using the replacement map to replace any
16173 -- Itype references within the copied tree.
16174
16175 -- The following hash tables are used if the Map supplied has more
16176 -- than hash threshold entries to speed up access to the map. If
16177 -- there are fewer entries, then the map is searched sequentially
16178 -- (because setting up a hash table for only a few entries takes
16179 -- more time than it saves.
16180
16181 function New_Copy_Hash (E : Entity_Id) return NCT_Header_Num;
16182 -- Hash function used for hash operations
16183
16184 -------------------
16185 -- New_Copy_Hash --
16186 -------------------
16187
16188 function New_Copy_Hash (E : Entity_Id) return NCT_Header_Num is
16189 begin
16190 return Nat (E) mod (NCT_Header_Num'Last + 1);
16191 end New_Copy_Hash;
16192
16193 ---------------
16194 -- NCT_Assoc --
16195 ---------------
16196
16197 -- The hash table NCT_Assoc associates old entities in the table
16198 -- with their corresponding new entities (i.e. the pairs of entries
16199 -- presented in the original Map argument are Key-Element pairs).
16200
16201 package NCT_Assoc is new Simple_HTable (
16202 Header_Num => NCT_Header_Num,
16203 Element => Entity_Id,
16204 No_Element => Empty,
16205 Key => Entity_Id,
16206 Hash => New_Copy_Hash,
16207 Equal => Types."=");
16208
16209 ---------------------
16210 -- NCT_Itype_Assoc --
16211 ---------------------
16212
16213 -- The hash table NCT_Itype_Assoc contains entries only for those
16214 -- old nodes which have a non-empty Associated_Node_For_Itype set.
16215 -- The key is the associated node, and the element is the new node
16216 -- itself (NOT the associated node for the new node).
16217
16218 package NCT_Itype_Assoc is new Simple_HTable (
16219 Header_Num => NCT_Header_Num,
16220 Element => Entity_Id,
16221 No_Element => Empty,
16222 Key => Entity_Id,
16223 Hash => New_Copy_Hash,
16224 Equal => Types."=");
16225
16226 -------------------
16227 -- New_Copy_Tree --
16228 -------------------
16229
16230 function New_Copy_Tree
16231 (Source : Node_Id;
16232 Map : Elist_Id := No_Elist;
16233 New_Sloc : Source_Ptr := No_Location;
16234 New_Scope : Entity_Id := Empty) return Node_Id
16235 is
16236 Actual_Map : Elist_Id := Map;
16237 -- This is the actual map for the copy. It is initialized with the
16238 -- given elements, and then enlarged as required for Itypes that are
16239 -- copied during the first phase of the copy operation. The visit
16240 -- procedures add elements to this map as Itypes are encountered.
16241 -- The reason we cannot use Map directly, is that it may well be
16242 -- (and normally is) initialized to No_Elist, and if we have mapped
16243 -- entities, we have to reset it to point to a real Elist.
16244
16245 function Assoc (N : Node_Or_Entity_Id) return Node_Id;
16246 -- Called during second phase to map entities into their corresponding
16247 -- copies using Actual_Map. If the argument is not an entity, or is not
16248 -- in Actual_Map, then it is returned unchanged.
16249
16250 procedure Build_NCT_Hash_Tables;
16251 -- Builds hash tables (number of elements >= threshold value)
16252
16253 function Copy_Elist_With_Replacement
16254 (Old_Elist : Elist_Id) return Elist_Id;
16255 -- Called during second phase to copy element list doing replacements
16256
16257 procedure Copy_Itype_With_Replacement (New_Itype : Entity_Id);
16258 -- Called during the second phase to process a copied Itype. The actual
16259 -- copy happened during the first phase (so that we could make the entry
16260 -- in the mapping), but we still have to deal with the descendants of
16261 -- the copied Itype and copy them where necessary.
16262
16263 function Copy_List_With_Replacement (Old_List : List_Id) return List_Id;
16264 -- Called during second phase to copy list doing replacements
16265
16266 function Copy_Node_With_Replacement (Old_Node : Node_Id) return Node_Id;
16267 -- Called during second phase to copy node doing replacements
16268
16269 procedure Visit_Elist (E : Elist_Id);
16270 -- Called during first phase to visit all elements of an Elist
16271
16272 procedure Visit_Field (F : Union_Id; N : Node_Id);
16273 -- Visit a single field, recursing to call Visit_Node or Visit_List
16274 -- if the field is a syntactic descendant of the current node (i.e.
16275 -- its parent is Node N).
16276
16277 procedure Visit_Itype (Old_Itype : Entity_Id);
16278 -- Called during first phase to visit subsidiary fields of a defining
16279 -- Itype, and also create a copy and make an entry in the replacement
16280 -- map for the new copy.
16281
16282 procedure Visit_List (L : List_Id);
16283 -- Called during first phase to visit all elements of a List
16284
16285 procedure Visit_Node (N : Node_Or_Entity_Id);
16286 -- Called during first phase to visit a node and all its subtrees
16287
16288 -----------
16289 -- Assoc --
16290 -----------
16291
16292 function Assoc (N : Node_Or_Entity_Id) return Node_Id is
16293 E : Elmt_Id;
16294 Ent : Entity_Id;
16295
16296 begin
16297 if not Has_Extension (N) or else No (Actual_Map) then
16298 return N;
16299
16300 elsif NCT_Hash_Tables_Used then
16301 Ent := NCT_Assoc.Get (Entity_Id (N));
16302
16303 if Present (Ent) then
16304 return Ent;
16305 else
16306 return N;
16307 end if;
16308
16309 -- No hash table used, do serial search
16310
16311 else
16312 E := First_Elmt (Actual_Map);
16313 while Present (E) loop
16314 if Node (E) = N then
16315 return Node (Next_Elmt (E));
16316 else
16317 E := Next_Elmt (Next_Elmt (E));
16318 end if;
16319 end loop;
16320 end if;
16321
16322 return N;
16323 end Assoc;
16324
16325 ---------------------------
16326 -- Build_NCT_Hash_Tables --
16327 ---------------------------
16328
16329 procedure Build_NCT_Hash_Tables is
16330 Elmt : Elmt_Id;
16331 Ent : Entity_Id;
16332 begin
16333 if NCT_Hash_Table_Setup then
16334 NCT_Assoc.Reset;
16335 NCT_Itype_Assoc.Reset;
16336 end if;
16337
16338 Elmt := First_Elmt (Actual_Map);
16339 while Present (Elmt) loop
16340 Ent := Node (Elmt);
16341
16342 -- Get new entity, and associate old and new
16343
16344 Next_Elmt (Elmt);
16345 NCT_Assoc.Set (Ent, Node (Elmt));
16346
16347 if Is_Type (Ent) then
16348 declare
16349 Anode : constant Entity_Id :=
16350 Associated_Node_For_Itype (Ent);
16351
16352 begin
16353 if Present (Anode) then
16354
16355 -- Enter a link between the associated node of the
16356 -- old Itype and the new Itype, for updating later
16357 -- when node is copied.
16358
16359 NCT_Itype_Assoc.Set (Anode, Node (Elmt));
16360 end if;
16361 end;
16362 end if;
16363
16364 Next_Elmt (Elmt);
16365 end loop;
16366
16367 NCT_Hash_Tables_Used := True;
16368 NCT_Hash_Table_Setup := True;
16369 end Build_NCT_Hash_Tables;
16370
16371 ---------------------------------
16372 -- Copy_Elist_With_Replacement --
16373 ---------------------------------
16374
16375 function Copy_Elist_With_Replacement
16376 (Old_Elist : Elist_Id) return Elist_Id
16377 is
16378 M : Elmt_Id;
16379 New_Elist : Elist_Id;
16380
16381 begin
16382 if No (Old_Elist) then
16383 return No_Elist;
16384
16385 else
16386 New_Elist := New_Elmt_List;
16387
16388 M := First_Elmt (Old_Elist);
16389 while Present (M) loop
16390 Append_Elmt (Copy_Node_With_Replacement (Node (M)), New_Elist);
16391 Next_Elmt (M);
16392 end loop;
16393 end if;
16394
16395 return New_Elist;
16396 end Copy_Elist_With_Replacement;
16397
16398 ---------------------------------
16399 -- Copy_Itype_With_Replacement --
16400 ---------------------------------
16401
16402 -- This routine exactly parallels its phase one analog Visit_Itype,
16403
16404 procedure Copy_Itype_With_Replacement (New_Itype : Entity_Id) is
16405 begin
16406 -- Translate Next_Entity, Scope, and Etype fields, in case they
16407 -- reference entities that have been mapped into copies.
16408
16409 Set_Next_Entity (New_Itype, Assoc (Next_Entity (New_Itype)));
16410 Set_Etype (New_Itype, Assoc (Etype (New_Itype)));
16411
16412 if Present (New_Scope) then
16413 Set_Scope (New_Itype, New_Scope);
16414 else
16415 Set_Scope (New_Itype, Assoc (Scope (New_Itype)));
16416 end if;
16417
16418 -- Copy referenced fields
16419
16420 if Is_Discrete_Type (New_Itype) then
16421 Set_Scalar_Range (New_Itype,
16422 Copy_Node_With_Replacement (Scalar_Range (New_Itype)));
16423
16424 elsif Has_Discriminants (Base_Type (New_Itype)) then
16425 Set_Discriminant_Constraint (New_Itype,
16426 Copy_Elist_With_Replacement
16427 (Discriminant_Constraint (New_Itype)));
16428
16429 elsif Is_Array_Type (New_Itype) then
16430 if Present (First_Index (New_Itype)) then
16431 Set_First_Index (New_Itype,
16432 First (Copy_List_With_Replacement
16433 (List_Containing (First_Index (New_Itype)))));
16434 end if;
16435
16436 if Is_Packed (New_Itype) then
16437 Set_Packed_Array_Impl_Type (New_Itype,
16438 Copy_Node_With_Replacement
16439 (Packed_Array_Impl_Type (New_Itype)));
16440 end if;
16441 end if;
16442 end Copy_Itype_With_Replacement;
16443
16444 --------------------------------
16445 -- Copy_List_With_Replacement --
16446 --------------------------------
16447
16448 function Copy_List_With_Replacement
16449 (Old_List : List_Id) return List_Id
16450 is
16451 New_List : List_Id;
16452 E : Node_Id;
16453
16454 begin
16455 if Old_List = No_List then
16456 return No_List;
16457
16458 else
16459 New_List := Empty_List;
16460
16461 E := First (Old_List);
16462 while Present (E) loop
16463 Append (Copy_Node_With_Replacement (E), New_List);
16464 Next (E);
16465 end loop;
16466
16467 return New_List;
16468 end if;
16469 end Copy_List_With_Replacement;
16470
16471 --------------------------------
16472 -- Copy_Node_With_Replacement --
16473 --------------------------------
16474
16475 function Copy_Node_With_Replacement
16476 (Old_Node : Node_Id) return Node_Id
16477 is
16478 New_Node : Node_Id;
16479
16480 procedure Adjust_Named_Associations
16481 (Old_Node : Node_Id;
16482 New_Node : Node_Id);
16483 -- If a call node has named associations, these are chained through
16484 -- the First_Named_Actual, Next_Named_Actual links. These must be
16485 -- propagated separately to the new parameter list, because these
16486 -- are not syntactic fields.
16487
16488 function Copy_Field_With_Replacement
16489 (Field : Union_Id) return Union_Id;
16490 -- Given Field, which is a field of Old_Node, return a copy of it
16491 -- if it is a syntactic field (i.e. its parent is Node), setting
16492 -- the parent of the copy to poit to New_Node. Otherwise returns
16493 -- the field (possibly mapped if it is an entity).
16494
16495 -------------------------------
16496 -- Adjust_Named_Associations --
16497 -------------------------------
16498
16499 procedure Adjust_Named_Associations
16500 (Old_Node : Node_Id;
16501 New_Node : Node_Id)
16502 is
16503 Old_E : Node_Id;
16504 New_E : Node_Id;
16505
16506 Old_Next : Node_Id;
16507 New_Next : Node_Id;
16508
16509 begin
16510 Old_E := First (Parameter_Associations (Old_Node));
16511 New_E := First (Parameter_Associations (New_Node));
16512 while Present (Old_E) loop
16513 if Nkind (Old_E) = N_Parameter_Association
16514 and then Present (Next_Named_Actual (Old_E))
16515 then
16516 if First_Named_Actual (Old_Node)
16517 = Explicit_Actual_Parameter (Old_E)
16518 then
16519 Set_First_Named_Actual
16520 (New_Node, Explicit_Actual_Parameter (New_E));
16521 end if;
16522
16523 -- Now scan parameter list from the beginning,to locate
16524 -- next named actual, which can be out of order.
16525
16526 Old_Next := First (Parameter_Associations (Old_Node));
16527 New_Next := First (Parameter_Associations (New_Node));
16528
16529 while Nkind (Old_Next) /= N_Parameter_Association
16530 or else Explicit_Actual_Parameter (Old_Next) /=
16531 Next_Named_Actual (Old_E)
16532 loop
16533 Next (Old_Next);
16534 Next (New_Next);
16535 end loop;
16536
16537 Set_Next_Named_Actual
16538 (New_E, Explicit_Actual_Parameter (New_Next));
16539 end if;
16540
16541 Next (Old_E);
16542 Next (New_E);
16543 end loop;
16544 end Adjust_Named_Associations;
16545
16546 ---------------------------------
16547 -- Copy_Field_With_Replacement --
16548 ---------------------------------
16549
16550 function Copy_Field_With_Replacement
16551 (Field : Union_Id) return Union_Id
16552 is
16553 begin
16554 if Field = Union_Id (Empty) then
16555 return Field;
16556
16557 elsif Field in Node_Range then
16558 declare
16559 Old_N : constant Node_Id := Node_Id (Field);
16560 New_N : Node_Id;
16561
16562 begin
16563 -- If syntactic field, as indicated by the parent pointer
16564 -- being set, then copy the referenced node recursively.
16565
16566 if Parent (Old_N) = Old_Node then
16567 New_N := Copy_Node_With_Replacement (Old_N);
16568
16569 if New_N /= Old_N then
16570 Set_Parent (New_N, New_Node);
16571 end if;
16572
16573 -- For semantic fields, update possible entity reference
16574 -- from the replacement map.
16575
16576 else
16577 New_N := Assoc (Old_N);
16578 end if;
16579
16580 return Union_Id (New_N);
16581 end;
16582
16583 elsif Field in List_Range then
16584 declare
16585 Old_L : constant List_Id := List_Id (Field);
16586 New_L : List_Id;
16587
16588 begin
16589 -- If syntactic field, as indicated by the parent pointer,
16590 -- then recursively copy the entire referenced list.
16591
16592 if Parent (Old_L) = Old_Node then
16593 New_L := Copy_List_With_Replacement (Old_L);
16594 Set_Parent (New_L, New_Node);
16595
16596 -- For semantic list, just returned unchanged
16597
16598 else
16599 New_L := Old_L;
16600 end if;
16601
16602 return Union_Id (New_L);
16603 end;
16604
16605 -- Anything other than a list or a node is returned unchanged
16606
16607 else
16608 return Field;
16609 end if;
16610 end Copy_Field_With_Replacement;
16611
16612 -- Start of processing for Copy_Node_With_Replacement
16613
16614 begin
16615 if Old_Node <= Empty_Or_Error then
16616 return Old_Node;
16617
16618 elsif Has_Extension (Old_Node) then
16619 return Assoc (Old_Node);
16620
16621 else
16622 New_Node := New_Copy (Old_Node);
16623
16624 -- If the node we are copying is the associated node of a
16625 -- previously copied Itype, then adjust the associated node
16626 -- of the copy of that Itype accordingly.
16627
16628 if Present (Actual_Map) then
16629 declare
16630 E : Elmt_Id;
16631 Ent : Entity_Id;
16632
16633 begin
16634 -- Case of hash table used
16635
16636 if NCT_Hash_Tables_Used then
16637 Ent := NCT_Itype_Assoc.Get (Old_Node);
16638
16639 if Present (Ent) then
16640 Set_Associated_Node_For_Itype (Ent, New_Node);
16641 end if;
16642
16643 -- Case of no hash table used
16644
16645 else
16646 E := First_Elmt (Actual_Map);
16647 while Present (E) loop
16648 if Is_Itype (Node (E))
16649 and then
16650 Old_Node = Associated_Node_For_Itype (Node (E))
16651 then
16652 Set_Associated_Node_For_Itype
16653 (Node (Next_Elmt (E)), New_Node);
16654 end if;
16655
16656 E := Next_Elmt (Next_Elmt (E));
16657 end loop;
16658 end if;
16659 end;
16660 end if;
16661
16662 -- Recursively copy descendants
16663
16664 Set_Field1
16665 (New_Node, Copy_Field_With_Replacement (Field1 (New_Node)));
16666 Set_Field2
16667 (New_Node, Copy_Field_With_Replacement (Field2 (New_Node)));
16668 Set_Field3
16669 (New_Node, Copy_Field_With_Replacement (Field3 (New_Node)));
16670 Set_Field4
16671 (New_Node, Copy_Field_With_Replacement (Field4 (New_Node)));
16672 Set_Field5
16673 (New_Node, Copy_Field_With_Replacement (Field5 (New_Node)));
16674
16675 -- Adjust Sloc of new node if necessary
16676
16677 if New_Sloc /= No_Location then
16678 Set_Sloc (New_Node, New_Sloc);
16679
16680 -- If we adjust the Sloc, then we are essentially making a
16681 -- completely new node, so the Comes_From_Source flag should
16682 -- be reset to the proper default value.
16683
16684 Set_Comes_From_Source
16685 (New_Node, Default_Node.Comes_From_Source);
16686 end if;
16687
16688 -- If the node is a call and has named associations, set the
16689 -- corresponding links in the copy.
16690
16691 if Nkind_In (Old_Node, N_Entry_Call_Statement,
16692 N_Function_Call,
16693 N_Procedure_Call_Statement)
16694 and then Present (First_Named_Actual (Old_Node))
16695 then
16696 Adjust_Named_Associations (Old_Node, New_Node);
16697 end if;
16698
16699 -- Reset First_Real_Statement for Handled_Sequence_Of_Statements.
16700 -- The replacement mechanism applies to entities, and is not used
16701 -- here. Eventually we may need a more general graph-copying
16702 -- routine. For now, do a sequential search to find desired node.
16703
16704 if Nkind (Old_Node) = N_Handled_Sequence_Of_Statements
16705 and then Present (First_Real_Statement (Old_Node))
16706 then
16707 declare
16708 Old_F : constant Node_Id := First_Real_Statement (Old_Node);
16709 N1, N2 : Node_Id;
16710
16711 begin
16712 N1 := First (Statements (Old_Node));
16713 N2 := First (Statements (New_Node));
16714
16715 while N1 /= Old_F loop
16716 Next (N1);
16717 Next (N2);
16718 end loop;
16719
16720 Set_First_Real_Statement (New_Node, N2);
16721 end;
16722 end if;
16723 end if;
16724
16725 -- All done, return copied node
16726
16727 return New_Node;
16728 end Copy_Node_With_Replacement;
16729
16730 -----------------
16731 -- Visit_Elist --
16732 -----------------
16733
16734 procedure Visit_Elist (E : Elist_Id) is
16735 Elmt : Elmt_Id;
16736 begin
16737 if Present (E) then
16738 Elmt := First_Elmt (E);
16739
16740 while Elmt /= No_Elmt loop
16741 Visit_Node (Node (Elmt));
16742 Next_Elmt (Elmt);
16743 end loop;
16744 end if;
16745 end Visit_Elist;
16746
16747 -----------------
16748 -- Visit_Field --
16749 -----------------
16750
16751 procedure Visit_Field (F : Union_Id; N : Node_Id) is
16752 begin
16753 if F = Union_Id (Empty) then
16754 return;
16755
16756 elsif F in Node_Range then
16757
16758 -- Copy node if it is syntactic, i.e. its parent pointer is
16759 -- set to point to the field that referenced it (certain
16760 -- Itypes will also meet this criterion, which is fine, since
16761 -- these are clearly Itypes that do need to be copied, since
16762 -- we are copying their parent.)
16763
16764 if Parent (Node_Id (F)) = N then
16765 Visit_Node (Node_Id (F));
16766 return;
16767
16768 -- Another case, if we are pointing to an Itype, then we want
16769 -- to copy it if its associated node is somewhere in the tree
16770 -- being copied.
16771
16772 -- Note: the exclusion of self-referential copies is just an
16773 -- optimization, since the search of the already copied list
16774 -- would catch it, but it is a common case (Etype pointing
16775 -- to itself for an Itype that is a base type).
16776
16777 elsif Has_Extension (Node_Id (F))
16778 and then Is_Itype (Entity_Id (F))
16779 and then Node_Id (F) /= N
16780 then
16781 declare
16782 P : Node_Id;
16783
16784 begin
16785 P := Associated_Node_For_Itype (Node_Id (F));
16786 while Present (P) loop
16787 if P = Source then
16788 Visit_Node (Node_Id (F));
16789 return;
16790 else
16791 P := Parent (P);
16792 end if;
16793 end loop;
16794
16795 -- An Itype whose parent is not being copied definitely
16796 -- should NOT be copied, since it does not belong in any
16797 -- sense to the copied subtree.
16798
16799 return;
16800 end;
16801 end if;
16802
16803 elsif F in List_Range and then Parent (List_Id (F)) = N then
16804 Visit_List (List_Id (F));
16805 return;
16806 end if;
16807 end Visit_Field;
16808
16809 -----------------
16810 -- Visit_Itype --
16811 -----------------
16812
16813 procedure Visit_Itype (Old_Itype : Entity_Id) is
16814 New_Itype : Entity_Id;
16815 E : Elmt_Id;
16816 Ent : Entity_Id;
16817
16818 begin
16819 -- Itypes that describe the designated type of access to subprograms
16820 -- have the structure of subprogram declarations, with signatures,
16821 -- etc. Either we duplicate the signatures completely, or choose to
16822 -- share such itypes, which is fine because their elaboration will
16823 -- have no side effects.
16824
16825 if Ekind (Old_Itype) = E_Subprogram_Type then
16826 return;
16827 end if;
16828
16829 New_Itype := New_Copy (Old_Itype);
16830
16831 -- The new Itype has all the attributes of the old one, and
16832 -- we just copy the contents of the entity. However, the back-end
16833 -- needs different names for debugging purposes, so we create a
16834 -- new internal name for it in all cases.
16835
16836 Set_Chars (New_Itype, New_Internal_Name ('T'));
16837
16838 -- If our associated node is an entity that has already been copied,
16839 -- then set the associated node of the copy to point to the right
16840 -- copy. If we have copied an Itype that is itself the associated
16841 -- node of some previously copied Itype, then we set the right
16842 -- pointer in the other direction.
16843
16844 if Present (Actual_Map) then
16845
16846 -- Case of hash tables used
16847
16848 if NCT_Hash_Tables_Used then
16849
16850 Ent := NCT_Assoc.Get (Associated_Node_For_Itype (Old_Itype));
16851
16852 if Present (Ent) then
16853 Set_Associated_Node_For_Itype (New_Itype, Ent);
16854 end if;
16855
16856 Ent := NCT_Itype_Assoc.Get (Old_Itype);
16857 if Present (Ent) then
16858 Set_Associated_Node_For_Itype (Ent, New_Itype);
16859
16860 -- If the hash table has no association for this Itype and
16861 -- its associated node, enter one now.
16862
16863 else
16864 NCT_Itype_Assoc.Set
16865 (Associated_Node_For_Itype (Old_Itype), New_Itype);
16866 end if;
16867
16868 -- Case of hash tables not used
16869
16870 else
16871 E := First_Elmt (Actual_Map);
16872 while Present (E) loop
16873 if Associated_Node_For_Itype (Old_Itype) = Node (E) then
16874 Set_Associated_Node_For_Itype
16875 (New_Itype, Node (Next_Elmt (E)));
16876 end if;
16877
16878 if Is_Type (Node (E))
16879 and then Old_Itype = Associated_Node_For_Itype (Node (E))
16880 then
16881 Set_Associated_Node_For_Itype
16882 (Node (Next_Elmt (E)), New_Itype);
16883 end if;
16884
16885 E := Next_Elmt (Next_Elmt (E));
16886 end loop;
16887 end if;
16888 end if;
16889
16890 if Present (Freeze_Node (New_Itype)) then
16891 Set_Is_Frozen (New_Itype, False);
16892 Set_Freeze_Node (New_Itype, Empty);
16893 end if;
16894
16895 -- Add new association to map
16896
16897 if No (Actual_Map) then
16898 Actual_Map := New_Elmt_List;
16899 end if;
16900
16901 Append_Elmt (Old_Itype, Actual_Map);
16902 Append_Elmt (New_Itype, Actual_Map);
16903
16904 if NCT_Hash_Tables_Used then
16905 NCT_Assoc.Set (Old_Itype, New_Itype);
16906
16907 else
16908 NCT_Table_Entries := NCT_Table_Entries + 1;
16909
16910 if NCT_Table_Entries > NCT_Hash_Threshold then
16911 Build_NCT_Hash_Tables;
16912 end if;
16913 end if;
16914
16915 -- If a record subtype is simply copied, the entity list will be
16916 -- shared. Thus cloned_Subtype must be set to indicate the sharing.
16917
16918 if Ekind_In (Old_Itype, E_Record_Subtype, E_Class_Wide_Subtype) then
16919 Set_Cloned_Subtype (New_Itype, Old_Itype);
16920 end if;
16921
16922 -- Visit descendants that eventually get copied
16923
16924 Visit_Field (Union_Id (Etype (Old_Itype)), Old_Itype);
16925
16926 if Is_Discrete_Type (Old_Itype) then
16927 Visit_Field (Union_Id (Scalar_Range (Old_Itype)), Old_Itype);
16928
16929 elsif Has_Discriminants (Base_Type (Old_Itype)) then
16930 -- ??? This should involve call to Visit_Field
16931 Visit_Elist (Discriminant_Constraint (Old_Itype));
16932
16933 elsif Is_Array_Type (Old_Itype) then
16934 if Present (First_Index (Old_Itype)) then
16935 Visit_Field (Union_Id (List_Containing
16936 (First_Index (Old_Itype))),
16937 Old_Itype);
16938 end if;
16939
16940 if Is_Packed (Old_Itype) then
16941 Visit_Field (Union_Id (Packed_Array_Impl_Type (Old_Itype)),
16942 Old_Itype);
16943 end if;
16944 end if;
16945 end Visit_Itype;
16946
16947 ----------------
16948 -- Visit_List --
16949 ----------------
16950
16951 procedure Visit_List (L : List_Id) is
16952 N : Node_Id;
16953 begin
16954 if L /= No_List then
16955 N := First (L);
16956
16957 while Present (N) loop
16958 Visit_Node (N);
16959 Next (N);
16960 end loop;
16961 end if;
16962 end Visit_List;
16963
16964 ----------------
16965 -- Visit_Node --
16966 ----------------
16967
16968 procedure Visit_Node (N : Node_Or_Entity_Id) is
16969
16970 -- Start of processing for Visit_Node
16971
16972 begin
16973 -- Handle case of an Itype, which must be copied
16974
16975 if Has_Extension (N) and then Is_Itype (N) then
16976
16977 -- Nothing to do if already in the list. This can happen with an
16978 -- Itype entity that appears more than once in the tree.
16979 -- Note that we do not want to visit descendants in this case.
16980
16981 -- Test for already in list when hash table is used
16982
16983 if NCT_Hash_Tables_Used then
16984 if Present (NCT_Assoc.Get (Entity_Id (N))) then
16985 return;
16986 end if;
16987
16988 -- Test for already in list when hash table not used
16989
16990 else
16991 declare
16992 E : Elmt_Id;
16993 begin
16994 if Present (Actual_Map) then
16995 E := First_Elmt (Actual_Map);
16996 while Present (E) loop
16997 if Node (E) = N then
16998 return;
16999 else
17000 E := Next_Elmt (Next_Elmt (E));
17001 end if;
17002 end loop;
17003 end if;
17004 end;
17005 end if;
17006
17007 Visit_Itype (N);
17008 end if;
17009
17010 -- Visit descendants
17011
17012 Visit_Field (Field1 (N), N);
17013 Visit_Field (Field2 (N), N);
17014 Visit_Field (Field3 (N), N);
17015 Visit_Field (Field4 (N), N);
17016 Visit_Field (Field5 (N), N);
17017 end Visit_Node;
17018
17019 -- Start of processing for New_Copy_Tree
17020
17021 begin
17022 Actual_Map := Map;
17023
17024 -- See if we should use hash table
17025
17026 if No (Actual_Map) then
17027 NCT_Hash_Tables_Used := False;
17028
17029 else
17030 declare
17031 Elmt : Elmt_Id;
17032
17033 begin
17034 NCT_Table_Entries := 0;
17035
17036 Elmt := First_Elmt (Actual_Map);
17037 while Present (Elmt) loop
17038 NCT_Table_Entries := NCT_Table_Entries + 1;
17039 Next_Elmt (Elmt);
17040 Next_Elmt (Elmt);
17041 end loop;
17042
17043 if NCT_Table_Entries > NCT_Hash_Threshold then
17044 Build_NCT_Hash_Tables;
17045 else
17046 NCT_Hash_Tables_Used := False;
17047 end if;
17048 end;
17049 end if;
17050
17051 -- Hash table set up if required, now start phase one by visiting
17052 -- top node (we will recursively visit the descendants).
17053
17054 Visit_Node (Source);
17055
17056 -- Now the second phase of the copy can start. First we process
17057 -- all the mapped entities, copying their descendants.
17058
17059 if Present (Actual_Map) then
17060 declare
17061 Elmt : Elmt_Id;
17062 New_Itype : Entity_Id;
17063 begin
17064 Elmt := First_Elmt (Actual_Map);
17065 while Present (Elmt) loop
17066 Next_Elmt (Elmt);
17067 New_Itype := Node (Elmt);
17068
17069 if Is_Itype (New_Itype) then
17070 Copy_Itype_With_Replacement (New_Itype);
17071 end if;
17072 Next_Elmt (Elmt);
17073 end loop;
17074 end;
17075 end if;
17076
17077 -- Now we can copy the actual tree
17078
17079 return Copy_Node_With_Replacement (Source);
17080 end New_Copy_Tree;
17081
17082 -------------------------
17083 -- New_External_Entity --
17084 -------------------------
17085
17086 function New_External_Entity
17087 (Kind : Entity_Kind;
17088 Scope_Id : Entity_Id;
17089 Sloc_Value : Source_Ptr;
17090 Related_Id : Entity_Id;
17091 Suffix : Character;
17092 Suffix_Index : Nat := 0;
17093 Prefix : Character := ' ') return Entity_Id
17094 is
17095 N : constant Entity_Id :=
17096 Make_Defining_Identifier (Sloc_Value,
17097 New_External_Name
17098 (Chars (Related_Id), Suffix, Suffix_Index, Prefix));
17099
17100 begin
17101 Set_Ekind (N, Kind);
17102 Set_Is_Internal (N, True);
17103 Append_Entity (N, Scope_Id);
17104 Set_Public_Status (N);
17105
17106 if Kind in Type_Kind then
17107 Init_Size_Align (N);
17108 end if;
17109
17110 return N;
17111 end New_External_Entity;
17112
17113 -------------------------
17114 -- New_Internal_Entity --
17115 -------------------------
17116
17117 function New_Internal_Entity
17118 (Kind : Entity_Kind;
17119 Scope_Id : Entity_Id;
17120 Sloc_Value : Source_Ptr;
17121 Id_Char : Character) return Entity_Id
17122 is
17123 N : constant Entity_Id := Make_Temporary (Sloc_Value, Id_Char);
17124
17125 begin
17126 Set_Ekind (N, Kind);
17127 Set_Is_Internal (N, True);
17128 Append_Entity (N, Scope_Id);
17129
17130 if Kind in Type_Kind then
17131 Init_Size_Align (N);
17132 end if;
17133
17134 return N;
17135 end New_Internal_Entity;
17136
17137 -----------------
17138 -- Next_Actual --
17139 -----------------
17140
17141 function Next_Actual (Actual_Id : Node_Id) return Node_Id is
17142 N : Node_Id;
17143
17144 begin
17145 -- If we are pointing at a positional parameter, it is a member of a
17146 -- node list (the list of parameters), and the next parameter is the
17147 -- next node on the list, unless we hit a parameter association, then
17148 -- we shift to using the chain whose head is the First_Named_Actual in
17149 -- the parent, and then is threaded using the Next_Named_Actual of the
17150 -- Parameter_Association. All this fiddling is because the original node
17151 -- list is in the textual call order, and what we need is the
17152 -- declaration order.
17153
17154 if Is_List_Member (Actual_Id) then
17155 N := Next (Actual_Id);
17156
17157 if Nkind (N) = N_Parameter_Association then
17158 return First_Named_Actual (Parent (Actual_Id));
17159 else
17160 return N;
17161 end if;
17162
17163 else
17164 return Next_Named_Actual (Parent (Actual_Id));
17165 end if;
17166 end Next_Actual;
17167
17168 procedure Next_Actual (Actual_Id : in out Node_Id) is
17169 begin
17170 Actual_Id := Next_Actual (Actual_Id);
17171 end Next_Actual;
17172
17173 -----------------------
17174 -- Normalize_Actuals --
17175 -----------------------
17176
17177 -- Chain actuals according to formals of subprogram. If there are no named
17178 -- associations, the chain is simply the list of Parameter Associations,
17179 -- since the order is the same as the declaration order. If there are named
17180 -- associations, then the First_Named_Actual field in the N_Function_Call
17181 -- or N_Procedure_Call_Statement node points to the Parameter_Association
17182 -- node for the parameter that comes first in declaration order. The
17183 -- remaining named parameters are then chained in declaration order using
17184 -- Next_Named_Actual.
17185
17186 -- This routine also verifies that the number of actuals is compatible with
17187 -- the number and default values of formals, but performs no type checking
17188 -- (type checking is done by the caller).
17189
17190 -- If the matching succeeds, Success is set to True and the caller proceeds
17191 -- with type-checking. If the match is unsuccessful, then Success is set to
17192 -- False, and the caller attempts a different interpretation, if there is
17193 -- one.
17194
17195 -- If the flag Report is on, the call is not overloaded, and a failure to
17196 -- match can be reported here, rather than in the caller.
17197
17198 procedure Normalize_Actuals
17199 (N : Node_Id;
17200 S : Entity_Id;
17201 Report : Boolean;
17202 Success : out Boolean)
17203 is
17204 Actuals : constant List_Id := Parameter_Associations (N);
17205 Actual : Node_Id := Empty;
17206 Formal : Entity_Id;
17207 Last : Node_Id := Empty;
17208 First_Named : Node_Id := Empty;
17209 Found : Boolean;
17210
17211 Formals_To_Match : Integer := 0;
17212 Actuals_To_Match : Integer := 0;
17213
17214 procedure Chain (A : Node_Id);
17215 -- Add named actual at the proper place in the list, using the
17216 -- Next_Named_Actual link.
17217
17218 function Reporting return Boolean;
17219 -- Determines if an error is to be reported. To report an error, we
17220 -- need Report to be True, and also we do not report errors caused
17221 -- by calls to init procs that occur within other init procs. Such
17222 -- errors must always be cascaded errors, since if all the types are
17223 -- declared correctly, the compiler will certainly build decent calls.
17224
17225 -----------
17226 -- Chain --
17227 -----------
17228
17229 procedure Chain (A : Node_Id) is
17230 begin
17231 if No (Last) then
17232
17233 -- Call node points to first actual in list
17234
17235 Set_First_Named_Actual (N, Explicit_Actual_Parameter (A));
17236
17237 else
17238 Set_Next_Named_Actual (Last, Explicit_Actual_Parameter (A));
17239 end if;
17240
17241 Last := A;
17242 Set_Next_Named_Actual (Last, Empty);
17243 end Chain;
17244
17245 ---------------
17246 -- Reporting --
17247 ---------------
17248
17249 function Reporting return Boolean is
17250 begin
17251 if not Report then
17252 return False;
17253
17254 elsif not Within_Init_Proc then
17255 return True;
17256
17257 elsif Is_Init_Proc (Entity (Name (N))) then
17258 return False;
17259
17260 else
17261 return True;
17262 end if;
17263 end Reporting;
17264
17265 -- Start of processing for Normalize_Actuals
17266
17267 begin
17268 if Is_Access_Type (S) then
17269
17270 -- The name in the call is a function call that returns an access
17271 -- to subprogram. The designated type has the list of formals.
17272
17273 Formal := First_Formal (Designated_Type (S));
17274 else
17275 Formal := First_Formal (S);
17276 end if;
17277
17278 while Present (Formal) loop
17279 Formals_To_Match := Formals_To_Match + 1;
17280 Next_Formal (Formal);
17281 end loop;
17282
17283 -- Find if there is a named association, and verify that no positional
17284 -- associations appear after named ones.
17285
17286 if Present (Actuals) then
17287 Actual := First (Actuals);
17288 end if;
17289
17290 while Present (Actual)
17291 and then Nkind (Actual) /= N_Parameter_Association
17292 loop
17293 Actuals_To_Match := Actuals_To_Match + 1;
17294 Next (Actual);
17295 end loop;
17296
17297 if No (Actual) and Actuals_To_Match = Formals_To_Match then
17298
17299 -- Most common case: positional notation, no defaults
17300
17301 Success := True;
17302 return;
17303
17304 elsif Actuals_To_Match > Formals_To_Match then
17305
17306 -- Too many actuals: will not work
17307
17308 if Reporting then
17309 if Is_Entity_Name (Name (N)) then
17310 Error_Msg_N ("too many arguments in call to&", Name (N));
17311 else
17312 Error_Msg_N ("too many arguments in call", N);
17313 end if;
17314 end if;
17315
17316 Success := False;
17317 return;
17318 end if;
17319
17320 First_Named := Actual;
17321
17322 while Present (Actual) loop
17323 if Nkind (Actual) /= N_Parameter_Association then
17324 Error_Msg_N
17325 ("positional parameters not allowed after named ones", Actual);
17326 Success := False;
17327 return;
17328
17329 else
17330 Actuals_To_Match := Actuals_To_Match + 1;
17331 end if;
17332
17333 Next (Actual);
17334 end loop;
17335
17336 if Present (Actuals) then
17337 Actual := First (Actuals);
17338 end if;
17339
17340 Formal := First_Formal (S);
17341 while Present (Formal) loop
17342
17343 -- Match the formals in order. If the corresponding actual is
17344 -- positional, nothing to do. Else scan the list of named actuals
17345 -- to find the one with the right name.
17346
17347 if Present (Actual)
17348 and then Nkind (Actual) /= N_Parameter_Association
17349 then
17350 Next (Actual);
17351 Actuals_To_Match := Actuals_To_Match - 1;
17352 Formals_To_Match := Formals_To_Match - 1;
17353
17354 else
17355 -- For named parameters, search the list of actuals to find
17356 -- one that matches the next formal name.
17357
17358 Actual := First_Named;
17359 Found := False;
17360 while Present (Actual) loop
17361 if Chars (Selector_Name (Actual)) = Chars (Formal) then
17362 Found := True;
17363 Chain (Actual);
17364 Actuals_To_Match := Actuals_To_Match - 1;
17365 Formals_To_Match := Formals_To_Match - 1;
17366 exit;
17367 end if;
17368
17369 Next (Actual);
17370 end loop;
17371
17372 if not Found then
17373 if Ekind (Formal) /= E_In_Parameter
17374 or else No (Default_Value (Formal))
17375 then
17376 if Reporting then
17377 if (Comes_From_Source (S)
17378 or else Sloc (S) = Standard_Location)
17379 and then Is_Overloadable (S)
17380 then
17381 if No (Actuals)
17382 and then
17383 Nkind_In (Parent (N), N_Procedure_Call_Statement,
17384 N_Function_Call,
17385 N_Parameter_Association)
17386 and then Ekind (S) /= E_Function
17387 then
17388 Set_Etype (N, Etype (S));
17389
17390 else
17391 Error_Msg_Name_1 := Chars (S);
17392 Error_Msg_Sloc := Sloc (S);
17393 Error_Msg_NE
17394 ("missing argument for parameter & "
17395 & "in call to % declared #", N, Formal);
17396 end if;
17397
17398 elsif Is_Overloadable (S) then
17399 Error_Msg_Name_1 := Chars (S);
17400
17401 -- Point to type derivation that generated the
17402 -- operation.
17403
17404 Error_Msg_Sloc := Sloc (Parent (S));
17405
17406 Error_Msg_NE
17407 ("missing argument for parameter & "
17408 & "in call to % (inherited) #", N, Formal);
17409
17410 else
17411 Error_Msg_NE
17412 ("missing argument for parameter &", N, Formal);
17413 end if;
17414 end if;
17415
17416 Success := False;
17417 return;
17418
17419 else
17420 Formals_To_Match := Formals_To_Match - 1;
17421 end if;
17422 end if;
17423 end if;
17424
17425 Next_Formal (Formal);
17426 end loop;
17427
17428 if Formals_To_Match = 0 and then Actuals_To_Match = 0 then
17429 Success := True;
17430 return;
17431
17432 else
17433 if Reporting then
17434
17435 -- Find some superfluous named actual that did not get
17436 -- attached to the list of associations.
17437
17438 Actual := First (Actuals);
17439 while Present (Actual) loop
17440 if Nkind (Actual) = N_Parameter_Association
17441 and then Actual /= Last
17442 and then No (Next_Named_Actual (Actual))
17443 then
17444 -- A validity check may introduce a copy of a call that
17445 -- includes an extra actual (for example for an unrelated
17446 -- accessibility check). Check that the extra actual matches
17447 -- some extra formal, which must exist already because
17448 -- subprogram must be frozen at this point.
17449
17450 if Present (Extra_Formals (S))
17451 and then not Comes_From_Source (Actual)
17452 and then Nkind (Actual) = N_Parameter_Association
17453 and then Chars (Extra_Formals (S)) =
17454 Chars (Selector_Name (Actual))
17455 then
17456 null;
17457 else
17458 Error_Msg_N
17459 ("unmatched actual & in call", Selector_Name (Actual));
17460 exit;
17461 end if;
17462 end if;
17463
17464 Next (Actual);
17465 end loop;
17466 end if;
17467
17468 Success := False;
17469 return;
17470 end if;
17471 end Normalize_Actuals;
17472
17473 --------------------------------
17474 -- Note_Possible_Modification --
17475 --------------------------------
17476
17477 procedure Note_Possible_Modification (N : Node_Id; Sure : Boolean) is
17478 Modification_Comes_From_Source : constant Boolean :=
17479 Comes_From_Source (Parent (N));
17480
17481 Ent : Entity_Id;
17482 Exp : Node_Id;
17483
17484 begin
17485 -- Loop to find referenced entity, if there is one
17486
17487 Exp := N;
17488 loop
17489 Ent := Empty;
17490
17491 if Is_Entity_Name (Exp) then
17492 Ent := Entity (Exp);
17493
17494 -- If the entity is missing, it is an undeclared identifier,
17495 -- and there is nothing to annotate.
17496
17497 if No (Ent) then
17498 return;
17499 end if;
17500
17501 elsif Nkind (Exp) = N_Explicit_Dereference then
17502 declare
17503 P : constant Node_Id := Prefix (Exp);
17504
17505 begin
17506 -- In formal verification mode, keep track of all reads and
17507 -- writes through explicit dereferences.
17508
17509 if GNATprove_Mode then
17510 SPARK_Specific.Generate_Dereference (N, 'm');
17511 end if;
17512
17513 if Nkind (P) = N_Selected_Component
17514 and then Present (Entry_Formal (Entity (Selector_Name (P))))
17515 then
17516 -- Case of a reference to an entry formal
17517
17518 Ent := Entry_Formal (Entity (Selector_Name (P)));
17519
17520 elsif Nkind (P) = N_Identifier
17521 and then Nkind (Parent (Entity (P))) = N_Object_Declaration
17522 and then Present (Expression (Parent (Entity (P))))
17523 and then Nkind (Expression (Parent (Entity (P)))) =
17524 N_Reference
17525 then
17526 -- Case of a reference to a value on which side effects have
17527 -- been removed.
17528
17529 Exp := Prefix (Expression (Parent (Entity (P))));
17530 goto Continue;
17531
17532 else
17533 return;
17534 end if;
17535 end;
17536
17537 elsif Nkind_In (Exp, N_Type_Conversion,
17538 N_Unchecked_Type_Conversion)
17539 then
17540 Exp := Expression (Exp);
17541 goto Continue;
17542
17543 elsif Nkind_In (Exp, N_Slice,
17544 N_Indexed_Component,
17545 N_Selected_Component)
17546 then
17547 -- Special check, if the prefix is an access type, then return
17548 -- since we are modifying the thing pointed to, not the prefix.
17549 -- When we are expanding, most usually the prefix is replaced
17550 -- by an explicit dereference, and this test is not needed, but
17551 -- in some cases (notably -gnatc mode and generics) when we do
17552 -- not do full expansion, we need this special test.
17553
17554 if Is_Access_Type (Etype (Prefix (Exp))) then
17555 return;
17556
17557 -- Otherwise go to prefix and keep going
17558
17559 else
17560 Exp := Prefix (Exp);
17561 goto Continue;
17562 end if;
17563
17564 -- All other cases, not a modification
17565
17566 else
17567 return;
17568 end if;
17569
17570 -- Now look for entity being referenced
17571
17572 if Present (Ent) then
17573 if Is_Object (Ent) then
17574 if Comes_From_Source (Exp)
17575 or else Modification_Comes_From_Source
17576 then
17577 -- Give warning if pragma unmodified given and we are
17578 -- sure this is a modification.
17579
17580 if Has_Pragma_Unmodified (Ent) and then Sure then
17581 Error_Msg_NE ("??pragma Unmodified given for &!", N, Ent);
17582 end if;
17583
17584 Set_Never_Set_In_Source (Ent, False);
17585 end if;
17586
17587 Set_Is_True_Constant (Ent, False);
17588 Set_Current_Value (Ent, Empty);
17589 Set_Is_Known_Null (Ent, False);
17590
17591 if not Can_Never_Be_Null (Ent) then
17592 Set_Is_Known_Non_Null (Ent, False);
17593 end if;
17594
17595 -- Follow renaming chain
17596
17597 if (Ekind (Ent) = E_Variable or else Ekind (Ent) = E_Constant)
17598 and then Present (Renamed_Object (Ent))
17599 then
17600 Exp := Renamed_Object (Ent);
17601
17602 -- If the entity is the loop variable in an iteration over
17603 -- a container, retrieve container expression to indicate
17604 -- possible modification.
17605
17606 if Present (Related_Expression (Ent))
17607 and then Nkind (Parent (Related_Expression (Ent))) =
17608 N_Iterator_Specification
17609 then
17610 Exp := Original_Node (Related_Expression (Ent));
17611 end if;
17612
17613 goto Continue;
17614
17615 -- The expression may be the renaming of a subcomponent of an
17616 -- array or container. The assignment to the subcomponent is
17617 -- a modification of the container.
17618
17619 elsif Comes_From_Source (Original_Node (Exp))
17620 and then Nkind_In (Original_Node (Exp), N_Selected_Component,
17621 N_Indexed_Component)
17622 then
17623 Exp := Prefix (Original_Node (Exp));
17624 goto Continue;
17625 end if;
17626
17627 -- Generate a reference only if the assignment comes from
17628 -- source. This excludes, for example, calls to a dispatching
17629 -- assignment operation when the left-hand side is tagged. In
17630 -- GNATprove mode, we need those references also on generated
17631 -- code, as these are used to compute the local effects of
17632 -- subprograms.
17633
17634 if Modification_Comes_From_Source or GNATprove_Mode then
17635 Generate_Reference (Ent, Exp, 'm');
17636
17637 -- If the target of the assignment is the bound variable
17638 -- in an iterator, indicate that the corresponding array
17639 -- or container is also modified.
17640
17641 if Ada_Version >= Ada_2012
17642 and then Nkind (Parent (Ent)) = N_Iterator_Specification
17643 then
17644 declare
17645 Domain : constant Node_Id := Name (Parent (Ent));
17646
17647 begin
17648 -- TBD : in the full version of the construct, the
17649 -- domain of iteration can be given by an expression.
17650
17651 if Is_Entity_Name (Domain) then
17652 Generate_Reference (Entity (Domain), Exp, 'm');
17653 Set_Is_True_Constant (Entity (Domain), False);
17654 Set_Never_Set_In_Source (Entity (Domain), False);
17655 end if;
17656 end;
17657 end if;
17658 end if;
17659 end if;
17660
17661 Kill_Checks (Ent);
17662
17663 -- If we are sure this is a modification from source, and we know
17664 -- this modifies a constant, then give an appropriate warning.
17665
17666 if Sure
17667 and then Modification_Comes_From_Source
17668 and then Overlays_Constant (Ent)
17669 and then Address_Clause_Overlay_Warnings
17670 then
17671 declare
17672 Addr : constant Node_Id := Address_Clause (Ent);
17673 O_Ent : Entity_Id;
17674 Off : Boolean;
17675
17676 begin
17677 Find_Overlaid_Entity (Addr, O_Ent, Off);
17678
17679 Error_Msg_Sloc := Sloc (Addr);
17680 Error_Msg_NE
17681 ("??constant& may be modified via address clause#",
17682 N, O_Ent);
17683 end;
17684 end if;
17685
17686 return;
17687 end if;
17688
17689 <<Continue>>
17690 null;
17691 end loop;
17692 end Note_Possible_Modification;
17693
17694 --------------------------------------
17695 -- Null_To_Null_Address_Convert_OK --
17696 --------------------------------------
17697
17698 function Null_To_Null_Address_Convert_OK
17699 (N : Node_Id;
17700 Typ : Entity_Id := Empty) return Boolean
17701 is
17702 begin
17703 if not Relaxed_RM_Semantics then
17704 return False;
17705 end if;
17706
17707 if Nkind (N) = N_Null then
17708 return Present (Typ) and then Is_Descendant_Of_Address (Typ);
17709
17710 elsif Nkind_In (N, N_Op_Eq, N_Op_Ge, N_Op_Gt, N_Op_Le, N_Op_Lt, N_Op_Ne)
17711 then
17712 declare
17713 L : constant Node_Id := Left_Opnd (N);
17714 R : constant Node_Id := Right_Opnd (N);
17715
17716 begin
17717 -- We check the Etype of the complementary operand since the
17718 -- N_Null node is not decorated at this stage.
17719
17720 return
17721 ((Nkind (L) = N_Null
17722 and then Is_Descendant_Of_Address (Etype (R)))
17723 or else
17724 (Nkind (R) = N_Null
17725 and then Is_Descendant_Of_Address (Etype (L))));
17726 end;
17727 end if;
17728
17729 return False;
17730 end Null_To_Null_Address_Convert_OK;
17731
17732 -------------------------
17733 -- Object_Access_Level --
17734 -------------------------
17735
17736 -- Returns the static accessibility level of the view denoted by Obj. Note
17737 -- that the value returned is the result of a call to Scope_Depth. Only
17738 -- scope depths associated with dynamic scopes can actually be returned.
17739 -- Since only relative levels matter for accessibility checking, the fact
17740 -- that the distance between successive levels of accessibility is not
17741 -- always one is immaterial (invariant: if level(E2) is deeper than
17742 -- level(E1), then Scope_Depth(E1) < Scope_Depth(E2)).
17743
17744 function Object_Access_Level (Obj : Node_Id) return Uint is
17745 function Is_Interface_Conversion (N : Node_Id) return Boolean;
17746 -- Determine whether N is a construct of the form
17747 -- Some_Type (Operand._tag'Address)
17748 -- This construct appears in the context of dispatching calls.
17749
17750 function Reference_To (Obj : Node_Id) return Node_Id;
17751 -- An explicit dereference is created when removing side-effects from
17752 -- expressions for constraint checking purposes. In this case a local
17753 -- access type is created for it. The correct access level is that of
17754 -- the original source node. We detect this case by noting that the
17755 -- prefix of the dereference is created by an object declaration whose
17756 -- initial expression is a reference.
17757
17758 -----------------------------
17759 -- Is_Interface_Conversion --
17760 -----------------------------
17761
17762 function Is_Interface_Conversion (N : Node_Id) return Boolean is
17763 begin
17764 return Nkind (N) = N_Unchecked_Type_Conversion
17765 and then Nkind (Expression (N)) = N_Attribute_Reference
17766 and then Attribute_Name (Expression (N)) = Name_Address;
17767 end Is_Interface_Conversion;
17768
17769 ------------------
17770 -- Reference_To --
17771 ------------------
17772
17773 function Reference_To (Obj : Node_Id) return Node_Id is
17774 Pref : constant Node_Id := Prefix (Obj);
17775 begin
17776 if Is_Entity_Name (Pref)
17777 and then Nkind (Parent (Entity (Pref))) = N_Object_Declaration
17778 and then Present (Expression (Parent (Entity (Pref))))
17779 and then Nkind (Expression (Parent (Entity (Pref)))) = N_Reference
17780 then
17781 return (Prefix (Expression (Parent (Entity (Pref)))));
17782 else
17783 return Empty;
17784 end if;
17785 end Reference_To;
17786
17787 -- Local variables
17788
17789 E : Entity_Id;
17790
17791 -- Start of processing for Object_Access_Level
17792
17793 begin
17794 if Nkind (Obj) = N_Defining_Identifier
17795 or else Is_Entity_Name (Obj)
17796 then
17797 if Nkind (Obj) = N_Defining_Identifier then
17798 E := Obj;
17799 else
17800 E := Entity (Obj);
17801 end if;
17802
17803 if Is_Prival (E) then
17804 E := Prival_Link (E);
17805 end if;
17806
17807 -- If E is a type then it denotes a current instance. For this case
17808 -- we add one to the normal accessibility level of the type to ensure
17809 -- that current instances are treated as always being deeper than
17810 -- than the level of any visible named access type (see 3.10.2(21)).
17811
17812 if Is_Type (E) then
17813 return Type_Access_Level (E) + 1;
17814
17815 elsif Present (Renamed_Object (E)) then
17816 return Object_Access_Level (Renamed_Object (E));
17817
17818 -- Similarly, if E is a component of the current instance of a
17819 -- protected type, any instance of it is assumed to be at a deeper
17820 -- level than the type. For a protected object (whose type is an
17821 -- anonymous protected type) its components are at the same level
17822 -- as the type itself.
17823
17824 elsif not Is_Overloadable (E)
17825 and then Ekind (Scope (E)) = E_Protected_Type
17826 and then Comes_From_Source (Scope (E))
17827 then
17828 return Type_Access_Level (Scope (E)) + 1;
17829
17830 else
17831 -- Aliased formals of functions take their access level from the
17832 -- point of call, i.e. require a dynamic check. For static check
17833 -- purposes, this is smaller than the level of the subprogram
17834 -- itself. For procedures the aliased makes no difference.
17835
17836 if Is_Formal (E)
17837 and then Is_Aliased (E)
17838 and then Ekind (Scope (E)) = E_Function
17839 then
17840 return Type_Access_Level (Etype (E));
17841
17842 else
17843 return Scope_Depth (Enclosing_Dynamic_Scope (E));
17844 end if;
17845 end if;
17846
17847 elsif Nkind (Obj) = N_Selected_Component then
17848 if Is_Access_Type (Etype (Prefix (Obj))) then
17849 return Type_Access_Level (Etype (Prefix (Obj)));
17850 else
17851 return Object_Access_Level (Prefix (Obj));
17852 end if;
17853
17854 elsif Nkind (Obj) = N_Indexed_Component then
17855 if Is_Access_Type (Etype (Prefix (Obj))) then
17856 return Type_Access_Level (Etype (Prefix (Obj)));
17857 else
17858 return Object_Access_Level (Prefix (Obj));
17859 end if;
17860
17861 elsif Nkind (Obj) = N_Explicit_Dereference then
17862
17863 -- If the prefix is a selected access discriminant then we make a
17864 -- recursive call on the prefix, which will in turn check the level
17865 -- of the prefix object of the selected discriminant.
17866
17867 -- In Ada 2012, if the discriminant has implicit dereference and
17868 -- the context is a selected component, treat this as an object of
17869 -- unknown scope (see below). This is necessary in compile-only mode;
17870 -- otherwise expansion will already have transformed the prefix into
17871 -- a temporary.
17872
17873 if Nkind (Prefix (Obj)) = N_Selected_Component
17874 and then Ekind (Etype (Prefix (Obj))) = E_Anonymous_Access_Type
17875 and then
17876 Ekind (Entity (Selector_Name (Prefix (Obj)))) = E_Discriminant
17877 and then
17878 (not Has_Implicit_Dereference
17879 (Entity (Selector_Name (Prefix (Obj))))
17880 or else Nkind (Parent (Obj)) /= N_Selected_Component)
17881 then
17882 return Object_Access_Level (Prefix (Obj));
17883
17884 -- Detect an interface conversion in the context of a dispatching
17885 -- call. Use the original form of the conversion to find the access
17886 -- level of the operand.
17887
17888 elsif Is_Interface (Etype (Obj))
17889 and then Is_Interface_Conversion (Prefix (Obj))
17890 and then Nkind (Original_Node (Obj)) = N_Type_Conversion
17891 then
17892 return Object_Access_Level (Original_Node (Obj));
17893
17894 elsif not Comes_From_Source (Obj) then
17895 declare
17896 Ref : constant Node_Id := Reference_To (Obj);
17897 begin
17898 if Present (Ref) then
17899 return Object_Access_Level (Ref);
17900 else
17901 return Type_Access_Level (Etype (Prefix (Obj)));
17902 end if;
17903 end;
17904
17905 else
17906 return Type_Access_Level (Etype (Prefix (Obj)));
17907 end if;
17908
17909 elsif Nkind_In (Obj, N_Type_Conversion, N_Unchecked_Type_Conversion) then
17910 return Object_Access_Level (Expression (Obj));
17911
17912 elsif Nkind (Obj) = N_Function_Call then
17913
17914 -- Function results are objects, so we get either the access level of
17915 -- the function or, in the case of an indirect call, the level of the
17916 -- access-to-subprogram type. (This code is used for Ada 95, but it
17917 -- looks wrong, because it seems that we should be checking the level
17918 -- of the call itself, even for Ada 95. However, using the Ada 2005
17919 -- version of the code causes regressions in several tests that are
17920 -- compiled with -gnat95. ???)
17921
17922 if Ada_Version < Ada_2005 then
17923 if Is_Entity_Name (Name (Obj)) then
17924 return Subprogram_Access_Level (Entity (Name (Obj)));
17925 else
17926 return Type_Access_Level (Etype (Prefix (Name (Obj))));
17927 end if;
17928
17929 -- For Ada 2005, the level of the result object of a function call is
17930 -- defined to be the level of the call's innermost enclosing master.
17931 -- We determine that by querying the depth of the innermost enclosing
17932 -- dynamic scope.
17933
17934 else
17935 Return_Master_Scope_Depth_Of_Call : declare
17936
17937 function Innermost_Master_Scope_Depth
17938 (N : Node_Id) return Uint;
17939 -- Returns the scope depth of the given node's innermost
17940 -- enclosing dynamic scope (effectively the accessibility
17941 -- level of the innermost enclosing master).
17942
17943 ----------------------------------
17944 -- Innermost_Master_Scope_Depth --
17945 ----------------------------------
17946
17947 function Innermost_Master_Scope_Depth
17948 (N : Node_Id) return Uint
17949 is
17950 Node_Par : Node_Id := Parent (N);
17951
17952 begin
17953 -- Locate the nearest enclosing node (by traversing Parents)
17954 -- that Defining_Entity can be applied to, and return the
17955 -- depth of that entity's nearest enclosing dynamic scope.
17956
17957 while Present (Node_Par) loop
17958 case Nkind (Node_Par) is
17959 when N_Component_Declaration |
17960 N_Entry_Declaration |
17961 N_Formal_Object_Declaration |
17962 N_Formal_Type_Declaration |
17963 N_Full_Type_Declaration |
17964 N_Incomplete_Type_Declaration |
17965 N_Loop_Parameter_Specification |
17966 N_Object_Declaration |
17967 N_Protected_Type_Declaration |
17968 N_Private_Extension_Declaration |
17969 N_Private_Type_Declaration |
17970 N_Subtype_Declaration |
17971 N_Function_Specification |
17972 N_Procedure_Specification |
17973 N_Task_Type_Declaration |
17974 N_Body_Stub |
17975 N_Generic_Instantiation |
17976 N_Proper_Body |
17977 N_Implicit_Label_Declaration |
17978 N_Package_Declaration |
17979 N_Single_Task_Declaration |
17980 N_Subprogram_Declaration |
17981 N_Generic_Declaration |
17982 N_Renaming_Declaration |
17983 N_Block_Statement |
17984 N_Formal_Subprogram_Declaration |
17985 N_Abstract_Subprogram_Declaration |
17986 N_Entry_Body |
17987 N_Exception_Declaration |
17988 N_Formal_Package_Declaration |
17989 N_Number_Declaration |
17990 N_Package_Specification |
17991 N_Parameter_Specification |
17992 N_Single_Protected_Declaration |
17993 N_Subunit =>
17994
17995 return Scope_Depth
17996 (Nearest_Dynamic_Scope
17997 (Defining_Entity (Node_Par)));
17998
17999 when others =>
18000 null;
18001 end case;
18002
18003 Node_Par := Parent (Node_Par);
18004 end loop;
18005
18006 pragma Assert (False);
18007
18008 -- Should never reach the following return
18009
18010 return Scope_Depth (Current_Scope) + 1;
18011 end Innermost_Master_Scope_Depth;
18012
18013 -- Start of processing for Return_Master_Scope_Depth_Of_Call
18014
18015 begin
18016 return Innermost_Master_Scope_Depth (Obj);
18017 end Return_Master_Scope_Depth_Of_Call;
18018 end if;
18019
18020 -- For convenience we handle qualified expressions, even though they
18021 -- aren't technically object names.
18022
18023 elsif Nkind (Obj) = N_Qualified_Expression then
18024 return Object_Access_Level (Expression (Obj));
18025
18026 -- Ditto for aggregates. They have the level of the temporary that
18027 -- will hold their value.
18028
18029 elsif Nkind (Obj) = N_Aggregate then
18030 return Object_Access_Level (Current_Scope);
18031
18032 -- Otherwise return the scope level of Standard. (If there are cases
18033 -- that fall through to this point they will be treated as having
18034 -- global accessibility for now. ???)
18035
18036 else
18037 return Scope_Depth (Standard_Standard);
18038 end if;
18039 end Object_Access_Level;
18040
18041 ---------------------------------
18042 -- Original_Aspect_Pragma_Name --
18043 ---------------------------------
18044
18045 function Original_Aspect_Pragma_Name (N : Node_Id) return Name_Id is
18046 Item : Node_Id;
18047 Item_Nam : Name_Id;
18048
18049 begin
18050 pragma Assert (Nkind_In (N, N_Aspect_Specification, N_Pragma));
18051
18052 Item := N;
18053
18054 -- The pragma was generated to emulate an aspect, use the original
18055 -- aspect specification.
18056
18057 if Nkind (Item) = N_Pragma and then From_Aspect_Specification (Item) then
18058 Item := Corresponding_Aspect (Item);
18059 end if;
18060
18061 -- Retrieve the name of the aspect/pragma. Note that Pre, Pre_Class,
18062 -- Post and Post_Class rewrite their pragma identifier to preserve the
18063 -- original name.
18064 -- ??? this is kludgey
18065
18066 if Nkind (Item) = N_Pragma then
18067 Item_Nam := Chars (Original_Node (Pragma_Identifier (Item)));
18068
18069 else
18070 pragma Assert (Nkind (Item) = N_Aspect_Specification);
18071 Item_Nam := Chars (Identifier (Item));
18072 end if;
18073
18074 -- Deal with 'Class by converting the name to its _XXX form
18075
18076 if Class_Present (Item) then
18077 if Item_Nam = Name_Invariant then
18078 Item_Nam := Name_uInvariant;
18079
18080 elsif Item_Nam = Name_Post then
18081 Item_Nam := Name_uPost;
18082
18083 elsif Item_Nam = Name_Pre then
18084 Item_Nam := Name_uPre;
18085
18086 elsif Nam_In (Item_Nam, Name_Type_Invariant,
18087 Name_Type_Invariant_Class)
18088 then
18089 Item_Nam := Name_uType_Invariant;
18090
18091 -- Nothing to do for other cases (e.g. a Check that derived from
18092 -- Pre_Class and has the flag set). Also we do nothing if the name
18093 -- is already in special _xxx form.
18094
18095 end if;
18096 end if;
18097
18098 return Item_Nam;
18099 end Original_Aspect_Pragma_Name;
18100
18101 --------------------------------------
18102 -- Original_Corresponding_Operation --
18103 --------------------------------------
18104
18105 function Original_Corresponding_Operation (S : Entity_Id) return Entity_Id
18106 is
18107 Typ : constant Entity_Id := Find_Dispatching_Type (S);
18108
18109 begin
18110 -- If S is an inherited primitive S2 the original corresponding
18111 -- operation of S is the original corresponding operation of S2
18112
18113 if Present (Alias (S))
18114 and then Find_Dispatching_Type (Alias (S)) /= Typ
18115 then
18116 return Original_Corresponding_Operation (Alias (S));
18117
18118 -- If S overrides an inherited subprogram S2 the original corresponding
18119 -- operation of S is the original corresponding operation of S2
18120
18121 elsif Present (Overridden_Operation (S)) then
18122 return Original_Corresponding_Operation (Overridden_Operation (S));
18123
18124 -- otherwise it is S itself
18125
18126 else
18127 return S;
18128 end if;
18129 end Original_Corresponding_Operation;
18130
18131 -------------------
18132 -- Output_Entity --
18133 -------------------
18134
18135 procedure Output_Entity (Id : Entity_Id) is
18136 Scop : Entity_Id;
18137
18138 begin
18139 Scop := Scope (Id);
18140
18141 -- The entity may lack a scope when it is in the process of being
18142 -- analyzed. Use the current scope as an approximation.
18143
18144 if No (Scop) then
18145 Scop := Current_Scope;
18146 end if;
18147
18148 Output_Name (Chars (Id), Scop);
18149 end Output_Entity;
18150
18151 -----------------
18152 -- Output_Name --
18153 -----------------
18154
18155 procedure Output_Name (Nam : Name_Id; Scop : Entity_Id := Current_Scope) is
18156 begin
18157 Write_Str
18158 (Get_Name_String
18159 (Get_Qualified_Name
18160 (Nam => Nam,
18161 Suffix => No_Name,
18162 Scop => Scop)));
18163 Write_Eol;
18164 end Output_Name;
18165
18166 ----------------------
18167 -- Policy_In_Effect --
18168 ----------------------
18169
18170 function Policy_In_Effect (Policy : Name_Id) return Name_Id is
18171 function Policy_In_List (List : Node_Id) return Name_Id;
18172 -- Determine the mode of a policy in a N_Pragma list
18173
18174 --------------------
18175 -- Policy_In_List --
18176 --------------------
18177
18178 function Policy_In_List (List : Node_Id) return Name_Id is
18179 Arg1 : Node_Id;
18180 Arg2 : Node_Id;
18181 Prag : Node_Id;
18182
18183 begin
18184 Prag := List;
18185 while Present (Prag) loop
18186 Arg1 := First (Pragma_Argument_Associations (Prag));
18187 Arg2 := Next (Arg1);
18188
18189 Arg1 := Get_Pragma_Arg (Arg1);
18190 Arg2 := Get_Pragma_Arg (Arg2);
18191
18192 -- The current Check_Policy pragma matches the requested policy or
18193 -- appears in the single argument form (Assertion, policy_id).
18194
18195 if Nam_In (Chars (Arg1), Name_Assertion, Policy) then
18196 return Chars (Arg2);
18197 end if;
18198
18199 Prag := Next_Pragma (Prag);
18200 end loop;
18201
18202 return No_Name;
18203 end Policy_In_List;
18204
18205 -- Local variables
18206
18207 Kind : Name_Id;
18208
18209 -- Start of processing for Policy_In_Effect
18210
18211 begin
18212 if not Is_Valid_Assertion_Kind (Policy) then
18213 raise Program_Error;
18214 end if;
18215
18216 -- Inspect all policy pragmas that appear within scopes (if any)
18217
18218 Kind := Policy_In_List (Check_Policy_List);
18219
18220 -- Inspect all configuration policy pragmas (if any)
18221
18222 if Kind = No_Name then
18223 Kind := Policy_In_List (Check_Policy_List_Config);
18224 end if;
18225
18226 -- The context lacks policy pragmas, determine the mode based on whether
18227 -- assertions are enabled at the configuration level. This ensures that
18228 -- the policy is preserved when analyzing generics.
18229
18230 if Kind = No_Name then
18231 if Assertions_Enabled_Config then
18232 Kind := Name_Check;
18233 else
18234 Kind := Name_Ignore;
18235 end if;
18236 end if;
18237
18238 return Kind;
18239 end Policy_In_Effect;
18240
18241 ----------------------------------
18242 -- Predicate_Tests_On_Arguments --
18243 ----------------------------------
18244
18245 function Predicate_Tests_On_Arguments (Subp : Entity_Id) return Boolean is
18246 begin
18247 -- Always test predicates on indirect call
18248
18249 if Ekind (Subp) = E_Subprogram_Type then
18250 return True;
18251
18252 -- Do not test predicates on call to generated default Finalize, since
18253 -- we are not interested in whether something we are finalizing (and
18254 -- typically destroying) satisfies its predicates.
18255
18256 elsif Chars (Subp) = Name_Finalize
18257 and then not Comes_From_Source (Subp)
18258 then
18259 return False;
18260
18261 -- Do not test predicates on any internally generated routines
18262
18263 elsif Is_Internal_Name (Chars (Subp)) then
18264 return False;
18265
18266 -- Do not test predicates on call to Init_Proc, since if needed the
18267 -- predicate test will occur at some other point.
18268
18269 elsif Is_Init_Proc (Subp) then
18270 return False;
18271
18272 -- Do not test predicates on call to predicate function, since this
18273 -- would cause infinite recursion.
18274
18275 elsif Ekind (Subp) = E_Function
18276 and then (Is_Predicate_Function (Subp)
18277 or else
18278 Is_Predicate_Function_M (Subp))
18279 then
18280 return False;
18281
18282 -- For now, no other exceptions
18283
18284 else
18285 return True;
18286 end if;
18287 end Predicate_Tests_On_Arguments;
18288
18289 -----------------------
18290 -- Private_Component --
18291 -----------------------
18292
18293 function Private_Component (Type_Id : Entity_Id) return Entity_Id is
18294 Ancestor : constant Entity_Id := Base_Type (Type_Id);
18295
18296 function Trace_Components
18297 (T : Entity_Id;
18298 Check : Boolean) return Entity_Id;
18299 -- Recursive function that does the work, and checks against circular
18300 -- definition for each subcomponent type.
18301
18302 ----------------------
18303 -- Trace_Components --
18304 ----------------------
18305
18306 function Trace_Components
18307 (T : Entity_Id;
18308 Check : Boolean) return Entity_Id
18309 is
18310 Btype : constant Entity_Id := Base_Type (T);
18311 Component : Entity_Id;
18312 P : Entity_Id;
18313 Candidate : Entity_Id := Empty;
18314
18315 begin
18316 if Check and then Btype = Ancestor then
18317 Error_Msg_N ("circular type definition", Type_Id);
18318 return Any_Type;
18319 end if;
18320
18321 if Is_Private_Type (Btype) and then not Is_Generic_Type (Btype) then
18322 if Present (Full_View (Btype))
18323 and then Is_Record_Type (Full_View (Btype))
18324 and then not Is_Frozen (Btype)
18325 then
18326 -- To indicate that the ancestor depends on a private type, the
18327 -- current Btype is sufficient. However, to check for circular
18328 -- definition we must recurse on the full view.
18329
18330 Candidate := Trace_Components (Full_View (Btype), True);
18331
18332 if Candidate = Any_Type then
18333 return Any_Type;
18334 else
18335 return Btype;
18336 end if;
18337
18338 else
18339 return Btype;
18340 end if;
18341
18342 elsif Is_Array_Type (Btype) then
18343 return Trace_Components (Component_Type (Btype), True);
18344
18345 elsif Is_Record_Type (Btype) then
18346 Component := First_Entity (Btype);
18347 while Present (Component)
18348 and then Comes_From_Source (Component)
18349 loop
18350 -- Skip anonymous types generated by constrained components
18351
18352 if not Is_Type (Component) then
18353 P := Trace_Components (Etype (Component), True);
18354
18355 if Present (P) then
18356 if P = Any_Type then
18357 return P;
18358 else
18359 Candidate := P;
18360 end if;
18361 end if;
18362 end if;
18363
18364 Next_Entity (Component);
18365 end loop;
18366
18367 return Candidate;
18368
18369 else
18370 return Empty;
18371 end if;
18372 end Trace_Components;
18373
18374 -- Start of processing for Private_Component
18375
18376 begin
18377 return Trace_Components (Type_Id, False);
18378 end Private_Component;
18379
18380 ---------------------------
18381 -- Primitive_Names_Match --
18382 ---------------------------
18383
18384 function Primitive_Names_Match (E1, E2 : Entity_Id) return Boolean is
18385
18386 function Non_Internal_Name (E : Entity_Id) return Name_Id;
18387 -- Given an internal name, returns the corresponding non-internal name
18388
18389 ------------------------
18390 -- Non_Internal_Name --
18391 ------------------------
18392
18393 function Non_Internal_Name (E : Entity_Id) return Name_Id is
18394 begin
18395 Get_Name_String (Chars (E));
18396 Name_Len := Name_Len - 1;
18397 return Name_Find;
18398 end Non_Internal_Name;
18399
18400 -- Start of processing for Primitive_Names_Match
18401
18402 begin
18403 pragma Assert (Present (E1) and then Present (E2));
18404
18405 return Chars (E1) = Chars (E2)
18406 or else
18407 (not Is_Internal_Name (Chars (E1))
18408 and then Is_Internal_Name (Chars (E2))
18409 and then Non_Internal_Name (E2) = Chars (E1))
18410 or else
18411 (not Is_Internal_Name (Chars (E2))
18412 and then Is_Internal_Name (Chars (E1))
18413 and then Non_Internal_Name (E1) = Chars (E2))
18414 or else
18415 (Is_Predefined_Dispatching_Operation (E1)
18416 and then Is_Predefined_Dispatching_Operation (E2)
18417 and then Same_TSS (E1, E2))
18418 or else
18419 (Is_Init_Proc (E1) and then Is_Init_Proc (E2));
18420 end Primitive_Names_Match;
18421
18422 -----------------------
18423 -- Process_End_Label --
18424 -----------------------
18425
18426 procedure Process_End_Label
18427 (N : Node_Id;
18428 Typ : Character;
18429 Ent : Entity_Id)
18430 is
18431 Loc : Source_Ptr;
18432 Nam : Node_Id;
18433 Scop : Entity_Id;
18434
18435 Label_Ref : Boolean;
18436 -- Set True if reference to end label itself is required
18437
18438 Endl : Node_Id;
18439 -- Gets set to the operator symbol or identifier that references the
18440 -- entity Ent. For the child unit case, this is the identifier from the
18441 -- designator. For other cases, this is simply Endl.
18442
18443 procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id);
18444 -- N is an identifier node that appears as a parent unit reference in
18445 -- the case where Ent is a child unit. This procedure generates an
18446 -- appropriate cross-reference entry. E is the corresponding entity.
18447
18448 -------------------------
18449 -- Generate_Parent_Ref --
18450 -------------------------
18451
18452 procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id) is
18453 begin
18454 -- If names do not match, something weird, skip reference
18455
18456 if Chars (E) = Chars (N) then
18457
18458 -- Generate the reference. We do NOT consider this as a reference
18459 -- for unreferenced symbol purposes.
18460
18461 Generate_Reference (E, N, 'r', Set_Ref => False, Force => True);
18462
18463 if Style_Check then
18464 Style.Check_Identifier (N, E);
18465 end if;
18466 end if;
18467 end Generate_Parent_Ref;
18468
18469 -- Start of processing for Process_End_Label
18470
18471 begin
18472 -- If no node, ignore. This happens in some error situations, and
18473 -- also for some internally generated structures where no end label
18474 -- references are required in any case.
18475
18476 if No (N) then
18477 return;
18478 end if;
18479
18480 -- Nothing to do if no End_Label, happens for internally generated
18481 -- constructs where we don't want an end label reference anyway. Also
18482 -- nothing to do if Endl is a string literal, which means there was
18483 -- some prior error (bad operator symbol)
18484
18485 Endl := End_Label (N);
18486
18487 if No (Endl) or else Nkind (Endl) = N_String_Literal then
18488 return;
18489 end if;
18490
18491 -- Reference node is not in extended main source unit
18492
18493 if not In_Extended_Main_Source_Unit (N) then
18494
18495 -- Generally we do not collect references except for the extended
18496 -- main source unit. The one exception is the 'e' entry for a
18497 -- package spec, where it is useful for a client to have the
18498 -- ending information to define scopes.
18499
18500 if Typ /= 'e' then
18501 return;
18502
18503 else
18504 Label_Ref := False;
18505
18506 -- For this case, we can ignore any parent references, but we
18507 -- need the package name itself for the 'e' entry.
18508
18509 if Nkind (Endl) = N_Designator then
18510 Endl := Identifier (Endl);
18511 end if;
18512 end if;
18513
18514 -- Reference is in extended main source unit
18515
18516 else
18517 Label_Ref := True;
18518
18519 -- For designator, generate references for the parent entries
18520
18521 if Nkind (Endl) = N_Designator then
18522
18523 -- Generate references for the prefix if the END line comes from
18524 -- source (otherwise we do not need these references) We climb the
18525 -- scope stack to find the expected entities.
18526
18527 if Comes_From_Source (Endl) then
18528 Nam := Name (Endl);
18529 Scop := Current_Scope;
18530 while Nkind (Nam) = N_Selected_Component loop
18531 Scop := Scope (Scop);
18532 exit when No (Scop);
18533 Generate_Parent_Ref (Selector_Name (Nam), Scop);
18534 Nam := Prefix (Nam);
18535 end loop;
18536
18537 if Present (Scop) then
18538 Generate_Parent_Ref (Nam, Scope (Scop));
18539 end if;
18540 end if;
18541
18542 Endl := Identifier (Endl);
18543 end if;
18544 end if;
18545
18546 -- If the end label is not for the given entity, then either we have
18547 -- some previous error, or this is a generic instantiation for which
18548 -- we do not need to make a cross-reference in this case anyway. In
18549 -- either case we simply ignore the call.
18550
18551 if Chars (Ent) /= Chars (Endl) then
18552 return;
18553 end if;
18554
18555 -- If label was really there, then generate a normal reference and then
18556 -- adjust the location in the end label to point past the name (which
18557 -- should almost always be the semicolon).
18558
18559 Loc := Sloc (Endl);
18560
18561 if Comes_From_Source (Endl) then
18562
18563 -- If a label reference is required, then do the style check and
18564 -- generate an l-type cross-reference entry for the label
18565
18566 if Label_Ref then
18567 if Style_Check then
18568 Style.Check_Identifier (Endl, Ent);
18569 end if;
18570
18571 Generate_Reference (Ent, Endl, 'l', Set_Ref => False);
18572 end if;
18573
18574 -- Set the location to point past the label (normally this will
18575 -- mean the semicolon immediately following the label). This is
18576 -- done for the sake of the 'e' or 't' entry generated below.
18577
18578 Get_Decoded_Name_String (Chars (Endl));
18579 Set_Sloc (Endl, Sloc (Endl) + Source_Ptr (Name_Len));
18580
18581 else
18582 -- In SPARK mode, no missing label is allowed for packages and
18583 -- subprogram bodies. Detect those cases by testing whether
18584 -- Process_End_Label was called for a body (Typ = 't') or a package.
18585
18586 if Restriction_Check_Required (SPARK_05)
18587 and then (Typ = 't' or else Ekind (Ent) = E_Package)
18588 then
18589 Error_Msg_Node_1 := Endl;
18590 Check_SPARK_05_Restriction
18591 ("`END &` required", Endl, Force => True);
18592 end if;
18593 end if;
18594
18595 -- Now generate the e/t reference
18596
18597 Generate_Reference (Ent, Endl, Typ, Set_Ref => False, Force => True);
18598
18599 -- Restore Sloc, in case modified above, since we have an identifier
18600 -- and the normal Sloc should be left set in the tree.
18601
18602 Set_Sloc (Endl, Loc);
18603 end Process_End_Label;
18604
18605 ------------------------------------
18606 -- Propagate_Invariant_Attributes --
18607 ------------------------------------
18608
18609 procedure Propagate_Invariant_Attributes
18610 (Typ : Entity_Id;
18611 From_Typ : Entity_Id)
18612 is
18613 Full_IP : Entity_Id;
18614 Part_IP : Entity_Id;
18615
18616 begin
18617 if Present (Typ) and then Present (From_Typ) then
18618 pragma Assert (Is_Type (Typ) and then Is_Type (From_Typ));
18619
18620 -- Nothing to do if both the source and the destination denote the
18621 -- same type.
18622
18623 if From_Typ = Typ then
18624 return;
18625 end if;
18626
18627 Full_IP := Invariant_Procedure (From_Typ);
18628 Part_IP := Partial_Invariant_Procedure (From_Typ);
18629
18630 -- The setting of the attributes is intentionally conservative. This
18631 -- prevents accidental clobbering of enabled attributes.
18632
18633 if Has_Inheritable_Invariants (From_Typ)
18634 and then not Has_Inheritable_Invariants (Typ)
18635 then
18636 Set_Has_Inheritable_Invariants (Typ, True);
18637 end if;
18638
18639 if Has_Inherited_Invariants (From_Typ)
18640 and then not Has_Inherited_Invariants (Typ)
18641 then
18642 Set_Has_Inherited_Invariants (Typ, True);
18643 end if;
18644
18645 if Has_Own_Invariants (From_Typ)
18646 and then not Has_Own_Invariants (Typ)
18647 then
18648 Set_Has_Own_Invariants (Typ, True);
18649 end if;
18650
18651 if Present (Full_IP) and then No (Invariant_Procedure (Typ)) then
18652 Set_Invariant_Procedure (Typ, Full_IP);
18653 end if;
18654
18655 if Present (Part_IP) and then No (Partial_Invariant_Procedure (Typ))
18656 then
18657 Set_Partial_Invariant_Procedure (Typ, Part_IP);
18658 end if;
18659 end if;
18660 end Propagate_Invariant_Attributes;
18661
18662 --------------------------------
18663 -- Propagate_Concurrent_Flags --
18664 --------------------------------
18665
18666 procedure Propagate_Concurrent_Flags
18667 (Typ : Entity_Id;
18668 Comp_Typ : Entity_Id)
18669 is
18670 begin
18671 if Has_Task (Comp_Typ) then
18672 Set_Has_Task (Typ);
18673 end if;
18674
18675 if Has_Protected (Comp_Typ) then
18676 Set_Has_Protected (Typ);
18677 end if;
18678
18679 if Has_Timing_Event (Comp_Typ) then
18680 Set_Has_Timing_Event (Typ);
18681 end if;
18682 end Propagate_Concurrent_Flags;
18683
18684 ---------------------------------------
18685 -- Record_Possible_Part_Of_Reference --
18686 ---------------------------------------
18687
18688 procedure Record_Possible_Part_Of_Reference
18689 (Var_Id : Entity_Id;
18690 Ref : Node_Id)
18691 is
18692 Encap : constant Entity_Id := Encapsulating_State (Var_Id);
18693 Refs : Elist_Id;
18694
18695 begin
18696 -- The variable is a constituent of a single protected/task type. Such
18697 -- a variable acts as a component of the type and must appear within a
18698 -- specific region (SPARK RM 9.3). Instead of recording the reference,
18699 -- verify its legality now.
18700
18701 if Present (Encap) and then Is_Single_Concurrent_Object (Encap) then
18702 Check_Part_Of_Reference (Var_Id, Ref);
18703
18704 -- The variable is subject to pragma Part_Of and may eventually become a
18705 -- constituent of a single protected/task type. Record the reference to
18706 -- verify its placement when the contract of the variable is analyzed.
18707
18708 elsif Present (Get_Pragma (Var_Id, Pragma_Part_Of)) then
18709 Refs := Part_Of_References (Var_Id);
18710
18711 if No (Refs) then
18712 Refs := New_Elmt_List;
18713 Set_Part_Of_References (Var_Id, Refs);
18714 end if;
18715
18716 Append_Elmt (Ref, Refs);
18717 end if;
18718 end Record_Possible_Part_Of_Reference;
18719
18720 ----------------
18721 -- Referenced --
18722 ----------------
18723
18724 function Referenced (Id : Entity_Id; Expr : Node_Id) return Boolean is
18725 Seen : Boolean := False;
18726
18727 function Is_Reference (N : Node_Id) return Traverse_Result;
18728 -- Determine whether node N denotes a reference to Id. If this is the
18729 -- case, set global flag Seen to True and stop the traversal.
18730
18731 ------------------
18732 -- Is_Reference --
18733 ------------------
18734
18735 function Is_Reference (N : Node_Id) return Traverse_Result is
18736 begin
18737 if Is_Entity_Name (N)
18738 and then Present (Entity (N))
18739 and then Entity (N) = Id
18740 then
18741 Seen := True;
18742 return Abandon;
18743 else
18744 return OK;
18745 end if;
18746 end Is_Reference;
18747
18748 procedure Inspect_Expression is new Traverse_Proc (Is_Reference);
18749
18750 -- Start of processing for Referenced
18751
18752 begin
18753 Inspect_Expression (Expr);
18754 return Seen;
18755 end Referenced;
18756
18757 ------------------------------------
18758 -- References_Generic_Formal_Type --
18759 ------------------------------------
18760
18761 function References_Generic_Formal_Type (N : Node_Id) return Boolean is
18762
18763 function Process (N : Node_Id) return Traverse_Result;
18764 -- Process one node in search for generic formal type
18765
18766 -------------
18767 -- Process --
18768 -------------
18769
18770 function Process (N : Node_Id) return Traverse_Result is
18771 begin
18772 if Nkind (N) in N_Has_Entity then
18773 declare
18774 E : constant Entity_Id := Entity (N);
18775 begin
18776 if Present (E) then
18777 if Is_Generic_Type (E) then
18778 return Abandon;
18779 elsif Present (Etype (E))
18780 and then Is_Generic_Type (Etype (E))
18781 then
18782 return Abandon;
18783 end if;
18784 end if;
18785 end;
18786 end if;
18787
18788 return Atree.OK;
18789 end Process;
18790
18791 function Traverse is new Traverse_Func (Process);
18792 -- Traverse tree to look for generic type
18793
18794 begin
18795 if Inside_A_Generic then
18796 return Traverse (N) = Abandon;
18797 else
18798 return False;
18799 end if;
18800 end References_Generic_Formal_Type;
18801
18802 --------------------
18803 -- Remove_Homonym --
18804 --------------------
18805
18806 procedure Remove_Homonym (E : Entity_Id) is
18807 Prev : Entity_Id := Empty;
18808 H : Entity_Id;
18809
18810 begin
18811 if E = Current_Entity (E) then
18812 if Present (Homonym (E)) then
18813 Set_Current_Entity (Homonym (E));
18814 else
18815 Set_Name_Entity_Id (Chars (E), Empty);
18816 end if;
18817
18818 else
18819 H := Current_Entity (E);
18820 while Present (H) and then H /= E loop
18821 Prev := H;
18822 H := Homonym (H);
18823 end loop;
18824
18825 -- If E is not on the homonym chain, nothing to do
18826
18827 if Present (H) then
18828 Set_Homonym (Prev, Homonym (E));
18829 end if;
18830 end if;
18831 end Remove_Homonym;
18832
18833 ------------------------------
18834 -- Remove_Overloaded_Entity --
18835 ------------------------------
18836
18837 procedure Remove_Overloaded_Entity (Id : Entity_Id) is
18838 procedure Remove_Primitive_Of (Typ : Entity_Id);
18839 -- Remove primitive subprogram Id from the list of primitives that
18840 -- belong to type Typ.
18841
18842 -------------------------
18843 -- Remove_Primitive_Of --
18844 -------------------------
18845
18846 procedure Remove_Primitive_Of (Typ : Entity_Id) is
18847 Prims : Elist_Id;
18848
18849 begin
18850 if Is_Tagged_Type (Typ) then
18851 Prims := Direct_Primitive_Operations (Typ);
18852
18853 if Present (Prims) then
18854 Remove (Prims, Id);
18855 end if;
18856 end if;
18857 end Remove_Primitive_Of;
18858
18859 -- Local variables
18860
18861 Scop : constant Entity_Id := Scope (Id);
18862 Formal : Entity_Id;
18863 Prev_Id : Entity_Id;
18864
18865 -- Start of processing for Remove_Overloaded_Entity
18866
18867 begin
18868 -- Remove the entity from the homonym chain. When the entity is the
18869 -- head of the chain, associate the entry in the name table with its
18870 -- homonym effectively making it the new head of the chain.
18871
18872 if Current_Entity (Id) = Id then
18873 Set_Name_Entity_Id (Chars (Id), Homonym (Id));
18874
18875 -- Otherwise link the previous and next homonyms
18876
18877 else
18878 Prev_Id := Current_Entity (Id);
18879 while Present (Prev_Id) and then Homonym (Prev_Id) /= Id loop
18880 Prev_Id := Homonym (Prev_Id);
18881 end loop;
18882
18883 Set_Homonym (Prev_Id, Homonym (Id));
18884 end if;
18885
18886 -- Remove the entity from the scope entity chain. When the entity is
18887 -- the head of the chain, set the next entity as the new head of the
18888 -- chain.
18889
18890 if First_Entity (Scop) = Id then
18891 Prev_Id := Empty;
18892 Set_First_Entity (Scop, Next_Entity (Id));
18893
18894 -- Otherwise the entity is either in the middle of the chain or it acts
18895 -- as its tail. Traverse and link the previous and next entities.
18896
18897 else
18898 Prev_Id := First_Entity (Scop);
18899 while Present (Prev_Id) and then Next_Entity (Prev_Id) /= Id loop
18900 Next_Entity (Prev_Id);
18901 end loop;
18902
18903 Set_Next_Entity (Prev_Id, Next_Entity (Id));
18904 end if;
18905
18906 -- Handle the case where the entity acts as the tail of the scope entity
18907 -- chain.
18908
18909 if Last_Entity (Scop) = Id then
18910 Set_Last_Entity (Scop, Prev_Id);
18911 end if;
18912
18913 -- The entity denotes a primitive subprogram. Remove it from the list of
18914 -- primitives of the associated controlling type.
18915
18916 if Ekind_In (Id, E_Function, E_Procedure) and then Is_Primitive (Id) then
18917 Formal := First_Formal (Id);
18918 while Present (Formal) loop
18919 if Is_Controlling_Formal (Formal) then
18920 Remove_Primitive_Of (Etype (Formal));
18921 exit;
18922 end if;
18923
18924 Next_Formal (Formal);
18925 end loop;
18926
18927 if Ekind (Id) = E_Function and then Has_Controlling_Result (Id) then
18928 Remove_Primitive_Of (Etype (Id));
18929 end if;
18930 end if;
18931 end Remove_Overloaded_Entity;
18932
18933 ---------------------
18934 -- Rep_To_Pos_Flag --
18935 ---------------------
18936
18937 function Rep_To_Pos_Flag (E : Entity_Id; Loc : Source_Ptr) return Node_Id is
18938 begin
18939 return New_Occurrence_Of
18940 (Boolean_Literals (not Range_Checks_Suppressed (E)), Loc);
18941 end Rep_To_Pos_Flag;
18942
18943 --------------------
18944 -- Require_Entity --
18945 --------------------
18946
18947 procedure Require_Entity (N : Node_Id) is
18948 begin
18949 if Is_Entity_Name (N) and then No (Entity (N)) then
18950 if Total_Errors_Detected /= 0 then
18951 Set_Entity (N, Any_Id);
18952 else
18953 raise Program_Error;
18954 end if;
18955 end if;
18956 end Require_Entity;
18957
18958 ------------------------------
18959 -- Requires_Transient_Scope --
18960 ------------------------------
18961
18962 -- A transient scope is required when variable-sized temporaries are
18963 -- allocated on the secondary stack, or when finalization actions must be
18964 -- generated before the next instruction.
18965
18966 function Old_Requires_Transient_Scope (Id : Entity_Id) return Boolean;
18967 function New_Requires_Transient_Scope (Id : Entity_Id) return Boolean;
18968 -- ???We retain the old and new algorithms for Requires_Transient_Scope for
18969 -- the time being. New_Requires_Transient_Scope is used by default; the
18970 -- debug switch -gnatdQ can be used to do Old_Requires_Transient_Scope
18971 -- instead. The intent is to use this temporarily to measure before/after
18972 -- efficiency. Note: when this temporary code is removed, the documentation
18973 -- of dQ in debug.adb should be removed.
18974
18975 procedure Results_Differ (Id : Entity_Id);
18976 -- ???Debugging code. Called when the Old_ and New_ results differ. Will be
18977 -- removed when New_Requires_Transient_Scope becomes
18978 -- Requires_Transient_Scope and Old_Requires_Transient_Scope is eliminated.
18979
18980 procedure Results_Differ (Id : Entity_Id) is
18981 begin
18982 if False then -- False to disable; True for debugging
18983 Treepr.Print_Tree_Node (Id);
18984
18985 if Old_Requires_Transient_Scope (Id) =
18986 New_Requires_Transient_Scope (Id)
18987 then
18988 raise Program_Error;
18989 end if;
18990 end if;
18991 end Results_Differ;
18992
18993 function Requires_Transient_Scope (Id : Entity_Id) return Boolean is
18994 Old_Result : constant Boolean := Old_Requires_Transient_Scope (Id);
18995
18996 begin
18997 if Debug_Flag_QQ then
18998 return Old_Result;
18999 end if;
19000
19001 declare
19002 New_Result : constant Boolean := New_Requires_Transient_Scope (Id);
19003
19004 begin
19005 -- Assert that we're not putting things on the secondary stack if we
19006 -- didn't before; we are trying to AVOID secondary stack when
19007 -- possible.
19008
19009 if not Old_Result then
19010 pragma Assert (not New_Result);
19011 null;
19012 end if;
19013
19014 if New_Result /= Old_Result then
19015 Results_Differ (Id);
19016 end if;
19017
19018 return New_Result;
19019 end;
19020 end Requires_Transient_Scope;
19021
19022 ----------------------------------
19023 -- Old_Requires_Transient_Scope --
19024 ----------------------------------
19025
19026 function Old_Requires_Transient_Scope (Id : Entity_Id) return Boolean is
19027 Typ : constant Entity_Id := Underlying_Type (Id);
19028
19029 begin
19030 -- This is a private type which is not completed yet. This can only
19031 -- happen in a default expression (of a formal parameter or of a
19032 -- record component). Do not expand transient scope in this case.
19033
19034 if No (Typ) then
19035 return False;
19036
19037 -- Do not expand transient scope for non-existent procedure return
19038
19039 elsif Typ = Standard_Void_Type then
19040 return False;
19041
19042 -- Elementary types do not require a transient scope
19043
19044 elsif Is_Elementary_Type (Typ) then
19045 return False;
19046
19047 -- Generally, indefinite subtypes require a transient scope, since the
19048 -- back end cannot generate temporaries, since this is not a valid type
19049 -- for declaring an object. It might be possible to relax this in the
19050 -- future, e.g. by declaring the maximum possible space for the type.
19051
19052 elsif not Is_Definite_Subtype (Typ) then
19053 return True;
19054
19055 -- Functions returning tagged types may dispatch on result so their
19056 -- returned value is allocated on the secondary stack. Controlled
19057 -- type temporaries need finalization.
19058
19059 elsif Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
19060 return True;
19061
19062 -- Record type
19063
19064 elsif Is_Record_Type (Typ) then
19065 declare
19066 Comp : Entity_Id;
19067
19068 begin
19069 Comp := First_Entity (Typ);
19070 while Present (Comp) loop
19071 if Ekind (Comp) = E_Component then
19072
19073 -- ???It's not clear we need a full recursive call to
19074 -- Old_Requires_Transient_Scope here. Note that the
19075 -- following can't happen.
19076
19077 pragma Assert (Is_Definite_Subtype (Etype (Comp)));
19078 pragma Assert (not Has_Controlled_Component (Etype (Comp)));
19079
19080 if Old_Requires_Transient_Scope (Etype (Comp)) then
19081 return True;
19082 end if;
19083 end if;
19084
19085 Next_Entity (Comp);
19086 end loop;
19087 end;
19088
19089 return False;
19090
19091 -- String literal types never require transient scope
19092
19093 elsif Ekind (Typ) = E_String_Literal_Subtype then
19094 return False;
19095
19096 -- Array type. Note that we already know that this is a constrained
19097 -- array, since unconstrained arrays will fail the indefinite test.
19098
19099 elsif Is_Array_Type (Typ) then
19100
19101 -- If component type requires a transient scope, the array does too
19102
19103 if Old_Requires_Transient_Scope (Component_Type (Typ)) then
19104 return True;
19105
19106 -- Otherwise, we only need a transient scope if the size depends on
19107 -- the value of one or more discriminants.
19108
19109 else
19110 return Size_Depends_On_Discriminant (Typ);
19111 end if;
19112
19113 -- All other cases do not require a transient scope
19114
19115 else
19116 pragma Assert (Is_Protected_Type (Typ) or else Is_Task_Type (Typ));
19117 return False;
19118 end if;
19119 end Old_Requires_Transient_Scope;
19120
19121 ----------------------------------
19122 -- New_Requires_Transient_Scope --
19123 ----------------------------------
19124
19125 function New_Requires_Transient_Scope (Id : Entity_Id) return Boolean is
19126
19127 function Caller_Known_Size_Record (Typ : Entity_Id) return Boolean;
19128 -- This is called for untagged records and protected types, with
19129 -- nondefaulted discriminants. Returns True if the size of function
19130 -- results is known at the call site, False otherwise. Returns False
19131 -- if there is a variant part that depends on the discriminants of
19132 -- this type, or if there is an array constrained by the discriminants
19133 -- of this type. ???Currently, this is overly conservative (the array
19134 -- could be nested inside some other record that is constrained by
19135 -- nondiscriminants). That is, the recursive calls are too conservative.
19136
19137 function Large_Max_Size_Mutable (Typ : Entity_Id) return Boolean;
19138 -- Returns True if Typ is a nonlimited record with defaulted
19139 -- discriminants whose max size makes it unsuitable for allocating on
19140 -- the primary stack.
19141
19142 ------------------------------
19143 -- Caller_Known_Size_Record --
19144 ------------------------------
19145
19146 function Caller_Known_Size_Record (Typ : Entity_Id) return Boolean is
19147 pragma Assert (Typ = Underlying_Type (Typ));
19148
19149 begin
19150 if Has_Variant_Part (Typ) and then not Is_Definite_Subtype (Typ) then
19151 return False;
19152 end if;
19153
19154 declare
19155 Comp : Entity_Id;
19156
19157 begin
19158 Comp := First_Entity (Typ);
19159 while Present (Comp) loop
19160
19161 -- Only look at E_Component entities. No need to look at
19162 -- E_Discriminant entities, and we must ignore internal
19163 -- subtypes generated for constrained components.
19164
19165 if Ekind (Comp) = E_Component then
19166 declare
19167 Comp_Type : constant Entity_Id :=
19168 Underlying_Type (Etype (Comp));
19169
19170 begin
19171 if Is_Record_Type (Comp_Type)
19172 or else
19173 Is_Protected_Type (Comp_Type)
19174 then
19175 if not Caller_Known_Size_Record (Comp_Type) then
19176 return False;
19177 end if;
19178
19179 elsif Is_Array_Type (Comp_Type) then
19180 if Size_Depends_On_Discriminant (Comp_Type) then
19181 return False;
19182 end if;
19183 end if;
19184 end;
19185 end if;
19186
19187 Next_Entity (Comp);
19188 end loop;
19189 end;
19190
19191 return True;
19192 end Caller_Known_Size_Record;
19193
19194 ------------------------------
19195 -- Large_Max_Size_Mutable --
19196 ------------------------------
19197
19198 function Large_Max_Size_Mutable (Typ : Entity_Id) return Boolean is
19199 pragma Assert (Typ = Underlying_Type (Typ));
19200
19201 function Is_Large_Discrete_Type (T : Entity_Id) return Boolean;
19202 -- Returns true if the discrete type T has a large range
19203
19204 ----------------------------
19205 -- Is_Large_Discrete_Type --
19206 ----------------------------
19207
19208 function Is_Large_Discrete_Type (T : Entity_Id) return Boolean is
19209 Threshold : constant Int := 16;
19210 -- Arbitrary threshold above which we consider it "large". We want
19211 -- a fairly large threshold, because these large types really
19212 -- shouldn't have default discriminants in the first place, in
19213 -- most cases.
19214
19215 begin
19216 return UI_To_Int (RM_Size (T)) > Threshold;
19217 end Is_Large_Discrete_Type;
19218
19219 begin
19220 if Is_Record_Type (Typ)
19221 and then not Is_Limited_View (Typ)
19222 and then Has_Defaulted_Discriminants (Typ)
19223 then
19224 -- Loop through the components, looking for an array whose upper
19225 -- bound(s) depends on discriminants, where both the subtype of
19226 -- the discriminant and the index subtype are too large.
19227
19228 declare
19229 Comp : Entity_Id;
19230
19231 begin
19232 Comp := First_Entity (Typ);
19233 while Present (Comp) loop
19234 if Ekind (Comp) = E_Component then
19235 declare
19236 Comp_Type : constant Entity_Id :=
19237 Underlying_Type (Etype (Comp));
19238 Indx : Node_Id;
19239 Ityp : Entity_Id;
19240 Hi : Node_Id;
19241
19242 begin
19243 if Is_Array_Type (Comp_Type) then
19244 Indx := First_Index (Comp_Type);
19245
19246 while Present (Indx) loop
19247 Ityp := Etype (Indx);
19248 Hi := Type_High_Bound (Ityp);
19249
19250 if Nkind (Hi) = N_Identifier
19251 and then Ekind (Entity (Hi)) = E_Discriminant
19252 and then Is_Large_Discrete_Type (Ityp)
19253 and then Is_Large_Discrete_Type
19254 (Etype (Entity (Hi)))
19255 then
19256 return True;
19257 end if;
19258
19259 Next_Index (Indx);
19260 end loop;
19261 end if;
19262 end;
19263 end if;
19264
19265 Next_Entity (Comp);
19266 end loop;
19267 end;
19268 end if;
19269
19270 return False;
19271 end Large_Max_Size_Mutable;
19272
19273 -- Local declarations
19274
19275 Typ : constant Entity_Id := Underlying_Type (Id);
19276
19277 -- Start of processing for New_Requires_Transient_Scope
19278
19279 begin
19280 -- This is a private type which is not completed yet. This can only
19281 -- happen in a default expression (of a formal parameter or of a
19282 -- record component). Do not expand transient scope in this case.
19283
19284 if No (Typ) then
19285 return False;
19286
19287 -- Do not expand transient scope for non-existent procedure return or
19288 -- string literal types.
19289
19290 elsif Typ = Standard_Void_Type
19291 or else Ekind (Typ) = E_String_Literal_Subtype
19292 then
19293 return False;
19294
19295 -- If Typ is a generic formal incomplete type, then we want to look at
19296 -- the actual type.
19297
19298 elsif Ekind (Typ) = E_Record_Subtype
19299 and then Present (Cloned_Subtype (Typ))
19300 then
19301 return New_Requires_Transient_Scope (Cloned_Subtype (Typ));
19302
19303 -- Functions returning specific tagged types may dispatch on result, so
19304 -- their returned value is allocated on the secondary stack, even in the
19305 -- definite case. We must treat nondispatching functions the same way,
19306 -- because access-to-function types can point at both, so the calling
19307 -- conventions must be compatible. Is_Tagged_Type includes controlled
19308 -- types and class-wide types. Controlled type temporaries need
19309 -- finalization.
19310
19311 -- ???It's not clear why we need to return noncontrolled types with
19312 -- controlled components on the secondary stack.
19313
19314 elsif Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
19315 return True;
19316
19317 -- Untagged definite subtypes are known size. This includes all
19318 -- elementary [sub]types. Tasks are known size even if they have
19319 -- discriminants. So we return False here, with one exception:
19320 -- For a type like:
19321 -- type T (Last : Natural := 0) is
19322 -- X : String (1 .. Last);
19323 -- end record;
19324 -- we return True. That's because for "P(F(...));", where F returns T,
19325 -- we don't know the size of the result at the call site, so if we
19326 -- allocated it on the primary stack, we would have to allocate the
19327 -- maximum size, which is way too big.
19328
19329 elsif Is_Definite_Subtype (Typ) or else Is_Task_Type (Typ) then
19330 return Large_Max_Size_Mutable (Typ);
19331
19332 -- Indefinite (discriminated) untagged record or protected type
19333
19334 elsif Is_Record_Type (Typ) or else Is_Protected_Type (Typ) then
19335 return not Caller_Known_Size_Record (Typ);
19336
19337 -- Unconstrained array
19338
19339 else
19340 pragma Assert (Is_Array_Type (Typ) and not Is_Definite_Subtype (Typ));
19341 return True;
19342 end if;
19343 end New_Requires_Transient_Scope;
19344
19345 --------------------------
19346 -- Reset_Analyzed_Flags --
19347 --------------------------
19348
19349 procedure Reset_Analyzed_Flags (N : Node_Id) is
19350
19351 function Clear_Analyzed (N : Node_Id) return Traverse_Result;
19352 -- Function used to reset Analyzed flags in tree. Note that we do
19353 -- not reset Analyzed flags in entities, since there is no need to
19354 -- reanalyze entities, and indeed, it is wrong to do so, since it
19355 -- can result in generating auxiliary stuff more than once.
19356
19357 --------------------
19358 -- Clear_Analyzed --
19359 --------------------
19360
19361 function Clear_Analyzed (N : Node_Id) return Traverse_Result is
19362 begin
19363 if not Has_Extension (N) then
19364 Set_Analyzed (N, False);
19365 end if;
19366
19367 return OK;
19368 end Clear_Analyzed;
19369
19370 procedure Reset_Analyzed is new Traverse_Proc (Clear_Analyzed);
19371
19372 -- Start of processing for Reset_Analyzed_Flags
19373
19374 begin
19375 Reset_Analyzed (N);
19376 end Reset_Analyzed_Flags;
19377
19378 ------------------------
19379 -- Restore_SPARK_Mode --
19380 ------------------------
19381
19382 procedure Restore_SPARK_Mode (Mode : SPARK_Mode_Type) is
19383 begin
19384 SPARK_Mode := Mode;
19385 end Restore_SPARK_Mode;
19386
19387 --------------------------------
19388 -- Returns_Unconstrained_Type --
19389 --------------------------------
19390
19391 function Returns_Unconstrained_Type (Subp : Entity_Id) return Boolean is
19392 begin
19393 return Ekind (Subp) = E_Function
19394 and then not Is_Scalar_Type (Etype (Subp))
19395 and then not Is_Access_Type (Etype (Subp))
19396 and then not Is_Constrained (Etype (Subp));
19397 end Returns_Unconstrained_Type;
19398
19399 ----------------------------
19400 -- Root_Type_Of_Full_View --
19401 ----------------------------
19402
19403 function Root_Type_Of_Full_View (T : Entity_Id) return Entity_Id is
19404 Rtyp : constant Entity_Id := Root_Type (T);
19405
19406 begin
19407 -- The root type of the full view may itself be a private type. Keep
19408 -- looking for the ultimate derivation parent.
19409
19410 if Is_Private_Type (Rtyp) and then Present (Full_View (Rtyp)) then
19411 return Root_Type_Of_Full_View (Full_View (Rtyp));
19412 else
19413 return Rtyp;
19414 end if;
19415 end Root_Type_Of_Full_View;
19416
19417 ---------------------------
19418 -- Safe_To_Capture_Value --
19419 ---------------------------
19420
19421 function Safe_To_Capture_Value
19422 (N : Node_Id;
19423 Ent : Entity_Id;
19424 Cond : Boolean := False) return Boolean
19425 is
19426 begin
19427 -- The only entities for which we track constant values are variables
19428 -- which are not renamings, constants, out parameters, and in out
19429 -- parameters, so check if we have this case.
19430
19431 -- Note: it may seem odd to track constant values for constants, but in
19432 -- fact this routine is used for other purposes than simply capturing
19433 -- the value. In particular, the setting of Known[_Non]_Null.
19434
19435 if (Ekind (Ent) = E_Variable and then No (Renamed_Object (Ent)))
19436 or else
19437 Ekind_In (Ent, E_Constant, E_Out_Parameter, E_In_Out_Parameter)
19438 then
19439 null;
19440
19441 -- For conditionals, we also allow loop parameters and all formals,
19442 -- including in parameters.
19443
19444 elsif Cond and then Ekind_In (Ent, E_Loop_Parameter, E_In_Parameter) then
19445 null;
19446
19447 -- For all other cases, not just unsafe, but impossible to capture
19448 -- Current_Value, since the above are the only entities which have
19449 -- Current_Value fields.
19450
19451 else
19452 return False;
19453 end if;
19454
19455 -- Skip if volatile or aliased, since funny things might be going on in
19456 -- these cases which we cannot necessarily track. Also skip any variable
19457 -- for which an address clause is given, or whose address is taken. Also
19458 -- never capture value of library level variables (an attempt to do so
19459 -- can occur in the case of package elaboration code).
19460
19461 if Treat_As_Volatile (Ent)
19462 or else Is_Aliased (Ent)
19463 or else Present (Address_Clause (Ent))
19464 or else Address_Taken (Ent)
19465 or else (Is_Library_Level_Entity (Ent)
19466 and then Ekind (Ent) = E_Variable)
19467 then
19468 return False;
19469 end if;
19470
19471 -- OK, all above conditions are met. We also require that the scope of
19472 -- the reference be the same as the scope of the entity, not counting
19473 -- packages and blocks and loops.
19474
19475 declare
19476 E_Scope : constant Entity_Id := Scope (Ent);
19477 R_Scope : Entity_Id;
19478
19479 begin
19480 R_Scope := Current_Scope;
19481 while R_Scope /= Standard_Standard loop
19482 exit when R_Scope = E_Scope;
19483
19484 if not Ekind_In (R_Scope, E_Package, E_Block, E_Loop) then
19485 return False;
19486 else
19487 R_Scope := Scope (R_Scope);
19488 end if;
19489 end loop;
19490 end;
19491
19492 -- We also require that the reference does not appear in a context
19493 -- where it is not sure to be executed (i.e. a conditional context
19494 -- or an exception handler). We skip this if Cond is True, since the
19495 -- capturing of values from conditional tests handles this ok.
19496
19497 if Cond then
19498 return True;
19499 end if;
19500
19501 declare
19502 Desc : Node_Id;
19503 P : Node_Id;
19504
19505 begin
19506 Desc := N;
19507
19508 -- Seems dubious that case expressions are not handled here ???
19509
19510 P := Parent (N);
19511 while Present (P) loop
19512 if Nkind (P) = N_If_Statement
19513 or else Nkind (P) = N_Case_Statement
19514 or else (Nkind (P) in N_Short_Circuit
19515 and then Desc = Right_Opnd (P))
19516 or else (Nkind (P) = N_If_Expression
19517 and then Desc /= First (Expressions (P)))
19518 or else Nkind (P) = N_Exception_Handler
19519 or else Nkind (P) = N_Selective_Accept
19520 or else Nkind (P) = N_Conditional_Entry_Call
19521 or else Nkind (P) = N_Timed_Entry_Call
19522 or else Nkind (P) = N_Asynchronous_Select
19523 then
19524 return False;
19525
19526 else
19527 Desc := P;
19528 P := Parent (P);
19529
19530 -- A special Ada 2012 case: the original node may be part
19531 -- of the else_actions of a conditional expression, in which
19532 -- case it might not have been expanded yet, and appears in
19533 -- a non-syntactic list of actions. In that case it is clearly
19534 -- not safe to save a value.
19535
19536 if No (P)
19537 and then Is_List_Member (Desc)
19538 and then No (Parent (List_Containing (Desc)))
19539 then
19540 return False;
19541 end if;
19542 end if;
19543 end loop;
19544 end;
19545
19546 -- OK, looks safe to set value
19547
19548 return True;
19549 end Safe_To_Capture_Value;
19550
19551 ---------------
19552 -- Same_Name --
19553 ---------------
19554
19555 function Same_Name (N1, N2 : Node_Id) return Boolean is
19556 K1 : constant Node_Kind := Nkind (N1);
19557 K2 : constant Node_Kind := Nkind (N2);
19558
19559 begin
19560 if (K1 = N_Identifier or else K1 = N_Defining_Identifier)
19561 and then (K2 = N_Identifier or else K2 = N_Defining_Identifier)
19562 then
19563 return Chars (N1) = Chars (N2);
19564
19565 elsif (K1 = N_Selected_Component or else K1 = N_Expanded_Name)
19566 and then (K2 = N_Selected_Component or else K2 = N_Expanded_Name)
19567 then
19568 return Same_Name (Selector_Name (N1), Selector_Name (N2))
19569 and then Same_Name (Prefix (N1), Prefix (N2));
19570
19571 else
19572 return False;
19573 end if;
19574 end Same_Name;
19575
19576 -----------------
19577 -- Same_Object --
19578 -----------------
19579
19580 function Same_Object (Node1, Node2 : Node_Id) return Boolean is
19581 N1 : constant Node_Id := Original_Node (Node1);
19582 N2 : constant Node_Id := Original_Node (Node2);
19583 -- We do the tests on original nodes, since we are most interested
19584 -- in the original source, not any expansion that got in the way.
19585
19586 K1 : constant Node_Kind := Nkind (N1);
19587 K2 : constant Node_Kind := Nkind (N2);
19588
19589 begin
19590 -- First case, both are entities with same entity
19591
19592 if K1 in N_Has_Entity and then K2 in N_Has_Entity then
19593 declare
19594 EN1 : constant Entity_Id := Entity (N1);
19595 EN2 : constant Entity_Id := Entity (N2);
19596 begin
19597 if Present (EN1) and then Present (EN2)
19598 and then (Ekind_In (EN1, E_Variable, E_Constant)
19599 or else Is_Formal (EN1))
19600 and then EN1 = EN2
19601 then
19602 return True;
19603 end if;
19604 end;
19605 end if;
19606
19607 -- Second case, selected component with same selector, same record
19608
19609 if K1 = N_Selected_Component
19610 and then K2 = N_Selected_Component
19611 and then Chars (Selector_Name (N1)) = Chars (Selector_Name (N2))
19612 then
19613 return Same_Object (Prefix (N1), Prefix (N2));
19614
19615 -- Third case, indexed component with same subscripts, same array
19616
19617 elsif K1 = N_Indexed_Component
19618 and then K2 = N_Indexed_Component
19619 and then Same_Object (Prefix (N1), Prefix (N2))
19620 then
19621 declare
19622 E1, E2 : Node_Id;
19623 begin
19624 E1 := First (Expressions (N1));
19625 E2 := First (Expressions (N2));
19626 while Present (E1) loop
19627 if not Same_Value (E1, E2) then
19628 return False;
19629 else
19630 Next (E1);
19631 Next (E2);
19632 end if;
19633 end loop;
19634
19635 return True;
19636 end;
19637
19638 -- Fourth case, slice of same array with same bounds
19639
19640 elsif K1 = N_Slice
19641 and then K2 = N_Slice
19642 and then Nkind (Discrete_Range (N1)) = N_Range
19643 and then Nkind (Discrete_Range (N2)) = N_Range
19644 and then Same_Value (Low_Bound (Discrete_Range (N1)),
19645 Low_Bound (Discrete_Range (N2)))
19646 and then Same_Value (High_Bound (Discrete_Range (N1)),
19647 High_Bound (Discrete_Range (N2)))
19648 then
19649 return Same_Name (Prefix (N1), Prefix (N2));
19650
19651 -- All other cases, not clearly the same object
19652
19653 else
19654 return False;
19655 end if;
19656 end Same_Object;
19657
19658 ---------------
19659 -- Same_Type --
19660 ---------------
19661
19662 function Same_Type (T1, T2 : Entity_Id) return Boolean is
19663 begin
19664 if T1 = T2 then
19665 return True;
19666
19667 elsif not Is_Constrained (T1)
19668 and then not Is_Constrained (T2)
19669 and then Base_Type (T1) = Base_Type (T2)
19670 then
19671 return True;
19672
19673 -- For now don't bother with case of identical constraints, to be
19674 -- fiddled with later on perhaps (this is only used for optimization
19675 -- purposes, so it is not critical to do a best possible job)
19676
19677 else
19678 return False;
19679 end if;
19680 end Same_Type;
19681
19682 ----------------
19683 -- Same_Value --
19684 ----------------
19685
19686 function Same_Value (Node1, Node2 : Node_Id) return Boolean is
19687 begin
19688 if Compile_Time_Known_Value (Node1)
19689 and then Compile_Time_Known_Value (Node2)
19690 and then Expr_Value (Node1) = Expr_Value (Node2)
19691 then
19692 return True;
19693 elsif Same_Object (Node1, Node2) then
19694 return True;
19695 else
19696 return False;
19697 end if;
19698 end Same_Value;
19699
19700 -----------------------------
19701 -- Save_SPARK_Mode_And_Set --
19702 -----------------------------
19703
19704 procedure Save_SPARK_Mode_And_Set
19705 (Context : Entity_Id;
19706 Mode : out SPARK_Mode_Type)
19707 is
19708 begin
19709 -- Save the current mode in effect
19710
19711 Mode := SPARK_Mode;
19712
19713 -- Do not consider illegal or partially decorated constructs
19714
19715 if Ekind (Context) = E_Void or else Error_Posted (Context) then
19716 null;
19717
19718 elsif Present (SPARK_Pragma (Context)) then
19719 SPARK_Mode := Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Context));
19720 end if;
19721 end Save_SPARK_Mode_And_Set;
19722
19723 -------------------------
19724 -- Scalar_Part_Present --
19725 -------------------------
19726
19727 function Scalar_Part_Present (T : Entity_Id) return Boolean is
19728 C : Entity_Id;
19729
19730 begin
19731 if Is_Scalar_Type (T) then
19732 return True;
19733
19734 elsif Is_Array_Type (T) then
19735 return Scalar_Part_Present (Component_Type (T));
19736
19737 elsif Is_Record_Type (T) or else Has_Discriminants (T) then
19738 C := First_Component_Or_Discriminant (T);
19739 while Present (C) loop
19740 if Scalar_Part_Present (Etype (C)) then
19741 return True;
19742 else
19743 Next_Component_Or_Discriminant (C);
19744 end if;
19745 end loop;
19746 end if;
19747
19748 return False;
19749 end Scalar_Part_Present;
19750
19751 ------------------------
19752 -- Scope_Is_Transient --
19753 ------------------------
19754
19755 function Scope_Is_Transient return Boolean is
19756 begin
19757 return Scope_Stack.Table (Scope_Stack.Last).Is_Transient;
19758 end Scope_Is_Transient;
19759
19760 ------------------
19761 -- Scope_Within --
19762 ------------------
19763
19764 function Scope_Within (Scope1, Scope2 : Entity_Id) return Boolean is
19765 Scop : Entity_Id;
19766
19767 begin
19768 Scop := Scope1;
19769 while Scop /= Standard_Standard loop
19770 Scop := Scope (Scop);
19771
19772 if Scop = Scope2 then
19773 return True;
19774 end if;
19775 end loop;
19776
19777 return False;
19778 end Scope_Within;
19779
19780 --------------------------
19781 -- Scope_Within_Or_Same --
19782 --------------------------
19783
19784 function Scope_Within_Or_Same (Scope1, Scope2 : Entity_Id) return Boolean is
19785 Scop : Entity_Id;
19786
19787 begin
19788 Scop := Scope1;
19789 while Scop /= Standard_Standard loop
19790 if Scop = Scope2 then
19791 return True;
19792 else
19793 Scop := Scope (Scop);
19794 end if;
19795 end loop;
19796
19797 return False;
19798 end Scope_Within_Or_Same;
19799
19800 --------------------
19801 -- Set_Convention --
19802 --------------------
19803
19804 procedure Set_Convention (E : Entity_Id; Val : Snames.Convention_Id) is
19805 begin
19806 Basic_Set_Convention (E, Val);
19807
19808 if Is_Type (E)
19809 and then Is_Access_Subprogram_Type (Base_Type (E))
19810 and then Has_Foreign_Convention (E)
19811 then
19812
19813 -- A pragma Convention in an instance may apply to the subtype
19814 -- created for a formal, in which case we have already verified
19815 -- that conventions of actual and formal match and there is nothing
19816 -- to flag on the subtype.
19817
19818 if In_Instance then
19819 null;
19820 else
19821 Set_Can_Use_Internal_Rep (E, False);
19822 end if;
19823 end if;
19824
19825 -- If E is an object or component, and the type of E is an anonymous
19826 -- access type with no convention set, then also set the convention of
19827 -- the anonymous access type. We do not do this for anonymous protected
19828 -- types, since protected types always have the default convention.
19829
19830 if Present (Etype (E))
19831 and then (Is_Object (E)
19832 or else Ekind (E) = E_Component
19833
19834 -- Allow E_Void (happens for pragma Convention appearing
19835 -- in the middle of a record applying to a component)
19836
19837 or else Ekind (E) = E_Void)
19838 then
19839 declare
19840 Typ : constant Entity_Id := Etype (E);
19841
19842 begin
19843 if Ekind_In (Typ, E_Anonymous_Access_Type,
19844 E_Anonymous_Access_Subprogram_Type)
19845 and then not Has_Convention_Pragma (Typ)
19846 then
19847 Basic_Set_Convention (Typ, Val);
19848 Set_Has_Convention_Pragma (Typ);
19849
19850 -- And for the access subprogram type, deal similarly with the
19851 -- designated E_Subprogram_Type if it is also internal (which
19852 -- it always is?)
19853
19854 if Ekind (Typ) = E_Anonymous_Access_Subprogram_Type then
19855 declare
19856 Dtype : constant Entity_Id := Designated_Type (Typ);
19857 begin
19858 if Ekind (Dtype) = E_Subprogram_Type
19859 and then Is_Itype (Dtype)
19860 and then not Has_Convention_Pragma (Dtype)
19861 then
19862 Basic_Set_Convention (Dtype, Val);
19863 Set_Has_Convention_Pragma (Dtype);
19864 end if;
19865 end;
19866 end if;
19867 end if;
19868 end;
19869 end if;
19870 end Set_Convention;
19871
19872 ------------------------
19873 -- Set_Current_Entity --
19874 ------------------------
19875
19876 -- The given entity is to be set as the currently visible definition of its
19877 -- associated name (i.e. the Node_Id associated with its name). All we have
19878 -- to do is to get the name from the identifier, and then set the
19879 -- associated Node_Id to point to the given entity.
19880
19881 procedure Set_Current_Entity (E : Entity_Id) is
19882 begin
19883 Set_Name_Entity_Id (Chars (E), E);
19884 end Set_Current_Entity;
19885
19886 ---------------------------
19887 -- Set_Debug_Info_Needed --
19888 ---------------------------
19889
19890 procedure Set_Debug_Info_Needed (T : Entity_Id) is
19891
19892 procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id);
19893 pragma Inline (Set_Debug_Info_Needed_If_Not_Set);
19894 -- Used to set debug info in a related node if not set already
19895
19896 --------------------------------------
19897 -- Set_Debug_Info_Needed_If_Not_Set --
19898 --------------------------------------
19899
19900 procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id) is
19901 begin
19902 if Present (E) and then not Needs_Debug_Info (E) then
19903 Set_Debug_Info_Needed (E);
19904
19905 -- For a private type, indicate that the full view also needs
19906 -- debug information.
19907
19908 if Is_Type (E)
19909 and then Is_Private_Type (E)
19910 and then Present (Full_View (E))
19911 then
19912 Set_Debug_Info_Needed (Full_View (E));
19913 end if;
19914 end if;
19915 end Set_Debug_Info_Needed_If_Not_Set;
19916
19917 -- Start of processing for Set_Debug_Info_Needed
19918
19919 begin
19920 -- Nothing to do if argument is Empty or has Debug_Info_Off set, which
19921 -- indicates that Debug_Info_Needed is never required for the entity.
19922 -- Nothing to do if entity comes from a predefined file. Library files
19923 -- are compiled without debug information, but inlined bodies of these
19924 -- routines may appear in user code, and debug information on them ends
19925 -- up complicating debugging the user code.
19926
19927 if No (T)
19928 or else Debug_Info_Off (T)
19929 then
19930 return;
19931
19932 elsif In_Inlined_Body
19933 and then Is_Predefined_File_Name
19934 (Unit_File_Name (Get_Source_Unit (Sloc (T))))
19935 then
19936 Set_Needs_Debug_Info (T, False);
19937 end if;
19938
19939 -- Set flag in entity itself. Note that we will go through the following
19940 -- circuitry even if the flag is already set on T. That's intentional,
19941 -- it makes sure that the flag will be set in subsidiary entities.
19942
19943 Set_Needs_Debug_Info (T);
19944
19945 -- Set flag on subsidiary entities if not set already
19946
19947 if Is_Object (T) then
19948 Set_Debug_Info_Needed_If_Not_Set (Etype (T));
19949
19950 elsif Is_Type (T) then
19951 Set_Debug_Info_Needed_If_Not_Set (Etype (T));
19952
19953 if Is_Record_Type (T) then
19954 declare
19955 Ent : Entity_Id := First_Entity (T);
19956 begin
19957 while Present (Ent) loop
19958 Set_Debug_Info_Needed_If_Not_Set (Ent);
19959 Next_Entity (Ent);
19960 end loop;
19961 end;
19962
19963 -- For a class wide subtype, we also need debug information
19964 -- for the equivalent type.
19965
19966 if Ekind (T) = E_Class_Wide_Subtype then
19967 Set_Debug_Info_Needed_If_Not_Set (Equivalent_Type (T));
19968 end if;
19969
19970 elsif Is_Array_Type (T) then
19971 Set_Debug_Info_Needed_If_Not_Set (Component_Type (T));
19972
19973 declare
19974 Indx : Node_Id := First_Index (T);
19975 begin
19976 while Present (Indx) loop
19977 Set_Debug_Info_Needed_If_Not_Set (Etype (Indx));
19978 Indx := Next_Index (Indx);
19979 end loop;
19980 end;
19981
19982 -- For a packed array type, we also need debug information for
19983 -- the type used to represent the packed array. Conversely, we
19984 -- also need it for the former if we need it for the latter.
19985
19986 if Is_Packed (T) then
19987 Set_Debug_Info_Needed_If_Not_Set (Packed_Array_Impl_Type (T));
19988 end if;
19989
19990 if Is_Packed_Array_Impl_Type (T) then
19991 Set_Debug_Info_Needed_If_Not_Set (Original_Array_Type (T));
19992 end if;
19993
19994 elsif Is_Access_Type (T) then
19995 Set_Debug_Info_Needed_If_Not_Set (Directly_Designated_Type (T));
19996
19997 elsif Is_Private_Type (T) then
19998 declare
19999 FV : constant Entity_Id := Full_View (T);
20000
20001 begin
20002 Set_Debug_Info_Needed_If_Not_Set (FV);
20003
20004 -- If the full view is itself a derived private type, we need
20005 -- debug information on its underlying type.
20006
20007 if Present (FV)
20008 and then Is_Private_Type (FV)
20009 and then Present (Underlying_Full_View (FV))
20010 then
20011 Set_Needs_Debug_Info (Underlying_Full_View (FV));
20012 end if;
20013 end;
20014
20015 elsif Is_Protected_Type (T) then
20016 Set_Debug_Info_Needed_If_Not_Set (Corresponding_Record_Type (T));
20017
20018 elsif Is_Scalar_Type (T) then
20019
20020 -- If the subrange bounds are materialized by dedicated constant
20021 -- objects, also include them in the debug info to make sure the
20022 -- debugger can properly use them.
20023
20024 if Present (Scalar_Range (T))
20025 and then Nkind (Scalar_Range (T)) = N_Range
20026 then
20027 declare
20028 Low_Bnd : constant Node_Id := Type_Low_Bound (T);
20029 High_Bnd : constant Node_Id := Type_High_Bound (T);
20030
20031 begin
20032 if Is_Entity_Name (Low_Bnd) then
20033 Set_Debug_Info_Needed_If_Not_Set (Entity (Low_Bnd));
20034 end if;
20035
20036 if Is_Entity_Name (High_Bnd) then
20037 Set_Debug_Info_Needed_If_Not_Set (Entity (High_Bnd));
20038 end if;
20039 end;
20040 end if;
20041 end if;
20042 end if;
20043 end Set_Debug_Info_Needed;
20044
20045 ----------------------------
20046 -- Set_Entity_With_Checks --
20047 ----------------------------
20048
20049 procedure Set_Entity_With_Checks (N : Node_Id; Val : Entity_Id) is
20050 Val_Actual : Entity_Id;
20051 Nod : Node_Id;
20052 Post_Node : Node_Id;
20053
20054 begin
20055 -- Unconditionally set the entity
20056
20057 Set_Entity (N, Val);
20058
20059 -- The node to post on is the selector in the case of an expanded name,
20060 -- and otherwise the node itself.
20061
20062 if Nkind (N) = N_Expanded_Name then
20063 Post_Node := Selector_Name (N);
20064 else
20065 Post_Node := N;
20066 end if;
20067
20068 -- Check for violation of No_Fixed_IO
20069
20070 if Restriction_Check_Required (No_Fixed_IO)
20071 and then
20072 ((RTU_Loaded (Ada_Text_IO)
20073 and then (Is_RTE (Val, RE_Decimal_IO)
20074 or else
20075 Is_RTE (Val, RE_Fixed_IO)))
20076
20077 or else
20078 (RTU_Loaded (Ada_Wide_Text_IO)
20079 and then (Is_RTE (Val, RO_WT_Decimal_IO)
20080 or else
20081 Is_RTE (Val, RO_WT_Fixed_IO)))
20082
20083 or else
20084 (RTU_Loaded (Ada_Wide_Wide_Text_IO)
20085 and then (Is_RTE (Val, RO_WW_Decimal_IO)
20086 or else
20087 Is_RTE (Val, RO_WW_Fixed_IO))))
20088
20089 -- A special extra check, don't complain about a reference from within
20090 -- the Ada.Interrupts package itself!
20091
20092 and then not In_Same_Extended_Unit (N, Val)
20093 then
20094 Check_Restriction (No_Fixed_IO, Post_Node);
20095 end if;
20096
20097 -- Remaining checks are only done on source nodes. Note that we test
20098 -- for violation of No_Fixed_IO even on non-source nodes, because the
20099 -- cases for checking violations of this restriction are instantiations
20100 -- where the reference in the instance has Comes_From_Source False.
20101
20102 if not Comes_From_Source (N) then
20103 return;
20104 end if;
20105
20106 -- Check for violation of No_Abort_Statements, which is triggered by
20107 -- call to Ada.Task_Identification.Abort_Task.
20108
20109 if Restriction_Check_Required (No_Abort_Statements)
20110 and then (Is_RTE (Val, RE_Abort_Task))
20111
20112 -- A special extra check, don't complain about a reference from within
20113 -- the Ada.Task_Identification package itself!
20114
20115 and then not In_Same_Extended_Unit (N, Val)
20116 then
20117 Check_Restriction (No_Abort_Statements, Post_Node);
20118 end if;
20119
20120 if Val = Standard_Long_Long_Integer then
20121 Check_Restriction (No_Long_Long_Integers, Post_Node);
20122 end if;
20123
20124 -- Check for violation of No_Dynamic_Attachment
20125
20126 if Restriction_Check_Required (No_Dynamic_Attachment)
20127 and then RTU_Loaded (Ada_Interrupts)
20128 and then (Is_RTE (Val, RE_Is_Reserved) or else
20129 Is_RTE (Val, RE_Is_Attached) or else
20130 Is_RTE (Val, RE_Current_Handler) or else
20131 Is_RTE (Val, RE_Attach_Handler) or else
20132 Is_RTE (Val, RE_Exchange_Handler) or else
20133 Is_RTE (Val, RE_Detach_Handler) or else
20134 Is_RTE (Val, RE_Reference))
20135
20136 -- A special extra check, don't complain about a reference from within
20137 -- the Ada.Interrupts package itself!
20138
20139 and then not In_Same_Extended_Unit (N, Val)
20140 then
20141 Check_Restriction (No_Dynamic_Attachment, Post_Node);
20142 end if;
20143
20144 -- Check for No_Implementation_Identifiers
20145
20146 if Restriction_Check_Required (No_Implementation_Identifiers) then
20147
20148 -- We have an implementation defined entity if it is marked as
20149 -- implementation defined, or is defined in a package marked as
20150 -- implementation defined. However, library packages themselves
20151 -- are excluded (we don't want to flag Interfaces itself, just
20152 -- the entities within it).
20153
20154 if (Is_Implementation_Defined (Val)
20155 or else
20156 (Present (Scope (Val))
20157 and then Is_Implementation_Defined (Scope (Val))))
20158 and then not (Ekind_In (Val, E_Package, E_Generic_Package)
20159 and then Is_Library_Level_Entity (Val))
20160 then
20161 Check_Restriction (No_Implementation_Identifiers, Post_Node);
20162 end if;
20163 end if;
20164
20165 -- Do the style check
20166
20167 if Style_Check
20168 and then not Suppress_Style_Checks (Val)
20169 and then not In_Instance
20170 then
20171 if Nkind (N) = N_Identifier then
20172 Nod := N;
20173 elsif Nkind (N) = N_Expanded_Name then
20174 Nod := Selector_Name (N);
20175 else
20176 return;
20177 end if;
20178
20179 -- A special situation arises for derived operations, where we want
20180 -- to do the check against the parent (since the Sloc of the derived
20181 -- operation points to the derived type declaration itself).
20182
20183 Val_Actual := Val;
20184 while not Comes_From_Source (Val_Actual)
20185 and then Nkind (Val_Actual) in N_Entity
20186 and then (Ekind (Val_Actual) = E_Enumeration_Literal
20187 or else Is_Subprogram_Or_Generic_Subprogram (Val_Actual))
20188 and then Present (Alias (Val_Actual))
20189 loop
20190 Val_Actual := Alias (Val_Actual);
20191 end loop;
20192
20193 -- Renaming declarations for generic actuals do not come from source,
20194 -- and have a different name from that of the entity they rename, so
20195 -- there is no style check to perform here.
20196
20197 if Chars (Nod) = Chars (Val_Actual) then
20198 Style.Check_Identifier (Nod, Val_Actual);
20199 end if;
20200 end if;
20201
20202 Set_Entity (N, Val);
20203 end Set_Entity_With_Checks;
20204
20205 ------------------------
20206 -- Set_Name_Entity_Id --
20207 ------------------------
20208
20209 procedure Set_Name_Entity_Id (Id : Name_Id; Val : Entity_Id) is
20210 begin
20211 Set_Name_Table_Int (Id, Int (Val));
20212 end Set_Name_Entity_Id;
20213
20214 ---------------------
20215 -- Set_Next_Actual --
20216 ---------------------
20217
20218 procedure Set_Next_Actual (Ass1_Id : Node_Id; Ass2_Id : Node_Id) is
20219 begin
20220 if Nkind (Parent (Ass1_Id)) = N_Parameter_Association then
20221 Set_First_Named_Actual (Parent (Ass1_Id), Ass2_Id);
20222 end if;
20223 end Set_Next_Actual;
20224
20225 ----------------------------------
20226 -- Set_Optimize_Alignment_Flags --
20227 ----------------------------------
20228
20229 procedure Set_Optimize_Alignment_Flags (E : Entity_Id) is
20230 begin
20231 if Optimize_Alignment = 'S' then
20232 Set_Optimize_Alignment_Space (E);
20233 elsif Optimize_Alignment = 'T' then
20234 Set_Optimize_Alignment_Time (E);
20235 end if;
20236 end Set_Optimize_Alignment_Flags;
20237
20238 -----------------------
20239 -- Set_Public_Status --
20240 -----------------------
20241
20242 procedure Set_Public_Status (Id : Entity_Id) is
20243 S : constant Entity_Id := Current_Scope;
20244
20245 function Within_HSS_Or_If (E : Entity_Id) return Boolean;
20246 -- Determines if E is defined within handled statement sequence or
20247 -- an if statement, returns True if so, False otherwise.
20248
20249 ----------------------
20250 -- Within_HSS_Or_If --
20251 ----------------------
20252
20253 function Within_HSS_Or_If (E : Entity_Id) return Boolean is
20254 N : Node_Id;
20255 begin
20256 N := Declaration_Node (E);
20257 loop
20258 N := Parent (N);
20259
20260 if No (N) then
20261 return False;
20262
20263 elsif Nkind_In (N, N_Handled_Sequence_Of_Statements,
20264 N_If_Statement)
20265 then
20266 return True;
20267 end if;
20268 end loop;
20269 end Within_HSS_Or_If;
20270
20271 -- Start of processing for Set_Public_Status
20272
20273 begin
20274 -- Everything in the scope of Standard is public
20275
20276 if S = Standard_Standard then
20277 Set_Is_Public (Id);
20278
20279 -- Entity is definitely not public if enclosing scope is not public
20280
20281 elsif not Is_Public (S) then
20282 return;
20283
20284 -- An object or function declaration that occurs in a handled sequence
20285 -- of statements or within an if statement is the declaration for a
20286 -- temporary object or local subprogram generated by the expander. It
20287 -- never needs to be made public and furthermore, making it public can
20288 -- cause back end problems.
20289
20290 elsif Nkind_In (Parent (Id), N_Object_Declaration,
20291 N_Function_Specification)
20292 and then Within_HSS_Or_If (Id)
20293 then
20294 return;
20295
20296 -- Entities in public packages or records are public
20297
20298 elsif Ekind (S) = E_Package or Is_Record_Type (S) then
20299 Set_Is_Public (Id);
20300
20301 -- The bounds of an entry family declaration can generate object
20302 -- declarations that are visible to the back-end, e.g. in the
20303 -- the declaration of a composite type that contains tasks.
20304
20305 elsif Is_Concurrent_Type (S)
20306 and then not Has_Completion (S)
20307 and then Nkind (Parent (Id)) = N_Object_Declaration
20308 then
20309 Set_Is_Public (Id);
20310 end if;
20311 end Set_Public_Status;
20312
20313 -----------------------------
20314 -- Set_Referenced_Modified --
20315 -----------------------------
20316
20317 procedure Set_Referenced_Modified (N : Node_Id; Out_Param : Boolean) is
20318 Pref : Node_Id;
20319
20320 begin
20321 -- Deal with indexed or selected component where prefix is modified
20322
20323 if Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
20324 Pref := Prefix (N);
20325
20326 -- If prefix is access type, then it is the designated object that is
20327 -- being modified, which means we have no entity to set the flag on.
20328
20329 if No (Etype (Pref)) or else Is_Access_Type (Etype (Pref)) then
20330 return;
20331
20332 -- Otherwise chase the prefix
20333
20334 else
20335 Set_Referenced_Modified (Pref, Out_Param);
20336 end if;
20337
20338 -- Otherwise see if we have an entity name (only other case to process)
20339
20340 elsif Is_Entity_Name (N) and then Present (Entity (N)) then
20341 Set_Referenced_As_LHS (Entity (N), not Out_Param);
20342 Set_Referenced_As_Out_Parameter (Entity (N), Out_Param);
20343 end if;
20344 end Set_Referenced_Modified;
20345
20346 ----------------------------
20347 -- Set_Scope_Is_Transient --
20348 ----------------------------
20349
20350 procedure Set_Scope_Is_Transient (V : Boolean := True) is
20351 begin
20352 Scope_Stack.Table (Scope_Stack.Last).Is_Transient := V;
20353 end Set_Scope_Is_Transient;
20354
20355 -------------------
20356 -- Set_Size_Info --
20357 -------------------
20358
20359 procedure Set_Size_Info (T1, T2 : Entity_Id) is
20360 begin
20361 -- We copy Esize, but not RM_Size, since in general RM_Size is
20362 -- subtype specific and does not get inherited by all subtypes.
20363
20364 Set_Esize (T1, Esize (T2));
20365 Set_Has_Biased_Representation (T1, Has_Biased_Representation (T2));
20366
20367 if Is_Discrete_Or_Fixed_Point_Type (T1)
20368 and then
20369 Is_Discrete_Or_Fixed_Point_Type (T2)
20370 then
20371 Set_Is_Unsigned_Type (T1, Is_Unsigned_Type (T2));
20372 end if;
20373
20374 Set_Alignment (T1, Alignment (T2));
20375 end Set_Size_Info;
20376
20377 --------------------
20378 -- Static_Boolean --
20379 --------------------
20380
20381 function Static_Boolean (N : Node_Id) return Uint is
20382 begin
20383 Analyze_And_Resolve (N, Standard_Boolean);
20384
20385 if N = Error
20386 or else Error_Posted (N)
20387 or else Etype (N) = Any_Type
20388 then
20389 return No_Uint;
20390 end if;
20391
20392 if Is_OK_Static_Expression (N) then
20393 if not Raises_Constraint_Error (N) then
20394 return Expr_Value (N);
20395 else
20396 return No_Uint;
20397 end if;
20398
20399 elsif Etype (N) = Any_Type then
20400 return No_Uint;
20401
20402 else
20403 Flag_Non_Static_Expr
20404 ("static boolean expression required here", N);
20405 return No_Uint;
20406 end if;
20407 end Static_Boolean;
20408
20409 --------------------
20410 -- Static_Integer --
20411 --------------------
20412
20413 function Static_Integer (N : Node_Id) return Uint is
20414 begin
20415 Analyze_And_Resolve (N, Any_Integer);
20416
20417 if N = Error
20418 or else Error_Posted (N)
20419 or else Etype (N) = Any_Type
20420 then
20421 return No_Uint;
20422 end if;
20423
20424 if Is_OK_Static_Expression (N) then
20425 if not Raises_Constraint_Error (N) then
20426 return Expr_Value (N);
20427 else
20428 return No_Uint;
20429 end if;
20430
20431 elsif Etype (N) = Any_Type then
20432 return No_Uint;
20433
20434 else
20435 Flag_Non_Static_Expr
20436 ("static integer expression required here", N);
20437 return No_Uint;
20438 end if;
20439 end Static_Integer;
20440
20441 --------------------------
20442 -- Statically_Different --
20443 --------------------------
20444
20445 function Statically_Different (E1, E2 : Node_Id) return Boolean is
20446 R1 : constant Node_Id := Get_Referenced_Object (E1);
20447 R2 : constant Node_Id := Get_Referenced_Object (E2);
20448 begin
20449 return Is_Entity_Name (R1)
20450 and then Is_Entity_Name (R2)
20451 and then Entity (R1) /= Entity (R2)
20452 and then not Is_Formal (Entity (R1))
20453 and then not Is_Formal (Entity (R2));
20454 end Statically_Different;
20455
20456 --------------------------------------
20457 -- Subject_To_Loop_Entry_Attributes --
20458 --------------------------------------
20459
20460 function Subject_To_Loop_Entry_Attributes (N : Node_Id) return Boolean is
20461 Stmt : Node_Id;
20462
20463 begin
20464 Stmt := N;
20465
20466 -- The expansion mechanism transform a loop subject to at least one
20467 -- 'Loop_Entry attribute into a conditional block. Infinite loops lack
20468 -- the conditional part.
20469
20470 if Nkind_In (Stmt, N_Block_Statement, N_If_Statement)
20471 and then Nkind (Original_Node (N)) = N_Loop_Statement
20472 then
20473 Stmt := Original_Node (N);
20474 end if;
20475
20476 return
20477 Nkind (Stmt) = N_Loop_Statement
20478 and then Present (Identifier (Stmt))
20479 and then Present (Entity (Identifier (Stmt)))
20480 and then Has_Loop_Entry_Attributes (Entity (Identifier (Stmt)));
20481 end Subject_To_Loop_Entry_Attributes;
20482
20483 -----------------------------
20484 -- Subprogram_Access_Level --
20485 -----------------------------
20486
20487 function Subprogram_Access_Level (Subp : Entity_Id) return Uint is
20488 begin
20489 if Present (Alias (Subp)) then
20490 return Subprogram_Access_Level (Alias (Subp));
20491 else
20492 return Scope_Depth (Enclosing_Dynamic_Scope (Subp));
20493 end if;
20494 end Subprogram_Access_Level;
20495
20496 -------------------------------
20497 -- Support_Atomic_Primitives --
20498 -------------------------------
20499
20500 function Support_Atomic_Primitives (Typ : Entity_Id) return Boolean is
20501 Size : Int;
20502
20503 begin
20504 -- Verify the alignment of Typ is known
20505
20506 if not Known_Alignment (Typ) then
20507 return False;
20508 end if;
20509
20510 if Known_Static_Esize (Typ) then
20511 Size := UI_To_Int (Esize (Typ));
20512
20513 -- If the Esize (Object_Size) is unknown at compile time, look at the
20514 -- RM_Size (Value_Size) which may have been set by an explicit rep item.
20515
20516 elsif Known_Static_RM_Size (Typ) then
20517 Size := UI_To_Int (RM_Size (Typ));
20518
20519 -- Otherwise, the size is considered to be unknown.
20520
20521 else
20522 return False;
20523 end if;
20524
20525 -- Check that the size of the component is 8, 16, 32, or 64 bits and
20526 -- that Typ is properly aligned.
20527
20528 case Size is
20529 when 8 | 16 | 32 | 64 =>
20530 return Size = UI_To_Int (Alignment (Typ)) * 8;
20531 when others =>
20532 return False;
20533 end case;
20534 end Support_Atomic_Primitives;
20535
20536 -----------------
20537 -- Trace_Scope --
20538 -----------------
20539
20540 procedure Trace_Scope (N : Node_Id; E : Entity_Id; Msg : String) is
20541 begin
20542 if Debug_Flag_W then
20543 for J in 0 .. Scope_Stack.Last loop
20544 Write_Str (" ");
20545 end loop;
20546
20547 Write_Str (Msg);
20548 Write_Name (Chars (E));
20549 Write_Str (" from ");
20550 Write_Location (Sloc (N));
20551 Write_Eol;
20552 end if;
20553 end Trace_Scope;
20554
20555 -----------------------
20556 -- Transfer_Entities --
20557 -----------------------
20558
20559 procedure Transfer_Entities (From : Entity_Id; To : Entity_Id) is
20560 procedure Set_Public_Status_Of (Id : Entity_Id);
20561 -- Set the Is_Public attribute of arbitrary entity Id by calling routine
20562 -- Set_Public_Status. If successfull and Id denotes a record type, set
20563 -- the Is_Public attribute of its fields.
20564
20565 --------------------------
20566 -- Set_Public_Status_Of --
20567 --------------------------
20568
20569 procedure Set_Public_Status_Of (Id : Entity_Id) is
20570 Field : Entity_Id;
20571
20572 begin
20573 if not Is_Public (Id) then
20574 Set_Public_Status (Id);
20575
20576 -- When the input entity is a public record type, ensure that all
20577 -- its internal fields are also exposed to the linker. The fields
20578 -- of a class-wide type are never made public.
20579
20580 if Is_Public (Id)
20581 and then Is_Record_Type (Id)
20582 and then not Is_Class_Wide_Type (Id)
20583 then
20584 Field := First_Entity (Id);
20585 while Present (Field) loop
20586 Set_Is_Public (Field);
20587 Next_Entity (Field);
20588 end loop;
20589 end if;
20590 end if;
20591 end Set_Public_Status_Of;
20592
20593 -- Local variables
20594
20595 Full_Id : Entity_Id;
20596 Id : Entity_Id;
20597
20598 -- Start of processing for Transfer_Entities
20599
20600 begin
20601 Id := First_Entity (From);
20602
20603 if Present (Id) then
20604
20605 -- Merge the entity chain of the source scope with that of the
20606 -- destination scope.
20607
20608 if Present (Last_Entity (To)) then
20609 Set_Next_Entity (Last_Entity (To), Id);
20610 else
20611 Set_First_Entity (To, Id);
20612 end if;
20613
20614 Set_Last_Entity (To, Last_Entity (From));
20615
20616 -- Inspect the entities of the source scope and update their Scope
20617 -- attribute.
20618
20619 while Present (Id) loop
20620 Set_Scope (Id, To);
20621 Set_Public_Status_Of (Id);
20622
20623 -- Handle an internally generated full view for a private type
20624
20625 if Is_Private_Type (Id)
20626 and then Present (Full_View (Id))
20627 and then Is_Itype (Full_View (Id))
20628 then
20629 Full_Id := Full_View (Id);
20630
20631 Set_Scope (Full_Id, To);
20632 Set_Public_Status_Of (Full_Id);
20633 end if;
20634
20635 Next_Entity (Id);
20636 end loop;
20637
20638 Set_First_Entity (From, Empty);
20639 Set_Last_Entity (From, Empty);
20640 end if;
20641 end Transfer_Entities;
20642
20643 -----------------------
20644 -- Type_Access_Level --
20645 -----------------------
20646
20647 function Type_Access_Level (Typ : Entity_Id) return Uint is
20648 Btyp : Entity_Id;
20649
20650 begin
20651 Btyp := Base_Type (Typ);
20652
20653 -- Ada 2005 (AI-230): For most cases of anonymous access types, we
20654 -- simply use the level where the type is declared. This is true for
20655 -- stand-alone object declarations, and for anonymous access types
20656 -- associated with components the level is the same as that of the
20657 -- enclosing composite type. However, special treatment is needed for
20658 -- the cases of access parameters, return objects of an anonymous access
20659 -- type, and, in Ada 95, access discriminants of limited types.
20660
20661 if Is_Access_Type (Btyp) then
20662 if Ekind (Btyp) = E_Anonymous_Access_Type then
20663
20664 -- If the type is a nonlocal anonymous access type (such as for
20665 -- an access parameter) we treat it as being declared at the
20666 -- library level to ensure that names such as X.all'access don't
20667 -- fail static accessibility checks.
20668
20669 if not Is_Local_Anonymous_Access (Typ) then
20670 return Scope_Depth (Standard_Standard);
20671
20672 -- If this is a return object, the accessibility level is that of
20673 -- the result subtype of the enclosing function. The test here is
20674 -- little complicated, because we have to account for extended
20675 -- return statements that have been rewritten as blocks, in which
20676 -- case we have to find and the Is_Return_Object attribute of the
20677 -- itype's associated object. It would be nice to find a way to
20678 -- simplify this test, but it doesn't seem worthwhile to add a new
20679 -- flag just for purposes of this test. ???
20680
20681 elsif Ekind (Scope (Btyp)) = E_Return_Statement
20682 or else
20683 (Is_Itype (Btyp)
20684 and then Nkind (Associated_Node_For_Itype (Btyp)) =
20685 N_Object_Declaration
20686 and then Is_Return_Object
20687 (Defining_Identifier
20688 (Associated_Node_For_Itype (Btyp))))
20689 then
20690 declare
20691 Scop : Entity_Id;
20692
20693 begin
20694 Scop := Scope (Scope (Btyp));
20695 while Present (Scop) loop
20696 exit when Ekind (Scop) = E_Function;
20697 Scop := Scope (Scop);
20698 end loop;
20699
20700 -- Treat the return object's type as having the level of the
20701 -- function's result subtype (as per RM05-6.5(5.3/2)).
20702
20703 return Type_Access_Level (Etype (Scop));
20704 end;
20705 end if;
20706 end if;
20707
20708 Btyp := Root_Type (Btyp);
20709
20710 -- The accessibility level of anonymous access types associated with
20711 -- discriminants is that of the current instance of the type, and
20712 -- that's deeper than the type itself (AARM 3.10.2 (12.3.21)).
20713
20714 -- AI-402: access discriminants have accessibility based on the
20715 -- object rather than the type in Ada 2005, so the above paragraph
20716 -- doesn't apply.
20717
20718 -- ??? Needs completion with rules from AI-416
20719
20720 if Ada_Version <= Ada_95
20721 and then Ekind (Typ) = E_Anonymous_Access_Type
20722 and then Present (Associated_Node_For_Itype (Typ))
20723 and then Nkind (Associated_Node_For_Itype (Typ)) =
20724 N_Discriminant_Specification
20725 then
20726 return Scope_Depth (Enclosing_Dynamic_Scope (Btyp)) + 1;
20727 end if;
20728 end if;
20729
20730 -- Return library level for a generic formal type. This is done because
20731 -- RM(10.3.2) says that "The statically deeper relationship does not
20732 -- apply to ... a descendant of a generic formal type". Rather than
20733 -- checking at each point where a static accessibility check is
20734 -- performed to see if we are dealing with a formal type, this rule is
20735 -- implemented by having Type_Access_Level and Deepest_Type_Access_Level
20736 -- return extreme values for a formal type; Deepest_Type_Access_Level
20737 -- returns Int'Last. By calling the appropriate function from among the
20738 -- two, we ensure that the static accessibility check will pass if we
20739 -- happen to run into a formal type. More specifically, we should call
20740 -- Deepest_Type_Access_Level instead of Type_Access_Level whenever the
20741 -- call occurs as part of a static accessibility check and the error
20742 -- case is the case where the type's level is too shallow (as opposed
20743 -- to too deep).
20744
20745 if Is_Generic_Type (Root_Type (Btyp)) then
20746 return Scope_Depth (Standard_Standard);
20747 end if;
20748
20749 return Scope_Depth (Enclosing_Dynamic_Scope (Btyp));
20750 end Type_Access_Level;
20751
20752 ------------------------------------
20753 -- Type_Without_Stream_Operation --
20754 ------------------------------------
20755
20756 function Type_Without_Stream_Operation
20757 (T : Entity_Id;
20758 Op : TSS_Name_Type := TSS_Null) return Entity_Id
20759 is
20760 BT : constant Entity_Id := Base_Type (T);
20761 Op_Missing : Boolean;
20762
20763 begin
20764 if not Restriction_Active (No_Default_Stream_Attributes) then
20765 return Empty;
20766 end if;
20767
20768 if Is_Elementary_Type (T) then
20769 if Op = TSS_Null then
20770 Op_Missing :=
20771 No (TSS (BT, TSS_Stream_Read))
20772 or else No (TSS (BT, TSS_Stream_Write));
20773
20774 else
20775 Op_Missing := No (TSS (BT, Op));
20776 end if;
20777
20778 if Op_Missing then
20779 return T;
20780 else
20781 return Empty;
20782 end if;
20783
20784 elsif Is_Array_Type (T) then
20785 return Type_Without_Stream_Operation (Component_Type (T), Op);
20786
20787 elsif Is_Record_Type (T) then
20788 declare
20789 Comp : Entity_Id;
20790 C_Typ : Entity_Id;
20791
20792 begin
20793 Comp := First_Component (T);
20794 while Present (Comp) loop
20795 C_Typ := Type_Without_Stream_Operation (Etype (Comp), Op);
20796
20797 if Present (C_Typ) then
20798 return C_Typ;
20799 end if;
20800
20801 Next_Component (Comp);
20802 end loop;
20803
20804 return Empty;
20805 end;
20806
20807 elsif Is_Private_Type (T) and then Present (Full_View (T)) then
20808 return Type_Without_Stream_Operation (Full_View (T), Op);
20809 else
20810 return Empty;
20811 end if;
20812 end Type_Without_Stream_Operation;
20813
20814 ----------------------------
20815 -- Unique_Defining_Entity --
20816 ----------------------------
20817
20818 function Unique_Defining_Entity (N : Node_Id) return Entity_Id is
20819 begin
20820 return Unique_Entity (Defining_Entity (N));
20821 end Unique_Defining_Entity;
20822
20823 -------------------
20824 -- Unique_Entity --
20825 -------------------
20826
20827 function Unique_Entity (E : Entity_Id) return Entity_Id is
20828 U : Entity_Id := E;
20829 P : Node_Id;
20830
20831 begin
20832 case Ekind (E) is
20833 when E_Constant =>
20834 if Present (Full_View (E)) then
20835 U := Full_View (E);
20836 end if;
20837
20838 when Entry_Kind =>
20839 if Nkind (Parent (E)) = N_Entry_Body then
20840 declare
20841 Prot_Item : Entity_Id;
20842 begin
20843 -- Traverse the entity list of the protected type and locate
20844 -- an entry declaration which matches the entry body.
20845
20846 Prot_Item := First_Entity (Scope (E));
20847 while Present (Prot_Item) loop
20848 if Ekind (Prot_Item) = E_Entry
20849 and then Corresponding_Body (Parent (Prot_Item)) = E
20850 then
20851 U := Prot_Item;
20852 exit;
20853 end if;
20854
20855 Next_Entity (Prot_Item);
20856 end loop;
20857 end;
20858 end if;
20859
20860 when Formal_Kind =>
20861 if Present (Spec_Entity (E)) then
20862 U := Spec_Entity (E);
20863 end if;
20864
20865 when E_Package_Body =>
20866 P := Parent (E);
20867
20868 if Nkind (P) = N_Defining_Program_Unit_Name then
20869 P := Parent (P);
20870 end if;
20871
20872 if Nkind (P) = N_Package_Body
20873 and then Present (Corresponding_Spec (P))
20874 then
20875 U := Corresponding_Spec (P);
20876
20877 elsif Nkind (P) = N_Package_Body_Stub
20878 and then Present (Corresponding_Spec_Of_Stub (P))
20879 then
20880 U := Corresponding_Spec_Of_Stub (P);
20881 end if;
20882
20883 when E_Protected_Body =>
20884 P := Parent (E);
20885
20886 if Nkind (P) = N_Protected_Body
20887 and then Present (Corresponding_Spec (P))
20888 then
20889 U := Corresponding_Spec (P);
20890
20891 elsif Nkind (P) = N_Protected_Body_Stub
20892 and then Present (Corresponding_Spec_Of_Stub (P))
20893 then
20894 U := Corresponding_Spec_Of_Stub (P);
20895 end if;
20896
20897 when E_Subprogram_Body =>
20898 P := Parent (E);
20899
20900 if Nkind (P) = N_Defining_Program_Unit_Name then
20901 P := Parent (P);
20902 end if;
20903
20904 P := Parent (P);
20905
20906 if Nkind (P) = N_Subprogram_Body
20907 and then Present (Corresponding_Spec (P))
20908 then
20909 U := Corresponding_Spec (P);
20910
20911 elsif Nkind (P) = N_Subprogram_Body_Stub
20912 and then Present (Corresponding_Spec_Of_Stub (P))
20913 then
20914 U := Corresponding_Spec_Of_Stub (P);
20915
20916 elsif Nkind (P) = N_Subprogram_Renaming_Declaration then
20917 U := Corresponding_Spec (P);
20918 end if;
20919
20920 when E_Task_Body =>
20921 P := Parent (E);
20922
20923 if Nkind (P) = N_Task_Body
20924 and then Present (Corresponding_Spec (P))
20925 then
20926 U := Corresponding_Spec (P);
20927
20928 elsif Nkind (P) = N_Task_Body_Stub
20929 and then Present (Corresponding_Spec_Of_Stub (P))
20930 then
20931 U := Corresponding_Spec_Of_Stub (P);
20932 end if;
20933
20934 when Type_Kind =>
20935 if Present (Full_View (E)) then
20936 U := Full_View (E);
20937 end if;
20938
20939 when others =>
20940 null;
20941 end case;
20942
20943 return U;
20944 end Unique_Entity;
20945
20946 -----------------
20947 -- Unique_Name --
20948 -----------------
20949
20950 function Unique_Name (E : Entity_Id) return String is
20951
20952 -- Names of E_Subprogram_Body or E_Package_Body entities are not
20953 -- reliable, as they may not include the overloading suffix. Instead,
20954 -- when looking for the name of E or one of its enclosing scope, we get
20955 -- the name of the corresponding Unique_Entity.
20956
20957 function Get_Scoped_Name (E : Entity_Id) return String;
20958 -- Return the name of E prefixed by all the names of the scopes to which
20959 -- E belongs, except for Standard.
20960
20961 ---------------------
20962 -- Get_Scoped_Name --
20963 ---------------------
20964
20965 function Get_Scoped_Name (E : Entity_Id) return String is
20966 Name : constant String := Get_Name_String (Chars (E));
20967 begin
20968 if Has_Fully_Qualified_Name (E)
20969 or else Scope (E) = Standard_Standard
20970 then
20971 return Name;
20972 else
20973 return Get_Scoped_Name (Unique_Entity (Scope (E))) & "__" & Name;
20974 end if;
20975 end Get_Scoped_Name;
20976
20977 -- Start of processing for Unique_Name
20978
20979 begin
20980 if E = Standard_Standard then
20981 return Get_Name_String (Name_Standard);
20982
20983 elsif Scope (E) = Standard_Standard
20984 and then not (Ekind (E) = E_Package or else Is_Subprogram (E))
20985 then
20986 return Get_Name_String (Name_Standard) & "__" &
20987 Get_Name_String (Chars (E));
20988
20989 elsif Ekind (E) = E_Enumeration_Literal then
20990 return Unique_Name (Etype (E)) & "__" & Get_Name_String (Chars (E));
20991
20992 else
20993 return Get_Scoped_Name (Unique_Entity (E));
20994 end if;
20995 end Unique_Name;
20996
20997 ---------------------
20998 -- Unit_Is_Visible --
20999 ---------------------
21000
21001 function Unit_Is_Visible (U : Entity_Id) return Boolean is
21002 Curr : constant Node_Id := Cunit (Current_Sem_Unit);
21003 Curr_Entity : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
21004
21005 function Unit_In_Parent_Context (Par_Unit : Node_Id) return Boolean;
21006 -- For a child unit, check whether unit appears in a with_clause
21007 -- of a parent.
21008
21009 function Unit_In_Context (Comp_Unit : Node_Id) return Boolean;
21010 -- Scan the context clause of one compilation unit looking for a
21011 -- with_clause for the unit in question.
21012
21013 ----------------------------
21014 -- Unit_In_Parent_Context --
21015 ----------------------------
21016
21017 function Unit_In_Parent_Context (Par_Unit : Node_Id) return Boolean is
21018 begin
21019 if Unit_In_Context (Par_Unit) then
21020 return True;
21021
21022 elsif Is_Child_Unit (Defining_Entity (Unit (Par_Unit))) then
21023 return Unit_In_Parent_Context (Parent_Spec (Unit (Par_Unit)));
21024
21025 else
21026 return False;
21027 end if;
21028 end Unit_In_Parent_Context;
21029
21030 ---------------------
21031 -- Unit_In_Context --
21032 ---------------------
21033
21034 function Unit_In_Context (Comp_Unit : Node_Id) return Boolean is
21035 Clause : Node_Id;
21036
21037 begin
21038 Clause := First (Context_Items (Comp_Unit));
21039 while Present (Clause) loop
21040 if Nkind (Clause) = N_With_Clause then
21041 if Library_Unit (Clause) = U then
21042 return True;
21043
21044 -- The with_clause may denote a renaming of the unit we are
21045 -- looking for, eg. Text_IO which renames Ada.Text_IO.
21046
21047 elsif
21048 Renamed_Entity (Entity (Name (Clause))) =
21049 Defining_Entity (Unit (U))
21050 then
21051 return True;
21052 end if;
21053 end if;
21054
21055 Next (Clause);
21056 end loop;
21057
21058 return False;
21059 end Unit_In_Context;
21060
21061 -- Start of processing for Unit_Is_Visible
21062
21063 begin
21064 -- The currrent unit is directly visible
21065
21066 if Curr = U then
21067 return True;
21068
21069 elsif Unit_In_Context (Curr) then
21070 return True;
21071
21072 -- If the current unit is a body, check the context of the spec
21073
21074 elsif Nkind (Unit (Curr)) = N_Package_Body
21075 or else
21076 (Nkind (Unit (Curr)) = N_Subprogram_Body
21077 and then not Acts_As_Spec (Unit (Curr)))
21078 then
21079 if Unit_In_Context (Library_Unit (Curr)) then
21080 return True;
21081 end if;
21082 end if;
21083
21084 -- If the spec is a child unit, examine the parents
21085
21086 if Is_Child_Unit (Curr_Entity) then
21087 if Nkind (Unit (Curr)) in N_Unit_Body then
21088 return
21089 Unit_In_Parent_Context
21090 (Parent_Spec (Unit (Library_Unit (Curr))));
21091 else
21092 return Unit_In_Parent_Context (Parent_Spec (Unit (Curr)));
21093 end if;
21094
21095 else
21096 return False;
21097 end if;
21098 end Unit_Is_Visible;
21099
21100 ------------------------------
21101 -- Universal_Interpretation --
21102 ------------------------------
21103
21104 function Universal_Interpretation (Opnd : Node_Id) return Entity_Id is
21105 Index : Interp_Index;
21106 It : Interp;
21107
21108 begin
21109 -- The argument may be a formal parameter of an operator or subprogram
21110 -- with multiple interpretations, or else an expression for an actual.
21111
21112 if Nkind (Opnd) = N_Defining_Identifier
21113 or else not Is_Overloaded (Opnd)
21114 then
21115 if Etype (Opnd) = Universal_Integer
21116 or else Etype (Opnd) = Universal_Real
21117 then
21118 return Etype (Opnd);
21119 else
21120 return Empty;
21121 end if;
21122
21123 else
21124 Get_First_Interp (Opnd, Index, It);
21125 while Present (It.Typ) loop
21126 if It.Typ = Universal_Integer
21127 or else It.Typ = Universal_Real
21128 then
21129 return It.Typ;
21130 end if;
21131
21132 Get_Next_Interp (Index, It);
21133 end loop;
21134
21135 return Empty;
21136 end if;
21137 end Universal_Interpretation;
21138
21139 ---------------
21140 -- Unqualify --
21141 ---------------
21142
21143 function Unqualify (Expr : Node_Id) return Node_Id is
21144 begin
21145 -- Recurse to handle unlikely case of multiple levels of qualification
21146
21147 if Nkind (Expr) = N_Qualified_Expression then
21148 return Unqualify (Expression (Expr));
21149
21150 -- Normal case, not a qualified expression
21151
21152 else
21153 return Expr;
21154 end if;
21155 end Unqualify;
21156
21157 -----------------------
21158 -- Visible_Ancestors --
21159 -----------------------
21160
21161 function Visible_Ancestors (Typ : Entity_Id) return Elist_Id is
21162 List_1 : Elist_Id;
21163 List_2 : Elist_Id;
21164 Elmt : Elmt_Id;
21165
21166 begin
21167 pragma Assert (Is_Record_Type (Typ) and then Is_Tagged_Type (Typ));
21168
21169 -- Collect all the parents and progenitors of Typ. If the full-view of
21170 -- private parents and progenitors is available then it is used to
21171 -- generate the list of visible ancestors; otherwise their partial
21172 -- view is added to the resulting list.
21173
21174 Collect_Parents
21175 (T => Typ,
21176 List => List_1,
21177 Use_Full_View => True);
21178
21179 Collect_Interfaces
21180 (T => Typ,
21181 Ifaces_List => List_2,
21182 Exclude_Parents => True,
21183 Use_Full_View => True);
21184
21185 -- Join the two lists. Avoid duplications because an interface may
21186 -- simultaneously be parent and progenitor of a type.
21187
21188 Elmt := First_Elmt (List_2);
21189 while Present (Elmt) loop
21190 Append_Unique_Elmt (Node (Elmt), List_1);
21191 Next_Elmt (Elmt);
21192 end loop;
21193
21194 return List_1;
21195 end Visible_Ancestors;
21196
21197 ----------------------
21198 -- Within_Init_Proc --
21199 ----------------------
21200
21201 function Within_Init_Proc return Boolean is
21202 S : Entity_Id;
21203
21204 begin
21205 S := Current_Scope;
21206 while not Is_Overloadable (S) loop
21207 if S = Standard_Standard then
21208 return False;
21209 else
21210 S := Scope (S);
21211 end if;
21212 end loop;
21213
21214 return Is_Init_Proc (S);
21215 end Within_Init_Proc;
21216
21217 ------------------
21218 -- Within_Scope --
21219 ------------------
21220
21221 function Within_Scope (E : Entity_Id; S : Entity_Id) return Boolean is
21222 begin
21223 return Scope_Within_Or_Same (Scope (E), S);
21224 end Within_Scope;
21225
21226 ----------------
21227 -- Wrong_Type --
21228 ----------------
21229
21230 procedure Wrong_Type (Expr : Node_Id; Expected_Type : Entity_Id) is
21231 Found_Type : constant Entity_Id := First_Subtype (Etype (Expr));
21232 Expec_Type : constant Entity_Id := First_Subtype (Expected_Type);
21233
21234 Matching_Field : Entity_Id;
21235 -- Entity to give a more precise suggestion on how to write a one-
21236 -- element positional aggregate.
21237
21238 function Has_One_Matching_Field return Boolean;
21239 -- Determines if Expec_Type is a record type with a single component or
21240 -- discriminant whose type matches the found type or is one dimensional
21241 -- array whose component type matches the found type. In the case of
21242 -- one discriminant, we ignore the variant parts. That's not accurate,
21243 -- but good enough for the warning.
21244
21245 ----------------------------
21246 -- Has_One_Matching_Field --
21247 ----------------------------
21248
21249 function Has_One_Matching_Field return Boolean is
21250 E : Entity_Id;
21251
21252 begin
21253 Matching_Field := Empty;
21254
21255 if Is_Array_Type (Expec_Type)
21256 and then Number_Dimensions (Expec_Type) = 1
21257 and then Covers (Etype (Component_Type (Expec_Type)), Found_Type)
21258 then
21259 -- Use type name if available. This excludes multidimensional
21260 -- arrays and anonymous arrays.
21261
21262 if Comes_From_Source (Expec_Type) then
21263 Matching_Field := Expec_Type;
21264
21265 -- For an assignment, use name of target
21266
21267 elsif Nkind (Parent (Expr)) = N_Assignment_Statement
21268 and then Is_Entity_Name (Name (Parent (Expr)))
21269 then
21270 Matching_Field := Entity (Name (Parent (Expr)));
21271 end if;
21272
21273 return True;
21274
21275 elsif not Is_Record_Type (Expec_Type) then
21276 return False;
21277
21278 else
21279 E := First_Entity (Expec_Type);
21280 loop
21281 if No (E) then
21282 return False;
21283
21284 elsif not Ekind_In (E, E_Discriminant, E_Component)
21285 or else Nam_In (Chars (E), Name_uTag, Name_uParent)
21286 then
21287 Next_Entity (E);
21288
21289 else
21290 exit;
21291 end if;
21292 end loop;
21293
21294 if not Covers (Etype (E), Found_Type) then
21295 return False;
21296
21297 elsif Present (Next_Entity (E))
21298 and then (Ekind (E) = E_Component
21299 or else Ekind (Next_Entity (E)) = E_Discriminant)
21300 then
21301 return False;
21302
21303 else
21304 Matching_Field := E;
21305 return True;
21306 end if;
21307 end if;
21308 end Has_One_Matching_Field;
21309
21310 -- Start of processing for Wrong_Type
21311
21312 begin
21313 -- Don't output message if either type is Any_Type, or if a message
21314 -- has already been posted for this node. We need to do the latter
21315 -- check explicitly (it is ordinarily done in Errout), because we
21316 -- are using ! to force the output of the error messages.
21317
21318 if Expec_Type = Any_Type
21319 or else Found_Type = Any_Type
21320 or else Error_Posted (Expr)
21321 then
21322 return;
21323
21324 -- If one of the types is a Taft-Amendment type and the other it its
21325 -- completion, it must be an illegal use of a TAT in the spec, for
21326 -- which an error was already emitted. Avoid cascaded errors.
21327
21328 elsif Is_Incomplete_Type (Expec_Type)
21329 and then Has_Completion_In_Body (Expec_Type)
21330 and then Full_View (Expec_Type) = Etype (Expr)
21331 then
21332 return;
21333
21334 elsif Is_Incomplete_Type (Etype (Expr))
21335 and then Has_Completion_In_Body (Etype (Expr))
21336 and then Full_View (Etype (Expr)) = Expec_Type
21337 then
21338 return;
21339
21340 -- In an instance, there is an ongoing problem with completion of
21341 -- type derived from private types. Their structure is what Gigi
21342 -- expects, but the Etype is the parent type rather than the
21343 -- derived private type itself. Do not flag error in this case. The
21344 -- private completion is an entity without a parent, like an Itype.
21345 -- Similarly, full and partial views may be incorrect in the instance.
21346 -- There is no simple way to insure that it is consistent ???
21347
21348 -- A similar view discrepancy can happen in an inlined body, for the
21349 -- same reason: inserted body may be outside of the original package
21350 -- and only partial views are visible at the point of insertion.
21351
21352 elsif In_Instance or else In_Inlined_Body then
21353 if Etype (Etype (Expr)) = Etype (Expected_Type)
21354 and then
21355 (Has_Private_Declaration (Expected_Type)
21356 or else Has_Private_Declaration (Etype (Expr)))
21357 and then No (Parent (Expected_Type))
21358 then
21359 return;
21360
21361 elsif Nkind (Parent (Expr)) = N_Qualified_Expression
21362 and then Entity (Subtype_Mark (Parent (Expr))) = Expected_Type
21363 then
21364 return;
21365
21366 elsif Is_Private_Type (Expected_Type)
21367 and then Present (Full_View (Expected_Type))
21368 and then Covers (Full_View (Expected_Type), Etype (Expr))
21369 then
21370 return;
21371
21372 -- Conversely, type of expression may be the private one
21373
21374 elsif Is_Private_Type (Base_Type (Etype (Expr)))
21375 and then Full_View (Base_Type (Etype (Expr))) = Expected_Type
21376 then
21377 return;
21378 end if;
21379 end if;
21380
21381 -- An interesting special check. If the expression is parenthesized
21382 -- and its type corresponds to the type of the sole component of the
21383 -- expected record type, or to the component type of the expected one
21384 -- dimensional array type, then assume we have a bad aggregate attempt.
21385
21386 if Nkind (Expr) in N_Subexpr
21387 and then Paren_Count (Expr) /= 0
21388 and then Has_One_Matching_Field
21389 then
21390 Error_Msg_N ("positional aggregate cannot have one component", Expr);
21391
21392 if Present (Matching_Field) then
21393 if Is_Array_Type (Expec_Type) then
21394 Error_Msg_NE
21395 ("\write instead `&''First ='> ...`", Expr, Matching_Field);
21396 else
21397 Error_Msg_NE
21398 ("\write instead `& ='> ...`", Expr, Matching_Field);
21399 end if;
21400 end if;
21401
21402 -- Another special check, if we are looking for a pool-specific access
21403 -- type and we found an E_Access_Attribute_Type, then we have the case
21404 -- of an Access attribute being used in a context which needs a pool-
21405 -- specific type, which is never allowed. The one extra check we make
21406 -- is that the expected designated type covers the Found_Type.
21407
21408 elsif Is_Access_Type (Expec_Type)
21409 and then Ekind (Found_Type) = E_Access_Attribute_Type
21410 and then Ekind (Base_Type (Expec_Type)) /= E_General_Access_Type
21411 and then Ekind (Base_Type (Expec_Type)) /= E_Anonymous_Access_Type
21412 and then Covers
21413 (Designated_Type (Expec_Type), Designated_Type (Found_Type))
21414 then
21415 Error_Msg_N -- CODEFIX
21416 ("result must be general access type!", Expr);
21417 Error_Msg_NE -- CODEFIX
21418 ("add ALL to }!", Expr, Expec_Type);
21419
21420 -- Another special check, if the expected type is an integer type,
21421 -- but the expression is of type System.Address, and the parent is
21422 -- an addition or subtraction operation whose left operand is the
21423 -- expression in question and whose right operand is of an integral
21424 -- type, then this is an attempt at address arithmetic, so give
21425 -- appropriate message.
21426
21427 elsif Is_Integer_Type (Expec_Type)
21428 and then Is_RTE (Found_Type, RE_Address)
21429 and then Nkind_In (Parent (Expr), N_Op_Add, N_Op_Subtract)
21430 and then Expr = Left_Opnd (Parent (Expr))
21431 and then Is_Integer_Type (Etype (Right_Opnd (Parent (Expr))))
21432 then
21433 Error_Msg_N
21434 ("address arithmetic not predefined in package System",
21435 Parent (Expr));
21436 Error_Msg_N
21437 ("\possible missing with/use of System.Storage_Elements",
21438 Parent (Expr));
21439 return;
21440
21441 -- If the expected type is an anonymous access type, as for access
21442 -- parameters and discriminants, the error is on the designated types.
21443
21444 elsif Ekind (Expec_Type) = E_Anonymous_Access_Type then
21445 if Comes_From_Source (Expec_Type) then
21446 Error_Msg_NE ("expected}!", Expr, Expec_Type);
21447 else
21448 Error_Msg_NE
21449 ("expected an access type with designated}",
21450 Expr, Designated_Type (Expec_Type));
21451 end if;
21452
21453 if Is_Access_Type (Found_Type)
21454 and then not Comes_From_Source (Found_Type)
21455 then
21456 Error_Msg_NE
21457 ("\\found an access type with designated}!",
21458 Expr, Designated_Type (Found_Type));
21459 else
21460 if From_Limited_With (Found_Type) then
21461 Error_Msg_NE ("\\found incomplete}!", Expr, Found_Type);
21462 Error_Msg_Qual_Level := 99;
21463 Error_Msg_NE -- CODEFIX
21464 ("\\missing `WITH &;", Expr, Scope (Found_Type));
21465 Error_Msg_Qual_Level := 0;
21466 else
21467 Error_Msg_NE ("found}!", Expr, Found_Type);
21468 end if;
21469 end if;
21470
21471 -- Normal case of one type found, some other type expected
21472
21473 else
21474 -- If the names of the two types are the same, see if some number
21475 -- of levels of qualification will help. Don't try more than three
21476 -- levels, and if we get to standard, it's no use (and probably
21477 -- represents an error in the compiler) Also do not bother with
21478 -- internal scope names.
21479
21480 declare
21481 Expec_Scope : Entity_Id;
21482 Found_Scope : Entity_Id;
21483
21484 begin
21485 Expec_Scope := Expec_Type;
21486 Found_Scope := Found_Type;
21487
21488 for Levels in Nat range 0 .. 3 loop
21489 if Chars (Expec_Scope) /= Chars (Found_Scope) then
21490 Error_Msg_Qual_Level := Levels;
21491 exit;
21492 end if;
21493
21494 Expec_Scope := Scope (Expec_Scope);
21495 Found_Scope := Scope (Found_Scope);
21496
21497 exit when Expec_Scope = Standard_Standard
21498 or else Found_Scope = Standard_Standard
21499 or else not Comes_From_Source (Expec_Scope)
21500 or else not Comes_From_Source (Found_Scope);
21501 end loop;
21502 end;
21503
21504 if Is_Record_Type (Expec_Type)
21505 and then Present (Corresponding_Remote_Type (Expec_Type))
21506 then
21507 Error_Msg_NE ("expected}!", Expr,
21508 Corresponding_Remote_Type (Expec_Type));
21509 else
21510 Error_Msg_NE ("expected}!", Expr, Expec_Type);
21511 end if;
21512
21513 if Is_Entity_Name (Expr)
21514 and then Is_Package_Or_Generic_Package (Entity (Expr))
21515 then
21516 Error_Msg_N ("\\found package name!", Expr);
21517
21518 elsif Is_Entity_Name (Expr)
21519 and then Ekind_In (Entity (Expr), E_Procedure, E_Generic_Procedure)
21520 then
21521 if Ekind (Expec_Type) = E_Access_Subprogram_Type then
21522 Error_Msg_N
21523 ("found procedure name, possibly missing Access attribute!",
21524 Expr);
21525 else
21526 Error_Msg_N
21527 ("\\found procedure name instead of function!", Expr);
21528 end if;
21529
21530 elsif Nkind (Expr) = N_Function_Call
21531 and then Ekind (Expec_Type) = E_Access_Subprogram_Type
21532 and then Etype (Designated_Type (Expec_Type)) = Etype (Expr)
21533 and then No (Parameter_Associations (Expr))
21534 then
21535 Error_Msg_N
21536 ("found function name, possibly missing Access attribute!",
21537 Expr);
21538
21539 -- Catch common error: a prefix or infix operator which is not
21540 -- directly visible because the type isn't.
21541
21542 elsif Nkind (Expr) in N_Op
21543 and then Is_Overloaded (Expr)
21544 and then not Is_Immediately_Visible (Expec_Type)
21545 and then not Is_Potentially_Use_Visible (Expec_Type)
21546 and then not In_Use (Expec_Type)
21547 and then Has_Compatible_Type (Right_Opnd (Expr), Expec_Type)
21548 then
21549 Error_Msg_N
21550 ("operator of the type is not directly visible!", Expr);
21551
21552 elsif Ekind (Found_Type) = E_Void
21553 and then Present (Parent (Found_Type))
21554 and then Nkind (Parent (Found_Type)) = N_Full_Type_Declaration
21555 then
21556 Error_Msg_NE ("\\found premature usage of}!", Expr, Found_Type);
21557
21558 else
21559 Error_Msg_NE ("\\found}!", Expr, Found_Type);
21560 end if;
21561
21562 -- A special check for cases like M1 and M2 = 0 where M1 and M2 are
21563 -- of the same modular type, and (M1 and M2) = 0 was intended.
21564
21565 if Expec_Type = Standard_Boolean
21566 and then Is_Modular_Integer_Type (Found_Type)
21567 and then Nkind_In (Parent (Expr), N_Op_And, N_Op_Or, N_Op_Xor)
21568 and then Nkind (Right_Opnd (Parent (Expr))) in N_Op_Compare
21569 then
21570 declare
21571 Op : constant Node_Id := Right_Opnd (Parent (Expr));
21572 L : constant Node_Id := Left_Opnd (Op);
21573 R : constant Node_Id := Right_Opnd (Op);
21574
21575 begin
21576 -- The case for the message is when the left operand of the
21577 -- comparison is the same modular type, or when it is an
21578 -- integer literal (or other universal integer expression),
21579 -- which would have been typed as the modular type if the
21580 -- parens had been there.
21581
21582 if (Etype (L) = Found_Type
21583 or else
21584 Etype (L) = Universal_Integer)
21585 and then Is_Integer_Type (Etype (R))
21586 then
21587 Error_Msg_N
21588 ("\\possible missing parens for modular operation", Expr);
21589 end if;
21590 end;
21591 end if;
21592
21593 -- Reset error message qualification indication
21594
21595 Error_Msg_Qual_Level := 0;
21596 end if;
21597 end Wrong_Type;
21598
21599 --------------------------------
21600 -- Yields_Synchronized_Object --
21601 --------------------------------
21602
21603 function Yields_Synchronized_Object (Typ : Entity_Id) return Boolean is
21604 Has_Sync_Comp : Boolean := False;
21605 Id : Entity_Id;
21606
21607 begin
21608 -- An array type yields a synchronized object if its component type
21609 -- yields a synchronized object.
21610
21611 if Is_Array_Type (Typ) then
21612 return Yields_Synchronized_Object (Component_Type (Typ));
21613
21614 -- A descendant of type Ada.Synchronous_Task_Control.Suspension_Object
21615 -- yields a synchronized object by default.
21616
21617 elsif Is_Descendant_Of_Suspension_Object (Typ) then
21618 return True;
21619
21620 -- A protected type yields a synchronized object by default
21621
21622 elsif Is_Protected_Type (Typ) then
21623 return True;
21624
21625 -- A record type or type extension yields a synchronized object when its
21626 -- discriminants (if any) lack default values and all components are of
21627 -- a type that yelds a synchronized object.
21628
21629 elsif Is_Record_Type (Typ) then
21630
21631 -- Inspect all entities defined in the scope of the type, looking for
21632 -- components of a type that does not yeld a synchronized object or
21633 -- for discriminants with default values.
21634
21635 Id := First_Entity (Typ);
21636 while Present (Id) loop
21637 if Comes_From_Source (Id) then
21638 if Ekind (Id) = E_Component then
21639 if Yields_Synchronized_Object (Etype (Id)) then
21640 Has_Sync_Comp := True;
21641
21642 -- The component does not yield a synchronized object
21643
21644 else
21645 return False;
21646 end if;
21647
21648 elsif Ekind (Id) = E_Discriminant
21649 and then Present (Expression (Parent (Id)))
21650 then
21651 return False;
21652 end if;
21653 end if;
21654
21655 Next_Entity (Id);
21656 end loop;
21657
21658 -- Ensure that the parent type of a type extension yields a
21659 -- synchronized object.
21660
21661 if Etype (Typ) /= Typ
21662 and then not Yields_Synchronized_Object (Etype (Typ))
21663 then
21664 return False;
21665 end if;
21666
21667 -- If we get here, then all discriminants lack default values and all
21668 -- components are of a type that yields a synchronized object.
21669
21670 return Has_Sync_Comp;
21671
21672 -- A synchronized interface type yields a synchronized object by default
21673
21674 elsif Is_Synchronized_Interface (Typ) then
21675 return True;
21676
21677 -- A task type yelds a synchronized object by default
21678
21679 elsif Is_Task_Type (Typ) then
21680 return True;
21681
21682 -- Otherwise the type does not yield a synchronized object
21683
21684 else
21685 return False;
21686 end if;
21687 end Yields_Synchronized_Object;
21688
21689 ---------------------------
21690 -- Yields_Universal_Type --
21691 ---------------------------
21692
21693 function Yields_Universal_Type (N : Node_Id) return Boolean is
21694 begin
21695 -- Integer and real literals are of a universal type
21696
21697 if Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
21698 return True;
21699
21700 -- The values of certain attributes are of a universal type
21701
21702 elsif Nkind (N) = N_Attribute_Reference then
21703 return
21704 Universal_Type_Attribute (Get_Attribute_Id (Attribute_Name (N)));
21705
21706 -- ??? There are possibly other cases to consider
21707
21708 else
21709 return False;
21710 end if;
21711 end Yields_Universal_Type;
21712
21713 end Sem_Util;