[multiple changes]
[gcc.git] / gcc / ada / sem_util.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ U T I L --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-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 Lib; use Lib;
41 with Lib.Xref; use Lib.Xref;
42 with Namet.Sp; use Namet.Sp;
43 with Nlists; use Nlists;
44 with Nmake; use Nmake;
45 with Output; use Output;
46 with Restrict; use Restrict;
47 with Rident; use Rident;
48 with Rtsfind; use Rtsfind;
49 with Sem; use Sem;
50 with Sem_Aux; use Sem_Aux;
51 with Sem_Attr; use Sem_Attr;
52 with Sem_Ch6; use Sem_Ch6;
53 with Sem_Ch8; use Sem_Ch8;
54 with Sem_Disp; use Sem_Disp;
55 with Sem_Eval; use Sem_Eval;
56 with Sem_Prag; use Sem_Prag;
57 with Sem_Res; use Sem_Res;
58 with Sem_Warn; use Sem_Warn;
59 with Sem_Type; use Sem_Type;
60 with Sinfo; use Sinfo;
61 with Sinput; use Sinput;
62 with Stand; use Stand;
63 with Style;
64 with Stringt; use Stringt;
65 with Targparm; use Targparm;
66 with Tbuild; use Tbuild;
67 with Ttypes; use Ttypes;
68 with Uname; use Uname;
69
70 with GNAT.HTable; use GNAT.HTable;
71
72 package body Sem_Util is
73
74 ----------------------------------------
75 -- Global Variables for New_Copy_Tree --
76 ----------------------------------------
77
78 -- These global variables are used by New_Copy_Tree. See description of the
79 -- body of this subprogram for details. Global variables can be safely used
80 -- by New_Copy_Tree, since there is no case of a recursive call from the
81 -- processing inside New_Copy_Tree.
82
83 NCT_Hash_Threshold : constant := 20;
84 -- If there are more than this number of pairs of entries in the map, then
85 -- Hash_Tables_Used will be set, and the hash tables will be initialized
86 -- and used for the searches.
87
88 NCT_Hash_Tables_Used : Boolean := False;
89 -- Set to True if hash tables are in use
90
91 NCT_Table_Entries : Nat := 0;
92 -- Count entries in table to see if threshold is reached
93
94 NCT_Hash_Table_Setup : Boolean := False;
95 -- Set to True if hash table contains data. We set this True if we setup
96 -- the hash table with data, and leave it set permanently from then on,
97 -- this is a signal that second and subsequent users of the hash table
98 -- must clear the old entries before reuse.
99
100 subtype NCT_Header_Num is Int range 0 .. 511;
101 -- Defines range of headers in hash tables (512 headers)
102
103 -----------------------
104 -- Local Subprograms --
105 -----------------------
106
107 function Build_Component_Subtype
108 (C : List_Id;
109 Loc : Source_Ptr;
110 T : Entity_Id) return Node_Id;
111 -- This function builds the subtype for Build_Actual_Subtype_Of_Component
112 -- and Build_Discriminal_Subtype_Of_Component. C is a list of constraints,
113 -- Loc is the source location, T is the original subtype.
114
115 function Has_Enabled_Property
116 (Item_Id : Entity_Id;
117 Property : Name_Id) return Boolean;
118 -- Subsidiary to routines Async_xxx_Enabled and Effective_xxx_Enabled.
119 -- Determine whether an abstract state or a variable denoted by entity
120 -- Item_Id has enabled property Property.
121
122 function Has_Null_Extension (T : Entity_Id) return Boolean;
123 -- T is a derived tagged type. Check whether the type extension is null.
124 -- If the parent type is fully initialized, T can be treated as such.
125
126 function Is_Fully_Initialized_Variant (Typ : Entity_Id) return Boolean;
127 -- Subsidiary to Is_Fully_Initialized_Type. For an unconstrained type
128 -- with discriminants whose default values are static, examine only the
129 -- components in the selected variant to determine whether all of them
130 -- have a default.
131
132 ------------------------------
133 -- Abstract_Interface_List --
134 ------------------------------
135
136 function Abstract_Interface_List (Typ : Entity_Id) return List_Id is
137 Nod : Node_Id;
138
139 begin
140 if Is_Concurrent_Type (Typ) then
141
142 -- If we are dealing with a synchronized subtype, go to the base
143 -- type, whose declaration has the interface list.
144
145 -- Shouldn't this be Declaration_Node???
146
147 Nod := Parent (Base_Type (Typ));
148
149 if Nkind (Nod) = N_Full_Type_Declaration then
150 return Empty_List;
151 end if;
152
153 elsif Ekind (Typ) = E_Record_Type_With_Private then
154 if Nkind (Parent (Typ)) = N_Full_Type_Declaration then
155 Nod := Type_Definition (Parent (Typ));
156
157 elsif Nkind (Parent (Typ)) = N_Private_Type_Declaration then
158 if Present (Full_View (Typ))
159 and then
160 Nkind (Parent (Full_View (Typ))) = N_Full_Type_Declaration
161 then
162 Nod := Type_Definition (Parent (Full_View (Typ)));
163
164 -- If the full-view is not available we cannot do anything else
165 -- here (the source has errors).
166
167 else
168 return Empty_List;
169 end if;
170
171 -- Support for generic formals with interfaces is still missing ???
172
173 elsif Nkind (Parent (Typ)) = N_Formal_Type_Declaration then
174 return Empty_List;
175
176 else
177 pragma Assert
178 (Nkind (Parent (Typ)) = N_Private_Extension_Declaration);
179 Nod := Parent (Typ);
180 end if;
181
182 elsif Ekind (Typ) = E_Record_Subtype then
183 Nod := Type_Definition (Parent (Etype (Typ)));
184
185 elsif Ekind (Typ) = E_Record_Subtype_With_Private then
186
187 -- Recurse, because parent may still be a private extension. Also
188 -- note that the full view of the subtype or the full view of its
189 -- base type may (both) be unavailable.
190
191 return Abstract_Interface_List (Etype (Typ));
192
193 else pragma Assert ((Ekind (Typ)) = E_Record_Type);
194 if Nkind (Parent (Typ)) = N_Formal_Type_Declaration then
195 Nod := Formal_Type_Definition (Parent (Typ));
196 else
197 Nod := Type_Definition (Parent (Typ));
198 end if;
199 end if;
200
201 return Interface_List (Nod);
202 end Abstract_Interface_List;
203
204 --------------------------------
205 -- Add_Access_Type_To_Process --
206 --------------------------------
207
208 procedure Add_Access_Type_To_Process (E : Entity_Id; A : Entity_Id) is
209 L : Elist_Id;
210
211 begin
212 Ensure_Freeze_Node (E);
213 L := Access_Types_To_Process (Freeze_Node (E));
214
215 if No (L) then
216 L := New_Elmt_List;
217 Set_Access_Types_To_Process (Freeze_Node (E), L);
218 end if;
219
220 Append_Elmt (A, L);
221 end Add_Access_Type_To_Process;
222
223 --------------------------
224 -- Add_Block_Identifier --
225 --------------------------
226
227 procedure Add_Block_Identifier (N : Node_Id; Id : out Entity_Id) is
228 Loc : constant Source_Ptr := Sloc (N);
229
230 begin
231 pragma Assert (Nkind (N) = N_Block_Statement);
232
233 -- The block already has a label, return its entity
234
235 if Present (Identifier (N)) then
236 Id := Entity (Identifier (N));
237
238 -- Create a new block label and set its attributes
239
240 else
241 Id := New_Internal_Entity (E_Block, Current_Scope, Loc, 'B');
242 Set_Etype (Id, Standard_Void_Type);
243 Set_Parent (Id, N);
244
245 Set_Identifier (N, New_Occurrence_Of (Id, Loc));
246 Set_Block_Node (Id, Identifier (N));
247 end if;
248 end Add_Block_Identifier;
249
250 ----------------------------
251 -- Add_Global_Declaration --
252 ----------------------------
253
254 procedure Add_Global_Declaration (N : Node_Id) is
255 Aux_Node : constant Node_Id := Aux_Decls_Node (Cunit (Current_Sem_Unit));
256
257 begin
258 if No (Declarations (Aux_Node)) then
259 Set_Declarations (Aux_Node, New_List);
260 end if;
261
262 Append_To (Declarations (Aux_Node), N);
263 Analyze (N);
264 end Add_Global_Declaration;
265
266 --------------------------------
267 -- Address_Integer_Convert_OK --
268 --------------------------------
269
270 function Address_Integer_Convert_OK (T1, T2 : Entity_Id) return Boolean is
271 begin
272 if Allow_Integer_Address
273 and then ((Is_Descendant_Of_Address (T1)
274 and then Is_Private_Type (T1)
275 and then Is_Integer_Type (T2))
276 or else
277 (Is_Descendant_Of_Address (T2)
278 and then Is_Private_Type (T2)
279 and then Is_Integer_Type (T1)))
280 then
281 return True;
282 else
283 return False;
284 end if;
285 end Address_Integer_Convert_OK;
286
287 -------------------
288 -- Address_Value --
289 -------------------
290
291 function Address_Value (N : Node_Id) return Node_Id is
292 Expr : Node_Id := N;
293
294 begin
295 loop
296 -- For constant, get constant expression
297
298 if Is_Entity_Name (Expr)
299 and then Ekind (Entity (Expr)) = E_Constant
300 then
301 Expr := Constant_Value (Entity (Expr));
302
303 -- For unchecked conversion, get result to convert
304
305 elsif Nkind (Expr) = N_Unchecked_Type_Conversion then
306 Expr := Expression (Expr);
307
308 -- For (common case) of To_Address call, get argument
309
310 elsif Nkind (Expr) = N_Function_Call
311 and then Is_Entity_Name (Name (Expr))
312 and then Is_RTE (Entity (Name (Expr)), RE_To_Address)
313 then
314 Expr := First (Parameter_Associations (Expr));
315
316 if Nkind (Expr) = N_Parameter_Association then
317 Expr := Explicit_Actual_Parameter (Expr);
318 end if;
319
320 -- We finally have the real expression
321
322 else
323 exit;
324 end if;
325 end loop;
326
327 return Expr;
328 end Address_Value;
329
330 -----------------
331 -- Addressable --
332 -----------------
333
334 -- For now, just 8/16/32/64
335
336 function Addressable (V : Uint) return Boolean is
337 begin
338 return V = Uint_8 or else
339 V = Uint_16 or else
340 V = Uint_32 or else
341 V = Uint_64;
342 end Addressable;
343
344 function Addressable (V : Int) return Boolean is
345 begin
346 return V = 8 or else
347 V = 16 or else
348 V = 32 or else
349 V = 64;
350 end Addressable;
351
352 ---------------------------------
353 -- Aggregate_Constraint_Checks --
354 ---------------------------------
355
356 procedure Aggregate_Constraint_Checks
357 (Exp : Node_Id;
358 Check_Typ : Entity_Id)
359 is
360 Exp_Typ : constant Entity_Id := Etype (Exp);
361
362 begin
363 if Raises_Constraint_Error (Exp) then
364 return;
365 end if;
366
367 -- Ada 2005 (AI-230): Generate a conversion to an anonymous access
368 -- component's type to force the appropriate accessibility checks.
369
370 -- Ada 2005 (AI-231): Generate conversion to the null-excluding type to
371 -- force the corresponding run-time check
372
373 if Is_Access_Type (Check_Typ)
374 and then Is_Local_Anonymous_Access (Check_Typ)
375 then
376 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
377 Analyze_And_Resolve (Exp, Check_Typ);
378 Check_Unset_Reference (Exp);
379 end if;
380
381 -- What follows is really expansion activity, so check that expansion
382 -- is on and is allowed. In GNATprove mode, we also want check flags to
383 -- be added in the tree, so that the formal verification can rely on
384 -- those to be present. In GNATprove mode for formal verification, some
385 -- treatment typically only done during expansion needs to be performed
386 -- on the tree, but it should not be applied inside generics. Otherwise,
387 -- this breaks the name resolution mechanism for generic instances.
388
389 if not Expander_Active
390 and (Inside_A_Generic or not Full_Analysis or not GNATprove_Mode)
391 then
392 return;
393 end if;
394
395 if Is_Access_Type (Check_Typ)
396 and then Can_Never_Be_Null (Check_Typ)
397 and then not Can_Never_Be_Null (Exp_Typ)
398 then
399 Install_Null_Excluding_Check (Exp);
400 end if;
401
402 -- First check if we have to insert discriminant checks
403
404 if Has_Discriminants (Exp_Typ) then
405 Apply_Discriminant_Check (Exp, Check_Typ);
406
407 -- Next emit length checks for array aggregates
408
409 elsif Is_Array_Type (Exp_Typ) then
410 Apply_Length_Check (Exp, Check_Typ);
411
412 -- Finally emit scalar and string checks. If we are dealing with a
413 -- scalar literal we need to check by hand because the Etype of
414 -- literals is not necessarily correct.
415
416 elsif Is_Scalar_Type (Exp_Typ)
417 and then Compile_Time_Known_Value (Exp)
418 then
419 if Is_Out_Of_Range (Exp, Base_Type (Check_Typ)) then
420 Apply_Compile_Time_Constraint_Error
421 (Exp, "value not in range of}??", CE_Range_Check_Failed,
422 Ent => Base_Type (Check_Typ),
423 Typ => Base_Type (Check_Typ));
424
425 elsif Is_Out_Of_Range (Exp, Check_Typ) then
426 Apply_Compile_Time_Constraint_Error
427 (Exp, "value not in range of}??", CE_Range_Check_Failed,
428 Ent => Check_Typ,
429 Typ => Check_Typ);
430
431 elsif not Range_Checks_Suppressed (Check_Typ) then
432 Apply_Scalar_Range_Check (Exp, Check_Typ);
433 end if;
434
435 -- Verify that target type is also scalar, to prevent view anomalies
436 -- in instantiations.
437
438 elsif (Is_Scalar_Type (Exp_Typ)
439 or else Nkind (Exp) = N_String_Literal)
440 and then Is_Scalar_Type (Check_Typ)
441 and then Exp_Typ /= Check_Typ
442 then
443 if Is_Entity_Name (Exp)
444 and then Ekind (Entity (Exp)) = E_Constant
445 then
446 -- If expression is a constant, it is worthwhile checking whether
447 -- it is a bound of the type.
448
449 if (Is_Entity_Name (Type_Low_Bound (Check_Typ))
450 and then Entity (Exp) = Entity (Type_Low_Bound (Check_Typ)))
451 or else
452 (Is_Entity_Name (Type_High_Bound (Check_Typ))
453 and then Entity (Exp) = Entity (Type_High_Bound (Check_Typ)))
454 then
455 return;
456
457 else
458 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
459 Analyze_And_Resolve (Exp, Check_Typ);
460 Check_Unset_Reference (Exp);
461 end if;
462
463 -- Could use a comment on this case ???
464
465 else
466 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
467 Analyze_And_Resolve (Exp, Check_Typ);
468 Check_Unset_Reference (Exp);
469 end if;
470
471 end if;
472 end Aggregate_Constraint_Checks;
473
474 -----------------------
475 -- Alignment_In_Bits --
476 -----------------------
477
478 function Alignment_In_Bits (E : Entity_Id) return Uint is
479 begin
480 return Alignment (E) * System_Storage_Unit;
481 end Alignment_In_Bits;
482
483 --------------------------------------
484 -- All_Composite_Constraints_Static --
485 --------------------------------------
486
487 function All_Composite_Constraints_Static
488 (Constr : Node_Id) return Boolean
489 is
490 begin
491 if No (Constr) or else Error_Posted (Constr) then
492 return True;
493 end if;
494
495 case Nkind (Constr) is
496 when N_Subexpr =>
497 if Nkind (Constr) in N_Has_Entity
498 and then Present (Entity (Constr))
499 then
500 if Is_Type (Entity (Constr)) then
501 return
502 not Is_Discrete_Type (Entity (Constr))
503 or else Is_OK_Static_Subtype (Entity (Constr));
504 end if;
505
506 elsif Nkind (Constr) = N_Range then
507 return
508 Is_OK_Static_Expression (Low_Bound (Constr))
509 and then
510 Is_OK_Static_Expression (High_Bound (Constr));
511
512 elsif Nkind (Constr) = N_Attribute_Reference
513 and then Attribute_Name (Constr) = Name_Range
514 then
515 return
516 Is_OK_Static_Expression
517 (Type_Low_Bound (Etype (Prefix (Constr))))
518 and then
519 Is_OK_Static_Expression
520 (Type_High_Bound (Etype (Prefix (Constr))));
521 end if;
522
523 return
524 not Present (Etype (Constr)) -- previous error
525 or else not Is_Discrete_Type (Etype (Constr))
526 or else Is_OK_Static_Expression (Constr);
527
528 when N_Discriminant_Association =>
529 return All_Composite_Constraints_Static (Expression (Constr));
530
531 when N_Range_Constraint =>
532 return
533 All_Composite_Constraints_Static (Range_Expression (Constr));
534
535 when N_Index_Or_Discriminant_Constraint =>
536 declare
537 One_Cstr : Entity_Id;
538 begin
539 One_Cstr := First (Constraints (Constr));
540 while Present (One_Cstr) loop
541 if not All_Composite_Constraints_Static (One_Cstr) then
542 return False;
543 end if;
544
545 Next (One_Cstr);
546 end loop;
547 end;
548
549 return True;
550
551 when N_Subtype_Indication =>
552 return
553 All_Composite_Constraints_Static (Subtype_Mark (Constr))
554 and then
555 All_Composite_Constraints_Static (Constraint (Constr));
556
557 when others =>
558 raise Program_Error;
559 end case;
560 end All_Composite_Constraints_Static;
561
562 ---------------------------------
563 -- Append_Inherited_Subprogram --
564 ---------------------------------
565
566 procedure Append_Inherited_Subprogram (S : Entity_Id) is
567 Par : constant Entity_Id := Alias (S);
568 -- The parent subprogram
569
570 Scop : constant Entity_Id := Scope (Par);
571 -- The scope of definition of the parent subprogram
572
573 Typ : constant Entity_Id := Defining_Entity (Parent (S));
574 -- The derived type of which S is a primitive operation
575
576 Decl : Node_Id;
577 Next_E : Entity_Id;
578
579 begin
580 if Ekind (Current_Scope) = E_Package
581 and then In_Private_Part (Current_Scope)
582 and then Has_Private_Declaration (Typ)
583 and then Is_Tagged_Type (Typ)
584 and then Scop = Current_Scope
585 then
586 -- The inherited operation is available at the earliest place after
587 -- the derived type declaration ( RM 7.3.1 (6/1)). This is only
588 -- relevant for type extensions. If the parent operation appears
589 -- after the type extension, the operation is not visible.
590
591 Decl := First
592 (Visible_Declarations
593 (Package_Specification (Current_Scope)));
594 while Present (Decl) loop
595 if Nkind (Decl) = N_Private_Extension_Declaration
596 and then Defining_Entity (Decl) = Typ
597 then
598 if Sloc (Decl) > Sloc (Par) then
599 Next_E := Next_Entity (Par);
600 Set_Next_Entity (Par, S);
601 Set_Next_Entity (S, Next_E);
602 return;
603
604 else
605 exit;
606 end if;
607 end if;
608
609 Next (Decl);
610 end loop;
611 end if;
612
613 -- If partial view is not a type extension, or it appears before the
614 -- subprogram declaration, insert normally at end of entity list.
615
616 Append_Entity (S, Current_Scope);
617 end Append_Inherited_Subprogram;
618
619 -----------------------------------------
620 -- Apply_Compile_Time_Constraint_Error --
621 -----------------------------------------
622
623 procedure Apply_Compile_Time_Constraint_Error
624 (N : Node_Id;
625 Msg : String;
626 Reason : RT_Exception_Code;
627 Ent : Entity_Id := Empty;
628 Typ : Entity_Id := Empty;
629 Loc : Source_Ptr := No_Location;
630 Rep : Boolean := True;
631 Warn : Boolean := False)
632 is
633 Stat : constant Boolean := Is_Static_Expression (N);
634 R_Stat : constant Node_Id :=
635 Make_Raise_Constraint_Error (Sloc (N), Reason => Reason);
636 Rtyp : Entity_Id;
637
638 begin
639 if No (Typ) then
640 Rtyp := Etype (N);
641 else
642 Rtyp := Typ;
643 end if;
644
645 Discard_Node
646 (Compile_Time_Constraint_Error (N, Msg, Ent, Loc, Warn => Warn));
647
648 -- In GNATprove mode, do not replace the node with an exception raised.
649 -- In such a case, either the call to Compile_Time_Constraint_Error
650 -- issues an error which stops analysis, or it issues a warning in
651 -- a few cases where a suitable check flag is set for GNATprove to
652 -- generate a check message.
653
654 if not Rep or GNATprove_Mode then
655 return;
656 end if;
657
658 -- Now we replace the node by an N_Raise_Constraint_Error node
659 -- This does not need reanalyzing, so set it as analyzed now.
660
661 Rewrite (N, R_Stat);
662 Set_Analyzed (N, True);
663
664 Set_Etype (N, Rtyp);
665 Set_Raises_Constraint_Error (N);
666
667 -- Now deal with possible local raise handling
668
669 Possible_Local_Raise (N, Standard_Constraint_Error);
670
671 -- If the original expression was marked as static, the result is
672 -- still marked as static, but the Raises_Constraint_Error flag is
673 -- always set so that further static evaluation is not attempted.
674
675 if Stat then
676 Set_Is_Static_Expression (N);
677 end if;
678 end Apply_Compile_Time_Constraint_Error;
679
680 ---------------------------
681 -- Async_Readers_Enabled --
682 ---------------------------
683
684 function Async_Readers_Enabled (Id : Entity_Id) return Boolean is
685 begin
686 return Has_Enabled_Property (Id, Name_Async_Readers);
687 end Async_Readers_Enabled;
688
689 ---------------------------
690 -- Async_Writers_Enabled --
691 ---------------------------
692
693 function Async_Writers_Enabled (Id : Entity_Id) return Boolean is
694 begin
695 return Has_Enabled_Property (Id, Name_Async_Writers);
696 end Async_Writers_Enabled;
697
698 --------------------------------------
699 -- Available_Full_View_Of_Component --
700 --------------------------------------
701
702 function Available_Full_View_Of_Component (T : Entity_Id) return Boolean is
703 ST : constant Entity_Id := Scope (T);
704 SCT : constant Entity_Id := Scope (Component_Type (T));
705 begin
706 return In_Open_Scopes (ST)
707 and then In_Open_Scopes (SCT)
708 and then Scope_Depth (ST) >= Scope_Depth (SCT);
709 end Available_Full_View_Of_Component;
710
711 -------------------
712 -- Bad_Attribute --
713 -------------------
714
715 procedure Bad_Attribute
716 (N : Node_Id;
717 Nam : Name_Id;
718 Warn : Boolean := False)
719 is
720 begin
721 Error_Msg_Warn := Warn;
722 Error_Msg_N ("unrecognized attribute&<<", N);
723
724 -- Check for possible misspelling
725
726 Error_Msg_Name_1 := First_Attribute_Name;
727 while Error_Msg_Name_1 <= Last_Attribute_Name loop
728 if Is_Bad_Spelling_Of (Nam, Error_Msg_Name_1) then
729 Error_Msg_N -- CODEFIX
730 ("\possible misspelling of %<<", N);
731 exit;
732 end if;
733
734 Error_Msg_Name_1 := Error_Msg_Name_1 + 1;
735 end loop;
736 end Bad_Attribute;
737
738 --------------------------------
739 -- Bad_Predicated_Subtype_Use --
740 --------------------------------
741
742 procedure Bad_Predicated_Subtype_Use
743 (Msg : String;
744 N : Node_Id;
745 Typ : Entity_Id;
746 Suggest_Static : Boolean := False)
747 is
748 Gen : Entity_Id;
749
750 begin
751 -- Avoid cascaded errors
752
753 if Error_Posted (N) then
754 return;
755 end if;
756
757 if Inside_A_Generic then
758 Gen := Current_Scope;
759 while Present (Gen) and then Ekind (Gen) /= E_Generic_Package loop
760 Gen := Scope (Gen);
761 end loop;
762
763 if No (Gen) then
764 return;
765 end if;
766
767 if Is_Generic_Formal (Typ) and then Is_Discrete_Type (Typ) then
768 Set_No_Predicate_On_Actual (Typ);
769 end if;
770
771 elsif Has_Predicates (Typ) then
772 if Is_Generic_Actual_Type (Typ) then
773
774 -- The restriction on loop parameters is only that the type
775 -- should have no dynamic predicates.
776
777 if Nkind (Parent (N)) = N_Loop_Parameter_Specification
778 and then not Has_Dynamic_Predicate_Aspect (Typ)
779 and then Is_OK_Static_Subtype (Typ)
780 then
781 return;
782 end if;
783
784 Gen := Current_Scope;
785 while not Is_Generic_Instance (Gen) loop
786 Gen := Scope (Gen);
787 end loop;
788
789 pragma Assert (Present (Gen));
790
791 if Ekind (Gen) = E_Package and then In_Package_Body (Gen) then
792 Error_Msg_Warn := SPARK_Mode /= On;
793 Error_Msg_FE (Msg & "<<", N, Typ);
794 Error_Msg_F ("\Program_Error [<<", N);
795
796 Insert_Action (N,
797 Make_Raise_Program_Error (Sloc (N),
798 Reason => PE_Bad_Predicated_Generic_Type));
799
800 else
801 Error_Msg_FE (Msg & "<<", N, Typ);
802 end if;
803
804 else
805 Error_Msg_FE (Msg, N, Typ);
806 end if;
807
808 -- Emit an optional suggestion on how to remedy the error if the
809 -- context warrants it.
810
811 if Suggest_Static and then Has_Static_Predicate (Typ) then
812 Error_Msg_FE ("\predicate of & should be marked static", N, Typ);
813 end if;
814 end if;
815 end Bad_Predicated_Subtype_Use;
816
817 -----------------------------------------
818 -- Bad_Unordered_Enumeration_Reference --
819 -----------------------------------------
820
821 function Bad_Unordered_Enumeration_Reference
822 (N : Node_Id;
823 T : Entity_Id) return Boolean
824 is
825 begin
826 return Is_Enumeration_Type (T)
827 and then Warn_On_Unordered_Enumeration_Type
828 and then not Is_Generic_Type (T)
829 and then Comes_From_Source (N)
830 and then not Has_Pragma_Ordered (T)
831 and then not In_Same_Extended_Unit (N, T);
832 end Bad_Unordered_Enumeration_Reference;
833
834 --------------------------
835 -- Build_Actual_Subtype --
836 --------------------------
837
838 function Build_Actual_Subtype
839 (T : Entity_Id;
840 N : Node_Or_Entity_Id) return Node_Id
841 is
842 Loc : Source_Ptr;
843 -- Normally Sloc (N), but may point to corresponding body in some cases
844
845 Constraints : List_Id;
846 Decl : Node_Id;
847 Discr : Entity_Id;
848 Hi : Node_Id;
849 Lo : Node_Id;
850 Subt : Entity_Id;
851 Disc_Type : Entity_Id;
852 Obj : Node_Id;
853
854 begin
855 Loc := Sloc (N);
856
857 if Nkind (N) = N_Defining_Identifier then
858 Obj := New_Occurrence_Of (N, Loc);
859
860 -- If this is a formal parameter of a subprogram declaration, and
861 -- we are compiling the body, we want the declaration for the
862 -- actual subtype to carry the source position of the body, to
863 -- prevent anomalies in gdb when stepping through the code.
864
865 if Is_Formal (N) then
866 declare
867 Decl : constant Node_Id := Unit_Declaration_Node (Scope (N));
868 begin
869 if Nkind (Decl) = N_Subprogram_Declaration
870 and then Present (Corresponding_Body (Decl))
871 then
872 Loc := Sloc (Corresponding_Body (Decl));
873 end if;
874 end;
875 end if;
876
877 else
878 Obj := N;
879 end if;
880
881 if Is_Array_Type (T) then
882 Constraints := New_List;
883 for J in 1 .. Number_Dimensions (T) loop
884
885 -- Build an array subtype declaration with the nominal subtype and
886 -- the bounds of the actual. Add the declaration in front of the
887 -- local declarations for the subprogram, for analysis before any
888 -- reference to the formal in the body.
889
890 Lo :=
891 Make_Attribute_Reference (Loc,
892 Prefix =>
893 Duplicate_Subexpr_No_Checks (Obj, Name_Req => True),
894 Attribute_Name => Name_First,
895 Expressions => New_List (
896 Make_Integer_Literal (Loc, J)));
897
898 Hi :=
899 Make_Attribute_Reference (Loc,
900 Prefix =>
901 Duplicate_Subexpr_No_Checks (Obj, Name_Req => True),
902 Attribute_Name => Name_Last,
903 Expressions => New_List (
904 Make_Integer_Literal (Loc, J)));
905
906 Append (Make_Range (Loc, Lo, Hi), Constraints);
907 end loop;
908
909 -- If the type has unknown discriminants there is no constrained
910 -- subtype to build. This is never called for a formal or for a
911 -- lhs, so returning the type is ok ???
912
913 elsif Has_Unknown_Discriminants (T) then
914 return T;
915
916 else
917 Constraints := New_List;
918
919 -- Type T is a generic derived type, inherit the discriminants from
920 -- the parent type.
921
922 if Is_Private_Type (T)
923 and then No (Full_View (T))
924
925 -- T was flagged as an error if it was declared as a formal
926 -- derived type with known discriminants. In this case there
927 -- is no need to look at the parent type since T already carries
928 -- its own discriminants.
929
930 and then not Error_Posted (T)
931 then
932 Disc_Type := Etype (Base_Type (T));
933 else
934 Disc_Type := T;
935 end if;
936
937 Discr := First_Discriminant (Disc_Type);
938 while Present (Discr) loop
939 Append_To (Constraints,
940 Make_Selected_Component (Loc,
941 Prefix =>
942 Duplicate_Subexpr_No_Checks (Obj),
943 Selector_Name => New_Occurrence_Of (Discr, Loc)));
944 Next_Discriminant (Discr);
945 end loop;
946 end if;
947
948 Subt := Make_Temporary (Loc, 'S', Related_Node => N);
949 Set_Is_Internal (Subt);
950
951 Decl :=
952 Make_Subtype_Declaration (Loc,
953 Defining_Identifier => Subt,
954 Subtype_Indication =>
955 Make_Subtype_Indication (Loc,
956 Subtype_Mark => New_Occurrence_Of (T, Loc),
957 Constraint =>
958 Make_Index_Or_Discriminant_Constraint (Loc,
959 Constraints => Constraints)));
960
961 Mark_Rewrite_Insertion (Decl);
962 return Decl;
963 end Build_Actual_Subtype;
964
965 ---------------------------------------
966 -- Build_Actual_Subtype_Of_Component --
967 ---------------------------------------
968
969 function Build_Actual_Subtype_Of_Component
970 (T : Entity_Id;
971 N : Node_Id) return Node_Id
972 is
973 Loc : constant Source_Ptr := Sloc (N);
974 P : constant Node_Id := Prefix (N);
975 D : Elmt_Id;
976 Id : Node_Id;
977 Index_Typ : Entity_Id;
978
979 Desig_Typ : Entity_Id;
980 -- This is either a copy of T, or if T is an access type, then it is
981 -- the directly designated type of this access type.
982
983 function Build_Actual_Array_Constraint return List_Id;
984 -- If one or more of the bounds of the component depends on
985 -- discriminants, build actual constraint using the discriminants
986 -- of the prefix.
987
988 function Build_Actual_Record_Constraint return List_Id;
989 -- Similar to previous one, for discriminated components constrained
990 -- by the discriminant of the enclosing object.
991
992 -----------------------------------
993 -- Build_Actual_Array_Constraint --
994 -----------------------------------
995
996 function Build_Actual_Array_Constraint return List_Id is
997 Constraints : constant List_Id := New_List;
998 Indx : Node_Id;
999 Hi : Node_Id;
1000 Lo : Node_Id;
1001 Old_Hi : Node_Id;
1002 Old_Lo : Node_Id;
1003
1004 begin
1005 Indx := First_Index (Desig_Typ);
1006 while Present (Indx) loop
1007 Old_Lo := Type_Low_Bound (Etype (Indx));
1008 Old_Hi := Type_High_Bound (Etype (Indx));
1009
1010 if Denotes_Discriminant (Old_Lo) then
1011 Lo :=
1012 Make_Selected_Component (Loc,
1013 Prefix => New_Copy_Tree (P),
1014 Selector_Name => New_Occurrence_Of (Entity (Old_Lo), Loc));
1015
1016 else
1017 Lo := New_Copy_Tree (Old_Lo);
1018
1019 -- The new bound will be reanalyzed in the enclosing
1020 -- declaration. For literal bounds that come from a type
1021 -- declaration, the type of the context must be imposed, so
1022 -- insure that analysis will take place. For non-universal
1023 -- types this is not strictly necessary.
1024
1025 Set_Analyzed (Lo, False);
1026 end if;
1027
1028 if Denotes_Discriminant (Old_Hi) then
1029 Hi :=
1030 Make_Selected_Component (Loc,
1031 Prefix => New_Copy_Tree (P),
1032 Selector_Name => New_Occurrence_Of (Entity (Old_Hi), Loc));
1033
1034 else
1035 Hi := New_Copy_Tree (Old_Hi);
1036 Set_Analyzed (Hi, False);
1037 end if;
1038
1039 Append (Make_Range (Loc, Lo, Hi), Constraints);
1040 Next_Index (Indx);
1041 end loop;
1042
1043 return Constraints;
1044 end Build_Actual_Array_Constraint;
1045
1046 ------------------------------------
1047 -- Build_Actual_Record_Constraint --
1048 ------------------------------------
1049
1050 function Build_Actual_Record_Constraint return List_Id is
1051 Constraints : constant List_Id := New_List;
1052 D : Elmt_Id;
1053 D_Val : Node_Id;
1054
1055 begin
1056 D := First_Elmt (Discriminant_Constraint (Desig_Typ));
1057 while Present (D) loop
1058 if Denotes_Discriminant (Node (D)) then
1059 D_Val := Make_Selected_Component (Loc,
1060 Prefix => New_Copy_Tree (P),
1061 Selector_Name => New_Occurrence_Of (Entity (Node (D)), Loc));
1062
1063 else
1064 D_Val := New_Copy_Tree (Node (D));
1065 end if;
1066
1067 Append (D_Val, Constraints);
1068 Next_Elmt (D);
1069 end loop;
1070
1071 return Constraints;
1072 end Build_Actual_Record_Constraint;
1073
1074 -- Start of processing for Build_Actual_Subtype_Of_Component
1075
1076 begin
1077 -- Why the test for Spec_Expression mode here???
1078
1079 if In_Spec_Expression then
1080 return Empty;
1081
1082 -- More comments for the rest of this body would be good ???
1083
1084 elsif Nkind (N) = N_Explicit_Dereference then
1085 if Is_Composite_Type (T)
1086 and then not Is_Constrained (T)
1087 and then not (Is_Class_Wide_Type (T)
1088 and then Is_Constrained (Root_Type (T)))
1089 and then not Has_Unknown_Discriminants (T)
1090 then
1091 -- If the type of the dereference is already constrained, it is an
1092 -- actual subtype.
1093
1094 if Is_Array_Type (Etype (N))
1095 and then Is_Constrained (Etype (N))
1096 then
1097 return Empty;
1098 else
1099 Remove_Side_Effects (P);
1100 return Build_Actual_Subtype (T, N);
1101 end if;
1102 else
1103 return Empty;
1104 end if;
1105 end if;
1106
1107 if Ekind (T) = E_Access_Subtype then
1108 Desig_Typ := Designated_Type (T);
1109 else
1110 Desig_Typ := T;
1111 end if;
1112
1113 if Ekind (Desig_Typ) = E_Array_Subtype then
1114 Id := First_Index (Desig_Typ);
1115 while Present (Id) loop
1116 Index_Typ := Underlying_Type (Etype (Id));
1117
1118 if Denotes_Discriminant (Type_Low_Bound (Index_Typ))
1119 or else
1120 Denotes_Discriminant (Type_High_Bound (Index_Typ))
1121 then
1122 Remove_Side_Effects (P);
1123 return
1124 Build_Component_Subtype
1125 (Build_Actual_Array_Constraint, Loc, Base_Type (T));
1126 end if;
1127
1128 Next_Index (Id);
1129 end loop;
1130
1131 elsif Is_Composite_Type (Desig_Typ)
1132 and then Has_Discriminants (Desig_Typ)
1133 and then not Has_Unknown_Discriminants (Desig_Typ)
1134 then
1135 if Is_Private_Type (Desig_Typ)
1136 and then No (Discriminant_Constraint (Desig_Typ))
1137 then
1138 Desig_Typ := Full_View (Desig_Typ);
1139 end if;
1140
1141 D := First_Elmt (Discriminant_Constraint (Desig_Typ));
1142 while Present (D) loop
1143 if Denotes_Discriminant (Node (D)) then
1144 Remove_Side_Effects (P);
1145 return
1146 Build_Component_Subtype (
1147 Build_Actual_Record_Constraint, Loc, Base_Type (T));
1148 end if;
1149
1150 Next_Elmt (D);
1151 end loop;
1152 end if;
1153
1154 -- If none of the above, the actual and nominal subtypes are the same
1155
1156 return Empty;
1157 end Build_Actual_Subtype_Of_Component;
1158
1159 -----------------------------
1160 -- Build_Component_Subtype --
1161 -----------------------------
1162
1163 function Build_Component_Subtype
1164 (C : List_Id;
1165 Loc : Source_Ptr;
1166 T : Entity_Id) return Node_Id
1167 is
1168 Subt : Entity_Id;
1169 Decl : Node_Id;
1170
1171 begin
1172 -- Unchecked_Union components do not require component subtypes
1173
1174 if Is_Unchecked_Union (T) then
1175 return Empty;
1176 end if;
1177
1178 Subt := Make_Temporary (Loc, 'S');
1179 Set_Is_Internal (Subt);
1180
1181 Decl :=
1182 Make_Subtype_Declaration (Loc,
1183 Defining_Identifier => Subt,
1184 Subtype_Indication =>
1185 Make_Subtype_Indication (Loc,
1186 Subtype_Mark => New_Occurrence_Of (Base_Type (T), Loc),
1187 Constraint =>
1188 Make_Index_Or_Discriminant_Constraint (Loc,
1189 Constraints => C)));
1190
1191 Mark_Rewrite_Insertion (Decl);
1192 return Decl;
1193 end Build_Component_Subtype;
1194
1195 ---------------------------
1196 -- Build_Default_Subtype --
1197 ---------------------------
1198
1199 function Build_Default_Subtype
1200 (T : Entity_Id;
1201 N : Node_Id) return Entity_Id
1202 is
1203 Loc : constant Source_Ptr := Sloc (N);
1204 Disc : Entity_Id;
1205
1206 Bas : Entity_Id;
1207 -- The base type that is to be constrained by the defaults
1208
1209 begin
1210 if not Has_Discriminants (T) or else Is_Constrained (T) then
1211 return T;
1212 end if;
1213
1214 Bas := Base_Type (T);
1215
1216 -- If T is non-private but its base type is private, this is the
1217 -- completion of a subtype declaration whose parent type is private
1218 -- (see Complete_Private_Subtype in Sem_Ch3). The proper discriminants
1219 -- are to be found in the full view of the base. Check that the private
1220 -- status of T and its base differ.
1221
1222 if Is_Private_Type (Bas)
1223 and then not Is_Private_Type (T)
1224 and then Present (Full_View (Bas))
1225 then
1226 Bas := Full_View (Bas);
1227 end if;
1228
1229 Disc := First_Discriminant (T);
1230
1231 if No (Discriminant_Default_Value (Disc)) then
1232 return T;
1233 end if;
1234
1235 declare
1236 Act : constant Entity_Id := Make_Temporary (Loc, 'S');
1237 Constraints : constant List_Id := New_List;
1238 Decl : Node_Id;
1239
1240 begin
1241 while Present (Disc) loop
1242 Append_To (Constraints,
1243 New_Copy_Tree (Discriminant_Default_Value (Disc)));
1244 Next_Discriminant (Disc);
1245 end loop;
1246
1247 Decl :=
1248 Make_Subtype_Declaration (Loc,
1249 Defining_Identifier => Act,
1250 Subtype_Indication =>
1251 Make_Subtype_Indication (Loc,
1252 Subtype_Mark => New_Occurrence_Of (Bas, Loc),
1253 Constraint =>
1254 Make_Index_Or_Discriminant_Constraint (Loc,
1255 Constraints => Constraints)));
1256
1257 Insert_Action (N, Decl);
1258
1259 -- If the context is a component declaration the subtype declaration
1260 -- will be analyzed when the enclosing type is frozen, otherwise do
1261 -- it now.
1262
1263 if Ekind (Current_Scope) /= E_Record_Type then
1264 Analyze (Decl);
1265 end if;
1266
1267 return Act;
1268 end;
1269 end Build_Default_Subtype;
1270
1271 --------------------------------------------
1272 -- Build_Discriminal_Subtype_Of_Component --
1273 --------------------------------------------
1274
1275 function Build_Discriminal_Subtype_Of_Component
1276 (T : Entity_Id) return Node_Id
1277 is
1278 Loc : constant Source_Ptr := Sloc (T);
1279 D : Elmt_Id;
1280 Id : Node_Id;
1281
1282 function Build_Discriminal_Array_Constraint return List_Id;
1283 -- If one or more of the bounds of the component depends on
1284 -- discriminants, build actual constraint using the discriminants
1285 -- of the prefix.
1286
1287 function Build_Discriminal_Record_Constraint return List_Id;
1288 -- Similar to previous one, for discriminated components constrained by
1289 -- the discriminant of the enclosing object.
1290
1291 ----------------------------------------
1292 -- Build_Discriminal_Array_Constraint --
1293 ----------------------------------------
1294
1295 function Build_Discriminal_Array_Constraint return List_Id is
1296 Constraints : constant List_Id := New_List;
1297 Indx : Node_Id;
1298 Hi : Node_Id;
1299 Lo : Node_Id;
1300 Old_Hi : Node_Id;
1301 Old_Lo : Node_Id;
1302
1303 begin
1304 Indx := First_Index (T);
1305 while Present (Indx) loop
1306 Old_Lo := Type_Low_Bound (Etype (Indx));
1307 Old_Hi := Type_High_Bound (Etype (Indx));
1308
1309 if Denotes_Discriminant (Old_Lo) then
1310 Lo := New_Occurrence_Of (Discriminal (Entity (Old_Lo)), Loc);
1311
1312 else
1313 Lo := New_Copy_Tree (Old_Lo);
1314 end if;
1315
1316 if Denotes_Discriminant (Old_Hi) then
1317 Hi := New_Occurrence_Of (Discriminal (Entity (Old_Hi)), Loc);
1318
1319 else
1320 Hi := New_Copy_Tree (Old_Hi);
1321 end if;
1322
1323 Append (Make_Range (Loc, Lo, Hi), Constraints);
1324 Next_Index (Indx);
1325 end loop;
1326
1327 return Constraints;
1328 end Build_Discriminal_Array_Constraint;
1329
1330 -----------------------------------------
1331 -- Build_Discriminal_Record_Constraint --
1332 -----------------------------------------
1333
1334 function Build_Discriminal_Record_Constraint return List_Id is
1335 Constraints : constant List_Id := New_List;
1336 D : Elmt_Id;
1337 D_Val : Node_Id;
1338
1339 begin
1340 D := First_Elmt (Discriminant_Constraint (T));
1341 while Present (D) loop
1342 if Denotes_Discriminant (Node (D)) then
1343 D_Val :=
1344 New_Occurrence_Of (Discriminal (Entity (Node (D))), Loc);
1345 else
1346 D_Val := New_Copy_Tree (Node (D));
1347 end if;
1348
1349 Append (D_Val, Constraints);
1350 Next_Elmt (D);
1351 end loop;
1352
1353 return Constraints;
1354 end Build_Discriminal_Record_Constraint;
1355
1356 -- Start of processing for Build_Discriminal_Subtype_Of_Component
1357
1358 begin
1359 if Ekind (T) = E_Array_Subtype then
1360 Id := First_Index (T);
1361 while Present (Id) loop
1362 if Denotes_Discriminant (Type_Low_Bound (Etype (Id)))
1363 or else
1364 Denotes_Discriminant (Type_High_Bound (Etype (Id)))
1365 then
1366 return Build_Component_Subtype
1367 (Build_Discriminal_Array_Constraint, Loc, T);
1368 end if;
1369
1370 Next_Index (Id);
1371 end loop;
1372
1373 elsif Ekind (T) = E_Record_Subtype
1374 and then Has_Discriminants (T)
1375 and then not Has_Unknown_Discriminants (T)
1376 then
1377 D := First_Elmt (Discriminant_Constraint (T));
1378 while Present (D) loop
1379 if Denotes_Discriminant (Node (D)) then
1380 return Build_Component_Subtype
1381 (Build_Discriminal_Record_Constraint, Loc, T);
1382 end if;
1383
1384 Next_Elmt (D);
1385 end loop;
1386 end if;
1387
1388 -- If none of the above, the actual and nominal subtypes are the same
1389
1390 return Empty;
1391 end Build_Discriminal_Subtype_Of_Component;
1392
1393 ------------------------------
1394 -- Build_Elaboration_Entity --
1395 ------------------------------
1396
1397 procedure Build_Elaboration_Entity (N : Node_Id; Spec_Id : Entity_Id) is
1398 Loc : constant Source_Ptr := Sloc (N);
1399 Decl : Node_Id;
1400 Elab_Ent : Entity_Id;
1401
1402 procedure Set_Package_Name (Ent : Entity_Id);
1403 -- Given an entity, sets the fully qualified name of the entity in
1404 -- Name_Buffer, with components separated by double underscores. This
1405 -- is a recursive routine that climbs the scope chain to Standard.
1406
1407 ----------------------
1408 -- Set_Package_Name --
1409 ----------------------
1410
1411 procedure Set_Package_Name (Ent : Entity_Id) is
1412 begin
1413 if Scope (Ent) /= Standard_Standard then
1414 Set_Package_Name (Scope (Ent));
1415
1416 declare
1417 Nam : constant String := Get_Name_String (Chars (Ent));
1418 begin
1419 Name_Buffer (Name_Len + 1) := '_';
1420 Name_Buffer (Name_Len + 2) := '_';
1421 Name_Buffer (Name_Len + 3 .. Name_Len + Nam'Length + 2) := Nam;
1422 Name_Len := Name_Len + Nam'Length + 2;
1423 end;
1424
1425 else
1426 Get_Name_String (Chars (Ent));
1427 end if;
1428 end Set_Package_Name;
1429
1430 -- Start of processing for Build_Elaboration_Entity
1431
1432 begin
1433 -- Ignore call if already constructed
1434
1435 if Present (Elaboration_Entity (Spec_Id)) then
1436 return;
1437
1438 -- Ignore in ASIS mode, elaboration entity is not in source and plays
1439 -- no role in analysis.
1440
1441 elsif ASIS_Mode then
1442 return;
1443
1444 -- See if we need elaboration entity.
1445
1446 -- We always need an elaboration entity when preserving control flow, as
1447 -- we want to remain explicit about the unit's elaboration order.
1448
1449 elsif Opt.Suppress_Control_Flow_Optimizations then
1450 null;
1451
1452 -- We always need an elaboration entity for the dynamic elaboration
1453 -- model, since it is needed to properly generate the PE exception for
1454 -- access before elaboration.
1455
1456 elsif Dynamic_Elaboration_Checks then
1457 null;
1458
1459 -- For the static model, we don't need the elaboration counter if this
1460 -- unit is sure to have no elaboration code, since that means there
1461 -- is no elaboration unit to be called. Note that we can't just decide
1462 -- after the fact by looking to see whether there was elaboration code,
1463 -- because that's too late to make this decision.
1464
1465 elsif Restriction_Active (No_Elaboration_Code) then
1466 return;
1467
1468 -- Similarly, for the static model, we can skip the elaboration counter
1469 -- if we have the No_Multiple_Elaboration restriction, since for the
1470 -- static model, that's the only purpose of the counter (to avoid
1471 -- multiple elaboration).
1472
1473 elsif Restriction_Active (No_Multiple_Elaboration) then
1474 return;
1475 end if;
1476
1477 -- Here we need the elaboration entity
1478
1479 -- Construct name of elaboration entity as xxx_E, where xxx is the unit
1480 -- name with dots replaced by double underscore. We have to manually
1481 -- construct this name, since it will be elaborated in the outer scope,
1482 -- and thus will not have the unit name automatically prepended.
1483
1484 Set_Package_Name (Spec_Id);
1485 Add_Str_To_Name_Buffer ("_E");
1486
1487 -- Create elaboration counter
1488
1489 Elab_Ent := Make_Defining_Identifier (Loc, Chars => Name_Find);
1490 Set_Elaboration_Entity (Spec_Id, Elab_Ent);
1491
1492 Decl :=
1493 Make_Object_Declaration (Loc,
1494 Defining_Identifier => Elab_Ent,
1495 Object_Definition =>
1496 New_Occurrence_Of (Standard_Short_Integer, Loc),
1497 Expression => Make_Integer_Literal (Loc, Uint_0));
1498
1499 Push_Scope (Standard_Standard);
1500 Add_Global_Declaration (Decl);
1501 Pop_Scope;
1502
1503 -- Reset True_Constant indication, since we will indeed assign a value
1504 -- to the variable in the binder main. We also kill the Current_Value
1505 -- and Last_Assignment fields for the same reason.
1506
1507 Set_Is_True_Constant (Elab_Ent, False);
1508 Set_Current_Value (Elab_Ent, Empty);
1509 Set_Last_Assignment (Elab_Ent, Empty);
1510
1511 -- We do not want any further qualification of the name (if we did not
1512 -- do this, we would pick up the name of the generic package in the case
1513 -- of a library level generic instantiation).
1514
1515 Set_Has_Qualified_Name (Elab_Ent);
1516 Set_Has_Fully_Qualified_Name (Elab_Ent);
1517 end Build_Elaboration_Entity;
1518
1519 --------------------------------
1520 -- Build_Explicit_Dereference --
1521 --------------------------------
1522
1523 procedure Build_Explicit_Dereference
1524 (Expr : Node_Id;
1525 Disc : Entity_Id)
1526 is
1527 Loc : constant Source_Ptr := Sloc (Expr);
1528 I : Interp_Index;
1529 It : Interp;
1530
1531 begin
1532 -- An entity of a type with a reference aspect is overloaded with
1533 -- both interpretations: with and without the dereference. Now that
1534 -- the dereference is made explicit, set the type of the node properly,
1535 -- to prevent anomalies in the backend. Same if the expression is an
1536 -- overloaded function call whose return type has a reference aspect.
1537
1538 if Is_Entity_Name (Expr) then
1539 Set_Etype (Expr, Etype (Entity (Expr)));
1540
1541 -- The designated entity will not be examined again when resolving
1542 -- the dereference, so generate a reference to it now.
1543
1544 Generate_Reference (Entity (Expr), Expr);
1545
1546 elsif Nkind (Expr) = N_Function_Call then
1547
1548 -- If the name of the indexing function is overloaded, locate the one
1549 -- whose return type has an implicit dereference on the desired
1550 -- discriminant, and set entity and type of function call.
1551
1552 if Is_Overloaded (Name (Expr)) then
1553 Get_First_Interp (Name (Expr), I, It);
1554
1555 while Present (It.Nam) loop
1556 if Ekind ((It.Typ)) = E_Record_Type
1557 and then First_Entity ((It.Typ)) = Disc
1558 then
1559 Set_Entity (Name (Expr), It.Nam);
1560 Set_Etype (Name (Expr), Etype (It.Nam));
1561 exit;
1562 end if;
1563
1564 Get_Next_Interp (I, It);
1565 end loop;
1566 end if;
1567
1568 -- Set type of call from resolved function name.
1569
1570 Set_Etype (Expr, Etype (Name (Expr)));
1571 end if;
1572
1573 Set_Is_Overloaded (Expr, False);
1574
1575 -- The expression will often be a generalized indexing that yields a
1576 -- container element that is then dereferenced, in which case the
1577 -- generalized indexing call is also non-overloaded.
1578
1579 if Nkind (Expr) = N_Indexed_Component
1580 and then Present (Generalized_Indexing (Expr))
1581 then
1582 Set_Is_Overloaded (Generalized_Indexing (Expr), False);
1583 end if;
1584
1585 Rewrite (Expr,
1586 Make_Explicit_Dereference (Loc,
1587 Prefix =>
1588 Make_Selected_Component (Loc,
1589 Prefix => Relocate_Node (Expr),
1590 Selector_Name => New_Occurrence_Of (Disc, Loc))));
1591 Set_Etype (Prefix (Expr), Etype (Disc));
1592 Set_Etype (Expr, Designated_Type (Etype (Disc)));
1593 end Build_Explicit_Dereference;
1594
1595 -----------------------------------
1596 -- Cannot_Raise_Constraint_Error --
1597 -----------------------------------
1598
1599 function Cannot_Raise_Constraint_Error (Expr : Node_Id) return Boolean is
1600 begin
1601 if Compile_Time_Known_Value (Expr) then
1602 return True;
1603
1604 elsif Do_Range_Check (Expr) then
1605 return False;
1606
1607 elsif Raises_Constraint_Error (Expr) then
1608 return False;
1609
1610 else
1611 case Nkind (Expr) is
1612 when N_Identifier =>
1613 return True;
1614
1615 when N_Expanded_Name =>
1616 return True;
1617
1618 when N_Selected_Component =>
1619 return not Do_Discriminant_Check (Expr);
1620
1621 when N_Attribute_Reference =>
1622 if Do_Overflow_Check (Expr) then
1623 return False;
1624
1625 elsif No (Expressions (Expr)) then
1626 return True;
1627
1628 else
1629 declare
1630 N : Node_Id;
1631
1632 begin
1633 N := First (Expressions (Expr));
1634 while Present (N) loop
1635 if Cannot_Raise_Constraint_Error (N) then
1636 Next (N);
1637 else
1638 return False;
1639 end if;
1640 end loop;
1641
1642 return True;
1643 end;
1644 end if;
1645
1646 when N_Type_Conversion =>
1647 if Do_Overflow_Check (Expr)
1648 or else Do_Length_Check (Expr)
1649 or else Do_Tag_Check (Expr)
1650 then
1651 return False;
1652 else
1653 return Cannot_Raise_Constraint_Error (Expression (Expr));
1654 end if;
1655
1656 when N_Unchecked_Type_Conversion =>
1657 return Cannot_Raise_Constraint_Error (Expression (Expr));
1658
1659 when N_Unary_Op =>
1660 if Do_Overflow_Check (Expr) then
1661 return False;
1662 else
1663 return Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
1664 end if;
1665
1666 when N_Op_Divide |
1667 N_Op_Mod |
1668 N_Op_Rem
1669 =>
1670 if Do_Division_Check (Expr)
1671 or else
1672 Do_Overflow_Check (Expr)
1673 then
1674 return False;
1675 else
1676 return
1677 Cannot_Raise_Constraint_Error (Left_Opnd (Expr))
1678 and then
1679 Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
1680 end if;
1681
1682 when N_Op_Add |
1683 N_Op_And |
1684 N_Op_Concat |
1685 N_Op_Eq |
1686 N_Op_Expon |
1687 N_Op_Ge |
1688 N_Op_Gt |
1689 N_Op_Le |
1690 N_Op_Lt |
1691 N_Op_Multiply |
1692 N_Op_Ne |
1693 N_Op_Or |
1694 N_Op_Rotate_Left |
1695 N_Op_Rotate_Right |
1696 N_Op_Shift_Left |
1697 N_Op_Shift_Right |
1698 N_Op_Shift_Right_Arithmetic |
1699 N_Op_Subtract |
1700 N_Op_Xor
1701 =>
1702 if Do_Overflow_Check (Expr) then
1703 return False;
1704 else
1705 return
1706 Cannot_Raise_Constraint_Error (Left_Opnd (Expr))
1707 and then
1708 Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
1709 end if;
1710
1711 when others =>
1712 return False;
1713 end case;
1714 end if;
1715 end Cannot_Raise_Constraint_Error;
1716
1717 -----------------------------
1718 -- Check_Part_Of_Reference --
1719 -----------------------------
1720
1721 procedure Check_Part_Of_Reference (Var_Id : Entity_Id; Ref : Node_Id) is
1722 Conc_Typ : constant Entity_Id := Encapsulating_State (Var_Id);
1723 Decl : Node_Id;
1724 OK_Use : Boolean := False;
1725 Par : Node_Id;
1726 Prag_Nam : Name_Id;
1727 Spec_Id : Entity_Id;
1728
1729 begin
1730 -- Traverse the parent chain looking for a suitable context for the
1731 -- reference to the concurrent constituent.
1732
1733 Par := Parent (Ref);
1734 while Present (Par) loop
1735 if Nkind (Par) = N_Pragma then
1736 Prag_Nam := Pragma_Name (Par);
1737
1738 -- A concurrent constituent is allowed to appear in pragmas
1739 -- Initial_Condition and Initializes as this is part of the
1740 -- elaboration checks for the constituent (SPARK RM 9.3).
1741
1742 if Nam_In (Prag_Nam, Name_Initial_Condition, Name_Initializes) then
1743 OK_Use := True;
1744 exit;
1745
1746 -- When the reference appears within pragma Depends or Global,
1747 -- check whether the pragma applies to a single task type. Note
1748 -- that the pragma is not encapsulated by the type definition,
1749 -- but this is still a valid context.
1750
1751 elsif Nam_In (Prag_Nam, Name_Depends, Name_Global) then
1752 Decl := Find_Related_Declaration_Or_Body (Par);
1753
1754 if Nkind (Decl) = N_Object_Declaration
1755 and then Defining_Entity (Decl) = Conc_Typ
1756 then
1757 OK_Use := True;
1758 exit;
1759 end if;
1760 end if;
1761
1762 -- The reference appears somewhere in the definition of the single
1763 -- protected/task type (SPARK RM 9.3).
1764
1765 elsif Nkind_In (Par, N_Single_Protected_Declaration,
1766 N_Single_Task_Declaration)
1767 and then Defining_Entity (Par) = Conc_Typ
1768 then
1769 OK_Use := True;
1770 exit;
1771
1772 -- The reference appears within the expanded declaration or the body
1773 -- of the single protected/task type (SPARK RM 9.3).
1774
1775 elsif Nkind_In (Par, N_Protected_Body,
1776 N_Protected_Type_Declaration,
1777 N_Task_Body,
1778 N_Task_Type_Declaration)
1779 then
1780 Spec_Id := Unique_Defining_Entity (Par);
1781
1782 if Present (Anonymous_Object (Spec_Id))
1783 and then Anonymous_Object (Spec_Id) = Conc_Typ
1784 then
1785 OK_Use := True;
1786 exit;
1787 end if;
1788
1789 -- The reference has been relocated within an internally generated
1790 -- package or subprogram. Assume that the reference is legal as the
1791 -- real check was already performed in the original context of the
1792 -- reference.
1793
1794 elsif Nkind_In (Par, N_Package_Body,
1795 N_Package_Declaration,
1796 N_Subprogram_Body,
1797 N_Subprogram_Declaration)
1798 and then not Comes_From_Source (Par)
1799 then
1800 OK_Use := True;
1801 exit;
1802
1803 -- The reference has been relocated to an inlined body for GNATprove.
1804 -- Assume that the reference is legal as the real check was already
1805 -- performed in the original context of the reference.
1806
1807 elsif GNATprove_Mode
1808 and then Nkind (Par) = N_Subprogram_Body
1809 and then Chars (Defining_Entity (Par)) = Name_uParent
1810 then
1811 OK_Use := True;
1812 exit;
1813 end if;
1814
1815 Par := Parent (Par);
1816 end loop;
1817
1818 -- The reference is illegal as it appears outside the definition or
1819 -- body of the single protected/task type.
1820
1821 if not OK_Use then
1822 Error_Msg_NE
1823 ("reference to variable & cannot appear in this context",
1824 Ref, Var_Id);
1825 Error_Msg_Name_1 := Chars (Var_Id);
1826
1827 if Ekind (Conc_Typ) = E_Protected_Type then
1828 Error_Msg_NE
1829 ("\% is constituent of single protected type &", Ref, Conc_Typ);
1830 else
1831 Error_Msg_NE
1832 ("\% is constituent of single task type &", Ref, Conc_Typ);
1833 end if;
1834 end if;
1835 end Check_Part_Of_Reference;
1836
1837 -----------------------------------------
1838 -- Check_Dynamically_Tagged_Expression --
1839 -----------------------------------------
1840
1841 procedure Check_Dynamically_Tagged_Expression
1842 (Expr : Node_Id;
1843 Typ : Entity_Id;
1844 Related_Nod : Node_Id)
1845 is
1846 begin
1847 pragma Assert (Is_Tagged_Type (Typ));
1848
1849 -- In order to avoid spurious errors when analyzing the expanded code,
1850 -- this check is done only for nodes that come from source and for
1851 -- actuals of generic instantiations.
1852
1853 if (Comes_From_Source (Related_Nod)
1854 or else In_Generic_Actual (Expr))
1855 and then (Is_Class_Wide_Type (Etype (Expr))
1856 or else Is_Dynamically_Tagged (Expr))
1857 and then Is_Tagged_Type (Typ)
1858 and then not Is_Class_Wide_Type (Typ)
1859 then
1860 Error_Msg_N ("dynamically tagged expression not allowed!", Expr);
1861 end if;
1862 end Check_Dynamically_Tagged_Expression;
1863
1864 --------------------------
1865 -- Check_Fully_Declared --
1866 --------------------------
1867
1868 procedure Check_Fully_Declared (T : Entity_Id; N : Node_Id) is
1869 begin
1870 if Ekind (T) = E_Incomplete_Type then
1871
1872 -- Ada 2005 (AI-50217): If the type is available through a limited
1873 -- with_clause, verify that its full view has been analyzed.
1874
1875 if From_Limited_With (T)
1876 and then Present (Non_Limited_View (T))
1877 and then Ekind (Non_Limited_View (T)) /= E_Incomplete_Type
1878 then
1879 -- The non-limited view is fully declared
1880
1881 null;
1882
1883 else
1884 Error_Msg_NE
1885 ("premature usage of incomplete}", N, First_Subtype (T));
1886 end if;
1887
1888 -- Need comments for these tests ???
1889
1890 elsif Has_Private_Component (T)
1891 and then not Is_Generic_Type (Root_Type (T))
1892 and then not In_Spec_Expression
1893 then
1894 -- Special case: if T is the anonymous type created for a single
1895 -- task or protected object, use the name of the source object.
1896
1897 if Is_Concurrent_Type (T)
1898 and then not Comes_From_Source (T)
1899 and then Nkind (N) = N_Object_Declaration
1900 then
1901 Error_Msg_NE
1902 ("type of& has incomplete component",
1903 N, Defining_Identifier (N));
1904 else
1905 Error_Msg_NE
1906 ("premature usage of incomplete}",
1907 N, First_Subtype (T));
1908 end if;
1909 end if;
1910 end Check_Fully_Declared;
1911
1912 -------------------------------------------
1913 -- Check_Function_With_Address_Parameter --
1914 -------------------------------------------
1915
1916 procedure Check_Function_With_Address_Parameter (Subp_Id : Entity_Id) is
1917 F : Entity_Id;
1918 T : Entity_Id;
1919
1920 begin
1921 F := First_Formal (Subp_Id);
1922 while Present (F) loop
1923 T := Etype (F);
1924
1925 if Is_Private_Type (T) and then Present (Full_View (T)) then
1926 T := Full_View (T);
1927 end if;
1928
1929 if Is_Descendant_Of_Address (T) or else Is_Limited_Type (T) then
1930 Set_Is_Pure (Subp_Id, False);
1931 exit;
1932 end if;
1933
1934 Next_Formal (F);
1935 end loop;
1936 end Check_Function_With_Address_Parameter;
1937
1938 -------------------------------------
1939 -- Check_Function_Writable_Actuals --
1940 -------------------------------------
1941
1942 procedure Check_Function_Writable_Actuals (N : Node_Id) is
1943 Writable_Actuals_List : Elist_Id := No_Elist;
1944 Identifiers_List : Elist_Id := No_Elist;
1945 Aggr_Error_Node : Node_Id := Empty;
1946 Error_Node : Node_Id := Empty;
1947
1948 procedure Collect_Identifiers (N : Node_Id);
1949 -- In a single traversal of subtree N collect in Writable_Actuals_List
1950 -- all the actuals of functions with writable actuals, and in the list
1951 -- Identifiers_List collect all the identifiers that are not actuals of
1952 -- functions with writable actuals. If a writable actual is referenced
1953 -- twice as writable actual then Error_Node is set to reference its
1954 -- second occurrence, the error is reported, and the tree traversal
1955 -- is abandoned.
1956
1957 function Get_Function_Id (Call : Node_Id) return Entity_Id;
1958 -- Return the entity associated with the function call
1959
1960 procedure Preanalyze_Without_Errors (N : Node_Id);
1961 -- Preanalyze N without reporting errors. Very dubious, you can't just
1962 -- go analyzing things more than once???
1963
1964 -------------------------
1965 -- Collect_Identifiers --
1966 -------------------------
1967
1968 procedure Collect_Identifiers (N : Node_Id) is
1969
1970 function Check_Node (N : Node_Id) return Traverse_Result;
1971 -- Process a single node during the tree traversal to collect the
1972 -- writable actuals of functions and all the identifiers which are
1973 -- not writable actuals of functions.
1974
1975 function Contains (List : Elist_Id; N : Node_Id) return Boolean;
1976 -- Returns True if List has a node whose Entity is Entity (N)
1977
1978 -------------------------
1979 -- Check_Function_Call --
1980 -------------------------
1981
1982 function Check_Node (N : Node_Id) return Traverse_Result is
1983 Is_Writable_Actual : Boolean := False;
1984 Id : Entity_Id;
1985
1986 begin
1987 if Nkind (N) = N_Identifier then
1988
1989 -- No analysis possible if the entity is not decorated
1990
1991 if No (Entity (N)) then
1992 return Skip;
1993
1994 -- Don't collect identifiers of packages, called functions, etc
1995
1996 elsif Ekind_In (Entity (N), E_Package,
1997 E_Function,
1998 E_Procedure,
1999 E_Entry)
2000 then
2001 return Skip;
2002
2003 -- For rewritten nodes, continue the traversal in the original
2004 -- subtree. Needed to handle aggregates in original expressions
2005 -- extracted from the tree by Remove_Side_Effects.
2006
2007 elsif Is_Rewrite_Substitution (N) then
2008 Collect_Identifiers (Original_Node (N));
2009 return Skip;
2010
2011 -- For now we skip aggregate discriminants, since they require
2012 -- performing the analysis in two phases to identify conflicts:
2013 -- first one analyzing discriminants and second one analyzing
2014 -- the rest of components (since at run time, discriminants are
2015 -- evaluated prior to components): too much computation cost
2016 -- to identify a corner case???
2017
2018 elsif Nkind (Parent (N)) = N_Component_Association
2019 and then Nkind_In (Parent (Parent (N)),
2020 N_Aggregate,
2021 N_Extension_Aggregate)
2022 then
2023 declare
2024 Choice : constant Node_Id := First (Choices (Parent (N)));
2025
2026 begin
2027 if Ekind (Entity (N)) = E_Discriminant then
2028 return Skip;
2029
2030 elsif Expression (Parent (N)) = N
2031 and then Nkind (Choice) = N_Identifier
2032 and then Ekind (Entity (Choice)) = E_Discriminant
2033 then
2034 return Skip;
2035 end if;
2036 end;
2037
2038 -- Analyze if N is a writable actual of a function
2039
2040 elsif Nkind (Parent (N)) = N_Function_Call then
2041 declare
2042 Call : constant Node_Id := Parent (N);
2043 Actual : Node_Id;
2044 Formal : Node_Id;
2045
2046 begin
2047 Id := Get_Function_Id (Call);
2048
2049 -- In case of previous error, no check is possible
2050
2051 if No (Id) then
2052 return Abandon;
2053 end if;
2054
2055 if Ekind_In (Id, E_Function, E_Generic_Function)
2056 and then Has_Out_Or_In_Out_Parameter (Id)
2057 then
2058 Formal := First_Formal (Id);
2059 Actual := First_Actual (Call);
2060 while Present (Actual) and then Present (Formal) loop
2061 if Actual = N then
2062 if Ekind_In (Formal, E_Out_Parameter,
2063 E_In_Out_Parameter)
2064 then
2065 Is_Writable_Actual := True;
2066 end if;
2067
2068 exit;
2069 end if;
2070
2071 Next_Formal (Formal);
2072 Next_Actual (Actual);
2073 end loop;
2074 end if;
2075 end;
2076 end if;
2077
2078 if Is_Writable_Actual then
2079
2080 -- Skip checking the error in non-elementary types since
2081 -- RM 6.4.1(6.15/3) is restricted to elementary types, but
2082 -- store this actual in Writable_Actuals_List since it is
2083 -- needed to perform checks on other constructs that have
2084 -- arbitrary order of evaluation (for example, aggregates).
2085
2086 if not Is_Elementary_Type (Etype (N)) then
2087 if not Contains (Writable_Actuals_List, N) then
2088 Append_New_Elmt (N, To => Writable_Actuals_List);
2089 end if;
2090
2091 -- Second occurrence of an elementary type writable actual
2092
2093 elsif Contains (Writable_Actuals_List, N) then
2094
2095 -- Report the error on the second occurrence of the
2096 -- identifier. We cannot assume that N is the second
2097 -- occurrence (according to their location in the
2098 -- sources), since Traverse_Func walks through Field2
2099 -- last (see comment in the body of Traverse_Func).
2100
2101 declare
2102 Elmt : Elmt_Id;
2103
2104 begin
2105 Elmt := First_Elmt (Writable_Actuals_List);
2106 while Present (Elmt)
2107 and then Entity (Node (Elmt)) /= Entity (N)
2108 loop
2109 Next_Elmt (Elmt);
2110 end loop;
2111
2112 if Sloc (N) > Sloc (Node (Elmt)) then
2113 Error_Node := N;
2114 else
2115 Error_Node := Node (Elmt);
2116 end if;
2117
2118 Error_Msg_NE
2119 ("value may be affected by call to & "
2120 & "because order of evaluation is arbitrary",
2121 Error_Node, Id);
2122 return Abandon;
2123 end;
2124
2125 -- First occurrence of a elementary type writable actual
2126
2127 else
2128 Append_New_Elmt (N, To => Writable_Actuals_List);
2129 end if;
2130
2131 else
2132 if Identifiers_List = No_Elist then
2133 Identifiers_List := New_Elmt_List;
2134 end if;
2135
2136 Append_Unique_Elmt (N, Identifiers_List);
2137 end if;
2138 end if;
2139
2140 return OK;
2141 end Check_Node;
2142
2143 --------------
2144 -- Contains --
2145 --------------
2146
2147 function Contains
2148 (List : Elist_Id;
2149 N : Node_Id) return Boolean
2150 is
2151 pragma Assert (Nkind (N) in N_Has_Entity);
2152
2153 Elmt : Elmt_Id;
2154
2155 begin
2156 if List = No_Elist then
2157 return False;
2158 end if;
2159
2160 Elmt := First_Elmt (List);
2161 while Present (Elmt) loop
2162 if Entity (Node (Elmt)) = Entity (N) then
2163 return True;
2164 else
2165 Next_Elmt (Elmt);
2166 end if;
2167 end loop;
2168
2169 return False;
2170 end Contains;
2171
2172 ------------------
2173 -- Do_Traversal --
2174 ------------------
2175
2176 procedure Do_Traversal is new Traverse_Proc (Check_Node);
2177 -- The traversal procedure
2178
2179 -- Start of processing for Collect_Identifiers
2180
2181 begin
2182 if Present (Error_Node) then
2183 return;
2184 end if;
2185
2186 if Nkind (N) in N_Subexpr and then Is_OK_Static_Expression (N) then
2187 return;
2188 end if;
2189
2190 Do_Traversal (N);
2191 end Collect_Identifiers;
2192
2193 ---------------------
2194 -- Get_Function_Id --
2195 ---------------------
2196
2197 function Get_Function_Id (Call : Node_Id) return Entity_Id is
2198 Nam : constant Node_Id := Name (Call);
2199 Id : Entity_Id;
2200
2201 begin
2202 if Nkind (Nam) = N_Explicit_Dereference then
2203 Id := Etype (Nam);
2204 pragma Assert (Ekind (Id) = E_Subprogram_Type);
2205
2206 elsif Nkind (Nam) = N_Selected_Component then
2207 Id := Entity (Selector_Name (Nam));
2208
2209 elsif Nkind (Nam) = N_Indexed_Component then
2210 Id := Entity (Selector_Name (Prefix (Nam)));
2211
2212 else
2213 Id := Entity (Nam);
2214 end if;
2215
2216 return Id;
2217 end Get_Function_Id;
2218
2219 -------------------------------
2220 -- Preanalyze_Without_Errors --
2221 -------------------------------
2222
2223 procedure Preanalyze_Without_Errors (N : Node_Id) is
2224 Status : constant Boolean := Get_Ignore_Errors;
2225 begin
2226 Set_Ignore_Errors (True);
2227 Preanalyze (N);
2228 Set_Ignore_Errors (Status);
2229 end Preanalyze_Without_Errors;
2230
2231 -- Start of processing for Check_Function_Writable_Actuals
2232
2233 begin
2234 -- The check only applies to Ada 2012 code on which Check_Actuals has
2235 -- been set, and only to constructs that have multiple constituents
2236 -- whose order of evaluation is not specified by the language.
2237
2238 if Ada_Version < Ada_2012
2239 or else not Check_Actuals (N)
2240 or else (not (Nkind (N) in N_Op)
2241 and then not (Nkind (N) in N_Membership_Test)
2242 and then not Nkind_In (N, N_Range,
2243 N_Aggregate,
2244 N_Extension_Aggregate,
2245 N_Full_Type_Declaration,
2246 N_Function_Call,
2247 N_Procedure_Call_Statement,
2248 N_Entry_Call_Statement))
2249 or else (Nkind (N) = N_Full_Type_Declaration
2250 and then not Is_Record_Type (Defining_Identifier (N)))
2251
2252 -- In addition, this check only applies to source code, not to code
2253 -- generated by constraint checks.
2254
2255 or else not Comes_From_Source (N)
2256 then
2257 return;
2258 end if;
2259
2260 -- If a construct C has two or more direct constituents that are names
2261 -- or expressions whose evaluation may occur in an arbitrary order, at
2262 -- least one of which contains a function call with an in out or out
2263 -- parameter, then the construct is legal only if: for each name N that
2264 -- is passed as a parameter of mode in out or out to some inner function
2265 -- call C2 (not including the construct C itself), there is no other
2266 -- name anywhere within a direct constituent of the construct C other
2267 -- than the one containing C2, that is known to refer to the same
2268 -- object (RM 6.4.1(6.17/3)).
2269
2270 case Nkind (N) is
2271 when N_Range =>
2272 Collect_Identifiers (Low_Bound (N));
2273 Collect_Identifiers (High_Bound (N));
2274
2275 when N_Op | N_Membership_Test =>
2276 declare
2277 Expr : Node_Id;
2278
2279 begin
2280 Collect_Identifiers (Left_Opnd (N));
2281
2282 if Present (Right_Opnd (N)) then
2283 Collect_Identifiers (Right_Opnd (N));
2284 end if;
2285
2286 if Nkind_In (N, N_In, N_Not_In)
2287 and then Present (Alternatives (N))
2288 then
2289 Expr := First (Alternatives (N));
2290 while Present (Expr) loop
2291 Collect_Identifiers (Expr);
2292
2293 Next (Expr);
2294 end loop;
2295 end if;
2296 end;
2297
2298 when N_Full_Type_Declaration =>
2299 declare
2300 function Get_Record_Part (N : Node_Id) return Node_Id;
2301 -- Return the record part of this record type definition
2302
2303 function Get_Record_Part (N : Node_Id) return Node_Id is
2304 Type_Def : constant Node_Id := Type_Definition (N);
2305 begin
2306 if Nkind (Type_Def) = N_Derived_Type_Definition then
2307 return Record_Extension_Part (Type_Def);
2308 else
2309 return Type_Def;
2310 end if;
2311 end Get_Record_Part;
2312
2313 Comp : Node_Id;
2314 Def_Id : Entity_Id := Defining_Identifier (N);
2315 Rec : Node_Id := Get_Record_Part (N);
2316
2317 begin
2318 -- No need to perform any analysis if the record has no
2319 -- components
2320
2321 if No (Rec) or else No (Component_List (Rec)) then
2322 return;
2323 end if;
2324
2325 -- Collect the identifiers starting from the deepest
2326 -- derivation. Done to report the error in the deepest
2327 -- derivation.
2328
2329 loop
2330 if Present (Component_List (Rec)) then
2331 Comp := First (Component_Items (Component_List (Rec)));
2332 while Present (Comp) loop
2333 if Nkind (Comp) = N_Component_Declaration
2334 and then Present (Expression (Comp))
2335 then
2336 Collect_Identifiers (Expression (Comp));
2337 end if;
2338
2339 Next (Comp);
2340 end loop;
2341 end if;
2342
2343 exit when No (Underlying_Type (Etype (Def_Id)))
2344 or else Base_Type (Underlying_Type (Etype (Def_Id)))
2345 = Def_Id;
2346
2347 Def_Id := Base_Type (Underlying_Type (Etype (Def_Id)));
2348 Rec := Get_Record_Part (Parent (Def_Id));
2349 end loop;
2350 end;
2351
2352 when N_Subprogram_Call |
2353 N_Entry_Call_Statement =>
2354 declare
2355 Id : constant Entity_Id := Get_Function_Id (N);
2356 Formal : Node_Id;
2357 Actual : Node_Id;
2358
2359 begin
2360 Formal := First_Formal (Id);
2361 Actual := First_Actual (N);
2362 while Present (Actual) and then Present (Formal) loop
2363 if Ekind_In (Formal, E_Out_Parameter,
2364 E_In_Out_Parameter)
2365 then
2366 Collect_Identifiers (Actual);
2367 end if;
2368
2369 Next_Formal (Formal);
2370 Next_Actual (Actual);
2371 end loop;
2372 end;
2373
2374 when N_Aggregate |
2375 N_Extension_Aggregate =>
2376 declare
2377 Assoc : Node_Id;
2378 Choice : Node_Id;
2379 Comp_Expr : Node_Id;
2380
2381 begin
2382 -- Handle the N_Others_Choice of array aggregates with static
2383 -- bounds. There is no need to perform this analysis in
2384 -- aggregates without static bounds since we cannot evaluate
2385 -- if the N_Others_Choice covers several elements. There is
2386 -- no need to handle the N_Others choice of record aggregates
2387 -- since at this stage it has been already expanded by
2388 -- Resolve_Record_Aggregate.
2389
2390 if Is_Array_Type (Etype (N))
2391 and then Nkind (N) = N_Aggregate
2392 and then Present (Aggregate_Bounds (N))
2393 and then Compile_Time_Known_Bounds (Etype (N))
2394 and then Expr_Value (High_Bound (Aggregate_Bounds (N)))
2395 >
2396 Expr_Value (Low_Bound (Aggregate_Bounds (N)))
2397 then
2398 declare
2399 Count_Components : Uint := Uint_0;
2400 Num_Components : Uint;
2401 Others_Assoc : Node_Id;
2402 Others_Choice : Node_Id := Empty;
2403 Others_Box_Present : Boolean := False;
2404
2405 begin
2406 -- Count positional associations
2407
2408 if Present (Expressions (N)) then
2409 Comp_Expr := First (Expressions (N));
2410 while Present (Comp_Expr) loop
2411 Count_Components := Count_Components + 1;
2412 Next (Comp_Expr);
2413 end loop;
2414 end if;
2415
2416 -- Count the rest of elements and locate the N_Others
2417 -- choice (if any)
2418
2419 Assoc := First (Component_Associations (N));
2420 while Present (Assoc) loop
2421 Choice := First (Choices (Assoc));
2422 while Present (Choice) loop
2423 if Nkind (Choice) = N_Others_Choice then
2424 Others_Assoc := Assoc;
2425 Others_Choice := Choice;
2426 Others_Box_Present := Box_Present (Assoc);
2427
2428 -- Count several components
2429
2430 elsif Nkind_In (Choice, N_Range,
2431 N_Subtype_Indication)
2432 or else (Is_Entity_Name (Choice)
2433 and then Is_Type (Entity (Choice)))
2434 then
2435 declare
2436 L, H : Node_Id;
2437 begin
2438 Get_Index_Bounds (Choice, L, H);
2439 pragma Assert
2440 (Compile_Time_Known_Value (L)
2441 and then Compile_Time_Known_Value (H));
2442 Count_Components :=
2443 Count_Components
2444 + Expr_Value (H) - Expr_Value (L) + 1;
2445 end;
2446
2447 -- Count single component. No other case available
2448 -- since we are handling an aggregate with static
2449 -- bounds.
2450
2451 else
2452 pragma Assert (Is_OK_Static_Expression (Choice)
2453 or else Nkind (Choice) = N_Identifier
2454 or else Nkind (Choice) = N_Integer_Literal);
2455
2456 Count_Components := Count_Components + 1;
2457 end if;
2458
2459 Next (Choice);
2460 end loop;
2461
2462 Next (Assoc);
2463 end loop;
2464
2465 Num_Components :=
2466 Expr_Value (High_Bound (Aggregate_Bounds (N))) -
2467 Expr_Value (Low_Bound (Aggregate_Bounds (N))) + 1;
2468
2469 pragma Assert (Count_Components <= Num_Components);
2470
2471 -- Handle the N_Others choice if it covers several
2472 -- components
2473
2474 if Present (Others_Choice)
2475 and then (Num_Components - Count_Components) > 1
2476 then
2477 if not Others_Box_Present then
2478
2479 -- At this stage, if expansion is active, the
2480 -- expression of the others choice has not been
2481 -- analyzed. Hence we generate a duplicate and
2482 -- we analyze it silently to have available the
2483 -- minimum decoration required to collect the
2484 -- identifiers.
2485
2486 if not Expander_Active then
2487 Comp_Expr := Expression (Others_Assoc);
2488 else
2489 Comp_Expr :=
2490 New_Copy_Tree (Expression (Others_Assoc));
2491 Preanalyze_Without_Errors (Comp_Expr);
2492 end if;
2493
2494 Collect_Identifiers (Comp_Expr);
2495
2496 if Writable_Actuals_List /= No_Elist then
2497
2498 -- As suggested by Robert, at current stage we
2499 -- report occurrences of this case as warnings.
2500
2501 Error_Msg_N
2502 ("writable function parameter may affect "
2503 & "value in other component because order "
2504 & "of evaluation is unspecified??",
2505 Node (First_Elmt (Writable_Actuals_List)));
2506 end if;
2507 end if;
2508 end if;
2509 end;
2510
2511 -- For an array aggregate, a discrete_choice_list that has
2512 -- a nonstatic range is considered as two or more separate
2513 -- occurrences of the expression (RM 6.4.1(20/3)).
2514
2515 elsif Is_Array_Type (Etype (N))
2516 and then Nkind (N) = N_Aggregate
2517 and then Present (Aggregate_Bounds (N))
2518 and then not Compile_Time_Known_Bounds (Etype (N))
2519 then
2520 -- Collect identifiers found in the dynamic bounds
2521
2522 declare
2523 Count_Components : Natural := 0;
2524 Low, High : Node_Id;
2525
2526 begin
2527 Assoc := First (Component_Associations (N));
2528 while Present (Assoc) loop
2529 Choice := First (Choices (Assoc));
2530 while Present (Choice) loop
2531 if Nkind_In (Choice, N_Range,
2532 N_Subtype_Indication)
2533 or else (Is_Entity_Name (Choice)
2534 and then Is_Type (Entity (Choice)))
2535 then
2536 Get_Index_Bounds (Choice, Low, High);
2537
2538 if not Compile_Time_Known_Value (Low) then
2539 Collect_Identifiers (Low);
2540
2541 if No (Aggr_Error_Node) then
2542 Aggr_Error_Node := Low;
2543 end if;
2544 end if;
2545
2546 if not Compile_Time_Known_Value (High) then
2547 Collect_Identifiers (High);
2548
2549 if No (Aggr_Error_Node) then
2550 Aggr_Error_Node := High;
2551 end if;
2552 end if;
2553
2554 -- The RM rule is violated if there is more than
2555 -- a single choice in a component association.
2556
2557 else
2558 Count_Components := Count_Components + 1;
2559
2560 if No (Aggr_Error_Node)
2561 and then Count_Components > 1
2562 then
2563 Aggr_Error_Node := Choice;
2564 end if;
2565
2566 if not Compile_Time_Known_Value (Choice) then
2567 Collect_Identifiers (Choice);
2568 end if;
2569 end if;
2570
2571 Next (Choice);
2572 end loop;
2573
2574 Next (Assoc);
2575 end loop;
2576 end;
2577 end if;
2578
2579 -- Handle ancestor part of extension aggregates
2580
2581 if Nkind (N) = N_Extension_Aggregate then
2582 Collect_Identifiers (Ancestor_Part (N));
2583 end if;
2584
2585 -- Handle positional associations
2586
2587 if Present (Expressions (N)) then
2588 Comp_Expr := First (Expressions (N));
2589 while Present (Comp_Expr) loop
2590 if not Is_OK_Static_Expression (Comp_Expr) then
2591 Collect_Identifiers (Comp_Expr);
2592 end if;
2593
2594 Next (Comp_Expr);
2595 end loop;
2596 end if;
2597
2598 -- Handle discrete associations
2599
2600 if Present (Component_Associations (N)) then
2601 Assoc := First (Component_Associations (N));
2602 while Present (Assoc) loop
2603
2604 if not Box_Present (Assoc) then
2605 Choice := First (Choices (Assoc));
2606 while Present (Choice) loop
2607
2608 -- For now we skip discriminants since it requires
2609 -- performing the analysis in two phases: first one
2610 -- analyzing discriminants and second one analyzing
2611 -- the rest of components since discriminants are
2612 -- evaluated prior to components: too much extra
2613 -- work to detect a corner case???
2614
2615 if Nkind (Choice) in N_Has_Entity
2616 and then Present (Entity (Choice))
2617 and then Ekind (Entity (Choice)) = E_Discriminant
2618 then
2619 null;
2620
2621 elsif Box_Present (Assoc) then
2622 null;
2623
2624 else
2625 if not Analyzed (Expression (Assoc)) then
2626 Comp_Expr :=
2627 New_Copy_Tree (Expression (Assoc));
2628 Set_Parent (Comp_Expr, Parent (N));
2629 Preanalyze_Without_Errors (Comp_Expr);
2630 else
2631 Comp_Expr := Expression (Assoc);
2632 end if;
2633
2634 Collect_Identifiers (Comp_Expr);
2635 end if;
2636
2637 Next (Choice);
2638 end loop;
2639 end if;
2640
2641 Next (Assoc);
2642 end loop;
2643 end if;
2644 end;
2645
2646 when others =>
2647 return;
2648 end case;
2649
2650 -- No further action needed if we already reported an error
2651
2652 if Present (Error_Node) then
2653 return;
2654 end if;
2655
2656 -- Check violation of RM 6.20/3 in aggregates
2657
2658 if Present (Aggr_Error_Node)
2659 and then Writable_Actuals_List /= No_Elist
2660 then
2661 Error_Msg_N
2662 ("value may be affected by call in other component because they "
2663 & "are evaluated in unspecified order",
2664 Node (First_Elmt (Writable_Actuals_List)));
2665 return;
2666 end if;
2667
2668 -- Check if some writable argument of a function is referenced
2669
2670 if Writable_Actuals_List /= No_Elist
2671 and then Identifiers_List /= No_Elist
2672 then
2673 declare
2674 Elmt_1 : Elmt_Id;
2675 Elmt_2 : Elmt_Id;
2676
2677 begin
2678 Elmt_1 := First_Elmt (Writable_Actuals_List);
2679 while Present (Elmt_1) loop
2680 Elmt_2 := First_Elmt (Identifiers_List);
2681 while Present (Elmt_2) loop
2682 if Entity (Node (Elmt_1)) = Entity (Node (Elmt_2)) then
2683 case Nkind (Parent (Node (Elmt_2))) is
2684 when N_Aggregate |
2685 N_Component_Association |
2686 N_Component_Declaration =>
2687 Error_Msg_N
2688 ("value may be affected by call in other "
2689 & "component because they are evaluated "
2690 & "in unspecified order",
2691 Node (Elmt_2));
2692
2693 when N_In | N_Not_In =>
2694 Error_Msg_N
2695 ("value may be affected by call in other "
2696 & "alternative because they are evaluated "
2697 & "in unspecified order",
2698 Node (Elmt_2));
2699
2700 when others =>
2701 Error_Msg_N
2702 ("value of actual may be affected by call in "
2703 & "other actual because they are evaluated "
2704 & "in unspecified order",
2705 Node (Elmt_2));
2706 end case;
2707 end if;
2708
2709 Next_Elmt (Elmt_2);
2710 end loop;
2711
2712 Next_Elmt (Elmt_1);
2713 end loop;
2714 end;
2715 end if;
2716 end Check_Function_Writable_Actuals;
2717
2718 --------------------------------
2719 -- Check_Implicit_Dereference --
2720 --------------------------------
2721
2722 procedure Check_Implicit_Dereference (N : Node_Id; Typ : Entity_Id) is
2723 Disc : Entity_Id;
2724 Desig : Entity_Id;
2725 Nam : Node_Id;
2726
2727 begin
2728 if Nkind (N) = N_Indexed_Component
2729 and then Present (Generalized_Indexing (N))
2730 then
2731 Nam := Generalized_Indexing (N);
2732 else
2733 Nam := N;
2734 end if;
2735
2736 if Ada_Version < Ada_2012
2737 or else not Has_Implicit_Dereference (Base_Type (Typ))
2738 then
2739 return;
2740
2741 elsif not Comes_From_Source (N)
2742 and then Nkind (N) /= N_Indexed_Component
2743 then
2744 return;
2745
2746 elsif Is_Entity_Name (Nam) and then Is_Type (Entity (Nam)) then
2747 null;
2748
2749 else
2750 Disc := First_Discriminant (Typ);
2751 while Present (Disc) loop
2752 if Has_Implicit_Dereference (Disc) then
2753 Desig := Designated_Type (Etype (Disc));
2754 Add_One_Interp (Nam, Disc, Desig);
2755
2756 -- If the node is a generalized indexing, add interpretation
2757 -- to that node as well, for subsequent resolution.
2758
2759 if Nkind (N) = N_Indexed_Component then
2760 Add_One_Interp (N, Disc, Desig);
2761 end if;
2762
2763 -- If the operation comes from a generic unit and the context
2764 -- is a selected component, the selector name may be global
2765 -- and set in the instance already. Remove the entity to
2766 -- force resolution of the selected component, and the
2767 -- generation of an explicit dereference if needed.
2768
2769 if In_Instance
2770 and then Nkind (Parent (Nam)) = N_Selected_Component
2771 then
2772 Set_Entity (Selector_Name (Parent (Nam)), Empty);
2773 end if;
2774
2775 exit;
2776 end if;
2777
2778 Next_Discriminant (Disc);
2779 end loop;
2780 end if;
2781 end Check_Implicit_Dereference;
2782
2783 ----------------------------------
2784 -- Check_Internal_Protected_Use --
2785 ----------------------------------
2786
2787 procedure Check_Internal_Protected_Use (N : Node_Id; Nam : Entity_Id) is
2788 S : Entity_Id;
2789 Prot : Entity_Id;
2790
2791 begin
2792 S := Current_Scope;
2793 while Present (S) loop
2794 if S = Standard_Standard then
2795 return;
2796
2797 elsif Ekind (S) = E_Function
2798 and then Ekind (Scope (S)) = E_Protected_Type
2799 then
2800 Prot := Scope (S);
2801 exit;
2802 end if;
2803
2804 S := Scope (S);
2805 end loop;
2806
2807 if Scope (Nam) = Prot and then Ekind (Nam) /= E_Function then
2808
2809 -- An indirect function call (e.g. a callback within a protected
2810 -- function body) is not statically illegal. If the access type is
2811 -- anonymous and is the type of an access parameter, the scope of Nam
2812 -- will be the protected type, but it is not a protected operation.
2813
2814 if Ekind (Nam) = E_Subprogram_Type
2815 and then
2816 Nkind (Associated_Node_For_Itype (Nam)) = N_Function_Specification
2817 then
2818 null;
2819
2820 elsif Nkind (N) = N_Subprogram_Renaming_Declaration then
2821 Error_Msg_N
2822 ("within protected function cannot use protected "
2823 & "procedure in renaming or as generic actual", N);
2824
2825 elsif Nkind (N) = N_Attribute_Reference then
2826 Error_Msg_N
2827 ("within protected function cannot take access of "
2828 & " protected procedure", N);
2829
2830 else
2831 Error_Msg_N
2832 ("within protected function, protected object is constant", N);
2833 Error_Msg_N
2834 ("\cannot call operation that may modify it", N);
2835 end if;
2836 end if;
2837 end Check_Internal_Protected_Use;
2838
2839 ---------------------------------------
2840 -- Check_Later_Vs_Basic_Declarations --
2841 ---------------------------------------
2842
2843 procedure Check_Later_Vs_Basic_Declarations
2844 (Decls : List_Id;
2845 During_Parsing : Boolean)
2846 is
2847 Body_Sloc : Source_Ptr;
2848 Decl : Node_Id;
2849
2850 function Is_Later_Declarative_Item (Decl : Node_Id) return Boolean;
2851 -- Return whether Decl is considered as a declarative item.
2852 -- When During_Parsing is True, the semantics of Ada 83 is followed.
2853 -- When During_Parsing is False, the semantics of SPARK is followed.
2854
2855 -------------------------------
2856 -- Is_Later_Declarative_Item --
2857 -------------------------------
2858
2859 function Is_Later_Declarative_Item (Decl : Node_Id) return Boolean is
2860 begin
2861 if Nkind (Decl) in N_Later_Decl_Item then
2862 return True;
2863
2864 elsif Nkind (Decl) = N_Pragma then
2865 return True;
2866
2867 elsif During_Parsing then
2868 return False;
2869
2870 -- In SPARK, a package declaration is not considered as a later
2871 -- declarative item.
2872
2873 elsif Nkind (Decl) = N_Package_Declaration then
2874 return False;
2875
2876 -- In SPARK, a renaming is considered as a later declarative item
2877
2878 elsif Nkind (Decl) in N_Renaming_Declaration then
2879 return True;
2880
2881 else
2882 return False;
2883 end if;
2884 end Is_Later_Declarative_Item;
2885
2886 -- Start of processing for Check_Later_Vs_Basic_Declarations
2887
2888 begin
2889 Decl := First (Decls);
2890
2891 -- Loop through sequence of basic declarative items
2892
2893 Outer : while Present (Decl) loop
2894 if not Nkind_In (Decl, N_Subprogram_Body, N_Package_Body, N_Task_Body)
2895 and then Nkind (Decl) not in N_Body_Stub
2896 then
2897 Next (Decl);
2898
2899 -- Once a body is encountered, we only allow later declarative
2900 -- items. The inner loop checks the rest of the list.
2901
2902 else
2903 Body_Sloc := Sloc (Decl);
2904
2905 Inner : while Present (Decl) loop
2906 if not Is_Later_Declarative_Item (Decl) then
2907 if During_Parsing then
2908 if Ada_Version = Ada_83 then
2909 Error_Msg_Sloc := Body_Sloc;
2910 Error_Msg_N
2911 ("(Ada 83) decl cannot appear after body#", Decl);
2912 end if;
2913 else
2914 Error_Msg_Sloc := Body_Sloc;
2915 Check_SPARK_05_Restriction
2916 ("decl cannot appear after body#", Decl);
2917 end if;
2918 end if;
2919
2920 Next (Decl);
2921 end loop Inner;
2922 end if;
2923 end loop Outer;
2924 end Check_Later_Vs_Basic_Declarations;
2925
2926 ---------------------------
2927 -- Check_No_Hidden_State --
2928 ---------------------------
2929
2930 procedure Check_No_Hidden_State (Id : Entity_Id) is
2931 function Has_Null_Abstract_State (Pkg : Entity_Id) return Boolean;
2932 -- Determine whether the entity of a package denoted by Pkg has a null
2933 -- abstract state.
2934
2935 -----------------------------
2936 -- Has_Null_Abstract_State --
2937 -----------------------------
2938
2939 function Has_Null_Abstract_State (Pkg : Entity_Id) return Boolean is
2940 States : constant Elist_Id := Abstract_States (Pkg);
2941
2942 begin
2943 -- Check first available state of related package. A null abstract
2944 -- state always appears as the sole element of the state list.
2945
2946 return
2947 Present (States)
2948 and then Is_Null_State (Node (First_Elmt (States)));
2949 end Has_Null_Abstract_State;
2950
2951 -- Local variables
2952
2953 Context : Entity_Id := Empty;
2954 Not_Visible : Boolean := False;
2955 Scop : Entity_Id;
2956
2957 -- Start of processing for Check_No_Hidden_State
2958
2959 begin
2960 pragma Assert (Ekind_In (Id, E_Abstract_State, E_Variable));
2961
2962 -- Find the proper context where the object or state appears
2963
2964 Scop := Scope (Id);
2965 while Present (Scop) loop
2966 Context := Scop;
2967
2968 -- Keep track of the context's visibility
2969
2970 Not_Visible := Not_Visible or else In_Private_Part (Context);
2971
2972 -- Prevent the search from going too far
2973
2974 if Context = Standard_Standard then
2975 return;
2976
2977 -- Objects and states that appear immediately within a subprogram or
2978 -- inside a construct nested within a subprogram do not introduce a
2979 -- hidden state. They behave as local variable declarations.
2980
2981 elsif Is_Subprogram (Context) then
2982 return;
2983
2984 -- When examining a package body, use the entity of the spec as it
2985 -- carries the abstract state declarations.
2986
2987 elsif Ekind (Context) = E_Package_Body then
2988 Context := Spec_Entity (Context);
2989 end if;
2990
2991 -- Stop the traversal when a package subject to a null abstract state
2992 -- has been found.
2993
2994 if Ekind_In (Context, E_Generic_Package, E_Package)
2995 and then Has_Null_Abstract_State (Context)
2996 then
2997 exit;
2998 end if;
2999
3000 Scop := Scope (Scop);
3001 end loop;
3002
3003 -- At this point we know that there is at least one package with a null
3004 -- abstract state in visibility. Emit an error message unconditionally
3005 -- if the entity being processed is a state because the placement of the
3006 -- related package is irrelevant. This is not the case for objects as
3007 -- the intermediate context matters.
3008
3009 if Present (Context)
3010 and then (Ekind (Id) = E_Abstract_State or else Not_Visible)
3011 then
3012 Error_Msg_N ("cannot introduce hidden state &", Id);
3013 Error_Msg_NE ("\package & has null abstract state", Id, Context);
3014 end if;
3015 end Check_No_Hidden_State;
3016
3017 ----------------------------------------
3018 -- Check_Nonvolatile_Function_Profile --
3019 ----------------------------------------
3020
3021 procedure Check_Nonvolatile_Function_Profile (Func_Id : Entity_Id) is
3022 Formal : Entity_Id;
3023
3024 begin
3025 -- Inspect all formal parameters
3026
3027 Formal := First_Formal (Func_Id);
3028 while Present (Formal) loop
3029 if Is_Effectively_Volatile (Etype (Formal)) then
3030 Error_Msg_NE
3031 ("nonvolatile function & cannot have a volatile parameter",
3032 Formal, Func_Id);
3033 end if;
3034
3035 Next_Formal (Formal);
3036 end loop;
3037
3038 -- Inspect the return type
3039
3040 if Is_Effectively_Volatile (Etype (Func_Id)) then
3041 Error_Msg_NE
3042 ("nonvolatile function & cannot have a volatile return type",
3043 Result_Definition (Parent (Func_Id)), Func_Id);
3044 end if;
3045 end Check_Nonvolatile_Function_Profile;
3046
3047 ------------------------------------------
3048 -- Check_Potentially_Blocking_Operation --
3049 ------------------------------------------
3050
3051 procedure Check_Potentially_Blocking_Operation (N : Node_Id) is
3052 S : Entity_Id;
3053
3054 begin
3055 -- N is one of the potentially blocking operations listed in 9.5.1(8).
3056 -- When pragma Detect_Blocking is active, the run time will raise
3057 -- Program_Error. Here we only issue a warning, since we generally
3058 -- support the use of potentially blocking operations in the absence
3059 -- of the pragma.
3060
3061 -- Indirect blocking through a subprogram call cannot be diagnosed
3062 -- statically without interprocedural analysis, so we do not attempt
3063 -- to do it here.
3064
3065 S := Scope (Current_Scope);
3066 while Present (S) and then S /= Standard_Standard loop
3067 if Is_Protected_Type (S) then
3068 Error_Msg_N
3069 ("potentially blocking operation in protected operation??", N);
3070 return;
3071 end if;
3072
3073 S := Scope (S);
3074 end loop;
3075 end Check_Potentially_Blocking_Operation;
3076
3077 ---------------------------------
3078 -- Check_Result_And_Post_State --
3079 ---------------------------------
3080
3081 procedure Check_Result_And_Post_State (Subp_Id : Entity_Id) is
3082 procedure Check_Result_And_Post_State_In_Pragma
3083 (Prag : Node_Id;
3084 Result_Seen : in out Boolean);
3085 -- Determine whether pragma Prag mentions attribute 'Result and whether
3086 -- the pragma contains an expression that evaluates differently in pre-
3087 -- and post-state. Prag is a [refined] postcondition or a contract-cases
3088 -- pragma. Result_Seen is set when the pragma mentions attribute 'Result
3089
3090 function Has_In_Out_Parameter (Subp_Id : Entity_Id) return Boolean;
3091 -- Determine whether subprogram Subp_Id contains at least one IN OUT
3092 -- formal parameter.
3093
3094 -------------------------------------------
3095 -- Check_Result_And_Post_State_In_Pragma --
3096 -------------------------------------------
3097
3098 procedure Check_Result_And_Post_State_In_Pragma
3099 (Prag : Node_Id;
3100 Result_Seen : in out Boolean)
3101 is
3102 procedure Check_Expression (Expr : Node_Id);
3103 -- Perform the 'Result and post-state checks on a given expression
3104
3105 function Is_Function_Result (N : Node_Id) return Traverse_Result;
3106 -- Attempt to find attribute 'Result in a subtree denoted by N
3107
3108 function Is_Trivial_Boolean (N : Node_Id) return Boolean;
3109 -- Determine whether source node N denotes "True" or "False"
3110
3111 function Mentions_Post_State (N : Node_Id) return Boolean;
3112 -- Determine whether a subtree denoted by N mentions any construct
3113 -- that denotes a post-state.
3114
3115 procedure Check_Function_Result is
3116 new Traverse_Proc (Is_Function_Result);
3117
3118 ----------------------
3119 -- Check_Expression --
3120 ----------------------
3121
3122 procedure Check_Expression (Expr : Node_Id) is
3123 begin
3124 if not Is_Trivial_Boolean (Expr) then
3125 Check_Function_Result (Expr);
3126
3127 if not Mentions_Post_State (Expr) then
3128 if Pragma_Name (Prag) = Name_Contract_Cases then
3129 Error_Msg_NE
3130 ("contract case does not check the outcome of calling "
3131 & "&?T?", Expr, Subp_Id);
3132
3133 elsif Pragma_Name (Prag) = Name_Refined_Post then
3134 Error_Msg_NE
3135 ("refined postcondition does not check the outcome of "
3136 & "calling &?T?", Prag, Subp_Id);
3137
3138 else
3139 Error_Msg_NE
3140 ("postcondition does not check the outcome of calling "
3141 & "&?T?", Prag, Subp_Id);
3142 end if;
3143 end if;
3144 end if;
3145 end Check_Expression;
3146
3147 ------------------------
3148 -- Is_Function_Result --
3149 ------------------------
3150
3151 function Is_Function_Result (N : Node_Id) return Traverse_Result is
3152 begin
3153 if Is_Attribute_Result (N) then
3154 Result_Seen := True;
3155 return Abandon;
3156
3157 -- Continue the traversal
3158
3159 else
3160 return OK;
3161 end if;
3162 end Is_Function_Result;
3163
3164 ------------------------
3165 -- Is_Trivial_Boolean --
3166 ------------------------
3167
3168 function Is_Trivial_Boolean (N : Node_Id) return Boolean is
3169 begin
3170 return
3171 Comes_From_Source (N)
3172 and then Is_Entity_Name (N)
3173 and then (Entity (N) = Standard_True
3174 or else
3175 Entity (N) = Standard_False);
3176 end Is_Trivial_Boolean;
3177
3178 -------------------------
3179 -- Mentions_Post_State --
3180 -------------------------
3181
3182 function Mentions_Post_State (N : Node_Id) return Boolean is
3183 Post_State_Seen : Boolean := False;
3184
3185 function Is_Post_State (N : Node_Id) return Traverse_Result;
3186 -- Attempt to find a construct that denotes a post-state. If this
3187 -- is the case, set flag Post_State_Seen.
3188
3189 -------------------
3190 -- Is_Post_State --
3191 -------------------
3192
3193 function Is_Post_State (N : Node_Id) return Traverse_Result is
3194 Ent : Entity_Id;
3195
3196 begin
3197 if Nkind_In (N, N_Explicit_Dereference, N_Function_Call) then
3198 Post_State_Seen := True;
3199 return Abandon;
3200
3201 elsif Nkind_In (N, N_Expanded_Name, N_Identifier) then
3202 Ent := Entity (N);
3203
3204 -- The entity may be modifiable through an implicit
3205 -- dereference.
3206
3207 if No (Ent)
3208 or else Ekind (Ent) in Assignable_Kind
3209 or else (Is_Access_Type (Etype (Ent))
3210 and then Nkind (Parent (N)) =
3211 N_Selected_Component)
3212 then
3213 Post_State_Seen := True;
3214 return Abandon;
3215 end if;
3216
3217 elsif Nkind (N) = N_Attribute_Reference then
3218 if Attribute_Name (N) = Name_Old then
3219 return Skip;
3220
3221 elsif Attribute_Name (N) = Name_Result then
3222 Post_State_Seen := True;
3223 return Abandon;
3224 end if;
3225 end if;
3226
3227 return OK;
3228 end Is_Post_State;
3229
3230 procedure Find_Post_State is new Traverse_Proc (Is_Post_State);
3231
3232 -- Start of processing for Mentions_Post_State
3233
3234 begin
3235 Find_Post_State (N);
3236
3237 return Post_State_Seen;
3238 end Mentions_Post_State;
3239
3240 -- Local variables
3241
3242 Expr : constant Node_Id :=
3243 Get_Pragma_Arg
3244 (First (Pragma_Argument_Associations (Prag)));
3245 Nam : constant Name_Id := Pragma_Name (Prag);
3246 CCase : Node_Id;
3247
3248 -- Start of processing for Check_Result_And_Post_State_In_Pragma
3249
3250 begin
3251 -- Examine all consequences
3252
3253 if Nam = Name_Contract_Cases then
3254 CCase := First (Component_Associations (Expr));
3255 while Present (CCase) loop
3256 Check_Expression (Expression (CCase));
3257
3258 Next (CCase);
3259 end loop;
3260
3261 -- Examine the expression of a postcondition
3262
3263 else pragma Assert (Nam_In (Nam, Name_Postcondition,
3264 Name_Refined_Post));
3265 Check_Expression (Expr);
3266 end if;
3267 end Check_Result_And_Post_State_In_Pragma;
3268
3269 --------------------------
3270 -- Has_In_Out_Parameter --
3271 --------------------------
3272
3273 function Has_In_Out_Parameter (Subp_Id : Entity_Id) return Boolean is
3274 Formal : Entity_Id;
3275
3276 begin
3277 -- Traverse the formals looking for an IN OUT parameter
3278
3279 Formal := First_Formal (Subp_Id);
3280 while Present (Formal) loop
3281 if Ekind (Formal) = E_In_Out_Parameter then
3282 return True;
3283 end if;
3284
3285 Next_Formal (Formal);
3286 end loop;
3287
3288 return False;
3289 end Has_In_Out_Parameter;
3290
3291 -- Local variables
3292
3293 Items : constant Node_Id := Contract (Subp_Id);
3294 Subp_Decl : constant Node_Id := Unit_Declaration_Node (Subp_Id);
3295 Case_Prag : Node_Id := Empty;
3296 Post_Prag : Node_Id := Empty;
3297 Prag : Node_Id;
3298 Seen_In_Case : Boolean := False;
3299 Seen_In_Post : Boolean := False;
3300 Spec_Id : Entity_Id;
3301
3302 -- Start of processing for Check_Result_And_Post_State
3303
3304 begin
3305 -- The lack of attribute 'Result or a post-state is classified as a
3306 -- suspicious contract. Do not perform the check if the corresponding
3307 -- swich is not set.
3308
3309 if not Warn_On_Suspicious_Contract then
3310 return;
3311
3312 -- Nothing to do if there is no contract
3313
3314 elsif No (Items) then
3315 return;
3316 end if;
3317
3318 -- Retrieve the entity of the subprogram spec (if any)
3319
3320 if Nkind (Subp_Decl) = N_Subprogram_Body
3321 and then Present (Corresponding_Spec (Subp_Decl))
3322 then
3323 Spec_Id := Corresponding_Spec (Subp_Decl);
3324
3325 elsif Nkind (Subp_Decl) = N_Subprogram_Body_Stub
3326 and then Present (Corresponding_Spec_Of_Stub (Subp_Decl))
3327 then
3328 Spec_Id := Corresponding_Spec_Of_Stub (Subp_Decl);
3329
3330 else
3331 Spec_Id := Subp_Id;
3332 end if;
3333
3334 -- Examine all postconditions for attribute 'Result and a post-state
3335
3336 Prag := Pre_Post_Conditions (Items);
3337 while Present (Prag) loop
3338 if Nam_In (Pragma_Name_Unmapped (Prag),
3339 Name_Postcondition, Name_Refined_Post)
3340 and then not Error_Posted (Prag)
3341 then
3342 Post_Prag := Prag;
3343 Check_Result_And_Post_State_In_Pragma (Prag, Seen_In_Post);
3344 end if;
3345
3346 Prag := Next_Pragma (Prag);
3347 end loop;
3348
3349 -- Examine the contract cases of the subprogram for attribute 'Result
3350 -- and a post-state.
3351
3352 Prag := Contract_Test_Cases (Items);
3353 while Present (Prag) loop
3354 if Pragma_Name (Prag) = Name_Contract_Cases
3355 and then not Error_Posted (Prag)
3356 then
3357 Case_Prag := Prag;
3358 Check_Result_And_Post_State_In_Pragma (Prag, Seen_In_Case);
3359 end if;
3360
3361 Prag := Next_Pragma (Prag);
3362 end loop;
3363
3364 -- Do not emit any errors if the subprogram is not a function
3365
3366 if not Ekind_In (Spec_Id, E_Function, E_Generic_Function) then
3367 null;
3368
3369 -- Regardless of whether the function has postconditions or contract
3370 -- cases, or whether they mention attribute 'Result, an IN OUT formal
3371 -- parameter is always treated as a result.
3372
3373 elsif Has_In_Out_Parameter (Spec_Id) then
3374 null;
3375
3376 -- The function has both a postcondition and contract cases and they do
3377 -- not mention attribute 'Result.
3378
3379 elsif Present (Case_Prag)
3380 and then not Seen_In_Case
3381 and then Present (Post_Prag)
3382 and then not Seen_In_Post
3383 then
3384 Error_Msg_N
3385 ("neither postcondition nor contract cases mention function "
3386 & "result?T?", Post_Prag);
3387
3388 -- The function has contract cases only and they do not mention
3389 -- attribute 'Result.
3390
3391 elsif Present (Case_Prag) and then not Seen_In_Case then
3392 Error_Msg_N ("contract cases do not mention result?T?", Case_Prag);
3393
3394 -- The function has postconditions only and they do not mention
3395 -- attribute 'Result.
3396
3397 elsif Present (Post_Prag) and then not Seen_In_Post then
3398 Error_Msg_N
3399 ("postcondition does not mention function result?T?", Post_Prag);
3400 end if;
3401 end Check_Result_And_Post_State;
3402
3403 -----------------------------
3404 -- Check_State_Refinements --
3405 -----------------------------
3406
3407 procedure Check_State_Refinements
3408 (Context : Node_Id;
3409 Is_Main_Unit : Boolean := False)
3410 is
3411 procedure Check_Package (Pack : Node_Id);
3412 -- Verify that all abstract states of a [generic] package denoted by its
3413 -- declarative node Pack have proper refinement. Recursively verify the
3414 -- visible and private declarations of the [generic] package for other
3415 -- nested packages.
3416
3417 procedure Check_Packages_In (Decls : List_Id);
3418 -- Seek out [generic] package declarations within declarative list Decls
3419 -- and verify the status of their abstract state refinement.
3420
3421 function SPARK_Mode_Is_Off (N : Node_Id) return Boolean;
3422 -- Determine whether construct N is subject to pragma SPARK_Mode Off
3423
3424 -------------------
3425 -- Check_Package --
3426 -------------------
3427
3428 procedure Check_Package (Pack : Node_Id) is
3429 Body_Id : constant Entity_Id := Corresponding_Body (Pack);
3430 Spec : constant Node_Id := Specification (Pack);
3431 States : constant Elist_Id :=
3432 Abstract_States (Defining_Entity (Pack));
3433
3434 State_Elmt : Elmt_Id;
3435 State_Id : Entity_Id;
3436
3437 begin
3438 -- Do not verify proper state refinement when the package is subject
3439 -- to pragma SPARK_Mode Off because this disables the requirement for
3440 -- state refinement.
3441
3442 if SPARK_Mode_Is_Off (Pack) then
3443 null;
3444
3445 -- State refinement can only occur in a completing packge body. Do
3446 -- not verify proper state refinement when the body is subject to
3447 -- pragma SPARK_Mode Off because this disables the requirement for
3448 -- state refinement.
3449
3450 elsif Present (Body_Id)
3451 and then SPARK_Mode_Is_Off (Unit_Declaration_Node (Body_Id))
3452 then
3453 null;
3454
3455 -- Do not verify proper state refinement when the package is an
3456 -- instance as this check was already performed in the generic.
3457
3458 elsif Present (Generic_Parent (Spec)) then
3459 null;
3460
3461 -- Otherwise examine the contents of the package
3462
3463 else
3464 if Present (States) then
3465 State_Elmt := First_Elmt (States);
3466 while Present (State_Elmt) loop
3467 State_Id := Node (State_Elmt);
3468
3469 -- Emit an error when a non-null state lacks any form of
3470 -- refinement.
3471
3472 if not Is_Null_State (State_Id)
3473 and then not Has_Null_Refinement (State_Id)
3474 and then not Has_Non_Null_Refinement (State_Id)
3475 then
3476 Error_Msg_N ("state & requires refinement", State_Id);
3477 end if;
3478
3479 Next_Elmt (State_Elmt);
3480 end loop;
3481 end if;
3482
3483 Check_Packages_In (Visible_Declarations (Spec));
3484 Check_Packages_In (Private_Declarations (Spec));
3485 end if;
3486 end Check_Package;
3487
3488 -----------------------
3489 -- Check_Packages_In --
3490 -----------------------
3491
3492 procedure Check_Packages_In (Decls : List_Id) is
3493 Decl : Node_Id;
3494
3495 begin
3496 if Present (Decls) then
3497 Decl := First (Decls);
3498 while Present (Decl) loop
3499 if Nkind_In (Decl, N_Generic_Package_Declaration,
3500 N_Package_Declaration)
3501 then
3502 Check_Package (Decl);
3503 end if;
3504
3505 Next (Decl);
3506 end loop;
3507 end if;
3508 end Check_Packages_In;
3509
3510 -----------------------
3511 -- SPARK_Mode_Is_Off --
3512 -----------------------
3513
3514 function SPARK_Mode_Is_Off (N : Node_Id) return Boolean is
3515 Prag : constant Node_Id := SPARK_Pragma (Defining_Entity (N));
3516
3517 begin
3518 return
3519 Present (Prag) and then Get_SPARK_Mode_From_Annotation (Prag) = Off;
3520 end SPARK_Mode_Is_Off;
3521
3522 -- Start of processing for Check_State_Refinements
3523
3524 begin
3525 -- A block may declare a nested package
3526
3527 if Nkind (Context) = N_Block_Statement then
3528 Check_Packages_In (Declarations (Context));
3529
3530 -- An entry, protected, subprogram, or task body may declare a nested
3531 -- package.
3532
3533 elsif Nkind_In (Context, N_Entry_Body,
3534 N_Protected_Body,
3535 N_Subprogram_Body,
3536 N_Task_Body)
3537 then
3538 -- Do not verify proper state refinement when the body is subject to
3539 -- pragma SPARK_Mode Off because this disables the requirement for
3540 -- state refinement.
3541
3542 if not SPARK_Mode_Is_Off (Context) then
3543 Check_Packages_In (Declarations (Context));
3544 end if;
3545
3546 -- A package body may declare a nested package
3547
3548 elsif Nkind (Context) = N_Package_Body then
3549 Check_Package (Unit_Declaration_Node (Corresponding_Spec (Context)));
3550
3551 -- Do not verify proper state refinement when the body is subject to
3552 -- pragma SPARK_Mode Off because this disables the requirement for
3553 -- state refinement.
3554
3555 if not SPARK_Mode_Is_Off (Context) then
3556 Check_Packages_In (Declarations (Context));
3557 end if;
3558
3559 -- A library level [generic] package may declare a nested package
3560
3561 elsif Nkind_In (Context, N_Generic_Package_Declaration,
3562 N_Package_Declaration)
3563 and then Is_Main_Unit
3564 then
3565 Check_Package (Context);
3566 end if;
3567 end Check_State_Refinements;
3568
3569 ------------------------------
3570 -- Check_Unprotected_Access --
3571 ------------------------------
3572
3573 procedure Check_Unprotected_Access
3574 (Context : Node_Id;
3575 Expr : Node_Id)
3576 is
3577 Cont_Encl_Typ : Entity_Id;
3578 Pref_Encl_Typ : Entity_Id;
3579
3580 function Enclosing_Protected_Type (Obj : Node_Id) return Entity_Id;
3581 -- Check whether Obj is a private component of a protected object.
3582 -- Return the protected type where the component resides, Empty
3583 -- otherwise.
3584
3585 function Is_Public_Operation return Boolean;
3586 -- Verify that the enclosing operation is callable from outside the
3587 -- protected object, to minimize false positives.
3588
3589 ------------------------------
3590 -- Enclosing_Protected_Type --
3591 ------------------------------
3592
3593 function Enclosing_Protected_Type (Obj : Node_Id) return Entity_Id is
3594 begin
3595 if Is_Entity_Name (Obj) then
3596 declare
3597 Ent : Entity_Id := Entity (Obj);
3598
3599 begin
3600 -- The object can be a renaming of a private component, use
3601 -- the original record component.
3602
3603 if Is_Prival (Ent) then
3604 Ent := Prival_Link (Ent);
3605 end if;
3606
3607 if Is_Protected_Type (Scope (Ent)) then
3608 return Scope (Ent);
3609 end if;
3610 end;
3611 end if;
3612
3613 -- For indexed and selected components, recursively check the prefix
3614
3615 if Nkind_In (Obj, N_Indexed_Component, N_Selected_Component) then
3616 return Enclosing_Protected_Type (Prefix (Obj));
3617
3618 -- The object does not denote a protected component
3619
3620 else
3621 return Empty;
3622 end if;
3623 end Enclosing_Protected_Type;
3624
3625 -------------------------
3626 -- Is_Public_Operation --
3627 -------------------------
3628
3629 function Is_Public_Operation return Boolean is
3630 S : Entity_Id;
3631 E : Entity_Id;
3632
3633 begin
3634 S := Current_Scope;
3635 while Present (S) and then S /= Pref_Encl_Typ loop
3636 if Scope (S) = Pref_Encl_Typ then
3637 E := First_Entity (Pref_Encl_Typ);
3638 while Present (E)
3639 and then E /= First_Private_Entity (Pref_Encl_Typ)
3640 loop
3641 if E = S then
3642 return True;
3643 end if;
3644
3645 Next_Entity (E);
3646 end loop;
3647 end if;
3648
3649 S := Scope (S);
3650 end loop;
3651
3652 return False;
3653 end Is_Public_Operation;
3654
3655 -- Start of processing for Check_Unprotected_Access
3656
3657 begin
3658 if Nkind (Expr) = N_Attribute_Reference
3659 and then Attribute_Name (Expr) = Name_Unchecked_Access
3660 then
3661 Cont_Encl_Typ := Enclosing_Protected_Type (Context);
3662 Pref_Encl_Typ := Enclosing_Protected_Type (Prefix (Expr));
3663
3664 -- Check whether we are trying to export a protected component to a
3665 -- context with an equal or lower access level.
3666
3667 if Present (Pref_Encl_Typ)
3668 and then No (Cont_Encl_Typ)
3669 and then Is_Public_Operation
3670 and then Scope_Depth (Pref_Encl_Typ) >=
3671 Object_Access_Level (Context)
3672 then
3673 Error_Msg_N
3674 ("??possible unprotected access to protected data", Expr);
3675 end if;
3676 end if;
3677 end Check_Unprotected_Access;
3678
3679 ------------------------------
3680 -- Check_Unused_Body_States --
3681 ------------------------------
3682
3683 procedure Check_Unused_Body_States (Body_Id : Entity_Id) is
3684 procedure Process_Refinement_Clause
3685 (Clause : Node_Id;
3686 States : Elist_Id);
3687 -- Inspect all constituents of refinement clause Clause and remove any
3688 -- matches from body state list States.
3689
3690 procedure Report_Unused_Body_States (States : Elist_Id);
3691 -- Emit errors for each abstract state or object found in list States
3692
3693 -------------------------------
3694 -- Process_Refinement_Clause --
3695 -------------------------------
3696
3697 procedure Process_Refinement_Clause
3698 (Clause : Node_Id;
3699 States : Elist_Id)
3700 is
3701 procedure Process_Constituent (Constit : Node_Id);
3702 -- Remove constituent Constit from body state list States
3703
3704 -------------------------
3705 -- Process_Constituent --
3706 -------------------------
3707
3708 procedure Process_Constituent (Constit : Node_Id) is
3709 Constit_Id : Entity_Id;
3710
3711 begin
3712 -- Guard against illegal constituents. Only abstract states and
3713 -- objects can appear on the right hand side of a refinement.
3714
3715 if Is_Entity_Name (Constit) then
3716 Constit_Id := Entity_Of (Constit);
3717
3718 if Present (Constit_Id)
3719 and then Ekind_In (Constit_Id, E_Abstract_State,
3720 E_Constant,
3721 E_Variable)
3722 then
3723 Remove (States, Constit_Id);
3724 end if;
3725 end if;
3726 end Process_Constituent;
3727
3728 -- Local variables
3729
3730 Constit : Node_Id;
3731
3732 -- Start of processing for Process_Refinement_Clause
3733
3734 begin
3735 if Nkind (Clause) = N_Component_Association then
3736 Constit := Expression (Clause);
3737
3738 -- Multiple constituents appear as an aggregate
3739
3740 if Nkind (Constit) = N_Aggregate then
3741 Constit := First (Expressions (Constit));
3742 while Present (Constit) loop
3743 Process_Constituent (Constit);
3744 Next (Constit);
3745 end loop;
3746
3747 -- Various forms of a single constituent
3748
3749 else
3750 Process_Constituent (Constit);
3751 end if;
3752 end if;
3753 end Process_Refinement_Clause;
3754
3755 -------------------------------
3756 -- Report_Unused_Body_States --
3757 -------------------------------
3758
3759 procedure Report_Unused_Body_States (States : Elist_Id) is
3760 Posted : Boolean := False;
3761 State_Elmt : Elmt_Id;
3762 State_Id : Entity_Id;
3763
3764 begin
3765 if Present (States) then
3766 State_Elmt := First_Elmt (States);
3767 while Present (State_Elmt) loop
3768 State_Id := Node (State_Elmt);
3769
3770 -- Constants are part of the hidden state of a package, but the
3771 -- compiler cannot determine whether they have variable input
3772 -- (SPARK RM 7.1.1(2)) and cannot classify them properly as a
3773 -- hidden state. Do not emit an error when a constant does not
3774 -- participate in a state refinement, even though it acts as a
3775 -- hidden state.
3776
3777 if Ekind (State_Id) = E_Constant then
3778 null;
3779
3780 -- Generate an error message of the form:
3781
3782 -- body of package ... has unused hidden states
3783 -- abstract state ... defined at ...
3784 -- variable ... defined at ...
3785
3786 else
3787 if not Posted then
3788 Posted := True;
3789 SPARK_Msg_N
3790 ("body of package & has unused hidden states", Body_Id);
3791 end if;
3792
3793 Error_Msg_Sloc := Sloc (State_Id);
3794
3795 if Ekind (State_Id) = E_Abstract_State then
3796 SPARK_Msg_NE
3797 ("\abstract state & defined #", Body_Id, State_Id);
3798
3799 else
3800 SPARK_Msg_NE ("\variable & defined #", Body_Id, State_Id);
3801 end if;
3802 end if;
3803
3804 Next_Elmt (State_Elmt);
3805 end loop;
3806 end if;
3807 end Report_Unused_Body_States;
3808
3809 -- Local variables
3810
3811 Prag : constant Node_Id := Get_Pragma (Body_Id, Pragma_Refined_State);
3812 Spec_Id : constant Entity_Id := Spec_Entity (Body_Id);
3813 Clause : Node_Id;
3814 States : Elist_Id;
3815
3816 -- Start of processing for Check_Unused_Body_States
3817
3818 begin
3819 -- Inspect the clauses of pragma Refined_State and determine whether all
3820 -- visible states declared within the package body participate in the
3821 -- refinement.
3822
3823 if Present (Prag) then
3824 Clause := Expression (Get_Argument (Prag, Spec_Id));
3825 States := Collect_Body_States (Body_Id);
3826
3827 -- Multiple non-null state refinements appear as an aggregate
3828
3829 if Nkind (Clause) = N_Aggregate then
3830 Clause := First (Component_Associations (Clause));
3831 while Present (Clause) loop
3832 Process_Refinement_Clause (Clause, States);
3833 Next (Clause);
3834 end loop;
3835
3836 -- Various forms of a single state refinement
3837
3838 else
3839 Process_Refinement_Clause (Clause, States);
3840 end if;
3841
3842 -- Ensure that all abstract states and objects declared in the
3843 -- package body state space are utilized as constituents.
3844
3845 Report_Unused_Body_States (States);
3846 end if;
3847 end Check_Unused_Body_States;
3848
3849 -------------------------
3850 -- Collect_Body_States --
3851 -------------------------
3852
3853 function Collect_Body_States (Body_Id : Entity_Id) return Elist_Id is
3854 function Is_Visible_Object (Obj_Id : Entity_Id) return Boolean;
3855 -- Determine whether object Obj_Id is a suitable visible state of a
3856 -- package body.
3857
3858 procedure Collect_Visible_States
3859 (Pack_Id : Entity_Id;
3860 States : in out Elist_Id);
3861 -- Gather the entities of all abstract states and objects declared in
3862 -- the visible state space of package Pack_Id.
3863
3864 ----------------------------
3865 -- Collect_Visible_States --
3866 ----------------------------
3867
3868 procedure Collect_Visible_States
3869 (Pack_Id : Entity_Id;
3870 States : in out Elist_Id)
3871 is
3872 Item_Id : Entity_Id;
3873
3874 begin
3875 -- Traverse the entity chain of the package and inspect all visible
3876 -- items.
3877
3878 Item_Id := First_Entity (Pack_Id);
3879 while Present (Item_Id) and then not In_Private_Part (Item_Id) loop
3880
3881 -- Do not consider internally generated items as those cannot be
3882 -- named and participate in refinement.
3883
3884 if not Comes_From_Source (Item_Id) then
3885 null;
3886
3887 elsif Ekind (Item_Id) = E_Abstract_State then
3888 Append_New_Elmt (Item_Id, States);
3889
3890 elsif Ekind_In (Item_Id, E_Constant, E_Variable)
3891 and then Is_Visible_Object (Item_Id)
3892 then
3893 Append_New_Elmt (Item_Id, States);
3894
3895 -- Recursively gather the visible states of a nested package
3896
3897 elsif Ekind (Item_Id) = E_Package then
3898 Collect_Visible_States (Item_Id, States);
3899 end if;
3900
3901 Next_Entity (Item_Id);
3902 end loop;
3903 end Collect_Visible_States;
3904
3905 -----------------------
3906 -- Is_Visible_Object --
3907 -----------------------
3908
3909 function Is_Visible_Object (Obj_Id : Entity_Id) return Boolean is
3910 begin
3911 -- Objects that map generic formals to their actuals are not visible
3912 -- from outside the generic instantiation.
3913
3914 if Present (Corresponding_Generic_Association
3915 (Declaration_Node (Obj_Id)))
3916 then
3917 return False;
3918
3919 -- Constituents of a single protected/task type act as components of
3920 -- the type and are not visible from outside the type.
3921
3922 elsif Ekind (Obj_Id) = E_Variable
3923 and then Present (Encapsulating_State (Obj_Id))
3924 and then Is_Single_Concurrent_Object (Encapsulating_State (Obj_Id))
3925 then
3926 return False;
3927
3928 else
3929 return True;
3930 end if;
3931 end Is_Visible_Object;
3932
3933 -- Local variables
3934
3935 Body_Decl : constant Node_Id := Unit_Declaration_Node (Body_Id);
3936 Decl : Node_Id;
3937 Item_Id : Entity_Id;
3938 States : Elist_Id := No_Elist;
3939
3940 -- Start of processing for Collect_Body_States
3941
3942 begin
3943 -- Inspect the declarations of the body looking for source objects,
3944 -- packages and package instantiations. Note that even though this
3945 -- processing is very similar to Collect_Visible_States, a package
3946 -- body does not have a First/Next_Entity list.
3947
3948 Decl := First (Declarations (Body_Decl));
3949 while Present (Decl) loop
3950
3951 -- Capture source objects as internally generated temporaries cannot
3952 -- be named and participate in refinement.
3953
3954 if Nkind (Decl) = N_Object_Declaration then
3955 Item_Id := Defining_Entity (Decl);
3956
3957 if Comes_From_Source (Item_Id)
3958 and then Is_Visible_Object (Item_Id)
3959 then
3960 Append_New_Elmt (Item_Id, States);
3961 end if;
3962
3963 -- Capture the visible abstract states and objects of a source
3964 -- package [instantiation].
3965
3966 elsif Nkind (Decl) = N_Package_Declaration then
3967 Item_Id := Defining_Entity (Decl);
3968
3969 if Comes_From_Source (Item_Id) then
3970 Collect_Visible_States (Item_Id, States);
3971 end if;
3972 end if;
3973
3974 Next (Decl);
3975 end loop;
3976
3977 return States;
3978 end Collect_Body_States;
3979
3980 ------------------------
3981 -- Collect_Interfaces --
3982 ------------------------
3983
3984 procedure Collect_Interfaces
3985 (T : Entity_Id;
3986 Ifaces_List : out Elist_Id;
3987 Exclude_Parents : Boolean := False;
3988 Use_Full_View : Boolean := True)
3989 is
3990 procedure Collect (Typ : Entity_Id);
3991 -- Subsidiary subprogram used to traverse the whole list
3992 -- of directly and indirectly implemented interfaces
3993
3994 -------------
3995 -- Collect --
3996 -------------
3997
3998 procedure Collect (Typ : Entity_Id) is
3999 Ancestor : Entity_Id;
4000 Full_T : Entity_Id;
4001 Id : Node_Id;
4002 Iface : Entity_Id;
4003
4004 begin
4005 Full_T := Typ;
4006
4007 -- Handle private types and subtypes
4008
4009 if Use_Full_View
4010 and then Is_Private_Type (Typ)
4011 and then Present (Full_View (Typ))
4012 then
4013 Full_T := Full_View (Typ);
4014
4015 if Ekind (Full_T) = E_Record_Subtype then
4016 Full_T := Etype (Typ);
4017
4018 if Present (Full_View (Full_T)) then
4019 Full_T := Full_View (Full_T);
4020 end if;
4021 end if;
4022 end if;
4023
4024 -- Include the ancestor if we are generating the whole list of
4025 -- abstract interfaces.
4026
4027 if Etype (Full_T) /= Typ
4028
4029 -- Protect the frontend against wrong sources. For example:
4030
4031 -- package P is
4032 -- type A is tagged null record;
4033 -- type B is new A with private;
4034 -- type C is new A with private;
4035 -- private
4036 -- type B is new C with null record;
4037 -- type C is new B with null record;
4038 -- end P;
4039
4040 and then Etype (Full_T) /= T
4041 then
4042 Ancestor := Etype (Full_T);
4043 Collect (Ancestor);
4044
4045 if Is_Interface (Ancestor) and then not Exclude_Parents then
4046 Append_Unique_Elmt (Ancestor, Ifaces_List);
4047 end if;
4048 end if;
4049
4050 -- Traverse the graph of ancestor interfaces
4051
4052 if Is_Non_Empty_List (Abstract_Interface_List (Full_T)) then
4053 Id := First (Abstract_Interface_List (Full_T));
4054 while Present (Id) loop
4055 Iface := Etype (Id);
4056
4057 -- Protect against wrong uses. For example:
4058 -- type I is interface;
4059 -- type O is tagged null record;
4060 -- type Wrong is new I and O with null record; -- ERROR
4061
4062 if Is_Interface (Iface) then
4063 if Exclude_Parents
4064 and then Etype (T) /= T
4065 and then Interface_Present_In_Ancestor (Etype (T), Iface)
4066 then
4067 null;
4068 else
4069 Collect (Iface);
4070 Append_Unique_Elmt (Iface, Ifaces_List);
4071 end if;
4072 end if;
4073
4074 Next (Id);
4075 end loop;
4076 end if;
4077 end Collect;
4078
4079 -- Start of processing for Collect_Interfaces
4080
4081 begin
4082 pragma Assert (Is_Tagged_Type (T) or else Is_Concurrent_Type (T));
4083 Ifaces_List := New_Elmt_List;
4084 Collect (T);
4085 end Collect_Interfaces;
4086
4087 ----------------------------------
4088 -- Collect_Interface_Components --
4089 ----------------------------------
4090
4091 procedure Collect_Interface_Components
4092 (Tagged_Type : Entity_Id;
4093 Components_List : out Elist_Id)
4094 is
4095 procedure Collect (Typ : Entity_Id);
4096 -- Subsidiary subprogram used to climb to the parents
4097
4098 -------------
4099 -- Collect --
4100 -------------
4101
4102 procedure Collect (Typ : Entity_Id) is
4103 Tag_Comp : Entity_Id;
4104 Parent_Typ : Entity_Id;
4105
4106 begin
4107 -- Handle private types
4108
4109 if Present (Full_View (Etype (Typ))) then
4110 Parent_Typ := Full_View (Etype (Typ));
4111 else
4112 Parent_Typ := Etype (Typ);
4113 end if;
4114
4115 if Parent_Typ /= Typ
4116
4117 -- Protect the frontend against wrong sources. For example:
4118
4119 -- package P is
4120 -- type A is tagged null record;
4121 -- type B is new A with private;
4122 -- type C is new A with private;
4123 -- private
4124 -- type B is new C with null record;
4125 -- type C is new B with null record;
4126 -- end P;
4127
4128 and then Parent_Typ /= Tagged_Type
4129 then
4130 Collect (Parent_Typ);
4131 end if;
4132
4133 -- Collect the components containing tags of secondary dispatch
4134 -- tables.
4135
4136 Tag_Comp := Next_Tag_Component (First_Tag_Component (Typ));
4137 while Present (Tag_Comp) loop
4138 pragma Assert (Present (Related_Type (Tag_Comp)));
4139 Append_Elmt (Tag_Comp, Components_List);
4140
4141 Tag_Comp := Next_Tag_Component (Tag_Comp);
4142 end loop;
4143 end Collect;
4144
4145 -- Start of processing for Collect_Interface_Components
4146
4147 begin
4148 pragma Assert (Ekind (Tagged_Type) = E_Record_Type
4149 and then Is_Tagged_Type (Tagged_Type));
4150
4151 Components_List := New_Elmt_List;
4152 Collect (Tagged_Type);
4153 end Collect_Interface_Components;
4154
4155 -----------------------------
4156 -- Collect_Interfaces_Info --
4157 -----------------------------
4158
4159 procedure Collect_Interfaces_Info
4160 (T : Entity_Id;
4161 Ifaces_List : out Elist_Id;
4162 Components_List : out Elist_Id;
4163 Tags_List : out Elist_Id)
4164 is
4165 Comps_List : Elist_Id;
4166 Comp_Elmt : Elmt_Id;
4167 Comp_Iface : Entity_Id;
4168 Iface_Elmt : Elmt_Id;
4169 Iface : Entity_Id;
4170
4171 function Search_Tag (Iface : Entity_Id) return Entity_Id;
4172 -- Search for the secondary tag associated with the interface type
4173 -- Iface that is implemented by T.
4174
4175 ----------------
4176 -- Search_Tag --
4177 ----------------
4178
4179 function Search_Tag (Iface : Entity_Id) return Entity_Id is
4180 ADT : Elmt_Id;
4181 begin
4182 if not Is_CPP_Class (T) then
4183 ADT := Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (T))));
4184 else
4185 ADT := Next_Elmt (First_Elmt (Access_Disp_Table (T)));
4186 end if;
4187
4188 while Present (ADT)
4189 and then Is_Tag (Node (ADT))
4190 and then Related_Type (Node (ADT)) /= Iface
4191 loop
4192 -- Skip secondary dispatch table referencing thunks to user
4193 -- defined primitives covered by this interface.
4194
4195 pragma Assert (Has_Suffix (Node (ADT), 'P'));
4196 Next_Elmt (ADT);
4197
4198 -- Skip secondary dispatch tables of Ada types
4199
4200 if not Is_CPP_Class (T) then
4201
4202 -- Skip secondary dispatch table referencing thunks to
4203 -- predefined primitives.
4204
4205 pragma Assert (Has_Suffix (Node (ADT), 'Y'));
4206 Next_Elmt (ADT);
4207
4208 -- Skip secondary dispatch table referencing user-defined
4209 -- primitives covered by this interface.
4210
4211 pragma Assert (Has_Suffix (Node (ADT), 'D'));
4212 Next_Elmt (ADT);
4213
4214 -- Skip secondary dispatch table referencing predefined
4215 -- primitives.
4216
4217 pragma Assert (Has_Suffix (Node (ADT), 'Z'));
4218 Next_Elmt (ADT);
4219 end if;
4220 end loop;
4221
4222 pragma Assert (Is_Tag (Node (ADT)));
4223 return Node (ADT);
4224 end Search_Tag;
4225
4226 -- Start of processing for Collect_Interfaces_Info
4227
4228 begin
4229 Collect_Interfaces (T, Ifaces_List);
4230 Collect_Interface_Components (T, Comps_List);
4231
4232 -- Search for the record component and tag associated with each
4233 -- interface type of T.
4234
4235 Components_List := New_Elmt_List;
4236 Tags_List := New_Elmt_List;
4237
4238 Iface_Elmt := First_Elmt (Ifaces_List);
4239 while Present (Iface_Elmt) loop
4240 Iface := Node (Iface_Elmt);
4241
4242 -- Associate the primary tag component and the primary dispatch table
4243 -- with all the interfaces that are parents of T
4244
4245 if Is_Ancestor (Iface, T, Use_Full_View => True) then
4246 Append_Elmt (First_Tag_Component (T), Components_List);
4247 Append_Elmt (Node (First_Elmt (Access_Disp_Table (T))), Tags_List);
4248
4249 -- Otherwise search for the tag component and secondary dispatch
4250 -- table of Iface
4251
4252 else
4253 Comp_Elmt := First_Elmt (Comps_List);
4254 while Present (Comp_Elmt) loop
4255 Comp_Iface := Related_Type (Node (Comp_Elmt));
4256
4257 if Comp_Iface = Iface
4258 or else Is_Ancestor (Iface, Comp_Iface, Use_Full_View => True)
4259 then
4260 Append_Elmt (Node (Comp_Elmt), Components_List);
4261 Append_Elmt (Search_Tag (Comp_Iface), Tags_List);
4262 exit;
4263 end if;
4264
4265 Next_Elmt (Comp_Elmt);
4266 end loop;
4267 pragma Assert (Present (Comp_Elmt));
4268 end if;
4269
4270 Next_Elmt (Iface_Elmt);
4271 end loop;
4272 end Collect_Interfaces_Info;
4273
4274 ---------------------
4275 -- Collect_Parents --
4276 ---------------------
4277
4278 procedure Collect_Parents
4279 (T : Entity_Id;
4280 List : out Elist_Id;
4281 Use_Full_View : Boolean := True)
4282 is
4283 Current_Typ : Entity_Id := T;
4284 Parent_Typ : Entity_Id;
4285
4286 begin
4287 List := New_Elmt_List;
4288
4289 -- No action if the if the type has no parents
4290
4291 if T = Etype (T) then
4292 return;
4293 end if;
4294
4295 loop
4296 Parent_Typ := Etype (Current_Typ);
4297
4298 if Is_Private_Type (Parent_Typ)
4299 and then Present (Full_View (Parent_Typ))
4300 and then Use_Full_View
4301 then
4302 Parent_Typ := Full_View (Base_Type (Parent_Typ));
4303 end if;
4304
4305 Append_Elmt (Parent_Typ, List);
4306
4307 exit when Parent_Typ = Current_Typ;
4308 Current_Typ := Parent_Typ;
4309 end loop;
4310 end Collect_Parents;
4311
4312 ----------------------------------
4313 -- Collect_Primitive_Operations --
4314 ----------------------------------
4315
4316 function Collect_Primitive_Operations (T : Entity_Id) return Elist_Id is
4317 B_Type : constant Entity_Id := Base_Type (T);
4318 B_Decl : constant Node_Id := Original_Node (Parent (B_Type));
4319 B_Scope : Entity_Id := Scope (B_Type);
4320 Op_List : Elist_Id;
4321 Formal : Entity_Id;
4322 Is_Prim : Boolean;
4323 Is_Type_In_Pkg : Boolean;
4324 Formal_Derived : Boolean := False;
4325 Id : Entity_Id;
4326
4327 function Match (E : Entity_Id) return Boolean;
4328 -- True if E's base type is B_Type, or E is of an anonymous access type
4329 -- and the base type of its designated type is B_Type.
4330
4331 -----------
4332 -- Match --
4333 -----------
4334
4335 function Match (E : Entity_Id) return Boolean is
4336 Etyp : Entity_Id := Etype (E);
4337
4338 begin
4339 if Ekind (Etyp) = E_Anonymous_Access_Type then
4340 Etyp := Designated_Type (Etyp);
4341 end if;
4342
4343 -- In Ada 2012 a primitive operation may have a formal of an
4344 -- incomplete view of the parent type.
4345
4346 return Base_Type (Etyp) = B_Type
4347 or else
4348 (Ada_Version >= Ada_2012
4349 and then Ekind (Etyp) = E_Incomplete_Type
4350 and then Full_View (Etyp) = B_Type);
4351 end Match;
4352
4353 -- Start of processing for Collect_Primitive_Operations
4354
4355 begin
4356 -- For tagged types, the primitive operations are collected as they
4357 -- are declared, and held in an explicit list which is simply returned.
4358
4359 if Is_Tagged_Type (B_Type) then
4360 return Primitive_Operations (B_Type);
4361
4362 -- An untagged generic type that is a derived type inherits the
4363 -- primitive operations of its parent type. Other formal types only
4364 -- have predefined operators, which are not explicitly represented.
4365
4366 elsif Is_Generic_Type (B_Type) then
4367 if Nkind (B_Decl) = N_Formal_Type_Declaration
4368 and then Nkind (Formal_Type_Definition (B_Decl)) =
4369 N_Formal_Derived_Type_Definition
4370 then
4371 Formal_Derived := True;
4372 else
4373 return New_Elmt_List;
4374 end if;
4375 end if;
4376
4377 Op_List := New_Elmt_List;
4378
4379 if B_Scope = Standard_Standard then
4380 if B_Type = Standard_String then
4381 Append_Elmt (Standard_Op_Concat, Op_List);
4382
4383 elsif B_Type = Standard_Wide_String then
4384 Append_Elmt (Standard_Op_Concatw, Op_List);
4385
4386 else
4387 null;
4388 end if;
4389
4390 -- Locate the primitive subprograms of the type
4391
4392 else
4393 -- The primitive operations appear after the base type, except
4394 -- if the derivation happens within the private part of B_Scope
4395 -- and the type is a private type, in which case both the type
4396 -- and some primitive operations may appear before the base
4397 -- type, and the list of candidates starts after the type.
4398
4399 if In_Open_Scopes (B_Scope)
4400 and then Scope (T) = B_Scope
4401 and then In_Private_Part (B_Scope)
4402 then
4403 Id := Next_Entity (T);
4404
4405 -- In Ada 2012, If the type has an incomplete partial view, there
4406 -- may be primitive operations declared before the full view, so
4407 -- we need to start scanning from the incomplete view, which is
4408 -- earlier on the entity chain.
4409
4410 elsif Nkind (Parent (B_Type)) = N_Full_Type_Declaration
4411 and then Present (Incomplete_View (Parent (B_Type)))
4412 then
4413 Id := Defining_Entity (Incomplete_View (Parent (B_Type)));
4414
4415 -- If T is a derived from a type with an incomplete view declared
4416 -- elsewhere, that incomplete view is irrelevant, we want the
4417 -- operations in the scope of T.
4418
4419 if Scope (Id) /= Scope (B_Type) then
4420 Id := Next_Entity (B_Type);
4421 end if;
4422
4423 else
4424 Id := Next_Entity (B_Type);
4425 end if;
4426
4427 -- Set flag if this is a type in a package spec
4428
4429 Is_Type_In_Pkg :=
4430 Is_Package_Or_Generic_Package (B_Scope)
4431 and then
4432 Nkind (Parent (Declaration_Node (First_Subtype (T)))) /=
4433 N_Package_Body;
4434
4435 while Present (Id) loop
4436
4437 -- Test whether the result type or any of the parameter types of
4438 -- each subprogram following the type match that type when the
4439 -- type is declared in a package spec, is a derived type, or the
4440 -- subprogram is marked as primitive. (The Is_Primitive test is
4441 -- needed to find primitives of nonderived types in declarative
4442 -- parts that happen to override the predefined "=" operator.)
4443
4444 -- Note that generic formal subprograms are not considered to be
4445 -- primitive operations and thus are never inherited.
4446
4447 if Is_Overloadable (Id)
4448 and then (Is_Type_In_Pkg
4449 or else Is_Derived_Type (B_Type)
4450 or else Is_Primitive (Id))
4451 and then Nkind (Parent (Parent (Id)))
4452 not in N_Formal_Subprogram_Declaration
4453 then
4454 Is_Prim := False;
4455
4456 if Match (Id) then
4457 Is_Prim := True;
4458
4459 else
4460 Formal := First_Formal (Id);
4461 while Present (Formal) loop
4462 if Match (Formal) then
4463 Is_Prim := True;
4464 exit;
4465 end if;
4466
4467 Next_Formal (Formal);
4468 end loop;
4469 end if;
4470
4471 -- For a formal derived type, the only primitives are the ones
4472 -- inherited from the parent type. Operations appearing in the
4473 -- package declaration are not primitive for it.
4474
4475 if Is_Prim
4476 and then (not Formal_Derived or else Present (Alias (Id)))
4477 then
4478 -- In the special case of an equality operator aliased to
4479 -- an overriding dispatching equality belonging to the same
4480 -- type, we don't include it in the list of primitives.
4481 -- This avoids inheriting multiple equality operators when
4482 -- deriving from untagged private types whose full type is
4483 -- tagged, which can otherwise cause ambiguities. Note that
4484 -- this should only happen for this kind of untagged parent
4485 -- type, since normally dispatching operations are inherited
4486 -- using the type's Primitive_Operations list.
4487
4488 if Chars (Id) = Name_Op_Eq
4489 and then Is_Dispatching_Operation (Id)
4490 and then Present (Alias (Id))
4491 and then Present (Overridden_Operation (Alias (Id)))
4492 and then Base_Type (Etype (First_Entity (Id))) =
4493 Base_Type (Etype (First_Entity (Alias (Id))))
4494 then
4495 null;
4496
4497 -- Include the subprogram in the list of primitives
4498
4499 else
4500 Append_Elmt (Id, Op_List);
4501 end if;
4502 end if;
4503 end if;
4504
4505 Next_Entity (Id);
4506
4507 -- For a type declared in System, some of its operations may
4508 -- appear in the target-specific extension to System.
4509
4510 if No (Id)
4511 and then B_Scope = RTU_Entity (System)
4512 and then Present_System_Aux
4513 then
4514 B_Scope := System_Aux_Id;
4515 Id := First_Entity (System_Aux_Id);
4516 end if;
4517 end loop;
4518 end if;
4519
4520 return Op_List;
4521 end Collect_Primitive_Operations;
4522
4523 -----------------------------------
4524 -- Compile_Time_Constraint_Error --
4525 -----------------------------------
4526
4527 function Compile_Time_Constraint_Error
4528 (N : Node_Id;
4529 Msg : String;
4530 Ent : Entity_Id := Empty;
4531 Loc : Source_Ptr := No_Location;
4532 Warn : Boolean := False) return Node_Id
4533 is
4534 Msgc : String (1 .. Msg'Length + 3);
4535 -- Copy of message, with room for possible ?? or << and ! at end
4536
4537 Msgl : Natural;
4538 Wmsg : Boolean;
4539 Eloc : Source_Ptr;
4540
4541 -- Start of processing for Compile_Time_Constraint_Error
4542
4543 begin
4544 -- If this is a warning, convert it into an error if we are in code
4545 -- subject to SPARK_Mode being set On, unless Warn is True to force a
4546 -- warning. The rationale is that a compile-time constraint error should
4547 -- lead to an error instead of a warning when SPARK_Mode is On, but in
4548 -- a few cases we prefer to issue a warning and generate both a suitable
4549 -- run-time error in GNAT and a suitable check message in GNATprove.
4550 -- Those cases are those that likely correspond to deactivated SPARK
4551 -- code, so that this kind of code can be compiled and analyzed instead
4552 -- of being rejected.
4553
4554 Error_Msg_Warn := Warn or SPARK_Mode /= On;
4555
4556 -- A static constraint error in an instance body is not a fatal error.
4557 -- we choose to inhibit the message altogether, because there is no
4558 -- obvious node (for now) on which to post it. On the other hand the
4559 -- offending node must be replaced with a constraint_error in any case.
4560
4561 -- No messages are generated if we already posted an error on this node
4562
4563 if not Error_Posted (N) then
4564 if Loc /= No_Location then
4565 Eloc := Loc;
4566 else
4567 Eloc := Sloc (N);
4568 end if;
4569
4570 -- Copy message to Msgc, converting any ? in the message into
4571 -- < instead, so that we have an error in GNATprove mode.
4572
4573 Msgl := Msg'Length;
4574
4575 for J in 1 .. Msgl loop
4576 if Msg (J) = '?' and then (J = 1 or else Msg (J - 1) /= ''') then
4577 Msgc (J) := '<';
4578 else
4579 Msgc (J) := Msg (J);
4580 end if;
4581 end loop;
4582
4583 -- Message is a warning, even in Ada 95 case
4584
4585 if Msg (Msg'Last) = '?' or else Msg (Msg'Last) = '<' then
4586 Wmsg := True;
4587
4588 -- In Ada 83, all messages are warnings. In the private part and
4589 -- the body of an instance, constraint_checks are only warnings.
4590 -- We also make this a warning if the Warn parameter is set.
4591
4592 elsif Warn
4593 or else (Ada_Version = Ada_83 and then Comes_From_Source (N))
4594 then
4595 Msgl := Msgl + 1;
4596 Msgc (Msgl) := '<';
4597 Msgl := Msgl + 1;
4598 Msgc (Msgl) := '<';
4599 Wmsg := True;
4600
4601 elsif In_Instance_Not_Visible then
4602 Msgl := Msgl + 1;
4603 Msgc (Msgl) := '<';
4604 Msgl := Msgl + 1;
4605 Msgc (Msgl) := '<';
4606 Wmsg := True;
4607
4608 -- Otherwise we have a real error message (Ada 95 static case)
4609 -- and we make this an unconditional message. Note that in the
4610 -- warning case we do not make the message unconditional, it seems
4611 -- quite reasonable to delete messages like this (about exceptions
4612 -- that will be raised) in dead code.
4613
4614 else
4615 Wmsg := False;
4616 Msgl := Msgl + 1;
4617 Msgc (Msgl) := '!';
4618 end if;
4619
4620 -- One more test, skip the warning if the related expression is
4621 -- statically unevaluated, since we don't want to warn about what
4622 -- will happen when something is evaluated if it never will be
4623 -- evaluated.
4624
4625 if not Is_Statically_Unevaluated (N) then
4626 if Present (Ent) then
4627 Error_Msg_NEL (Msgc (1 .. Msgl), N, Ent, Eloc);
4628 else
4629 Error_Msg_NEL (Msgc (1 .. Msgl), N, Etype (N), Eloc);
4630 end if;
4631
4632 if Wmsg then
4633
4634 -- Check whether the context is an Init_Proc
4635
4636 if Inside_Init_Proc then
4637 declare
4638 Conc_Typ : constant Entity_Id :=
4639 Corresponding_Concurrent_Type
4640 (Entity (Parameter_Type (First
4641 (Parameter_Specifications
4642 (Parent (Current_Scope))))));
4643
4644 begin
4645 -- Don't complain if the corresponding concurrent type
4646 -- doesn't come from source (i.e. a single task/protected
4647 -- object).
4648
4649 if Present (Conc_Typ)
4650 and then not Comes_From_Source (Conc_Typ)
4651 then
4652 Error_Msg_NEL
4653 ("\& [<<", N, Standard_Constraint_Error, Eloc);
4654
4655 else
4656 if GNATprove_Mode then
4657 Error_Msg_NEL
4658 ("\& would have been raised for objects of this "
4659 & "type", N, Standard_Constraint_Error, Eloc);
4660 else
4661 Error_Msg_NEL
4662 ("\& will be raised for objects of this type??",
4663 N, Standard_Constraint_Error, Eloc);
4664 end if;
4665 end if;
4666 end;
4667
4668 else
4669 Error_Msg_NEL ("\& [<<", N, Standard_Constraint_Error, Eloc);
4670 end if;
4671
4672 else
4673 Error_Msg ("\static expression fails Constraint_Check", Eloc);
4674 Set_Error_Posted (N);
4675 end if;
4676 end if;
4677 end if;
4678
4679 return N;
4680 end Compile_Time_Constraint_Error;
4681
4682 -----------------------
4683 -- Conditional_Delay --
4684 -----------------------
4685
4686 procedure Conditional_Delay (New_Ent, Old_Ent : Entity_Id) is
4687 begin
4688 if Has_Delayed_Freeze (Old_Ent) and then not Is_Frozen (Old_Ent) then
4689 Set_Has_Delayed_Freeze (New_Ent);
4690 end if;
4691 end Conditional_Delay;
4692
4693 ----------------------------
4694 -- Contains_Refined_State --
4695 ----------------------------
4696
4697 function Contains_Refined_State (Prag : Node_Id) return Boolean is
4698 function Has_State_In_Dependency (List : Node_Id) return Boolean;
4699 -- Determine whether a dependency list mentions a state with a visible
4700 -- refinement.
4701
4702 function Has_State_In_Global (List : Node_Id) return Boolean;
4703 -- Determine whether a global list mentions a state with a visible
4704 -- refinement.
4705
4706 function Is_Refined_State (Item : Node_Id) return Boolean;
4707 -- Determine whether Item is a reference to an abstract state with a
4708 -- visible refinement.
4709
4710 -----------------------------
4711 -- Has_State_In_Dependency --
4712 -----------------------------
4713
4714 function Has_State_In_Dependency (List : Node_Id) return Boolean is
4715 Clause : Node_Id;
4716 Output : Node_Id;
4717
4718 begin
4719 -- A null dependency list does not mention any states
4720
4721 if Nkind (List) = N_Null then
4722 return False;
4723
4724 -- Dependency clauses appear as component associations of an
4725 -- aggregate.
4726
4727 elsif Nkind (List) = N_Aggregate
4728 and then Present (Component_Associations (List))
4729 then
4730 Clause := First (Component_Associations (List));
4731 while Present (Clause) loop
4732
4733 -- Inspect the outputs of a dependency clause
4734
4735 Output := First (Choices (Clause));
4736 while Present (Output) loop
4737 if Is_Refined_State (Output) then
4738 return True;
4739 end if;
4740
4741 Next (Output);
4742 end loop;
4743
4744 -- Inspect the outputs of a dependency clause
4745
4746 if Is_Refined_State (Expression (Clause)) then
4747 return True;
4748 end if;
4749
4750 Next (Clause);
4751 end loop;
4752
4753 -- If we get here, then none of the dependency clauses mention a
4754 -- state with visible refinement.
4755
4756 return False;
4757
4758 -- An illegal pragma managed to sneak in
4759
4760 else
4761 raise Program_Error;
4762 end if;
4763 end Has_State_In_Dependency;
4764
4765 -------------------------
4766 -- Has_State_In_Global --
4767 -------------------------
4768
4769 function Has_State_In_Global (List : Node_Id) return Boolean is
4770 Item : Node_Id;
4771
4772 begin
4773 -- A null global list does not mention any states
4774
4775 if Nkind (List) = N_Null then
4776 return False;
4777
4778 -- Simple global list or moded global list declaration
4779
4780 elsif Nkind (List) = N_Aggregate then
4781
4782 -- The declaration of a simple global list appear as a collection
4783 -- of expressions.
4784
4785 if Present (Expressions (List)) then
4786 Item := First (Expressions (List));
4787 while Present (Item) loop
4788 if Is_Refined_State (Item) then
4789 return True;
4790 end if;
4791
4792 Next (Item);
4793 end loop;
4794
4795 -- The declaration of a moded global list appears as a collection
4796 -- of component associations where individual choices denote
4797 -- modes.
4798
4799 else
4800 Item := First (Component_Associations (List));
4801 while Present (Item) loop
4802 if Has_State_In_Global (Expression (Item)) then
4803 return True;
4804 end if;
4805
4806 Next (Item);
4807 end loop;
4808 end if;
4809
4810 -- If we get here, then the simple/moded global list did not
4811 -- mention any states with a visible refinement.
4812
4813 return False;
4814
4815 -- Single global item declaration
4816
4817 elsif Is_Entity_Name (List) then
4818 return Is_Refined_State (List);
4819
4820 -- An illegal pragma managed to sneak in
4821
4822 else
4823 raise Program_Error;
4824 end if;
4825 end Has_State_In_Global;
4826
4827 ----------------------
4828 -- Is_Refined_State --
4829 ----------------------
4830
4831 function Is_Refined_State (Item : Node_Id) return Boolean is
4832 Elmt : Node_Id;
4833 Item_Id : Entity_Id;
4834
4835 begin
4836 if Nkind (Item) = N_Null then
4837 return False;
4838
4839 -- States cannot be subject to attribute 'Result. This case arises
4840 -- in dependency relations.
4841
4842 elsif Nkind (Item) = N_Attribute_Reference
4843 and then Attribute_Name (Item) = Name_Result
4844 then
4845 return False;
4846
4847 -- Multiple items appear as an aggregate. This case arises in
4848 -- dependency relations.
4849
4850 elsif Nkind (Item) = N_Aggregate
4851 and then Present (Expressions (Item))
4852 then
4853 Elmt := First (Expressions (Item));
4854 while Present (Elmt) loop
4855 if Is_Refined_State (Elmt) then
4856 return True;
4857 end if;
4858
4859 Next (Elmt);
4860 end loop;
4861
4862 -- If we get here, then none of the inputs or outputs reference a
4863 -- state with visible refinement.
4864
4865 return False;
4866
4867 -- Single item
4868
4869 else
4870 Item_Id := Entity_Of (Item);
4871
4872 return
4873 Present (Item_Id)
4874 and then Ekind (Item_Id) = E_Abstract_State
4875 and then Has_Visible_Refinement (Item_Id);
4876 end if;
4877 end Is_Refined_State;
4878
4879 -- Local variables
4880
4881 Arg : constant Node_Id :=
4882 Get_Pragma_Arg (First (Pragma_Argument_Associations (Prag)));
4883 Nam : constant Name_Id := Pragma_Name (Prag);
4884
4885 -- Start of processing for Contains_Refined_State
4886
4887 begin
4888 if Nam = Name_Depends then
4889 return Has_State_In_Dependency (Arg);
4890
4891 else pragma Assert (Nam = Name_Global);
4892 return Has_State_In_Global (Arg);
4893 end if;
4894 end Contains_Refined_State;
4895
4896 -------------------------
4897 -- Copy_Component_List --
4898 -------------------------
4899
4900 function Copy_Component_List
4901 (R_Typ : Entity_Id;
4902 Loc : Source_Ptr) return List_Id
4903 is
4904 Comp : Node_Id;
4905 Comps : constant List_Id := New_List;
4906
4907 begin
4908 Comp := First_Component (Underlying_Type (R_Typ));
4909 while Present (Comp) loop
4910 if Comes_From_Source (Comp) then
4911 declare
4912 Comp_Decl : constant Node_Id := Declaration_Node (Comp);
4913 begin
4914 Append_To (Comps,
4915 Make_Component_Declaration (Loc,
4916 Defining_Identifier =>
4917 Make_Defining_Identifier (Loc, Chars (Comp)),
4918 Component_Definition =>
4919 New_Copy_Tree
4920 (Component_Definition (Comp_Decl), New_Sloc => Loc)));
4921 end;
4922 end if;
4923
4924 Next_Component (Comp);
4925 end loop;
4926
4927 return Comps;
4928 end Copy_Component_List;
4929
4930 -------------------------
4931 -- Copy_Parameter_List --
4932 -------------------------
4933
4934 function Copy_Parameter_List (Subp_Id : Entity_Id) return List_Id is
4935 Loc : constant Source_Ptr := Sloc (Subp_Id);
4936 Plist : List_Id;
4937 Formal : Entity_Id;
4938
4939 begin
4940 if No (First_Formal (Subp_Id)) then
4941 return No_List;
4942 else
4943 Plist := New_List;
4944 Formal := First_Formal (Subp_Id);
4945 while Present (Formal) loop
4946 Append_To (Plist,
4947 Make_Parameter_Specification (Loc,
4948 Defining_Identifier =>
4949 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)),
4950 In_Present => In_Present (Parent (Formal)),
4951 Out_Present => Out_Present (Parent (Formal)),
4952 Parameter_Type =>
4953 New_Occurrence_Of (Etype (Formal), Loc),
4954 Expression =>
4955 New_Copy_Tree (Expression (Parent (Formal)))));
4956
4957 Next_Formal (Formal);
4958 end loop;
4959 end if;
4960
4961 return Plist;
4962 end Copy_Parameter_List;
4963
4964 --------------------------
4965 -- Copy_Subprogram_Spec --
4966 --------------------------
4967
4968 function Copy_Subprogram_Spec (Spec : Node_Id) return Node_Id is
4969 Def_Id : Node_Id;
4970 Formal_Spec : Node_Id;
4971 Result : Node_Id;
4972
4973 begin
4974 -- The structure of the original tree must be replicated without any
4975 -- alterations. Use New_Copy_Tree for this purpose.
4976
4977 Result := New_Copy_Tree (Spec);
4978
4979 -- Create a new entity for the defining unit name
4980
4981 Def_Id := Defining_Unit_Name (Result);
4982 Set_Defining_Unit_Name (Result,
4983 Make_Defining_Identifier (Sloc (Def_Id), Chars (Def_Id)));
4984
4985 -- Create new entities for the formal parameters
4986
4987 if Present (Parameter_Specifications (Result)) then
4988 Formal_Spec := First (Parameter_Specifications (Result));
4989 while Present (Formal_Spec) loop
4990 Def_Id := Defining_Identifier (Formal_Spec);
4991 Set_Defining_Identifier (Formal_Spec,
4992 Make_Defining_Identifier (Sloc (Def_Id), Chars (Def_Id)));
4993
4994 Next (Formal_Spec);
4995 end loop;
4996 end if;
4997
4998 return Result;
4999 end Copy_Subprogram_Spec;
5000
5001 --------------------------------
5002 -- Corresponding_Generic_Type --
5003 --------------------------------
5004
5005 function Corresponding_Generic_Type (T : Entity_Id) return Entity_Id is
5006 Inst : Entity_Id;
5007 Gen : Entity_Id;
5008 Typ : Entity_Id;
5009
5010 begin
5011 if not Is_Generic_Actual_Type (T) then
5012 return Any_Type;
5013
5014 -- If the actual is the actual of an enclosing instance, resolution
5015 -- was correct in the generic.
5016
5017 elsif Nkind (Parent (T)) = N_Subtype_Declaration
5018 and then Is_Entity_Name (Subtype_Indication (Parent (T)))
5019 and then
5020 Is_Generic_Actual_Type (Entity (Subtype_Indication (Parent (T))))
5021 then
5022 return Any_Type;
5023
5024 else
5025 Inst := Scope (T);
5026
5027 if Is_Wrapper_Package (Inst) then
5028 Inst := Related_Instance (Inst);
5029 end if;
5030
5031 Gen :=
5032 Generic_Parent
5033 (Specification (Unit_Declaration_Node (Inst)));
5034
5035 -- Generic actual has the same name as the corresponding formal
5036
5037 Typ := First_Entity (Gen);
5038 while Present (Typ) loop
5039 if Chars (Typ) = Chars (T) then
5040 return Typ;
5041 end if;
5042
5043 Next_Entity (Typ);
5044 end loop;
5045
5046 return Any_Type;
5047 end if;
5048 end Corresponding_Generic_Type;
5049
5050 --------------------
5051 -- Current_Entity --
5052 --------------------
5053
5054 -- The currently visible definition for a given identifier is the
5055 -- one most chained at the start of the visibility chain, i.e. the
5056 -- one that is referenced by the Node_Id value of the name of the
5057 -- given identifier.
5058
5059 function Current_Entity (N : Node_Id) return Entity_Id is
5060 begin
5061 return Get_Name_Entity_Id (Chars (N));
5062 end Current_Entity;
5063
5064 -----------------------------
5065 -- Current_Entity_In_Scope --
5066 -----------------------------
5067
5068 function Current_Entity_In_Scope (N : Node_Id) return Entity_Id is
5069 E : Entity_Id;
5070 CS : constant Entity_Id := Current_Scope;
5071
5072 Transient_Case : constant Boolean := Scope_Is_Transient;
5073
5074 begin
5075 E := Get_Name_Entity_Id (Chars (N));
5076 while Present (E)
5077 and then Scope (E) /= CS
5078 and then (not Transient_Case or else Scope (E) /= Scope (CS))
5079 loop
5080 E := Homonym (E);
5081 end loop;
5082
5083 return E;
5084 end Current_Entity_In_Scope;
5085
5086 -------------------
5087 -- Current_Scope --
5088 -------------------
5089
5090 function Current_Scope return Entity_Id is
5091 begin
5092 if Scope_Stack.Last = -1 then
5093 return Standard_Standard;
5094 else
5095 declare
5096 C : constant Entity_Id :=
5097 Scope_Stack.Table (Scope_Stack.Last).Entity;
5098 begin
5099 if Present (C) then
5100 return C;
5101 else
5102 return Standard_Standard;
5103 end if;
5104 end;
5105 end if;
5106 end Current_Scope;
5107
5108 ----------------------------
5109 -- Current_Scope_No_Loops --
5110 ----------------------------
5111
5112 function Current_Scope_No_Loops return Entity_Id is
5113 S : Entity_Id;
5114
5115 begin
5116 -- Examine the scope stack starting from the current scope and skip any
5117 -- internally generated loops.
5118
5119 S := Current_Scope;
5120 while Present (S) and then S /= Standard_Standard loop
5121 if Ekind (S) = E_Loop and then not Comes_From_Source (S) then
5122 S := Scope (S);
5123 else
5124 exit;
5125 end if;
5126 end loop;
5127
5128 return S;
5129 end Current_Scope_No_Loops;
5130
5131 ------------------------
5132 -- Current_Subprogram --
5133 ------------------------
5134
5135 function Current_Subprogram return Entity_Id is
5136 Scop : constant Entity_Id := Current_Scope;
5137 begin
5138 if Is_Subprogram_Or_Generic_Subprogram (Scop) then
5139 return Scop;
5140 else
5141 return Enclosing_Subprogram (Scop);
5142 end if;
5143 end Current_Subprogram;
5144
5145 ----------------------------------
5146 -- Deepest_Type_Access_Level --
5147 ----------------------------------
5148
5149 function Deepest_Type_Access_Level (Typ : Entity_Id) return Uint is
5150 begin
5151 if Ekind (Typ) = E_Anonymous_Access_Type
5152 and then not Is_Local_Anonymous_Access (Typ)
5153 and then Nkind (Associated_Node_For_Itype (Typ)) = N_Object_Declaration
5154 then
5155 -- Typ is the type of an Ada 2012 stand-alone object of an anonymous
5156 -- access type.
5157
5158 return
5159 Scope_Depth (Enclosing_Dynamic_Scope
5160 (Defining_Identifier
5161 (Associated_Node_For_Itype (Typ))));
5162
5163 -- For generic formal type, return Int'Last (infinite).
5164 -- See comment preceding Is_Generic_Type call in Type_Access_Level.
5165
5166 elsif Is_Generic_Type (Root_Type (Typ)) then
5167 return UI_From_Int (Int'Last);
5168
5169 else
5170 return Type_Access_Level (Typ);
5171 end if;
5172 end Deepest_Type_Access_Level;
5173
5174 ---------------------
5175 -- Defining_Entity --
5176 ---------------------
5177
5178 function Defining_Entity
5179 (N : Node_Id;
5180 Empty_On_Errors : Boolean := False) return Entity_Id
5181 is
5182 Err : Entity_Id := Empty;
5183
5184 begin
5185 case Nkind (N) is
5186 when N_Abstract_Subprogram_Declaration |
5187 N_Expression_Function |
5188 N_Formal_Subprogram_Declaration |
5189 N_Generic_Package_Declaration |
5190 N_Generic_Subprogram_Declaration |
5191 N_Package_Declaration |
5192 N_Subprogram_Body |
5193 N_Subprogram_Body_Stub |
5194 N_Subprogram_Declaration |
5195 N_Subprogram_Renaming_Declaration
5196 =>
5197 return Defining_Entity (Specification (N));
5198
5199 when N_Component_Declaration |
5200 N_Defining_Program_Unit_Name |
5201 N_Discriminant_Specification |
5202 N_Entry_Body |
5203 N_Entry_Declaration |
5204 N_Entry_Index_Specification |
5205 N_Exception_Declaration |
5206 N_Exception_Renaming_Declaration |
5207 N_Formal_Object_Declaration |
5208 N_Formal_Package_Declaration |
5209 N_Formal_Type_Declaration |
5210 N_Full_Type_Declaration |
5211 N_Implicit_Label_Declaration |
5212 N_Incomplete_Type_Declaration |
5213 N_Loop_Parameter_Specification |
5214 N_Number_Declaration |
5215 N_Object_Declaration |
5216 N_Object_Renaming_Declaration |
5217 N_Package_Body_Stub |
5218 N_Parameter_Specification |
5219 N_Private_Extension_Declaration |
5220 N_Private_Type_Declaration |
5221 N_Protected_Body |
5222 N_Protected_Body_Stub |
5223 N_Protected_Type_Declaration |
5224 N_Single_Protected_Declaration |
5225 N_Single_Task_Declaration |
5226 N_Subtype_Declaration |
5227 N_Task_Body |
5228 N_Task_Body_Stub |
5229 N_Task_Type_Declaration
5230 =>
5231 return Defining_Identifier (N);
5232
5233 when N_Subunit =>
5234 return Defining_Entity (Proper_Body (N));
5235
5236 when N_Function_Instantiation |
5237 N_Function_Specification |
5238 N_Generic_Function_Renaming_Declaration |
5239 N_Generic_Package_Renaming_Declaration |
5240 N_Generic_Procedure_Renaming_Declaration |
5241 N_Package_Body |
5242 N_Package_Instantiation |
5243 N_Package_Renaming_Declaration |
5244 N_Package_Specification |
5245 N_Procedure_Instantiation |
5246 N_Procedure_Specification
5247 =>
5248 declare
5249 Nam : constant Node_Id := Defining_Unit_Name (N);
5250
5251 begin
5252 if Nkind (Nam) in N_Entity then
5253 return Nam;
5254
5255 -- For Error, make up a name and attach to declaration so we
5256 -- can continue semantic analysis.
5257
5258 elsif Nam = Error then
5259 if Empty_On_Errors then
5260 return Empty;
5261 else
5262 Err := Make_Temporary (Sloc (N), 'T');
5263 Set_Defining_Unit_Name (N, Err);
5264
5265 return Err;
5266 end if;
5267
5268 -- If not an entity, get defining identifier
5269
5270 else
5271 return Defining_Identifier (Nam);
5272 end if;
5273 end;
5274
5275 when N_Block_Statement |
5276 N_Loop_Statement =>
5277 return Entity (Identifier (N));
5278
5279 when others =>
5280 if Empty_On_Errors then
5281 return Empty;
5282 else
5283 raise Program_Error;
5284 end if;
5285
5286 end case;
5287 end Defining_Entity;
5288
5289 --------------------------
5290 -- Denotes_Discriminant --
5291 --------------------------
5292
5293 function Denotes_Discriminant
5294 (N : Node_Id;
5295 Check_Concurrent : Boolean := False) return Boolean
5296 is
5297 E : Entity_Id;
5298
5299 begin
5300 if not Is_Entity_Name (N) or else No (Entity (N)) then
5301 return False;
5302 else
5303 E := Entity (N);
5304 end if;
5305
5306 -- If we are checking for a protected type, the discriminant may have
5307 -- been rewritten as the corresponding discriminal of the original type
5308 -- or of the corresponding concurrent record, depending on whether we
5309 -- are in the spec or body of the protected type.
5310
5311 return Ekind (E) = E_Discriminant
5312 or else
5313 (Check_Concurrent
5314 and then Ekind (E) = E_In_Parameter
5315 and then Present (Discriminal_Link (E))
5316 and then
5317 (Is_Concurrent_Type (Scope (Discriminal_Link (E)))
5318 or else
5319 Is_Concurrent_Record_Type (Scope (Discriminal_Link (E)))));
5320 end Denotes_Discriminant;
5321
5322 -------------------------
5323 -- Denotes_Same_Object --
5324 -------------------------
5325
5326 function Denotes_Same_Object (A1, A2 : Node_Id) return Boolean is
5327 Obj1 : Node_Id := A1;
5328 Obj2 : Node_Id := A2;
5329
5330 function Has_Prefix (N : Node_Id) return Boolean;
5331 -- Return True if N has attribute Prefix
5332
5333 function Is_Renaming (N : Node_Id) return Boolean;
5334 -- Return true if N names a renaming entity
5335
5336 function Is_Valid_Renaming (N : Node_Id) return Boolean;
5337 -- For renamings, return False if the prefix of any dereference within
5338 -- the renamed object_name is a variable, or any expression within the
5339 -- renamed object_name contains references to variables or calls on
5340 -- nonstatic functions; otherwise return True (RM 6.4.1(6.10/3))
5341
5342 ----------------
5343 -- Has_Prefix --
5344 ----------------
5345
5346 function Has_Prefix (N : Node_Id) return Boolean is
5347 begin
5348 return
5349 Nkind_In (N,
5350 N_Attribute_Reference,
5351 N_Expanded_Name,
5352 N_Explicit_Dereference,
5353 N_Indexed_Component,
5354 N_Reference,
5355 N_Selected_Component,
5356 N_Slice);
5357 end Has_Prefix;
5358
5359 -----------------
5360 -- Is_Renaming --
5361 -----------------
5362
5363 function Is_Renaming (N : Node_Id) return Boolean is
5364 begin
5365 return Is_Entity_Name (N)
5366 and then Present (Renamed_Entity (Entity (N)));
5367 end Is_Renaming;
5368
5369 -----------------------
5370 -- Is_Valid_Renaming --
5371 -----------------------
5372
5373 function Is_Valid_Renaming (N : Node_Id) return Boolean is
5374
5375 function Check_Renaming (N : Node_Id) return Boolean;
5376 -- Recursive function used to traverse all the prefixes of N
5377
5378 function Check_Renaming (N : Node_Id) return Boolean is
5379 begin
5380 if Is_Renaming (N)
5381 and then not Check_Renaming (Renamed_Entity (Entity (N)))
5382 then
5383 return False;
5384 end if;
5385
5386 if Nkind (N) = N_Indexed_Component then
5387 declare
5388 Indx : Node_Id;
5389
5390 begin
5391 Indx := First (Expressions (N));
5392 while Present (Indx) loop
5393 if not Is_OK_Static_Expression (Indx) then
5394 return False;
5395 end if;
5396
5397 Next_Index (Indx);
5398 end loop;
5399 end;
5400 end if;
5401
5402 if Has_Prefix (N) then
5403 declare
5404 P : constant Node_Id := Prefix (N);
5405
5406 begin
5407 if Nkind (N) = N_Explicit_Dereference
5408 and then Is_Variable (P)
5409 then
5410 return False;
5411
5412 elsif Is_Entity_Name (P)
5413 and then Ekind (Entity (P)) = E_Function
5414 then
5415 return False;
5416
5417 elsif Nkind (P) = N_Function_Call then
5418 return False;
5419 end if;
5420
5421 -- Recursion to continue traversing the prefix of the
5422 -- renaming expression
5423
5424 return Check_Renaming (P);
5425 end;
5426 end if;
5427
5428 return True;
5429 end Check_Renaming;
5430
5431 -- Start of processing for Is_Valid_Renaming
5432
5433 begin
5434 return Check_Renaming (N);
5435 end Is_Valid_Renaming;
5436
5437 -- Start of processing for Denotes_Same_Object
5438
5439 begin
5440 -- Both names statically denote the same stand-alone object or parameter
5441 -- (RM 6.4.1(6.5/3))
5442
5443 if Is_Entity_Name (Obj1)
5444 and then Is_Entity_Name (Obj2)
5445 and then Entity (Obj1) = Entity (Obj2)
5446 then
5447 return True;
5448 end if;
5449
5450 -- For renamings, the prefix of any dereference within the renamed
5451 -- object_name is not a variable, and any expression within the
5452 -- renamed object_name contains no references to variables nor
5453 -- calls on nonstatic functions (RM 6.4.1(6.10/3)).
5454
5455 if Is_Renaming (Obj1) then
5456 if Is_Valid_Renaming (Obj1) then
5457 Obj1 := Renamed_Entity (Entity (Obj1));
5458 else
5459 return False;
5460 end if;
5461 end if;
5462
5463 if Is_Renaming (Obj2) then
5464 if Is_Valid_Renaming (Obj2) then
5465 Obj2 := Renamed_Entity (Entity (Obj2));
5466 else
5467 return False;
5468 end if;
5469 end if;
5470
5471 -- No match if not same node kind (such cases are handled by
5472 -- Denotes_Same_Prefix)
5473
5474 if Nkind (Obj1) /= Nkind (Obj2) then
5475 return False;
5476
5477 -- After handling valid renamings, one of the two names statically
5478 -- denoted a renaming declaration whose renamed object_name is known
5479 -- to denote the same object as the other (RM 6.4.1(6.10/3))
5480
5481 elsif Is_Entity_Name (Obj1) then
5482 if Is_Entity_Name (Obj2) then
5483 return Entity (Obj1) = Entity (Obj2);
5484 else
5485 return False;
5486 end if;
5487
5488 -- Both names are selected_components, their prefixes are known to
5489 -- denote the same object, and their selector_names denote the same
5490 -- component (RM 6.4.1(6.6/3)).
5491
5492 elsif Nkind (Obj1) = N_Selected_Component then
5493 return Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2))
5494 and then
5495 Entity (Selector_Name (Obj1)) = Entity (Selector_Name (Obj2));
5496
5497 -- Both names are dereferences and the dereferenced names are known to
5498 -- denote the same object (RM 6.4.1(6.7/3))
5499
5500 elsif Nkind (Obj1) = N_Explicit_Dereference then
5501 return Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2));
5502
5503 -- Both names are indexed_components, their prefixes are known to denote
5504 -- the same object, and each of the pairs of corresponding index values
5505 -- are either both static expressions with the same static value or both
5506 -- names that are known to denote the same object (RM 6.4.1(6.8/3))
5507
5508 elsif Nkind (Obj1) = N_Indexed_Component then
5509 if not Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2)) then
5510 return False;
5511 else
5512 declare
5513 Indx1 : Node_Id;
5514 Indx2 : Node_Id;
5515
5516 begin
5517 Indx1 := First (Expressions (Obj1));
5518 Indx2 := First (Expressions (Obj2));
5519 while Present (Indx1) loop
5520
5521 -- Indexes must denote the same static value or same object
5522
5523 if Is_OK_Static_Expression (Indx1) then
5524 if not Is_OK_Static_Expression (Indx2) then
5525 return False;
5526
5527 elsif Expr_Value (Indx1) /= Expr_Value (Indx2) then
5528 return False;
5529 end if;
5530
5531 elsif not Denotes_Same_Object (Indx1, Indx2) then
5532 return False;
5533 end if;
5534
5535 Next (Indx1);
5536 Next (Indx2);
5537 end loop;
5538
5539 return True;
5540 end;
5541 end if;
5542
5543 -- Both names are slices, their prefixes are known to denote the same
5544 -- object, and the two slices have statically matching index constraints
5545 -- (RM 6.4.1(6.9/3))
5546
5547 elsif Nkind (Obj1) = N_Slice
5548 and then Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2))
5549 then
5550 declare
5551 Lo1, Lo2, Hi1, Hi2 : Node_Id;
5552
5553 begin
5554 Get_Index_Bounds (Etype (Obj1), Lo1, Hi1);
5555 Get_Index_Bounds (Etype (Obj2), Lo2, Hi2);
5556
5557 -- Check whether bounds are statically identical. There is no
5558 -- attempt to detect partial overlap of slices.
5559
5560 return Denotes_Same_Object (Lo1, Lo2)
5561 and then
5562 Denotes_Same_Object (Hi1, Hi2);
5563 end;
5564
5565 -- In the recursion, literals appear as indexes
5566
5567 elsif Nkind (Obj1) = N_Integer_Literal
5568 and then
5569 Nkind (Obj2) = N_Integer_Literal
5570 then
5571 return Intval (Obj1) = Intval (Obj2);
5572
5573 else
5574 return False;
5575 end if;
5576 end Denotes_Same_Object;
5577
5578 -------------------------
5579 -- Denotes_Same_Prefix --
5580 -------------------------
5581
5582 function Denotes_Same_Prefix (A1, A2 : Node_Id) return Boolean is
5583 begin
5584 if Is_Entity_Name (A1) then
5585 if Nkind_In (A2, N_Selected_Component, N_Indexed_Component)
5586 and then not Is_Access_Type (Etype (A1))
5587 then
5588 return Denotes_Same_Object (A1, Prefix (A2))
5589 or else Denotes_Same_Prefix (A1, Prefix (A2));
5590 else
5591 return False;
5592 end if;
5593
5594 elsif Is_Entity_Name (A2) then
5595 return Denotes_Same_Prefix (A1 => A2, A2 => A1);
5596
5597 elsif Nkind_In (A1, N_Selected_Component, N_Indexed_Component, N_Slice)
5598 and then
5599 Nkind_In (A2, N_Selected_Component, N_Indexed_Component, N_Slice)
5600 then
5601 declare
5602 Root1, Root2 : Node_Id;
5603 Depth1, Depth2 : Nat := 0;
5604
5605 begin
5606 Root1 := Prefix (A1);
5607 while not Is_Entity_Name (Root1) loop
5608 if not Nkind_In
5609 (Root1, N_Selected_Component, N_Indexed_Component)
5610 then
5611 return False;
5612 else
5613 Root1 := Prefix (Root1);
5614 end if;
5615
5616 Depth1 := Depth1 + 1;
5617 end loop;
5618
5619 Root2 := Prefix (A2);
5620 while not Is_Entity_Name (Root2) loop
5621 if not Nkind_In (Root2, N_Selected_Component,
5622 N_Indexed_Component)
5623 then
5624 return False;
5625 else
5626 Root2 := Prefix (Root2);
5627 end if;
5628
5629 Depth2 := Depth2 + 1;
5630 end loop;
5631
5632 -- If both have the same depth and they do not denote the same
5633 -- object, they are disjoint and no warning is needed.
5634
5635 if Depth1 = Depth2 then
5636 return False;
5637
5638 elsif Depth1 > Depth2 then
5639 Root1 := Prefix (A1);
5640 for J in 1 .. Depth1 - Depth2 - 1 loop
5641 Root1 := Prefix (Root1);
5642 end loop;
5643
5644 return Denotes_Same_Object (Root1, A2);
5645
5646 else
5647 Root2 := Prefix (A2);
5648 for J in 1 .. Depth2 - Depth1 - 1 loop
5649 Root2 := Prefix (Root2);
5650 end loop;
5651
5652 return Denotes_Same_Object (A1, Root2);
5653 end if;
5654 end;
5655
5656 else
5657 return False;
5658 end if;
5659 end Denotes_Same_Prefix;
5660
5661 ----------------------
5662 -- Denotes_Variable --
5663 ----------------------
5664
5665 function Denotes_Variable (N : Node_Id) return Boolean is
5666 begin
5667 return Is_Variable (N) and then Paren_Count (N) = 0;
5668 end Denotes_Variable;
5669
5670 -----------------------------
5671 -- Depends_On_Discriminant --
5672 -----------------------------
5673
5674 function Depends_On_Discriminant (N : Node_Id) return Boolean is
5675 L : Node_Id;
5676 H : Node_Id;
5677
5678 begin
5679 Get_Index_Bounds (N, L, H);
5680 return Denotes_Discriminant (L) or else Denotes_Discriminant (H);
5681 end Depends_On_Discriminant;
5682
5683 -------------------------
5684 -- Designate_Same_Unit --
5685 -------------------------
5686
5687 function Designate_Same_Unit
5688 (Name1 : Node_Id;
5689 Name2 : Node_Id) return Boolean
5690 is
5691 K1 : constant Node_Kind := Nkind (Name1);
5692 K2 : constant Node_Kind := Nkind (Name2);
5693
5694 function Prefix_Node (N : Node_Id) return Node_Id;
5695 -- Returns the parent unit name node of a defining program unit name
5696 -- or the prefix if N is a selected component or an expanded name.
5697
5698 function Select_Node (N : Node_Id) return Node_Id;
5699 -- Returns the defining identifier node of a defining program unit
5700 -- name or the selector node if N is a selected component or an
5701 -- expanded name.
5702
5703 -----------------
5704 -- Prefix_Node --
5705 -----------------
5706
5707 function Prefix_Node (N : Node_Id) return Node_Id is
5708 begin
5709 if Nkind (N) = N_Defining_Program_Unit_Name then
5710 return Name (N);
5711 else
5712 return Prefix (N);
5713 end if;
5714 end Prefix_Node;
5715
5716 -----------------
5717 -- Select_Node --
5718 -----------------
5719
5720 function Select_Node (N : Node_Id) return Node_Id is
5721 begin
5722 if Nkind (N) = N_Defining_Program_Unit_Name then
5723 return Defining_Identifier (N);
5724 else
5725 return Selector_Name (N);
5726 end if;
5727 end Select_Node;
5728
5729 -- Start of processing for Designate_Same_Unit
5730
5731 begin
5732 if Nkind_In (K1, N_Identifier, N_Defining_Identifier)
5733 and then
5734 Nkind_In (K2, N_Identifier, N_Defining_Identifier)
5735 then
5736 return Chars (Name1) = Chars (Name2);
5737
5738 elsif Nkind_In (K1, N_Expanded_Name,
5739 N_Selected_Component,
5740 N_Defining_Program_Unit_Name)
5741 and then
5742 Nkind_In (K2, N_Expanded_Name,
5743 N_Selected_Component,
5744 N_Defining_Program_Unit_Name)
5745 then
5746 return
5747 (Chars (Select_Node (Name1)) = Chars (Select_Node (Name2)))
5748 and then
5749 Designate_Same_Unit (Prefix_Node (Name1), Prefix_Node (Name2));
5750
5751 else
5752 return False;
5753 end if;
5754 end Designate_Same_Unit;
5755
5756 ------------------------------------------
5757 -- function Dynamic_Accessibility_Level --
5758 ------------------------------------------
5759
5760 function Dynamic_Accessibility_Level (Expr : Node_Id) return Node_Id is
5761 E : Entity_Id;
5762 Loc : constant Source_Ptr := Sloc (Expr);
5763
5764 function Make_Level_Literal (Level : Uint) return Node_Id;
5765 -- Construct an integer literal representing an accessibility level
5766 -- with its type set to Natural.
5767
5768 ------------------------
5769 -- Make_Level_Literal --
5770 ------------------------
5771
5772 function Make_Level_Literal (Level : Uint) return Node_Id is
5773 Result : constant Node_Id := Make_Integer_Literal (Loc, Level);
5774 begin
5775 Set_Etype (Result, Standard_Natural);
5776 return Result;
5777 end Make_Level_Literal;
5778
5779 -- Start of processing for Dynamic_Accessibility_Level
5780
5781 begin
5782 if Is_Entity_Name (Expr) then
5783 E := Entity (Expr);
5784
5785 if Present (Renamed_Object (E)) then
5786 return Dynamic_Accessibility_Level (Renamed_Object (E));
5787 end if;
5788
5789 if Is_Formal (E) or else Ekind_In (E, E_Variable, E_Constant) then
5790 if Present (Extra_Accessibility (E)) then
5791 return New_Occurrence_Of (Extra_Accessibility (E), Loc);
5792 end if;
5793 end if;
5794 end if;
5795
5796 -- Unimplemented: Ptr.all'Access, where Ptr has Extra_Accessibility ???
5797
5798 case Nkind (Expr) is
5799
5800 -- For access discriminant, the level of the enclosing object
5801
5802 when N_Selected_Component =>
5803 if Ekind (Entity (Selector_Name (Expr))) = E_Discriminant
5804 and then Ekind (Etype (Entity (Selector_Name (Expr)))) =
5805 E_Anonymous_Access_Type
5806 then
5807 return Make_Level_Literal (Object_Access_Level (Expr));
5808 end if;
5809
5810 when N_Attribute_Reference =>
5811 case Get_Attribute_Id (Attribute_Name (Expr)) is
5812
5813 -- For X'Access, the level of the prefix X
5814
5815 when Attribute_Access =>
5816 return Make_Level_Literal
5817 (Object_Access_Level (Prefix (Expr)));
5818
5819 -- Treat the unchecked attributes as library-level
5820
5821 when Attribute_Unchecked_Access |
5822 Attribute_Unrestricted_Access =>
5823 return Make_Level_Literal (Scope_Depth (Standard_Standard));
5824
5825 -- No other access-valued attributes
5826
5827 when others =>
5828 raise Program_Error;
5829 end case;
5830
5831 when N_Allocator =>
5832
5833 -- Unimplemented: depends on context. As an actual parameter where
5834 -- formal type is anonymous, use
5835 -- Scope_Depth (Current_Scope) + 1.
5836 -- For other cases, see 3.10.2(14/3) and following. ???
5837
5838 null;
5839
5840 when N_Type_Conversion =>
5841 if not Is_Local_Anonymous_Access (Etype (Expr)) then
5842
5843 -- Handle type conversions introduced for a rename of an
5844 -- Ada 2012 stand-alone object of an anonymous access type.
5845
5846 return Dynamic_Accessibility_Level (Expression (Expr));
5847 end if;
5848
5849 when others =>
5850 null;
5851 end case;
5852
5853 return Make_Level_Literal (Type_Access_Level (Etype (Expr)));
5854 end Dynamic_Accessibility_Level;
5855
5856 -----------------------------------
5857 -- Effective_Extra_Accessibility --
5858 -----------------------------------
5859
5860 function Effective_Extra_Accessibility (Id : Entity_Id) return Entity_Id is
5861 begin
5862 if Present (Renamed_Object (Id))
5863 and then Is_Entity_Name (Renamed_Object (Id))
5864 then
5865 return Effective_Extra_Accessibility (Entity (Renamed_Object (Id)));
5866 else
5867 return Extra_Accessibility (Id);
5868 end if;
5869 end Effective_Extra_Accessibility;
5870
5871 -----------------------------
5872 -- Effective_Reads_Enabled --
5873 -----------------------------
5874
5875 function Effective_Reads_Enabled (Id : Entity_Id) return Boolean is
5876 begin
5877 return Has_Enabled_Property (Id, Name_Effective_Reads);
5878 end Effective_Reads_Enabled;
5879
5880 ------------------------------
5881 -- Effective_Writes_Enabled --
5882 ------------------------------
5883
5884 function Effective_Writes_Enabled (Id : Entity_Id) return Boolean is
5885 begin
5886 return Has_Enabled_Property (Id, Name_Effective_Writes);
5887 end Effective_Writes_Enabled;
5888
5889 ------------------------------
5890 -- Enclosing_Comp_Unit_Node --
5891 ------------------------------
5892
5893 function Enclosing_Comp_Unit_Node (N : Node_Id) return Node_Id is
5894 Current_Node : Node_Id;
5895
5896 begin
5897 Current_Node := N;
5898 while Present (Current_Node)
5899 and then Nkind (Current_Node) /= N_Compilation_Unit
5900 loop
5901 Current_Node := Parent (Current_Node);
5902 end loop;
5903
5904 if Nkind (Current_Node) /= N_Compilation_Unit then
5905 return Empty;
5906 else
5907 return Current_Node;
5908 end if;
5909 end Enclosing_Comp_Unit_Node;
5910
5911 --------------------------
5912 -- Enclosing_CPP_Parent --
5913 --------------------------
5914
5915 function Enclosing_CPP_Parent (Typ : Entity_Id) return Entity_Id is
5916 Parent_Typ : Entity_Id := Typ;
5917
5918 begin
5919 while not Is_CPP_Class (Parent_Typ)
5920 and then Etype (Parent_Typ) /= Parent_Typ
5921 loop
5922 Parent_Typ := Etype (Parent_Typ);
5923
5924 if Is_Private_Type (Parent_Typ) then
5925 Parent_Typ := Full_View (Base_Type (Parent_Typ));
5926 end if;
5927 end loop;
5928
5929 pragma Assert (Is_CPP_Class (Parent_Typ));
5930 return Parent_Typ;
5931 end Enclosing_CPP_Parent;
5932
5933 ---------------------------
5934 -- Enclosing_Declaration --
5935 ---------------------------
5936
5937 function Enclosing_Declaration (N : Node_Id) return Node_Id is
5938 Decl : Node_Id := N;
5939
5940 begin
5941 while Present (Decl)
5942 and then not (Nkind (Decl) in N_Declaration
5943 or else
5944 Nkind (Decl) in N_Later_Decl_Item)
5945 loop
5946 Decl := Parent (Decl);
5947 end loop;
5948
5949 return Decl;
5950 end Enclosing_Declaration;
5951
5952 ----------------------------
5953 -- Enclosing_Generic_Body --
5954 ----------------------------
5955
5956 function Enclosing_Generic_Body
5957 (N : Node_Id) return Node_Id
5958 is
5959 P : Node_Id;
5960 Decl : Node_Id;
5961 Spec : Node_Id;
5962
5963 begin
5964 P := Parent (N);
5965 while Present (P) loop
5966 if Nkind (P) = N_Package_Body
5967 or else Nkind (P) = N_Subprogram_Body
5968 then
5969 Spec := Corresponding_Spec (P);
5970
5971 if Present (Spec) then
5972 Decl := Unit_Declaration_Node (Spec);
5973
5974 if Nkind (Decl) = N_Generic_Package_Declaration
5975 or else Nkind (Decl) = N_Generic_Subprogram_Declaration
5976 then
5977 return P;
5978 end if;
5979 end if;
5980 end if;
5981
5982 P := Parent (P);
5983 end loop;
5984
5985 return Empty;
5986 end Enclosing_Generic_Body;
5987
5988 ----------------------------
5989 -- Enclosing_Generic_Unit --
5990 ----------------------------
5991
5992 function Enclosing_Generic_Unit
5993 (N : Node_Id) return Node_Id
5994 is
5995 P : Node_Id;
5996 Decl : Node_Id;
5997 Spec : Node_Id;
5998
5999 begin
6000 P := Parent (N);
6001 while Present (P) loop
6002 if Nkind (P) = N_Generic_Package_Declaration
6003 or else Nkind (P) = N_Generic_Subprogram_Declaration
6004 then
6005 return P;
6006
6007 elsif Nkind (P) = N_Package_Body
6008 or else Nkind (P) = N_Subprogram_Body
6009 then
6010 Spec := Corresponding_Spec (P);
6011
6012 if Present (Spec) then
6013 Decl := Unit_Declaration_Node (Spec);
6014
6015 if Nkind (Decl) = N_Generic_Package_Declaration
6016 or else Nkind (Decl) = N_Generic_Subprogram_Declaration
6017 then
6018 return Decl;
6019 end if;
6020 end if;
6021 end if;
6022
6023 P := Parent (P);
6024 end loop;
6025
6026 return Empty;
6027 end Enclosing_Generic_Unit;
6028
6029 -------------------------------
6030 -- Enclosing_Lib_Unit_Entity --
6031 -------------------------------
6032
6033 function Enclosing_Lib_Unit_Entity
6034 (E : Entity_Id := Current_Scope) return Entity_Id
6035 is
6036 Unit_Entity : Entity_Id;
6037
6038 begin
6039 -- Look for enclosing library unit entity by following scope links.
6040 -- Equivalent to, but faster than indexing through the scope stack.
6041
6042 Unit_Entity := E;
6043 while (Present (Scope (Unit_Entity))
6044 and then Scope (Unit_Entity) /= Standard_Standard)
6045 and not Is_Child_Unit (Unit_Entity)
6046 loop
6047 Unit_Entity := Scope (Unit_Entity);
6048 end loop;
6049
6050 return Unit_Entity;
6051 end Enclosing_Lib_Unit_Entity;
6052
6053 -----------------------------
6054 -- Enclosing_Lib_Unit_Node --
6055 -----------------------------
6056
6057 function Enclosing_Lib_Unit_Node (N : Node_Id) return Node_Id is
6058 Encl_Unit : Node_Id;
6059
6060 begin
6061 Encl_Unit := Enclosing_Comp_Unit_Node (N);
6062 while Present (Encl_Unit)
6063 and then Nkind (Unit (Encl_Unit)) = N_Subunit
6064 loop
6065 Encl_Unit := Library_Unit (Encl_Unit);
6066 end loop;
6067
6068 pragma Assert (Nkind (Encl_Unit) = N_Compilation_Unit);
6069 return Encl_Unit;
6070 end Enclosing_Lib_Unit_Node;
6071
6072 -----------------------
6073 -- Enclosing_Package --
6074 -----------------------
6075
6076 function Enclosing_Package (E : Entity_Id) return Entity_Id is
6077 Dynamic_Scope : constant Entity_Id := Enclosing_Dynamic_Scope (E);
6078
6079 begin
6080 if Dynamic_Scope = Standard_Standard then
6081 return Standard_Standard;
6082
6083 elsif Dynamic_Scope = Empty then
6084 return Empty;
6085
6086 elsif Ekind_In (Dynamic_Scope, E_Package, E_Package_Body,
6087 E_Generic_Package)
6088 then
6089 return Dynamic_Scope;
6090
6091 else
6092 return Enclosing_Package (Dynamic_Scope);
6093 end if;
6094 end Enclosing_Package;
6095
6096 -------------------------------------
6097 -- Enclosing_Package_Or_Subprogram --
6098 -------------------------------------
6099
6100 function Enclosing_Package_Or_Subprogram (E : Entity_Id) return Entity_Id is
6101 S : Entity_Id;
6102
6103 begin
6104 S := Scope (E);
6105 while Present (S) loop
6106 if Is_Package_Or_Generic_Package (S)
6107 or else Ekind (S) = E_Package_Body
6108 then
6109 return S;
6110
6111 elsif Is_Subprogram_Or_Generic_Subprogram (S)
6112 or else Ekind (S) = E_Subprogram_Body
6113 then
6114 return S;
6115
6116 else
6117 S := Scope (S);
6118 end if;
6119 end loop;
6120
6121 return Empty;
6122 end Enclosing_Package_Or_Subprogram;
6123
6124 --------------------------
6125 -- Enclosing_Subprogram --
6126 --------------------------
6127
6128 function Enclosing_Subprogram (E : Entity_Id) return Entity_Id is
6129 Dynamic_Scope : constant Entity_Id := Enclosing_Dynamic_Scope (E);
6130
6131 begin
6132 if Dynamic_Scope = Standard_Standard then
6133 return Empty;
6134
6135 elsif Dynamic_Scope = Empty then
6136 return Empty;
6137
6138 elsif Ekind (Dynamic_Scope) = E_Subprogram_Body then
6139 return Corresponding_Spec (Parent (Parent (Dynamic_Scope)));
6140
6141 elsif Ekind (Dynamic_Scope) = E_Block
6142 or else Ekind (Dynamic_Scope) = E_Return_Statement
6143 then
6144 return Enclosing_Subprogram (Dynamic_Scope);
6145
6146 elsif Ekind (Dynamic_Scope) = E_Task_Type then
6147 return Get_Task_Body_Procedure (Dynamic_Scope);
6148
6149 elsif Ekind (Dynamic_Scope) = E_Limited_Private_Type
6150 and then Present (Full_View (Dynamic_Scope))
6151 and then Ekind (Full_View (Dynamic_Scope)) = E_Task_Type
6152 then
6153 return Get_Task_Body_Procedure (Full_View (Dynamic_Scope));
6154
6155 -- No body is generated if the protected operation is eliminated
6156
6157 elsif Convention (Dynamic_Scope) = Convention_Protected
6158 and then not Is_Eliminated (Dynamic_Scope)
6159 and then Present (Protected_Body_Subprogram (Dynamic_Scope))
6160 then
6161 return Protected_Body_Subprogram (Dynamic_Scope);
6162
6163 else
6164 return Dynamic_Scope;
6165 end if;
6166 end Enclosing_Subprogram;
6167
6168 ------------------------
6169 -- Ensure_Freeze_Node --
6170 ------------------------
6171
6172 procedure Ensure_Freeze_Node (E : Entity_Id) is
6173 FN : Node_Id;
6174 begin
6175 if No (Freeze_Node (E)) then
6176 FN := Make_Freeze_Entity (Sloc (E));
6177 Set_Has_Delayed_Freeze (E);
6178 Set_Freeze_Node (E, FN);
6179 Set_Access_Types_To_Process (FN, No_Elist);
6180 Set_TSS_Elist (FN, No_Elist);
6181 Set_Entity (FN, E);
6182 end if;
6183 end Ensure_Freeze_Node;
6184
6185 ----------------
6186 -- Enter_Name --
6187 ----------------
6188
6189 procedure Enter_Name (Def_Id : Entity_Id) is
6190 C : constant Entity_Id := Current_Entity (Def_Id);
6191 E : constant Entity_Id := Current_Entity_In_Scope (Def_Id);
6192 S : constant Entity_Id := Current_Scope;
6193
6194 begin
6195 Generate_Definition (Def_Id);
6196
6197 -- Add new name to current scope declarations. Check for duplicate
6198 -- declaration, which may or may not be a genuine error.
6199
6200 if Present (E) then
6201
6202 -- Case of previous entity entered because of a missing declaration
6203 -- or else a bad subtype indication. Best is to use the new entity,
6204 -- and make the previous one invisible.
6205
6206 if Etype (E) = Any_Type then
6207 Set_Is_Immediately_Visible (E, False);
6208
6209 -- Case of renaming declaration constructed for package instances.
6210 -- if there is an explicit declaration with the same identifier,
6211 -- the renaming is not immediately visible any longer, but remains
6212 -- visible through selected component notation.
6213
6214 elsif Nkind (Parent (E)) = N_Package_Renaming_Declaration
6215 and then not Comes_From_Source (E)
6216 then
6217 Set_Is_Immediately_Visible (E, False);
6218
6219 -- The new entity may be the package renaming, which has the same
6220 -- same name as a generic formal which has been seen already.
6221
6222 elsif Nkind (Parent (Def_Id)) = N_Package_Renaming_Declaration
6223 and then not Comes_From_Source (Def_Id)
6224 then
6225 Set_Is_Immediately_Visible (E, False);
6226
6227 -- For a fat pointer corresponding to a remote access to subprogram,
6228 -- we use the same identifier as the RAS type, so that the proper
6229 -- name appears in the stub. This type is only retrieved through
6230 -- the RAS type and never by visibility, and is not added to the
6231 -- visibility list (see below).
6232
6233 elsif Nkind (Parent (Def_Id)) = N_Full_Type_Declaration
6234 and then Ekind (Def_Id) = E_Record_Type
6235 and then Present (Corresponding_Remote_Type (Def_Id))
6236 then
6237 null;
6238
6239 -- Case of an implicit operation or derived literal. The new entity
6240 -- hides the implicit one, which is removed from all visibility,
6241 -- i.e. the entity list of its scope, and homonym chain of its name.
6242
6243 elsif (Is_Overloadable (E) and then Is_Inherited_Operation (E))
6244 or else Is_Internal (E)
6245 then
6246 declare
6247 Decl : constant Node_Id := Parent (E);
6248 Prev : Entity_Id;
6249 Prev_Vis : Entity_Id;
6250
6251 begin
6252 -- If E is an implicit declaration, it cannot be the first
6253 -- entity in the scope.
6254
6255 Prev := First_Entity (Current_Scope);
6256 while Present (Prev) and then Next_Entity (Prev) /= E loop
6257 Next_Entity (Prev);
6258 end loop;
6259
6260 if No (Prev) then
6261
6262 -- If E is not on the entity chain of the current scope,
6263 -- it is an implicit declaration in the generic formal
6264 -- part of a generic subprogram. When analyzing the body,
6265 -- the generic formals are visible but not on the entity
6266 -- chain of the subprogram. The new entity will become
6267 -- the visible one in the body.
6268
6269 pragma Assert
6270 (Nkind (Parent (Decl)) = N_Generic_Subprogram_Declaration);
6271 null;
6272
6273 else
6274 Set_Next_Entity (Prev, Next_Entity (E));
6275
6276 if No (Next_Entity (Prev)) then
6277 Set_Last_Entity (Current_Scope, Prev);
6278 end if;
6279
6280 if E = Current_Entity (E) then
6281 Prev_Vis := Empty;
6282
6283 else
6284 Prev_Vis := Current_Entity (E);
6285 while Homonym (Prev_Vis) /= E loop
6286 Prev_Vis := Homonym (Prev_Vis);
6287 end loop;
6288 end if;
6289
6290 if Present (Prev_Vis) then
6291
6292 -- Skip E in the visibility chain
6293
6294 Set_Homonym (Prev_Vis, Homonym (E));
6295
6296 else
6297 Set_Name_Entity_Id (Chars (E), Homonym (E));
6298 end if;
6299 end if;
6300 end;
6301
6302 -- This section of code could use a comment ???
6303
6304 elsif Present (Etype (E))
6305 and then Is_Concurrent_Type (Etype (E))
6306 and then E = Def_Id
6307 then
6308 return;
6309
6310 -- If the homograph is a protected component renaming, it should not
6311 -- be hiding the current entity. Such renamings are treated as weak
6312 -- declarations.
6313
6314 elsif Is_Prival (E) then
6315 Set_Is_Immediately_Visible (E, False);
6316
6317 -- In this case the current entity is a protected component renaming.
6318 -- Perform minimal decoration by setting the scope and return since
6319 -- the prival should not be hiding other visible entities.
6320
6321 elsif Is_Prival (Def_Id) then
6322 Set_Scope (Def_Id, Current_Scope);
6323 return;
6324
6325 -- Analogous to privals, the discriminal generated for an entry index
6326 -- parameter acts as a weak declaration. Perform minimal decoration
6327 -- to avoid bogus errors.
6328
6329 elsif Is_Discriminal (Def_Id)
6330 and then Ekind (Discriminal_Link (Def_Id)) = E_Entry_Index_Parameter
6331 then
6332 Set_Scope (Def_Id, Current_Scope);
6333 return;
6334
6335 -- In the body or private part of an instance, a type extension may
6336 -- introduce a component with the same name as that of an actual. The
6337 -- legality rule is not enforced, but the semantics of the full type
6338 -- with two components of same name are not clear at this point???
6339
6340 elsif In_Instance_Not_Visible then
6341 null;
6342
6343 -- When compiling a package body, some child units may have become
6344 -- visible. They cannot conflict with local entities that hide them.
6345
6346 elsif Is_Child_Unit (E)
6347 and then In_Open_Scopes (Scope (E))
6348 and then not Is_Immediately_Visible (E)
6349 then
6350 null;
6351
6352 -- Conversely, with front-end inlining we may compile the parent body
6353 -- first, and a child unit subsequently. The context is now the
6354 -- parent spec, and body entities are not visible.
6355
6356 elsif Is_Child_Unit (Def_Id)
6357 and then Is_Package_Body_Entity (E)
6358 and then not In_Package_Body (Current_Scope)
6359 then
6360 null;
6361
6362 -- Case of genuine duplicate declaration
6363
6364 else
6365 Error_Msg_Sloc := Sloc (E);
6366
6367 -- If the previous declaration is an incomplete type declaration
6368 -- this may be an attempt to complete it with a private type. The
6369 -- following avoids confusing cascaded errors.
6370
6371 if Nkind (Parent (E)) = N_Incomplete_Type_Declaration
6372 and then Nkind (Parent (Def_Id)) = N_Private_Type_Declaration
6373 then
6374 Error_Msg_N
6375 ("incomplete type cannot be completed with a private " &
6376 "declaration", Parent (Def_Id));
6377 Set_Is_Immediately_Visible (E, False);
6378 Set_Full_View (E, Def_Id);
6379
6380 -- An inherited component of a record conflicts with a new
6381 -- discriminant. The discriminant is inserted first in the scope,
6382 -- but the error should be posted on it, not on the component.
6383
6384 elsif Ekind (E) = E_Discriminant
6385 and then Present (Scope (Def_Id))
6386 and then Scope (Def_Id) /= Current_Scope
6387 then
6388 Error_Msg_Sloc := Sloc (Def_Id);
6389 Error_Msg_N ("& conflicts with declaration#", E);
6390 return;
6391
6392 -- If the name of the unit appears in its own context clause, a
6393 -- dummy package with the name has already been created, and the
6394 -- error emitted. Try to continue quietly.
6395
6396 elsif Error_Posted (E)
6397 and then Sloc (E) = No_Location
6398 and then Nkind (Parent (E)) = N_Package_Specification
6399 and then Current_Scope = Standard_Standard
6400 then
6401 Set_Scope (Def_Id, Current_Scope);
6402 return;
6403
6404 else
6405 Error_Msg_N ("& conflicts with declaration#", Def_Id);
6406
6407 -- Avoid cascaded messages with duplicate components in
6408 -- derived types.
6409
6410 if Ekind_In (E, E_Component, E_Discriminant) then
6411 return;
6412 end if;
6413 end if;
6414
6415 if Nkind (Parent (Parent (Def_Id))) =
6416 N_Generic_Subprogram_Declaration
6417 and then Def_Id =
6418 Defining_Entity (Specification (Parent (Parent (Def_Id))))
6419 then
6420 Error_Msg_N ("\generic units cannot be overloaded", Def_Id);
6421 end if;
6422
6423 -- If entity is in standard, then we are in trouble, because it
6424 -- means that we have a library package with a duplicated name.
6425 -- That's hard to recover from, so abort.
6426
6427 if S = Standard_Standard then
6428 raise Unrecoverable_Error;
6429
6430 -- Otherwise we continue with the declaration. Having two
6431 -- identical declarations should not cause us too much trouble.
6432
6433 else
6434 null;
6435 end if;
6436 end if;
6437 end if;
6438
6439 -- If we fall through, declaration is OK, at least OK enough to continue
6440
6441 -- If Def_Id is a discriminant or a record component we are in the midst
6442 -- of inheriting components in a derived record definition. Preserve
6443 -- their Ekind and Etype.
6444
6445 if Ekind_In (Def_Id, E_Discriminant, E_Component) then
6446 null;
6447
6448 -- If a type is already set, leave it alone (happens when a type
6449 -- declaration is reanalyzed following a call to the optimizer).
6450
6451 elsif Present (Etype (Def_Id)) then
6452 null;
6453
6454 -- Otherwise, the kind E_Void insures that premature uses of the entity
6455 -- will be detected. Any_Type insures that no cascaded errors will occur
6456
6457 else
6458 Set_Ekind (Def_Id, E_Void);
6459 Set_Etype (Def_Id, Any_Type);
6460 end if;
6461
6462 -- Inherited discriminants and components in derived record types are
6463 -- immediately visible. Itypes are not.
6464
6465 -- Unless the Itype is for a record type with a corresponding remote
6466 -- type (what is that about, it was not commented ???)
6467
6468 if Ekind_In (Def_Id, E_Discriminant, E_Component)
6469 or else
6470 ((not Is_Record_Type (Def_Id)
6471 or else No (Corresponding_Remote_Type (Def_Id)))
6472 and then not Is_Itype (Def_Id))
6473 then
6474 Set_Is_Immediately_Visible (Def_Id);
6475 Set_Current_Entity (Def_Id);
6476 end if;
6477
6478 Set_Homonym (Def_Id, C);
6479 Append_Entity (Def_Id, S);
6480 Set_Public_Status (Def_Id);
6481
6482 -- Declaring a homonym is not allowed in SPARK ...
6483
6484 if Present (C) and then Restriction_Check_Required (SPARK_05) then
6485 declare
6486 Enclosing_Subp : constant Node_Id := Enclosing_Subprogram (Def_Id);
6487 Enclosing_Pack : constant Node_Id := Enclosing_Package (Def_Id);
6488 Other_Scope : constant Node_Id := Enclosing_Dynamic_Scope (C);
6489
6490 begin
6491 -- ... unless the new declaration is in a subprogram, and the
6492 -- visible declaration is a variable declaration or a parameter
6493 -- specification outside that subprogram.
6494
6495 if Present (Enclosing_Subp)
6496 and then Nkind_In (Parent (C), N_Object_Declaration,
6497 N_Parameter_Specification)
6498 and then not Scope_Within_Or_Same (Other_Scope, Enclosing_Subp)
6499 then
6500 null;
6501
6502 -- ... or the new declaration is in a package, and the visible
6503 -- declaration occurs outside that package.
6504
6505 elsif Present (Enclosing_Pack)
6506 and then not Scope_Within_Or_Same (Other_Scope, Enclosing_Pack)
6507 then
6508 null;
6509
6510 -- ... or the new declaration is a component declaration in a
6511 -- record type definition.
6512
6513 elsif Nkind (Parent (Def_Id)) = N_Component_Declaration then
6514 null;
6515
6516 -- Don't issue error for non-source entities
6517
6518 elsif Comes_From_Source (Def_Id)
6519 and then Comes_From_Source (C)
6520 then
6521 Error_Msg_Sloc := Sloc (C);
6522 Check_SPARK_05_Restriction
6523 ("redeclaration of identifier &#", Def_Id);
6524 end if;
6525 end;
6526 end if;
6527
6528 -- Warn if new entity hides an old one
6529
6530 if Warn_On_Hiding and then Present (C)
6531
6532 -- Don't warn for record components since they always have a well
6533 -- defined scope which does not confuse other uses. Note that in
6534 -- some cases, Ekind has not been set yet.
6535
6536 and then Ekind (C) /= E_Component
6537 and then Ekind (C) /= E_Discriminant
6538 and then Nkind (Parent (C)) /= N_Component_Declaration
6539 and then Ekind (Def_Id) /= E_Component
6540 and then Ekind (Def_Id) /= E_Discriminant
6541 and then Nkind (Parent (Def_Id)) /= N_Component_Declaration
6542
6543 -- Don't warn for one character variables. It is too common to use
6544 -- such variables as locals and will just cause too many false hits.
6545
6546 and then Length_Of_Name (Chars (C)) /= 1
6547
6548 -- Don't warn for non-source entities
6549
6550 and then Comes_From_Source (C)
6551 and then Comes_From_Source (Def_Id)
6552
6553 -- Don't warn unless entity in question is in extended main source
6554
6555 and then In_Extended_Main_Source_Unit (Def_Id)
6556
6557 -- Finally, the hidden entity must be either immediately visible or
6558 -- use visible (i.e. from a used package).
6559
6560 and then
6561 (Is_Immediately_Visible (C)
6562 or else
6563 Is_Potentially_Use_Visible (C))
6564 then
6565 Error_Msg_Sloc := Sloc (C);
6566 Error_Msg_N ("declaration hides &#?h?", Def_Id);
6567 end if;
6568 end Enter_Name;
6569
6570 ---------------
6571 -- Entity_Of --
6572 ---------------
6573
6574 function Entity_Of (N : Node_Id) return Entity_Id is
6575 Id : Entity_Id;
6576
6577 begin
6578 Id := Empty;
6579
6580 if Is_Entity_Name (N) then
6581 Id := Entity (N);
6582
6583 -- Follow a possible chain of renamings to reach the root renamed
6584 -- object.
6585
6586 while Present (Id)
6587 and then Is_Object (Id)
6588 and then Present (Renamed_Object (Id))
6589 loop
6590 if Is_Entity_Name (Renamed_Object (Id)) then
6591 Id := Entity (Renamed_Object (Id));
6592 else
6593 Id := Empty;
6594 exit;
6595 end if;
6596 end loop;
6597 end if;
6598
6599 return Id;
6600 end Entity_Of;
6601
6602 --------------------------
6603 -- Explain_Limited_Type --
6604 --------------------------
6605
6606 procedure Explain_Limited_Type (T : Entity_Id; N : Node_Id) is
6607 C : Entity_Id;
6608
6609 begin
6610 -- For array, component type must be limited
6611
6612 if Is_Array_Type (T) then
6613 Error_Msg_Node_2 := T;
6614 Error_Msg_NE
6615 ("\component type& of type& is limited", N, Component_Type (T));
6616 Explain_Limited_Type (Component_Type (T), N);
6617
6618 elsif Is_Record_Type (T) then
6619
6620 -- No need for extra messages if explicit limited record
6621
6622 if Is_Limited_Record (Base_Type (T)) then
6623 return;
6624 end if;
6625
6626 -- Otherwise find a limited component. Check only components that
6627 -- come from source, or inherited components that appear in the
6628 -- source of the ancestor.
6629
6630 C := First_Component (T);
6631 while Present (C) loop
6632 if Is_Limited_Type (Etype (C))
6633 and then
6634 (Comes_From_Source (C)
6635 or else
6636 (Present (Original_Record_Component (C))
6637 and then
6638 Comes_From_Source (Original_Record_Component (C))))
6639 then
6640 Error_Msg_Node_2 := T;
6641 Error_Msg_NE ("\component& of type& has limited type", N, C);
6642 Explain_Limited_Type (Etype (C), N);
6643 return;
6644 end if;
6645
6646 Next_Component (C);
6647 end loop;
6648
6649 -- The type may be declared explicitly limited, even if no component
6650 -- of it is limited, in which case we fall out of the loop.
6651 return;
6652 end if;
6653 end Explain_Limited_Type;
6654
6655 -------------------------------
6656 -- Extensions_Visible_Status --
6657 -------------------------------
6658
6659 function Extensions_Visible_Status
6660 (Id : Entity_Id) return Extensions_Visible_Mode
6661 is
6662 Arg : Node_Id;
6663 Decl : Node_Id;
6664 Expr : Node_Id;
6665 Prag : Node_Id;
6666 Subp : Entity_Id;
6667
6668 begin
6669 -- When a formal parameter is subject to Extensions_Visible, the pragma
6670 -- is stored in the contract of related subprogram.
6671
6672 if Is_Formal (Id) then
6673 Subp := Scope (Id);
6674
6675 elsif Is_Subprogram_Or_Generic_Subprogram (Id) then
6676 Subp := Id;
6677
6678 -- No other construct carries this pragma
6679
6680 else
6681 return Extensions_Visible_None;
6682 end if;
6683
6684 Prag := Get_Pragma (Subp, Pragma_Extensions_Visible);
6685
6686 -- In certain cases analysis may request the Extensions_Visible status
6687 -- of an expression function before the pragma has been analyzed yet.
6688 -- Inspect the declarative items after the expression function looking
6689 -- for the pragma (if any).
6690
6691 if No (Prag) and then Is_Expression_Function (Subp) then
6692 Decl := Next (Unit_Declaration_Node (Subp));
6693 while Present (Decl) loop
6694 if Nkind (Decl) = N_Pragma
6695 and then Pragma_Name (Decl) = Name_Extensions_Visible
6696 then
6697 Prag := Decl;
6698 exit;
6699
6700 -- A source construct ends the region where Extensions_Visible may
6701 -- appear, stop the traversal. An expanded expression function is
6702 -- no longer a source construct, but it must still be recognized.
6703
6704 elsif Comes_From_Source (Decl)
6705 or else
6706 (Nkind_In (Decl, N_Subprogram_Body,
6707 N_Subprogram_Declaration)
6708 and then Is_Expression_Function (Defining_Entity (Decl)))
6709 then
6710 exit;
6711 end if;
6712
6713 Next (Decl);
6714 end loop;
6715 end if;
6716
6717 -- Extract the value from the Boolean expression (if any)
6718
6719 if Present (Prag) then
6720 Arg := First (Pragma_Argument_Associations (Prag));
6721
6722 if Present (Arg) then
6723 Expr := Get_Pragma_Arg (Arg);
6724
6725 -- When the associated subprogram is an expression function, the
6726 -- argument of the pragma may not have been analyzed.
6727
6728 if not Analyzed (Expr) then
6729 Preanalyze_And_Resolve (Expr, Standard_Boolean);
6730 end if;
6731
6732 -- Guard against cascading errors when the argument of pragma
6733 -- Extensions_Visible is not a valid static Boolean expression.
6734
6735 if Error_Posted (Expr) then
6736 return Extensions_Visible_None;
6737
6738 elsif Is_True (Expr_Value (Expr)) then
6739 return Extensions_Visible_True;
6740
6741 else
6742 return Extensions_Visible_False;
6743 end if;
6744
6745 -- Otherwise the aspect or pragma defaults to True
6746
6747 else
6748 return Extensions_Visible_True;
6749 end if;
6750
6751 -- Otherwise aspect or pragma Extensions_Visible is not inherited or
6752 -- directly specified. In SPARK code, its value defaults to "False".
6753
6754 elsif SPARK_Mode = On then
6755 return Extensions_Visible_False;
6756
6757 -- In non-SPARK code, aspect or pragma Extensions_Visible defaults to
6758 -- "True".
6759
6760 else
6761 return Extensions_Visible_True;
6762 end if;
6763 end Extensions_Visible_Status;
6764
6765 -----------------
6766 -- Find_Actual --
6767 -----------------
6768
6769 procedure Find_Actual
6770 (N : Node_Id;
6771 Formal : out Entity_Id;
6772 Call : out Node_Id)
6773 is
6774 Context : constant Node_Id := Parent (N);
6775 Actual : Node_Id;
6776 Call_Nam : Node_Id;
6777
6778 begin
6779 if Nkind_In (Context, N_Indexed_Component, N_Selected_Component)
6780 and then N = Prefix (Context)
6781 then
6782 Find_Actual (Context, Formal, Call);
6783 return;
6784
6785 elsif Nkind (Context) = N_Parameter_Association
6786 and then N = Explicit_Actual_Parameter (Context)
6787 then
6788 Call := Parent (Context);
6789
6790 elsif Nkind_In (Context, N_Entry_Call_Statement,
6791 N_Function_Call,
6792 N_Procedure_Call_Statement)
6793 then
6794 Call := Context;
6795
6796 else
6797 Formal := Empty;
6798 Call := Empty;
6799 return;
6800 end if;
6801
6802 -- If we have a call to a subprogram look for the parameter. Note that
6803 -- we exclude overloaded calls, since we don't know enough to be sure
6804 -- of giving the right answer in this case.
6805
6806 if Nkind_In (Call, N_Entry_Call_Statement,
6807 N_Function_Call,
6808 N_Procedure_Call_Statement)
6809 then
6810 Call_Nam := Name (Call);
6811
6812 -- A call to a protected or task entry appears as a selected
6813 -- component rather than an expanded name.
6814
6815 if Nkind (Call_Nam) = N_Selected_Component then
6816 Call_Nam := Selector_Name (Call_Nam);
6817 end if;
6818
6819 if Is_Entity_Name (Call_Nam)
6820 and then Present (Entity (Call_Nam))
6821 and then Is_Overloadable (Entity (Call_Nam))
6822 and then not Is_Overloaded (Call_Nam)
6823 then
6824 -- If node is name in call it is not an actual
6825
6826 if N = Call_Nam then
6827 Formal := Empty;
6828 Call := Empty;
6829 return;
6830 end if;
6831
6832 -- Fall here if we are definitely a parameter
6833
6834 Actual := First_Actual (Call);
6835 Formal := First_Formal (Entity (Call_Nam));
6836 while Present (Formal) and then Present (Actual) loop
6837 if Actual = N then
6838 return;
6839
6840 -- An actual that is the prefix in a prefixed call may have
6841 -- been rewritten in the call, after the deferred reference
6842 -- was collected. Check if sloc and kinds and names match.
6843
6844 elsif Sloc (Actual) = Sloc (N)
6845 and then Nkind (Actual) = N_Identifier
6846 and then Nkind (Actual) = Nkind (N)
6847 and then Chars (Actual) = Chars (N)
6848 then
6849 return;
6850
6851 else
6852 Actual := Next_Actual (Actual);
6853 Formal := Next_Formal (Formal);
6854 end if;
6855 end loop;
6856 end if;
6857 end if;
6858
6859 -- Fall through here if we did not find matching actual
6860
6861 Formal := Empty;
6862 Call := Empty;
6863 end Find_Actual;
6864
6865 ---------------------------
6866 -- Find_Body_Discriminal --
6867 ---------------------------
6868
6869 function Find_Body_Discriminal
6870 (Spec_Discriminant : Entity_Id) return Entity_Id
6871 is
6872 Tsk : Entity_Id;
6873 Disc : Entity_Id;
6874
6875 begin
6876 -- If expansion is suppressed, then the scope can be the concurrent type
6877 -- itself rather than a corresponding concurrent record type.
6878
6879 if Is_Concurrent_Type (Scope (Spec_Discriminant)) then
6880 Tsk := Scope (Spec_Discriminant);
6881
6882 else
6883 pragma Assert (Is_Concurrent_Record_Type (Scope (Spec_Discriminant)));
6884
6885 Tsk := Corresponding_Concurrent_Type (Scope (Spec_Discriminant));
6886 end if;
6887
6888 -- Find discriminant of original concurrent type, and use its current
6889 -- discriminal, which is the renaming within the task/protected body.
6890
6891 Disc := First_Discriminant (Tsk);
6892 while Present (Disc) loop
6893 if Chars (Disc) = Chars (Spec_Discriminant) then
6894 return Discriminal (Disc);
6895 end if;
6896
6897 Next_Discriminant (Disc);
6898 end loop;
6899
6900 -- That loop should always succeed in finding a matching entry and
6901 -- returning. Fatal error if not.
6902
6903 raise Program_Error;
6904 end Find_Body_Discriminal;
6905
6906 -------------------------------------
6907 -- Find_Corresponding_Discriminant --
6908 -------------------------------------
6909
6910 function Find_Corresponding_Discriminant
6911 (Id : Node_Id;
6912 Typ : Entity_Id) return Entity_Id
6913 is
6914 Par_Disc : Entity_Id;
6915 Old_Disc : Entity_Id;
6916 New_Disc : Entity_Id;
6917
6918 begin
6919 Par_Disc := Original_Record_Component (Original_Discriminant (Id));
6920
6921 -- The original type may currently be private, and the discriminant
6922 -- only appear on its full view.
6923
6924 if Is_Private_Type (Scope (Par_Disc))
6925 and then not Has_Discriminants (Scope (Par_Disc))
6926 and then Present (Full_View (Scope (Par_Disc)))
6927 then
6928 Old_Disc := First_Discriminant (Full_View (Scope (Par_Disc)));
6929 else
6930 Old_Disc := First_Discriminant (Scope (Par_Disc));
6931 end if;
6932
6933 if Is_Class_Wide_Type (Typ) then
6934 New_Disc := First_Discriminant (Root_Type (Typ));
6935 else
6936 New_Disc := First_Discriminant (Typ);
6937 end if;
6938
6939 while Present (Old_Disc) and then Present (New_Disc) loop
6940 if Old_Disc = Par_Disc then
6941 return New_Disc;
6942 end if;
6943
6944 Next_Discriminant (Old_Disc);
6945 Next_Discriminant (New_Disc);
6946 end loop;
6947
6948 -- Should always find it
6949
6950 raise Program_Error;
6951 end Find_Corresponding_Discriminant;
6952
6953 ----------------------------------
6954 -- Find_Enclosing_Iterator_Loop --
6955 ----------------------------------
6956
6957 function Find_Enclosing_Iterator_Loop (Id : Entity_Id) return Entity_Id is
6958 Constr : Node_Id;
6959 S : Entity_Id;
6960
6961 begin
6962 -- Traverse the scope chain looking for an iterator loop. Such loops are
6963 -- usually transformed into blocks, hence the use of Original_Node.
6964
6965 S := Id;
6966 while Present (S) and then S /= Standard_Standard loop
6967 if Ekind (S) = E_Loop
6968 and then Nkind (Parent (S)) = N_Implicit_Label_Declaration
6969 then
6970 Constr := Original_Node (Label_Construct (Parent (S)));
6971
6972 if Nkind (Constr) = N_Loop_Statement
6973 and then Present (Iteration_Scheme (Constr))
6974 and then Nkind (Iterator_Specification
6975 (Iteration_Scheme (Constr))) =
6976 N_Iterator_Specification
6977 then
6978 return S;
6979 end if;
6980 end if;
6981
6982 S := Scope (S);
6983 end loop;
6984
6985 return Empty;
6986 end Find_Enclosing_Iterator_Loop;
6987
6988 ------------------------------------
6989 -- Find_Loop_In_Conditional_Block --
6990 ------------------------------------
6991
6992 function Find_Loop_In_Conditional_Block (N : Node_Id) return Node_Id is
6993 Stmt : Node_Id;
6994
6995 begin
6996 Stmt := N;
6997
6998 if Nkind (Stmt) = N_If_Statement then
6999 Stmt := First (Then_Statements (Stmt));
7000 end if;
7001
7002 pragma Assert (Nkind (Stmt) = N_Block_Statement);
7003
7004 -- Inspect the statements of the conditional block. In general the loop
7005 -- should be the first statement in the statement sequence of the block,
7006 -- but the finalization machinery may have introduced extra object
7007 -- declarations.
7008
7009 Stmt := First (Statements (Handled_Statement_Sequence (Stmt)));
7010 while Present (Stmt) loop
7011 if Nkind (Stmt) = N_Loop_Statement then
7012 return Stmt;
7013 end if;
7014
7015 Next (Stmt);
7016 end loop;
7017
7018 -- The expansion of attribute 'Loop_Entry produced a malformed block
7019
7020 raise Program_Error;
7021 end Find_Loop_In_Conditional_Block;
7022
7023 --------------------------
7024 -- Find_Overlaid_Entity --
7025 --------------------------
7026
7027 procedure Find_Overlaid_Entity
7028 (N : Node_Id;
7029 Ent : out Entity_Id;
7030 Off : out Boolean)
7031 is
7032 Expr : Node_Id;
7033
7034 begin
7035 -- We are looking for one of the two following forms:
7036
7037 -- for X'Address use Y'Address
7038
7039 -- or
7040
7041 -- Const : constant Address := expr;
7042 -- ...
7043 -- for X'Address use Const;
7044
7045 -- In the second case, the expr is either Y'Address, or recursively a
7046 -- constant that eventually references Y'Address.
7047
7048 Ent := Empty;
7049 Off := False;
7050
7051 if Nkind (N) = N_Attribute_Definition_Clause
7052 and then Chars (N) = Name_Address
7053 then
7054 Expr := Expression (N);
7055
7056 -- This loop checks the form of the expression for Y'Address,
7057 -- using recursion to deal with intermediate constants.
7058
7059 loop
7060 -- Check for Y'Address
7061
7062 if Nkind (Expr) = N_Attribute_Reference
7063 and then Attribute_Name (Expr) = Name_Address
7064 then
7065 Expr := Prefix (Expr);
7066 exit;
7067
7068 -- Check for Const where Const is a constant entity
7069
7070 elsif Is_Entity_Name (Expr)
7071 and then Ekind (Entity (Expr)) = E_Constant
7072 then
7073 Expr := Constant_Value (Entity (Expr));
7074
7075 -- Anything else does not need checking
7076
7077 else
7078 return;
7079 end if;
7080 end loop;
7081
7082 -- This loop checks the form of the prefix for an entity, using
7083 -- recursion to deal with intermediate components.
7084
7085 loop
7086 -- Check for Y where Y is an entity
7087
7088 if Is_Entity_Name (Expr) then
7089 Ent := Entity (Expr);
7090 return;
7091
7092 -- Check for components
7093
7094 elsif
7095 Nkind_In (Expr, N_Selected_Component, N_Indexed_Component)
7096 then
7097 Expr := Prefix (Expr);
7098 Off := True;
7099
7100 -- Anything else does not need checking
7101
7102 else
7103 return;
7104 end if;
7105 end loop;
7106 end if;
7107 end Find_Overlaid_Entity;
7108
7109 -------------------------
7110 -- Find_Parameter_Type --
7111 -------------------------
7112
7113 function Find_Parameter_Type (Param : Node_Id) return Entity_Id is
7114 begin
7115 if Nkind (Param) /= N_Parameter_Specification then
7116 return Empty;
7117
7118 -- For an access parameter, obtain the type from the formal entity
7119 -- itself, because access to subprogram nodes do not carry a type.
7120 -- Shouldn't we always use the formal entity ???
7121
7122 elsif Nkind (Parameter_Type (Param)) = N_Access_Definition then
7123 return Etype (Defining_Identifier (Param));
7124
7125 else
7126 return Etype (Parameter_Type (Param));
7127 end if;
7128 end Find_Parameter_Type;
7129
7130 -----------------------------------
7131 -- Find_Placement_In_State_Space --
7132 -----------------------------------
7133
7134 procedure Find_Placement_In_State_Space
7135 (Item_Id : Entity_Id;
7136 Placement : out State_Space_Kind;
7137 Pack_Id : out Entity_Id)
7138 is
7139 Context : Entity_Id;
7140
7141 begin
7142 -- Assume that the item does not appear in the state space of a package
7143
7144 Placement := Not_In_Package;
7145 Pack_Id := Empty;
7146
7147 -- Climb the scope stack and examine the enclosing context
7148
7149 Context := Scope (Item_Id);
7150 while Present (Context) and then Context /= Standard_Standard loop
7151 if Ekind (Context) = E_Package then
7152 Pack_Id := Context;
7153
7154 -- A package body is a cut off point for the traversal as the item
7155 -- cannot be visible to the outside from this point on. Note that
7156 -- this test must be done first as a body is also classified as a
7157 -- private part.
7158
7159 if In_Package_Body (Context) then
7160 Placement := Body_State_Space;
7161 return;
7162
7163 -- The private part of a package is a cut off point for the
7164 -- traversal as the item cannot be visible to the outside from
7165 -- this point on.
7166
7167 elsif In_Private_Part (Context) then
7168 Placement := Private_State_Space;
7169 return;
7170
7171 -- When the item appears in the visible state space of a package,
7172 -- continue to climb the scope stack as this may not be the final
7173 -- state space.
7174
7175 else
7176 Placement := Visible_State_Space;
7177
7178 -- The visible state space of a child unit acts as the proper
7179 -- placement of an item.
7180
7181 if Is_Child_Unit (Context) then
7182 return;
7183 end if;
7184 end if;
7185
7186 -- The item or its enclosing package appear in a construct that has
7187 -- no state space.
7188
7189 else
7190 Placement := Not_In_Package;
7191 return;
7192 end if;
7193
7194 Context := Scope (Context);
7195 end loop;
7196 end Find_Placement_In_State_Space;
7197
7198 ------------------------
7199 -- Find_Specific_Type --
7200 ------------------------
7201
7202 function Find_Specific_Type (CW : Entity_Id) return Entity_Id is
7203 Typ : Entity_Id := Root_Type (CW);
7204
7205 begin
7206 if Ekind (Typ) = E_Incomplete_Type then
7207 if From_Limited_With (Typ) then
7208 Typ := Non_Limited_View (Typ);
7209 else
7210 Typ := Full_View (Typ);
7211 end if;
7212 end if;
7213
7214 if Is_Private_Type (Typ)
7215 and then not Is_Tagged_Type (Typ)
7216 and then Present (Full_View (Typ))
7217 then
7218 return Full_View (Typ);
7219 else
7220 return Typ;
7221 end if;
7222 end Find_Specific_Type;
7223
7224 -----------------------------
7225 -- Find_Static_Alternative --
7226 -----------------------------
7227
7228 function Find_Static_Alternative (N : Node_Id) return Node_Id is
7229 Expr : constant Node_Id := Expression (N);
7230 Val : constant Uint := Expr_Value (Expr);
7231 Alt : Node_Id;
7232 Choice : Node_Id;
7233
7234 begin
7235 Alt := First (Alternatives (N));
7236
7237 Search : loop
7238 if Nkind (Alt) /= N_Pragma then
7239 Choice := First (Discrete_Choices (Alt));
7240 while Present (Choice) loop
7241
7242 -- Others choice, always matches
7243
7244 if Nkind (Choice) = N_Others_Choice then
7245 exit Search;
7246
7247 -- Range, check if value is in the range
7248
7249 elsif Nkind (Choice) = N_Range then
7250 exit Search when
7251 Val >= Expr_Value (Low_Bound (Choice))
7252 and then
7253 Val <= Expr_Value (High_Bound (Choice));
7254
7255 -- Choice is a subtype name. Note that we know it must
7256 -- be a static subtype, since otherwise it would have
7257 -- been diagnosed as illegal.
7258
7259 elsif Is_Entity_Name (Choice)
7260 and then Is_Type (Entity (Choice))
7261 then
7262 exit Search when Is_In_Range (Expr, Etype (Choice),
7263 Assume_Valid => False);
7264
7265 -- Choice is a subtype indication
7266
7267 elsif Nkind (Choice) = N_Subtype_Indication then
7268 declare
7269 C : constant Node_Id := Constraint (Choice);
7270 R : constant Node_Id := Range_Expression (C);
7271
7272 begin
7273 exit Search when
7274 Val >= Expr_Value (Low_Bound (R))
7275 and then
7276 Val <= Expr_Value (High_Bound (R));
7277 end;
7278
7279 -- Choice is a simple expression
7280
7281 else
7282 exit Search when Val = Expr_Value (Choice);
7283 end if;
7284
7285 Next (Choice);
7286 end loop;
7287 end if;
7288
7289 Next (Alt);
7290 pragma Assert (Present (Alt));
7291 end loop Search;
7292
7293 -- The above loop *must* terminate by finding a match, since
7294 -- we know the case statement is valid, and the value of the
7295 -- expression is known at compile time. When we fall out of
7296 -- the loop, Alt points to the alternative that we know will
7297 -- be selected at run time.
7298
7299 return Alt;
7300 end Find_Static_Alternative;
7301
7302 ------------------
7303 -- First_Actual --
7304 ------------------
7305
7306 function First_Actual (Node : Node_Id) return Node_Id is
7307 N : Node_Id;
7308
7309 begin
7310 if No (Parameter_Associations (Node)) then
7311 return Empty;
7312 end if;
7313
7314 N := First (Parameter_Associations (Node));
7315
7316 if Nkind (N) = N_Parameter_Association then
7317 return First_Named_Actual (Node);
7318 else
7319 return N;
7320 end if;
7321 end First_Actual;
7322
7323 -------------
7324 -- Fix_Msg --
7325 -------------
7326
7327 function Fix_Msg (Id : Entity_Id; Msg : String) return String is
7328 Is_Task : constant Boolean :=
7329 Ekind_In (Id, E_Task_Body, E_Task_Type)
7330 or else Is_Single_Task_Object (Id);
7331 Msg_Last : constant Natural := Msg'Last;
7332 Msg_Index : Natural;
7333 Res : String (Msg'Range) := (others => ' ');
7334 Res_Index : Natural;
7335
7336 begin
7337 -- Copy all characters from the input message Msg to result Res with
7338 -- suitable replacements.
7339
7340 Msg_Index := Msg'First;
7341 Res_Index := Res'First;
7342 while Msg_Index <= Msg_Last loop
7343
7344 -- Replace "subprogram" with a different word
7345
7346 if Msg_Index <= Msg_Last - 10
7347 and then Msg (Msg_Index .. Msg_Index + 9) = "subprogram"
7348 then
7349 if Ekind_In (Id, E_Entry, E_Entry_Family) then
7350 Res (Res_Index .. Res_Index + 4) := "entry";
7351 Res_Index := Res_Index + 5;
7352
7353 elsif Is_Task then
7354 Res (Res_Index .. Res_Index + 8) := "task type";
7355 Res_Index := Res_Index + 9;
7356
7357 else
7358 Res (Res_Index .. Res_Index + 9) := "subprogram";
7359 Res_Index := Res_Index + 10;
7360 end if;
7361
7362 Msg_Index := Msg_Index + 10;
7363
7364 -- Replace "protected" with a different word
7365
7366 elsif Msg_Index <= Msg_Last - 9
7367 and then Msg (Msg_Index .. Msg_Index + 8) = "protected"
7368 and then Is_Task
7369 then
7370 Res (Res_Index .. Res_Index + 3) := "task";
7371 Res_Index := Res_Index + 4;
7372 Msg_Index := Msg_Index + 9;
7373
7374 -- Otherwise copy the character
7375
7376 else
7377 Res (Res_Index) := Msg (Msg_Index);
7378 Msg_Index := Msg_Index + 1;
7379 Res_Index := Res_Index + 1;
7380 end if;
7381 end loop;
7382
7383 return Res (Res'First .. Res_Index - 1);
7384 end Fix_Msg;
7385
7386 -----------------------
7387 -- Gather_Components --
7388 -----------------------
7389
7390 procedure Gather_Components
7391 (Typ : Entity_Id;
7392 Comp_List : Node_Id;
7393 Governed_By : List_Id;
7394 Into : Elist_Id;
7395 Report_Errors : out Boolean)
7396 is
7397 Assoc : Node_Id;
7398 Variant : Node_Id;
7399 Discrete_Choice : Node_Id;
7400 Comp_Item : Node_Id;
7401
7402 Discrim : Entity_Id;
7403 Discrim_Name : Node_Id;
7404 Discrim_Value : Node_Id;
7405
7406 begin
7407 Report_Errors := False;
7408
7409 if No (Comp_List) or else Null_Present (Comp_List) then
7410 return;
7411
7412 elsif Present (Component_Items (Comp_List)) then
7413 Comp_Item := First (Component_Items (Comp_List));
7414
7415 else
7416 Comp_Item := Empty;
7417 end if;
7418
7419 while Present (Comp_Item) loop
7420
7421 -- Skip the tag of a tagged record, the interface tags, as well
7422 -- as all items that are not user components (anonymous types,
7423 -- rep clauses, Parent field, controller field).
7424
7425 if Nkind (Comp_Item) = N_Component_Declaration then
7426 declare
7427 Comp : constant Entity_Id := Defining_Identifier (Comp_Item);
7428 begin
7429 if not Is_Tag (Comp) and then Chars (Comp) /= Name_uParent then
7430 Append_Elmt (Comp, Into);
7431 end if;
7432 end;
7433 end if;
7434
7435 Next (Comp_Item);
7436 end loop;
7437
7438 if No (Variant_Part (Comp_List)) then
7439 return;
7440 else
7441 Discrim_Name := Name (Variant_Part (Comp_List));
7442 Variant := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
7443 end if;
7444
7445 -- Look for the discriminant that governs this variant part.
7446 -- The discriminant *must* be in the Governed_By List
7447
7448 Assoc := First (Governed_By);
7449 Find_Constraint : loop
7450 Discrim := First (Choices (Assoc));
7451 exit Find_Constraint when Chars (Discrim_Name) = Chars (Discrim)
7452 or else (Present (Corresponding_Discriminant (Entity (Discrim)))
7453 and then
7454 Chars (Corresponding_Discriminant (Entity (Discrim))) =
7455 Chars (Discrim_Name))
7456 or else Chars (Original_Record_Component (Entity (Discrim)))
7457 = Chars (Discrim_Name);
7458
7459 if No (Next (Assoc)) then
7460 if not Is_Constrained (Typ)
7461 and then Is_Derived_Type (Typ)
7462 and then Present (Stored_Constraint (Typ))
7463 then
7464 -- If the type is a tagged type with inherited discriminants,
7465 -- use the stored constraint on the parent in order to find
7466 -- the values of discriminants that are otherwise hidden by an
7467 -- explicit constraint. Renamed discriminants are handled in
7468 -- the code above.
7469
7470 -- If several parent discriminants are renamed by a single
7471 -- discriminant of the derived type, the call to obtain the
7472 -- Corresponding_Discriminant field only retrieves the last
7473 -- of them. We recover the constraint on the others from the
7474 -- Stored_Constraint as well.
7475
7476 declare
7477 D : Entity_Id;
7478 C : Elmt_Id;
7479
7480 begin
7481 D := First_Discriminant (Etype (Typ));
7482 C := First_Elmt (Stored_Constraint (Typ));
7483 while Present (D) and then Present (C) loop
7484 if Chars (Discrim_Name) = Chars (D) then
7485 if Is_Entity_Name (Node (C))
7486 and then Entity (Node (C)) = Entity (Discrim)
7487 then
7488 -- D is renamed by Discrim, whose value is given in
7489 -- Assoc.
7490
7491 null;
7492
7493 else
7494 Assoc :=
7495 Make_Component_Association (Sloc (Typ),
7496 New_List
7497 (New_Occurrence_Of (D, Sloc (Typ))),
7498 Duplicate_Subexpr_No_Checks (Node (C)));
7499 end if;
7500 exit Find_Constraint;
7501 end if;
7502
7503 Next_Discriminant (D);
7504 Next_Elmt (C);
7505 end loop;
7506 end;
7507 end if;
7508 end if;
7509
7510 if No (Next (Assoc)) then
7511 Error_Msg_NE (" missing value for discriminant&",
7512 First (Governed_By), Discrim_Name);
7513 Report_Errors := True;
7514 return;
7515 end if;
7516
7517 Next (Assoc);
7518 end loop Find_Constraint;
7519
7520 Discrim_Value := Expression (Assoc);
7521
7522 if not Is_OK_Static_Expression (Discrim_Value) then
7523
7524 -- If the variant part is governed by a discriminant of the type
7525 -- this is an error. If the variant part and the discriminant are
7526 -- inherited from an ancestor this is legal (AI05-120) unless the
7527 -- components are being gathered for an aggregate, in which case
7528 -- the caller must check Report_Errors.
7529
7530 if Scope (Original_Record_Component
7531 ((Entity (First (Choices (Assoc)))))) = Typ
7532 then
7533 Error_Msg_FE
7534 ("value for discriminant & must be static!",
7535 Discrim_Value, Discrim);
7536 Why_Not_Static (Discrim_Value);
7537 end if;
7538
7539 Report_Errors := True;
7540 return;
7541 end if;
7542
7543 Search_For_Discriminant_Value : declare
7544 Low : Node_Id;
7545 High : Node_Id;
7546
7547 UI_High : Uint;
7548 UI_Low : Uint;
7549 UI_Discrim_Value : constant Uint := Expr_Value (Discrim_Value);
7550
7551 begin
7552 Find_Discrete_Value : while Present (Variant) loop
7553 Discrete_Choice := First (Discrete_Choices (Variant));
7554 while Present (Discrete_Choice) loop
7555 exit Find_Discrete_Value when
7556 Nkind (Discrete_Choice) = N_Others_Choice;
7557
7558 Get_Index_Bounds (Discrete_Choice, Low, High);
7559
7560 UI_Low := Expr_Value (Low);
7561 UI_High := Expr_Value (High);
7562
7563 exit Find_Discrete_Value when
7564 UI_Low <= UI_Discrim_Value
7565 and then
7566 UI_High >= UI_Discrim_Value;
7567
7568 Next (Discrete_Choice);
7569 end loop;
7570
7571 Next_Non_Pragma (Variant);
7572 end loop Find_Discrete_Value;
7573 end Search_For_Discriminant_Value;
7574
7575 -- The case statement must include a variant that corresponds to the
7576 -- value of the discriminant, unless the discriminant type has a
7577 -- static predicate. In that case the absence of an others_choice that
7578 -- would cover this value becomes a run-time error (3.8,1 (21.1/2)).
7579
7580 if No (Variant)
7581 and then not Has_Static_Predicate (Etype (Discrim_Name))
7582 then
7583 Error_Msg_NE
7584 ("value of discriminant & is out of range", Discrim_Value, Discrim);
7585 Report_Errors := True;
7586 return;
7587 end if;
7588
7589 -- If we have found the corresponding choice, recursively add its
7590 -- components to the Into list. The nested components are part of
7591 -- the same record type.
7592
7593 if Present (Variant) then
7594 Gather_Components
7595 (Typ, Component_List (Variant), Governed_By, Into, Report_Errors);
7596 end if;
7597 end Gather_Components;
7598
7599 ------------------------
7600 -- Get_Actual_Subtype --
7601 ------------------------
7602
7603 function Get_Actual_Subtype (N : Node_Id) return Entity_Id is
7604 Typ : constant Entity_Id := Etype (N);
7605 Utyp : Entity_Id := Underlying_Type (Typ);
7606 Decl : Node_Id;
7607 Atyp : Entity_Id;
7608
7609 begin
7610 if No (Utyp) then
7611 Utyp := Typ;
7612 end if;
7613
7614 -- If what we have is an identifier that references a subprogram
7615 -- formal, or a variable or constant object, then we get the actual
7616 -- subtype from the referenced entity if one has been built.
7617
7618 if Nkind (N) = N_Identifier
7619 and then
7620 (Is_Formal (Entity (N))
7621 or else Ekind (Entity (N)) = E_Constant
7622 or else Ekind (Entity (N)) = E_Variable)
7623 and then Present (Actual_Subtype (Entity (N)))
7624 then
7625 return Actual_Subtype (Entity (N));
7626
7627 -- Actual subtype of unchecked union is always itself. We never need
7628 -- the "real" actual subtype. If we did, we couldn't get it anyway
7629 -- because the discriminant is not available. The restrictions on
7630 -- Unchecked_Union are designed to make sure that this is OK.
7631
7632 elsif Is_Unchecked_Union (Base_Type (Utyp)) then
7633 return Typ;
7634
7635 -- Here for the unconstrained case, we must find actual subtype
7636 -- No actual subtype is available, so we must build it on the fly.
7637
7638 -- Checking the type, not the underlying type, for constrainedness
7639 -- seems to be necessary. Maybe all the tests should be on the type???
7640
7641 elsif (not Is_Constrained (Typ))
7642 and then (Is_Array_Type (Utyp)
7643 or else (Is_Record_Type (Utyp)
7644 and then Has_Discriminants (Utyp)))
7645 and then not Has_Unknown_Discriminants (Utyp)
7646 and then not (Ekind (Utyp) = E_String_Literal_Subtype)
7647 then
7648 -- Nothing to do if in spec expression (why not???)
7649
7650 if In_Spec_Expression then
7651 return Typ;
7652
7653 elsif Is_Private_Type (Typ) and then not Has_Discriminants (Typ) then
7654
7655 -- If the type has no discriminants, there is no subtype to
7656 -- build, even if the underlying type is discriminated.
7657
7658 return Typ;
7659
7660 -- Else build the actual subtype
7661
7662 else
7663 Decl := Build_Actual_Subtype (Typ, N);
7664 Atyp := Defining_Identifier (Decl);
7665
7666 -- If Build_Actual_Subtype generated a new declaration then use it
7667
7668 if Atyp /= Typ then
7669
7670 -- The actual subtype is an Itype, so analyze the declaration,
7671 -- but do not attach it to the tree, to get the type defined.
7672
7673 Set_Parent (Decl, N);
7674 Set_Is_Itype (Atyp);
7675 Analyze (Decl, Suppress => All_Checks);
7676 Set_Associated_Node_For_Itype (Atyp, N);
7677 Set_Has_Delayed_Freeze (Atyp, False);
7678
7679 -- We need to freeze the actual subtype immediately. This is
7680 -- needed, because otherwise this Itype will not get frozen
7681 -- at all, and it is always safe to freeze on creation because
7682 -- any associated types must be frozen at this point.
7683
7684 Freeze_Itype (Atyp, N);
7685 return Atyp;
7686
7687 -- Otherwise we did not build a declaration, so return original
7688
7689 else
7690 return Typ;
7691 end if;
7692 end if;
7693
7694 -- For all remaining cases, the actual subtype is the same as
7695 -- the nominal type.
7696
7697 else
7698 return Typ;
7699 end if;
7700 end Get_Actual_Subtype;
7701
7702 -------------------------------------
7703 -- Get_Actual_Subtype_If_Available --
7704 -------------------------------------
7705
7706 function Get_Actual_Subtype_If_Available (N : Node_Id) return Entity_Id is
7707 Typ : constant Entity_Id := Etype (N);
7708
7709 begin
7710 -- If what we have is an identifier that references a subprogram
7711 -- formal, or a variable or constant object, then we get the actual
7712 -- subtype from the referenced entity if one has been built.
7713
7714 if Nkind (N) = N_Identifier
7715 and then
7716 (Is_Formal (Entity (N))
7717 or else Ekind (Entity (N)) = E_Constant
7718 or else Ekind (Entity (N)) = E_Variable)
7719 and then Present (Actual_Subtype (Entity (N)))
7720 then
7721 return Actual_Subtype (Entity (N));
7722
7723 -- Otherwise the Etype of N is returned unchanged
7724
7725 else
7726 return Typ;
7727 end if;
7728 end Get_Actual_Subtype_If_Available;
7729
7730 ------------------------
7731 -- Get_Body_From_Stub --
7732 ------------------------
7733
7734 function Get_Body_From_Stub (N : Node_Id) return Node_Id is
7735 begin
7736 return Proper_Body (Unit (Library_Unit (N)));
7737 end Get_Body_From_Stub;
7738
7739 ---------------------
7740 -- Get_Cursor_Type --
7741 ---------------------
7742
7743 function Get_Cursor_Type
7744 (Aspect : Node_Id;
7745 Typ : Entity_Id) return Entity_Id
7746 is
7747 Assoc : Node_Id;
7748 Func : Entity_Id;
7749 First_Op : Entity_Id;
7750 Cursor : Entity_Id;
7751
7752 begin
7753 -- If error already detected, return
7754
7755 if Error_Posted (Aspect) then
7756 return Any_Type;
7757 end if;
7758
7759 -- The cursor type for an Iterable aspect is the return type of a
7760 -- non-overloaded First primitive operation. Locate association for
7761 -- First.
7762
7763 Assoc := First (Component_Associations (Expression (Aspect)));
7764 First_Op := Any_Id;
7765 while Present (Assoc) loop
7766 if Chars (First (Choices (Assoc))) = Name_First then
7767 First_Op := Expression (Assoc);
7768 exit;
7769 end if;
7770
7771 Next (Assoc);
7772 end loop;
7773
7774 if First_Op = Any_Id then
7775 Error_Msg_N ("aspect Iterable must specify First operation", Aspect);
7776 return Any_Type;
7777 end if;
7778
7779 Cursor := Any_Type;
7780
7781 -- Locate function with desired name and profile in scope of type
7782 -- In the rare case where the type is an integer type, a base type
7783 -- is created for it, check that the base type of the first formal
7784 -- of First matches the base type of the domain.
7785
7786 Func := First_Entity (Scope (Typ));
7787 while Present (Func) loop
7788 if Chars (Func) = Chars (First_Op)
7789 and then Ekind (Func) = E_Function
7790 and then Present (First_Formal (Func))
7791 and then Base_Type (Etype (First_Formal (Func))) = Base_Type (Typ)
7792 and then No (Next_Formal (First_Formal (Func)))
7793 then
7794 if Cursor /= Any_Type then
7795 Error_Msg_N
7796 ("Operation First for iterable type must be unique", Aspect);
7797 return Any_Type;
7798 else
7799 Cursor := Etype (Func);
7800 end if;
7801 end if;
7802
7803 Next_Entity (Func);
7804 end loop;
7805
7806 -- If not found, no way to resolve remaining primitives.
7807
7808 if Cursor = Any_Type then
7809 Error_Msg_N
7810 ("No legal primitive operation First for Iterable type", Aspect);
7811 end if;
7812
7813 return Cursor;
7814 end Get_Cursor_Type;
7815
7816 function Get_Cursor_Type (Typ : Entity_Id) return Entity_Id is
7817 begin
7818 return Etype (Get_Iterable_Type_Primitive (Typ, Name_First));
7819 end Get_Cursor_Type;
7820
7821 -------------------------------
7822 -- Get_Default_External_Name --
7823 -------------------------------
7824
7825 function Get_Default_External_Name (E : Node_Or_Entity_Id) return Node_Id is
7826 begin
7827 Get_Decoded_Name_String (Chars (E));
7828
7829 if Opt.External_Name_Imp_Casing = Uppercase then
7830 Set_Casing (All_Upper_Case);
7831 else
7832 Set_Casing (All_Lower_Case);
7833 end if;
7834
7835 return
7836 Make_String_Literal (Sloc (E),
7837 Strval => String_From_Name_Buffer);
7838 end Get_Default_External_Name;
7839
7840 --------------------------
7841 -- Get_Enclosing_Object --
7842 --------------------------
7843
7844 function Get_Enclosing_Object (N : Node_Id) return Entity_Id is
7845 begin
7846 if Is_Entity_Name (N) then
7847 return Entity (N);
7848 else
7849 case Nkind (N) is
7850 when N_Indexed_Component |
7851 N_Slice |
7852 N_Selected_Component =>
7853
7854 -- If not generating code, a dereference may be left implicit.
7855 -- In thoses cases, return Empty.
7856
7857 if Is_Access_Type (Etype (Prefix (N))) then
7858 return Empty;
7859 else
7860 return Get_Enclosing_Object (Prefix (N));
7861 end if;
7862
7863 when N_Type_Conversion =>
7864 return Get_Enclosing_Object (Expression (N));
7865
7866 when others =>
7867 return Empty;
7868 end case;
7869 end if;
7870 end Get_Enclosing_Object;
7871
7872 ---------------------------
7873 -- Get_Enum_Lit_From_Pos --
7874 ---------------------------
7875
7876 function Get_Enum_Lit_From_Pos
7877 (T : Entity_Id;
7878 Pos : Uint;
7879 Loc : Source_Ptr) return Node_Id
7880 is
7881 Btyp : Entity_Id := Base_Type (T);
7882 Lit : Node_Id;
7883 LLoc : Source_Ptr;
7884
7885 begin
7886 -- In the case where the literal is of type Character, Wide_Character
7887 -- or Wide_Wide_Character or of a type derived from them, there needs
7888 -- to be some special handling since there is no explicit chain of
7889 -- literals to search. Instead, an N_Character_Literal node is created
7890 -- with the appropriate Char_Code and Chars fields.
7891
7892 if Is_Standard_Character_Type (T) then
7893 Set_Character_Literal_Name (UI_To_CC (Pos));
7894
7895 return
7896 Make_Character_Literal (Loc,
7897 Chars => Name_Find,
7898 Char_Literal_Value => Pos);
7899
7900 -- For all other cases, we have a complete table of literals, and
7901 -- we simply iterate through the chain of literal until the one
7902 -- with the desired position value is found.
7903
7904 else
7905 if Is_Private_Type (Btyp) and then Present (Full_View (Btyp)) then
7906 Btyp := Full_View (Btyp);
7907 end if;
7908
7909 Lit := First_Literal (Btyp);
7910 for J in 1 .. UI_To_Int (Pos) loop
7911 Next_Literal (Lit);
7912
7913 -- If Lit is Empty, Pos is not in range, so raise Constraint_Error
7914 -- inside the loop to avoid calling Next_Literal on Empty.
7915
7916 if No (Lit) then
7917 raise Constraint_Error;
7918 end if;
7919 end loop;
7920
7921 -- Create a new node from Lit, with source location provided by Loc
7922 -- if not equal to No_Location, or by copying the source location of
7923 -- Lit otherwise.
7924
7925 LLoc := Loc;
7926
7927 if LLoc = No_Location then
7928 LLoc := Sloc (Lit);
7929 end if;
7930
7931 return New_Occurrence_Of (Lit, LLoc);
7932 end if;
7933 end Get_Enum_Lit_From_Pos;
7934
7935 ------------------------
7936 -- Get_Generic_Entity --
7937 ------------------------
7938
7939 function Get_Generic_Entity (N : Node_Id) return Entity_Id is
7940 Ent : constant Entity_Id := Entity (Name (N));
7941 begin
7942 if Present (Renamed_Object (Ent)) then
7943 return Renamed_Object (Ent);
7944 else
7945 return Ent;
7946 end if;
7947 end Get_Generic_Entity;
7948
7949 -------------------------------------
7950 -- Get_Incomplete_View_Of_Ancestor --
7951 -------------------------------------
7952
7953 function Get_Incomplete_View_Of_Ancestor (E : Entity_Id) return Entity_Id is
7954 Cur_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
7955 Par_Scope : Entity_Id;
7956 Par_Type : Entity_Id;
7957
7958 begin
7959 -- The incomplete view of an ancestor is only relevant for private
7960 -- derived types in child units.
7961
7962 if not Is_Derived_Type (E)
7963 or else not Is_Child_Unit (Cur_Unit)
7964 then
7965 return Empty;
7966
7967 else
7968 Par_Scope := Scope (Cur_Unit);
7969 if No (Par_Scope) then
7970 return Empty;
7971 end if;
7972
7973 Par_Type := Etype (Base_Type (E));
7974
7975 -- Traverse list of ancestor types until we find one declared in
7976 -- a parent or grandparent unit (two levels seem sufficient).
7977
7978 while Present (Par_Type) loop
7979 if Scope (Par_Type) = Par_Scope
7980 or else Scope (Par_Type) = Scope (Par_Scope)
7981 then
7982 return Par_Type;
7983
7984 elsif not Is_Derived_Type (Par_Type) then
7985 return Empty;
7986
7987 else
7988 Par_Type := Etype (Base_Type (Par_Type));
7989 end if;
7990 end loop;
7991
7992 -- If none found, there is no relevant ancestor type.
7993
7994 return Empty;
7995 end if;
7996 end Get_Incomplete_View_Of_Ancestor;
7997
7998 ----------------------
7999 -- Get_Index_Bounds --
8000 ----------------------
8001
8002 procedure Get_Index_Bounds (N : Node_Id; L, H : out Node_Id) is
8003 Kind : constant Node_Kind := Nkind (N);
8004 R : Node_Id;
8005
8006 begin
8007 if Kind = N_Range then
8008 L := Low_Bound (N);
8009 H := High_Bound (N);
8010
8011 elsif Kind = N_Subtype_Indication then
8012 R := Range_Expression (Constraint (N));
8013
8014 if R = Error then
8015 L := Error;
8016 H := Error;
8017 return;
8018
8019 else
8020 L := Low_Bound (Range_Expression (Constraint (N)));
8021 H := High_Bound (Range_Expression (Constraint (N)));
8022 end if;
8023
8024 elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
8025 if Error_Posted (Scalar_Range (Entity (N))) then
8026 L := Error;
8027 H := Error;
8028
8029 elsif Nkind (Scalar_Range (Entity (N))) = N_Subtype_Indication then
8030 Get_Index_Bounds (Scalar_Range (Entity (N)), L, H);
8031
8032 else
8033 L := Low_Bound (Scalar_Range (Entity (N)));
8034 H := High_Bound (Scalar_Range (Entity (N)));
8035 end if;
8036
8037 else
8038 -- N is an expression, indicating a range with one value
8039
8040 L := N;
8041 H := N;
8042 end if;
8043 end Get_Index_Bounds;
8044
8045 ---------------------------------
8046 -- Get_Iterable_Type_Primitive --
8047 ---------------------------------
8048
8049 function Get_Iterable_Type_Primitive
8050 (Typ : Entity_Id;
8051 Nam : Name_Id) return Entity_Id
8052 is
8053 Funcs : constant Node_Id := Find_Value_Of_Aspect (Typ, Aspect_Iterable);
8054 Assoc : Node_Id;
8055
8056 begin
8057 if No (Funcs) then
8058 return Empty;
8059
8060 else
8061 Assoc := First (Component_Associations (Funcs));
8062 while Present (Assoc) loop
8063 if Chars (First (Choices (Assoc))) = Nam then
8064 return Entity (Expression (Assoc));
8065 end if;
8066
8067 Assoc := Next (Assoc);
8068 end loop;
8069
8070 return Empty;
8071 end if;
8072 end Get_Iterable_Type_Primitive;
8073
8074 ----------------------------------
8075 -- Get_Library_Unit_Name_string --
8076 ----------------------------------
8077
8078 procedure Get_Library_Unit_Name_String (Decl_Node : Node_Id) is
8079 Unit_Name_Id : constant Unit_Name_Type := Get_Unit_Name (Decl_Node);
8080
8081 begin
8082 Get_Unit_Name_String (Unit_Name_Id);
8083
8084 -- Remove seven last character (" (spec)" or " (body)")
8085
8086 Name_Len := Name_Len - 7;
8087 pragma Assert (Name_Buffer (Name_Len + 1) = ' ');
8088 end Get_Library_Unit_Name_String;
8089
8090 --------------------------
8091 -- Get_Max_Queue_Length --
8092 --------------------------
8093
8094 function Get_Max_Queue_Length (Id : Entity_Id) return Uint is
8095 pragma Assert (Is_Entry (Id));
8096 Prag : constant Entity_Id := Get_Pragma (Id, Pragma_Max_Queue_Length);
8097
8098 begin
8099 -- A value of 0 represents no maximum specified, and entries and entry
8100 -- families with no Max_Queue_Length aspect or pragma default to it.
8101
8102 if not Present (Prag) then
8103 return Uint_0;
8104 end if;
8105
8106 return Intval (Expression (First (Pragma_Argument_Associations (Prag))));
8107 end Get_Max_Queue_Length;
8108
8109 ------------------------
8110 -- Get_Name_Entity_Id --
8111 ------------------------
8112
8113 function Get_Name_Entity_Id (Id : Name_Id) return Entity_Id is
8114 begin
8115 return Entity_Id (Get_Name_Table_Int (Id));
8116 end Get_Name_Entity_Id;
8117
8118 ------------------------------
8119 -- Get_Name_From_CTC_Pragma --
8120 ------------------------------
8121
8122 function Get_Name_From_CTC_Pragma (N : Node_Id) return String_Id is
8123 Arg : constant Node_Id :=
8124 Get_Pragma_Arg (First (Pragma_Argument_Associations (N)));
8125 begin
8126 return Strval (Expr_Value_S (Arg));
8127 end Get_Name_From_CTC_Pragma;
8128
8129 -----------------------
8130 -- Get_Parent_Entity --
8131 -----------------------
8132
8133 function Get_Parent_Entity (Unit : Node_Id) return Entity_Id is
8134 begin
8135 if Nkind (Unit) = N_Package_Body
8136 and then Nkind (Original_Node (Unit)) = N_Package_Instantiation
8137 then
8138 return Defining_Entity
8139 (Specification (Instance_Spec (Original_Node (Unit))));
8140 elsif Nkind (Unit) = N_Package_Instantiation then
8141 return Defining_Entity (Specification (Instance_Spec (Unit)));
8142 else
8143 return Defining_Entity (Unit);
8144 end if;
8145 end Get_Parent_Entity;
8146
8147 -------------------
8148 -- Get_Pragma_Id --
8149 -------------------
8150
8151 function Get_Pragma_Id (N : Node_Id) return Pragma_Id is
8152 begin
8153 return Get_Pragma_Id (Pragma_Name_Unmapped (N));
8154 end Get_Pragma_Id;
8155
8156 ------------------------
8157 -- Get_Qualified_Name --
8158 ------------------------
8159
8160 function Get_Qualified_Name
8161 (Id : Entity_Id;
8162 Suffix : Entity_Id := Empty) return Name_Id
8163 is
8164 Suffix_Nam : Name_Id := No_Name;
8165
8166 begin
8167 if Present (Suffix) then
8168 Suffix_Nam := Chars (Suffix);
8169 end if;
8170
8171 return Get_Qualified_Name (Chars (Id), Suffix_Nam, Scope (Id));
8172 end Get_Qualified_Name;
8173
8174 function Get_Qualified_Name
8175 (Nam : Name_Id;
8176 Suffix : Name_Id := No_Name;
8177 Scop : Entity_Id := Current_Scope) return Name_Id
8178 is
8179 procedure Add_Scope (S : Entity_Id);
8180 -- Add the fully qualified form of scope S to the name buffer. The
8181 -- format is:
8182 -- s-1__s__
8183
8184 ---------------
8185 -- Add_Scope --
8186 ---------------
8187
8188 procedure Add_Scope (S : Entity_Id) is
8189 begin
8190 if S = Empty then
8191 null;
8192
8193 elsif S = Standard_Standard then
8194 null;
8195
8196 else
8197 Add_Scope (Scope (S));
8198 Get_Name_String_And_Append (Chars (S));
8199 Add_Str_To_Name_Buffer ("__");
8200 end if;
8201 end Add_Scope;
8202
8203 -- Start of processing for Get_Qualified_Name
8204
8205 begin
8206 Name_Len := 0;
8207 Add_Scope (Scop);
8208
8209 -- Append the base name after all scopes have been chained
8210
8211 Get_Name_String_And_Append (Nam);
8212
8213 -- Append the suffix (if present)
8214
8215 if Suffix /= No_Name then
8216 Add_Str_To_Name_Buffer ("__");
8217 Get_Name_String_And_Append (Suffix);
8218 end if;
8219
8220 return Name_Find;
8221 end Get_Qualified_Name;
8222
8223 -----------------------
8224 -- Get_Reason_String --
8225 -----------------------
8226
8227 procedure Get_Reason_String (N : Node_Id) is
8228 begin
8229 if Nkind (N) = N_String_Literal then
8230 Store_String_Chars (Strval (N));
8231
8232 elsif Nkind (N) = N_Op_Concat then
8233 Get_Reason_String (Left_Opnd (N));
8234 Get_Reason_String (Right_Opnd (N));
8235
8236 -- If not of required form, error
8237
8238 else
8239 Error_Msg_N
8240 ("Reason for pragma Warnings has wrong form", N);
8241 Error_Msg_N
8242 ("\must be string literal or concatenation of string literals", N);
8243 return;
8244 end if;
8245 end Get_Reason_String;
8246
8247 --------------------------------
8248 -- Get_Reference_Discriminant --
8249 --------------------------------
8250
8251 function Get_Reference_Discriminant (Typ : Entity_Id) return Entity_Id is
8252 D : Entity_Id;
8253
8254 begin
8255 D := First_Discriminant (Typ);
8256 while Present (D) loop
8257 if Has_Implicit_Dereference (D) then
8258 return D;
8259 end if;
8260 Next_Discriminant (D);
8261 end loop;
8262
8263 return Empty;
8264 end Get_Reference_Discriminant;
8265
8266 ---------------------------
8267 -- Get_Referenced_Object --
8268 ---------------------------
8269
8270 function Get_Referenced_Object (N : Node_Id) return Node_Id is
8271 R : Node_Id;
8272
8273 begin
8274 R := N;
8275 while Is_Entity_Name (R)
8276 and then Present (Renamed_Object (Entity (R)))
8277 loop
8278 R := Renamed_Object (Entity (R));
8279 end loop;
8280
8281 return R;
8282 end Get_Referenced_Object;
8283
8284 ------------------------
8285 -- Get_Renamed_Entity --
8286 ------------------------
8287
8288 function Get_Renamed_Entity (E : Entity_Id) return Entity_Id is
8289 R : Entity_Id;
8290
8291 begin
8292 R := E;
8293 while Present (Renamed_Entity (R)) loop
8294 R := Renamed_Entity (R);
8295 end loop;
8296
8297 return R;
8298 end Get_Renamed_Entity;
8299
8300 -----------------------
8301 -- Get_Return_Object --
8302 -----------------------
8303
8304 function Get_Return_Object (N : Node_Id) return Entity_Id is
8305 Decl : Node_Id;
8306
8307 begin
8308 Decl := First (Return_Object_Declarations (N));
8309 while Present (Decl) loop
8310 exit when Nkind (Decl) = N_Object_Declaration
8311 and then Is_Return_Object (Defining_Identifier (Decl));
8312 Next (Decl);
8313 end loop;
8314
8315 pragma Assert (Present (Decl));
8316 return Defining_Identifier (Decl);
8317 end Get_Return_Object;
8318
8319 ---------------------------
8320 -- Get_Subprogram_Entity --
8321 ---------------------------
8322
8323 function Get_Subprogram_Entity (Nod : Node_Id) return Entity_Id is
8324 Subp : Node_Id;
8325 Subp_Id : Entity_Id;
8326
8327 begin
8328 if Nkind (Nod) = N_Accept_Statement then
8329 Subp := Entry_Direct_Name (Nod);
8330
8331 elsif Nkind (Nod) = N_Slice then
8332 Subp := Prefix (Nod);
8333
8334 else
8335 Subp := Name (Nod);
8336 end if;
8337
8338 -- Strip the subprogram call
8339
8340 loop
8341 if Nkind_In (Subp, N_Explicit_Dereference,
8342 N_Indexed_Component,
8343 N_Selected_Component)
8344 then
8345 Subp := Prefix (Subp);
8346
8347 elsif Nkind_In (Subp, N_Type_Conversion,
8348 N_Unchecked_Type_Conversion)
8349 then
8350 Subp := Expression (Subp);
8351
8352 else
8353 exit;
8354 end if;
8355 end loop;
8356
8357 -- Extract the entity of the subprogram call
8358
8359 if Is_Entity_Name (Subp) then
8360 Subp_Id := Entity (Subp);
8361
8362 if Ekind (Subp_Id) = E_Access_Subprogram_Type then
8363 Subp_Id := Directly_Designated_Type (Subp_Id);
8364 end if;
8365
8366 if Is_Subprogram (Subp_Id) then
8367 return Subp_Id;
8368 else
8369 return Empty;
8370 end if;
8371
8372 -- The search did not find a construct that denotes a subprogram
8373
8374 else
8375 return Empty;
8376 end if;
8377 end Get_Subprogram_Entity;
8378
8379 -----------------------------
8380 -- Get_Task_Body_Procedure --
8381 -----------------------------
8382
8383 function Get_Task_Body_Procedure (E : Entity_Id) return Node_Id is
8384 begin
8385 -- Note: A task type may be the completion of a private type with
8386 -- discriminants. When performing elaboration checks on a task
8387 -- declaration, the current view of the type may be the private one,
8388 -- and the procedure that holds the body of the task is held in its
8389 -- underlying type.
8390
8391 -- This is an odd function, why not have Task_Body_Procedure do
8392 -- the following digging???
8393
8394 return Task_Body_Procedure (Underlying_Type (Root_Type (E)));
8395 end Get_Task_Body_Procedure;
8396
8397 -------------------------
8398 -- Get_User_Defined_Eq --
8399 -------------------------
8400
8401 function Get_User_Defined_Eq (E : Entity_Id) return Entity_Id is
8402 Prim : Elmt_Id;
8403 Op : Entity_Id;
8404
8405 begin
8406 Prim := First_Elmt (Collect_Primitive_Operations (E));
8407 while Present (Prim) loop
8408 Op := Node (Prim);
8409
8410 if Chars (Op) = Name_Op_Eq
8411 and then Etype (Op) = Standard_Boolean
8412 and then Etype (First_Formal (Op)) = E
8413 and then Etype (Next_Formal (First_Formal (Op))) = E
8414 then
8415 return Op;
8416 end if;
8417
8418 Next_Elmt (Prim);
8419 end loop;
8420
8421 return Empty;
8422 end Get_User_Defined_Eq;
8423
8424 ---------------
8425 -- Get_Views --
8426 ---------------
8427
8428 procedure Get_Views
8429 (Typ : Entity_Id;
8430 Priv_Typ : out Entity_Id;
8431 Full_Typ : out Entity_Id;
8432 Full_Base : out Entity_Id;
8433 CRec_Typ : out Entity_Id)
8434 is
8435 IP_View : Entity_Id;
8436
8437 begin
8438 -- Assume that none of the views can be recovered
8439
8440 Priv_Typ := Empty;
8441 Full_Typ := Empty;
8442 Full_Base := Empty;
8443 CRec_Typ := Empty;
8444
8445 -- The input type is the corresponding record type of a protected or a
8446 -- task type.
8447
8448 if Ekind (Typ) = E_Record_Type
8449 and then Is_Concurrent_Record_Type (Typ)
8450 then
8451 CRec_Typ := Typ;
8452 Full_Typ := Corresponding_Concurrent_Type (CRec_Typ);
8453 Full_Base := Base_Type (Full_Typ);
8454 Priv_Typ := Incomplete_Or_Partial_View (Full_Typ);
8455
8456 -- Otherwise the input type denotes an arbitrary type
8457
8458 else
8459 IP_View := Incomplete_Or_Partial_View (Typ);
8460
8461 -- The input type denotes the full view of a private type
8462
8463 if Present (IP_View) then
8464 Priv_Typ := IP_View;
8465 Full_Typ := Typ;
8466
8467 -- The input type is a private type
8468
8469 elsif Is_Private_Type (Typ) then
8470 Priv_Typ := Typ;
8471 Full_Typ := Full_View (Priv_Typ);
8472
8473 -- Otherwise the input type does not have any views
8474
8475 else
8476 Full_Typ := Typ;
8477 end if;
8478
8479 if Present (Full_Typ) then
8480 Full_Base := Base_Type (Full_Typ);
8481
8482 if Ekind_In (Full_Typ, E_Protected_Type, E_Task_Type) then
8483 CRec_Typ := Corresponding_Record_Type (Full_Typ);
8484 end if;
8485 end if;
8486 end if;
8487 end Get_Views;
8488
8489 -----------------------
8490 -- Has_Access_Values --
8491 -----------------------
8492
8493 function Has_Access_Values (T : Entity_Id) return Boolean is
8494 Typ : constant Entity_Id := Underlying_Type (T);
8495
8496 begin
8497 -- Case of a private type which is not completed yet. This can only
8498 -- happen in the case of a generic format type appearing directly, or
8499 -- as a component of the type to which this function is being applied
8500 -- at the top level. Return False in this case, since we certainly do
8501 -- not know that the type contains access types.
8502
8503 if No (Typ) then
8504 return False;
8505
8506 elsif Is_Access_Type (Typ) then
8507 return True;
8508
8509 elsif Is_Array_Type (Typ) then
8510 return Has_Access_Values (Component_Type (Typ));
8511
8512 elsif Is_Record_Type (Typ) then
8513 declare
8514 Comp : Entity_Id;
8515
8516 begin
8517 -- Loop to Check components
8518
8519 Comp := First_Component_Or_Discriminant (Typ);
8520 while Present (Comp) loop
8521
8522 -- Check for access component, tag field does not count, even
8523 -- though it is implemented internally using an access type.
8524
8525 if Has_Access_Values (Etype (Comp))
8526 and then Chars (Comp) /= Name_uTag
8527 then
8528 return True;
8529 end if;
8530
8531 Next_Component_Or_Discriminant (Comp);
8532 end loop;
8533 end;
8534
8535 return False;
8536
8537 else
8538 return False;
8539 end if;
8540 end Has_Access_Values;
8541
8542 ------------------------------
8543 -- Has_Compatible_Alignment --
8544 ------------------------------
8545
8546 function Has_Compatible_Alignment
8547 (Obj : Entity_Id;
8548 Expr : Node_Id;
8549 Layout_Done : Boolean) return Alignment_Result
8550 is
8551 function Has_Compatible_Alignment_Internal
8552 (Obj : Entity_Id;
8553 Expr : Node_Id;
8554 Layout_Done : Boolean;
8555 Default : Alignment_Result) return Alignment_Result;
8556 -- This is the internal recursive function that actually does the work.
8557 -- There is one additional parameter, which says what the result should
8558 -- be if no alignment information is found, and there is no definite
8559 -- indication of compatible alignments. At the outer level, this is set
8560 -- to Unknown, but for internal recursive calls in the case where types
8561 -- are known to be correct, it is set to Known_Compatible.
8562
8563 ---------------------------------------
8564 -- Has_Compatible_Alignment_Internal --
8565 ---------------------------------------
8566
8567 function Has_Compatible_Alignment_Internal
8568 (Obj : Entity_Id;
8569 Expr : Node_Id;
8570 Layout_Done : Boolean;
8571 Default : Alignment_Result) return Alignment_Result
8572 is
8573 Result : Alignment_Result := Known_Compatible;
8574 -- Holds the current status of the result. Note that once a value of
8575 -- Known_Incompatible is set, it is sticky and does not get changed
8576 -- to Unknown (the value in Result only gets worse as we go along,
8577 -- never better).
8578
8579 Offs : Uint := No_Uint;
8580 -- Set to a factor of the offset from the base object when Expr is a
8581 -- selected or indexed component, based on Component_Bit_Offset and
8582 -- Component_Size respectively. A negative value is used to represent
8583 -- a value which is not known at compile time.
8584
8585 procedure Check_Prefix;
8586 -- Checks the prefix recursively in the case where the expression
8587 -- is an indexed or selected component.
8588
8589 procedure Set_Result (R : Alignment_Result);
8590 -- If R represents a worse outcome (unknown instead of known
8591 -- compatible, or known incompatible), then set Result to R.
8592
8593 ------------------
8594 -- Check_Prefix --
8595 ------------------
8596
8597 procedure Check_Prefix is
8598 begin
8599 -- The subtlety here is that in doing a recursive call to check
8600 -- the prefix, we have to decide what to do in the case where we
8601 -- don't find any specific indication of an alignment problem.
8602
8603 -- At the outer level, we normally set Unknown as the result in
8604 -- this case, since we can only set Known_Compatible if we really
8605 -- know that the alignment value is OK, but for the recursive
8606 -- call, in the case where the types match, and we have not
8607 -- specified a peculiar alignment for the object, we are only
8608 -- concerned about suspicious rep clauses, the default case does
8609 -- not affect us, since the compiler will, in the absence of such
8610 -- rep clauses, ensure that the alignment is correct.
8611
8612 if Default = Known_Compatible
8613 or else
8614 (Etype (Obj) = Etype (Expr)
8615 and then (Unknown_Alignment (Obj)
8616 or else
8617 Alignment (Obj) = Alignment (Etype (Obj))))
8618 then
8619 Set_Result
8620 (Has_Compatible_Alignment_Internal
8621 (Obj, Prefix (Expr), Layout_Done, Known_Compatible));
8622
8623 -- In all other cases, we need a full check on the prefix
8624
8625 else
8626 Set_Result
8627 (Has_Compatible_Alignment_Internal
8628 (Obj, Prefix (Expr), Layout_Done, Unknown));
8629 end if;
8630 end Check_Prefix;
8631
8632 ----------------
8633 -- Set_Result --
8634 ----------------
8635
8636 procedure Set_Result (R : Alignment_Result) is
8637 begin
8638 if R > Result then
8639 Result := R;
8640 end if;
8641 end Set_Result;
8642
8643 -- Start of processing for Has_Compatible_Alignment_Internal
8644
8645 begin
8646 -- If Expr is a selected component, we must make sure there is no
8647 -- potentially troublesome component clause and that the record is
8648 -- not packed if the layout is not done.
8649
8650 if Nkind (Expr) = N_Selected_Component then
8651
8652 -- Packing generates unknown alignment if layout is not done
8653
8654 if Is_Packed (Etype (Prefix (Expr))) and then not Layout_Done then
8655 Set_Result (Unknown);
8656 end if;
8657
8658 -- Check prefix and component offset
8659
8660 Check_Prefix;
8661 Offs := Component_Bit_Offset (Entity (Selector_Name (Expr)));
8662
8663 -- If Expr is an indexed component, we must make sure there is no
8664 -- potentially troublesome Component_Size clause and that the array
8665 -- is not bit-packed if the layout is not done.
8666
8667 elsif Nkind (Expr) = N_Indexed_Component then
8668 declare
8669 Typ : constant Entity_Id := Etype (Prefix (Expr));
8670
8671 begin
8672 -- Packing generates unknown alignment if layout is not done
8673
8674 if Is_Bit_Packed_Array (Typ) and then not Layout_Done then
8675 Set_Result (Unknown);
8676 end if;
8677
8678 -- Check prefix and component offset (or at least size)
8679
8680 Check_Prefix;
8681 Offs := Indexed_Component_Bit_Offset (Expr);
8682 if Offs = No_Uint then
8683 Offs := Component_Size (Typ);
8684 end if;
8685 end;
8686 end if;
8687
8688 -- If we have a null offset, the result is entirely determined by
8689 -- the base object and has already been computed recursively.
8690
8691 if Offs = Uint_0 then
8692 null;
8693
8694 -- Case where we know the alignment of the object
8695
8696 elsif Known_Alignment (Obj) then
8697 declare
8698 ObjA : constant Uint := Alignment (Obj);
8699 ExpA : Uint := No_Uint;
8700 SizA : Uint := No_Uint;
8701
8702 begin
8703 -- If alignment of Obj is 1, then we are always OK
8704
8705 if ObjA = 1 then
8706 Set_Result (Known_Compatible);
8707
8708 -- Alignment of Obj is greater than 1, so we need to check
8709
8710 else
8711 -- If we have an offset, see if it is compatible
8712
8713 if Offs /= No_Uint and Offs > Uint_0 then
8714 if Offs mod (System_Storage_Unit * ObjA) /= 0 then
8715 Set_Result (Known_Incompatible);
8716 end if;
8717
8718 -- See if Expr is an object with known alignment
8719
8720 elsif Is_Entity_Name (Expr)
8721 and then Known_Alignment (Entity (Expr))
8722 then
8723 ExpA := Alignment (Entity (Expr));
8724
8725 -- Otherwise, we can use the alignment of the type of
8726 -- Expr given that we already checked for
8727 -- discombobulating rep clauses for the cases of indexed
8728 -- and selected components above.
8729
8730 elsif Known_Alignment (Etype (Expr)) then
8731 ExpA := Alignment (Etype (Expr));
8732
8733 -- Otherwise the alignment is unknown
8734
8735 else
8736 Set_Result (Default);
8737 end if;
8738
8739 -- If we got an alignment, see if it is acceptable
8740
8741 if ExpA /= No_Uint and then ExpA < ObjA then
8742 Set_Result (Known_Incompatible);
8743 end if;
8744
8745 -- If Expr is not a piece of a larger object, see if size
8746 -- is given. If so, check that it is not too small for the
8747 -- required alignment.
8748
8749 if Offs /= No_Uint then
8750 null;
8751
8752 -- See if Expr is an object with known size
8753
8754 elsif Is_Entity_Name (Expr)
8755 and then Known_Static_Esize (Entity (Expr))
8756 then
8757 SizA := Esize (Entity (Expr));
8758
8759 -- Otherwise, we check the object size of the Expr type
8760
8761 elsif Known_Static_Esize (Etype (Expr)) then
8762 SizA := Esize (Etype (Expr));
8763 end if;
8764
8765 -- If we got a size, see if it is a multiple of the Obj
8766 -- alignment, if not, then the alignment cannot be
8767 -- acceptable, since the size is always a multiple of the
8768 -- alignment.
8769
8770 if SizA /= No_Uint then
8771 if SizA mod (ObjA * Ttypes.System_Storage_Unit) /= 0 then
8772 Set_Result (Known_Incompatible);
8773 end if;
8774 end if;
8775 end if;
8776 end;
8777
8778 -- If we do not know required alignment, any non-zero offset is a
8779 -- potential problem (but certainly may be OK, so result is unknown).
8780
8781 elsif Offs /= No_Uint then
8782 Set_Result (Unknown);
8783
8784 -- If we can't find the result by direct comparison of alignment
8785 -- values, then there is still one case that we can determine known
8786 -- result, and that is when we can determine that the types are the
8787 -- same, and no alignments are specified. Then we known that the
8788 -- alignments are compatible, even if we don't know the alignment
8789 -- value in the front end.
8790
8791 elsif Etype (Obj) = Etype (Expr) then
8792
8793 -- Types are the same, but we have to check for possible size
8794 -- and alignments on the Expr object that may make the alignment
8795 -- different, even though the types are the same.
8796
8797 if Is_Entity_Name (Expr) then
8798
8799 -- First check alignment of the Expr object. Any alignment less
8800 -- than Maximum_Alignment is worrisome since this is the case
8801 -- where we do not know the alignment of Obj.
8802
8803 if Known_Alignment (Entity (Expr))
8804 and then UI_To_Int (Alignment (Entity (Expr))) <
8805 Ttypes.Maximum_Alignment
8806 then
8807 Set_Result (Unknown);
8808
8809 -- Now check size of Expr object. Any size that is not an
8810 -- even multiple of Maximum_Alignment is also worrisome
8811 -- since it may cause the alignment of the object to be less
8812 -- than the alignment of the type.
8813
8814 elsif Known_Static_Esize (Entity (Expr))
8815 and then
8816 (UI_To_Int (Esize (Entity (Expr))) mod
8817 (Ttypes.Maximum_Alignment * Ttypes.System_Storage_Unit))
8818 /= 0
8819 then
8820 Set_Result (Unknown);
8821
8822 -- Otherwise same type is decisive
8823
8824 else
8825 Set_Result (Known_Compatible);
8826 end if;
8827 end if;
8828
8829 -- Another case to deal with is when there is an explicit size or
8830 -- alignment clause when the types are not the same. If so, then the
8831 -- result is Unknown. We don't need to do this test if the Default is
8832 -- Unknown, since that result will be set in any case.
8833
8834 elsif Default /= Unknown
8835 and then (Has_Size_Clause (Etype (Expr))
8836 or else
8837 Has_Alignment_Clause (Etype (Expr)))
8838 then
8839 Set_Result (Unknown);
8840
8841 -- If no indication found, set default
8842
8843 else
8844 Set_Result (Default);
8845 end if;
8846
8847 -- Return worst result found
8848
8849 return Result;
8850 end Has_Compatible_Alignment_Internal;
8851
8852 -- Start of processing for Has_Compatible_Alignment
8853
8854 begin
8855 -- If Obj has no specified alignment, then set alignment from the type
8856 -- alignment. Perhaps we should always do this, but for sure we should
8857 -- do it when there is an address clause since we can do more if the
8858 -- alignment is known.
8859
8860 if Unknown_Alignment (Obj) then
8861 Set_Alignment (Obj, Alignment (Etype (Obj)));
8862 end if;
8863
8864 -- Now do the internal call that does all the work
8865
8866 return
8867 Has_Compatible_Alignment_Internal (Obj, Expr, Layout_Done, Unknown);
8868 end Has_Compatible_Alignment;
8869
8870 ----------------------
8871 -- Has_Declarations --
8872 ----------------------
8873
8874 function Has_Declarations (N : Node_Id) return Boolean is
8875 begin
8876 return Nkind_In (Nkind (N), N_Accept_Statement,
8877 N_Block_Statement,
8878 N_Compilation_Unit_Aux,
8879 N_Entry_Body,
8880 N_Package_Body,
8881 N_Protected_Body,
8882 N_Subprogram_Body,
8883 N_Task_Body,
8884 N_Package_Specification);
8885 end Has_Declarations;
8886
8887 ---------------------------------
8888 -- Has_Defaulted_Discriminants --
8889 ---------------------------------
8890
8891 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean is
8892 begin
8893 return Has_Discriminants (Typ)
8894 and then Present (First_Discriminant (Typ))
8895 and then Present (Discriminant_Default_Value
8896 (First_Discriminant (Typ)));
8897 end Has_Defaulted_Discriminants;
8898
8899 -------------------
8900 -- Has_Denormals --
8901 -------------------
8902
8903 function Has_Denormals (E : Entity_Id) return Boolean is
8904 begin
8905 return Is_Floating_Point_Type (E) and then Denorm_On_Target;
8906 end Has_Denormals;
8907
8908 -------------------------------------------
8909 -- Has_Discriminant_Dependent_Constraint --
8910 -------------------------------------------
8911
8912 function Has_Discriminant_Dependent_Constraint
8913 (Comp : Entity_Id) return Boolean
8914 is
8915 Comp_Decl : constant Node_Id := Parent (Comp);
8916 Subt_Indic : Node_Id;
8917 Constr : Node_Id;
8918 Assn : Node_Id;
8919
8920 begin
8921 -- Discriminants can't depend on discriminants
8922
8923 if Ekind (Comp) = E_Discriminant then
8924 return False;
8925
8926 else
8927 Subt_Indic := Subtype_Indication (Component_Definition (Comp_Decl));
8928
8929 if Nkind (Subt_Indic) = N_Subtype_Indication then
8930 Constr := Constraint (Subt_Indic);
8931
8932 if Nkind (Constr) = N_Index_Or_Discriminant_Constraint then
8933 Assn := First (Constraints (Constr));
8934 while Present (Assn) loop
8935 case Nkind (Assn) is
8936 when N_Subtype_Indication |
8937 N_Range |
8938 N_Identifier
8939 =>
8940 if Depends_On_Discriminant (Assn) then
8941 return True;
8942 end if;
8943
8944 when N_Discriminant_Association =>
8945 if Depends_On_Discriminant (Expression (Assn)) then
8946 return True;
8947 end if;
8948
8949 when others =>
8950 null;
8951 end case;
8952
8953 Next (Assn);
8954 end loop;
8955 end if;
8956 end if;
8957 end if;
8958
8959 return False;
8960 end Has_Discriminant_Dependent_Constraint;
8961
8962 --------------------------------------
8963 -- Has_Effectively_Volatile_Profile --
8964 --------------------------------------
8965
8966 function Has_Effectively_Volatile_Profile
8967 (Subp_Id : Entity_Id) return Boolean
8968 is
8969 Formal : Entity_Id;
8970
8971 begin
8972 -- Inspect the formal parameters looking for an effectively volatile
8973 -- type.
8974
8975 Formal := First_Formal (Subp_Id);
8976 while Present (Formal) loop
8977 if Is_Effectively_Volatile (Etype (Formal)) then
8978 return True;
8979 end if;
8980
8981 Next_Formal (Formal);
8982 end loop;
8983
8984 -- Inspect the return type of functions
8985
8986 if Ekind_In (Subp_Id, E_Function, E_Generic_Function)
8987 and then Is_Effectively_Volatile (Etype (Subp_Id))
8988 then
8989 return True;
8990 end if;
8991
8992 return False;
8993 end Has_Effectively_Volatile_Profile;
8994
8995 --------------------------
8996 -- Has_Enabled_Property --
8997 --------------------------
8998
8999 function Has_Enabled_Property
9000 (Item_Id : Entity_Id;
9001 Property : Name_Id) return Boolean
9002 is
9003 function State_Has_Enabled_Property return Boolean;
9004 -- Determine whether a state denoted by Item_Id has the property enabled
9005
9006 function Variable_Has_Enabled_Property return Boolean;
9007 -- Determine whether a variable denoted by Item_Id has the property
9008 -- enabled.
9009
9010 --------------------------------
9011 -- State_Has_Enabled_Property --
9012 --------------------------------
9013
9014 function State_Has_Enabled_Property return Boolean is
9015 Decl : constant Node_Id := Parent (Item_Id);
9016 Opt : Node_Id;
9017 Opt_Nam : Node_Id;
9018 Prop : Node_Id;
9019 Prop_Nam : Node_Id;
9020 Props : Node_Id;
9021
9022 begin
9023 -- The declaration of an external abstract state appears as an
9024 -- extension aggregate. If this is not the case, properties can never
9025 -- be set.
9026
9027 if Nkind (Decl) /= N_Extension_Aggregate then
9028 return False;
9029 end if;
9030
9031 -- When External appears as a simple option, it automatically enables
9032 -- all properties.
9033
9034 Opt := First (Expressions (Decl));
9035 while Present (Opt) loop
9036 if Nkind (Opt) = N_Identifier
9037 and then Chars (Opt) = Name_External
9038 then
9039 return True;
9040 end if;
9041
9042 Next (Opt);
9043 end loop;
9044
9045 -- When External specifies particular properties, inspect those and
9046 -- find the desired one (if any).
9047
9048 Opt := First (Component_Associations (Decl));
9049 while Present (Opt) loop
9050 Opt_Nam := First (Choices (Opt));
9051
9052 if Nkind (Opt_Nam) = N_Identifier
9053 and then Chars (Opt_Nam) = Name_External
9054 then
9055 Props := Expression (Opt);
9056
9057 -- Multiple properties appear as an aggregate
9058
9059 if Nkind (Props) = N_Aggregate then
9060
9061 -- Simple property form
9062
9063 Prop := First (Expressions (Props));
9064 while Present (Prop) loop
9065 if Chars (Prop) = Property then
9066 return True;
9067 end if;
9068
9069 Next (Prop);
9070 end loop;
9071
9072 -- Property with expression form
9073
9074 Prop := First (Component_Associations (Props));
9075 while Present (Prop) loop
9076 Prop_Nam := First (Choices (Prop));
9077
9078 -- The property can be represented in two ways:
9079 -- others => <value>
9080 -- <property> => <value>
9081
9082 if Nkind (Prop_Nam) = N_Others_Choice
9083 or else (Nkind (Prop_Nam) = N_Identifier
9084 and then Chars (Prop_Nam) = Property)
9085 then
9086 return Is_True (Expr_Value (Expression (Prop)));
9087 end if;
9088
9089 Next (Prop);
9090 end loop;
9091
9092 -- Single property
9093
9094 else
9095 return Chars (Props) = Property;
9096 end if;
9097 end if;
9098
9099 Next (Opt);
9100 end loop;
9101
9102 return False;
9103 end State_Has_Enabled_Property;
9104
9105 -----------------------------------
9106 -- Variable_Has_Enabled_Property --
9107 -----------------------------------
9108
9109 function Variable_Has_Enabled_Property return Boolean is
9110 function Is_Enabled (Prag : Node_Id) return Boolean;
9111 -- Determine whether property pragma Prag (if present) denotes an
9112 -- enabled property.
9113
9114 ----------------
9115 -- Is_Enabled --
9116 ----------------
9117
9118 function Is_Enabled (Prag : Node_Id) return Boolean is
9119 Arg1 : Node_Id;
9120
9121 begin
9122 if Present (Prag) then
9123 Arg1 := First (Pragma_Argument_Associations (Prag));
9124
9125 -- The pragma has an optional Boolean expression, the related
9126 -- property is enabled only when the expression evaluates to
9127 -- True.
9128
9129 if Present (Arg1) then
9130 return Is_True (Expr_Value (Get_Pragma_Arg (Arg1)));
9131
9132 -- Otherwise the lack of expression enables the property by
9133 -- default.
9134
9135 else
9136 return True;
9137 end if;
9138
9139 -- The property was never set in the first place
9140
9141 else
9142 return False;
9143 end if;
9144 end Is_Enabled;
9145
9146 -- Local variables
9147
9148 AR : constant Node_Id :=
9149 Get_Pragma (Item_Id, Pragma_Async_Readers);
9150 AW : constant Node_Id :=
9151 Get_Pragma (Item_Id, Pragma_Async_Writers);
9152 ER : constant Node_Id :=
9153 Get_Pragma (Item_Id, Pragma_Effective_Reads);
9154 EW : constant Node_Id :=
9155 Get_Pragma (Item_Id, Pragma_Effective_Writes);
9156
9157 -- Start of processing for Variable_Has_Enabled_Property
9158
9159 begin
9160 -- A non-effectively volatile object can never possess external
9161 -- properties.
9162
9163 if not Is_Effectively_Volatile (Item_Id) then
9164 return False;
9165
9166 -- External properties related to variables come in two flavors -
9167 -- explicit and implicit. The explicit case is characterized by the
9168 -- presence of a property pragma with an optional Boolean flag. The
9169 -- property is enabled when the flag evaluates to True or the flag is
9170 -- missing altogether.
9171
9172 elsif Property = Name_Async_Readers and then Is_Enabled (AR) then
9173 return True;
9174
9175 elsif Property = Name_Async_Writers and then Is_Enabled (AW) then
9176 return True;
9177
9178 elsif Property = Name_Effective_Reads and then Is_Enabled (ER) then
9179 return True;
9180
9181 elsif Property = Name_Effective_Writes and then Is_Enabled (EW) then
9182 return True;
9183
9184 -- The implicit case lacks all property pragmas
9185
9186 elsif No (AR) and then No (AW) and then No (ER) and then No (EW) then
9187 return True;
9188
9189 else
9190 return False;
9191 end if;
9192 end Variable_Has_Enabled_Property;
9193
9194 -- Start of processing for Has_Enabled_Property
9195
9196 begin
9197 -- Abstract states and variables have a flexible scheme of specifying
9198 -- external properties.
9199
9200 if Ekind (Item_Id) = E_Abstract_State then
9201 return State_Has_Enabled_Property;
9202
9203 elsif Ekind (Item_Id) = E_Variable then
9204 return Variable_Has_Enabled_Property;
9205
9206 -- Otherwise a property is enabled when the related item is effectively
9207 -- volatile.
9208
9209 else
9210 return Is_Effectively_Volatile (Item_Id);
9211 end if;
9212 end Has_Enabled_Property;
9213
9214 -------------------------------------
9215 -- Has_Full_Default_Initialization --
9216 -------------------------------------
9217
9218 function Has_Full_Default_Initialization (Typ : Entity_Id) return Boolean is
9219 Comp : Entity_Id;
9220 Prag : Node_Id;
9221
9222 begin
9223 -- A type subject to pragma Default_Initial_Condition is fully default
9224 -- initialized when the pragma appears with a non-null argument. Since
9225 -- any type may act as the full view of a private type, this check must
9226 -- be performed prior to the specialized tests below.
9227
9228 if Has_DIC (Typ) then
9229 Prag := Get_Pragma (Typ, Pragma_Default_Initial_Condition);
9230 pragma Assert (Present (Prag));
9231
9232 return Is_Verifiable_DIC_Pragma (Prag);
9233 end if;
9234
9235 -- A scalar type is fully default initialized if it is subject to aspect
9236 -- Default_Value.
9237
9238 if Is_Scalar_Type (Typ) then
9239 return Has_Default_Aspect (Typ);
9240
9241 -- An array type is fully default initialized if its element type is
9242 -- scalar and the array type carries aspect Default_Component_Value or
9243 -- the element type is fully default initialized.
9244
9245 elsif Is_Array_Type (Typ) then
9246 return
9247 Has_Default_Aspect (Typ)
9248 or else Has_Full_Default_Initialization (Component_Type (Typ));
9249
9250 -- A protected type, record type, or type extension is fully default
9251 -- initialized if all its components either carry an initialization
9252 -- expression or have a type that is fully default initialized. The
9253 -- parent type of a type extension must be fully default initialized.
9254
9255 elsif Is_Record_Type (Typ) or else Is_Protected_Type (Typ) then
9256
9257 -- Inspect all entities defined in the scope of the type, looking for
9258 -- uninitialized components.
9259
9260 Comp := First_Entity (Typ);
9261 while Present (Comp) loop
9262 if Ekind (Comp) = E_Component
9263 and then Comes_From_Source (Comp)
9264 and then No (Expression (Parent (Comp)))
9265 and then not Has_Full_Default_Initialization (Etype (Comp))
9266 then
9267 return False;
9268 end if;
9269
9270 Next_Entity (Comp);
9271 end loop;
9272
9273 -- Ensure that the parent type of a type extension is fully default
9274 -- initialized.
9275
9276 if Etype (Typ) /= Typ
9277 and then not Has_Full_Default_Initialization (Etype (Typ))
9278 then
9279 return False;
9280 end if;
9281
9282 -- If we get here, then all components and parent portion are fully
9283 -- default initialized.
9284
9285 return True;
9286
9287 -- A task type is fully default initialized by default
9288
9289 elsif Is_Task_Type (Typ) then
9290 return True;
9291
9292 -- Otherwise the type is not fully default initialized
9293
9294 else
9295 return False;
9296 end if;
9297 end Has_Full_Default_Initialization;
9298
9299 --------------------
9300 -- Has_Infinities --
9301 --------------------
9302
9303 function Has_Infinities (E : Entity_Id) return Boolean is
9304 begin
9305 return
9306 Is_Floating_Point_Type (E)
9307 and then Nkind (Scalar_Range (E)) = N_Range
9308 and then Includes_Infinities (Scalar_Range (E));
9309 end Has_Infinities;
9310
9311 --------------------
9312 -- Has_Interfaces --
9313 --------------------
9314
9315 function Has_Interfaces
9316 (T : Entity_Id;
9317 Use_Full_View : Boolean := True) return Boolean
9318 is
9319 Typ : Entity_Id := Base_Type (T);
9320
9321 begin
9322 -- Handle concurrent types
9323
9324 if Is_Concurrent_Type (Typ) then
9325 Typ := Corresponding_Record_Type (Typ);
9326 end if;
9327
9328 if not Present (Typ)
9329 or else not Is_Record_Type (Typ)
9330 or else not Is_Tagged_Type (Typ)
9331 then
9332 return False;
9333 end if;
9334
9335 -- Handle private types
9336
9337 if Use_Full_View and then Present (Full_View (Typ)) then
9338 Typ := Full_View (Typ);
9339 end if;
9340
9341 -- Handle concurrent record types
9342
9343 if Is_Concurrent_Record_Type (Typ)
9344 and then Is_Non_Empty_List (Abstract_Interface_List (Typ))
9345 then
9346 return True;
9347 end if;
9348
9349 loop
9350 if Is_Interface (Typ)
9351 or else
9352 (Is_Record_Type (Typ)
9353 and then Present (Interfaces (Typ))
9354 and then not Is_Empty_Elmt_List (Interfaces (Typ)))
9355 then
9356 return True;
9357 end if;
9358
9359 exit when Etype (Typ) = Typ
9360
9361 -- Handle private types
9362
9363 or else (Present (Full_View (Etype (Typ)))
9364 and then Full_View (Etype (Typ)) = Typ)
9365
9366 -- Protect frontend against wrong sources with cyclic derivations
9367
9368 or else Etype (Typ) = T;
9369
9370 -- Climb to the ancestor type handling private types
9371
9372 if Present (Full_View (Etype (Typ))) then
9373 Typ := Full_View (Etype (Typ));
9374 else
9375 Typ := Etype (Typ);
9376 end if;
9377 end loop;
9378
9379 return False;
9380 end Has_Interfaces;
9381
9382 --------------------------
9383 -- Has_Max_Queue_Length --
9384 --------------------------
9385
9386 function Has_Max_Queue_Length (Id : Entity_Id) return Boolean is
9387 begin
9388 return
9389 Ekind (Id) = E_Entry
9390 and then Present (Get_Pragma (Id, Pragma_Max_Queue_Length));
9391 end Has_Max_Queue_Length;
9392
9393 ---------------------------------
9394 -- Has_No_Obvious_Side_Effects --
9395 ---------------------------------
9396
9397 function Has_No_Obvious_Side_Effects (N : Node_Id) return Boolean is
9398 begin
9399 -- For now handle literals, constants, and non-volatile variables and
9400 -- expressions combining these with operators or short circuit forms.
9401
9402 if Nkind (N) in N_Numeric_Or_String_Literal then
9403 return True;
9404
9405 elsif Nkind (N) = N_Character_Literal then
9406 return True;
9407
9408 elsif Nkind (N) in N_Unary_Op then
9409 return Has_No_Obvious_Side_Effects (Right_Opnd (N));
9410
9411 elsif Nkind (N) in N_Binary_Op or else Nkind (N) in N_Short_Circuit then
9412 return Has_No_Obvious_Side_Effects (Left_Opnd (N))
9413 and then
9414 Has_No_Obvious_Side_Effects (Right_Opnd (N));
9415
9416 elsif Nkind (N) = N_Expression_With_Actions
9417 and then Is_Empty_List (Actions (N))
9418 then
9419 return Has_No_Obvious_Side_Effects (Expression (N));
9420
9421 elsif Nkind (N) in N_Has_Entity then
9422 return Present (Entity (N))
9423 and then Ekind_In (Entity (N), E_Variable,
9424 E_Constant,
9425 E_Enumeration_Literal,
9426 E_In_Parameter,
9427 E_Out_Parameter,
9428 E_In_Out_Parameter)
9429 and then not Is_Volatile (Entity (N));
9430
9431 else
9432 return False;
9433 end if;
9434 end Has_No_Obvious_Side_Effects;
9435
9436 -----------------------------
9437 -- Has_Non_Null_Refinement --
9438 -----------------------------
9439
9440 function Has_Non_Null_Refinement (Id : Entity_Id) return Boolean is
9441 Constits : Elist_Id;
9442
9443 begin
9444 pragma Assert (Ekind (Id) = E_Abstract_State);
9445 Constits := Refinement_Constituents (Id);
9446
9447 -- For a refinement to be non-null, the first constituent must be
9448 -- anything other than null.
9449
9450 return
9451 Present (Constits)
9452 and then Nkind (Node (First_Elmt (Constits))) /= N_Null;
9453 end Has_Non_Null_Refinement;
9454
9455 -------------------
9456 -- Has_Null_Body --
9457 -------------------
9458
9459 function Has_Null_Body (Proc_Id : Entity_Id) return Boolean is
9460 Body_Id : Entity_Id;
9461 Decl : Node_Id;
9462 Spec : Node_Id;
9463 Stmt1 : Node_Id;
9464 Stmt2 : Node_Id;
9465
9466 begin
9467 Spec := Parent (Proc_Id);
9468 Decl := Parent (Spec);
9469
9470 -- Retrieve the entity of the procedure body (e.g. invariant proc).
9471
9472 if Nkind (Spec) = N_Procedure_Specification
9473 and then Nkind (Decl) = N_Subprogram_Declaration
9474 then
9475 Body_Id := Corresponding_Body (Decl);
9476
9477 -- The body acts as a spec
9478
9479 else
9480 Body_Id := Proc_Id;
9481 end if;
9482
9483 -- The body will be generated later
9484
9485 if No (Body_Id) then
9486 return False;
9487 end if;
9488
9489 Spec := Parent (Body_Id);
9490 Decl := Parent (Spec);
9491
9492 pragma Assert
9493 (Nkind (Spec) = N_Procedure_Specification
9494 and then Nkind (Decl) = N_Subprogram_Body);
9495
9496 Stmt1 := First (Statements (Handled_Statement_Sequence (Decl)));
9497
9498 -- Look for a null statement followed by an optional return
9499 -- statement.
9500
9501 if Nkind (Stmt1) = N_Null_Statement then
9502 Stmt2 := Next (Stmt1);
9503
9504 if Present (Stmt2) then
9505 return Nkind (Stmt2) = N_Simple_Return_Statement;
9506 else
9507 return True;
9508 end if;
9509 end if;
9510
9511 return False;
9512 end Has_Null_Body;
9513
9514 ------------------------
9515 -- Has_Null_Exclusion --
9516 ------------------------
9517
9518 function Has_Null_Exclusion (N : Node_Id) return Boolean is
9519 begin
9520 case Nkind (N) is
9521 when N_Access_Definition |
9522 N_Access_Function_Definition |
9523 N_Access_Procedure_Definition |
9524 N_Access_To_Object_Definition |
9525 N_Allocator |
9526 N_Derived_Type_Definition |
9527 N_Function_Specification |
9528 N_Subtype_Declaration =>
9529 return Null_Exclusion_Present (N);
9530
9531 when N_Component_Definition |
9532 N_Formal_Object_Declaration |
9533 N_Object_Renaming_Declaration =>
9534 if Present (Subtype_Mark (N)) then
9535 return Null_Exclusion_Present (N);
9536 else pragma Assert (Present (Access_Definition (N)));
9537 return Null_Exclusion_Present (Access_Definition (N));
9538 end if;
9539
9540 when N_Discriminant_Specification =>
9541 if Nkind (Discriminant_Type (N)) = N_Access_Definition then
9542 return Null_Exclusion_Present (Discriminant_Type (N));
9543 else
9544 return Null_Exclusion_Present (N);
9545 end if;
9546
9547 when N_Object_Declaration =>
9548 if Nkind (Object_Definition (N)) = N_Access_Definition then
9549 return Null_Exclusion_Present (Object_Definition (N));
9550 else
9551 return Null_Exclusion_Present (N);
9552 end if;
9553
9554 when N_Parameter_Specification =>
9555 if Nkind (Parameter_Type (N)) = N_Access_Definition then
9556 return Null_Exclusion_Present (Parameter_Type (N));
9557 else
9558 return Null_Exclusion_Present (N);
9559 end if;
9560
9561 when others =>
9562 return False;
9563
9564 end case;
9565 end Has_Null_Exclusion;
9566
9567 ------------------------
9568 -- Has_Null_Extension --
9569 ------------------------
9570
9571 function Has_Null_Extension (T : Entity_Id) return Boolean is
9572 B : constant Entity_Id := Base_Type (T);
9573 Comps : Node_Id;
9574 Ext : Node_Id;
9575
9576 begin
9577 if Nkind (Parent (B)) = N_Full_Type_Declaration
9578 and then Present (Record_Extension_Part (Type_Definition (Parent (B))))
9579 then
9580 Ext := Record_Extension_Part (Type_Definition (Parent (B)));
9581
9582 if Present (Ext) then
9583 if Null_Present (Ext) then
9584 return True;
9585 else
9586 Comps := Component_List (Ext);
9587
9588 -- The null component list is rewritten during analysis to
9589 -- include the parent component. Any other component indicates
9590 -- that the extension was not originally null.
9591
9592 return Null_Present (Comps)
9593 or else No (Next (First (Component_Items (Comps))));
9594 end if;
9595 else
9596 return False;
9597 end if;
9598
9599 else
9600 return False;
9601 end if;
9602 end Has_Null_Extension;
9603
9604 -------------------------
9605 -- Has_Null_Refinement --
9606 -------------------------
9607
9608 function Has_Null_Refinement (Id : Entity_Id) return Boolean is
9609 Constits : Elist_Id;
9610
9611 begin
9612 pragma Assert (Ekind (Id) = E_Abstract_State);
9613 Constits := Refinement_Constituents (Id);
9614
9615 -- For a refinement to be null, the state's sole constituent must be a
9616 -- null.
9617
9618 return
9619 Present (Constits)
9620 and then Nkind (Node (First_Elmt (Constits))) = N_Null;
9621 end Has_Null_Refinement;
9622
9623 -------------------------------
9624 -- Has_Overriding_Initialize --
9625 -------------------------------
9626
9627 function Has_Overriding_Initialize (T : Entity_Id) return Boolean is
9628 BT : constant Entity_Id := Base_Type (T);
9629 P : Elmt_Id;
9630
9631 begin
9632 if Is_Controlled (BT) then
9633 if Is_RTU (Scope (BT), Ada_Finalization) then
9634 return False;
9635
9636 elsif Present (Primitive_Operations (BT)) then
9637 P := First_Elmt (Primitive_Operations (BT));
9638 while Present (P) loop
9639 declare
9640 Init : constant Entity_Id := Node (P);
9641 Formal : constant Entity_Id := First_Formal (Init);
9642 begin
9643 if Ekind (Init) = E_Procedure
9644 and then Chars (Init) = Name_Initialize
9645 and then Comes_From_Source (Init)
9646 and then Present (Formal)
9647 and then Etype (Formal) = BT
9648 and then No (Next_Formal (Formal))
9649 and then (Ada_Version < Ada_2012
9650 or else not Null_Present (Parent (Init)))
9651 then
9652 return True;
9653 end if;
9654 end;
9655
9656 Next_Elmt (P);
9657 end loop;
9658 end if;
9659
9660 -- Here if type itself does not have a non-null Initialize operation:
9661 -- check immediate ancestor.
9662
9663 if Is_Derived_Type (BT)
9664 and then Has_Overriding_Initialize (Etype (BT))
9665 then
9666 return True;
9667 end if;
9668 end if;
9669
9670 return False;
9671 end Has_Overriding_Initialize;
9672
9673 --------------------------------------
9674 -- Has_Preelaborable_Initialization --
9675 --------------------------------------
9676
9677 function Has_Preelaborable_Initialization (E : Entity_Id) return Boolean is
9678 Has_PE : Boolean;
9679
9680 procedure Check_Components (E : Entity_Id);
9681 -- Check component/discriminant chain, sets Has_PE False if a component
9682 -- or discriminant does not meet the preelaborable initialization rules.
9683
9684 ----------------------
9685 -- Check_Components --
9686 ----------------------
9687
9688 procedure Check_Components (E : Entity_Id) is
9689 Ent : Entity_Id;
9690 Exp : Node_Id;
9691
9692 function Is_Preelaborable_Expression (N : Node_Id) return Boolean;
9693 -- Returns True if and only if the expression denoted by N does not
9694 -- violate restrictions on preelaborable constructs (RM-10.2.1(5-9)).
9695
9696 ---------------------------------
9697 -- Is_Preelaborable_Expression --
9698 ---------------------------------
9699
9700 function Is_Preelaborable_Expression (N : Node_Id) return Boolean is
9701 Exp : Node_Id;
9702 Assn : Node_Id;
9703 Choice : Node_Id;
9704 Comp_Type : Entity_Id;
9705 Is_Array_Aggr : Boolean;
9706
9707 begin
9708 if Is_OK_Static_Expression (N) then
9709 return True;
9710
9711 elsif Nkind (N) = N_Null then
9712 return True;
9713
9714 -- Attributes are allowed in general, even if their prefix is a
9715 -- formal type. (It seems that certain attributes known not to be
9716 -- static might not be allowed, but there are no rules to prevent
9717 -- them.)
9718
9719 elsif Nkind (N) = N_Attribute_Reference then
9720 return True;
9721
9722 -- The name of a discriminant evaluated within its parent type is
9723 -- defined to be preelaborable (10.2.1(8)). Note that we test for
9724 -- names that denote discriminals as well as discriminants to
9725 -- catch references occurring within init procs.
9726
9727 elsif Is_Entity_Name (N)
9728 and then
9729 (Ekind (Entity (N)) = E_Discriminant
9730 or else (Ekind_In (Entity (N), E_Constant, E_In_Parameter)
9731 and then Present (Discriminal_Link (Entity (N)))))
9732 then
9733 return True;
9734
9735 elsif Nkind (N) = N_Qualified_Expression then
9736 return Is_Preelaborable_Expression (Expression (N));
9737
9738 -- For aggregates we have to check that each of the associations
9739 -- is preelaborable.
9740
9741 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
9742 Is_Array_Aggr := Is_Array_Type (Etype (N));
9743
9744 if Is_Array_Aggr then
9745 Comp_Type := Component_Type (Etype (N));
9746 end if;
9747
9748 -- Check the ancestor part of extension aggregates, which must
9749 -- be either the name of a type that has preelaborable init or
9750 -- an expression that is preelaborable.
9751
9752 if Nkind (N) = N_Extension_Aggregate then
9753 declare
9754 Anc_Part : constant Node_Id := Ancestor_Part (N);
9755
9756 begin
9757 if Is_Entity_Name (Anc_Part)
9758 and then Is_Type (Entity (Anc_Part))
9759 then
9760 if not Has_Preelaborable_Initialization
9761 (Entity (Anc_Part))
9762 then
9763 return False;
9764 end if;
9765
9766 elsif not Is_Preelaborable_Expression (Anc_Part) then
9767 return False;
9768 end if;
9769 end;
9770 end if;
9771
9772 -- Check positional associations
9773
9774 Exp := First (Expressions (N));
9775 while Present (Exp) loop
9776 if not Is_Preelaborable_Expression (Exp) then
9777 return False;
9778 end if;
9779
9780 Next (Exp);
9781 end loop;
9782
9783 -- Check named associations
9784
9785 Assn := First (Component_Associations (N));
9786 while Present (Assn) loop
9787 Choice := First (Choices (Assn));
9788 while Present (Choice) loop
9789 if Is_Array_Aggr then
9790 if Nkind (Choice) = N_Others_Choice then
9791 null;
9792
9793 elsif Nkind (Choice) = N_Range then
9794 if not Is_OK_Static_Range (Choice) then
9795 return False;
9796 end if;
9797
9798 elsif not Is_OK_Static_Expression (Choice) then
9799 return False;
9800 end if;
9801
9802 else
9803 Comp_Type := Etype (Choice);
9804 end if;
9805
9806 Next (Choice);
9807 end loop;
9808
9809 -- If the association has a <> at this point, then we have
9810 -- to check whether the component's type has preelaborable
9811 -- initialization. Note that this only occurs when the
9812 -- association's corresponding component does not have a
9813 -- default expression, the latter case having already been
9814 -- expanded as an expression for the association.
9815
9816 if Box_Present (Assn) then
9817 if not Has_Preelaborable_Initialization (Comp_Type) then
9818 return False;
9819 end if;
9820
9821 -- In the expression case we check whether the expression
9822 -- is preelaborable.
9823
9824 elsif
9825 not Is_Preelaborable_Expression (Expression (Assn))
9826 then
9827 return False;
9828 end if;
9829
9830 Next (Assn);
9831 end loop;
9832
9833 -- If we get here then aggregate as a whole is preelaborable
9834
9835 return True;
9836
9837 -- All other cases are not preelaborable
9838
9839 else
9840 return False;
9841 end if;
9842 end Is_Preelaborable_Expression;
9843
9844 -- Start of processing for Check_Components
9845
9846 begin
9847 -- Loop through entities of record or protected type
9848
9849 Ent := E;
9850 while Present (Ent) loop
9851
9852 -- We are interested only in components and discriminants
9853
9854 Exp := Empty;
9855
9856 case Ekind (Ent) is
9857 when E_Component =>
9858
9859 -- Get default expression if any. If there is no declaration
9860 -- node, it means we have an internal entity. The parent and
9861 -- tag fields are examples of such entities. For such cases,
9862 -- we just test the type of the entity.
9863
9864 if Present (Declaration_Node (Ent)) then
9865 Exp := Expression (Declaration_Node (Ent));
9866 end if;
9867
9868 when E_Discriminant =>
9869
9870 -- Note: for a renamed discriminant, the Declaration_Node
9871 -- may point to the one from the ancestor, and have a
9872 -- different expression, so use the proper attribute to
9873 -- retrieve the expression from the derived constraint.
9874
9875 Exp := Discriminant_Default_Value (Ent);
9876
9877 when others =>
9878 goto Check_Next_Entity;
9879 end case;
9880
9881 -- A component has PI if it has no default expression and the
9882 -- component type has PI.
9883
9884 if No (Exp) then
9885 if not Has_Preelaborable_Initialization (Etype (Ent)) then
9886 Has_PE := False;
9887 exit;
9888 end if;
9889
9890 -- Require the default expression to be preelaborable
9891
9892 elsif not Is_Preelaborable_Expression (Exp) then
9893 Has_PE := False;
9894 exit;
9895 end if;
9896
9897 <<Check_Next_Entity>>
9898 Next_Entity (Ent);
9899 end loop;
9900 end Check_Components;
9901
9902 -- Start of processing for Has_Preelaborable_Initialization
9903
9904 begin
9905 -- Immediate return if already marked as known preelaborable init. This
9906 -- covers types for which this function has already been called once
9907 -- and returned True (in which case the result is cached), and also
9908 -- types to which a pragma Preelaborable_Initialization applies.
9909
9910 if Known_To_Have_Preelab_Init (E) then
9911 return True;
9912 end if;
9913
9914 -- If the type is a subtype representing a generic actual type, then
9915 -- test whether its base type has preelaborable initialization since
9916 -- the subtype representing the actual does not inherit this attribute
9917 -- from the actual or formal. (but maybe it should???)
9918
9919 if Is_Generic_Actual_Type (E) then
9920 return Has_Preelaborable_Initialization (Base_Type (E));
9921 end if;
9922
9923 -- All elementary types have preelaborable initialization
9924
9925 if Is_Elementary_Type (E) then
9926 Has_PE := True;
9927
9928 -- Array types have PI if the component type has PI
9929
9930 elsif Is_Array_Type (E) then
9931 Has_PE := Has_Preelaborable_Initialization (Component_Type (E));
9932
9933 -- A derived type has preelaborable initialization if its parent type
9934 -- has preelaborable initialization and (in the case of a derived record
9935 -- extension) if the non-inherited components all have preelaborable
9936 -- initialization. However, a user-defined controlled type with an
9937 -- overriding Initialize procedure does not have preelaborable
9938 -- initialization.
9939
9940 elsif Is_Derived_Type (E) then
9941
9942 -- If the derived type is a private extension then it doesn't have
9943 -- preelaborable initialization.
9944
9945 if Ekind (Base_Type (E)) = E_Record_Type_With_Private then
9946 return False;
9947 end if;
9948
9949 -- First check whether ancestor type has preelaborable initialization
9950
9951 Has_PE := Has_Preelaborable_Initialization (Etype (Base_Type (E)));
9952
9953 -- If OK, check extension components (if any)
9954
9955 if Has_PE and then Is_Record_Type (E) then
9956 Check_Components (First_Entity (E));
9957 end if;
9958
9959 -- Check specifically for 10.2.1(11.4/2) exception: a controlled type
9960 -- with a user defined Initialize procedure does not have PI. If
9961 -- the type is untagged, the control primitives come from a component
9962 -- that has already been checked.
9963
9964 if Has_PE
9965 and then Is_Controlled (E)
9966 and then Is_Tagged_Type (E)
9967 and then Has_Overriding_Initialize (E)
9968 then
9969 Has_PE := False;
9970 end if;
9971
9972 -- Private types not derived from a type having preelaborable init and
9973 -- that are not marked with pragma Preelaborable_Initialization do not
9974 -- have preelaborable initialization.
9975
9976 elsif Is_Private_Type (E) then
9977 return False;
9978
9979 -- Record type has PI if it is non private and all components have PI
9980
9981 elsif Is_Record_Type (E) then
9982 Has_PE := True;
9983 Check_Components (First_Entity (E));
9984
9985 -- Protected types must not have entries, and components must meet
9986 -- same set of rules as for record components.
9987
9988 elsif Is_Protected_Type (E) then
9989 if Has_Entries (E) then
9990 Has_PE := False;
9991 else
9992 Has_PE := True;
9993 Check_Components (First_Entity (E));
9994 Check_Components (First_Private_Entity (E));
9995 end if;
9996
9997 -- Type System.Address always has preelaborable initialization
9998
9999 elsif Is_RTE (E, RE_Address) then
10000 Has_PE := True;
10001
10002 -- In all other cases, type does not have preelaborable initialization
10003
10004 else
10005 return False;
10006 end if;
10007
10008 -- If type has preelaborable initialization, cache result
10009
10010 if Has_PE then
10011 Set_Known_To_Have_Preelab_Init (E);
10012 end if;
10013
10014 return Has_PE;
10015 end Has_Preelaborable_Initialization;
10016
10017 ---------------------------
10018 -- Has_Private_Component --
10019 ---------------------------
10020
10021 function Has_Private_Component (Type_Id : Entity_Id) return Boolean is
10022 Btype : Entity_Id := Base_Type (Type_Id);
10023 Component : Entity_Id;
10024
10025 begin
10026 if Error_Posted (Type_Id)
10027 or else Error_Posted (Btype)
10028 then
10029 return False;
10030 end if;
10031
10032 if Is_Class_Wide_Type (Btype) then
10033 Btype := Root_Type (Btype);
10034 end if;
10035
10036 if Is_Private_Type (Btype) then
10037 declare
10038 UT : constant Entity_Id := Underlying_Type (Btype);
10039 begin
10040 if No (UT) then
10041 if No (Full_View (Btype)) then
10042 return not Is_Generic_Type (Btype)
10043 and then
10044 not Is_Generic_Type (Root_Type (Btype));
10045 else
10046 return not Is_Generic_Type (Root_Type (Full_View (Btype)));
10047 end if;
10048 else
10049 return not Is_Frozen (UT) and then Has_Private_Component (UT);
10050 end if;
10051 end;
10052
10053 elsif Is_Array_Type (Btype) then
10054 return Has_Private_Component (Component_Type (Btype));
10055
10056 elsif Is_Record_Type (Btype) then
10057 Component := First_Component (Btype);
10058 while Present (Component) loop
10059 if Has_Private_Component (Etype (Component)) then
10060 return True;
10061 end if;
10062
10063 Next_Component (Component);
10064 end loop;
10065
10066 return False;
10067
10068 elsif Is_Protected_Type (Btype)
10069 and then Present (Corresponding_Record_Type (Btype))
10070 then
10071 return Has_Private_Component (Corresponding_Record_Type (Btype));
10072
10073 else
10074 return False;
10075 end if;
10076 end Has_Private_Component;
10077
10078 ----------------------
10079 -- Has_Signed_Zeros --
10080 ----------------------
10081
10082 function Has_Signed_Zeros (E : Entity_Id) return Boolean is
10083 begin
10084 return Is_Floating_Point_Type (E) and then Signed_Zeros_On_Target;
10085 end Has_Signed_Zeros;
10086
10087 ------------------------------
10088 -- Has_Significant_Contract --
10089 ------------------------------
10090
10091 function Has_Significant_Contract (Subp_Id : Entity_Id) return Boolean is
10092 Subp_Nam : constant Name_Id := Chars (Subp_Id);
10093
10094 begin
10095 -- _Finalizer procedure
10096
10097 if Subp_Nam = Name_uFinalizer then
10098 return False;
10099
10100 -- _Postconditions procedure
10101
10102 elsif Subp_Nam = Name_uPostconditions then
10103 return False;
10104
10105 -- Predicate function
10106
10107 elsif Ekind (Subp_Id) = E_Function
10108 and then Is_Predicate_Function (Subp_Id)
10109 then
10110 return False;
10111
10112 -- TSS subprogram
10113
10114 elsif Get_TSS_Name (Subp_Id) /= TSS_Null then
10115 return False;
10116
10117 else
10118 return True;
10119 end if;
10120 end Has_Significant_Contract;
10121
10122 -----------------------------
10123 -- Has_Static_Array_Bounds --
10124 -----------------------------
10125
10126 function Has_Static_Array_Bounds (Typ : Node_Id) return Boolean is
10127 Ndims : constant Nat := Number_Dimensions (Typ);
10128
10129 Index : Node_Id;
10130 Low : Node_Id;
10131 High : Node_Id;
10132
10133 begin
10134 -- Unconstrained types do not have static bounds
10135
10136 if not Is_Constrained (Typ) then
10137 return False;
10138 end if;
10139
10140 -- First treat string literals specially, as the lower bound and length
10141 -- of string literals are not stored like those of arrays.
10142
10143 -- A string literal always has static bounds
10144
10145 if Ekind (Typ) = E_String_Literal_Subtype then
10146 return True;
10147 end if;
10148
10149 -- Treat all dimensions in turn
10150
10151 Index := First_Index (Typ);
10152 for Indx in 1 .. Ndims loop
10153
10154 -- In case of an illegal index which is not a discrete type, return
10155 -- that the type is not static.
10156
10157 if not Is_Discrete_Type (Etype (Index))
10158 or else Etype (Index) = Any_Type
10159 then
10160 return False;
10161 end if;
10162
10163 Get_Index_Bounds (Index, Low, High);
10164
10165 if Error_Posted (Low) or else Error_Posted (High) then
10166 return False;
10167 end if;
10168
10169 if Is_OK_Static_Expression (Low)
10170 and then
10171 Is_OK_Static_Expression (High)
10172 then
10173 null;
10174 else
10175 return False;
10176 end if;
10177
10178 Next (Index);
10179 end loop;
10180
10181 -- If we fall through the loop, all indexes matched
10182
10183 return True;
10184 end Has_Static_Array_Bounds;
10185
10186 ----------------
10187 -- Has_Stream --
10188 ----------------
10189
10190 function Has_Stream (T : Entity_Id) return Boolean is
10191 E : Entity_Id;
10192
10193 begin
10194 if No (T) then
10195 return False;
10196
10197 elsif Is_RTE (Root_Type (T), RE_Root_Stream_Type) then
10198 return True;
10199
10200 elsif Is_Array_Type (T) then
10201 return Has_Stream (Component_Type (T));
10202
10203 elsif Is_Record_Type (T) then
10204 E := First_Component (T);
10205 while Present (E) loop
10206 if Has_Stream (Etype (E)) then
10207 return True;
10208 else
10209 Next_Component (E);
10210 end if;
10211 end loop;
10212
10213 return False;
10214
10215 elsif Is_Private_Type (T) then
10216 return Has_Stream (Underlying_Type (T));
10217
10218 else
10219 return False;
10220 end if;
10221 end Has_Stream;
10222
10223 ----------------
10224 -- Has_Suffix --
10225 ----------------
10226
10227 function Has_Suffix (E : Entity_Id; Suffix : Character) return Boolean is
10228 begin
10229 Get_Name_String (Chars (E));
10230 return Name_Buffer (Name_Len) = Suffix;
10231 end Has_Suffix;
10232
10233 ----------------
10234 -- Add_Suffix --
10235 ----------------
10236
10237 function Add_Suffix (E : Entity_Id; Suffix : Character) return Name_Id is
10238 begin
10239 Get_Name_String (Chars (E));
10240 Add_Char_To_Name_Buffer (Suffix);
10241 return Name_Find;
10242 end Add_Suffix;
10243
10244 -------------------
10245 -- Remove_Suffix --
10246 -------------------
10247
10248 function Remove_Suffix (E : Entity_Id; Suffix : Character) return Name_Id is
10249 begin
10250 pragma Assert (Has_Suffix (E, Suffix));
10251 Get_Name_String (Chars (E));
10252 Name_Len := Name_Len - 1;
10253 return Name_Find;
10254 end Remove_Suffix;
10255
10256 ----------------------------------
10257 -- Replace_Null_By_Null_Address --
10258 ----------------------------------
10259
10260 procedure Replace_Null_By_Null_Address (N : Node_Id) is
10261 procedure Replace_Null_Operand (Op : Node_Id; Other_Op : Node_Id);
10262 -- Replace operand Op with a reference to Null_Address when the operand
10263 -- denotes a null Address. Other_Op denotes the other operand.
10264
10265 --------------------------
10266 -- Replace_Null_Operand --
10267 --------------------------
10268
10269 procedure Replace_Null_Operand (Op : Node_Id; Other_Op : Node_Id) is
10270 begin
10271 -- Check the type of the complementary operand since the N_Null node
10272 -- has not been decorated yet.
10273
10274 if Nkind (Op) = N_Null
10275 and then Is_Descendant_Of_Address (Etype (Other_Op))
10276 then
10277 Rewrite (Op, New_Occurrence_Of (RTE (RE_Null_Address), Sloc (Op)));
10278 end if;
10279 end Replace_Null_Operand;
10280
10281 -- Start of processing for Replace_Null_By_Null_Address
10282
10283 begin
10284 pragma Assert (Relaxed_RM_Semantics);
10285 pragma Assert (Nkind_In (N, N_Null,
10286 N_Op_Eq,
10287 N_Op_Ge,
10288 N_Op_Gt,
10289 N_Op_Le,
10290 N_Op_Lt,
10291 N_Op_Ne));
10292
10293 if Nkind (N) = N_Null then
10294 Rewrite (N, New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
10295
10296 else
10297 declare
10298 L : constant Node_Id := Left_Opnd (N);
10299 R : constant Node_Id := Right_Opnd (N);
10300
10301 begin
10302 Replace_Null_Operand (L, Other_Op => R);
10303 Replace_Null_Operand (R, Other_Op => L);
10304 end;
10305 end if;
10306 end Replace_Null_By_Null_Address;
10307
10308 --------------------------
10309 -- Has_Tagged_Component --
10310 --------------------------
10311
10312 function Has_Tagged_Component (Typ : Entity_Id) return Boolean is
10313 Comp : Entity_Id;
10314
10315 begin
10316 if Is_Private_Type (Typ) and then Present (Underlying_Type (Typ)) then
10317 return Has_Tagged_Component (Underlying_Type (Typ));
10318
10319 elsif Is_Array_Type (Typ) then
10320 return Has_Tagged_Component (Component_Type (Typ));
10321
10322 elsif Is_Tagged_Type (Typ) then
10323 return True;
10324
10325 elsif Is_Record_Type (Typ) then
10326 Comp := First_Component (Typ);
10327 while Present (Comp) loop
10328 if Has_Tagged_Component (Etype (Comp)) then
10329 return True;
10330 end if;
10331
10332 Next_Component (Comp);
10333 end loop;
10334
10335 return False;
10336
10337 else
10338 return False;
10339 end if;
10340 end Has_Tagged_Component;
10341
10342 -----------------------------
10343 -- Has_Undefined_Reference --
10344 -----------------------------
10345
10346 function Has_Undefined_Reference (Expr : Node_Id) return Boolean is
10347 Has_Undef_Ref : Boolean := False;
10348 -- Flag set when expression Expr contains at least one undefined
10349 -- reference.
10350
10351 function Is_Undefined_Reference (N : Node_Id) return Traverse_Result;
10352 -- Determine whether N denotes a reference and if it does, whether it is
10353 -- undefined.
10354
10355 ----------------------------
10356 -- Is_Undefined_Reference --
10357 ----------------------------
10358
10359 function Is_Undefined_Reference (N : Node_Id) return Traverse_Result is
10360 begin
10361 if Is_Entity_Name (N)
10362 and then Present (Entity (N))
10363 and then Entity (N) = Any_Id
10364 then
10365 Has_Undef_Ref := True;
10366 return Abandon;
10367 end if;
10368
10369 return OK;
10370 end Is_Undefined_Reference;
10371
10372 procedure Find_Undefined_References is
10373 new Traverse_Proc (Is_Undefined_Reference);
10374
10375 -- Start of processing for Has_Undefined_Reference
10376
10377 begin
10378 Find_Undefined_References (Expr);
10379
10380 return Has_Undef_Ref;
10381 end Has_Undefined_Reference;
10382
10383 ----------------------------
10384 -- Has_Volatile_Component --
10385 ----------------------------
10386
10387 function Has_Volatile_Component (Typ : Entity_Id) return Boolean is
10388 Comp : Entity_Id;
10389
10390 begin
10391 if Has_Volatile_Components (Typ) then
10392 return True;
10393
10394 elsif Is_Array_Type (Typ) then
10395 return Is_Volatile (Component_Type (Typ));
10396
10397 elsif Is_Record_Type (Typ) then
10398 Comp := First_Component (Typ);
10399 while Present (Comp) loop
10400 if Is_Volatile_Object (Comp) then
10401 return True;
10402 end if;
10403
10404 Comp := Next_Component (Comp);
10405 end loop;
10406 end if;
10407
10408 return False;
10409 end Has_Volatile_Component;
10410
10411 -------------------------
10412 -- Implementation_Kind --
10413 -------------------------
10414
10415 function Implementation_Kind (Subp : Entity_Id) return Name_Id is
10416 Impl_Prag : constant Node_Id := Get_Rep_Pragma (Subp, Name_Implemented);
10417 Arg : Node_Id;
10418 begin
10419 pragma Assert (Present (Impl_Prag));
10420 Arg := Last (Pragma_Argument_Associations (Impl_Prag));
10421 return Chars (Get_Pragma_Arg (Arg));
10422 end Implementation_Kind;
10423
10424 --------------------------
10425 -- Implements_Interface --
10426 --------------------------
10427
10428 function Implements_Interface
10429 (Typ_Ent : Entity_Id;
10430 Iface_Ent : Entity_Id;
10431 Exclude_Parents : Boolean := False) return Boolean
10432 is
10433 Ifaces_List : Elist_Id;
10434 Elmt : Elmt_Id;
10435 Iface : Entity_Id := Base_Type (Iface_Ent);
10436 Typ : Entity_Id := Base_Type (Typ_Ent);
10437
10438 begin
10439 if Is_Class_Wide_Type (Typ) then
10440 Typ := Root_Type (Typ);
10441 end if;
10442
10443 if not Has_Interfaces (Typ) then
10444 return False;
10445 end if;
10446
10447 if Is_Class_Wide_Type (Iface) then
10448 Iface := Root_Type (Iface);
10449 end if;
10450
10451 Collect_Interfaces (Typ, Ifaces_List);
10452
10453 Elmt := First_Elmt (Ifaces_List);
10454 while Present (Elmt) loop
10455 if Is_Ancestor (Node (Elmt), Typ, Use_Full_View => True)
10456 and then Exclude_Parents
10457 then
10458 null;
10459
10460 elsif Node (Elmt) = Iface then
10461 return True;
10462 end if;
10463
10464 Next_Elmt (Elmt);
10465 end loop;
10466
10467 return False;
10468 end Implements_Interface;
10469
10470 ------------------------------------
10471 -- In_Assertion_Expression_Pragma --
10472 ------------------------------------
10473
10474 function In_Assertion_Expression_Pragma (N : Node_Id) return Boolean is
10475 Par : Node_Id;
10476 Prag : Node_Id := Empty;
10477
10478 begin
10479 -- Climb the parent chain looking for an enclosing pragma
10480
10481 Par := N;
10482 while Present (Par) loop
10483 if Nkind (Par) = N_Pragma then
10484 Prag := Par;
10485 exit;
10486
10487 -- Precondition-like pragmas are expanded into if statements, check
10488 -- the original node instead.
10489
10490 elsif Nkind (Original_Node (Par)) = N_Pragma then
10491 Prag := Original_Node (Par);
10492 exit;
10493
10494 -- The expansion of attribute 'Old generates a constant to capture
10495 -- the result of the prefix. If the parent traversal reaches
10496 -- one of these constants, then the node technically came from a
10497 -- postcondition-like pragma. Note that the Ekind is not tested here
10498 -- because N may be the expression of an object declaration which is
10499 -- currently being analyzed. Such objects carry Ekind of E_Void.
10500
10501 elsif Nkind (Par) = N_Object_Declaration
10502 and then Constant_Present (Par)
10503 and then Stores_Attribute_Old_Prefix (Defining_Entity (Par))
10504 then
10505 return True;
10506
10507 -- Prevent the search from going too far
10508
10509 elsif Is_Body_Or_Package_Declaration (Par) then
10510 return False;
10511 end if;
10512
10513 Par := Parent (Par);
10514 end loop;
10515
10516 return
10517 Present (Prag)
10518 and then Assertion_Expression_Pragma (Get_Pragma_Id (Prag));
10519 end In_Assertion_Expression_Pragma;
10520
10521 ----------------------
10522 -- In_Generic_Scope --
10523 ----------------------
10524
10525 function In_Generic_Scope (E : Entity_Id) return Boolean is
10526 S : Entity_Id;
10527
10528 begin
10529 S := Scope (E);
10530 while Present (S) and then S /= Standard_Standard loop
10531 if Is_Generic_Unit (S) then
10532 return True;
10533 end if;
10534
10535 S := Scope (S);
10536 end loop;
10537
10538 return False;
10539 end In_Generic_Scope;
10540
10541 -----------------
10542 -- In_Instance --
10543 -----------------
10544
10545 function In_Instance return Boolean is
10546 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
10547 S : Entity_Id;
10548
10549 begin
10550 S := Current_Scope;
10551 while Present (S) and then S /= Standard_Standard loop
10552 if Ekind_In (S, E_Function, E_Package, E_Procedure)
10553 and then Is_Generic_Instance (S)
10554 then
10555 -- A child instance is always compiled in the context of a parent
10556 -- instance. Nevertheless, the actuals are not analyzed in an
10557 -- instance context. We detect this case by examining the current
10558 -- compilation unit, which must be a child instance, and checking
10559 -- that it is not currently on the scope stack.
10560
10561 if Is_Child_Unit (Curr_Unit)
10562 and then Nkind (Unit (Cunit (Current_Sem_Unit))) =
10563 N_Package_Instantiation
10564 and then not In_Open_Scopes (Curr_Unit)
10565 then
10566 return False;
10567 else
10568 return True;
10569 end if;
10570 end if;
10571
10572 S := Scope (S);
10573 end loop;
10574
10575 return False;
10576 end In_Instance;
10577
10578 ----------------------
10579 -- In_Instance_Body --
10580 ----------------------
10581
10582 function In_Instance_Body return Boolean is
10583 S : Entity_Id;
10584
10585 begin
10586 S := Current_Scope;
10587 while Present (S) and then S /= Standard_Standard loop
10588 if Ekind_In (S, E_Function, E_Procedure)
10589 and then Is_Generic_Instance (S)
10590 then
10591 return True;
10592
10593 elsif Ekind (S) = E_Package
10594 and then In_Package_Body (S)
10595 and then Is_Generic_Instance (S)
10596 then
10597 return True;
10598 end if;
10599
10600 S := Scope (S);
10601 end loop;
10602
10603 return False;
10604 end In_Instance_Body;
10605
10606 -----------------------------
10607 -- In_Instance_Not_Visible --
10608 -----------------------------
10609
10610 function In_Instance_Not_Visible return Boolean is
10611 S : Entity_Id;
10612
10613 begin
10614 S := Current_Scope;
10615 while Present (S) and then S /= Standard_Standard loop
10616 if Ekind_In (S, E_Function, E_Procedure)
10617 and then Is_Generic_Instance (S)
10618 then
10619 return True;
10620
10621 elsif Ekind (S) = E_Package
10622 and then (In_Package_Body (S) or else In_Private_Part (S))
10623 and then Is_Generic_Instance (S)
10624 then
10625 return True;
10626 end if;
10627
10628 S := Scope (S);
10629 end loop;
10630
10631 return False;
10632 end In_Instance_Not_Visible;
10633
10634 ------------------------------
10635 -- In_Instance_Visible_Part --
10636 ------------------------------
10637
10638 function In_Instance_Visible_Part return Boolean is
10639 S : Entity_Id;
10640
10641 begin
10642 S := Current_Scope;
10643 while Present (S) and then S /= Standard_Standard loop
10644 if Ekind (S) = E_Package
10645 and then Is_Generic_Instance (S)
10646 and then not In_Package_Body (S)
10647 and then not In_Private_Part (S)
10648 then
10649 return True;
10650 end if;
10651
10652 S := Scope (S);
10653 end loop;
10654
10655 return False;
10656 end In_Instance_Visible_Part;
10657
10658 ---------------------
10659 -- In_Package_Body --
10660 ---------------------
10661
10662 function In_Package_Body return Boolean is
10663 S : Entity_Id;
10664
10665 begin
10666 S := Current_Scope;
10667 while Present (S) and then S /= Standard_Standard loop
10668 if Ekind (S) = E_Package and then In_Package_Body (S) then
10669 return True;
10670 else
10671 S := Scope (S);
10672 end if;
10673 end loop;
10674
10675 return False;
10676 end In_Package_Body;
10677
10678 --------------------------------
10679 -- In_Parameter_Specification --
10680 --------------------------------
10681
10682 function In_Parameter_Specification (N : Node_Id) return Boolean is
10683 PN : Node_Id;
10684
10685 begin
10686 PN := Parent (N);
10687 while Present (PN) loop
10688 if Nkind (PN) = N_Parameter_Specification then
10689 return True;
10690 end if;
10691
10692 PN := Parent (PN);
10693 end loop;
10694
10695 return False;
10696 end In_Parameter_Specification;
10697
10698 --------------------------
10699 -- In_Pragma_Expression --
10700 --------------------------
10701
10702 function In_Pragma_Expression (N : Node_Id; Nam : Name_Id) return Boolean is
10703 P : Node_Id;
10704 begin
10705 P := Parent (N);
10706 loop
10707 if No (P) then
10708 return False;
10709 elsif Nkind (P) = N_Pragma and then Pragma_Name (P) = Nam then
10710 return True;
10711 else
10712 P := Parent (P);
10713 end if;
10714 end loop;
10715 end In_Pragma_Expression;
10716
10717 ---------------------------
10718 -- In_Pre_Post_Condition --
10719 ---------------------------
10720
10721 function In_Pre_Post_Condition (N : Node_Id) return Boolean is
10722 Par : Node_Id;
10723 Prag : Node_Id := Empty;
10724 Prag_Id : Pragma_Id;
10725
10726 begin
10727 -- Climb the parent chain looking for an enclosing pragma
10728
10729 Par := N;
10730 while Present (Par) loop
10731 if Nkind (Par) = N_Pragma then
10732 Prag := Par;
10733 exit;
10734
10735 -- Prevent the search from going too far
10736
10737 elsif Is_Body_Or_Package_Declaration (Par) then
10738 exit;
10739 end if;
10740
10741 Par := Parent (Par);
10742 end loop;
10743
10744 if Present (Prag) then
10745 Prag_Id := Get_Pragma_Id (Prag);
10746
10747 return
10748 Prag_Id = Pragma_Post
10749 or else Prag_Id = Pragma_Post_Class
10750 or else Prag_Id = Pragma_Postcondition
10751 or else Prag_Id = Pragma_Pre
10752 or else Prag_Id = Pragma_Pre_Class
10753 or else Prag_Id = Pragma_Precondition;
10754
10755 -- Otherwise the node is not enclosed by a pre/postcondition pragma
10756
10757 else
10758 return False;
10759 end if;
10760 end In_Pre_Post_Condition;
10761
10762 -------------------------------------
10763 -- In_Reverse_Storage_Order_Object --
10764 -------------------------------------
10765
10766 function In_Reverse_Storage_Order_Object (N : Node_Id) return Boolean is
10767 Pref : Node_Id;
10768 Btyp : Entity_Id := Empty;
10769
10770 begin
10771 -- Climb up indexed components
10772
10773 Pref := N;
10774 loop
10775 case Nkind (Pref) is
10776 when N_Selected_Component =>
10777 Pref := Prefix (Pref);
10778 exit;
10779
10780 when N_Indexed_Component =>
10781 Pref := Prefix (Pref);
10782
10783 when others =>
10784 Pref := Empty;
10785 exit;
10786 end case;
10787 end loop;
10788
10789 if Present (Pref) then
10790 Btyp := Base_Type (Etype (Pref));
10791 end if;
10792
10793 return Present (Btyp)
10794 and then (Is_Record_Type (Btyp) or else Is_Array_Type (Btyp))
10795 and then Reverse_Storage_Order (Btyp);
10796 end In_Reverse_Storage_Order_Object;
10797
10798 --------------------------------------
10799 -- In_Subprogram_Or_Concurrent_Unit --
10800 --------------------------------------
10801
10802 function In_Subprogram_Or_Concurrent_Unit return Boolean is
10803 E : Entity_Id;
10804 K : Entity_Kind;
10805
10806 begin
10807 -- Use scope chain to check successively outer scopes
10808
10809 E := Current_Scope;
10810 loop
10811 K := Ekind (E);
10812
10813 if K in Subprogram_Kind
10814 or else K in Concurrent_Kind
10815 or else K in Generic_Subprogram_Kind
10816 then
10817 return True;
10818
10819 elsif E = Standard_Standard then
10820 return False;
10821 end if;
10822
10823 E := Scope (E);
10824 end loop;
10825 end In_Subprogram_Or_Concurrent_Unit;
10826
10827 ---------------------
10828 -- In_Visible_Part --
10829 ---------------------
10830
10831 function In_Visible_Part (Scope_Id : Entity_Id) return Boolean is
10832 begin
10833 return Is_Package_Or_Generic_Package (Scope_Id)
10834 and then In_Open_Scopes (Scope_Id)
10835 and then not In_Package_Body (Scope_Id)
10836 and then not In_Private_Part (Scope_Id);
10837 end In_Visible_Part;
10838
10839 --------------------------------
10840 -- Incomplete_Or_Partial_View --
10841 --------------------------------
10842
10843 function Incomplete_Or_Partial_View (Id : Entity_Id) return Entity_Id is
10844 function Inspect_Decls
10845 (Decls : List_Id;
10846 Taft : Boolean := False) return Entity_Id;
10847 -- Check whether a declarative region contains the incomplete or partial
10848 -- view of Id.
10849
10850 -------------------
10851 -- Inspect_Decls --
10852 -------------------
10853
10854 function Inspect_Decls
10855 (Decls : List_Id;
10856 Taft : Boolean := False) return Entity_Id
10857 is
10858 Decl : Node_Id;
10859 Match : Node_Id;
10860
10861 begin
10862 Decl := First (Decls);
10863 while Present (Decl) loop
10864 Match := Empty;
10865
10866 -- The partial view of a Taft-amendment type is an incomplete
10867 -- type.
10868
10869 if Taft then
10870 if Nkind (Decl) = N_Incomplete_Type_Declaration then
10871 Match := Defining_Identifier (Decl);
10872 end if;
10873
10874 -- Otherwise look for a private type whose full view matches the
10875 -- input type. Note that this checks full_type_declaration nodes
10876 -- to account for derivations from a private type where the type
10877 -- declaration hold the partial view and the full view is an
10878 -- itype.
10879
10880 elsif Nkind_In (Decl, N_Full_Type_Declaration,
10881 N_Private_Extension_Declaration,
10882 N_Private_Type_Declaration)
10883 then
10884 Match := Defining_Identifier (Decl);
10885 end if;
10886
10887 -- Guard against unanalyzed entities
10888
10889 if Present (Match)
10890 and then Is_Type (Match)
10891 and then Present (Full_View (Match))
10892 and then Full_View (Match) = Id
10893 then
10894 return Match;
10895 end if;
10896
10897 Next (Decl);
10898 end loop;
10899
10900 return Empty;
10901 end Inspect_Decls;
10902
10903 -- Local variables
10904
10905 Prev : Entity_Id;
10906
10907 -- Start of processing for Incomplete_Or_Partial_View
10908
10909 begin
10910 -- Deferred constant or incomplete type case
10911
10912 Prev := Current_Entity_In_Scope (Id);
10913
10914 if Present (Prev)
10915 and then (Is_Incomplete_Type (Prev) or else Ekind (Prev) = E_Constant)
10916 and then Present (Full_View (Prev))
10917 and then Full_View (Prev) = Id
10918 then
10919 return Prev;
10920 end if;
10921
10922 -- Private or Taft amendment type case
10923
10924 declare
10925 Pkg : constant Entity_Id := Scope (Id);
10926 Pkg_Decl : Node_Id := Pkg;
10927
10928 begin
10929 if Present (Pkg)
10930 and then Ekind_In (Pkg, E_Generic_Package, E_Package)
10931 then
10932 while Nkind (Pkg_Decl) /= N_Package_Specification loop
10933 Pkg_Decl := Parent (Pkg_Decl);
10934 end loop;
10935
10936 -- It is knows that Typ has a private view, look for it in the
10937 -- visible declarations of the enclosing scope. A special case
10938 -- of this is when the two views have been exchanged - the full
10939 -- appears earlier than the private.
10940
10941 if Has_Private_Declaration (Id) then
10942 Prev := Inspect_Decls (Visible_Declarations (Pkg_Decl));
10943
10944 -- Exchanged view case, look in the private declarations
10945
10946 if No (Prev) then
10947 Prev := Inspect_Decls (Private_Declarations (Pkg_Decl));
10948 end if;
10949
10950 return Prev;
10951
10952 -- Otherwise if this is the package body, then Typ is a potential
10953 -- Taft amendment type. The incomplete view should be located in
10954 -- the private declarations of the enclosing scope.
10955
10956 elsif In_Package_Body (Pkg) then
10957 return Inspect_Decls (Private_Declarations (Pkg_Decl), True);
10958 end if;
10959 end if;
10960 end;
10961
10962 -- The type has no incomplete or private view
10963
10964 return Empty;
10965 end Incomplete_Or_Partial_View;
10966
10967 ----------------------------------
10968 -- Indexed_Component_Bit_Offset --
10969 ----------------------------------
10970
10971 function Indexed_Component_Bit_Offset (N : Node_Id) return Uint is
10972 Exp : constant Node_Id := First (Expressions (N));
10973 Typ : constant Entity_Id := Etype (Prefix (N));
10974 Off : constant Uint := Component_Size (Typ);
10975 Ind : Node_Id;
10976
10977 begin
10978 -- Return early if the component size is not known or variable
10979
10980 if Off = No_Uint or else Off < Uint_0 then
10981 return No_Uint;
10982 end if;
10983
10984 -- Deal with the degenerate case of an empty component
10985
10986 if Off = Uint_0 then
10987 return Off;
10988 end if;
10989
10990 -- Check that both the index value and the low bound are known
10991
10992 if not Compile_Time_Known_Value (Exp) then
10993 return No_Uint;
10994 end if;
10995
10996 Ind := First_Index (Typ);
10997 if No (Ind) then
10998 return No_Uint;
10999 end if;
11000
11001 if Nkind (Ind) = N_Subtype_Indication then
11002 Ind := Constraint (Ind);
11003
11004 if Nkind (Ind) = N_Range_Constraint then
11005 Ind := Range_Expression (Ind);
11006 end if;
11007 end if;
11008
11009 if Nkind (Ind) /= N_Range
11010 or else not Compile_Time_Known_Value (Low_Bound (Ind))
11011 then
11012 return No_Uint;
11013 end if;
11014
11015 -- Return the scaled offset
11016
11017 return Off * (Expr_Value (Exp) - Expr_Value (Low_Bound ((Ind))));
11018 end Indexed_Component_Bit_Offset;
11019
11020 ----------------------------
11021 -- Inherit_Rep_Item_Chain --
11022 ----------------------------
11023
11024 procedure Inherit_Rep_Item_Chain (Typ : Entity_Id; From_Typ : Entity_Id) is
11025 Item : Node_Id;
11026 Next_Item : Node_Id;
11027
11028 begin
11029 -- There are several inheritance scenarios to consider depending on
11030 -- whether both types have rep item chains and whether the destination
11031 -- type already inherits part of the source type's rep item chain.
11032
11033 -- 1) The source type lacks a rep item chain
11034 -- From_Typ ---> Empty
11035 --
11036 -- Typ --------> Item (or Empty)
11037
11038 -- In this case inheritance cannot take place because there are no items
11039 -- to inherit.
11040
11041 -- 2) The destination type lacks a rep item chain
11042 -- From_Typ ---> Item ---> ...
11043 --
11044 -- Typ --------> Empty
11045
11046 -- Inheritance takes place by setting the First_Rep_Item of the
11047 -- destination type to the First_Rep_Item of the source type.
11048 -- From_Typ ---> Item ---> ...
11049 -- ^
11050 -- Typ -----------+
11051
11052 -- 3.1) Both source and destination types have at least one rep item.
11053 -- The destination type does NOT inherit a rep item from the source
11054 -- type.
11055 -- From_Typ ---> Item ---> Item
11056 --
11057 -- Typ --------> Item ---> Item
11058
11059 -- Inheritance takes place by setting the Next_Rep_Item of the last item
11060 -- of the destination type to the First_Rep_Item of the source type.
11061 -- From_Typ -------------------> Item ---> Item
11062 -- ^
11063 -- Typ --------> Item ---> Item --+
11064
11065 -- 3.2) Both source and destination types have at least one rep item.
11066 -- The destination type DOES inherit part of the rep item chain of the
11067 -- source type.
11068 -- From_Typ ---> Item ---> Item ---> Item
11069 -- ^
11070 -- Typ --------> Item ------+
11071
11072 -- This rare case arises when the full view of a private extension must
11073 -- inherit the rep item chain from the full view of its parent type and
11074 -- the full view of the parent type contains extra rep items. Currently
11075 -- only invariants may lead to such form of inheritance.
11076
11077 -- type From_Typ is tagged private
11078 -- with Type_Invariant'Class => Item_2;
11079
11080 -- type Typ is new From_Typ with private
11081 -- with Type_Invariant => Item_4;
11082
11083 -- At this point the rep item chains contain the following items
11084
11085 -- From_Typ -----------> Item_2 ---> Item_3
11086 -- ^
11087 -- Typ --------> Item_4 --+
11088
11089 -- The full views of both types may introduce extra invariants
11090
11091 -- type From_Typ is tagged null record
11092 -- with Type_Invariant => Item_1;
11093
11094 -- type Typ is new From_Typ with null record;
11095
11096 -- The full view of Typ would have to inherit any new rep items added to
11097 -- the full view of From_Typ.
11098
11099 -- From_Typ -----------> Item_1 ---> Item_2 ---> Item_3
11100 -- ^
11101 -- Typ --------> Item_4 --+
11102
11103 -- To achieve this form of inheritance, the destination type must first
11104 -- sever the link between its own rep chain and that of the source type,
11105 -- then inheritance 3.1 takes place.
11106
11107 -- Case 1: The source type lacks a rep item chain
11108
11109 if No (First_Rep_Item (From_Typ)) then
11110 return;
11111
11112 -- Case 2: The destination type lacks a rep item chain
11113
11114 elsif No (First_Rep_Item (Typ)) then
11115 Set_First_Rep_Item (Typ, First_Rep_Item (From_Typ));
11116
11117 -- Case 3: Both the source and destination types have at least one rep
11118 -- item. Traverse the rep item chain of the destination type to find the
11119 -- last rep item.
11120
11121 else
11122 Item := Empty;
11123 Next_Item := First_Rep_Item (Typ);
11124 while Present (Next_Item) loop
11125
11126 -- Detect a link between the destination type's rep chain and that
11127 -- of the source type. There are two possibilities:
11128
11129 -- Variant 1
11130 -- Next_Item
11131 -- V
11132 -- From_Typ ---> Item_1 --->
11133 -- ^
11134 -- Typ -----------+
11135 --
11136 -- Item is Empty
11137
11138 -- Variant 2
11139 -- Next_Item
11140 -- V
11141 -- From_Typ ---> Item_1 ---> Item_2 --->
11142 -- ^
11143 -- Typ --------> Item_3 ------+
11144 -- ^
11145 -- Item
11146
11147 if Has_Rep_Item (From_Typ, Next_Item) then
11148 exit;
11149 end if;
11150
11151 Item := Next_Item;
11152 Next_Item := Next_Rep_Item (Next_Item);
11153 end loop;
11154
11155 -- Inherit the source type's rep item chain
11156
11157 if Present (Item) then
11158 Set_Next_Rep_Item (Item, First_Rep_Item (From_Typ));
11159 else
11160 Set_First_Rep_Item (Typ, First_Rep_Item (From_Typ));
11161 end if;
11162 end if;
11163 end Inherit_Rep_Item_Chain;
11164
11165 ---------------------------------
11166 -- Insert_Explicit_Dereference --
11167 ---------------------------------
11168
11169 procedure Insert_Explicit_Dereference (N : Node_Id) is
11170 New_Prefix : constant Node_Id := Relocate_Node (N);
11171 Ent : Entity_Id := Empty;
11172 Pref : Node_Id;
11173 I : Interp_Index;
11174 It : Interp;
11175 T : Entity_Id;
11176
11177 begin
11178 Save_Interps (N, New_Prefix);
11179
11180 Rewrite (N,
11181 Make_Explicit_Dereference (Sloc (Parent (N)),
11182 Prefix => New_Prefix));
11183
11184 Set_Etype (N, Designated_Type (Etype (New_Prefix)));
11185
11186 if Is_Overloaded (New_Prefix) then
11187
11188 -- The dereference is also overloaded, and its interpretations are
11189 -- the designated types of the interpretations of the original node.
11190
11191 Set_Etype (N, Any_Type);
11192
11193 Get_First_Interp (New_Prefix, I, It);
11194 while Present (It.Nam) loop
11195 T := It.Typ;
11196
11197 if Is_Access_Type (T) then
11198 Add_One_Interp (N, Designated_Type (T), Designated_Type (T));
11199 end if;
11200
11201 Get_Next_Interp (I, It);
11202 end loop;
11203
11204 End_Interp_List;
11205
11206 else
11207 -- Prefix is unambiguous: mark the original prefix (which might
11208 -- Come_From_Source) as a reference, since the new (relocated) one
11209 -- won't be taken into account.
11210
11211 if Is_Entity_Name (New_Prefix) then
11212 Ent := Entity (New_Prefix);
11213 Pref := New_Prefix;
11214
11215 -- For a retrieval of a subcomponent of some composite object,
11216 -- retrieve the ultimate entity if there is one.
11217
11218 elsif Nkind_In (New_Prefix, N_Selected_Component,
11219 N_Indexed_Component)
11220 then
11221 Pref := Prefix (New_Prefix);
11222 while Present (Pref)
11223 and then Nkind_In (Pref, N_Selected_Component,
11224 N_Indexed_Component)
11225 loop
11226 Pref := Prefix (Pref);
11227 end loop;
11228
11229 if Present (Pref) and then Is_Entity_Name (Pref) then
11230 Ent := Entity (Pref);
11231 end if;
11232 end if;
11233
11234 -- Place the reference on the entity node
11235
11236 if Present (Ent) then
11237 Generate_Reference (Ent, Pref);
11238 end if;
11239 end if;
11240 end Insert_Explicit_Dereference;
11241
11242 ------------------------------------------
11243 -- Inspect_Deferred_Constant_Completion --
11244 ------------------------------------------
11245
11246 procedure Inspect_Deferred_Constant_Completion (Decls : List_Id) is
11247 Decl : Node_Id;
11248
11249 begin
11250 Decl := First (Decls);
11251 while Present (Decl) loop
11252
11253 -- Deferred constant signature
11254
11255 if Nkind (Decl) = N_Object_Declaration
11256 and then Constant_Present (Decl)
11257 and then No (Expression (Decl))
11258
11259 -- No need to check internally generated constants
11260
11261 and then Comes_From_Source (Decl)
11262
11263 -- The constant is not completed. A full object declaration or a
11264 -- pragma Import complete a deferred constant.
11265
11266 and then not Has_Completion (Defining_Identifier (Decl))
11267 then
11268 Error_Msg_N
11269 ("constant declaration requires initialization expression",
11270 Defining_Identifier (Decl));
11271 end if;
11272
11273 Decl := Next (Decl);
11274 end loop;
11275 end Inspect_Deferred_Constant_Completion;
11276
11277 -----------------------------
11278 -- Install_Generic_Formals --
11279 -----------------------------
11280
11281 procedure Install_Generic_Formals (Subp_Id : Entity_Id) is
11282 E : Entity_Id;
11283
11284 begin
11285 pragma Assert (Is_Generic_Subprogram (Subp_Id));
11286
11287 E := First_Entity (Subp_Id);
11288 while Present (E) loop
11289 Install_Entity (E);
11290 Next_Entity (E);
11291 end loop;
11292 end Install_Generic_Formals;
11293
11294 -----------------------------
11295 -- Is_Actual_Out_Parameter --
11296 -----------------------------
11297
11298 function Is_Actual_Out_Parameter (N : Node_Id) return Boolean is
11299 Formal : Entity_Id;
11300 Call : Node_Id;
11301 begin
11302 Find_Actual (N, Formal, Call);
11303 return Present (Formal) and then Ekind (Formal) = E_Out_Parameter;
11304 end Is_Actual_Out_Parameter;
11305
11306 -------------------------
11307 -- Is_Actual_Parameter --
11308 -------------------------
11309
11310 function Is_Actual_Parameter (N : Node_Id) return Boolean is
11311 PK : constant Node_Kind := Nkind (Parent (N));
11312
11313 begin
11314 case PK is
11315 when N_Parameter_Association =>
11316 return N = Explicit_Actual_Parameter (Parent (N));
11317
11318 when N_Subprogram_Call =>
11319 return Is_List_Member (N)
11320 and then
11321 List_Containing (N) = Parameter_Associations (Parent (N));
11322
11323 when others =>
11324 return False;
11325 end case;
11326 end Is_Actual_Parameter;
11327
11328 --------------------------------
11329 -- Is_Actual_Tagged_Parameter --
11330 --------------------------------
11331
11332 function Is_Actual_Tagged_Parameter (N : Node_Id) return Boolean is
11333 Formal : Entity_Id;
11334 Call : Node_Id;
11335 begin
11336 Find_Actual (N, Formal, Call);
11337 return Present (Formal) and then Is_Tagged_Type (Etype (Formal));
11338 end Is_Actual_Tagged_Parameter;
11339
11340 ---------------------
11341 -- Is_Aliased_View --
11342 ---------------------
11343
11344 function Is_Aliased_View (Obj : Node_Id) return Boolean is
11345 E : Entity_Id;
11346
11347 begin
11348 if Is_Entity_Name (Obj) then
11349 E := Entity (Obj);
11350
11351 return
11352 (Is_Object (E)
11353 and then
11354 (Is_Aliased (E)
11355 or else (Present (Renamed_Object (E))
11356 and then Is_Aliased_View (Renamed_Object (E)))))
11357
11358 or else ((Is_Formal (E)
11359 or else Ekind_In (E, E_Generic_In_Out_Parameter,
11360 E_Generic_In_Parameter))
11361 and then Is_Tagged_Type (Etype (E)))
11362
11363 or else (Is_Concurrent_Type (E) and then In_Open_Scopes (E))
11364
11365 -- Current instance of type, either directly or as rewritten
11366 -- reference to the current object.
11367
11368 or else (Is_Entity_Name (Original_Node (Obj))
11369 and then Present (Entity (Original_Node (Obj)))
11370 and then Is_Type (Entity (Original_Node (Obj))))
11371
11372 or else (Is_Type (E) and then E = Current_Scope)
11373
11374 or else (Is_Incomplete_Or_Private_Type (E)
11375 and then Full_View (E) = Current_Scope)
11376
11377 -- Ada 2012 AI05-0053: the return object of an extended return
11378 -- statement is aliased if its type is immutably limited.
11379
11380 or else (Is_Return_Object (E)
11381 and then Is_Limited_View (Etype (E)));
11382
11383 elsif Nkind (Obj) = N_Selected_Component then
11384 return Is_Aliased (Entity (Selector_Name (Obj)));
11385
11386 elsif Nkind (Obj) = N_Indexed_Component then
11387 return Has_Aliased_Components (Etype (Prefix (Obj)))
11388 or else
11389 (Is_Access_Type (Etype (Prefix (Obj)))
11390 and then Has_Aliased_Components
11391 (Designated_Type (Etype (Prefix (Obj)))));
11392
11393 elsif Nkind_In (Obj, N_Unchecked_Type_Conversion, N_Type_Conversion) then
11394 return Is_Tagged_Type (Etype (Obj))
11395 and then Is_Aliased_View (Expression (Obj));
11396
11397 elsif Nkind (Obj) = N_Explicit_Dereference then
11398 return Nkind (Original_Node (Obj)) /= N_Function_Call;
11399
11400 else
11401 return False;
11402 end if;
11403 end Is_Aliased_View;
11404
11405 -------------------------
11406 -- Is_Ancestor_Package --
11407 -------------------------
11408
11409 function Is_Ancestor_Package
11410 (E1 : Entity_Id;
11411 E2 : Entity_Id) return Boolean
11412 is
11413 Par : Entity_Id;
11414
11415 begin
11416 Par := E2;
11417 while Present (Par) and then Par /= Standard_Standard loop
11418 if Par = E1 then
11419 return True;
11420 end if;
11421
11422 Par := Scope (Par);
11423 end loop;
11424
11425 return False;
11426 end Is_Ancestor_Package;
11427
11428 ----------------------
11429 -- Is_Atomic_Object --
11430 ----------------------
11431
11432 function Is_Atomic_Object (N : Node_Id) return Boolean is
11433
11434 function Object_Has_Atomic_Components (N : Node_Id) return Boolean;
11435 -- Determines if given object has atomic components
11436
11437 function Is_Atomic_Prefix (N : Node_Id) return Boolean;
11438 -- If prefix is an implicit dereference, examine designated type
11439
11440 ----------------------
11441 -- Is_Atomic_Prefix --
11442 ----------------------
11443
11444 function Is_Atomic_Prefix (N : Node_Id) return Boolean is
11445 begin
11446 if Is_Access_Type (Etype (N)) then
11447 return
11448 Has_Atomic_Components (Designated_Type (Etype (N)));
11449 else
11450 return Object_Has_Atomic_Components (N);
11451 end if;
11452 end Is_Atomic_Prefix;
11453
11454 ----------------------------------
11455 -- Object_Has_Atomic_Components --
11456 ----------------------------------
11457
11458 function Object_Has_Atomic_Components (N : Node_Id) return Boolean is
11459 begin
11460 if Has_Atomic_Components (Etype (N))
11461 or else Is_Atomic (Etype (N))
11462 then
11463 return True;
11464
11465 elsif Is_Entity_Name (N)
11466 and then (Has_Atomic_Components (Entity (N))
11467 or else Is_Atomic (Entity (N)))
11468 then
11469 return True;
11470
11471 elsif Nkind (N) = N_Selected_Component
11472 and then Is_Atomic (Entity (Selector_Name (N)))
11473 then
11474 return True;
11475
11476 elsif Nkind (N) = N_Indexed_Component
11477 or else Nkind (N) = N_Selected_Component
11478 then
11479 return Is_Atomic_Prefix (Prefix (N));
11480
11481 else
11482 return False;
11483 end if;
11484 end Object_Has_Atomic_Components;
11485
11486 -- Start of processing for Is_Atomic_Object
11487
11488 begin
11489 -- Predicate is not relevant to subprograms
11490
11491 if Is_Entity_Name (N) and then Is_Overloadable (Entity (N)) then
11492 return False;
11493
11494 elsif Is_Atomic (Etype (N))
11495 or else (Is_Entity_Name (N) and then Is_Atomic (Entity (N)))
11496 then
11497 return True;
11498
11499 elsif Nkind (N) = N_Selected_Component
11500 and then Is_Atomic (Entity (Selector_Name (N)))
11501 then
11502 return True;
11503
11504 elsif Nkind (N) = N_Indexed_Component
11505 or else Nkind (N) = N_Selected_Component
11506 then
11507 return Is_Atomic_Prefix (Prefix (N));
11508
11509 else
11510 return False;
11511 end if;
11512 end Is_Atomic_Object;
11513
11514 -----------------------------
11515 -- Is_Atomic_Or_VFA_Object --
11516 -----------------------------
11517
11518 function Is_Atomic_Or_VFA_Object (N : Node_Id) return Boolean is
11519 begin
11520 return Is_Atomic_Object (N)
11521 or else (Is_Object_Reference (N)
11522 and then Is_Entity_Name (N)
11523 and then (Is_Volatile_Full_Access (Entity (N))
11524 or else
11525 Is_Volatile_Full_Access (Etype (Entity (N)))));
11526 end Is_Atomic_Or_VFA_Object;
11527
11528 -------------------------
11529 -- Is_Attribute_Result --
11530 -------------------------
11531
11532 function Is_Attribute_Result (N : Node_Id) return Boolean is
11533 begin
11534 return Nkind (N) = N_Attribute_Reference
11535 and then Attribute_Name (N) = Name_Result;
11536 end Is_Attribute_Result;
11537
11538 -------------------------
11539 -- Is_Attribute_Update --
11540 -------------------------
11541
11542 function Is_Attribute_Update (N : Node_Id) return Boolean is
11543 begin
11544 return Nkind (N) = N_Attribute_Reference
11545 and then Attribute_Name (N) = Name_Update;
11546 end Is_Attribute_Update;
11547
11548 ------------------------------------
11549 -- Is_Body_Or_Package_Declaration --
11550 ------------------------------------
11551
11552 function Is_Body_Or_Package_Declaration (N : Node_Id) return Boolean is
11553 begin
11554 return Nkind_In (N, N_Entry_Body,
11555 N_Package_Body,
11556 N_Package_Declaration,
11557 N_Protected_Body,
11558 N_Subprogram_Body,
11559 N_Task_Body);
11560 end Is_Body_Or_Package_Declaration;
11561
11562 -----------------------
11563 -- Is_Bounded_String --
11564 -----------------------
11565
11566 function Is_Bounded_String (T : Entity_Id) return Boolean is
11567 Under : constant Entity_Id := Underlying_Type (Root_Type (T));
11568
11569 begin
11570 -- Check whether T is ultimately derived from Ada.Strings.Superbounded.
11571 -- Super_String, or one of the [Wide_]Wide_ versions. This will
11572 -- be True for all the Bounded_String types in instances of the
11573 -- Generic_Bounded_Length generics, and for types derived from those.
11574
11575 return Present (Under)
11576 and then (Is_RTE (Root_Type (Under), RO_SU_Super_String) or else
11577 Is_RTE (Root_Type (Under), RO_WI_Super_String) or else
11578 Is_RTE (Root_Type (Under), RO_WW_Super_String));
11579 end Is_Bounded_String;
11580
11581 -------------------------
11582 -- Is_Child_Or_Sibling --
11583 -------------------------
11584
11585 function Is_Child_Or_Sibling
11586 (Pack_1 : Entity_Id;
11587 Pack_2 : Entity_Id) return Boolean
11588 is
11589 function Distance_From_Standard (Pack : Entity_Id) return Nat;
11590 -- Given an arbitrary package, return the number of "climbs" necessary
11591 -- to reach scope Standard_Standard.
11592
11593 procedure Equalize_Depths
11594 (Pack : in out Entity_Id;
11595 Depth : in out Nat;
11596 Depth_To_Reach : Nat);
11597 -- Given an arbitrary package, its depth and a target depth to reach,
11598 -- climb the scope chain until the said depth is reached. The pointer
11599 -- to the package and its depth a modified during the climb.
11600
11601 ----------------------------
11602 -- Distance_From_Standard --
11603 ----------------------------
11604
11605 function Distance_From_Standard (Pack : Entity_Id) return Nat is
11606 Dist : Nat;
11607 Scop : Entity_Id;
11608
11609 begin
11610 Dist := 0;
11611 Scop := Pack;
11612 while Present (Scop) and then Scop /= Standard_Standard loop
11613 Dist := Dist + 1;
11614 Scop := Scope (Scop);
11615 end loop;
11616
11617 return Dist;
11618 end Distance_From_Standard;
11619
11620 ---------------------
11621 -- Equalize_Depths --
11622 ---------------------
11623
11624 procedure Equalize_Depths
11625 (Pack : in out Entity_Id;
11626 Depth : in out Nat;
11627 Depth_To_Reach : Nat)
11628 is
11629 begin
11630 -- The package must be at a greater or equal depth
11631
11632 if Depth < Depth_To_Reach then
11633 raise Program_Error;
11634 end if;
11635
11636 -- Climb the scope chain until the desired depth is reached
11637
11638 while Present (Pack) and then Depth /= Depth_To_Reach loop
11639 Pack := Scope (Pack);
11640 Depth := Depth - 1;
11641 end loop;
11642 end Equalize_Depths;
11643
11644 -- Local variables
11645
11646 P_1 : Entity_Id := Pack_1;
11647 P_1_Child : Boolean := False;
11648 P_1_Depth : Nat := Distance_From_Standard (P_1);
11649 P_2 : Entity_Id := Pack_2;
11650 P_2_Child : Boolean := False;
11651 P_2_Depth : Nat := Distance_From_Standard (P_2);
11652
11653 -- Start of processing for Is_Child_Or_Sibling
11654
11655 begin
11656 pragma Assert
11657 (Ekind (Pack_1) = E_Package and then Ekind (Pack_2) = E_Package);
11658
11659 -- Both packages denote the same entity, therefore they cannot be
11660 -- children or siblings.
11661
11662 if P_1 = P_2 then
11663 return False;
11664
11665 -- One of the packages is at a deeper level than the other. Note that
11666 -- both may still come from differen hierarchies.
11667
11668 -- (root) P_2
11669 -- / \ :
11670 -- X P_2 or X
11671 -- : :
11672 -- P_1 P_1
11673
11674 elsif P_1_Depth > P_2_Depth then
11675 Equalize_Depths
11676 (Pack => P_1,
11677 Depth => P_1_Depth,
11678 Depth_To_Reach => P_2_Depth);
11679 P_1_Child := True;
11680
11681 -- (root) P_1
11682 -- / \ :
11683 -- P_1 X or X
11684 -- : :
11685 -- P_2 P_2
11686
11687 elsif P_2_Depth > P_1_Depth then
11688 Equalize_Depths
11689 (Pack => P_2,
11690 Depth => P_2_Depth,
11691 Depth_To_Reach => P_1_Depth);
11692 P_2_Child := True;
11693 end if;
11694
11695 -- At this stage the package pointers have been elevated to the same
11696 -- depth. If the related entities are the same, then one package is a
11697 -- potential child of the other:
11698
11699 -- P_1
11700 -- :
11701 -- X became P_1 P_2 or vica versa
11702 -- :
11703 -- P_2
11704
11705 if P_1 = P_2 then
11706 if P_1_Child then
11707 return Is_Child_Unit (Pack_1);
11708
11709 else pragma Assert (P_2_Child);
11710 return Is_Child_Unit (Pack_2);
11711 end if;
11712
11713 -- The packages may come from the same package chain or from entirely
11714 -- different hierarcies. To determine this, climb the scope stack until
11715 -- a common root is found.
11716
11717 -- (root) (root 1) (root 2)
11718 -- / \ | |
11719 -- P_1 P_2 P_1 P_2
11720
11721 else
11722 while Present (P_1) and then Present (P_2) loop
11723
11724 -- The two packages may be siblings
11725
11726 if P_1 = P_2 then
11727 return Is_Child_Unit (Pack_1) and then Is_Child_Unit (Pack_2);
11728 end if;
11729
11730 P_1 := Scope (P_1);
11731 P_2 := Scope (P_2);
11732 end loop;
11733 end if;
11734
11735 return False;
11736 end Is_Child_Or_Sibling;
11737
11738 -----------------------------
11739 -- Is_Concurrent_Interface --
11740 -----------------------------
11741
11742 function Is_Concurrent_Interface (T : Entity_Id) return Boolean is
11743 begin
11744 return Is_Interface (T)
11745 and then
11746 (Is_Protected_Interface (T)
11747 or else Is_Synchronized_Interface (T)
11748 or else Is_Task_Interface (T));
11749 end Is_Concurrent_Interface;
11750
11751 -----------------------
11752 -- Is_Constant_Bound --
11753 -----------------------
11754
11755 function Is_Constant_Bound (Exp : Node_Id) return Boolean is
11756 begin
11757 if Compile_Time_Known_Value (Exp) then
11758 return True;
11759
11760 elsif Is_Entity_Name (Exp) and then Present (Entity (Exp)) then
11761 return Is_Constant_Object (Entity (Exp))
11762 or else Ekind (Entity (Exp)) = E_Enumeration_Literal;
11763
11764 elsif Nkind (Exp) in N_Binary_Op then
11765 return Is_Constant_Bound (Left_Opnd (Exp))
11766 and then Is_Constant_Bound (Right_Opnd (Exp))
11767 and then Scope (Entity (Exp)) = Standard_Standard;
11768
11769 else
11770 return False;
11771 end if;
11772 end Is_Constant_Bound;
11773
11774 ---------------------------
11775 -- Is_Container_Element --
11776 ---------------------------
11777
11778 function Is_Container_Element (Exp : Node_Id) return Boolean is
11779 Loc : constant Source_Ptr := Sloc (Exp);
11780 Pref : constant Node_Id := Prefix (Exp);
11781
11782 Call : Node_Id;
11783 -- Call to an indexing aspect
11784
11785 Cont_Typ : Entity_Id;
11786 -- The type of the container being accessed
11787
11788 Elem_Typ : Entity_Id;
11789 -- Its element type
11790
11791 Indexing : Entity_Id;
11792 Is_Const : Boolean;
11793 -- Indicates that constant indexing is used, and the element is thus
11794 -- a constant.
11795
11796 Ref_Typ : Entity_Id;
11797 -- The reference type returned by the indexing operation
11798
11799 begin
11800 -- If C is a container, in a context that imposes the element type of
11801 -- that container, the indexing notation C (X) is rewritten as:
11802
11803 -- Indexing (C, X).Discr.all
11804
11805 -- where Indexing is one of the indexing aspects of the container.
11806 -- If the context does not require a reference, the construct can be
11807 -- rewritten as
11808
11809 -- Element (C, X)
11810
11811 -- First, verify that the construct has the proper form
11812
11813 if not Expander_Active then
11814 return False;
11815
11816 elsif Nkind (Pref) /= N_Selected_Component then
11817 return False;
11818
11819 elsif Nkind (Prefix (Pref)) /= N_Function_Call then
11820 return False;
11821
11822 else
11823 Call := Prefix (Pref);
11824 Ref_Typ := Etype (Call);
11825 end if;
11826
11827 if not Has_Implicit_Dereference (Ref_Typ)
11828 or else No (First (Parameter_Associations (Call)))
11829 or else not Is_Entity_Name (Name (Call))
11830 then
11831 return False;
11832 end if;
11833
11834 -- Retrieve type of container object, and its iterator aspects
11835
11836 Cont_Typ := Etype (First (Parameter_Associations (Call)));
11837 Indexing := Find_Value_Of_Aspect (Cont_Typ, Aspect_Constant_Indexing);
11838 Is_Const := False;
11839
11840 if No (Indexing) then
11841
11842 -- Container should have at least one indexing operation
11843
11844 return False;
11845
11846 elsif Entity (Name (Call)) /= Entity (Indexing) then
11847
11848 -- This may be a variable indexing operation
11849
11850 Indexing := Find_Value_Of_Aspect (Cont_Typ, Aspect_Variable_Indexing);
11851
11852 if No (Indexing)
11853 or else Entity (Name (Call)) /= Entity (Indexing)
11854 then
11855 return False;
11856 end if;
11857
11858 else
11859 Is_Const := True;
11860 end if;
11861
11862 Elem_Typ := Find_Value_Of_Aspect (Cont_Typ, Aspect_Iterator_Element);
11863
11864 if No (Elem_Typ) or else Entity (Elem_Typ) /= Etype (Exp) then
11865 return False;
11866 end if;
11867
11868 -- Check that the expression is not the target of an assignment, in
11869 -- which case the rewriting is not possible.
11870
11871 if not Is_Const then
11872 declare
11873 Par : Node_Id;
11874
11875 begin
11876 Par := Exp;
11877 while Present (Par)
11878 loop
11879 if Nkind (Parent (Par)) = N_Assignment_Statement
11880 and then Par = Name (Parent (Par))
11881 then
11882 return False;
11883
11884 -- A renaming produces a reference, and the transformation
11885 -- does not apply.
11886
11887 elsif Nkind (Parent (Par)) = N_Object_Renaming_Declaration then
11888 return False;
11889
11890 elsif Nkind_In
11891 (Nkind (Parent (Par)), N_Function_Call,
11892 N_Procedure_Call_Statement,
11893 N_Entry_Call_Statement)
11894 then
11895 -- Check that the element is not part of an actual for an
11896 -- in-out parameter.
11897
11898 declare
11899 F : Entity_Id;
11900 A : Node_Id;
11901
11902 begin
11903 F := First_Formal (Entity (Name (Parent (Par))));
11904 A := First (Parameter_Associations (Parent (Par)));
11905 while Present (F) loop
11906 if A = Par and then Ekind (F) /= E_In_Parameter then
11907 return False;
11908 end if;
11909
11910 Next_Formal (F);
11911 Next (A);
11912 end loop;
11913 end;
11914
11915 -- E_In_Parameter in a call: element is not modified.
11916
11917 exit;
11918 end if;
11919
11920 Par := Parent (Par);
11921 end loop;
11922 end;
11923 end if;
11924
11925 -- The expression has the proper form and the context requires the
11926 -- element type. Retrieve the Element function of the container and
11927 -- rewrite the construct as a call to it.
11928
11929 declare
11930 Op : Elmt_Id;
11931
11932 begin
11933 Op := First_Elmt (Primitive_Operations (Cont_Typ));
11934 while Present (Op) loop
11935 exit when Chars (Node (Op)) = Name_Element;
11936 Next_Elmt (Op);
11937 end loop;
11938
11939 if No (Op) then
11940 return False;
11941
11942 else
11943 Rewrite (Exp,
11944 Make_Function_Call (Loc,
11945 Name => New_Occurrence_Of (Node (Op), Loc),
11946 Parameter_Associations => Parameter_Associations (Call)));
11947 Analyze_And_Resolve (Exp, Entity (Elem_Typ));
11948 return True;
11949 end if;
11950 end;
11951 end Is_Container_Element;
11952
11953 ----------------------------
11954 -- Is_Contract_Annotation --
11955 ----------------------------
11956
11957 function Is_Contract_Annotation (Item : Node_Id) return Boolean is
11958 begin
11959 return Is_Package_Contract_Annotation (Item)
11960 or else
11961 Is_Subprogram_Contract_Annotation (Item);
11962 end Is_Contract_Annotation;
11963
11964 --------------------------------------
11965 -- Is_Controlling_Limited_Procedure --
11966 --------------------------------------
11967
11968 function Is_Controlling_Limited_Procedure
11969 (Proc_Nam : Entity_Id) return Boolean
11970 is
11971 Param_Typ : Entity_Id := Empty;
11972
11973 begin
11974 if Ekind (Proc_Nam) = E_Procedure
11975 and then Present (Parameter_Specifications (Parent (Proc_Nam)))
11976 then
11977 Param_Typ := Etype (Parameter_Type (First (
11978 Parameter_Specifications (Parent (Proc_Nam)))));
11979
11980 -- In this case where an Itype was created, the procedure call has been
11981 -- rewritten.
11982
11983 elsif Present (Associated_Node_For_Itype (Proc_Nam))
11984 and then Present (Original_Node (Associated_Node_For_Itype (Proc_Nam)))
11985 and then
11986 Present (Parameter_Associations
11987 (Associated_Node_For_Itype (Proc_Nam)))
11988 then
11989 Param_Typ :=
11990 Etype (First (Parameter_Associations
11991 (Associated_Node_For_Itype (Proc_Nam))));
11992 end if;
11993
11994 if Present (Param_Typ) then
11995 return
11996 Is_Interface (Param_Typ)
11997 and then Is_Limited_Record (Param_Typ);
11998 end if;
11999
12000 return False;
12001 end Is_Controlling_Limited_Procedure;
12002
12003 -----------------------------
12004 -- Is_CPP_Constructor_Call --
12005 -----------------------------
12006
12007 function Is_CPP_Constructor_Call (N : Node_Id) return Boolean is
12008 begin
12009 return Nkind (N) = N_Function_Call
12010 and then Is_CPP_Class (Etype (Etype (N)))
12011 and then Is_Constructor (Entity (Name (N)))
12012 and then Is_Imported (Entity (Name (N)));
12013 end Is_CPP_Constructor_Call;
12014
12015 -------------------------
12016 -- Is_Current_Instance --
12017 -------------------------
12018
12019 function Is_Current_Instance (N : Node_Id) return Boolean is
12020 Typ : constant Entity_Id := Entity (N);
12021 P : Node_Id;
12022
12023 begin
12024 -- Simplest case: entity is a concurrent type and we are currently
12025 -- inside the body. This will eventually be expanded into a
12026 -- call to Self (for tasks) or _object (for protected objects).
12027
12028 if Is_Concurrent_Type (Typ) and then In_Open_Scopes (Typ) then
12029 return True;
12030
12031 else
12032 -- Check whether the context is a (sub)type declaration for the
12033 -- type entity.
12034
12035 P := Parent (N);
12036 while Present (P) loop
12037 if Nkind_In (P, N_Full_Type_Declaration,
12038 N_Private_Type_Declaration,
12039 N_Subtype_Declaration)
12040 and then Comes_From_Source (P)
12041 and then Defining_Entity (P) = Typ
12042 then
12043 return True;
12044
12045 -- A subtype name may appear in an aspect specification for a
12046 -- Predicate_Failure aspect, for which we do not construct a
12047 -- wrapper procedure. The subtype will be replaced by the
12048 -- expression being tested when the corresponding predicate
12049 -- check is expanded.
12050
12051 elsif Nkind (P) = N_Aspect_Specification
12052 and then Nkind (Parent (P)) = N_Subtype_Declaration
12053 then
12054 return True;
12055
12056 elsif Nkind (P) = N_Pragma
12057 and then
12058 Get_Pragma_Id (P) = Pragma_Predicate_Failure
12059 then
12060 return True;
12061 end if;
12062
12063 P := Parent (P);
12064 end loop;
12065 end if;
12066
12067 -- In any other context this is not a current occurrence
12068
12069 return False;
12070 end Is_Current_Instance;
12071
12072 --------------------
12073 -- Is_Declaration --
12074 --------------------
12075
12076 function Is_Declaration (N : Node_Id) return Boolean is
12077 begin
12078 case Nkind (N) is
12079 when N_Abstract_Subprogram_Declaration |
12080 N_Exception_Declaration |
12081 N_Exception_Renaming_Declaration |
12082 N_Full_Type_Declaration |
12083 N_Generic_Function_Renaming_Declaration |
12084 N_Generic_Package_Declaration |
12085 N_Generic_Package_Renaming_Declaration |
12086 N_Generic_Procedure_Renaming_Declaration |
12087 N_Generic_Subprogram_Declaration |
12088 N_Number_Declaration |
12089 N_Object_Declaration |
12090 N_Object_Renaming_Declaration |
12091 N_Package_Declaration |
12092 N_Package_Renaming_Declaration |
12093 N_Private_Extension_Declaration |
12094 N_Private_Type_Declaration |
12095 N_Subprogram_Declaration |
12096 N_Subprogram_Renaming_Declaration |
12097 N_Subtype_Declaration =>
12098 return True;
12099
12100 when others =>
12101 return False;
12102 end case;
12103 end Is_Declaration;
12104
12105 --------------------------------
12106 -- Is_Declared_Within_Variant --
12107 --------------------------------
12108
12109 function Is_Declared_Within_Variant (Comp : Entity_Id) return Boolean is
12110 Comp_Decl : constant Node_Id := Parent (Comp);
12111 Comp_List : constant Node_Id := Parent (Comp_Decl);
12112 begin
12113 return Nkind (Parent (Comp_List)) = N_Variant;
12114 end Is_Declared_Within_Variant;
12115
12116 ----------------------------------------------
12117 -- Is_Dependent_Component_Of_Mutable_Object --
12118 ----------------------------------------------
12119
12120 function Is_Dependent_Component_Of_Mutable_Object
12121 (Object : Node_Id) return Boolean
12122 is
12123 P : Node_Id;
12124 Prefix_Type : Entity_Id;
12125 P_Aliased : Boolean := False;
12126 Comp : Entity_Id;
12127
12128 Deref : Node_Id := Object;
12129 -- Dereference node, in something like X.all.Y(2)
12130
12131 -- Start of processing for Is_Dependent_Component_Of_Mutable_Object
12132
12133 begin
12134 -- Find the dereference node if any
12135
12136 while Nkind_In (Deref, N_Indexed_Component,
12137 N_Selected_Component,
12138 N_Slice)
12139 loop
12140 Deref := Prefix (Deref);
12141 end loop;
12142
12143 -- Ada 2005: If we have a component or slice of a dereference,
12144 -- something like X.all.Y (2), and the type of X is access-to-constant,
12145 -- Is_Variable will return False, because it is indeed a constant
12146 -- view. But it might be a view of a variable object, so we want the
12147 -- following condition to be True in that case.
12148
12149 if Is_Variable (Object)
12150 or else (Ada_Version >= Ada_2005
12151 and then Nkind (Deref) = N_Explicit_Dereference)
12152 then
12153 if Nkind (Object) = N_Selected_Component then
12154 P := Prefix (Object);
12155 Prefix_Type := Etype (P);
12156
12157 if Is_Entity_Name (P) then
12158 if Ekind (Entity (P)) = E_Generic_In_Out_Parameter then
12159 Prefix_Type := Base_Type (Prefix_Type);
12160 end if;
12161
12162 if Is_Aliased (Entity (P)) then
12163 P_Aliased := True;
12164 end if;
12165
12166 -- A discriminant check on a selected component may be expanded
12167 -- into a dereference when removing side-effects. Recover the
12168 -- original node and its type, which may be unconstrained.
12169
12170 elsif Nkind (P) = N_Explicit_Dereference
12171 and then not (Comes_From_Source (P))
12172 then
12173 P := Original_Node (P);
12174 Prefix_Type := Etype (P);
12175
12176 else
12177 -- Check for prefix being an aliased component???
12178
12179 null;
12180
12181 end if;
12182
12183 -- A heap object is constrained by its initial value
12184
12185 -- Ada 2005 (AI-363): Always assume the object could be mutable in
12186 -- the dereferenced case, since the access value might denote an
12187 -- unconstrained aliased object, whereas in Ada 95 the designated
12188 -- object is guaranteed to be constrained. A worst-case assumption
12189 -- has to apply in Ada 2005 because we can't tell at compile
12190 -- time whether the object is "constrained by its initial value"
12191 -- (despite the fact that 3.10.2(26/2) and 8.5.1(5/2) are semantic
12192 -- rules (these rules are acknowledged to need fixing).
12193
12194 if Ada_Version < Ada_2005 then
12195 if Is_Access_Type (Prefix_Type)
12196 or else Nkind (P) = N_Explicit_Dereference
12197 then
12198 return False;
12199 end if;
12200
12201 else pragma Assert (Ada_Version >= Ada_2005);
12202 if Is_Access_Type (Prefix_Type) then
12203
12204 -- If the access type is pool-specific, and there is no
12205 -- constrained partial view of the designated type, then the
12206 -- designated object is known to be constrained.
12207
12208 if Ekind (Prefix_Type) = E_Access_Type
12209 and then not Object_Type_Has_Constrained_Partial_View
12210 (Typ => Designated_Type (Prefix_Type),
12211 Scop => Current_Scope)
12212 then
12213 return False;
12214
12215 -- Otherwise (general access type, or there is a constrained
12216 -- partial view of the designated type), we need to check
12217 -- based on the designated type.
12218
12219 else
12220 Prefix_Type := Designated_Type (Prefix_Type);
12221 end if;
12222 end if;
12223 end if;
12224
12225 Comp :=
12226 Original_Record_Component (Entity (Selector_Name (Object)));
12227
12228 -- As per AI-0017, the renaming is illegal in a generic body, even
12229 -- if the subtype is indefinite.
12230
12231 -- Ada 2005 (AI-363): In Ada 2005 an aliased object can be mutable
12232
12233 if not Is_Constrained (Prefix_Type)
12234 and then (Is_Definite_Subtype (Prefix_Type)
12235 or else
12236 (Is_Generic_Type (Prefix_Type)
12237 and then Ekind (Current_Scope) = E_Generic_Package
12238 and then In_Package_Body (Current_Scope)))
12239
12240 and then (Is_Declared_Within_Variant (Comp)
12241 or else Has_Discriminant_Dependent_Constraint (Comp))
12242 and then (not P_Aliased or else Ada_Version >= Ada_2005)
12243 then
12244 return True;
12245
12246 -- If the prefix is of an access type at this point, then we want
12247 -- to return False, rather than calling this function recursively
12248 -- on the access object (which itself might be a discriminant-
12249 -- dependent component of some other object, but that isn't
12250 -- relevant to checking the object passed to us). This avoids
12251 -- issuing wrong errors when compiling with -gnatc, where there
12252 -- can be implicit dereferences that have not been expanded.
12253
12254 elsif Is_Access_Type (Etype (Prefix (Object))) then
12255 return False;
12256
12257 else
12258 return
12259 Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
12260 end if;
12261
12262 elsif Nkind (Object) = N_Indexed_Component
12263 or else Nkind (Object) = N_Slice
12264 then
12265 return Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
12266
12267 -- A type conversion that Is_Variable is a view conversion:
12268 -- go back to the denoted object.
12269
12270 elsif Nkind (Object) = N_Type_Conversion then
12271 return
12272 Is_Dependent_Component_Of_Mutable_Object (Expression (Object));
12273 end if;
12274 end if;
12275
12276 return False;
12277 end Is_Dependent_Component_Of_Mutable_Object;
12278
12279 ---------------------
12280 -- Is_Dereferenced --
12281 ---------------------
12282
12283 function Is_Dereferenced (N : Node_Id) return Boolean is
12284 P : constant Node_Id := Parent (N);
12285 begin
12286 return Nkind_In (P, N_Selected_Component,
12287 N_Explicit_Dereference,
12288 N_Indexed_Component,
12289 N_Slice)
12290 and then Prefix (P) = N;
12291 end Is_Dereferenced;
12292
12293 ----------------------
12294 -- Is_Descendant_Of --
12295 ----------------------
12296
12297 function Is_Descendant_Of (T1 : Entity_Id; T2 : Entity_Id) return Boolean is
12298 T : Entity_Id;
12299 Etyp : Entity_Id;
12300
12301 begin
12302 pragma Assert (Nkind (T1) in N_Entity);
12303 pragma Assert (Nkind (T2) in N_Entity);
12304
12305 T := Base_Type (T1);
12306
12307 -- Immediate return if the types match
12308
12309 if T = T2 then
12310 return True;
12311
12312 -- Comment needed here ???
12313
12314 elsif Ekind (T) = E_Class_Wide_Type then
12315 return Etype (T) = T2;
12316
12317 -- All other cases
12318
12319 else
12320 loop
12321 Etyp := Etype (T);
12322
12323 -- Done if we found the type we are looking for
12324
12325 if Etyp = T2 then
12326 return True;
12327
12328 -- Done if no more derivations to check
12329
12330 elsif T = T1
12331 or else T = Etyp
12332 then
12333 return False;
12334
12335 -- Following test catches error cases resulting from prev errors
12336
12337 elsif No (Etyp) then
12338 return False;
12339
12340 elsif Is_Private_Type (T) and then Etyp = Full_View (T) then
12341 return False;
12342
12343 elsif Is_Private_Type (Etyp) and then Full_View (Etyp) = T then
12344 return False;
12345 end if;
12346
12347 T := Base_Type (Etyp);
12348 end loop;
12349 end if;
12350 end Is_Descendant_Of;
12351
12352 ----------------------------------------
12353 -- Is_Descendant_Of_Suspension_Object --
12354 ----------------------------------------
12355
12356 function Is_Descendant_Of_Suspension_Object
12357 (Typ : Entity_Id) return Boolean
12358 is
12359 Cur_Typ : Entity_Id;
12360 Par_Typ : Entity_Id;
12361
12362 begin
12363 -- Climb the type derivation chain checking each parent type against
12364 -- Suspension_Object.
12365
12366 Cur_Typ := Base_Type (Typ);
12367 while Present (Cur_Typ) loop
12368 Par_Typ := Etype (Cur_Typ);
12369
12370 -- The current type is a match
12371
12372 if Is_Suspension_Object (Cur_Typ) then
12373 return True;
12374
12375 -- Stop the traversal once the root of the derivation chain has been
12376 -- reached. In that case the current type is its own base type.
12377
12378 elsif Cur_Typ = Par_Typ then
12379 exit;
12380 end if;
12381
12382 Cur_Typ := Base_Type (Par_Typ);
12383 end loop;
12384
12385 return False;
12386 end Is_Descendant_Of_Suspension_Object;
12387
12388 ---------------------------------------------
12389 -- Is_Double_Precision_Floating_Point_Type --
12390 ---------------------------------------------
12391
12392 function Is_Double_Precision_Floating_Point_Type
12393 (E : Entity_Id) return Boolean is
12394 begin
12395 return Is_Floating_Point_Type (E)
12396 and then Machine_Radix_Value (E) = Uint_2
12397 and then Machine_Mantissa_Value (E) = UI_From_Int (53)
12398 and then Machine_Emax_Value (E) = Uint_2 ** Uint_10
12399 and then Machine_Emin_Value (E) = Uint_3 - (Uint_2 ** Uint_10);
12400 end Is_Double_Precision_Floating_Point_Type;
12401
12402 -----------------------------
12403 -- Is_Effectively_Volatile --
12404 -----------------------------
12405
12406 function Is_Effectively_Volatile (Id : Entity_Id) return Boolean is
12407 begin
12408 if Is_Type (Id) then
12409
12410 -- An arbitrary type is effectively volatile when it is subject to
12411 -- pragma Atomic or Volatile.
12412
12413 if Is_Volatile (Id) then
12414 return True;
12415
12416 -- An array type is effectively volatile when it is subject to pragma
12417 -- Atomic_Components or Volatile_Components or its component type is
12418 -- effectively volatile.
12419
12420 elsif Is_Array_Type (Id) then
12421 return
12422 Has_Volatile_Components (Id)
12423 or else
12424 Is_Effectively_Volatile (Component_Type (Base_Type (Id)));
12425
12426 -- A protected type is always volatile
12427
12428 elsif Is_Protected_Type (Id) then
12429 return True;
12430
12431 -- A descendant of Ada.Synchronous_Task_Control.Suspension_Object is
12432 -- automatically volatile.
12433
12434 elsif Is_Descendant_Of_Suspension_Object (Id) then
12435 return True;
12436
12437 -- Otherwise the type is not effectively volatile
12438
12439 else
12440 return False;
12441 end if;
12442
12443 -- Otherwise Id denotes an object
12444
12445 else
12446 return
12447 Is_Volatile (Id)
12448 or else Has_Volatile_Components (Id)
12449 or else Is_Effectively_Volatile (Etype (Id));
12450 end if;
12451 end Is_Effectively_Volatile;
12452
12453 ------------------------------------
12454 -- Is_Effectively_Volatile_Object --
12455 ------------------------------------
12456
12457 function Is_Effectively_Volatile_Object (N : Node_Id) return Boolean is
12458 begin
12459 if Is_Entity_Name (N) then
12460 return Is_Effectively_Volatile (Entity (N));
12461
12462 elsif Nkind (N) = N_Indexed_Component then
12463 return Is_Effectively_Volatile_Object (Prefix (N));
12464
12465 elsif Nkind (N) = N_Selected_Component then
12466 return
12467 Is_Effectively_Volatile_Object (Prefix (N))
12468 or else
12469 Is_Effectively_Volatile_Object (Selector_Name (N));
12470
12471 else
12472 return False;
12473 end if;
12474 end Is_Effectively_Volatile_Object;
12475
12476 -------------------
12477 -- Is_Entry_Body --
12478 -------------------
12479
12480 function Is_Entry_Body (Id : Entity_Id) return Boolean is
12481 begin
12482 return
12483 Ekind_In (Id, E_Entry, E_Entry_Family)
12484 and then Nkind (Unit_Declaration_Node (Id)) = N_Entry_Body;
12485 end Is_Entry_Body;
12486
12487 --------------------------
12488 -- Is_Entry_Declaration --
12489 --------------------------
12490
12491 function Is_Entry_Declaration (Id : Entity_Id) return Boolean is
12492 begin
12493 return
12494 Ekind_In (Id, E_Entry, E_Entry_Family)
12495 and then Nkind (Unit_Declaration_Node (Id)) = N_Entry_Declaration;
12496 end Is_Entry_Declaration;
12497
12498 ------------------------------------
12499 -- Is_Expanded_Priority_Attribute --
12500 ------------------------------------
12501
12502 function Is_Expanded_Priority_Attribute (E : Entity_Id) return Boolean is
12503 begin
12504 return
12505 Nkind (E) = N_Function_Call
12506 and then not Configurable_Run_Time_Mode
12507 and then (Entity (Name (E)) = RTE (RE_Get_Ceiling)
12508 or else Entity (Name (E)) = RTE (RO_PE_Get_Ceiling));
12509 end Is_Expanded_Priority_Attribute;
12510
12511 ----------------------------
12512 -- Is_Expression_Function --
12513 ----------------------------
12514
12515 function Is_Expression_Function (Subp : Entity_Id) return Boolean is
12516 begin
12517 if Ekind_In (Subp, E_Function, E_Subprogram_Body) then
12518 return
12519 Nkind (Original_Node (Unit_Declaration_Node (Subp))) =
12520 N_Expression_Function;
12521 else
12522 return False;
12523 end if;
12524 end Is_Expression_Function;
12525
12526 ------------------------------------------
12527 -- Is_Expression_Function_Or_Completion --
12528 ------------------------------------------
12529
12530 function Is_Expression_Function_Or_Completion
12531 (Subp : Entity_Id) return Boolean
12532 is
12533 Subp_Decl : Node_Id;
12534
12535 begin
12536 if Ekind (Subp) = E_Function then
12537 Subp_Decl := Unit_Declaration_Node (Subp);
12538
12539 -- The function declaration is either an expression function or is
12540 -- completed by an expression function body.
12541
12542 return
12543 Is_Expression_Function (Subp)
12544 or else (Nkind (Subp_Decl) = N_Subprogram_Declaration
12545 and then Present (Corresponding_Body (Subp_Decl))
12546 and then Is_Expression_Function
12547 (Corresponding_Body (Subp_Decl)));
12548
12549 elsif Ekind (Subp) = E_Subprogram_Body then
12550 return Is_Expression_Function (Subp);
12551
12552 else
12553 return False;
12554 end if;
12555 end Is_Expression_Function_Or_Completion;
12556
12557 -----------------------
12558 -- Is_EVF_Expression --
12559 -----------------------
12560
12561 function Is_EVF_Expression (N : Node_Id) return Boolean is
12562 Orig_N : constant Node_Id := Original_Node (N);
12563 Alt : Node_Id;
12564 Expr : Node_Id;
12565 Id : Entity_Id;
12566
12567 begin
12568 -- Detect a reference to a formal parameter of a specific tagged type
12569 -- whose related subprogram is subject to pragma Expresions_Visible with
12570 -- value "False".
12571
12572 if Is_Entity_Name (N) and then Present (Entity (N)) then
12573 Id := Entity (N);
12574
12575 return
12576 Is_Formal (Id)
12577 and then Is_Specific_Tagged_Type (Etype (Id))
12578 and then Extensions_Visible_Status (Id) =
12579 Extensions_Visible_False;
12580
12581 -- A case expression is an EVF expression when it contains at least one
12582 -- EVF dependent_expression. Note that a case expression may have been
12583 -- expanded, hence the use of Original_Node.
12584
12585 elsif Nkind (Orig_N) = N_Case_Expression then
12586 Alt := First (Alternatives (Orig_N));
12587 while Present (Alt) loop
12588 if Is_EVF_Expression (Expression (Alt)) then
12589 return True;
12590 end if;
12591
12592 Next (Alt);
12593 end loop;
12594
12595 -- An if expression is an EVF expression when it contains at least one
12596 -- EVF dependent_expression. Note that an if expression may have been
12597 -- expanded, hence the use of Original_Node.
12598
12599 elsif Nkind (Orig_N) = N_If_Expression then
12600 Expr := Next (First (Expressions (Orig_N)));
12601 while Present (Expr) loop
12602 if Is_EVF_Expression (Expr) then
12603 return True;
12604 end if;
12605
12606 Next (Expr);
12607 end loop;
12608
12609 -- A qualified expression or a type conversion is an EVF expression when
12610 -- its operand is an EVF expression.
12611
12612 elsif Nkind_In (N, N_Qualified_Expression,
12613 N_Unchecked_Type_Conversion,
12614 N_Type_Conversion)
12615 then
12616 return Is_EVF_Expression (Expression (N));
12617
12618 -- Attributes 'Loop_Entry, 'Old, and 'Update are EVF expressions when
12619 -- their prefix denotes an EVF expression.
12620
12621 elsif Nkind (N) = N_Attribute_Reference
12622 and then Nam_In (Attribute_Name (N), Name_Loop_Entry,
12623 Name_Old,
12624 Name_Update)
12625 then
12626 return Is_EVF_Expression (Prefix (N));
12627 end if;
12628
12629 return False;
12630 end Is_EVF_Expression;
12631
12632 --------------
12633 -- Is_False --
12634 --------------
12635
12636 function Is_False (U : Uint) return Boolean is
12637 begin
12638 return (U = 0);
12639 end Is_False;
12640
12641 ---------------------------
12642 -- Is_Fixed_Model_Number --
12643 ---------------------------
12644
12645 function Is_Fixed_Model_Number (U : Ureal; T : Entity_Id) return Boolean is
12646 S : constant Ureal := Small_Value (T);
12647 M : Urealp.Save_Mark;
12648 R : Boolean;
12649 begin
12650 M := Urealp.Mark;
12651 R := (U = UR_Trunc (U / S) * S);
12652 Urealp.Release (M);
12653 return R;
12654 end Is_Fixed_Model_Number;
12655
12656 -------------------------------
12657 -- Is_Fully_Initialized_Type --
12658 -------------------------------
12659
12660 function Is_Fully_Initialized_Type (Typ : Entity_Id) return Boolean is
12661 begin
12662 -- Scalar types
12663
12664 if Is_Scalar_Type (Typ) then
12665
12666 -- A scalar type with an aspect Default_Value is fully initialized
12667
12668 -- Note: Iniitalize/Normalize_Scalars also ensure full initialization
12669 -- of a scalar type, but we don't take that into account here, since
12670 -- we don't want these to affect warnings.
12671
12672 return Has_Default_Aspect (Typ);
12673
12674 elsif Is_Access_Type (Typ) then
12675 return True;
12676
12677 elsif Is_Array_Type (Typ) then
12678 if Is_Fully_Initialized_Type (Component_Type (Typ))
12679 or else (Ada_Version >= Ada_2012 and then Has_Default_Aspect (Typ))
12680 then
12681 return True;
12682 end if;
12683
12684 -- An interesting case, if we have a constrained type one of whose
12685 -- bounds is known to be null, then there are no elements to be
12686 -- initialized, so all the elements are initialized.
12687
12688 if Is_Constrained (Typ) then
12689 declare
12690 Indx : Node_Id;
12691 Indx_Typ : Entity_Id;
12692 Lbd, Hbd : Node_Id;
12693
12694 begin
12695 Indx := First_Index (Typ);
12696 while Present (Indx) loop
12697 if Etype (Indx) = Any_Type then
12698 return False;
12699
12700 -- If index is a range, use directly
12701
12702 elsif Nkind (Indx) = N_Range then
12703 Lbd := Low_Bound (Indx);
12704 Hbd := High_Bound (Indx);
12705
12706 else
12707 Indx_Typ := Etype (Indx);
12708
12709 if Is_Private_Type (Indx_Typ) then
12710 Indx_Typ := Full_View (Indx_Typ);
12711 end if;
12712
12713 if No (Indx_Typ) or else Etype (Indx_Typ) = Any_Type then
12714 return False;
12715 else
12716 Lbd := Type_Low_Bound (Indx_Typ);
12717 Hbd := Type_High_Bound (Indx_Typ);
12718 end if;
12719 end if;
12720
12721 if Compile_Time_Known_Value (Lbd)
12722 and then
12723 Compile_Time_Known_Value (Hbd)
12724 then
12725 if Expr_Value (Hbd) < Expr_Value (Lbd) then
12726 return True;
12727 end if;
12728 end if;
12729
12730 Next_Index (Indx);
12731 end loop;
12732 end;
12733 end if;
12734
12735 -- If no null indexes, then type is not fully initialized
12736
12737 return False;
12738
12739 -- Record types
12740
12741 elsif Is_Record_Type (Typ) then
12742 if Has_Discriminants (Typ)
12743 and then
12744 Present (Discriminant_Default_Value (First_Discriminant (Typ)))
12745 and then Is_Fully_Initialized_Variant (Typ)
12746 then
12747 return True;
12748 end if;
12749
12750 -- We consider bounded string types to be fully initialized, because
12751 -- otherwise we get false alarms when the Data component is not
12752 -- default-initialized.
12753
12754 if Is_Bounded_String (Typ) then
12755 return True;
12756 end if;
12757
12758 -- Controlled records are considered to be fully initialized if
12759 -- there is a user defined Initialize routine. This may not be
12760 -- entirely correct, but as the spec notes, we are guessing here
12761 -- what is best from the point of view of issuing warnings.
12762
12763 if Is_Controlled (Typ) then
12764 declare
12765 Utyp : constant Entity_Id := Underlying_Type (Typ);
12766
12767 begin
12768 if Present (Utyp) then
12769 declare
12770 Init : constant Entity_Id :=
12771 (Find_Optional_Prim_Op
12772 (Underlying_Type (Typ), Name_Initialize));
12773
12774 begin
12775 if Present (Init)
12776 and then Comes_From_Source (Init)
12777 and then not
12778 Is_Predefined_File_Name
12779 (File_Name (Get_Source_File_Index (Sloc (Init))))
12780 then
12781 return True;
12782
12783 elsif Has_Null_Extension (Typ)
12784 and then
12785 Is_Fully_Initialized_Type
12786 (Etype (Base_Type (Typ)))
12787 then
12788 return True;
12789 end if;
12790 end;
12791 end if;
12792 end;
12793 end if;
12794
12795 -- Otherwise see if all record components are initialized
12796
12797 declare
12798 Ent : Entity_Id;
12799
12800 begin
12801 Ent := First_Entity (Typ);
12802 while Present (Ent) loop
12803 if Ekind (Ent) = E_Component
12804 and then (No (Parent (Ent))
12805 or else No (Expression (Parent (Ent))))
12806 and then not Is_Fully_Initialized_Type (Etype (Ent))
12807
12808 -- Special VM case for tag components, which need to be
12809 -- defined in this case, but are never initialized as VMs
12810 -- are using other dispatching mechanisms. Ignore this
12811 -- uninitialized case. Note that this applies both to the
12812 -- uTag entry and the main vtable pointer (CPP_Class case).
12813
12814 and then (Tagged_Type_Expansion or else not Is_Tag (Ent))
12815 then
12816 return False;
12817 end if;
12818
12819 Next_Entity (Ent);
12820 end loop;
12821 end;
12822
12823 -- No uninitialized components, so type is fully initialized.
12824 -- Note that this catches the case of no components as well.
12825
12826 return True;
12827
12828 elsif Is_Concurrent_Type (Typ) then
12829 return True;
12830
12831 elsif Is_Private_Type (Typ) then
12832 declare
12833 U : constant Entity_Id := Underlying_Type (Typ);
12834
12835 begin
12836 if No (U) then
12837 return False;
12838 else
12839 return Is_Fully_Initialized_Type (U);
12840 end if;
12841 end;
12842
12843 else
12844 return False;
12845 end if;
12846 end Is_Fully_Initialized_Type;
12847
12848 ----------------------------------
12849 -- Is_Fully_Initialized_Variant --
12850 ----------------------------------
12851
12852 function Is_Fully_Initialized_Variant (Typ : Entity_Id) return Boolean is
12853 Loc : constant Source_Ptr := Sloc (Typ);
12854 Constraints : constant List_Id := New_List;
12855 Components : constant Elist_Id := New_Elmt_List;
12856 Comp_Elmt : Elmt_Id;
12857 Comp_Id : Node_Id;
12858 Comp_List : Node_Id;
12859 Discr : Entity_Id;
12860 Discr_Val : Node_Id;
12861
12862 Report_Errors : Boolean;
12863 pragma Warnings (Off, Report_Errors);
12864
12865 begin
12866 if Serious_Errors_Detected > 0 then
12867 return False;
12868 end if;
12869
12870 if Is_Record_Type (Typ)
12871 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
12872 and then Nkind (Type_Definition (Parent (Typ))) = N_Record_Definition
12873 then
12874 Comp_List := Component_List (Type_Definition (Parent (Typ)));
12875
12876 Discr := First_Discriminant (Typ);
12877 while Present (Discr) loop
12878 if Nkind (Parent (Discr)) = N_Discriminant_Specification then
12879 Discr_Val := Expression (Parent (Discr));
12880
12881 if Present (Discr_Val)
12882 and then Is_OK_Static_Expression (Discr_Val)
12883 then
12884 Append_To (Constraints,
12885 Make_Component_Association (Loc,
12886 Choices => New_List (New_Occurrence_Of (Discr, Loc)),
12887 Expression => New_Copy (Discr_Val)));
12888 else
12889 return False;
12890 end if;
12891 else
12892 return False;
12893 end if;
12894
12895 Next_Discriminant (Discr);
12896 end loop;
12897
12898 Gather_Components
12899 (Typ => Typ,
12900 Comp_List => Comp_List,
12901 Governed_By => Constraints,
12902 Into => Components,
12903 Report_Errors => Report_Errors);
12904
12905 -- Check that each component present is fully initialized
12906
12907 Comp_Elmt := First_Elmt (Components);
12908 while Present (Comp_Elmt) loop
12909 Comp_Id := Node (Comp_Elmt);
12910
12911 if Ekind (Comp_Id) = E_Component
12912 and then (No (Parent (Comp_Id))
12913 or else No (Expression (Parent (Comp_Id))))
12914 and then not Is_Fully_Initialized_Type (Etype (Comp_Id))
12915 then
12916 return False;
12917 end if;
12918
12919 Next_Elmt (Comp_Elmt);
12920 end loop;
12921
12922 return True;
12923
12924 elsif Is_Private_Type (Typ) then
12925 declare
12926 U : constant Entity_Id := Underlying_Type (Typ);
12927
12928 begin
12929 if No (U) then
12930 return False;
12931 else
12932 return Is_Fully_Initialized_Variant (U);
12933 end if;
12934 end;
12935
12936 else
12937 return False;
12938 end if;
12939 end Is_Fully_Initialized_Variant;
12940
12941 ------------------------------------
12942 -- Is_Generic_Declaration_Or_Body --
12943 ------------------------------------
12944
12945 function Is_Generic_Declaration_Or_Body (Decl : Node_Id) return Boolean is
12946 Spec_Decl : Node_Id;
12947
12948 begin
12949 -- Package/subprogram body
12950
12951 if Nkind_In (Decl, N_Package_Body, N_Subprogram_Body)
12952 and then Present (Corresponding_Spec (Decl))
12953 then
12954 Spec_Decl := Unit_Declaration_Node (Corresponding_Spec (Decl));
12955
12956 -- Package/subprogram body stub
12957
12958 elsif Nkind_In (Decl, N_Package_Body_Stub, N_Subprogram_Body_Stub)
12959 and then Present (Corresponding_Spec_Of_Stub (Decl))
12960 then
12961 Spec_Decl :=
12962 Unit_Declaration_Node (Corresponding_Spec_Of_Stub (Decl));
12963
12964 -- All other cases
12965
12966 else
12967 Spec_Decl := Decl;
12968 end if;
12969
12970 -- Rather than inspecting the defining entity of the spec declaration,
12971 -- look at its Nkind. This takes care of the case where the analysis of
12972 -- a generic body modifies the Ekind of its spec to allow for recursive
12973 -- calls.
12974
12975 return
12976 Nkind_In (Spec_Decl, N_Generic_Package_Declaration,
12977 N_Generic_Subprogram_Declaration);
12978 end Is_Generic_Declaration_Or_Body;
12979
12980 ----------------------------
12981 -- Is_Inherited_Operation --
12982 ----------------------------
12983
12984 function Is_Inherited_Operation (E : Entity_Id) return Boolean is
12985 pragma Assert (Is_Overloadable (E));
12986 Kind : constant Node_Kind := Nkind (Parent (E));
12987 begin
12988 return Kind = N_Full_Type_Declaration
12989 or else Kind = N_Private_Extension_Declaration
12990 or else Kind = N_Subtype_Declaration
12991 or else (Ekind (E) = E_Enumeration_Literal
12992 and then Is_Derived_Type (Etype (E)));
12993 end Is_Inherited_Operation;
12994
12995 -------------------------------------
12996 -- Is_Inherited_Operation_For_Type --
12997 -------------------------------------
12998
12999 function Is_Inherited_Operation_For_Type
13000 (E : Entity_Id;
13001 Typ : Entity_Id) return Boolean
13002 is
13003 begin
13004 -- Check that the operation has been created by the type declaration
13005
13006 return Is_Inherited_Operation (E)
13007 and then Defining_Identifier (Parent (E)) = Typ;
13008 end Is_Inherited_Operation_For_Type;
13009
13010 -----------------
13011 -- Is_Iterator --
13012 -----------------
13013
13014 function Is_Iterator (Typ : Entity_Id) return Boolean is
13015 function Denotes_Iterator (Iter_Typ : Entity_Id) return Boolean;
13016 -- Determine whether type Iter_Typ is a predefined forward or reversible
13017 -- iterator.
13018
13019 ----------------------
13020 -- Denotes_Iterator --
13021 ----------------------
13022
13023 function Denotes_Iterator (Iter_Typ : Entity_Id) return Boolean is
13024 begin
13025 -- Check that the name matches, and that the ultimate ancestor is in
13026 -- a predefined unit, i.e the one that declares iterator interfaces.
13027
13028 return
13029 Nam_In (Chars (Iter_Typ), Name_Forward_Iterator,
13030 Name_Reversible_Iterator)
13031 and then Is_Predefined_File_Name
13032 (Unit_File_Name (Get_Source_Unit (Root_Type (Iter_Typ))));
13033 end Denotes_Iterator;
13034
13035 -- Local variables
13036
13037 Iface_Elmt : Elmt_Id;
13038 Ifaces : Elist_Id;
13039
13040 -- Start of processing for Is_Iterator
13041
13042 begin
13043 -- The type may be a subtype of a descendant of the proper instance of
13044 -- the predefined interface type, so we must use the root type of the
13045 -- given type. The same is done for Is_Reversible_Iterator.
13046
13047 if Is_Class_Wide_Type (Typ)
13048 and then Denotes_Iterator (Root_Type (Typ))
13049 then
13050 return True;
13051
13052 elsif not Is_Tagged_Type (Typ) or else not Is_Derived_Type (Typ) then
13053 return False;
13054
13055 elsif Present (Find_Value_Of_Aspect (Typ, Aspect_Iterable)) then
13056 return True;
13057
13058 else
13059 Collect_Interfaces (Typ, Ifaces);
13060
13061 Iface_Elmt := First_Elmt (Ifaces);
13062 while Present (Iface_Elmt) loop
13063 if Denotes_Iterator (Node (Iface_Elmt)) then
13064 return True;
13065 end if;
13066
13067 Next_Elmt (Iface_Elmt);
13068 end loop;
13069
13070 return False;
13071 end if;
13072 end Is_Iterator;
13073
13074 ----------------------------
13075 -- Is_Iterator_Over_Array --
13076 ----------------------------
13077
13078 function Is_Iterator_Over_Array (N : Node_Id) return Boolean is
13079 Container : constant Node_Id := Name (N);
13080 Container_Typ : constant Entity_Id := Base_Type (Etype (Container));
13081 begin
13082 return Is_Array_Type (Container_Typ);
13083 end Is_Iterator_Over_Array;
13084
13085 ------------
13086 -- Is_LHS --
13087 ------------
13088
13089 -- We seem to have a lot of overlapping functions that do similar things
13090 -- (testing for left hand sides or lvalues???).
13091
13092 function Is_LHS (N : Node_Id) return Is_LHS_Result is
13093 P : constant Node_Id := Parent (N);
13094
13095 begin
13096 -- Return True if we are the left hand side of an assignment statement
13097
13098 if Nkind (P) = N_Assignment_Statement then
13099 if Name (P) = N then
13100 return Yes;
13101 else
13102 return No;
13103 end if;
13104
13105 -- Case of prefix of indexed or selected component or slice
13106
13107 elsif Nkind_In (P, N_Indexed_Component, N_Selected_Component, N_Slice)
13108 and then N = Prefix (P)
13109 then
13110 -- Here we have the case where the parent P is N.Q or N(Q .. R).
13111 -- If P is an LHS, then N is also effectively an LHS, but there
13112 -- is an important exception. If N is of an access type, then
13113 -- what we really have is N.all.Q (or N.all(Q .. R)). In either
13114 -- case this makes N.all a left hand side but not N itself.
13115
13116 -- If we don't know the type yet, this is the case where we return
13117 -- Unknown, since the answer depends on the type which is unknown.
13118
13119 if No (Etype (N)) then
13120 return Unknown;
13121
13122 -- We have an Etype set, so we can check it
13123
13124 elsif Is_Access_Type (Etype (N)) then
13125 return No;
13126
13127 -- OK, not access type case, so just test whole expression
13128
13129 else
13130 return Is_LHS (P);
13131 end if;
13132
13133 -- All other cases are not left hand sides
13134
13135 else
13136 return No;
13137 end if;
13138 end Is_LHS;
13139
13140 -----------------------------
13141 -- Is_Library_Level_Entity --
13142 -----------------------------
13143
13144 function Is_Library_Level_Entity (E : Entity_Id) return Boolean is
13145 begin
13146 -- The following is a small optimization, and it also properly handles
13147 -- discriminals, which in task bodies might appear in expressions before
13148 -- the corresponding procedure has been created, and which therefore do
13149 -- not have an assigned scope.
13150
13151 if Is_Formal (E) then
13152 return False;
13153 end if;
13154
13155 -- Normal test is simply that the enclosing dynamic scope is Standard
13156
13157 return Enclosing_Dynamic_Scope (E) = Standard_Standard;
13158 end Is_Library_Level_Entity;
13159
13160 --------------------------------
13161 -- Is_Limited_Class_Wide_Type --
13162 --------------------------------
13163
13164 function Is_Limited_Class_Wide_Type (Typ : Entity_Id) return Boolean is
13165 begin
13166 return
13167 Is_Class_Wide_Type (Typ)
13168 and then (Is_Limited_Type (Typ) or else From_Limited_With (Typ));
13169 end Is_Limited_Class_Wide_Type;
13170
13171 ---------------------------------
13172 -- Is_Local_Variable_Reference --
13173 ---------------------------------
13174
13175 function Is_Local_Variable_Reference (Expr : Node_Id) return Boolean is
13176 begin
13177 if not Is_Entity_Name (Expr) then
13178 return False;
13179
13180 else
13181 declare
13182 Ent : constant Entity_Id := Entity (Expr);
13183 Sub : constant Entity_Id := Enclosing_Subprogram (Ent);
13184 begin
13185 if not Ekind_In (Ent, E_Variable, E_In_Out_Parameter) then
13186 return False;
13187 else
13188 return Present (Sub) and then Sub = Current_Subprogram;
13189 end if;
13190 end;
13191 end if;
13192 end Is_Local_Variable_Reference;
13193
13194 ---------------------------------
13195 -- Is_Nontrivial_DIC_Procedure --
13196 ---------------------------------
13197
13198 function Is_Nontrivial_DIC_Procedure (Id : Entity_Id) return Boolean is
13199 Body_Decl : Node_Id;
13200 Stmt : Node_Id;
13201
13202 begin
13203 if Ekind (Id) = E_Procedure and then Is_DIC_Procedure (Id) then
13204 Body_Decl :=
13205 Unit_Declaration_Node
13206 (Corresponding_Body (Unit_Declaration_Node (Id)));
13207
13208 -- The body of the Default_Initial_Condition procedure must contain
13209 -- at least one statement, otherwise the generation of the subprogram
13210 -- body failed.
13211
13212 pragma Assert (Present (Handled_Statement_Sequence (Body_Decl)));
13213
13214 -- To qualify as nontrivial, the first statement of the procedure
13215 -- must be a check in the form of an if statement. If the original
13216 -- Default_Initial_Condition expression was folded, then the first
13217 -- statement is not a check.
13218
13219 Stmt := First (Statements (Handled_Statement_Sequence (Body_Decl)));
13220
13221 return
13222 Nkind (Stmt) = N_If_Statement
13223 and then Nkind (Original_Node (Stmt)) = N_Pragma;
13224 end if;
13225
13226 return False;
13227 end Is_Nontrivial_DIC_Procedure;
13228
13229 -------------------------
13230 -- Is_Null_Record_Type --
13231 -------------------------
13232
13233 function Is_Null_Record_Type (T : Entity_Id) return Boolean is
13234 Decl : constant Node_Id := Parent (T);
13235 begin
13236 return Nkind (Decl) = N_Full_Type_Declaration
13237 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
13238 and then
13239 (No (Component_List (Type_Definition (Decl)))
13240 or else Null_Present (Component_List (Type_Definition (Decl))));
13241 end Is_Null_Record_Type;
13242
13243 -------------------------
13244 -- Is_Object_Reference --
13245 -------------------------
13246
13247 function Is_Object_Reference (N : Node_Id) return Boolean is
13248 function Is_Internally_Generated_Renaming (N : Node_Id) return Boolean;
13249 -- Determine whether N is the name of an internally-generated renaming
13250
13251 --------------------------------------
13252 -- Is_Internally_Generated_Renaming --
13253 --------------------------------------
13254
13255 function Is_Internally_Generated_Renaming (N : Node_Id) return Boolean is
13256 P : Node_Id;
13257
13258 begin
13259 P := N;
13260 while Present (P) loop
13261 if Nkind (P) = N_Object_Renaming_Declaration then
13262 return not Comes_From_Source (P);
13263 elsif Is_List_Member (P) then
13264 return False;
13265 end if;
13266
13267 P := Parent (P);
13268 end loop;
13269
13270 return False;
13271 end Is_Internally_Generated_Renaming;
13272
13273 -- Start of processing for Is_Object_Reference
13274
13275 begin
13276 if Is_Entity_Name (N) then
13277 return Present (Entity (N)) and then Is_Object (Entity (N));
13278
13279 else
13280 case Nkind (N) is
13281 when N_Indexed_Component | N_Slice =>
13282 return
13283 Is_Object_Reference (Prefix (N))
13284 or else Is_Access_Type (Etype (Prefix (N)));
13285
13286 -- In Ada 95, a function call is a constant object; a procedure
13287 -- call is not.
13288
13289 when N_Function_Call =>
13290 return Etype (N) /= Standard_Void_Type;
13291
13292 -- Attributes 'Input, 'Loop_Entry, 'Old, and 'Result produce
13293 -- objects.
13294
13295 when N_Attribute_Reference =>
13296 return
13297 Nam_In (Attribute_Name (N), Name_Input,
13298 Name_Loop_Entry,
13299 Name_Old,
13300 Name_Result);
13301
13302 when N_Selected_Component =>
13303 return
13304 Is_Object_Reference (Selector_Name (N))
13305 and then
13306 (Is_Object_Reference (Prefix (N))
13307 or else Is_Access_Type (Etype (Prefix (N))));
13308
13309 when N_Explicit_Dereference =>
13310 return True;
13311
13312 -- A view conversion of a tagged object is an object reference
13313
13314 when N_Type_Conversion =>
13315 return Is_Tagged_Type (Etype (Subtype_Mark (N)))
13316 and then Is_Tagged_Type (Etype (Expression (N)))
13317 and then Is_Object_Reference (Expression (N));
13318
13319 -- An unchecked type conversion is considered to be an object if
13320 -- the operand is an object (this construction arises only as a
13321 -- result of expansion activities).
13322
13323 when N_Unchecked_Type_Conversion =>
13324 return True;
13325
13326 -- Allow string literals to act as objects as long as they appear
13327 -- in internally-generated renamings. The expansion of iterators
13328 -- may generate such renamings when the range involves a string
13329 -- literal.
13330
13331 when N_String_Literal =>
13332 return Is_Internally_Generated_Renaming (Parent (N));
13333
13334 -- AI05-0003: In Ada 2012 a qualified expression is a name.
13335 -- This allows disambiguation of function calls and the use
13336 -- of aggregates in more contexts.
13337
13338 when N_Qualified_Expression =>
13339 if Ada_Version < Ada_2012 then
13340 return False;
13341 else
13342 return Is_Object_Reference (Expression (N))
13343 or else Nkind (Expression (N)) = N_Aggregate;
13344 end if;
13345
13346 when others =>
13347 return False;
13348 end case;
13349 end if;
13350 end Is_Object_Reference;
13351
13352 -----------------------------------
13353 -- Is_OK_Variable_For_Out_Formal --
13354 -----------------------------------
13355
13356 function Is_OK_Variable_For_Out_Formal (AV : Node_Id) return Boolean is
13357 begin
13358 Note_Possible_Modification (AV, Sure => True);
13359
13360 -- We must reject parenthesized variable names. Comes_From_Source is
13361 -- checked because there are currently cases where the compiler violates
13362 -- this rule (e.g. passing a task object to its controlled Initialize
13363 -- routine). This should be properly documented in sinfo???
13364
13365 if Paren_Count (AV) > 0 and then Comes_From_Source (AV) then
13366 return False;
13367
13368 -- A variable is always allowed
13369
13370 elsif Is_Variable (AV) then
13371 return True;
13372
13373 -- Generalized indexing operations are rewritten as explicit
13374 -- dereferences, and it is only during resolution that we can
13375 -- check whether the context requires an access_to_variable type.
13376
13377 elsif Nkind (AV) = N_Explicit_Dereference
13378 and then Ada_Version >= Ada_2012
13379 and then Nkind (Original_Node (AV)) = N_Indexed_Component
13380 and then Present (Etype (Original_Node (AV)))
13381 and then Has_Implicit_Dereference (Etype (Original_Node (AV)))
13382 then
13383 return not Is_Access_Constant (Etype (Prefix (AV)));
13384
13385 -- Unchecked conversions are allowed only if they come from the
13386 -- generated code, which sometimes uses unchecked conversions for out
13387 -- parameters in cases where code generation is unaffected. We tell
13388 -- source unchecked conversions by seeing if they are rewrites of
13389 -- an original Unchecked_Conversion function call, or of an explicit
13390 -- conversion of a function call or an aggregate (as may happen in the
13391 -- expansion of a packed array aggregate).
13392
13393 elsif Nkind (AV) = N_Unchecked_Type_Conversion then
13394 if Nkind_In (Original_Node (AV), N_Function_Call, N_Aggregate) then
13395 return False;
13396
13397 elsif Comes_From_Source (AV)
13398 and then Nkind (Original_Node (Expression (AV))) = N_Function_Call
13399 then
13400 return False;
13401
13402 elsif Nkind (Original_Node (AV)) = N_Type_Conversion then
13403 return Is_OK_Variable_For_Out_Formal (Expression (AV));
13404
13405 else
13406 return True;
13407 end if;
13408
13409 -- Normal type conversions are allowed if argument is a variable
13410
13411 elsif Nkind (AV) = N_Type_Conversion then
13412 if Is_Variable (Expression (AV))
13413 and then Paren_Count (Expression (AV)) = 0
13414 then
13415 Note_Possible_Modification (Expression (AV), Sure => True);
13416 return True;
13417
13418 -- We also allow a non-parenthesized expression that raises
13419 -- constraint error if it rewrites what used to be a variable
13420
13421 elsif Raises_Constraint_Error (Expression (AV))
13422 and then Paren_Count (Expression (AV)) = 0
13423 and then Is_Variable (Original_Node (Expression (AV)))
13424 then
13425 return True;
13426
13427 -- Type conversion of something other than a variable
13428
13429 else
13430 return False;
13431 end if;
13432
13433 -- If this node is rewritten, then test the original form, if that is
13434 -- OK, then we consider the rewritten node OK (for example, if the
13435 -- original node is a conversion, then Is_Variable will not be true
13436 -- but we still want to allow the conversion if it converts a variable).
13437
13438 elsif Original_Node (AV) /= AV then
13439
13440 -- In Ada 2012, the explicit dereference may be a rewritten call to a
13441 -- Reference function.
13442
13443 if Ada_Version >= Ada_2012
13444 and then Nkind (Original_Node (AV)) = N_Function_Call
13445 and then
13446 Has_Implicit_Dereference (Etype (Name (Original_Node (AV))))
13447 then
13448
13449 -- Check that this is not a constant reference.
13450
13451 return not Is_Access_Constant (Etype (Prefix (AV)));
13452
13453 elsif Has_Implicit_Dereference (Etype (Original_Node (AV))) then
13454 return
13455 not Is_Access_Constant (Etype
13456 (Get_Reference_Discriminant (Etype (Original_Node (AV)))));
13457
13458 else
13459 return Is_OK_Variable_For_Out_Formal (Original_Node (AV));
13460 end if;
13461
13462 -- All other non-variables are rejected
13463
13464 else
13465 return False;
13466 end if;
13467 end Is_OK_Variable_For_Out_Formal;
13468
13469 ----------------------------
13470 -- Is_OK_Volatile_Context --
13471 ----------------------------
13472
13473 function Is_OK_Volatile_Context
13474 (Context : Node_Id;
13475 Obj_Ref : Node_Id) return Boolean
13476 is
13477 function Is_Protected_Operation_Call (Nod : Node_Id) return Boolean;
13478 -- Determine whether an arbitrary node denotes a call to a protected
13479 -- entry, function, or procedure in prefixed form where the prefix is
13480 -- Obj_Ref.
13481
13482 function Within_Check (Nod : Node_Id) return Boolean;
13483 -- Determine whether an arbitrary node appears in a check node
13484
13485 function Within_Subprogram_Call (Nod : Node_Id) return Boolean;
13486 -- Determine whether an arbitrary node appears in an entry, function, or
13487 -- procedure call.
13488
13489 function Within_Volatile_Function (Id : Entity_Id) return Boolean;
13490 -- Determine whether an arbitrary entity appears in a volatile function
13491
13492 ---------------------------------
13493 -- Is_Protected_Operation_Call --
13494 ---------------------------------
13495
13496 function Is_Protected_Operation_Call (Nod : Node_Id) return Boolean is
13497 Pref : Node_Id;
13498 Subp : Node_Id;
13499
13500 begin
13501 -- A call to a protected operations retains its selected component
13502 -- form as opposed to other prefixed calls that are transformed in
13503 -- expanded names.
13504
13505 if Nkind (Nod) = N_Selected_Component then
13506 Pref := Prefix (Nod);
13507 Subp := Selector_Name (Nod);
13508
13509 return
13510 Pref = Obj_Ref
13511 and then Present (Etype (Pref))
13512 and then Is_Protected_Type (Etype (Pref))
13513 and then Is_Entity_Name (Subp)
13514 and then Present (Entity (Subp))
13515 and then Ekind_In (Entity (Subp), E_Entry,
13516 E_Entry_Family,
13517 E_Function,
13518 E_Procedure);
13519 else
13520 return False;
13521 end if;
13522 end Is_Protected_Operation_Call;
13523
13524 ------------------
13525 -- Within_Check --
13526 ------------------
13527
13528 function Within_Check (Nod : Node_Id) return Boolean is
13529 Par : Node_Id;
13530
13531 begin
13532 -- Climb the parent chain looking for a check node
13533
13534 Par := Nod;
13535 while Present (Par) loop
13536 if Nkind (Par) in N_Raise_xxx_Error then
13537 return True;
13538
13539 -- Prevent the search from going too far
13540
13541 elsif Is_Body_Or_Package_Declaration (Par) then
13542 exit;
13543 end if;
13544
13545 Par := Parent (Par);
13546 end loop;
13547
13548 return False;
13549 end Within_Check;
13550
13551 ----------------------------
13552 -- Within_Subprogram_Call --
13553 ----------------------------
13554
13555 function Within_Subprogram_Call (Nod : Node_Id) return Boolean is
13556 Par : Node_Id;
13557
13558 begin
13559 -- Climb the parent chain looking for a function or procedure call
13560
13561 Par := Nod;
13562 while Present (Par) loop
13563 if Nkind_In (Par, N_Entry_Call_Statement,
13564 N_Function_Call,
13565 N_Procedure_Call_Statement)
13566 then
13567 return True;
13568
13569 -- Prevent the search from going too far
13570
13571 elsif Is_Body_Or_Package_Declaration (Par) then
13572 exit;
13573 end if;
13574
13575 Par := Parent (Par);
13576 end loop;
13577
13578 return False;
13579 end Within_Subprogram_Call;
13580
13581 ------------------------------
13582 -- Within_Volatile_Function --
13583 ------------------------------
13584
13585 function Within_Volatile_Function (Id : Entity_Id) return Boolean is
13586 Func_Id : Entity_Id;
13587
13588 begin
13589 -- Traverse the scope stack looking for a [generic] function
13590
13591 Func_Id := Id;
13592 while Present (Func_Id) and then Func_Id /= Standard_Standard loop
13593 if Ekind_In (Func_Id, E_Function, E_Generic_Function) then
13594 return Is_Volatile_Function (Func_Id);
13595 end if;
13596
13597 Func_Id := Scope (Func_Id);
13598 end loop;
13599
13600 return False;
13601 end Within_Volatile_Function;
13602
13603 -- Local variables
13604
13605 Obj_Id : Entity_Id;
13606
13607 -- Start of processing for Is_OK_Volatile_Context
13608
13609 begin
13610 -- The volatile object appears on either side of an assignment
13611
13612 if Nkind (Context) = N_Assignment_Statement then
13613 return True;
13614
13615 -- The volatile object is part of the initialization expression of
13616 -- another object.
13617
13618 elsif Nkind (Context) = N_Object_Declaration
13619 and then Present (Expression (Context))
13620 and then Expression (Context) = Obj_Ref
13621 then
13622 Obj_Id := Defining_Entity (Context);
13623
13624 -- The volatile object acts as the initialization expression of an
13625 -- extended return statement. This is valid context as long as the
13626 -- function is volatile.
13627
13628 if Is_Return_Object (Obj_Id) then
13629 return Within_Volatile_Function (Obj_Id);
13630
13631 -- Otherwise this is a normal object initialization
13632
13633 else
13634 return True;
13635 end if;
13636
13637 -- The volatile object acts as the name of a renaming declaration
13638
13639 elsif Nkind (Context) = N_Object_Renaming_Declaration
13640 and then Name (Context) = Obj_Ref
13641 then
13642 return True;
13643
13644 -- The volatile object appears as an actual parameter in a call to an
13645 -- instance of Unchecked_Conversion whose result is renamed.
13646
13647 elsif Nkind (Context) = N_Function_Call
13648 and then Is_Entity_Name (Name (Context))
13649 and then Is_Unchecked_Conversion_Instance (Entity (Name (Context)))
13650 and then Nkind (Parent (Context)) = N_Object_Renaming_Declaration
13651 then
13652 return True;
13653
13654 -- The volatile object is actually the prefix in a protected entry,
13655 -- function, or procedure call.
13656
13657 elsif Is_Protected_Operation_Call (Context) then
13658 return True;
13659
13660 -- The volatile object appears as the expression of a simple return
13661 -- statement that applies to a volatile function.
13662
13663 elsif Nkind (Context) = N_Simple_Return_Statement
13664 and then Expression (Context) = Obj_Ref
13665 then
13666 return
13667 Within_Volatile_Function (Return_Statement_Entity (Context));
13668
13669 -- The volatile object appears as the prefix of a name occurring in a
13670 -- non-interfering context.
13671
13672 elsif Nkind_In (Context, N_Attribute_Reference,
13673 N_Explicit_Dereference,
13674 N_Indexed_Component,
13675 N_Selected_Component,
13676 N_Slice)
13677 and then Prefix (Context) = Obj_Ref
13678 and then Is_OK_Volatile_Context
13679 (Context => Parent (Context),
13680 Obj_Ref => Context)
13681 then
13682 return True;
13683
13684 -- The volatile object appears as the prefix of attributes Address,
13685 -- Alignment, Component_Size, First_Bit, Last_Bit, Position, Size,
13686 -- Storage_Size.
13687
13688 elsif Nkind (Context) = N_Attribute_Reference
13689 and then Prefix (Context) = Obj_Ref
13690 and then Nam_In (Attribute_Name (Context), Name_Address,
13691 Name_Alignment,
13692 Name_Component_Size,
13693 Name_First_Bit,
13694 Name_Last_Bit,
13695 Name_Position,
13696 Name_Size,
13697 Name_Storage_Size)
13698 then
13699 return True;
13700
13701 -- The volatile object appears as the expression of a type conversion
13702 -- occurring in a non-interfering context.
13703
13704 elsif Nkind_In (Context, N_Type_Conversion,
13705 N_Unchecked_Type_Conversion)
13706 and then Expression (Context) = Obj_Ref
13707 and then Is_OK_Volatile_Context
13708 (Context => Parent (Context),
13709 Obj_Ref => Context)
13710 then
13711 return True;
13712
13713 -- Allow references to volatile objects in various checks. This is not a
13714 -- direct SPARK 2014 requirement.
13715
13716 elsif Within_Check (Context) then
13717 return True;
13718
13719 -- Assume that references to effectively volatile objects that appear
13720 -- as actual parameters in a subprogram call are always legal. A full
13721 -- legality check is done when the actuals are resolved (see routine
13722 -- Resolve_Actuals).
13723
13724 elsif Within_Subprogram_Call (Context) then
13725 return True;
13726
13727 -- Otherwise the context is not suitable for an effectively volatile
13728 -- object.
13729
13730 else
13731 return False;
13732 end if;
13733 end Is_OK_Volatile_Context;
13734
13735 ------------------------------------
13736 -- Is_Package_Contract_Annotation --
13737 ------------------------------------
13738
13739 function Is_Package_Contract_Annotation (Item : Node_Id) return Boolean is
13740 Nam : Name_Id;
13741
13742 begin
13743 if Nkind (Item) = N_Aspect_Specification then
13744 Nam := Chars (Identifier (Item));
13745
13746 else pragma Assert (Nkind (Item) = N_Pragma);
13747 Nam := Pragma_Name (Item);
13748 end if;
13749
13750 return Nam = Name_Abstract_State
13751 or else Nam = Name_Initial_Condition
13752 or else Nam = Name_Initializes
13753 or else Nam = Name_Refined_State;
13754 end Is_Package_Contract_Annotation;
13755
13756 -----------------------------------
13757 -- Is_Partially_Initialized_Type --
13758 -----------------------------------
13759
13760 function Is_Partially_Initialized_Type
13761 (Typ : Entity_Id;
13762 Include_Implicit : Boolean := True) return Boolean
13763 is
13764 begin
13765 if Is_Scalar_Type (Typ) then
13766 return False;
13767
13768 elsif Is_Access_Type (Typ) then
13769 return Include_Implicit;
13770
13771 elsif Is_Array_Type (Typ) then
13772
13773 -- If component type is partially initialized, so is array type
13774
13775 if Is_Partially_Initialized_Type
13776 (Component_Type (Typ), Include_Implicit)
13777 then
13778 return True;
13779
13780 -- Otherwise we are only partially initialized if we are fully
13781 -- initialized (this is the empty array case, no point in us
13782 -- duplicating that code here).
13783
13784 else
13785 return Is_Fully_Initialized_Type (Typ);
13786 end if;
13787
13788 elsif Is_Record_Type (Typ) then
13789
13790 -- A discriminated type is always partially initialized if in
13791 -- all mode
13792
13793 if Has_Discriminants (Typ) and then Include_Implicit then
13794 return True;
13795
13796 -- A tagged type is always partially initialized
13797
13798 elsif Is_Tagged_Type (Typ) then
13799 return True;
13800
13801 -- Case of non-discriminated record
13802
13803 else
13804 declare
13805 Ent : Entity_Id;
13806
13807 Component_Present : Boolean := False;
13808 -- Set True if at least one component is present. If no
13809 -- components are present, then record type is fully
13810 -- initialized (another odd case, like the null array).
13811
13812 begin
13813 -- Loop through components
13814
13815 Ent := First_Entity (Typ);
13816 while Present (Ent) loop
13817 if Ekind (Ent) = E_Component then
13818 Component_Present := True;
13819
13820 -- If a component has an initialization expression then
13821 -- the enclosing record type is partially initialized
13822
13823 if Present (Parent (Ent))
13824 and then Present (Expression (Parent (Ent)))
13825 then
13826 return True;
13827
13828 -- If a component is of a type which is itself partially
13829 -- initialized, then the enclosing record type is also.
13830
13831 elsif Is_Partially_Initialized_Type
13832 (Etype (Ent), Include_Implicit)
13833 then
13834 return True;
13835 end if;
13836 end if;
13837
13838 Next_Entity (Ent);
13839 end loop;
13840
13841 -- No initialized components found. If we found any components
13842 -- they were all uninitialized so the result is false.
13843
13844 if Component_Present then
13845 return False;
13846
13847 -- But if we found no components, then all the components are
13848 -- initialized so we consider the type to be initialized.
13849
13850 else
13851 return True;
13852 end if;
13853 end;
13854 end if;
13855
13856 -- Concurrent types are always fully initialized
13857
13858 elsif Is_Concurrent_Type (Typ) then
13859 return True;
13860
13861 -- For a private type, go to underlying type. If there is no underlying
13862 -- type then just assume this partially initialized. Not clear if this
13863 -- can happen in a non-error case, but no harm in testing for this.
13864
13865 elsif Is_Private_Type (Typ) then
13866 declare
13867 U : constant Entity_Id := Underlying_Type (Typ);
13868 begin
13869 if No (U) then
13870 return True;
13871 else
13872 return Is_Partially_Initialized_Type (U, Include_Implicit);
13873 end if;
13874 end;
13875
13876 -- For any other type (are there any?) assume partially initialized
13877
13878 else
13879 return True;
13880 end if;
13881 end Is_Partially_Initialized_Type;
13882
13883 ------------------------------------
13884 -- Is_Potentially_Persistent_Type --
13885 ------------------------------------
13886
13887 function Is_Potentially_Persistent_Type (T : Entity_Id) return Boolean is
13888 Comp : Entity_Id;
13889 Indx : Node_Id;
13890
13891 begin
13892 -- For private type, test corresponding full type
13893
13894 if Is_Private_Type (T) then
13895 return Is_Potentially_Persistent_Type (Full_View (T));
13896
13897 -- Scalar types are potentially persistent
13898
13899 elsif Is_Scalar_Type (T) then
13900 return True;
13901
13902 -- Record type is potentially persistent if not tagged and the types of
13903 -- all it components are potentially persistent, and no component has
13904 -- an initialization expression.
13905
13906 elsif Is_Record_Type (T)
13907 and then not Is_Tagged_Type (T)
13908 and then not Is_Partially_Initialized_Type (T)
13909 then
13910 Comp := First_Component (T);
13911 while Present (Comp) loop
13912 if not Is_Potentially_Persistent_Type (Etype (Comp)) then
13913 return False;
13914 else
13915 Next_Entity (Comp);
13916 end if;
13917 end loop;
13918
13919 return True;
13920
13921 -- Array type is potentially persistent if its component type is
13922 -- potentially persistent and if all its constraints are static.
13923
13924 elsif Is_Array_Type (T) then
13925 if not Is_Potentially_Persistent_Type (Component_Type (T)) then
13926 return False;
13927 end if;
13928
13929 Indx := First_Index (T);
13930 while Present (Indx) loop
13931 if not Is_OK_Static_Subtype (Etype (Indx)) then
13932 return False;
13933 else
13934 Next_Index (Indx);
13935 end if;
13936 end loop;
13937
13938 return True;
13939
13940 -- All other types are not potentially persistent
13941
13942 else
13943 return False;
13944 end if;
13945 end Is_Potentially_Persistent_Type;
13946
13947 --------------------------------
13948 -- Is_Potentially_Unevaluated --
13949 --------------------------------
13950
13951 function Is_Potentially_Unevaluated (N : Node_Id) return Boolean is
13952 Par : Node_Id;
13953 Expr : Node_Id;
13954
13955 begin
13956 Expr := N;
13957 Par := Parent (N);
13958
13959 -- A postcondition whose expression is a short-circuit is broken down
13960 -- into individual aspects for better exception reporting. The original
13961 -- short-circuit expression is rewritten as the second operand, and an
13962 -- occurrence of 'Old in that operand is potentially unevaluated.
13963 -- See Sem_ch13.adb for details of this transformation.
13964
13965 if Nkind (Original_Node (Par)) = N_And_Then then
13966 return True;
13967 end if;
13968
13969 while not Nkind_In (Par, N_If_Expression,
13970 N_Case_Expression,
13971 N_And_Then,
13972 N_Or_Else,
13973 N_In,
13974 N_Not_In)
13975 loop
13976 Expr := Par;
13977 Par := Parent (Par);
13978
13979 -- If the context is not an expression, or if is the result of
13980 -- expansion of an enclosing construct (such as another attribute)
13981 -- the predicate does not apply.
13982
13983 if Nkind (Par) not in N_Subexpr
13984 or else not Comes_From_Source (Par)
13985 then
13986 return False;
13987 end if;
13988 end loop;
13989
13990 if Nkind (Par) = N_If_Expression then
13991 return Is_Elsif (Par) or else Expr /= First (Expressions (Par));
13992
13993 elsif Nkind (Par) = N_Case_Expression then
13994 return Expr /= Expression (Par);
13995
13996 elsif Nkind_In (Par, N_And_Then, N_Or_Else) then
13997 return Expr = Right_Opnd (Par);
13998
13999 elsif Nkind_In (Par, N_In, N_Not_In) then
14000 return Expr /= Left_Opnd (Par);
14001
14002 else
14003 return False;
14004 end if;
14005 end Is_Potentially_Unevaluated;
14006
14007 ---------------------------------
14008 -- Is_Protected_Self_Reference --
14009 ---------------------------------
14010
14011 function Is_Protected_Self_Reference (N : Node_Id) return Boolean is
14012
14013 function In_Access_Definition (N : Node_Id) return Boolean;
14014 -- Returns true if N belongs to an access definition
14015
14016 --------------------------
14017 -- In_Access_Definition --
14018 --------------------------
14019
14020 function In_Access_Definition (N : Node_Id) return Boolean is
14021 P : Node_Id;
14022
14023 begin
14024 P := Parent (N);
14025 while Present (P) loop
14026 if Nkind (P) = N_Access_Definition then
14027 return True;
14028 end if;
14029
14030 P := Parent (P);
14031 end loop;
14032
14033 return False;
14034 end In_Access_Definition;
14035
14036 -- Start of processing for Is_Protected_Self_Reference
14037
14038 begin
14039 -- Verify that prefix is analyzed and has the proper form. Note that
14040 -- the attributes Elab_Spec, Elab_Body, and Elab_Subp_Body, which also
14041 -- produce the address of an entity, do not analyze their prefix
14042 -- because they denote entities that are not necessarily visible.
14043 -- Neither of them can apply to a protected type.
14044
14045 return Ada_Version >= Ada_2005
14046 and then Is_Entity_Name (N)
14047 and then Present (Entity (N))
14048 and then Is_Protected_Type (Entity (N))
14049 and then In_Open_Scopes (Entity (N))
14050 and then not In_Access_Definition (N);
14051 end Is_Protected_Self_Reference;
14052
14053 -----------------------------
14054 -- Is_RCI_Pkg_Spec_Or_Body --
14055 -----------------------------
14056
14057 function Is_RCI_Pkg_Spec_Or_Body (Cunit : Node_Id) return Boolean is
14058
14059 function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean;
14060 -- Return True if the unit of Cunit is an RCI package declaration
14061
14062 ---------------------------
14063 -- Is_RCI_Pkg_Decl_Cunit --
14064 ---------------------------
14065
14066 function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean is
14067 The_Unit : constant Node_Id := Unit (Cunit);
14068
14069 begin
14070 if Nkind (The_Unit) /= N_Package_Declaration then
14071 return False;
14072 end if;
14073
14074 return Is_Remote_Call_Interface (Defining_Entity (The_Unit));
14075 end Is_RCI_Pkg_Decl_Cunit;
14076
14077 -- Start of processing for Is_RCI_Pkg_Spec_Or_Body
14078
14079 begin
14080 return Is_RCI_Pkg_Decl_Cunit (Cunit)
14081 or else
14082 (Nkind (Unit (Cunit)) = N_Package_Body
14083 and then Is_RCI_Pkg_Decl_Cunit (Library_Unit (Cunit)));
14084 end Is_RCI_Pkg_Spec_Or_Body;
14085
14086 -----------------------------------------
14087 -- Is_Remote_Access_To_Class_Wide_Type --
14088 -----------------------------------------
14089
14090 function Is_Remote_Access_To_Class_Wide_Type
14091 (E : Entity_Id) return Boolean
14092 is
14093 begin
14094 -- A remote access to class-wide type is a general access to object type
14095 -- declared in the visible part of a Remote_Types or Remote_Call_
14096 -- Interface unit.
14097
14098 return Ekind (E) = E_General_Access_Type
14099 and then (Is_Remote_Call_Interface (E) or else Is_Remote_Types (E));
14100 end Is_Remote_Access_To_Class_Wide_Type;
14101
14102 -----------------------------------------
14103 -- Is_Remote_Access_To_Subprogram_Type --
14104 -----------------------------------------
14105
14106 function Is_Remote_Access_To_Subprogram_Type
14107 (E : Entity_Id) return Boolean
14108 is
14109 begin
14110 return (Ekind (E) = E_Access_Subprogram_Type
14111 or else (Ekind (E) = E_Record_Type
14112 and then Present (Corresponding_Remote_Type (E))))
14113 and then (Is_Remote_Call_Interface (E) or else Is_Remote_Types (E));
14114 end Is_Remote_Access_To_Subprogram_Type;
14115
14116 --------------------
14117 -- Is_Remote_Call --
14118 --------------------
14119
14120 function Is_Remote_Call (N : Node_Id) return Boolean is
14121 begin
14122 if Nkind (N) not in N_Subprogram_Call then
14123
14124 -- An entry call cannot be remote
14125
14126 return False;
14127
14128 elsif Nkind (Name (N)) in N_Has_Entity
14129 and then Is_Remote_Call_Interface (Entity (Name (N)))
14130 then
14131 -- A subprogram declared in the spec of a RCI package is remote
14132
14133 return True;
14134
14135 elsif Nkind (Name (N)) = N_Explicit_Dereference
14136 and then Is_Remote_Access_To_Subprogram_Type
14137 (Etype (Prefix (Name (N))))
14138 then
14139 -- The dereference of a RAS is a remote call
14140
14141 return True;
14142
14143 elsif Present (Controlling_Argument (N))
14144 and then Is_Remote_Access_To_Class_Wide_Type
14145 (Etype (Controlling_Argument (N)))
14146 then
14147 -- Any primitive operation call with a controlling argument of
14148 -- a RACW type is a remote call.
14149
14150 return True;
14151 end if;
14152
14153 -- All other calls are local calls
14154
14155 return False;
14156 end Is_Remote_Call;
14157
14158 ----------------------
14159 -- Is_Renamed_Entry --
14160 ----------------------
14161
14162 function Is_Renamed_Entry (Proc_Nam : Entity_Id) return Boolean is
14163 Orig_Node : Node_Id := Empty;
14164 Subp_Decl : Node_Id := Parent (Parent (Proc_Nam));
14165
14166 function Is_Entry (Nam : Node_Id) return Boolean;
14167 -- Determine whether Nam is an entry. Traverse selectors if there are
14168 -- nested selected components.
14169
14170 --------------
14171 -- Is_Entry --
14172 --------------
14173
14174 function Is_Entry (Nam : Node_Id) return Boolean is
14175 begin
14176 if Nkind (Nam) = N_Selected_Component then
14177 return Is_Entry (Selector_Name (Nam));
14178 end if;
14179
14180 return Ekind (Entity (Nam)) = E_Entry;
14181 end Is_Entry;
14182
14183 -- Start of processing for Is_Renamed_Entry
14184
14185 begin
14186 if Present (Alias (Proc_Nam)) then
14187 Subp_Decl := Parent (Parent (Alias (Proc_Nam)));
14188 end if;
14189
14190 -- Look for a rewritten subprogram renaming declaration
14191
14192 if Nkind (Subp_Decl) = N_Subprogram_Declaration
14193 and then Present (Original_Node (Subp_Decl))
14194 then
14195 Orig_Node := Original_Node (Subp_Decl);
14196 end if;
14197
14198 -- The rewritten subprogram is actually an entry
14199
14200 if Present (Orig_Node)
14201 and then Nkind (Orig_Node) = N_Subprogram_Renaming_Declaration
14202 and then Is_Entry (Name (Orig_Node))
14203 then
14204 return True;
14205 end if;
14206
14207 return False;
14208 end Is_Renamed_Entry;
14209
14210 -----------------------------
14211 -- Is_Renaming_Declaration --
14212 -----------------------------
14213
14214 function Is_Renaming_Declaration (N : Node_Id) return Boolean is
14215 begin
14216 case Nkind (N) is
14217 when N_Exception_Renaming_Declaration |
14218 N_Generic_Function_Renaming_Declaration |
14219 N_Generic_Package_Renaming_Declaration |
14220 N_Generic_Procedure_Renaming_Declaration |
14221 N_Object_Renaming_Declaration |
14222 N_Package_Renaming_Declaration |
14223 N_Subprogram_Renaming_Declaration =>
14224 return True;
14225
14226 when others =>
14227 return False;
14228 end case;
14229 end Is_Renaming_Declaration;
14230
14231 ----------------------------
14232 -- Is_Reversible_Iterator --
14233 ----------------------------
14234
14235 function Is_Reversible_Iterator (Typ : Entity_Id) return Boolean is
14236 Ifaces_List : Elist_Id;
14237 Iface_Elmt : Elmt_Id;
14238 Iface : Entity_Id;
14239
14240 begin
14241 if Is_Class_Wide_Type (Typ)
14242 and then Chars (Root_Type (Typ)) = Name_Reversible_Iterator
14243 and then Is_Predefined_File_Name
14244 (Unit_File_Name (Get_Source_Unit (Root_Type (Typ))))
14245 then
14246 return True;
14247
14248 elsif not Is_Tagged_Type (Typ) or else not Is_Derived_Type (Typ) then
14249 return False;
14250
14251 else
14252 Collect_Interfaces (Typ, Ifaces_List);
14253
14254 Iface_Elmt := First_Elmt (Ifaces_List);
14255 while Present (Iface_Elmt) loop
14256 Iface := Node (Iface_Elmt);
14257 if Chars (Iface) = Name_Reversible_Iterator
14258 and then
14259 Is_Predefined_File_Name
14260 (Unit_File_Name (Get_Source_Unit (Iface)))
14261 then
14262 return True;
14263 end if;
14264
14265 Next_Elmt (Iface_Elmt);
14266 end loop;
14267 end if;
14268
14269 return False;
14270 end Is_Reversible_Iterator;
14271
14272 ----------------------
14273 -- Is_Selector_Name --
14274 ----------------------
14275
14276 function Is_Selector_Name (N : Node_Id) return Boolean is
14277 begin
14278 if not Is_List_Member (N) then
14279 declare
14280 P : constant Node_Id := Parent (N);
14281 begin
14282 return Nkind_In (P, N_Expanded_Name,
14283 N_Generic_Association,
14284 N_Parameter_Association,
14285 N_Selected_Component)
14286 and then Selector_Name (P) = N;
14287 end;
14288
14289 else
14290 declare
14291 L : constant List_Id := List_Containing (N);
14292 P : constant Node_Id := Parent (L);
14293 begin
14294 return (Nkind (P) = N_Discriminant_Association
14295 and then Selector_Names (P) = L)
14296 or else
14297 (Nkind (P) = N_Component_Association
14298 and then Choices (P) = L);
14299 end;
14300 end if;
14301 end Is_Selector_Name;
14302
14303 ---------------------------------
14304 -- Is_Single_Concurrent_Object --
14305 ---------------------------------
14306
14307 function Is_Single_Concurrent_Object (Id : Entity_Id) return Boolean is
14308 begin
14309 return
14310 Is_Single_Protected_Object (Id) or else Is_Single_Task_Object (Id);
14311 end Is_Single_Concurrent_Object;
14312
14313 -------------------------------
14314 -- Is_Single_Concurrent_Type --
14315 -------------------------------
14316
14317 function Is_Single_Concurrent_Type (Id : Entity_Id) return Boolean is
14318 begin
14319 return
14320 Ekind_In (Id, E_Protected_Type, E_Task_Type)
14321 and then Is_Single_Concurrent_Type_Declaration
14322 (Declaration_Node (Id));
14323 end Is_Single_Concurrent_Type;
14324
14325 -------------------------------------------
14326 -- Is_Single_Concurrent_Type_Declaration --
14327 -------------------------------------------
14328
14329 function Is_Single_Concurrent_Type_Declaration
14330 (N : Node_Id) return Boolean
14331 is
14332 begin
14333 return Nkind_In (Original_Node (N), N_Single_Protected_Declaration,
14334 N_Single_Task_Declaration);
14335 end Is_Single_Concurrent_Type_Declaration;
14336
14337 ---------------------------------------------
14338 -- Is_Single_Precision_Floating_Point_Type --
14339 ---------------------------------------------
14340
14341 function Is_Single_Precision_Floating_Point_Type
14342 (E : Entity_Id) return Boolean is
14343 begin
14344 return Is_Floating_Point_Type (E)
14345 and then Machine_Radix_Value (E) = Uint_2
14346 and then Machine_Mantissa_Value (E) = Uint_24
14347 and then Machine_Emax_Value (E) = Uint_2 ** Uint_7
14348 and then Machine_Emin_Value (E) = Uint_3 - (Uint_2 ** Uint_7);
14349 end Is_Single_Precision_Floating_Point_Type;
14350
14351 --------------------------------
14352 -- Is_Single_Protected_Object --
14353 --------------------------------
14354
14355 function Is_Single_Protected_Object (Id : Entity_Id) return Boolean is
14356 begin
14357 return
14358 Ekind (Id) = E_Variable
14359 and then Ekind (Etype (Id)) = E_Protected_Type
14360 and then Is_Single_Concurrent_Type (Etype (Id));
14361 end Is_Single_Protected_Object;
14362
14363 ---------------------------
14364 -- Is_Single_Task_Object --
14365 ---------------------------
14366
14367 function Is_Single_Task_Object (Id : Entity_Id) return Boolean is
14368 begin
14369 return
14370 Ekind (Id) = E_Variable
14371 and then Ekind (Etype (Id)) = E_Task_Type
14372 and then Is_Single_Concurrent_Type (Etype (Id));
14373 end Is_Single_Task_Object;
14374
14375 -------------------------------------
14376 -- Is_SPARK_05_Initialization_Expr --
14377 -------------------------------------
14378
14379 function Is_SPARK_05_Initialization_Expr (N : Node_Id) return Boolean is
14380 Is_Ok : Boolean;
14381 Expr : Node_Id;
14382 Comp_Assn : Node_Id;
14383 Orig_N : constant Node_Id := Original_Node (N);
14384
14385 begin
14386 Is_Ok := True;
14387
14388 if not Comes_From_Source (Orig_N) then
14389 goto Done;
14390 end if;
14391
14392 pragma Assert (Nkind (Orig_N) in N_Subexpr);
14393
14394 case Nkind (Orig_N) is
14395 when N_Character_Literal |
14396 N_Integer_Literal |
14397 N_Real_Literal |
14398 N_String_Literal =>
14399 null;
14400
14401 when N_Identifier |
14402 N_Expanded_Name =>
14403 if Is_Entity_Name (Orig_N)
14404 and then Present (Entity (Orig_N)) -- needed in some cases
14405 then
14406 case Ekind (Entity (Orig_N)) is
14407 when E_Constant |
14408 E_Enumeration_Literal |
14409 E_Named_Integer |
14410 E_Named_Real =>
14411 null;
14412 when others =>
14413 if Is_Type (Entity (Orig_N)) then
14414 null;
14415 else
14416 Is_Ok := False;
14417 end if;
14418 end case;
14419 end if;
14420
14421 when N_Qualified_Expression |
14422 N_Type_Conversion =>
14423 Is_Ok := Is_SPARK_05_Initialization_Expr (Expression (Orig_N));
14424
14425 when N_Unary_Op =>
14426 Is_Ok := Is_SPARK_05_Initialization_Expr (Right_Opnd (Orig_N));
14427
14428 when N_Binary_Op |
14429 N_Short_Circuit |
14430 N_Membership_Test =>
14431 Is_Ok := Is_SPARK_05_Initialization_Expr (Left_Opnd (Orig_N))
14432 and then
14433 Is_SPARK_05_Initialization_Expr (Right_Opnd (Orig_N));
14434
14435 when N_Aggregate |
14436 N_Extension_Aggregate =>
14437 if Nkind (Orig_N) = N_Extension_Aggregate then
14438 Is_Ok :=
14439 Is_SPARK_05_Initialization_Expr (Ancestor_Part (Orig_N));
14440 end if;
14441
14442 Expr := First (Expressions (Orig_N));
14443 while Present (Expr) loop
14444 if not Is_SPARK_05_Initialization_Expr (Expr) then
14445 Is_Ok := False;
14446 goto Done;
14447 end if;
14448
14449 Next (Expr);
14450 end loop;
14451
14452 Comp_Assn := First (Component_Associations (Orig_N));
14453 while Present (Comp_Assn) loop
14454 Expr := Expression (Comp_Assn);
14455
14456 -- Note: test for Present here needed for box assocation
14457
14458 if Present (Expr)
14459 and then not Is_SPARK_05_Initialization_Expr (Expr)
14460 then
14461 Is_Ok := False;
14462 goto Done;
14463 end if;
14464
14465 Next (Comp_Assn);
14466 end loop;
14467
14468 when N_Attribute_Reference =>
14469 if Nkind (Prefix (Orig_N)) in N_Subexpr then
14470 Is_Ok := Is_SPARK_05_Initialization_Expr (Prefix (Orig_N));
14471 end if;
14472
14473 Expr := First (Expressions (Orig_N));
14474 while Present (Expr) loop
14475 if not Is_SPARK_05_Initialization_Expr (Expr) then
14476 Is_Ok := False;
14477 goto Done;
14478 end if;
14479
14480 Next (Expr);
14481 end loop;
14482
14483 -- Selected components might be expanded named not yet resolved, so
14484 -- default on the safe side. (Eg on sparklex.ads)
14485
14486 when N_Selected_Component =>
14487 null;
14488
14489 when others =>
14490 Is_Ok := False;
14491 end case;
14492
14493 <<Done>>
14494 return Is_Ok;
14495 end Is_SPARK_05_Initialization_Expr;
14496
14497 ----------------------------------
14498 -- Is_SPARK_05_Object_Reference --
14499 ----------------------------------
14500
14501 function Is_SPARK_05_Object_Reference (N : Node_Id) return Boolean is
14502 begin
14503 if Is_Entity_Name (N) then
14504 return Present (Entity (N))
14505 and then
14506 (Ekind_In (Entity (N), E_Constant, E_Variable)
14507 or else Ekind (Entity (N)) in Formal_Kind);
14508
14509 else
14510 case Nkind (N) is
14511 when N_Selected_Component =>
14512 return Is_SPARK_05_Object_Reference (Prefix (N));
14513
14514 when others =>
14515 return False;
14516 end case;
14517 end if;
14518 end Is_SPARK_05_Object_Reference;
14519
14520 -----------------------------
14521 -- Is_Specific_Tagged_Type --
14522 -----------------------------
14523
14524 function Is_Specific_Tagged_Type (Typ : Entity_Id) return Boolean is
14525 Full_Typ : Entity_Id;
14526
14527 begin
14528 -- Handle private types
14529
14530 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
14531 Full_Typ := Full_View (Typ);
14532 else
14533 Full_Typ := Typ;
14534 end if;
14535
14536 -- A specific tagged type is a non-class-wide tagged type
14537
14538 return Is_Tagged_Type (Full_Typ) and not Is_Class_Wide_Type (Full_Typ);
14539 end Is_Specific_Tagged_Type;
14540
14541 ------------------
14542 -- Is_Statement --
14543 ------------------
14544
14545 function Is_Statement (N : Node_Id) return Boolean is
14546 begin
14547 return
14548 Nkind (N) in N_Statement_Other_Than_Procedure_Call
14549 or else Nkind (N) = N_Procedure_Call_Statement;
14550 end Is_Statement;
14551
14552 ---------------------------------------
14553 -- Is_Subprogram_Contract_Annotation --
14554 ---------------------------------------
14555
14556 function Is_Subprogram_Contract_Annotation
14557 (Item : Node_Id) return Boolean
14558 is
14559 Nam : Name_Id;
14560
14561 begin
14562 if Nkind (Item) = N_Aspect_Specification then
14563 Nam := Chars (Identifier (Item));
14564
14565 else pragma Assert (Nkind (Item) = N_Pragma);
14566 Nam := Pragma_Name (Item);
14567 end if;
14568
14569 return Nam = Name_Contract_Cases
14570 or else Nam = Name_Depends
14571 or else Nam = Name_Extensions_Visible
14572 or else Nam = Name_Global
14573 or else Nam = Name_Post
14574 or else Nam = Name_Post_Class
14575 or else Nam = Name_Postcondition
14576 or else Nam = Name_Pre
14577 or else Nam = Name_Pre_Class
14578 or else Nam = Name_Precondition
14579 or else Nam = Name_Refined_Depends
14580 or else Nam = Name_Refined_Global
14581 or else Nam = Name_Refined_Post
14582 or else Nam = Name_Test_Case;
14583 end Is_Subprogram_Contract_Annotation;
14584
14585 --------------------------------------------------
14586 -- Is_Subprogram_Stub_Without_Prior_Declaration --
14587 --------------------------------------------------
14588
14589 function Is_Subprogram_Stub_Without_Prior_Declaration
14590 (N : Node_Id) return Boolean
14591 is
14592 begin
14593 -- A subprogram stub without prior declaration serves as declaration for
14594 -- the actual subprogram body. As such, it has an attached defining
14595 -- entity of E_[Generic_]Function or E_[Generic_]Procedure.
14596
14597 return Nkind (N) = N_Subprogram_Body_Stub
14598 and then Ekind (Defining_Entity (N)) /= E_Subprogram_Body;
14599 end Is_Subprogram_Stub_Without_Prior_Declaration;
14600
14601 --------------------------
14602 -- Is_Suspension_Object --
14603 --------------------------
14604
14605 function Is_Suspension_Object (Id : Entity_Id) return Boolean is
14606 begin
14607 -- This approach does an exact name match rather than to rely on
14608 -- RTSfind. Routine Is_Effectively_Volatile is used by clients of the
14609 -- front end at point where all auxiliary tables are locked and any
14610 -- modifications to them are treated as violations. Do not tamper with
14611 -- the tables, instead examine the Chars fields of all the scopes of Id.
14612
14613 return
14614 Chars (Id) = Name_Suspension_Object
14615 and then Present (Scope (Id))
14616 and then Chars (Scope (Id)) = Name_Synchronous_Task_Control
14617 and then Present (Scope (Scope (Id)))
14618 and then Chars (Scope (Scope (Id))) = Name_Ada
14619 and then Present (Scope (Scope (Scope (Id))))
14620 and then Scope (Scope (Scope (Id))) = Standard_Standard;
14621 end Is_Suspension_Object;
14622
14623 ----------------------------
14624 -- Is_Synchronized_Object --
14625 ----------------------------
14626
14627 function Is_Synchronized_Object (Id : Entity_Id) return Boolean is
14628 Prag : Node_Id;
14629
14630 begin
14631 if Is_Object (Id) then
14632
14633 -- The object is synchronized if it is of a type that yields a
14634 -- synchronized object.
14635
14636 if Yields_Synchronized_Object (Etype (Id)) then
14637 return True;
14638
14639 -- The object is synchronized if it is atomic and Async_Writers is
14640 -- enabled.
14641
14642 elsif Is_Atomic (Id) and then Async_Writers_Enabled (Id) then
14643 return True;
14644
14645 -- A constant is a synchronized object by default
14646
14647 elsif Ekind (Id) = E_Constant then
14648 return True;
14649
14650 -- A variable is a synchronized object if it is subject to pragma
14651 -- Constant_After_Elaboration.
14652
14653 elsif Ekind (Id) = E_Variable then
14654 Prag := Get_Pragma (Id, Pragma_Constant_After_Elaboration);
14655
14656 return Present (Prag) and then Is_Enabled_Pragma (Prag);
14657 end if;
14658 end if;
14659
14660 -- Otherwise the input is not an object or it does not qualify as a
14661 -- synchronized object.
14662
14663 return False;
14664 end Is_Synchronized_Object;
14665
14666 ---------------------------------
14667 -- Is_Synchronized_Tagged_Type --
14668 ---------------------------------
14669
14670 function Is_Synchronized_Tagged_Type (E : Entity_Id) return Boolean is
14671 Kind : constant Entity_Kind := Ekind (Base_Type (E));
14672
14673 begin
14674 -- A task or protected type derived from an interface is a tagged type.
14675 -- Such a tagged type is called a synchronized tagged type, as are
14676 -- synchronized interfaces and private extensions whose declaration
14677 -- includes the reserved word synchronized.
14678
14679 return (Is_Tagged_Type (E)
14680 and then (Kind = E_Task_Type
14681 or else
14682 Kind = E_Protected_Type))
14683 or else
14684 (Is_Interface (E)
14685 and then Is_Synchronized_Interface (E))
14686 or else
14687 (Ekind (E) = E_Record_Type_With_Private
14688 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
14689 and then (Synchronized_Present (Parent (E))
14690 or else Is_Synchronized_Interface (Etype (E))));
14691 end Is_Synchronized_Tagged_Type;
14692
14693 -----------------
14694 -- Is_Transfer --
14695 -----------------
14696
14697 function Is_Transfer (N : Node_Id) return Boolean is
14698 Kind : constant Node_Kind := Nkind (N);
14699
14700 begin
14701 if Kind = N_Simple_Return_Statement
14702 or else
14703 Kind = N_Extended_Return_Statement
14704 or else
14705 Kind = N_Goto_Statement
14706 or else
14707 Kind = N_Raise_Statement
14708 or else
14709 Kind = N_Requeue_Statement
14710 then
14711 return True;
14712
14713 elsif (Kind = N_Exit_Statement or else Kind in N_Raise_xxx_Error)
14714 and then No (Condition (N))
14715 then
14716 return True;
14717
14718 elsif Kind = N_Procedure_Call_Statement
14719 and then Is_Entity_Name (Name (N))
14720 and then Present (Entity (Name (N)))
14721 and then No_Return (Entity (Name (N)))
14722 then
14723 return True;
14724
14725 elsif Nkind (Original_Node (N)) = N_Raise_Statement then
14726 return True;
14727
14728 else
14729 return False;
14730 end if;
14731 end Is_Transfer;
14732
14733 -------------
14734 -- Is_True --
14735 -------------
14736
14737 function Is_True (U : Uint) return Boolean is
14738 begin
14739 return (U /= 0);
14740 end Is_True;
14741
14742 --------------------------------------
14743 -- Is_Unchecked_Conversion_Instance --
14744 --------------------------------------
14745
14746 function Is_Unchecked_Conversion_Instance (Id : Entity_Id) return Boolean is
14747 Par : Node_Id;
14748
14749 begin
14750 -- Look for a function whose generic parent is the predefined intrinsic
14751 -- function Unchecked_Conversion, or for one that renames such an
14752 -- instance.
14753
14754 if Ekind (Id) = E_Function then
14755 Par := Parent (Id);
14756
14757 if Nkind (Par) = N_Function_Specification then
14758 Par := Generic_Parent (Par);
14759
14760 if Present (Par) then
14761 return
14762 Chars (Par) = Name_Unchecked_Conversion
14763 and then Is_Intrinsic_Subprogram (Par)
14764 and then Is_Predefined_File_Name
14765 (Unit_File_Name (Get_Source_Unit (Par)));
14766 else
14767 return
14768 Present (Alias (Id))
14769 and then Is_Unchecked_Conversion_Instance (Alias (Id));
14770 end if;
14771 end if;
14772 end if;
14773
14774 return False;
14775 end Is_Unchecked_Conversion_Instance;
14776
14777 -------------------------------
14778 -- Is_Universal_Numeric_Type --
14779 -------------------------------
14780
14781 function Is_Universal_Numeric_Type (T : Entity_Id) return Boolean is
14782 begin
14783 return T = Universal_Integer or else T = Universal_Real;
14784 end Is_Universal_Numeric_Type;
14785
14786 ----------------------------
14787 -- Is_Variable_Size_Array --
14788 ----------------------------
14789
14790 function Is_Variable_Size_Array (E : Entity_Id) return Boolean is
14791 Idx : Node_Id;
14792
14793 begin
14794 pragma Assert (Is_Array_Type (E));
14795
14796 -- Check if some index is initialized with a non-constant value
14797
14798 Idx := First_Index (E);
14799 while Present (Idx) loop
14800 if Nkind (Idx) = N_Range then
14801 if not Is_Constant_Bound (Low_Bound (Idx))
14802 or else not Is_Constant_Bound (High_Bound (Idx))
14803 then
14804 return True;
14805 end if;
14806 end if;
14807
14808 Idx := Next_Index (Idx);
14809 end loop;
14810
14811 return False;
14812 end Is_Variable_Size_Array;
14813
14814 -----------------------------
14815 -- Is_Variable_Size_Record --
14816 -----------------------------
14817
14818 function Is_Variable_Size_Record (E : Entity_Id) return Boolean is
14819 Comp : Entity_Id;
14820 Comp_Typ : Entity_Id;
14821
14822 begin
14823 pragma Assert (Is_Record_Type (E));
14824
14825 Comp := First_Entity (E);
14826 while Present (Comp) loop
14827 Comp_Typ := Etype (Comp);
14828
14829 -- Recursive call if the record type has discriminants
14830
14831 if Is_Record_Type (Comp_Typ)
14832 and then Has_Discriminants (Comp_Typ)
14833 and then Is_Variable_Size_Record (Comp_Typ)
14834 then
14835 return True;
14836
14837 elsif Is_Array_Type (Comp_Typ)
14838 and then Is_Variable_Size_Array (Comp_Typ)
14839 then
14840 return True;
14841 end if;
14842
14843 Next_Entity (Comp);
14844 end loop;
14845
14846 return False;
14847 end Is_Variable_Size_Record;
14848
14849 -----------------
14850 -- Is_Variable --
14851 -----------------
14852
14853 function Is_Variable
14854 (N : Node_Id;
14855 Use_Original_Node : Boolean := True) return Boolean
14856 is
14857 Orig_Node : Node_Id;
14858
14859 function In_Protected_Function (E : Entity_Id) return Boolean;
14860 -- Within a protected function, the private components of the enclosing
14861 -- protected type are constants. A function nested within a (protected)
14862 -- procedure is not itself protected. Within the body of a protected
14863 -- function the current instance of the protected type is a constant.
14864
14865 function Is_Variable_Prefix (P : Node_Id) return Boolean;
14866 -- Prefixes can involve implicit dereferences, in which case we must
14867 -- test for the case of a reference of a constant access type, which can
14868 -- can never be a variable.
14869
14870 ---------------------------
14871 -- In_Protected_Function --
14872 ---------------------------
14873
14874 function In_Protected_Function (E : Entity_Id) return Boolean is
14875 Prot : Entity_Id;
14876 S : Entity_Id;
14877
14878 begin
14879 -- E is the current instance of a type
14880
14881 if Is_Type (E) then
14882 Prot := E;
14883
14884 -- E is an object
14885
14886 else
14887 Prot := Scope (E);
14888 end if;
14889
14890 if not Is_Protected_Type (Prot) then
14891 return False;
14892
14893 else
14894 S := Current_Scope;
14895 while Present (S) and then S /= Prot loop
14896 if Ekind (S) = E_Function and then Scope (S) = Prot then
14897 return True;
14898 end if;
14899
14900 S := Scope (S);
14901 end loop;
14902
14903 return False;
14904 end if;
14905 end In_Protected_Function;
14906
14907 ------------------------
14908 -- Is_Variable_Prefix --
14909 ------------------------
14910
14911 function Is_Variable_Prefix (P : Node_Id) return Boolean is
14912 begin
14913 if Is_Access_Type (Etype (P)) then
14914 return not Is_Access_Constant (Root_Type (Etype (P)));
14915
14916 -- For the case of an indexed component whose prefix has a packed
14917 -- array type, the prefix has been rewritten into a type conversion.
14918 -- Determine variable-ness from the converted expression.
14919
14920 elsif Nkind (P) = N_Type_Conversion
14921 and then not Comes_From_Source (P)
14922 and then Is_Array_Type (Etype (P))
14923 and then Is_Packed (Etype (P))
14924 then
14925 return Is_Variable (Expression (P));
14926
14927 else
14928 return Is_Variable (P);
14929 end if;
14930 end Is_Variable_Prefix;
14931
14932 -- Start of processing for Is_Variable
14933
14934 begin
14935 -- Special check, allow x'Deref(expr) as a variable
14936
14937 if Nkind (N) = N_Attribute_Reference
14938 and then Attribute_Name (N) = Name_Deref
14939 then
14940 return True;
14941 end if;
14942
14943 -- Check if we perform the test on the original node since this may be a
14944 -- test of syntactic categories which must not be disturbed by whatever
14945 -- rewriting might have occurred. For example, an aggregate, which is
14946 -- certainly NOT a variable, could be turned into a variable by
14947 -- expansion.
14948
14949 if Use_Original_Node then
14950 Orig_Node := Original_Node (N);
14951 else
14952 Orig_Node := N;
14953 end if;
14954
14955 -- Definitely OK if Assignment_OK is set. Since this is something that
14956 -- only gets set for expanded nodes, the test is on N, not Orig_Node.
14957
14958 if Nkind (N) in N_Subexpr and then Assignment_OK (N) then
14959 return True;
14960
14961 -- Normally we go to the original node, but there is one exception where
14962 -- we use the rewritten node, namely when it is an explicit dereference.
14963 -- The generated code may rewrite a prefix which is an access type with
14964 -- an explicit dereference. The dereference is a variable, even though
14965 -- the original node may not be (since it could be a constant of the
14966 -- access type).
14967
14968 -- In Ada 2005 we have a further case to consider: the prefix may be a
14969 -- function call given in prefix notation. The original node appears to
14970 -- be a selected component, but we need to examine the call.
14971
14972 elsif Nkind (N) = N_Explicit_Dereference
14973 and then Nkind (Orig_Node) /= N_Explicit_Dereference
14974 and then Present (Etype (Orig_Node))
14975 and then Is_Access_Type (Etype (Orig_Node))
14976 then
14977 -- Note that if the prefix is an explicit dereference that does not
14978 -- come from source, we must check for a rewritten function call in
14979 -- prefixed notation before other forms of rewriting, to prevent a
14980 -- compiler crash.
14981
14982 return
14983 (Nkind (Orig_Node) = N_Function_Call
14984 and then not Is_Access_Constant (Etype (Prefix (N))))
14985 or else
14986 Is_Variable_Prefix (Original_Node (Prefix (N)));
14987
14988 -- in Ada 2012, the dereference may have been added for a type with
14989 -- a declared implicit dereference aspect. Check that it is not an
14990 -- access to constant.
14991
14992 elsif Nkind (N) = N_Explicit_Dereference
14993 and then Present (Etype (Orig_Node))
14994 and then Ada_Version >= Ada_2012
14995 and then Has_Implicit_Dereference (Etype (Orig_Node))
14996 then
14997 return not Is_Access_Constant (Etype (Prefix (N)));
14998
14999 -- A function call is never a variable
15000
15001 elsif Nkind (N) = N_Function_Call then
15002 return False;
15003
15004 -- All remaining checks use the original node
15005
15006 elsif Is_Entity_Name (Orig_Node)
15007 and then Present (Entity (Orig_Node))
15008 then
15009 declare
15010 E : constant Entity_Id := Entity (Orig_Node);
15011 K : constant Entity_Kind := Ekind (E);
15012
15013 begin
15014 return (K = E_Variable
15015 and then Nkind (Parent (E)) /= N_Exception_Handler)
15016 or else (K = E_Component
15017 and then not In_Protected_Function (E))
15018 or else K = E_Out_Parameter
15019 or else K = E_In_Out_Parameter
15020 or else K = E_Generic_In_Out_Parameter
15021
15022 -- Current instance of type. If this is a protected type, check
15023 -- we are not within the body of one of its protected functions.
15024
15025 or else (Is_Type (E)
15026 and then In_Open_Scopes (E)
15027 and then not In_Protected_Function (E))
15028
15029 or else (Is_Incomplete_Or_Private_Type (E)
15030 and then In_Open_Scopes (Full_View (E)));
15031 end;
15032
15033 else
15034 case Nkind (Orig_Node) is
15035 when N_Indexed_Component | N_Slice =>
15036 return Is_Variable_Prefix (Prefix (Orig_Node));
15037
15038 when N_Selected_Component =>
15039 return (Is_Variable (Selector_Name (Orig_Node))
15040 and then Is_Variable_Prefix (Prefix (Orig_Node)))
15041 or else
15042 (Nkind (N) = N_Expanded_Name
15043 and then Scope (Entity (N)) = Entity (Prefix (N)));
15044
15045 -- For an explicit dereference, the type of the prefix cannot
15046 -- be an access to constant or an access to subprogram.
15047
15048 when N_Explicit_Dereference =>
15049 declare
15050 Typ : constant Entity_Id := Etype (Prefix (Orig_Node));
15051 begin
15052 return Is_Access_Type (Typ)
15053 and then not Is_Access_Constant (Root_Type (Typ))
15054 and then Ekind (Typ) /= E_Access_Subprogram_Type;
15055 end;
15056
15057 -- The type conversion is the case where we do not deal with the
15058 -- context dependent special case of an actual parameter. Thus
15059 -- the type conversion is only considered a variable for the
15060 -- purposes of this routine if the target type is tagged. However,
15061 -- a type conversion is considered to be a variable if it does not
15062 -- come from source (this deals for example with the conversions
15063 -- of expressions to their actual subtypes).
15064
15065 when N_Type_Conversion =>
15066 return Is_Variable (Expression (Orig_Node))
15067 and then
15068 (not Comes_From_Source (Orig_Node)
15069 or else
15070 (Is_Tagged_Type (Etype (Subtype_Mark (Orig_Node)))
15071 and then
15072 Is_Tagged_Type (Etype (Expression (Orig_Node)))));
15073
15074 -- GNAT allows an unchecked type conversion as a variable. This
15075 -- only affects the generation of internal expanded code, since
15076 -- calls to instantiations of Unchecked_Conversion are never
15077 -- considered variables (since they are function calls).
15078
15079 when N_Unchecked_Type_Conversion =>
15080 return Is_Variable (Expression (Orig_Node));
15081
15082 when others =>
15083 return False;
15084 end case;
15085 end if;
15086 end Is_Variable;
15087
15088 ------------------------------
15089 -- Is_Verifiable_DIC_Pragma --
15090 ------------------------------
15091
15092 function Is_Verifiable_DIC_Pragma (Prag : Node_Id) return Boolean is
15093 Args : constant List_Id := Pragma_Argument_Associations (Prag);
15094
15095 begin
15096 -- To qualify as verifiable, a DIC pragma must have a non-null argument
15097
15098 return
15099 Present (Args)
15100 and then Nkind (Get_Pragma_Arg (First (Args))) /= N_Null;
15101 end Is_Verifiable_DIC_Pragma;
15102
15103 ---------------------------
15104 -- Is_Visibly_Controlled --
15105 ---------------------------
15106
15107 function Is_Visibly_Controlled (T : Entity_Id) return Boolean is
15108 Root : constant Entity_Id := Root_Type (T);
15109 begin
15110 return Chars (Scope (Root)) = Name_Finalization
15111 and then Chars (Scope (Scope (Root))) = Name_Ada
15112 and then Scope (Scope (Scope (Root))) = Standard_Standard;
15113 end Is_Visibly_Controlled;
15114
15115 --------------------------
15116 -- Is_Volatile_Function --
15117 --------------------------
15118
15119 function Is_Volatile_Function (Func_Id : Entity_Id) return Boolean is
15120 begin
15121 pragma Assert (Ekind_In (Func_Id, E_Function, E_Generic_Function));
15122
15123 -- A function declared within a protected type is volatile
15124
15125 if Is_Protected_Type (Scope (Func_Id)) then
15126 return True;
15127
15128 -- An instance of Ada.Unchecked_Conversion is a volatile function if
15129 -- either the source or the target are effectively volatile.
15130
15131 elsif Is_Unchecked_Conversion_Instance (Func_Id)
15132 and then Has_Effectively_Volatile_Profile (Func_Id)
15133 then
15134 return True;
15135
15136 -- Otherwise the function is treated as volatile if it is subject to
15137 -- enabled pragma Volatile_Function.
15138
15139 else
15140 return
15141 Is_Enabled_Pragma (Get_Pragma (Func_Id, Pragma_Volatile_Function));
15142 end if;
15143 end Is_Volatile_Function;
15144
15145 ------------------------
15146 -- Is_Volatile_Object --
15147 ------------------------
15148
15149 function Is_Volatile_Object (N : Node_Id) return Boolean is
15150
15151 function Is_Volatile_Prefix (N : Node_Id) return Boolean;
15152 -- If prefix is an implicit dereference, examine designated type
15153
15154 function Object_Has_Volatile_Components (N : Node_Id) return Boolean;
15155 -- Determines if given object has volatile components
15156
15157 ------------------------
15158 -- Is_Volatile_Prefix --
15159 ------------------------
15160
15161 function Is_Volatile_Prefix (N : Node_Id) return Boolean is
15162 Typ : constant Entity_Id := Etype (N);
15163
15164 begin
15165 if Is_Access_Type (Typ) then
15166 declare
15167 Dtyp : constant Entity_Id := Designated_Type (Typ);
15168
15169 begin
15170 return Is_Volatile (Dtyp)
15171 or else Has_Volatile_Components (Dtyp);
15172 end;
15173
15174 else
15175 return Object_Has_Volatile_Components (N);
15176 end if;
15177 end Is_Volatile_Prefix;
15178
15179 ------------------------------------
15180 -- Object_Has_Volatile_Components --
15181 ------------------------------------
15182
15183 function Object_Has_Volatile_Components (N : Node_Id) return Boolean is
15184 Typ : constant Entity_Id := Etype (N);
15185
15186 begin
15187 if Is_Volatile (Typ)
15188 or else Has_Volatile_Components (Typ)
15189 then
15190 return True;
15191
15192 elsif Is_Entity_Name (N)
15193 and then (Has_Volatile_Components (Entity (N))
15194 or else Is_Volatile (Entity (N)))
15195 then
15196 return True;
15197
15198 elsif Nkind (N) = N_Indexed_Component
15199 or else Nkind (N) = N_Selected_Component
15200 then
15201 return Is_Volatile_Prefix (Prefix (N));
15202
15203 else
15204 return False;
15205 end if;
15206 end Object_Has_Volatile_Components;
15207
15208 -- Start of processing for Is_Volatile_Object
15209
15210 begin
15211 if Nkind (N) = N_Defining_Identifier then
15212 return Is_Volatile (N) or else Is_Volatile (Etype (N));
15213
15214 elsif Nkind (N) = N_Expanded_Name then
15215 return Is_Volatile_Object (Entity (N));
15216
15217 elsif Is_Volatile (Etype (N))
15218 or else (Is_Entity_Name (N) and then Is_Volatile (Entity (N)))
15219 then
15220 return True;
15221
15222 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component)
15223 and then Is_Volatile_Prefix (Prefix (N))
15224 then
15225 return True;
15226
15227 elsif Nkind (N) = N_Selected_Component
15228 and then Is_Volatile (Entity (Selector_Name (N)))
15229 then
15230 return True;
15231
15232 else
15233 return False;
15234 end if;
15235 end Is_Volatile_Object;
15236
15237 ---------------------------
15238 -- Itype_Has_Declaration --
15239 ---------------------------
15240
15241 function Itype_Has_Declaration (Id : Entity_Id) return Boolean is
15242 begin
15243 pragma Assert (Is_Itype (Id));
15244 return Present (Parent (Id))
15245 and then Nkind_In (Parent (Id), N_Full_Type_Declaration,
15246 N_Subtype_Declaration)
15247 and then Defining_Entity (Parent (Id)) = Id;
15248 end Itype_Has_Declaration;
15249
15250 -------------------------
15251 -- Kill_Current_Values --
15252 -------------------------
15253
15254 procedure Kill_Current_Values
15255 (Ent : Entity_Id;
15256 Last_Assignment_Only : Boolean := False)
15257 is
15258 begin
15259 if Is_Assignable (Ent) then
15260 Set_Last_Assignment (Ent, Empty);
15261 end if;
15262
15263 if Is_Object (Ent) then
15264 if not Last_Assignment_Only then
15265 Kill_Checks (Ent);
15266 Set_Current_Value (Ent, Empty);
15267
15268 -- Do not reset the Is_Known_[Non_]Null and Is_Known_Valid flags
15269 -- for a constant. Once the constant is elaborated, its value is
15270 -- not changed, therefore the associated flags that describe the
15271 -- value should not be modified either.
15272
15273 if Ekind (Ent) = E_Constant then
15274 null;
15275
15276 -- Non-constant entities
15277
15278 else
15279 if not Can_Never_Be_Null (Ent) then
15280 Set_Is_Known_Non_Null (Ent, False);
15281 end if;
15282
15283 Set_Is_Known_Null (Ent, False);
15284
15285 -- Reset the Is_Known_Valid flag unless the type is always
15286 -- valid. This does not apply to a loop parameter because its
15287 -- bounds are defined by the loop header and therefore always
15288 -- valid.
15289
15290 if not Is_Known_Valid (Etype (Ent))
15291 and then Ekind (Ent) /= E_Loop_Parameter
15292 then
15293 Set_Is_Known_Valid (Ent, False);
15294 end if;
15295 end if;
15296 end if;
15297 end if;
15298 end Kill_Current_Values;
15299
15300 procedure Kill_Current_Values (Last_Assignment_Only : Boolean := False) is
15301 S : Entity_Id;
15302
15303 procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id);
15304 -- Clear current value for entity E and all entities chained to E
15305
15306 ------------------------------------------
15307 -- Kill_Current_Values_For_Entity_Chain --
15308 ------------------------------------------
15309
15310 procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id) is
15311 Ent : Entity_Id;
15312 begin
15313 Ent := E;
15314 while Present (Ent) loop
15315 Kill_Current_Values (Ent, Last_Assignment_Only);
15316 Next_Entity (Ent);
15317 end loop;
15318 end Kill_Current_Values_For_Entity_Chain;
15319
15320 -- Start of processing for Kill_Current_Values
15321
15322 begin
15323 -- Kill all saved checks, a special case of killing saved values
15324
15325 if not Last_Assignment_Only then
15326 Kill_All_Checks;
15327 end if;
15328
15329 -- Loop through relevant scopes, which includes the current scope and
15330 -- any parent scopes if the current scope is a block or a package.
15331
15332 S := Current_Scope;
15333 Scope_Loop : loop
15334
15335 -- Clear current values of all entities in current scope
15336
15337 Kill_Current_Values_For_Entity_Chain (First_Entity (S));
15338
15339 -- If scope is a package, also clear current values of all private
15340 -- entities in the scope.
15341
15342 if Is_Package_Or_Generic_Package (S)
15343 or else Is_Concurrent_Type (S)
15344 then
15345 Kill_Current_Values_For_Entity_Chain (First_Private_Entity (S));
15346 end if;
15347
15348 -- If this is a not a subprogram, deal with parents
15349
15350 if not Is_Subprogram (S) then
15351 S := Scope (S);
15352 exit Scope_Loop when S = Standard_Standard;
15353 else
15354 exit Scope_Loop;
15355 end if;
15356 end loop Scope_Loop;
15357 end Kill_Current_Values;
15358
15359 --------------------------
15360 -- Kill_Size_Check_Code --
15361 --------------------------
15362
15363 procedure Kill_Size_Check_Code (E : Entity_Id) is
15364 begin
15365 if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
15366 and then Present (Size_Check_Code (E))
15367 then
15368 Remove (Size_Check_Code (E));
15369 Set_Size_Check_Code (E, Empty);
15370 end if;
15371 end Kill_Size_Check_Code;
15372
15373 --------------------------
15374 -- Known_To_Be_Assigned --
15375 --------------------------
15376
15377 function Known_To_Be_Assigned (N : Node_Id) return Boolean is
15378 P : constant Node_Id := Parent (N);
15379
15380 begin
15381 case Nkind (P) is
15382
15383 -- Test left side of assignment
15384
15385 when N_Assignment_Statement =>
15386 return N = Name (P);
15387
15388 -- Function call arguments are never lvalues
15389
15390 when N_Function_Call =>
15391 return False;
15392
15393 -- Positional parameter for procedure or accept call
15394
15395 when N_Procedure_Call_Statement |
15396 N_Accept_Statement
15397 =>
15398 declare
15399 Proc : Entity_Id;
15400 Form : Entity_Id;
15401 Act : Node_Id;
15402
15403 begin
15404 Proc := Get_Subprogram_Entity (P);
15405
15406 if No (Proc) then
15407 return False;
15408 end if;
15409
15410 -- If we are not a list member, something is strange, so
15411 -- be conservative and return False.
15412
15413 if not Is_List_Member (N) then
15414 return False;
15415 end if;
15416
15417 -- We are going to find the right formal by stepping forward
15418 -- through the formals, as we step backwards in the actuals.
15419
15420 Form := First_Formal (Proc);
15421 Act := N;
15422 loop
15423 -- If no formal, something is weird, so be conservative
15424 -- and return False.
15425
15426 if No (Form) then
15427 return False;
15428 end if;
15429
15430 Prev (Act);
15431 exit when No (Act);
15432 Next_Formal (Form);
15433 end loop;
15434
15435 return Ekind (Form) /= E_In_Parameter;
15436 end;
15437
15438 -- Named parameter for procedure or accept call
15439
15440 when N_Parameter_Association =>
15441 declare
15442 Proc : Entity_Id;
15443 Form : Entity_Id;
15444
15445 begin
15446 Proc := Get_Subprogram_Entity (Parent (P));
15447
15448 if No (Proc) then
15449 return False;
15450 end if;
15451
15452 -- Loop through formals to find the one that matches
15453
15454 Form := First_Formal (Proc);
15455 loop
15456 -- If no matching formal, that's peculiar, some kind of
15457 -- previous error, so return False to be conservative.
15458 -- Actually this also happens in legal code in the case
15459 -- where P is a parameter association for an Extra_Formal???
15460
15461 if No (Form) then
15462 return False;
15463 end if;
15464
15465 -- Else test for match
15466
15467 if Chars (Form) = Chars (Selector_Name (P)) then
15468 return Ekind (Form) /= E_In_Parameter;
15469 end if;
15470
15471 Next_Formal (Form);
15472 end loop;
15473 end;
15474
15475 -- Test for appearing in a conversion that itself appears
15476 -- in an lvalue context, since this should be an lvalue.
15477
15478 when N_Type_Conversion =>
15479 return Known_To_Be_Assigned (P);
15480
15481 -- All other references are definitely not known to be modifications
15482
15483 when others =>
15484 return False;
15485
15486 end case;
15487 end Known_To_Be_Assigned;
15488
15489 ---------------------------
15490 -- Last_Source_Statement --
15491 ---------------------------
15492
15493 function Last_Source_Statement (HSS : Node_Id) return Node_Id is
15494 N : Node_Id;
15495
15496 begin
15497 N := Last (Statements (HSS));
15498 while Present (N) loop
15499 exit when Comes_From_Source (N);
15500 Prev (N);
15501 end loop;
15502
15503 return N;
15504 end Last_Source_Statement;
15505
15506 ----------------------------------
15507 -- Matching_Static_Array_Bounds --
15508 ----------------------------------
15509
15510 function Matching_Static_Array_Bounds
15511 (L_Typ : Node_Id;
15512 R_Typ : Node_Id) return Boolean
15513 is
15514 L_Ndims : constant Nat := Number_Dimensions (L_Typ);
15515 R_Ndims : constant Nat := Number_Dimensions (R_Typ);
15516
15517 L_Index : Node_Id;
15518 R_Index : Node_Id;
15519 L_Low : Node_Id;
15520 L_High : Node_Id;
15521 L_Len : Uint;
15522 R_Low : Node_Id;
15523 R_High : Node_Id;
15524 R_Len : Uint;
15525
15526 begin
15527 if L_Ndims /= R_Ndims then
15528 return False;
15529 end if;
15530
15531 -- Unconstrained types do not have static bounds
15532
15533 if not Is_Constrained (L_Typ) or else not Is_Constrained (R_Typ) then
15534 return False;
15535 end if;
15536
15537 -- First treat specially the first dimension, as the lower bound and
15538 -- length of string literals are not stored like those of arrays.
15539
15540 if Ekind (L_Typ) = E_String_Literal_Subtype then
15541 L_Low := String_Literal_Low_Bound (L_Typ);
15542 L_Len := String_Literal_Length (L_Typ);
15543 else
15544 L_Index := First_Index (L_Typ);
15545 Get_Index_Bounds (L_Index, L_Low, L_High);
15546
15547 if Is_OK_Static_Expression (L_Low)
15548 and then
15549 Is_OK_Static_Expression (L_High)
15550 then
15551 if Expr_Value (L_High) < Expr_Value (L_Low) then
15552 L_Len := Uint_0;
15553 else
15554 L_Len := (Expr_Value (L_High) - Expr_Value (L_Low)) + 1;
15555 end if;
15556 else
15557 return False;
15558 end if;
15559 end if;
15560
15561 if Ekind (R_Typ) = E_String_Literal_Subtype then
15562 R_Low := String_Literal_Low_Bound (R_Typ);
15563 R_Len := String_Literal_Length (R_Typ);
15564 else
15565 R_Index := First_Index (R_Typ);
15566 Get_Index_Bounds (R_Index, R_Low, R_High);
15567
15568 if Is_OK_Static_Expression (R_Low)
15569 and then
15570 Is_OK_Static_Expression (R_High)
15571 then
15572 if Expr_Value (R_High) < Expr_Value (R_Low) then
15573 R_Len := Uint_0;
15574 else
15575 R_Len := (Expr_Value (R_High) - Expr_Value (R_Low)) + 1;
15576 end if;
15577 else
15578 return False;
15579 end if;
15580 end if;
15581
15582 if (Is_OK_Static_Expression (L_Low)
15583 and then
15584 Is_OK_Static_Expression (R_Low))
15585 and then Expr_Value (L_Low) = Expr_Value (R_Low)
15586 and then L_Len = R_Len
15587 then
15588 null;
15589 else
15590 return False;
15591 end if;
15592
15593 -- Then treat all other dimensions
15594
15595 for Indx in 2 .. L_Ndims loop
15596 Next (L_Index);
15597 Next (R_Index);
15598
15599 Get_Index_Bounds (L_Index, L_Low, L_High);
15600 Get_Index_Bounds (R_Index, R_Low, R_High);
15601
15602 if (Is_OK_Static_Expression (L_Low) and then
15603 Is_OK_Static_Expression (L_High) and then
15604 Is_OK_Static_Expression (R_Low) and then
15605 Is_OK_Static_Expression (R_High))
15606 and then (Expr_Value (L_Low) = Expr_Value (R_Low)
15607 and then
15608 Expr_Value (L_High) = Expr_Value (R_High))
15609 then
15610 null;
15611 else
15612 return False;
15613 end if;
15614 end loop;
15615
15616 -- If we fall through the loop, all indexes matched
15617
15618 return True;
15619 end Matching_Static_Array_Bounds;
15620
15621 -------------------
15622 -- May_Be_Lvalue --
15623 -------------------
15624
15625 function May_Be_Lvalue (N : Node_Id) return Boolean is
15626 P : constant Node_Id := Parent (N);
15627
15628 begin
15629 case Nkind (P) is
15630
15631 -- Test left side of assignment
15632
15633 when N_Assignment_Statement =>
15634 return N = Name (P);
15635
15636 -- Test prefix of component or attribute. Note that the prefix of an
15637 -- explicit or implicit dereference cannot be an l-value. In the case
15638 -- of a 'Read attribute, the reference can be an actual in the
15639 -- argument list of the attribute.
15640
15641 when N_Attribute_Reference =>
15642 return (N = Prefix (P)
15643 and then Name_Implies_Lvalue_Prefix (Attribute_Name (P)))
15644 or else
15645 Attribute_Name (P) = Name_Read;
15646
15647 -- For an expanded name, the name is an lvalue if the expanded name
15648 -- is an lvalue, but the prefix is never an lvalue, since it is just
15649 -- the scope where the name is found.
15650
15651 when N_Expanded_Name =>
15652 if N = Prefix (P) then
15653 return May_Be_Lvalue (P);
15654 else
15655 return False;
15656 end if;
15657
15658 -- For a selected component A.B, A is certainly an lvalue if A.B is.
15659 -- B is a little interesting, if we have A.B := 3, there is some
15660 -- discussion as to whether B is an lvalue or not, we choose to say
15661 -- it is. Note however that A is not an lvalue if it is of an access
15662 -- type since this is an implicit dereference.
15663
15664 when N_Selected_Component =>
15665 if N = Prefix (P)
15666 and then Present (Etype (N))
15667 and then Is_Access_Type (Etype (N))
15668 then
15669 return False;
15670 else
15671 return May_Be_Lvalue (P);
15672 end if;
15673
15674 -- For an indexed component or slice, the index or slice bounds is
15675 -- never an lvalue. The prefix is an lvalue if the indexed component
15676 -- or slice is an lvalue, except if it is an access type, where we
15677 -- have an implicit dereference.
15678
15679 when N_Indexed_Component | N_Slice =>
15680 if N /= Prefix (P)
15681 or else (Present (Etype (N)) and then Is_Access_Type (Etype (N)))
15682 then
15683 return False;
15684 else
15685 return May_Be_Lvalue (P);
15686 end if;
15687
15688 -- Prefix of a reference is an lvalue if the reference is an lvalue
15689
15690 when N_Reference =>
15691 return May_Be_Lvalue (P);
15692
15693 -- Prefix of explicit dereference is never an lvalue
15694
15695 when N_Explicit_Dereference =>
15696 return False;
15697
15698 -- Positional parameter for subprogram, entry, or accept call.
15699 -- In older versions of Ada function call arguments are never
15700 -- lvalues. In Ada 2012 functions can have in-out parameters.
15701
15702 when N_Subprogram_Call |
15703 N_Entry_Call_Statement |
15704 N_Accept_Statement
15705 =>
15706 if Nkind (P) = N_Function_Call and then Ada_Version < Ada_2012 then
15707 return False;
15708 end if;
15709
15710 -- The following mechanism is clumsy and fragile. A single flag
15711 -- set in Resolve_Actuals would be preferable ???
15712
15713 declare
15714 Proc : Entity_Id;
15715 Form : Entity_Id;
15716 Act : Node_Id;
15717
15718 begin
15719 Proc := Get_Subprogram_Entity (P);
15720
15721 if No (Proc) then
15722 return True;
15723 end if;
15724
15725 -- If we are not a list member, something is strange, so be
15726 -- conservative and return True.
15727
15728 if not Is_List_Member (N) then
15729 return True;
15730 end if;
15731
15732 -- We are going to find the right formal by stepping forward
15733 -- through the formals, as we step backwards in the actuals.
15734
15735 Form := First_Formal (Proc);
15736 Act := N;
15737 loop
15738 -- If no formal, something is weird, so be conservative and
15739 -- return True.
15740
15741 if No (Form) then
15742 return True;
15743 end if;
15744
15745 Prev (Act);
15746 exit when No (Act);
15747 Next_Formal (Form);
15748 end loop;
15749
15750 return Ekind (Form) /= E_In_Parameter;
15751 end;
15752
15753 -- Named parameter for procedure or accept call
15754
15755 when N_Parameter_Association =>
15756 declare
15757 Proc : Entity_Id;
15758 Form : Entity_Id;
15759
15760 begin
15761 Proc := Get_Subprogram_Entity (Parent (P));
15762
15763 if No (Proc) then
15764 return True;
15765 end if;
15766
15767 -- Loop through formals to find the one that matches
15768
15769 Form := First_Formal (Proc);
15770 loop
15771 -- If no matching formal, that's peculiar, some kind of
15772 -- previous error, so return True to be conservative.
15773 -- Actually happens with legal code for an unresolved call
15774 -- where we may get the wrong homonym???
15775
15776 if No (Form) then
15777 return True;
15778 end if;
15779
15780 -- Else test for match
15781
15782 if Chars (Form) = Chars (Selector_Name (P)) then
15783 return Ekind (Form) /= E_In_Parameter;
15784 end if;
15785
15786 Next_Formal (Form);
15787 end loop;
15788 end;
15789
15790 -- Test for appearing in a conversion that itself appears in an
15791 -- lvalue context, since this should be an lvalue.
15792
15793 when N_Type_Conversion =>
15794 return May_Be_Lvalue (P);
15795
15796 -- Test for appearance in object renaming declaration
15797
15798 when N_Object_Renaming_Declaration =>
15799 return True;
15800
15801 -- All other references are definitely not lvalues
15802
15803 when others =>
15804 return False;
15805
15806 end case;
15807 end May_Be_Lvalue;
15808
15809 -----------------------
15810 -- Mark_Coextensions --
15811 -----------------------
15812
15813 procedure Mark_Coextensions (Context_Nod : Node_Id; Root_Nod : Node_Id) is
15814 Is_Dynamic : Boolean;
15815 -- Indicates whether the context causes nested coextensions to be
15816 -- dynamic or static
15817
15818 function Mark_Allocator (N : Node_Id) return Traverse_Result;
15819 -- Recognize an allocator node and label it as a dynamic coextension
15820
15821 --------------------
15822 -- Mark_Allocator --
15823 --------------------
15824
15825 function Mark_Allocator (N : Node_Id) return Traverse_Result is
15826 begin
15827 if Nkind (N) = N_Allocator then
15828 if Is_Dynamic then
15829 Set_Is_Dynamic_Coextension (N);
15830
15831 -- If the allocator expression is potentially dynamic, it may
15832 -- be expanded out of order and require dynamic allocation
15833 -- anyway, so we treat the coextension itself as dynamic.
15834 -- Potential optimization ???
15835
15836 elsif Nkind (Expression (N)) = N_Qualified_Expression
15837 and then Nkind (Expression (Expression (N))) = N_Op_Concat
15838 then
15839 Set_Is_Dynamic_Coextension (N);
15840 else
15841 Set_Is_Static_Coextension (N);
15842 end if;
15843 end if;
15844
15845 return OK;
15846 end Mark_Allocator;
15847
15848 procedure Mark_Allocators is new Traverse_Proc (Mark_Allocator);
15849
15850 -- Start of processing for Mark_Coextensions
15851
15852 begin
15853 -- An allocator that appears on the right-hand side of an assignment is
15854 -- treated as a potentially dynamic coextension when the right-hand side
15855 -- is an allocator or a qualified expression.
15856
15857 -- Obj := new ...'(new Coextension ...);
15858
15859 if Nkind (Context_Nod) = N_Assignment_Statement then
15860 Is_Dynamic :=
15861 Nkind_In (Expression (Context_Nod), N_Allocator,
15862 N_Qualified_Expression);
15863
15864 -- An allocator that appears within the expression of a simple return
15865 -- statement is treated as a potentially dynamic coextension when the
15866 -- expression is either aggregate, allocator, or qualified expression.
15867
15868 -- return (new Coextension ...);
15869 -- return new ...'(new Coextension ...);
15870
15871 elsif Nkind (Context_Nod) = N_Simple_Return_Statement then
15872 Is_Dynamic :=
15873 Nkind_In (Expression (Context_Nod), N_Aggregate,
15874 N_Allocator,
15875 N_Qualified_Expression);
15876
15877 -- An alloctor that appears within the initialization expression of an
15878 -- object declaration is considered a potentially dynamic coextension
15879 -- when the initialization expression is an allocator or a qualified
15880 -- expression.
15881
15882 -- Obj : ... := new ...'(new Coextension ...);
15883
15884 -- A similar case arises when the object declaration is part of an
15885 -- extended return statement.
15886
15887 -- return Obj : ... := new ...'(new Coextension ...);
15888 -- return Obj : ... := (new Coextension ...);
15889
15890 elsif Nkind (Context_Nod) = N_Object_Declaration then
15891 Is_Dynamic :=
15892 Nkind_In (Root_Nod, N_Allocator, N_Qualified_Expression)
15893 or else
15894 Nkind (Parent (Context_Nod)) = N_Extended_Return_Statement;
15895
15896 -- This routine should not be called with constructs that cannot contain
15897 -- coextensions.
15898
15899 else
15900 raise Program_Error;
15901 end if;
15902
15903 Mark_Allocators (Root_Nod);
15904 end Mark_Coextensions;
15905
15906 ----------------------
15907 -- Needs_One_Actual --
15908 ----------------------
15909
15910 function Needs_One_Actual (E : Entity_Id) return Boolean is
15911 Formal : Entity_Id;
15912
15913 begin
15914 -- Ada 2005 or later, and formals present
15915
15916 if Ada_Version >= Ada_2005 and then Present (First_Formal (E)) then
15917 Formal := Next_Formal (First_Formal (E));
15918 while Present (Formal) loop
15919 if No (Default_Value (Formal)) then
15920 return False;
15921 end if;
15922
15923 Next_Formal (Formal);
15924 end loop;
15925
15926 return True;
15927
15928 -- Ada 83/95 or no formals
15929
15930 else
15931 return False;
15932 end if;
15933 end Needs_One_Actual;
15934
15935 ------------------------
15936 -- New_Copy_List_Tree --
15937 ------------------------
15938
15939 function New_Copy_List_Tree (List : List_Id) return List_Id is
15940 NL : List_Id;
15941 E : Node_Id;
15942
15943 begin
15944 if List = No_List then
15945 return No_List;
15946
15947 else
15948 NL := New_List;
15949 E := First (List);
15950
15951 while Present (E) loop
15952 Append (New_Copy_Tree (E), NL);
15953 E := Next (E);
15954 end loop;
15955
15956 return NL;
15957 end if;
15958 end New_Copy_List_Tree;
15959
15960 --------------------------------------------------
15961 -- New_Copy_Tree Auxiliary Data and Subprograms --
15962 --------------------------------------------------
15963
15964 use Atree.Unchecked_Access;
15965 use Atree_Private_Part;
15966
15967 -- Our approach here requires a two pass traversal of the tree. The
15968 -- first pass visits all nodes that eventually will be copied looking
15969 -- for defining Itypes. If any defining Itypes are found, then they are
15970 -- copied, and an entry is added to the replacement map. In the second
15971 -- phase, the tree is copied, using the replacement map to replace any
15972 -- Itype references within the copied tree.
15973
15974 -- The following hash tables are used if the Map supplied has more
15975 -- than hash threshold entries to speed up access to the map. If
15976 -- there are fewer entries, then the map is searched sequentially
15977 -- (because setting up a hash table for only a few entries takes
15978 -- more time than it saves.
15979
15980 function New_Copy_Hash (E : Entity_Id) return NCT_Header_Num;
15981 -- Hash function used for hash operations
15982
15983 -------------------
15984 -- New_Copy_Hash --
15985 -------------------
15986
15987 function New_Copy_Hash (E : Entity_Id) return NCT_Header_Num is
15988 begin
15989 return Nat (E) mod (NCT_Header_Num'Last + 1);
15990 end New_Copy_Hash;
15991
15992 ---------------
15993 -- NCT_Assoc --
15994 ---------------
15995
15996 -- The hash table NCT_Assoc associates old entities in the table
15997 -- with their corresponding new entities (i.e. the pairs of entries
15998 -- presented in the original Map argument are Key-Element pairs).
15999
16000 package NCT_Assoc is new Simple_HTable (
16001 Header_Num => NCT_Header_Num,
16002 Element => Entity_Id,
16003 No_Element => Empty,
16004 Key => Entity_Id,
16005 Hash => New_Copy_Hash,
16006 Equal => Types."=");
16007
16008 ---------------------
16009 -- NCT_Itype_Assoc --
16010 ---------------------
16011
16012 -- The hash table NCT_Itype_Assoc contains entries only for those
16013 -- old nodes which have a non-empty Associated_Node_For_Itype set.
16014 -- The key is the associated node, and the element is the new node
16015 -- itself (NOT the associated node for the new node).
16016
16017 package NCT_Itype_Assoc is new Simple_HTable (
16018 Header_Num => NCT_Header_Num,
16019 Element => Entity_Id,
16020 No_Element => Empty,
16021 Key => Entity_Id,
16022 Hash => New_Copy_Hash,
16023 Equal => Types."=");
16024
16025 -------------------
16026 -- New_Copy_Tree --
16027 -------------------
16028
16029 function New_Copy_Tree
16030 (Source : Node_Id;
16031 Map : Elist_Id := No_Elist;
16032 New_Sloc : Source_Ptr := No_Location;
16033 New_Scope : Entity_Id := Empty) return Node_Id
16034 is
16035 Actual_Map : Elist_Id := Map;
16036 -- This is the actual map for the copy. It is initialized with the
16037 -- given elements, and then enlarged as required for Itypes that are
16038 -- copied during the first phase of the copy operation. The visit
16039 -- procedures add elements to this map as Itypes are encountered.
16040 -- The reason we cannot use Map directly, is that it may well be
16041 -- (and normally is) initialized to No_Elist, and if we have mapped
16042 -- entities, we have to reset it to point to a real Elist.
16043
16044 function Assoc (N : Node_Or_Entity_Id) return Node_Id;
16045 -- Called during second phase to map entities into their corresponding
16046 -- copies using Actual_Map. If the argument is not an entity, or is not
16047 -- in Actual_Map, then it is returned unchanged.
16048
16049 procedure Build_NCT_Hash_Tables;
16050 -- Builds hash tables (number of elements >= threshold value)
16051
16052 function Copy_Elist_With_Replacement
16053 (Old_Elist : Elist_Id) return Elist_Id;
16054 -- Called during second phase to copy element list doing replacements
16055
16056 procedure Copy_Itype_With_Replacement (New_Itype : Entity_Id);
16057 -- Called during the second phase to process a copied Itype. The actual
16058 -- copy happened during the first phase (so that we could make the entry
16059 -- in the mapping), but we still have to deal with the descendants of
16060 -- the copied Itype and copy them where necessary.
16061
16062 function Copy_List_With_Replacement (Old_List : List_Id) return List_Id;
16063 -- Called during second phase to copy list doing replacements
16064
16065 function Copy_Node_With_Replacement (Old_Node : Node_Id) return Node_Id;
16066 -- Called during second phase to copy node doing replacements
16067
16068 procedure Visit_Elist (E : Elist_Id);
16069 -- Called during first phase to visit all elements of an Elist
16070
16071 procedure Visit_Field (F : Union_Id; N : Node_Id);
16072 -- Visit a single field, recursing to call Visit_Node or Visit_List
16073 -- if the field is a syntactic descendant of the current node (i.e.
16074 -- its parent is Node N).
16075
16076 procedure Visit_Itype (Old_Itype : Entity_Id);
16077 -- Called during first phase to visit subsidiary fields of a defining
16078 -- Itype, and also create a copy and make an entry in the replacement
16079 -- map for the new copy.
16080
16081 procedure Visit_List (L : List_Id);
16082 -- Called during first phase to visit all elements of a List
16083
16084 procedure Visit_Node (N : Node_Or_Entity_Id);
16085 -- Called during first phase to visit a node and all its subtrees
16086
16087 -----------
16088 -- Assoc --
16089 -----------
16090
16091 function Assoc (N : Node_Or_Entity_Id) return Node_Id is
16092 E : Elmt_Id;
16093 Ent : Entity_Id;
16094
16095 begin
16096 if not Has_Extension (N) or else No (Actual_Map) then
16097 return N;
16098
16099 elsif NCT_Hash_Tables_Used then
16100 Ent := NCT_Assoc.Get (Entity_Id (N));
16101
16102 if Present (Ent) then
16103 return Ent;
16104 else
16105 return N;
16106 end if;
16107
16108 -- No hash table used, do serial search
16109
16110 else
16111 E := First_Elmt (Actual_Map);
16112 while Present (E) loop
16113 if Node (E) = N then
16114 return Node (Next_Elmt (E));
16115 else
16116 E := Next_Elmt (Next_Elmt (E));
16117 end if;
16118 end loop;
16119 end if;
16120
16121 return N;
16122 end Assoc;
16123
16124 ---------------------------
16125 -- Build_NCT_Hash_Tables --
16126 ---------------------------
16127
16128 procedure Build_NCT_Hash_Tables is
16129 Elmt : Elmt_Id;
16130 Ent : Entity_Id;
16131 begin
16132 if NCT_Hash_Table_Setup then
16133 NCT_Assoc.Reset;
16134 NCT_Itype_Assoc.Reset;
16135 end if;
16136
16137 Elmt := First_Elmt (Actual_Map);
16138 while Present (Elmt) loop
16139 Ent := Node (Elmt);
16140
16141 -- Get new entity, and associate old and new
16142
16143 Next_Elmt (Elmt);
16144 NCT_Assoc.Set (Ent, Node (Elmt));
16145
16146 if Is_Type (Ent) then
16147 declare
16148 Anode : constant Entity_Id :=
16149 Associated_Node_For_Itype (Ent);
16150
16151 begin
16152 if Present (Anode) then
16153
16154 -- Enter a link between the associated node of the
16155 -- old Itype and the new Itype, for updating later
16156 -- when node is copied.
16157
16158 NCT_Itype_Assoc.Set (Anode, Node (Elmt));
16159 end if;
16160 end;
16161 end if;
16162
16163 Next_Elmt (Elmt);
16164 end loop;
16165
16166 NCT_Hash_Tables_Used := True;
16167 NCT_Hash_Table_Setup := True;
16168 end Build_NCT_Hash_Tables;
16169
16170 ---------------------------------
16171 -- Copy_Elist_With_Replacement --
16172 ---------------------------------
16173
16174 function Copy_Elist_With_Replacement
16175 (Old_Elist : Elist_Id) return Elist_Id
16176 is
16177 M : Elmt_Id;
16178 New_Elist : Elist_Id;
16179
16180 begin
16181 if No (Old_Elist) then
16182 return No_Elist;
16183
16184 else
16185 New_Elist := New_Elmt_List;
16186
16187 M := First_Elmt (Old_Elist);
16188 while Present (M) loop
16189 Append_Elmt (Copy_Node_With_Replacement (Node (M)), New_Elist);
16190 Next_Elmt (M);
16191 end loop;
16192 end if;
16193
16194 return New_Elist;
16195 end Copy_Elist_With_Replacement;
16196
16197 ---------------------------------
16198 -- Copy_Itype_With_Replacement --
16199 ---------------------------------
16200
16201 -- This routine exactly parallels its phase one analog Visit_Itype,
16202
16203 procedure Copy_Itype_With_Replacement (New_Itype : Entity_Id) is
16204 begin
16205 -- Translate Next_Entity, Scope, and Etype fields, in case they
16206 -- reference entities that have been mapped into copies.
16207
16208 Set_Next_Entity (New_Itype, Assoc (Next_Entity (New_Itype)));
16209 Set_Etype (New_Itype, Assoc (Etype (New_Itype)));
16210
16211 if Present (New_Scope) then
16212 Set_Scope (New_Itype, New_Scope);
16213 else
16214 Set_Scope (New_Itype, Assoc (Scope (New_Itype)));
16215 end if;
16216
16217 -- Copy referenced fields
16218
16219 if Is_Discrete_Type (New_Itype) then
16220 Set_Scalar_Range (New_Itype,
16221 Copy_Node_With_Replacement (Scalar_Range (New_Itype)));
16222
16223 elsif Has_Discriminants (Base_Type (New_Itype)) then
16224 Set_Discriminant_Constraint (New_Itype,
16225 Copy_Elist_With_Replacement
16226 (Discriminant_Constraint (New_Itype)));
16227
16228 elsif Is_Array_Type (New_Itype) then
16229 if Present (First_Index (New_Itype)) then
16230 Set_First_Index (New_Itype,
16231 First (Copy_List_With_Replacement
16232 (List_Containing (First_Index (New_Itype)))));
16233 end if;
16234
16235 if Is_Packed (New_Itype) then
16236 Set_Packed_Array_Impl_Type (New_Itype,
16237 Copy_Node_With_Replacement
16238 (Packed_Array_Impl_Type (New_Itype)));
16239 end if;
16240 end if;
16241 end Copy_Itype_With_Replacement;
16242
16243 --------------------------------
16244 -- Copy_List_With_Replacement --
16245 --------------------------------
16246
16247 function Copy_List_With_Replacement
16248 (Old_List : List_Id) return List_Id
16249 is
16250 New_List : List_Id;
16251 E : Node_Id;
16252
16253 begin
16254 if Old_List = No_List then
16255 return No_List;
16256
16257 else
16258 New_List := Empty_List;
16259
16260 E := First (Old_List);
16261 while Present (E) loop
16262 Append (Copy_Node_With_Replacement (E), New_List);
16263 Next (E);
16264 end loop;
16265
16266 return New_List;
16267 end if;
16268 end Copy_List_With_Replacement;
16269
16270 --------------------------------
16271 -- Copy_Node_With_Replacement --
16272 --------------------------------
16273
16274 function Copy_Node_With_Replacement
16275 (Old_Node : Node_Id) return Node_Id
16276 is
16277 New_Node : Node_Id;
16278
16279 procedure Adjust_Named_Associations
16280 (Old_Node : Node_Id;
16281 New_Node : Node_Id);
16282 -- If a call node has named associations, these are chained through
16283 -- the First_Named_Actual, Next_Named_Actual links. These must be
16284 -- propagated separately to the new parameter list, because these
16285 -- are not syntactic fields.
16286
16287 function Copy_Field_With_Replacement
16288 (Field : Union_Id) return Union_Id;
16289 -- Given Field, which is a field of Old_Node, return a copy of it
16290 -- if it is a syntactic field (i.e. its parent is Node), setting
16291 -- the parent of the copy to poit to New_Node. Otherwise returns
16292 -- the field (possibly mapped if it is an entity).
16293
16294 -------------------------------
16295 -- Adjust_Named_Associations --
16296 -------------------------------
16297
16298 procedure Adjust_Named_Associations
16299 (Old_Node : Node_Id;
16300 New_Node : Node_Id)
16301 is
16302 Old_E : Node_Id;
16303 New_E : Node_Id;
16304
16305 Old_Next : Node_Id;
16306 New_Next : Node_Id;
16307
16308 begin
16309 Old_E := First (Parameter_Associations (Old_Node));
16310 New_E := First (Parameter_Associations (New_Node));
16311 while Present (Old_E) loop
16312 if Nkind (Old_E) = N_Parameter_Association
16313 and then Present (Next_Named_Actual (Old_E))
16314 then
16315 if First_Named_Actual (Old_Node)
16316 = Explicit_Actual_Parameter (Old_E)
16317 then
16318 Set_First_Named_Actual
16319 (New_Node, Explicit_Actual_Parameter (New_E));
16320 end if;
16321
16322 -- Now scan parameter list from the beginning,to locate
16323 -- next named actual, which can be out of order.
16324
16325 Old_Next := First (Parameter_Associations (Old_Node));
16326 New_Next := First (Parameter_Associations (New_Node));
16327
16328 while Nkind (Old_Next) /= N_Parameter_Association
16329 or else Explicit_Actual_Parameter (Old_Next) /=
16330 Next_Named_Actual (Old_E)
16331 loop
16332 Next (Old_Next);
16333 Next (New_Next);
16334 end loop;
16335
16336 Set_Next_Named_Actual
16337 (New_E, Explicit_Actual_Parameter (New_Next));
16338 end if;
16339
16340 Next (Old_E);
16341 Next (New_E);
16342 end loop;
16343 end Adjust_Named_Associations;
16344
16345 ---------------------------------
16346 -- Copy_Field_With_Replacement --
16347 ---------------------------------
16348
16349 function Copy_Field_With_Replacement
16350 (Field : Union_Id) return Union_Id
16351 is
16352 begin
16353 if Field = Union_Id (Empty) then
16354 return Field;
16355
16356 elsif Field in Node_Range then
16357 declare
16358 Old_N : constant Node_Id := Node_Id (Field);
16359 New_N : Node_Id;
16360
16361 begin
16362 -- If syntactic field, as indicated by the parent pointer
16363 -- being set, then copy the referenced node recursively.
16364
16365 if Parent (Old_N) = Old_Node then
16366 New_N := Copy_Node_With_Replacement (Old_N);
16367
16368 if New_N /= Old_N then
16369 Set_Parent (New_N, New_Node);
16370 end if;
16371
16372 -- For semantic fields, update possible entity reference
16373 -- from the replacement map.
16374
16375 else
16376 New_N := Assoc (Old_N);
16377 end if;
16378
16379 return Union_Id (New_N);
16380 end;
16381
16382 elsif Field in List_Range then
16383 declare
16384 Old_L : constant List_Id := List_Id (Field);
16385 New_L : List_Id;
16386
16387 begin
16388 -- If syntactic field, as indicated by the parent pointer,
16389 -- then recursively copy the entire referenced list.
16390
16391 if Parent (Old_L) = Old_Node then
16392 New_L := Copy_List_With_Replacement (Old_L);
16393 Set_Parent (New_L, New_Node);
16394
16395 -- For semantic list, just returned unchanged
16396
16397 else
16398 New_L := Old_L;
16399 end if;
16400
16401 return Union_Id (New_L);
16402 end;
16403
16404 -- Anything other than a list or a node is returned unchanged
16405
16406 else
16407 return Field;
16408 end if;
16409 end Copy_Field_With_Replacement;
16410
16411 -- Start of processing for Copy_Node_With_Replacement
16412
16413 begin
16414 if Old_Node <= Empty_Or_Error then
16415 return Old_Node;
16416
16417 elsif Has_Extension (Old_Node) then
16418 return Assoc (Old_Node);
16419
16420 else
16421 New_Node := New_Copy (Old_Node);
16422
16423 -- If the node we are copying is the associated node of a
16424 -- previously copied Itype, then adjust the associated node
16425 -- of the copy of that Itype accordingly.
16426
16427 if Present (Actual_Map) then
16428 declare
16429 E : Elmt_Id;
16430 Ent : Entity_Id;
16431
16432 begin
16433 -- Case of hash table used
16434
16435 if NCT_Hash_Tables_Used then
16436 Ent := NCT_Itype_Assoc.Get (Old_Node);
16437
16438 if Present (Ent) then
16439 Set_Associated_Node_For_Itype (Ent, New_Node);
16440 end if;
16441
16442 -- Case of no hash table used
16443
16444 else
16445 E := First_Elmt (Actual_Map);
16446 while Present (E) loop
16447 if Is_Itype (Node (E))
16448 and then
16449 Old_Node = Associated_Node_For_Itype (Node (E))
16450 then
16451 Set_Associated_Node_For_Itype
16452 (Node (Next_Elmt (E)), New_Node);
16453 end if;
16454
16455 E := Next_Elmt (Next_Elmt (E));
16456 end loop;
16457 end if;
16458 end;
16459 end if;
16460
16461 -- Recursively copy descendants
16462
16463 Set_Field1
16464 (New_Node, Copy_Field_With_Replacement (Field1 (New_Node)));
16465 Set_Field2
16466 (New_Node, Copy_Field_With_Replacement (Field2 (New_Node)));
16467 Set_Field3
16468 (New_Node, Copy_Field_With_Replacement (Field3 (New_Node)));
16469 Set_Field4
16470 (New_Node, Copy_Field_With_Replacement (Field4 (New_Node)));
16471 Set_Field5
16472 (New_Node, Copy_Field_With_Replacement (Field5 (New_Node)));
16473
16474 -- Adjust Sloc of new node if necessary
16475
16476 if New_Sloc /= No_Location then
16477 Set_Sloc (New_Node, New_Sloc);
16478
16479 -- If we adjust the Sloc, then we are essentially making a
16480 -- completely new node, so the Comes_From_Source flag should
16481 -- be reset to the proper default value.
16482
16483 Set_Comes_From_Source
16484 (New_Node, Default_Node.Comes_From_Source);
16485 end if;
16486
16487 -- If the node is a call and has named associations, set the
16488 -- corresponding links in the copy.
16489
16490 if Nkind_In (Old_Node, N_Entry_Call_Statement,
16491 N_Function_Call,
16492 N_Procedure_Call_Statement)
16493 and then Present (First_Named_Actual (Old_Node))
16494 then
16495 Adjust_Named_Associations (Old_Node, New_Node);
16496 end if;
16497
16498 -- Reset First_Real_Statement for Handled_Sequence_Of_Statements.
16499 -- The replacement mechanism applies to entities, and is not used
16500 -- here. Eventually we may need a more general graph-copying
16501 -- routine. For now, do a sequential search to find desired node.
16502
16503 if Nkind (Old_Node) = N_Handled_Sequence_Of_Statements
16504 and then Present (First_Real_Statement (Old_Node))
16505 then
16506 declare
16507 Old_F : constant Node_Id := First_Real_Statement (Old_Node);
16508 N1, N2 : Node_Id;
16509
16510 begin
16511 N1 := First (Statements (Old_Node));
16512 N2 := First (Statements (New_Node));
16513
16514 while N1 /= Old_F loop
16515 Next (N1);
16516 Next (N2);
16517 end loop;
16518
16519 Set_First_Real_Statement (New_Node, N2);
16520 end;
16521 end if;
16522 end if;
16523
16524 -- All done, return copied node
16525
16526 return New_Node;
16527 end Copy_Node_With_Replacement;
16528
16529 -----------------
16530 -- Visit_Elist --
16531 -----------------
16532
16533 procedure Visit_Elist (E : Elist_Id) is
16534 Elmt : Elmt_Id;
16535 begin
16536 if Present (E) then
16537 Elmt := First_Elmt (E);
16538
16539 while Elmt /= No_Elmt loop
16540 Visit_Node (Node (Elmt));
16541 Next_Elmt (Elmt);
16542 end loop;
16543 end if;
16544 end Visit_Elist;
16545
16546 -----------------
16547 -- Visit_Field --
16548 -----------------
16549
16550 procedure Visit_Field (F : Union_Id; N : Node_Id) is
16551 begin
16552 if F = Union_Id (Empty) then
16553 return;
16554
16555 elsif F in Node_Range then
16556
16557 -- Copy node if it is syntactic, i.e. its parent pointer is
16558 -- set to point to the field that referenced it (certain
16559 -- Itypes will also meet this criterion, which is fine, since
16560 -- these are clearly Itypes that do need to be copied, since
16561 -- we are copying their parent.)
16562
16563 if Parent (Node_Id (F)) = N then
16564 Visit_Node (Node_Id (F));
16565 return;
16566
16567 -- Another case, if we are pointing to an Itype, then we want
16568 -- to copy it if its associated node is somewhere in the tree
16569 -- being copied.
16570
16571 -- Note: the exclusion of self-referential copies is just an
16572 -- optimization, since the search of the already copied list
16573 -- would catch it, but it is a common case (Etype pointing
16574 -- to itself for an Itype that is a base type).
16575
16576 elsif Has_Extension (Node_Id (F))
16577 and then Is_Itype (Entity_Id (F))
16578 and then Node_Id (F) /= N
16579 then
16580 declare
16581 P : Node_Id;
16582
16583 begin
16584 P := Associated_Node_For_Itype (Node_Id (F));
16585 while Present (P) loop
16586 if P = Source then
16587 Visit_Node (Node_Id (F));
16588 return;
16589 else
16590 P := Parent (P);
16591 end if;
16592 end loop;
16593
16594 -- An Itype whose parent is not being copied definitely
16595 -- should NOT be copied, since it does not belong in any
16596 -- sense to the copied subtree.
16597
16598 return;
16599 end;
16600 end if;
16601
16602 elsif F in List_Range and then Parent (List_Id (F)) = N then
16603 Visit_List (List_Id (F));
16604 return;
16605 end if;
16606 end Visit_Field;
16607
16608 -----------------
16609 -- Visit_Itype --
16610 -----------------
16611
16612 procedure Visit_Itype (Old_Itype : Entity_Id) is
16613 New_Itype : Entity_Id;
16614 E : Elmt_Id;
16615 Ent : Entity_Id;
16616
16617 begin
16618 -- Itypes that describe the designated type of access to subprograms
16619 -- have the structure of subprogram declarations, with signatures,
16620 -- etc. Either we duplicate the signatures completely, or choose to
16621 -- share such itypes, which is fine because their elaboration will
16622 -- have no side effects.
16623
16624 if Ekind (Old_Itype) = E_Subprogram_Type then
16625 return;
16626 end if;
16627
16628 New_Itype := New_Copy (Old_Itype);
16629
16630 -- The new Itype has all the attributes of the old one, and
16631 -- we just copy the contents of the entity. However, the back-end
16632 -- needs different names for debugging purposes, so we create a
16633 -- new internal name for it in all cases.
16634
16635 Set_Chars (New_Itype, New_Internal_Name ('T'));
16636
16637 -- If our associated node is an entity that has already been copied,
16638 -- then set the associated node of the copy to point to the right
16639 -- copy. If we have copied an Itype that is itself the associated
16640 -- node of some previously copied Itype, then we set the right
16641 -- pointer in the other direction.
16642
16643 if Present (Actual_Map) then
16644
16645 -- Case of hash tables used
16646
16647 if NCT_Hash_Tables_Used then
16648
16649 Ent := NCT_Assoc.Get (Associated_Node_For_Itype (Old_Itype));
16650
16651 if Present (Ent) then
16652 Set_Associated_Node_For_Itype (New_Itype, Ent);
16653 end if;
16654
16655 Ent := NCT_Itype_Assoc.Get (Old_Itype);
16656 if Present (Ent) then
16657 Set_Associated_Node_For_Itype (Ent, New_Itype);
16658
16659 -- If the hash table has no association for this Itype and
16660 -- its associated node, enter one now.
16661
16662 else
16663 NCT_Itype_Assoc.Set
16664 (Associated_Node_For_Itype (Old_Itype), New_Itype);
16665 end if;
16666
16667 -- Case of hash tables not used
16668
16669 else
16670 E := First_Elmt (Actual_Map);
16671 while Present (E) loop
16672 if Associated_Node_For_Itype (Old_Itype) = Node (E) then
16673 Set_Associated_Node_For_Itype
16674 (New_Itype, Node (Next_Elmt (E)));
16675 end if;
16676
16677 if Is_Type (Node (E))
16678 and then Old_Itype = Associated_Node_For_Itype (Node (E))
16679 then
16680 Set_Associated_Node_For_Itype
16681 (Node (Next_Elmt (E)), New_Itype);
16682 end if;
16683
16684 E := Next_Elmt (Next_Elmt (E));
16685 end loop;
16686 end if;
16687 end if;
16688
16689 if Present (Freeze_Node (New_Itype)) then
16690 Set_Is_Frozen (New_Itype, False);
16691 Set_Freeze_Node (New_Itype, Empty);
16692 end if;
16693
16694 -- Add new association to map
16695
16696 if No (Actual_Map) then
16697 Actual_Map := New_Elmt_List;
16698 end if;
16699
16700 Append_Elmt (Old_Itype, Actual_Map);
16701 Append_Elmt (New_Itype, Actual_Map);
16702
16703 if NCT_Hash_Tables_Used then
16704 NCT_Assoc.Set (Old_Itype, New_Itype);
16705
16706 else
16707 NCT_Table_Entries := NCT_Table_Entries + 1;
16708
16709 if NCT_Table_Entries > NCT_Hash_Threshold then
16710 Build_NCT_Hash_Tables;
16711 end if;
16712 end if;
16713
16714 -- If a record subtype is simply copied, the entity list will be
16715 -- shared. Thus cloned_Subtype must be set to indicate the sharing.
16716
16717 if Ekind_In (Old_Itype, E_Record_Subtype, E_Class_Wide_Subtype) then
16718 Set_Cloned_Subtype (New_Itype, Old_Itype);
16719 end if;
16720
16721 -- Visit descendants that eventually get copied
16722
16723 Visit_Field (Union_Id (Etype (Old_Itype)), Old_Itype);
16724
16725 if Is_Discrete_Type (Old_Itype) then
16726 Visit_Field (Union_Id (Scalar_Range (Old_Itype)), Old_Itype);
16727
16728 elsif Has_Discriminants (Base_Type (Old_Itype)) then
16729 -- ??? This should involve call to Visit_Field
16730 Visit_Elist (Discriminant_Constraint (Old_Itype));
16731
16732 elsif Is_Array_Type (Old_Itype) then
16733 if Present (First_Index (Old_Itype)) then
16734 Visit_Field (Union_Id (List_Containing
16735 (First_Index (Old_Itype))),
16736 Old_Itype);
16737 end if;
16738
16739 if Is_Packed (Old_Itype) then
16740 Visit_Field (Union_Id (Packed_Array_Impl_Type (Old_Itype)),
16741 Old_Itype);
16742 end if;
16743 end if;
16744 end Visit_Itype;
16745
16746 ----------------
16747 -- Visit_List --
16748 ----------------
16749
16750 procedure Visit_List (L : List_Id) is
16751 N : Node_Id;
16752 begin
16753 if L /= No_List then
16754 N := First (L);
16755
16756 while Present (N) loop
16757 Visit_Node (N);
16758 Next (N);
16759 end loop;
16760 end if;
16761 end Visit_List;
16762
16763 ----------------
16764 -- Visit_Node --
16765 ----------------
16766
16767 procedure Visit_Node (N : Node_Or_Entity_Id) is
16768
16769 -- Start of processing for Visit_Node
16770
16771 begin
16772 -- Handle case of an Itype, which must be copied
16773
16774 if Has_Extension (N) and then Is_Itype (N) then
16775
16776 -- Nothing to do if already in the list. This can happen with an
16777 -- Itype entity that appears more than once in the tree.
16778 -- Note that we do not want to visit descendants in this case.
16779
16780 -- Test for already in list when hash table is used
16781
16782 if NCT_Hash_Tables_Used then
16783 if Present (NCT_Assoc.Get (Entity_Id (N))) then
16784 return;
16785 end if;
16786
16787 -- Test for already in list when hash table not used
16788
16789 else
16790 declare
16791 E : Elmt_Id;
16792 begin
16793 if Present (Actual_Map) then
16794 E := First_Elmt (Actual_Map);
16795 while Present (E) loop
16796 if Node (E) = N then
16797 return;
16798 else
16799 E := Next_Elmt (Next_Elmt (E));
16800 end if;
16801 end loop;
16802 end if;
16803 end;
16804 end if;
16805
16806 Visit_Itype (N);
16807 end if;
16808
16809 -- Visit descendants
16810
16811 Visit_Field (Field1 (N), N);
16812 Visit_Field (Field2 (N), N);
16813 Visit_Field (Field3 (N), N);
16814 Visit_Field (Field4 (N), N);
16815 Visit_Field (Field5 (N), N);
16816 end Visit_Node;
16817
16818 -- Start of processing for New_Copy_Tree
16819
16820 begin
16821 Actual_Map := Map;
16822
16823 -- See if we should use hash table
16824
16825 if No (Actual_Map) then
16826 NCT_Hash_Tables_Used := False;
16827
16828 else
16829 declare
16830 Elmt : Elmt_Id;
16831
16832 begin
16833 NCT_Table_Entries := 0;
16834
16835 Elmt := First_Elmt (Actual_Map);
16836 while Present (Elmt) loop
16837 NCT_Table_Entries := NCT_Table_Entries + 1;
16838 Next_Elmt (Elmt);
16839 Next_Elmt (Elmt);
16840 end loop;
16841
16842 if NCT_Table_Entries > NCT_Hash_Threshold then
16843 Build_NCT_Hash_Tables;
16844 else
16845 NCT_Hash_Tables_Used := False;
16846 end if;
16847 end;
16848 end if;
16849
16850 -- Hash table set up if required, now start phase one by visiting
16851 -- top node (we will recursively visit the descendants).
16852
16853 Visit_Node (Source);
16854
16855 -- Now the second phase of the copy can start. First we process
16856 -- all the mapped entities, copying their descendants.
16857
16858 if Present (Actual_Map) then
16859 declare
16860 Elmt : Elmt_Id;
16861 New_Itype : Entity_Id;
16862 begin
16863 Elmt := First_Elmt (Actual_Map);
16864 while Present (Elmt) loop
16865 Next_Elmt (Elmt);
16866 New_Itype := Node (Elmt);
16867
16868 if Is_Itype (New_Itype) then
16869 Copy_Itype_With_Replacement (New_Itype);
16870 end if;
16871 Next_Elmt (Elmt);
16872 end loop;
16873 end;
16874 end if;
16875
16876 -- Now we can copy the actual tree
16877
16878 return Copy_Node_With_Replacement (Source);
16879 end New_Copy_Tree;
16880
16881 -------------------------
16882 -- New_External_Entity --
16883 -------------------------
16884
16885 function New_External_Entity
16886 (Kind : Entity_Kind;
16887 Scope_Id : Entity_Id;
16888 Sloc_Value : Source_Ptr;
16889 Related_Id : Entity_Id;
16890 Suffix : Character;
16891 Suffix_Index : Nat := 0;
16892 Prefix : Character := ' ') return Entity_Id
16893 is
16894 N : constant Entity_Id :=
16895 Make_Defining_Identifier (Sloc_Value,
16896 New_External_Name
16897 (Chars (Related_Id), Suffix, Suffix_Index, Prefix));
16898
16899 begin
16900 Set_Ekind (N, Kind);
16901 Set_Is_Internal (N, True);
16902 Append_Entity (N, Scope_Id);
16903 Set_Public_Status (N);
16904
16905 if Kind in Type_Kind then
16906 Init_Size_Align (N);
16907 end if;
16908
16909 return N;
16910 end New_External_Entity;
16911
16912 -------------------------
16913 -- New_Internal_Entity --
16914 -------------------------
16915
16916 function New_Internal_Entity
16917 (Kind : Entity_Kind;
16918 Scope_Id : Entity_Id;
16919 Sloc_Value : Source_Ptr;
16920 Id_Char : Character) return Entity_Id
16921 is
16922 N : constant Entity_Id := Make_Temporary (Sloc_Value, Id_Char);
16923
16924 begin
16925 Set_Ekind (N, Kind);
16926 Set_Is_Internal (N, True);
16927 Append_Entity (N, Scope_Id);
16928
16929 if Kind in Type_Kind then
16930 Init_Size_Align (N);
16931 end if;
16932
16933 return N;
16934 end New_Internal_Entity;
16935
16936 -----------------
16937 -- Next_Actual --
16938 -----------------
16939
16940 function Next_Actual (Actual_Id : Node_Id) return Node_Id is
16941 N : Node_Id;
16942
16943 begin
16944 -- If we are pointing at a positional parameter, it is a member of a
16945 -- node list (the list of parameters), and the next parameter is the
16946 -- next node on the list, unless we hit a parameter association, then
16947 -- we shift to using the chain whose head is the First_Named_Actual in
16948 -- the parent, and then is threaded using the Next_Named_Actual of the
16949 -- Parameter_Association. All this fiddling is because the original node
16950 -- list is in the textual call order, and what we need is the
16951 -- declaration order.
16952
16953 if Is_List_Member (Actual_Id) then
16954 N := Next (Actual_Id);
16955
16956 if Nkind (N) = N_Parameter_Association then
16957 return First_Named_Actual (Parent (Actual_Id));
16958 else
16959 return N;
16960 end if;
16961
16962 else
16963 return Next_Named_Actual (Parent (Actual_Id));
16964 end if;
16965 end Next_Actual;
16966
16967 procedure Next_Actual (Actual_Id : in out Node_Id) is
16968 begin
16969 Actual_Id := Next_Actual (Actual_Id);
16970 end Next_Actual;
16971
16972 -----------------------
16973 -- Normalize_Actuals --
16974 -----------------------
16975
16976 -- Chain actuals according to formals of subprogram. If there are no named
16977 -- associations, the chain is simply the list of Parameter Associations,
16978 -- since the order is the same as the declaration order. If there are named
16979 -- associations, then the First_Named_Actual field in the N_Function_Call
16980 -- or N_Procedure_Call_Statement node points to the Parameter_Association
16981 -- node for the parameter that comes first in declaration order. The
16982 -- remaining named parameters are then chained in declaration order using
16983 -- Next_Named_Actual.
16984
16985 -- This routine also verifies that the number of actuals is compatible with
16986 -- the number and default values of formals, but performs no type checking
16987 -- (type checking is done by the caller).
16988
16989 -- If the matching succeeds, Success is set to True and the caller proceeds
16990 -- with type-checking. If the match is unsuccessful, then Success is set to
16991 -- False, and the caller attempts a different interpretation, if there is
16992 -- one.
16993
16994 -- If the flag Report is on, the call is not overloaded, and a failure to
16995 -- match can be reported here, rather than in the caller.
16996
16997 procedure Normalize_Actuals
16998 (N : Node_Id;
16999 S : Entity_Id;
17000 Report : Boolean;
17001 Success : out Boolean)
17002 is
17003 Actuals : constant List_Id := Parameter_Associations (N);
17004 Actual : Node_Id := Empty;
17005 Formal : Entity_Id;
17006 Last : Node_Id := Empty;
17007 First_Named : Node_Id := Empty;
17008 Found : Boolean;
17009
17010 Formals_To_Match : Integer := 0;
17011 Actuals_To_Match : Integer := 0;
17012
17013 procedure Chain (A : Node_Id);
17014 -- Add named actual at the proper place in the list, using the
17015 -- Next_Named_Actual link.
17016
17017 function Reporting return Boolean;
17018 -- Determines if an error is to be reported. To report an error, we
17019 -- need Report to be True, and also we do not report errors caused
17020 -- by calls to init procs that occur within other init procs. Such
17021 -- errors must always be cascaded errors, since if all the types are
17022 -- declared correctly, the compiler will certainly build decent calls.
17023
17024 -----------
17025 -- Chain --
17026 -----------
17027
17028 procedure Chain (A : Node_Id) is
17029 begin
17030 if No (Last) then
17031
17032 -- Call node points to first actual in list
17033
17034 Set_First_Named_Actual (N, Explicit_Actual_Parameter (A));
17035
17036 else
17037 Set_Next_Named_Actual (Last, Explicit_Actual_Parameter (A));
17038 end if;
17039
17040 Last := A;
17041 Set_Next_Named_Actual (Last, Empty);
17042 end Chain;
17043
17044 ---------------
17045 -- Reporting --
17046 ---------------
17047
17048 function Reporting return Boolean is
17049 begin
17050 if not Report then
17051 return False;
17052
17053 elsif not Within_Init_Proc then
17054 return True;
17055
17056 elsif Is_Init_Proc (Entity (Name (N))) then
17057 return False;
17058
17059 else
17060 return True;
17061 end if;
17062 end Reporting;
17063
17064 -- Start of processing for Normalize_Actuals
17065
17066 begin
17067 if Is_Access_Type (S) then
17068
17069 -- The name in the call is a function call that returns an access
17070 -- to subprogram. The designated type has the list of formals.
17071
17072 Formal := First_Formal (Designated_Type (S));
17073 else
17074 Formal := First_Formal (S);
17075 end if;
17076
17077 while Present (Formal) loop
17078 Formals_To_Match := Formals_To_Match + 1;
17079 Next_Formal (Formal);
17080 end loop;
17081
17082 -- Find if there is a named association, and verify that no positional
17083 -- associations appear after named ones.
17084
17085 if Present (Actuals) then
17086 Actual := First (Actuals);
17087 end if;
17088
17089 while Present (Actual)
17090 and then Nkind (Actual) /= N_Parameter_Association
17091 loop
17092 Actuals_To_Match := Actuals_To_Match + 1;
17093 Next (Actual);
17094 end loop;
17095
17096 if No (Actual) and Actuals_To_Match = Formals_To_Match then
17097
17098 -- Most common case: positional notation, no defaults
17099
17100 Success := True;
17101 return;
17102
17103 elsif Actuals_To_Match > Formals_To_Match then
17104
17105 -- Too many actuals: will not work
17106
17107 if Reporting then
17108 if Is_Entity_Name (Name (N)) then
17109 Error_Msg_N ("too many arguments in call to&", Name (N));
17110 else
17111 Error_Msg_N ("too many arguments in call", N);
17112 end if;
17113 end if;
17114
17115 Success := False;
17116 return;
17117 end if;
17118
17119 First_Named := Actual;
17120
17121 while Present (Actual) loop
17122 if Nkind (Actual) /= N_Parameter_Association then
17123 Error_Msg_N
17124 ("positional parameters not allowed after named ones", Actual);
17125 Success := False;
17126 return;
17127
17128 else
17129 Actuals_To_Match := Actuals_To_Match + 1;
17130 end if;
17131
17132 Next (Actual);
17133 end loop;
17134
17135 if Present (Actuals) then
17136 Actual := First (Actuals);
17137 end if;
17138
17139 Formal := First_Formal (S);
17140 while Present (Formal) loop
17141
17142 -- Match the formals in order. If the corresponding actual is
17143 -- positional, nothing to do. Else scan the list of named actuals
17144 -- to find the one with the right name.
17145
17146 if Present (Actual)
17147 and then Nkind (Actual) /= N_Parameter_Association
17148 then
17149 Next (Actual);
17150 Actuals_To_Match := Actuals_To_Match - 1;
17151 Formals_To_Match := Formals_To_Match - 1;
17152
17153 else
17154 -- For named parameters, search the list of actuals to find
17155 -- one that matches the next formal name.
17156
17157 Actual := First_Named;
17158 Found := False;
17159 while Present (Actual) loop
17160 if Chars (Selector_Name (Actual)) = Chars (Formal) then
17161 Found := True;
17162 Chain (Actual);
17163 Actuals_To_Match := Actuals_To_Match - 1;
17164 Formals_To_Match := Formals_To_Match - 1;
17165 exit;
17166 end if;
17167
17168 Next (Actual);
17169 end loop;
17170
17171 if not Found then
17172 if Ekind (Formal) /= E_In_Parameter
17173 or else No (Default_Value (Formal))
17174 then
17175 if Reporting then
17176 if (Comes_From_Source (S)
17177 or else Sloc (S) = Standard_Location)
17178 and then Is_Overloadable (S)
17179 then
17180 if No (Actuals)
17181 and then
17182 Nkind_In (Parent (N), N_Procedure_Call_Statement,
17183 N_Function_Call,
17184 N_Parameter_Association)
17185 and then Ekind (S) /= E_Function
17186 then
17187 Set_Etype (N, Etype (S));
17188
17189 else
17190 Error_Msg_Name_1 := Chars (S);
17191 Error_Msg_Sloc := Sloc (S);
17192 Error_Msg_NE
17193 ("missing argument for parameter & "
17194 & "in call to % declared #", N, Formal);
17195 end if;
17196
17197 elsif Is_Overloadable (S) then
17198 Error_Msg_Name_1 := Chars (S);
17199
17200 -- Point to type derivation that generated the
17201 -- operation.
17202
17203 Error_Msg_Sloc := Sloc (Parent (S));
17204
17205 Error_Msg_NE
17206 ("missing argument for parameter & "
17207 & "in call to % (inherited) #", N, Formal);
17208
17209 else
17210 Error_Msg_NE
17211 ("missing argument for parameter &", N, Formal);
17212 end if;
17213 end if;
17214
17215 Success := False;
17216 return;
17217
17218 else
17219 Formals_To_Match := Formals_To_Match - 1;
17220 end if;
17221 end if;
17222 end if;
17223
17224 Next_Formal (Formal);
17225 end loop;
17226
17227 if Formals_To_Match = 0 and then Actuals_To_Match = 0 then
17228 Success := True;
17229 return;
17230
17231 else
17232 if Reporting then
17233
17234 -- Find some superfluous named actual that did not get
17235 -- attached to the list of associations.
17236
17237 Actual := First (Actuals);
17238 while Present (Actual) loop
17239 if Nkind (Actual) = N_Parameter_Association
17240 and then Actual /= Last
17241 and then No (Next_Named_Actual (Actual))
17242 then
17243 -- A validity check may introduce a copy of a call that
17244 -- includes an extra actual (for example for an unrelated
17245 -- accessibility check). Check that the extra actual matches
17246 -- some extra formal, which must exist already because
17247 -- subprogram must be frozen at this point.
17248
17249 if Present (Extra_Formals (S))
17250 and then not Comes_From_Source (Actual)
17251 and then Nkind (Actual) = N_Parameter_Association
17252 and then Chars (Extra_Formals (S)) =
17253 Chars (Selector_Name (Actual))
17254 then
17255 null;
17256 else
17257 Error_Msg_N
17258 ("unmatched actual & in call", Selector_Name (Actual));
17259 exit;
17260 end if;
17261 end if;
17262
17263 Next (Actual);
17264 end loop;
17265 end if;
17266
17267 Success := False;
17268 return;
17269 end if;
17270 end Normalize_Actuals;
17271
17272 --------------------------------
17273 -- Note_Possible_Modification --
17274 --------------------------------
17275
17276 procedure Note_Possible_Modification (N : Node_Id; Sure : Boolean) is
17277 Modification_Comes_From_Source : constant Boolean :=
17278 Comes_From_Source (Parent (N));
17279
17280 Ent : Entity_Id;
17281 Exp : Node_Id;
17282
17283 begin
17284 -- Loop to find referenced entity, if there is one
17285
17286 Exp := N;
17287 loop
17288 Ent := Empty;
17289
17290 if Is_Entity_Name (Exp) then
17291 Ent := Entity (Exp);
17292
17293 -- If the entity is missing, it is an undeclared identifier,
17294 -- and there is nothing to annotate.
17295
17296 if No (Ent) then
17297 return;
17298 end if;
17299
17300 elsif Nkind (Exp) = N_Explicit_Dereference then
17301 declare
17302 P : constant Node_Id := Prefix (Exp);
17303
17304 begin
17305 -- In formal verification mode, keep track of all reads and
17306 -- writes through explicit dereferences.
17307
17308 if GNATprove_Mode then
17309 SPARK_Specific.Generate_Dereference (N, 'm');
17310 end if;
17311
17312 if Nkind (P) = N_Selected_Component
17313 and then Present (Entry_Formal (Entity (Selector_Name (P))))
17314 then
17315 -- Case of a reference to an entry formal
17316
17317 Ent := Entry_Formal (Entity (Selector_Name (P)));
17318
17319 elsif Nkind (P) = N_Identifier
17320 and then Nkind (Parent (Entity (P))) = N_Object_Declaration
17321 and then Present (Expression (Parent (Entity (P))))
17322 and then Nkind (Expression (Parent (Entity (P)))) =
17323 N_Reference
17324 then
17325 -- Case of a reference to a value on which side effects have
17326 -- been removed.
17327
17328 Exp := Prefix (Expression (Parent (Entity (P))));
17329 goto Continue;
17330
17331 else
17332 return;
17333 end if;
17334 end;
17335
17336 elsif Nkind_In (Exp, N_Type_Conversion,
17337 N_Unchecked_Type_Conversion)
17338 then
17339 Exp := Expression (Exp);
17340 goto Continue;
17341
17342 elsif Nkind_In (Exp, N_Slice,
17343 N_Indexed_Component,
17344 N_Selected_Component)
17345 then
17346 -- Special check, if the prefix is an access type, then return
17347 -- since we are modifying the thing pointed to, not the prefix.
17348 -- When we are expanding, most usually the prefix is replaced
17349 -- by an explicit dereference, and this test is not needed, but
17350 -- in some cases (notably -gnatc mode and generics) when we do
17351 -- not do full expansion, we need this special test.
17352
17353 if Is_Access_Type (Etype (Prefix (Exp))) then
17354 return;
17355
17356 -- Otherwise go to prefix and keep going
17357
17358 else
17359 Exp := Prefix (Exp);
17360 goto Continue;
17361 end if;
17362
17363 -- All other cases, not a modification
17364
17365 else
17366 return;
17367 end if;
17368
17369 -- Now look for entity being referenced
17370
17371 if Present (Ent) then
17372 if Is_Object (Ent) then
17373 if Comes_From_Source (Exp)
17374 or else Modification_Comes_From_Source
17375 then
17376 -- Give warning if pragma unmodified is given and we are
17377 -- sure this is a modification.
17378
17379 if Has_Pragma_Unmodified (Ent) and then Sure then
17380
17381 -- Note that the entity may be present only as a result
17382 -- of pragma Unused.
17383
17384 if Has_Pragma_Unused (Ent) then
17385 Error_Msg_NE ("??pragma Unused given for &!", N, Ent);
17386 else
17387 Error_Msg_NE
17388 ("??pragma Unmodified given for &!", N, Ent);
17389 end if;
17390 end if;
17391
17392 Set_Never_Set_In_Source (Ent, False);
17393 end if;
17394
17395 Set_Is_True_Constant (Ent, False);
17396 Set_Current_Value (Ent, Empty);
17397 Set_Is_Known_Null (Ent, False);
17398
17399 if not Can_Never_Be_Null (Ent) then
17400 Set_Is_Known_Non_Null (Ent, False);
17401 end if;
17402
17403 -- Follow renaming chain
17404
17405 if (Ekind (Ent) = E_Variable or else Ekind (Ent) = E_Constant)
17406 and then Present (Renamed_Object (Ent))
17407 then
17408 Exp := Renamed_Object (Ent);
17409
17410 -- If the entity is the loop variable in an iteration over
17411 -- a container, retrieve container expression to indicate
17412 -- possible modification.
17413
17414 if Present (Related_Expression (Ent))
17415 and then Nkind (Parent (Related_Expression (Ent))) =
17416 N_Iterator_Specification
17417 then
17418 Exp := Original_Node (Related_Expression (Ent));
17419 end if;
17420
17421 goto Continue;
17422
17423 -- The expression may be the renaming of a subcomponent of an
17424 -- array or container. The assignment to the subcomponent is
17425 -- a modification of the container.
17426
17427 elsif Comes_From_Source (Original_Node (Exp))
17428 and then Nkind_In (Original_Node (Exp), N_Selected_Component,
17429 N_Indexed_Component)
17430 then
17431 Exp := Prefix (Original_Node (Exp));
17432 goto Continue;
17433 end if;
17434
17435 -- Generate a reference only if the assignment comes from
17436 -- source. This excludes, for example, calls to a dispatching
17437 -- assignment operation when the left-hand side is tagged. In
17438 -- GNATprove mode, we need those references also on generated
17439 -- code, as these are used to compute the local effects of
17440 -- subprograms.
17441
17442 if Modification_Comes_From_Source or GNATprove_Mode then
17443 Generate_Reference (Ent, Exp, 'm');
17444
17445 -- If the target of the assignment is the bound variable
17446 -- in an iterator, indicate that the corresponding array
17447 -- or container is also modified.
17448
17449 if Ada_Version >= Ada_2012
17450 and then Nkind (Parent (Ent)) = N_Iterator_Specification
17451 then
17452 declare
17453 Domain : constant Node_Id := Name (Parent (Ent));
17454
17455 begin
17456 -- TBD : in the full version of the construct, the
17457 -- domain of iteration can be given by an expression.
17458
17459 if Is_Entity_Name (Domain) then
17460 Generate_Reference (Entity (Domain), Exp, 'm');
17461 Set_Is_True_Constant (Entity (Domain), False);
17462 Set_Never_Set_In_Source (Entity (Domain), False);
17463 end if;
17464 end;
17465 end if;
17466 end if;
17467 end if;
17468
17469 Kill_Checks (Ent);
17470
17471 -- If we are sure this is a modification from source, and we know
17472 -- this modifies a constant, then give an appropriate warning.
17473
17474 if Sure
17475 and then Modification_Comes_From_Source
17476 and then Overlays_Constant (Ent)
17477 and then Address_Clause_Overlay_Warnings
17478 then
17479 declare
17480 Addr : constant Node_Id := Address_Clause (Ent);
17481 O_Ent : Entity_Id;
17482 Off : Boolean;
17483
17484 begin
17485 Find_Overlaid_Entity (Addr, O_Ent, Off);
17486
17487 Error_Msg_Sloc := Sloc (Addr);
17488 Error_Msg_NE
17489 ("??constant& may be modified via address clause#",
17490 N, O_Ent);
17491 end;
17492 end if;
17493
17494 return;
17495 end if;
17496
17497 <<Continue>>
17498 null;
17499 end loop;
17500 end Note_Possible_Modification;
17501
17502 --------------------------------------
17503 -- Null_To_Null_Address_Convert_OK --
17504 --------------------------------------
17505
17506 function Null_To_Null_Address_Convert_OK
17507 (N : Node_Id;
17508 Typ : Entity_Id := Empty) return Boolean
17509 is
17510 begin
17511 if not Relaxed_RM_Semantics then
17512 return False;
17513 end if;
17514
17515 if Nkind (N) = N_Null then
17516 return Present (Typ) and then Is_Descendant_Of_Address (Typ);
17517
17518 elsif Nkind_In (N, N_Op_Eq, N_Op_Ge, N_Op_Gt, N_Op_Le, N_Op_Lt, N_Op_Ne)
17519 then
17520 declare
17521 L : constant Node_Id := Left_Opnd (N);
17522 R : constant Node_Id := Right_Opnd (N);
17523
17524 begin
17525 -- We check the Etype of the complementary operand since the
17526 -- N_Null node is not decorated at this stage.
17527
17528 return
17529 ((Nkind (L) = N_Null
17530 and then Is_Descendant_Of_Address (Etype (R)))
17531 or else
17532 (Nkind (R) = N_Null
17533 and then Is_Descendant_Of_Address (Etype (L))));
17534 end;
17535 end if;
17536
17537 return False;
17538 end Null_To_Null_Address_Convert_OK;
17539
17540 -------------------------
17541 -- Object_Access_Level --
17542 -------------------------
17543
17544 -- Returns the static accessibility level of the view denoted by Obj. Note
17545 -- that the value returned is the result of a call to Scope_Depth. Only
17546 -- scope depths associated with dynamic scopes can actually be returned.
17547 -- Since only relative levels matter for accessibility checking, the fact
17548 -- that the distance between successive levels of accessibility is not
17549 -- always one is immaterial (invariant: if level(E2) is deeper than
17550 -- level(E1), then Scope_Depth(E1) < Scope_Depth(E2)).
17551
17552 function Object_Access_Level (Obj : Node_Id) return Uint is
17553 function Is_Interface_Conversion (N : Node_Id) return Boolean;
17554 -- Determine whether N is a construct of the form
17555 -- Some_Type (Operand._tag'Address)
17556 -- This construct appears in the context of dispatching calls.
17557
17558 function Reference_To (Obj : Node_Id) return Node_Id;
17559 -- An explicit dereference is created when removing side-effects from
17560 -- expressions for constraint checking purposes. In this case a local
17561 -- access type is created for it. The correct access level is that of
17562 -- the original source node. We detect this case by noting that the
17563 -- prefix of the dereference is created by an object declaration whose
17564 -- initial expression is a reference.
17565
17566 -----------------------------
17567 -- Is_Interface_Conversion --
17568 -----------------------------
17569
17570 function Is_Interface_Conversion (N : Node_Id) return Boolean is
17571 begin
17572 return Nkind (N) = N_Unchecked_Type_Conversion
17573 and then Nkind (Expression (N)) = N_Attribute_Reference
17574 and then Attribute_Name (Expression (N)) = Name_Address;
17575 end Is_Interface_Conversion;
17576
17577 ------------------
17578 -- Reference_To --
17579 ------------------
17580
17581 function Reference_To (Obj : Node_Id) return Node_Id is
17582 Pref : constant Node_Id := Prefix (Obj);
17583 begin
17584 if Is_Entity_Name (Pref)
17585 and then Nkind (Parent (Entity (Pref))) = N_Object_Declaration
17586 and then Present (Expression (Parent (Entity (Pref))))
17587 and then Nkind (Expression (Parent (Entity (Pref)))) = N_Reference
17588 then
17589 return (Prefix (Expression (Parent (Entity (Pref)))));
17590 else
17591 return Empty;
17592 end if;
17593 end Reference_To;
17594
17595 -- Local variables
17596
17597 E : Entity_Id;
17598
17599 -- Start of processing for Object_Access_Level
17600
17601 begin
17602 if Nkind (Obj) = N_Defining_Identifier
17603 or else Is_Entity_Name (Obj)
17604 then
17605 if Nkind (Obj) = N_Defining_Identifier then
17606 E := Obj;
17607 else
17608 E := Entity (Obj);
17609 end if;
17610
17611 if Is_Prival (E) then
17612 E := Prival_Link (E);
17613 end if;
17614
17615 -- If E is a type then it denotes a current instance. For this case
17616 -- we add one to the normal accessibility level of the type to ensure
17617 -- that current instances are treated as always being deeper than
17618 -- than the level of any visible named access type (see 3.10.2(21)).
17619
17620 if Is_Type (E) then
17621 return Type_Access_Level (E) + 1;
17622
17623 elsif Present (Renamed_Object (E)) then
17624 return Object_Access_Level (Renamed_Object (E));
17625
17626 -- Similarly, if E is a component of the current instance of a
17627 -- protected type, any instance of it is assumed to be at a deeper
17628 -- level than the type. For a protected object (whose type is an
17629 -- anonymous protected type) its components are at the same level
17630 -- as the type itself.
17631
17632 elsif not Is_Overloadable (E)
17633 and then Ekind (Scope (E)) = E_Protected_Type
17634 and then Comes_From_Source (Scope (E))
17635 then
17636 return Type_Access_Level (Scope (E)) + 1;
17637
17638 else
17639 -- Aliased formals of functions take their access level from the
17640 -- point of call, i.e. require a dynamic check. For static check
17641 -- purposes, this is smaller than the level of the subprogram
17642 -- itself. For procedures the aliased makes no difference.
17643
17644 if Is_Formal (E)
17645 and then Is_Aliased (E)
17646 and then Ekind (Scope (E)) = E_Function
17647 then
17648 return Type_Access_Level (Etype (E));
17649
17650 else
17651 return Scope_Depth (Enclosing_Dynamic_Scope (E));
17652 end if;
17653 end if;
17654
17655 elsif Nkind (Obj) = N_Selected_Component then
17656 if Is_Access_Type (Etype (Prefix (Obj))) then
17657 return Type_Access_Level (Etype (Prefix (Obj)));
17658 else
17659 return Object_Access_Level (Prefix (Obj));
17660 end if;
17661
17662 elsif Nkind (Obj) = N_Indexed_Component then
17663 if Is_Access_Type (Etype (Prefix (Obj))) then
17664 return Type_Access_Level (Etype (Prefix (Obj)));
17665 else
17666 return Object_Access_Level (Prefix (Obj));
17667 end if;
17668
17669 elsif Nkind (Obj) = N_Explicit_Dereference then
17670
17671 -- If the prefix is a selected access discriminant then we make a
17672 -- recursive call on the prefix, which will in turn check the level
17673 -- of the prefix object of the selected discriminant.
17674
17675 -- In Ada 2012, if the discriminant has implicit dereference and
17676 -- the context is a selected component, treat this as an object of
17677 -- unknown scope (see below). This is necessary in compile-only mode;
17678 -- otherwise expansion will already have transformed the prefix into
17679 -- a temporary.
17680
17681 if Nkind (Prefix (Obj)) = N_Selected_Component
17682 and then Ekind (Etype (Prefix (Obj))) = E_Anonymous_Access_Type
17683 and then
17684 Ekind (Entity (Selector_Name (Prefix (Obj)))) = E_Discriminant
17685 and then
17686 (not Has_Implicit_Dereference
17687 (Entity (Selector_Name (Prefix (Obj))))
17688 or else Nkind (Parent (Obj)) /= N_Selected_Component)
17689 then
17690 return Object_Access_Level (Prefix (Obj));
17691
17692 -- Detect an interface conversion in the context of a dispatching
17693 -- call. Use the original form of the conversion to find the access
17694 -- level of the operand.
17695
17696 elsif Is_Interface (Etype (Obj))
17697 and then Is_Interface_Conversion (Prefix (Obj))
17698 and then Nkind (Original_Node (Obj)) = N_Type_Conversion
17699 then
17700 return Object_Access_Level (Original_Node (Obj));
17701
17702 elsif not Comes_From_Source (Obj) then
17703 declare
17704 Ref : constant Node_Id := Reference_To (Obj);
17705 begin
17706 if Present (Ref) then
17707 return Object_Access_Level (Ref);
17708 else
17709 return Type_Access_Level (Etype (Prefix (Obj)));
17710 end if;
17711 end;
17712
17713 else
17714 return Type_Access_Level (Etype (Prefix (Obj)));
17715 end if;
17716
17717 elsif Nkind_In (Obj, N_Type_Conversion, N_Unchecked_Type_Conversion) then
17718 return Object_Access_Level (Expression (Obj));
17719
17720 elsif Nkind (Obj) = N_Function_Call then
17721
17722 -- Function results are objects, so we get either the access level of
17723 -- the function or, in the case of an indirect call, the level of the
17724 -- access-to-subprogram type. (This code is used for Ada 95, but it
17725 -- looks wrong, because it seems that we should be checking the level
17726 -- of the call itself, even for Ada 95. However, using the Ada 2005
17727 -- version of the code causes regressions in several tests that are
17728 -- compiled with -gnat95. ???)
17729
17730 if Ada_Version < Ada_2005 then
17731 if Is_Entity_Name (Name (Obj)) then
17732 return Subprogram_Access_Level (Entity (Name (Obj)));
17733 else
17734 return Type_Access_Level (Etype (Prefix (Name (Obj))));
17735 end if;
17736
17737 -- For Ada 2005, the level of the result object of a function call is
17738 -- defined to be the level of the call's innermost enclosing master.
17739 -- We determine that by querying the depth of the innermost enclosing
17740 -- dynamic scope.
17741
17742 else
17743 Return_Master_Scope_Depth_Of_Call : declare
17744
17745 function Innermost_Master_Scope_Depth
17746 (N : Node_Id) return Uint;
17747 -- Returns the scope depth of the given node's innermost
17748 -- enclosing dynamic scope (effectively the accessibility
17749 -- level of the innermost enclosing master).
17750
17751 ----------------------------------
17752 -- Innermost_Master_Scope_Depth --
17753 ----------------------------------
17754
17755 function Innermost_Master_Scope_Depth
17756 (N : Node_Id) return Uint
17757 is
17758 Node_Par : Node_Id := Parent (N);
17759
17760 begin
17761 -- Locate the nearest enclosing node (by traversing Parents)
17762 -- that Defining_Entity can be applied to, and return the
17763 -- depth of that entity's nearest enclosing dynamic scope.
17764
17765 while Present (Node_Par) loop
17766 case Nkind (Node_Par) is
17767 when N_Component_Declaration |
17768 N_Entry_Declaration |
17769 N_Formal_Object_Declaration |
17770 N_Formal_Type_Declaration |
17771 N_Full_Type_Declaration |
17772 N_Incomplete_Type_Declaration |
17773 N_Loop_Parameter_Specification |
17774 N_Object_Declaration |
17775 N_Protected_Type_Declaration |
17776 N_Private_Extension_Declaration |
17777 N_Private_Type_Declaration |
17778 N_Subtype_Declaration |
17779 N_Function_Specification |
17780 N_Procedure_Specification |
17781 N_Task_Type_Declaration |
17782 N_Body_Stub |
17783 N_Generic_Instantiation |
17784 N_Proper_Body |
17785 N_Implicit_Label_Declaration |
17786 N_Package_Declaration |
17787 N_Single_Task_Declaration |
17788 N_Subprogram_Declaration |
17789 N_Generic_Declaration |
17790 N_Renaming_Declaration |
17791 N_Block_Statement |
17792 N_Formal_Subprogram_Declaration |
17793 N_Abstract_Subprogram_Declaration |
17794 N_Entry_Body |
17795 N_Exception_Declaration |
17796 N_Formal_Package_Declaration |
17797 N_Number_Declaration |
17798 N_Package_Specification |
17799 N_Parameter_Specification |
17800 N_Single_Protected_Declaration |
17801 N_Subunit =>
17802
17803 return Scope_Depth
17804 (Nearest_Dynamic_Scope
17805 (Defining_Entity (Node_Par)));
17806
17807 when others =>
17808 null;
17809 end case;
17810
17811 Node_Par := Parent (Node_Par);
17812 end loop;
17813
17814 pragma Assert (False);
17815
17816 -- Should never reach the following return
17817
17818 return Scope_Depth (Current_Scope) + 1;
17819 end Innermost_Master_Scope_Depth;
17820
17821 -- Start of processing for Return_Master_Scope_Depth_Of_Call
17822
17823 begin
17824 return Innermost_Master_Scope_Depth (Obj);
17825 end Return_Master_Scope_Depth_Of_Call;
17826 end if;
17827
17828 -- For convenience we handle qualified expressions, even though they
17829 -- aren't technically object names.
17830
17831 elsif Nkind (Obj) = N_Qualified_Expression then
17832 return Object_Access_Level (Expression (Obj));
17833
17834 -- Ditto for aggregates. They have the level of the temporary that
17835 -- will hold their value.
17836
17837 elsif Nkind (Obj) = N_Aggregate then
17838 return Object_Access_Level (Current_Scope);
17839
17840 -- Otherwise return the scope level of Standard. (If there are cases
17841 -- that fall through to this point they will be treated as having
17842 -- global accessibility for now. ???)
17843
17844 else
17845 return Scope_Depth (Standard_Standard);
17846 end if;
17847 end Object_Access_Level;
17848
17849 ---------------------------------
17850 -- Original_Aspect_Pragma_Name --
17851 ---------------------------------
17852
17853 function Original_Aspect_Pragma_Name (N : Node_Id) return Name_Id is
17854 Item : Node_Id;
17855 Item_Nam : Name_Id;
17856
17857 begin
17858 pragma Assert (Nkind_In (N, N_Aspect_Specification, N_Pragma));
17859
17860 Item := N;
17861
17862 -- The pragma was generated to emulate an aspect, use the original
17863 -- aspect specification.
17864
17865 if Nkind (Item) = N_Pragma and then From_Aspect_Specification (Item) then
17866 Item := Corresponding_Aspect (Item);
17867 end if;
17868
17869 -- Retrieve the name of the aspect/pragma. Note that Pre, Pre_Class,
17870 -- Post and Post_Class rewrite their pragma identifier to preserve the
17871 -- original name.
17872 -- ??? this is kludgey
17873
17874 if Nkind (Item) = N_Pragma then
17875 Item_Nam := Chars (Original_Node (Pragma_Identifier (Item)));
17876
17877 else
17878 pragma Assert (Nkind (Item) = N_Aspect_Specification);
17879 Item_Nam := Chars (Identifier (Item));
17880 end if;
17881
17882 -- Deal with 'Class by converting the name to its _XXX form
17883
17884 if Class_Present (Item) then
17885 if Item_Nam = Name_Invariant then
17886 Item_Nam := Name_uInvariant;
17887
17888 elsif Item_Nam = Name_Post then
17889 Item_Nam := Name_uPost;
17890
17891 elsif Item_Nam = Name_Pre then
17892 Item_Nam := Name_uPre;
17893
17894 elsif Nam_In (Item_Nam, Name_Type_Invariant,
17895 Name_Type_Invariant_Class)
17896 then
17897 Item_Nam := Name_uType_Invariant;
17898
17899 -- Nothing to do for other cases (e.g. a Check that derived from
17900 -- Pre_Class and has the flag set). Also we do nothing if the name
17901 -- is already in special _xxx form.
17902
17903 end if;
17904 end if;
17905
17906 return Item_Nam;
17907 end Original_Aspect_Pragma_Name;
17908
17909 --------------------------------------
17910 -- Original_Corresponding_Operation --
17911 --------------------------------------
17912
17913 function Original_Corresponding_Operation (S : Entity_Id) return Entity_Id
17914 is
17915 Typ : constant Entity_Id := Find_Dispatching_Type (S);
17916
17917 begin
17918 -- If S is an inherited primitive S2 the original corresponding
17919 -- operation of S is the original corresponding operation of S2
17920
17921 if Present (Alias (S))
17922 and then Find_Dispatching_Type (Alias (S)) /= Typ
17923 then
17924 return Original_Corresponding_Operation (Alias (S));
17925
17926 -- If S overrides an inherited subprogram S2 the original corresponding
17927 -- operation of S is the original corresponding operation of S2
17928
17929 elsif Present (Overridden_Operation (S)) then
17930 return Original_Corresponding_Operation (Overridden_Operation (S));
17931
17932 -- otherwise it is S itself
17933
17934 else
17935 return S;
17936 end if;
17937 end Original_Corresponding_Operation;
17938
17939 -------------------
17940 -- Output_Entity --
17941 -------------------
17942
17943 procedure Output_Entity (Id : Entity_Id) is
17944 Scop : Entity_Id;
17945
17946 begin
17947 Scop := Scope (Id);
17948
17949 -- The entity may lack a scope when it is in the process of being
17950 -- analyzed. Use the current scope as an approximation.
17951
17952 if No (Scop) then
17953 Scop := Current_Scope;
17954 end if;
17955
17956 Output_Name (Chars (Id), Scop);
17957 end Output_Entity;
17958
17959 -----------------
17960 -- Output_Name --
17961 -----------------
17962
17963 procedure Output_Name (Nam : Name_Id; Scop : Entity_Id := Current_Scope) is
17964 begin
17965 Write_Str
17966 (Get_Name_String
17967 (Get_Qualified_Name
17968 (Nam => Nam,
17969 Suffix => No_Name,
17970 Scop => Scop)));
17971 Write_Eol;
17972 end Output_Name;
17973
17974 ----------------------
17975 -- Policy_In_Effect --
17976 ----------------------
17977
17978 function Policy_In_Effect (Policy : Name_Id) return Name_Id is
17979 function Policy_In_List (List : Node_Id) return Name_Id;
17980 -- Determine the mode of a policy in a N_Pragma list
17981
17982 --------------------
17983 -- Policy_In_List --
17984 --------------------
17985
17986 function Policy_In_List (List : Node_Id) return Name_Id is
17987 Arg1 : Node_Id;
17988 Arg2 : Node_Id;
17989 Prag : Node_Id;
17990
17991 begin
17992 Prag := List;
17993 while Present (Prag) loop
17994 Arg1 := First (Pragma_Argument_Associations (Prag));
17995 Arg2 := Next (Arg1);
17996
17997 Arg1 := Get_Pragma_Arg (Arg1);
17998 Arg2 := Get_Pragma_Arg (Arg2);
17999
18000 -- The current Check_Policy pragma matches the requested policy or
18001 -- appears in the single argument form (Assertion, policy_id).
18002
18003 if Nam_In (Chars (Arg1), Name_Assertion, Policy) then
18004 return Chars (Arg2);
18005 end if;
18006
18007 Prag := Next_Pragma (Prag);
18008 end loop;
18009
18010 return No_Name;
18011 end Policy_In_List;
18012
18013 -- Local variables
18014
18015 Kind : Name_Id;
18016
18017 -- Start of processing for Policy_In_Effect
18018
18019 begin
18020 if not Is_Valid_Assertion_Kind (Policy) then
18021 raise Program_Error;
18022 end if;
18023
18024 -- Inspect all policy pragmas that appear within scopes (if any)
18025
18026 Kind := Policy_In_List (Check_Policy_List);
18027
18028 -- Inspect all configuration policy pragmas (if any)
18029
18030 if Kind = No_Name then
18031 Kind := Policy_In_List (Check_Policy_List_Config);
18032 end if;
18033
18034 -- The context lacks policy pragmas, determine the mode based on whether
18035 -- assertions are enabled at the configuration level. This ensures that
18036 -- the policy is preserved when analyzing generics.
18037
18038 if Kind = No_Name then
18039 if Assertions_Enabled_Config then
18040 Kind := Name_Check;
18041 else
18042 Kind := Name_Ignore;
18043 end if;
18044 end if;
18045
18046 return Kind;
18047 end Policy_In_Effect;
18048
18049 ----------------------------------
18050 -- Predicate_Tests_On_Arguments --
18051 ----------------------------------
18052
18053 function Predicate_Tests_On_Arguments (Subp : Entity_Id) return Boolean is
18054 begin
18055 -- Always test predicates on indirect call
18056
18057 if Ekind (Subp) = E_Subprogram_Type then
18058 return True;
18059
18060 -- Do not test predicates on call to generated default Finalize, since
18061 -- we are not interested in whether something we are finalizing (and
18062 -- typically destroying) satisfies its predicates.
18063
18064 elsif Chars (Subp) = Name_Finalize
18065 and then not Comes_From_Source (Subp)
18066 then
18067 return False;
18068
18069 -- Do not test predicates on any internally generated routines
18070
18071 elsif Is_Internal_Name (Chars (Subp)) then
18072 return False;
18073
18074 -- Do not test predicates on call to Init_Proc, since if needed the
18075 -- predicate test will occur at some other point.
18076
18077 elsif Is_Init_Proc (Subp) then
18078 return False;
18079
18080 -- Do not test predicates on call to predicate function, since this
18081 -- would cause infinite recursion.
18082
18083 elsif Ekind (Subp) = E_Function
18084 and then (Is_Predicate_Function (Subp)
18085 or else
18086 Is_Predicate_Function_M (Subp))
18087 then
18088 return False;
18089
18090 -- For now, no other exceptions
18091
18092 else
18093 return True;
18094 end if;
18095 end Predicate_Tests_On_Arguments;
18096
18097 -----------------------
18098 -- Private_Component --
18099 -----------------------
18100
18101 function Private_Component (Type_Id : Entity_Id) return Entity_Id is
18102 Ancestor : constant Entity_Id := Base_Type (Type_Id);
18103
18104 function Trace_Components
18105 (T : Entity_Id;
18106 Check : Boolean) return Entity_Id;
18107 -- Recursive function that does the work, and checks against circular
18108 -- definition for each subcomponent type.
18109
18110 ----------------------
18111 -- Trace_Components --
18112 ----------------------
18113
18114 function Trace_Components
18115 (T : Entity_Id;
18116 Check : Boolean) return Entity_Id
18117 is
18118 Btype : constant Entity_Id := Base_Type (T);
18119 Component : Entity_Id;
18120 P : Entity_Id;
18121 Candidate : Entity_Id := Empty;
18122
18123 begin
18124 if Check and then Btype = Ancestor then
18125 Error_Msg_N ("circular type definition", Type_Id);
18126 return Any_Type;
18127 end if;
18128
18129 if Is_Private_Type (Btype) and then not Is_Generic_Type (Btype) then
18130 if Present (Full_View (Btype))
18131 and then Is_Record_Type (Full_View (Btype))
18132 and then not Is_Frozen (Btype)
18133 then
18134 -- To indicate that the ancestor depends on a private type, the
18135 -- current Btype is sufficient. However, to check for circular
18136 -- definition we must recurse on the full view.
18137
18138 Candidate := Trace_Components (Full_View (Btype), True);
18139
18140 if Candidate = Any_Type then
18141 return Any_Type;
18142 else
18143 return Btype;
18144 end if;
18145
18146 else
18147 return Btype;
18148 end if;
18149
18150 elsif Is_Array_Type (Btype) then
18151 return Trace_Components (Component_Type (Btype), True);
18152
18153 elsif Is_Record_Type (Btype) then
18154 Component := First_Entity (Btype);
18155 while Present (Component)
18156 and then Comes_From_Source (Component)
18157 loop
18158 -- Skip anonymous types generated by constrained components
18159
18160 if not Is_Type (Component) then
18161 P := Trace_Components (Etype (Component), True);
18162
18163 if Present (P) then
18164 if P = Any_Type then
18165 return P;
18166 else
18167 Candidate := P;
18168 end if;
18169 end if;
18170 end if;
18171
18172 Next_Entity (Component);
18173 end loop;
18174
18175 return Candidate;
18176
18177 else
18178 return Empty;
18179 end if;
18180 end Trace_Components;
18181
18182 -- Start of processing for Private_Component
18183
18184 begin
18185 return Trace_Components (Type_Id, False);
18186 end Private_Component;
18187
18188 ---------------------------
18189 -- Primitive_Names_Match --
18190 ---------------------------
18191
18192 function Primitive_Names_Match (E1, E2 : Entity_Id) return Boolean is
18193 function Non_Internal_Name (E : Entity_Id) return Name_Id;
18194 -- Given an internal name, returns the corresponding non-internal name
18195
18196 ------------------------
18197 -- Non_Internal_Name --
18198 ------------------------
18199
18200 function Non_Internal_Name (E : Entity_Id) return Name_Id is
18201 begin
18202 Get_Name_String (Chars (E));
18203 Name_Len := Name_Len - 1;
18204 return Name_Find;
18205 end Non_Internal_Name;
18206
18207 -- Start of processing for Primitive_Names_Match
18208
18209 begin
18210 pragma Assert (Present (E1) and then Present (E2));
18211
18212 return Chars (E1) = Chars (E2)
18213 or else
18214 (not Is_Internal_Name (Chars (E1))
18215 and then Is_Internal_Name (Chars (E2))
18216 and then Non_Internal_Name (E2) = Chars (E1))
18217 or else
18218 (not Is_Internal_Name (Chars (E2))
18219 and then Is_Internal_Name (Chars (E1))
18220 and then Non_Internal_Name (E1) = Chars (E2))
18221 or else
18222 (Is_Predefined_Dispatching_Operation (E1)
18223 and then Is_Predefined_Dispatching_Operation (E2)
18224 and then Same_TSS (E1, E2))
18225 or else
18226 (Is_Init_Proc (E1) and then Is_Init_Proc (E2));
18227 end Primitive_Names_Match;
18228
18229 -----------------------
18230 -- Process_End_Label --
18231 -----------------------
18232
18233 procedure Process_End_Label
18234 (N : Node_Id;
18235 Typ : Character;
18236 Ent : Entity_Id)
18237 is
18238 Loc : Source_Ptr;
18239 Nam : Node_Id;
18240 Scop : Entity_Id;
18241
18242 Label_Ref : Boolean;
18243 -- Set True if reference to end label itself is required
18244
18245 Endl : Node_Id;
18246 -- Gets set to the operator symbol or identifier that references the
18247 -- entity Ent. For the child unit case, this is the identifier from the
18248 -- designator. For other cases, this is simply Endl.
18249
18250 procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id);
18251 -- N is an identifier node that appears as a parent unit reference in
18252 -- the case where Ent is a child unit. This procedure generates an
18253 -- appropriate cross-reference entry. E is the corresponding entity.
18254
18255 -------------------------
18256 -- Generate_Parent_Ref --
18257 -------------------------
18258
18259 procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id) is
18260 begin
18261 -- If names do not match, something weird, skip reference
18262
18263 if Chars (E) = Chars (N) then
18264
18265 -- Generate the reference. We do NOT consider this as a reference
18266 -- for unreferenced symbol purposes.
18267
18268 Generate_Reference (E, N, 'r', Set_Ref => False, Force => True);
18269
18270 if Style_Check then
18271 Style.Check_Identifier (N, E);
18272 end if;
18273 end if;
18274 end Generate_Parent_Ref;
18275
18276 -- Start of processing for Process_End_Label
18277
18278 begin
18279 -- If no node, ignore. This happens in some error situations, and
18280 -- also for some internally generated structures where no end label
18281 -- references are required in any case.
18282
18283 if No (N) then
18284 return;
18285 end if;
18286
18287 -- Nothing to do if no End_Label, happens for internally generated
18288 -- constructs where we don't want an end label reference anyway. Also
18289 -- nothing to do if Endl is a string literal, which means there was
18290 -- some prior error (bad operator symbol)
18291
18292 Endl := End_Label (N);
18293
18294 if No (Endl) or else Nkind (Endl) = N_String_Literal then
18295 return;
18296 end if;
18297
18298 -- Reference node is not in extended main source unit
18299
18300 if not In_Extended_Main_Source_Unit (N) then
18301
18302 -- Generally we do not collect references except for the extended
18303 -- main source unit. The one exception is the 'e' entry for a
18304 -- package spec, where it is useful for a client to have the
18305 -- ending information to define scopes.
18306
18307 if Typ /= 'e' then
18308 return;
18309
18310 else
18311 Label_Ref := False;
18312
18313 -- For this case, we can ignore any parent references, but we
18314 -- need the package name itself for the 'e' entry.
18315
18316 if Nkind (Endl) = N_Designator then
18317 Endl := Identifier (Endl);
18318 end if;
18319 end if;
18320
18321 -- Reference is in extended main source unit
18322
18323 else
18324 Label_Ref := True;
18325
18326 -- For designator, generate references for the parent entries
18327
18328 if Nkind (Endl) = N_Designator then
18329
18330 -- Generate references for the prefix if the END line comes from
18331 -- source (otherwise we do not need these references) We climb the
18332 -- scope stack to find the expected entities.
18333
18334 if Comes_From_Source (Endl) then
18335 Nam := Name (Endl);
18336 Scop := Current_Scope;
18337 while Nkind (Nam) = N_Selected_Component loop
18338 Scop := Scope (Scop);
18339 exit when No (Scop);
18340 Generate_Parent_Ref (Selector_Name (Nam), Scop);
18341 Nam := Prefix (Nam);
18342 end loop;
18343
18344 if Present (Scop) then
18345 Generate_Parent_Ref (Nam, Scope (Scop));
18346 end if;
18347 end if;
18348
18349 Endl := Identifier (Endl);
18350 end if;
18351 end if;
18352
18353 -- If the end label is not for the given entity, then either we have
18354 -- some previous error, or this is a generic instantiation for which
18355 -- we do not need to make a cross-reference in this case anyway. In
18356 -- either case we simply ignore the call.
18357
18358 if Chars (Ent) /= Chars (Endl) then
18359 return;
18360 end if;
18361
18362 -- If label was really there, then generate a normal reference and then
18363 -- adjust the location in the end label to point past the name (which
18364 -- should almost always be the semicolon).
18365
18366 Loc := Sloc (Endl);
18367
18368 if Comes_From_Source (Endl) then
18369
18370 -- If a label reference is required, then do the style check and
18371 -- generate an l-type cross-reference entry for the label
18372
18373 if Label_Ref then
18374 if Style_Check then
18375 Style.Check_Identifier (Endl, Ent);
18376 end if;
18377
18378 Generate_Reference (Ent, Endl, 'l', Set_Ref => False);
18379 end if;
18380
18381 -- Set the location to point past the label (normally this will
18382 -- mean the semicolon immediately following the label). This is
18383 -- done for the sake of the 'e' or 't' entry generated below.
18384
18385 Get_Decoded_Name_String (Chars (Endl));
18386 Set_Sloc (Endl, Sloc (Endl) + Source_Ptr (Name_Len));
18387
18388 else
18389 -- In SPARK mode, no missing label is allowed for packages and
18390 -- subprogram bodies. Detect those cases by testing whether
18391 -- Process_End_Label was called for a body (Typ = 't') or a package.
18392
18393 if Restriction_Check_Required (SPARK_05)
18394 and then (Typ = 't' or else Ekind (Ent) = E_Package)
18395 then
18396 Error_Msg_Node_1 := Endl;
18397 Check_SPARK_05_Restriction
18398 ("`END &` required", Endl, Force => True);
18399 end if;
18400 end if;
18401
18402 -- Now generate the e/t reference
18403
18404 Generate_Reference (Ent, Endl, Typ, Set_Ref => False, Force => True);
18405
18406 -- Restore Sloc, in case modified above, since we have an identifier
18407 -- and the normal Sloc should be left set in the tree.
18408
18409 Set_Sloc (Endl, Loc);
18410 end Process_End_Label;
18411
18412 --------------------------------
18413 -- Propagate_Concurrent_Flags --
18414 --------------------------------
18415
18416 procedure Propagate_Concurrent_Flags
18417 (Typ : Entity_Id;
18418 Comp_Typ : Entity_Id)
18419 is
18420 begin
18421 if Has_Task (Comp_Typ) then
18422 Set_Has_Task (Typ);
18423 end if;
18424
18425 if Has_Protected (Comp_Typ) then
18426 Set_Has_Protected (Typ);
18427 end if;
18428
18429 if Has_Timing_Event (Comp_Typ) then
18430 Set_Has_Timing_Event (Typ);
18431 end if;
18432 end Propagate_Concurrent_Flags;
18433
18434 ------------------------------
18435 -- Propagate_DIC_Attributes --
18436 ------------------------------
18437
18438 procedure Propagate_DIC_Attributes
18439 (Typ : Entity_Id;
18440 From_Typ : Entity_Id)
18441 is
18442 DIC_Proc : Entity_Id;
18443
18444 begin
18445 if Present (Typ) and then Present (From_Typ) then
18446 pragma Assert (Is_Type (Typ) and then Is_Type (From_Typ));
18447
18448 -- Nothing to do if both the source and the destination denote the
18449 -- same type.
18450
18451 if From_Typ = Typ then
18452 return;
18453 end if;
18454
18455 DIC_Proc := DIC_Procedure (From_Typ);
18456
18457 -- The setting of the attributes is intentionally conservative. This
18458 -- prevents accidental clobbering of enabled attributes.
18459
18460 if Has_Inherited_DIC (From_Typ)
18461 and then not Has_Inherited_DIC (Typ)
18462 then
18463 Set_Has_Inherited_DIC (Typ);
18464 end if;
18465
18466 if Has_Own_DIC (From_Typ) and then not Has_Own_DIC (Typ) then
18467 Set_Has_Own_DIC (Typ);
18468 end if;
18469
18470 if Present (DIC_Proc) and then No (DIC_Procedure (Typ)) then
18471 Set_DIC_Procedure (Typ, DIC_Proc);
18472 end if;
18473 end if;
18474 end Propagate_DIC_Attributes;
18475
18476 ------------------------------------
18477 -- Propagate_Invariant_Attributes --
18478 ------------------------------------
18479
18480 procedure Propagate_Invariant_Attributes
18481 (Typ : Entity_Id;
18482 From_Typ : Entity_Id)
18483 is
18484 Full_IP : Entity_Id;
18485 Part_IP : Entity_Id;
18486
18487 begin
18488 if Present (Typ) and then Present (From_Typ) then
18489 pragma Assert (Is_Type (Typ) and then Is_Type (From_Typ));
18490
18491 -- Nothing to do if both the source and the destination denote the
18492 -- same type.
18493
18494 if From_Typ = Typ then
18495 return;
18496 end if;
18497
18498 Full_IP := Invariant_Procedure (From_Typ);
18499 Part_IP := Partial_Invariant_Procedure (From_Typ);
18500
18501 -- The setting of the attributes is intentionally conservative. This
18502 -- prevents accidental clobbering of enabled attributes.
18503
18504 if Has_Inheritable_Invariants (From_Typ)
18505 and then not Has_Inheritable_Invariants (Typ)
18506 then
18507 Set_Has_Inheritable_Invariants (Typ, True);
18508 end if;
18509
18510 if Has_Inherited_Invariants (From_Typ)
18511 and then not Has_Inherited_Invariants (Typ)
18512 then
18513 Set_Has_Inherited_Invariants (Typ, True);
18514 end if;
18515
18516 if Has_Own_Invariants (From_Typ)
18517 and then not Has_Own_Invariants (Typ)
18518 then
18519 Set_Has_Own_Invariants (Typ, True);
18520 end if;
18521
18522 if Present (Full_IP) and then No (Invariant_Procedure (Typ)) then
18523 Set_Invariant_Procedure (Typ, Full_IP);
18524 end if;
18525
18526 if Present (Part_IP) and then No (Partial_Invariant_Procedure (Typ))
18527 then
18528 Set_Partial_Invariant_Procedure (Typ, Part_IP);
18529 end if;
18530 end if;
18531 end Propagate_Invariant_Attributes;
18532
18533 ---------------------------------------
18534 -- Record_Possible_Part_Of_Reference --
18535 ---------------------------------------
18536
18537 procedure Record_Possible_Part_Of_Reference
18538 (Var_Id : Entity_Id;
18539 Ref : Node_Id)
18540 is
18541 Encap : constant Entity_Id := Encapsulating_State (Var_Id);
18542 Refs : Elist_Id;
18543
18544 begin
18545 -- The variable is a constituent of a single protected/task type. Such
18546 -- a variable acts as a component of the type and must appear within a
18547 -- specific region (SPARK RM 9.3). Instead of recording the reference,
18548 -- verify its legality now.
18549
18550 if Present (Encap) and then Is_Single_Concurrent_Object (Encap) then
18551 Check_Part_Of_Reference (Var_Id, Ref);
18552
18553 -- The variable is subject to pragma Part_Of and may eventually become a
18554 -- constituent of a single protected/task type. Record the reference to
18555 -- verify its placement when the contract of the variable is analyzed.
18556
18557 elsif Present (Get_Pragma (Var_Id, Pragma_Part_Of)) then
18558 Refs := Part_Of_References (Var_Id);
18559
18560 if No (Refs) then
18561 Refs := New_Elmt_List;
18562 Set_Part_Of_References (Var_Id, Refs);
18563 end if;
18564
18565 Append_Elmt (Ref, Refs);
18566 end if;
18567 end Record_Possible_Part_Of_Reference;
18568
18569 ----------------
18570 -- Referenced --
18571 ----------------
18572
18573 function Referenced (Id : Entity_Id; Expr : Node_Id) return Boolean is
18574 Seen : Boolean := False;
18575
18576 function Is_Reference (N : Node_Id) return Traverse_Result;
18577 -- Determine whether node N denotes a reference to Id. If this is the
18578 -- case, set global flag Seen to True and stop the traversal.
18579
18580 ------------------
18581 -- Is_Reference --
18582 ------------------
18583
18584 function Is_Reference (N : Node_Id) return Traverse_Result is
18585 begin
18586 if Is_Entity_Name (N)
18587 and then Present (Entity (N))
18588 and then Entity (N) = Id
18589 then
18590 Seen := True;
18591 return Abandon;
18592 else
18593 return OK;
18594 end if;
18595 end Is_Reference;
18596
18597 procedure Inspect_Expression is new Traverse_Proc (Is_Reference);
18598
18599 -- Start of processing for Referenced
18600
18601 begin
18602 Inspect_Expression (Expr);
18603 return Seen;
18604 end Referenced;
18605
18606 ------------------------------------
18607 -- References_Generic_Formal_Type --
18608 ------------------------------------
18609
18610 function References_Generic_Formal_Type (N : Node_Id) return Boolean is
18611
18612 function Process (N : Node_Id) return Traverse_Result;
18613 -- Process one node in search for generic formal type
18614
18615 -------------
18616 -- Process --
18617 -------------
18618
18619 function Process (N : Node_Id) return Traverse_Result is
18620 begin
18621 if Nkind (N) in N_Has_Entity then
18622 declare
18623 E : constant Entity_Id := Entity (N);
18624 begin
18625 if Present (E) then
18626 if Is_Generic_Type (E) then
18627 return Abandon;
18628 elsif Present (Etype (E))
18629 and then Is_Generic_Type (Etype (E))
18630 then
18631 return Abandon;
18632 end if;
18633 end if;
18634 end;
18635 end if;
18636
18637 return Atree.OK;
18638 end Process;
18639
18640 function Traverse is new Traverse_Func (Process);
18641 -- Traverse tree to look for generic type
18642
18643 begin
18644 if Inside_A_Generic then
18645 return Traverse (N) = Abandon;
18646 else
18647 return False;
18648 end if;
18649 end References_Generic_Formal_Type;
18650
18651 --------------------
18652 -- Remove_Homonym --
18653 --------------------
18654
18655 procedure Remove_Homonym (E : Entity_Id) is
18656 Prev : Entity_Id := Empty;
18657 H : Entity_Id;
18658
18659 begin
18660 if E = Current_Entity (E) then
18661 if Present (Homonym (E)) then
18662 Set_Current_Entity (Homonym (E));
18663 else
18664 Set_Name_Entity_Id (Chars (E), Empty);
18665 end if;
18666
18667 else
18668 H := Current_Entity (E);
18669 while Present (H) and then H /= E loop
18670 Prev := H;
18671 H := Homonym (H);
18672 end loop;
18673
18674 -- If E is not on the homonym chain, nothing to do
18675
18676 if Present (H) then
18677 Set_Homonym (Prev, Homonym (E));
18678 end if;
18679 end if;
18680 end Remove_Homonym;
18681
18682 ------------------------------
18683 -- Remove_Overloaded_Entity --
18684 ------------------------------
18685
18686 procedure Remove_Overloaded_Entity (Id : Entity_Id) is
18687 procedure Remove_Primitive_Of (Typ : Entity_Id);
18688 -- Remove primitive subprogram Id from the list of primitives that
18689 -- belong to type Typ.
18690
18691 -------------------------
18692 -- Remove_Primitive_Of --
18693 -------------------------
18694
18695 procedure Remove_Primitive_Of (Typ : Entity_Id) is
18696 Prims : Elist_Id;
18697
18698 begin
18699 if Is_Tagged_Type (Typ) then
18700 Prims := Direct_Primitive_Operations (Typ);
18701
18702 if Present (Prims) then
18703 Remove (Prims, Id);
18704 end if;
18705 end if;
18706 end Remove_Primitive_Of;
18707
18708 -- Local variables
18709
18710 Scop : constant Entity_Id := Scope (Id);
18711 Formal : Entity_Id;
18712 Prev_Id : Entity_Id;
18713
18714 -- Start of processing for Remove_Overloaded_Entity
18715
18716 begin
18717 -- Remove the entity from the homonym chain. When the entity is the
18718 -- head of the chain, associate the entry in the name table with its
18719 -- homonym effectively making it the new head of the chain.
18720
18721 if Current_Entity (Id) = Id then
18722 Set_Name_Entity_Id (Chars (Id), Homonym (Id));
18723
18724 -- Otherwise link the previous and next homonyms
18725
18726 else
18727 Prev_Id := Current_Entity (Id);
18728 while Present (Prev_Id) and then Homonym (Prev_Id) /= Id loop
18729 Prev_Id := Homonym (Prev_Id);
18730 end loop;
18731
18732 Set_Homonym (Prev_Id, Homonym (Id));
18733 end if;
18734
18735 -- Remove the entity from the scope entity chain. When the entity is
18736 -- the head of the chain, set the next entity as the new head of the
18737 -- chain.
18738
18739 if First_Entity (Scop) = Id then
18740 Prev_Id := Empty;
18741 Set_First_Entity (Scop, Next_Entity (Id));
18742
18743 -- Otherwise the entity is either in the middle of the chain or it acts
18744 -- as its tail. Traverse and link the previous and next entities.
18745
18746 else
18747 Prev_Id := First_Entity (Scop);
18748 while Present (Prev_Id) and then Next_Entity (Prev_Id) /= Id loop
18749 Next_Entity (Prev_Id);
18750 end loop;
18751
18752 Set_Next_Entity (Prev_Id, Next_Entity (Id));
18753 end if;
18754
18755 -- Handle the case where the entity acts as the tail of the scope entity
18756 -- chain.
18757
18758 if Last_Entity (Scop) = Id then
18759 Set_Last_Entity (Scop, Prev_Id);
18760 end if;
18761
18762 -- The entity denotes a primitive subprogram. Remove it from the list of
18763 -- primitives of the associated controlling type.
18764
18765 if Ekind_In (Id, E_Function, E_Procedure) and then Is_Primitive (Id) then
18766 Formal := First_Formal (Id);
18767 while Present (Formal) loop
18768 if Is_Controlling_Formal (Formal) then
18769 Remove_Primitive_Of (Etype (Formal));
18770 exit;
18771 end if;
18772
18773 Next_Formal (Formal);
18774 end loop;
18775
18776 if Ekind (Id) = E_Function and then Has_Controlling_Result (Id) then
18777 Remove_Primitive_Of (Etype (Id));
18778 end if;
18779 end if;
18780 end Remove_Overloaded_Entity;
18781
18782 ---------------------
18783 -- Rep_To_Pos_Flag --
18784 ---------------------
18785
18786 function Rep_To_Pos_Flag (E : Entity_Id; Loc : Source_Ptr) return Node_Id is
18787 begin
18788 return New_Occurrence_Of
18789 (Boolean_Literals (not Range_Checks_Suppressed (E)), Loc);
18790 end Rep_To_Pos_Flag;
18791
18792 --------------------
18793 -- Require_Entity --
18794 --------------------
18795
18796 procedure Require_Entity (N : Node_Id) is
18797 begin
18798 if Is_Entity_Name (N) and then No (Entity (N)) then
18799 if Total_Errors_Detected /= 0 then
18800 Set_Entity (N, Any_Id);
18801 else
18802 raise Program_Error;
18803 end if;
18804 end if;
18805 end Require_Entity;
18806
18807 ------------------------------
18808 -- Requires_Transient_Scope --
18809 ------------------------------
18810
18811 -- A transient scope is required when variable-sized temporaries are
18812 -- allocated on the secondary stack, or when finalization actions must be
18813 -- generated before the next instruction.
18814
18815 function Old_Requires_Transient_Scope (Id : Entity_Id) return Boolean;
18816 function New_Requires_Transient_Scope (Id : Entity_Id) return Boolean;
18817 -- ???We retain the old and new algorithms for Requires_Transient_Scope for
18818 -- the time being. New_Requires_Transient_Scope is used by default; the
18819 -- debug switch -gnatdQ can be used to do Old_Requires_Transient_Scope
18820 -- instead. The intent is to use this temporarily to measure before/after
18821 -- efficiency. Note: when this temporary code is removed, the documentation
18822 -- of dQ in debug.adb should be removed.
18823
18824 procedure Results_Differ (Id : Entity_Id);
18825 -- ???Debugging code. Called when the Old_ and New_ results differ. Will be
18826 -- removed when New_Requires_Transient_Scope becomes
18827 -- Requires_Transient_Scope and Old_Requires_Transient_Scope is eliminated.
18828
18829 procedure Results_Differ (Id : Entity_Id) is
18830 begin
18831 if False then -- False to disable; True for debugging
18832 Treepr.Print_Tree_Node (Id);
18833
18834 if Old_Requires_Transient_Scope (Id) =
18835 New_Requires_Transient_Scope (Id)
18836 then
18837 raise Program_Error;
18838 end if;
18839 end if;
18840 end Results_Differ;
18841
18842 function Requires_Transient_Scope (Id : Entity_Id) return Boolean is
18843 Old_Result : constant Boolean := Old_Requires_Transient_Scope (Id);
18844
18845 begin
18846 if Debug_Flag_QQ then
18847 return Old_Result;
18848 end if;
18849
18850 declare
18851 New_Result : constant Boolean := New_Requires_Transient_Scope (Id);
18852
18853 begin
18854 -- Assert that we're not putting things on the secondary stack if we
18855 -- didn't before; we are trying to AVOID secondary stack when
18856 -- possible.
18857
18858 if not Old_Result then
18859 pragma Assert (not New_Result);
18860 null;
18861 end if;
18862
18863 if New_Result /= Old_Result then
18864 Results_Differ (Id);
18865 end if;
18866
18867 return New_Result;
18868 end;
18869 end Requires_Transient_Scope;
18870
18871 ----------------------------------
18872 -- Old_Requires_Transient_Scope --
18873 ----------------------------------
18874
18875 function Old_Requires_Transient_Scope (Id : Entity_Id) return Boolean is
18876 Typ : constant Entity_Id := Underlying_Type (Id);
18877
18878 begin
18879 -- This is a private type which is not completed yet. This can only
18880 -- happen in a default expression (of a formal parameter or of a
18881 -- record component). Do not expand transient scope in this case.
18882
18883 if No (Typ) then
18884 return False;
18885
18886 -- Do not expand transient scope for non-existent procedure return
18887
18888 elsif Typ = Standard_Void_Type then
18889 return False;
18890
18891 -- Elementary types do not require a transient scope
18892
18893 elsif Is_Elementary_Type (Typ) then
18894 return False;
18895
18896 -- Generally, indefinite subtypes require a transient scope, since the
18897 -- back end cannot generate temporaries, since this is not a valid type
18898 -- for declaring an object. It might be possible to relax this in the
18899 -- future, e.g. by declaring the maximum possible space for the type.
18900
18901 elsif not Is_Definite_Subtype (Typ) then
18902 return True;
18903
18904 -- Functions returning tagged types may dispatch on result so their
18905 -- returned value is allocated on the secondary stack. Controlled
18906 -- type temporaries need finalization.
18907
18908 elsif Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
18909 return True;
18910
18911 -- Record type
18912
18913 elsif Is_Record_Type (Typ) then
18914 declare
18915 Comp : Entity_Id;
18916
18917 begin
18918 Comp := First_Entity (Typ);
18919 while Present (Comp) loop
18920 if Ekind (Comp) = E_Component then
18921
18922 -- ???It's not clear we need a full recursive call to
18923 -- Old_Requires_Transient_Scope here. Note that the
18924 -- following can't happen.
18925
18926 pragma Assert (Is_Definite_Subtype (Etype (Comp)));
18927 pragma Assert (not Has_Controlled_Component (Etype (Comp)));
18928
18929 if Old_Requires_Transient_Scope (Etype (Comp)) then
18930 return True;
18931 end if;
18932 end if;
18933
18934 Next_Entity (Comp);
18935 end loop;
18936 end;
18937
18938 return False;
18939
18940 -- String literal types never require transient scope
18941
18942 elsif Ekind (Typ) = E_String_Literal_Subtype then
18943 return False;
18944
18945 -- Array type. Note that we already know that this is a constrained
18946 -- array, since unconstrained arrays will fail the indefinite test.
18947
18948 elsif Is_Array_Type (Typ) then
18949
18950 -- If component type requires a transient scope, the array does too
18951
18952 if Old_Requires_Transient_Scope (Component_Type (Typ)) then
18953 return True;
18954
18955 -- Otherwise, we only need a transient scope if the size depends on
18956 -- the value of one or more discriminants.
18957
18958 else
18959 return Size_Depends_On_Discriminant (Typ);
18960 end if;
18961
18962 -- All other cases do not require a transient scope
18963
18964 else
18965 pragma Assert (Is_Protected_Type (Typ) or else Is_Task_Type (Typ));
18966 return False;
18967 end if;
18968 end Old_Requires_Transient_Scope;
18969
18970 ----------------------------------
18971 -- New_Requires_Transient_Scope --
18972 ----------------------------------
18973
18974 function New_Requires_Transient_Scope (Id : Entity_Id) return Boolean is
18975
18976 function Caller_Known_Size_Record (Typ : Entity_Id) return Boolean;
18977 -- This is called for untagged records and protected types, with
18978 -- nondefaulted discriminants. Returns True if the size of function
18979 -- results is known at the call site, False otherwise. Returns False
18980 -- if there is a variant part that depends on the discriminants of
18981 -- this type, or if there is an array constrained by the discriminants
18982 -- of this type. ???Currently, this is overly conservative (the array
18983 -- could be nested inside some other record that is constrained by
18984 -- nondiscriminants). That is, the recursive calls are too conservative.
18985
18986 function Large_Max_Size_Mutable (Typ : Entity_Id) return Boolean;
18987 -- Returns True if Typ is a nonlimited record with defaulted
18988 -- discriminants whose max size makes it unsuitable for allocating on
18989 -- the primary stack.
18990
18991 ------------------------------
18992 -- Caller_Known_Size_Record --
18993 ------------------------------
18994
18995 function Caller_Known_Size_Record (Typ : Entity_Id) return Boolean is
18996 pragma Assert (Typ = Underlying_Type (Typ));
18997
18998 begin
18999 if Has_Variant_Part (Typ) and then not Is_Definite_Subtype (Typ) then
19000 return False;
19001 end if;
19002
19003 declare
19004 Comp : Entity_Id;
19005
19006 begin
19007 Comp := First_Entity (Typ);
19008 while Present (Comp) loop
19009
19010 -- Only look at E_Component entities. No need to look at
19011 -- E_Discriminant entities, and we must ignore internal
19012 -- subtypes generated for constrained components.
19013
19014 if Ekind (Comp) = E_Component then
19015 declare
19016 Comp_Type : constant Entity_Id :=
19017 Underlying_Type (Etype (Comp));
19018
19019 begin
19020 if Is_Record_Type (Comp_Type)
19021 or else
19022 Is_Protected_Type (Comp_Type)
19023 then
19024 if not Caller_Known_Size_Record (Comp_Type) then
19025 return False;
19026 end if;
19027
19028 elsif Is_Array_Type (Comp_Type) then
19029 if Size_Depends_On_Discriminant (Comp_Type) then
19030 return False;
19031 end if;
19032 end if;
19033 end;
19034 end if;
19035
19036 Next_Entity (Comp);
19037 end loop;
19038 end;
19039
19040 return True;
19041 end Caller_Known_Size_Record;
19042
19043 ------------------------------
19044 -- Large_Max_Size_Mutable --
19045 ------------------------------
19046
19047 function Large_Max_Size_Mutable (Typ : Entity_Id) return Boolean is
19048 pragma Assert (Typ = Underlying_Type (Typ));
19049
19050 function Is_Large_Discrete_Type (T : Entity_Id) return Boolean;
19051 -- Returns true if the discrete type T has a large range
19052
19053 ----------------------------
19054 -- Is_Large_Discrete_Type --
19055 ----------------------------
19056
19057 function Is_Large_Discrete_Type (T : Entity_Id) return Boolean is
19058 Threshold : constant Int := 16;
19059 -- Arbitrary threshold above which we consider it "large". We want
19060 -- a fairly large threshold, because these large types really
19061 -- shouldn't have default discriminants in the first place, in
19062 -- most cases.
19063
19064 begin
19065 return UI_To_Int (RM_Size (T)) > Threshold;
19066 end Is_Large_Discrete_Type;
19067
19068 begin
19069 if Is_Record_Type (Typ)
19070 and then not Is_Limited_View (Typ)
19071 and then Has_Defaulted_Discriminants (Typ)
19072 then
19073 -- Loop through the components, looking for an array whose upper
19074 -- bound(s) depends on discriminants, where both the subtype of
19075 -- the discriminant and the index subtype are too large.
19076
19077 declare
19078 Comp : Entity_Id;
19079
19080 begin
19081 Comp := First_Entity (Typ);
19082 while Present (Comp) loop
19083 if Ekind (Comp) = E_Component then
19084 declare
19085 Comp_Type : constant Entity_Id :=
19086 Underlying_Type (Etype (Comp));
19087 Indx : Node_Id;
19088 Ityp : Entity_Id;
19089 Hi : Node_Id;
19090
19091 begin
19092 if Is_Array_Type (Comp_Type) then
19093 Indx := First_Index (Comp_Type);
19094
19095 while Present (Indx) loop
19096 Ityp := Etype (Indx);
19097 Hi := Type_High_Bound (Ityp);
19098
19099 if Nkind (Hi) = N_Identifier
19100 and then Ekind (Entity (Hi)) = E_Discriminant
19101 and then Is_Large_Discrete_Type (Ityp)
19102 and then Is_Large_Discrete_Type
19103 (Etype (Entity (Hi)))
19104 then
19105 return True;
19106 end if;
19107
19108 Next_Index (Indx);
19109 end loop;
19110 end if;
19111 end;
19112 end if;
19113
19114 Next_Entity (Comp);
19115 end loop;
19116 end;
19117 end if;
19118
19119 return False;
19120 end Large_Max_Size_Mutable;
19121
19122 -- Local declarations
19123
19124 Typ : constant Entity_Id := Underlying_Type (Id);
19125
19126 -- Start of processing for New_Requires_Transient_Scope
19127
19128 begin
19129 -- This is a private type which is not completed yet. This can only
19130 -- happen in a default expression (of a formal parameter or of a
19131 -- record component). Do not expand transient scope in this case.
19132
19133 if No (Typ) then
19134 return False;
19135
19136 -- Do not expand transient scope for non-existent procedure return or
19137 -- string literal types.
19138
19139 elsif Typ = Standard_Void_Type
19140 or else Ekind (Typ) = E_String_Literal_Subtype
19141 then
19142 return False;
19143
19144 -- If Typ is a generic formal incomplete type, then we want to look at
19145 -- the actual type.
19146
19147 elsif Ekind (Typ) = E_Record_Subtype
19148 and then Present (Cloned_Subtype (Typ))
19149 then
19150 return New_Requires_Transient_Scope (Cloned_Subtype (Typ));
19151
19152 -- Functions returning specific tagged types may dispatch on result, so
19153 -- their returned value is allocated on the secondary stack, even in the
19154 -- definite case. We must treat nondispatching functions the same way,
19155 -- because access-to-function types can point at both, so the calling
19156 -- conventions must be compatible. Is_Tagged_Type includes controlled
19157 -- types and class-wide types. Controlled type temporaries need
19158 -- finalization.
19159
19160 -- ???It's not clear why we need to return noncontrolled types with
19161 -- controlled components on the secondary stack.
19162
19163 elsif Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
19164 return True;
19165
19166 -- Untagged definite subtypes are known size. This includes all
19167 -- elementary [sub]types. Tasks are known size even if they have
19168 -- discriminants. So we return False here, with one exception:
19169 -- For a type like:
19170 -- type T (Last : Natural := 0) is
19171 -- X : String (1 .. Last);
19172 -- end record;
19173 -- we return True. That's because for "P(F(...));", where F returns T,
19174 -- we don't know the size of the result at the call site, so if we
19175 -- allocated it on the primary stack, we would have to allocate the
19176 -- maximum size, which is way too big.
19177
19178 elsif Is_Definite_Subtype (Typ) or else Is_Task_Type (Typ) then
19179 return Large_Max_Size_Mutable (Typ);
19180
19181 -- Indefinite (discriminated) untagged record or protected type
19182
19183 elsif Is_Record_Type (Typ) or else Is_Protected_Type (Typ) then
19184 return not Caller_Known_Size_Record (Typ);
19185
19186 -- Unconstrained array
19187
19188 else
19189 pragma Assert (Is_Array_Type (Typ) and not Is_Definite_Subtype (Typ));
19190 return True;
19191 end if;
19192 end New_Requires_Transient_Scope;
19193
19194 --------------------------
19195 -- Reset_Analyzed_Flags --
19196 --------------------------
19197
19198 procedure Reset_Analyzed_Flags (N : Node_Id) is
19199
19200 function Clear_Analyzed (N : Node_Id) return Traverse_Result;
19201 -- Function used to reset Analyzed flags in tree. Note that we do
19202 -- not reset Analyzed flags in entities, since there is no need to
19203 -- reanalyze entities, and indeed, it is wrong to do so, since it
19204 -- can result in generating auxiliary stuff more than once.
19205
19206 --------------------
19207 -- Clear_Analyzed --
19208 --------------------
19209
19210 function Clear_Analyzed (N : Node_Id) return Traverse_Result is
19211 begin
19212 if not Has_Extension (N) then
19213 Set_Analyzed (N, False);
19214 end if;
19215
19216 return OK;
19217 end Clear_Analyzed;
19218
19219 procedure Reset_Analyzed is new Traverse_Proc (Clear_Analyzed);
19220
19221 -- Start of processing for Reset_Analyzed_Flags
19222
19223 begin
19224 Reset_Analyzed (N);
19225 end Reset_Analyzed_Flags;
19226
19227 ------------------------
19228 -- Restore_SPARK_Mode --
19229 ------------------------
19230
19231 procedure Restore_SPARK_Mode (Mode : SPARK_Mode_Type) is
19232 begin
19233 SPARK_Mode := Mode;
19234 end Restore_SPARK_Mode;
19235
19236 --------------------------------
19237 -- Returns_Unconstrained_Type --
19238 --------------------------------
19239
19240 function Returns_Unconstrained_Type (Subp : Entity_Id) return Boolean is
19241 begin
19242 return Ekind (Subp) = E_Function
19243 and then not Is_Scalar_Type (Etype (Subp))
19244 and then not Is_Access_Type (Etype (Subp))
19245 and then not Is_Constrained (Etype (Subp));
19246 end Returns_Unconstrained_Type;
19247
19248 ----------------------------
19249 -- Root_Type_Of_Full_View --
19250 ----------------------------
19251
19252 function Root_Type_Of_Full_View (T : Entity_Id) return Entity_Id is
19253 Rtyp : constant Entity_Id := Root_Type (T);
19254
19255 begin
19256 -- The root type of the full view may itself be a private type. Keep
19257 -- looking for the ultimate derivation parent.
19258
19259 if Is_Private_Type (Rtyp) and then Present (Full_View (Rtyp)) then
19260 return Root_Type_Of_Full_View (Full_View (Rtyp));
19261 else
19262 return Rtyp;
19263 end if;
19264 end Root_Type_Of_Full_View;
19265
19266 ---------------------------
19267 -- Safe_To_Capture_Value --
19268 ---------------------------
19269
19270 function Safe_To_Capture_Value
19271 (N : Node_Id;
19272 Ent : Entity_Id;
19273 Cond : Boolean := False) return Boolean
19274 is
19275 begin
19276 -- The only entities for which we track constant values are variables
19277 -- which are not renamings, constants, out parameters, and in out
19278 -- parameters, so check if we have this case.
19279
19280 -- Note: it may seem odd to track constant values for constants, but in
19281 -- fact this routine is used for other purposes than simply capturing
19282 -- the value. In particular, the setting of Known[_Non]_Null.
19283
19284 if (Ekind (Ent) = E_Variable and then No (Renamed_Object (Ent)))
19285 or else
19286 Ekind_In (Ent, E_Constant, E_Out_Parameter, E_In_Out_Parameter)
19287 then
19288 null;
19289
19290 -- For conditionals, we also allow loop parameters and all formals,
19291 -- including in parameters.
19292
19293 elsif Cond and then Ekind_In (Ent, E_Loop_Parameter, E_In_Parameter) then
19294 null;
19295
19296 -- For all other cases, not just unsafe, but impossible to capture
19297 -- Current_Value, since the above are the only entities which have
19298 -- Current_Value fields.
19299
19300 else
19301 return False;
19302 end if;
19303
19304 -- Skip if volatile or aliased, since funny things might be going on in
19305 -- these cases which we cannot necessarily track. Also skip any variable
19306 -- for which an address clause is given, or whose address is taken. Also
19307 -- never capture value of library level variables (an attempt to do so
19308 -- can occur in the case of package elaboration code).
19309
19310 if Treat_As_Volatile (Ent)
19311 or else Is_Aliased (Ent)
19312 or else Present (Address_Clause (Ent))
19313 or else Address_Taken (Ent)
19314 or else (Is_Library_Level_Entity (Ent)
19315 and then Ekind (Ent) = E_Variable)
19316 then
19317 return False;
19318 end if;
19319
19320 -- OK, all above conditions are met. We also require that the scope of
19321 -- the reference be the same as the scope of the entity, not counting
19322 -- packages and blocks and loops.
19323
19324 declare
19325 E_Scope : constant Entity_Id := Scope (Ent);
19326 R_Scope : Entity_Id;
19327
19328 begin
19329 R_Scope := Current_Scope;
19330 while R_Scope /= Standard_Standard loop
19331 exit when R_Scope = E_Scope;
19332
19333 if not Ekind_In (R_Scope, E_Package, E_Block, E_Loop) then
19334 return False;
19335 else
19336 R_Scope := Scope (R_Scope);
19337 end if;
19338 end loop;
19339 end;
19340
19341 -- We also require that the reference does not appear in a context
19342 -- where it is not sure to be executed (i.e. a conditional context
19343 -- or an exception handler). We skip this if Cond is True, since the
19344 -- capturing of values from conditional tests handles this ok.
19345
19346 if Cond then
19347 return True;
19348 end if;
19349
19350 declare
19351 Desc : Node_Id;
19352 P : Node_Id;
19353
19354 begin
19355 Desc := N;
19356
19357 -- Seems dubious that case expressions are not handled here ???
19358
19359 P := Parent (N);
19360 while Present (P) loop
19361 if Nkind (P) = N_If_Statement
19362 or else Nkind (P) = N_Case_Statement
19363 or else (Nkind (P) in N_Short_Circuit
19364 and then Desc = Right_Opnd (P))
19365 or else (Nkind (P) = N_If_Expression
19366 and then Desc /= First (Expressions (P)))
19367 or else Nkind (P) = N_Exception_Handler
19368 or else Nkind (P) = N_Selective_Accept
19369 or else Nkind (P) = N_Conditional_Entry_Call
19370 or else Nkind (P) = N_Timed_Entry_Call
19371 or else Nkind (P) = N_Asynchronous_Select
19372 then
19373 return False;
19374
19375 else
19376 Desc := P;
19377 P := Parent (P);
19378
19379 -- A special Ada 2012 case: the original node may be part
19380 -- of the else_actions of a conditional expression, in which
19381 -- case it might not have been expanded yet, and appears in
19382 -- a non-syntactic list of actions. In that case it is clearly
19383 -- not safe to save a value.
19384
19385 if No (P)
19386 and then Is_List_Member (Desc)
19387 and then No (Parent (List_Containing (Desc)))
19388 then
19389 return False;
19390 end if;
19391 end if;
19392 end loop;
19393 end;
19394
19395 -- OK, looks safe to set value
19396
19397 return True;
19398 end Safe_To_Capture_Value;
19399
19400 ---------------
19401 -- Same_Name --
19402 ---------------
19403
19404 function Same_Name (N1, N2 : Node_Id) return Boolean is
19405 K1 : constant Node_Kind := Nkind (N1);
19406 K2 : constant Node_Kind := Nkind (N2);
19407
19408 begin
19409 if (K1 = N_Identifier or else K1 = N_Defining_Identifier)
19410 and then (K2 = N_Identifier or else K2 = N_Defining_Identifier)
19411 then
19412 return Chars (N1) = Chars (N2);
19413
19414 elsif (K1 = N_Selected_Component or else K1 = N_Expanded_Name)
19415 and then (K2 = N_Selected_Component or else K2 = N_Expanded_Name)
19416 then
19417 return Same_Name (Selector_Name (N1), Selector_Name (N2))
19418 and then Same_Name (Prefix (N1), Prefix (N2));
19419
19420 else
19421 return False;
19422 end if;
19423 end Same_Name;
19424
19425 -----------------
19426 -- Same_Object --
19427 -----------------
19428
19429 function Same_Object (Node1, Node2 : Node_Id) return Boolean is
19430 N1 : constant Node_Id := Original_Node (Node1);
19431 N2 : constant Node_Id := Original_Node (Node2);
19432 -- We do the tests on original nodes, since we are most interested
19433 -- in the original source, not any expansion that got in the way.
19434
19435 K1 : constant Node_Kind := Nkind (N1);
19436 K2 : constant Node_Kind := Nkind (N2);
19437
19438 begin
19439 -- First case, both are entities with same entity
19440
19441 if K1 in N_Has_Entity and then K2 in N_Has_Entity then
19442 declare
19443 EN1 : constant Entity_Id := Entity (N1);
19444 EN2 : constant Entity_Id := Entity (N2);
19445 begin
19446 if Present (EN1) and then Present (EN2)
19447 and then (Ekind_In (EN1, E_Variable, E_Constant)
19448 or else Is_Formal (EN1))
19449 and then EN1 = EN2
19450 then
19451 return True;
19452 end if;
19453 end;
19454 end if;
19455
19456 -- Second case, selected component with same selector, same record
19457
19458 if K1 = N_Selected_Component
19459 and then K2 = N_Selected_Component
19460 and then Chars (Selector_Name (N1)) = Chars (Selector_Name (N2))
19461 then
19462 return Same_Object (Prefix (N1), Prefix (N2));
19463
19464 -- Third case, indexed component with same subscripts, same array
19465
19466 elsif K1 = N_Indexed_Component
19467 and then K2 = N_Indexed_Component
19468 and then Same_Object (Prefix (N1), Prefix (N2))
19469 then
19470 declare
19471 E1, E2 : Node_Id;
19472 begin
19473 E1 := First (Expressions (N1));
19474 E2 := First (Expressions (N2));
19475 while Present (E1) loop
19476 if not Same_Value (E1, E2) then
19477 return False;
19478 else
19479 Next (E1);
19480 Next (E2);
19481 end if;
19482 end loop;
19483
19484 return True;
19485 end;
19486
19487 -- Fourth case, slice of same array with same bounds
19488
19489 elsif K1 = N_Slice
19490 and then K2 = N_Slice
19491 and then Nkind (Discrete_Range (N1)) = N_Range
19492 and then Nkind (Discrete_Range (N2)) = N_Range
19493 and then Same_Value (Low_Bound (Discrete_Range (N1)),
19494 Low_Bound (Discrete_Range (N2)))
19495 and then Same_Value (High_Bound (Discrete_Range (N1)),
19496 High_Bound (Discrete_Range (N2)))
19497 then
19498 return Same_Name (Prefix (N1), Prefix (N2));
19499
19500 -- All other cases, not clearly the same object
19501
19502 else
19503 return False;
19504 end if;
19505 end Same_Object;
19506
19507 ---------------
19508 -- Same_Type --
19509 ---------------
19510
19511 function Same_Type (T1, T2 : Entity_Id) return Boolean is
19512 begin
19513 if T1 = T2 then
19514 return True;
19515
19516 elsif not Is_Constrained (T1)
19517 and then not Is_Constrained (T2)
19518 and then Base_Type (T1) = Base_Type (T2)
19519 then
19520 return True;
19521
19522 -- For now don't bother with case of identical constraints, to be
19523 -- fiddled with later on perhaps (this is only used for optimization
19524 -- purposes, so it is not critical to do a best possible job)
19525
19526 else
19527 return False;
19528 end if;
19529 end Same_Type;
19530
19531 ----------------
19532 -- Same_Value --
19533 ----------------
19534
19535 function Same_Value (Node1, Node2 : Node_Id) return Boolean is
19536 begin
19537 if Compile_Time_Known_Value (Node1)
19538 and then Compile_Time_Known_Value (Node2)
19539 and then Expr_Value (Node1) = Expr_Value (Node2)
19540 then
19541 return True;
19542 elsif Same_Object (Node1, Node2) then
19543 return True;
19544 else
19545 return False;
19546 end if;
19547 end Same_Value;
19548
19549 -----------------------------
19550 -- Save_SPARK_Mode_And_Set --
19551 -----------------------------
19552
19553 procedure Save_SPARK_Mode_And_Set
19554 (Context : Entity_Id;
19555 Mode : out SPARK_Mode_Type)
19556 is
19557 begin
19558 -- Save the current mode in effect
19559
19560 Mode := SPARK_Mode;
19561
19562 -- Do not consider illegal or partially decorated constructs
19563
19564 if Ekind (Context) = E_Void or else Error_Posted (Context) then
19565 null;
19566
19567 elsif Present (SPARK_Pragma (Context)) then
19568 SPARK_Mode := Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Context));
19569 end if;
19570 end Save_SPARK_Mode_And_Set;
19571
19572 -------------------------
19573 -- Scalar_Part_Present --
19574 -------------------------
19575
19576 function Scalar_Part_Present (T : Entity_Id) return Boolean is
19577 C : Entity_Id;
19578
19579 begin
19580 if Is_Scalar_Type (T) then
19581 return True;
19582
19583 elsif Is_Array_Type (T) then
19584 return Scalar_Part_Present (Component_Type (T));
19585
19586 elsif Is_Record_Type (T) or else Has_Discriminants (T) then
19587 C := First_Component_Or_Discriminant (T);
19588 while Present (C) loop
19589 if Scalar_Part_Present (Etype (C)) then
19590 return True;
19591 else
19592 Next_Component_Or_Discriminant (C);
19593 end if;
19594 end loop;
19595 end if;
19596
19597 return False;
19598 end Scalar_Part_Present;
19599
19600 ------------------------
19601 -- Scope_Is_Transient --
19602 ------------------------
19603
19604 function Scope_Is_Transient return Boolean is
19605 begin
19606 return Scope_Stack.Table (Scope_Stack.Last).Is_Transient;
19607 end Scope_Is_Transient;
19608
19609 ------------------
19610 -- Scope_Within --
19611 ------------------
19612
19613 function Scope_Within (Scope1, Scope2 : Entity_Id) return Boolean is
19614 Scop : Entity_Id;
19615
19616 begin
19617 Scop := Scope1;
19618 while Scop /= Standard_Standard loop
19619 Scop := Scope (Scop);
19620
19621 if Scop = Scope2 then
19622 return True;
19623 end if;
19624 end loop;
19625
19626 return False;
19627 end Scope_Within;
19628
19629 --------------------------
19630 -- Scope_Within_Or_Same --
19631 --------------------------
19632
19633 function Scope_Within_Or_Same (Scope1, Scope2 : Entity_Id) return Boolean is
19634 Scop : Entity_Id;
19635
19636 begin
19637 Scop := Scope1;
19638 while Scop /= Standard_Standard loop
19639 if Scop = Scope2 then
19640 return True;
19641 else
19642 Scop := Scope (Scop);
19643 end if;
19644 end loop;
19645
19646 return False;
19647 end Scope_Within_Or_Same;
19648
19649 --------------------
19650 -- Set_Convention --
19651 --------------------
19652
19653 procedure Set_Convention (E : Entity_Id; Val : Snames.Convention_Id) is
19654 begin
19655 Basic_Set_Convention (E, Val);
19656
19657 if Is_Type (E)
19658 and then Is_Access_Subprogram_Type (Base_Type (E))
19659 and then Has_Foreign_Convention (E)
19660 then
19661
19662 -- A pragma Convention in an instance may apply to the subtype
19663 -- created for a formal, in which case we have already verified
19664 -- that conventions of actual and formal match and there is nothing
19665 -- to flag on the subtype.
19666
19667 if In_Instance then
19668 null;
19669 else
19670 Set_Can_Use_Internal_Rep (E, False);
19671 end if;
19672 end if;
19673
19674 -- If E is an object or component, and the type of E is an anonymous
19675 -- access type with no convention set, then also set the convention of
19676 -- the anonymous access type. We do not do this for anonymous protected
19677 -- types, since protected types always have the default convention.
19678
19679 if Present (Etype (E))
19680 and then (Is_Object (E)
19681 or else Ekind (E) = E_Component
19682
19683 -- Allow E_Void (happens for pragma Convention appearing
19684 -- in the middle of a record applying to a component)
19685
19686 or else Ekind (E) = E_Void)
19687 then
19688 declare
19689 Typ : constant Entity_Id := Etype (E);
19690
19691 begin
19692 if Ekind_In (Typ, E_Anonymous_Access_Type,
19693 E_Anonymous_Access_Subprogram_Type)
19694 and then not Has_Convention_Pragma (Typ)
19695 then
19696 Basic_Set_Convention (Typ, Val);
19697 Set_Has_Convention_Pragma (Typ);
19698
19699 -- And for the access subprogram type, deal similarly with the
19700 -- designated E_Subprogram_Type if it is also internal (which
19701 -- it always is?)
19702
19703 if Ekind (Typ) = E_Anonymous_Access_Subprogram_Type then
19704 declare
19705 Dtype : constant Entity_Id := Designated_Type (Typ);
19706 begin
19707 if Ekind (Dtype) = E_Subprogram_Type
19708 and then Is_Itype (Dtype)
19709 and then not Has_Convention_Pragma (Dtype)
19710 then
19711 Basic_Set_Convention (Dtype, Val);
19712 Set_Has_Convention_Pragma (Dtype);
19713 end if;
19714 end;
19715 end if;
19716 end if;
19717 end;
19718 end if;
19719 end Set_Convention;
19720
19721 ------------------------
19722 -- Set_Current_Entity --
19723 ------------------------
19724
19725 -- The given entity is to be set as the currently visible definition of its
19726 -- associated name (i.e. the Node_Id associated with its name). All we have
19727 -- to do is to get the name from the identifier, and then set the
19728 -- associated Node_Id to point to the given entity.
19729
19730 procedure Set_Current_Entity (E : Entity_Id) is
19731 begin
19732 Set_Name_Entity_Id (Chars (E), E);
19733 end Set_Current_Entity;
19734
19735 ---------------------------
19736 -- Set_Debug_Info_Needed --
19737 ---------------------------
19738
19739 procedure Set_Debug_Info_Needed (T : Entity_Id) is
19740
19741 procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id);
19742 pragma Inline (Set_Debug_Info_Needed_If_Not_Set);
19743 -- Used to set debug info in a related node if not set already
19744
19745 --------------------------------------
19746 -- Set_Debug_Info_Needed_If_Not_Set --
19747 --------------------------------------
19748
19749 procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id) is
19750 begin
19751 if Present (E) and then not Needs_Debug_Info (E) then
19752 Set_Debug_Info_Needed (E);
19753
19754 -- For a private type, indicate that the full view also needs
19755 -- debug information.
19756
19757 if Is_Type (E)
19758 and then Is_Private_Type (E)
19759 and then Present (Full_View (E))
19760 then
19761 Set_Debug_Info_Needed (Full_View (E));
19762 end if;
19763 end if;
19764 end Set_Debug_Info_Needed_If_Not_Set;
19765
19766 -- Start of processing for Set_Debug_Info_Needed
19767
19768 begin
19769 -- Nothing to do if argument is Empty or has Debug_Info_Off set, which
19770 -- indicates that Debug_Info_Needed is never required for the entity.
19771 -- Nothing to do if entity comes from a predefined file. Library files
19772 -- are compiled without debug information, but inlined bodies of these
19773 -- routines may appear in user code, and debug information on them ends
19774 -- up complicating debugging the user code.
19775
19776 if No (T)
19777 or else Debug_Info_Off (T)
19778 then
19779 return;
19780
19781 elsif In_Inlined_Body
19782 and then Is_Predefined_File_Name
19783 (Unit_File_Name (Get_Source_Unit (Sloc (T))))
19784 then
19785 Set_Needs_Debug_Info (T, False);
19786 end if;
19787
19788 -- Set flag in entity itself. Note that we will go through the following
19789 -- circuitry even if the flag is already set on T. That's intentional,
19790 -- it makes sure that the flag will be set in subsidiary entities.
19791
19792 Set_Needs_Debug_Info (T);
19793
19794 -- Set flag on subsidiary entities if not set already
19795
19796 if Is_Object (T) then
19797 Set_Debug_Info_Needed_If_Not_Set (Etype (T));
19798
19799 elsif Is_Type (T) then
19800 Set_Debug_Info_Needed_If_Not_Set (Etype (T));
19801
19802 if Is_Record_Type (T) then
19803 declare
19804 Ent : Entity_Id := First_Entity (T);
19805 begin
19806 while Present (Ent) loop
19807 Set_Debug_Info_Needed_If_Not_Set (Ent);
19808 Next_Entity (Ent);
19809 end loop;
19810 end;
19811
19812 -- For a class wide subtype, we also need debug information
19813 -- for the equivalent type.
19814
19815 if Ekind (T) = E_Class_Wide_Subtype then
19816 Set_Debug_Info_Needed_If_Not_Set (Equivalent_Type (T));
19817 end if;
19818
19819 elsif Is_Array_Type (T) then
19820 Set_Debug_Info_Needed_If_Not_Set (Component_Type (T));
19821
19822 declare
19823 Indx : Node_Id := First_Index (T);
19824 begin
19825 while Present (Indx) loop
19826 Set_Debug_Info_Needed_If_Not_Set (Etype (Indx));
19827 Indx := Next_Index (Indx);
19828 end loop;
19829 end;
19830
19831 -- For a packed array type, we also need debug information for
19832 -- the type used to represent the packed array. Conversely, we
19833 -- also need it for the former if we need it for the latter.
19834
19835 if Is_Packed (T) then
19836 Set_Debug_Info_Needed_If_Not_Set (Packed_Array_Impl_Type (T));
19837 end if;
19838
19839 if Is_Packed_Array_Impl_Type (T) then
19840 Set_Debug_Info_Needed_If_Not_Set (Original_Array_Type (T));
19841 end if;
19842
19843 elsif Is_Access_Type (T) then
19844 Set_Debug_Info_Needed_If_Not_Set (Directly_Designated_Type (T));
19845
19846 elsif Is_Private_Type (T) then
19847 declare
19848 FV : constant Entity_Id := Full_View (T);
19849
19850 begin
19851 Set_Debug_Info_Needed_If_Not_Set (FV);
19852
19853 -- If the full view is itself a derived private type, we need
19854 -- debug information on its underlying type.
19855
19856 if Present (FV)
19857 and then Is_Private_Type (FV)
19858 and then Present (Underlying_Full_View (FV))
19859 then
19860 Set_Needs_Debug_Info (Underlying_Full_View (FV));
19861 end if;
19862 end;
19863
19864 elsif Is_Protected_Type (T) then
19865 Set_Debug_Info_Needed_If_Not_Set (Corresponding_Record_Type (T));
19866
19867 elsif Is_Scalar_Type (T) then
19868
19869 -- If the subrange bounds are materialized by dedicated constant
19870 -- objects, also include them in the debug info to make sure the
19871 -- debugger can properly use them.
19872
19873 if Present (Scalar_Range (T))
19874 and then Nkind (Scalar_Range (T)) = N_Range
19875 then
19876 declare
19877 Low_Bnd : constant Node_Id := Type_Low_Bound (T);
19878 High_Bnd : constant Node_Id := Type_High_Bound (T);
19879
19880 begin
19881 if Is_Entity_Name (Low_Bnd) then
19882 Set_Debug_Info_Needed_If_Not_Set (Entity (Low_Bnd));
19883 end if;
19884
19885 if Is_Entity_Name (High_Bnd) then
19886 Set_Debug_Info_Needed_If_Not_Set (Entity (High_Bnd));
19887 end if;
19888 end;
19889 end if;
19890 end if;
19891 end if;
19892 end Set_Debug_Info_Needed;
19893
19894 ----------------------------
19895 -- Set_Entity_With_Checks --
19896 ----------------------------
19897
19898 procedure Set_Entity_With_Checks (N : Node_Id; Val : Entity_Id) is
19899 Val_Actual : Entity_Id;
19900 Nod : Node_Id;
19901 Post_Node : Node_Id;
19902
19903 begin
19904 -- Unconditionally set the entity
19905
19906 Set_Entity (N, Val);
19907
19908 -- The node to post on is the selector in the case of an expanded name,
19909 -- and otherwise the node itself.
19910
19911 if Nkind (N) = N_Expanded_Name then
19912 Post_Node := Selector_Name (N);
19913 else
19914 Post_Node := N;
19915 end if;
19916
19917 -- Check for violation of No_Fixed_IO
19918
19919 if Restriction_Check_Required (No_Fixed_IO)
19920 and then
19921 ((RTU_Loaded (Ada_Text_IO)
19922 and then (Is_RTE (Val, RE_Decimal_IO)
19923 or else
19924 Is_RTE (Val, RE_Fixed_IO)))
19925
19926 or else
19927 (RTU_Loaded (Ada_Wide_Text_IO)
19928 and then (Is_RTE (Val, RO_WT_Decimal_IO)
19929 or else
19930 Is_RTE (Val, RO_WT_Fixed_IO)))
19931
19932 or else
19933 (RTU_Loaded (Ada_Wide_Wide_Text_IO)
19934 and then (Is_RTE (Val, RO_WW_Decimal_IO)
19935 or else
19936 Is_RTE (Val, RO_WW_Fixed_IO))))
19937
19938 -- A special extra check, don't complain about a reference from within
19939 -- the Ada.Interrupts package itself!
19940
19941 and then not In_Same_Extended_Unit (N, Val)
19942 then
19943 Check_Restriction (No_Fixed_IO, Post_Node);
19944 end if;
19945
19946 -- Remaining checks are only done on source nodes. Note that we test
19947 -- for violation of No_Fixed_IO even on non-source nodes, because the
19948 -- cases for checking violations of this restriction are instantiations
19949 -- where the reference in the instance has Comes_From_Source False.
19950
19951 if not Comes_From_Source (N) then
19952 return;
19953 end if;
19954
19955 -- Check for violation of No_Abort_Statements, which is triggered by
19956 -- call to Ada.Task_Identification.Abort_Task.
19957
19958 if Restriction_Check_Required (No_Abort_Statements)
19959 and then (Is_RTE (Val, RE_Abort_Task))
19960
19961 -- A special extra check, don't complain about a reference from within
19962 -- the Ada.Task_Identification package itself!
19963
19964 and then not In_Same_Extended_Unit (N, Val)
19965 then
19966 Check_Restriction (No_Abort_Statements, Post_Node);
19967 end if;
19968
19969 if Val = Standard_Long_Long_Integer then
19970 Check_Restriction (No_Long_Long_Integers, Post_Node);
19971 end if;
19972
19973 -- Check for violation of No_Dynamic_Attachment
19974
19975 if Restriction_Check_Required (No_Dynamic_Attachment)
19976 and then RTU_Loaded (Ada_Interrupts)
19977 and then (Is_RTE (Val, RE_Is_Reserved) or else
19978 Is_RTE (Val, RE_Is_Attached) or else
19979 Is_RTE (Val, RE_Current_Handler) or else
19980 Is_RTE (Val, RE_Attach_Handler) or else
19981 Is_RTE (Val, RE_Exchange_Handler) or else
19982 Is_RTE (Val, RE_Detach_Handler) or else
19983 Is_RTE (Val, RE_Reference))
19984
19985 -- A special extra check, don't complain about a reference from within
19986 -- the Ada.Interrupts package itself!
19987
19988 and then not In_Same_Extended_Unit (N, Val)
19989 then
19990 Check_Restriction (No_Dynamic_Attachment, Post_Node);
19991 end if;
19992
19993 -- Check for No_Implementation_Identifiers
19994
19995 if Restriction_Check_Required (No_Implementation_Identifiers) then
19996
19997 -- We have an implementation defined entity if it is marked as
19998 -- implementation defined, or is defined in a package marked as
19999 -- implementation defined. However, library packages themselves
20000 -- are excluded (we don't want to flag Interfaces itself, just
20001 -- the entities within it).
20002
20003 if (Is_Implementation_Defined (Val)
20004 or else
20005 (Present (Scope (Val))
20006 and then Is_Implementation_Defined (Scope (Val))))
20007 and then not (Ekind_In (Val, E_Package, E_Generic_Package)
20008 and then Is_Library_Level_Entity (Val))
20009 then
20010 Check_Restriction (No_Implementation_Identifiers, Post_Node);
20011 end if;
20012 end if;
20013
20014 -- Do the style check
20015
20016 if Style_Check
20017 and then not Suppress_Style_Checks (Val)
20018 and then not In_Instance
20019 then
20020 if Nkind (N) = N_Identifier then
20021 Nod := N;
20022 elsif Nkind (N) = N_Expanded_Name then
20023 Nod := Selector_Name (N);
20024 else
20025 return;
20026 end if;
20027
20028 -- A special situation arises for derived operations, where we want
20029 -- to do the check against the parent (since the Sloc of the derived
20030 -- operation points to the derived type declaration itself).
20031
20032 Val_Actual := Val;
20033 while not Comes_From_Source (Val_Actual)
20034 and then Nkind (Val_Actual) in N_Entity
20035 and then (Ekind (Val_Actual) = E_Enumeration_Literal
20036 or else Is_Subprogram_Or_Generic_Subprogram (Val_Actual))
20037 and then Present (Alias (Val_Actual))
20038 loop
20039 Val_Actual := Alias (Val_Actual);
20040 end loop;
20041
20042 -- Renaming declarations for generic actuals do not come from source,
20043 -- and have a different name from that of the entity they rename, so
20044 -- there is no style check to perform here.
20045
20046 if Chars (Nod) = Chars (Val_Actual) then
20047 Style.Check_Identifier (Nod, Val_Actual);
20048 end if;
20049 end if;
20050
20051 Set_Entity (N, Val);
20052 end Set_Entity_With_Checks;
20053
20054 ------------------------
20055 -- Set_Name_Entity_Id --
20056 ------------------------
20057
20058 procedure Set_Name_Entity_Id (Id : Name_Id; Val : Entity_Id) is
20059 begin
20060 Set_Name_Table_Int (Id, Int (Val));
20061 end Set_Name_Entity_Id;
20062
20063 ---------------------
20064 -- Set_Next_Actual --
20065 ---------------------
20066
20067 procedure Set_Next_Actual (Ass1_Id : Node_Id; Ass2_Id : Node_Id) is
20068 begin
20069 if Nkind (Parent (Ass1_Id)) = N_Parameter_Association then
20070 Set_First_Named_Actual (Parent (Ass1_Id), Ass2_Id);
20071 end if;
20072 end Set_Next_Actual;
20073
20074 ----------------------------------
20075 -- Set_Optimize_Alignment_Flags --
20076 ----------------------------------
20077
20078 procedure Set_Optimize_Alignment_Flags (E : Entity_Id) is
20079 begin
20080 if Optimize_Alignment = 'S' then
20081 Set_Optimize_Alignment_Space (E);
20082 elsif Optimize_Alignment = 'T' then
20083 Set_Optimize_Alignment_Time (E);
20084 end if;
20085 end Set_Optimize_Alignment_Flags;
20086
20087 -----------------------
20088 -- Set_Public_Status --
20089 -----------------------
20090
20091 procedure Set_Public_Status (Id : Entity_Id) is
20092 S : constant Entity_Id := Current_Scope;
20093
20094 function Within_HSS_Or_If (E : Entity_Id) return Boolean;
20095 -- Determines if E is defined within handled statement sequence or
20096 -- an if statement, returns True if so, False otherwise.
20097
20098 ----------------------
20099 -- Within_HSS_Or_If --
20100 ----------------------
20101
20102 function Within_HSS_Or_If (E : Entity_Id) return Boolean is
20103 N : Node_Id;
20104 begin
20105 N := Declaration_Node (E);
20106 loop
20107 N := Parent (N);
20108
20109 if No (N) then
20110 return False;
20111
20112 elsif Nkind_In (N, N_Handled_Sequence_Of_Statements,
20113 N_If_Statement)
20114 then
20115 return True;
20116 end if;
20117 end loop;
20118 end Within_HSS_Or_If;
20119
20120 -- Start of processing for Set_Public_Status
20121
20122 begin
20123 -- Everything in the scope of Standard is public
20124
20125 if S = Standard_Standard then
20126 Set_Is_Public (Id);
20127
20128 -- Entity is definitely not public if enclosing scope is not public
20129
20130 elsif not Is_Public (S) then
20131 return;
20132
20133 -- An object or function declaration that occurs in a handled sequence
20134 -- of statements or within an if statement is the declaration for a
20135 -- temporary object or local subprogram generated by the expander. It
20136 -- never needs to be made public and furthermore, making it public can
20137 -- cause back end problems.
20138
20139 elsif Nkind_In (Parent (Id), N_Object_Declaration,
20140 N_Function_Specification)
20141 and then Within_HSS_Or_If (Id)
20142 then
20143 return;
20144
20145 -- Entities in public packages or records are public
20146
20147 elsif Ekind (S) = E_Package or Is_Record_Type (S) then
20148 Set_Is_Public (Id);
20149
20150 -- The bounds of an entry family declaration can generate object
20151 -- declarations that are visible to the back-end, e.g. in the
20152 -- the declaration of a composite type that contains tasks.
20153
20154 elsif Is_Concurrent_Type (S)
20155 and then not Has_Completion (S)
20156 and then Nkind (Parent (Id)) = N_Object_Declaration
20157 then
20158 Set_Is_Public (Id);
20159 end if;
20160 end Set_Public_Status;
20161
20162 -----------------------------
20163 -- Set_Referenced_Modified --
20164 -----------------------------
20165
20166 procedure Set_Referenced_Modified (N : Node_Id; Out_Param : Boolean) is
20167 Pref : Node_Id;
20168
20169 begin
20170 -- Deal with indexed or selected component where prefix is modified
20171
20172 if Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
20173 Pref := Prefix (N);
20174
20175 -- If prefix is access type, then it is the designated object that is
20176 -- being modified, which means we have no entity to set the flag on.
20177
20178 if No (Etype (Pref)) or else Is_Access_Type (Etype (Pref)) then
20179 return;
20180
20181 -- Otherwise chase the prefix
20182
20183 else
20184 Set_Referenced_Modified (Pref, Out_Param);
20185 end if;
20186
20187 -- Otherwise see if we have an entity name (only other case to process)
20188
20189 elsif Is_Entity_Name (N) and then Present (Entity (N)) then
20190 Set_Referenced_As_LHS (Entity (N), not Out_Param);
20191 Set_Referenced_As_Out_Parameter (Entity (N), Out_Param);
20192 end if;
20193 end Set_Referenced_Modified;
20194
20195 ----------------------------
20196 -- Set_Scope_Is_Transient --
20197 ----------------------------
20198
20199 procedure Set_Scope_Is_Transient (V : Boolean := True) is
20200 begin
20201 Scope_Stack.Table (Scope_Stack.Last).Is_Transient := V;
20202 end Set_Scope_Is_Transient;
20203
20204 -------------------
20205 -- Set_Size_Info --
20206 -------------------
20207
20208 procedure Set_Size_Info (T1, T2 : Entity_Id) is
20209 begin
20210 -- We copy Esize, but not RM_Size, since in general RM_Size is
20211 -- subtype specific and does not get inherited by all subtypes.
20212
20213 Set_Esize (T1, Esize (T2));
20214 Set_Has_Biased_Representation (T1, Has_Biased_Representation (T2));
20215
20216 if Is_Discrete_Or_Fixed_Point_Type (T1)
20217 and then
20218 Is_Discrete_Or_Fixed_Point_Type (T2)
20219 then
20220 Set_Is_Unsigned_Type (T1, Is_Unsigned_Type (T2));
20221 end if;
20222
20223 Set_Alignment (T1, Alignment (T2));
20224 end Set_Size_Info;
20225
20226 --------------------
20227 -- Static_Boolean --
20228 --------------------
20229
20230 function Static_Boolean (N : Node_Id) return Uint is
20231 begin
20232 Analyze_And_Resolve (N, Standard_Boolean);
20233
20234 if N = Error
20235 or else Error_Posted (N)
20236 or else Etype (N) = Any_Type
20237 then
20238 return No_Uint;
20239 end if;
20240
20241 if Is_OK_Static_Expression (N) then
20242 if not Raises_Constraint_Error (N) then
20243 return Expr_Value (N);
20244 else
20245 return No_Uint;
20246 end if;
20247
20248 elsif Etype (N) = Any_Type then
20249 return No_Uint;
20250
20251 else
20252 Flag_Non_Static_Expr
20253 ("static boolean expression required here", N);
20254 return No_Uint;
20255 end if;
20256 end Static_Boolean;
20257
20258 --------------------
20259 -- Static_Integer --
20260 --------------------
20261
20262 function Static_Integer (N : Node_Id) return Uint is
20263 begin
20264 Analyze_And_Resolve (N, Any_Integer);
20265
20266 if N = Error
20267 or else Error_Posted (N)
20268 or else Etype (N) = Any_Type
20269 then
20270 return No_Uint;
20271 end if;
20272
20273 if Is_OK_Static_Expression (N) then
20274 if not Raises_Constraint_Error (N) then
20275 return Expr_Value (N);
20276 else
20277 return No_Uint;
20278 end if;
20279
20280 elsif Etype (N) = Any_Type then
20281 return No_Uint;
20282
20283 else
20284 Flag_Non_Static_Expr
20285 ("static integer expression required here", N);
20286 return No_Uint;
20287 end if;
20288 end Static_Integer;
20289
20290 --------------------------
20291 -- Statically_Different --
20292 --------------------------
20293
20294 function Statically_Different (E1, E2 : Node_Id) return Boolean is
20295 R1 : constant Node_Id := Get_Referenced_Object (E1);
20296 R2 : constant Node_Id := Get_Referenced_Object (E2);
20297 begin
20298 return Is_Entity_Name (R1)
20299 and then Is_Entity_Name (R2)
20300 and then Entity (R1) /= Entity (R2)
20301 and then not Is_Formal (Entity (R1))
20302 and then not Is_Formal (Entity (R2));
20303 end Statically_Different;
20304
20305 --------------------------------------
20306 -- Subject_To_Loop_Entry_Attributes --
20307 --------------------------------------
20308
20309 function Subject_To_Loop_Entry_Attributes (N : Node_Id) return Boolean is
20310 Stmt : Node_Id;
20311
20312 begin
20313 Stmt := N;
20314
20315 -- The expansion mechanism transform a loop subject to at least one
20316 -- 'Loop_Entry attribute into a conditional block. Infinite loops lack
20317 -- the conditional part.
20318
20319 if Nkind_In (Stmt, N_Block_Statement, N_If_Statement)
20320 and then Nkind (Original_Node (N)) = N_Loop_Statement
20321 then
20322 Stmt := Original_Node (N);
20323 end if;
20324
20325 return
20326 Nkind (Stmt) = N_Loop_Statement
20327 and then Present (Identifier (Stmt))
20328 and then Present (Entity (Identifier (Stmt)))
20329 and then Has_Loop_Entry_Attributes (Entity (Identifier (Stmt)));
20330 end Subject_To_Loop_Entry_Attributes;
20331
20332 -----------------------------
20333 -- Subprogram_Access_Level --
20334 -----------------------------
20335
20336 function Subprogram_Access_Level (Subp : Entity_Id) return Uint is
20337 begin
20338 if Present (Alias (Subp)) then
20339 return Subprogram_Access_Level (Alias (Subp));
20340 else
20341 return Scope_Depth (Enclosing_Dynamic_Scope (Subp));
20342 end if;
20343 end Subprogram_Access_Level;
20344
20345 -------------------------------
20346 -- Support_Atomic_Primitives --
20347 -------------------------------
20348
20349 function Support_Atomic_Primitives (Typ : Entity_Id) return Boolean is
20350 Size : Int;
20351
20352 begin
20353 -- Verify the alignment of Typ is known
20354
20355 if not Known_Alignment (Typ) then
20356 return False;
20357 end if;
20358
20359 if Known_Static_Esize (Typ) then
20360 Size := UI_To_Int (Esize (Typ));
20361
20362 -- If the Esize (Object_Size) is unknown at compile time, look at the
20363 -- RM_Size (Value_Size) which may have been set by an explicit rep item.
20364
20365 elsif Known_Static_RM_Size (Typ) then
20366 Size := UI_To_Int (RM_Size (Typ));
20367
20368 -- Otherwise, the size is considered to be unknown.
20369
20370 else
20371 return False;
20372 end if;
20373
20374 -- Check that the size of the component is 8, 16, 32, or 64 bits and
20375 -- that Typ is properly aligned.
20376
20377 case Size is
20378 when 8 | 16 | 32 | 64 =>
20379 return Size = UI_To_Int (Alignment (Typ)) * 8;
20380 when others =>
20381 return False;
20382 end case;
20383 end Support_Atomic_Primitives;
20384
20385 -----------------
20386 -- Trace_Scope --
20387 -----------------
20388
20389 procedure Trace_Scope (N : Node_Id; E : Entity_Id; Msg : String) is
20390 begin
20391 if Debug_Flag_W then
20392 for J in 0 .. Scope_Stack.Last loop
20393 Write_Str (" ");
20394 end loop;
20395
20396 Write_Str (Msg);
20397 Write_Name (Chars (E));
20398 Write_Str (" from ");
20399 Write_Location (Sloc (N));
20400 Write_Eol;
20401 end if;
20402 end Trace_Scope;
20403
20404 -----------------------
20405 -- Transfer_Entities --
20406 -----------------------
20407
20408 procedure Transfer_Entities (From : Entity_Id; To : Entity_Id) is
20409 procedure Set_Public_Status_Of (Id : Entity_Id);
20410 -- Set the Is_Public attribute of arbitrary entity Id by calling routine
20411 -- Set_Public_Status. If successfull and Id denotes a record type, set
20412 -- the Is_Public attribute of its fields.
20413
20414 --------------------------
20415 -- Set_Public_Status_Of --
20416 --------------------------
20417
20418 procedure Set_Public_Status_Of (Id : Entity_Id) is
20419 Field : Entity_Id;
20420
20421 begin
20422 if not Is_Public (Id) then
20423 Set_Public_Status (Id);
20424
20425 -- When the input entity is a public record type, ensure that all
20426 -- its internal fields are also exposed to the linker. The fields
20427 -- of a class-wide type are never made public.
20428
20429 if Is_Public (Id)
20430 and then Is_Record_Type (Id)
20431 and then not Is_Class_Wide_Type (Id)
20432 then
20433 Field := First_Entity (Id);
20434 while Present (Field) loop
20435 Set_Is_Public (Field);
20436 Next_Entity (Field);
20437 end loop;
20438 end if;
20439 end if;
20440 end Set_Public_Status_Of;
20441
20442 -- Local variables
20443
20444 Full_Id : Entity_Id;
20445 Id : Entity_Id;
20446
20447 -- Start of processing for Transfer_Entities
20448
20449 begin
20450 Id := First_Entity (From);
20451
20452 if Present (Id) then
20453
20454 -- Merge the entity chain of the source scope with that of the
20455 -- destination scope.
20456
20457 if Present (Last_Entity (To)) then
20458 Set_Next_Entity (Last_Entity (To), Id);
20459 else
20460 Set_First_Entity (To, Id);
20461 end if;
20462
20463 Set_Last_Entity (To, Last_Entity (From));
20464
20465 -- Inspect the entities of the source scope and update their Scope
20466 -- attribute.
20467
20468 while Present (Id) loop
20469 Set_Scope (Id, To);
20470 Set_Public_Status_Of (Id);
20471
20472 -- Handle an internally generated full view for a private type
20473
20474 if Is_Private_Type (Id)
20475 and then Present (Full_View (Id))
20476 and then Is_Itype (Full_View (Id))
20477 then
20478 Full_Id := Full_View (Id);
20479
20480 Set_Scope (Full_Id, To);
20481 Set_Public_Status_Of (Full_Id);
20482 end if;
20483
20484 Next_Entity (Id);
20485 end loop;
20486
20487 Set_First_Entity (From, Empty);
20488 Set_Last_Entity (From, Empty);
20489 end if;
20490 end Transfer_Entities;
20491
20492 -----------------------
20493 -- Type_Access_Level --
20494 -----------------------
20495
20496 function Type_Access_Level (Typ : Entity_Id) return Uint is
20497 Btyp : Entity_Id;
20498
20499 begin
20500 Btyp := Base_Type (Typ);
20501
20502 -- Ada 2005 (AI-230): For most cases of anonymous access types, we
20503 -- simply use the level where the type is declared. This is true for
20504 -- stand-alone object declarations, and for anonymous access types
20505 -- associated with components the level is the same as that of the
20506 -- enclosing composite type. However, special treatment is needed for
20507 -- the cases of access parameters, return objects of an anonymous access
20508 -- type, and, in Ada 95, access discriminants of limited types.
20509
20510 if Is_Access_Type (Btyp) then
20511 if Ekind (Btyp) = E_Anonymous_Access_Type then
20512
20513 -- If the type is a nonlocal anonymous access type (such as for
20514 -- an access parameter) we treat it as being declared at the
20515 -- library level to ensure that names such as X.all'access don't
20516 -- fail static accessibility checks.
20517
20518 if not Is_Local_Anonymous_Access (Typ) then
20519 return Scope_Depth (Standard_Standard);
20520
20521 -- If this is a return object, the accessibility level is that of
20522 -- the result subtype of the enclosing function. The test here is
20523 -- little complicated, because we have to account for extended
20524 -- return statements that have been rewritten as blocks, in which
20525 -- case we have to find and the Is_Return_Object attribute of the
20526 -- itype's associated object. It would be nice to find a way to
20527 -- simplify this test, but it doesn't seem worthwhile to add a new
20528 -- flag just for purposes of this test. ???
20529
20530 elsif Ekind (Scope (Btyp)) = E_Return_Statement
20531 or else
20532 (Is_Itype (Btyp)
20533 and then Nkind (Associated_Node_For_Itype (Btyp)) =
20534 N_Object_Declaration
20535 and then Is_Return_Object
20536 (Defining_Identifier
20537 (Associated_Node_For_Itype (Btyp))))
20538 then
20539 declare
20540 Scop : Entity_Id;
20541
20542 begin
20543 Scop := Scope (Scope (Btyp));
20544 while Present (Scop) loop
20545 exit when Ekind (Scop) = E_Function;
20546 Scop := Scope (Scop);
20547 end loop;
20548
20549 -- Treat the return object's type as having the level of the
20550 -- function's result subtype (as per RM05-6.5(5.3/2)).
20551
20552 return Type_Access_Level (Etype (Scop));
20553 end;
20554 end if;
20555 end if;
20556
20557 Btyp := Root_Type (Btyp);
20558
20559 -- The accessibility level of anonymous access types associated with
20560 -- discriminants is that of the current instance of the type, and
20561 -- that's deeper than the type itself (AARM 3.10.2 (12.3.21)).
20562
20563 -- AI-402: access discriminants have accessibility based on the
20564 -- object rather than the type in Ada 2005, so the above paragraph
20565 -- doesn't apply.
20566
20567 -- ??? Needs completion with rules from AI-416
20568
20569 if Ada_Version <= Ada_95
20570 and then Ekind (Typ) = E_Anonymous_Access_Type
20571 and then Present (Associated_Node_For_Itype (Typ))
20572 and then Nkind (Associated_Node_For_Itype (Typ)) =
20573 N_Discriminant_Specification
20574 then
20575 return Scope_Depth (Enclosing_Dynamic_Scope (Btyp)) + 1;
20576 end if;
20577 end if;
20578
20579 -- Return library level for a generic formal type. This is done because
20580 -- RM(10.3.2) says that "The statically deeper relationship does not
20581 -- apply to ... a descendant of a generic formal type". Rather than
20582 -- checking at each point where a static accessibility check is
20583 -- performed to see if we are dealing with a formal type, this rule is
20584 -- implemented by having Type_Access_Level and Deepest_Type_Access_Level
20585 -- return extreme values for a formal type; Deepest_Type_Access_Level
20586 -- returns Int'Last. By calling the appropriate function from among the
20587 -- two, we ensure that the static accessibility check will pass if we
20588 -- happen to run into a formal type. More specifically, we should call
20589 -- Deepest_Type_Access_Level instead of Type_Access_Level whenever the
20590 -- call occurs as part of a static accessibility check and the error
20591 -- case is the case where the type's level is too shallow (as opposed
20592 -- to too deep).
20593
20594 if Is_Generic_Type (Root_Type (Btyp)) then
20595 return Scope_Depth (Standard_Standard);
20596 end if;
20597
20598 return Scope_Depth (Enclosing_Dynamic_Scope (Btyp));
20599 end Type_Access_Level;
20600
20601 ------------------------------------
20602 -- Type_Without_Stream_Operation --
20603 ------------------------------------
20604
20605 function Type_Without_Stream_Operation
20606 (T : Entity_Id;
20607 Op : TSS_Name_Type := TSS_Null) return Entity_Id
20608 is
20609 BT : constant Entity_Id := Base_Type (T);
20610 Op_Missing : Boolean;
20611
20612 begin
20613 if not Restriction_Active (No_Default_Stream_Attributes) then
20614 return Empty;
20615 end if;
20616
20617 if Is_Elementary_Type (T) then
20618 if Op = TSS_Null then
20619 Op_Missing :=
20620 No (TSS (BT, TSS_Stream_Read))
20621 or else No (TSS (BT, TSS_Stream_Write));
20622
20623 else
20624 Op_Missing := No (TSS (BT, Op));
20625 end if;
20626
20627 if Op_Missing then
20628 return T;
20629 else
20630 return Empty;
20631 end if;
20632
20633 elsif Is_Array_Type (T) then
20634 return Type_Without_Stream_Operation (Component_Type (T), Op);
20635
20636 elsif Is_Record_Type (T) then
20637 declare
20638 Comp : Entity_Id;
20639 C_Typ : Entity_Id;
20640
20641 begin
20642 Comp := First_Component (T);
20643 while Present (Comp) loop
20644 C_Typ := Type_Without_Stream_Operation (Etype (Comp), Op);
20645
20646 if Present (C_Typ) then
20647 return C_Typ;
20648 end if;
20649
20650 Next_Component (Comp);
20651 end loop;
20652
20653 return Empty;
20654 end;
20655
20656 elsif Is_Private_Type (T) and then Present (Full_View (T)) then
20657 return Type_Without_Stream_Operation (Full_View (T), Op);
20658 else
20659 return Empty;
20660 end if;
20661 end Type_Without_Stream_Operation;
20662
20663 ----------------------------
20664 -- Unique_Defining_Entity --
20665 ----------------------------
20666
20667 function Unique_Defining_Entity (N : Node_Id) return Entity_Id is
20668 begin
20669 return Unique_Entity (Defining_Entity (N));
20670 end Unique_Defining_Entity;
20671
20672 -------------------
20673 -- Unique_Entity --
20674 -------------------
20675
20676 function Unique_Entity (E : Entity_Id) return Entity_Id is
20677 U : Entity_Id := E;
20678 P : Node_Id;
20679
20680 begin
20681 case Ekind (E) is
20682 when E_Constant =>
20683 if Present (Full_View (E)) then
20684 U := Full_View (E);
20685 end if;
20686
20687 when Entry_Kind =>
20688 if Nkind (Parent (E)) = N_Entry_Body then
20689 declare
20690 Prot_Item : Entity_Id;
20691 Prot_Type : Entity_Id;
20692
20693 begin
20694 if Ekind (E) = E_Entry then
20695 Prot_Type := Scope (E);
20696
20697 -- Bodies of entry families are nested within an extra scope
20698 -- that contains an entry index declaration
20699
20700 else
20701 Prot_Type := Scope (Scope (E));
20702 end if;
20703
20704 pragma Assert (Ekind (Prot_Type) = E_Protected_Type);
20705
20706 -- Traverse the entity list of the protected type and locate
20707 -- an entry declaration which matches the entry body.
20708
20709 Prot_Item := First_Entity (Prot_Type);
20710 while Present (Prot_Item) loop
20711 if Ekind (Prot_Item) in Entry_Kind
20712 and then Corresponding_Body (Parent (Prot_Item)) = E
20713 then
20714 U := Prot_Item;
20715 exit;
20716 end if;
20717
20718 Next_Entity (Prot_Item);
20719 end loop;
20720 end;
20721 end if;
20722
20723 when Formal_Kind =>
20724 if Present (Spec_Entity (E)) then
20725 U := Spec_Entity (E);
20726 end if;
20727
20728 when E_Package_Body =>
20729 P := Parent (E);
20730
20731 if Nkind (P) = N_Defining_Program_Unit_Name then
20732 P := Parent (P);
20733 end if;
20734
20735 if Nkind (P) = N_Package_Body
20736 and then Present (Corresponding_Spec (P))
20737 then
20738 U := Corresponding_Spec (P);
20739
20740 elsif Nkind (P) = N_Package_Body_Stub
20741 and then Present (Corresponding_Spec_Of_Stub (P))
20742 then
20743 U := Corresponding_Spec_Of_Stub (P);
20744 end if;
20745
20746 when E_Protected_Body =>
20747 P := Parent (E);
20748
20749 if Nkind (P) = N_Protected_Body
20750 and then Present (Corresponding_Spec (P))
20751 then
20752 U := Corresponding_Spec (P);
20753
20754 elsif Nkind (P) = N_Protected_Body_Stub
20755 and then Present (Corresponding_Spec_Of_Stub (P))
20756 then
20757 U := Corresponding_Spec_Of_Stub (P);
20758
20759 if Is_Single_Protected_Object (U) then
20760 U := Etype (U);
20761 end if;
20762 end if;
20763
20764 when E_Subprogram_Body =>
20765 P := Parent (E);
20766
20767 if Nkind (P) = N_Defining_Program_Unit_Name then
20768 P := Parent (P);
20769 end if;
20770
20771 P := Parent (P);
20772
20773 if Nkind (P) = N_Subprogram_Body
20774 and then Present (Corresponding_Spec (P))
20775 then
20776 U := Corresponding_Spec (P);
20777
20778 elsif Nkind (P) = N_Subprogram_Body_Stub
20779 and then Present (Corresponding_Spec_Of_Stub (P))
20780 then
20781 U := Corresponding_Spec_Of_Stub (P);
20782
20783 elsif Nkind (P) = N_Subprogram_Renaming_Declaration then
20784 U := Corresponding_Spec (P);
20785 end if;
20786
20787 when E_Task_Body =>
20788 P := Parent (E);
20789
20790 if Nkind (P) = N_Task_Body
20791 and then Present (Corresponding_Spec (P))
20792 then
20793 U := Corresponding_Spec (P);
20794
20795 elsif Nkind (P) = N_Task_Body_Stub
20796 and then Present (Corresponding_Spec_Of_Stub (P))
20797 then
20798 U := Corresponding_Spec_Of_Stub (P);
20799
20800 if Is_Single_Task_Object (U) then
20801 U := Etype (U);
20802 end if;
20803 end if;
20804
20805 when Type_Kind =>
20806 if Present (Full_View (E)) then
20807 U := Full_View (E);
20808 end if;
20809
20810 when others =>
20811 null;
20812 end case;
20813
20814 return U;
20815 end Unique_Entity;
20816
20817 -----------------
20818 -- Unique_Name --
20819 -----------------
20820
20821 function Unique_Name (E : Entity_Id) return String is
20822
20823 -- Names of E_Subprogram_Body or E_Package_Body entities are not
20824 -- reliable, as they may not include the overloading suffix. Instead,
20825 -- when looking for the name of E or one of its enclosing scope, we get
20826 -- the name of the corresponding Unique_Entity.
20827
20828 function Get_Scoped_Name (E : Entity_Id) return String;
20829 -- Return the name of E prefixed by all the names of the scopes to which
20830 -- E belongs, except for Standard.
20831
20832 ---------------------
20833 -- Get_Scoped_Name --
20834 ---------------------
20835
20836 function Get_Scoped_Name (E : Entity_Id) return String is
20837 Name : constant String := Get_Name_String (Chars (E));
20838 begin
20839 if Has_Fully_Qualified_Name (E)
20840 or else Scope (E) = Standard_Standard
20841 then
20842 return Name;
20843 else
20844 return Get_Scoped_Name (Unique_Entity (Scope (E))) & "__" & Name;
20845 end if;
20846 end Get_Scoped_Name;
20847
20848 -- Start of processing for Unique_Name
20849
20850 begin
20851 if E = Standard_Standard then
20852 return Get_Name_String (Name_Standard);
20853
20854 elsif Scope (E) = Standard_Standard
20855 and then not (Ekind (E) = E_Package or else Is_Subprogram (E))
20856 then
20857 return Get_Name_String (Name_Standard) & "__" &
20858 Get_Name_String (Chars (E));
20859
20860 elsif Ekind (E) = E_Enumeration_Literal then
20861 return Unique_Name (Etype (E)) & "__" & Get_Name_String (Chars (E));
20862
20863 else
20864 return Get_Scoped_Name (Unique_Entity (E));
20865 end if;
20866 end Unique_Name;
20867
20868 ---------------------
20869 -- Unit_Is_Visible --
20870 ---------------------
20871
20872 function Unit_Is_Visible (U : Entity_Id) return Boolean is
20873 Curr : constant Node_Id := Cunit (Current_Sem_Unit);
20874 Curr_Entity : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
20875
20876 function Unit_In_Parent_Context (Par_Unit : Node_Id) return Boolean;
20877 -- For a child unit, check whether unit appears in a with_clause
20878 -- of a parent.
20879
20880 function Unit_In_Context (Comp_Unit : Node_Id) return Boolean;
20881 -- Scan the context clause of one compilation unit looking for a
20882 -- with_clause for the unit in question.
20883
20884 ----------------------------
20885 -- Unit_In_Parent_Context --
20886 ----------------------------
20887
20888 function Unit_In_Parent_Context (Par_Unit : Node_Id) return Boolean is
20889 begin
20890 if Unit_In_Context (Par_Unit) then
20891 return True;
20892
20893 elsif Is_Child_Unit (Defining_Entity (Unit (Par_Unit))) then
20894 return Unit_In_Parent_Context (Parent_Spec (Unit (Par_Unit)));
20895
20896 else
20897 return False;
20898 end if;
20899 end Unit_In_Parent_Context;
20900
20901 ---------------------
20902 -- Unit_In_Context --
20903 ---------------------
20904
20905 function Unit_In_Context (Comp_Unit : Node_Id) return Boolean is
20906 Clause : Node_Id;
20907
20908 begin
20909 Clause := First (Context_Items (Comp_Unit));
20910 while Present (Clause) loop
20911 if Nkind (Clause) = N_With_Clause then
20912 if Library_Unit (Clause) = U then
20913 return True;
20914
20915 -- The with_clause may denote a renaming of the unit we are
20916 -- looking for, eg. Text_IO which renames Ada.Text_IO.
20917
20918 elsif
20919 Renamed_Entity (Entity (Name (Clause))) =
20920 Defining_Entity (Unit (U))
20921 then
20922 return True;
20923 end if;
20924 end if;
20925
20926 Next (Clause);
20927 end loop;
20928
20929 return False;
20930 end Unit_In_Context;
20931
20932 -- Start of processing for Unit_Is_Visible
20933
20934 begin
20935 -- The currrent unit is directly visible
20936
20937 if Curr = U then
20938 return True;
20939
20940 elsif Unit_In_Context (Curr) then
20941 return True;
20942
20943 -- If the current unit is a body, check the context of the spec
20944
20945 elsif Nkind (Unit (Curr)) = N_Package_Body
20946 or else
20947 (Nkind (Unit (Curr)) = N_Subprogram_Body
20948 and then not Acts_As_Spec (Unit (Curr)))
20949 then
20950 if Unit_In_Context (Library_Unit (Curr)) then
20951 return True;
20952 end if;
20953 end if;
20954
20955 -- If the spec is a child unit, examine the parents
20956
20957 if Is_Child_Unit (Curr_Entity) then
20958 if Nkind (Unit (Curr)) in N_Unit_Body then
20959 return
20960 Unit_In_Parent_Context
20961 (Parent_Spec (Unit (Library_Unit (Curr))));
20962 else
20963 return Unit_In_Parent_Context (Parent_Spec (Unit (Curr)));
20964 end if;
20965
20966 else
20967 return False;
20968 end if;
20969 end Unit_Is_Visible;
20970
20971 ------------------------------
20972 -- Universal_Interpretation --
20973 ------------------------------
20974
20975 function Universal_Interpretation (Opnd : Node_Id) return Entity_Id is
20976 Index : Interp_Index;
20977 It : Interp;
20978
20979 begin
20980 -- The argument may be a formal parameter of an operator or subprogram
20981 -- with multiple interpretations, or else an expression for an actual.
20982
20983 if Nkind (Opnd) = N_Defining_Identifier
20984 or else not Is_Overloaded (Opnd)
20985 then
20986 if Etype (Opnd) = Universal_Integer
20987 or else Etype (Opnd) = Universal_Real
20988 then
20989 return Etype (Opnd);
20990 else
20991 return Empty;
20992 end if;
20993
20994 else
20995 Get_First_Interp (Opnd, Index, It);
20996 while Present (It.Typ) loop
20997 if It.Typ = Universal_Integer
20998 or else It.Typ = Universal_Real
20999 then
21000 return It.Typ;
21001 end if;
21002
21003 Get_Next_Interp (Index, It);
21004 end loop;
21005
21006 return Empty;
21007 end if;
21008 end Universal_Interpretation;
21009
21010 ---------------
21011 -- Unqualify --
21012 ---------------
21013
21014 function Unqualify (Expr : Node_Id) return Node_Id is
21015 begin
21016 -- Recurse to handle unlikely case of multiple levels of qualification
21017
21018 if Nkind (Expr) = N_Qualified_Expression then
21019 return Unqualify (Expression (Expr));
21020
21021 -- Normal case, not a qualified expression
21022
21023 else
21024 return Expr;
21025 end if;
21026 end Unqualify;
21027
21028 -----------------------
21029 -- Visible_Ancestors --
21030 -----------------------
21031
21032 function Visible_Ancestors (Typ : Entity_Id) return Elist_Id is
21033 List_1 : Elist_Id;
21034 List_2 : Elist_Id;
21035 Elmt : Elmt_Id;
21036
21037 begin
21038 pragma Assert (Is_Record_Type (Typ) and then Is_Tagged_Type (Typ));
21039
21040 -- Collect all the parents and progenitors of Typ. If the full-view of
21041 -- private parents and progenitors is available then it is used to
21042 -- generate the list of visible ancestors; otherwise their partial
21043 -- view is added to the resulting list.
21044
21045 Collect_Parents
21046 (T => Typ,
21047 List => List_1,
21048 Use_Full_View => True);
21049
21050 Collect_Interfaces
21051 (T => Typ,
21052 Ifaces_List => List_2,
21053 Exclude_Parents => True,
21054 Use_Full_View => True);
21055
21056 -- Join the two lists. Avoid duplications because an interface may
21057 -- simultaneously be parent and progenitor of a type.
21058
21059 Elmt := First_Elmt (List_2);
21060 while Present (Elmt) loop
21061 Append_Unique_Elmt (Node (Elmt), List_1);
21062 Next_Elmt (Elmt);
21063 end loop;
21064
21065 return List_1;
21066 end Visible_Ancestors;
21067
21068 ----------------------
21069 -- Within_Init_Proc --
21070 ----------------------
21071
21072 function Within_Init_Proc return Boolean is
21073 S : Entity_Id;
21074
21075 begin
21076 S := Current_Scope;
21077 while not Is_Overloadable (S) loop
21078 if S = Standard_Standard then
21079 return False;
21080 else
21081 S := Scope (S);
21082 end if;
21083 end loop;
21084
21085 return Is_Init_Proc (S);
21086 end Within_Init_Proc;
21087
21088 ------------------
21089 -- Within_Scope --
21090 ------------------
21091
21092 function Within_Scope (E : Entity_Id; S : Entity_Id) return Boolean is
21093 begin
21094 return Scope_Within_Or_Same (Scope (E), S);
21095 end Within_Scope;
21096
21097 ----------------
21098 -- Wrong_Type --
21099 ----------------
21100
21101 procedure Wrong_Type (Expr : Node_Id; Expected_Type : Entity_Id) is
21102 Found_Type : constant Entity_Id := First_Subtype (Etype (Expr));
21103 Expec_Type : constant Entity_Id := First_Subtype (Expected_Type);
21104
21105 Matching_Field : Entity_Id;
21106 -- Entity to give a more precise suggestion on how to write a one-
21107 -- element positional aggregate.
21108
21109 function Has_One_Matching_Field return Boolean;
21110 -- Determines if Expec_Type is a record type with a single component or
21111 -- discriminant whose type matches the found type or is one dimensional
21112 -- array whose component type matches the found type. In the case of
21113 -- one discriminant, we ignore the variant parts. That's not accurate,
21114 -- but good enough for the warning.
21115
21116 ----------------------------
21117 -- Has_One_Matching_Field --
21118 ----------------------------
21119
21120 function Has_One_Matching_Field return Boolean is
21121 E : Entity_Id;
21122
21123 begin
21124 Matching_Field := Empty;
21125
21126 if Is_Array_Type (Expec_Type)
21127 and then Number_Dimensions (Expec_Type) = 1
21128 and then Covers (Etype (Component_Type (Expec_Type)), Found_Type)
21129 then
21130 -- Use type name if available. This excludes multidimensional
21131 -- arrays and anonymous arrays.
21132
21133 if Comes_From_Source (Expec_Type) then
21134 Matching_Field := Expec_Type;
21135
21136 -- For an assignment, use name of target
21137
21138 elsif Nkind (Parent (Expr)) = N_Assignment_Statement
21139 and then Is_Entity_Name (Name (Parent (Expr)))
21140 then
21141 Matching_Field := Entity (Name (Parent (Expr)));
21142 end if;
21143
21144 return True;
21145
21146 elsif not Is_Record_Type (Expec_Type) then
21147 return False;
21148
21149 else
21150 E := First_Entity (Expec_Type);
21151 loop
21152 if No (E) then
21153 return False;
21154
21155 elsif not Ekind_In (E, E_Discriminant, E_Component)
21156 or else Nam_In (Chars (E), Name_uTag, Name_uParent)
21157 then
21158 Next_Entity (E);
21159
21160 else
21161 exit;
21162 end if;
21163 end loop;
21164
21165 if not Covers (Etype (E), Found_Type) then
21166 return False;
21167
21168 elsif Present (Next_Entity (E))
21169 and then (Ekind (E) = E_Component
21170 or else Ekind (Next_Entity (E)) = E_Discriminant)
21171 then
21172 return False;
21173
21174 else
21175 Matching_Field := E;
21176 return True;
21177 end if;
21178 end if;
21179 end Has_One_Matching_Field;
21180
21181 -- Start of processing for Wrong_Type
21182
21183 begin
21184 -- Don't output message if either type is Any_Type, or if a message
21185 -- has already been posted for this node. We need to do the latter
21186 -- check explicitly (it is ordinarily done in Errout), because we
21187 -- are using ! to force the output of the error messages.
21188
21189 if Expec_Type = Any_Type
21190 or else Found_Type = Any_Type
21191 or else Error_Posted (Expr)
21192 then
21193 return;
21194
21195 -- If one of the types is a Taft-Amendment type and the other it its
21196 -- completion, it must be an illegal use of a TAT in the spec, for
21197 -- which an error was already emitted. Avoid cascaded errors.
21198
21199 elsif Is_Incomplete_Type (Expec_Type)
21200 and then Has_Completion_In_Body (Expec_Type)
21201 and then Full_View (Expec_Type) = Etype (Expr)
21202 then
21203 return;
21204
21205 elsif Is_Incomplete_Type (Etype (Expr))
21206 and then Has_Completion_In_Body (Etype (Expr))
21207 and then Full_View (Etype (Expr)) = Expec_Type
21208 then
21209 return;
21210
21211 -- In an instance, there is an ongoing problem with completion of
21212 -- type derived from private types. Their structure is what Gigi
21213 -- expects, but the Etype is the parent type rather than the
21214 -- derived private type itself. Do not flag error in this case. The
21215 -- private completion is an entity without a parent, like an Itype.
21216 -- Similarly, full and partial views may be incorrect in the instance.
21217 -- There is no simple way to insure that it is consistent ???
21218
21219 -- A similar view discrepancy can happen in an inlined body, for the
21220 -- same reason: inserted body may be outside of the original package
21221 -- and only partial views are visible at the point of insertion.
21222
21223 elsif In_Instance or else In_Inlined_Body then
21224 if Etype (Etype (Expr)) = Etype (Expected_Type)
21225 and then
21226 (Has_Private_Declaration (Expected_Type)
21227 or else Has_Private_Declaration (Etype (Expr)))
21228 and then No (Parent (Expected_Type))
21229 then
21230 return;
21231
21232 elsif Nkind (Parent (Expr)) = N_Qualified_Expression
21233 and then Entity (Subtype_Mark (Parent (Expr))) = Expected_Type
21234 then
21235 return;
21236
21237 elsif Is_Private_Type (Expected_Type)
21238 and then Present (Full_View (Expected_Type))
21239 and then Covers (Full_View (Expected_Type), Etype (Expr))
21240 then
21241 return;
21242
21243 -- Conversely, type of expression may be the private one
21244
21245 elsif Is_Private_Type (Base_Type (Etype (Expr)))
21246 and then Full_View (Base_Type (Etype (Expr))) = Expected_Type
21247 then
21248 return;
21249 end if;
21250 end if;
21251
21252 -- An interesting special check. If the expression is parenthesized
21253 -- and its type corresponds to the type of the sole component of the
21254 -- expected record type, or to the component type of the expected one
21255 -- dimensional array type, then assume we have a bad aggregate attempt.
21256
21257 if Nkind (Expr) in N_Subexpr
21258 and then Paren_Count (Expr) /= 0
21259 and then Has_One_Matching_Field
21260 then
21261 Error_Msg_N ("positional aggregate cannot have one component", Expr);
21262
21263 if Present (Matching_Field) then
21264 if Is_Array_Type (Expec_Type) then
21265 Error_Msg_NE
21266 ("\write instead `&''First ='> ...`", Expr, Matching_Field);
21267 else
21268 Error_Msg_NE
21269 ("\write instead `& ='> ...`", Expr, Matching_Field);
21270 end if;
21271 end if;
21272
21273 -- Another special check, if we are looking for a pool-specific access
21274 -- type and we found an E_Access_Attribute_Type, then we have the case
21275 -- of an Access attribute being used in a context which needs a pool-
21276 -- specific type, which is never allowed. The one extra check we make
21277 -- is that the expected designated type covers the Found_Type.
21278
21279 elsif Is_Access_Type (Expec_Type)
21280 and then Ekind (Found_Type) = E_Access_Attribute_Type
21281 and then Ekind (Base_Type (Expec_Type)) /= E_General_Access_Type
21282 and then Ekind (Base_Type (Expec_Type)) /= E_Anonymous_Access_Type
21283 and then Covers
21284 (Designated_Type (Expec_Type), Designated_Type (Found_Type))
21285 then
21286 Error_Msg_N -- CODEFIX
21287 ("result must be general access type!", Expr);
21288 Error_Msg_NE -- CODEFIX
21289 ("add ALL to }!", Expr, Expec_Type);
21290
21291 -- Another special check, if the expected type is an integer type,
21292 -- but the expression is of type System.Address, and the parent is
21293 -- an addition or subtraction operation whose left operand is the
21294 -- expression in question and whose right operand is of an integral
21295 -- type, then this is an attempt at address arithmetic, so give
21296 -- appropriate message.
21297
21298 elsif Is_Integer_Type (Expec_Type)
21299 and then Is_RTE (Found_Type, RE_Address)
21300 and then Nkind_In (Parent (Expr), N_Op_Add, N_Op_Subtract)
21301 and then Expr = Left_Opnd (Parent (Expr))
21302 and then Is_Integer_Type (Etype (Right_Opnd (Parent (Expr))))
21303 then
21304 Error_Msg_N
21305 ("address arithmetic not predefined in package System",
21306 Parent (Expr));
21307 Error_Msg_N
21308 ("\possible missing with/use of System.Storage_Elements",
21309 Parent (Expr));
21310 return;
21311
21312 -- If the expected type is an anonymous access type, as for access
21313 -- parameters and discriminants, the error is on the designated types.
21314
21315 elsif Ekind (Expec_Type) = E_Anonymous_Access_Type then
21316 if Comes_From_Source (Expec_Type) then
21317 Error_Msg_NE ("expected}!", Expr, Expec_Type);
21318 else
21319 Error_Msg_NE
21320 ("expected an access type with designated}",
21321 Expr, Designated_Type (Expec_Type));
21322 end if;
21323
21324 if Is_Access_Type (Found_Type)
21325 and then not Comes_From_Source (Found_Type)
21326 then
21327 Error_Msg_NE
21328 ("\\found an access type with designated}!",
21329 Expr, Designated_Type (Found_Type));
21330 else
21331 if From_Limited_With (Found_Type) then
21332 Error_Msg_NE ("\\found incomplete}!", Expr, Found_Type);
21333 Error_Msg_Qual_Level := 99;
21334 Error_Msg_NE -- CODEFIX
21335 ("\\missing `WITH &;", Expr, Scope (Found_Type));
21336 Error_Msg_Qual_Level := 0;
21337 else
21338 Error_Msg_NE ("found}!", Expr, Found_Type);
21339 end if;
21340 end if;
21341
21342 -- Normal case of one type found, some other type expected
21343
21344 else
21345 -- If the names of the two types are the same, see if some number
21346 -- of levels of qualification will help. Don't try more than three
21347 -- levels, and if we get to standard, it's no use (and probably
21348 -- represents an error in the compiler) Also do not bother with
21349 -- internal scope names.
21350
21351 declare
21352 Expec_Scope : Entity_Id;
21353 Found_Scope : Entity_Id;
21354
21355 begin
21356 Expec_Scope := Expec_Type;
21357 Found_Scope := Found_Type;
21358
21359 for Levels in Nat range 0 .. 3 loop
21360 if Chars (Expec_Scope) /= Chars (Found_Scope) then
21361 Error_Msg_Qual_Level := Levels;
21362 exit;
21363 end if;
21364
21365 Expec_Scope := Scope (Expec_Scope);
21366 Found_Scope := Scope (Found_Scope);
21367
21368 exit when Expec_Scope = Standard_Standard
21369 or else Found_Scope = Standard_Standard
21370 or else not Comes_From_Source (Expec_Scope)
21371 or else not Comes_From_Source (Found_Scope);
21372 end loop;
21373 end;
21374
21375 if Is_Record_Type (Expec_Type)
21376 and then Present (Corresponding_Remote_Type (Expec_Type))
21377 then
21378 Error_Msg_NE ("expected}!", Expr,
21379 Corresponding_Remote_Type (Expec_Type));
21380 else
21381 Error_Msg_NE ("expected}!", Expr, Expec_Type);
21382 end if;
21383
21384 if Is_Entity_Name (Expr)
21385 and then Is_Package_Or_Generic_Package (Entity (Expr))
21386 then
21387 Error_Msg_N ("\\found package name!", Expr);
21388
21389 elsif Is_Entity_Name (Expr)
21390 and then Ekind_In (Entity (Expr), E_Procedure, E_Generic_Procedure)
21391 then
21392 if Ekind (Expec_Type) = E_Access_Subprogram_Type then
21393 Error_Msg_N
21394 ("found procedure name, possibly missing Access attribute!",
21395 Expr);
21396 else
21397 Error_Msg_N
21398 ("\\found procedure name instead of function!", Expr);
21399 end if;
21400
21401 elsif Nkind (Expr) = N_Function_Call
21402 and then Ekind (Expec_Type) = E_Access_Subprogram_Type
21403 and then Etype (Designated_Type (Expec_Type)) = Etype (Expr)
21404 and then No (Parameter_Associations (Expr))
21405 then
21406 Error_Msg_N
21407 ("found function name, possibly missing Access attribute!",
21408 Expr);
21409
21410 -- Catch common error: a prefix or infix operator which is not
21411 -- directly visible because the type isn't.
21412
21413 elsif Nkind (Expr) in N_Op
21414 and then Is_Overloaded (Expr)
21415 and then not Is_Immediately_Visible (Expec_Type)
21416 and then not Is_Potentially_Use_Visible (Expec_Type)
21417 and then not In_Use (Expec_Type)
21418 and then Has_Compatible_Type (Right_Opnd (Expr), Expec_Type)
21419 then
21420 Error_Msg_N
21421 ("operator of the type is not directly visible!", Expr);
21422
21423 elsif Ekind (Found_Type) = E_Void
21424 and then Present (Parent (Found_Type))
21425 and then Nkind (Parent (Found_Type)) = N_Full_Type_Declaration
21426 then
21427 Error_Msg_NE ("\\found premature usage of}!", Expr, Found_Type);
21428
21429 else
21430 Error_Msg_NE ("\\found}!", Expr, Found_Type);
21431 end if;
21432
21433 -- A special check for cases like M1 and M2 = 0 where M1 and M2 are
21434 -- of the same modular type, and (M1 and M2) = 0 was intended.
21435
21436 if Expec_Type = Standard_Boolean
21437 and then Is_Modular_Integer_Type (Found_Type)
21438 and then Nkind_In (Parent (Expr), N_Op_And, N_Op_Or, N_Op_Xor)
21439 and then Nkind (Right_Opnd (Parent (Expr))) in N_Op_Compare
21440 then
21441 declare
21442 Op : constant Node_Id := Right_Opnd (Parent (Expr));
21443 L : constant Node_Id := Left_Opnd (Op);
21444 R : constant Node_Id := Right_Opnd (Op);
21445
21446 begin
21447 -- The case for the message is when the left operand of the
21448 -- comparison is the same modular type, or when it is an
21449 -- integer literal (or other universal integer expression),
21450 -- which would have been typed as the modular type if the
21451 -- parens had been there.
21452
21453 if (Etype (L) = Found_Type
21454 or else
21455 Etype (L) = Universal_Integer)
21456 and then Is_Integer_Type (Etype (R))
21457 then
21458 Error_Msg_N
21459 ("\\possible missing parens for modular operation", Expr);
21460 end if;
21461 end;
21462 end if;
21463
21464 -- Reset error message qualification indication
21465
21466 Error_Msg_Qual_Level := 0;
21467 end if;
21468 end Wrong_Type;
21469
21470 --------------------------------
21471 -- Yields_Synchronized_Object --
21472 --------------------------------
21473
21474 function Yields_Synchronized_Object (Typ : Entity_Id) return Boolean is
21475 Has_Sync_Comp : Boolean := False;
21476 Id : Entity_Id;
21477
21478 begin
21479 -- An array type yields a synchronized object if its component type
21480 -- yields a synchronized object.
21481
21482 if Is_Array_Type (Typ) then
21483 return Yields_Synchronized_Object (Component_Type (Typ));
21484
21485 -- A descendant of type Ada.Synchronous_Task_Control.Suspension_Object
21486 -- yields a synchronized object by default.
21487
21488 elsif Is_Descendant_Of_Suspension_Object (Typ) then
21489 return True;
21490
21491 -- A protected type yields a synchronized object by default
21492
21493 elsif Is_Protected_Type (Typ) then
21494 return True;
21495
21496 -- A record type or type extension yields a synchronized object when its
21497 -- discriminants (if any) lack default values and all components are of
21498 -- a type that yelds a synchronized object.
21499
21500 elsif Is_Record_Type (Typ) then
21501
21502 -- Inspect all entities defined in the scope of the type, looking for
21503 -- components of a type that does not yeld a synchronized object or
21504 -- for discriminants with default values.
21505
21506 Id := First_Entity (Typ);
21507 while Present (Id) loop
21508 if Comes_From_Source (Id) then
21509 if Ekind (Id) = E_Component then
21510 if Yields_Synchronized_Object (Etype (Id)) then
21511 Has_Sync_Comp := True;
21512
21513 -- The component does not yield a synchronized object
21514
21515 else
21516 return False;
21517 end if;
21518
21519 elsif Ekind (Id) = E_Discriminant
21520 and then Present (Expression (Parent (Id)))
21521 then
21522 return False;
21523 end if;
21524 end if;
21525
21526 Next_Entity (Id);
21527 end loop;
21528
21529 -- Ensure that the parent type of a type extension yields a
21530 -- synchronized object.
21531
21532 if Etype (Typ) /= Typ
21533 and then not Yields_Synchronized_Object (Etype (Typ))
21534 then
21535 return False;
21536 end if;
21537
21538 -- If we get here, then all discriminants lack default values and all
21539 -- components are of a type that yields a synchronized object.
21540
21541 return Has_Sync_Comp;
21542
21543 -- A synchronized interface type yields a synchronized object by default
21544
21545 elsif Is_Synchronized_Interface (Typ) then
21546 return True;
21547
21548 -- A task type yelds a synchronized object by default
21549
21550 elsif Is_Task_Type (Typ) then
21551 return True;
21552
21553 -- Otherwise the type does not yield a synchronized object
21554
21555 else
21556 return False;
21557 end if;
21558 end Yields_Synchronized_Object;
21559
21560 ---------------------------
21561 -- Yields_Universal_Type --
21562 ---------------------------
21563
21564 function Yields_Universal_Type (N : Node_Id) return Boolean is
21565 begin
21566 -- Integer and real literals are of a universal type
21567
21568 if Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
21569 return True;
21570
21571 -- The values of certain attributes are of a universal type
21572
21573 elsif Nkind (N) = N_Attribute_Reference then
21574 return
21575 Universal_Type_Attribute (Get_Attribute_Id (Attribute_Name (N)));
21576
21577 -- ??? There are possibly other cases to consider
21578
21579 else
21580 return False;
21581 end if;
21582 end Yields_Universal_Type;
21583
21584 end Sem_Util;