00dfd6d99fe4a5f37a5691a0f9cff6e3fa5a26d5
[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-2017, 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 -- Local Subprograms --
76 -----------------------
77
78 function Build_Component_Subtype
79 (C : List_Id;
80 Loc : Source_Ptr;
81 T : Entity_Id) return Node_Id;
82 -- This function builds the subtype for Build_Actual_Subtype_Of_Component
83 -- and Build_Discriminal_Subtype_Of_Component. C is a list of constraints,
84 -- Loc is the source location, T is the original subtype.
85
86 function Has_Enabled_Property
87 (Item_Id : Entity_Id;
88 Property : Name_Id) return Boolean;
89 -- Subsidiary to routines Async_xxx_Enabled and Effective_xxx_Enabled.
90 -- Determine whether an abstract state or a variable denoted by entity
91 -- Item_Id has enabled property Property.
92
93 function Has_Null_Extension (T : Entity_Id) return Boolean;
94 -- T is a derived tagged type. Check whether the type extension is null.
95 -- If the parent type is fully initialized, T can be treated as such.
96
97 function Is_Fully_Initialized_Variant (Typ : Entity_Id) return Boolean;
98 -- Subsidiary to Is_Fully_Initialized_Type. For an unconstrained type
99 -- with discriminants whose default values are static, examine only the
100 -- components in the selected variant to determine whether all of them
101 -- have a default.
102
103 type Null_Status_Kind is
104 (Is_Null,
105 -- This value indicates that a subexpression is known to have a null
106 -- value at compile time.
107
108 Is_Non_Null,
109 -- This value indicates that a subexpression is known to have a non-null
110 -- value at compile time.
111
112 Unknown);
113 -- This value indicates that it cannot be determined at compile time
114 -- whether a subexpression yields a null or non-null value.
115
116 function Null_Status (N : Node_Id) return Null_Status_Kind;
117 -- Determine whether subexpression N of an access type yields a null value,
118 -- a non-null value, or the value cannot be determined at compile time. The
119 -- routine does not take simple flow diagnostics into account, it relies on
120 -- static facts such as the presence of null exclusions.
121
122 function Old_Requires_Transient_Scope (Id : Entity_Id) return Boolean;
123 function New_Requires_Transient_Scope (Id : Entity_Id) return Boolean;
124 -- ???We retain the old and new algorithms for Requires_Transient_Scope for
125 -- the time being. New_Requires_Transient_Scope is used by default; the
126 -- debug switch -gnatdQ can be used to do Old_Requires_Transient_Scope
127 -- instead. The intent is to use this temporarily to measure before/after
128 -- efficiency. Note: when this temporary code is removed, the documentation
129 -- of dQ in debug.adb should be removed.
130
131 procedure Results_Differ
132 (Id : Entity_Id;
133 Old_Val : Boolean;
134 New_Val : Boolean);
135 -- ???Debugging code. Called when the Old_Val and New_Val differ. This
136 -- routine will be removed eventially when New_Requires_Transient_Scope
137 -- becomes Requires_Transient_Scope and Old_Requires_Transient_Scope is
138 -- eliminated.
139
140 ------------------------------
141 -- Abstract_Interface_List --
142 ------------------------------
143
144 function Abstract_Interface_List (Typ : Entity_Id) return List_Id is
145 Nod : Node_Id;
146
147 begin
148 if Is_Concurrent_Type (Typ) then
149
150 -- If we are dealing with a synchronized subtype, go to the base
151 -- type, whose declaration has the interface list.
152
153 -- Shouldn't this be Declaration_Node???
154
155 Nod := Parent (Base_Type (Typ));
156
157 if Nkind (Nod) = N_Full_Type_Declaration then
158 return Empty_List;
159 end if;
160
161 elsif Ekind (Typ) = E_Record_Type_With_Private then
162 if Nkind (Parent (Typ)) = N_Full_Type_Declaration then
163 Nod := Type_Definition (Parent (Typ));
164
165 elsif Nkind (Parent (Typ)) = N_Private_Type_Declaration then
166 if Present (Full_View (Typ))
167 and then
168 Nkind (Parent (Full_View (Typ))) = N_Full_Type_Declaration
169 then
170 Nod := Type_Definition (Parent (Full_View (Typ)));
171
172 -- If the full-view is not available we cannot do anything else
173 -- here (the source has errors).
174
175 else
176 return Empty_List;
177 end if;
178
179 -- Support for generic formals with interfaces is still missing ???
180
181 elsif Nkind (Parent (Typ)) = N_Formal_Type_Declaration then
182 return Empty_List;
183
184 else
185 pragma Assert
186 (Nkind (Parent (Typ)) = N_Private_Extension_Declaration);
187 Nod := Parent (Typ);
188 end if;
189
190 elsif Ekind (Typ) = E_Record_Subtype then
191 Nod := Type_Definition (Parent (Etype (Typ)));
192
193 elsif Ekind (Typ) = E_Record_Subtype_With_Private then
194
195 -- Recurse, because parent may still be a private extension. Also
196 -- note that the full view of the subtype or the full view of its
197 -- base type may (both) be unavailable.
198
199 return Abstract_Interface_List (Etype (Typ));
200
201 else pragma Assert ((Ekind (Typ)) = E_Record_Type);
202 if Nkind (Parent (Typ)) = N_Formal_Type_Declaration then
203 Nod := Formal_Type_Definition (Parent (Typ));
204 else
205 Nod := Type_Definition (Parent (Typ));
206 end if;
207 end if;
208
209 return Interface_List (Nod);
210 end Abstract_Interface_List;
211
212 --------------------------------
213 -- Add_Access_Type_To_Process --
214 --------------------------------
215
216 procedure Add_Access_Type_To_Process (E : Entity_Id; A : Entity_Id) is
217 L : Elist_Id;
218
219 begin
220 Ensure_Freeze_Node (E);
221 L := Access_Types_To_Process (Freeze_Node (E));
222
223 if No (L) then
224 L := New_Elmt_List;
225 Set_Access_Types_To_Process (Freeze_Node (E), L);
226 end if;
227
228 Append_Elmt (A, L);
229 end Add_Access_Type_To_Process;
230
231 --------------------------
232 -- Add_Block_Identifier --
233 --------------------------
234
235 procedure Add_Block_Identifier (N : Node_Id; Id : out Entity_Id) is
236 Loc : constant Source_Ptr := Sloc (N);
237
238 begin
239 pragma Assert (Nkind (N) = N_Block_Statement);
240
241 -- The block already has a label, return its entity
242
243 if Present (Identifier (N)) then
244 Id := Entity (Identifier (N));
245
246 -- Create a new block label and set its attributes
247
248 else
249 Id := New_Internal_Entity (E_Block, Current_Scope, Loc, 'B');
250 Set_Etype (Id, Standard_Void_Type);
251 Set_Parent (Id, N);
252
253 Set_Identifier (N, New_Occurrence_Of (Id, Loc));
254 Set_Block_Node (Id, Identifier (N));
255 end if;
256 end Add_Block_Identifier;
257
258 ----------------------------
259 -- Add_Global_Declaration --
260 ----------------------------
261
262 procedure Add_Global_Declaration (N : Node_Id) is
263 Aux_Node : constant Node_Id := Aux_Decls_Node (Cunit (Current_Sem_Unit));
264
265 begin
266 if No (Declarations (Aux_Node)) then
267 Set_Declarations (Aux_Node, New_List);
268 end if;
269
270 Append_To (Declarations (Aux_Node), N);
271 Analyze (N);
272 end Add_Global_Declaration;
273
274 --------------------------------
275 -- Address_Integer_Convert_OK --
276 --------------------------------
277
278 function Address_Integer_Convert_OK (T1, T2 : Entity_Id) return Boolean is
279 begin
280 if Allow_Integer_Address
281 and then ((Is_Descendant_Of_Address (T1)
282 and then Is_Private_Type (T1)
283 and then Is_Integer_Type (T2))
284 or else
285 (Is_Descendant_Of_Address (T2)
286 and then Is_Private_Type (T2)
287 and then Is_Integer_Type (T1)))
288 then
289 return True;
290 else
291 return False;
292 end if;
293 end Address_Integer_Convert_OK;
294
295 -------------------
296 -- Address_Value --
297 -------------------
298
299 function Address_Value (N : Node_Id) return Node_Id is
300 Expr : Node_Id := N;
301
302 begin
303 loop
304 -- For constant, get constant expression
305
306 if Is_Entity_Name (Expr)
307 and then Ekind (Entity (Expr)) = E_Constant
308 then
309 Expr := Constant_Value (Entity (Expr));
310
311 -- For unchecked conversion, get result to convert
312
313 elsif Nkind (Expr) = N_Unchecked_Type_Conversion then
314 Expr := Expression (Expr);
315
316 -- For (common case) of To_Address call, get argument
317
318 elsif Nkind (Expr) = N_Function_Call
319 and then Is_Entity_Name (Name (Expr))
320 and then Is_RTE (Entity (Name (Expr)), RE_To_Address)
321 then
322 Expr := First (Parameter_Associations (Expr));
323
324 if Nkind (Expr) = N_Parameter_Association then
325 Expr := Explicit_Actual_Parameter (Expr);
326 end if;
327
328 -- We finally have the real expression
329
330 else
331 exit;
332 end if;
333 end loop;
334
335 return Expr;
336 end Address_Value;
337
338 -----------------
339 -- Addressable --
340 -----------------
341
342 -- For now, just 8/16/32/64
343
344 function Addressable (V : Uint) return Boolean is
345 begin
346 return V = Uint_8 or else
347 V = Uint_16 or else
348 V = Uint_32 or else
349 V = Uint_64;
350 end Addressable;
351
352 function Addressable (V : Int) return Boolean is
353 begin
354 return V = 8 or else
355 V = 16 or else
356 V = 32 or else
357 V = 64;
358 end Addressable;
359
360 ---------------------------------
361 -- Aggregate_Constraint_Checks --
362 ---------------------------------
363
364 procedure Aggregate_Constraint_Checks
365 (Exp : Node_Id;
366 Check_Typ : Entity_Id)
367 is
368 Exp_Typ : constant Entity_Id := Etype (Exp);
369
370 begin
371 if Raises_Constraint_Error (Exp) then
372 return;
373 end if;
374
375 -- Ada 2005 (AI-230): Generate a conversion to an anonymous access
376 -- component's type to force the appropriate accessibility checks.
377
378 -- Ada 2005 (AI-231): Generate conversion to the null-excluding type to
379 -- force the corresponding run-time check
380
381 if Is_Access_Type (Check_Typ)
382 and then Is_Local_Anonymous_Access (Check_Typ)
383 then
384 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
385 Analyze_And_Resolve (Exp, Check_Typ);
386 Check_Unset_Reference (Exp);
387 end if;
388
389 -- What follows is really expansion activity, so check that expansion
390 -- is on and is allowed. In GNATprove mode, we also want check flags to
391 -- be added in the tree, so that the formal verification can rely on
392 -- those to be present. In GNATprove mode for formal verification, some
393 -- treatment typically only done during expansion needs to be performed
394 -- on the tree, but it should not be applied inside generics. Otherwise,
395 -- this breaks the name resolution mechanism for generic instances.
396
397 if not Expander_Active
398 and (Inside_A_Generic or not Full_Analysis or not GNATprove_Mode)
399 then
400 return;
401 end if;
402
403 if Is_Access_Type (Check_Typ)
404 and then Can_Never_Be_Null (Check_Typ)
405 and then not Can_Never_Be_Null (Exp_Typ)
406 then
407 Install_Null_Excluding_Check (Exp);
408 end if;
409
410 -- First check if we have to insert discriminant checks
411
412 if Has_Discriminants (Exp_Typ) then
413 Apply_Discriminant_Check (Exp, Check_Typ);
414
415 -- Next emit length checks for array aggregates
416
417 elsif Is_Array_Type (Exp_Typ) then
418 Apply_Length_Check (Exp, Check_Typ);
419
420 -- Finally emit scalar and string checks. If we are dealing with a
421 -- scalar literal we need to check by hand because the Etype of
422 -- literals is not necessarily correct.
423
424 elsif Is_Scalar_Type (Exp_Typ)
425 and then Compile_Time_Known_Value (Exp)
426 then
427 if Is_Out_Of_Range (Exp, Base_Type (Check_Typ)) then
428 Apply_Compile_Time_Constraint_Error
429 (Exp, "value not in range of}??", CE_Range_Check_Failed,
430 Ent => Base_Type (Check_Typ),
431 Typ => Base_Type (Check_Typ));
432
433 elsif Is_Out_Of_Range (Exp, Check_Typ) then
434 Apply_Compile_Time_Constraint_Error
435 (Exp, "value not in range of}??", CE_Range_Check_Failed,
436 Ent => Check_Typ,
437 Typ => Check_Typ);
438
439 elsif not Range_Checks_Suppressed (Check_Typ) then
440 Apply_Scalar_Range_Check (Exp, Check_Typ);
441 end if;
442
443 -- Verify that target type is also scalar, to prevent view anomalies
444 -- in instantiations.
445
446 elsif (Is_Scalar_Type (Exp_Typ)
447 or else Nkind (Exp) = N_String_Literal)
448 and then Is_Scalar_Type (Check_Typ)
449 and then Exp_Typ /= Check_Typ
450 then
451 if Is_Entity_Name (Exp)
452 and then Ekind (Entity (Exp)) = E_Constant
453 then
454 -- If expression is a constant, it is worthwhile checking whether
455 -- it is a bound of the type.
456
457 if (Is_Entity_Name (Type_Low_Bound (Check_Typ))
458 and then Entity (Exp) = Entity (Type_Low_Bound (Check_Typ)))
459 or else
460 (Is_Entity_Name (Type_High_Bound (Check_Typ))
461 and then Entity (Exp) = Entity (Type_High_Bound (Check_Typ)))
462 then
463 return;
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 -- Could use a comment on this case ???
472
473 else
474 Rewrite (Exp, Convert_To (Check_Typ, Relocate_Node (Exp)));
475 Analyze_And_Resolve (Exp, Check_Typ);
476 Check_Unset_Reference (Exp);
477 end if;
478
479 end if;
480 end Aggregate_Constraint_Checks;
481
482 -----------------------
483 -- Alignment_In_Bits --
484 -----------------------
485
486 function Alignment_In_Bits (E : Entity_Id) return Uint is
487 begin
488 return Alignment (E) * System_Storage_Unit;
489 end Alignment_In_Bits;
490
491 --------------------------------------
492 -- All_Composite_Constraints_Static --
493 --------------------------------------
494
495 function All_Composite_Constraints_Static
496 (Constr : Node_Id) return Boolean
497 is
498 begin
499 if No (Constr) or else Error_Posted (Constr) then
500 return True;
501 end if;
502
503 case Nkind (Constr) is
504 when N_Subexpr =>
505 if Nkind (Constr) in N_Has_Entity
506 and then Present (Entity (Constr))
507 then
508 if Is_Type (Entity (Constr)) then
509 return
510 not Is_Discrete_Type (Entity (Constr))
511 or else Is_OK_Static_Subtype (Entity (Constr));
512 end if;
513
514 elsif Nkind (Constr) = N_Range then
515 return
516 Is_OK_Static_Expression (Low_Bound (Constr))
517 and then
518 Is_OK_Static_Expression (High_Bound (Constr));
519
520 elsif Nkind (Constr) = N_Attribute_Reference
521 and then Attribute_Name (Constr) = Name_Range
522 then
523 return
524 Is_OK_Static_Expression
525 (Type_Low_Bound (Etype (Prefix (Constr))))
526 and then
527 Is_OK_Static_Expression
528 (Type_High_Bound (Etype (Prefix (Constr))));
529 end if;
530
531 return
532 not Present (Etype (Constr)) -- previous error
533 or else not Is_Discrete_Type (Etype (Constr))
534 or else Is_OK_Static_Expression (Constr);
535
536 when N_Discriminant_Association =>
537 return All_Composite_Constraints_Static (Expression (Constr));
538
539 when N_Range_Constraint =>
540 return
541 All_Composite_Constraints_Static (Range_Expression (Constr));
542
543 when N_Index_Or_Discriminant_Constraint =>
544 declare
545 One_Cstr : Entity_Id;
546 begin
547 One_Cstr := First (Constraints (Constr));
548 while Present (One_Cstr) loop
549 if not All_Composite_Constraints_Static (One_Cstr) then
550 return False;
551 end if;
552
553 Next (One_Cstr);
554 end loop;
555 end;
556
557 return True;
558
559 when N_Subtype_Indication =>
560 return
561 All_Composite_Constraints_Static (Subtype_Mark (Constr))
562 and then
563 All_Composite_Constraints_Static (Constraint (Constr));
564
565 when others =>
566 raise Program_Error;
567 end case;
568 end All_Composite_Constraints_Static;
569
570 ---------------------------------
571 -- Append_Inherited_Subprogram --
572 ---------------------------------
573
574 procedure Append_Inherited_Subprogram (S : Entity_Id) is
575 Par : constant Entity_Id := Alias (S);
576 -- The parent subprogram
577
578 Scop : constant Entity_Id := Scope (Par);
579 -- The scope of definition of the parent subprogram
580
581 Typ : constant Entity_Id := Defining_Entity (Parent (S));
582 -- The derived type of which S is a primitive operation
583
584 Decl : Node_Id;
585 Next_E : Entity_Id;
586
587 begin
588 if Ekind (Current_Scope) = E_Package
589 and then In_Private_Part (Current_Scope)
590 and then Has_Private_Declaration (Typ)
591 and then Is_Tagged_Type (Typ)
592 and then Scop = Current_Scope
593 then
594 -- The inherited operation is available at the earliest place after
595 -- the derived type declaration ( RM 7.3.1 (6/1)). This is only
596 -- relevant for type extensions. If the parent operation appears
597 -- after the type extension, the operation is not visible.
598
599 Decl := First
600 (Visible_Declarations
601 (Package_Specification (Current_Scope)));
602 while Present (Decl) loop
603 if Nkind (Decl) = N_Private_Extension_Declaration
604 and then Defining_Entity (Decl) = Typ
605 then
606 if Sloc (Decl) > Sloc (Par) then
607 Next_E := Next_Entity (Par);
608 Set_Next_Entity (Par, S);
609 Set_Next_Entity (S, Next_E);
610 return;
611
612 else
613 exit;
614 end if;
615 end if;
616
617 Next (Decl);
618 end loop;
619 end if;
620
621 -- If partial view is not a type extension, or it appears before the
622 -- subprogram declaration, insert normally at end of entity list.
623
624 Append_Entity (S, Current_Scope);
625 end Append_Inherited_Subprogram;
626
627 -----------------------------------------
628 -- Apply_Compile_Time_Constraint_Error --
629 -----------------------------------------
630
631 procedure Apply_Compile_Time_Constraint_Error
632 (N : Node_Id;
633 Msg : String;
634 Reason : RT_Exception_Code;
635 Ent : Entity_Id := Empty;
636 Typ : Entity_Id := Empty;
637 Loc : Source_Ptr := No_Location;
638 Rep : Boolean := True;
639 Warn : Boolean := False)
640 is
641 Stat : constant Boolean := Is_Static_Expression (N);
642 R_Stat : constant Node_Id :=
643 Make_Raise_Constraint_Error (Sloc (N), Reason => Reason);
644 Rtyp : Entity_Id;
645
646 begin
647 if No (Typ) then
648 Rtyp := Etype (N);
649 else
650 Rtyp := Typ;
651 end if;
652
653 Discard_Node
654 (Compile_Time_Constraint_Error (N, Msg, Ent, Loc, Warn => Warn));
655
656 -- In GNATprove mode, do not replace the node with an exception raised.
657 -- In such a case, either the call to Compile_Time_Constraint_Error
658 -- issues an error which stops analysis, or it issues a warning in
659 -- a few cases where a suitable check flag is set for GNATprove to
660 -- generate a check message.
661
662 if not Rep or GNATprove_Mode then
663 return;
664 end if;
665
666 -- Now we replace the node by an N_Raise_Constraint_Error node
667 -- This does not need reanalyzing, so set it as analyzed now.
668
669 Rewrite (N, R_Stat);
670 Set_Analyzed (N, True);
671
672 Set_Etype (N, Rtyp);
673 Set_Raises_Constraint_Error (N);
674
675 -- Now deal with possible local raise handling
676
677 Possible_Local_Raise (N, Standard_Constraint_Error);
678
679 -- If the original expression was marked as static, the result is
680 -- still marked as static, but the Raises_Constraint_Error flag is
681 -- always set so that further static evaluation is not attempted.
682
683 if Stat then
684 Set_Is_Static_Expression (N);
685 end if;
686 end Apply_Compile_Time_Constraint_Error;
687
688 ---------------------------
689 -- Async_Readers_Enabled --
690 ---------------------------
691
692 function Async_Readers_Enabled (Id : Entity_Id) return Boolean is
693 begin
694 return Has_Enabled_Property (Id, Name_Async_Readers);
695 end Async_Readers_Enabled;
696
697 ---------------------------
698 -- Async_Writers_Enabled --
699 ---------------------------
700
701 function Async_Writers_Enabled (Id : Entity_Id) return Boolean is
702 begin
703 return Has_Enabled_Property (Id, Name_Async_Writers);
704 end Async_Writers_Enabled;
705
706 --------------------------------------
707 -- Available_Full_View_Of_Component --
708 --------------------------------------
709
710 function Available_Full_View_Of_Component (T : Entity_Id) return Boolean is
711 ST : constant Entity_Id := Scope (T);
712 SCT : constant Entity_Id := Scope (Component_Type (T));
713 begin
714 return In_Open_Scopes (ST)
715 and then In_Open_Scopes (SCT)
716 and then Scope_Depth (ST) >= Scope_Depth (SCT);
717 end Available_Full_View_Of_Component;
718
719 -------------------
720 -- Bad_Attribute --
721 -------------------
722
723 procedure Bad_Attribute
724 (N : Node_Id;
725 Nam : Name_Id;
726 Warn : Boolean := False)
727 is
728 begin
729 Error_Msg_Warn := Warn;
730 Error_Msg_N ("unrecognized attribute&<<", N);
731
732 -- Check for possible misspelling
733
734 Error_Msg_Name_1 := First_Attribute_Name;
735 while Error_Msg_Name_1 <= Last_Attribute_Name loop
736 if Is_Bad_Spelling_Of (Nam, Error_Msg_Name_1) then
737 Error_Msg_N -- CODEFIX
738 ("\possible misspelling of %<<", N);
739 exit;
740 end if;
741
742 Error_Msg_Name_1 := Error_Msg_Name_1 + 1;
743 end loop;
744 end Bad_Attribute;
745
746 --------------------------------
747 -- Bad_Predicated_Subtype_Use --
748 --------------------------------
749
750 procedure Bad_Predicated_Subtype_Use
751 (Msg : String;
752 N : Node_Id;
753 Typ : Entity_Id;
754 Suggest_Static : Boolean := False)
755 is
756 Gen : Entity_Id;
757
758 begin
759 -- Avoid cascaded errors
760
761 if Error_Posted (N) then
762 return;
763 end if;
764
765 if Inside_A_Generic then
766 Gen := Current_Scope;
767 while Present (Gen) and then Ekind (Gen) /= E_Generic_Package loop
768 Gen := Scope (Gen);
769 end loop;
770
771 if No (Gen) then
772 return;
773 end if;
774
775 if Is_Generic_Formal (Typ) and then Is_Discrete_Type (Typ) then
776 Set_No_Predicate_On_Actual (Typ);
777 end if;
778
779 elsif Has_Predicates (Typ) then
780 if Is_Generic_Actual_Type (Typ) then
781
782 -- The restriction on loop parameters is only that the type
783 -- should have no dynamic predicates.
784
785 if Nkind (Parent (N)) = N_Loop_Parameter_Specification
786 and then not Has_Dynamic_Predicate_Aspect (Typ)
787 and then Is_OK_Static_Subtype (Typ)
788 then
789 return;
790 end if;
791
792 Gen := Current_Scope;
793 while not Is_Generic_Instance (Gen) loop
794 Gen := Scope (Gen);
795 end loop;
796
797 pragma Assert (Present (Gen));
798
799 if Ekind (Gen) = E_Package and then In_Package_Body (Gen) then
800 Error_Msg_Warn := SPARK_Mode /= On;
801 Error_Msg_FE (Msg & "<<", N, Typ);
802 Error_Msg_F ("\Program_Error [<<", N);
803
804 Insert_Action (N,
805 Make_Raise_Program_Error (Sloc (N),
806 Reason => PE_Bad_Predicated_Generic_Type));
807
808 else
809 Error_Msg_FE (Msg & "<<", N, Typ);
810 end if;
811
812 else
813 Error_Msg_FE (Msg, N, Typ);
814 end if;
815
816 -- Emit an optional suggestion on how to remedy the error if the
817 -- context warrants it.
818
819 if Suggest_Static and then Has_Static_Predicate (Typ) then
820 Error_Msg_FE ("\predicate of & should be marked static", N, Typ);
821 end if;
822 end if;
823 end Bad_Predicated_Subtype_Use;
824
825 -----------------------------------------
826 -- Bad_Unordered_Enumeration_Reference --
827 -----------------------------------------
828
829 function Bad_Unordered_Enumeration_Reference
830 (N : Node_Id;
831 T : Entity_Id) return Boolean
832 is
833 begin
834 return Is_Enumeration_Type (T)
835 and then Warn_On_Unordered_Enumeration_Type
836 and then not Is_Generic_Type (T)
837 and then Comes_From_Source (N)
838 and then not Has_Pragma_Ordered (T)
839 and then not In_Same_Extended_Unit (N, T);
840 end Bad_Unordered_Enumeration_Reference;
841
842 --------------------------
843 -- Build_Actual_Subtype --
844 --------------------------
845
846 function Build_Actual_Subtype
847 (T : Entity_Id;
848 N : Node_Or_Entity_Id) return Node_Id
849 is
850 Loc : Source_Ptr;
851 -- Normally Sloc (N), but may point to corresponding body in some cases
852
853 Constraints : List_Id;
854 Decl : Node_Id;
855 Discr : Entity_Id;
856 Hi : Node_Id;
857 Lo : Node_Id;
858 Subt : Entity_Id;
859 Disc_Type : Entity_Id;
860 Obj : Node_Id;
861
862 begin
863 Loc := Sloc (N);
864
865 if Nkind (N) = N_Defining_Identifier then
866 Obj := New_Occurrence_Of (N, Loc);
867
868 -- If this is a formal parameter of a subprogram declaration, and
869 -- we are compiling the body, we want the declaration for the
870 -- actual subtype to carry the source position of the body, to
871 -- prevent anomalies in gdb when stepping through the code.
872
873 if Is_Formal (N) then
874 declare
875 Decl : constant Node_Id := Unit_Declaration_Node (Scope (N));
876 begin
877 if Nkind (Decl) = N_Subprogram_Declaration
878 and then Present (Corresponding_Body (Decl))
879 then
880 Loc := Sloc (Corresponding_Body (Decl));
881 end if;
882 end;
883 end if;
884
885 else
886 Obj := N;
887 end if;
888
889 if Is_Array_Type (T) then
890 Constraints := New_List;
891 for J in 1 .. Number_Dimensions (T) loop
892
893 -- Build an array subtype declaration with the nominal subtype and
894 -- the bounds of the actual. Add the declaration in front of the
895 -- local declarations for the subprogram, for analysis before any
896 -- reference to the formal in the body.
897
898 Lo :=
899 Make_Attribute_Reference (Loc,
900 Prefix =>
901 Duplicate_Subexpr_No_Checks (Obj, Name_Req => True),
902 Attribute_Name => Name_First,
903 Expressions => New_List (
904 Make_Integer_Literal (Loc, J)));
905
906 Hi :=
907 Make_Attribute_Reference (Loc,
908 Prefix =>
909 Duplicate_Subexpr_No_Checks (Obj, Name_Req => True),
910 Attribute_Name => Name_Last,
911 Expressions => New_List (
912 Make_Integer_Literal (Loc, J)));
913
914 Append (Make_Range (Loc, Lo, Hi), Constraints);
915 end loop;
916
917 -- If the type has unknown discriminants there is no constrained
918 -- subtype to build. This is never called for a formal or for a
919 -- lhs, so returning the type is ok ???
920
921 elsif Has_Unknown_Discriminants (T) then
922 return T;
923
924 else
925 Constraints := New_List;
926
927 -- Type T is a generic derived type, inherit the discriminants from
928 -- the parent type.
929
930 if Is_Private_Type (T)
931 and then No (Full_View (T))
932
933 -- T was flagged as an error if it was declared as a formal
934 -- derived type with known discriminants. In this case there
935 -- is no need to look at the parent type since T already carries
936 -- its own discriminants.
937
938 and then not Error_Posted (T)
939 then
940 Disc_Type := Etype (Base_Type (T));
941 else
942 Disc_Type := T;
943 end if;
944
945 Discr := First_Discriminant (Disc_Type);
946 while Present (Discr) loop
947 Append_To (Constraints,
948 Make_Selected_Component (Loc,
949 Prefix =>
950 Duplicate_Subexpr_No_Checks (Obj),
951 Selector_Name => New_Occurrence_Of (Discr, Loc)));
952 Next_Discriminant (Discr);
953 end loop;
954 end if;
955
956 Subt := Make_Temporary (Loc, 'S', Related_Node => N);
957 Set_Is_Internal (Subt);
958
959 Decl :=
960 Make_Subtype_Declaration (Loc,
961 Defining_Identifier => Subt,
962 Subtype_Indication =>
963 Make_Subtype_Indication (Loc,
964 Subtype_Mark => New_Occurrence_Of (T, Loc),
965 Constraint =>
966 Make_Index_Or_Discriminant_Constraint (Loc,
967 Constraints => Constraints)));
968
969 Mark_Rewrite_Insertion (Decl);
970 return Decl;
971 end Build_Actual_Subtype;
972
973 ---------------------------------------
974 -- Build_Actual_Subtype_Of_Component --
975 ---------------------------------------
976
977 function Build_Actual_Subtype_Of_Component
978 (T : Entity_Id;
979 N : Node_Id) return Node_Id
980 is
981 Loc : constant Source_Ptr := Sloc (N);
982 P : constant Node_Id := Prefix (N);
983 D : Elmt_Id;
984 Id : Node_Id;
985 Index_Typ : Entity_Id;
986
987 Desig_Typ : Entity_Id;
988 -- This is either a copy of T, or if T is an access type, then it is
989 -- the directly designated type of this access type.
990
991 function Build_Actual_Array_Constraint return List_Id;
992 -- If one or more of the bounds of the component depends on
993 -- discriminants, build actual constraint using the discriminants
994 -- of the prefix.
995
996 function Build_Actual_Record_Constraint return List_Id;
997 -- Similar to previous one, for discriminated components constrained
998 -- by the discriminant of the enclosing object.
999
1000 -----------------------------------
1001 -- Build_Actual_Array_Constraint --
1002 -----------------------------------
1003
1004 function Build_Actual_Array_Constraint return List_Id is
1005 Constraints : constant List_Id := New_List;
1006 Indx : Node_Id;
1007 Hi : Node_Id;
1008 Lo : Node_Id;
1009 Old_Hi : Node_Id;
1010 Old_Lo : Node_Id;
1011
1012 begin
1013 Indx := First_Index (Desig_Typ);
1014 while Present (Indx) loop
1015 Old_Lo := Type_Low_Bound (Etype (Indx));
1016 Old_Hi := Type_High_Bound (Etype (Indx));
1017
1018 if Denotes_Discriminant (Old_Lo) then
1019 Lo :=
1020 Make_Selected_Component (Loc,
1021 Prefix => New_Copy_Tree (P),
1022 Selector_Name => New_Occurrence_Of (Entity (Old_Lo), Loc));
1023
1024 else
1025 Lo := New_Copy_Tree (Old_Lo);
1026
1027 -- The new bound will be reanalyzed in the enclosing
1028 -- declaration. For literal bounds that come from a type
1029 -- declaration, the type of the context must be imposed, so
1030 -- insure that analysis will take place. For non-universal
1031 -- types this is not strictly necessary.
1032
1033 Set_Analyzed (Lo, False);
1034 end if;
1035
1036 if Denotes_Discriminant (Old_Hi) then
1037 Hi :=
1038 Make_Selected_Component (Loc,
1039 Prefix => New_Copy_Tree (P),
1040 Selector_Name => New_Occurrence_Of (Entity (Old_Hi), Loc));
1041
1042 else
1043 Hi := New_Copy_Tree (Old_Hi);
1044 Set_Analyzed (Hi, False);
1045 end if;
1046
1047 Append (Make_Range (Loc, Lo, Hi), Constraints);
1048 Next_Index (Indx);
1049 end loop;
1050
1051 return Constraints;
1052 end Build_Actual_Array_Constraint;
1053
1054 ------------------------------------
1055 -- Build_Actual_Record_Constraint --
1056 ------------------------------------
1057
1058 function Build_Actual_Record_Constraint return List_Id is
1059 Constraints : constant List_Id := New_List;
1060 D : Elmt_Id;
1061 D_Val : Node_Id;
1062
1063 begin
1064 D := First_Elmt (Discriminant_Constraint (Desig_Typ));
1065 while Present (D) loop
1066 if Denotes_Discriminant (Node (D)) then
1067 D_Val := Make_Selected_Component (Loc,
1068 Prefix => New_Copy_Tree (P),
1069 Selector_Name => New_Occurrence_Of (Entity (Node (D)), Loc));
1070
1071 else
1072 D_Val := New_Copy_Tree (Node (D));
1073 end if;
1074
1075 Append (D_Val, Constraints);
1076 Next_Elmt (D);
1077 end loop;
1078
1079 return Constraints;
1080 end Build_Actual_Record_Constraint;
1081
1082 -- Start of processing for Build_Actual_Subtype_Of_Component
1083
1084 begin
1085 -- Why the test for Spec_Expression mode here???
1086
1087 if In_Spec_Expression then
1088 return Empty;
1089
1090 -- More comments for the rest of this body would be good ???
1091
1092 elsif Nkind (N) = N_Explicit_Dereference then
1093 if Is_Composite_Type (T)
1094 and then not Is_Constrained (T)
1095 and then not (Is_Class_Wide_Type (T)
1096 and then Is_Constrained (Root_Type (T)))
1097 and then not Has_Unknown_Discriminants (T)
1098 then
1099 -- If the type of the dereference is already constrained, it is an
1100 -- actual subtype.
1101
1102 if Is_Array_Type (Etype (N))
1103 and then Is_Constrained (Etype (N))
1104 then
1105 return Empty;
1106 else
1107 Remove_Side_Effects (P);
1108 return Build_Actual_Subtype (T, N);
1109 end if;
1110 else
1111 return Empty;
1112 end if;
1113 end if;
1114
1115 if Ekind (T) = E_Access_Subtype then
1116 Desig_Typ := Designated_Type (T);
1117 else
1118 Desig_Typ := T;
1119 end if;
1120
1121 if Ekind (Desig_Typ) = E_Array_Subtype then
1122 Id := First_Index (Desig_Typ);
1123 while Present (Id) loop
1124 Index_Typ := Underlying_Type (Etype (Id));
1125
1126 if Denotes_Discriminant (Type_Low_Bound (Index_Typ))
1127 or else
1128 Denotes_Discriminant (Type_High_Bound (Index_Typ))
1129 then
1130 Remove_Side_Effects (P);
1131 return
1132 Build_Component_Subtype
1133 (Build_Actual_Array_Constraint, Loc, Base_Type (T));
1134 end if;
1135
1136 Next_Index (Id);
1137 end loop;
1138
1139 elsif Is_Composite_Type (Desig_Typ)
1140 and then Has_Discriminants (Desig_Typ)
1141 and then not Has_Unknown_Discriminants (Desig_Typ)
1142 then
1143 if Is_Private_Type (Desig_Typ)
1144 and then No (Discriminant_Constraint (Desig_Typ))
1145 then
1146 Desig_Typ := Full_View (Desig_Typ);
1147 end if;
1148
1149 D := First_Elmt (Discriminant_Constraint (Desig_Typ));
1150 while Present (D) loop
1151 if Denotes_Discriminant (Node (D)) then
1152 Remove_Side_Effects (P);
1153 return
1154 Build_Component_Subtype (
1155 Build_Actual_Record_Constraint, Loc, Base_Type (T));
1156 end if;
1157
1158 Next_Elmt (D);
1159 end loop;
1160 end if;
1161
1162 -- If none of the above, the actual and nominal subtypes are the same
1163
1164 return Empty;
1165 end Build_Actual_Subtype_Of_Component;
1166
1167 -----------------------------
1168 -- Build_Component_Subtype --
1169 -----------------------------
1170
1171 function Build_Component_Subtype
1172 (C : List_Id;
1173 Loc : Source_Ptr;
1174 T : Entity_Id) return Node_Id
1175 is
1176 Subt : Entity_Id;
1177 Decl : Node_Id;
1178
1179 begin
1180 -- Unchecked_Union components do not require component subtypes
1181
1182 if Is_Unchecked_Union (T) then
1183 return Empty;
1184 end if;
1185
1186 Subt := Make_Temporary (Loc, 'S');
1187 Set_Is_Internal (Subt);
1188
1189 Decl :=
1190 Make_Subtype_Declaration (Loc,
1191 Defining_Identifier => Subt,
1192 Subtype_Indication =>
1193 Make_Subtype_Indication (Loc,
1194 Subtype_Mark => New_Occurrence_Of (Base_Type (T), Loc),
1195 Constraint =>
1196 Make_Index_Or_Discriminant_Constraint (Loc,
1197 Constraints => C)));
1198
1199 Mark_Rewrite_Insertion (Decl);
1200 return Decl;
1201 end Build_Component_Subtype;
1202
1203 ---------------------------
1204 -- Build_Default_Subtype --
1205 ---------------------------
1206
1207 function Build_Default_Subtype
1208 (T : Entity_Id;
1209 N : Node_Id) return Entity_Id
1210 is
1211 Loc : constant Source_Ptr := Sloc (N);
1212 Disc : Entity_Id;
1213
1214 Bas : Entity_Id;
1215 -- The base type that is to be constrained by the defaults
1216
1217 begin
1218 if not Has_Discriminants (T) or else Is_Constrained (T) then
1219 return T;
1220 end if;
1221
1222 Bas := Base_Type (T);
1223
1224 -- If T is non-private but its base type is private, this is the
1225 -- completion of a subtype declaration whose parent type is private
1226 -- (see Complete_Private_Subtype in Sem_Ch3). The proper discriminants
1227 -- are to be found in the full view of the base. Check that the private
1228 -- status of T and its base differ.
1229
1230 if Is_Private_Type (Bas)
1231 and then not Is_Private_Type (T)
1232 and then Present (Full_View (Bas))
1233 then
1234 Bas := Full_View (Bas);
1235 end if;
1236
1237 Disc := First_Discriminant (T);
1238
1239 if No (Discriminant_Default_Value (Disc)) then
1240 return T;
1241 end if;
1242
1243 declare
1244 Act : constant Entity_Id := Make_Temporary (Loc, 'S');
1245 Constraints : constant List_Id := New_List;
1246 Decl : Node_Id;
1247
1248 begin
1249 while Present (Disc) loop
1250 Append_To (Constraints,
1251 New_Copy_Tree (Discriminant_Default_Value (Disc)));
1252 Next_Discriminant (Disc);
1253 end loop;
1254
1255 Decl :=
1256 Make_Subtype_Declaration (Loc,
1257 Defining_Identifier => Act,
1258 Subtype_Indication =>
1259 Make_Subtype_Indication (Loc,
1260 Subtype_Mark => New_Occurrence_Of (Bas, Loc),
1261 Constraint =>
1262 Make_Index_Or_Discriminant_Constraint (Loc,
1263 Constraints => Constraints)));
1264
1265 Insert_Action (N, Decl);
1266
1267 -- If the context is a component declaration the subtype declaration
1268 -- will be analyzed when the enclosing type is frozen, otherwise do
1269 -- it now.
1270
1271 if Ekind (Current_Scope) /= E_Record_Type then
1272 Analyze (Decl);
1273 end if;
1274
1275 return Act;
1276 end;
1277 end Build_Default_Subtype;
1278
1279 --------------------------------------------
1280 -- Build_Discriminal_Subtype_Of_Component --
1281 --------------------------------------------
1282
1283 function Build_Discriminal_Subtype_Of_Component
1284 (T : Entity_Id) return Node_Id
1285 is
1286 Loc : constant Source_Ptr := Sloc (T);
1287 D : Elmt_Id;
1288 Id : Node_Id;
1289
1290 function Build_Discriminal_Array_Constraint return List_Id;
1291 -- If one or more of the bounds of the component depends on
1292 -- discriminants, build actual constraint using the discriminants
1293 -- of the prefix.
1294
1295 function Build_Discriminal_Record_Constraint return List_Id;
1296 -- Similar to previous one, for discriminated components constrained by
1297 -- the discriminant of the enclosing object.
1298
1299 ----------------------------------------
1300 -- Build_Discriminal_Array_Constraint --
1301 ----------------------------------------
1302
1303 function Build_Discriminal_Array_Constraint return List_Id is
1304 Constraints : constant List_Id := New_List;
1305 Indx : Node_Id;
1306 Hi : Node_Id;
1307 Lo : Node_Id;
1308 Old_Hi : Node_Id;
1309 Old_Lo : Node_Id;
1310
1311 begin
1312 Indx := First_Index (T);
1313 while Present (Indx) loop
1314 Old_Lo := Type_Low_Bound (Etype (Indx));
1315 Old_Hi := Type_High_Bound (Etype (Indx));
1316
1317 if Denotes_Discriminant (Old_Lo) then
1318 Lo := New_Occurrence_Of (Discriminal (Entity (Old_Lo)), Loc);
1319
1320 else
1321 Lo := New_Copy_Tree (Old_Lo);
1322 end if;
1323
1324 if Denotes_Discriminant (Old_Hi) then
1325 Hi := New_Occurrence_Of (Discriminal (Entity (Old_Hi)), Loc);
1326
1327 else
1328 Hi := New_Copy_Tree (Old_Hi);
1329 end if;
1330
1331 Append (Make_Range (Loc, Lo, Hi), Constraints);
1332 Next_Index (Indx);
1333 end loop;
1334
1335 return Constraints;
1336 end Build_Discriminal_Array_Constraint;
1337
1338 -----------------------------------------
1339 -- Build_Discriminal_Record_Constraint --
1340 -----------------------------------------
1341
1342 function Build_Discriminal_Record_Constraint return List_Id is
1343 Constraints : constant List_Id := New_List;
1344 D : Elmt_Id;
1345 D_Val : Node_Id;
1346
1347 begin
1348 D := First_Elmt (Discriminant_Constraint (T));
1349 while Present (D) loop
1350 if Denotes_Discriminant (Node (D)) then
1351 D_Val :=
1352 New_Occurrence_Of (Discriminal (Entity (Node (D))), Loc);
1353 else
1354 D_Val := New_Copy_Tree (Node (D));
1355 end if;
1356
1357 Append (D_Val, Constraints);
1358 Next_Elmt (D);
1359 end loop;
1360
1361 return Constraints;
1362 end Build_Discriminal_Record_Constraint;
1363
1364 -- Start of processing for Build_Discriminal_Subtype_Of_Component
1365
1366 begin
1367 if Ekind (T) = E_Array_Subtype then
1368 Id := First_Index (T);
1369 while Present (Id) loop
1370 if Denotes_Discriminant (Type_Low_Bound (Etype (Id)))
1371 or else
1372 Denotes_Discriminant (Type_High_Bound (Etype (Id)))
1373 then
1374 return Build_Component_Subtype
1375 (Build_Discriminal_Array_Constraint, Loc, T);
1376 end if;
1377
1378 Next_Index (Id);
1379 end loop;
1380
1381 elsif Ekind (T) = E_Record_Subtype
1382 and then Has_Discriminants (T)
1383 and then not Has_Unknown_Discriminants (T)
1384 then
1385 D := First_Elmt (Discriminant_Constraint (T));
1386 while Present (D) loop
1387 if Denotes_Discriminant (Node (D)) then
1388 return Build_Component_Subtype
1389 (Build_Discriminal_Record_Constraint, Loc, T);
1390 end if;
1391
1392 Next_Elmt (D);
1393 end loop;
1394 end if;
1395
1396 -- If none of the above, the actual and nominal subtypes are the same
1397
1398 return Empty;
1399 end Build_Discriminal_Subtype_Of_Component;
1400
1401 ------------------------------
1402 -- Build_Elaboration_Entity --
1403 ------------------------------
1404
1405 procedure Build_Elaboration_Entity (N : Node_Id; Spec_Id : Entity_Id) is
1406 Loc : constant Source_Ptr := Sloc (N);
1407 Decl : Node_Id;
1408 Elab_Ent : Entity_Id;
1409
1410 procedure Set_Package_Name (Ent : Entity_Id);
1411 -- Given an entity, sets the fully qualified name of the entity in
1412 -- Name_Buffer, with components separated by double underscores. This
1413 -- is a recursive routine that climbs the scope chain to Standard.
1414
1415 ----------------------
1416 -- Set_Package_Name --
1417 ----------------------
1418
1419 procedure Set_Package_Name (Ent : Entity_Id) is
1420 begin
1421 if Scope (Ent) /= Standard_Standard then
1422 Set_Package_Name (Scope (Ent));
1423
1424 declare
1425 Nam : constant String := Get_Name_String (Chars (Ent));
1426 begin
1427 Name_Buffer (Name_Len + 1) := '_';
1428 Name_Buffer (Name_Len + 2) := '_';
1429 Name_Buffer (Name_Len + 3 .. Name_Len + Nam'Length + 2) := Nam;
1430 Name_Len := Name_Len + Nam'Length + 2;
1431 end;
1432
1433 else
1434 Get_Name_String (Chars (Ent));
1435 end if;
1436 end Set_Package_Name;
1437
1438 -- Start of processing for Build_Elaboration_Entity
1439
1440 begin
1441 -- Ignore call if already constructed
1442
1443 if Present (Elaboration_Entity (Spec_Id)) then
1444 return;
1445
1446 -- Ignore in ASIS mode, elaboration entity is not in source and plays
1447 -- no role in analysis.
1448
1449 elsif ASIS_Mode then
1450 return;
1451
1452 -- See if we need elaboration entity.
1453
1454 -- We always need an elaboration entity when preserving control flow, as
1455 -- we want to remain explicit about the unit's elaboration order.
1456
1457 elsif Opt.Suppress_Control_Flow_Optimizations then
1458 null;
1459
1460 -- We always need an elaboration entity for the dynamic elaboration
1461 -- model, since it is needed to properly generate the PE exception for
1462 -- access before elaboration.
1463
1464 elsif Dynamic_Elaboration_Checks then
1465 null;
1466
1467 -- For the static model, we don't need the elaboration counter if this
1468 -- unit is sure to have no elaboration code, since that means there
1469 -- is no elaboration unit to be called. Note that we can't just decide
1470 -- after the fact by looking to see whether there was elaboration code,
1471 -- because that's too late to make this decision.
1472
1473 elsif Restriction_Active (No_Elaboration_Code) then
1474 return;
1475
1476 -- Similarly, for the static model, we can skip the elaboration counter
1477 -- if we have the No_Multiple_Elaboration restriction, since for the
1478 -- static model, that's the only purpose of the counter (to avoid
1479 -- multiple elaboration).
1480
1481 elsif Restriction_Active (No_Multiple_Elaboration) then
1482 return;
1483 end if;
1484
1485 -- Here we need the elaboration entity
1486
1487 -- Construct name of elaboration entity as xxx_E, where xxx is the unit
1488 -- name with dots replaced by double underscore. We have to manually
1489 -- construct this name, since it will be elaborated in the outer scope,
1490 -- and thus will not have the unit name automatically prepended.
1491
1492 Set_Package_Name (Spec_Id);
1493 Add_Str_To_Name_Buffer ("_E");
1494
1495 -- Create elaboration counter
1496
1497 Elab_Ent := Make_Defining_Identifier (Loc, Chars => Name_Find);
1498 Set_Elaboration_Entity (Spec_Id, Elab_Ent);
1499
1500 Decl :=
1501 Make_Object_Declaration (Loc,
1502 Defining_Identifier => Elab_Ent,
1503 Object_Definition =>
1504 New_Occurrence_Of (Standard_Short_Integer, Loc),
1505 Expression => Make_Integer_Literal (Loc, Uint_0));
1506
1507 Push_Scope (Standard_Standard);
1508 Add_Global_Declaration (Decl);
1509 Pop_Scope;
1510
1511 -- Reset True_Constant indication, since we will indeed assign a value
1512 -- to the variable in the binder main. We also kill the Current_Value
1513 -- and Last_Assignment fields for the same reason.
1514
1515 Set_Is_True_Constant (Elab_Ent, False);
1516 Set_Current_Value (Elab_Ent, Empty);
1517 Set_Last_Assignment (Elab_Ent, Empty);
1518
1519 -- We do not want any further qualification of the name (if we did not
1520 -- do this, we would pick up the name of the generic package in the case
1521 -- of a library level generic instantiation).
1522
1523 Set_Has_Qualified_Name (Elab_Ent);
1524 Set_Has_Fully_Qualified_Name (Elab_Ent);
1525 end Build_Elaboration_Entity;
1526
1527 --------------------------------
1528 -- Build_Explicit_Dereference --
1529 --------------------------------
1530
1531 procedure Build_Explicit_Dereference
1532 (Expr : Node_Id;
1533 Disc : Entity_Id)
1534 is
1535 Loc : constant Source_Ptr := Sloc (Expr);
1536 I : Interp_Index;
1537 It : Interp;
1538
1539 begin
1540 -- An entity of a type with a reference aspect is overloaded with
1541 -- both interpretations: with and without the dereference. Now that
1542 -- the dereference is made explicit, set the type of the node properly,
1543 -- to prevent anomalies in the backend. Same if the expression is an
1544 -- overloaded function call whose return type has a reference aspect.
1545
1546 if Is_Entity_Name (Expr) then
1547 Set_Etype (Expr, Etype (Entity (Expr)));
1548
1549 -- The designated entity will not be examined again when resolving
1550 -- the dereference, so generate a reference to it now.
1551
1552 Generate_Reference (Entity (Expr), Expr);
1553
1554 elsif Nkind (Expr) = N_Function_Call then
1555
1556 -- If the name of the indexing function is overloaded, locate the one
1557 -- whose return type has an implicit dereference on the desired
1558 -- discriminant, and set entity and type of function call.
1559
1560 if Is_Overloaded (Name (Expr)) then
1561 Get_First_Interp (Name (Expr), I, It);
1562
1563 while Present (It.Nam) loop
1564 if Ekind ((It.Typ)) = E_Record_Type
1565 and then First_Entity ((It.Typ)) = Disc
1566 then
1567 Set_Entity (Name (Expr), It.Nam);
1568 Set_Etype (Name (Expr), Etype (It.Nam));
1569 exit;
1570 end if;
1571
1572 Get_Next_Interp (I, It);
1573 end loop;
1574 end if;
1575
1576 -- Set type of call from resolved function name.
1577
1578 Set_Etype (Expr, Etype (Name (Expr)));
1579 end if;
1580
1581 Set_Is_Overloaded (Expr, False);
1582
1583 -- The expression will often be a generalized indexing that yields a
1584 -- container element that is then dereferenced, in which case the
1585 -- generalized indexing call is also non-overloaded.
1586
1587 if Nkind (Expr) = N_Indexed_Component
1588 and then Present (Generalized_Indexing (Expr))
1589 then
1590 Set_Is_Overloaded (Generalized_Indexing (Expr), False);
1591 end if;
1592
1593 Rewrite (Expr,
1594 Make_Explicit_Dereference (Loc,
1595 Prefix =>
1596 Make_Selected_Component (Loc,
1597 Prefix => Relocate_Node (Expr),
1598 Selector_Name => New_Occurrence_Of (Disc, Loc))));
1599 Set_Etype (Prefix (Expr), Etype (Disc));
1600 Set_Etype (Expr, Designated_Type (Etype (Disc)));
1601 end Build_Explicit_Dereference;
1602
1603 ---------------------------
1604 -- Build_Overriding_Spec --
1605 ---------------------------
1606
1607 function Build_Overriding_Spec
1608 (Op : Entity_Id;
1609 Typ : Entity_Id) return Node_Id
1610 is
1611 Loc : constant Source_Ptr := Sloc (Typ);
1612 Par_Typ : constant Entity_Id := Find_Dispatching_Type (Op);
1613 Spec : constant Node_Id := Specification (Unit_Declaration_Node (Op));
1614
1615 Formal_Spec : Node_Id;
1616 Formal_Type : Node_Id;
1617 New_Spec : Node_Id;
1618
1619 begin
1620 New_Spec := Copy_Subprogram_Spec (Spec);
1621
1622 Formal_Spec := First (Parameter_Specifications (New_Spec));
1623 while Present (Formal_Spec) loop
1624 Formal_Type := Parameter_Type (Formal_Spec);
1625
1626 if Is_Entity_Name (Formal_Type)
1627 and then Entity (Formal_Type) = Par_Typ
1628 then
1629 Rewrite (Formal_Type, New_Occurrence_Of (Typ, Loc));
1630 end if;
1631
1632 -- Nothing needs to be done for access parameters
1633
1634 Next (Formal_Spec);
1635 end loop;
1636
1637 return New_Spec;
1638 end Build_Overriding_Spec;
1639
1640 -----------------------------------
1641 -- Cannot_Raise_Constraint_Error --
1642 -----------------------------------
1643
1644 function Cannot_Raise_Constraint_Error (Expr : Node_Id) return Boolean is
1645 begin
1646 if Compile_Time_Known_Value (Expr) then
1647 return True;
1648
1649 elsif Do_Range_Check (Expr) then
1650 return False;
1651
1652 elsif Raises_Constraint_Error (Expr) then
1653 return False;
1654
1655 else
1656 case Nkind (Expr) is
1657 when N_Identifier =>
1658 return True;
1659
1660 when N_Expanded_Name =>
1661 return True;
1662
1663 when N_Selected_Component =>
1664 return not Do_Discriminant_Check (Expr);
1665
1666 when N_Attribute_Reference =>
1667 if Do_Overflow_Check (Expr) then
1668 return False;
1669
1670 elsif No (Expressions (Expr)) then
1671 return True;
1672
1673 else
1674 declare
1675 N : Node_Id;
1676
1677 begin
1678 N := First (Expressions (Expr));
1679 while Present (N) loop
1680 if Cannot_Raise_Constraint_Error (N) then
1681 Next (N);
1682 else
1683 return False;
1684 end if;
1685 end loop;
1686
1687 return True;
1688 end;
1689 end if;
1690
1691 when N_Type_Conversion =>
1692 if Do_Overflow_Check (Expr)
1693 or else Do_Length_Check (Expr)
1694 or else Do_Tag_Check (Expr)
1695 then
1696 return False;
1697 else
1698 return Cannot_Raise_Constraint_Error (Expression (Expr));
1699 end if;
1700
1701 when N_Unchecked_Type_Conversion =>
1702 return Cannot_Raise_Constraint_Error (Expression (Expr));
1703
1704 when N_Unary_Op =>
1705 if Do_Overflow_Check (Expr) then
1706 return False;
1707 else
1708 return Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
1709 end if;
1710
1711 when N_Op_Divide
1712 | N_Op_Mod
1713 | N_Op_Rem
1714 =>
1715 if Do_Division_Check (Expr)
1716 or else
1717 Do_Overflow_Check (Expr)
1718 then
1719 return False;
1720 else
1721 return
1722 Cannot_Raise_Constraint_Error (Left_Opnd (Expr))
1723 and then
1724 Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
1725 end if;
1726
1727 when N_Op_Add
1728 | N_Op_And
1729 | N_Op_Concat
1730 | N_Op_Eq
1731 | N_Op_Expon
1732 | N_Op_Ge
1733 | N_Op_Gt
1734 | N_Op_Le
1735 | N_Op_Lt
1736 | N_Op_Multiply
1737 | N_Op_Ne
1738 | N_Op_Or
1739 | N_Op_Rotate_Left
1740 | N_Op_Rotate_Right
1741 | N_Op_Shift_Left
1742 | N_Op_Shift_Right
1743 | N_Op_Shift_Right_Arithmetic
1744 | N_Op_Subtract
1745 | N_Op_Xor
1746 =>
1747 if Do_Overflow_Check (Expr) then
1748 return False;
1749 else
1750 return
1751 Cannot_Raise_Constraint_Error (Left_Opnd (Expr))
1752 and then
1753 Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
1754 end if;
1755
1756 when others =>
1757 return False;
1758 end case;
1759 end if;
1760 end Cannot_Raise_Constraint_Error;
1761
1762 -----------------------------
1763 -- Check_Part_Of_Reference --
1764 -----------------------------
1765
1766 procedure Check_Part_Of_Reference (Var_Id : Entity_Id; Ref : Node_Id) is
1767 Conc_Typ : constant Entity_Id := Encapsulating_State (Var_Id);
1768 Decl : Node_Id;
1769 OK_Use : Boolean := False;
1770 Par : Node_Id;
1771 Prag_Nam : Name_Id;
1772 Spec_Id : Entity_Id;
1773
1774 begin
1775 -- Traverse the parent chain looking for a suitable context for the
1776 -- reference to the concurrent constituent.
1777
1778 Par := Parent (Ref);
1779 while Present (Par) loop
1780 if Nkind (Par) = N_Pragma then
1781 Prag_Nam := Pragma_Name (Par);
1782
1783 -- A concurrent constituent is allowed to appear in pragmas
1784 -- Initial_Condition and Initializes as this is part of the
1785 -- elaboration checks for the constituent (SPARK RM 9.3).
1786
1787 if Nam_In (Prag_Nam, Name_Initial_Condition, Name_Initializes) then
1788 OK_Use := True;
1789 exit;
1790
1791 -- When the reference appears within pragma Depends or Global,
1792 -- check whether the pragma applies to a single task type. Note
1793 -- that the pragma is not encapsulated by the type definition,
1794 -- but this is still a valid context.
1795
1796 elsif Nam_In (Prag_Nam, Name_Depends, Name_Global) then
1797 Decl := Find_Related_Declaration_Or_Body (Par);
1798
1799 if Nkind (Decl) = N_Object_Declaration
1800 and then Defining_Entity (Decl) = Conc_Typ
1801 then
1802 OK_Use := True;
1803 exit;
1804 end if;
1805 end if;
1806
1807 -- The reference appears somewhere in the definition of the single
1808 -- protected/task type (SPARK RM 9.3).
1809
1810 elsif Nkind_In (Par, N_Single_Protected_Declaration,
1811 N_Single_Task_Declaration)
1812 and then Defining_Entity (Par) = Conc_Typ
1813 then
1814 OK_Use := True;
1815 exit;
1816
1817 -- The reference appears within the expanded declaration or the body
1818 -- of the single protected/task type (SPARK RM 9.3).
1819
1820 elsif Nkind_In (Par, N_Protected_Body,
1821 N_Protected_Type_Declaration,
1822 N_Task_Body,
1823 N_Task_Type_Declaration)
1824 then
1825 Spec_Id := Unique_Defining_Entity (Par);
1826
1827 if Present (Anonymous_Object (Spec_Id))
1828 and then Anonymous_Object (Spec_Id) = Conc_Typ
1829 then
1830 OK_Use := True;
1831 exit;
1832 end if;
1833
1834 -- The reference has been relocated within an internally generated
1835 -- package or subprogram. Assume that the reference is legal as the
1836 -- real check was already performed in the original context of the
1837 -- reference.
1838
1839 elsif Nkind_In (Par, N_Package_Body,
1840 N_Package_Declaration,
1841 N_Subprogram_Body,
1842 N_Subprogram_Declaration)
1843 and then not Comes_From_Source (Par)
1844 then
1845 -- Continue to examine the context if the reference appears in a
1846 -- subprogram body which was previously an expression function.
1847
1848 if Nkind (Par) = N_Subprogram_Body
1849 and then Was_Expression_Function (Par)
1850 then
1851 null;
1852
1853 -- Otherwise the reference is legal
1854
1855 else
1856 OK_Use := True;
1857 exit;
1858 end if;
1859
1860 -- The reference has been relocated to an inlined body for GNATprove.
1861 -- Assume that the reference is legal as the real check was already
1862 -- performed in the original context of the reference.
1863
1864 elsif GNATprove_Mode
1865 and then Nkind (Par) = N_Subprogram_Body
1866 and then Chars (Defining_Entity (Par)) = Name_uParent
1867 then
1868 OK_Use := True;
1869 exit;
1870 end if;
1871
1872 Par := Parent (Par);
1873 end loop;
1874
1875 -- The reference is illegal as it appears outside the definition or
1876 -- body of the single protected/task type.
1877
1878 if not OK_Use then
1879 Error_Msg_NE
1880 ("reference to variable & cannot appear in this context",
1881 Ref, Var_Id);
1882 Error_Msg_Name_1 := Chars (Var_Id);
1883
1884 if Ekind (Conc_Typ) = E_Protected_Type then
1885 Error_Msg_NE
1886 ("\% is constituent of single protected type &", Ref, Conc_Typ);
1887 else
1888 Error_Msg_NE
1889 ("\% is constituent of single task type &", Ref, Conc_Typ);
1890 end if;
1891 end if;
1892 end Check_Part_Of_Reference;
1893
1894 -----------------------------------------
1895 -- Check_Dynamically_Tagged_Expression --
1896 -----------------------------------------
1897
1898 procedure Check_Dynamically_Tagged_Expression
1899 (Expr : Node_Id;
1900 Typ : Entity_Id;
1901 Related_Nod : Node_Id)
1902 is
1903 begin
1904 pragma Assert (Is_Tagged_Type (Typ));
1905
1906 -- In order to avoid spurious errors when analyzing the expanded code,
1907 -- this check is done only for nodes that come from source and for
1908 -- actuals of generic instantiations.
1909
1910 if (Comes_From_Source (Related_Nod)
1911 or else In_Generic_Actual (Expr))
1912 and then (Is_Class_Wide_Type (Etype (Expr))
1913 or else Is_Dynamically_Tagged (Expr))
1914 and then Is_Tagged_Type (Typ)
1915 and then not Is_Class_Wide_Type (Typ)
1916 then
1917 Error_Msg_N ("dynamically tagged expression not allowed!", Expr);
1918 end if;
1919 end Check_Dynamically_Tagged_Expression;
1920
1921 --------------------------
1922 -- Check_Fully_Declared --
1923 --------------------------
1924
1925 procedure Check_Fully_Declared (T : Entity_Id; N : Node_Id) is
1926 begin
1927 if Ekind (T) = E_Incomplete_Type then
1928
1929 -- Ada 2005 (AI-50217): If the type is available through a limited
1930 -- with_clause, verify that its full view has been analyzed.
1931
1932 if From_Limited_With (T)
1933 and then Present (Non_Limited_View (T))
1934 and then Ekind (Non_Limited_View (T)) /= E_Incomplete_Type
1935 then
1936 -- The non-limited view is fully declared
1937
1938 null;
1939
1940 else
1941 Error_Msg_NE
1942 ("premature usage of incomplete}", N, First_Subtype (T));
1943 end if;
1944
1945 -- Need comments for these tests ???
1946
1947 elsif Has_Private_Component (T)
1948 and then not Is_Generic_Type (Root_Type (T))
1949 and then not In_Spec_Expression
1950 then
1951 -- Special case: if T is the anonymous type created for a single
1952 -- task or protected object, use the name of the source object.
1953
1954 if Is_Concurrent_Type (T)
1955 and then not Comes_From_Source (T)
1956 and then Nkind (N) = N_Object_Declaration
1957 then
1958 Error_Msg_NE
1959 ("type of& has incomplete component",
1960 N, Defining_Identifier (N));
1961 else
1962 Error_Msg_NE
1963 ("premature usage of incomplete}",
1964 N, First_Subtype (T));
1965 end if;
1966 end if;
1967 end Check_Fully_Declared;
1968
1969 -------------------------------------------
1970 -- Check_Function_With_Address_Parameter --
1971 -------------------------------------------
1972
1973 procedure Check_Function_With_Address_Parameter (Subp_Id : Entity_Id) is
1974 F : Entity_Id;
1975 T : Entity_Id;
1976
1977 begin
1978 F := First_Formal (Subp_Id);
1979 while Present (F) loop
1980 T := Etype (F);
1981
1982 if Is_Private_Type (T) and then Present (Full_View (T)) then
1983 T := Full_View (T);
1984 end if;
1985
1986 if Is_Descendant_Of_Address (T) or else Is_Limited_Type (T) then
1987 Set_Is_Pure (Subp_Id, False);
1988 exit;
1989 end if;
1990
1991 Next_Formal (F);
1992 end loop;
1993 end Check_Function_With_Address_Parameter;
1994
1995 -------------------------------------
1996 -- Check_Function_Writable_Actuals --
1997 -------------------------------------
1998
1999 procedure Check_Function_Writable_Actuals (N : Node_Id) is
2000 Writable_Actuals_List : Elist_Id := No_Elist;
2001 Identifiers_List : Elist_Id := No_Elist;
2002 Aggr_Error_Node : Node_Id := Empty;
2003 Error_Node : Node_Id := Empty;
2004
2005 procedure Collect_Identifiers (N : Node_Id);
2006 -- In a single traversal of subtree N collect in Writable_Actuals_List
2007 -- all the actuals of functions with writable actuals, and in the list
2008 -- Identifiers_List collect all the identifiers that are not actuals of
2009 -- functions with writable actuals. If a writable actual is referenced
2010 -- twice as writable actual then Error_Node is set to reference its
2011 -- second occurrence, the error is reported, and the tree traversal
2012 -- is abandoned.
2013
2014 function Get_Function_Id (Call : Node_Id) return Entity_Id;
2015 -- Return the entity associated with the function call
2016
2017 procedure Preanalyze_Without_Errors (N : Node_Id);
2018 -- Preanalyze N without reporting errors. Very dubious, you can't just
2019 -- go analyzing things more than once???
2020
2021 -------------------------
2022 -- Collect_Identifiers --
2023 -------------------------
2024
2025 procedure Collect_Identifiers (N : Node_Id) is
2026
2027 function Check_Node (N : Node_Id) return Traverse_Result;
2028 -- Process a single node during the tree traversal to collect the
2029 -- writable actuals of functions and all the identifiers which are
2030 -- not writable actuals of functions.
2031
2032 function Contains (List : Elist_Id; N : Node_Id) return Boolean;
2033 -- Returns True if List has a node whose Entity is Entity (N)
2034
2035 ----------------
2036 -- Check_Node --
2037 ----------------
2038
2039 function Check_Node (N : Node_Id) return Traverse_Result is
2040 Is_Writable_Actual : Boolean := False;
2041 Id : Entity_Id;
2042
2043 begin
2044 if Nkind (N) = N_Identifier then
2045
2046 -- No analysis possible if the entity is not decorated
2047
2048 if No (Entity (N)) then
2049 return Skip;
2050
2051 -- Don't collect identifiers of packages, called functions, etc
2052
2053 elsif Ekind_In (Entity (N), E_Package,
2054 E_Function,
2055 E_Procedure,
2056 E_Entry)
2057 then
2058 return Skip;
2059
2060 -- For rewritten nodes, continue the traversal in the original
2061 -- subtree. Needed to handle aggregates in original expressions
2062 -- extracted from the tree by Remove_Side_Effects.
2063
2064 elsif Is_Rewrite_Substitution (N) then
2065 Collect_Identifiers (Original_Node (N));
2066 return Skip;
2067
2068 -- For now we skip aggregate discriminants, since they require
2069 -- performing the analysis in two phases to identify conflicts:
2070 -- first one analyzing discriminants and second one analyzing
2071 -- the rest of components (since at run time, discriminants are
2072 -- evaluated prior to components): too much computation cost
2073 -- to identify a corner case???
2074
2075 elsif Nkind (Parent (N)) = N_Component_Association
2076 and then Nkind_In (Parent (Parent (N)),
2077 N_Aggregate,
2078 N_Extension_Aggregate)
2079 then
2080 declare
2081 Choice : constant Node_Id := First (Choices (Parent (N)));
2082
2083 begin
2084 if Ekind (Entity (N)) = E_Discriminant then
2085 return Skip;
2086
2087 elsif Expression (Parent (N)) = N
2088 and then Nkind (Choice) = N_Identifier
2089 and then Ekind (Entity (Choice)) = E_Discriminant
2090 then
2091 return Skip;
2092 end if;
2093 end;
2094
2095 -- Analyze if N is a writable actual of a function
2096
2097 elsif Nkind (Parent (N)) = N_Function_Call then
2098 declare
2099 Call : constant Node_Id := Parent (N);
2100 Actual : Node_Id;
2101 Formal : Node_Id;
2102
2103 begin
2104 Id := Get_Function_Id (Call);
2105
2106 -- In case of previous error, no check is possible
2107
2108 if No (Id) then
2109 return Abandon;
2110 end if;
2111
2112 if Ekind_In (Id, E_Function, E_Generic_Function)
2113 and then Has_Out_Or_In_Out_Parameter (Id)
2114 then
2115 Formal := First_Formal (Id);
2116 Actual := First_Actual (Call);
2117 while Present (Actual) and then Present (Formal) loop
2118 if Actual = N then
2119 if Ekind_In (Formal, E_Out_Parameter,
2120 E_In_Out_Parameter)
2121 then
2122 Is_Writable_Actual := True;
2123 end if;
2124
2125 exit;
2126 end if;
2127
2128 Next_Formal (Formal);
2129 Next_Actual (Actual);
2130 end loop;
2131 end if;
2132 end;
2133 end if;
2134
2135 if Is_Writable_Actual then
2136
2137 -- Skip checking the error in non-elementary types since
2138 -- RM 6.4.1(6.15/3) is restricted to elementary types, but
2139 -- store this actual in Writable_Actuals_List since it is
2140 -- needed to perform checks on other constructs that have
2141 -- arbitrary order of evaluation (for example, aggregates).
2142
2143 if not Is_Elementary_Type (Etype (N)) then
2144 if not Contains (Writable_Actuals_List, N) then
2145 Append_New_Elmt (N, To => Writable_Actuals_List);
2146 end if;
2147
2148 -- Second occurrence of an elementary type writable actual
2149
2150 elsif Contains (Writable_Actuals_List, N) then
2151
2152 -- Report the error on the second occurrence of the
2153 -- identifier. We cannot assume that N is the second
2154 -- occurrence (according to their location in the
2155 -- sources), since Traverse_Func walks through Field2
2156 -- last (see comment in the body of Traverse_Func).
2157
2158 declare
2159 Elmt : Elmt_Id;
2160
2161 begin
2162 Elmt := First_Elmt (Writable_Actuals_List);
2163 while Present (Elmt)
2164 and then Entity (Node (Elmt)) /= Entity (N)
2165 loop
2166 Next_Elmt (Elmt);
2167 end loop;
2168
2169 if Sloc (N) > Sloc (Node (Elmt)) then
2170 Error_Node := N;
2171 else
2172 Error_Node := Node (Elmt);
2173 end if;
2174
2175 Error_Msg_NE
2176 ("value may be affected by call to & "
2177 & "because order of evaluation is arbitrary",
2178 Error_Node, Id);
2179 return Abandon;
2180 end;
2181
2182 -- First occurrence of a elementary type writable actual
2183
2184 else
2185 Append_New_Elmt (N, To => Writable_Actuals_List);
2186 end if;
2187
2188 else
2189 if Identifiers_List = No_Elist then
2190 Identifiers_List := New_Elmt_List;
2191 end if;
2192
2193 Append_Unique_Elmt (N, Identifiers_List);
2194 end if;
2195 end if;
2196
2197 return OK;
2198 end Check_Node;
2199
2200 --------------
2201 -- Contains --
2202 --------------
2203
2204 function Contains
2205 (List : Elist_Id;
2206 N : Node_Id) return Boolean
2207 is
2208 pragma Assert (Nkind (N) in N_Has_Entity);
2209
2210 Elmt : Elmt_Id;
2211
2212 begin
2213 if List = No_Elist then
2214 return False;
2215 end if;
2216
2217 Elmt := First_Elmt (List);
2218 while Present (Elmt) loop
2219 if Entity (Node (Elmt)) = Entity (N) then
2220 return True;
2221 else
2222 Next_Elmt (Elmt);
2223 end if;
2224 end loop;
2225
2226 return False;
2227 end Contains;
2228
2229 ------------------
2230 -- Do_Traversal --
2231 ------------------
2232
2233 procedure Do_Traversal is new Traverse_Proc (Check_Node);
2234 -- The traversal procedure
2235
2236 -- Start of processing for Collect_Identifiers
2237
2238 begin
2239 if Present (Error_Node) then
2240 return;
2241 end if;
2242
2243 if Nkind (N) in N_Subexpr and then Is_OK_Static_Expression (N) then
2244 return;
2245 end if;
2246
2247 Do_Traversal (N);
2248 end Collect_Identifiers;
2249
2250 ---------------------
2251 -- Get_Function_Id --
2252 ---------------------
2253
2254 function Get_Function_Id (Call : Node_Id) return Entity_Id is
2255 Nam : constant Node_Id := Name (Call);
2256 Id : Entity_Id;
2257
2258 begin
2259 if Nkind (Nam) = N_Explicit_Dereference then
2260 Id := Etype (Nam);
2261 pragma Assert (Ekind (Id) = E_Subprogram_Type);
2262
2263 elsif Nkind (Nam) = N_Selected_Component then
2264 Id := Entity (Selector_Name (Nam));
2265
2266 elsif Nkind (Nam) = N_Indexed_Component then
2267 Id := Entity (Selector_Name (Prefix (Nam)));
2268
2269 else
2270 Id := Entity (Nam);
2271 end if;
2272
2273 return Id;
2274 end Get_Function_Id;
2275
2276 -------------------------------
2277 -- Preanalyze_Without_Errors --
2278 -------------------------------
2279
2280 procedure Preanalyze_Without_Errors (N : Node_Id) is
2281 Status : constant Boolean := Get_Ignore_Errors;
2282 begin
2283 Set_Ignore_Errors (True);
2284 Preanalyze (N);
2285 Set_Ignore_Errors (Status);
2286 end Preanalyze_Without_Errors;
2287
2288 -- Start of processing for Check_Function_Writable_Actuals
2289
2290 begin
2291 -- The check only applies to Ada 2012 code on which Check_Actuals has
2292 -- been set, and only to constructs that have multiple constituents
2293 -- whose order of evaluation is not specified by the language.
2294
2295 if Ada_Version < Ada_2012
2296 or else not Check_Actuals (N)
2297 or else (not (Nkind (N) in N_Op)
2298 and then not (Nkind (N) in N_Membership_Test)
2299 and then not Nkind_In (N, N_Range,
2300 N_Aggregate,
2301 N_Extension_Aggregate,
2302 N_Full_Type_Declaration,
2303 N_Function_Call,
2304 N_Procedure_Call_Statement,
2305 N_Entry_Call_Statement))
2306 or else (Nkind (N) = N_Full_Type_Declaration
2307 and then not Is_Record_Type (Defining_Identifier (N)))
2308
2309 -- In addition, this check only applies to source code, not to code
2310 -- generated by constraint checks.
2311
2312 or else not Comes_From_Source (N)
2313 then
2314 return;
2315 end if;
2316
2317 -- If a construct C has two or more direct constituents that are names
2318 -- or expressions whose evaluation may occur in an arbitrary order, at
2319 -- least one of which contains a function call with an in out or out
2320 -- parameter, then the construct is legal only if: for each name N that
2321 -- is passed as a parameter of mode in out or out to some inner function
2322 -- call C2 (not including the construct C itself), there is no other
2323 -- name anywhere within a direct constituent of the construct C other
2324 -- than the one containing C2, that is known to refer to the same
2325 -- object (RM 6.4.1(6.17/3)).
2326
2327 case Nkind (N) is
2328 when N_Range =>
2329 Collect_Identifiers (Low_Bound (N));
2330 Collect_Identifiers (High_Bound (N));
2331
2332 when N_Membership_Test
2333 | N_Op
2334 =>
2335 declare
2336 Expr : Node_Id;
2337
2338 begin
2339 Collect_Identifiers (Left_Opnd (N));
2340
2341 if Present (Right_Opnd (N)) then
2342 Collect_Identifiers (Right_Opnd (N));
2343 end if;
2344
2345 if Nkind_In (N, N_In, N_Not_In)
2346 and then Present (Alternatives (N))
2347 then
2348 Expr := First (Alternatives (N));
2349 while Present (Expr) loop
2350 Collect_Identifiers (Expr);
2351
2352 Next (Expr);
2353 end loop;
2354 end if;
2355 end;
2356
2357 when N_Full_Type_Declaration =>
2358 declare
2359 function Get_Record_Part (N : Node_Id) return Node_Id;
2360 -- Return the record part of this record type definition
2361
2362 function Get_Record_Part (N : Node_Id) return Node_Id is
2363 Type_Def : constant Node_Id := Type_Definition (N);
2364 begin
2365 if Nkind (Type_Def) = N_Derived_Type_Definition then
2366 return Record_Extension_Part (Type_Def);
2367 else
2368 return Type_Def;
2369 end if;
2370 end Get_Record_Part;
2371
2372 Comp : Node_Id;
2373 Def_Id : Entity_Id := Defining_Identifier (N);
2374 Rec : Node_Id := Get_Record_Part (N);
2375
2376 begin
2377 -- No need to perform any analysis if the record has no
2378 -- components
2379
2380 if No (Rec) or else No (Component_List (Rec)) then
2381 return;
2382 end if;
2383
2384 -- Collect the identifiers starting from the deepest
2385 -- derivation. Done to report the error in the deepest
2386 -- derivation.
2387
2388 loop
2389 if Present (Component_List (Rec)) then
2390 Comp := First (Component_Items (Component_List (Rec)));
2391 while Present (Comp) loop
2392 if Nkind (Comp) = N_Component_Declaration
2393 and then Present (Expression (Comp))
2394 then
2395 Collect_Identifiers (Expression (Comp));
2396 end if;
2397
2398 Next (Comp);
2399 end loop;
2400 end if;
2401
2402 exit when No (Underlying_Type (Etype (Def_Id)))
2403 or else Base_Type (Underlying_Type (Etype (Def_Id)))
2404 = Def_Id;
2405
2406 Def_Id := Base_Type (Underlying_Type (Etype (Def_Id)));
2407 Rec := Get_Record_Part (Parent (Def_Id));
2408 end loop;
2409 end;
2410
2411 when N_Entry_Call_Statement
2412 | N_Subprogram_Call
2413 =>
2414 declare
2415 Id : constant Entity_Id := Get_Function_Id (N);
2416 Formal : Node_Id;
2417 Actual : Node_Id;
2418
2419 begin
2420 Formal := First_Formal (Id);
2421 Actual := First_Actual (N);
2422 while Present (Actual) and then Present (Formal) loop
2423 if Ekind_In (Formal, E_Out_Parameter,
2424 E_In_Out_Parameter)
2425 then
2426 Collect_Identifiers (Actual);
2427 end if;
2428
2429 Next_Formal (Formal);
2430 Next_Actual (Actual);
2431 end loop;
2432 end;
2433
2434 when N_Aggregate
2435 | N_Extension_Aggregate
2436 =>
2437 declare
2438 Assoc : Node_Id;
2439 Choice : Node_Id;
2440 Comp_Expr : Node_Id;
2441
2442 begin
2443 -- Handle the N_Others_Choice of array aggregates with static
2444 -- bounds. There is no need to perform this analysis in
2445 -- aggregates without static bounds since we cannot evaluate
2446 -- if the N_Others_Choice covers several elements. There is
2447 -- no need to handle the N_Others choice of record aggregates
2448 -- since at this stage it has been already expanded by
2449 -- Resolve_Record_Aggregate.
2450
2451 if Is_Array_Type (Etype (N))
2452 and then Nkind (N) = N_Aggregate
2453 and then Present (Aggregate_Bounds (N))
2454 and then Compile_Time_Known_Bounds (Etype (N))
2455 and then Expr_Value (High_Bound (Aggregate_Bounds (N)))
2456 >
2457 Expr_Value (Low_Bound (Aggregate_Bounds (N)))
2458 then
2459 declare
2460 Count_Components : Uint := Uint_0;
2461 Num_Components : Uint;
2462 Others_Assoc : Node_Id;
2463 Others_Choice : Node_Id := Empty;
2464 Others_Box_Present : Boolean := False;
2465
2466 begin
2467 -- Count positional associations
2468
2469 if Present (Expressions (N)) then
2470 Comp_Expr := First (Expressions (N));
2471 while Present (Comp_Expr) loop
2472 Count_Components := Count_Components + 1;
2473 Next (Comp_Expr);
2474 end loop;
2475 end if;
2476
2477 -- Count the rest of elements and locate the N_Others
2478 -- choice (if any)
2479
2480 Assoc := First (Component_Associations (N));
2481 while Present (Assoc) loop
2482 Choice := First (Choices (Assoc));
2483 while Present (Choice) loop
2484 if Nkind (Choice) = N_Others_Choice then
2485 Others_Assoc := Assoc;
2486 Others_Choice := Choice;
2487 Others_Box_Present := Box_Present (Assoc);
2488
2489 -- Count several components
2490
2491 elsif Nkind_In (Choice, N_Range,
2492 N_Subtype_Indication)
2493 or else (Is_Entity_Name (Choice)
2494 and then Is_Type (Entity (Choice)))
2495 then
2496 declare
2497 L, H : Node_Id;
2498 begin
2499 Get_Index_Bounds (Choice, L, H);
2500 pragma Assert
2501 (Compile_Time_Known_Value (L)
2502 and then Compile_Time_Known_Value (H));
2503 Count_Components :=
2504 Count_Components
2505 + Expr_Value (H) - Expr_Value (L) + 1;
2506 end;
2507
2508 -- Count single component. No other case available
2509 -- since we are handling an aggregate with static
2510 -- bounds.
2511
2512 else
2513 pragma Assert (Is_OK_Static_Expression (Choice)
2514 or else Nkind (Choice) = N_Identifier
2515 or else Nkind (Choice) = N_Integer_Literal);
2516
2517 Count_Components := Count_Components + 1;
2518 end if;
2519
2520 Next (Choice);
2521 end loop;
2522
2523 Next (Assoc);
2524 end loop;
2525
2526 Num_Components :=
2527 Expr_Value (High_Bound (Aggregate_Bounds (N))) -
2528 Expr_Value (Low_Bound (Aggregate_Bounds (N))) + 1;
2529
2530 pragma Assert (Count_Components <= Num_Components);
2531
2532 -- Handle the N_Others choice if it covers several
2533 -- components
2534
2535 if Present (Others_Choice)
2536 and then (Num_Components - Count_Components) > 1
2537 then
2538 if not Others_Box_Present then
2539
2540 -- At this stage, if expansion is active, the
2541 -- expression of the others choice has not been
2542 -- analyzed. Hence we generate a duplicate and
2543 -- we analyze it silently to have available the
2544 -- minimum decoration required to collect the
2545 -- identifiers.
2546
2547 if not Expander_Active then
2548 Comp_Expr := Expression (Others_Assoc);
2549 else
2550 Comp_Expr :=
2551 New_Copy_Tree (Expression (Others_Assoc));
2552 Preanalyze_Without_Errors (Comp_Expr);
2553 end if;
2554
2555 Collect_Identifiers (Comp_Expr);
2556
2557 if Writable_Actuals_List /= No_Elist then
2558
2559 -- As suggested by Robert, at current stage we
2560 -- report occurrences of this case as warnings.
2561
2562 Error_Msg_N
2563 ("writable function parameter may affect "
2564 & "value in other component because order "
2565 & "of evaluation is unspecified??",
2566 Node (First_Elmt (Writable_Actuals_List)));
2567 end if;
2568 end if;
2569 end if;
2570 end;
2571
2572 -- For an array aggregate, a discrete_choice_list that has
2573 -- a nonstatic range is considered as two or more separate
2574 -- occurrences of the expression (RM 6.4.1(20/3)).
2575
2576 elsif Is_Array_Type (Etype (N))
2577 and then Nkind (N) = N_Aggregate
2578 and then Present (Aggregate_Bounds (N))
2579 and then not Compile_Time_Known_Bounds (Etype (N))
2580 then
2581 -- Collect identifiers found in the dynamic bounds
2582
2583 declare
2584 Count_Components : Natural := 0;
2585 Low, High : Node_Id;
2586
2587 begin
2588 Assoc := First (Component_Associations (N));
2589 while Present (Assoc) loop
2590 Choice := First (Choices (Assoc));
2591 while Present (Choice) loop
2592 if Nkind_In (Choice, N_Range,
2593 N_Subtype_Indication)
2594 or else (Is_Entity_Name (Choice)
2595 and then Is_Type (Entity (Choice)))
2596 then
2597 Get_Index_Bounds (Choice, Low, High);
2598
2599 if not Compile_Time_Known_Value (Low) then
2600 Collect_Identifiers (Low);
2601
2602 if No (Aggr_Error_Node) then
2603 Aggr_Error_Node := Low;
2604 end if;
2605 end if;
2606
2607 if not Compile_Time_Known_Value (High) then
2608 Collect_Identifiers (High);
2609
2610 if No (Aggr_Error_Node) then
2611 Aggr_Error_Node := High;
2612 end if;
2613 end if;
2614
2615 -- The RM rule is violated if there is more than
2616 -- a single choice in a component association.
2617
2618 else
2619 Count_Components := Count_Components + 1;
2620
2621 if No (Aggr_Error_Node)
2622 and then Count_Components > 1
2623 then
2624 Aggr_Error_Node := Choice;
2625 end if;
2626
2627 if not Compile_Time_Known_Value (Choice) then
2628 Collect_Identifiers (Choice);
2629 end if;
2630 end if;
2631
2632 Next (Choice);
2633 end loop;
2634
2635 Next (Assoc);
2636 end loop;
2637 end;
2638 end if;
2639
2640 -- Handle ancestor part of extension aggregates
2641
2642 if Nkind (N) = N_Extension_Aggregate then
2643 Collect_Identifiers (Ancestor_Part (N));
2644 end if;
2645
2646 -- Handle positional associations
2647
2648 if Present (Expressions (N)) then
2649 Comp_Expr := First (Expressions (N));
2650 while Present (Comp_Expr) loop
2651 if not Is_OK_Static_Expression (Comp_Expr) then
2652 Collect_Identifiers (Comp_Expr);
2653 end if;
2654
2655 Next (Comp_Expr);
2656 end loop;
2657 end if;
2658
2659 -- Handle discrete associations
2660
2661 if Present (Component_Associations (N)) then
2662 Assoc := First (Component_Associations (N));
2663 while Present (Assoc) loop
2664
2665 if not Box_Present (Assoc) then
2666 Choice := First (Choices (Assoc));
2667 while Present (Choice) loop
2668
2669 -- For now we skip discriminants since it requires
2670 -- performing the analysis in two phases: first one
2671 -- analyzing discriminants and second one analyzing
2672 -- the rest of components since discriminants are
2673 -- evaluated prior to components: too much extra
2674 -- work to detect a corner case???
2675
2676 if Nkind (Choice) in N_Has_Entity
2677 and then Present (Entity (Choice))
2678 and then Ekind (Entity (Choice)) = E_Discriminant
2679 then
2680 null;
2681
2682 elsif Box_Present (Assoc) then
2683 null;
2684
2685 else
2686 if not Analyzed (Expression (Assoc)) then
2687 Comp_Expr :=
2688 New_Copy_Tree (Expression (Assoc));
2689 Set_Parent (Comp_Expr, Parent (N));
2690 Preanalyze_Without_Errors (Comp_Expr);
2691 else
2692 Comp_Expr := Expression (Assoc);
2693 end if;
2694
2695 Collect_Identifiers (Comp_Expr);
2696 end if;
2697
2698 Next (Choice);
2699 end loop;
2700 end if;
2701
2702 Next (Assoc);
2703 end loop;
2704 end if;
2705 end;
2706
2707 when others =>
2708 return;
2709 end case;
2710
2711 -- No further action needed if we already reported an error
2712
2713 if Present (Error_Node) then
2714 return;
2715 end if;
2716
2717 -- Check violation of RM 6.20/3 in aggregates
2718
2719 if Present (Aggr_Error_Node)
2720 and then Writable_Actuals_List /= No_Elist
2721 then
2722 Error_Msg_N
2723 ("value may be affected by call in other component because they "
2724 & "are evaluated in unspecified order",
2725 Node (First_Elmt (Writable_Actuals_List)));
2726 return;
2727 end if;
2728
2729 -- Check if some writable argument of a function is referenced
2730
2731 if Writable_Actuals_List /= No_Elist
2732 and then Identifiers_List /= No_Elist
2733 then
2734 declare
2735 Elmt_1 : Elmt_Id;
2736 Elmt_2 : Elmt_Id;
2737
2738 begin
2739 Elmt_1 := First_Elmt (Writable_Actuals_List);
2740 while Present (Elmt_1) loop
2741 Elmt_2 := First_Elmt (Identifiers_List);
2742 while Present (Elmt_2) loop
2743 if Entity (Node (Elmt_1)) = Entity (Node (Elmt_2)) then
2744 case Nkind (Parent (Node (Elmt_2))) is
2745 when N_Aggregate
2746 | N_Component_Association
2747 | N_Component_Declaration
2748 =>
2749 Error_Msg_N
2750 ("value may be affected by call in other "
2751 & "component because they are evaluated "
2752 & "in unspecified order",
2753 Node (Elmt_2));
2754
2755 when N_In
2756 | N_Not_In
2757 =>
2758 Error_Msg_N
2759 ("value may be affected by call in other "
2760 & "alternative because they are evaluated "
2761 & "in unspecified order",
2762 Node (Elmt_2));
2763
2764 when others =>
2765 Error_Msg_N
2766 ("value of actual may be affected by call in "
2767 & "other actual because they are evaluated "
2768 & "in unspecified order",
2769 Node (Elmt_2));
2770 end case;
2771 end if;
2772
2773 Next_Elmt (Elmt_2);
2774 end loop;
2775
2776 Next_Elmt (Elmt_1);
2777 end loop;
2778 end;
2779 end if;
2780 end Check_Function_Writable_Actuals;
2781
2782 --------------------------------
2783 -- Check_Implicit_Dereference --
2784 --------------------------------
2785
2786 procedure Check_Implicit_Dereference (N : Node_Id; Typ : Entity_Id) is
2787 Disc : Entity_Id;
2788 Desig : Entity_Id;
2789 Nam : Node_Id;
2790
2791 begin
2792 if Nkind (N) = N_Indexed_Component
2793 and then Present (Generalized_Indexing (N))
2794 then
2795 Nam := Generalized_Indexing (N);
2796 else
2797 Nam := N;
2798 end if;
2799
2800 if Ada_Version < Ada_2012
2801 or else not Has_Implicit_Dereference (Base_Type (Typ))
2802 then
2803 return;
2804
2805 elsif not Comes_From_Source (N)
2806 and then Nkind (N) /= N_Indexed_Component
2807 then
2808 return;
2809
2810 elsif Is_Entity_Name (Nam) and then Is_Type (Entity (Nam)) then
2811 null;
2812
2813 else
2814 Disc := First_Discriminant (Typ);
2815 while Present (Disc) loop
2816 if Has_Implicit_Dereference (Disc) then
2817 Desig := Designated_Type (Etype (Disc));
2818 Add_One_Interp (Nam, Disc, Desig);
2819
2820 -- If the node is a generalized indexing, add interpretation
2821 -- to that node as well, for subsequent resolution.
2822
2823 if Nkind (N) = N_Indexed_Component then
2824 Add_One_Interp (N, Disc, Desig);
2825 end if;
2826
2827 -- If the operation comes from a generic unit and the context
2828 -- is a selected component, the selector name may be global
2829 -- and set in the instance already. Remove the entity to
2830 -- force resolution of the selected component, and the
2831 -- generation of an explicit dereference if needed.
2832
2833 if In_Instance
2834 and then Nkind (Parent (Nam)) = N_Selected_Component
2835 then
2836 Set_Entity (Selector_Name (Parent (Nam)), Empty);
2837 end if;
2838
2839 exit;
2840 end if;
2841
2842 Next_Discriminant (Disc);
2843 end loop;
2844 end if;
2845 end Check_Implicit_Dereference;
2846
2847 ----------------------------------
2848 -- Check_Internal_Protected_Use --
2849 ----------------------------------
2850
2851 procedure Check_Internal_Protected_Use (N : Node_Id; Nam : Entity_Id) is
2852 S : Entity_Id;
2853 Prot : Entity_Id;
2854
2855 begin
2856 Prot := Empty;
2857
2858 S := Current_Scope;
2859 while Present (S) loop
2860 if S = Standard_Standard then
2861 exit;
2862
2863 elsif Ekind (S) = E_Function
2864 and then Ekind (Scope (S)) = E_Protected_Type
2865 then
2866 Prot := Scope (S);
2867 exit;
2868 end if;
2869
2870 S := Scope (S);
2871 end loop;
2872
2873 if Present (Prot)
2874 and then Scope (Nam) = Prot
2875 and then Ekind (Nam) /= E_Function
2876 then
2877 -- An indirect function call (e.g. a callback within a protected
2878 -- function body) is not statically illegal. If the access type is
2879 -- anonymous and is the type of an access parameter, the scope of Nam
2880 -- will be the protected type, but it is not a protected operation.
2881
2882 if Ekind (Nam) = E_Subprogram_Type
2883 and then Nkind (Associated_Node_For_Itype (Nam)) =
2884 N_Function_Specification
2885 then
2886 null;
2887
2888 elsif Nkind (N) = N_Subprogram_Renaming_Declaration then
2889 Error_Msg_N
2890 ("within protected function cannot use protected procedure in "
2891 & "renaming or as generic actual", N);
2892
2893 elsif Nkind (N) = N_Attribute_Reference then
2894 Error_Msg_N
2895 ("within protected function cannot take access of protected "
2896 & "procedure", N);
2897
2898 else
2899 Error_Msg_N
2900 ("within protected function, protected object is constant", N);
2901 Error_Msg_N
2902 ("\cannot call operation that may modify it", N);
2903 end if;
2904 end if;
2905
2906 -- Verify that an internal call does not appear within a precondition
2907 -- of a protected operation. This implements AI12-0166.
2908 -- The precondition aspect has been rewritten as a pragma Precondition
2909 -- and we check whether the scope of the called subprogram is the same
2910 -- as that of the entity to which the aspect applies.
2911
2912 if Convention (Nam) = Convention_Protected then
2913 declare
2914 P : Node_Id;
2915
2916 begin
2917 P := Parent (N);
2918 while Present (P) loop
2919 if Nkind (P) = N_Pragma
2920 and then Chars (Pragma_Identifier (P)) = Name_Precondition
2921 and then From_Aspect_Specification (P)
2922 and then
2923 Scope (Entity (Corresponding_Aspect (P))) = Scope (Nam)
2924 then
2925 Error_Msg_N
2926 ("internal call cannot appear in precondition of "
2927 & "protected operation", N);
2928 return;
2929
2930 elsif Nkind (P) = N_Pragma
2931 and then Chars (Pragma_Identifier (P)) = Name_Contract_Cases
2932 then
2933 -- Check whether call is in a case guard. It is legal in a
2934 -- consequence.
2935
2936 P := N;
2937 while Present (P) loop
2938 if Nkind (Parent (P)) = N_Component_Association
2939 and then P /= Expression (Parent (P))
2940 then
2941 Error_Msg_N
2942 ("internal call cannot appear in case guard in a "
2943 & "contract case", N);
2944 end if;
2945
2946 P := Parent (P);
2947 end loop;
2948
2949 return;
2950
2951 elsif Nkind (P) = N_Parameter_Specification
2952 and then Scope (Current_Scope) = Scope (Nam)
2953 and then Nkind_In (Parent (P), N_Entry_Declaration,
2954 N_Subprogram_Declaration)
2955 then
2956 Error_Msg_N
2957 ("internal call cannot appear in default for formal of "
2958 & "protected operation", N);
2959 return;
2960 end if;
2961
2962 P := Parent (P);
2963 end loop;
2964 end;
2965 end if;
2966 end Check_Internal_Protected_Use;
2967
2968 ---------------------------------------
2969 -- Check_Later_Vs_Basic_Declarations --
2970 ---------------------------------------
2971
2972 procedure Check_Later_Vs_Basic_Declarations
2973 (Decls : List_Id;
2974 During_Parsing : Boolean)
2975 is
2976 Body_Sloc : Source_Ptr;
2977 Decl : Node_Id;
2978
2979 function Is_Later_Declarative_Item (Decl : Node_Id) return Boolean;
2980 -- Return whether Decl is considered as a declarative item.
2981 -- When During_Parsing is True, the semantics of Ada 83 is followed.
2982 -- When During_Parsing is False, the semantics of SPARK is followed.
2983
2984 -------------------------------
2985 -- Is_Later_Declarative_Item --
2986 -------------------------------
2987
2988 function Is_Later_Declarative_Item (Decl : Node_Id) return Boolean is
2989 begin
2990 if Nkind (Decl) in N_Later_Decl_Item then
2991 return True;
2992
2993 elsif Nkind (Decl) = N_Pragma then
2994 return True;
2995
2996 elsif During_Parsing then
2997 return False;
2998
2999 -- In SPARK, a package declaration is not considered as a later
3000 -- declarative item.
3001
3002 elsif Nkind (Decl) = N_Package_Declaration then
3003 return False;
3004
3005 -- In SPARK, a renaming is considered as a later declarative item
3006
3007 elsif Nkind (Decl) in N_Renaming_Declaration then
3008 return True;
3009
3010 else
3011 return False;
3012 end if;
3013 end Is_Later_Declarative_Item;
3014
3015 -- Start of processing for Check_Later_Vs_Basic_Declarations
3016
3017 begin
3018 Decl := First (Decls);
3019
3020 -- Loop through sequence of basic declarative items
3021
3022 Outer : while Present (Decl) loop
3023 if not Nkind_In (Decl, N_Subprogram_Body, N_Package_Body, N_Task_Body)
3024 and then Nkind (Decl) not in N_Body_Stub
3025 then
3026 Next (Decl);
3027
3028 -- Once a body is encountered, we only allow later declarative
3029 -- items. The inner loop checks the rest of the list.
3030
3031 else
3032 Body_Sloc := Sloc (Decl);
3033
3034 Inner : while Present (Decl) loop
3035 if not Is_Later_Declarative_Item (Decl) then
3036 if During_Parsing then
3037 if Ada_Version = Ada_83 then
3038 Error_Msg_Sloc := Body_Sloc;
3039 Error_Msg_N
3040 ("(Ada 83) decl cannot appear after body#", Decl);
3041 end if;
3042 else
3043 Error_Msg_Sloc := Body_Sloc;
3044 Check_SPARK_05_Restriction
3045 ("decl cannot appear after body#", Decl);
3046 end if;
3047 end if;
3048
3049 Next (Decl);
3050 end loop Inner;
3051 end if;
3052 end loop Outer;
3053 end Check_Later_Vs_Basic_Declarations;
3054
3055 ---------------------------
3056 -- Check_No_Hidden_State --
3057 ---------------------------
3058
3059 procedure Check_No_Hidden_State (Id : Entity_Id) is
3060 function Has_Null_Abstract_State (Pkg : Entity_Id) return Boolean;
3061 -- Determine whether the entity of a package denoted by Pkg has a null
3062 -- abstract state.
3063
3064 -----------------------------
3065 -- Has_Null_Abstract_State --
3066 -----------------------------
3067
3068 function Has_Null_Abstract_State (Pkg : Entity_Id) return Boolean is
3069 States : constant Elist_Id := Abstract_States (Pkg);
3070
3071 begin
3072 -- Check first available state of related package. A null abstract
3073 -- state always appears as the sole element of the state list.
3074
3075 return
3076 Present (States)
3077 and then Is_Null_State (Node (First_Elmt (States)));
3078 end Has_Null_Abstract_State;
3079
3080 -- Local variables
3081
3082 Context : Entity_Id := Empty;
3083 Not_Visible : Boolean := False;
3084 Scop : Entity_Id;
3085
3086 -- Start of processing for Check_No_Hidden_State
3087
3088 begin
3089 pragma Assert (Ekind_In (Id, E_Abstract_State, E_Variable));
3090
3091 -- Find the proper context where the object or state appears
3092
3093 Scop := Scope (Id);
3094 while Present (Scop) loop
3095 Context := Scop;
3096
3097 -- Keep track of the context's visibility
3098
3099 Not_Visible := Not_Visible or else In_Private_Part (Context);
3100
3101 -- Prevent the search from going too far
3102
3103 if Context = Standard_Standard then
3104 return;
3105
3106 -- Objects and states that appear immediately within a subprogram or
3107 -- inside a construct nested within a subprogram do not introduce a
3108 -- hidden state. They behave as local variable declarations.
3109
3110 elsif Is_Subprogram (Context) then
3111 return;
3112
3113 -- When examining a package body, use the entity of the spec as it
3114 -- carries the abstract state declarations.
3115
3116 elsif Ekind (Context) = E_Package_Body then
3117 Context := Spec_Entity (Context);
3118 end if;
3119
3120 -- Stop the traversal when a package subject to a null abstract state
3121 -- has been found.
3122
3123 if Ekind_In (Context, E_Generic_Package, E_Package)
3124 and then Has_Null_Abstract_State (Context)
3125 then
3126 exit;
3127 end if;
3128
3129 Scop := Scope (Scop);
3130 end loop;
3131
3132 -- At this point we know that there is at least one package with a null
3133 -- abstract state in visibility. Emit an error message unconditionally
3134 -- if the entity being processed is a state because the placement of the
3135 -- related package is irrelevant. This is not the case for objects as
3136 -- the intermediate context matters.
3137
3138 if Present (Context)
3139 and then (Ekind (Id) = E_Abstract_State or else Not_Visible)
3140 then
3141 Error_Msg_N ("cannot introduce hidden state &", Id);
3142 Error_Msg_NE ("\package & has null abstract state", Id, Context);
3143 end if;
3144 end Check_No_Hidden_State;
3145
3146 ----------------------------------------
3147 -- Check_Nonvolatile_Function_Profile --
3148 ----------------------------------------
3149
3150 procedure Check_Nonvolatile_Function_Profile (Func_Id : Entity_Id) is
3151 Formal : Entity_Id;
3152
3153 begin
3154 -- Inspect all formal parameters
3155
3156 Formal := First_Formal (Func_Id);
3157 while Present (Formal) loop
3158 if Is_Effectively_Volatile (Etype (Formal)) then
3159 Error_Msg_NE
3160 ("nonvolatile function & cannot have a volatile parameter",
3161 Formal, Func_Id);
3162 end if;
3163
3164 Next_Formal (Formal);
3165 end loop;
3166
3167 -- Inspect the return type
3168
3169 if Is_Effectively_Volatile (Etype (Func_Id)) then
3170 Error_Msg_NE
3171 ("nonvolatile function & cannot have a volatile return type",
3172 Result_Definition (Parent (Func_Id)), Func_Id);
3173 end if;
3174 end Check_Nonvolatile_Function_Profile;
3175
3176 ------------------------------------------
3177 -- Check_Potentially_Blocking_Operation --
3178 ------------------------------------------
3179
3180 procedure Check_Potentially_Blocking_Operation (N : Node_Id) is
3181 S : Entity_Id;
3182
3183 begin
3184 -- N is one of the potentially blocking operations listed in 9.5.1(8).
3185 -- When pragma Detect_Blocking is active, the run time will raise
3186 -- Program_Error. Here we only issue a warning, since we generally
3187 -- support the use of potentially blocking operations in the absence
3188 -- of the pragma.
3189
3190 -- Indirect blocking through a subprogram call cannot be diagnosed
3191 -- statically without interprocedural analysis, so we do not attempt
3192 -- to do it here.
3193
3194 S := Scope (Current_Scope);
3195 while Present (S) and then S /= Standard_Standard loop
3196 if Is_Protected_Type (S) then
3197 Error_Msg_N
3198 ("potentially blocking operation in protected operation??", N);
3199 return;
3200 end if;
3201
3202 S := Scope (S);
3203 end loop;
3204 end Check_Potentially_Blocking_Operation;
3205
3206 ---------------------------------
3207 -- Check_Result_And_Post_State --
3208 ---------------------------------
3209
3210 procedure Check_Result_And_Post_State (Subp_Id : Entity_Id) is
3211 procedure Check_Result_And_Post_State_In_Pragma
3212 (Prag : Node_Id;
3213 Result_Seen : in out Boolean);
3214 -- Determine whether pragma Prag mentions attribute 'Result and whether
3215 -- the pragma contains an expression that evaluates differently in pre-
3216 -- and post-state. Prag is a [refined] postcondition or a contract-cases
3217 -- pragma. Result_Seen is set when the pragma mentions attribute 'Result
3218
3219 function Has_In_Out_Parameter (Subp_Id : Entity_Id) return Boolean;
3220 -- Determine whether subprogram Subp_Id contains at least one IN OUT
3221 -- formal parameter.
3222
3223 -------------------------------------------
3224 -- Check_Result_And_Post_State_In_Pragma --
3225 -------------------------------------------
3226
3227 procedure Check_Result_And_Post_State_In_Pragma
3228 (Prag : Node_Id;
3229 Result_Seen : in out Boolean)
3230 is
3231 procedure Check_Conjunct (Expr : Node_Id);
3232 -- Check an individual conjunct in a conjunction of Boolean
3233 -- expressions, connected by "and" or "and then" operators.
3234
3235 procedure Check_Conjuncts (Expr : Node_Id);
3236 -- Apply the post-state check to every conjunct in an expression, in
3237 -- case this is a conjunction of Boolean expressions. Otherwise apply
3238 -- it to the expression as a whole.
3239
3240 procedure Check_Expression (Expr : Node_Id);
3241 -- Perform the 'Result and post-state checks on a given expression
3242
3243 function Is_Function_Result (N : Node_Id) return Traverse_Result;
3244 -- Attempt to find attribute 'Result in a subtree denoted by N
3245
3246 function Is_Trivial_Boolean (N : Node_Id) return Boolean;
3247 -- Determine whether source node N denotes "True" or "False"
3248
3249 function Mentions_Post_State (N : Node_Id) return Boolean;
3250 -- Determine whether a subtree denoted by N mentions any construct
3251 -- that denotes a post-state.
3252
3253 procedure Check_Function_Result is
3254 new Traverse_Proc (Is_Function_Result);
3255
3256 --------------------
3257 -- Check_Conjunct --
3258 --------------------
3259
3260 procedure Check_Conjunct (Expr : Node_Id) is
3261 function Adjust_Message (Msg : String) return String;
3262 -- Prepend a prefix to the input message Msg denoting that the
3263 -- message applies to a conjunct in the expression, when this
3264 -- is the case.
3265
3266 function Applied_On_Conjunct return Boolean;
3267 -- Returns True if the message applies to a conjunct in the
3268 -- expression, instead of the whole expression.
3269
3270 --------------------
3271 -- Adjust_Message --
3272 --------------------
3273
3274 function Adjust_Message (Msg : String) return String is
3275 begin
3276 if Applied_On_Conjunct then
3277 return "conjunct in " & Msg;
3278 else
3279 return Msg;
3280 end if;
3281 end Adjust_Message;
3282
3283 -------------------------
3284 -- Applied_On_Conjunct --
3285 -------------------------
3286
3287 function Applied_On_Conjunct return Boolean is
3288 begin
3289 -- Expr is the conjunct of an enclosing "and" expression
3290
3291 return Nkind (Parent (Expr)) in N_Subexpr
3292
3293 -- or Expr is a conjunct of an enclosing "and then"
3294 -- expression in a postcondition aspect that was split into
3295 -- multiple pragmas. The first conjunct has the "and then"
3296 -- expression as Original_Node, and other conjuncts have
3297 -- Split_PCC set to True.
3298
3299 or else Nkind (Original_Node (Expr)) = N_And_Then
3300 or else Split_PPC (Prag);
3301 end Applied_On_Conjunct;
3302
3303 -- Local variables
3304
3305 Err_Node : Node_Id;
3306 -- Error node when reporting a warning on a (refined)
3307 -- postcondition.
3308
3309 -- Start of processing for Check_Conjunct
3310
3311 begin
3312 if Applied_On_Conjunct then
3313 Err_Node := Expr;
3314 else
3315 Err_Node := Prag;
3316 end if;
3317
3318 if not Is_Trivial_Boolean (Expr)
3319 and then not Mentions_Post_State (Expr)
3320 then
3321 if Pragma_Name (Prag) = Name_Contract_Cases then
3322 Error_Msg_NE (Adjust_Message
3323 ("contract case does not check the outcome of calling "
3324 & "&?T?"), Expr, Subp_Id);
3325
3326 elsif Pragma_Name (Prag) = Name_Refined_Post then
3327 Error_Msg_NE (Adjust_Message
3328 ("refined postcondition does not check the outcome of "
3329 & "calling &?T?"), Err_Node, Subp_Id);
3330
3331 else
3332 Error_Msg_NE (Adjust_Message
3333 ("postcondition does not check the outcome of calling "
3334 & "&?T?"), Err_Node, Subp_Id);
3335 end if;
3336 end if;
3337 end Check_Conjunct;
3338
3339 ---------------------
3340 -- Check_Conjuncts --
3341 ---------------------
3342
3343 procedure Check_Conjuncts (Expr : Node_Id) is
3344 begin
3345 if Nkind_In (Expr, N_Op_And, N_And_Then) then
3346 Check_Conjuncts (Left_Opnd (Expr));
3347 Check_Conjuncts (Right_Opnd (Expr));
3348 else
3349 Check_Conjunct (Expr);
3350 end if;
3351 end Check_Conjuncts;
3352
3353 ----------------------
3354 -- Check_Expression --
3355 ----------------------
3356
3357 procedure Check_Expression (Expr : Node_Id) is
3358 begin
3359 if not Is_Trivial_Boolean (Expr) then
3360 Check_Function_Result (Expr);
3361 Check_Conjuncts (Expr);
3362 end if;
3363 end Check_Expression;
3364
3365 ------------------------
3366 -- Is_Function_Result --
3367 ------------------------
3368
3369 function Is_Function_Result (N : Node_Id) return Traverse_Result is
3370 begin
3371 if Is_Attribute_Result (N) then
3372 Result_Seen := True;
3373 return Abandon;
3374
3375 -- Continue the traversal
3376
3377 else
3378 return OK;
3379 end if;
3380 end Is_Function_Result;
3381
3382 ------------------------
3383 -- Is_Trivial_Boolean --
3384 ------------------------
3385
3386 function Is_Trivial_Boolean (N : Node_Id) return Boolean is
3387 begin
3388 return
3389 Comes_From_Source (N)
3390 and then Is_Entity_Name (N)
3391 and then (Entity (N) = Standard_True
3392 or else
3393 Entity (N) = Standard_False);
3394 end Is_Trivial_Boolean;
3395
3396 -------------------------
3397 -- Mentions_Post_State --
3398 -------------------------
3399
3400 function Mentions_Post_State (N : Node_Id) return Boolean is
3401 Post_State_Seen : Boolean := False;
3402
3403 function Is_Post_State (N : Node_Id) return Traverse_Result;
3404 -- Attempt to find a construct that denotes a post-state. If this
3405 -- is the case, set flag Post_State_Seen.
3406
3407 -------------------
3408 -- Is_Post_State --
3409 -------------------
3410
3411 function Is_Post_State (N : Node_Id) return Traverse_Result is
3412 Ent : Entity_Id;
3413
3414 begin
3415 if Nkind_In (N, N_Explicit_Dereference, N_Function_Call) then
3416 Post_State_Seen := True;
3417 return Abandon;
3418
3419 elsif Nkind_In (N, N_Expanded_Name, N_Identifier) then
3420 Ent := Entity (N);
3421
3422 -- Treat an undecorated reference as OK
3423
3424 if No (Ent)
3425
3426 -- A reference to an assignable entity is considered a
3427 -- change in the post-state of a subprogram.
3428
3429 or else Ekind_In (Ent, E_Generic_In_Out_Parameter,
3430 E_In_Out_Parameter,
3431 E_Out_Parameter,
3432 E_Variable)
3433
3434 -- The reference may be modified through a dereference
3435
3436 or else (Is_Access_Type (Etype (Ent))
3437 and then Nkind (Parent (N)) =
3438 N_Selected_Component)
3439 then
3440 Post_State_Seen := True;
3441 return Abandon;
3442 end if;
3443
3444 elsif Nkind (N) = N_Attribute_Reference then
3445 if Attribute_Name (N) = Name_Old then
3446 return Skip;
3447
3448 elsif Attribute_Name (N) = Name_Result then
3449 Post_State_Seen := True;
3450 return Abandon;
3451 end if;
3452 end if;
3453
3454 return OK;
3455 end Is_Post_State;
3456
3457 procedure Find_Post_State is new Traverse_Proc (Is_Post_State);
3458
3459 -- Start of processing for Mentions_Post_State
3460
3461 begin
3462 Find_Post_State (N);
3463
3464 return Post_State_Seen;
3465 end Mentions_Post_State;
3466
3467 -- Local variables
3468
3469 Expr : constant Node_Id :=
3470 Get_Pragma_Arg
3471 (First (Pragma_Argument_Associations (Prag)));
3472 Nam : constant Name_Id := Pragma_Name (Prag);
3473 CCase : Node_Id;
3474
3475 -- Start of processing for Check_Result_And_Post_State_In_Pragma
3476
3477 begin
3478 -- Examine all consequences
3479
3480 if Nam = Name_Contract_Cases then
3481 CCase := First (Component_Associations (Expr));
3482 while Present (CCase) loop
3483 Check_Expression (Expression (CCase));
3484
3485 Next (CCase);
3486 end loop;
3487
3488 -- Examine the expression of a postcondition
3489
3490 else pragma Assert (Nam_In (Nam, Name_Postcondition,
3491 Name_Refined_Post));
3492 Check_Expression (Expr);
3493 end if;
3494 end Check_Result_And_Post_State_In_Pragma;
3495
3496 --------------------------
3497 -- Has_In_Out_Parameter --
3498 --------------------------
3499
3500 function Has_In_Out_Parameter (Subp_Id : Entity_Id) return Boolean is
3501 Formal : Entity_Id;
3502
3503 begin
3504 -- Traverse the formals looking for an IN OUT parameter
3505
3506 Formal := First_Formal (Subp_Id);
3507 while Present (Formal) loop
3508 if Ekind (Formal) = E_In_Out_Parameter then
3509 return True;
3510 end if;
3511
3512 Next_Formal (Formal);
3513 end loop;
3514
3515 return False;
3516 end Has_In_Out_Parameter;
3517
3518 -- Local variables
3519
3520 Items : constant Node_Id := Contract (Subp_Id);
3521 Subp_Decl : constant Node_Id := Unit_Declaration_Node (Subp_Id);
3522 Case_Prag : Node_Id := Empty;
3523 Post_Prag : Node_Id := Empty;
3524 Prag : Node_Id;
3525 Seen_In_Case : Boolean := False;
3526 Seen_In_Post : Boolean := False;
3527 Spec_Id : Entity_Id;
3528
3529 -- Start of processing for Check_Result_And_Post_State
3530
3531 begin
3532 -- The lack of attribute 'Result or a post-state is classified as a
3533 -- suspicious contract. Do not perform the check if the corresponding
3534 -- swich is not set.
3535
3536 if not Warn_On_Suspicious_Contract then
3537 return;
3538
3539 -- Nothing to do if there is no contract
3540
3541 elsif No (Items) then
3542 return;
3543 end if;
3544
3545 -- Retrieve the entity of the subprogram spec (if any)
3546
3547 if Nkind (Subp_Decl) = N_Subprogram_Body
3548 and then Present (Corresponding_Spec (Subp_Decl))
3549 then
3550 Spec_Id := Corresponding_Spec (Subp_Decl);
3551
3552 elsif Nkind (Subp_Decl) = N_Subprogram_Body_Stub
3553 and then Present (Corresponding_Spec_Of_Stub (Subp_Decl))
3554 then
3555 Spec_Id := Corresponding_Spec_Of_Stub (Subp_Decl);
3556
3557 else
3558 Spec_Id := Subp_Id;
3559 end if;
3560
3561 -- Examine all postconditions for attribute 'Result and a post-state
3562
3563 Prag := Pre_Post_Conditions (Items);
3564 while Present (Prag) loop
3565 if Nam_In (Pragma_Name_Unmapped (Prag),
3566 Name_Postcondition, Name_Refined_Post)
3567 and then not Error_Posted (Prag)
3568 then
3569 Post_Prag := Prag;
3570 Check_Result_And_Post_State_In_Pragma (Prag, Seen_In_Post);
3571 end if;
3572
3573 Prag := Next_Pragma (Prag);
3574 end loop;
3575
3576 -- Examine the contract cases of the subprogram for attribute 'Result
3577 -- and a post-state.
3578
3579 Prag := Contract_Test_Cases (Items);
3580 while Present (Prag) loop
3581 if Pragma_Name (Prag) = Name_Contract_Cases
3582 and then not Error_Posted (Prag)
3583 then
3584 Case_Prag := Prag;
3585 Check_Result_And_Post_State_In_Pragma (Prag, Seen_In_Case);
3586 end if;
3587
3588 Prag := Next_Pragma (Prag);
3589 end loop;
3590
3591 -- Do not emit any errors if the subprogram is not a function
3592
3593 if not Ekind_In (Spec_Id, E_Function, E_Generic_Function) then
3594 null;
3595
3596 -- Regardless of whether the function has postconditions or contract
3597 -- cases, or whether they mention attribute 'Result, an IN OUT formal
3598 -- parameter is always treated as a result.
3599
3600 elsif Has_In_Out_Parameter (Spec_Id) then
3601 null;
3602
3603 -- The function has both a postcondition and contract cases and they do
3604 -- not mention attribute 'Result.
3605
3606 elsif Present (Case_Prag)
3607 and then not Seen_In_Case
3608 and then Present (Post_Prag)
3609 and then not Seen_In_Post
3610 then
3611 Error_Msg_N
3612 ("neither postcondition nor contract cases mention function "
3613 & "result?T?", Post_Prag);
3614
3615 -- The function has contract cases only and they do not mention
3616 -- attribute 'Result.
3617
3618 elsif Present (Case_Prag) and then not Seen_In_Case then
3619 Error_Msg_N ("contract cases do not mention result?T?", Case_Prag);
3620
3621 -- The function has postconditions only and they do not mention
3622 -- attribute 'Result.
3623
3624 elsif Present (Post_Prag) and then not Seen_In_Post then
3625 Error_Msg_N
3626 ("postcondition does not mention function result?T?", Post_Prag);
3627 end if;
3628 end Check_Result_And_Post_State;
3629
3630 -----------------------------
3631 -- Check_State_Refinements --
3632 -----------------------------
3633
3634 procedure Check_State_Refinements
3635 (Context : Node_Id;
3636 Is_Main_Unit : Boolean := False)
3637 is
3638 procedure Check_Package (Pack : Node_Id);
3639 -- Verify that all abstract states of a [generic] package denoted by its
3640 -- declarative node Pack have proper refinement. Recursively verify the
3641 -- visible and private declarations of the [generic] package for other
3642 -- nested packages.
3643
3644 procedure Check_Packages_In (Decls : List_Id);
3645 -- Seek out [generic] package declarations within declarative list Decls
3646 -- and verify the status of their abstract state refinement.
3647
3648 function SPARK_Mode_Is_Off (N : Node_Id) return Boolean;
3649 -- Determine whether construct N is subject to pragma SPARK_Mode Off
3650
3651 -------------------
3652 -- Check_Package --
3653 -------------------
3654
3655 procedure Check_Package (Pack : Node_Id) is
3656 Body_Id : constant Entity_Id := Corresponding_Body (Pack);
3657 Spec : constant Node_Id := Specification (Pack);
3658 States : constant Elist_Id :=
3659 Abstract_States (Defining_Entity (Pack));
3660
3661 State_Elmt : Elmt_Id;
3662 State_Id : Entity_Id;
3663
3664 begin
3665 -- Do not verify proper state refinement when the package is subject
3666 -- to pragma SPARK_Mode Off because this disables the requirement for
3667 -- state refinement.
3668
3669 if SPARK_Mode_Is_Off (Pack) then
3670 null;
3671
3672 -- State refinement can only occur in a completing packge body. Do
3673 -- not verify proper state refinement when the body is subject to
3674 -- pragma SPARK_Mode Off because this disables the requirement for
3675 -- state refinement.
3676
3677 elsif Present (Body_Id)
3678 and then SPARK_Mode_Is_Off (Unit_Declaration_Node (Body_Id))
3679 then
3680 null;
3681
3682 -- Do not verify proper state refinement when the package is an
3683 -- instance as this check was already performed in the generic.
3684
3685 elsif Present (Generic_Parent (Spec)) then
3686 null;
3687
3688 -- Otherwise examine the contents of the package
3689
3690 else
3691 if Present (States) then
3692 State_Elmt := First_Elmt (States);
3693 while Present (State_Elmt) loop
3694 State_Id := Node (State_Elmt);
3695
3696 -- Emit an error when a non-null state lacks any form of
3697 -- refinement.
3698
3699 if not Is_Null_State (State_Id)
3700 and then not Has_Null_Refinement (State_Id)
3701 and then not Has_Non_Null_Refinement (State_Id)
3702 then
3703 Error_Msg_N ("state & requires refinement", State_Id);
3704 end if;
3705
3706 Next_Elmt (State_Elmt);
3707 end loop;
3708 end if;
3709
3710 Check_Packages_In (Visible_Declarations (Spec));
3711 Check_Packages_In (Private_Declarations (Spec));
3712 end if;
3713 end Check_Package;
3714
3715 -----------------------
3716 -- Check_Packages_In --
3717 -----------------------
3718
3719 procedure Check_Packages_In (Decls : List_Id) is
3720 Decl : Node_Id;
3721
3722 begin
3723 if Present (Decls) then
3724 Decl := First (Decls);
3725 while Present (Decl) loop
3726 if Nkind_In (Decl, N_Generic_Package_Declaration,
3727 N_Package_Declaration)
3728 then
3729 Check_Package (Decl);
3730 end if;
3731
3732 Next (Decl);
3733 end loop;
3734 end if;
3735 end Check_Packages_In;
3736
3737 -----------------------
3738 -- SPARK_Mode_Is_Off --
3739 -----------------------
3740
3741 function SPARK_Mode_Is_Off (N : Node_Id) return Boolean is
3742 Id : constant Entity_Id := Defining_Entity (N);
3743 Prag : constant Node_Id := SPARK_Pragma (Id);
3744
3745 begin
3746 -- Default the mode to "off" when the context is an instance and all
3747 -- SPARK_Mode pragmas found within are to be ignored.
3748
3749 if Ignore_SPARK_Mode_Pragmas (Id) then
3750 return True;
3751
3752 else
3753 return
3754 Present (Prag)
3755 and then Get_SPARK_Mode_From_Annotation (Prag) = Off;
3756 end if;
3757 end SPARK_Mode_Is_Off;
3758
3759 -- Start of processing for Check_State_Refinements
3760
3761 begin
3762 -- A block may declare a nested package
3763
3764 if Nkind (Context) = N_Block_Statement then
3765 Check_Packages_In (Declarations (Context));
3766
3767 -- An entry, protected, subprogram, or task body may declare a nested
3768 -- package.
3769
3770 elsif Nkind_In (Context, N_Entry_Body,
3771 N_Protected_Body,
3772 N_Subprogram_Body,
3773 N_Task_Body)
3774 then
3775 -- Do not verify proper state refinement when the body is subject to
3776 -- pragma SPARK_Mode Off because this disables the requirement for
3777 -- state refinement.
3778
3779 if not SPARK_Mode_Is_Off (Context) then
3780 Check_Packages_In (Declarations (Context));
3781 end if;
3782
3783 -- A package body may declare a nested package
3784
3785 elsif Nkind (Context) = N_Package_Body then
3786 Check_Package (Unit_Declaration_Node (Corresponding_Spec (Context)));
3787
3788 -- Do not verify proper state refinement when the body is subject to
3789 -- pragma SPARK_Mode Off because this disables the requirement for
3790 -- state refinement.
3791
3792 if not SPARK_Mode_Is_Off (Context) then
3793 Check_Packages_In (Declarations (Context));
3794 end if;
3795
3796 -- A library level [generic] package may declare a nested package
3797
3798 elsif Nkind_In (Context, N_Generic_Package_Declaration,
3799 N_Package_Declaration)
3800 and then Is_Main_Unit
3801 then
3802 Check_Package (Context);
3803 end if;
3804 end Check_State_Refinements;
3805
3806 ------------------------------
3807 -- Check_Unprotected_Access --
3808 ------------------------------
3809
3810 procedure Check_Unprotected_Access
3811 (Context : Node_Id;
3812 Expr : Node_Id)
3813 is
3814 Cont_Encl_Typ : Entity_Id;
3815 Pref_Encl_Typ : Entity_Id;
3816
3817 function Enclosing_Protected_Type (Obj : Node_Id) return Entity_Id;
3818 -- Check whether Obj is a private component of a protected object.
3819 -- Return the protected type where the component resides, Empty
3820 -- otherwise.
3821
3822 function Is_Public_Operation return Boolean;
3823 -- Verify that the enclosing operation is callable from outside the
3824 -- protected object, to minimize false positives.
3825
3826 ------------------------------
3827 -- Enclosing_Protected_Type --
3828 ------------------------------
3829
3830 function Enclosing_Protected_Type (Obj : Node_Id) return Entity_Id is
3831 begin
3832 if Is_Entity_Name (Obj) then
3833 declare
3834 Ent : Entity_Id := Entity (Obj);
3835
3836 begin
3837 -- The object can be a renaming of a private component, use
3838 -- the original record component.
3839
3840 if Is_Prival (Ent) then
3841 Ent := Prival_Link (Ent);
3842 end if;
3843
3844 if Is_Protected_Type (Scope (Ent)) then
3845 return Scope (Ent);
3846 end if;
3847 end;
3848 end if;
3849
3850 -- For indexed and selected components, recursively check the prefix
3851
3852 if Nkind_In (Obj, N_Indexed_Component, N_Selected_Component) then
3853 return Enclosing_Protected_Type (Prefix (Obj));
3854
3855 -- The object does not denote a protected component
3856
3857 else
3858 return Empty;
3859 end if;
3860 end Enclosing_Protected_Type;
3861
3862 -------------------------
3863 -- Is_Public_Operation --
3864 -------------------------
3865
3866 function Is_Public_Operation return Boolean is
3867 S : Entity_Id;
3868 E : Entity_Id;
3869
3870 begin
3871 S := Current_Scope;
3872 while Present (S) and then S /= Pref_Encl_Typ loop
3873 if Scope (S) = Pref_Encl_Typ then
3874 E := First_Entity (Pref_Encl_Typ);
3875 while Present (E)
3876 and then E /= First_Private_Entity (Pref_Encl_Typ)
3877 loop
3878 if E = S then
3879 return True;
3880 end if;
3881
3882 Next_Entity (E);
3883 end loop;
3884 end if;
3885
3886 S := Scope (S);
3887 end loop;
3888
3889 return False;
3890 end Is_Public_Operation;
3891
3892 -- Start of processing for Check_Unprotected_Access
3893
3894 begin
3895 if Nkind (Expr) = N_Attribute_Reference
3896 and then Attribute_Name (Expr) = Name_Unchecked_Access
3897 then
3898 Cont_Encl_Typ := Enclosing_Protected_Type (Context);
3899 Pref_Encl_Typ := Enclosing_Protected_Type (Prefix (Expr));
3900
3901 -- Check whether we are trying to export a protected component to a
3902 -- context with an equal or lower access level.
3903
3904 if Present (Pref_Encl_Typ)
3905 and then No (Cont_Encl_Typ)
3906 and then Is_Public_Operation
3907 and then Scope_Depth (Pref_Encl_Typ) >=
3908 Object_Access_Level (Context)
3909 then
3910 Error_Msg_N
3911 ("??possible unprotected access to protected data", Expr);
3912 end if;
3913 end if;
3914 end Check_Unprotected_Access;
3915
3916 ------------------------------
3917 -- Check_Unused_Body_States --
3918 ------------------------------
3919
3920 procedure Check_Unused_Body_States (Body_Id : Entity_Id) is
3921 procedure Process_Refinement_Clause
3922 (Clause : Node_Id;
3923 States : Elist_Id);
3924 -- Inspect all constituents of refinement clause Clause and remove any
3925 -- matches from body state list States.
3926
3927 procedure Report_Unused_Body_States (States : Elist_Id);
3928 -- Emit errors for each abstract state or object found in list States
3929
3930 -------------------------------
3931 -- Process_Refinement_Clause --
3932 -------------------------------
3933
3934 procedure Process_Refinement_Clause
3935 (Clause : Node_Id;
3936 States : Elist_Id)
3937 is
3938 procedure Process_Constituent (Constit : Node_Id);
3939 -- Remove constituent Constit from body state list States
3940
3941 -------------------------
3942 -- Process_Constituent --
3943 -------------------------
3944
3945 procedure Process_Constituent (Constit : Node_Id) is
3946 Constit_Id : Entity_Id;
3947
3948 begin
3949 -- Guard against illegal constituents. Only abstract states and
3950 -- objects can appear on the right hand side of a refinement.
3951
3952 if Is_Entity_Name (Constit) then
3953 Constit_Id := Entity_Of (Constit);
3954
3955 if Present (Constit_Id)
3956 and then Ekind_In (Constit_Id, E_Abstract_State,
3957 E_Constant,
3958 E_Variable)
3959 then
3960 Remove (States, Constit_Id);
3961 end if;
3962 end if;
3963 end Process_Constituent;
3964
3965 -- Local variables
3966
3967 Constit : Node_Id;
3968
3969 -- Start of processing for Process_Refinement_Clause
3970
3971 begin
3972 if Nkind (Clause) = N_Component_Association then
3973 Constit := Expression (Clause);
3974
3975 -- Multiple constituents appear as an aggregate
3976
3977 if Nkind (Constit) = N_Aggregate then
3978 Constit := First (Expressions (Constit));
3979 while Present (Constit) loop
3980 Process_Constituent (Constit);
3981 Next (Constit);
3982 end loop;
3983
3984 -- Various forms of a single constituent
3985
3986 else
3987 Process_Constituent (Constit);
3988 end if;
3989 end if;
3990 end Process_Refinement_Clause;
3991
3992 -------------------------------
3993 -- Report_Unused_Body_States --
3994 -------------------------------
3995
3996 procedure Report_Unused_Body_States (States : Elist_Id) is
3997 Posted : Boolean := False;
3998 State_Elmt : Elmt_Id;
3999 State_Id : Entity_Id;
4000
4001 begin
4002 if Present (States) then
4003 State_Elmt := First_Elmt (States);
4004 while Present (State_Elmt) loop
4005 State_Id := Node (State_Elmt);
4006
4007 -- Constants are part of the hidden state of a package, but the
4008 -- compiler cannot determine whether they have variable input
4009 -- (SPARK RM 7.1.1(2)) and cannot classify them properly as a
4010 -- hidden state. Do not emit an error when a constant does not
4011 -- participate in a state refinement, even though it acts as a
4012 -- hidden state.
4013
4014 if Ekind (State_Id) = E_Constant then
4015 null;
4016
4017 -- Generate an error message of the form:
4018
4019 -- body of package ... has unused hidden states
4020 -- abstract state ... defined at ...
4021 -- variable ... defined at ...
4022
4023 else
4024 if not Posted then
4025 Posted := True;
4026 SPARK_Msg_N
4027 ("body of package & has unused hidden states", Body_Id);
4028 end if;
4029
4030 Error_Msg_Sloc := Sloc (State_Id);
4031
4032 if Ekind (State_Id) = E_Abstract_State then
4033 SPARK_Msg_NE
4034 ("\abstract state & defined #", Body_Id, State_Id);
4035
4036 else
4037 SPARK_Msg_NE ("\variable & defined #", Body_Id, State_Id);
4038 end if;
4039 end if;
4040
4041 Next_Elmt (State_Elmt);
4042 end loop;
4043 end if;
4044 end Report_Unused_Body_States;
4045
4046 -- Local variables
4047
4048 Prag : constant Node_Id := Get_Pragma (Body_Id, Pragma_Refined_State);
4049 Spec_Id : constant Entity_Id := Spec_Entity (Body_Id);
4050 Clause : Node_Id;
4051 States : Elist_Id;
4052
4053 -- Start of processing for Check_Unused_Body_States
4054
4055 begin
4056 -- Inspect the clauses of pragma Refined_State and determine whether all
4057 -- visible states declared within the package body participate in the
4058 -- refinement.
4059
4060 if Present (Prag) then
4061 Clause := Expression (Get_Argument (Prag, Spec_Id));
4062 States := Collect_Body_States (Body_Id);
4063
4064 -- Multiple non-null state refinements appear as an aggregate
4065
4066 if Nkind (Clause) = N_Aggregate then
4067 Clause := First (Component_Associations (Clause));
4068 while Present (Clause) loop
4069 Process_Refinement_Clause (Clause, States);
4070 Next (Clause);
4071 end loop;
4072
4073 -- Various forms of a single state refinement
4074
4075 else
4076 Process_Refinement_Clause (Clause, States);
4077 end if;
4078
4079 -- Ensure that all abstract states and objects declared in the
4080 -- package body state space are utilized as constituents.
4081
4082 Report_Unused_Body_States (States);
4083 end if;
4084 end Check_Unused_Body_States;
4085
4086 -----------------
4087 -- Choice_List --
4088 -----------------
4089
4090 function Choice_List (N : Node_Id) return List_Id is
4091 begin
4092 if Nkind (N) = N_Iterated_Component_Association then
4093 return Discrete_Choices (N);
4094 else
4095 return Choices (N);
4096 end if;
4097 end Choice_List;
4098
4099 -------------------------
4100 -- Collect_Body_States --
4101 -------------------------
4102
4103 function Collect_Body_States (Body_Id : Entity_Id) return Elist_Id is
4104 function Is_Visible_Object (Obj_Id : Entity_Id) return Boolean;
4105 -- Determine whether object Obj_Id is a suitable visible state of a
4106 -- package body.
4107
4108 procedure Collect_Visible_States
4109 (Pack_Id : Entity_Id;
4110 States : in out Elist_Id);
4111 -- Gather the entities of all abstract states and objects declared in
4112 -- the visible state space of package Pack_Id.
4113
4114 ----------------------------
4115 -- Collect_Visible_States --
4116 ----------------------------
4117
4118 procedure Collect_Visible_States
4119 (Pack_Id : Entity_Id;
4120 States : in out Elist_Id)
4121 is
4122 Item_Id : Entity_Id;
4123
4124 begin
4125 -- Traverse the entity chain of the package and inspect all visible
4126 -- items.
4127
4128 Item_Id := First_Entity (Pack_Id);
4129 while Present (Item_Id) and then not In_Private_Part (Item_Id) loop
4130
4131 -- Do not consider internally generated items as those cannot be
4132 -- named and participate in refinement.
4133
4134 if not Comes_From_Source (Item_Id) then
4135 null;
4136
4137 elsif Ekind (Item_Id) = E_Abstract_State then
4138 Append_New_Elmt (Item_Id, States);
4139
4140 elsif Ekind_In (Item_Id, E_Constant, E_Variable)
4141 and then Is_Visible_Object (Item_Id)
4142 then
4143 Append_New_Elmt (Item_Id, States);
4144
4145 -- Recursively gather the visible states of a nested package
4146
4147 elsif Ekind (Item_Id) = E_Package then
4148 Collect_Visible_States (Item_Id, States);
4149 end if;
4150
4151 Next_Entity (Item_Id);
4152 end loop;
4153 end Collect_Visible_States;
4154
4155 -----------------------
4156 -- Is_Visible_Object --
4157 -----------------------
4158
4159 function Is_Visible_Object (Obj_Id : Entity_Id) return Boolean is
4160 begin
4161 -- Objects that map generic formals to their actuals are not visible
4162 -- from outside the generic instantiation.
4163
4164 if Present (Corresponding_Generic_Association
4165 (Declaration_Node (Obj_Id)))
4166 then
4167 return False;
4168
4169 -- Constituents of a single protected/task type act as components of
4170 -- the type and are not visible from outside the type.
4171
4172 elsif Ekind (Obj_Id) = E_Variable
4173 and then Present (Encapsulating_State (Obj_Id))
4174 and then Is_Single_Concurrent_Object (Encapsulating_State (Obj_Id))
4175 then
4176 return False;
4177
4178 else
4179 return True;
4180 end if;
4181 end Is_Visible_Object;
4182
4183 -- Local variables
4184
4185 Body_Decl : constant Node_Id := Unit_Declaration_Node (Body_Id);
4186 Decl : Node_Id;
4187 Item_Id : Entity_Id;
4188 States : Elist_Id := No_Elist;
4189
4190 -- Start of processing for Collect_Body_States
4191
4192 begin
4193 -- Inspect the declarations of the body looking for source objects,
4194 -- packages and package instantiations. Note that even though this
4195 -- processing is very similar to Collect_Visible_States, a package
4196 -- body does not have a First/Next_Entity list.
4197
4198 Decl := First (Declarations (Body_Decl));
4199 while Present (Decl) loop
4200
4201 -- Capture source objects as internally generated temporaries cannot
4202 -- be named and participate in refinement.
4203
4204 if Nkind (Decl) = N_Object_Declaration then
4205 Item_Id := Defining_Entity (Decl);
4206
4207 if Comes_From_Source (Item_Id)
4208 and then Is_Visible_Object (Item_Id)
4209 then
4210 Append_New_Elmt (Item_Id, States);
4211 end if;
4212
4213 -- Capture the visible abstract states and objects of a source
4214 -- package [instantiation].
4215
4216 elsif Nkind (Decl) = N_Package_Declaration then
4217 Item_Id := Defining_Entity (Decl);
4218
4219 if Comes_From_Source (Item_Id) then
4220 Collect_Visible_States (Item_Id, States);
4221 end if;
4222 end if;
4223
4224 Next (Decl);
4225 end loop;
4226
4227 return States;
4228 end Collect_Body_States;
4229
4230 ------------------------
4231 -- Collect_Interfaces --
4232 ------------------------
4233
4234 procedure Collect_Interfaces
4235 (T : Entity_Id;
4236 Ifaces_List : out Elist_Id;
4237 Exclude_Parents : Boolean := False;
4238 Use_Full_View : Boolean := True)
4239 is
4240 procedure Collect (Typ : Entity_Id);
4241 -- Subsidiary subprogram used to traverse the whole list
4242 -- of directly and indirectly implemented interfaces
4243
4244 -------------
4245 -- Collect --
4246 -------------
4247
4248 procedure Collect (Typ : Entity_Id) is
4249 Ancestor : Entity_Id;
4250 Full_T : Entity_Id;
4251 Id : Node_Id;
4252 Iface : Entity_Id;
4253
4254 begin
4255 Full_T := Typ;
4256
4257 -- Handle private types and subtypes
4258
4259 if Use_Full_View
4260 and then Is_Private_Type (Typ)
4261 and then Present (Full_View (Typ))
4262 then
4263 Full_T := Full_View (Typ);
4264
4265 if Ekind (Full_T) = E_Record_Subtype then
4266 Full_T := Etype (Typ);
4267
4268 if Present (Full_View (Full_T)) then
4269 Full_T := Full_View (Full_T);
4270 end if;
4271 end if;
4272 end if;
4273
4274 -- Include the ancestor if we are generating the whole list of
4275 -- abstract interfaces.
4276
4277 if Etype (Full_T) /= Typ
4278
4279 -- Protect the frontend against wrong sources. For example:
4280
4281 -- package P is
4282 -- type A is tagged null record;
4283 -- type B is new A with private;
4284 -- type C is new A with private;
4285 -- private
4286 -- type B is new C with null record;
4287 -- type C is new B with null record;
4288 -- end P;
4289
4290 and then Etype (Full_T) /= T
4291 then
4292 Ancestor := Etype (Full_T);
4293 Collect (Ancestor);
4294
4295 if Is_Interface (Ancestor) and then not Exclude_Parents then
4296 Append_Unique_Elmt (Ancestor, Ifaces_List);
4297 end if;
4298 end if;
4299
4300 -- Traverse the graph of ancestor interfaces
4301
4302 if Is_Non_Empty_List (Abstract_Interface_List (Full_T)) then
4303 Id := First (Abstract_Interface_List (Full_T));
4304 while Present (Id) loop
4305 Iface := Etype (Id);
4306
4307 -- Protect against wrong uses. For example:
4308 -- type I is interface;
4309 -- type O is tagged null record;
4310 -- type Wrong is new I and O with null record; -- ERROR
4311
4312 if Is_Interface (Iface) then
4313 if Exclude_Parents
4314 and then Etype (T) /= T
4315 and then Interface_Present_In_Ancestor (Etype (T), Iface)
4316 then
4317 null;
4318 else
4319 Collect (Iface);
4320 Append_Unique_Elmt (Iface, Ifaces_List);
4321 end if;
4322 end if;
4323
4324 Next (Id);
4325 end loop;
4326 end if;
4327 end Collect;
4328
4329 -- Start of processing for Collect_Interfaces
4330
4331 begin
4332 pragma Assert (Is_Tagged_Type (T) or else Is_Concurrent_Type (T));
4333 Ifaces_List := New_Elmt_List;
4334 Collect (T);
4335 end Collect_Interfaces;
4336
4337 ----------------------------------
4338 -- Collect_Interface_Components --
4339 ----------------------------------
4340
4341 procedure Collect_Interface_Components
4342 (Tagged_Type : Entity_Id;
4343 Components_List : out Elist_Id)
4344 is
4345 procedure Collect (Typ : Entity_Id);
4346 -- Subsidiary subprogram used to climb to the parents
4347
4348 -------------
4349 -- Collect --
4350 -------------
4351
4352 procedure Collect (Typ : Entity_Id) is
4353 Tag_Comp : Entity_Id;
4354 Parent_Typ : Entity_Id;
4355
4356 begin
4357 -- Handle private types
4358
4359 if Present (Full_View (Etype (Typ))) then
4360 Parent_Typ := Full_View (Etype (Typ));
4361 else
4362 Parent_Typ := Etype (Typ);
4363 end if;
4364
4365 if Parent_Typ /= Typ
4366
4367 -- Protect the frontend against wrong sources. For example:
4368
4369 -- package P is
4370 -- type A is tagged null record;
4371 -- type B is new A with private;
4372 -- type C is new A with private;
4373 -- private
4374 -- type B is new C with null record;
4375 -- type C is new B with null record;
4376 -- end P;
4377
4378 and then Parent_Typ /= Tagged_Type
4379 then
4380 Collect (Parent_Typ);
4381 end if;
4382
4383 -- Collect the components containing tags of secondary dispatch
4384 -- tables.
4385
4386 Tag_Comp := Next_Tag_Component (First_Tag_Component (Typ));
4387 while Present (Tag_Comp) loop
4388 pragma Assert (Present (Related_Type (Tag_Comp)));
4389 Append_Elmt (Tag_Comp, Components_List);
4390
4391 Tag_Comp := Next_Tag_Component (Tag_Comp);
4392 end loop;
4393 end Collect;
4394
4395 -- Start of processing for Collect_Interface_Components
4396
4397 begin
4398 pragma Assert (Ekind (Tagged_Type) = E_Record_Type
4399 and then Is_Tagged_Type (Tagged_Type));
4400
4401 Components_List := New_Elmt_List;
4402 Collect (Tagged_Type);
4403 end Collect_Interface_Components;
4404
4405 -----------------------------
4406 -- Collect_Interfaces_Info --
4407 -----------------------------
4408
4409 procedure Collect_Interfaces_Info
4410 (T : Entity_Id;
4411 Ifaces_List : out Elist_Id;
4412 Components_List : out Elist_Id;
4413 Tags_List : out Elist_Id)
4414 is
4415 Comps_List : Elist_Id;
4416 Comp_Elmt : Elmt_Id;
4417 Comp_Iface : Entity_Id;
4418 Iface_Elmt : Elmt_Id;
4419 Iface : Entity_Id;
4420
4421 function Search_Tag (Iface : Entity_Id) return Entity_Id;
4422 -- Search for the secondary tag associated with the interface type
4423 -- Iface that is implemented by T.
4424
4425 ----------------
4426 -- Search_Tag --
4427 ----------------
4428
4429 function Search_Tag (Iface : Entity_Id) return Entity_Id is
4430 ADT : Elmt_Id;
4431 begin
4432 if not Is_CPP_Class (T) then
4433 ADT := Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (T))));
4434 else
4435 ADT := Next_Elmt (First_Elmt (Access_Disp_Table (T)));
4436 end if;
4437
4438 while Present (ADT)
4439 and then Is_Tag (Node (ADT))
4440 and then Related_Type (Node (ADT)) /= Iface
4441 loop
4442 -- Skip secondary dispatch table referencing thunks to user
4443 -- defined primitives covered by this interface.
4444
4445 pragma Assert (Has_Suffix (Node (ADT), 'P'));
4446 Next_Elmt (ADT);
4447
4448 -- Skip secondary dispatch tables of Ada types
4449
4450 if not Is_CPP_Class (T) then
4451
4452 -- Skip secondary dispatch table referencing thunks to
4453 -- predefined primitives.
4454
4455 pragma Assert (Has_Suffix (Node (ADT), 'Y'));
4456 Next_Elmt (ADT);
4457
4458 -- Skip secondary dispatch table referencing user-defined
4459 -- primitives covered by this interface.
4460
4461 pragma Assert (Has_Suffix (Node (ADT), 'D'));
4462 Next_Elmt (ADT);
4463
4464 -- Skip secondary dispatch table referencing predefined
4465 -- primitives.
4466
4467 pragma Assert (Has_Suffix (Node (ADT), 'Z'));
4468 Next_Elmt (ADT);
4469 end if;
4470 end loop;
4471
4472 pragma Assert (Is_Tag (Node (ADT)));
4473 return Node (ADT);
4474 end Search_Tag;
4475
4476 -- Start of processing for Collect_Interfaces_Info
4477
4478 begin
4479 Collect_Interfaces (T, Ifaces_List);
4480 Collect_Interface_Components (T, Comps_List);
4481
4482 -- Search for the record component and tag associated with each
4483 -- interface type of T.
4484
4485 Components_List := New_Elmt_List;
4486 Tags_List := New_Elmt_List;
4487
4488 Iface_Elmt := First_Elmt (Ifaces_List);
4489 while Present (Iface_Elmt) loop
4490 Iface := Node (Iface_Elmt);
4491
4492 -- Associate the primary tag component and the primary dispatch table
4493 -- with all the interfaces that are parents of T
4494
4495 if Is_Ancestor (Iface, T, Use_Full_View => True) then
4496 Append_Elmt (First_Tag_Component (T), Components_List);
4497 Append_Elmt (Node (First_Elmt (Access_Disp_Table (T))), Tags_List);
4498
4499 -- Otherwise search for the tag component and secondary dispatch
4500 -- table of Iface
4501
4502 else
4503 Comp_Elmt := First_Elmt (Comps_List);
4504 while Present (Comp_Elmt) loop
4505 Comp_Iface := Related_Type (Node (Comp_Elmt));
4506
4507 if Comp_Iface = Iface
4508 or else Is_Ancestor (Iface, Comp_Iface, Use_Full_View => True)
4509 then
4510 Append_Elmt (Node (Comp_Elmt), Components_List);
4511 Append_Elmt (Search_Tag (Comp_Iface), Tags_List);
4512 exit;
4513 end if;
4514
4515 Next_Elmt (Comp_Elmt);
4516 end loop;
4517 pragma Assert (Present (Comp_Elmt));
4518 end if;
4519
4520 Next_Elmt (Iface_Elmt);
4521 end loop;
4522 end Collect_Interfaces_Info;
4523
4524 ---------------------
4525 -- Collect_Parents --
4526 ---------------------
4527
4528 procedure Collect_Parents
4529 (T : Entity_Id;
4530 List : out Elist_Id;
4531 Use_Full_View : Boolean := True)
4532 is
4533 Current_Typ : Entity_Id := T;
4534 Parent_Typ : Entity_Id;
4535
4536 begin
4537 List := New_Elmt_List;
4538
4539 -- No action if the if the type has no parents
4540
4541 if T = Etype (T) then
4542 return;
4543 end if;
4544
4545 loop
4546 Parent_Typ := Etype (Current_Typ);
4547
4548 if Is_Private_Type (Parent_Typ)
4549 and then Present (Full_View (Parent_Typ))
4550 and then Use_Full_View
4551 then
4552 Parent_Typ := Full_View (Base_Type (Parent_Typ));
4553 end if;
4554
4555 Append_Elmt (Parent_Typ, List);
4556
4557 exit when Parent_Typ = Current_Typ;
4558 Current_Typ := Parent_Typ;
4559 end loop;
4560 end Collect_Parents;
4561
4562 ----------------------------------
4563 -- Collect_Primitive_Operations --
4564 ----------------------------------
4565
4566 function Collect_Primitive_Operations (T : Entity_Id) return Elist_Id is
4567 B_Type : constant Entity_Id := Base_Type (T);
4568 B_Decl : constant Node_Id := Original_Node (Parent (B_Type));
4569 B_Scope : Entity_Id := Scope (B_Type);
4570 Op_List : Elist_Id;
4571 Formal : Entity_Id;
4572 Is_Prim : Boolean;
4573 Is_Type_In_Pkg : Boolean;
4574 Formal_Derived : Boolean := False;
4575 Id : Entity_Id;
4576
4577 function Match (E : Entity_Id) return Boolean;
4578 -- True if E's base type is B_Type, or E is of an anonymous access type
4579 -- and the base type of its designated type is B_Type.
4580
4581 -----------
4582 -- Match --
4583 -----------
4584
4585 function Match (E : Entity_Id) return Boolean is
4586 Etyp : Entity_Id := Etype (E);
4587
4588 begin
4589 if Ekind (Etyp) = E_Anonymous_Access_Type then
4590 Etyp := Designated_Type (Etyp);
4591 end if;
4592
4593 -- In Ada 2012 a primitive operation may have a formal of an
4594 -- incomplete view of the parent type.
4595
4596 return Base_Type (Etyp) = B_Type
4597 or else
4598 (Ada_Version >= Ada_2012
4599 and then Ekind (Etyp) = E_Incomplete_Type
4600 and then Full_View (Etyp) = B_Type);
4601 end Match;
4602
4603 -- Start of processing for Collect_Primitive_Operations
4604
4605 begin
4606 -- For tagged types, the primitive operations are collected as they
4607 -- are declared, and held in an explicit list which is simply returned.
4608
4609 if Is_Tagged_Type (B_Type) then
4610 return Primitive_Operations (B_Type);
4611
4612 -- An untagged generic type that is a derived type inherits the
4613 -- primitive operations of its parent type. Other formal types only
4614 -- have predefined operators, which are not explicitly represented.
4615
4616 elsif Is_Generic_Type (B_Type) then
4617 if Nkind (B_Decl) = N_Formal_Type_Declaration
4618 and then Nkind (Formal_Type_Definition (B_Decl)) =
4619 N_Formal_Derived_Type_Definition
4620 then
4621 Formal_Derived := True;
4622 else
4623 return New_Elmt_List;
4624 end if;
4625 end if;
4626
4627 Op_List := New_Elmt_List;
4628
4629 if B_Scope = Standard_Standard then
4630 if B_Type = Standard_String then
4631 Append_Elmt (Standard_Op_Concat, Op_List);
4632
4633 elsif B_Type = Standard_Wide_String then
4634 Append_Elmt (Standard_Op_Concatw, Op_List);
4635
4636 else
4637 null;
4638 end if;
4639
4640 -- Locate the primitive subprograms of the type
4641
4642 else
4643 -- The primitive operations appear after the base type, except
4644 -- if the derivation happens within the private part of B_Scope
4645 -- and the type is a private type, in which case both the type
4646 -- and some primitive operations may appear before the base
4647 -- type, and the list of candidates starts after the type.
4648
4649 if In_Open_Scopes (B_Scope)
4650 and then Scope (T) = B_Scope
4651 and then In_Private_Part (B_Scope)
4652 then
4653 Id := Next_Entity (T);
4654
4655 -- In Ada 2012, If the type has an incomplete partial view, there
4656 -- may be primitive operations declared before the full view, so
4657 -- we need to start scanning from the incomplete view, which is
4658 -- earlier on the entity chain.
4659
4660 elsif Nkind (Parent (B_Type)) = N_Full_Type_Declaration
4661 and then Present (Incomplete_View (Parent (B_Type)))
4662 then
4663 Id := Defining_Entity (Incomplete_View (Parent (B_Type)));
4664
4665 -- If T is a derived from a type with an incomplete view declared
4666 -- elsewhere, that incomplete view is irrelevant, we want the
4667 -- operations in the scope of T.
4668
4669 if Scope (Id) /= Scope (B_Type) then
4670 Id := Next_Entity (B_Type);
4671 end if;
4672
4673 else
4674 Id := Next_Entity (B_Type);
4675 end if;
4676
4677 -- Set flag if this is a type in a package spec
4678
4679 Is_Type_In_Pkg :=
4680 Is_Package_Or_Generic_Package (B_Scope)
4681 and then
4682 Nkind (Parent (Declaration_Node (First_Subtype (T)))) /=
4683 N_Package_Body;
4684
4685 while Present (Id) loop
4686
4687 -- Test whether the result type or any of the parameter types of
4688 -- each subprogram following the type match that type when the
4689 -- type is declared in a package spec, is a derived type, or the
4690 -- subprogram is marked as primitive. (The Is_Primitive test is
4691 -- needed to find primitives of nonderived types in declarative
4692 -- parts that happen to override the predefined "=" operator.)
4693
4694 -- Note that generic formal subprograms are not considered to be
4695 -- primitive operations and thus are never inherited.
4696
4697 if Is_Overloadable (Id)
4698 and then (Is_Type_In_Pkg
4699 or else Is_Derived_Type (B_Type)
4700 or else Is_Primitive (Id))
4701 and then Nkind (Parent (Parent (Id)))
4702 not in N_Formal_Subprogram_Declaration
4703 then
4704 Is_Prim := False;
4705
4706 if Match (Id) then
4707 Is_Prim := True;
4708
4709 else
4710 Formal := First_Formal (Id);
4711 while Present (Formal) loop
4712 if Match (Formal) then
4713 Is_Prim := True;
4714 exit;
4715 end if;
4716
4717 Next_Formal (Formal);
4718 end loop;
4719 end if;
4720
4721 -- For a formal derived type, the only primitives are the ones
4722 -- inherited from the parent type. Operations appearing in the
4723 -- package declaration are not primitive for it.
4724
4725 if Is_Prim
4726 and then (not Formal_Derived or else Present (Alias (Id)))
4727 then
4728 -- In the special case of an equality operator aliased to
4729 -- an overriding dispatching equality belonging to the same
4730 -- type, we don't include it in the list of primitives.
4731 -- This avoids inheriting multiple equality operators when
4732 -- deriving from untagged private types whose full type is
4733 -- tagged, which can otherwise cause ambiguities. Note that
4734 -- this should only happen for this kind of untagged parent
4735 -- type, since normally dispatching operations are inherited
4736 -- using the type's Primitive_Operations list.
4737
4738 if Chars (Id) = Name_Op_Eq
4739 and then Is_Dispatching_Operation (Id)
4740 and then Present (Alias (Id))
4741 and then Present (Overridden_Operation (Alias (Id)))
4742 and then Base_Type (Etype (First_Entity (Id))) =
4743 Base_Type (Etype (First_Entity (Alias (Id))))
4744 then
4745 null;
4746
4747 -- Include the subprogram in the list of primitives
4748
4749 else
4750 Append_Elmt (Id, Op_List);
4751 end if;
4752 end if;
4753 end if;
4754
4755 Next_Entity (Id);
4756
4757 -- For a type declared in System, some of its operations may
4758 -- appear in the target-specific extension to System.
4759
4760 if No (Id)
4761 and then B_Scope = RTU_Entity (System)
4762 and then Present_System_Aux
4763 then
4764 B_Scope := System_Aux_Id;
4765 Id := First_Entity (System_Aux_Id);
4766 end if;
4767 end loop;
4768 end if;
4769
4770 return Op_List;
4771 end Collect_Primitive_Operations;
4772
4773 -----------------------------------
4774 -- Compile_Time_Constraint_Error --
4775 -----------------------------------
4776
4777 function Compile_Time_Constraint_Error
4778 (N : Node_Id;
4779 Msg : String;
4780 Ent : Entity_Id := Empty;
4781 Loc : Source_Ptr := No_Location;
4782 Warn : Boolean := False) return Node_Id
4783 is
4784 Msgc : String (1 .. Msg'Length + 3);
4785 -- Copy of message, with room for possible ?? or << and ! at end
4786
4787 Msgl : Natural;
4788 Wmsg : Boolean;
4789 Eloc : Source_Ptr;
4790
4791 -- Start of processing for Compile_Time_Constraint_Error
4792
4793 begin
4794 -- If this is a warning, convert it into an error if we are in code
4795 -- subject to SPARK_Mode being set On, unless Warn is True to force a
4796 -- warning. The rationale is that a compile-time constraint error should
4797 -- lead to an error instead of a warning when SPARK_Mode is On, but in
4798 -- a few cases we prefer to issue a warning and generate both a suitable
4799 -- run-time error in GNAT and a suitable check message in GNATprove.
4800 -- Those cases are those that likely correspond to deactivated SPARK
4801 -- code, so that this kind of code can be compiled and analyzed instead
4802 -- of being rejected.
4803
4804 Error_Msg_Warn := Warn or SPARK_Mode /= On;
4805
4806 -- A static constraint error in an instance body is not a fatal error.
4807 -- we choose to inhibit the message altogether, because there is no
4808 -- obvious node (for now) on which to post it. On the other hand the
4809 -- offending node must be replaced with a constraint_error in any case.
4810
4811 -- No messages are generated if we already posted an error on this node
4812
4813 if not Error_Posted (N) then
4814 if Loc /= No_Location then
4815 Eloc := Loc;
4816 else
4817 Eloc := Sloc (N);
4818 end if;
4819
4820 -- Copy message to Msgc, converting any ? in the message into
4821 -- < instead, so that we have an error in GNATprove mode.
4822
4823 Msgl := Msg'Length;
4824
4825 for J in 1 .. Msgl loop
4826 if Msg (J) = '?' and then (J = 1 or else Msg (J - 1) /= ''') then
4827 Msgc (J) := '<';
4828 else
4829 Msgc (J) := Msg (J);
4830 end if;
4831 end loop;
4832
4833 -- Message is a warning, even in Ada 95 case
4834
4835 if Msg (Msg'Last) = '?' or else Msg (Msg'Last) = '<' then
4836 Wmsg := True;
4837
4838 -- In Ada 83, all messages are warnings. In the private part and
4839 -- the body of an instance, constraint_checks are only warnings.
4840 -- We also make this a warning if the Warn parameter is set.
4841
4842 elsif Warn
4843 or else (Ada_Version = Ada_83 and then Comes_From_Source (N))
4844 then
4845 Msgl := Msgl + 1;
4846 Msgc (Msgl) := '<';
4847 Msgl := Msgl + 1;
4848 Msgc (Msgl) := '<';
4849 Wmsg := True;
4850
4851 elsif In_Instance_Not_Visible then
4852 Msgl := Msgl + 1;
4853 Msgc (Msgl) := '<';
4854 Msgl := Msgl + 1;
4855 Msgc (Msgl) := '<';
4856 Wmsg := True;
4857
4858 -- Otherwise we have a real error message (Ada 95 static case)
4859 -- and we make this an unconditional message. Note that in the
4860 -- warning case we do not make the message unconditional, it seems
4861 -- quite reasonable to delete messages like this (about exceptions
4862 -- that will be raised) in dead code.
4863
4864 else
4865 Wmsg := False;
4866 Msgl := Msgl + 1;
4867 Msgc (Msgl) := '!';
4868 end if;
4869
4870 -- One more test, skip the warning if the related expression is
4871 -- statically unevaluated, since we don't want to warn about what
4872 -- will happen when something is evaluated if it never will be
4873 -- evaluated.
4874
4875 if not Is_Statically_Unevaluated (N) then
4876 if Present (Ent) then
4877 Error_Msg_NEL (Msgc (1 .. Msgl), N, Ent, Eloc);
4878 else
4879 Error_Msg_NEL (Msgc (1 .. Msgl), N, Etype (N), Eloc);
4880 end if;
4881
4882 if Wmsg then
4883
4884 -- Check whether the context is an Init_Proc
4885
4886 if Inside_Init_Proc then
4887 declare
4888 Conc_Typ : constant Entity_Id :=
4889 Corresponding_Concurrent_Type
4890 (Entity (Parameter_Type (First
4891 (Parameter_Specifications
4892 (Parent (Current_Scope))))));
4893
4894 begin
4895 -- Don't complain if the corresponding concurrent type
4896 -- doesn't come from source (i.e. a single task/protected
4897 -- object).
4898
4899 if Present (Conc_Typ)
4900 and then not Comes_From_Source (Conc_Typ)
4901 then
4902 Error_Msg_NEL
4903 ("\& [<<", N, Standard_Constraint_Error, Eloc);
4904
4905 else
4906 if GNATprove_Mode then
4907 Error_Msg_NEL
4908 ("\& would have been raised for objects of this "
4909 & "type", N, Standard_Constraint_Error, Eloc);
4910 else
4911 Error_Msg_NEL
4912 ("\& will be raised for objects of this type??",
4913 N, Standard_Constraint_Error, Eloc);
4914 end if;
4915 end if;
4916 end;
4917
4918 else
4919 Error_Msg_NEL ("\& [<<", N, Standard_Constraint_Error, Eloc);
4920 end if;
4921
4922 else
4923 Error_Msg ("\static expression fails Constraint_Check", Eloc);
4924 Set_Error_Posted (N);
4925 end if;
4926 end if;
4927 end if;
4928
4929 return N;
4930 end Compile_Time_Constraint_Error;
4931
4932 -----------------------
4933 -- Conditional_Delay --
4934 -----------------------
4935
4936 procedure Conditional_Delay (New_Ent, Old_Ent : Entity_Id) is
4937 begin
4938 if Has_Delayed_Freeze (Old_Ent) and then not Is_Frozen (Old_Ent) then
4939 Set_Has_Delayed_Freeze (New_Ent);
4940 end if;
4941 end Conditional_Delay;
4942
4943 ----------------------------
4944 -- Contains_Refined_State --
4945 ----------------------------
4946
4947 function Contains_Refined_State (Prag : Node_Id) return Boolean is
4948 function Has_State_In_Dependency (List : Node_Id) return Boolean;
4949 -- Determine whether a dependency list mentions a state with a visible
4950 -- refinement.
4951
4952 function Has_State_In_Global (List : Node_Id) return Boolean;
4953 -- Determine whether a global list mentions a state with a visible
4954 -- refinement.
4955
4956 function Is_Refined_State (Item : Node_Id) return Boolean;
4957 -- Determine whether Item is a reference to an abstract state with a
4958 -- visible refinement.
4959
4960 -----------------------------
4961 -- Has_State_In_Dependency --
4962 -----------------------------
4963
4964 function Has_State_In_Dependency (List : Node_Id) return Boolean is
4965 Clause : Node_Id;
4966 Output : Node_Id;
4967
4968 begin
4969 -- A null dependency list does not mention any states
4970
4971 if Nkind (List) = N_Null then
4972 return False;
4973
4974 -- Dependency clauses appear as component associations of an
4975 -- aggregate.
4976
4977 elsif Nkind (List) = N_Aggregate
4978 and then Present (Component_Associations (List))
4979 then
4980 Clause := First (Component_Associations (List));
4981 while Present (Clause) loop
4982
4983 -- Inspect the outputs of a dependency clause
4984
4985 Output := First (Choices (Clause));
4986 while Present (Output) loop
4987 if Is_Refined_State (Output) then
4988 return True;
4989 end if;
4990
4991 Next (Output);
4992 end loop;
4993
4994 -- Inspect the outputs of a dependency clause
4995
4996 if Is_Refined_State (Expression (Clause)) then
4997 return True;
4998 end if;
4999
5000 Next (Clause);
5001 end loop;
5002
5003 -- If we get here, then none of the dependency clauses mention a
5004 -- state with visible refinement.
5005
5006 return False;
5007
5008 -- An illegal pragma managed to sneak in
5009
5010 else
5011 raise Program_Error;
5012 end if;
5013 end Has_State_In_Dependency;
5014
5015 -------------------------
5016 -- Has_State_In_Global --
5017 -------------------------
5018
5019 function Has_State_In_Global (List : Node_Id) return Boolean is
5020 Item : Node_Id;
5021
5022 begin
5023 -- A null global list does not mention any states
5024
5025 if Nkind (List) = N_Null then
5026 return False;
5027
5028 -- Simple global list or moded global list declaration
5029
5030 elsif Nkind (List) = N_Aggregate then
5031
5032 -- The declaration of a simple global list appear as a collection
5033 -- of expressions.
5034
5035 if Present (Expressions (List)) then
5036 Item := First (Expressions (List));
5037 while Present (Item) loop
5038 if Is_Refined_State (Item) then
5039 return True;
5040 end if;
5041
5042 Next (Item);
5043 end loop;
5044
5045 -- The declaration of a moded global list appears as a collection
5046 -- of component associations where individual choices denote
5047 -- modes.
5048
5049 else
5050 Item := First (Component_Associations (List));
5051 while Present (Item) loop
5052 if Has_State_In_Global (Expression (Item)) then
5053 return True;
5054 end if;
5055
5056 Next (Item);
5057 end loop;
5058 end if;
5059
5060 -- If we get here, then the simple/moded global list did not
5061 -- mention any states with a visible refinement.
5062
5063 return False;
5064
5065 -- Single global item declaration
5066
5067 elsif Is_Entity_Name (List) then
5068 return Is_Refined_State (List);
5069
5070 -- An illegal pragma managed to sneak in
5071
5072 else
5073 raise Program_Error;
5074 end if;
5075 end Has_State_In_Global;
5076
5077 ----------------------
5078 -- Is_Refined_State --
5079 ----------------------
5080
5081 function Is_Refined_State (Item : Node_Id) return Boolean is
5082 Elmt : Node_Id;
5083 Item_Id : Entity_Id;
5084
5085 begin
5086 if Nkind (Item) = N_Null then
5087 return False;
5088
5089 -- States cannot be subject to attribute 'Result. This case arises
5090 -- in dependency relations.
5091
5092 elsif Nkind (Item) = N_Attribute_Reference
5093 and then Attribute_Name (Item) = Name_Result
5094 then
5095 return False;
5096
5097 -- Multiple items appear as an aggregate. This case arises in
5098 -- dependency relations.
5099
5100 elsif Nkind (Item) = N_Aggregate
5101 and then Present (Expressions (Item))
5102 then
5103 Elmt := First (Expressions (Item));
5104 while Present (Elmt) loop
5105 if Is_Refined_State (Elmt) then
5106 return True;
5107 end if;
5108
5109 Next (Elmt);
5110 end loop;
5111
5112 -- If we get here, then none of the inputs or outputs reference a
5113 -- state with visible refinement.
5114
5115 return False;
5116
5117 -- Single item
5118
5119 else
5120 Item_Id := Entity_Of (Item);
5121
5122 return
5123 Present (Item_Id)
5124 and then Ekind (Item_Id) = E_Abstract_State
5125 and then Has_Visible_Refinement (Item_Id);
5126 end if;
5127 end Is_Refined_State;
5128
5129 -- Local variables
5130
5131 Arg : constant Node_Id :=
5132 Get_Pragma_Arg (First (Pragma_Argument_Associations (Prag)));
5133 Nam : constant Name_Id := Pragma_Name (Prag);
5134
5135 -- Start of processing for Contains_Refined_State
5136
5137 begin
5138 if Nam = Name_Depends then
5139 return Has_State_In_Dependency (Arg);
5140
5141 else pragma Assert (Nam = Name_Global);
5142 return Has_State_In_Global (Arg);
5143 end if;
5144 end Contains_Refined_State;
5145
5146 -------------------------
5147 -- Copy_Component_List --
5148 -------------------------
5149
5150 function Copy_Component_List
5151 (R_Typ : Entity_Id;
5152 Loc : Source_Ptr) return List_Id
5153 is
5154 Comp : Node_Id;
5155 Comps : constant List_Id := New_List;
5156
5157 begin
5158 Comp := First_Component (Underlying_Type (R_Typ));
5159 while Present (Comp) loop
5160 if Comes_From_Source (Comp) then
5161 declare
5162 Comp_Decl : constant Node_Id := Declaration_Node (Comp);
5163 begin
5164 Append_To (Comps,
5165 Make_Component_Declaration (Loc,
5166 Defining_Identifier =>
5167 Make_Defining_Identifier (Loc, Chars (Comp)),
5168 Component_Definition =>
5169 New_Copy_Tree
5170 (Component_Definition (Comp_Decl), New_Sloc => Loc)));
5171 end;
5172 end if;
5173
5174 Next_Component (Comp);
5175 end loop;
5176
5177 return Comps;
5178 end Copy_Component_List;
5179
5180 -------------------------
5181 -- Copy_Parameter_List --
5182 -------------------------
5183
5184 function Copy_Parameter_List (Subp_Id : Entity_Id) return List_Id is
5185 Loc : constant Source_Ptr := Sloc (Subp_Id);
5186 Plist : List_Id;
5187 Formal : Entity_Id;
5188
5189 begin
5190 if No (First_Formal (Subp_Id)) then
5191 return No_List;
5192 else
5193 Plist := New_List;
5194 Formal := First_Formal (Subp_Id);
5195 while Present (Formal) loop
5196 Append_To (Plist,
5197 Make_Parameter_Specification (Loc,
5198 Defining_Identifier =>
5199 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)),
5200 In_Present => In_Present (Parent (Formal)),
5201 Out_Present => Out_Present (Parent (Formal)),
5202 Parameter_Type =>
5203 New_Occurrence_Of (Etype (Formal), Loc),
5204 Expression =>
5205 New_Copy_Tree (Expression (Parent (Formal)))));
5206
5207 Next_Formal (Formal);
5208 end loop;
5209 end if;
5210
5211 return Plist;
5212 end Copy_Parameter_List;
5213
5214 ----------------------------
5215 -- Copy_SPARK_Mode_Aspect --
5216 ----------------------------
5217
5218 procedure Copy_SPARK_Mode_Aspect (From : Node_Id; To : Node_Id) is
5219 pragma Assert (not Has_Aspects (To));
5220 Asp : Node_Id;
5221
5222 begin
5223 if Has_Aspects (From) then
5224 Asp := Find_Aspect (Defining_Entity (From), Aspect_SPARK_Mode);
5225
5226 if Present (Asp) then
5227 Set_Aspect_Specifications (To, New_List (New_Copy_Tree (Asp)));
5228 Set_Has_Aspects (To, True);
5229 end if;
5230 end if;
5231 end Copy_SPARK_Mode_Aspect;
5232
5233 --------------------------
5234 -- Copy_Subprogram_Spec --
5235 --------------------------
5236
5237 function Copy_Subprogram_Spec (Spec : Node_Id) return Node_Id is
5238 Def_Id : Node_Id;
5239 Formal_Spec : Node_Id;
5240 Result : Node_Id;
5241
5242 begin
5243 -- The structure of the original tree must be replicated without any
5244 -- alterations. Use New_Copy_Tree for this purpose.
5245
5246 Result := New_Copy_Tree (Spec);
5247
5248 -- Create a new entity for the defining unit name
5249
5250 Def_Id := Defining_Unit_Name (Result);
5251 Set_Defining_Unit_Name (Result,
5252 Make_Defining_Identifier (Sloc (Def_Id), Chars (Def_Id)));
5253
5254 -- Create new entities for the formal parameters
5255
5256 if Present (Parameter_Specifications (Result)) then
5257 Formal_Spec := First (Parameter_Specifications (Result));
5258 while Present (Formal_Spec) loop
5259 Def_Id := Defining_Identifier (Formal_Spec);
5260 Set_Defining_Identifier (Formal_Spec,
5261 Make_Defining_Identifier (Sloc (Def_Id), Chars (Def_Id)));
5262
5263 Next (Formal_Spec);
5264 end loop;
5265 end if;
5266
5267 return Result;
5268 end Copy_Subprogram_Spec;
5269
5270 --------------------------------
5271 -- Corresponding_Generic_Type --
5272 --------------------------------
5273
5274 function Corresponding_Generic_Type (T : Entity_Id) return Entity_Id is
5275 Inst : Entity_Id;
5276 Gen : Entity_Id;
5277 Typ : Entity_Id;
5278
5279 begin
5280 if not Is_Generic_Actual_Type (T) then
5281 return Any_Type;
5282
5283 -- If the actual is the actual of an enclosing instance, resolution
5284 -- was correct in the generic.
5285
5286 elsif Nkind (Parent (T)) = N_Subtype_Declaration
5287 and then Is_Entity_Name (Subtype_Indication (Parent (T)))
5288 and then
5289 Is_Generic_Actual_Type (Entity (Subtype_Indication (Parent (T))))
5290 then
5291 return Any_Type;
5292
5293 else
5294 Inst := Scope (T);
5295
5296 if Is_Wrapper_Package (Inst) then
5297 Inst := Related_Instance (Inst);
5298 end if;
5299
5300 Gen :=
5301 Generic_Parent
5302 (Specification (Unit_Declaration_Node (Inst)));
5303
5304 -- Generic actual has the same name as the corresponding formal
5305
5306 Typ := First_Entity (Gen);
5307 while Present (Typ) loop
5308 if Chars (Typ) = Chars (T) then
5309 return Typ;
5310 end if;
5311
5312 Next_Entity (Typ);
5313 end loop;
5314
5315 return Any_Type;
5316 end if;
5317 end Corresponding_Generic_Type;
5318
5319 --------------------
5320 -- Current_Entity --
5321 --------------------
5322
5323 -- The currently visible definition for a given identifier is the
5324 -- one most chained at the start of the visibility chain, i.e. the
5325 -- one that is referenced by the Node_Id value of the name of the
5326 -- given identifier.
5327
5328 function Current_Entity (N : Node_Id) return Entity_Id is
5329 begin
5330 return Get_Name_Entity_Id (Chars (N));
5331 end Current_Entity;
5332
5333 -----------------------------
5334 -- Current_Entity_In_Scope --
5335 -----------------------------
5336
5337 function Current_Entity_In_Scope (N : Node_Id) return Entity_Id is
5338 E : Entity_Id;
5339 CS : constant Entity_Id := Current_Scope;
5340
5341 Transient_Case : constant Boolean := Scope_Is_Transient;
5342
5343 begin
5344 E := Get_Name_Entity_Id (Chars (N));
5345 while Present (E)
5346 and then Scope (E) /= CS
5347 and then (not Transient_Case or else Scope (E) /= Scope (CS))
5348 loop
5349 E := Homonym (E);
5350 end loop;
5351
5352 return E;
5353 end Current_Entity_In_Scope;
5354
5355 -------------------
5356 -- Current_Scope --
5357 -------------------
5358
5359 function Current_Scope return Entity_Id is
5360 begin
5361 if Scope_Stack.Last = -1 then
5362 return Standard_Standard;
5363 else
5364 declare
5365 C : constant Entity_Id :=
5366 Scope_Stack.Table (Scope_Stack.Last).Entity;
5367 begin
5368 if Present (C) then
5369 return C;
5370 else
5371 return Standard_Standard;
5372 end if;
5373 end;
5374 end if;
5375 end Current_Scope;
5376
5377 ----------------------------
5378 -- Current_Scope_No_Loops --
5379 ----------------------------
5380
5381 function Current_Scope_No_Loops return Entity_Id is
5382 S : Entity_Id;
5383
5384 begin
5385 -- Examine the scope stack starting from the current scope and skip any
5386 -- internally generated loops.
5387
5388 S := Current_Scope;
5389 while Present (S) and then S /= Standard_Standard loop
5390 if Ekind (S) = E_Loop and then not Comes_From_Source (S) then
5391 S := Scope (S);
5392 else
5393 exit;
5394 end if;
5395 end loop;
5396
5397 return S;
5398 end Current_Scope_No_Loops;
5399
5400 ------------------------
5401 -- Current_Subprogram --
5402 ------------------------
5403
5404 function Current_Subprogram return Entity_Id is
5405 Scop : constant Entity_Id := Current_Scope;
5406 begin
5407 if Is_Subprogram_Or_Generic_Subprogram (Scop) then
5408 return Scop;
5409 else
5410 return Enclosing_Subprogram (Scop);
5411 end if;
5412 end Current_Subprogram;
5413
5414 ----------------------------------
5415 -- Deepest_Type_Access_Level --
5416 ----------------------------------
5417
5418 function Deepest_Type_Access_Level (Typ : Entity_Id) return Uint is
5419 begin
5420 if Ekind (Typ) = E_Anonymous_Access_Type
5421 and then not Is_Local_Anonymous_Access (Typ)
5422 and then Nkind (Associated_Node_For_Itype (Typ)) = N_Object_Declaration
5423 then
5424 -- Typ is the type of an Ada 2012 stand-alone object of an anonymous
5425 -- access type.
5426
5427 return
5428 Scope_Depth (Enclosing_Dynamic_Scope
5429 (Defining_Identifier
5430 (Associated_Node_For_Itype (Typ))));
5431
5432 -- For generic formal type, return Int'Last (infinite).
5433 -- See comment preceding Is_Generic_Type call in Type_Access_Level.
5434
5435 elsif Is_Generic_Type (Root_Type (Typ)) then
5436 return UI_From_Int (Int'Last);
5437
5438 else
5439 return Type_Access_Level (Typ);
5440 end if;
5441 end Deepest_Type_Access_Level;
5442
5443 ---------------------
5444 -- Defining_Entity --
5445 ---------------------
5446
5447 function Defining_Entity
5448 (N : Node_Id;
5449 Empty_On_Errors : Boolean := False) return Entity_Id
5450 is
5451 Err : Entity_Id := Empty;
5452
5453 begin
5454 case Nkind (N) is
5455 when N_Abstract_Subprogram_Declaration
5456 | N_Expression_Function
5457 | N_Formal_Subprogram_Declaration
5458 | N_Generic_Package_Declaration
5459 | N_Generic_Subprogram_Declaration
5460 | N_Package_Declaration
5461 | N_Subprogram_Body
5462 | N_Subprogram_Body_Stub
5463 | N_Subprogram_Declaration
5464 | N_Subprogram_Renaming_Declaration
5465 =>
5466 return Defining_Entity (Specification (N));
5467
5468 when N_Component_Declaration
5469 | N_Defining_Program_Unit_Name
5470 | N_Discriminant_Specification
5471 | N_Entry_Body
5472 | N_Entry_Declaration
5473 | N_Entry_Index_Specification
5474 | N_Exception_Declaration
5475 | N_Exception_Renaming_Declaration
5476 | N_Formal_Object_Declaration
5477 | N_Formal_Package_Declaration
5478 | N_Formal_Type_Declaration
5479 | N_Full_Type_Declaration
5480 | N_Implicit_Label_Declaration
5481 | N_Incomplete_Type_Declaration
5482 | N_Iterator_Specification
5483 | N_Loop_Parameter_Specification
5484 | N_Number_Declaration
5485 | N_Object_Declaration
5486 | N_Object_Renaming_Declaration
5487 | N_Package_Body_Stub
5488 | N_Parameter_Specification
5489 | N_Private_Extension_Declaration
5490 | N_Private_Type_Declaration
5491 | N_Protected_Body
5492 | N_Protected_Body_Stub
5493 | N_Protected_Type_Declaration
5494 | N_Single_Protected_Declaration
5495 | N_Single_Task_Declaration
5496 | N_Subtype_Declaration
5497 | N_Task_Body
5498 | N_Task_Body_Stub
5499 | N_Task_Type_Declaration
5500 =>
5501 return Defining_Identifier (N);
5502
5503 when N_Subunit =>
5504 return Defining_Entity (Proper_Body (N));
5505
5506 when N_Function_Instantiation
5507 | N_Function_Specification
5508 | N_Generic_Function_Renaming_Declaration
5509 | N_Generic_Package_Renaming_Declaration
5510 | N_Generic_Procedure_Renaming_Declaration
5511 | N_Package_Body
5512 | N_Package_Instantiation
5513 | N_Package_Renaming_Declaration
5514 | N_Package_Specification
5515 | N_Procedure_Instantiation
5516 | N_Procedure_Specification
5517 =>
5518 declare
5519 Nam : constant Node_Id := Defining_Unit_Name (N);
5520
5521 begin
5522 if Nkind (Nam) in N_Entity then
5523 return Nam;
5524
5525 -- For Error, make up a name and attach to declaration so we
5526 -- can continue semantic analysis.
5527
5528 elsif Nam = Error then
5529 if Empty_On_Errors then
5530 return Empty;
5531 else
5532 Err := Make_Temporary (Sloc (N), 'T');
5533 Set_Defining_Unit_Name (N, Err);
5534
5535 return Err;
5536 end if;
5537
5538 -- If not an entity, get defining identifier
5539
5540 else
5541 return Defining_Identifier (Nam);
5542 end if;
5543 end;
5544
5545 when N_Block_Statement
5546 | N_Loop_Statement
5547 =>
5548 return Entity (Identifier (N));
5549
5550 when others =>
5551 if Empty_On_Errors then
5552 return Empty;
5553 else
5554 raise Program_Error;
5555 end if;
5556 end case;
5557 end Defining_Entity;
5558
5559 --------------------------
5560 -- Denotes_Discriminant --
5561 --------------------------
5562
5563 function Denotes_Discriminant
5564 (N : Node_Id;
5565 Check_Concurrent : Boolean := False) return Boolean
5566 is
5567 E : Entity_Id;
5568
5569 begin
5570 if not Is_Entity_Name (N) or else No (Entity (N)) then
5571 return False;
5572 else
5573 E := Entity (N);
5574 end if;
5575
5576 -- If we are checking for a protected type, the discriminant may have
5577 -- been rewritten as the corresponding discriminal of the original type
5578 -- or of the corresponding concurrent record, depending on whether we
5579 -- are in the spec or body of the protected type.
5580
5581 return Ekind (E) = E_Discriminant
5582 or else
5583 (Check_Concurrent
5584 and then Ekind (E) = E_In_Parameter
5585 and then Present (Discriminal_Link (E))
5586 and then
5587 (Is_Concurrent_Type (Scope (Discriminal_Link (E)))
5588 or else
5589 Is_Concurrent_Record_Type (Scope (Discriminal_Link (E)))));
5590 end Denotes_Discriminant;
5591
5592 -------------------------
5593 -- Denotes_Same_Object --
5594 -------------------------
5595
5596 function Denotes_Same_Object (A1, A2 : Node_Id) return Boolean is
5597 Obj1 : Node_Id := A1;
5598 Obj2 : Node_Id := A2;
5599
5600 function Has_Prefix (N : Node_Id) return Boolean;
5601 -- Return True if N has attribute Prefix
5602
5603 function Is_Renaming (N : Node_Id) return Boolean;
5604 -- Return true if N names a renaming entity
5605
5606 function Is_Valid_Renaming (N : Node_Id) return Boolean;
5607 -- For renamings, return False if the prefix of any dereference within
5608 -- the renamed object_name is a variable, or any expression within the
5609 -- renamed object_name contains references to variables or calls on
5610 -- nonstatic functions; otherwise return True (RM 6.4.1(6.10/3))
5611
5612 ----------------
5613 -- Has_Prefix --
5614 ----------------
5615
5616 function Has_Prefix (N : Node_Id) return Boolean is
5617 begin
5618 return
5619 Nkind_In (N,
5620 N_Attribute_Reference,
5621 N_Expanded_Name,
5622 N_Explicit_Dereference,
5623 N_Indexed_Component,
5624 N_Reference,
5625 N_Selected_Component,
5626 N_Slice);
5627 end Has_Prefix;
5628
5629 -----------------
5630 -- Is_Renaming --
5631 -----------------
5632
5633 function Is_Renaming (N : Node_Id) return Boolean is
5634 begin
5635 return Is_Entity_Name (N)
5636 and then Present (Renamed_Entity (Entity (N)));
5637 end Is_Renaming;
5638
5639 -----------------------
5640 -- Is_Valid_Renaming --
5641 -----------------------
5642
5643 function Is_Valid_Renaming (N : Node_Id) return Boolean is
5644
5645 function Check_Renaming (N : Node_Id) return Boolean;
5646 -- Recursive function used to traverse all the prefixes of N
5647
5648 function Check_Renaming (N : Node_Id) return Boolean is
5649 begin
5650 if Is_Renaming (N)
5651 and then not Check_Renaming (Renamed_Entity (Entity (N)))
5652 then
5653 return False;
5654 end if;
5655
5656 if Nkind (N) = N_Indexed_Component then
5657 declare
5658 Indx : Node_Id;
5659
5660 begin
5661 Indx := First (Expressions (N));
5662 while Present (Indx) loop
5663 if not Is_OK_Static_Expression (Indx) then
5664 return False;
5665 end if;
5666
5667 Next_Index (Indx);
5668 end loop;
5669 end;
5670 end if;
5671
5672 if Has_Prefix (N) then
5673 declare
5674 P : constant Node_Id := Prefix (N);
5675
5676 begin
5677 if Nkind (N) = N_Explicit_Dereference
5678 and then Is_Variable (P)
5679 then
5680 return False;
5681
5682 elsif Is_Entity_Name (P)
5683 and then Ekind (Entity (P)) = E_Function
5684 then
5685 return False;
5686
5687 elsif Nkind (P) = N_Function_Call then
5688 return False;
5689 end if;
5690
5691 -- Recursion to continue traversing the prefix of the
5692 -- renaming expression
5693
5694 return Check_Renaming (P);
5695 end;
5696 end if;
5697
5698 return True;
5699 end Check_Renaming;
5700
5701 -- Start of processing for Is_Valid_Renaming
5702
5703 begin
5704 return Check_Renaming (N);
5705 end Is_Valid_Renaming;
5706
5707 -- Start of processing for Denotes_Same_Object
5708
5709 begin
5710 -- Both names statically denote the same stand-alone object or parameter
5711 -- (RM 6.4.1(6.5/3))
5712
5713 if Is_Entity_Name (Obj1)
5714 and then Is_Entity_Name (Obj2)
5715 and then Entity (Obj1) = Entity (Obj2)
5716 then
5717 return True;
5718 end if;
5719
5720 -- For renamings, the prefix of any dereference within the renamed
5721 -- object_name is not a variable, and any expression within the
5722 -- renamed object_name contains no references to variables nor
5723 -- calls on nonstatic functions (RM 6.4.1(6.10/3)).
5724
5725 if Is_Renaming (Obj1) then
5726 if Is_Valid_Renaming (Obj1) then
5727 Obj1 := Renamed_Entity (Entity (Obj1));
5728 else
5729 return False;
5730 end if;
5731 end if;
5732
5733 if Is_Renaming (Obj2) then
5734 if Is_Valid_Renaming (Obj2) then
5735 Obj2 := Renamed_Entity (Entity (Obj2));
5736 else
5737 return False;
5738 end if;
5739 end if;
5740
5741 -- No match if not same node kind (such cases are handled by
5742 -- Denotes_Same_Prefix)
5743
5744 if Nkind (Obj1) /= Nkind (Obj2) then
5745 return False;
5746
5747 -- After handling valid renamings, one of the two names statically
5748 -- denoted a renaming declaration whose renamed object_name is known
5749 -- to denote the same object as the other (RM 6.4.1(6.10/3))
5750
5751 elsif Is_Entity_Name (Obj1) then
5752 if Is_Entity_Name (Obj2) then
5753 return Entity (Obj1) = Entity (Obj2);
5754 else
5755 return False;
5756 end if;
5757
5758 -- Both names are selected_components, their prefixes are known to
5759 -- denote the same object, and their selector_names denote the same
5760 -- component (RM 6.4.1(6.6/3)).
5761
5762 elsif Nkind (Obj1) = N_Selected_Component then
5763 return Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2))
5764 and then
5765 Entity (Selector_Name (Obj1)) = Entity (Selector_Name (Obj2));
5766
5767 -- Both names are dereferences and the dereferenced names are known to
5768 -- denote the same object (RM 6.4.1(6.7/3))
5769
5770 elsif Nkind (Obj1) = N_Explicit_Dereference then
5771 return Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2));
5772
5773 -- Both names are indexed_components, their prefixes are known to denote
5774 -- the same object, and each of the pairs of corresponding index values
5775 -- are either both static expressions with the same static value or both
5776 -- names that are known to denote the same object (RM 6.4.1(6.8/3))
5777
5778 elsif Nkind (Obj1) = N_Indexed_Component then
5779 if not Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2)) then
5780 return False;
5781 else
5782 declare
5783 Indx1 : Node_Id;
5784 Indx2 : Node_Id;
5785
5786 begin
5787 Indx1 := First (Expressions (Obj1));
5788 Indx2 := First (Expressions (Obj2));
5789 while Present (Indx1) loop
5790
5791 -- Indexes must denote the same static value or same object
5792
5793 if Is_OK_Static_Expression (Indx1) then
5794 if not Is_OK_Static_Expression (Indx2) then
5795 return False;
5796
5797 elsif Expr_Value (Indx1) /= Expr_Value (Indx2) then
5798 return False;
5799 end if;
5800
5801 elsif not Denotes_Same_Object (Indx1, Indx2) then
5802 return False;
5803 end if;
5804
5805 Next (Indx1);
5806 Next (Indx2);
5807 end loop;
5808
5809 return True;
5810 end;
5811 end if;
5812
5813 -- Both names are slices, their prefixes are known to denote the same
5814 -- object, and the two slices have statically matching index constraints
5815 -- (RM 6.4.1(6.9/3))
5816
5817 elsif Nkind (Obj1) = N_Slice
5818 and then Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2))
5819 then
5820 declare
5821 Lo1, Lo2, Hi1, Hi2 : Node_Id;
5822
5823 begin
5824 Get_Index_Bounds (Etype (Obj1), Lo1, Hi1);
5825 Get_Index_Bounds (Etype (Obj2), Lo2, Hi2);
5826
5827 -- Check whether bounds are statically identical. There is no
5828 -- attempt to detect partial overlap of slices.
5829
5830 return Denotes_Same_Object (Lo1, Lo2)
5831 and then
5832 Denotes_Same_Object (Hi1, Hi2);
5833 end;
5834
5835 -- In the recursion, literals appear as indexes
5836
5837 elsif Nkind (Obj1) = N_Integer_Literal
5838 and then
5839 Nkind (Obj2) = N_Integer_Literal
5840 then
5841 return Intval (Obj1) = Intval (Obj2);
5842
5843 else
5844 return False;
5845 end if;
5846 end Denotes_Same_Object;
5847
5848 -------------------------
5849 -- Denotes_Same_Prefix --
5850 -------------------------
5851
5852 function Denotes_Same_Prefix (A1, A2 : Node_Id) return Boolean is
5853 begin
5854 if Is_Entity_Name (A1) then
5855 if Nkind_In (A2, N_Selected_Component, N_Indexed_Component)
5856 and then not Is_Access_Type (Etype (A1))
5857 then
5858 return Denotes_Same_Object (A1, Prefix (A2))
5859 or else Denotes_Same_Prefix (A1, Prefix (A2));
5860 else
5861 return False;
5862 end if;
5863
5864 elsif Is_Entity_Name (A2) then
5865 return Denotes_Same_Prefix (A1 => A2, A2 => A1);
5866
5867 elsif Nkind_In (A1, N_Selected_Component, N_Indexed_Component, N_Slice)
5868 and then
5869 Nkind_In (A2, N_Selected_Component, N_Indexed_Component, N_Slice)
5870 then
5871 declare
5872 Root1, Root2 : Node_Id;
5873 Depth1, Depth2 : Nat := 0;
5874
5875 begin
5876 Root1 := Prefix (A1);
5877 while not Is_Entity_Name (Root1) loop
5878 if not Nkind_In
5879 (Root1, N_Selected_Component, N_Indexed_Component)
5880 then
5881 return False;
5882 else
5883 Root1 := Prefix (Root1);
5884 end if;
5885
5886 Depth1 := Depth1 + 1;
5887 end loop;
5888
5889 Root2 := Prefix (A2);
5890 while not Is_Entity_Name (Root2) loop
5891 if not Nkind_In (Root2, N_Selected_Component,
5892 N_Indexed_Component)
5893 then
5894 return False;
5895 else
5896 Root2 := Prefix (Root2);
5897 end if;
5898
5899 Depth2 := Depth2 + 1;
5900 end loop;
5901
5902 -- If both have the same depth and they do not denote the same
5903 -- object, they are disjoint and no warning is needed.
5904
5905 if Depth1 = Depth2 then
5906 return False;
5907
5908 elsif Depth1 > Depth2 then
5909 Root1 := Prefix (A1);
5910 for J in 1 .. Depth1 - Depth2 - 1 loop
5911 Root1 := Prefix (Root1);
5912 end loop;
5913
5914 return Denotes_Same_Object (Root1, A2);
5915
5916 else
5917 Root2 := Prefix (A2);
5918 for J in 1 .. Depth2 - Depth1 - 1 loop
5919 Root2 := Prefix (Root2);
5920 end loop;
5921
5922 return Denotes_Same_Object (A1, Root2);
5923 end if;
5924 end;
5925
5926 else
5927 return False;
5928 end if;
5929 end Denotes_Same_Prefix;
5930
5931 ----------------------
5932 -- Denotes_Variable --
5933 ----------------------
5934
5935 function Denotes_Variable (N : Node_Id) return Boolean is
5936 begin
5937 return Is_Variable (N) and then Paren_Count (N) = 0;
5938 end Denotes_Variable;
5939
5940 -----------------------------
5941 -- Depends_On_Discriminant --
5942 -----------------------------
5943
5944 function Depends_On_Discriminant (N : Node_Id) return Boolean is
5945 L : Node_Id;
5946 H : Node_Id;
5947
5948 begin
5949 Get_Index_Bounds (N, L, H);
5950 return Denotes_Discriminant (L) or else Denotes_Discriminant (H);
5951 end Depends_On_Discriminant;
5952
5953 -------------------------
5954 -- Designate_Same_Unit --
5955 -------------------------
5956
5957 function Designate_Same_Unit
5958 (Name1 : Node_Id;
5959 Name2 : Node_Id) return Boolean
5960 is
5961 K1 : constant Node_Kind := Nkind (Name1);
5962 K2 : constant Node_Kind := Nkind (Name2);
5963
5964 function Prefix_Node (N : Node_Id) return Node_Id;
5965 -- Returns the parent unit name node of a defining program unit name
5966 -- or the prefix if N is a selected component or an expanded name.
5967
5968 function Select_Node (N : Node_Id) return Node_Id;
5969 -- Returns the defining identifier node of a defining program unit
5970 -- name or the selector node if N is a selected component or an
5971 -- expanded name.
5972
5973 -----------------
5974 -- Prefix_Node --
5975 -----------------
5976
5977 function Prefix_Node (N : Node_Id) return Node_Id is
5978 begin
5979 if Nkind (N) = N_Defining_Program_Unit_Name then
5980 return Name (N);
5981 else
5982 return Prefix (N);
5983 end if;
5984 end Prefix_Node;
5985
5986 -----------------
5987 -- Select_Node --
5988 -----------------
5989
5990 function Select_Node (N : Node_Id) return Node_Id is
5991 begin
5992 if Nkind (N) = N_Defining_Program_Unit_Name then
5993 return Defining_Identifier (N);
5994 else
5995 return Selector_Name (N);
5996 end if;
5997 end Select_Node;
5998
5999 -- Start of processing for Designate_Same_Unit
6000
6001 begin
6002 if Nkind_In (K1, N_Identifier, N_Defining_Identifier)
6003 and then
6004 Nkind_In (K2, N_Identifier, N_Defining_Identifier)
6005 then
6006 return Chars (Name1) = Chars (Name2);
6007
6008 elsif Nkind_In (K1, N_Expanded_Name,
6009 N_Selected_Component,
6010 N_Defining_Program_Unit_Name)
6011 and then
6012 Nkind_In (K2, N_Expanded_Name,
6013 N_Selected_Component,
6014 N_Defining_Program_Unit_Name)
6015 then
6016 return
6017 (Chars (Select_Node (Name1)) = Chars (Select_Node (Name2)))
6018 and then
6019 Designate_Same_Unit (Prefix_Node (Name1), Prefix_Node (Name2));
6020
6021 else
6022 return False;
6023 end if;
6024 end Designate_Same_Unit;
6025
6026 ---------------------------------------------
6027 -- Diagnose_Iterated_Component_Association --
6028 ---------------------------------------------
6029
6030 procedure Diagnose_Iterated_Component_Association (N : Node_Id) is
6031 Def_Id : constant Entity_Id := Defining_Identifier (N);
6032 Aggr : Node_Id;
6033
6034 begin
6035 -- Determine whether the iterated component association appears within
6036 -- an aggregate. If this is the case, raise Program_Error because the
6037 -- iterated component association cannot be left in the tree as is and
6038 -- must always be processed by the related aggregate.
6039
6040 Aggr := N;
6041 while Present (Aggr) loop
6042 if Nkind (Aggr) = N_Aggregate then
6043 raise Program_Error;
6044
6045 -- Prevent the search from going too far
6046
6047 elsif Is_Body_Or_Package_Declaration (Aggr) then
6048 exit;
6049 end if;
6050
6051 Aggr := Parent (Aggr);
6052 end loop;
6053
6054 -- At this point it is known that the iterated component association is
6055 -- not within an aggregate. This is really a quantified expression with
6056 -- a missing "all" or "some" quantifier.
6057
6058 Error_Msg_N ("missing quantifier", Def_Id);
6059
6060 -- Rewrite the iterated component association as True to prevent any
6061 -- cascaded errors.
6062
6063 Rewrite (N, New_Occurrence_Of (Standard_True, Sloc (N)));
6064 Analyze (N);
6065 end Diagnose_Iterated_Component_Association;
6066
6067 ---------------------------------
6068 -- Dynamic_Accessibility_Level --
6069 ---------------------------------
6070
6071 function Dynamic_Accessibility_Level (Expr : Node_Id) return Node_Id is
6072 Loc : constant Source_Ptr := Sloc (Expr);
6073
6074 function Make_Level_Literal (Level : Uint) return Node_Id;
6075 -- Construct an integer literal representing an accessibility level
6076 -- with its type set to Natural.
6077
6078 ------------------------
6079 -- Make_Level_Literal --
6080 ------------------------
6081
6082 function Make_Level_Literal (Level : Uint) return Node_Id is
6083 Result : constant Node_Id := Make_Integer_Literal (Loc, Level);
6084
6085 begin
6086 Set_Etype (Result, Standard_Natural);
6087 return Result;
6088 end Make_Level_Literal;
6089
6090 -- Local variables
6091
6092 E : Entity_Id;
6093
6094 -- Start of processing for Dynamic_Accessibility_Level
6095
6096 begin
6097 if Is_Entity_Name (Expr) then
6098 E := Entity (Expr);
6099
6100 if Present (Renamed_Object (E)) then
6101 return Dynamic_Accessibility_Level (Renamed_Object (E));
6102 end if;
6103
6104 if Is_Formal (E) or else Ekind_In (E, E_Variable, E_Constant) then
6105 if Present (Extra_Accessibility (E)) then
6106 return New_Occurrence_Of (Extra_Accessibility (E), Loc);
6107 end if;
6108 end if;
6109 end if;
6110
6111 -- Unimplemented: Ptr.all'Access, where Ptr has Extra_Accessibility ???
6112
6113 case Nkind (Expr) is
6114
6115 -- For access discriminant, the level of the enclosing object
6116
6117 when N_Selected_Component =>
6118 if Ekind (Entity (Selector_Name (Expr))) = E_Discriminant
6119 and then Ekind (Etype (Entity (Selector_Name (Expr)))) =
6120 E_Anonymous_Access_Type
6121 then
6122 return Make_Level_Literal (Object_Access_Level (Expr));
6123 end if;
6124
6125 when N_Attribute_Reference =>
6126 case Get_Attribute_Id (Attribute_Name (Expr)) is
6127
6128 -- For X'Access, the level of the prefix X
6129
6130 when Attribute_Access =>
6131 return Make_Level_Literal
6132 (Object_Access_Level (Prefix (Expr)));
6133
6134 -- Treat the unchecked attributes as library-level
6135
6136 when Attribute_Unchecked_Access
6137 | Attribute_Unrestricted_Access
6138 =>
6139 return Make_Level_Literal (Scope_Depth (Standard_Standard));
6140
6141 -- No other access-valued attributes
6142
6143 when others =>
6144 raise Program_Error;
6145 end case;
6146
6147 when N_Allocator =>
6148
6149 -- Unimplemented: depends on context. As an actual parameter where
6150 -- formal type is anonymous, use
6151 -- Scope_Depth (Current_Scope) + 1.
6152 -- For other cases, see 3.10.2(14/3) and following. ???
6153
6154 null;
6155
6156 when N_Type_Conversion =>
6157 if not Is_Local_Anonymous_Access (Etype (Expr)) then
6158
6159 -- Handle type conversions introduced for a rename of an
6160 -- Ada 2012 stand-alone object of an anonymous access type.
6161
6162 return Dynamic_Accessibility_Level (Expression (Expr));
6163 end if;
6164
6165 when others =>
6166 null;
6167 end case;
6168
6169 return Make_Level_Literal (Type_Access_Level (Etype (Expr)));
6170 end Dynamic_Accessibility_Level;
6171
6172 -----------------------------------
6173 -- Effective_Extra_Accessibility --
6174 -----------------------------------
6175
6176 function Effective_Extra_Accessibility (Id : Entity_Id) return Entity_Id is
6177 begin
6178 if Present (Renamed_Object (Id))
6179 and then Is_Entity_Name (Renamed_Object (Id))
6180 then
6181 return Effective_Extra_Accessibility (Entity (Renamed_Object (Id)));
6182 else
6183 return Extra_Accessibility (Id);
6184 end if;
6185 end Effective_Extra_Accessibility;
6186
6187 -----------------------------
6188 -- Effective_Reads_Enabled --
6189 -----------------------------
6190
6191 function Effective_Reads_Enabled (Id : Entity_Id) return Boolean is
6192 begin
6193 return Has_Enabled_Property (Id, Name_Effective_Reads);
6194 end Effective_Reads_Enabled;
6195
6196 ------------------------------
6197 -- Effective_Writes_Enabled --
6198 ------------------------------
6199
6200 function Effective_Writes_Enabled (Id : Entity_Id) return Boolean is
6201 begin
6202 return Has_Enabled_Property (Id, Name_Effective_Writes);
6203 end Effective_Writes_Enabled;
6204
6205 ------------------------------
6206 -- Enclosing_Comp_Unit_Node --
6207 ------------------------------
6208
6209 function Enclosing_Comp_Unit_Node (N : Node_Id) return Node_Id is
6210 Current_Node : Node_Id;
6211
6212 begin
6213 Current_Node := N;
6214 while Present (Current_Node)
6215 and then Nkind (Current_Node) /= N_Compilation_Unit
6216 loop
6217 Current_Node := Parent (Current_Node);
6218 end loop;
6219
6220 if Nkind (Current_Node) /= N_Compilation_Unit then
6221 return Empty;
6222 else
6223 return Current_Node;
6224 end if;
6225 end Enclosing_Comp_Unit_Node;
6226
6227 --------------------------
6228 -- Enclosing_CPP_Parent --
6229 --------------------------
6230
6231 function Enclosing_CPP_Parent (Typ : Entity_Id) return Entity_Id is
6232 Parent_Typ : Entity_Id := Typ;
6233
6234 begin
6235 while not Is_CPP_Class (Parent_Typ)
6236 and then Etype (Parent_Typ) /= Parent_Typ
6237 loop
6238 Parent_Typ := Etype (Parent_Typ);
6239
6240 if Is_Private_Type (Parent_Typ) then
6241 Parent_Typ := Full_View (Base_Type (Parent_Typ));
6242 end if;
6243 end loop;
6244
6245 pragma Assert (Is_CPP_Class (Parent_Typ));
6246 return Parent_Typ;
6247 end Enclosing_CPP_Parent;
6248
6249 ---------------------------
6250 -- Enclosing_Declaration --
6251 ---------------------------
6252
6253 function Enclosing_Declaration (N : Node_Id) return Node_Id is
6254 Decl : Node_Id := N;
6255
6256 begin
6257 while Present (Decl)
6258 and then not (Nkind (Decl) in N_Declaration
6259 or else
6260 Nkind (Decl) in N_Later_Decl_Item)
6261 loop
6262 Decl := Parent (Decl);
6263 end loop;
6264
6265 return Decl;
6266 end Enclosing_Declaration;
6267
6268 ----------------------------
6269 -- Enclosing_Generic_Body --
6270 ----------------------------
6271
6272 function Enclosing_Generic_Body
6273 (N : Node_Id) return Node_Id
6274 is
6275 P : Node_Id;
6276 Decl : Node_Id;
6277 Spec : Node_Id;
6278
6279 begin
6280 P := Parent (N);
6281 while Present (P) loop
6282 if Nkind (P) = N_Package_Body
6283 or else Nkind (P) = N_Subprogram_Body
6284 then
6285 Spec := Corresponding_Spec (P);
6286
6287 if Present (Spec) then
6288 Decl := Unit_Declaration_Node (Spec);
6289
6290 if Nkind (Decl) = N_Generic_Package_Declaration
6291 or else Nkind (Decl) = N_Generic_Subprogram_Declaration
6292 then
6293 return P;
6294 end if;
6295 end if;
6296 end if;
6297
6298 P := Parent (P);
6299 end loop;
6300
6301 return Empty;
6302 end Enclosing_Generic_Body;
6303
6304 ----------------------------
6305 -- Enclosing_Generic_Unit --
6306 ----------------------------
6307
6308 function Enclosing_Generic_Unit
6309 (N : Node_Id) return Node_Id
6310 is
6311 P : Node_Id;
6312 Decl : Node_Id;
6313 Spec : Node_Id;
6314
6315 begin
6316 P := Parent (N);
6317 while Present (P) loop
6318 if Nkind (P) = N_Generic_Package_Declaration
6319 or else Nkind (P) = N_Generic_Subprogram_Declaration
6320 then
6321 return P;
6322
6323 elsif Nkind (P) = N_Package_Body
6324 or else Nkind (P) = N_Subprogram_Body
6325 then
6326 Spec := Corresponding_Spec (P);
6327
6328 if Present (Spec) then
6329 Decl := Unit_Declaration_Node (Spec);
6330
6331 if Nkind (Decl) = N_Generic_Package_Declaration
6332 or else Nkind (Decl) = N_Generic_Subprogram_Declaration
6333 then
6334 return Decl;
6335 end if;
6336 end if;
6337 end if;
6338
6339 P := Parent (P);
6340 end loop;
6341
6342 return Empty;
6343 end Enclosing_Generic_Unit;
6344
6345 -------------------------------
6346 -- Enclosing_Lib_Unit_Entity --
6347 -------------------------------
6348
6349 function Enclosing_Lib_Unit_Entity
6350 (E : Entity_Id := Current_Scope) return Entity_Id
6351 is
6352 Unit_Entity : Entity_Id;
6353
6354 begin
6355 -- Look for enclosing library unit entity by following scope links.
6356 -- Equivalent to, but faster than indexing through the scope stack.
6357
6358 Unit_Entity := E;
6359 while (Present (Scope (Unit_Entity))
6360 and then Scope (Unit_Entity) /= Standard_Standard)
6361 and not Is_Child_Unit (Unit_Entity)
6362 loop
6363 Unit_Entity := Scope (Unit_Entity);
6364 end loop;
6365
6366 return Unit_Entity;
6367 end Enclosing_Lib_Unit_Entity;
6368
6369 -----------------------------
6370 -- Enclosing_Lib_Unit_Node --
6371 -----------------------------
6372
6373 function Enclosing_Lib_Unit_Node (N : Node_Id) return Node_Id is
6374 Encl_Unit : Node_Id;
6375
6376 begin
6377 Encl_Unit := Enclosing_Comp_Unit_Node (N);
6378 while Present (Encl_Unit)
6379 and then Nkind (Unit (Encl_Unit)) = N_Subunit
6380 loop
6381 Encl_Unit := Library_Unit (Encl_Unit);
6382 end loop;
6383
6384 pragma Assert (Nkind (Encl_Unit) = N_Compilation_Unit);
6385 return Encl_Unit;
6386 end Enclosing_Lib_Unit_Node;
6387
6388 -----------------------
6389 -- Enclosing_Package --
6390 -----------------------
6391
6392 function Enclosing_Package (E : Entity_Id) return Entity_Id is
6393 Dynamic_Scope : constant Entity_Id := Enclosing_Dynamic_Scope (E);
6394
6395 begin
6396 if Dynamic_Scope = Standard_Standard then
6397 return Standard_Standard;
6398
6399 elsif Dynamic_Scope = Empty then
6400 return Empty;
6401
6402 elsif Ekind_In (Dynamic_Scope, E_Package, E_Package_Body,
6403 E_Generic_Package)
6404 then
6405 return Dynamic_Scope;
6406
6407 else
6408 return Enclosing_Package (Dynamic_Scope);
6409 end if;
6410 end Enclosing_Package;
6411
6412 -------------------------------------
6413 -- Enclosing_Package_Or_Subprogram --
6414 -------------------------------------
6415
6416 function Enclosing_Package_Or_Subprogram (E : Entity_Id) return Entity_Id is
6417 S : Entity_Id;
6418
6419 begin
6420 S := Scope (E);
6421 while Present (S) loop
6422 if Is_Package_Or_Generic_Package (S)
6423 or else Ekind (S) = E_Package_Body
6424 then
6425 return S;
6426
6427 elsif Is_Subprogram_Or_Generic_Subprogram (S)
6428 or else Ekind (S) = E_Subprogram_Body
6429 then
6430 return S;
6431
6432 else
6433 S := Scope (S);
6434 end if;
6435 end loop;
6436
6437 return Empty;
6438 end Enclosing_Package_Or_Subprogram;
6439
6440 --------------------------
6441 -- Enclosing_Subprogram --
6442 --------------------------
6443
6444 function Enclosing_Subprogram (E : Entity_Id) return Entity_Id is
6445 Dynamic_Scope : constant Entity_Id := Enclosing_Dynamic_Scope (E);
6446
6447 begin
6448 if Dynamic_Scope = Standard_Standard then
6449 return Empty;
6450
6451 elsif Dynamic_Scope = Empty then
6452 return Empty;
6453
6454 elsif Ekind (Dynamic_Scope) = E_Subprogram_Body then
6455 return Corresponding_Spec (Parent (Parent (Dynamic_Scope)));
6456
6457 elsif Ekind (Dynamic_Scope) = E_Block
6458 or else Ekind (Dynamic_Scope) = E_Return_Statement
6459 then
6460 return Enclosing_Subprogram (Dynamic_Scope);
6461
6462 elsif Ekind (Dynamic_Scope) = E_Task_Type then
6463 return Get_Task_Body_Procedure (Dynamic_Scope);
6464
6465 elsif Ekind (Dynamic_Scope) = E_Limited_Private_Type
6466 and then Present (Full_View (Dynamic_Scope))
6467 and then Ekind (Full_View (Dynamic_Scope)) = E_Task_Type
6468 then
6469 return Get_Task_Body_Procedure (Full_View (Dynamic_Scope));
6470
6471 -- No body is generated if the protected operation is eliminated
6472
6473 elsif Convention (Dynamic_Scope) = Convention_Protected
6474 and then not Is_Eliminated (Dynamic_Scope)
6475 and then Present (Protected_Body_Subprogram (Dynamic_Scope))
6476 then
6477 return Protected_Body_Subprogram (Dynamic_Scope);
6478
6479 else
6480 return Dynamic_Scope;
6481 end if;
6482 end Enclosing_Subprogram;
6483
6484 ------------------------
6485 -- Ensure_Freeze_Node --
6486 ------------------------
6487
6488 procedure Ensure_Freeze_Node (E : Entity_Id) is
6489 FN : Node_Id;
6490 begin
6491 if No (Freeze_Node (E)) then
6492 FN := Make_Freeze_Entity (Sloc (E));
6493 Set_Has_Delayed_Freeze (E);
6494 Set_Freeze_Node (E, FN);
6495 Set_Access_Types_To_Process (FN, No_Elist);
6496 Set_TSS_Elist (FN, No_Elist);
6497 Set_Entity (FN, E);
6498 end if;
6499 end Ensure_Freeze_Node;
6500
6501 ----------------
6502 -- Enter_Name --
6503 ----------------
6504
6505 procedure Enter_Name (Def_Id : Entity_Id) is
6506 C : constant Entity_Id := Current_Entity (Def_Id);
6507 E : constant Entity_Id := Current_Entity_In_Scope (Def_Id);
6508 S : constant Entity_Id := Current_Scope;
6509
6510 begin
6511 Generate_Definition (Def_Id);
6512
6513 -- Add new name to current scope declarations. Check for duplicate
6514 -- declaration, which may or may not be a genuine error.
6515
6516 if Present (E) then
6517
6518 -- Case of previous entity entered because of a missing declaration
6519 -- or else a bad subtype indication. Best is to use the new entity,
6520 -- and make the previous one invisible.
6521
6522 if Etype (E) = Any_Type then
6523 Set_Is_Immediately_Visible (E, False);
6524
6525 -- Case of renaming declaration constructed for package instances.
6526 -- if there is an explicit declaration with the same identifier,
6527 -- the renaming is not immediately visible any longer, but remains
6528 -- visible through selected component notation.
6529
6530 elsif Nkind (Parent (E)) = N_Package_Renaming_Declaration
6531 and then not Comes_From_Source (E)
6532 then
6533 Set_Is_Immediately_Visible (E, False);
6534
6535 -- The new entity may be the package renaming, which has the same
6536 -- same name as a generic formal which has been seen already.
6537
6538 elsif Nkind (Parent (Def_Id)) = N_Package_Renaming_Declaration
6539 and then not Comes_From_Source (Def_Id)
6540 then
6541 Set_Is_Immediately_Visible (E, False);
6542
6543 -- For a fat pointer corresponding to a remote access to subprogram,
6544 -- we use the same identifier as the RAS type, so that the proper
6545 -- name appears in the stub. This type is only retrieved through
6546 -- the RAS type and never by visibility, and is not added to the
6547 -- visibility list (see below).
6548
6549 elsif Nkind (Parent (Def_Id)) = N_Full_Type_Declaration
6550 and then Ekind (Def_Id) = E_Record_Type
6551 and then Present (Corresponding_Remote_Type (Def_Id))
6552 then
6553 null;
6554
6555 -- Case of an implicit operation or derived literal. The new entity
6556 -- hides the implicit one, which is removed from all visibility,
6557 -- i.e. the entity list of its scope, and homonym chain of its name.
6558
6559 elsif (Is_Overloadable (E) and then Is_Inherited_Operation (E))
6560 or else Is_Internal (E)
6561 then
6562 declare
6563 Decl : constant Node_Id := Parent (E);
6564 Prev : Entity_Id;
6565 Prev_Vis : Entity_Id;
6566
6567 begin
6568 -- If E is an implicit declaration, it cannot be the first
6569 -- entity in the scope.
6570
6571 Prev := First_Entity (Current_Scope);
6572 while Present (Prev) and then Next_Entity (Prev) /= E loop
6573 Next_Entity (Prev);
6574 end loop;
6575
6576 if No (Prev) then
6577
6578 -- If E is not on the entity chain of the current scope,
6579 -- it is an implicit declaration in the generic formal
6580 -- part of a generic subprogram. When analyzing the body,
6581 -- the generic formals are visible but not on the entity
6582 -- chain of the subprogram. The new entity will become
6583 -- the visible one in the body.
6584
6585 pragma Assert
6586 (Nkind (Parent (Decl)) = N_Generic_Subprogram_Declaration);
6587 null;
6588
6589 else
6590 Set_Next_Entity (Prev, Next_Entity (E));
6591
6592 if No (Next_Entity (Prev)) then
6593 Set_Last_Entity (Current_Scope, Prev);
6594 end if;
6595
6596 if E = Current_Entity (E) then
6597 Prev_Vis := Empty;
6598
6599 else
6600 Prev_Vis := Current_Entity (E);
6601 while Homonym (Prev_Vis) /= E loop
6602 Prev_Vis := Homonym (Prev_Vis);
6603 end loop;
6604 end if;
6605
6606 if Present (Prev_Vis) then
6607
6608 -- Skip E in the visibility chain
6609
6610 Set_Homonym (Prev_Vis, Homonym (E));
6611
6612 else
6613 Set_Name_Entity_Id (Chars (E), Homonym (E));
6614 end if;
6615 end if;
6616 end;
6617
6618 -- This section of code could use a comment ???
6619
6620 elsif Present (Etype (E))
6621 and then Is_Concurrent_Type (Etype (E))
6622 and then E = Def_Id
6623 then
6624 return;
6625
6626 -- If the homograph is a protected component renaming, it should not
6627 -- be hiding the current entity. Such renamings are treated as weak
6628 -- declarations.
6629
6630 elsif Is_Prival (E) then
6631 Set_Is_Immediately_Visible (E, False);
6632
6633 -- In this case the current entity is a protected component renaming.
6634 -- Perform minimal decoration by setting the scope and return since
6635 -- the prival should not be hiding other visible entities.
6636
6637 elsif Is_Prival (Def_Id) then
6638 Set_Scope (Def_Id, Current_Scope);
6639 return;
6640
6641 -- Analogous to privals, the discriminal generated for an entry index
6642 -- parameter acts as a weak declaration. Perform minimal decoration
6643 -- to avoid bogus errors.
6644
6645 elsif Is_Discriminal (Def_Id)
6646 and then Ekind (Discriminal_Link (Def_Id)) = E_Entry_Index_Parameter
6647 then
6648 Set_Scope (Def_Id, Current_Scope);
6649 return;
6650
6651 -- In the body or private part of an instance, a type extension may
6652 -- introduce a component with the same name as that of an actual. The
6653 -- legality rule is not enforced, but the semantics of the full type
6654 -- with two components of same name are not clear at this point???
6655
6656 elsif In_Instance_Not_Visible then
6657 null;
6658
6659 -- When compiling a package body, some child units may have become
6660 -- visible. They cannot conflict with local entities that hide them.
6661
6662 elsif Is_Child_Unit (E)
6663 and then In_Open_Scopes (Scope (E))
6664 and then not Is_Immediately_Visible (E)
6665 then
6666 null;
6667
6668 -- Conversely, with front-end inlining we may compile the parent body
6669 -- first, and a child unit subsequently. The context is now the
6670 -- parent spec, and body entities are not visible.
6671
6672 elsif Is_Child_Unit (Def_Id)
6673 and then Is_Package_Body_Entity (E)
6674 and then not In_Package_Body (Current_Scope)
6675 then
6676 null;
6677
6678 -- Case of genuine duplicate declaration
6679
6680 else
6681 Error_Msg_Sloc := Sloc (E);
6682
6683 -- If the previous declaration is an incomplete type declaration
6684 -- this may be an attempt to complete it with a private type. The
6685 -- following avoids confusing cascaded errors.
6686
6687 if Nkind (Parent (E)) = N_Incomplete_Type_Declaration
6688 and then Nkind (Parent (Def_Id)) = N_Private_Type_Declaration
6689 then
6690 Error_Msg_N
6691 ("incomplete type cannot be completed with a private " &
6692 "declaration", Parent (Def_Id));
6693 Set_Is_Immediately_Visible (E, False);
6694 Set_Full_View (E, Def_Id);
6695
6696 -- An inherited component of a record conflicts with a new
6697 -- discriminant. The discriminant is inserted first in the scope,
6698 -- but the error should be posted on it, not on the component.
6699
6700 elsif Ekind (E) = E_Discriminant
6701 and then Present (Scope (Def_Id))
6702 and then Scope (Def_Id) /= Current_Scope
6703 then
6704 Error_Msg_Sloc := Sloc (Def_Id);
6705 Error_Msg_N ("& conflicts with declaration#", E);
6706 return;
6707
6708 -- If the name of the unit appears in its own context clause, a
6709 -- dummy package with the name has already been created, and the
6710 -- error emitted. Try to continue quietly.
6711
6712 elsif Error_Posted (E)
6713 and then Sloc (E) = No_Location
6714 and then Nkind (Parent (E)) = N_Package_Specification
6715 and then Current_Scope = Standard_Standard
6716 then
6717 Set_Scope (Def_Id, Current_Scope);
6718 return;
6719
6720 else
6721 Error_Msg_N ("& conflicts with declaration#", Def_Id);
6722
6723 -- Avoid cascaded messages with duplicate components in
6724 -- derived types.
6725
6726 if Ekind_In (E, E_Component, E_Discriminant) then
6727 return;
6728 end if;
6729 end if;
6730
6731 if Nkind (Parent (Parent (Def_Id))) =
6732 N_Generic_Subprogram_Declaration
6733 and then Def_Id =
6734 Defining_Entity (Specification (Parent (Parent (Def_Id))))
6735 then
6736 Error_Msg_N ("\generic units cannot be overloaded", Def_Id);
6737 end if;
6738
6739 -- If entity is in standard, then we are in trouble, because it
6740 -- means that we have a library package with a duplicated name.
6741 -- That's hard to recover from, so abort.
6742
6743 if S = Standard_Standard then
6744 raise Unrecoverable_Error;
6745
6746 -- Otherwise we continue with the declaration. Having two
6747 -- identical declarations should not cause us too much trouble.
6748
6749 else
6750 null;
6751 end if;
6752 end if;
6753 end if;
6754
6755 -- If we fall through, declaration is OK, at least OK enough to continue
6756
6757 -- If Def_Id is a discriminant or a record component we are in the midst
6758 -- of inheriting components in a derived record definition. Preserve
6759 -- their Ekind and Etype.
6760
6761 if Ekind_In (Def_Id, E_Discriminant, E_Component) then
6762 null;
6763
6764 -- If a type is already set, leave it alone (happens when a type
6765 -- declaration is reanalyzed following a call to the optimizer).
6766
6767 elsif Present (Etype (Def_Id)) then
6768 null;
6769
6770 -- Otherwise, the kind E_Void insures that premature uses of the entity
6771 -- will be detected. Any_Type insures that no cascaded errors will occur
6772
6773 else
6774 Set_Ekind (Def_Id, E_Void);
6775 Set_Etype (Def_Id, Any_Type);
6776 end if;
6777
6778 -- Inherited discriminants and components in derived record types are
6779 -- immediately visible. Itypes are not.
6780
6781 -- Unless the Itype is for a record type with a corresponding remote
6782 -- type (what is that about, it was not commented ???)
6783
6784 if Ekind_In (Def_Id, E_Discriminant, E_Component)
6785 or else
6786 ((not Is_Record_Type (Def_Id)
6787 or else No (Corresponding_Remote_Type (Def_Id)))
6788 and then not Is_Itype (Def_Id))
6789 then
6790 Set_Is_Immediately_Visible (Def_Id);
6791 Set_Current_Entity (Def_Id);
6792 end if;
6793
6794 Set_Homonym (Def_Id, C);
6795 Append_Entity (Def_Id, S);
6796 Set_Public_Status (Def_Id);
6797
6798 -- Declaring a homonym is not allowed in SPARK ...
6799
6800 if Present (C) and then Restriction_Check_Required (SPARK_05) then
6801 declare
6802 Enclosing_Subp : constant Node_Id := Enclosing_Subprogram (Def_Id);
6803 Enclosing_Pack : constant Node_Id := Enclosing_Package (Def_Id);
6804 Other_Scope : constant Node_Id := Enclosing_Dynamic_Scope (C);
6805
6806 begin
6807 -- ... unless the new declaration is in a subprogram, and the
6808 -- visible declaration is a variable declaration or a parameter
6809 -- specification outside that subprogram.
6810
6811 if Present (Enclosing_Subp)
6812 and then Nkind_In (Parent (C), N_Object_Declaration,
6813 N_Parameter_Specification)
6814 and then not Scope_Within_Or_Same (Other_Scope, Enclosing_Subp)
6815 then
6816 null;
6817
6818 -- ... or the new declaration is in a package, and the visible
6819 -- declaration occurs outside that package.
6820
6821 elsif Present (Enclosing_Pack)
6822 and then not Scope_Within_Or_Same (Other_Scope, Enclosing_Pack)
6823 then
6824 null;
6825
6826 -- ... or the new declaration is a component declaration in a
6827 -- record type definition.
6828
6829 elsif Nkind (Parent (Def_Id)) = N_Component_Declaration then
6830 null;
6831
6832 -- Don't issue error for non-source entities
6833
6834 elsif Comes_From_Source (Def_Id)
6835 and then Comes_From_Source (C)
6836 then
6837 Error_Msg_Sloc := Sloc (C);
6838 Check_SPARK_05_Restriction
6839 ("redeclaration of identifier &#", Def_Id);
6840 end if;
6841 end;
6842 end if;
6843
6844 -- Warn if new entity hides an old one
6845
6846 if Warn_On_Hiding and then Present (C)
6847
6848 -- Don't warn for record components since they always have a well
6849 -- defined scope which does not confuse other uses. Note that in
6850 -- some cases, Ekind has not been set yet.
6851
6852 and then Ekind (C) /= E_Component
6853 and then Ekind (C) /= E_Discriminant
6854 and then Nkind (Parent (C)) /= N_Component_Declaration
6855 and then Ekind (Def_Id) /= E_Component
6856 and then Ekind (Def_Id) /= E_Discriminant
6857 and then Nkind (Parent (Def_Id)) /= N_Component_Declaration
6858
6859 -- Don't warn for one character variables. It is too common to use
6860 -- such variables as locals and will just cause too many false hits.
6861
6862 and then Length_Of_Name (Chars (C)) /= 1
6863
6864 -- Don't warn for non-source entities
6865
6866 and then Comes_From_Source (C)
6867 and then Comes_From_Source (Def_Id)
6868
6869 -- Don't warn unless entity in question is in extended main source
6870
6871 and then In_Extended_Main_Source_Unit (Def_Id)
6872
6873 -- Finally, the hidden entity must be either immediately visible or
6874 -- use visible (i.e. from a used package).
6875
6876 and then
6877 (Is_Immediately_Visible (C)
6878 or else
6879 Is_Potentially_Use_Visible (C))
6880 then
6881 Error_Msg_Sloc := Sloc (C);
6882 Error_Msg_N ("declaration hides &#?h?", Def_Id);
6883 end if;
6884 end Enter_Name;
6885
6886 ---------------
6887 -- Entity_Of --
6888 ---------------
6889
6890 function Entity_Of (N : Node_Id) return Entity_Id is
6891 Id : Entity_Id;
6892
6893 begin
6894 Id := Empty;
6895
6896 if Is_Entity_Name (N) then
6897 Id := Entity (N);
6898
6899 -- Follow a possible chain of renamings to reach the root renamed
6900 -- object.
6901
6902 while Present (Id)
6903 and then Is_Object (Id)
6904 and then Present (Renamed_Object (Id))
6905 loop
6906 if Is_Entity_Name (Renamed_Object (Id)) then
6907 Id := Entity (Renamed_Object (Id));
6908 else
6909 Id := Empty;
6910 exit;
6911 end if;
6912 end loop;
6913 end if;
6914
6915 return Id;
6916 end Entity_Of;
6917
6918 --------------------------
6919 -- Explain_Limited_Type --
6920 --------------------------
6921
6922 procedure Explain_Limited_Type (T : Entity_Id; N : Node_Id) is
6923 C : Entity_Id;
6924
6925 begin
6926 -- For array, component type must be limited
6927
6928 if Is_Array_Type (T) then
6929 Error_Msg_Node_2 := T;
6930 Error_Msg_NE
6931 ("\component type& of type& is limited", N, Component_Type (T));
6932 Explain_Limited_Type (Component_Type (T), N);
6933
6934 elsif Is_Record_Type (T) then
6935
6936 -- No need for extra messages if explicit limited record
6937
6938 if Is_Limited_Record (Base_Type (T)) then
6939 return;
6940 end if;
6941
6942 -- Otherwise find a limited component. Check only components that
6943 -- come from source, or inherited components that appear in the
6944 -- source of the ancestor.
6945
6946 C := First_Component (T);
6947 while Present (C) loop
6948 if Is_Limited_Type (Etype (C))
6949 and then
6950 (Comes_From_Source (C)
6951 or else
6952 (Present (Original_Record_Component (C))
6953 and then
6954 Comes_From_Source (Original_Record_Component (C))))
6955 then
6956 Error_Msg_Node_2 := T;
6957 Error_Msg_NE ("\component& of type& has limited type", N, C);
6958 Explain_Limited_Type (Etype (C), N);
6959 return;
6960 end if;
6961
6962 Next_Component (C);
6963 end loop;
6964
6965 -- The type may be declared explicitly limited, even if no component
6966 -- of it is limited, in which case we fall out of the loop.
6967 return;
6968 end if;
6969 end Explain_Limited_Type;
6970
6971 ---------------------------------------
6972 -- Expression_Of_Expression_Function --
6973 ---------------------------------------
6974
6975 function Expression_Of_Expression_Function
6976 (Subp : Entity_Id) return Node_Id
6977 is
6978 Expr_Func : Node_Id;
6979
6980 begin
6981 pragma Assert (Is_Expression_Function_Or_Completion (Subp));
6982
6983 if Nkind (Original_Node (Subprogram_Spec (Subp))) =
6984 N_Expression_Function
6985 then
6986 Expr_Func := Original_Node (Subprogram_Spec (Subp));
6987
6988 elsif Nkind (Original_Node (Subprogram_Body (Subp))) =
6989 N_Expression_Function
6990 then
6991 Expr_Func := Original_Node (Subprogram_Body (Subp));
6992
6993 else
6994 pragma Assert (False);
6995 null;
6996 end if;
6997
6998 return Original_Node (Expression (Expr_Func));
6999 end Expression_Of_Expression_Function;
7000
7001 -------------------------------
7002 -- Extensions_Visible_Status --
7003 -------------------------------
7004
7005 function Extensions_Visible_Status
7006 (Id : Entity_Id) return Extensions_Visible_Mode
7007 is
7008 Arg : Node_Id;
7009 Decl : Node_Id;
7010 Expr : Node_Id;
7011 Prag : Node_Id;
7012 Subp : Entity_Id;
7013
7014 begin
7015 -- When a formal parameter is subject to Extensions_Visible, the pragma
7016 -- is stored in the contract of related subprogram.
7017
7018 if Is_Formal (Id) then
7019 Subp := Scope (Id);
7020
7021 elsif Is_Subprogram_Or_Generic_Subprogram (Id) then
7022 Subp := Id;
7023
7024 -- No other construct carries this pragma
7025
7026 else
7027 return Extensions_Visible_None;
7028 end if;
7029
7030 Prag := Get_Pragma (Subp, Pragma_Extensions_Visible);
7031
7032 -- In certain cases analysis may request the Extensions_Visible status
7033 -- of an expression function before the pragma has been analyzed yet.
7034 -- Inspect the declarative items after the expression function looking
7035 -- for the pragma (if any).
7036
7037 if No (Prag) and then Is_Expression_Function (Subp) then
7038 Decl := Next (Unit_Declaration_Node (Subp));
7039 while Present (Decl) loop
7040 if Nkind (Decl) = N_Pragma
7041 and then Pragma_Name (Decl) = Name_Extensions_Visible
7042 then
7043 Prag := Decl;
7044 exit;
7045
7046 -- A source construct ends the region where Extensions_Visible may
7047 -- appear, stop the traversal. An expanded expression function is
7048 -- no longer a source construct, but it must still be recognized.
7049
7050 elsif Comes_From_Source (Decl)
7051 or else
7052 (Nkind_In (Decl, N_Subprogram_Body,
7053 N_Subprogram_Declaration)
7054 and then Is_Expression_Function (Defining_Entity (Decl)))
7055 then
7056 exit;
7057 end if;
7058
7059 Next (Decl);
7060 end loop;
7061 end if;
7062
7063 -- Extract the value from the Boolean expression (if any)
7064
7065 if Present (Prag) then
7066 Arg := First (Pragma_Argument_Associations (Prag));
7067
7068 if Present (Arg) then
7069 Expr := Get_Pragma_Arg (Arg);
7070
7071 -- When the associated subprogram is an expression function, the
7072 -- argument of the pragma may not have been analyzed.
7073
7074 if not Analyzed (Expr) then
7075 Preanalyze_And_Resolve (Expr, Standard_Boolean);
7076 end if;
7077
7078 -- Guard against cascading errors when the argument of pragma
7079 -- Extensions_Visible is not a valid static Boolean expression.
7080
7081 if Error_Posted (Expr) then
7082 return Extensions_Visible_None;
7083
7084 elsif Is_True (Expr_Value (Expr)) then
7085 return Extensions_Visible_True;
7086
7087 else
7088 return Extensions_Visible_False;
7089 end if;
7090
7091 -- Otherwise the aspect or pragma defaults to True
7092
7093 else
7094 return Extensions_Visible_True;
7095 end if;
7096
7097 -- Otherwise aspect or pragma Extensions_Visible is not inherited or
7098 -- directly specified. In SPARK code, its value defaults to "False".
7099
7100 elsif SPARK_Mode = On then
7101 return Extensions_Visible_False;
7102
7103 -- In non-SPARK code, aspect or pragma Extensions_Visible defaults to
7104 -- "True".
7105
7106 else
7107 return Extensions_Visible_True;
7108 end if;
7109 end Extensions_Visible_Status;
7110
7111 -----------------
7112 -- Find_Actual --
7113 -----------------
7114
7115 procedure Find_Actual
7116 (N : Node_Id;
7117 Formal : out Entity_Id;
7118 Call : out Node_Id)
7119 is
7120 Context : constant Node_Id := Parent (N);
7121 Actual : Node_Id;
7122 Call_Nam : Node_Id;
7123
7124 begin
7125 if Nkind_In (Context, N_Indexed_Component, N_Selected_Component)
7126 and then N = Prefix (Context)
7127 then
7128 Find_Actual (Context, Formal, Call);
7129 return;
7130
7131 elsif Nkind (Context) = N_Parameter_Association
7132 and then N = Explicit_Actual_Parameter (Context)
7133 then
7134 Call := Parent (Context);
7135
7136 elsif Nkind_In (Context, N_Entry_Call_Statement,
7137 N_Function_Call,
7138 N_Procedure_Call_Statement)
7139 then
7140 Call := Context;
7141
7142 else
7143 Formal := Empty;
7144 Call := Empty;
7145 return;
7146 end if;
7147
7148 -- If we have a call to a subprogram look for the parameter. Note that
7149 -- we exclude overloaded calls, since we don't know enough to be sure
7150 -- of giving the right answer in this case.
7151
7152 if Nkind_In (Call, N_Entry_Call_Statement,
7153 N_Function_Call,
7154 N_Procedure_Call_Statement)
7155 then
7156 Call_Nam := Name (Call);
7157
7158 -- A call to a protected or task entry appears as a selected
7159 -- component rather than an expanded name.
7160
7161 if Nkind (Call_Nam) = N_Selected_Component then
7162 Call_Nam := Selector_Name (Call_Nam);
7163 end if;
7164
7165 if Is_Entity_Name (Call_Nam)
7166 and then Present (Entity (Call_Nam))
7167 and then Is_Overloadable (Entity (Call_Nam))
7168 and then not Is_Overloaded (Call_Nam)
7169 then
7170 -- If node is name in call it is not an actual
7171
7172 if N = Call_Nam then
7173 Formal := Empty;
7174 Call := Empty;
7175 return;
7176 end if;
7177
7178 -- Fall here if we are definitely a parameter
7179
7180 Actual := First_Actual (Call);
7181 Formal := First_Formal (Entity (Call_Nam));
7182 while Present (Formal) and then Present (Actual) loop
7183 if Actual = N then
7184 return;
7185
7186 -- An actual that is the prefix in a prefixed call may have
7187 -- been rewritten in the call, after the deferred reference
7188 -- was collected. Check if sloc and kinds and names match.
7189
7190 elsif Sloc (Actual) = Sloc (N)
7191 and then Nkind (Actual) = N_Identifier
7192 and then Nkind (Actual) = Nkind (N)
7193 and then Chars (Actual) = Chars (N)
7194 then
7195 return;
7196
7197 else
7198 Actual := Next_Actual (Actual);
7199 Formal := Next_Formal (Formal);
7200 end if;
7201 end loop;
7202 end if;
7203 end if;
7204
7205 -- Fall through here if we did not find matching actual
7206
7207 Formal := Empty;
7208 Call := Empty;
7209 end Find_Actual;
7210
7211 ---------------------------
7212 -- Find_Body_Discriminal --
7213 ---------------------------
7214
7215 function Find_Body_Discriminal
7216 (Spec_Discriminant : Entity_Id) return Entity_Id
7217 is
7218 Tsk : Entity_Id;
7219 Disc : Entity_Id;
7220
7221 begin
7222 -- If expansion is suppressed, then the scope can be the concurrent type
7223 -- itself rather than a corresponding concurrent record type.
7224
7225 if Is_Concurrent_Type (Scope (Spec_Discriminant)) then
7226 Tsk := Scope (Spec_Discriminant);
7227
7228 else
7229 pragma Assert (Is_Concurrent_Record_Type (Scope (Spec_Discriminant)));
7230
7231 Tsk := Corresponding_Concurrent_Type (Scope (Spec_Discriminant));
7232 end if;
7233
7234 -- Find discriminant of original concurrent type, and use its current
7235 -- discriminal, which is the renaming within the task/protected body.
7236
7237 Disc := First_Discriminant (Tsk);
7238 while Present (Disc) loop
7239 if Chars (Disc) = Chars (Spec_Discriminant) then
7240 return Discriminal (Disc);
7241 end if;
7242
7243 Next_Discriminant (Disc);
7244 end loop;
7245
7246 -- That loop should always succeed in finding a matching entry and
7247 -- returning. Fatal error if not.
7248
7249 raise Program_Error;
7250 end Find_Body_Discriminal;
7251
7252 -------------------------------------
7253 -- Find_Corresponding_Discriminant --
7254 -------------------------------------
7255
7256 function Find_Corresponding_Discriminant
7257 (Id : Node_Id;
7258 Typ : Entity_Id) return Entity_Id
7259 is
7260 Par_Disc : Entity_Id;
7261 Old_Disc : Entity_Id;
7262 New_Disc : Entity_Id;
7263
7264 begin
7265 Par_Disc := Original_Record_Component (Original_Discriminant (Id));
7266
7267 -- The original type may currently be private, and the discriminant
7268 -- only appear on its full view.
7269
7270 if Is_Private_Type (Scope (Par_Disc))
7271 and then not Has_Discriminants (Scope (Par_Disc))
7272 and then Present (Full_View (Scope (Par_Disc)))
7273 then
7274 Old_Disc := First_Discriminant (Full_View (Scope (Par_Disc)));
7275 else
7276 Old_Disc := First_Discriminant (Scope (Par_Disc));
7277 end if;
7278
7279 if Is_Class_Wide_Type (Typ) then
7280 New_Disc := First_Discriminant (Root_Type (Typ));
7281 else
7282 New_Disc := First_Discriminant (Typ);
7283 end if;
7284
7285 while Present (Old_Disc) and then Present (New_Disc) loop
7286 if Old_Disc = Par_Disc then
7287 return New_Disc;
7288 end if;
7289
7290 Next_Discriminant (Old_Disc);
7291 Next_Discriminant (New_Disc);
7292 end loop;
7293
7294 -- Should always find it
7295
7296 raise Program_Error;
7297 end Find_Corresponding_Discriminant;
7298
7299 ----------------------------------
7300 -- Find_Enclosing_Iterator_Loop --
7301 ----------------------------------
7302
7303 function Find_Enclosing_Iterator_Loop (Id : Entity_Id) return Entity_Id is
7304 Constr : Node_Id;
7305 S : Entity_Id;
7306
7307 begin
7308 -- Traverse the scope chain looking for an iterator loop. Such loops are
7309 -- usually transformed into blocks, hence the use of Original_Node.
7310
7311 S := Id;
7312 while Present (S) and then S /= Standard_Standard loop
7313 if Ekind (S) = E_Loop
7314 and then Nkind (Parent (S)) = N_Implicit_Label_Declaration
7315 then
7316 Constr := Original_Node (Label_Construct (Parent (S)));
7317
7318 if Nkind (Constr) = N_Loop_Statement
7319 and then Present (Iteration_Scheme (Constr))
7320 and then Nkind (Iterator_Specification
7321 (Iteration_Scheme (Constr))) =
7322 N_Iterator_Specification
7323 then
7324 return S;
7325 end if;
7326 end if;
7327
7328 S := Scope (S);
7329 end loop;
7330
7331 return Empty;
7332 end Find_Enclosing_Iterator_Loop;
7333
7334 ------------------------------------
7335 -- Find_Loop_In_Conditional_Block --
7336 ------------------------------------
7337
7338 function Find_Loop_In_Conditional_Block (N : Node_Id) return Node_Id is
7339 Stmt : Node_Id;
7340
7341 begin
7342 Stmt := N;
7343
7344 if Nkind (Stmt) = N_If_Statement then
7345 Stmt := First (Then_Statements (Stmt));
7346 end if;
7347
7348 pragma Assert (Nkind (Stmt) = N_Block_Statement);
7349
7350 -- Inspect the statements of the conditional block. In general the loop
7351 -- should be the first statement in the statement sequence of the block,
7352 -- but the finalization machinery may have introduced extra object
7353 -- declarations.
7354
7355 Stmt := First (Statements (Handled_Statement_Sequence (Stmt)));
7356 while Present (Stmt) loop
7357 if Nkind (Stmt) = N_Loop_Statement then
7358 return Stmt;
7359 end if;
7360
7361 Next (Stmt);
7362 end loop;
7363
7364 -- The expansion of attribute 'Loop_Entry produced a malformed block
7365
7366 raise Program_Error;
7367 end Find_Loop_In_Conditional_Block;
7368
7369 --------------------------
7370 -- Find_Overlaid_Entity --
7371 --------------------------
7372
7373 procedure Find_Overlaid_Entity
7374 (N : Node_Id;
7375 Ent : out Entity_Id;
7376 Off : out Boolean)
7377 is
7378 Expr : Node_Id;
7379
7380 begin
7381 -- We are looking for one of the two following forms:
7382
7383 -- for X'Address use Y'Address
7384
7385 -- or
7386
7387 -- Const : constant Address := expr;
7388 -- ...
7389 -- for X'Address use Const;
7390
7391 -- In the second case, the expr is either Y'Address, or recursively a
7392 -- constant that eventually references Y'Address.
7393
7394 Ent := Empty;
7395 Off := False;
7396
7397 if Nkind (N) = N_Attribute_Definition_Clause
7398 and then Chars (N) = Name_Address
7399 then
7400 Expr := Expression (N);
7401
7402 -- This loop checks the form of the expression for Y'Address,
7403 -- using recursion to deal with intermediate constants.
7404
7405 loop
7406 -- Check for Y'Address
7407
7408 if Nkind (Expr) = N_Attribute_Reference
7409 and then Attribute_Name (Expr) = Name_Address
7410 then
7411 Expr := Prefix (Expr);
7412 exit;
7413
7414 -- Check for Const where Const is a constant entity
7415
7416 elsif Is_Entity_Name (Expr)
7417 and then Ekind (Entity (Expr)) = E_Constant
7418 then
7419 Expr := Constant_Value (Entity (Expr));
7420
7421 -- Anything else does not need checking
7422
7423 else
7424 return;
7425 end if;
7426 end loop;
7427
7428 -- This loop checks the form of the prefix for an entity, using
7429 -- recursion to deal with intermediate components.
7430
7431 loop
7432 -- Check for Y where Y is an entity
7433
7434 if Is_Entity_Name (Expr) then
7435 Ent := Entity (Expr);
7436 return;
7437
7438 -- Check for components
7439
7440 elsif
7441 Nkind_In (Expr, N_Selected_Component, N_Indexed_Component)
7442 then
7443 Expr := Prefix (Expr);
7444 Off := True;
7445
7446 -- Anything else does not need checking
7447
7448 else
7449 return;
7450 end if;
7451 end loop;
7452 end if;
7453 end Find_Overlaid_Entity;
7454
7455 -------------------------
7456 -- Find_Parameter_Type --
7457 -------------------------
7458
7459 function Find_Parameter_Type (Param : Node_Id) return Entity_Id is
7460 begin
7461 if Nkind (Param) /= N_Parameter_Specification then
7462 return Empty;
7463
7464 -- For an access parameter, obtain the type from the formal entity
7465 -- itself, because access to subprogram nodes do not carry a type.
7466 -- Shouldn't we always use the formal entity ???
7467
7468 elsif Nkind (Parameter_Type (Param)) = N_Access_Definition then
7469 return Etype (Defining_Identifier (Param));
7470
7471 else
7472 return Etype (Parameter_Type (Param));
7473 end if;
7474 end Find_Parameter_Type;
7475
7476 -----------------------------------
7477 -- Find_Placement_In_State_Space --
7478 -----------------------------------
7479
7480 procedure Find_Placement_In_State_Space
7481 (Item_Id : Entity_Id;
7482 Placement : out State_Space_Kind;
7483 Pack_Id : out Entity_Id)
7484 is
7485 Context : Entity_Id;
7486
7487 begin
7488 -- Assume that the item does not appear in the state space of a package
7489
7490 Placement := Not_In_Package;
7491 Pack_Id := Empty;
7492
7493 -- Climb the scope stack and examine the enclosing context
7494
7495 Context := Scope (Item_Id);
7496 while Present (Context) and then Context /= Standard_Standard loop
7497 if Ekind (Context) = E_Package then
7498 Pack_Id := Context;
7499
7500 -- A package body is a cut off point for the traversal as the item
7501 -- cannot be visible to the outside from this point on. Note that
7502 -- this test must be done first as a body is also classified as a
7503 -- private part.
7504
7505 if In_Package_Body (Context) then
7506 Placement := Body_State_Space;
7507 return;
7508
7509 -- The private part of a package is a cut off point for the
7510 -- traversal as the item cannot be visible to the outside from
7511 -- this point on.
7512
7513 elsif In_Private_Part (Context) then
7514 Placement := Private_State_Space;
7515 return;
7516
7517 -- When the item appears in the visible state space of a package,
7518 -- continue to climb the scope stack as this may not be the final
7519 -- state space.
7520
7521 else
7522 Placement := Visible_State_Space;
7523
7524 -- The visible state space of a child unit acts as the proper
7525 -- placement of an item.
7526
7527 if Is_Child_Unit (Context) then
7528 return;
7529 end if;
7530 end if;
7531
7532 -- The item or its enclosing package appear in a construct that has
7533 -- no state space.
7534
7535 else
7536 Placement := Not_In_Package;
7537 return;
7538 end if;
7539
7540 Context := Scope (Context);
7541 end loop;
7542 end Find_Placement_In_State_Space;
7543
7544 ------------------------
7545 -- Find_Specific_Type --
7546 ------------------------
7547
7548 function Find_Specific_Type (CW : Entity_Id) return Entity_Id is
7549 Typ : Entity_Id := Root_Type (CW);
7550
7551 begin
7552 if Ekind (Typ) = E_Incomplete_Type then
7553 if From_Limited_With (Typ) then
7554 Typ := Non_Limited_View (Typ);
7555 else
7556 Typ := Full_View (Typ);
7557 end if;
7558 end if;
7559
7560 if Is_Private_Type (Typ)
7561 and then not Is_Tagged_Type (Typ)
7562 and then Present (Full_View (Typ))
7563 then
7564 return Full_View (Typ);
7565 else
7566 return Typ;
7567 end if;
7568 end Find_Specific_Type;
7569
7570 -----------------------------
7571 -- Find_Static_Alternative --
7572 -----------------------------
7573
7574 function Find_Static_Alternative (N : Node_Id) return Node_Id is
7575 Expr : constant Node_Id := Expression (N);
7576 Val : constant Uint := Expr_Value (Expr);
7577 Alt : Node_Id;
7578 Choice : Node_Id;
7579
7580 begin
7581 Alt := First (Alternatives (N));
7582
7583 Search : loop
7584 if Nkind (Alt) /= N_Pragma then
7585 Choice := First (Discrete_Choices (Alt));
7586 while Present (Choice) loop
7587
7588 -- Others choice, always matches
7589
7590 if Nkind (Choice) = N_Others_Choice then
7591 exit Search;
7592
7593 -- Range, check if value is in the range
7594
7595 elsif Nkind (Choice) = N_Range then
7596 exit Search when
7597 Val >= Expr_Value (Low_Bound (Choice))
7598 and then
7599 Val <= Expr_Value (High_Bound (Choice));
7600
7601 -- Choice is a subtype name. Note that we know it must
7602 -- be a static subtype, since otherwise it would have
7603 -- been diagnosed as illegal.
7604
7605 elsif Is_Entity_Name (Choice)
7606 and then Is_Type (Entity (Choice))
7607 then
7608 exit Search when Is_In_Range (Expr, Etype (Choice),
7609 Assume_Valid => False);
7610
7611 -- Choice is a subtype indication
7612
7613 elsif Nkind (Choice) = N_Subtype_Indication then
7614 declare
7615 C : constant Node_Id := Constraint (Choice);
7616 R : constant Node_Id := Range_Expression (C);
7617
7618 begin
7619 exit Search when
7620 Val >= Expr_Value (Low_Bound (R))
7621 and then
7622 Val <= Expr_Value (High_Bound (R));
7623 end;
7624
7625 -- Choice is a simple expression
7626
7627 else
7628 exit Search when Val = Expr_Value (Choice);
7629 end if;
7630
7631 Next (Choice);
7632 end loop;
7633 end if;
7634
7635 Next (Alt);
7636 pragma Assert (Present (Alt));
7637 end loop Search;
7638
7639 -- The above loop *must* terminate by finding a match, since we know the
7640 -- case statement is valid, and the value of the expression is known at
7641 -- compile time. When we fall out of the loop, Alt points to the
7642 -- alternative that we know will be selected at run time.
7643
7644 return Alt;
7645 end Find_Static_Alternative;
7646
7647 ------------------
7648 -- First_Actual --
7649 ------------------
7650
7651 function First_Actual (Node : Node_Id) return Node_Id is
7652 N : Node_Id;
7653
7654 begin
7655 if No (Parameter_Associations (Node)) then
7656 return Empty;
7657 end if;
7658
7659 N := First (Parameter_Associations (Node));
7660
7661 if Nkind (N) = N_Parameter_Association then
7662 return First_Named_Actual (Node);
7663 else
7664 return N;
7665 end if;
7666 end First_Actual;
7667
7668 -------------
7669 -- Fix_Msg --
7670 -------------
7671
7672 function Fix_Msg (Id : Entity_Id; Msg : String) return String is
7673 Is_Task : constant Boolean :=
7674 Ekind_In (Id, E_Task_Body, E_Task_Type)
7675 or else Is_Single_Task_Object (Id);
7676 Msg_Last : constant Natural := Msg'Last;
7677 Msg_Index : Natural;
7678 Res : String (Msg'Range) := (others => ' ');
7679 Res_Index : Natural;
7680
7681 begin
7682 -- Copy all characters from the input message Msg to result Res with
7683 -- suitable replacements.
7684
7685 Msg_Index := Msg'First;
7686 Res_Index := Res'First;
7687 while Msg_Index <= Msg_Last loop
7688
7689 -- Replace "subprogram" with a different word
7690
7691 if Msg_Index <= Msg_Last - 10
7692 and then Msg (Msg_Index .. Msg_Index + 9) = "subprogram"
7693 then
7694 if Ekind_In (Id, E_Entry, E_Entry_Family) then
7695 Res (Res_Index .. Res_Index + 4) := "entry";
7696 Res_Index := Res_Index + 5;
7697
7698 elsif Is_Task then
7699 Res (Res_Index .. Res_Index + 8) := "task type";
7700 Res_Index := Res_Index + 9;
7701
7702 else
7703 Res (Res_Index .. Res_Index + 9) := "subprogram";
7704 Res_Index := Res_Index + 10;
7705 end if;
7706
7707 Msg_Index := Msg_Index + 10;
7708
7709 -- Replace "protected" with a different word
7710
7711 elsif Msg_Index <= Msg_Last - 9
7712 and then Msg (Msg_Index .. Msg_Index + 8) = "protected"
7713 and then Is_Task
7714 then
7715 Res (Res_Index .. Res_Index + 3) := "task";
7716 Res_Index := Res_Index + 4;
7717 Msg_Index := Msg_Index + 9;
7718
7719 -- Otherwise copy the character
7720
7721 else
7722 Res (Res_Index) := Msg (Msg_Index);
7723 Msg_Index := Msg_Index + 1;
7724 Res_Index := Res_Index + 1;
7725 end if;
7726 end loop;
7727
7728 return Res (Res'First .. Res_Index - 1);
7729 end Fix_Msg;
7730
7731 -------------------------
7732 -- From_Nested_Package --
7733 -------------------------
7734
7735 function From_Nested_Package (T : Entity_Id) return Boolean is
7736 Pack : constant Entity_Id := Scope (T);
7737
7738 begin
7739 return
7740 Ekind (Pack) = E_Package
7741 and then not Is_Frozen (Pack)
7742 and then not Scope_Within_Or_Same (Current_Scope, Pack)
7743 and then In_Open_Scopes (Scope (Pack));
7744 end From_Nested_Package;
7745
7746 -----------------------
7747 -- Gather_Components --
7748 -----------------------
7749
7750 procedure Gather_Components
7751 (Typ : Entity_Id;
7752 Comp_List : Node_Id;
7753 Governed_By : List_Id;
7754 Into : Elist_Id;
7755 Report_Errors : out Boolean)
7756 is
7757 Assoc : Node_Id;
7758 Variant : Node_Id;
7759 Discrete_Choice : Node_Id;
7760 Comp_Item : Node_Id;
7761
7762 Discrim : Entity_Id;
7763 Discrim_Name : Node_Id;
7764 Discrim_Value : Node_Id;
7765
7766 begin
7767 Report_Errors := False;
7768
7769 if No (Comp_List) or else Null_Present (Comp_List) then
7770 return;
7771
7772 elsif Present (Component_Items (Comp_List)) then
7773 Comp_Item := First (Component_Items (Comp_List));
7774
7775 else
7776 Comp_Item := Empty;
7777 end if;
7778
7779 while Present (Comp_Item) loop
7780
7781 -- Skip the tag of a tagged record, the interface tags, as well
7782 -- as all items that are not user components (anonymous types,
7783 -- rep clauses, Parent field, controller field).
7784
7785 if Nkind (Comp_Item) = N_Component_Declaration then
7786 declare
7787 Comp : constant Entity_Id := Defining_Identifier (Comp_Item);
7788 begin
7789 if not Is_Tag (Comp) and then Chars (Comp) /= Name_uParent then
7790 Append_Elmt (Comp, Into);
7791 end if;
7792 end;
7793 end if;
7794
7795 Next (Comp_Item);
7796 end loop;
7797
7798 if No (Variant_Part (Comp_List)) then
7799 return;
7800 else
7801 Discrim_Name := Name (Variant_Part (Comp_List));
7802 Variant := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
7803 end if;
7804
7805 -- Look for the discriminant that governs this variant part.
7806 -- The discriminant *must* be in the Governed_By List
7807
7808 Assoc := First (Governed_By);
7809 Find_Constraint : loop
7810 Discrim := First (Choices (Assoc));
7811 exit Find_Constraint when Chars (Discrim_Name) = Chars (Discrim)
7812 or else (Present (Corresponding_Discriminant (Entity (Discrim)))
7813 and then
7814 Chars (Corresponding_Discriminant (Entity (Discrim))) =
7815 Chars (Discrim_Name))
7816 or else Chars (Original_Record_Component (Entity (Discrim)))
7817 = Chars (Discrim_Name);
7818
7819 if No (Next (Assoc)) then
7820 if not Is_Constrained (Typ)
7821 and then Is_Derived_Type (Typ)
7822 and then Present (Stored_Constraint (Typ))
7823 then
7824 -- If the type is a tagged type with inherited discriminants,
7825 -- use the stored constraint on the parent in order to find
7826 -- the values of discriminants that are otherwise hidden by an
7827 -- explicit constraint. Renamed discriminants are handled in
7828 -- the code above.
7829
7830 -- If several parent discriminants are renamed by a single
7831 -- discriminant of the derived type, the call to obtain the
7832 -- Corresponding_Discriminant field only retrieves the last
7833 -- of them. We recover the constraint on the others from the
7834 -- Stored_Constraint as well.
7835
7836 declare
7837 D : Entity_Id;
7838 C : Elmt_Id;
7839
7840 begin
7841 D := First_Discriminant (Etype (Typ));
7842 C := First_Elmt (Stored_Constraint (Typ));
7843 while Present (D) and then Present (C) loop
7844 if Chars (Discrim_Name) = Chars (D) then
7845 if Is_Entity_Name (Node (C))
7846 and then Entity (Node (C)) = Entity (Discrim)
7847 then
7848 -- D is renamed by Discrim, whose value is given in
7849 -- Assoc.
7850
7851 null;
7852
7853 else
7854 Assoc :=
7855 Make_Component_Association (Sloc (Typ),
7856 New_List
7857 (New_Occurrence_Of (D, Sloc (Typ))),
7858 Duplicate_Subexpr_No_Checks (Node (C)));
7859 end if;
7860 exit Find_Constraint;
7861 end if;
7862
7863 Next_Discriminant (D);
7864 Next_Elmt (C);
7865 end loop;
7866 end;
7867 end if;
7868 end if;
7869
7870 if No (Next (Assoc)) then
7871 Error_Msg_NE (" missing value for discriminant&",
7872 First (Governed_By), Discrim_Name);
7873 Report_Errors := True;
7874 return;
7875 end if;
7876
7877 Next (Assoc);
7878 end loop Find_Constraint;
7879
7880 Discrim_Value := Expression (Assoc);
7881
7882 if not Is_OK_Static_Expression (Discrim_Value) then
7883
7884 -- If the variant part is governed by a discriminant of the type
7885 -- this is an error. If the variant part and the discriminant are
7886 -- inherited from an ancestor this is legal (AI05-120) unless the
7887 -- components are being gathered for an aggregate, in which case
7888 -- the caller must check Report_Errors.
7889
7890 if Scope (Original_Record_Component
7891 ((Entity (First (Choices (Assoc)))))) = Typ
7892 then
7893 Error_Msg_FE
7894 ("value for discriminant & must be static!",
7895 Discrim_Value, Discrim);
7896 Why_Not_Static (Discrim_Value);
7897 end if;
7898
7899 Report_Errors := True;
7900 return;
7901 end if;
7902
7903 Search_For_Discriminant_Value : declare
7904 Low : Node_Id;
7905 High : Node_Id;
7906
7907 UI_High : Uint;
7908 UI_Low : Uint;
7909 UI_Discrim_Value : constant Uint := Expr_Value (Discrim_Value);
7910
7911 begin
7912 Find_Discrete_Value : while Present (Variant) loop
7913 Discrete_Choice := First (Discrete_Choices (Variant));
7914 while Present (Discrete_Choice) loop
7915 exit Find_Discrete_Value when
7916 Nkind (Discrete_Choice) = N_Others_Choice;
7917
7918 Get_Index_Bounds (Discrete_Choice, Low, High);
7919
7920 UI_Low := Expr_Value (Low);
7921 UI_High := Expr_Value (High);
7922
7923 exit Find_Discrete_Value when
7924 UI_Low <= UI_Discrim_Value
7925 and then
7926 UI_High >= UI_Discrim_Value;
7927
7928 Next (Discrete_Choice);
7929 end loop;
7930
7931 Next_Non_Pragma (Variant);
7932 end loop Find_Discrete_Value;
7933 end Search_For_Discriminant_Value;
7934
7935 -- The case statement must include a variant that corresponds to the
7936 -- value of the discriminant, unless the discriminant type has a
7937 -- static predicate. In that case the absence of an others_choice that
7938 -- would cover this value becomes a run-time error (3.8,1 (21.1/2)).
7939
7940 if No (Variant)
7941 and then not Has_Static_Predicate (Etype (Discrim_Name))
7942 then
7943 Error_Msg_NE
7944 ("value of discriminant & is out of range", Discrim_Value, Discrim);
7945 Report_Errors := True;
7946 return;
7947 end if;
7948
7949 -- If we have found the corresponding choice, recursively add its
7950 -- components to the Into list. The nested components are part of
7951 -- the same record type.
7952
7953 if Present (Variant) then
7954 Gather_Components
7955 (Typ, Component_List (Variant), Governed_By, Into, Report_Errors);
7956 end if;
7957 end Gather_Components;
7958
7959 ------------------------
7960 -- Get_Actual_Subtype --
7961 ------------------------
7962
7963 function Get_Actual_Subtype (N : Node_Id) return Entity_Id is
7964 Typ : constant Entity_Id := Etype (N);
7965 Utyp : Entity_Id := Underlying_Type (Typ);
7966 Decl : Node_Id;
7967 Atyp : Entity_Id;
7968
7969 begin
7970 if No (Utyp) then
7971 Utyp := Typ;
7972 end if;
7973
7974 -- If what we have is an identifier that references a subprogram
7975 -- formal, or a variable or constant object, then we get the actual
7976 -- subtype from the referenced entity if one has been built.
7977
7978 if Nkind (N) = N_Identifier
7979 and then
7980 (Is_Formal (Entity (N))
7981 or else Ekind (Entity (N)) = E_Constant
7982 or else Ekind (Entity (N)) = E_Variable)
7983 and then Present (Actual_Subtype (Entity (N)))
7984 then
7985 return Actual_Subtype (Entity (N));
7986
7987 -- Actual subtype of unchecked union is always itself. We never need
7988 -- the "real" actual subtype. If we did, we couldn't get it anyway
7989 -- because the discriminant is not available. The restrictions on
7990 -- Unchecked_Union are designed to make sure that this is OK.
7991
7992 elsif Is_Unchecked_Union (Base_Type (Utyp)) then
7993 return Typ;
7994
7995 -- Here for the unconstrained case, we must find actual subtype
7996 -- No actual subtype is available, so we must build it on the fly.
7997
7998 -- Checking the type, not the underlying type, for constrainedness
7999 -- seems to be necessary. Maybe all the tests should be on the type???
8000
8001 elsif (not Is_Constrained (Typ))
8002 and then (Is_Array_Type (Utyp)
8003 or else (Is_Record_Type (Utyp)
8004 and then Has_Discriminants (Utyp)))
8005 and then not Has_Unknown_Discriminants (Utyp)
8006 and then not (Ekind (Utyp) = E_String_Literal_Subtype)
8007 then
8008 -- Nothing to do if in spec expression (why not???)
8009
8010 if In_Spec_Expression then
8011 return Typ;
8012
8013 elsif Is_Private_Type (Typ) and then not Has_Discriminants (Typ) then
8014
8015 -- If the type has no discriminants, there is no subtype to
8016 -- build, even if the underlying type is discriminated.
8017
8018 return Typ;
8019
8020 -- Else build the actual subtype
8021
8022 else
8023 Decl := Build_Actual_Subtype (Typ, N);
8024 Atyp := Defining_Identifier (Decl);
8025
8026 -- If Build_Actual_Subtype generated a new declaration then use it
8027
8028 if Atyp /= Typ then
8029
8030 -- The actual subtype is an Itype, so analyze the declaration,
8031 -- but do not attach it to the tree, to get the type defined.
8032
8033 Set_Parent (Decl, N);
8034 Set_Is_Itype (Atyp);
8035 Analyze (Decl, Suppress => All_Checks);
8036 Set_Associated_Node_For_Itype (Atyp, N);
8037 Set_Has_Delayed_Freeze (Atyp, False);
8038
8039 -- We need to freeze the actual subtype immediately. This is
8040 -- needed, because otherwise this Itype will not get frozen
8041 -- at all, and it is always safe to freeze on creation because
8042 -- any associated types must be frozen at this point.
8043
8044 Freeze_Itype (Atyp, N);
8045 return Atyp;
8046
8047 -- Otherwise we did not build a declaration, so return original
8048
8049 else
8050 return Typ;
8051 end if;
8052 end if;
8053
8054 -- For all remaining cases, the actual subtype is the same as
8055 -- the nominal type.
8056
8057 else
8058 return Typ;
8059 end if;
8060 end Get_Actual_Subtype;
8061
8062 -------------------------------------
8063 -- Get_Actual_Subtype_If_Available --
8064 -------------------------------------
8065
8066 function Get_Actual_Subtype_If_Available (N : Node_Id) return Entity_Id is
8067 Typ : constant Entity_Id := Etype (N);
8068
8069 begin
8070 -- If what we have is an identifier that references a subprogram
8071 -- formal, or a variable or constant object, then we get the actual
8072 -- subtype from the referenced entity if one has been built.
8073
8074 if Nkind (N) = N_Identifier
8075 and then
8076 (Is_Formal (Entity (N))
8077 or else Ekind (Entity (N)) = E_Constant
8078 or else Ekind (Entity (N)) = E_Variable)
8079 and then Present (Actual_Subtype (Entity (N)))
8080 then
8081 return Actual_Subtype (Entity (N));
8082
8083 -- Otherwise the Etype of N is returned unchanged
8084
8085 else
8086 return Typ;
8087 end if;
8088 end Get_Actual_Subtype_If_Available;
8089
8090 ------------------------
8091 -- Get_Body_From_Stub --
8092 ------------------------
8093
8094 function Get_Body_From_Stub (N : Node_Id) return Node_Id is
8095 begin
8096 return Proper_Body (Unit (Library_Unit (N)));
8097 end Get_Body_From_Stub;
8098
8099 ---------------------
8100 -- Get_Cursor_Type --
8101 ---------------------
8102
8103 function Get_Cursor_Type
8104 (Aspect : Node_Id;
8105 Typ : Entity_Id) return Entity_Id
8106 is
8107 Assoc : Node_Id;
8108 Func : Entity_Id;
8109 First_Op : Entity_Id;
8110 Cursor : Entity_Id;
8111
8112 begin
8113 -- If error already detected, return
8114
8115 if Error_Posted (Aspect) then
8116 return Any_Type;
8117 end if;
8118
8119 -- The cursor type for an Iterable aspect is the return type of a
8120 -- non-overloaded First primitive operation. Locate association for
8121 -- First.
8122
8123 Assoc := First (Component_Associations (Expression (Aspect)));
8124 First_Op := Any_Id;
8125 while Present (Assoc) loop
8126 if Chars (First (Choices (Assoc))) = Name_First then
8127 First_Op := Expression (Assoc);
8128 exit;
8129 end if;
8130
8131 Next (Assoc);
8132 end loop;
8133
8134 if First_Op = Any_Id then
8135 Error_Msg_N ("aspect Iterable must specify First operation", Aspect);
8136 return Any_Type;
8137 end if;
8138
8139 Cursor := Any_Type;
8140
8141 -- Locate function with desired name and profile in scope of type
8142 -- In the rare case where the type is an integer type, a base type
8143 -- is created for it, check that the base type of the first formal
8144 -- of First matches the base type of the domain.
8145
8146 Func := First_Entity (Scope (Typ));
8147 while Present (Func) loop
8148 if Chars (Func) = Chars (First_Op)
8149 and then Ekind (Func) = E_Function
8150 and then Present (First_Formal (Func))
8151 and then Base_Type (Etype (First_Formal (Func))) = Base_Type (Typ)
8152 and then No (Next_Formal (First_Formal (Func)))
8153 then
8154 if Cursor /= Any_Type then
8155 Error_Msg_N
8156 ("Operation First for iterable type must be unique", Aspect);
8157 return Any_Type;
8158 else
8159 Cursor := Etype (Func);
8160 end if;
8161 end if;
8162
8163 Next_Entity (Func);
8164 end loop;
8165
8166 -- If not found, no way to resolve remaining primitives.
8167
8168 if Cursor = Any_Type then
8169 Error_Msg_N
8170 ("No legal primitive operation First for Iterable type", Aspect);
8171 end if;
8172
8173 return Cursor;
8174 end Get_Cursor_Type;
8175
8176 function Get_Cursor_Type (Typ : Entity_Id) return Entity_Id is
8177 begin
8178 return Etype (Get_Iterable_Type_Primitive (Typ, Name_First));
8179 end Get_Cursor_Type;
8180
8181 -------------------------------
8182 -- Get_Default_External_Name --
8183 -------------------------------
8184
8185 function Get_Default_External_Name (E : Node_Or_Entity_Id) return Node_Id is
8186 begin
8187 Get_Decoded_Name_String (Chars (E));
8188
8189 if Opt.External_Name_Imp_Casing = Uppercase then
8190 Set_Casing (All_Upper_Case);
8191 else
8192 Set_Casing (All_Lower_Case);
8193 end if;
8194
8195 return
8196 Make_String_Literal (Sloc (E),
8197 Strval => String_From_Name_Buffer);
8198 end Get_Default_External_Name;
8199
8200 --------------------------
8201 -- Get_Enclosing_Object --
8202 --------------------------
8203
8204 function Get_Enclosing_Object (N : Node_Id) return Entity_Id is
8205 begin
8206 if Is_Entity_Name (N) then
8207 return Entity (N);
8208 else
8209 case Nkind (N) is
8210 when N_Indexed_Component
8211 | N_Selected_Component
8212 | N_Slice
8213 =>
8214 -- If not generating code, a dereference may be left implicit.
8215 -- In thoses cases, return Empty.
8216
8217 if Is_Access_Type (Etype (Prefix (N))) then
8218 return Empty;
8219 else
8220 return Get_Enclosing_Object (Prefix (N));
8221 end if;
8222
8223 when N_Type_Conversion =>
8224 return Get_Enclosing_Object (Expression (N));
8225
8226 when others =>
8227 return Empty;
8228 end case;
8229 end if;
8230 end Get_Enclosing_Object;
8231
8232 ---------------------------
8233 -- Get_Enum_Lit_From_Pos --
8234 ---------------------------
8235
8236 function Get_Enum_Lit_From_Pos
8237 (T : Entity_Id;
8238 Pos : Uint;
8239 Loc : Source_Ptr) return Node_Id
8240 is
8241 Btyp : Entity_Id := Base_Type (T);
8242 Lit : Node_Id;
8243 LLoc : Source_Ptr;
8244
8245 begin
8246 -- In the case where the literal is of type Character, Wide_Character
8247 -- or Wide_Wide_Character or of a type derived from them, there needs
8248 -- to be some special handling since there is no explicit chain of
8249 -- literals to search. Instead, an N_Character_Literal node is created
8250 -- with the appropriate Char_Code and Chars fields.
8251
8252 if Is_Standard_Character_Type (T) then
8253 Set_Character_Literal_Name (UI_To_CC (Pos));
8254
8255 return
8256 Make_Character_Literal (Loc,
8257 Chars => Name_Find,
8258 Char_Literal_Value => Pos);
8259
8260 -- For all other cases, we have a complete table of literals, and
8261 -- we simply iterate through the chain of literal until the one
8262 -- with the desired position value is found.
8263
8264 else
8265 if Is_Private_Type (Btyp) and then Present (Full_View (Btyp)) then
8266 Btyp := Full_View (Btyp);
8267 end if;
8268
8269 Lit := First_Literal (Btyp);
8270 for J in 1 .. UI_To_Int (Pos) loop
8271 Next_Literal (Lit);
8272
8273 -- If Lit is Empty, Pos is not in range, so raise Constraint_Error
8274 -- inside the loop to avoid calling Next_Literal on Empty.
8275
8276 if No (Lit) then
8277 raise Constraint_Error;
8278 end if;
8279 end loop;
8280
8281 -- Create a new node from Lit, with source location provided by Loc
8282 -- if not equal to No_Location, or by copying the source location of
8283 -- Lit otherwise.
8284
8285 LLoc := Loc;
8286
8287 if LLoc = No_Location then
8288 LLoc := Sloc (Lit);
8289 end if;
8290
8291 return New_Occurrence_Of (Lit, LLoc);
8292 end if;
8293 end Get_Enum_Lit_From_Pos;
8294
8295 ------------------------
8296 -- Get_Generic_Entity --
8297 ------------------------
8298
8299 function Get_Generic_Entity (N : Node_Id) return Entity_Id is
8300 Ent : constant Entity_Id := Entity (Name (N));
8301 begin
8302 if Present (Renamed_Object (Ent)) then
8303 return Renamed_Object (Ent);
8304 else
8305 return Ent;
8306 end if;
8307 end Get_Generic_Entity;
8308
8309 -------------------------------------
8310 -- Get_Incomplete_View_Of_Ancestor --
8311 -------------------------------------
8312
8313 function Get_Incomplete_View_Of_Ancestor (E : Entity_Id) return Entity_Id is
8314 Cur_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
8315 Par_Scope : Entity_Id;
8316 Par_Type : Entity_Id;
8317
8318 begin
8319 -- The incomplete view of an ancestor is only relevant for private
8320 -- derived types in child units.
8321
8322 if not Is_Derived_Type (E)
8323 or else not Is_Child_Unit (Cur_Unit)
8324 then
8325 return Empty;
8326
8327 else
8328 Par_Scope := Scope (Cur_Unit);
8329 if No (Par_Scope) then
8330 return Empty;
8331 end if;
8332
8333 Par_Type := Etype (Base_Type (E));
8334
8335 -- Traverse list of ancestor types until we find one declared in
8336 -- a parent or grandparent unit (two levels seem sufficient).
8337
8338 while Present (Par_Type) loop
8339 if Scope (Par_Type) = Par_Scope
8340 or else Scope (Par_Type) = Scope (Par_Scope)
8341 then
8342 return Par_Type;
8343
8344 elsif not Is_Derived_Type (Par_Type) then
8345 return Empty;
8346
8347 else
8348 Par_Type := Etype (Base_Type (Par_Type));
8349 end if;
8350 end loop;
8351
8352 -- If none found, there is no relevant ancestor type.
8353
8354 return Empty;
8355 end if;
8356 end Get_Incomplete_View_Of_Ancestor;
8357
8358 ----------------------
8359 -- Get_Index_Bounds --
8360 ----------------------
8361
8362 procedure Get_Index_Bounds
8363 (N : Node_Id;
8364 L : out Node_Id;
8365 H : out Node_Id;
8366 Use_Full_View : Boolean := False)
8367 is
8368 function Scalar_Range_Of_Type (Typ : Entity_Id) return Node_Id;
8369 -- Obtain the scalar range of type Typ. If flag Use_Full_View is set and
8370 -- Typ qualifies, the scalar range is obtained from the full view of the
8371 -- type.
8372
8373 --------------------------
8374 -- Scalar_Range_Of_Type --
8375 --------------------------
8376
8377 function Scalar_Range_Of_Type (Typ : Entity_Id) return Node_Id is
8378 T : Entity_Id := Typ;
8379
8380 begin
8381 if Use_Full_View and then Present (Full_View (T)) then
8382 T := Full_View (T);
8383 end if;
8384
8385 return Scalar_Range (T);
8386 end Scalar_Range_Of_Type;
8387
8388 -- Local variables
8389
8390 Kind : constant Node_Kind := Nkind (N);
8391 Rng : Node_Id;
8392
8393 -- Start of processing for Get_Index_Bounds
8394
8395 begin
8396 if Kind = N_Range then
8397 L := Low_Bound (N);
8398 H := High_Bound (N);
8399
8400 elsif Kind = N_Subtype_Indication then
8401 Rng := Range_Expression (Constraint (N));
8402
8403 if Rng = Error then
8404 L := Error;
8405 H := Error;
8406 return;
8407
8408 else
8409 L := Low_Bound (Range_Expression (Constraint (N)));
8410 H := High_Bound (Range_Expression (Constraint (N)));
8411 end if;
8412
8413 elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
8414 Rng := Scalar_Range_Of_Type (Entity (N));
8415
8416 if Error_Posted (Rng) then
8417 L := Error;
8418 H := Error;
8419
8420 elsif Nkind (Rng) = N_Subtype_Indication then
8421 Get_Index_Bounds (Rng, L, H);
8422
8423 else
8424 L := Low_Bound (Rng);
8425 H := High_Bound (Rng);
8426 end if;
8427
8428 else
8429 -- N is an expression, indicating a range with one value
8430
8431 L := N;
8432 H := N;
8433 end if;
8434 end Get_Index_Bounds;
8435
8436 -----------------------------
8437 -- Get_Interfacing_Aspects --
8438 -----------------------------
8439
8440 procedure Get_Interfacing_Aspects
8441 (Iface_Asp : Node_Id;
8442 Conv_Asp : out Node_Id;
8443 EN_Asp : out Node_Id;
8444 Expo_Asp : out Node_Id;
8445 Imp_Asp : out Node_Id;
8446 LN_Asp : out Node_Id;
8447 Do_Checks : Boolean := False)
8448 is
8449 procedure Save_Or_Duplication_Error
8450 (Asp : Node_Id;
8451 To : in out Node_Id);
8452 -- Save the value of aspect Asp in node To. If To already has a value,
8453 -- then this is considered a duplicate use of aspect. Emit an error if
8454 -- flag Do_Checks is set.
8455
8456 -------------------------------
8457 -- Save_Or_Duplication_Error --
8458 -------------------------------
8459
8460 procedure Save_Or_Duplication_Error
8461 (Asp : Node_Id;
8462 To : in out Node_Id)
8463 is
8464 begin
8465 -- Detect an extra aspect and issue an error
8466
8467 if Present (To) then
8468 if Do_Checks then
8469 Error_Msg_Name_1 := Chars (Identifier (Asp));
8470 Error_Msg_Sloc := Sloc (To);
8471 Error_Msg_N ("aspect % previously given #", Asp);
8472 end if;
8473
8474 -- Otherwise capture the aspect
8475
8476 else
8477 To := Asp;
8478 end if;
8479 end Save_Or_Duplication_Error;
8480
8481 -- Local variables
8482
8483 Asp : Node_Id;
8484 Asp_Id : Aspect_Id;
8485
8486 -- The following variables capture each individual aspect
8487
8488 Conv : Node_Id := Empty;
8489 EN : Node_Id := Empty;
8490 Expo : Node_Id := Empty;
8491 Imp : Node_Id := Empty;
8492 LN : Node_Id := Empty;
8493
8494 -- Start of processing for Get_Interfacing_Aspects
8495
8496 begin
8497 -- The input interfacing aspect should reside in an aspect specification
8498 -- list.
8499
8500 pragma Assert (Is_List_Member (Iface_Asp));
8501
8502 -- Examine the aspect specifications of the related entity. Find and
8503 -- capture all interfacing aspects. Detect duplicates and emit errors
8504 -- if applicable.
8505
8506 Asp := First (List_Containing (Iface_Asp));
8507 while Present (Asp) loop
8508 Asp_Id := Get_Aspect_Id (Asp);
8509
8510 if Asp_Id = Aspect_Convention then
8511 Save_Or_Duplication_Error (Asp, Conv);
8512
8513 elsif Asp_Id = Aspect_External_Name then
8514 Save_Or_Duplication_Error (Asp, EN);
8515
8516 elsif Asp_Id = Aspect_Export then
8517 Save_Or_Duplication_Error (Asp, Expo);
8518
8519 elsif Asp_Id = Aspect_Import then
8520 Save_Or_Duplication_Error (Asp, Imp);
8521
8522 elsif Asp_Id = Aspect_Link_Name then
8523 Save_Or_Duplication_Error (Asp, LN);
8524 end if;
8525
8526 Next (Asp);
8527 end loop;
8528
8529 Conv_Asp := Conv;
8530 EN_Asp := EN;
8531 Expo_Asp := Expo;
8532 Imp_Asp := Imp;
8533 LN_Asp := LN;
8534 end Get_Interfacing_Aspects;
8535
8536 ---------------------------------
8537 -- Get_Iterable_Type_Primitive --
8538 ---------------------------------
8539
8540 function Get_Iterable_Type_Primitive
8541 (Typ : Entity_Id;
8542 Nam : Name_Id) return Entity_Id
8543 is
8544 Funcs : constant Node_Id := Find_Value_Of_Aspect (Typ, Aspect_Iterable);
8545 Assoc : Node_Id;
8546
8547 begin
8548 if No (Funcs) then
8549 return Empty;
8550
8551 else
8552 Assoc := First (Component_Associations (Funcs));
8553 while Present (Assoc) loop
8554 if Chars (First (Choices (Assoc))) = Nam then
8555 return Entity (Expression (Assoc));
8556 end if;
8557
8558 Assoc := Next (Assoc);
8559 end loop;
8560
8561 return Empty;
8562 end if;
8563 end Get_Iterable_Type_Primitive;
8564
8565 ----------------------------------
8566 -- Get_Library_Unit_Name_string --
8567 ----------------------------------
8568
8569 procedure Get_Library_Unit_Name_String (Decl_Node : Node_Id) is
8570 Unit_Name_Id : constant Unit_Name_Type := Get_Unit_Name (Decl_Node);
8571
8572 begin
8573 Get_Unit_Name_String (Unit_Name_Id);
8574
8575 -- Remove seven last character (" (spec)" or " (body)")
8576
8577 Name_Len := Name_Len - 7;
8578 pragma Assert (Name_Buffer (Name_Len + 1) = ' ');
8579 end Get_Library_Unit_Name_String;
8580
8581 --------------------------
8582 -- Get_Max_Queue_Length --
8583 --------------------------
8584
8585 function Get_Max_Queue_Length (Id : Entity_Id) return Uint is
8586 pragma Assert (Is_Entry (Id));
8587 Prag : constant Entity_Id := Get_Pragma (Id, Pragma_Max_Queue_Length);
8588
8589 begin
8590 -- A value of 0 represents no maximum specified, and entries and entry
8591 -- families with no Max_Queue_Length aspect or pragma default to it.
8592
8593 if not Present (Prag) then
8594 return Uint_0;
8595 end if;
8596
8597 return Intval (Expression (First (Pragma_Argument_Associations (Prag))));
8598 end Get_Max_Queue_Length;
8599
8600 ------------------------
8601 -- Get_Name_Entity_Id --
8602 ------------------------
8603
8604 function Get_Name_Entity_Id (Id : Name_Id) return Entity_Id is
8605 begin
8606 return Entity_Id (Get_Name_Table_Int (Id));
8607 end Get_Name_Entity_Id;
8608
8609 ------------------------------
8610 -- Get_Name_From_CTC_Pragma --
8611 ------------------------------
8612
8613 function Get_Name_From_CTC_Pragma (N : Node_Id) return String_Id is
8614 Arg : constant Node_Id :=
8615 Get_Pragma_Arg (First (Pragma_Argument_Associations (N)));
8616 begin
8617 return Strval (Expr_Value_S (Arg));
8618 end Get_Name_From_CTC_Pragma;
8619
8620 -----------------------
8621 -- Get_Parent_Entity --
8622 -----------------------
8623
8624 function Get_Parent_Entity (Unit : Node_Id) return Entity_Id is
8625 begin
8626 if Nkind (Unit) = N_Package_Body
8627 and then Nkind (Original_Node (Unit)) = N_Package_Instantiation
8628 then
8629 return Defining_Entity
8630 (Specification (Instance_Spec (Original_Node (Unit))));
8631 elsif Nkind (Unit) = N_Package_Instantiation then
8632 return Defining_Entity (Specification (Instance_Spec (Unit)));
8633 else
8634 return Defining_Entity (Unit);
8635 end if;
8636 end Get_Parent_Entity;
8637
8638 -------------------
8639 -- Get_Pragma_Id --
8640 -------------------
8641
8642 function Get_Pragma_Id (N : Node_Id) return Pragma_Id is
8643 begin
8644 return Get_Pragma_Id (Pragma_Name_Unmapped (N));
8645 end Get_Pragma_Id;
8646
8647 ------------------------
8648 -- Get_Qualified_Name --
8649 ------------------------
8650
8651 function Get_Qualified_Name
8652 (Id : Entity_Id;
8653 Suffix : Entity_Id := Empty) return Name_Id
8654 is
8655 Suffix_Nam : Name_Id := No_Name;
8656
8657 begin
8658 if Present (Suffix) then
8659 Suffix_Nam := Chars (Suffix);
8660 end if;
8661
8662 return Get_Qualified_Name (Chars (Id), Suffix_Nam, Scope (Id));
8663 end Get_Qualified_Name;
8664
8665 function Get_Qualified_Name
8666 (Nam : Name_Id;
8667 Suffix : Name_Id := No_Name;
8668 Scop : Entity_Id := Current_Scope) return Name_Id
8669 is
8670 procedure Add_Scope (S : Entity_Id);
8671 -- Add the fully qualified form of scope S to the name buffer. The
8672 -- format is:
8673 -- s-1__s__
8674
8675 ---------------
8676 -- Add_Scope --
8677 ---------------
8678
8679 procedure Add_Scope (S : Entity_Id) is
8680 begin
8681 if S = Empty then
8682 null;
8683
8684 elsif S = Standard_Standard then
8685 null;
8686
8687 else
8688 Add_Scope (Scope (S));
8689 Get_Name_String_And_Append (Chars (S));
8690 Add_Str_To_Name_Buffer ("__");
8691 end if;
8692 end Add_Scope;
8693
8694 -- Start of processing for Get_Qualified_Name
8695
8696 begin
8697 Name_Len := 0;
8698 Add_Scope (Scop);
8699
8700 -- Append the base name after all scopes have been chained
8701
8702 Get_Name_String_And_Append (Nam);
8703
8704 -- Append the suffix (if present)
8705
8706 if Suffix /= No_Name then
8707 Add_Str_To_Name_Buffer ("__");
8708 Get_Name_String_And_Append (Suffix);
8709 end if;
8710
8711 return Name_Find;
8712 end Get_Qualified_Name;
8713
8714 -----------------------
8715 -- Get_Reason_String --
8716 -----------------------
8717
8718 procedure Get_Reason_String (N : Node_Id) is
8719 begin
8720 if Nkind (N) = N_String_Literal then
8721 Store_String_Chars (Strval (N));
8722
8723 elsif Nkind (N) = N_Op_Concat then
8724 Get_Reason_String (Left_Opnd (N));
8725 Get_Reason_String (Right_Opnd (N));
8726
8727 -- If not of required form, error
8728
8729 else
8730 Error_Msg_N
8731 ("Reason for pragma Warnings has wrong form", N);
8732 Error_Msg_N
8733 ("\must be string literal or concatenation of string literals", N);
8734 return;
8735 end if;
8736 end Get_Reason_String;
8737
8738 --------------------------------
8739 -- Get_Reference_Discriminant --
8740 --------------------------------
8741
8742 function Get_Reference_Discriminant (Typ : Entity_Id) return Entity_Id is
8743 D : Entity_Id;
8744
8745 begin
8746 D := First_Discriminant (Typ);
8747 while Present (D) loop
8748 if Has_Implicit_Dereference (D) then
8749 return D;
8750 end if;
8751 Next_Discriminant (D);
8752 end loop;
8753
8754 return Empty;
8755 end Get_Reference_Discriminant;
8756
8757 ---------------------------
8758 -- Get_Referenced_Object --
8759 ---------------------------
8760
8761 function Get_Referenced_Object (N : Node_Id) return Node_Id is
8762 R : Node_Id;
8763
8764 begin
8765 R := N;
8766 while Is_Entity_Name (R)
8767 and then Present (Renamed_Object (Entity (R)))
8768 loop
8769 R := Renamed_Object (Entity (R));
8770 end loop;
8771
8772 return R;
8773 end Get_Referenced_Object;
8774
8775 ------------------------
8776 -- Get_Renamed_Entity --
8777 ------------------------
8778
8779 function Get_Renamed_Entity (E : Entity_Id) return Entity_Id is
8780 R : Entity_Id;
8781
8782 begin
8783 R := E;
8784 while Present (Renamed_Entity (R)) loop
8785 R := Renamed_Entity (R);
8786 end loop;
8787
8788 return R;
8789 end Get_Renamed_Entity;
8790
8791 -----------------------
8792 -- Get_Return_Object --
8793 -----------------------
8794
8795 function Get_Return_Object (N : Node_Id) return Entity_Id is
8796 Decl : Node_Id;
8797
8798 begin
8799 Decl := First (Return_Object_Declarations (N));
8800 while Present (Decl) loop
8801 exit when Nkind (Decl) = N_Object_Declaration
8802 and then Is_Return_Object (Defining_Identifier (Decl));
8803 Next (Decl);
8804 end loop;
8805
8806 pragma Assert (Present (Decl));
8807 return Defining_Identifier (Decl);
8808 end Get_Return_Object;
8809
8810 ---------------------------
8811 -- Get_Subprogram_Entity --
8812 ---------------------------
8813
8814 function Get_Subprogram_Entity (Nod : Node_Id) return Entity_Id is
8815 Subp : Node_Id;
8816 Subp_Id : Entity_Id;
8817
8818 begin
8819 if Nkind (Nod) = N_Accept_Statement then
8820 Subp := Entry_Direct_Name (Nod);
8821
8822 elsif Nkind (Nod) = N_Slice then
8823 Subp := Prefix (Nod);
8824
8825 else
8826 Subp := Name (Nod);
8827 end if;
8828
8829 -- Strip the subprogram call
8830
8831 loop
8832 if Nkind_In (Subp, N_Explicit_Dereference,
8833 N_Indexed_Component,
8834 N_Selected_Component)
8835 then
8836 Subp := Prefix (Subp);
8837
8838 elsif Nkind_In (Subp, N_Type_Conversion,
8839 N_Unchecked_Type_Conversion)
8840 then
8841 Subp := Expression (Subp);
8842
8843 else
8844 exit;
8845 end if;
8846 end loop;
8847
8848 -- Extract the entity of the subprogram call
8849
8850 if Is_Entity_Name (Subp) then
8851 Subp_Id := Entity (Subp);
8852
8853 if Ekind (Subp_Id) = E_Access_Subprogram_Type then
8854 Subp_Id := Directly_Designated_Type (Subp_Id);
8855 end if;
8856
8857 if Is_Subprogram (Subp_Id) then
8858 return Subp_Id;
8859 else
8860 return Empty;
8861 end if;
8862
8863 -- The search did not find a construct that denotes a subprogram
8864
8865 else
8866 return Empty;
8867 end if;
8868 end Get_Subprogram_Entity;
8869
8870 -----------------------------
8871 -- Get_Task_Body_Procedure --
8872 -----------------------------
8873
8874 function Get_Task_Body_Procedure (E : Entity_Id) return Node_Id is
8875 begin
8876 -- Note: A task type may be the completion of a private type with
8877 -- discriminants. When performing elaboration checks on a task
8878 -- declaration, the current view of the type may be the private one,
8879 -- and the procedure that holds the body of the task is held in its
8880 -- underlying type.
8881
8882 -- This is an odd function, why not have Task_Body_Procedure do
8883 -- the following digging???
8884
8885 return Task_Body_Procedure (Underlying_Type (Root_Type (E)));
8886 end Get_Task_Body_Procedure;
8887
8888 -------------------------
8889 -- Get_User_Defined_Eq --
8890 -------------------------
8891
8892 function Get_User_Defined_Eq (E : Entity_Id) return Entity_Id is
8893 Prim : Elmt_Id;
8894 Op : Entity_Id;
8895
8896 begin
8897 Prim := First_Elmt (Collect_Primitive_Operations (E));
8898 while Present (Prim) loop
8899 Op := Node (Prim);
8900
8901 if Chars (Op) = Name_Op_Eq
8902 and then Etype (Op) = Standard_Boolean
8903 and then Etype (First_Formal (Op)) = E
8904 and then Etype (Next_Formal (First_Formal (Op))) = E
8905 then
8906 return Op;
8907 end if;
8908
8909 Next_Elmt (Prim);
8910 end loop;
8911
8912 return Empty;
8913 end Get_User_Defined_Eq;
8914
8915 ---------------
8916 -- Get_Views --
8917 ---------------
8918
8919 procedure Get_Views
8920 (Typ : Entity_Id;
8921 Priv_Typ : out Entity_Id;
8922 Full_Typ : out Entity_Id;
8923 Full_Base : out Entity_Id;
8924 CRec_Typ : out Entity_Id)
8925 is
8926 IP_View : Entity_Id;
8927
8928 begin
8929 -- Assume that none of the views can be recovered
8930
8931 Priv_Typ := Empty;
8932 Full_Typ := Empty;
8933 Full_Base := Empty;
8934 CRec_Typ := Empty;
8935
8936 -- The input type is the corresponding record type of a protected or a
8937 -- task type.
8938
8939 if Ekind (Typ) = E_Record_Type
8940 and then Is_Concurrent_Record_Type (Typ)
8941 then
8942 CRec_Typ := Typ;
8943 Full_Typ := Corresponding_Concurrent_Type (CRec_Typ);
8944 Full_Base := Base_Type (Full_Typ);
8945 Priv_Typ := Incomplete_Or_Partial_View (Full_Typ);
8946
8947 -- Otherwise the input type denotes an arbitrary type
8948
8949 else
8950 IP_View := Incomplete_Or_Partial_View (Typ);
8951
8952 -- The input type denotes the full view of a private type
8953
8954 if Present (IP_View) then
8955 Priv_Typ := IP_View;
8956 Full_Typ := Typ;
8957
8958 -- The input type is a private type
8959
8960 elsif Is_Private_Type (Typ) then
8961 Priv_Typ := Typ;
8962 Full_Typ := Full_View (Priv_Typ);
8963
8964 -- Otherwise the input type does not have any views
8965
8966 else
8967 Full_Typ := Typ;
8968 end if;
8969
8970 if Present (Full_Typ) then
8971 Full_Base := Base_Type (Full_Typ);
8972
8973 if Ekind_In (Full_Typ, E_Protected_Type, E_Task_Type) then
8974 CRec_Typ := Corresponding_Record_Type (Full_Typ);
8975 end if;
8976 end if;
8977 end if;
8978 end Get_Views;
8979
8980 -----------------------
8981 -- Has_Access_Values --
8982 -----------------------
8983
8984 function Has_Access_Values (T : Entity_Id) return Boolean is
8985 Typ : constant Entity_Id := Underlying_Type (T);
8986
8987 begin
8988 -- Case of a private type which is not completed yet. This can only
8989 -- happen in the case of a generic format type appearing directly, or
8990 -- as a component of the type to which this function is being applied
8991 -- at the top level. Return False in this case, since we certainly do
8992 -- not know that the type contains access types.
8993
8994 if No (Typ) then
8995 return False;
8996
8997 elsif Is_Access_Type (Typ) then
8998 return True;
8999
9000 elsif Is_Array_Type (Typ) then
9001 return Has_Access_Values (Component_Type (Typ));
9002
9003 elsif Is_Record_Type (Typ) then
9004 declare
9005 Comp : Entity_Id;
9006
9007 begin
9008 -- Loop to Check components
9009
9010 Comp := First_Component_Or_Discriminant (Typ);
9011 while Present (Comp) loop
9012
9013 -- Check for access component, tag field does not count, even
9014 -- though it is implemented internally using an access type.
9015
9016 if Has_Access_Values (Etype (Comp))
9017 and then Chars (Comp) /= Name_uTag
9018 then
9019 return True;
9020 end if;
9021
9022 Next_Component_Or_Discriminant (Comp);
9023 end loop;
9024 end;
9025
9026 return False;
9027
9028 else
9029 return False;
9030 end if;
9031 end Has_Access_Values;
9032
9033 ------------------------------
9034 -- Has_Compatible_Alignment --
9035 ------------------------------
9036
9037 function Has_Compatible_Alignment
9038 (Obj : Entity_Id;
9039 Expr : Node_Id;
9040 Layout_Done : Boolean) return Alignment_Result
9041 is
9042 function Has_Compatible_Alignment_Internal
9043 (Obj : Entity_Id;
9044 Expr : Node_Id;
9045 Layout_Done : Boolean;
9046 Default : Alignment_Result) return Alignment_Result;
9047 -- This is the internal recursive function that actually does the work.
9048 -- There is one additional parameter, which says what the result should
9049 -- be if no alignment information is found, and there is no definite
9050 -- indication of compatible alignments. At the outer level, this is set
9051 -- to Unknown, but for internal recursive calls in the case where types
9052 -- are known to be correct, it is set to Known_Compatible.
9053
9054 ---------------------------------------
9055 -- Has_Compatible_Alignment_Internal --
9056 ---------------------------------------
9057
9058 function Has_Compatible_Alignment_Internal
9059 (Obj : Entity_Id;
9060 Expr : Node_Id;
9061 Layout_Done : Boolean;
9062 Default : Alignment_Result) return Alignment_Result
9063 is
9064 Result : Alignment_Result := Known_Compatible;
9065 -- Holds the current status of the result. Note that once a value of
9066 -- Known_Incompatible is set, it is sticky and does not get changed
9067 -- to Unknown (the value in Result only gets worse as we go along,
9068 -- never better).
9069
9070 Offs : Uint := No_Uint;
9071 -- Set to a factor of the offset from the base object when Expr is a
9072 -- selected or indexed component, based on Component_Bit_Offset and
9073 -- Component_Size respectively. A negative value is used to represent
9074 -- a value which is not known at compile time.
9075
9076 procedure Check_Prefix;
9077 -- Checks the prefix recursively in the case where the expression
9078 -- is an indexed or selected component.
9079
9080 procedure Set_Result (R : Alignment_Result);
9081 -- If R represents a worse outcome (unknown instead of known
9082 -- compatible, or known incompatible), then set Result to R.
9083
9084 ------------------
9085 -- Check_Prefix --
9086 ------------------
9087
9088 procedure Check_Prefix is
9089 begin
9090 -- The subtlety here is that in doing a recursive call to check
9091 -- the prefix, we have to decide what to do in the case where we
9092 -- don't find any specific indication of an alignment problem.
9093
9094 -- At the outer level, we normally set Unknown as the result in
9095 -- this case, since we can only set Known_Compatible if we really
9096 -- know that the alignment value is OK, but for the recursive
9097 -- call, in the case where the types match, and we have not
9098 -- specified a peculiar alignment for the object, we are only
9099 -- concerned about suspicious rep clauses, the default case does
9100 -- not affect us, since the compiler will, in the absence of such
9101 -- rep clauses, ensure that the alignment is correct.
9102
9103 if Default = Known_Compatible
9104 or else
9105 (Etype (Obj) = Etype (Expr)
9106 and then (Unknown_Alignment (Obj)
9107 or else
9108 Alignment (Obj) = Alignment (Etype (Obj))))
9109 then
9110 Set_Result
9111 (Has_Compatible_Alignment_Internal
9112 (Obj, Prefix (Expr), Layout_Done, Known_Compatible));
9113
9114 -- In all other cases, we need a full check on the prefix
9115
9116 else
9117 Set_Result
9118 (Has_Compatible_Alignment_Internal
9119 (Obj, Prefix (Expr), Layout_Done, Unknown));
9120 end if;
9121 end Check_Prefix;
9122
9123 ----------------
9124 -- Set_Result --
9125 ----------------
9126
9127 procedure Set_Result (R : Alignment_Result) is
9128 begin
9129 if R > Result then
9130 Result := R;
9131 end if;
9132 end Set_Result;
9133
9134 -- Start of processing for Has_Compatible_Alignment_Internal
9135
9136 begin
9137 -- If Expr is a selected component, we must make sure there is no
9138 -- potentially troublesome component clause and that the record is
9139 -- not packed if the layout is not done.
9140
9141 if Nkind (Expr) = N_Selected_Component then
9142
9143 -- Packing generates unknown alignment if layout is not done
9144
9145 if Is_Packed (Etype (Prefix (Expr))) and then not Layout_Done then
9146 Set_Result (Unknown);
9147 end if;
9148
9149 -- Check prefix and component offset
9150
9151 Check_Prefix;
9152 Offs := Component_Bit_Offset (Entity (Selector_Name (Expr)));
9153
9154 -- If Expr is an indexed component, we must make sure there is no
9155 -- potentially troublesome Component_Size clause and that the array
9156 -- is not bit-packed if the layout is not done.
9157
9158 elsif Nkind (Expr) = N_Indexed_Component then
9159 declare
9160 Typ : constant Entity_Id := Etype (Prefix (Expr));
9161
9162 begin
9163 -- Packing generates unknown alignment if layout is not done
9164
9165 if Is_Bit_Packed_Array (Typ) and then not Layout_Done then
9166 Set_Result (Unknown);
9167 end if;
9168
9169 -- Check prefix and component offset (or at least size)
9170
9171 Check_Prefix;
9172 Offs := Indexed_Component_Bit_Offset (Expr);
9173 if Offs = No_Uint then
9174 Offs := Component_Size (Typ);
9175 end if;
9176 end;
9177 end if;
9178
9179 -- If we have a null offset, the result is entirely determined by
9180 -- the base object and has already been computed recursively.
9181
9182 if Offs = Uint_0 then
9183 null;
9184
9185 -- Case where we know the alignment of the object
9186
9187 elsif Known_Alignment (Obj) then
9188 declare
9189 ObjA : constant Uint := Alignment (Obj);
9190 ExpA : Uint := No_Uint;
9191 SizA : Uint := No_Uint;
9192
9193 begin
9194 -- If alignment of Obj is 1, then we are always OK
9195
9196 if ObjA = 1 then
9197 Set_Result (Known_Compatible);
9198
9199 -- Alignment of Obj is greater than 1, so we need to check
9200
9201 else
9202 -- If we have an offset, see if it is compatible
9203
9204 if Offs /= No_Uint and Offs > Uint_0 then
9205 if Offs mod (System_Storage_Unit * ObjA) /= 0 then
9206 Set_Result (Known_Incompatible);
9207 end if;
9208
9209 -- See if Expr is an object with known alignment
9210
9211 elsif Is_Entity_Name (Expr)
9212 and then Known_Alignment (Entity (Expr))
9213 then
9214 ExpA := Alignment (Entity (Expr));
9215
9216 -- Otherwise, we can use the alignment of the type of
9217 -- Expr given that we already checked for
9218 -- discombobulating rep clauses for the cases of indexed
9219 -- and selected components above.
9220
9221 elsif Known_Alignment (Etype (Expr)) then
9222 ExpA := Alignment (Etype (Expr));
9223
9224 -- Otherwise the alignment is unknown
9225
9226 else
9227 Set_Result (Default);
9228 end if;
9229
9230 -- If we got an alignment, see if it is acceptable
9231
9232 if ExpA /= No_Uint and then ExpA < ObjA then
9233 Set_Result (Known_Incompatible);
9234 end if;
9235
9236 -- If Expr is not a piece of a larger object, see if size
9237 -- is given. If so, check that it is not too small for the
9238 -- required alignment.
9239
9240 if Offs /= No_Uint then
9241 null;
9242
9243 -- See if Expr is an object with known size
9244
9245 elsif Is_Entity_Name (Expr)
9246 and then Known_Static_Esize (Entity (Expr))
9247 then
9248 SizA := Esize (Entity (Expr));
9249
9250 -- Otherwise, we check the object size of the Expr type
9251
9252 elsif Known_Static_Esize (Etype (Expr)) then
9253 SizA := Esize (Etype (Expr));
9254 end if;
9255
9256 -- If we got a size, see if it is a multiple of the Obj
9257 -- alignment, if not, then the alignment cannot be
9258 -- acceptable, since the size is always a multiple of the
9259 -- alignment.
9260
9261 if SizA /= No_Uint then
9262 if SizA mod (ObjA * Ttypes.System_Storage_Unit) /= 0 then
9263 Set_Result (Known_Incompatible);
9264 end if;
9265 end if;
9266 end if;
9267 end;
9268
9269 -- If we do not know required alignment, any non-zero offset is a
9270 -- potential problem (but certainly may be OK, so result is unknown).
9271
9272 elsif Offs /= No_Uint then
9273 Set_Result (Unknown);
9274
9275 -- If we can't find the result by direct comparison of alignment
9276 -- values, then there is still one case that we can determine known
9277 -- result, and that is when we can determine that the types are the
9278 -- same, and no alignments are specified. Then we known that the
9279 -- alignments are compatible, even if we don't know the alignment
9280 -- value in the front end.
9281
9282 elsif Etype (Obj) = Etype (Expr) then
9283
9284 -- Types are the same, but we have to check for possible size
9285 -- and alignments on the Expr object that may make the alignment
9286 -- different, even though the types are the same.
9287
9288 if Is_Entity_Name (Expr) then
9289
9290 -- First check alignment of the Expr object. Any alignment less
9291 -- than Maximum_Alignment is worrisome since this is the case
9292 -- where we do not know the alignment of Obj.
9293
9294 if Known_Alignment (Entity (Expr))
9295 and then UI_To_Int (Alignment (Entity (Expr))) <
9296 Ttypes.Maximum_Alignment
9297 then
9298 Set_Result (Unknown);
9299
9300 -- Now check size of Expr object. Any size that is not an
9301 -- even multiple of Maximum_Alignment is also worrisome
9302 -- since it may cause the alignment of the object to be less
9303 -- than the alignment of the type.
9304
9305 elsif Known_Static_Esize (Entity (Expr))
9306 and then
9307 (UI_To_Int (Esize (Entity (Expr))) mod
9308 (Ttypes.Maximum_Alignment * Ttypes.System_Storage_Unit))
9309 /= 0
9310 then
9311 Set_Result (Unknown);
9312
9313 -- Otherwise same type is decisive
9314
9315 else
9316 Set_Result (Known_Compatible);
9317 end if;
9318 end if;
9319
9320 -- Another case to deal with is when there is an explicit size or
9321 -- alignment clause when the types are not the same. If so, then the
9322 -- result is Unknown. We don't need to do this test if the Default is
9323 -- Unknown, since that result will be set in any case.
9324
9325 elsif Default /= Unknown
9326 and then (Has_Size_Clause (Etype (Expr))
9327 or else
9328 Has_Alignment_Clause (Etype (Expr)))
9329 then
9330 Set_Result (Unknown);
9331
9332 -- If no indication found, set default
9333
9334 else
9335 Set_Result (Default);
9336 end if;
9337
9338 -- Return worst result found
9339
9340 return Result;
9341 end Has_Compatible_Alignment_Internal;
9342
9343 -- Start of processing for Has_Compatible_Alignment
9344
9345 begin
9346 -- If Obj has no specified alignment, then set alignment from the type
9347 -- alignment. Perhaps we should always do this, but for sure we should
9348 -- do it when there is an address clause since we can do more if the
9349 -- alignment is known.
9350
9351 if Unknown_Alignment (Obj) then
9352 Set_Alignment (Obj, Alignment (Etype (Obj)));
9353 end if;
9354
9355 -- Now do the internal call that does all the work
9356
9357 return
9358 Has_Compatible_Alignment_Internal (Obj, Expr, Layout_Done, Unknown);
9359 end Has_Compatible_Alignment;
9360
9361 ----------------------
9362 -- Has_Declarations --
9363 ----------------------
9364
9365 function Has_Declarations (N : Node_Id) return Boolean is
9366 begin
9367 return Nkind_In (Nkind (N), N_Accept_Statement,
9368 N_Block_Statement,
9369 N_Compilation_Unit_Aux,
9370 N_Entry_Body,
9371 N_Package_Body,
9372 N_Protected_Body,
9373 N_Subprogram_Body,
9374 N_Task_Body,
9375 N_Package_Specification);
9376 end Has_Declarations;
9377
9378 ---------------------------------
9379 -- Has_Defaulted_Discriminants --
9380 ---------------------------------
9381
9382 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean is
9383 begin
9384 return Has_Discriminants (Typ)
9385 and then Present (First_Discriminant (Typ))
9386 and then Present (Discriminant_Default_Value
9387 (First_Discriminant (Typ)));
9388 end Has_Defaulted_Discriminants;
9389
9390 -------------------
9391 -- Has_Denormals --
9392 -------------------
9393
9394 function Has_Denormals (E : Entity_Id) return Boolean is
9395 begin
9396 return Is_Floating_Point_Type (E) and then Denorm_On_Target;
9397 end Has_Denormals;
9398
9399 -------------------------------------------
9400 -- Has_Discriminant_Dependent_Constraint --
9401 -------------------------------------------
9402
9403 function Has_Discriminant_Dependent_Constraint
9404 (Comp : Entity_Id) return Boolean
9405 is
9406 Comp_Decl : constant Node_Id := Parent (Comp);
9407 Subt_Indic : Node_Id;
9408 Constr : Node_Id;
9409 Assn : Node_Id;
9410
9411 begin
9412 -- Discriminants can't depend on discriminants
9413
9414 if Ekind (Comp) = E_Discriminant then
9415 return False;
9416
9417 else
9418 Subt_Indic := Subtype_Indication (Component_Definition (Comp_Decl));
9419
9420 if Nkind (Subt_Indic) = N_Subtype_Indication then
9421 Constr := Constraint (Subt_Indic);
9422
9423 if Nkind (Constr) = N_Index_Or_Discriminant_Constraint then
9424 Assn := First (Constraints (Constr));
9425 while Present (Assn) loop
9426 case Nkind (Assn) is
9427 when N_Identifier
9428 | N_Range
9429 | N_Subtype_Indication
9430 =>
9431 if Depends_On_Discriminant (Assn) then
9432 return True;
9433 end if;
9434
9435 when N_Discriminant_Association =>
9436 if Depends_On_Discriminant (Expression (Assn)) then
9437 return True;
9438 end if;
9439
9440 when others =>
9441 null;
9442 end case;
9443
9444 Next (Assn);
9445 end loop;
9446 end if;
9447 end if;
9448 end if;
9449
9450 return False;
9451 end Has_Discriminant_Dependent_Constraint;
9452
9453 --------------------------------------
9454 -- Has_Effectively_Volatile_Profile --
9455 --------------------------------------
9456
9457 function Has_Effectively_Volatile_Profile
9458 (Subp_Id : Entity_Id) return Boolean
9459 is
9460 Formal : Entity_Id;
9461
9462 begin
9463 -- Inspect the formal parameters looking for an effectively volatile
9464 -- type.
9465
9466 Formal := First_Formal (Subp_Id);
9467 while Present (Formal) loop
9468 if Is_Effectively_Volatile (Etype (Formal)) then
9469 return True;
9470 end if;
9471
9472 Next_Formal (Formal);
9473 end loop;
9474
9475 -- Inspect the return type of functions
9476
9477 if Ekind_In (Subp_Id, E_Function, E_Generic_Function)
9478 and then Is_Effectively_Volatile (Etype (Subp_Id))
9479 then
9480 return True;
9481 end if;
9482
9483 return False;
9484 end Has_Effectively_Volatile_Profile;
9485
9486 --------------------------
9487 -- Has_Enabled_Property --
9488 --------------------------
9489
9490 function Has_Enabled_Property
9491 (Item_Id : Entity_Id;
9492 Property : Name_Id) return Boolean
9493 is
9494 function Protected_Object_Has_Enabled_Property return Boolean;
9495 -- Determine whether a protected object denoted by Item_Id has the
9496 -- property enabled.
9497
9498 function State_Has_Enabled_Property return Boolean;
9499 -- Determine whether a state denoted by Item_Id has the property enabled
9500
9501 function Variable_Has_Enabled_Property return Boolean;
9502 -- Determine whether a variable denoted by Item_Id has the property
9503 -- enabled.
9504
9505 -------------------------------------------
9506 -- Protected_Object_Has_Enabled_Property --
9507 -------------------------------------------
9508
9509 function Protected_Object_Has_Enabled_Property return Boolean is
9510 Constits : constant Elist_Id := Part_Of_Constituents (Item_Id);
9511 Constit_Elmt : Elmt_Id;
9512 Constit_Id : Entity_Id;
9513
9514 begin
9515 -- Protected objects always have the properties Async_Readers and
9516 -- Async_Writers (SPARK RM 7.1.2(16)).
9517
9518 if Property = Name_Async_Readers
9519 or else Property = Name_Async_Writers
9520 then
9521 return True;
9522
9523 -- Protected objects that have Part_Of components also inherit their
9524 -- properties Effective_Reads and Effective_Writes
9525 -- (SPARK RM 7.1.2(16)).
9526
9527 elsif Present (Constits) then
9528 Constit_Elmt := First_Elmt (Constits);
9529 while Present (Constit_Elmt) loop
9530 Constit_Id := Node (Constit_Elmt);
9531
9532 if Has_Enabled_Property (Constit_Id, Property) then
9533 return True;
9534 end if;
9535
9536 Next_Elmt (Constit_Elmt);
9537 end loop;
9538 end if;
9539
9540 return False;
9541 end Protected_Object_Has_Enabled_Property;
9542
9543 --------------------------------
9544 -- State_Has_Enabled_Property --
9545 --------------------------------
9546
9547 function State_Has_Enabled_Property return Boolean is
9548 Decl : constant Node_Id := Parent (Item_Id);
9549 Opt : Node_Id;
9550 Opt_Nam : Node_Id;
9551 Prop : Node_Id;
9552 Prop_Nam : Node_Id;
9553 Props : Node_Id;
9554
9555 begin
9556 -- The declaration of an external abstract state appears as an
9557 -- extension aggregate. If this is not the case, properties can never
9558 -- be set.
9559
9560 if Nkind (Decl) /= N_Extension_Aggregate then
9561 return False;
9562 end if;
9563
9564 -- When External appears as a simple option, it automatically enables
9565 -- all properties.
9566
9567 Opt := First (Expressions (Decl));
9568 while Present (Opt) loop
9569 if Nkind (Opt) = N_Identifier
9570 and then Chars (Opt) = Name_External
9571 then
9572 return True;
9573 end if;
9574
9575 Next (Opt);
9576 end loop;
9577
9578 -- When External specifies particular properties, inspect those and
9579 -- find the desired one (if any).
9580
9581 Opt := First (Component_Associations (Decl));
9582 while Present (Opt) loop
9583 Opt_Nam := First (Choices (Opt));
9584
9585 if Nkind (Opt_Nam) = N_Identifier
9586 and then Chars (Opt_Nam) = Name_External
9587 then
9588 Props := Expression (Opt);
9589
9590 -- Multiple properties appear as an aggregate
9591
9592 if Nkind (Props) = N_Aggregate then
9593
9594 -- Simple property form
9595
9596 Prop := First (Expressions (Props));
9597 while Present (Prop) loop
9598 if Chars (Prop) = Property then
9599 return True;
9600 end if;
9601
9602 Next (Prop);
9603 end loop;
9604
9605 -- Property with expression form
9606
9607 Prop := First (Component_Associations (Props));
9608 while Present (Prop) loop
9609 Prop_Nam := First (Choices (Prop));
9610
9611 -- The property can be represented in two ways:
9612 -- others => <value>
9613 -- <property> => <value>
9614
9615 if Nkind (Prop_Nam) = N_Others_Choice
9616 or else (Nkind (Prop_Nam) = N_Identifier
9617 and then Chars (Prop_Nam) = Property)
9618 then
9619 return Is_True (Expr_Value (Expression (Prop)));
9620 end if;
9621
9622 Next (Prop);
9623 end loop;
9624
9625 -- Single property
9626
9627 else
9628 return Chars (Props) = Property;
9629 end if;
9630 end if;
9631
9632 Next (Opt);
9633 end loop;
9634
9635 return False;
9636 end State_Has_Enabled_Property;
9637
9638 -----------------------------------
9639 -- Variable_Has_Enabled_Property --
9640 -----------------------------------
9641
9642 function Variable_Has_Enabled_Property return Boolean is
9643 function Is_Enabled (Prag : Node_Id) return Boolean;
9644 -- Determine whether property pragma Prag (if present) denotes an
9645 -- enabled property.
9646
9647 ----------------
9648 -- Is_Enabled --
9649 ----------------
9650
9651 function Is_Enabled (Prag : Node_Id) return Boolean is
9652 Arg1 : Node_Id;
9653
9654 begin
9655 if Present (Prag) then
9656 Arg1 := First (Pragma_Argument_Associations (Prag));
9657
9658 -- The pragma has an optional Boolean expression, the related
9659 -- property is enabled only when the expression evaluates to
9660 -- True.
9661
9662 if Present (Arg1) then
9663 return Is_True (Expr_Value (Get_Pragma_Arg (Arg1)));
9664
9665 -- Otherwise the lack of expression enables the property by
9666 -- default.
9667
9668 else
9669 return True;
9670 end if;
9671
9672 -- The property was never set in the first place
9673
9674 else
9675 return False;
9676 end if;
9677 end Is_Enabled;
9678
9679 -- Local variables
9680
9681 AR : constant Node_Id :=
9682 Get_Pragma (Item_Id, Pragma_Async_Readers);
9683 AW : constant Node_Id :=
9684 Get_Pragma (Item_Id, Pragma_Async_Writers);
9685 ER : constant Node_Id :=
9686 Get_Pragma (Item_Id, Pragma_Effective_Reads);
9687 EW : constant Node_Id :=
9688 Get_Pragma (Item_Id, Pragma_Effective_Writes);
9689
9690 -- Start of processing for Variable_Has_Enabled_Property
9691
9692 begin
9693 -- A non-effectively volatile object can never possess external
9694 -- properties.
9695
9696 if not Is_Effectively_Volatile (Item_Id) then
9697 return False;
9698
9699 -- External properties related to variables come in two flavors -
9700 -- explicit and implicit. The explicit case is characterized by the
9701 -- presence of a property pragma with an optional Boolean flag. The
9702 -- property is enabled when the flag evaluates to True or the flag is
9703 -- missing altogether.
9704
9705 elsif Property = Name_Async_Readers and then Is_Enabled (AR) then
9706 return True;
9707
9708 elsif Property = Name_Async_Writers and then Is_Enabled (AW) then
9709 return True;
9710
9711 elsif Property = Name_Effective_Reads and then Is_Enabled (ER) then
9712 return True;
9713
9714 elsif Property = Name_Effective_Writes and then Is_Enabled (EW) then
9715 return True;
9716
9717 -- The implicit case lacks all property pragmas
9718
9719 elsif No (AR) and then No (AW) and then No (ER) and then No (EW) then
9720 if Is_Protected_Type (Etype (Item_Id)) then
9721 return Protected_Object_Has_Enabled_Property;
9722 else
9723 return True;
9724 end if;
9725
9726 else
9727 return False;
9728 end if;
9729 end Variable_Has_Enabled_Property;
9730
9731 -- Start of processing for Has_Enabled_Property
9732
9733 begin
9734 -- Abstract states and variables have a flexible scheme of specifying
9735 -- external properties.
9736
9737 if Ekind (Item_Id) = E_Abstract_State then
9738 return State_Has_Enabled_Property;
9739
9740 elsif Ekind (Item_Id) = E_Variable then
9741 return Variable_Has_Enabled_Property;
9742
9743 -- By default, protected objects only have the properties Async_Readers
9744 -- and Async_Writers. If they have Part_Of components, they also inherit
9745 -- their properties Effective_Reads and Effective_Writes
9746 -- (SPARK RM 7.1.2(16)).
9747
9748 elsif Ekind (Item_Id) = E_Protected_Object then
9749 return Protected_Object_Has_Enabled_Property;
9750
9751 -- Otherwise a property is enabled when the related item is effectively
9752 -- volatile.
9753
9754 else
9755 return Is_Effectively_Volatile (Item_Id);
9756 end if;
9757 end Has_Enabled_Property;
9758
9759 -------------------------------------
9760 -- Has_Full_Default_Initialization --
9761 -------------------------------------
9762
9763 function Has_Full_Default_Initialization (Typ : Entity_Id) return Boolean is
9764 Comp : Entity_Id;
9765 Prag : Node_Id;
9766
9767 begin
9768 -- A type subject to pragma Default_Initial_Condition is fully default
9769 -- initialized when the pragma appears with a non-null argument. Since
9770 -- any type may act as the full view of a private type, this check must
9771 -- be performed prior to the specialized tests below.
9772
9773 if Has_DIC (Typ) then
9774 Prag := Get_Pragma (Typ, Pragma_Default_Initial_Condition);
9775 pragma Assert (Present (Prag));
9776
9777 return Is_Verifiable_DIC_Pragma (Prag);
9778 end if;
9779
9780 -- A scalar type is fully default initialized if it is subject to aspect
9781 -- Default_Value.
9782
9783 if Is_Scalar_Type (Typ) then
9784 return Has_Default_Aspect (Typ);
9785
9786 -- An array type is fully default initialized if its element type is
9787 -- scalar and the array type carries aspect Default_Component_Value or
9788 -- the element type is fully default initialized.
9789
9790 elsif Is_Array_Type (Typ) then
9791 return
9792 Has_Default_Aspect (Typ)
9793 or else Has_Full_Default_Initialization (Component_Type (Typ));
9794
9795 -- A protected type, record type, or type extension is fully default
9796 -- initialized if all its components either carry an initialization
9797 -- expression or have a type that is fully default initialized. The
9798 -- parent type of a type extension must be fully default initialized.
9799
9800 elsif Is_Record_Type (Typ) or else Is_Protected_Type (Typ) then
9801
9802 -- Inspect all entities defined in the scope of the type, looking for
9803 -- uninitialized components.
9804
9805 Comp := First_Entity (Typ);
9806 while Present (Comp) loop
9807 if Ekind (Comp) = E_Component
9808 and then Comes_From_Source (Comp)
9809 and then No (Expression (Parent (Comp)))
9810 and then not Has_Full_Default_Initialization (Etype (Comp))
9811 then
9812 return False;
9813 end if;
9814
9815 Next_Entity (Comp);
9816 end loop;
9817
9818 -- Ensure that the parent type of a type extension is fully default
9819 -- initialized.
9820
9821 if Etype (Typ) /= Typ
9822 and then not Has_Full_Default_Initialization (Etype (Typ))
9823 then
9824 return False;
9825 end if;
9826
9827 -- If we get here, then all components and parent portion are fully
9828 -- default initialized.
9829
9830 return True;
9831
9832 -- A task type is fully default initialized by default
9833
9834 elsif Is_Task_Type (Typ) then
9835 return True;
9836
9837 -- Otherwise the type is not fully default initialized
9838
9839 else
9840 return False;
9841 end if;
9842 end Has_Full_Default_Initialization;
9843
9844 --------------------
9845 -- Has_Infinities --
9846 --------------------
9847
9848 function Has_Infinities (E : Entity_Id) return Boolean is
9849 begin
9850 return
9851 Is_Floating_Point_Type (E)
9852 and then Nkind (Scalar_Range (E)) = N_Range
9853 and then Includes_Infinities (Scalar_Range (E));
9854 end Has_Infinities;
9855
9856 --------------------
9857 -- Has_Interfaces --
9858 --------------------
9859
9860 function Has_Interfaces
9861 (T : Entity_Id;
9862 Use_Full_View : Boolean := True) return Boolean
9863 is
9864 Typ : Entity_Id := Base_Type (T);
9865
9866 begin
9867 -- Handle concurrent types
9868
9869 if Is_Concurrent_Type (Typ) then
9870 Typ := Corresponding_Record_Type (Typ);
9871 end if;
9872
9873 if not Present (Typ)
9874 or else not Is_Record_Type (Typ)
9875 or else not Is_Tagged_Type (Typ)
9876 then
9877 return False;
9878 end if;
9879
9880 -- Handle private types
9881
9882 if Use_Full_View and then Present (Full_View (Typ)) then
9883 Typ := Full_View (Typ);
9884 end if;
9885
9886 -- Handle concurrent record types
9887
9888 if Is_Concurrent_Record_Type (Typ)
9889 and then Is_Non_Empty_List (Abstract_Interface_List (Typ))
9890 then
9891 return True;
9892 end if;
9893
9894 loop
9895 if Is_Interface (Typ)
9896 or else
9897 (Is_Record_Type (Typ)
9898 and then Present (Interfaces (Typ))
9899 and then not Is_Empty_Elmt_List (Interfaces (Typ)))
9900 then
9901 return True;
9902 end if;
9903
9904 exit when Etype (Typ) = Typ
9905
9906 -- Handle private types
9907
9908 or else (Present (Full_View (Etype (Typ)))
9909 and then Full_View (Etype (Typ)) = Typ)
9910
9911 -- Protect frontend against wrong sources with cyclic derivations
9912
9913 or else Etype (Typ) = T;
9914
9915 -- Climb to the ancestor type handling private types
9916
9917 if Present (Full_View (Etype (Typ))) then
9918 Typ := Full_View (Etype (Typ));
9919 else
9920 Typ := Etype (Typ);
9921 end if;
9922 end loop;
9923
9924 return False;
9925 end Has_Interfaces;
9926
9927 --------------------------
9928 -- Has_Max_Queue_Length --
9929 --------------------------
9930
9931 function Has_Max_Queue_Length (Id : Entity_Id) return Boolean is
9932 begin
9933 return
9934 Ekind (Id) = E_Entry
9935 and then Present (Get_Pragma (Id, Pragma_Max_Queue_Length));
9936 end Has_Max_Queue_Length;
9937
9938 ---------------------------------
9939 -- Has_No_Obvious_Side_Effects --
9940 ---------------------------------
9941
9942 function Has_No_Obvious_Side_Effects (N : Node_Id) return Boolean is
9943 begin
9944 -- For now handle literals, constants, and non-volatile variables and
9945 -- expressions combining these with operators or short circuit forms.
9946
9947 if Nkind (N) in N_Numeric_Or_String_Literal then
9948 return True;
9949
9950 elsif Nkind (N) = N_Character_Literal then
9951 return True;
9952
9953 elsif Nkind (N) in N_Unary_Op then
9954 return Has_No_Obvious_Side_Effects (Right_Opnd (N));
9955
9956 elsif Nkind (N) in N_Binary_Op or else Nkind (N) in N_Short_Circuit then
9957 return Has_No_Obvious_Side_Effects (Left_Opnd (N))
9958 and then
9959 Has_No_Obvious_Side_Effects (Right_Opnd (N));
9960
9961 elsif Nkind (N) = N_Expression_With_Actions
9962 and then Is_Empty_List (Actions (N))
9963 then
9964 return Has_No_Obvious_Side_Effects (Expression (N));
9965
9966 elsif Nkind (N) in N_Has_Entity then
9967 return Present (Entity (N))
9968 and then Ekind_In (Entity (N), E_Variable,
9969 E_Constant,
9970 E_Enumeration_Literal,
9971 E_In_Parameter,
9972 E_Out_Parameter,
9973 E_In_Out_Parameter)
9974 and then not Is_Volatile (Entity (N));
9975
9976 else
9977 return False;
9978 end if;
9979 end Has_No_Obvious_Side_Effects;
9980
9981 -----------------------------
9982 -- Has_Non_Null_Refinement --
9983 -----------------------------
9984
9985 function Has_Non_Null_Refinement (Id : Entity_Id) return Boolean is
9986 Constits : Elist_Id;
9987
9988 begin
9989 pragma Assert (Ekind (Id) = E_Abstract_State);
9990 Constits := Refinement_Constituents (Id);
9991
9992 -- For a refinement to be non-null, the first constituent must be
9993 -- anything other than null.
9994
9995 return
9996 Present (Constits)
9997 and then Nkind (Node (First_Elmt (Constits))) /= N_Null;
9998 end Has_Non_Null_Refinement;
9999
10000 ----------------------------------
10001 -- Has_Non_Trivial_Precondition --
10002 ----------------------------------
10003
10004 function Has_Non_Trivial_Precondition (P : Entity_Id) return Boolean is
10005 Cont : constant Node_Id := Find_Aspect (P, Aspect_Pre);
10006 begin
10007 return Present (Cont)
10008 and then Class_Present (Cont)
10009 and then not Is_Entity_Name (Expression (Cont));
10010 end Has_Non_Trivial_Precondition;
10011
10012 -------------------
10013 -- Has_Null_Body --
10014 -------------------
10015
10016 function Has_Null_Body (Proc_Id : Entity_Id) return Boolean is
10017 Body_Id : Entity_Id;
10018 Decl : Node_Id;
10019 Spec : Node_Id;
10020 Stmt1 : Node_Id;
10021 Stmt2 : Node_Id;
10022
10023 begin
10024 Spec := Parent (Proc_Id);
10025 Decl := Parent (Spec);
10026
10027 -- Retrieve the entity of the procedure body (e.g. invariant proc).
10028
10029 if Nkind (Spec) = N_Procedure_Specification
10030 and then Nkind (Decl) = N_Subprogram_Declaration
10031 then
10032 Body_Id := Corresponding_Body (Decl);
10033
10034 -- The body acts as a spec
10035
10036 else
10037 Body_Id := Proc_Id;
10038 end if;
10039
10040 -- The body will be generated later
10041
10042 if No (Body_Id) then
10043 return False;
10044 end if;
10045
10046 Spec := Parent (Body_Id);
10047 Decl := Parent (Spec);
10048
10049 pragma Assert
10050 (Nkind (Spec) = N_Procedure_Specification
10051 and then Nkind (Decl) = N_Subprogram_Body);
10052
10053 Stmt1 := First (Statements (Handled_Statement_Sequence (Decl)));
10054
10055 -- Look for a null statement followed by an optional return
10056 -- statement.
10057
10058 if Nkind (Stmt1) = N_Null_Statement then
10059 Stmt2 := Next (Stmt1);
10060
10061 if Present (Stmt2) then
10062 return Nkind (Stmt2) = N_Simple_Return_Statement;
10063 else
10064 return True;
10065 end if;
10066 end if;
10067
10068 return False;
10069 end Has_Null_Body;
10070
10071 ------------------------
10072 -- Has_Null_Exclusion --
10073 ------------------------
10074
10075 function Has_Null_Exclusion (N : Node_Id) return Boolean is
10076 begin
10077 case Nkind (N) is
10078 when N_Access_Definition
10079 | N_Access_Function_Definition
10080 | N_Access_Procedure_Definition
10081 | N_Access_To_Object_Definition
10082 | N_Allocator
10083 | N_Derived_Type_Definition
10084 | N_Function_Specification
10085 | N_Subtype_Declaration
10086 =>
10087 return Null_Exclusion_Present (N);
10088
10089 when N_Component_Definition
10090 | N_Formal_Object_Declaration
10091 | N_Object_Renaming_Declaration
10092 =>
10093 if Present (Subtype_Mark (N)) then
10094 return Null_Exclusion_Present (N);
10095 else pragma Assert (Present (Access_Definition (N)));
10096 return Null_Exclusion_Present (Access_Definition (N));
10097 end if;
10098
10099 when N_Discriminant_Specification =>
10100 if Nkind (Discriminant_Type (N)) = N_Access_Definition then
10101 return Null_Exclusion_Present (Discriminant_Type (N));
10102 else
10103 return Null_Exclusion_Present (N);
10104 end if;
10105
10106 when N_Object_Declaration =>
10107 if Nkind (Object_Definition (N)) = N_Access_Definition then
10108 return Null_Exclusion_Present (Object_Definition (N));
10109 else
10110 return Null_Exclusion_Present (N);
10111 end if;
10112
10113 when N_Parameter_Specification =>
10114 if Nkind (Parameter_Type (N)) = N_Access_Definition then
10115 return Null_Exclusion_Present (Parameter_Type (N));
10116 else
10117 return Null_Exclusion_Present (N);
10118 end if;
10119
10120 when others =>
10121 return False;
10122 end case;
10123 end Has_Null_Exclusion;
10124
10125 ------------------------
10126 -- Has_Null_Extension --
10127 ------------------------
10128
10129 function Has_Null_Extension (T : Entity_Id) return Boolean is
10130 B : constant Entity_Id := Base_Type (T);
10131 Comps : Node_Id;
10132 Ext : Node_Id;
10133
10134 begin
10135 if Nkind (Parent (B)) = N_Full_Type_Declaration
10136 and then Present (Record_Extension_Part (Type_Definition (Parent (B))))
10137 then
10138 Ext := Record_Extension_Part (Type_Definition (Parent (B)));
10139
10140 if Present (Ext) then
10141 if Null_Present (Ext) then
10142 return True;
10143 else
10144 Comps := Component_List (Ext);
10145
10146 -- The null component list is rewritten during analysis to
10147 -- include the parent component. Any other component indicates
10148 -- that the extension was not originally null.
10149
10150 return Null_Present (Comps)
10151 or else No (Next (First (Component_Items (Comps))));
10152 end if;
10153 else
10154 return False;
10155 end if;
10156
10157 else
10158 return False;
10159 end if;
10160 end Has_Null_Extension;
10161
10162 -------------------------
10163 -- Has_Null_Refinement --
10164 -------------------------
10165
10166 function Has_Null_Refinement (Id : Entity_Id) return Boolean is
10167 Constits : Elist_Id;
10168
10169 begin
10170 pragma Assert (Ekind (Id) = E_Abstract_State);
10171 Constits := Refinement_Constituents (Id);
10172
10173 -- For a refinement to be null, the state's sole constituent must be a
10174 -- null.
10175
10176 return
10177 Present (Constits)
10178 and then Nkind (Node (First_Elmt (Constits))) = N_Null;
10179 end Has_Null_Refinement;
10180
10181 -------------------------------
10182 -- Has_Overriding_Initialize --
10183 -------------------------------
10184
10185 function Has_Overriding_Initialize (T : Entity_Id) return Boolean is
10186 BT : constant Entity_Id := Base_Type (T);
10187 P : Elmt_Id;
10188
10189 begin
10190 if Is_Controlled (BT) then
10191 if Is_RTU (Scope (BT), Ada_Finalization) then
10192 return False;
10193
10194 elsif Present (Primitive_Operations (BT)) then
10195 P := First_Elmt (Primitive_Operations (BT));
10196 while Present (P) loop
10197 declare
10198 Init : constant Entity_Id := Node (P);
10199 Formal : constant Entity_Id := First_Formal (Init);
10200 begin
10201 if Ekind (Init) = E_Procedure
10202 and then Chars (Init) = Name_Initialize
10203 and then Comes_From_Source (Init)
10204 and then Present (Formal)
10205 and then Etype (Formal) = BT
10206 and then No (Next_Formal (Formal))
10207 and then (Ada_Version < Ada_2012
10208 or else not Null_Present (Parent (Init)))
10209 then
10210 return True;
10211 end if;
10212 end;
10213
10214 Next_Elmt (P);
10215 end loop;
10216 end if;
10217
10218 -- Here if type itself does not have a non-null Initialize operation:
10219 -- check immediate ancestor.
10220
10221 if Is_Derived_Type (BT)
10222 and then Has_Overriding_Initialize (Etype (BT))
10223 then
10224 return True;
10225 end if;
10226 end if;
10227
10228 return False;
10229 end Has_Overriding_Initialize;
10230
10231 --------------------------------------
10232 -- Has_Preelaborable_Initialization --
10233 --------------------------------------
10234
10235 function Has_Preelaborable_Initialization (E : Entity_Id) return Boolean is
10236 Has_PE : Boolean;
10237
10238 procedure Check_Components (E : Entity_Id);
10239 -- Check component/discriminant chain, sets Has_PE False if a component
10240 -- or discriminant does not meet the preelaborable initialization rules.
10241
10242 ----------------------
10243 -- Check_Components --
10244 ----------------------
10245
10246 procedure Check_Components (E : Entity_Id) is
10247 Ent : Entity_Id;
10248 Exp : Node_Id;
10249
10250 function Is_Preelaborable_Expression (N : Node_Id) return Boolean;
10251 -- Returns True if and only if the expression denoted by N does not
10252 -- violate restrictions on preelaborable constructs (RM-10.2.1(5-9)).
10253
10254 ---------------------------------
10255 -- Is_Preelaborable_Expression --
10256 ---------------------------------
10257
10258 function Is_Preelaborable_Expression (N : Node_Id) return Boolean is
10259 Exp : Node_Id;
10260 Assn : Node_Id;
10261 Choice : Node_Id;
10262 Comp_Type : Entity_Id;
10263 Is_Array_Aggr : Boolean;
10264
10265 begin
10266 if Is_OK_Static_Expression (N) then
10267 return True;
10268
10269 elsif Nkind (N) = N_Null then
10270 return True;
10271
10272 -- Attributes are allowed in general, even if their prefix is a
10273 -- formal type. (It seems that certain attributes known not to be
10274 -- static might not be allowed, but there are no rules to prevent
10275 -- them.)
10276
10277 elsif Nkind (N) = N_Attribute_Reference then
10278 return True;
10279
10280 -- The name of a discriminant evaluated within its parent type is
10281 -- defined to be preelaborable (10.2.1(8)). Note that we test for
10282 -- names that denote discriminals as well as discriminants to
10283 -- catch references occurring within init procs.
10284
10285 elsif Is_Entity_Name (N)
10286 and then
10287 (Ekind (Entity (N)) = E_Discriminant
10288 or else (Ekind_In (Entity (N), E_Constant, E_In_Parameter)
10289 and then Present (Discriminal_Link (Entity (N)))))
10290 then
10291 return True;
10292
10293 elsif Nkind (N) = N_Qualified_Expression then
10294 return Is_Preelaborable_Expression (Expression (N));
10295
10296 -- For aggregates we have to check that each of the associations
10297 -- is preelaborable.
10298
10299 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
10300 Is_Array_Aggr := Is_Array_Type (Etype (N));
10301
10302 if Is_Array_Aggr then
10303 Comp_Type := Component_Type (Etype (N));
10304 end if;
10305
10306 -- Check the ancestor part of extension aggregates, which must
10307 -- be either the name of a type that has preelaborable init or
10308 -- an expression that is preelaborable.
10309
10310 if Nkind (N) = N_Extension_Aggregate then
10311 declare
10312 Anc_Part : constant Node_Id := Ancestor_Part (N);
10313
10314 begin
10315 if Is_Entity_Name (Anc_Part)
10316 and then Is_Type (Entity (Anc_Part))
10317 then
10318 if not Has_Preelaborable_Initialization
10319 (Entity (Anc_Part))
10320 then
10321 return False;
10322 end if;
10323
10324 elsif not Is_Preelaborable_Expression (Anc_Part) then
10325 return False;
10326 end if;
10327 end;
10328 end if;
10329
10330 -- Check positional associations
10331
10332 Exp := First (Expressions (N));
10333 while Present (Exp) loop
10334 if not Is_Preelaborable_Expression (Exp) then
10335 return False;
10336 end if;
10337
10338 Next (Exp);
10339 end loop;
10340
10341 -- Check named associations
10342
10343 Assn := First (Component_Associations (N));
10344 while Present (Assn) loop
10345 Choice := First (Choices (Assn));
10346 while Present (Choice) loop
10347 if Is_Array_Aggr then
10348 if Nkind (Choice) = N_Others_Choice then
10349 null;
10350
10351 elsif Nkind (Choice) = N_Range then
10352 if not Is_OK_Static_Range (Choice) then
10353 return False;
10354 end if;
10355
10356 elsif not Is_OK_Static_Expression (Choice) then
10357 return False;
10358 end if;
10359
10360 else
10361 Comp_Type := Etype (Choice);
10362 end if;
10363
10364 Next (Choice);
10365 end loop;
10366
10367 -- If the association has a <> at this point, then we have
10368 -- to check whether the component's type has preelaborable
10369 -- initialization. Note that this only occurs when the
10370 -- association's corresponding component does not have a
10371 -- default expression, the latter case having already been
10372 -- expanded as an expression for the association.
10373
10374 if Box_Present (Assn) then
10375 if not Has_Preelaborable_Initialization (Comp_Type) then
10376 return False;
10377 end if;
10378
10379 -- In the expression case we check whether the expression
10380 -- is preelaborable.
10381
10382 elsif
10383 not Is_Preelaborable_Expression (Expression (Assn))
10384 then
10385 return False;
10386 end if;
10387
10388 Next (Assn);
10389 end loop;
10390
10391 -- If we get here then aggregate as a whole is preelaborable
10392
10393 return True;
10394
10395 -- All other cases are not preelaborable
10396
10397 else
10398 return False;
10399 end if;
10400 end Is_Preelaborable_Expression;
10401
10402 -- Start of processing for Check_Components
10403
10404 begin
10405 -- Loop through entities of record or protected type
10406
10407 Ent := E;
10408 while Present (Ent) loop
10409
10410 -- We are interested only in components and discriminants
10411
10412 Exp := Empty;
10413
10414 case Ekind (Ent) is
10415 when E_Component =>
10416
10417 -- Get default expression if any. If there is no declaration
10418 -- node, it means we have an internal entity. The parent and
10419 -- tag fields are examples of such entities. For such cases,
10420 -- we just test the type of the entity.
10421
10422 if Present (Declaration_Node (Ent)) then
10423 Exp := Expression (Declaration_Node (Ent));
10424 end if;
10425
10426 when E_Discriminant =>
10427
10428 -- Note: for a renamed discriminant, the Declaration_Node
10429 -- may point to the one from the ancestor, and have a
10430 -- different expression, so use the proper attribute to
10431 -- retrieve the expression from the derived constraint.
10432
10433 Exp := Discriminant_Default_Value (Ent);
10434
10435 when others =>
10436 goto Check_Next_Entity;
10437 end case;
10438
10439 -- A component has PI if it has no default expression and the
10440 -- component type has PI.
10441
10442 if No (Exp) then
10443 if not Has_Preelaborable_Initialization (Etype (Ent)) then
10444 Has_PE := False;
10445 exit;
10446 end if;
10447
10448 -- Require the default expression to be preelaborable
10449
10450 elsif not Is_Preelaborable_Expression (Exp) then
10451 Has_PE := False;
10452 exit;
10453 end if;
10454
10455 <<Check_Next_Entity>>
10456 Next_Entity (Ent);
10457 end loop;
10458 end Check_Components;
10459
10460 -- Start of processing for Has_Preelaborable_Initialization
10461
10462 begin
10463 -- Immediate return if already marked as known preelaborable init. This
10464 -- covers types for which this function has already been called once
10465 -- and returned True (in which case the result is cached), and also
10466 -- types to which a pragma Preelaborable_Initialization applies.
10467
10468 if Known_To_Have_Preelab_Init (E) then
10469 return True;
10470 end if;
10471
10472 -- If the type is a subtype representing a generic actual type, then
10473 -- test whether its base type has preelaborable initialization since
10474 -- the subtype representing the actual does not inherit this attribute
10475 -- from the actual or formal. (but maybe it should???)
10476
10477 if Is_Generic_Actual_Type (E) then
10478 return Has_Preelaborable_Initialization (Base_Type (E));
10479 end if;
10480
10481 -- All elementary types have preelaborable initialization
10482
10483 if Is_Elementary_Type (E) then
10484 Has_PE := True;
10485
10486 -- Array types have PI if the component type has PI
10487
10488 elsif Is_Array_Type (E) then
10489 Has_PE := Has_Preelaborable_Initialization (Component_Type (E));
10490
10491 -- A derived type has preelaborable initialization if its parent type
10492 -- has preelaborable initialization and (in the case of a derived record
10493 -- extension) if the non-inherited components all have preelaborable
10494 -- initialization. However, a user-defined controlled type with an
10495 -- overriding Initialize procedure does not have preelaborable
10496 -- initialization.
10497
10498 elsif Is_Derived_Type (E) then
10499
10500 -- If the derived type is a private extension then it doesn't have
10501 -- preelaborable initialization.
10502
10503 if Ekind (Base_Type (E)) = E_Record_Type_With_Private then
10504 return False;
10505 end if;
10506
10507 -- First check whether ancestor type has preelaborable initialization
10508
10509 Has_PE := Has_Preelaborable_Initialization (Etype (Base_Type (E)));
10510
10511 -- If OK, check extension components (if any)
10512
10513 if Has_PE and then Is_Record_Type (E) then
10514 Check_Components (First_Entity (E));
10515 end if;
10516
10517 -- Check specifically for 10.2.1(11.4/2) exception: a controlled type
10518 -- with a user defined Initialize procedure does not have PI. If
10519 -- the type is untagged, the control primitives come from a component
10520 -- that has already been checked.
10521
10522 if Has_PE
10523 and then Is_Controlled (E)
10524 and then Is_Tagged_Type (E)
10525 and then Has_Overriding_Initialize (E)
10526 then
10527 Has_PE := False;
10528 end if;
10529
10530 -- Private types not derived from a type having preelaborable init and
10531 -- that are not marked with pragma Preelaborable_Initialization do not
10532 -- have preelaborable initialization.
10533
10534 elsif Is_Private_Type (E) then
10535 return False;
10536
10537 -- Record type has PI if it is non private and all components have PI
10538
10539 elsif Is_Record_Type (E) then
10540 Has_PE := True;
10541 Check_Components (First_Entity (E));
10542
10543 -- Protected types must not have entries, and components must meet
10544 -- same set of rules as for record components.
10545
10546 elsif Is_Protected_Type (E) then
10547 if Has_Entries (E) then
10548 Has_PE := False;
10549 else
10550 Has_PE := True;
10551 Check_Components (First_Entity (E));
10552 Check_Components (First_Private_Entity (E));
10553 end if;
10554
10555 -- Type System.Address always has preelaborable initialization
10556
10557 elsif Is_RTE (E, RE_Address) then
10558 Has_PE := True;
10559
10560 -- In all other cases, type does not have preelaborable initialization
10561
10562 else
10563 return False;
10564 end if;
10565
10566 -- If type has preelaborable initialization, cache result
10567
10568 if Has_PE then
10569 Set_Known_To_Have_Preelab_Init (E);
10570 end if;
10571
10572 return Has_PE;
10573 end Has_Preelaborable_Initialization;
10574
10575 ---------------------------
10576 -- Has_Private_Component --
10577 ---------------------------
10578
10579 function Has_Private_Component (Type_Id : Entity_Id) return Boolean is
10580 Btype : Entity_Id := Base_Type (Type_Id);
10581 Component : Entity_Id;
10582
10583 begin
10584 if Error_Posted (Type_Id)
10585 or else Error_Posted (Btype)
10586 then
10587 return False;
10588 end if;
10589
10590 if Is_Class_Wide_Type (Btype) then
10591 Btype := Root_Type (Btype);
10592 end if;
10593
10594 if Is_Private_Type (Btype) then
10595 declare
10596 UT : constant Entity_Id := Underlying_Type (Btype);
10597 begin
10598 if No (UT) then
10599 if No (Full_View (Btype)) then
10600 return not Is_Generic_Type (Btype)
10601 and then
10602 not Is_Generic_Type (Root_Type (Btype));
10603 else
10604 return not Is_Generic_Type (Root_Type (Full_View (Btype)));
10605 end if;
10606 else
10607 return not Is_Frozen (UT) and then Has_Private_Component (UT);
10608 end if;
10609 end;
10610
10611 elsif Is_Array_Type (Btype) then
10612 return Has_Private_Component (Component_Type (Btype));
10613
10614 elsif Is_Record_Type (Btype) then
10615 Component := First_Component (Btype);
10616 while Present (Component) loop
10617 if Has_Private_Component (Etype (Component)) then
10618 return True;
10619 end if;
10620
10621 Next_Component (Component);
10622 end loop;
10623
10624 return False;
10625
10626 elsif Is_Protected_Type (Btype)
10627 and then Present (Corresponding_Record_Type (Btype))
10628 then
10629 return Has_Private_Component (Corresponding_Record_Type (Btype));
10630
10631 else
10632 return False;
10633 end if;
10634 end Has_Private_Component;
10635
10636 ----------------------
10637 -- Has_Signed_Zeros --
10638 ----------------------
10639
10640 function Has_Signed_Zeros (E : Entity_Id) return Boolean is
10641 begin
10642 return Is_Floating_Point_Type (E) and then Signed_Zeros_On_Target;
10643 end Has_Signed_Zeros;
10644
10645 ------------------------------
10646 -- Has_Significant_Contract --
10647 ------------------------------
10648
10649 function Has_Significant_Contract (Subp_Id : Entity_Id) return Boolean is
10650 Subp_Nam : constant Name_Id := Chars (Subp_Id);
10651
10652 begin
10653 -- _Finalizer procedure
10654
10655 if Subp_Nam = Name_uFinalizer then
10656 return False;
10657
10658 -- _Postconditions procedure
10659
10660 elsif Subp_Nam = Name_uPostconditions then
10661 return False;
10662
10663 -- Predicate function
10664
10665 elsif Ekind (Subp_Id) = E_Function
10666 and then Is_Predicate_Function (Subp_Id)
10667 then
10668 return False;
10669
10670 -- TSS subprogram
10671
10672 elsif Get_TSS_Name (Subp_Id) /= TSS_Null then
10673 return False;
10674
10675 else
10676 return True;
10677 end if;
10678 end Has_Significant_Contract;
10679
10680 -----------------------------
10681 -- Has_Static_Array_Bounds --
10682 -----------------------------
10683
10684 function Has_Static_Array_Bounds (Typ : Node_Id) return Boolean is
10685 Ndims : constant Nat := Number_Dimensions (Typ);
10686
10687 Index : Node_Id;
10688 Low : Node_Id;
10689 High : Node_Id;
10690
10691 begin
10692 -- Unconstrained types do not have static bounds
10693
10694 if not Is_Constrained (Typ) then
10695 return False;
10696 end if;
10697
10698 -- First treat string literals specially, as the lower bound and length
10699 -- of string literals are not stored like those of arrays.
10700
10701 -- A string literal always has static bounds
10702
10703 if Ekind (Typ) = E_String_Literal_Subtype then
10704 return True;
10705 end if;
10706
10707 -- Treat all dimensions in turn
10708
10709 Index := First_Index (Typ);
10710 for Indx in 1 .. Ndims loop
10711
10712 -- In case of an illegal index which is not a discrete type, return
10713 -- that the type is not static.
10714
10715 if not Is_Discrete_Type (Etype (Index))
10716 or else Etype (Index) = Any_Type
10717 then
10718 return False;
10719 end if;
10720
10721 Get_Index_Bounds (Index, Low, High);
10722
10723 if Error_Posted (Low) or else Error_Posted (High) then
10724 return False;
10725 end if;
10726
10727 if Is_OK_Static_Expression (Low)
10728 and then
10729 Is_OK_Static_Expression (High)
10730 then
10731 null;
10732 else
10733 return False;
10734 end if;
10735
10736 Next (Index);
10737 end loop;
10738
10739 -- If we fall through the loop, all indexes matched
10740
10741 return True;
10742 end Has_Static_Array_Bounds;
10743
10744 ----------------
10745 -- Has_Stream --
10746 ----------------
10747
10748 function Has_Stream (T : Entity_Id) return Boolean is
10749 E : Entity_Id;
10750
10751 begin
10752 if No (T) then
10753 return False;
10754
10755 elsif Is_RTE (Root_Type (T), RE_Root_Stream_Type) then
10756 return True;
10757
10758 elsif Is_Array_Type (T) then
10759 return Has_Stream (Component_Type (T));
10760
10761 elsif Is_Record_Type (T) then
10762 E := First_Component (T);
10763 while Present (E) loop
10764 if Has_Stream (Etype (E)) then
10765 return True;
10766 else
10767 Next_Component (E);
10768 end if;
10769 end loop;
10770
10771 return False;
10772
10773 elsif Is_Private_Type (T) then
10774 return Has_Stream (Underlying_Type (T));
10775
10776 else
10777 return False;
10778 end if;
10779 end Has_Stream;
10780
10781 ----------------
10782 -- Has_Suffix --
10783 ----------------
10784
10785 function Has_Suffix (E : Entity_Id; Suffix : Character) return Boolean is
10786 begin
10787 Get_Name_String (Chars (E));
10788 return Name_Buffer (Name_Len) = Suffix;
10789 end Has_Suffix;
10790
10791 ----------------
10792 -- Add_Suffix --
10793 ----------------
10794
10795 function Add_Suffix (E : Entity_Id; Suffix : Character) return Name_Id is
10796 begin
10797 Get_Name_String (Chars (E));
10798 Add_Char_To_Name_Buffer (Suffix);
10799 return Name_Find;
10800 end Add_Suffix;
10801
10802 -------------------
10803 -- Remove_Suffix --
10804 -------------------
10805
10806 function Remove_Suffix (E : Entity_Id; Suffix : Character) return Name_Id is
10807 begin
10808 pragma Assert (Has_Suffix (E, Suffix));
10809 Get_Name_String (Chars (E));
10810 Name_Len := Name_Len - 1;
10811 return Name_Find;
10812 end Remove_Suffix;
10813
10814 ----------------------------------
10815 -- Replace_Null_By_Null_Address --
10816 ----------------------------------
10817
10818 procedure Replace_Null_By_Null_Address (N : Node_Id) is
10819 procedure Replace_Null_Operand (Op : Node_Id; Other_Op : Node_Id);
10820 -- Replace operand Op with a reference to Null_Address when the operand
10821 -- denotes a null Address. Other_Op denotes the other operand.
10822
10823 --------------------------
10824 -- Replace_Null_Operand --
10825 --------------------------
10826
10827 procedure Replace_Null_Operand (Op : Node_Id; Other_Op : Node_Id) is
10828 begin
10829 -- Check the type of the complementary operand since the N_Null node
10830 -- has not been decorated yet.
10831
10832 if Nkind (Op) = N_Null
10833 and then Is_Descendant_Of_Address (Etype (Other_Op))
10834 then
10835 Rewrite (Op, New_Occurrence_Of (RTE (RE_Null_Address), Sloc (Op)));
10836 end if;
10837 end Replace_Null_Operand;
10838
10839 -- Start of processing for Replace_Null_By_Null_Address
10840
10841 begin
10842 pragma Assert (Relaxed_RM_Semantics);
10843 pragma Assert (Nkind_In (N, N_Null,
10844 N_Op_Eq,
10845 N_Op_Ge,
10846 N_Op_Gt,
10847 N_Op_Le,
10848 N_Op_Lt,
10849 N_Op_Ne));
10850
10851 if Nkind (N) = N_Null then
10852 Rewrite (N, New_Occurrence_Of (RTE (RE_Null_Address), Sloc (N)));
10853
10854 else
10855 declare
10856 L : constant Node_Id := Left_Opnd (N);
10857 R : constant Node_Id := Right_Opnd (N);
10858
10859 begin
10860 Replace_Null_Operand (L, Other_Op => R);
10861 Replace_Null_Operand (R, Other_Op => L);
10862 end;
10863 end if;
10864 end Replace_Null_By_Null_Address;
10865
10866 --------------------------
10867 -- Has_Tagged_Component --
10868 --------------------------
10869
10870 function Has_Tagged_Component (Typ : Entity_Id) return Boolean is
10871 Comp : Entity_Id;
10872
10873 begin
10874 if Is_Private_Type (Typ) and then Present (Underlying_Type (Typ)) then
10875 return Has_Tagged_Component (Underlying_Type (Typ));
10876
10877 elsif Is_Array_Type (Typ) then
10878 return Has_Tagged_Component (Component_Type (Typ));
10879
10880 elsif Is_Tagged_Type (Typ) then
10881 return True;
10882
10883 elsif Is_Record_Type (Typ) then
10884 Comp := First_Component (Typ);
10885 while Present (Comp) loop
10886 if Has_Tagged_Component (Etype (Comp)) then
10887 return True;
10888 end if;
10889
10890 Next_Component (Comp);
10891 end loop;
10892
10893 return False;
10894
10895 else
10896 return False;
10897 end if;
10898 end Has_Tagged_Component;
10899
10900 -----------------------------
10901 -- Has_Undefined_Reference --
10902 -----------------------------
10903
10904 function Has_Undefined_Reference (Expr : Node_Id) return Boolean is
10905 Has_Undef_Ref : Boolean := False;
10906 -- Flag set when expression Expr contains at least one undefined
10907 -- reference.
10908
10909 function Is_Undefined_Reference (N : Node_Id) return Traverse_Result;
10910 -- Determine whether N denotes a reference and if it does, whether it is
10911 -- undefined.
10912
10913 ----------------------------
10914 -- Is_Undefined_Reference --
10915 ----------------------------
10916
10917 function Is_Undefined_Reference (N : Node_Id) return Traverse_Result is
10918 begin
10919 if Is_Entity_Name (N)
10920 and then Present (Entity (N))
10921 and then Entity (N) = Any_Id
10922 then
10923 Has_Undef_Ref := True;
10924 return Abandon;
10925 end if;
10926
10927 return OK;
10928 end Is_Undefined_Reference;
10929
10930 procedure Find_Undefined_References is
10931 new Traverse_Proc (Is_Undefined_Reference);
10932
10933 -- Start of processing for Has_Undefined_Reference
10934
10935 begin
10936 Find_Undefined_References (Expr);
10937
10938 return Has_Undef_Ref;
10939 end Has_Undefined_Reference;
10940
10941 ----------------------------
10942 -- Has_Volatile_Component --
10943 ----------------------------
10944
10945 function Has_Volatile_Component (Typ : Entity_Id) return Boolean is
10946 Comp : Entity_Id;
10947
10948 begin
10949 if Has_Volatile_Components (Typ) then
10950 return True;
10951
10952 elsif Is_Array_Type (Typ) then
10953 return Is_Volatile (Component_Type (Typ));
10954
10955 elsif Is_Record_Type (Typ) then
10956 Comp := First_Component (Typ);
10957 while Present (Comp) loop
10958 if Is_Volatile_Object (Comp) then
10959 return True;
10960 end if;
10961
10962 Comp := Next_Component (Comp);
10963 end loop;
10964 end if;
10965
10966 return False;
10967 end Has_Volatile_Component;
10968
10969 -------------------------
10970 -- Implementation_Kind --
10971 -------------------------
10972
10973 function Implementation_Kind (Subp : Entity_Id) return Name_Id is
10974 Impl_Prag : constant Node_Id := Get_Rep_Pragma (Subp, Name_Implemented);
10975 Arg : Node_Id;
10976 begin
10977 pragma Assert (Present (Impl_Prag));
10978 Arg := Last (Pragma_Argument_Associations (Impl_Prag));
10979 return Chars (Get_Pragma_Arg (Arg));
10980 end Implementation_Kind;
10981
10982 --------------------------
10983 -- Implements_Interface --
10984 --------------------------
10985
10986 function Implements_Interface
10987 (Typ_Ent : Entity_Id;
10988 Iface_Ent : Entity_Id;
10989 Exclude_Parents : Boolean := False) return Boolean
10990 is
10991 Ifaces_List : Elist_Id;
10992 Elmt : Elmt_Id;
10993 Iface : Entity_Id := Base_Type (Iface_Ent);
10994 Typ : Entity_Id := Base_Type (Typ_Ent);
10995
10996 begin
10997 if Is_Class_Wide_Type (Typ) then
10998 Typ := Root_Type (Typ);
10999 end if;
11000
11001 if not Has_Interfaces (Typ) then
11002 return False;
11003 end if;
11004
11005 if Is_Class_Wide_Type (Iface) then
11006 Iface := Root_Type (Iface);
11007 end if;
11008
11009 Collect_Interfaces (Typ, Ifaces_List);
11010
11011 Elmt := First_Elmt (Ifaces_List);
11012 while Present (Elmt) loop
11013 if Is_Ancestor (Node (Elmt), Typ, Use_Full_View => True)
11014 and then Exclude_Parents
11015 then
11016 null;
11017
11018 elsif Node (Elmt) = Iface then
11019 return True;
11020 end if;
11021
11022 Next_Elmt (Elmt);
11023 end loop;
11024
11025 return False;
11026 end Implements_Interface;
11027
11028 ------------------------------------
11029 -- In_Assertion_Expression_Pragma --
11030 ------------------------------------
11031
11032 function In_Assertion_Expression_Pragma (N : Node_Id) return Boolean is
11033 Par : Node_Id;
11034 Prag : Node_Id := Empty;
11035
11036 begin
11037 -- Climb the parent chain looking for an enclosing pragma
11038
11039 Par := N;
11040 while Present (Par) loop
11041 if Nkind (Par) = N_Pragma then
11042 Prag := Par;
11043 exit;
11044
11045 -- Precondition-like pragmas are expanded into if statements, check
11046 -- the original node instead.
11047
11048 elsif Nkind (Original_Node (Par)) = N_Pragma then
11049 Prag := Original_Node (Par);
11050 exit;
11051
11052 -- The expansion of attribute 'Old generates a constant to capture
11053 -- the result of the prefix. If the parent traversal reaches
11054 -- one of these constants, then the node technically came from a
11055 -- postcondition-like pragma. Note that the Ekind is not tested here
11056 -- because N may be the expression of an object declaration which is
11057 -- currently being analyzed. Such objects carry Ekind of E_Void.
11058
11059 elsif Nkind (Par) = N_Object_Declaration
11060 and then Constant_Present (Par)
11061 and then Stores_Attribute_Old_Prefix (Defining_Entity (Par))
11062 then
11063 return True;
11064
11065 -- Prevent the search from going too far
11066
11067 elsif Is_Body_Or_Package_Declaration (Par) then
11068 return False;
11069 end if;
11070
11071 Par := Parent (Par);
11072 end loop;
11073
11074 return
11075 Present (Prag)
11076 and then Assertion_Expression_Pragma (Get_Pragma_Id (Prag));
11077 end In_Assertion_Expression_Pragma;
11078
11079 ----------------------
11080 -- In_Generic_Scope --
11081 ----------------------
11082
11083 function In_Generic_Scope (E : Entity_Id) return Boolean is
11084 S : Entity_Id;
11085
11086 begin
11087 S := Scope (E);
11088 while Present (S) and then S /= Standard_Standard loop
11089 if Is_Generic_Unit (S) then
11090 return True;
11091 end if;
11092
11093 S := Scope (S);
11094 end loop;
11095
11096 return False;
11097 end In_Generic_Scope;
11098
11099 -----------------
11100 -- In_Instance --
11101 -----------------
11102
11103 function In_Instance return Boolean is
11104 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
11105 S : Entity_Id;
11106
11107 begin
11108 S := Current_Scope;
11109 while Present (S) and then S /= Standard_Standard loop
11110 if Ekind_In (S, E_Function, E_Package, E_Procedure)
11111 and then Is_Generic_Instance (S)
11112 then
11113 -- A child instance is always compiled in the context of a parent
11114 -- instance. Nevertheless, the actuals are not analyzed in an
11115 -- instance context. We detect this case by examining the current
11116 -- compilation unit, which must be a child instance, and checking
11117 -- that it is not currently on the scope stack.
11118
11119 if Is_Child_Unit (Curr_Unit)
11120 and then Nkind (Unit (Cunit (Current_Sem_Unit))) =
11121 N_Package_Instantiation
11122 and then not In_Open_Scopes (Curr_Unit)
11123 then
11124 return False;
11125 else
11126 return True;
11127 end if;
11128 end if;
11129
11130 S := Scope (S);
11131 end loop;
11132
11133 return False;
11134 end In_Instance;
11135
11136 ----------------------
11137 -- In_Instance_Body --
11138 ----------------------
11139
11140 function In_Instance_Body return Boolean is
11141 S : Entity_Id;
11142
11143 begin
11144 S := Current_Scope;
11145 while Present (S) and then S /= Standard_Standard loop
11146 if Ekind_In (S, E_Function, E_Procedure)
11147 and then Is_Generic_Instance (S)
11148 then
11149 return True;
11150
11151 elsif Ekind (S) = E_Package
11152 and then In_Package_Body (S)
11153 and then Is_Generic_Instance (S)
11154 then
11155 return True;
11156 end if;
11157
11158 S := Scope (S);
11159 end loop;
11160
11161 return False;
11162 end In_Instance_Body;
11163
11164 -----------------------------
11165 -- In_Instance_Not_Visible --
11166 -----------------------------
11167
11168 function In_Instance_Not_Visible return Boolean is
11169 S : Entity_Id;
11170
11171 begin
11172 S := Current_Scope;
11173 while Present (S) and then S /= Standard_Standard loop
11174 if Ekind_In (S, E_Function, E_Procedure)
11175 and then Is_Generic_Instance (S)
11176 then
11177 return True;
11178
11179 elsif Ekind (S) = E_Package
11180 and then (In_Package_Body (S) or else In_Private_Part (S))
11181 and then Is_Generic_Instance (S)
11182 then
11183 return True;
11184 end if;
11185
11186 S := Scope (S);
11187 end loop;
11188
11189 return False;
11190 end In_Instance_Not_Visible;
11191
11192 ------------------------------
11193 -- In_Instance_Visible_Part --
11194 ------------------------------
11195
11196 function In_Instance_Visible_Part return Boolean is
11197 S : Entity_Id;
11198
11199 begin
11200 S := Current_Scope;
11201 while Present (S) and then S /= Standard_Standard loop
11202 if Ekind (S) = E_Package
11203 and then Is_Generic_Instance (S)
11204 and then not In_Package_Body (S)
11205 and then not In_Private_Part (S)
11206 then
11207 return True;
11208 end if;
11209
11210 S := Scope (S);
11211 end loop;
11212
11213 return False;
11214 end In_Instance_Visible_Part;
11215
11216 ---------------------
11217 -- In_Package_Body --
11218 ---------------------
11219
11220 function In_Package_Body return Boolean is
11221 S : Entity_Id;
11222
11223 begin
11224 S := Current_Scope;
11225 while Present (S) and then S /= Standard_Standard loop
11226 if Ekind (S) = E_Package and then In_Package_Body (S) then
11227 return True;
11228 else
11229 S := Scope (S);
11230 end if;
11231 end loop;
11232
11233 return False;
11234 end In_Package_Body;
11235
11236 --------------------------------
11237 -- In_Parameter_Specification --
11238 --------------------------------
11239
11240 function In_Parameter_Specification (N : Node_Id) return Boolean is
11241 PN : Node_Id;
11242
11243 begin
11244 PN := Parent (N);
11245 while Present (PN) loop
11246 if Nkind (PN) = N_Parameter_Specification then
11247 return True;
11248 end if;
11249
11250 PN := Parent (PN);
11251 end loop;
11252
11253 return False;
11254 end In_Parameter_Specification;
11255
11256 --------------------------
11257 -- In_Pragma_Expression --
11258 --------------------------
11259
11260 function In_Pragma_Expression (N : Node_Id; Nam : Name_Id) return Boolean is
11261 P : Node_Id;
11262 begin
11263 P := Parent (N);
11264 loop
11265 if No (P) then
11266 return False;
11267 elsif Nkind (P) = N_Pragma and then Pragma_Name (P) = Nam then
11268 return True;
11269 else
11270 P := Parent (P);
11271 end if;
11272 end loop;
11273 end In_Pragma_Expression;
11274
11275 ---------------------------
11276 -- In_Pre_Post_Condition --
11277 ---------------------------
11278
11279 function In_Pre_Post_Condition (N : Node_Id) return Boolean is
11280 Par : Node_Id;
11281 Prag : Node_Id := Empty;
11282 Prag_Id : Pragma_Id;
11283
11284 begin
11285 -- Climb the parent chain looking for an enclosing pragma
11286
11287 Par := N;
11288 while Present (Par) loop
11289 if Nkind (Par) = N_Pragma then
11290 Prag := Par;
11291 exit;
11292
11293 -- Prevent the search from going too far
11294
11295 elsif Is_Body_Or_Package_Declaration (Par) then
11296 exit;
11297 end if;
11298
11299 Par := Parent (Par);
11300 end loop;
11301
11302 if Present (Prag) then
11303 Prag_Id := Get_Pragma_Id (Prag);
11304
11305 return
11306 Prag_Id = Pragma_Post
11307 or else Prag_Id = Pragma_Post_Class
11308 or else Prag_Id = Pragma_Postcondition
11309 or else Prag_Id = Pragma_Pre
11310 or else Prag_Id = Pragma_Pre_Class
11311 or else Prag_Id = Pragma_Precondition;
11312
11313 -- Otherwise the node is not enclosed by a pre/postcondition pragma
11314
11315 else
11316 return False;
11317 end if;
11318 end In_Pre_Post_Condition;
11319
11320 -------------------------------------
11321 -- In_Reverse_Storage_Order_Object --
11322 -------------------------------------
11323
11324 function In_Reverse_Storage_Order_Object (N : Node_Id) return Boolean is
11325 Pref : Node_Id;
11326 Btyp : Entity_Id := Empty;
11327
11328 begin
11329 -- Climb up indexed components
11330
11331 Pref := N;
11332 loop
11333 case Nkind (Pref) is
11334 when N_Selected_Component =>
11335 Pref := Prefix (Pref);
11336 exit;
11337
11338 when N_Indexed_Component =>
11339 Pref := Prefix (Pref);
11340
11341 when others =>
11342 Pref := Empty;
11343 exit;
11344 end case;
11345 end loop;
11346
11347 if Present (Pref) then
11348 Btyp := Base_Type (Etype (Pref));
11349 end if;
11350
11351 return Present (Btyp)
11352 and then (Is_Record_Type (Btyp) or else Is_Array_Type (Btyp))
11353 and then Reverse_Storage_Order (Btyp);
11354 end In_Reverse_Storage_Order_Object;
11355
11356 --------------------------------------
11357 -- In_Subprogram_Or_Concurrent_Unit --
11358 --------------------------------------
11359
11360 function In_Subprogram_Or_Concurrent_Unit return Boolean is
11361 E : Entity_Id;
11362 K : Entity_Kind;
11363
11364 begin
11365 -- Use scope chain to check successively outer scopes
11366
11367 E := Current_Scope;
11368 loop
11369 K := Ekind (E);
11370
11371 if K in Subprogram_Kind
11372 or else K in Concurrent_Kind
11373 or else K in Generic_Subprogram_Kind
11374 then
11375 return True;
11376
11377 elsif E = Standard_Standard then
11378 return False;
11379 end if;
11380
11381 E := Scope (E);
11382 end loop;
11383 end In_Subprogram_Or_Concurrent_Unit;
11384
11385 ---------------------
11386 -- In_Visible_Part --
11387 ---------------------
11388
11389 function In_Visible_Part (Scope_Id : Entity_Id) return Boolean is
11390 begin
11391 return Is_Package_Or_Generic_Package (Scope_Id)
11392 and then In_Open_Scopes (Scope_Id)
11393 and then not In_Package_Body (Scope_Id)
11394 and then not In_Private_Part (Scope_Id);
11395 end In_Visible_Part;
11396
11397 --------------------------------
11398 -- Incomplete_Or_Partial_View --
11399 --------------------------------
11400
11401 function Incomplete_Or_Partial_View (Id : Entity_Id) return Entity_Id is
11402 function Inspect_Decls
11403 (Decls : List_Id;
11404 Taft : Boolean := False) return Entity_Id;
11405 -- Check whether a declarative region contains the incomplete or partial
11406 -- view of Id.
11407
11408 -------------------
11409 -- Inspect_Decls --
11410 -------------------
11411
11412 function Inspect_Decls
11413 (Decls : List_Id;
11414 Taft : Boolean := False) return Entity_Id
11415 is
11416 Decl : Node_Id;
11417 Match : Node_Id;
11418
11419 begin
11420 Decl := First (Decls);
11421 while Present (Decl) loop
11422 Match := Empty;
11423
11424 -- The partial view of a Taft-amendment type is an incomplete
11425 -- type.
11426
11427 if Taft then
11428 if Nkind (Decl) = N_Incomplete_Type_Declaration then
11429 Match := Defining_Identifier (Decl);
11430 end if;
11431
11432 -- Otherwise look for a private type whose full view matches the
11433 -- input type. Note that this checks full_type_declaration nodes
11434 -- to account for derivations from a private type where the type
11435 -- declaration hold the partial view and the full view is an
11436 -- itype.
11437
11438 elsif Nkind_In (Decl, N_Full_Type_Declaration,
11439 N_Private_Extension_Declaration,
11440 N_Private_Type_Declaration)
11441 then
11442 Match := Defining_Identifier (Decl);
11443 end if;
11444
11445 -- Guard against unanalyzed entities
11446
11447 if Present (Match)
11448 and then Is_Type (Match)
11449 and then Present (Full_View (Match))
11450 and then Full_View (Match) = Id
11451 then
11452 return Match;
11453 end if;
11454
11455 Next (Decl);
11456 end loop;
11457
11458 return Empty;
11459 end Inspect_Decls;
11460
11461 -- Local variables
11462
11463 Prev : Entity_Id;
11464
11465 -- Start of processing for Incomplete_Or_Partial_View
11466
11467 begin
11468 -- Deferred constant or incomplete type case
11469
11470 Prev := Current_Entity_In_Scope (Id);
11471
11472 if Present (Prev)
11473 and then (Is_Incomplete_Type (Prev) or else Ekind (Prev) = E_Constant)
11474 and then Present (Full_View (Prev))
11475 and then Full_View (Prev) = Id
11476 then
11477 return Prev;
11478 end if;
11479
11480 -- Private or Taft amendment type case
11481
11482 declare
11483 Pkg : constant Entity_Id := Scope (Id);
11484 Pkg_Decl : Node_Id := Pkg;
11485
11486 begin
11487 if Present (Pkg)
11488 and then Ekind_In (Pkg, E_Generic_Package, E_Package)
11489 then
11490 while Nkind (Pkg_Decl) /= N_Package_Specification loop
11491 Pkg_Decl := Parent (Pkg_Decl);
11492 end loop;
11493
11494 -- It is knows that Typ has a private view, look for it in the
11495 -- visible declarations of the enclosing scope. A special case
11496 -- of this is when the two views have been exchanged - the full
11497 -- appears earlier than the private.
11498
11499 if Has_Private_Declaration (Id) then
11500 Prev := Inspect_Decls (Visible_Declarations (Pkg_Decl));
11501
11502 -- Exchanged view case, look in the private declarations
11503
11504 if No (Prev) then
11505 Prev := Inspect_Decls (Private_Declarations (Pkg_Decl));
11506 end if;
11507
11508 return Prev;
11509
11510 -- Otherwise if this is the package body, then Typ is a potential
11511 -- Taft amendment type. The incomplete view should be located in
11512 -- the private declarations of the enclosing scope.
11513
11514 elsif In_Package_Body (Pkg) then
11515 return Inspect_Decls (Private_Declarations (Pkg_Decl), True);
11516 end if;
11517 end if;
11518 end;
11519
11520 -- The type has no incomplete or private view
11521
11522 return Empty;
11523 end Incomplete_Or_Partial_View;
11524
11525 ----------------------------------
11526 -- Indexed_Component_Bit_Offset --
11527 ----------------------------------
11528
11529 function Indexed_Component_Bit_Offset (N : Node_Id) return Uint is
11530 Exp : constant Node_Id := First (Expressions (N));
11531 Typ : constant Entity_Id := Etype (Prefix (N));
11532 Off : constant Uint := Component_Size (Typ);
11533 Ind : Node_Id;
11534
11535 begin
11536 -- Return early if the component size is not known or variable
11537
11538 if Off = No_Uint or else Off < Uint_0 then
11539 return No_Uint;
11540 end if;
11541
11542 -- Deal with the degenerate case of an empty component
11543
11544 if Off = Uint_0 then
11545 return Off;
11546 end if;
11547
11548 -- Check that both the index value and the low bound are known
11549
11550 if not Compile_Time_Known_Value (Exp) then
11551 return No_Uint;
11552 end if;
11553
11554 Ind := First_Index (Typ);
11555 if No (Ind) then
11556 return No_Uint;
11557 end if;
11558
11559 if Nkind (Ind) = N_Subtype_Indication then
11560 Ind := Constraint (Ind);
11561
11562 if Nkind (Ind) = N_Range_Constraint then
11563 Ind := Range_Expression (Ind);
11564 end if;
11565 end if;
11566
11567 if Nkind (Ind) /= N_Range
11568 or else not Compile_Time_Known_Value (Low_Bound (Ind))
11569 then
11570 return No_Uint;
11571 end if;
11572
11573 -- Return the scaled offset
11574
11575 return Off * (Expr_Value (Exp) - Expr_Value (Low_Bound ((Ind))));
11576 end Indexed_Component_Bit_Offset;
11577
11578 ----------------------------
11579 -- Inherit_Rep_Item_Chain --
11580 ----------------------------
11581
11582 procedure Inherit_Rep_Item_Chain (Typ : Entity_Id; From_Typ : Entity_Id) is
11583 Item : Node_Id;
11584 Next_Item : Node_Id;
11585
11586 begin
11587 -- There are several inheritance scenarios to consider depending on
11588 -- whether both types have rep item chains and whether the destination
11589 -- type already inherits part of the source type's rep item chain.
11590
11591 -- 1) The source type lacks a rep item chain
11592 -- From_Typ ---> Empty
11593 --
11594 -- Typ --------> Item (or Empty)
11595
11596 -- In this case inheritance cannot take place because there are no items
11597 -- to inherit.
11598
11599 -- 2) The destination type lacks a rep item chain
11600 -- From_Typ ---> Item ---> ...
11601 --
11602 -- Typ --------> Empty
11603
11604 -- Inheritance takes place by setting the First_Rep_Item of the
11605 -- destination type to the First_Rep_Item of the source type.
11606 -- From_Typ ---> Item ---> ...
11607 -- ^
11608 -- Typ -----------+
11609
11610 -- 3.1) Both source and destination types have at least one rep item.
11611 -- The destination type does NOT inherit a rep item from the source
11612 -- type.
11613 -- From_Typ ---> Item ---> Item
11614 --
11615 -- Typ --------> Item ---> Item
11616
11617 -- Inheritance takes place by setting the Next_Rep_Item of the last item
11618 -- of the destination type to the First_Rep_Item of the source type.
11619 -- From_Typ -------------------> Item ---> Item
11620 -- ^
11621 -- Typ --------> Item ---> Item --+
11622
11623 -- 3.2) Both source and destination types have at least one rep item.
11624 -- The destination type DOES inherit part of the rep item chain of the
11625 -- source type.
11626 -- From_Typ ---> Item ---> Item ---> Item
11627 -- ^
11628 -- Typ --------> Item ------+
11629
11630 -- This rare case arises when the full view of a private extension must
11631 -- inherit the rep item chain from the full view of its parent type and
11632 -- the full view of the parent type contains extra rep items. Currently
11633 -- only invariants may lead to such form of inheritance.
11634
11635 -- type From_Typ is tagged private
11636 -- with Type_Invariant'Class => Item_2;
11637
11638 -- type Typ is new From_Typ with private
11639 -- with Type_Invariant => Item_4;
11640
11641 -- At this point the rep item chains contain the following items
11642
11643 -- From_Typ -----------> Item_2 ---> Item_3
11644 -- ^
11645 -- Typ --------> Item_4 --+
11646
11647 -- The full views of both types may introduce extra invariants
11648
11649 -- type From_Typ is tagged null record
11650 -- with Type_Invariant => Item_1;
11651
11652 -- type Typ is new From_Typ with null record;
11653
11654 -- The full view of Typ would have to inherit any new rep items added to
11655 -- the full view of From_Typ.
11656
11657 -- From_Typ -----------> Item_1 ---> Item_2 ---> Item_3
11658 -- ^
11659 -- Typ --------> Item_4 --+
11660
11661 -- To achieve this form of inheritance, the destination type must first
11662 -- sever the link between its own rep chain and that of the source type,
11663 -- then inheritance 3.1 takes place.
11664
11665 -- Case 1: The source type lacks a rep item chain
11666
11667 if No (First_Rep_Item (From_Typ)) then
11668 return;
11669
11670 -- Case 2: The destination type lacks a rep item chain
11671
11672 elsif No (First_Rep_Item (Typ)) then
11673 Set_First_Rep_Item (Typ, First_Rep_Item (From_Typ));
11674
11675 -- Case 3: Both the source and destination types have at least one rep
11676 -- item. Traverse the rep item chain of the destination type to find the
11677 -- last rep item.
11678
11679 else
11680 Item := Empty;
11681 Next_Item := First_Rep_Item (Typ);
11682 while Present (Next_Item) loop
11683
11684 -- Detect a link between the destination type's rep chain and that
11685 -- of the source type. There are two possibilities:
11686
11687 -- Variant 1
11688 -- Next_Item
11689 -- V
11690 -- From_Typ ---> Item_1 --->
11691 -- ^
11692 -- Typ -----------+
11693 --
11694 -- Item is Empty
11695
11696 -- Variant 2
11697 -- Next_Item
11698 -- V
11699 -- From_Typ ---> Item_1 ---> Item_2 --->
11700 -- ^
11701 -- Typ --------> Item_3 ------+
11702 -- ^
11703 -- Item
11704
11705 if Has_Rep_Item (From_Typ, Next_Item) then
11706 exit;
11707 end if;
11708
11709 Item := Next_Item;
11710 Next_Item := Next_Rep_Item (Next_Item);
11711 end loop;
11712
11713 -- Inherit the source type's rep item chain
11714
11715 if Present (Item) then
11716 Set_Next_Rep_Item (Item, First_Rep_Item (From_Typ));
11717 else
11718 Set_First_Rep_Item (Typ, First_Rep_Item (From_Typ));
11719 end if;
11720 end if;
11721 end Inherit_Rep_Item_Chain;
11722
11723 ---------------------------------
11724 -- Insert_Explicit_Dereference --
11725 ---------------------------------
11726
11727 procedure Insert_Explicit_Dereference (N : Node_Id) is
11728 New_Prefix : constant Node_Id := Relocate_Node (N);
11729 Ent : Entity_Id := Empty;
11730 Pref : Node_Id;
11731 I : Interp_Index;
11732 It : Interp;
11733 T : Entity_Id;
11734
11735 begin
11736 Save_Interps (N, New_Prefix);
11737
11738 Rewrite (N,
11739 Make_Explicit_Dereference (Sloc (Parent (N)),
11740 Prefix => New_Prefix));
11741
11742 Set_Etype (N, Designated_Type (Etype (New_Prefix)));
11743
11744 if Is_Overloaded (New_Prefix) then
11745
11746 -- The dereference is also overloaded, and its interpretations are
11747 -- the designated types of the interpretations of the original node.
11748
11749 Set_Etype (N, Any_Type);
11750
11751 Get_First_Interp (New_Prefix, I, It);
11752 while Present (It.Nam) loop
11753 T := It.Typ;
11754
11755 if Is_Access_Type (T) then
11756 Add_One_Interp (N, Designated_Type (T), Designated_Type (T));
11757 end if;
11758
11759 Get_Next_Interp (I, It);
11760 end loop;
11761
11762 End_Interp_List;
11763
11764 else
11765 -- Prefix is unambiguous: mark the original prefix (which might
11766 -- Come_From_Source) as a reference, since the new (relocated) one
11767 -- won't be taken into account.
11768
11769 if Is_Entity_Name (New_Prefix) then
11770 Ent := Entity (New_Prefix);
11771 Pref := New_Prefix;
11772
11773 -- For a retrieval of a subcomponent of some composite object,
11774 -- retrieve the ultimate entity if there is one.
11775
11776 elsif Nkind_In (New_Prefix, N_Selected_Component,
11777 N_Indexed_Component)
11778 then
11779 Pref := Prefix (New_Prefix);
11780 while Present (Pref)
11781 and then Nkind_In (Pref, N_Selected_Component,
11782 N_Indexed_Component)
11783 loop
11784 Pref := Prefix (Pref);
11785 end loop;
11786
11787 if Present (Pref) and then Is_Entity_Name (Pref) then
11788 Ent := Entity (Pref);
11789 end if;
11790 end if;
11791
11792 -- Place the reference on the entity node
11793
11794 if Present (Ent) then
11795 Generate_Reference (Ent, Pref);
11796 end if;
11797 end if;
11798 end Insert_Explicit_Dereference;
11799
11800 ------------------------------------------
11801 -- Inspect_Deferred_Constant_Completion --
11802 ------------------------------------------
11803
11804 procedure Inspect_Deferred_Constant_Completion (Decls : List_Id) is
11805 Decl : Node_Id;
11806
11807 begin
11808 Decl := First (Decls);
11809 while Present (Decl) loop
11810
11811 -- Deferred constant signature
11812
11813 if Nkind (Decl) = N_Object_Declaration
11814 and then Constant_Present (Decl)
11815 and then No (Expression (Decl))
11816
11817 -- No need to check internally generated constants
11818
11819 and then Comes_From_Source (Decl)
11820
11821 -- The constant is not completed. A full object declaration or a
11822 -- pragma Import complete a deferred constant.
11823
11824 and then not Has_Completion (Defining_Identifier (Decl))
11825 then
11826 Error_Msg_N
11827 ("constant declaration requires initialization expression",
11828 Defining_Identifier (Decl));
11829 end if;
11830
11831 Decl := Next (Decl);
11832 end loop;
11833 end Inspect_Deferred_Constant_Completion;
11834
11835 -----------------------------
11836 -- Install_Generic_Formals --
11837 -----------------------------
11838
11839 procedure Install_Generic_Formals (Subp_Id : Entity_Id) is
11840 E : Entity_Id;
11841
11842 begin
11843 pragma Assert (Is_Generic_Subprogram (Subp_Id));
11844
11845 E := First_Entity (Subp_Id);
11846 while Present (E) loop
11847 Install_Entity (E);
11848 Next_Entity (E);
11849 end loop;
11850 end Install_Generic_Formals;
11851
11852 ------------------------
11853 -- Install_SPARK_Mode --
11854 ------------------------
11855
11856 procedure Install_SPARK_Mode (Mode : SPARK_Mode_Type; Prag : Node_Id) is
11857 begin
11858 SPARK_Mode := Mode;
11859 SPARK_Mode_Pragma := Prag;
11860 end Install_SPARK_Mode;
11861
11862 -----------------------------
11863 -- Is_Actual_Out_Parameter --
11864 -----------------------------
11865
11866 function Is_Actual_Out_Parameter (N : Node_Id) return Boolean is
11867 Formal : Entity_Id;
11868 Call : Node_Id;
11869 begin
11870 Find_Actual (N, Formal, Call);
11871 return Present (Formal) and then Ekind (Formal) = E_Out_Parameter;
11872 end Is_Actual_Out_Parameter;
11873
11874 -------------------------
11875 -- Is_Actual_Parameter --
11876 -------------------------
11877
11878 function Is_Actual_Parameter (N : Node_Id) return Boolean is
11879 PK : constant Node_Kind := Nkind (Parent (N));
11880
11881 begin
11882 case PK is
11883 when N_Parameter_Association =>
11884 return N = Explicit_Actual_Parameter (Parent (N));
11885
11886 when N_Subprogram_Call =>
11887 return Is_List_Member (N)
11888 and then
11889 List_Containing (N) = Parameter_Associations (Parent (N));
11890
11891 when others =>
11892 return False;
11893 end case;
11894 end Is_Actual_Parameter;
11895
11896 --------------------------------
11897 -- Is_Actual_Tagged_Parameter --
11898 --------------------------------
11899
11900 function Is_Actual_Tagged_Parameter (N : Node_Id) return Boolean is
11901 Formal : Entity_Id;
11902 Call : Node_Id;
11903 begin
11904 Find_Actual (N, Formal, Call);
11905 return Present (Formal) and then Is_Tagged_Type (Etype (Formal));
11906 end Is_Actual_Tagged_Parameter;
11907
11908 ---------------------
11909 -- Is_Aliased_View --
11910 ---------------------
11911
11912 function Is_Aliased_View (Obj : Node_Id) return Boolean is
11913 E : Entity_Id;
11914
11915 begin
11916 if Is_Entity_Name (Obj) then
11917 E := Entity (Obj);
11918
11919 return
11920 (Is_Object (E)
11921 and then
11922 (Is_Aliased (E)
11923 or else (Present (Renamed_Object (E))
11924 and then Is_Aliased_View (Renamed_Object (E)))))
11925
11926 or else ((Is_Formal (E)
11927 or else Ekind_In (E, E_Generic_In_Out_Parameter,
11928 E_Generic_In_Parameter))
11929 and then Is_Tagged_Type (Etype (E)))
11930
11931 or else (Is_Concurrent_Type (E) and then In_Open_Scopes (E))
11932
11933 -- Current instance of type, either directly or as rewritten
11934 -- reference to the current object.
11935
11936 or else (Is_Entity_Name (Original_Node (Obj))
11937 and then Present (Entity (Original_Node (Obj)))
11938 and then Is_Type (Entity (Original_Node (Obj))))
11939
11940 or else (Is_Type (E) and then E = Current_Scope)
11941
11942 or else (Is_Incomplete_Or_Private_Type (E)
11943 and then Full_View (E) = Current_Scope)
11944
11945 -- Ada 2012 AI05-0053: the return object of an extended return
11946 -- statement is aliased if its type is immutably limited.
11947
11948 or else (Is_Return_Object (E)
11949 and then Is_Limited_View (Etype (E)));
11950
11951 elsif Nkind (Obj) = N_Selected_Component then
11952 return Is_Aliased (Entity (Selector_Name (Obj)));
11953
11954 elsif Nkind (Obj) = N_Indexed_Component then
11955 return Has_Aliased_Components (Etype (Prefix (Obj)))
11956 or else
11957 (Is_Access_Type (Etype (Prefix (Obj)))
11958 and then Has_Aliased_Components
11959 (Designated_Type (Etype (Prefix (Obj)))));
11960
11961 elsif Nkind_In (Obj, N_Unchecked_Type_Conversion, N_Type_Conversion) then
11962 return Is_Tagged_Type (Etype (Obj))
11963 and then Is_Aliased_View (Expression (Obj));
11964
11965 elsif Nkind (Obj) = N_Explicit_Dereference then
11966 return Nkind (Original_Node (Obj)) /= N_Function_Call;
11967
11968 else
11969 return False;
11970 end if;
11971 end Is_Aliased_View;
11972
11973 -------------------------
11974 -- Is_Ancestor_Package --
11975 -------------------------
11976
11977 function Is_Ancestor_Package
11978 (E1 : Entity_Id;
11979 E2 : Entity_Id) return Boolean
11980 is
11981 Par : Entity_Id;
11982
11983 begin
11984 Par := E2;
11985 while Present (Par) and then Par /= Standard_Standard loop
11986 if Par = E1 then
11987 return True;
11988 end if;
11989
11990 Par := Scope (Par);
11991 end loop;
11992
11993 return False;
11994 end Is_Ancestor_Package;
11995
11996 ----------------------
11997 -- Is_Atomic_Object --
11998 ----------------------
11999
12000 function Is_Atomic_Object (N : Node_Id) return Boolean is
12001
12002 function Object_Has_Atomic_Components (N : Node_Id) return Boolean;
12003 -- Determines if given object has atomic components
12004
12005 function Is_Atomic_Prefix (N : Node_Id) return Boolean;
12006 -- If prefix is an implicit dereference, examine designated type
12007
12008 ----------------------
12009 -- Is_Atomic_Prefix --
12010 ----------------------
12011
12012 function Is_Atomic_Prefix (N : Node_Id) return Boolean is
12013 begin
12014 if Is_Access_Type (Etype (N)) then
12015 return
12016 Has_Atomic_Components (Designated_Type (Etype (N)));
12017 else
12018 return Object_Has_Atomic_Components (N);
12019 end if;
12020 end Is_Atomic_Prefix;
12021
12022 ----------------------------------
12023 -- Object_Has_Atomic_Components --
12024 ----------------------------------
12025
12026 function Object_Has_Atomic_Components (N : Node_Id) return Boolean is
12027 begin
12028 if Has_Atomic_Components (Etype (N))
12029 or else Is_Atomic (Etype (N))
12030 then
12031 return True;
12032
12033 elsif Is_Entity_Name (N)
12034 and then (Has_Atomic_Components (Entity (N))
12035 or else Is_Atomic (Entity (N)))
12036 then
12037 return True;
12038
12039 elsif Nkind (N) = N_Selected_Component
12040 and then Is_Atomic (Entity (Selector_Name (N)))
12041 then
12042 return True;
12043
12044 elsif Nkind (N) = N_Indexed_Component
12045 or else Nkind (N) = N_Selected_Component
12046 then
12047 return Is_Atomic_Prefix (Prefix (N));
12048
12049 else
12050 return False;
12051 end if;
12052 end Object_Has_Atomic_Components;
12053
12054 -- Start of processing for Is_Atomic_Object
12055
12056 begin
12057 -- Predicate is not relevant to subprograms
12058
12059 if Is_Entity_Name (N) and then Is_Overloadable (Entity (N)) then
12060 return False;
12061
12062 elsif Is_Atomic (Etype (N))
12063 or else (Is_Entity_Name (N) and then Is_Atomic (Entity (N)))
12064 then
12065 return True;
12066
12067 elsif Nkind (N) = N_Selected_Component
12068 and then Is_Atomic (Entity (Selector_Name (N)))
12069 then
12070 return True;
12071
12072 elsif Nkind (N) = N_Indexed_Component
12073 or else Nkind (N) = N_Selected_Component
12074 then
12075 return Is_Atomic_Prefix (Prefix (N));
12076
12077 else
12078 return False;
12079 end if;
12080 end Is_Atomic_Object;
12081
12082 -----------------------------
12083 -- Is_Atomic_Or_VFA_Object --
12084 -----------------------------
12085
12086 function Is_Atomic_Or_VFA_Object (N : Node_Id) return Boolean is
12087 begin
12088 return Is_Atomic_Object (N)
12089 or else (Is_Object_Reference (N)
12090 and then Is_Entity_Name (N)
12091 and then (Is_Volatile_Full_Access (Entity (N))
12092 or else
12093 Is_Volatile_Full_Access (Etype (Entity (N)))));
12094 end Is_Atomic_Or_VFA_Object;
12095
12096 -------------------------
12097 -- Is_Attribute_Result --
12098 -------------------------
12099
12100 function Is_Attribute_Result (N : Node_Id) return Boolean is
12101 begin
12102 return Nkind (N) = N_Attribute_Reference
12103 and then Attribute_Name (N) = Name_Result;
12104 end Is_Attribute_Result;
12105
12106 -------------------------
12107 -- Is_Attribute_Update --
12108 -------------------------
12109
12110 function Is_Attribute_Update (N : Node_Id) return Boolean is
12111 begin
12112 return Nkind (N) = N_Attribute_Reference
12113 and then Attribute_Name (N) = Name_Update;
12114 end Is_Attribute_Update;
12115
12116 ------------------------------------
12117 -- Is_Body_Or_Package_Declaration --
12118 ------------------------------------
12119
12120 function Is_Body_Or_Package_Declaration (N : Node_Id) return Boolean is
12121 begin
12122 return Nkind_In (N, N_Entry_Body,
12123 N_Package_Body,
12124 N_Package_Declaration,
12125 N_Protected_Body,
12126 N_Subprogram_Body,
12127 N_Task_Body);
12128 end Is_Body_Or_Package_Declaration;
12129
12130 -----------------------
12131 -- Is_Bounded_String --
12132 -----------------------
12133
12134 function Is_Bounded_String (T : Entity_Id) return Boolean is
12135 Under : constant Entity_Id := Underlying_Type (Root_Type (T));
12136
12137 begin
12138 -- Check whether T is ultimately derived from Ada.Strings.Superbounded.
12139 -- Super_String, or one of the [Wide_]Wide_ versions. This will
12140 -- be True for all the Bounded_String types in instances of the
12141 -- Generic_Bounded_Length generics, and for types derived from those.
12142
12143 return Present (Under)
12144 and then (Is_RTE (Root_Type (Under), RO_SU_Super_String) or else
12145 Is_RTE (Root_Type (Under), RO_WI_Super_String) or else
12146 Is_RTE (Root_Type (Under), RO_WW_Super_String));
12147 end Is_Bounded_String;
12148
12149 -------------------------
12150 -- Is_Child_Or_Sibling --
12151 -------------------------
12152
12153 function Is_Child_Or_Sibling
12154 (Pack_1 : Entity_Id;
12155 Pack_2 : Entity_Id) return Boolean
12156 is
12157 function Distance_From_Standard (Pack : Entity_Id) return Nat;
12158 -- Given an arbitrary package, return the number of "climbs" necessary
12159 -- to reach scope Standard_Standard.
12160
12161 procedure Equalize_Depths
12162 (Pack : in out Entity_Id;
12163 Depth : in out Nat;
12164 Depth_To_Reach : Nat);
12165 -- Given an arbitrary package, its depth and a target depth to reach,
12166 -- climb the scope chain until the said depth is reached. The pointer
12167 -- to the package and its depth a modified during the climb.
12168
12169 ----------------------------
12170 -- Distance_From_Standard --
12171 ----------------------------
12172
12173 function Distance_From_Standard (Pack : Entity_Id) return Nat is
12174 Dist : Nat;
12175 Scop : Entity_Id;
12176
12177 begin
12178 Dist := 0;
12179 Scop := Pack;
12180 while Present (Scop) and then Scop /= Standard_Standard loop
12181 Dist := Dist + 1;
12182 Scop := Scope (Scop);
12183 end loop;
12184
12185 return Dist;
12186 end Distance_From_Standard;
12187
12188 ---------------------
12189 -- Equalize_Depths --
12190 ---------------------
12191
12192 procedure Equalize_Depths
12193 (Pack : in out Entity_Id;
12194 Depth : in out Nat;
12195 Depth_To_Reach : Nat)
12196 is
12197 begin
12198 -- The package must be at a greater or equal depth
12199
12200 if Depth < Depth_To_Reach then
12201 raise Program_Error;
12202 end if;
12203
12204 -- Climb the scope chain until the desired depth is reached
12205
12206 while Present (Pack) and then Depth /= Depth_To_Reach loop
12207 Pack := Scope (Pack);
12208 Depth := Depth - 1;
12209 end loop;
12210 end Equalize_Depths;
12211
12212 -- Local variables
12213
12214 P_1 : Entity_Id := Pack_1;
12215 P_1_Child : Boolean := False;
12216 P_1_Depth : Nat := Distance_From_Standard (P_1);
12217 P_2 : Entity_Id := Pack_2;
12218 P_2_Child : Boolean := False;
12219 P_2_Depth : Nat := Distance_From_Standard (P_2);
12220
12221 -- Start of processing for Is_Child_Or_Sibling
12222
12223 begin
12224 pragma Assert
12225 (Ekind (Pack_1) = E_Package and then Ekind (Pack_2) = E_Package);
12226
12227 -- Both packages denote the same entity, therefore they cannot be
12228 -- children or siblings.
12229
12230 if P_1 = P_2 then
12231 return False;
12232
12233 -- One of the packages is at a deeper level than the other. Note that
12234 -- both may still come from differen hierarchies.
12235
12236 -- (root) P_2
12237 -- / \ :
12238 -- X P_2 or X
12239 -- : :
12240 -- P_1 P_1
12241
12242 elsif P_1_Depth > P_2_Depth then
12243 Equalize_Depths
12244 (Pack => P_1,
12245 Depth => P_1_Depth,
12246 Depth_To_Reach => P_2_Depth);
12247 P_1_Child := True;
12248
12249 -- (root) P_1
12250 -- / \ :
12251 -- P_1 X or X
12252 -- : :
12253 -- P_2 P_2
12254
12255 elsif P_2_Depth > P_1_Depth then
12256 Equalize_Depths
12257 (Pack => P_2,
12258 Depth => P_2_Depth,
12259 Depth_To_Reach => P_1_Depth);
12260 P_2_Child := True;
12261 end if;
12262
12263 -- At this stage the package pointers have been elevated to the same
12264 -- depth. If the related entities are the same, then one package is a
12265 -- potential child of the other:
12266
12267 -- P_1
12268 -- :
12269 -- X became P_1 P_2 or vica versa
12270 -- :
12271 -- P_2
12272
12273 if P_1 = P_2 then
12274 if P_1_Child then
12275 return Is_Child_Unit (Pack_1);
12276
12277 else pragma Assert (P_2_Child);
12278 return Is_Child_Unit (Pack_2);
12279 end if;
12280
12281 -- The packages may come from the same package chain or from entirely
12282 -- different hierarcies. To determine this, climb the scope stack until
12283 -- a common root is found.
12284
12285 -- (root) (root 1) (root 2)
12286 -- / \ | |
12287 -- P_1 P_2 P_1 P_2
12288
12289 else
12290 while Present (P_1) and then Present (P_2) loop
12291
12292 -- The two packages may be siblings
12293
12294 if P_1 = P_2 then
12295 return Is_Child_Unit (Pack_1) and then Is_Child_Unit (Pack_2);
12296 end if;
12297
12298 P_1 := Scope (P_1);
12299 P_2 := Scope (P_2);
12300 end loop;
12301 end if;
12302
12303 return False;
12304 end Is_Child_Or_Sibling;
12305
12306 -----------------------------
12307 -- Is_Concurrent_Interface --
12308 -----------------------------
12309
12310 function Is_Concurrent_Interface (T : Entity_Id) return Boolean is
12311 begin
12312 return Is_Interface (T)
12313 and then
12314 (Is_Protected_Interface (T)
12315 or else Is_Synchronized_Interface (T)
12316 or else Is_Task_Interface (T));
12317 end Is_Concurrent_Interface;
12318
12319 -----------------------
12320 -- Is_Constant_Bound --
12321 -----------------------
12322
12323 function Is_Constant_Bound (Exp : Node_Id) return Boolean is
12324 begin
12325 if Compile_Time_Known_Value (Exp) then
12326 return True;
12327
12328 elsif Is_Entity_Name (Exp) and then Present (Entity (Exp)) then
12329 return Is_Constant_Object (Entity (Exp))
12330 or else Ekind (Entity (Exp)) = E_Enumeration_Literal;
12331
12332 elsif Nkind (Exp) in N_Binary_Op then
12333 return Is_Constant_Bound (Left_Opnd (Exp))
12334 and then Is_Constant_Bound (Right_Opnd (Exp))
12335 and then Scope (Entity (Exp)) = Standard_Standard;
12336
12337 else
12338 return False;
12339 end if;
12340 end Is_Constant_Bound;
12341
12342 ---------------------------
12343 -- Is_Container_Element --
12344 ---------------------------
12345
12346 function Is_Container_Element (Exp : Node_Id) return Boolean is
12347 Loc : constant Source_Ptr := Sloc (Exp);
12348 Pref : constant Node_Id := Prefix (Exp);
12349
12350 Call : Node_Id;
12351 -- Call to an indexing aspect
12352
12353 Cont_Typ : Entity_Id;
12354 -- The type of the container being accessed
12355
12356 Elem_Typ : Entity_Id;
12357 -- Its element type
12358
12359 Indexing : Entity_Id;
12360 Is_Const : Boolean;
12361 -- Indicates that constant indexing is used, and the element is thus
12362 -- a constant.
12363
12364 Ref_Typ : Entity_Id;
12365 -- The reference type returned by the indexing operation
12366
12367 begin
12368 -- If C is a container, in a context that imposes the element type of
12369 -- that container, the indexing notation C (X) is rewritten as:
12370
12371 -- Indexing (C, X).Discr.all
12372
12373 -- where Indexing is one of the indexing aspects of the container.
12374 -- If the context does not require a reference, the construct can be
12375 -- rewritten as
12376
12377 -- Element (C, X)
12378
12379 -- First, verify that the construct has the proper form
12380
12381 if not Expander_Active then
12382 return False;
12383
12384 elsif Nkind (Pref) /= N_Selected_Component then
12385 return False;
12386
12387 elsif Nkind (Prefix (Pref)) /= N_Function_Call then
12388 return False;
12389
12390 else
12391 Call := Prefix (Pref);
12392 Ref_Typ := Etype (Call);
12393 end if;
12394
12395 if not Has_Implicit_Dereference (Ref_Typ)
12396 or else No (First (Parameter_Associations (Call)))
12397 or else not Is_Entity_Name (Name (Call))
12398 then
12399 return False;
12400 end if;
12401
12402 -- Retrieve type of container object, and its iterator aspects
12403
12404 Cont_Typ := Etype (First (Parameter_Associations (Call)));
12405 Indexing := Find_Value_Of_Aspect (Cont_Typ, Aspect_Constant_Indexing);
12406 Is_Const := False;
12407
12408 if No (Indexing) then
12409
12410 -- Container should have at least one indexing operation
12411
12412 return False;
12413
12414 elsif Entity (Name (Call)) /= Entity (Indexing) then
12415
12416 -- This may be a variable indexing operation
12417
12418 Indexing := Find_Value_Of_Aspect (Cont_Typ, Aspect_Variable_Indexing);
12419
12420 if No (Indexing)
12421 or else Entity (Name (Call)) /= Entity (Indexing)
12422 then
12423 return False;
12424 end if;
12425
12426 else
12427 Is_Const := True;
12428 end if;
12429
12430 Elem_Typ := Find_Value_Of_Aspect (Cont_Typ, Aspect_Iterator_Element);
12431
12432 if No (Elem_Typ) or else Entity (Elem_Typ) /= Etype (Exp) then
12433 return False;
12434 end if;
12435
12436 -- Check that the expression is not the target of an assignment, in
12437 -- which case the rewriting is not possible.
12438
12439 if not Is_Const then
12440 declare
12441 Par : Node_Id;
12442
12443 begin
12444 Par := Exp;
12445 while Present (Par)
12446 loop
12447 if Nkind (Parent (Par)) = N_Assignment_Statement
12448 and then Par = Name (Parent (Par))
12449 then
12450 return False;
12451
12452 -- A renaming produces a reference, and the transformation
12453 -- does not apply.
12454
12455 elsif Nkind (Parent (Par)) = N_Object_Renaming_Declaration then
12456 return False;
12457
12458 elsif Nkind_In
12459 (Nkind (Parent (Par)), N_Function_Call,
12460 N_Procedure_Call_Statement,
12461 N_Entry_Call_Statement)
12462 then
12463 -- Check that the element is not part of an actual for an
12464 -- in-out parameter.
12465
12466 declare
12467 F : Entity_Id;
12468 A : Node_Id;
12469
12470 begin
12471 F := First_Formal (Entity (Name (Parent (Par))));
12472 A := First (Parameter_Associations (Parent (Par)));
12473 while Present (F) loop
12474 if A = Par and then Ekind (F) /= E_In_Parameter then
12475 return False;
12476 end if;
12477
12478 Next_Formal (F);
12479 Next (A);
12480 end loop;
12481 end;
12482
12483 -- E_In_Parameter in a call: element is not modified.
12484
12485 exit;
12486 end if;
12487
12488 Par := Parent (Par);
12489 end loop;
12490 end;
12491 end if;
12492
12493 -- The expression has the proper form and the context requires the
12494 -- element type. Retrieve the Element function of the container and
12495 -- rewrite the construct as a call to it.
12496
12497 declare
12498 Op : Elmt_Id;
12499
12500 begin
12501 Op := First_Elmt (Primitive_Operations (Cont_Typ));
12502 while Present (Op) loop
12503 exit when Chars (Node (Op)) = Name_Element;
12504 Next_Elmt (Op);
12505 end loop;
12506
12507 if No (Op) then
12508 return False;
12509
12510 else
12511 Rewrite (Exp,
12512 Make_Function_Call (Loc,
12513 Name => New_Occurrence_Of (Node (Op), Loc),
12514 Parameter_Associations => Parameter_Associations (Call)));
12515 Analyze_And_Resolve (Exp, Entity (Elem_Typ));
12516 return True;
12517 end if;
12518 end;
12519 end Is_Container_Element;
12520
12521 ----------------------------
12522 -- Is_Contract_Annotation --
12523 ----------------------------
12524
12525 function Is_Contract_Annotation (Item : Node_Id) return Boolean is
12526 begin
12527 return Is_Package_Contract_Annotation (Item)
12528 or else
12529 Is_Subprogram_Contract_Annotation (Item);
12530 end Is_Contract_Annotation;
12531
12532 --------------------------------------
12533 -- Is_Controlling_Limited_Procedure --
12534 --------------------------------------
12535
12536 function Is_Controlling_Limited_Procedure
12537 (Proc_Nam : Entity_Id) return Boolean
12538 is
12539 Param_Typ : Entity_Id := Empty;
12540
12541 begin
12542 if Ekind (Proc_Nam) = E_Procedure
12543 and then Present (Parameter_Specifications (Parent (Proc_Nam)))
12544 then
12545 Param_Typ := Etype (Parameter_Type (First (
12546 Parameter_Specifications (Parent (Proc_Nam)))));
12547
12548 -- In this case where an Itype was created, the procedure call has been
12549 -- rewritten.
12550
12551 elsif Present (Associated_Node_For_Itype (Proc_Nam))
12552 and then Present (Original_Node (Associated_Node_For_Itype (Proc_Nam)))
12553 and then
12554 Present (Parameter_Associations
12555 (Associated_Node_For_Itype (Proc_Nam)))
12556 then
12557 Param_Typ :=
12558 Etype (First (Parameter_Associations
12559 (Associated_Node_For_Itype (Proc_Nam))));
12560 end if;
12561
12562 if Present (Param_Typ) then
12563 return
12564 Is_Interface (Param_Typ)
12565 and then Is_Limited_Record (Param_Typ);
12566 end if;
12567
12568 return False;
12569 end Is_Controlling_Limited_Procedure;
12570
12571 -----------------------------
12572 -- Is_CPP_Constructor_Call --
12573 -----------------------------
12574
12575 function Is_CPP_Constructor_Call (N : Node_Id) return Boolean is
12576 begin
12577 return Nkind (N) = N_Function_Call
12578 and then Is_CPP_Class (Etype (Etype (N)))
12579 and then Is_Constructor (Entity (Name (N)))
12580 and then Is_Imported (Entity (Name (N)));
12581 end Is_CPP_Constructor_Call;
12582
12583 -------------------------
12584 -- Is_Current_Instance --
12585 -------------------------
12586
12587 function Is_Current_Instance (N : Node_Id) return Boolean is
12588 Typ : constant Entity_Id := Entity (N);
12589 P : Node_Id;
12590
12591 begin
12592 -- Simplest case: entity is a concurrent type and we are currently
12593 -- inside the body. This will eventually be expanded into a
12594 -- call to Self (for tasks) or _object (for protected objects).
12595
12596 if Is_Concurrent_Type (Typ) and then In_Open_Scopes (Typ) then
12597 return True;
12598
12599 else
12600 -- Check whether the context is a (sub)type declaration for the
12601 -- type entity.
12602
12603 P := Parent (N);
12604 while Present (P) loop
12605 if Nkind_In (P, N_Full_Type_Declaration,
12606 N_Private_Type_Declaration,
12607 N_Subtype_Declaration)
12608 and then Comes_From_Source (P)
12609 and then Defining_Entity (P) = Typ
12610 then
12611 return True;
12612
12613 -- A subtype name may appear in an aspect specification for a
12614 -- Predicate_Failure aspect, for which we do not construct a
12615 -- wrapper procedure. The subtype will be replaced by the
12616 -- expression being tested when the corresponding predicate
12617 -- check is expanded.
12618
12619 elsif Nkind (P) = N_Aspect_Specification
12620 and then Nkind (Parent (P)) = N_Subtype_Declaration
12621 then
12622 return True;
12623
12624 elsif Nkind (P) = N_Pragma
12625 and then
12626 Get_Pragma_Id (P) = Pragma_Predicate_Failure
12627 then
12628 return True;
12629 end if;
12630
12631 P := Parent (P);
12632 end loop;
12633 end if;
12634
12635 -- In any other context this is not a current occurrence
12636
12637 return False;
12638 end Is_Current_Instance;
12639
12640 --------------------
12641 -- Is_Declaration --
12642 --------------------
12643
12644 function Is_Declaration (N : Node_Id) return Boolean is
12645 begin
12646 return
12647 Is_Declaration_Other_Than_Renaming (N)
12648 or else Is_Renaming_Declaration (N);
12649 end Is_Declaration;
12650
12651 ----------------------------------------
12652 -- Is_Declaration_Other_Than_Renaming --
12653 ----------------------------------------
12654
12655 function Is_Declaration_Other_Than_Renaming (N : Node_Id) return Boolean is
12656 begin
12657 case Nkind (N) is
12658 when N_Abstract_Subprogram_Declaration
12659 | N_Exception_Declaration
12660 | N_Expression_Function
12661 | N_Full_Type_Declaration
12662 | N_Generic_Package_Declaration
12663 | N_Generic_Subprogram_Declaration
12664 | N_Number_Declaration
12665 | N_Object_Declaration
12666 | N_Package_Declaration
12667 | N_Private_Extension_Declaration
12668 | N_Private_Type_Declaration
12669 | N_Subprogram_Declaration
12670 | N_Subtype_Declaration
12671 =>
12672 return True;
12673
12674 when others =>
12675 return False;
12676 end case;
12677 end Is_Declaration_Other_Than_Renaming;
12678
12679 --------------------------------
12680 -- Is_Declared_Within_Variant --
12681 --------------------------------
12682
12683 function Is_Declared_Within_Variant (Comp : Entity_Id) return Boolean is
12684 Comp_Decl : constant Node_Id := Parent (Comp);
12685 Comp_List : constant Node_Id := Parent (Comp_Decl);
12686 begin
12687 return Nkind (Parent (Comp_List)) = N_Variant;
12688 end Is_Declared_Within_Variant;
12689
12690 ----------------------------------------------
12691 -- Is_Dependent_Component_Of_Mutable_Object --
12692 ----------------------------------------------
12693
12694 function Is_Dependent_Component_Of_Mutable_Object
12695 (Object : Node_Id) return Boolean
12696 is
12697 P : Node_Id;
12698 Prefix_Type : Entity_Id;
12699 P_Aliased : Boolean := False;
12700 Comp : Entity_Id;
12701
12702 Deref : Node_Id := Object;
12703 -- Dereference node, in something like X.all.Y(2)
12704
12705 -- Start of processing for Is_Dependent_Component_Of_Mutable_Object
12706
12707 begin
12708 -- Find the dereference node if any
12709
12710 while Nkind_In (Deref, N_Indexed_Component,
12711 N_Selected_Component,
12712 N_Slice)
12713 loop
12714 Deref := Prefix (Deref);
12715 end loop;
12716
12717 -- Ada 2005: If we have a component or slice of a dereference,
12718 -- something like X.all.Y (2), and the type of X is access-to-constant,
12719 -- Is_Variable will return False, because it is indeed a constant
12720 -- view. But it might be a view of a variable object, so we want the
12721 -- following condition to be True in that case.
12722
12723 if Is_Variable (Object)
12724 or else (Ada_Version >= Ada_2005
12725 and then Nkind (Deref) = N_Explicit_Dereference)
12726 then
12727 if Nkind (Object) = N_Selected_Component then
12728 P := Prefix (Object);
12729 Prefix_Type := Etype (P);
12730
12731 if Is_Entity_Name (P) then
12732 if Ekind (Entity (P)) = E_Generic_In_Out_Parameter then
12733 Prefix_Type := Base_Type (Prefix_Type);
12734 end if;
12735
12736 if Is_Aliased (Entity (P)) then
12737 P_Aliased := True;
12738 end if;
12739
12740 -- A discriminant check on a selected component may be expanded
12741 -- into a dereference when removing side-effects. Recover the
12742 -- original node and its type, which may be unconstrained.
12743
12744 elsif Nkind (P) = N_Explicit_Dereference
12745 and then not (Comes_From_Source (P))
12746 then
12747 P := Original_Node (P);
12748 Prefix_Type := Etype (P);
12749
12750 else
12751 -- Check for prefix being an aliased component???
12752
12753 null;
12754
12755 end if;
12756
12757 -- A heap object is constrained by its initial value
12758
12759 -- Ada 2005 (AI-363): Always assume the object could be mutable in
12760 -- the dereferenced case, since the access value might denote an
12761 -- unconstrained aliased object, whereas in Ada 95 the designated
12762 -- object is guaranteed to be constrained. A worst-case assumption
12763 -- has to apply in Ada 2005 because we can't tell at compile
12764 -- time whether the object is "constrained by its initial value"
12765 -- (despite the fact that 3.10.2(26/2) and 8.5.1(5/2) are semantic
12766 -- rules (these rules are acknowledged to need fixing).
12767
12768 if Ada_Version < Ada_2005 then
12769 if Is_Access_Type (Prefix_Type)
12770 or else Nkind (P) = N_Explicit_Dereference
12771 then
12772 return False;
12773 end if;
12774
12775 else pragma Assert (Ada_Version >= Ada_2005);
12776 if Is_Access_Type (Prefix_Type) then
12777
12778 -- If the access type is pool-specific, and there is no
12779 -- constrained partial view of the designated type, then the
12780 -- designated object is known to be constrained.
12781
12782 if Ekind (Prefix_Type) = E_Access_Type
12783 and then not Object_Type_Has_Constrained_Partial_View
12784 (Typ => Designated_Type (Prefix_Type),
12785 Scop => Current_Scope)
12786 then
12787 return False;
12788
12789 -- Otherwise (general access type, or there is a constrained
12790 -- partial view of the designated type), we need to check
12791 -- based on the designated type.
12792
12793 else
12794 Prefix_Type := Designated_Type (Prefix_Type);
12795 end if;
12796 end if;
12797 end if;
12798
12799 Comp :=
12800 Original_Record_Component (Entity (Selector_Name (Object)));
12801
12802 -- As per AI-0017, the renaming is illegal in a generic body, even
12803 -- if the subtype is indefinite.
12804
12805 -- Ada 2005 (AI-363): In Ada 2005 an aliased object can be mutable
12806
12807 if not Is_Constrained (Prefix_Type)
12808 and then (Is_Definite_Subtype (Prefix_Type)
12809 or else
12810 (Is_Generic_Type (Prefix_Type)
12811 and then Ekind (Current_Scope) = E_Generic_Package
12812 and then In_Package_Body (Current_Scope)))
12813
12814 and then (Is_Declared_Within_Variant (Comp)
12815 or else Has_Discriminant_Dependent_Constraint (Comp))
12816 and then (not P_Aliased or else Ada_Version >= Ada_2005)
12817 then
12818 return True;
12819
12820 -- If the prefix is of an access type at this point, then we want
12821 -- to return False, rather than calling this function recursively
12822 -- on the access object (which itself might be a discriminant-
12823 -- dependent component of some other object, but that isn't
12824 -- relevant to checking the object passed to us). This avoids
12825 -- issuing wrong errors when compiling with -gnatc, where there
12826 -- can be implicit dereferences that have not been expanded.
12827
12828 elsif Is_Access_Type (Etype (Prefix (Object))) then
12829 return False;
12830
12831 else
12832 return
12833 Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
12834 end if;
12835
12836 elsif Nkind (Object) = N_Indexed_Component
12837 or else Nkind (Object) = N_Slice
12838 then
12839 return Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
12840
12841 -- A type conversion that Is_Variable is a view conversion:
12842 -- go back to the denoted object.
12843
12844 elsif Nkind (Object) = N_Type_Conversion then
12845 return
12846 Is_Dependent_Component_Of_Mutable_Object (Expression (Object));
12847 end if;
12848 end if;
12849
12850 return False;
12851 end Is_Dependent_Component_Of_Mutable_Object;
12852
12853 ---------------------
12854 -- Is_Dereferenced --
12855 ---------------------
12856
12857 function Is_Dereferenced (N : Node_Id) return Boolean is
12858 P : constant Node_Id := Parent (N);
12859 begin
12860 return Nkind_In (P, N_Selected_Component,
12861 N_Explicit_Dereference,
12862 N_Indexed_Component,
12863 N_Slice)
12864 and then Prefix (P) = N;
12865 end Is_Dereferenced;
12866
12867 ----------------------
12868 -- Is_Descendant_Of --
12869 ----------------------
12870
12871 function Is_Descendant_Of (T1 : Entity_Id; T2 : Entity_Id) return Boolean is
12872 T : Entity_Id;
12873 Etyp : Entity_Id;
12874
12875 begin
12876 pragma Assert (Nkind (T1) in N_Entity);
12877 pragma Assert (Nkind (T2) in N_Entity);
12878
12879 T := Base_Type (T1);
12880
12881 -- Immediate return if the types match
12882
12883 if T = T2 then
12884 return True;
12885
12886 -- Comment needed here ???
12887
12888 elsif Ekind (T) = E_Class_Wide_Type then
12889 return Etype (T) = T2;
12890
12891 -- All other cases
12892
12893 else
12894 loop
12895 Etyp := Etype (T);
12896
12897 -- Done if we found the type we are looking for
12898
12899 if Etyp = T2 then
12900 return True;
12901
12902 -- Done if no more derivations to check
12903
12904 elsif T = T1
12905 or else T = Etyp
12906 then
12907 return False;
12908
12909 -- Following test catches error cases resulting from prev errors
12910
12911 elsif No (Etyp) then
12912 return False;
12913
12914 elsif Is_Private_Type (T) and then Etyp = Full_View (T) then
12915 return False;
12916
12917 elsif Is_Private_Type (Etyp) and then Full_View (Etyp) = T then
12918 return False;
12919 end if;
12920
12921 T := Base_Type (Etyp);
12922 end loop;
12923 end if;
12924 end Is_Descendant_Of;
12925
12926 ----------------------------------------
12927 -- Is_Descendant_Of_Suspension_Object --
12928 ----------------------------------------
12929
12930 function Is_Descendant_Of_Suspension_Object
12931 (Typ : Entity_Id) return Boolean
12932 is
12933 Cur_Typ : Entity_Id;
12934 Par_Typ : Entity_Id;
12935
12936 begin
12937 -- Climb the type derivation chain checking each parent type against
12938 -- Suspension_Object.
12939
12940 Cur_Typ := Base_Type (Typ);
12941 while Present (Cur_Typ) loop
12942 Par_Typ := Etype (Cur_Typ);
12943
12944 -- The current type is a match
12945
12946 if Is_Suspension_Object (Cur_Typ) then
12947 return True;
12948
12949 -- Stop the traversal once the root of the derivation chain has been
12950 -- reached. In that case the current type is its own base type.
12951
12952 elsif Cur_Typ = Par_Typ then
12953 exit;
12954 end if;
12955
12956 Cur_Typ := Base_Type (Par_Typ);
12957 end loop;
12958
12959 return False;
12960 end Is_Descendant_Of_Suspension_Object;
12961
12962 ---------------------------------------------
12963 -- Is_Double_Precision_Floating_Point_Type --
12964 ---------------------------------------------
12965
12966 function Is_Double_Precision_Floating_Point_Type
12967 (E : Entity_Id) return Boolean is
12968 begin
12969 return Is_Floating_Point_Type (E)
12970 and then Machine_Radix_Value (E) = Uint_2
12971 and then Machine_Mantissa_Value (E) = UI_From_Int (53)
12972 and then Machine_Emax_Value (E) = Uint_2 ** Uint_10
12973 and then Machine_Emin_Value (E) = Uint_3 - (Uint_2 ** Uint_10);
12974 end Is_Double_Precision_Floating_Point_Type;
12975
12976 -----------------------------
12977 -- Is_Effectively_Volatile --
12978 -----------------------------
12979
12980 function Is_Effectively_Volatile (Id : Entity_Id) return Boolean is
12981 begin
12982 if Is_Type (Id) then
12983
12984 -- An arbitrary type is effectively volatile when it is subject to
12985 -- pragma Atomic or Volatile.
12986
12987 if Is_Volatile (Id) then
12988 return True;
12989
12990 -- An array type is effectively volatile when it is subject to pragma
12991 -- Atomic_Components or Volatile_Components or its component type is
12992 -- effectively volatile.
12993
12994 elsif Is_Array_Type (Id) then
12995 declare
12996 Anc : Entity_Id := Base_Type (Id);
12997 begin
12998 if Is_Private_Type (Anc) then
12999 Anc := Full_View (Anc);
13000 end if;
13001
13002 -- Test for presence of ancestor, as the full view of a private
13003 -- type may be missing in case of error.
13004
13005 return
13006 Has_Volatile_Components (Id)
13007 or else
13008 (Present (Anc)
13009 and then Is_Effectively_Volatile (Component_Type (Anc)));
13010 end;
13011
13012 -- A protected type is always volatile
13013
13014 elsif Is_Protected_Type (Id) then
13015 return True;
13016
13017 -- A descendant of Ada.Synchronous_Task_Control.Suspension_Object is
13018 -- automatically volatile.
13019
13020 elsif Is_Descendant_Of_Suspension_Object (Id) then
13021 return True;
13022
13023 -- Otherwise the type is not effectively volatile
13024
13025 else
13026 return False;
13027 end if;
13028
13029 -- Otherwise Id denotes an object
13030
13031 else
13032 return
13033 Is_Volatile (Id)
13034 or else Has_Volatile_Components (Id)
13035 or else Is_Effectively_Volatile (Etype (Id));
13036 end if;
13037 end Is_Effectively_Volatile;
13038
13039 ------------------------------------
13040 -- Is_Effectively_Volatile_Object --
13041 ------------------------------------
13042
13043 function Is_Effectively_Volatile_Object (N : Node_Id) return Boolean is
13044 begin
13045 if Is_Entity_Name (N) then
13046 return Is_Effectively_Volatile (Entity (N));
13047
13048 elsif Nkind (N) = N_Indexed_Component then
13049 return Is_Effectively_Volatile_Object (Prefix (N));
13050
13051 elsif Nkind (N) = N_Selected_Component then
13052 return
13053 Is_Effectively_Volatile_Object (Prefix (N))
13054 or else
13055 Is_Effectively_Volatile_Object (Selector_Name (N));
13056
13057 else
13058 return False;
13059 end if;
13060 end Is_Effectively_Volatile_Object;
13061
13062 -------------------
13063 -- Is_Entry_Body --
13064 -------------------
13065
13066 function Is_Entry_Body (Id : Entity_Id) return Boolean is
13067 begin
13068 return
13069 Ekind_In (Id, E_Entry, E_Entry_Family)
13070 and then Nkind (Unit_Declaration_Node (Id)) = N_Entry_Body;
13071 end Is_Entry_Body;
13072
13073 --------------------------
13074 -- Is_Entry_Declaration --
13075 --------------------------
13076
13077 function Is_Entry_Declaration (Id : Entity_Id) return Boolean is
13078 begin
13079 return
13080 Ekind_In (Id, E_Entry, E_Entry_Family)
13081 and then Nkind (Unit_Declaration_Node (Id)) = N_Entry_Declaration;
13082 end Is_Entry_Declaration;
13083
13084 ------------------------------------
13085 -- Is_Expanded_Priority_Attribute --
13086 ------------------------------------
13087
13088 function Is_Expanded_Priority_Attribute (E : Entity_Id) return Boolean is
13089 begin
13090 return
13091 Nkind (E) = N_Function_Call
13092 and then not Configurable_Run_Time_Mode
13093 and then (Entity (Name (E)) = RTE (RE_Get_Ceiling)
13094 or else Entity (Name (E)) = RTE (RO_PE_Get_Ceiling));
13095 end Is_Expanded_Priority_Attribute;
13096
13097 ----------------------------
13098 -- Is_Expression_Function --
13099 ----------------------------
13100
13101 function Is_Expression_Function (Subp : Entity_Id) return Boolean is
13102 begin
13103 if Ekind_In (Subp, E_Function, E_Subprogram_Body) then
13104 return
13105 Nkind (Original_Node (Unit_Declaration_Node (Subp))) =
13106 N_Expression_Function;
13107 else
13108 return False;
13109 end if;
13110 end Is_Expression_Function;
13111
13112 ------------------------------------------
13113 -- Is_Expression_Function_Or_Completion --
13114 ------------------------------------------
13115
13116 function Is_Expression_Function_Or_Completion
13117 (Subp : Entity_Id) return Boolean
13118 is
13119 Subp_Decl : Node_Id;
13120
13121 begin
13122 if Ekind (Subp) = E_Function then
13123 Subp_Decl := Unit_Declaration_Node (Subp);
13124
13125 -- The function declaration is either an expression function or is
13126 -- completed by an expression function body.
13127
13128 return
13129 Is_Expression_Function (Subp)
13130 or else (Nkind (Subp_Decl) = N_Subprogram_Declaration
13131 and then Present (Corresponding_Body (Subp_Decl))
13132 and then Is_Expression_Function
13133 (Corresponding_Body (Subp_Decl)));
13134
13135 elsif Ekind (Subp) = E_Subprogram_Body then
13136 return Is_Expression_Function (Subp);
13137
13138 else
13139 return False;
13140 end if;
13141 end Is_Expression_Function_Or_Completion;
13142
13143 -----------------------
13144 -- Is_EVF_Expression --
13145 -----------------------
13146
13147 function Is_EVF_Expression (N : Node_Id) return Boolean is
13148 Orig_N : constant Node_Id := Original_Node (N);
13149 Alt : Node_Id;
13150 Expr : Node_Id;
13151 Id : Entity_Id;
13152
13153 begin
13154 -- Detect a reference to a formal parameter of a specific tagged type
13155 -- whose related subprogram is subject to pragma Expresions_Visible with
13156 -- value "False".
13157
13158 if Is_Entity_Name (N) and then Present (Entity (N)) then
13159 Id := Entity (N);
13160
13161 return
13162 Is_Formal (Id)
13163 and then Is_Specific_Tagged_Type (Etype (Id))
13164 and then Extensions_Visible_Status (Id) =
13165 Extensions_Visible_False;
13166
13167 -- A case expression is an EVF expression when it contains at least one
13168 -- EVF dependent_expression. Note that a case expression may have been
13169 -- expanded, hence the use of Original_Node.
13170
13171 elsif Nkind (Orig_N) = N_Case_Expression then
13172 Alt := First (Alternatives (Orig_N));
13173 while Present (Alt) loop
13174 if Is_EVF_Expression (Expression (Alt)) then
13175 return True;
13176 end if;
13177
13178 Next (Alt);
13179 end loop;
13180
13181 -- An if expression is an EVF expression when it contains at least one
13182 -- EVF dependent_expression. Note that an if expression may have been
13183 -- expanded, hence the use of Original_Node.
13184
13185 elsif Nkind (Orig_N) = N_If_Expression then
13186 Expr := Next (First (Expressions (Orig_N)));
13187 while Present (Expr) loop
13188 if Is_EVF_Expression (Expr) then
13189 return True;
13190 end if;
13191
13192 Next (Expr);
13193 end loop;
13194
13195 -- A qualified expression or a type conversion is an EVF expression when
13196 -- its operand is an EVF expression.
13197
13198 elsif Nkind_In (N, N_Qualified_Expression,
13199 N_Unchecked_Type_Conversion,
13200 N_Type_Conversion)
13201 then
13202 return Is_EVF_Expression (Expression (N));
13203
13204 -- Attributes 'Loop_Entry, 'Old, and 'Update are EVF expressions when
13205 -- their prefix denotes an EVF expression.
13206
13207 elsif Nkind (N) = N_Attribute_Reference
13208 and then Nam_In (Attribute_Name (N), Name_Loop_Entry,
13209 Name_Old,
13210 Name_Update)
13211 then
13212 return Is_EVF_Expression (Prefix (N));
13213 end if;
13214
13215 return False;
13216 end Is_EVF_Expression;
13217
13218 --------------
13219 -- Is_False --
13220 --------------
13221
13222 function Is_False (U : Uint) return Boolean is
13223 begin
13224 return (U = 0);
13225 end Is_False;
13226
13227 ---------------------------
13228 -- Is_Fixed_Model_Number --
13229 ---------------------------
13230
13231 function Is_Fixed_Model_Number (U : Ureal; T : Entity_Id) return Boolean is
13232 S : constant Ureal := Small_Value (T);
13233 M : Urealp.Save_Mark;
13234 R : Boolean;
13235
13236 begin
13237 M := Urealp.Mark;
13238 R := (U = UR_Trunc (U / S) * S);
13239 Urealp.Release (M);
13240 return R;
13241 end Is_Fixed_Model_Number;
13242
13243 -------------------------------
13244 -- Is_Fully_Initialized_Type --
13245 -------------------------------
13246
13247 function Is_Fully_Initialized_Type (Typ : Entity_Id) return Boolean is
13248 begin
13249 -- Scalar types
13250
13251 if Is_Scalar_Type (Typ) then
13252
13253 -- A scalar type with an aspect Default_Value is fully initialized
13254
13255 -- Note: Iniitalize/Normalize_Scalars also ensure full initialization
13256 -- of a scalar type, but we don't take that into account here, since
13257 -- we don't want these to affect warnings.
13258
13259 return Has_Default_Aspect (Typ);
13260
13261 elsif Is_Access_Type (Typ) then
13262 return True;
13263
13264 elsif Is_Array_Type (Typ) then
13265 if Is_Fully_Initialized_Type (Component_Type (Typ))
13266 or else (Ada_Version >= Ada_2012 and then Has_Default_Aspect (Typ))
13267 then
13268 return True;
13269 end if;
13270
13271 -- An interesting case, if we have a constrained type one of whose
13272 -- bounds is known to be null, then there are no elements to be
13273 -- initialized, so all the elements are initialized.
13274
13275 if Is_Constrained (Typ) then
13276 declare
13277 Indx : Node_Id;
13278 Indx_Typ : Entity_Id;
13279 Lbd, Hbd : Node_Id;
13280
13281 begin
13282 Indx := First_Index (Typ);
13283 while Present (Indx) loop
13284 if Etype (Indx) = Any_Type then
13285 return False;
13286
13287 -- If index is a range, use directly
13288
13289 elsif Nkind (Indx) = N_Range then
13290 Lbd := Low_Bound (Indx);
13291 Hbd := High_Bound (Indx);
13292
13293 else
13294 Indx_Typ := Etype (Indx);
13295
13296 if Is_Private_Type (Indx_Typ) then
13297 Indx_Typ := Full_View (Indx_Typ);
13298 end if;
13299
13300 if No (Indx_Typ) or else Etype (Indx_Typ) = Any_Type then
13301 return False;
13302 else
13303 Lbd := Type_Low_Bound (Indx_Typ);
13304 Hbd := Type_High_Bound (Indx_Typ);
13305 end if;
13306 end if;
13307
13308 if Compile_Time_Known_Value (Lbd)
13309 and then
13310 Compile_Time_Known_Value (Hbd)
13311 then
13312 if Expr_Value (Hbd) < Expr_Value (Lbd) then
13313 return True;
13314 end if;
13315 end if;
13316
13317 Next_Index (Indx);
13318 end loop;
13319 end;
13320 end if;
13321
13322 -- If no null indexes, then type is not fully initialized
13323
13324 return False;
13325
13326 -- Record types
13327
13328 elsif Is_Record_Type (Typ) then
13329 if Has_Discriminants (Typ)
13330 and then
13331 Present (Discriminant_Default_Value (First_Discriminant (Typ)))
13332 and then Is_Fully_Initialized_Variant (Typ)
13333 then
13334 return True;
13335 end if;
13336
13337 -- We consider bounded string types to be fully initialized, because
13338 -- otherwise we get false alarms when the Data component is not
13339 -- default-initialized.
13340
13341 if Is_Bounded_String (Typ) then
13342 return True;
13343 end if;
13344
13345 -- Controlled records are considered to be fully initialized if
13346 -- there is a user defined Initialize routine. This may not be
13347 -- entirely correct, but as the spec notes, we are guessing here
13348 -- what is best from the point of view of issuing warnings.
13349
13350 if Is_Controlled (Typ) then
13351 declare
13352 Utyp : constant Entity_Id := Underlying_Type (Typ);
13353
13354 begin
13355 if Present (Utyp) then
13356 declare
13357 Init : constant Entity_Id :=
13358 (Find_Optional_Prim_Op
13359 (Underlying_Type (Typ), Name_Initialize));
13360
13361 begin
13362 if Present (Init)
13363 and then Comes_From_Source (Init)
13364 and then not
13365 Is_Predefined_File_Name
13366 (File_Name (Get_Source_File_Index (Sloc (Init))))
13367 then
13368 return True;
13369
13370 elsif Has_Null_Extension (Typ)
13371 and then
13372 Is_Fully_Initialized_Type
13373 (Etype (Base_Type (Typ)))
13374 then
13375 return True;
13376 end if;
13377 end;
13378 end if;
13379 end;
13380 end if;
13381
13382 -- Otherwise see if all record components are initialized
13383
13384 declare
13385 Ent : Entity_Id;
13386
13387 begin
13388 Ent := First_Entity (Typ);
13389 while Present (Ent) loop
13390 if Ekind (Ent) = E_Component
13391 and then (No (Parent (Ent))
13392 or else No (Expression (Parent (Ent))))
13393 and then not Is_Fully_Initialized_Type (Etype (Ent))
13394
13395 -- Special VM case for tag components, which need to be
13396 -- defined in this case, but are never initialized as VMs
13397 -- are using other dispatching mechanisms. Ignore this
13398 -- uninitialized case. Note that this applies both to the
13399 -- uTag entry and the main vtable pointer (CPP_Class case).
13400
13401 and then (Tagged_Type_Expansion or else not Is_Tag (Ent))
13402 then
13403 return False;
13404 end if;
13405
13406 Next_Entity (Ent);
13407 end loop;
13408 end;
13409
13410 -- No uninitialized components, so type is fully initialized.
13411 -- Note that this catches the case of no components as well.
13412
13413 return True;
13414
13415 elsif Is_Concurrent_Type (Typ) then
13416 return True;
13417
13418 elsif Is_Private_Type (Typ) then
13419 declare
13420 U : constant Entity_Id := Underlying_Type (Typ);
13421
13422 begin
13423 if No (U) then
13424 return False;
13425 else
13426 return Is_Fully_Initialized_Type (U);
13427 end if;
13428 end;
13429
13430 else
13431 return False;
13432 end if;
13433 end Is_Fully_Initialized_Type;
13434
13435 ----------------------------------
13436 -- Is_Fully_Initialized_Variant --
13437 ----------------------------------
13438
13439 function Is_Fully_Initialized_Variant (Typ : Entity_Id) return Boolean is
13440 Loc : constant Source_Ptr := Sloc (Typ);
13441 Constraints : constant List_Id := New_List;
13442 Components : constant Elist_Id := New_Elmt_List;
13443 Comp_Elmt : Elmt_Id;
13444 Comp_Id : Node_Id;
13445 Comp_List : Node_Id;
13446 Discr : Entity_Id;
13447 Discr_Val : Node_Id;
13448
13449 Report_Errors : Boolean;
13450 pragma Warnings (Off, Report_Errors);
13451
13452 begin
13453 if Serious_Errors_Detected > 0 then
13454 return False;
13455 end if;
13456
13457 if Is_Record_Type (Typ)
13458 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
13459 and then Nkind (Type_Definition (Parent (Typ))) = N_Record_Definition
13460 then
13461 Comp_List := Component_List (Type_Definition (Parent (Typ)));
13462
13463 Discr := First_Discriminant (Typ);
13464 while Present (Discr) loop
13465 if Nkind (Parent (Discr)) = N_Discriminant_Specification then
13466 Discr_Val := Expression (Parent (Discr));
13467
13468 if Present (Discr_Val)
13469 and then Is_OK_Static_Expression (Discr_Val)
13470 then
13471 Append_To (Constraints,
13472 Make_Component_Association (Loc,
13473 Choices => New_List (New_Occurrence_Of (Discr, Loc)),
13474 Expression => New_Copy (Discr_Val)));
13475 else
13476 return False;
13477 end if;
13478 else
13479 return False;
13480 end if;
13481
13482 Next_Discriminant (Discr);
13483 end loop;
13484
13485 Gather_Components
13486 (Typ => Typ,
13487 Comp_List => Comp_List,
13488 Governed_By => Constraints,
13489 Into => Components,
13490 Report_Errors => Report_Errors);
13491
13492 -- Check that each component present is fully initialized
13493
13494 Comp_Elmt := First_Elmt (Components);
13495 while Present (Comp_Elmt) loop
13496 Comp_Id := Node (Comp_Elmt);
13497
13498 if Ekind (Comp_Id) = E_Component
13499 and then (No (Parent (Comp_Id))
13500 or else No (Expression (Parent (Comp_Id))))
13501 and then not Is_Fully_Initialized_Type (Etype (Comp_Id))
13502 then
13503 return False;
13504 end if;
13505
13506 Next_Elmt (Comp_Elmt);
13507 end loop;
13508
13509 return True;
13510
13511 elsif Is_Private_Type (Typ) then
13512 declare
13513 U : constant Entity_Id := Underlying_Type (Typ);
13514
13515 begin
13516 if No (U) then
13517 return False;
13518 else
13519 return Is_Fully_Initialized_Variant (U);
13520 end if;
13521 end;
13522
13523 else
13524 return False;
13525 end if;
13526 end Is_Fully_Initialized_Variant;
13527
13528 ------------------------------------
13529 -- Is_Generic_Declaration_Or_Body --
13530 ------------------------------------
13531
13532 function Is_Generic_Declaration_Or_Body (Decl : Node_Id) return Boolean is
13533 Spec_Decl : Node_Id;
13534
13535 begin
13536 -- Package/subprogram body
13537
13538 if Nkind_In (Decl, N_Package_Body, N_Subprogram_Body)
13539 and then Present (Corresponding_Spec (Decl))
13540 then
13541 Spec_Decl := Unit_Declaration_Node (Corresponding_Spec (Decl));
13542
13543 -- Package/subprogram body stub
13544
13545 elsif Nkind_In (Decl, N_Package_Body_Stub, N_Subprogram_Body_Stub)
13546 and then Present (Corresponding_Spec_Of_Stub (Decl))
13547 then
13548 Spec_Decl :=
13549 Unit_Declaration_Node (Corresponding_Spec_Of_Stub (Decl));
13550
13551 -- All other cases
13552
13553 else
13554 Spec_Decl := Decl;
13555 end if;
13556
13557 -- Rather than inspecting the defining entity of the spec declaration,
13558 -- look at its Nkind. This takes care of the case where the analysis of
13559 -- a generic body modifies the Ekind of its spec to allow for recursive
13560 -- calls.
13561
13562 return
13563 Nkind_In (Spec_Decl, N_Generic_Package_Declaration,
13564 N_Generic_Subprogram_Declaration);
13565 end Is_Generic_Declaration_Or_Body;
13566
13567 ----------------------------
13568 -- Is_Inherited_Operation --
13569 ----------------------------
13570
13571 function Is_Inherited_Operation (E : Entity_Id) return Boolean is
13572 pragma Assert (Is_Overloadable (E));
13573 Kind : constant Node_Kind := Nkind (Parent (E));
13574 begin
13575 return Kind = N_Full_Type_Declaration
13576 or else Kind = N_Private_Extension_Declaration
13577 or else Kind = N_Subtype_Declaration
13578 or else (Ekind (E) = E_Enumeration_Literal
13579 and then Is_Derived_Type (Etype (E)));
13580 end Is_Inherited_Operation;
13581
13582 -------------------------------------
13583 -- Is_Inherited_Operation_For_Type --
13584 -------------------------------------
13585
13586 function Is_Inherited_Operation_For_Type
13587 (E : Entity_Id;
13588 Typ : Entity_Id) return Boolean
13589 is
13590 begin
13591 -- Check that the operation has been created by the type declaration
13592
13593 return Is_Inherited_Operation (E)
13594 and then Defining_Identifier (Parent (E)) = Typ;
13595 end Is_Inherited_Operation_For_Type;
13596
13597 --------------------------------------
13598 -- Is_Inlinable_Expression_Function --
13599 --------------------------------------
13600
13601 function Is_Inlinable_Expression_Function
13602 (Subp : Entity_Id) return Boolean
13603 is
13604 Return_Expr : Node_Id;
13605
13606 begin
13607 if Is_Expression_Function_Or_Completion (Subp)
13608 and then Has_Pragma_Inline_Always (Subp)
13609 and then Needs_No_Actuals (Subp)
13610 and then No (Contract (Subp))
13611 and then not Is_Dispatching_Operation (Subp)
13612 and then Needs_Finalization (Etype (Subp))
13613 and then not Is_Class_Wide_Type (Etype (Subp))
13614 and then not (Has_Invariants (Etype (Subp)))
13615 and then Present (Subprogram_Body (Subp))
13616 and then Was_Expression_Function (Subprogram_Body (Subp))
13617 then
13618 Return_Expr := Expression_Of_Expression_Function (Subp);
13619
13620 -- The returned object must not have a qualified expression and its
13621 -- nominal subtype must be statically compatible with the result
13622 -- subtype of the expression function.
13623
13624 return
13625 Nkind (Return_Expr) = N_Identifier
13626 and then Etype (Return_Expr) = Etype (Subp);
13627 end if;
13628
13629 return False;
13630 end Is_Inlinable_Expression_Function;
13631
13632 -----------------
13633 -- Is_Iterator --
13634 -----------------
13635
13636 function Is_Iterator (Typ : Entity_Id) return Boolean is
13637 function Denotes_Iterator (Iter_Typ : Entity_Id) return Boolean;
13638 -- Determine whether type Iter_Typ is a predefined forward or reversible
13639 -- iterator.
13640
13641 ----------------------
13642 -- Denotes_Iterator --
13643 ----------------------
13644
13645 function Denotes_Iterator (Iter_Typ : Entity_Id) return Boolean is
13646 begin
13647 -- Check that the name matches, and that the ultimate ancestor is in
13648 -- a predefined unit, i.e the one that declares iterator interfaces.
13649
13650 return
13651 Nam_In (Chars (Iter_Typ), Name_Forward_Iterator,
13652 Name_Reversible_Iterator)
13653 and then Is_Predefined_File_Name
13654 (Unit_File_Name (Get_Source_Unit (Root_Type (Iter_Typ))));
13655 end Denotes_Iterator;
13656
13657 -- Local variables
13658
13659 Iface_Elmt : Elmt_Id;
13660 Ifaces : Elist_Id;
13661
13662 -- Start of processing for Is_Iterator
13663
13664 begin
13665 -- The type may be a subtype of a descendant of the proper instance of
13666 -- the predefined interface type, so we must use the root type of the
13667 -- given type. The same is done for Is_Reversible_Iterator.
13668
13669 if Is_Class_Wide_Type (Typ)
13670 and then Denotes_Iterator (Root_Type (Typ))
13671 then
13672 return True;
13673
13674 elsif not Is_Tagged_Type (Typ) or else not Is_Derived_Type (Typ) then
13675 return False;
13676
13677 elsif Present (Find_Value_Of_Aspect (Typ, Aspect_Iterable)) then
13678 return True;
13679
13680 else
13681 Collect_Interfaces (Typ, Ifaces);
13682
13683 Iface_Elmt := First_Elmt (Ifaces);
13684 while Present (Iface_Elmt) loop
13685 if Denotes_Iterator (Node (Iface_Elmt)) then
13686 return True;
13687 end if;
13688
13689 Next_Elmt (Iface_Elmt);
13690 end loop;
13691
13692 return False;
13693 end if;
13694 end Is_Iterator;
13695
13696 ----------------------------
13697 -- Is_Iterator_Over_Array --
13698 ----------------------------
13699
13700 function Is_Iterator_Over_Array (N : Node_Id) return Boolean is
13701 Container : constant Node_Id := Name (N);
13702 Container_Typ : constant Entity_Id := Base_Type (Etype (Container));
13703 begin
13704 return Is_Array_Type (Container_Typ);
13705 end Is_Iterator_Over_Array;
13706
13707 ------------
13708 -- Is_LHS --
13709 ------------
13710
13711 -- We seem to have a lot of overlapping functions that do similar things
13712 -- (testing for left hand sides or lvalues???).
13713
13714 function Is_LHS (N : Node_Id) return Is_LHS_Result is
13715 P : constant Node_Id := Parent (N);
13716
13717 begin
13718 -- Return True if we are the left hand side of an assignment statement
13719
13720 if Nkind (P) = N_Assignment_Statement then
13721 if Name (P) = N then
13722 return Yes;
13723 else
13724 return No;
13725 end if;
13726
13727 -- Case of prefix of indexed or selected component or slice
13728
13729 elsif Nkind_In (P, N_Indexed_Component, N_Selected_Component, N_Slice)
13730 and then N = Prefix (P)
13731 then
13732 -- Here we have the case where the parent P is N.Q or N(Q .. R).
13733 -- If P is an LHS, then N is also effectively an LHS, but there
13734 -- is an important exception. If N is of an access type, then
13735 -- what we really have is N.all.Q (or N.all(Q .. R)). In either
13736 -- case this makes N.all a left hand side but not N itself.
13737
13738 -- If we don't know the type yet, this is the case where we return
13739 -- Unknown, since the answer depends on the type which is unknown.
13740
13741 if No (Etype (N)) then
13742 return Unknown;
13743
13744 -- We have an Etype set, so we can check it
13745
13746 elsif Is_Access_Type (Etype (N)) then
13747 return No;
13748
13749 -- OK, not access type case, so just test whole expression
13750
13751 else
13752 return Is_LHS (P);
13753 end if;
13754
13755 -- All other cases are not left hand sides
13756
13757 else
13758 return No;
13759 end if;
13760 end Is_LHS;
13761
13762 -----------------------------
13763 -- Is_Library_Level_Entity --
13764 -----------------------------
13765
13766 function Is_Library_Level_Entity (E : Entity_Id) return Boolean is
13767 begin
13768 -- The following is a small optimization, and it also properly handles
13769 -- discriminals, which in task bodies might appear in expressions before
13770 -- the corresponding procedure has been created, and which therefore do
13771 -- not have an assigned scope.
13772
13773 if Is_Formal (E) then
13774 return False;
13775 end if;
13776
13777 -- Normal test is simply that the enclosing dynamic scope is Standard
13778
13779 return Enclosing_Dynamic_Scope (E) = Standard_Standard;
13780 end Is_Library_Level_Entity;
13781
13782 --------------------------------
13783 -- Is_Limited_Class_Wide_Type --
13784 --------------------------------
13785
13786 function Is_Limited_Class_Wide_Type (Typ : Entity_Id) return Boolean is
13787 begin
13788 return
13789 Is_Class_Wide_Type (Typ)
13790 and then (Is_Limited_Type (Typ) or else From_Limited_With (Typ));
13791 end Is_Limited_Class_Wide_Type;
13792
13793 ---------------------------------
13794 -- Is_Local_Variable_Reference --
13795 ---------------------------------
13796
13797 function Is_Local_Variable_Reference (Expr : Node_Id) return Boolean is
13798 begin
13799 if not Is_Entity_Name (Expr) then
13800 return False;
13801
13802 else
13803 declare
13804 Ent : constant Entity_Id := Entity (Expr);
13805 Sub : constant Entity_Id := Enclosing_Subprogram (Ent);
13806 begin
13807 if not Ekind_In (Ent, E_Variable, E_In_Out_Parameter) then
13808 return False;
13809 else
13810 return Present (Sub) and then Sub = Current_Subprogram;
13811 end if;
13812 end;
13813 end if;
13814 end Is_Local_Variable_Reference;
13815
13816 -----------------------
13817 -- Is_Name_Reference --
13818 -----------------------
13819
13820 function Is_Name_Reference (N : Node_Id) return Boolean is
13821 begin
13822 if Is_Entity_Name (N) then
13823 return Present (Entity (N)) and then Is_Object (Entity (N));
13824 end if;
13825
13826 case Nkind (N) is
13827 when N_Indexed_Component
13828 | N_Slice
13829 =>
13830 return
13831 Is_Name_Reference (Prefix (N))
13832 or else Is_Access_Type (Etype (Prefix (N)));
13833
13834 -- Attributes 'Input, 'Old and 'Result produce objects
13835
13836 when N_Attribute_Reference =>
13837 return
13838 Nam_In (Attribute_Name (N), Name_Input, Name_Old, Name_Result);
13839
13840 when N_Selected_Component =>
13841 return
13842 Is_Name_Reference (Selector_Name (N))
13843 and then
13844 (Is_Name_Reference (Prefix (N))
13845 or else Is_Access_Type (Etype (Prefix (N))));
13846
13847 when N_Explicit_Dereference =>
13848 return True;
13849
13850 -- A view conversion of a tagged name is a name reference
13851
13852 when N_Type_Conversion =>
13853 return
13854 Is_Tagged_Type (Etype (Subtype_Mark (N)))
13855 and then Is_Tagged_Type (Etype (Expression (N)))
13856 and then Is_Name_Reference (Expression (N));
13857
13858 -- An unchecked type conversion is considered to be a name if the
13859 -- operand is a name (this construction arises only as a result of
13860 -- expansion activities).
13861
13862 when N_Unchecked_Type_Conversion =>
13863 return Is_Name_Reference (Expression (N));
13864
13865 when others =>
13866 return False;
13867 end case;
13868 end Is_Name_Reference;
13869
13870 ---------------------------------
13871 -- Is_Nontrivial_DIC_Procedure --
13872 ---------------------------------
13873
13874 function Is_Nontrivial_DIC_Procedure (Id : Entity_Id) return Boolean is
13875 Body_Decl : Node_Id;
13876 Stmt : Node_Id;
13877
13878 begin
13879 if Ekind (Id) = E_Procedure and then Is_DIC_Procedure (Id) then
13880 Body_Decl :=
13881 Unit_Declaration_Node
13882 (Corresponding_Body (Unit_Declaration_Node (Id)));
13883
13884 -- The body of the Default_Initial_Condition procedure must contain
13885 -- at least one statement, otherwise the generation of the subprogram
13886 -- body failed.
13887
13888 pragma Assert (Present (Handled_Statement_Sequence (Body_Decl)));
13889
13890 -- To qualify as nontrivial, the first statement of the procedure
13891 -- must be a check in the form of an if statement. If the original
13892 -- Default_Initial_Condition expression was folded, then the first
13893 -- statement is not a check.
13894
13895 Stmt := First (Statements (Handled_Statement_Sequence (Body_Decl)));
13896
13897 return
13898 Nkind (Stmt) = N_If_Statement
13899 and then Nkind (Original_Node (Stmt)) = N_Pragma;
13900 end if;
13901
13902 return False;
13903 end Is_Nontrivial_DIC_Procedure;
13904
13905 -------------------------
13906 -- Is_Null_Record_Type --
13907 -------------------------
13908
13909 function Is_Null_Record_Type (T : Entity_Id) return Boolean is
13910 Decl : constant Node_Id := Parent (T);
13911 begin
13912 return Nkind (Decl) = N_Full_Type_Declaration
13913 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
13914 and then
13915 (No (Component_List (Type_Definition (Decl)))
13916 or else Null_Present (Component_List (Type_Definition (Decl))));
13917 end Is_Null_Record_Type;
13918
13919 -------------------------
13920 -- Is_Object_Reference --
13921 -------------------------
13922
13923 function Is_Object_Reference (N : Node_Id) return Boolean is
13924 function Is_Internally_Generated_Renaming (N : Node_Id) return Boolean;
13925 -- Determine whether N is the name of an internally-generated renaming
13926
13927 --------------------------------------
13928 -- Is_Internally_Generated_Renaming --
13929 --------------------------------------
13930
13931 function Is_Internally_Generated_Renaming (N : Node_Id) return Boolean is
13932 P : Node_Id;
13933
13934 begin
13935 P := N;
13936 while Present (P) loop
13937 if Nkind (P) = N_Object_Renaming_Declaration then
13938 return not Comes_From_Source (P);
13939 elsif Is_List_Member (P) then
13940 return False;
13941 end if;
13942
13943 P := Parent (P);
13944 end loop;
13945
13946 return False;
13947 end Is_Internally_Generated_Renaming;
13948
13949 -- Start of processing for Is_Object_Reference
13950
13951 begin
13952 if Is_Entity_Name (N) then
13953 return Present (Entity (N)) and then Is_Object (Entity (N));
13954
13955 else
13956 case Nkind (N) is
13957 when N_Indexed_Component
13958 | N_Slice
13959 =>
13960 return
13961 Is_Object_Reference (Prefix (N))
13962 or else Is_Access_Type (Etype (Prefix (N)));
13963
13964 -- In Ada 95, a function call is a constant object; a procedure
13965 -- call is not.
13966
13967 when N_Function_Call =>
13968 return Etype (N) /= Standard_Void_Type;
13969
13970 -- Attributes 'Input, 'Loop_Entry, 'Old, and 'Result produce
13971 -- objects.
13972
13973 when N_Attribute_Reference =>
13974 return
13975 Nam_In (Attribute_Name (N), Name_Input,
13976 Name_Loop_Entry,
13977 Name_Old,
13978 Name_Result);
13979
13980 when N_Selected_Component =>
13981 return
13982 Is_Object_Reference (Selector_Name (N))
13983 and then
13984 (Is_Object_Reference (Prefix (N))
13985 or else Is_Access_Type (Etype (Prefix (N))));
13986
13987 -- An explicit dereference denotes an object, except that a
13988 -- conditional expression gets turned into an explicit dereference
13989 -- in some cases, and conditional expressions are not object
13990 -- names.
13991
13992 when N_Explicit_Dereference =>
13993 return not Nkind_In (Original_Node (N), N_Case_Expression,
13994 N_If_Expression);
13995
13996 -- A view conversion of a tagged object is an object reference
13997
13998 when N_Type_Conversion =>
13999 return Is_Tagged_Type (Etype (Subtype_Mark (N)))
14000 and then Is_Tagged_Type (Etype (Expression (N)))
14001 and then Is_Object_Reference (Expression (N));
14002
14003 -- An unchecked type conversion is considered to be an object if
14004 -- the operand is an object (this construction arises only as a
14005 -- result of expansion activities).
14006
14007 when N_Unchecked_Type_Conversion =>
14008 return True;
14009
14010 -- Allow string literals to act as objects as long as they appear
14011 -- in internally-generated renamings. The expansion of iterators
14012 -- may generate such renamings when the range involves a string
14013 -- literal.
14014
14015 when N_String_Literal =>
14016 return Is_Internally_Generated_Renaming (Parent (N));
14017
14018 -- AI05-0003: In Ada 2012 a qualified expression is a name.
14019 -- This allows disambiguation of function calls and the use
14020 -- of aggregates in more contexts.
14021
14022 when N_Qualified_Expression =>
14023 if Ada_Version < Ada_2012 then
14024 return False;
14025 else
14026 return Is_Object_Reference (Expression (N))
14027 or else Nkind (Expression (N)) = N_Aggregate;
14028 end if;
14029
14030 when others =>
14031 return False;
14032 end case;
14033 end if;
14034 end Is_Object_Reference;
14035
14036 -----------------------------------
14037 -- Is_OK_Variable_For_Out_Formal --
14038 -----------------------------------
14039
14040 function Is_OK_Variable_For_Out_Formal (AV : Node_Id) return Boolean is
14041 begin
14042 Note_Possible_Modification (AV, Sure => True);
14043
14044 -- We must reject parenthesized variable names. Comes_From_Source is
14045 -- checked because there are currently cases where the compiler violates
14046 -- this rule (e.g. passing a task object to its controlled Initialize
14047 -- routine). This should be properly documented in sinfo???
14048
14049 if Paren_Count (AV) > 0 and then Comes_From_Source (AV) then
14050 return False;
14051
14052 -- A variable is always allowed
14053
14054 elsif Is_Variable (AV) then
14055 return True;
14056
14057 -- Generalized indexing operations are rewritten as explicit
14058 -- dereferences, and it is only during resolution that we can
14059 -- check whether the context requires an access_to_variable type.
14060
14061 elsif Nkind (AV) = N_Explicit_Dereference
14062 and then Ada_Version >= Ada_2012
14063 and then Nkind (Original_Node (AV)) = N_Indexed_Component
14064 and then Present (Etype (Original_Node (AV)))
14065 and then Has_Implicit_Dereference (Etype (Original_Node (AV)))
14066 then
14067 return not Is_Access_Constant (Etype (Prefix (AV)));
14068
14069 -- Unchecked conversions are allowed only if they come from the
14070 -- generated code, which sometimes uses unchecked conversions for out
14071 -- parameters in cases where code generation is unaffected. We tell
14072 -- source unchecked conversions by seeing if they are rewrites of
14073 -- an original Unchecked_Conversion function call, or of an explicit
14074 -- conversion of a function call or an aggregate (as may happen in the
14075 -- expansion of a packed array aggregate).
14076
14077 elsif Nkind (AV) = N_Unchecked_Type_Conversion then
14078 if Nkind_In (Original_Node (AV), N_Function_Call, N_Aggregate) then
14079 return False;
14080
14081 elsif Comes_From_Source (AV)
14082 and then Nkind (Original_Node (Expression (AV))) = N_Function_Call
14083 then
14084 return False;
14085
14086 elsif Nkind (Original_Node (AV)) = N_Type_Conversion then
14087 return Is_OK_Variable_For_Out_Formal (Expression (AV));
14088
14089 else
14090 return True;
14091 end if;
14092
14093 -- Normal type conversions are allowed if argument is a variable
14094
14095 elsif Nkind (AV) = N_Type_Conversion then
14096 if Is_Variable (Expression (AV))
14097 and then Paren_Count (Expression (AV)) = 0
14098 then
14099 Note_Possible_Modification (Expression (AV), Sure => True);
14100 return True;
14101
14102 -- We also allow a non-parenthesized expression that raises
14103 -- constraint error if it rewrites what used to be a variable
14104
14105 elsif Raises_Constraint_Error (Expression (AV))
14106 and then Paren_Count (Expression (AV)) = 0
14107 and then Is_Variable (Original_Node (Expression (AV)))
14108 then
14109 return True;
14110
14111 -- Type conversion of something other than a variable
14112
14113 else
14114 return False;
14115 end if;
14116
14117 -- If this node is rewritten, then test the original form, if that is
14118 -- OK, then we consider the rewritten node OK (for example, if the
14119 -- original node is a conversion, then Is_Variable will not be true
14120 -- but we still want to allow the conversion if it converts a variable).
14121
14122 elsif Original_Node (AV) /= AV then
14123
14124 -- In Ada 2012, the explicit dereference may be a rewritten call to a
14125 -- Reference function.
14126
14127 if Ada_Version >= Ada_2012
14128 and then Nkind (Original_Node (AV)) = N_Function_Call
14129 and then
14130 Has_Implicit_Dereference (Etype (Name (Original_Node (AV))))
14131 then
14132
14133 -- Check that this is not a constant reference.
14134
14135 return not Is_Access_Constant (Etype (Prefix (AV)));
14136
14137 elsif Has_Implicit_Dereference (Etype (Original_Node (AV))) then
14138 return
14139 not Is_Access_Constant (Etype
14140 (Get_Reference_Discriminant (Etype (Original_Node (AV)))));
14141
14142 else
14143 return Is_OK_Variable_For_Out_Formal (Original_Node (AV));
14144 end if;
14145
14146 -- All other non-variables are rejected
14147
14148 else
14149 return False;
14150 end if;
14151 end Is_OK_Variable_For_Out_Formal;
14152
14153 ----------------------------
14154 -- Is_OK_Volatile_Context --
14155 ----------------------------
14156
14157 function Is_OK_Volatile_Context
14158 (Context : Node_Id;
14159 Obj_Ref : Node_Id) return Boolean
14160 is
14161 function Is_Protected_Operation_Call (Nod : Node_Id) return Boolean;
14162 -- Determine whether an arbitrary node denotes a call to a protected
14163 -- entry, function, or procedure in prefixed form where the prefix is
14164 -- Obj_Ref.
14165
14166 function Within_Check (Nod : Node_Id) return Boolean;
14167 -- Determine whether an arbitrary node appears in a check node
14168
14169 function Within_Subprogram_Call (Nod : Node_Id) return Boolean;
14170 -- Determine whether an arbitrary node appears in an entry, function, or
14171 -- procedure call.
14172
14173 function Within_Volatile_Function (Id : Entity_Id) return Boolean;
14174 -- Determine whether an arbitrary entity appears in a volatile function
14175
14176 ---------------------------------
14177 -- Is_Protected_Operation_Call --
14178 ---------------------------------
14179
14180 function Is_Protected_Operation_Call (Nod : Node_Id) return Boolean is
14181 Pref : Node_Id;
14182 Subp : Node_Id;
14183
14184 begin
14185 -- A call to a protected operations retains its selected component
14186 -- form as opposed to other prefixed calls that are transformed in
14187 -- expanded names.
14188
14189 if Nkind (Nod) = N_Selected_Component then
14190 Pref := Prefix (Nod);
14191 Subp := Selector_Name (Nod);
14192
14193 return
14194 Pref = Obj_Ref
14195 and then Present (Etype (Pref))
14196 and then Is_Protected_Type (Etype (Pref))
14197 and then Is_Entity_Name (Subp)
14198 and then Present (Entity (Subp))
14199 and then Ekind_In (Entity (Subp), E_Entry,
14200 E_Entry_Family,
14201 E_Function,
14202 E_Procedure);
14203 else
14204 return False;
14205 end if;
14206 end Is_Protected_Operation_Call;
14207
14208 ------------------
14209 -- Within_Check --
14210 ------------------
14211
14212 function Within_Check (Nod : Node_Id) return Boolean is
14213 Par : Node_Id;
14214
14215 begin
14216 -- Climb the parent chain looking for a check node
14217
14218 Par := Nod;
14219 while Present (Par) loop
14220 if Nkind (Par) in N_Raise_xxx_Error then
14221 return True;
14222
14223 -- Prevent the search from going too far
14224
14225 elsif Is_Body_Or_Package_Declaration (Par) then
14226 exit;
14227 end if;
14228
14229 Par := Parent (Par);
14230 end loop;
14231
14232 return False;
14233 end Within_Check;
14234
14235 ----------------------------
14236 -- Within_Subprogram_Call --
14237 ----------------------------
14238
14239 function Within_Subprogram_Call (Nod : Node_Id) return Boolean is
14240 Par : Node_Id;
14241
14242 begin
14243 -- Climb the parent chain looking for a function or procedure call
14244
14245 Par := Nod;
14246 while Present (Par) loop
14247 if Nkind_In (Par, N_Entry_Call_Statement,
14248 N_Function_Call,
14249 N_Procedure_Call_Statement)
14250 then
14251 return True;
14252
14253 -- Prevent the search from going too far
14254
14255 elsif Is_Body_Or_Package_Declaration (Par) then
14256 exit;
14257 end if;
14258
14259 Par := Parent (Par);
14260 end loop;
14261
14262 return False;
14263 end Within_Subprogram_Call;
14264
14265 ------------------------------
14266 -- Within_Volatile_Function --
14267 ------------------------------
14268
14269 function Within_Volatile_Function (Id : Entity_Id) return Boolean is
14270 Func_Id : Entity_Id;
14271
14272 begin
14273 -- Traverse the scope stack looking for a [generic] function
14274
14275 Func_Id := Id;
14276 while Present (Func_Id) and then Func_Id /= Standard_Standard loop
14277 if Ekind_In (Func_Id, E_Function, E_Generic_Function) then
14278 return Is_Volatile_Function (Func_Id);
14279 end if;
14280
14281 Func_Id := Scope (Func_Id);
14282 end loop;
14283
14284 return False;
14285 end Within_Volatile_Function;
14286
14287 -- Local variables
14288
14289 Obj_Id : Entity_Id;
14290
14291 -- Start of processing for Is_OK_Volatile_Context
14292
14293 begin
14294 -- The volatile object appears on either side of an assignment
14295
14296 if Nkind (Context) = N_Assignment_Statement then
14297 return True;
14298
14299 -- The volatile object is part of the initialization expression of
14300 -- another object.
14301
14302 elsif Nkind (Context) = N_Object_Declaration
14303 and then Present (Expression (Context))
14304 and then Expression (Context) = Obj_Ref
14305 then
14306 Obj_Id := Defining_Entity (Context);
14307
14308 -- The volatile object acts as the initialization expression of an
14309 -- extended return statement. This is valid context as long as the
14310 -- function is volatile.
14311
14312 if Is_Return_Object (Obj_Id) then
14313 return Within_Volatile_Function (Obj_Id);
14314
14315 -- Otherwise this is a normal object initialization
14316
14317 else
14318 return True;
14319 end if;
14320
14321 -- The volatile object acts as the name of a renaming declaration
14322
14323 elsif Nkind (Context) = N_Object_Renaming_Declaration
14324 and then Name (Context) = Obj_Ref
14325 then
14326 return True;
14327
14328 -- The volatile object appears as an actual parameter in a call to an
14329 -- instance of Unchecked_Conversion whose result is renamed.
14330
14331 elsif Nkind (Context) = N_Function_Call
14332 and then Is_Entity_Name (Name (Context))
14333 and then Is_Unchecked_Conversion_Instance (Entity (Name (Context)))
14334 and then Nkind (Parent (Context)) = N_Object_Renaming_Declaration
14335 then
14336 return True;
14337
14338 -- The volatile object is actually the prefix in a protected entry,
14339 -- function, or procedure call.
14340
14341 elsif Is_Protected_Operation_Call (Context) then
14342 return True;
14343
14344 -- The volatile object appears as the expression of a simple return
14345 -- statement that applies to a volatile function.
14346
14347 elsif Nkind (Context) = N_Simple_Return_Statement
14348 and then Expression (Context) = Obj_Ref
14349 then
14350 return
14351 Within_Volatile_Function (Return_Statement_Entity (Context));
14352
14353 -- The volatile object appears as the prefix of a name occurring in a
14354 -- non-interfering context.
14355
14356 elsif Nkind_In (Context, N_Attribute_Reference,
14357 N_Explicit_Dereference,
14358 N_Indexed_Component,
14359 N_Selected_Component,
14360 N_Slice)
14361 and then Prefix (Context) = Obj_Ref
14362 and then Is_OK_Volatile_Context
14363 (Context => Parent (Context),
14364 Obj_Ref => Context)
14365 then
14366 return True;
14367
14368 -- The volatile object appears as the prefix of attributes Address,
14369 -- Alignment, Component_Size, First_Bit, Last_Bit, Position, Size,
14370 -- Storage_Size.
14371
14372 elsif Nkind (Context) = N_Attribute_Reference
14373 and then Prefix (Context) = Obj_Ref
14374 and then Nam_In (Attribute_Name (Context), Name_Address,
14375 Name_Alignment,
14376 Name_Component_Size,
14377 Name_First_Bit,
14378 Name_Last_Bit,
14379 Name_Position,
14380 Name_Size,
14381 Name_Storage_Size)
14382 then
14383 return True;
14384
14385 -- The volatile object appears as the expression of a type conversion
14386 -- occurring in a non-interfering context.
14387
14388 elsif Nkind_In (Context, N_Type_Conversion,
14389 N_Unchecked_Type_Conversion)
14390 and then Expression (Context) = Obj_Ref
14391 and then Is_OK_Volatile_Context
14392 (Context => Parent (Context),
14393 Obj_Ref => Context)
14394 then
14395 return True;
14396
14397 -- The volatile object appears as the expression in a delay statement
14398
14399 elsif Nkind (Context) in N_Delay_Statement then
14400 return True;
14401
14402 -- Allow references to volatile objects in various checks. This is not a
14403 -- direct SPARK 2014 requirement.
14404
14405 elsif Within_Check (Context) then
14406 return True;
14407
14408 -- Assume that references to effectively volatile objects that appear
14409 -- as actual parameters in a subprogram call are always legal. A full
14410 -- legality check is done when the actuals are resolved (see routine
14411 -- Resolve_Actuals).
14412
14413 elsif Within_Subprogram_Call (Context) then
14414 return True;
14415
14416 -- Otherwise the context is not suitable for an effectively volatile
14417 -- object.
14418
14419 else
14420 return False;
14421 end if;
14422 end Is_OK_Volatile_Context;
14423
14424 ------------------------------------
14425 -- Is_Package_Contract_Annotation --
14426 ------------------------------------
14427
14428 function Is_Package_Contract_Annotation (Item : Node_Id) return Boolean is
14429 Nam : Name_Id;
14430
14431 begin
14432 if Nkind (Item) = N_Aspect_Specification then
14433 Nam := Chars (Identifier (Item));
14434
14435 else pragma Assert (Nkind (Item) = N_Pragma);
14436 Nam := Pragma_Name (Item);
14437 end if;
14438
14439 return Nam = Name_Abstract_State
14440 or else Nam = Name_Initial_Condition
14441 or else Nam = Name_Initializes
14442 or else Nam = Name_Refined_State;
14443 end Is_Package_Contract_Annotation;
14444
14445 -----------------------------------
14446 -- Is_Partially_Initialized_Type --
14447 -----------------------------------
14448
14449 function Is_Partially_Initialized_Type
14450 (Typ : Entity_Id;
14451 Include_Implicit : Boolean := True) return Boolean
14452 is
14453 begin
14454 if Is_Scalar_Type (Typ) then
14455 return False;
14456
14457 elsif Is_Access_Type (Typ) then
14458 return Include_Implicit;
14459
14460 elsif Is_Array_Type (Typ) then
14461
14462 -- If component type is partially initialized, so is array type
14463
14464 if Is_Partially_Initialized_Type
14465 (Component_Type (Typ), Include_Implicit)
14466 then
14467 return True;
14468
14469 -- Otherwise we are only partially initialized if we are fully
14470 -- initialized (this is the empty array case, no point in us
14471 -- duplicating that code here).
14472
14473 else
14474 return Is_Fully_Initialized_Type (Typ);
14475 end if;
14476
14477 elsif Is_Record_Type (Typ) then
14478
14479 -- A discriminated type is always partially initialized if in
14480 -- all mode
14481
14482 if Has_Discriminants (Typ) and then Include_Implicit then
14483 return True;
14484
14485 -- A tagged type is always partially initialized
14486
14487 elsif Is_Tagged_Type (Typ) then
14488 return True;
14489
14490 -- Case of non-discriminated record
14491
14492 else
14493 declare
14494 Ent : Entity_Id;
14495
14496 Component_Present : Boolean := False;
14497 -- Set True if at least one component is present. If no
14498 -- components are present, then record type is fully
14499 -- initialized (another odd case, like the null array).
14500
14501 begin
14502 -- Loop through components
14503
14504 Ent := First_Entity (Typ);
14505 while Present (Ent) loop
14506 if Ekind (Ent) = E_Component then
14507 Component_Present := True;
14508
14509 -- If a component has an initialization expression then
14510 -- the enclosing record type is partially initialized
14511
14512 if Present (Parent (Ent))
14513 and then Present (Expression (Parent (Ent)))
14514 then
14515 return True;
14516
14517 -- If a component is of a type which is itself partially
14518 -- initialized, then the enclosing record type is also.
14519
14520 elsif Is_Partially_Initialized_Type
14521 (Etype (Ent), Include_Implicit)
14522 then
14523 return True;
14524 end if;
14525 end if;
14526
14527 Next_Entity (Ent);
14528 end loop;
14529
14530 -- No initialized components found. If we found any components
14531 -- they were all uninitialized so the result is false.
14532
14533 if Component_Present then
14534 return False;
14535
14536 -- But if we found no components, then all the components are
14537 -- initialized so we consider the type to be initialized.
14538
14539 else
14540 return True;
14541 end if;
14542 end;
14543 end if;
14544
14545 -- Concurrent types are always fully initialized
14546
14547 elsif Is_Concurrent_Type (Typ) then
14548 return True;
14549
14550 -- For a private type, go to underlying type. If there is no underlying
14551 -- type then just assume this partially initialized. Not clear if this
14552 -- can happen in a non-error case, but no harm in testing for this.
14553
14554 elsif Is_Private_Type (Typ) then
14555 declare
14556 U : constant Entity_Id := Underlying_Type (Typ);
14557 begin
14558 if No (U) then
14559 return True;
14560 else
14561 return Is_Partially_Initialized_Type (U, Include_Implicit);
14562 end if;
14563 end;
14564
14565 -- For any other type (are there any?) assume partially initialized
14566
14567 else
14568 return True;
14569 end if;
14570 end Is_Partially_Initialized_Type;
14571
14572 ------------------------------------
14573 -- Is_Potentially_Persistent_Type --
14574 ------------------------------------
14575
14576 function Is_Potentially_Persistent_Type (T : Entity_Id) return Boolean is
14577 Comp : Entity_Id;
14578 Indx : Node_Id;
14579
14580 begin
14581 -- For private type, test corresponding full type
14582
14583 if Is_Private_Type (T) then
14584 return Is_Potentially_Persistent_Type (Full_View (T));
14585
14586 -- Scalar types are potentially persistent
14587
14588 elsif Is_Scalar_Type (T) then
14589 return True;
14590
14591 -- Record type is potentially persistent if not tagged and the types of
14592 -- all it components are potentially persistent, and no component has
14593 -- an initialization expression.
14594
14595 elsif Is_Record_Type (T)
14596 and then not Is_Tagged_Type (T)
14597 and then not Is_Partially_Initialized_Type (T)
14598 then
14599 Comp := First_Component (T);
14600 while Present (Comp) loop
14601 if not Is_Potentially_Persistent_Type (Etype (Comp)) then
14602 return False;
14603 else
14604 Next_Entity (Comp);
14605 end if;
14606 end loop;
14607
14608 return True;
14609
14610 -- Array type is potentially persistent if its component type is
14611 -- potentially persistent and if all its constraints are static.
14612
14613 elsif Is_Array_Type (T) then
14614 if not Is_Potentially_Persistent_Type (Component_Type (T)) then
14615 return False;
14616 end if;
14617
14618 Indx := First_Index (T);
14619 while Present (Indx) loop
14620 if not Is_OK_Static_Subtype (Etype (Indx)) then
14621 return False;
14622 else
14623 Next_Index (Indx);
14624 end if;
14625 end loop;
14626
14627 return True;
14628
14629 -- All other types are not potentially persistent
14630
14631 else
14632 return False;
14633 end if;
14634 end Is_Potentially_Persistent_Type;
14635
14636 --------------------------------
14637 -- Is_Potentially_Unevaluated --
14638 --------------------------------
14639
14640 function Is_Potentially_Unevaluated (N : Node_Id) return Boolean is
14641 Par : Node_Id;
14642 Expr : Node_Id;
14643
14644 begin
14645 Expr := N;
14646 Par := Parent (N);
14647
14648 -- A postcondition whose expression is a short-circuit is broken down
14649 -- into individual aspects for better exception reporting. The original
14650 -- short-circuit expression is rewritten as the second operand, and an
14651 -- occurrence of 'Old in that operand is potentially unevaluated.
14652 -- See Sem_ch13.adb for details of this transformation.
14653
14654 if Nkind (Original_Node (Par)) = N_And_Then then
14655 return True;
14656 end if;
14657
14658 while not Nkind_In (Par, N_If_Expression,
14659 N_Case_Expression,
14660 N_And_Then,
14661 N_Or_Else,
14662 N_In,
14663 N_Not_In,
14664 N_Quantified_Expression)
14665 loop
14666 Expr := Par;
14667 Par := Parent (Par);
14668
14669 -- If the context is not an expression, or if is the result of
14670 -- expansion of an enclosing construct (such as another attribute)
14671 -- the predicate does not apply.
14672
14673 if Nkind (Par) = N_Case_Expression_Alternative then
14674 null;
14675
14676 elsif Nkind (Par) not in N_Subexpr
14677 or else not Comes_From_Source (Par)
14678 then
14679 return False;
14680 end if;
14681 end loop;
14682
14683 if Nkind (Par) = N_If_Expression then
14684 return Is_Elsif (Par) or else Expr /= First (Expressions (Par));
14685
14686 elsif Nkind (Par) = N_Case_Expression then
14687 return Expr /= Expression (Par);
14688
14689 elsif Nkind_In (Par, N_And_Then, N_Or_Else) then
14690 return Expr = Right_Opnd (Par);
14691
14692 elsif Nkind_In (Par, N_In, N_Not_In) then
14693
14694 -- If the membership includes several alternatives, only the first is
14695 -- definitely evaluated.
14696
14697 if Present (Alternatives (Par)) then
14698 return Expr /= First (Alternatives (Par));
14699
14700 -- If this is a range membership both bounds are evaluated
14701
14702 else
14703 return False;
14704 end if;
14705
14706 elsif Nkind (Par) = N_Quantified_Expression then
14707 return Expr = Condition (Par);
14708
14709 else
14710 return False;
14711 end if;
14712 end Is_Potentially_Unevaluated;
14713
14714 ---------------------------------
14715 -- Is_Protected_Self_Reference --
14716 ---------------------------------
14717
14718 function Is_Protected_Self_Reference (N : Node_Id) return Boolean is
14719
14720 function In_Access_Definition (N : Node_Id) return Boolean;
14721 -- Returns true if N belongs to an access definition
14722
14723 --------------------------
14724 -- In_Access_Definition --
14725 --------------------------
14726
14727 function In_Access_Definition (N : Node_Id) return Boolean is
14728 P : Node_Id;
14729
14730 begin
14731 P := Parent (N);
14732 while Present (P) loop
14733 if Nkind (P) = N_Access_Definition then
14734 return True;
14735 end if;
14736
14737 P := Parent (P);
14738 end loop;
14739
14740 return False;
14741 end In_Access_Definition;
14742
14743 -- Start of processing for Is_Protected_Self_Reference
14744
14745 begin
14746 -- Verify that prefix is analyzed and has the proper form. Note that
14747 -- the attributes Elab_Spec, Elab_Body, and Elab_Subp_Body, which also
14748 -- produce the address of an entity, do not analyze their prefix
14749 -- because they denote entities that are not necessarily visible.
14750 -- Neither of them can apply to a protected type.
14751
14752 return Ada_Version >= Ada_2005
14753 and then Is_Entity_Name (N)
14754 and then Present (Entity (N))
14755 and then Is_Protected_Type (Entity (N))
14756 and then In_Open_Scopes (Entity (N))
14757 and then not In_Access_Definition (N);
14758 end Is_Protected_Self_Reference;
14759
14760 -----------------------------
14761 -- Is_RCI_Pkg_Spec_Or_Body --
14762 -----------------------------
14763
14764 function Is_RCI_Pkg_Spec_Or_Body (Cunit : Node_Id) return Boolean is
14765
14766 function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean;
14767 -- Return True if the unit of Cunit is an RCI package declaration
14768
14769 ---------------------------
14770 -- Is_RCI_Pkg_Decl_Cunit --
14771 ---------------------------
14772
14773 function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean is
14774 The_Unit : constant Node_Id := Unit (Cunit);
14775
14776 begin
14777 if Nkind (The_Unit) /= N_Package_Declaration then
14778 return False;
14779 end if;
14780
14781 return Is_Remote_Call_Interface (Defining_Entity (The_Unit));
14782 end Is_RCI_Pkg_Decl_Cunit;
14783
14784 -- Start of processing for Is_RCI_Pkg_Spec_Or_Body
14785
14786 begin
14787 return Is_RCI_Pkg_Decl_Cunit (Cunit)
14788 or else
14789 (Nkind (Unit (Cunit)) = N_Package_Body
14790 and then Is_RCI_Pkg_Decl_Cunit (Library_Unit (Cunit)));
14791 end Is_RCI_Pkg_Spec_Or_Body;
14792
14793 -----------------------------------------
14794 -- Is_Remote_Access_To_Class_Wide_Type --
14795 -----------------------------------------
14796
14797 function Is_Remote_Access_To_Class_Wide_Type
14798 (E : Entity_Id) return Boolean
14799 is
14800 begin
14801 -- A remote access to class-wide type is a general access to object type
14802 -- declared in the visible part of a Remote_Types or Remote_Call_
14803 -- Interface unit.
14804
14805 return Ekind (E) = E_General_Access_Type
14806 and then (Is_Remote_Call_Interface (E) or else Is_Remote_Types (E));
14807 end Is_Remote_Access_To_Class_Wide_Type;
14808
14809 -----------------------------------------
14810 -- Is_Remote_Access_To_Subprogram_Type --
14811 -----------------------------------------
14812
14813 function Is_Remote_Access_To_Subprogram_Type
14814 (E : Entity_Id) return Boolean
14815 is
14816 begin
14817 return (Ekind (E) = E_Access_Subprogram_Type
14818 or else (Ekind (E) = E_Record_Type
14819 and then Present (Corresponding_Remote_Type (E))))
14820 and then (Is_Remote_Call_Interface (E) or else Is_Remote_Types (E));
14821 end Is_Remote_Access_To_Subprogram_Type;
14822
14823 --------------------
14824 -- Is_Remote_Call --
14825 --------------------
14826
14827 function Is_Remote_Call (N : Node_Id) return Boolean is
14828 begin
14829 if Nkind (N) not in N_Subprogram_Call then
14830
14831 -- An entry call cannot be remote
14832
14833 return False;
14834
14835 elsif Nkind (Name (N)) in N_Has_Entity
14836 and then Is_Remote_Call_Interface (Entity (Name (N)))
14837 then
14838 -- A subprogram declared in the spec of a RCI package is remote
14839
14840 return True;
14841
14842 elsif Nkind (Name (N)) = N_Explicit_Dereference
14843 and then Is_Remote_Access_To_Subprogram_Type
14844 (Etype (Prefix (Name (N))))
14845 then
14846 -- The dereference of a RAS is a remote call
14847
14848 return True;
14849
14850 elsif Present (Controlling_Argument (N))
14851 and then Is_Remote_Access_To_Class_Wide_Type
14852 (Etype (Controlling_Argument (N)))
14853 then
14854 -- Any primitive operation call with a controlling argument of
14855 -- a RACW type is a remote call.
14856
14857 return True;
14858 end if;
14859
14860 -- All other calls are local calls
14861
14862 return False;
14863 end Is_Remote_Call;
14864
14865 ----------------------
14866 -- Is_Renamed_Entry --
14867 ----------------------
14868
14869 function Is_Renamed_Entry (Proc_Nam : Entity_Id) return Boolean is
14870 Orig_Node : Node_Id := Empty;
14871 Subp_Decl : Node_Id := Parent (Parent (Proc_Nam));
14872
14873 function Is_Entry (Nam : Node_Id) return Boolean;
14874 -- Determine whether Nam is an entry. Traverse selectors if there are
14875 -- nested selected components.
14876
14877 --------------
14878 -- Is_Entry --
14879 --------------
14880
14881 function Is_Entry (Nam : Node_Id) return Boolean is
14882 begin
14883 if Nkind (Nam) = N_Selected_Component then
14884 return Is_Entry (Selector_Name (Nam));
14885 end if;
14886
14887 return Ekind (Entity (Nam)) = E_Entry;
14888 end Is_Entry;
14889
14890 -- Start of processing for Is_Renamed_Entry
14891
14892 begin
14893 if Present (Alias (Proc_Nam)) then
14894 Subp_Decl := Parent (Parent (Alias (Proc_Nam)));
14895 end if;
14896
14897 -- Look for a rewritten subprogram renaming declaration
14898
14899 if Nkind (Subp_Decl) = N_Subprogram_Declaration
14900 and then Present (Original_Node (Subp_Decl))
14901 then
14902 Orig_Node := Original_Node (Subp_Decl);
14903 end if;
14904
14905 -- The rewritten subprogram is actually an entry
14906
14907 if Present (Orig_Node)
14908 and then Nkind (Orig_Node) = N_Subprogram_Renaming_Declaration
14909 and then Is_Entry (Name (Orig_Node))
14910 then
14911 return True;
14912 end if;
14913
14914 return False;
14915 end Is_Renamed_Entry;
14916
14917 -----------------------------
14918 -- Is_Renaming_Declaration --
14919 -----------------------------
14920
14921 function Is_Renaming_Declaration (N : Node_Id) return Boolean is
14922 begin
14923 case Nkind (N) is
14924 when N_Exception_Renaming_Declaration
14925 | N_Generic_Function_Renaming_Declaration
14926 | N_Generic_Package_Renaming_Declaration
14927 | N_Generic_Procedure_Renaming_Declaration
14928 | N_Object_Renaming_Declaration
14929 | N_Package_Renaming_Declaration
14930 | N_Subprogram_Renaming_Declaration
14931 =>
14932 return True;
14933
14934 when others =>
14935 return False;
14936 end case;
14937 end Is_Renaming_Declaration;
14938
14939 ----------------------------
14940 -- Is_Reversible_Iterator --
14941 ----------------------------
14942
14943 function Is_Reversible_Iterator (Typ : Entity_Id) return Boolean is
14944 Ifaces_List : Elist_Id;
14945 Iface_Elmt : Elmt_Id;
14946 Iface : Entity_Id;
14947
14948 begin
14949 if Is_Class_Wide_Type (Typ)
14950 and then Chars (Root_Type (Typ)) = Name_Reversible_Iterator
14951 and then Is_Predefined_File_Name
14952 (Unit_File_Name (Get_Source_Unit (Root_Type (Typ))))
14953 then
14954 return True;
14955
14956 elsif not Is_Tagged_Type (Typ) or else not Is_Derived_Type (Typ) then
14957 return False;
14958
14959 else
14960 Collect_Interfaces (Typ, Ifaces_List);
14961
14962 Iface_Elmt := First_Elmt (Ifaces_List);
14963 while Present (Iface_Elmt) loop
14964 Iface := Node (Iface_Elmt);
14965 if Chars (Iface) = Name_Reversible_Iterator
14966 and then
14967 Is_Predefined_File_Name
14968 (Unit_File_Name (Get_Source_Unit (Iface)))
14969 then
14970 return True;
14971 end if;
14972
14973 Next_Elmt (Iface_Elmt);
14974 end loop;
14975 end if;
14976
14977 return False;
14978 end Is_Reversible_Iterator;
14979
14980 ----------------------
14981 -- Is_Selector_Name --
14982 ----------------------
14983
14984 function Is_Selector_Name (N : Node_Id) return Boolean is
14985 begin
14986 if not Is_List_Member (N) then
14987 declare
14988 P : constant Node_Id := Parent (N);
14989 begin
14990 return Nkind_In (P, N_Expanded_Name,
14991 N_Generic_Association,
14992 N_Parameter_Association,
14993 N_Selected_Component)
14994 and then Selector_Name (P) = N;
14995 end;
14996
14997 else
14998 declare
14999 L : constant List_Id := List_Containing (N);
15000 P : constant Node_Id := Parent (L);
15001 begin
15002 return (Nkind (P) = N_Discriminant_Association
15003 and then Selector_Names (P) = L)
15004 or else
15005 (Nkind (P) = N_Component_Association
15006 and then Choices (P) = L);
15007 end;
15008 end if;
15009 end Is_Selector_Name;
15010
15011 ---------------------------------
15012 -- Is_Single_Concurrent_Object --
15013 ---------------------------------
15014
15015 function Is_Single_Concurrent_Object (Id : Entity_Id) return Boolean is
15016 begin
15017 return
15018 Is_Single_Protected_Object (Id) or else Is_Single_Task_Object (Id);
15019 end Is_Single_Concurrent_Object;
15020
15021 -------------------------------
15022 -- Is_Single_Concurrent_Type --
15023 -------------------------------
15024
15025 function Is_Single_Concurrent_Type (Id : Entity_Id) return Boolean is
15026 begin
15027 return
15028 Ekind_In (Id, E_Protected_Type, E_Task_Type)
15029 and then Is_Single_Concurrent_Type_Declaration
15030 (Declaration_Node (Id));
15031 end Is_Single_Concurrent_Type;
15032
15033 -------------------------------------------
15034 -- Is_Single_Concurrent_Type_Declaration --
15035 -------------------------------------------
15036
15037 function Is_Single_Concurrent_Type_Declaration
15038 (N : Node_Id) return Boolean
15039 is
15040 begin
15041 return Nkind_In (Original_Node (N), N_Single_Protected_Declaration,
15042 N_Single_Task_Declaration);
15043 end Is_Single_Concurrent_Type_Declaration;
15044
15045 ---------------------------------------------
15046 -- Is_Single_Precision_Floating_Point_Type --
15047 ---------------------------------------------
15048
15049 function Is_Single_Precision_Floating_Point_Type
15050 (E : Entity_Id) return Boolean is
15051 begin
15052 return Is_Floating_Point_Type (E)
15053 and then Machine_Radix_Value (E) = Uint_2
15054 and then Machine_Mantissa_Value (E) = Uint_24
15055 and then Machine_Emax_Value (E) = Uint_2 ** Uint_7
15056 and then Machine_Emin_Value (E) = Uint_3 - (Uint_2 ** Uint_7);
15057 end Is_Single_Precision_Floating_Point_Type;
15058
15059 --------------------------------
15060 -- Is_Single_Protected_Object --
15061 --------------------------------
15062
15063 function Is_Single_Protected_Object (Id : Entity_Id) return Boolean is
15064 begin
15065 return
15066 Ekind (Id) = E_Variable
15067 and then Ekind (Etype (Id)) = E_Protected_Type
15068 and then Is_Single_Concurrent_Type (Etype (Id));
15069 end Is_Single_Protected_Object;
15070
15071 ---------------------------
15072 -- Is_Single_Task_Object --
15073 ---------------------------
15074
15075 function Is_Single_Task_Object (Id : Entity_Id) return Boolean is
15076 begin
15077 return
15078 Ekind (Id) = E_Variable
15079 and then Ekind (Etype (Id)) = E_Task_Type
15080 and then Is_Single_Concurrent_Type (Etype (Id));
15081 end Is_Single_Task_Object;
15082
15083 -------------------------------------
15084 -- Is_SPARK_05_Initialization_Expr --
15085 -------------------------------------
15086
15087 function Is_SPARK_05_Initialization_Expr (N : Node_Id) return Boolean is
15088 Is_Ok : Boolean;
15089 Expr : Node_Id;
15090 Comp_Assn : Node_Id;
15091 Orig_N : constant Node_Id := Original_Node (N);
15092
15093 begin
15094 Is_Ok := True;
15095
15096 if not Comes_From_Source (Orig_N) then
15097 goto Done;
15098 end if;
15099
15100 pragma Assert (Nkind (Orig_N) in N_Subexpr);
15101
15102 case Nkind (Orig_N) is
15103 when N_Character_Literal
15104 | N_Integer_Literal
15105 | N_Real_Literal
15106 | N_String_Literal
15107 =>
15108 null;
15109
15110 when N_Expanded_Name
15111 | N_Identifier
15112 =>
15113 if Is_Entity_Name (Orig_N)
15114 and then Present (Entity (Orig_N)) -- needed in some cases
15115 then
15116 case Ekind (Entity (Orig_N)) is
15117 when E_Constant
15118 | E_Enumeration_Literal
15119 | E_Named_Integer
15120 | E_Named_Real
15121 =>
15122 null;
15123
15124 when others =>
15125 if Is_Type (Entity (Orig_N)) then
15126 null;
15127 else
15128 Is_Ok := False;
15129 end if;
15130 end case;
15131 end if;
15132
15133 when N_Qualified_Expression
15134 | N_Type_Conversion
15135 =>
15136 Is_Ok := Is_SPARK_05_Initialization_Expr (Expression (Orig_N));
15137
15138 when N_Unary_Op =>
15139 Is_Ok := Is_SPARK_05_Initialization_Expr (Right_Opnd (Orig_N));
15140
15141 when N_Binary_Op
15142 | N_Membership_Test
15143 | N_Short_Circuit
15144 =>
15145 Is_Ok := Is_SPARK_05_Initialization_Expr (Left_Opnd (Orig_N))
15146 and then
15147 Is_SPARK_05_Initialization_Expr (Right_Opnd (Orig_N));
15148
15149 when N_Aggregate
15150 | N_Extension_Aggregate
15151 =>
15152 if Nkind (Orig_N) = N_Extension_Aggregate then
15153 Is_Ok :=
15154 Is_SPARK_05_Initialization_Expr (Ancestor_Part (Orig_N));
15155 end if;
15156
15157 Expr := First (Expressions (Orig_N));
15158 while Present (Expr) loop
15159 if not Is_SPARK_05_Initialization_Expr (Expr) then
15160 Is_Ok := False;
15161 goto Done;
15162 end if;
15163
15164 Next (Expr);
15165 end loop;
15166
15167 Comp_Assn := First (Component_Associations (Orig_N));
15168 while Present (Comp_Assn) loop
15169 Expr := Expression (Comp_Assn);
15170
15171 -- Note: test for Present here needed for box assocation
15172
15173 if Present (Expr)
15174 and then not Is_SPARK_05_Initialization_Expr (Expr)
15175 then
15176 Is_Ok := False;
15177 goto Done;
15178 end if;
15179
15180 Next (Comp_Assn);
15181 end loop;
15182
15183 when N_Attribute_Reference =>
15184 if Nkind (Prefix (Orig_N)) in N_Subexpr then
15185 Is_Ok := Is_SPARK_05_Initialization_Expr (Prefix (Orig_N));
15186 end if;
15187
15188 Expr := First (Expressions (Orig_N));
15189 while Present (Expr) loop
15190 if not Is_SPARK_05_Initialization_Expr (Expr) then
15191 Is_Ok := False;
15192 goto Done;
15193 end if;
15194
15195 Next (Expr);
15196 end loop;
15197
15198 -- Selected components might be expanded named not yet resolved, so
15199 -- default on the safe side. (Eg on sparklex.ads)
15200
15201 when N_Selected_Component =>
15202 null;
15203
15204 when others =>
15205 Is_Ok := False;
15206 end case;
15207
15208 <<Done>>
15209 return Is_Ok;
15210 end Is_SPARK_05_Initialization_Expr;
15211
15212 ----------------------------------
15213 -- Is_SPARK_05_Object_Reference --
15214 ----------------------------------
15215
15216 function Is_SPARK_05_Object_Reference (N : Node_Id) return Boolean is
15217 begin
15218 if Is_Entity_Name (N) then
15219 return Present (Entity (N))
15220 and then
15221 (Ekind_In (Entity (N), E_Constant, E_Variable)
15222 or else Ekind (Entity (N)) in Formal_Kind);
15223
15224 else
15225 case Nkind (N) is
15226 when N_Selected_Component =>
15227 return Is_SPARK_05_Object_Reference (Prefix (N));
15228
15229 when others =>
15230 return False;
15231 end case;
15232 end if;
15233 end Is_SPARK_05_Object_Reference;
15234
15235 -----------------------------
15236 -- Is_Specific_Tagged_Type --
15237 -----------------------------
15238
15239 function Is_Specific_Tagged_Type (Typ : Entity_Id) return Boolean is
15240 Full_Typ : Entity_Id;
15241
15242 begin
15243 -- Handle private types
15244
15245 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
15246 Full_Typ := Full_View (Typ);
15247 else
15248 Full_Typ := Typ;
15249 end if;
15250
15251 -- A specific tagged type is a non-class-wide tagged type
15252
15253 return Is_Tagged_Type (Full_Typ) and not Is_Class_Wide_Type (Full_Typ);
15254 end Is_Specific_Tagged_Type;
15255
15256 ------------------
15257 -- Is_Statement --
15258 ------------------
15259
15260 function Is_Statement (N : Node_Id) return Boolean is
15261 begin
15262 return
15263 Nkind (N) in N_Statement_Other_Than_Procedure_Call
15264 or else Nkind (N) = N_Procedure_Call_Statement;
15265 end Is_Statement;
15266
15267 ---------------------------------------
15268 -- Is_Subprogram_Contract_Annotation --
15269 ---------------------------------------
15270
15271 function Is_Subprogram_Contract_Annotation
15272 (Item : Node_Id) return Boolean
15273 is
15274 Nam : Name_Id;
15275
15276 begin
15277 if Nkind (Item) = N_Aspect_Specification then
15278 Nam := Chars (Identifier (Item));
15279
15280 else pragma Assert (Nkind (Item) = N_Pragma);
15281 Nam := Pragma_Name (Item);
15282 end if;
15283
15284 return Nam = Name_Contract_Cases
15285 or else Nam = Name_Depends
15286 or else Nam = Name_Extensions_Visible
15287 or else Nam = Name_Global
15288 or else Nam = Name_Post
15289 or else Nam = Name_Post_Class
15290 or else Nam = Name_Postcondition
15291 or else Nam = Name_Pre
15292 or else Nam = Name_Pre_Class
15293 or else Nam = Name_Precondition
15294 or else Nam = Name_Refined_Depends
15295 or else Nam = Name_Refined_Global
15296 or else Nam = Name_Refined_Post
15297 or else Nam = Name_Test_Case;
15298 end Is_Subprogram_Contract_Annotation;
15299
15300 --------------------------------------------------
15301 -- Is_Subprogram_Stub_Without_Prior_Declaration --
15302 --------------------------------------------------
15303
15304 function Is_Subprogram_Stub_Without_Prior_Declaration
15305 (N : Node_Id) return Boolean
15306 is
15307 begin
15308 -- A subprogram stub without prior declaration serves as declaration for
15309 -- the actual subprogram body. As such, it has an attached defining
15310 -- entity of E_[Generic_]Function or E_[Generic_]Procedure.
15311
15312 return Nkind (N) = N_Subprogram_Body_Stub
15313 and then Ekind (Defining_Entity (N)) /= E_Subprogram_Body;
15314 end Is_Subprogram_Stub_Without_Prior_Declaration;
15315
15316 --------------------------
15317 -- Is_Suspension_Object --
15318 --------------------------
15319
15320 function Is_Suspension_Object (Id : Entity_Id) return Boolean is
15321 begin
15322 -- This approach does an exact name match rather than to rely on
15323 -- RTSfind. Routine Is_Effectively_Volatile is used by clients of the
15324 -- front end at point where all auxiliary tables are locked and any
15325 -- modifications to them are treated as violations. Do not tamper with
15326 -- the tables, instead examine the Chars fields of all the scopes of Id.
15327
15328 return
15329 Chars (Id) = Name_Suspension_Object
15330 and then Present (Scope (Id))
15331 and then Chars (Scope (Id)) = Name_Synchronous_Task_Control
15332 and then Present (Scope (Scope (Id)))
15333 and then Chars (Scope (Scope (Id))) = Name_Ada
15334 and then Present (Scope (Scope (Scope (Id))))
15335 and then Scope (Scope (Scope (Id))) = Standard_Standard;
15336 end Is_Suspension_Object;
15337
15338 ----------------------------
15339 -- Is_Synchronized_Object --
15340 ----------------------------
15341
15342 function Is_Synchronized_Object (Id : Entity_Id) return Boolean is
15343 Prag : Node_Id;
15344
15345 begin
15346 if Is_Object (Id) then
15347
15348 -- The object is synchronized if it is of a type that yields a
15349 -- synchronized object.
15350
15351 if Yields_Synchronized_Object (Etype (Id)) then
15352 return True;
15353
15354 -- The object is synchronized if it is atomic and Async_Writers is
15355 -- enabled.
15356
15357 elsif Is_Atomic (Id) and then Async_Writers_Enabled (Id) then
15358 return True;
15359
15360 -- A constant is a synchronized object by default
15361
15362 elsif Ekind (Id) = E_Constant then
15363 return True;
15364
15365 -- A variable is a synchronized object if it is subject to pragma
15366 -- Constant_After_Elaboration.
15367
15368 elsif Ekind (Id) = E_Variable then
15369 Prag := Get_Pragma (Id, Pragma_Constant_After_Elaboration);
15370
15371 return Present (Prag) and then Is_Enabled_Pragma (Prag);
15372 end if;
15373 end if;
15374
15375 -- Otherwise the input is not an object or it does not qualify as a
15376 -- synchronized object.
15377
15378 return False;
15379 end Is_Synchronized_Object;
15380
15381 ---------------------------------
15382 -- Is_Synchronized_Tagged_Type --
15383 ---------------------------------
15384
15385 function Is_Synchronized_Tagged_Type (E : Entity_Id) return Boolean is
15386 Kind : constant Entity_Kind := Ekind (Base_Type (E));
15387
15388 begin
15389 -- A task or protected type derived from an interface is a tagged type.
15390 -- Such a tagged type is called a synchronized tagged type, as are
15391 -- synchronized interfaces and private extensions whose declaration
15392 -- includes the reserved word synchronized.
15393
15394 return (Is_Tagged_Type (E)
15395 and then (Kind = E_Task_Type
15396 or else
15397 Kind = E_Protected_Type))
15398 or else
15399 (Is_Interface (E)
15400 and then Is_Synchronized_Interface (E))
15401 or else
15402 (Ekind (E) = E_Record_Type_With_Private
15403 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
15404 and then (Synchronized_Present (Parent (E))
15405 or else Is_Synchronized_Interface (Etype (E))));
15406 end Is_Synchronized_Tagged_Type;
15407
15408 -----------------
15409 -- Is_Transfer --
15410 -----------------
15411
15412 function Is_Transfer (N : Node_Id) return Boolean is
15413 Kind : constant Node_Kind := Nkind (N);
15414
15415 begin
15416 if Kind = N_Simple_Return_Statement
15417 or else
15418 Kind = N_Extended_Return_Statement
15419 or else
15420 Kind = N_Goto_Statement
15421 or else
15422 Kind = N_Raise_Statement
15423 or else
15424 Kind = N_Requeue_Statement
15425 then
15426 return True;
15427
15428 elsif (Kind = N_Exit_Statement or else Kind in N_Raise_xxx_Error)
15429 and then No (Condition (N))
15430 then
15431 return True;
15432
15433 elsif Kind = N_Procedure_Call_Statement
15434 and then Is_Entity_Name (Name (N))
15435 and then Present (Entity (Name (N)))
15436 and then No_Return (Entity (Name (N)))
15437 then
15438 return True;
15439
15440 elsif Nkind (Original_Node (N)) = N_Raise_Statement then
15441 return True;
15442
15443 else
15444 return False;
15445 end if;
15446 end Is_Transfer;
15447
15448 -------------
15449 -- Is_True --
15450 -------------
15451
15452 function Is_True (U : Uint) return Boolean is
15453 begin
15454 return (U /= 0);
15455 end Is_True;
15456
15457 --------------------------------------
15458 -- Is_Unchecked_Conversion_Instance --
15459 --------------------------------------
15460
15461 function Is_Unchecked_Conversion_Instance (Id : Entity_Id) return Boolean is
15462 Par : Node_Id;
15463
15464 begin
15465 -- Look for a function whose generic parent is the predefined intrinsic
15466 -- function Unchecked_Conversion, or for one that renames such an
15467 -- instance.
15468
15469 if Ekind (Id) = E_Function then
15470 Par := Parent (Id);
15471
15472 if Nkind (Par) = N_Function_Specification then
15473 Par := Generic_Parent (Par);
15474
15475 if Present (Par) then
15476 return
15477 Chars (Par) = Name_Unchecked_Conversion
15478 and then Is_Intrinsic_Subprogram (Par)
15479 and then Is_Predefined_File_Name
15480 (Unit_File_Name (Get_Source_Unit (Par)));
15481 else
15482 return
15483 Present (Alias (Id))
15484 and then Is_Unchecked_Conversion_Instance (Alias (Id));
15485 end if;
15486 end if;
15487 end if;
15488
15489 return False;
15490 end Is_Unchecked_Conversion_Instance;
15491
15492 -------------------------------
15493 -- Is_Universal_Numeric_Type --
15494 -------------------------------
15495
15496 function Is_Universal_Numeric_Type (T : Entity_Id) return Boolean is
15497 begin
15498 return T = Universal_Integer or else T = Universal_Real;
15499 end Is_Universal_Numeric_Type;
15500
15501 --------------------------------------
15502 -- Is_Validation_Variable_Reference --
15503 --------------------------------------
15504
15505 function Is_Validation_Variable_Reference (N : Node_Id) return Boolean is
15506 Var : Node_Id;
15507 Var_Id : Entity_Id;
15508
15509 begin
15510 Var := N;
15511
15512 -- Use the expression when the context qualifies a reference in some
15513 -- fashion.
15514
15515 while Nkind_In (Var, N_Qualified_Expression,
15516 N_Type_Conversion,
15517 N_Unchecked_Type_Conversion)
15518 loop
15519 Var := Expression (Var);
15520 end loop;
15521
15522 Var_Id := Empty;
15523
15524 if Is_Entity_Name (Var) then
15525 Var_Id := Entity (Var);
15526 end if;
15527
15528 return
15529 Present (Var_Id)
15530 and then Ekind (Var_Id) = E_Variable
15531 and then Present (Validated_Object (Var_Id));
15532 end Is_Validation_Variable_Reference;
15533
15534 ----------------------------
15535 -- Is_Variable_Size_Array --
15536 ----------------------------
15537
15538 function Is_Variable_Size_Array (E : Entity_Id) return Boolean is
15539 Idx : Node_Id;
15540
15541 begin
15542 pragma Assert (Is_Array_Type (E));
15543
15544 -- Check if some index is initialized with a non-constant value
15545
15546 Idx := First_Index (E);
15547 while Present (Idx) loop
15548 if Nkind (Idx) = N_Range then
15549 if not Is_Constant_Bound (Low_Bound (Idx))
15550 or else not Is_Constant_Bound (High_Bound (Idx))
15551 then
15552 return True;
15553 end if;
15554 end if;
15555
15556 Idx := Next_Index (Idx);
15557 end loop;
15558
15559 return False;
15560 end Is_Variable_Size_Array;
15561
15562 -----------------------------
15563 -- Is_Variable_Size_Record --
15564 -----------------------------
15565
15566 function Is_Variable_Size_Record (E : Entity_Id) return Boolean is
15567 Comp : Entity_Id;
15568 Comp_Typ : Entity_Id;
15569
15570 begin
15571 pragma Assert (Is_Record_Type (E));
15572
15573 Comp := First_Entity (E);
15574 while Present (Comp) loop
15575 Comp_Typ := Etype (Comp);
15576
15577 -- Recursive call if the record type has discriminants
15578
15579 if Is_Record_Type (Comp_Typ)
15580 and then Has_Discriminants (Comp_Typ)
15581 and then Is_Variable_Size_Record (Comp_Typ)
15582 then
15583 return True;
15584
15585 elsif Is_Array_Type (Comp_Typ)
15586 and then Is_Variable_Size_Array (Comp_Typ)
15587 then
15588 return True;
15589 end if;
15590
15591 Next_Entity (Comp);
15592 end loop;
15593
15594 return False;
15595 end Is_Variable_Size_Record;
15596
15597 -----------------
15598 -- Is_Variable --
15599 -----------------
15600
15601 function Is_Variable
15602 (N : Node_Id;
15603 Use_Original_Node : Boolean := True) return Boolean
15604 is
15605 Orig_Node : Node_Id;
15606
15607 function In_Protected_Function (E : Entity_Id) return Boolean;
15608 -- Within a protected function, the private components of the enclosing
15609 -- protected type are constants. A function nested within a (protected)
15610 -- procedure is not itself protected. Within the body of a protected
15611 -- function the current instance of the protected type is a constant.
15612
15613 function Is_Variable_Prefix (P : Node_Id) return Boolean;
15614 -- Prefixes can involve implicit dereferences, in which case we must
15615 -- test for the case of a reference of a constant access type, which can
15616 -- can never be a variable.
15617
15618 ---------------------------
15619 -- In_Protected_Function --
15620 ---------------------------
15621
15622 function In_Protected_Function (E : Entity_Id) return Boolean is
15623 Prot : Entity_Id;
15624 S : Entity_Id;
15625
15626 begin
15627 -- E is the current instance of a type
15628
15629 if Is_Type (E) then
15630 Prot := E;
15631
15632 -- E is an object
15633
15634 else
15635 Prot := Scope (E);
15636 end if;
15637
15638 if not Is_Protected_Type (Prot) then
15639 return False;
15640
15641 else
15642 S := Current_Scope;
15643 while Present (S) and then S /= Prot loop
15644 if Ekind (S) = E_Function and then Scope (S) = Prot then
15645 return True;
15646 end if;
15647
15648 S := Scope (S);
15649 end loop;
15650
15651 return False;
15652 end if;
15653 end In_Protected_Function;
15654
15655 ------------------------
15656 -- Is_Variable_Prefix --
15657 ------------------------
15658
15659 function Is_Variable_Prefix (P : Node_Id) return Boolean is
15660 begin
15661 if Is_Access_Type (Etype (P)) then
15662 return not Is_Access_Constant (Root_Type (Etype (P)));
15663
15664 -- For the case of an indexed component whose prefix has a packed
15665 -- array type, the prefix has been rewritten into a type conversion.
15666 -- Determine variable-ness from the converted expression.
15667
15668 elsif Nkind (P) = N_Type_Conversion
15669 and then not Comes_From_Source (P)
15670 and then Is_Array_Type (Etype (P))
15671 and then Is_Packed (Etype (P))
15672 then
15673 return Is_Variable (Expression (P));
15674
15675 else
15676 return Is_Variable (P);
15677 end if;
15678 end Is_Variable_Prefix;
15679
15680 -- Start of processing for Is_Variable
15681
15682 begin
15683 -- Special check, allow x'Deref(expr) as a variable
15684
15685 if Nkind (N) = N_Attribute_Reference
15686 and then Attribute_Name (N) = Name_Deref
15687 then
15688 return True;
15689 end if;
15690
15691 -- Check if we perform the test on the original node since this may be a
15692 -- test of syntactic categories which must not be disturbed by whatever
15693 -- rewriting might have occurred. For example, an aggregate, which is
15694 -- certainly NOT a variable, could be turned into a variable by
15695 -- expansion.
15696
15697 if Use_Original_Node then
15698 Orig_Node := Original_Node (N);
15699 else
15700 Orig_Node := N;
15701 end if;
15702
15703 -- Definitely OK if Assignment_OK is set. Since this is something that
15704 -- only gets set for expanded nodes, the test is on N, not Orig_Node.
15705
15706 if Nkind (N) in N_Subexpr and then Assignment_OK (N) then
15707 return True;
15708
15709 -- Normally we go to the original node, but there is one exception where
15710 -- we use the rewritten node, namely when it is an explicit dereference.
15711 -- The generated code may rewrite a prefix which is an access type with
15712 -- an explicit dereference. The dereference is a variable, even though
15713 -- the original node may not be (since it could be a constant of the
15714 -- access type).
15715
15716 -- In Ada 2005 we have a further case to consider: the prefix may be a
15717 -- function call given in prefix notation. The original node appears to
15718 -- be a selected component, but we need to examine the call.
15719
15720 elsif Nkind (N) = N_Explicit_Dereference
15721 and then Nkind (Orig_Node) /= N_Explicit_Dereference
15722 and then Present (Etype (Orig_Node))
15723 and then Is_Access_Type (Etype (Orig_Node))
15724 then
15725 -- Note that if the prefix is an explicit dereference that does not
15726 -- come from source, we must check for a rewritten function call in
15727 -- prefixed notation before other forms of rewriting, to prevent a
15728 -- compiler crash.
15729
15730 return
15731 (Nkind (Orig_Node) = N_Function_Call
15732 and then not Is_Access_Constant (Etype (Prefix (N))))
15733 or else
15734 Is_Variable_Prefix (Original_Node (Prefix (N)));
15735
15736 -- in Ada 2012, the dereference may have been added for a type with
15737 -- a declared implicit dereference aspect. Check that it is not an
15738 -- access to constant.
15739
15740 elsif Nkind (N) = N_Explicit_Dereference
15741 and then Present (Etype (Orig_Node))
15742 and then Ada_Version >= Ada_2012
15743 and then Has_Implicit_Dereference (Etype (Orig_Node))
15744 then
15745 return not Is_Access_Constant (Etype (Prefix (N)));
15746
15747 -- A function call is never a variable
15748
15749 elsif Nkind (N) = N_Function_Call then
15750 return False;
15751
15752 -- All remaining checks use the original node
15753
15754 elsif Is_Entity_Name (Orig_Node)
15755 and then Present (Entity (Orig_Node))
15756 then
15757 declare
15758 E : constant Entity_Id := Entity (Orig_Node);
15759 K : constant Entity_Kind := Ekind (E);
15760
15761 begin
15762 return (K = E_Variable
15763 and then Nkind (Parent (E)) /= N_Exception_Handler)
15764 or else (K = E_Component
15765 and then not In_Protected_Function (E))
15766 or else K = E_Out_Parameter
15767 or else K = E_In_Out_Parameter
15768 or else K = E_Generic_In_Out_Parameter
15769
15770 -- Current instance of type. If this is a protected type, check
15771 -- we are not within the body of one of its protected functions.
15772
15773 or else (Is_Type (E)
15774 and then In_Open_Scopes (E)
15775 and then not In_Protected_Function (E))
15776
15777 or else (Is_Incomplete_Or_Private_Type (E)
15778 and then In_Open_Scopes (Full_View (E)));
15779 end;
15780
15781 else
15782 case Nkind (Orig_Node) is
15783 when N_Indexed_Component
15784 | N_Slice
15785 =>
15786 return Is_Variable_Prefix (Prefix (Orig_Node));
15787
15788 when N_Selected_Component =>
15789 return (Is_Variable (Selector_Name (Orig_Node))
15790 and then Is_Variable_Prefix (Prefix (Orig_Node)))
15791 or else
15792 (Nkind (N) = N_Expanded_Name
15793 and then Scope (Entity (N)) = Entity (Prefix (N)));
15794
15795 -- For an explicit dereference, the type of the prefix cannot
15796 -- be an access to constant or an access to subprogram.
15797
15798 when N_Explicit_Dereference =>
15799 declare
15800 Typ : constant Entity_Id := Etype (Prefix (Orig_Node));
15801 begin
15802 return Is_Access_Type (Typ)
15803 and then not Is_Access_Constant (Root_Type (Typ))
15804 and then Ekind (Typ) /= E_Access_Subprogram_Type;
15805 end;
15806
15807 -- The type conversion is the case where we do not deal with the
15808 -- context dependent special case of an actual parameter. Thus
15809 -- the type conversion is only considered a variable for the
15810 -- purposes of this routine if the target type is tagged. However,
15811 -- a type conversion is considered to be a variable if it does not
15812 -- come from source (this deals for example with the conversions
15813 -- of expressions to their actual subtypes).
15814
15815 when N_Type_Conversion =>
15816 return Is_Variable (Expression (Orig_Node))
15817 and then
15818 (not Comes_From_Source (Orig_Node)
15819 or else
15820 (Is_Tagged_Type (Etype (Subtype_Mark (Orig_Node)))
15821 and then
15822 Is_Tagged_Type (Etype (Expression (Orig_Node)))));
15823
15824 -- GNAT allows an unchecked type conversion as a variable. This
15825 -- only affects the generation of internal expanded code, since
15826 -- calls to instantiations of Unchecked_Conversion are never
15827 -- considered variables (since they are function calls).
15828
15829 when N_Unchecked_Type_Conversion =>
15830 return Is_Variable (Expression (Orig_Node));
15831
15832 when others =>
15833 return False;
15834 end case;
15835 end if;
15836 end Is_Variable;
15837
15838 ------------------------------
15839 -- Is_Verifiable_DIC_Pragma --
15840 ------------------------------
15841
15842 function Is_Verifiable_DIC_Pragma (Prag : Node_Id) return Boolean is
15843 Args : constant List_Id := Pragma_Argument_Associations (Prag);
15844
15845 begin
15846 -- To qualify as verifiable, a DIC pragma must have a non-null argument
15847
15848 return
15849 Present (Args)
15850 and then Nkind (Get_Pragma_Arg (First (Args))) /= N_Null;
15851 end Is_Verifiable_DIC_Pragma;
15852
15853 ---------------------------
15854 -- Is_Visibly_Controlled --
15855 ---------------------------
15856
15857 function Is_Visibly_Controlled (T : Entity_Id) return Boolean is
15858 Root : constant Entity_Id := Root_Type (T);
15859 begin
15860 return Chars (Scope (Root)) = Name_Finalization
15861 and then Chars (Scope (Scope (Root))) = Name_Ada
15862 and then Scope (Scope (Scope (Root))) = Standard_Standard;
15863 end Is_Visibly_Controlled;
15864
15865 --------------------------
15866 -- Is_Volatile_Function --
15867 --------------------------
15868
15869 function Is_Volatile_Function (Func_Id : Entity_Id) return Boolean is
15870 begin
15871 pragma Assert (Ekind_In (Func_Id, E_Function, E_Generic_Function));
15872
15873 -- A function declared within a protected type is volatile
15874
15875 if Is_Protected_Type (Scope (Func_Id)) then
15876 return True;
15877
15878 -- An instance of Ada.Unchecked_Conversion is a volatile function if
15879 -- either the source or the target are effectively volatile.
15880
15881 elsif Is_Unchecked_Conversion_Instance (Func_Id)
15882 and then Has_Effectively_Volatile_Profile (Func_Id)
15883 then
15884 return True;
15885
15886 -- Otherwise the function is treated as volatile if it is subject to
15887 -- enabled pragma Volatile_Function.
15888
15889 else
15890 return
15891 Is_Enabled_Pragma (Get_Pragma (Func_Id, Pragma_Volatile_Function));
15892 end if;
15893 end Is_Volatile_Function;
15894
15895 ------------------------
15896 -- Is_Volatile_Object --
15897 ------------------------
15898
15899 function Is_Volatile_Object (N : Node_Id) return Boolean is
15900 function Is_Volatile_Prefix (N : Node_Id) return Boolean;
15901 -- If prefix is an implicit dereference, examine designated type
15902
15903 function Object_Has_Volatile_Components (N : Node_Id) return Boolean;
15904 -- Determines if given object has volatile components
15905
15906 ------------------------
15907 -- Is_Volatile_Prefix --
15908 ------------------------
15909
15910 function Is_Volatile_Prefix (N : Node_Id) return Boolean is
15911 Typ : constant Entity_Id := Etype (N);
15912
15913 begin
15914 if Is_Access_Type (Typ) then
15915 declare
15916 Dtyp : constant Entity_Id := Designated_Type (Typ);
15917
15918 begin
15919 return Is_Volatile (Dtyp)
15920 or else Has_Volatile_Components (Dtyp);
15921 end;
15922
15923 else
15924 return Object_Has_Volatile_Components (N);
15925 end if;
15926 end Is_Volatile_Prefix;
15927
15928 ------------------------------------
15929 -- Object_Has_Volatile_Components --
15930 ------------------------------------
15931
15932 function Object_Has_Volatile_Components (N : Node_Id) return Boolean is
15933 Typ : constant Entity_Id := Etype (N);
15934
15935 begin
15936 if Is_Volatile (Typ)
15937 or else Has_Volatile_Components (Typ)
15938 then
15939 return True;
15940
15941 elsif Is_Entity_Name (N)
15942 and then (Has_Volatile_Components (Entity (N))
15943 or else Is_Volatile (Entity (N)))
15944 then
15945 return True;
15946
15947 elsif Nkind (N) = N_Indexed_Component
15948 or else Nkind (N) = N_Selected_Component
15949 then
15950 return Is_Volatile_Prefix (Prefix (N));
15951
15952 else
15953 return False;
15954 end if;
15955 end Object_Has_Volatile_Components;
15956
15957 -- Start of processing for Is_Volatile_Object
15958
15959 begin
15960 if Nkind (N) = N_Defining_Identifier then
15961 return Is_Volatile (N) or else Is_Volatile (Etype (N));
15962
15963 elsif Nkind (N) = N_Expanded_Name then
15964 return Is_Volatile_Object (Entity (N));
15965
15966 elsif Is_Volatile (Etype (N))
15967 or else (Is_Entity_Name (N) and then Is_Volatile (Entity (N)))
15968 then
15969 return True;
15970
15971 elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component)
15972 and then Is_Volatile_Prefix (Prefix (N))
15973 then
15974 return True;
15975
15976 elsif Nkind (N) = N_Selected_Component
15977 and then Is_Volatile (Entity (Selector_Name (N)))
15978 then
15979 return True;
15980
15981 else
15982 return False;
15983 end if;
15984 end Is_Volatile_Object;
15985
15986 ---------------------------
15987 -- Itype_Has_Declaration --
15988 ---------------------------
15989
15990 function Itype_Has_Declaration (Id : Entity_Id) return Boolean is
15991 begin
15992 pragma Assert (Is_Itype (Id));
15993 return Present (Parent (Id))
15994 and then Nkind_In (Parent (Id), N_Full_Type_Declaration,
15995 N_Subtype_Declaration)
15996 and then Defining_Entity (Parent (Id)) = Id;
15997 end Itype_Has_Declaration;
15998
15999 -------------------------
16000 -- Kill_Current_Values --
16001 -------------------------
16002
16003 procedure Kill_Current_Values
16004 (Ent : Entity_Id;
16005 Last_Assignment_Only : Boolean := False)
16006 is
16007 begin
16008 if Is_Assignable (Ent) then
16009 Set_Last_Assignment (Ent, Empty);
16010 end if;
16011
16012 if Is_Object (Ent) then
16013 if not Last_Assignment_Only then
16014 Kill_Checks (Ent);
16015 Set_Current_Value (Ent, Empty);
16016
16017 -- Do not reset the Is_Known_[Non_]Null and Is_Known_Valid flags
16018 -- for a constant. Once the constant is elaborated, its value is
16019 -- not changed, therefore the associated flags that describe the
16020 -- value should not be modified either.
16021
16022 if Ekind (Ent) = E_Constant then
16023 null;
16024
16025 -- Non-constant entities
16026
16027 else
16028 if not Can_Never_Be_Null (Ent) then
16029 Set_Is_Known_Non_Null (Ent, False);
16030 end if;
16031
16032 Set_Is_Known_Null (Ent, False);
16033
16034 -- Reset the Is_Known_Valid flag unless the type is always
16035 -- valid. This does not apply to a loop parameter because its
16036 -- bounds are defined by the loop header and therefore always
16037 -- valid.
16038
16039 if not Is_Known_Valid (Etype (Ent))
16040 and then Ekind (Ent) /= E_Loop_Parameter
16041 then
16042 Set_Is_Known_Valid (Ent, False);
16043 end if;
16044 end if;
16045 end if;
16046 end if;
16047 end Kill_Current_Values;
16048
16049 procedure Kill_Current_Values (Last_Assignment_Only : Boolean := False) is
16050 S : Entity_Id;
16051
16052 procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id);
16053 -- Clear current value for entity E and all entities chained to E
16054
16055 ------------------------------------------
16056 -- Kill_Current_Values_For_Entity_Chain --
16057 ------------------------------------------
16058
16059 procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id) is
16060 Ent : Entity_Id;
16061 begin
16062 Ent := E;
16063 while Present (Ent) loop
16064 Kill_Current_Values (Ent, Last_Assignment_Only);
16065 Next_Entity (Ent);
16066 end loop;
16067 end Kill_Current_Values_For_Entity_Chain;
16068
16069 -- Start of processing for Kill_Current_Values
16070
16071 begin
16072 -- Kill all saved checks, a special case of killing saved values
16073
16074 if not Last_Assignment_Only then
16075 Kill_All_Checks;
16076 end if;
16077
16078 -- Loop through relevant scopes, which includes the current scope and
16079 -- any parent scopes if the current scope is a block or a package.
16080
16081 S := Current_Scope;
16082 Scope_Loop : loop
16083
16084 -- Clear current values of all entities in current scope
16085
16086 Kill_Current_Values_For_Entity_Chain (First_Entity (S));
16087
16088 -- If scope is a package, also clear current values of all private
16089 -- entities in the scope.
16090
16091 if Is_Package_Or_Generic_Package (S)
16092 or else Is_Concurrent_Type (S)
16093 then
16094 Kill_Current_Values_For_Entity_Chain (First_Private_Entity (S));
16095 end if;
16096
16097 -- If this is a not a subprogram, deal with parents
16098
16099 if not Is_Subprogram (S) then
16100 S := Scope (S);
16101 exit Scope_Loop when S = Standard_Standard;
16102 else
16103 exit Scope_Loop;
16104 end if;
16105 end loop Scope_Loop;
16106 end Kill_Current_Values;
16107
16108 --------------------------
16109 -- Kill_Size_Check_Code --
16110 --------------------------
16111
16112 procedure Kill_Size_Check_Code (E : Entity_Id) is
16113 begin
16114 if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
16115 and then Present (Size_Check_Code (E))
16116 then
16117 Remove (Size_Check_Code (E));
16118 Set_Size_Check_Code (E, Empty);
16119 end if;
16120 end Kill_Size_Check_Code;
16121
16122 --------------------
16123 -- Known_Non_Null --
16124 --------------------
16125
16126 function Known_Non_Null (N : Node_Id) return Boolean is
16127 Status : constant Null_Status_Kind := Null_Status (N);
16128
16129 Id : Entity_Id;
16130 Op : Node_Kind;
16131 Val : Node_Id;
16132
16133 begin
16134 -- The expression yields a non-null value ignoring simple flow analysis
16135
16136 if Status = Is_Non_Null then
16137 return True;
16138
16139 -- Otherwise check whether N is a reference to an entity that appears
16140 -- within a conditional construct.
16141
16142 elsif Is_Entity_Name (N) and then Present (Entity (N)) then
16143
16144 -- First check if we are in decisive conditional
16145
16146 Get_Current_Value_Condition (N, Op, Val);
16147
16148 if Known_Null (Val) then
16149 if Op = N_Op_Eq then
16150 return False;
16151 elsif Op = N_Op_Ne then
16152 return True;
16153 end if;
16154 end if;
16155
16156 -- If OK to do replacement, test Is_Known_Non_Null flag
16157
16158 Id := Entity (N);
16159
16160 if OK_To_Do_Constant_Replacement (Id) then
16161 return Is_Known_Non_Null (Id);
16162 end if;
16163 end if;
16164
16165 -- Otherwise it is not possible to determine whether N yields a non-null
16166 -- value.
16167
16168 return False;
16169 end Known_Non_Null;
16170
16171 ----------------
16172 -- Known_Null --
16173 ----------------
16174
16175 function Known_Null (N : Node_Id) return Boolean is
16176 Status : constant Null_Status_Kind := Null_Status (N);
16177
16178 Id : Entity_Id;
16179 Op : Node_Kind;
16180 Val : Node_Id;
16181
16182 begin
16183 -- The expression yields a null value ignoring simple flow analysis
16184
16185 if Status = Is_Null then
16186 return True;
16187
16188 -- Otherwise check whether N is a reference to an entity that appears
16189 -- within a conditional construct.
16190
16191 elsif Is_Entity_Name (N) and then Present (Entity (N)) then
16192
16193 -- First check if we are in decisive conditional
16194
16195 Get_Current_Value_Condition (N, Op, Val);
16196
16197 if Known_Null (Val) then
16198 if Op = N_Op_Eq then
16199 return True;
16200 elsif Op = N_Op_Ne then
16201 return False;
16202 end if;
16203 end if;
16204
16205 -- If OK to do replacement, test Is_Known_Null flag
16206
16207 Id := Entity (N);
16208
16209 if OK_To_Do_Constant_Replacement (Id) then
16210 return Is_Known_Null (Id);
16211 end if;
16212 end if;
16213
16214 -- Otherwise it is not possible to determine whether N yields a null
16215 -- value.
16216
16217 return False;
16218 end Known_Null;
16219
16220 --------------------------
16221 -- Known_To_Be_Assigned --
16222 --------------------------
16223
16224 function Known_To_Be_Assigned (N : Node_Id) return Boolean is
16225 P : constant Node_Id := Parent (N);
16226
16227 begin
16228 case Nkind (P) is
16229
16230 -- Test left side of assignment
16231
16232 when N_Assignment_Statement =>
16233 return N = Name (P);
16234
16235 -- Function call arguments are never lvalues
16236
16237 when N_Function_Call =>
16238 return False;
16239
16240 -- Positional parameter for procedure or accept call
16241
16242 when N_Accept_Statement
16243 | N_Procedure_Call_Statement
16244 =>
16245 declare
16246 Proc : Entity_Id;
16247 Form : Entity_Id;
16248 Act : Node_Id;
16249
16250 begin
16251 Proc := Get_Subprogram_Entity (P);
16252
16253 if No (Proc) then
16254 return False;
16255 end if;
16256
16257 -- If we are not a list member, something is strange, so
16258 -- be conservative and return False.
16259
16260 if not Is_List_Member (N) then
16261 return False;
16262 end if;
16263
16264 -- We are going to find the right formal by stepping forward
16265 -- through the formals, as we step backwards in the actuals.
16266
16267 Form := First_Formal (Proc);
16268 Act := N;
16269 loop
16270 -- If no formal, something is weird, so be conservative
16271 -- and return False.
16272
16273 if No (Form) then
16274 return False;
16275 end if;
16276
16277 Prev (Act);
16278 exit when No (Act);
16279 Next_Formal (Form);
16280 end loop;
16281
16282 return Ekind (Form) /= E_In_Parameter;
16283 end;
16284
16285 -- Named parameter for procedure or accept call
16286
16287 when N_Parameter_Association =>
16288 declare
16289 Proc : Entity_Id;
16290 Form : Entity_Id;
16291
16292 begin
16293 Proc := Get_Subprogram_Entity (Parent (P));
16294
16295 if No (Proc) then
16296 return False;
16297 end if;
16298
16299 -- Loop through formals to find the one that matches
16300
16301 Form := First_Formal (Proc);
16302 loop
16303 -- If no matching formal, that's peculiar, some kind of
16304 -- previous error, so return False to be conservative.
16305 -- Actually this also happens in legal code in the case
16306 -- where P is a parameter association for an Extra_Formal???
16307
16308 if No (Form) then
16309 return False;
16310 end if;
16311
16312 -- Else test for match
16313
16314 if Chars (Form) = Chars (Selector_Name (P)) then
16315 return Ekind (Form) /= E_In_Parameter;
16316 end if;
16317
16318 Next_Formal (Form);
16319 end loop;
16320 end;
16321
16322 -- Test for appearing in a conversion that itself appears
16323 -- in an lvalue context, since this should be an lvalue.
16324
16325 when N_Type_Conversion =>
16326 return Known_To_Be_Assigned (P);
16327
16328 -- All other references are definitely not known to be modifications
16329
16330 when others =>
16331 return False;
16332 end case;
16333 end Known_To_Be_Assigned;
16334
16335 ---------------------------
16336 -- Last_Source_Statement --
16337 ---------------------------
16338
16339 function Last_Source_Statement (HSS : Node_Id) return Node_Id is
16340 N : Node_Id;
16341
16342 begin
16343 N := Last (Statements (HSS));
16344 while Present (N) loop
16345 exit when Comes_From_Source (N);
16346 Prev (N);
16347 end loop;
16348
16349 return N;
16350 end Last_Source_Statement;
16351
16352 ----------------------------------
16353 -- Matching_Static_Array_Bounds --
16354 ----------------------------------
16355
16356 function Matching_Static_Array_Bounds
16357 (L_Typ : Node_Id;
16358 R_Typ : Node_Id) return Boolean
16359 is
16360 L_Ndims : constant Nat := Number_Dimensions (L_Typ);
16361 R_Ndims : constant Nat := Number_Dimensions (R_Typ);
16362
16363 L_Index : Node_Id;
16364 R_Index : Node_Id;
16365 L_Low : Node_Id;
16366 L_High : Node_Id;
16367 L_Len : Uint;
16368 R_Low : Node_Id;
16369 R_High : Node_Id;
16370 R_Len : Uint;
16371
16372 begin
16373 if L_Ndims /= R_Ndims then
16374 return False;
16375 end if;
16376
16377 -- Unconstrained types do not have static bounds
16378
16379 if not Is_Constrained (L_Typ) or else not Is_Constrained (R_Typ) then
16380 return False;
16381 end if;
16382
16383 -- First treat specially the first dimension, as the lower bound and
16384 -- length of string literals are not stored like those of arrays.
16385
16386 if Ekind (L_Typ) = E_String_Literal_Subtype then
16387 L_Low := String_Literal_Low_Bound (L_Typ);
16388 L_Len := String_Literal_Length (L_Typ);
16389 else
16390 L_Index := First_Index (L_Typ);
16391 Get_Index_Bounds (L_Index, L_Low, L_High);
16392
16393 if Is_OK_Static_Expression (L_Low)
16394 and then
16395 Is_OK_Static_Expression (L_High)
16396 then
16397 if Expr_Value (L_High) < Expr_Value (L_Low) then
16398 L_Len := Uint_0;
16399 else
16400 L_Len := (Expr_Value (L_High) - Expr_Value (L_Low)) + 1;
16401 end if;
16402 else
16403 return False;
16404 end if;
16405 end if;
16406
16407 if Ekind (R_Typ) = E_String_Literal_Subtype then
16408 R_Low := String_Literal_Low_Bound (R_Typ);
16409 R_Len := String_Literal_Length (R_Typ);
16410 else
16411 R_Index := First_Index (R_Typ);
16412 Get_Index_Bounds (R_Index, R_Low, R_High);
16413
16414 if Is_OK_Static_Expression (R_Low)
16415 and then
16416 Is_OK_Static_Expression (R_High)
16417 then
16418 if Expr_Value (R_High) < Expr_Value (R_Low) then
16419 R_Len := Uint_0;
16420 else
16421 R_Len := (Expr_Value (R_High) - Expr_Value (R_Low)) + 1;
16422 end if;
16423 else
16424 return False;
16425 end if;
16426 end if;
16427
16428 if (Is_OK_Static_Expression (L_Low)
16429 and then
16430 Is_OK_Static_Expression (R_Low))
16431 and then Expr_Value (L_Low) = Expr_Value (R_Low)
16432 and then L_Len = R_Len
16433 then
16434 null;
16435 else
16436 return False;
16437 end if;
16438
16439 -- Then treat all other dimensions
16440
16441 for Indx in 2 .. L_Ndims loop
16442 Next (L_Index);
16443 Next (R_Index);
16444
16445 Get_Index_Bounds (L_Index, L_Low, L_High);
16446 Get_Index_Bounds (R_Index, R_Low, R_High);
16447
16448 if (Is_OK_Static_Expression (L_Low) and then
16449 Is_OK_Static_Expression (L_High) and then
16450 Is_OK_Static_Expression (R_Low) and then
16451 Is_OK_Static_Expression (R_High))
16452 and then (Expr_Value (L_Low) = Expr_Value (R_Low)
16453 and then
16454 Expr_Value (L_High) = Expr_Value (R_High))
16455 then
16456 null;
16457 else
16458 return False;
16459 end if;
16460 end loop;
16461
16462 -- If we fall through the loop, all indexes matched
16463
16464 return True;
16465 end Matching_Static_Array_Bounds;
16466
16467 -------------------
16468 -- May_Be_Lvalue --
16469 -------------------
16470
16471 function May_Be_Lvalue (N : Node_Id) return Boolean is
16472 P : constant Node_Id := Parent (N);
16473
16474 begin
16475 case Nkind (P) is
16476
16477 -- Test left side of assignment
16478
16479 when N_Assignment_Statement =>
16480 return N = Name (P);
16481
16482 -- Test prefix of component or attribute. Note that the prefix of an
16483 -- explicit or implicit dereference cannot be an l-value. In the case
16484 -- of a 'Read attribute, the reference can be an actual in the
16485 -- argument list of the attribute.
16486
16487 when N_Attribute_Reference =>
16488 return (N = Prefix (P)
16489 and then Name_Implies_Lvalue_Prefix (Attribute_Name (P)))
16490 or else
16491 Attribute_Name (P) = Name_Read;
16492
16493 -- For an expanded name, the name is an lvalue if the expanded name
16494 -- is an lvalue, but the prefix is never an lvalue, since it is just
16495 -- the scope where the name is found.
16496
16497 when N_Expanded_Name =>
16498 if N = Prefix (P) then
16499 return May_Be_Lvalue (P);
16500 else
16501 return False;
16502 end if;
16503
16504 -- For a selected component A.B, A is certainly an lvalue if A.B is.
16505 -- B is a little interesting, if we have A.B := 3, there is some
16506 -- discussion as to whether B is an lvalue or not, we choose to say
16507 -- it is. Note however that A is not an lvalue if it is of an access
16508 -- type since this is an implicit dereference.
16509
16510 when N_Selected_Component =>
16511 if N = Prefix (P)
16512 and then Present (Etype (N))
16513 and then Is_Access_Type (Etype (N))
16514 then
16515 return False;
16516 else
16517 return May_Be_Lvalue (P);
16518 end if;
16519
16520 -- For an indexed component or slice, the index or slice bounds is
16521 -- never an lvalue. The prefix is an lvalue if the indexed component
16522 -- or slice is an lvalue, except if it is an access type, where we
16523 -- have an implicit dereference.
16524
16525 when N_Indexed_Component
16526 | N_Slice
16527 =>
16528 if N /= Prefix (P)
16529 or else (Present (Etype (N)) and then Is_Access_Type (Etype (N)))
16530 then
16531 return False;
16532 else
16533 return May_Be_Lvalue (P);
16534 end if;
16535
16536 -- Prefix of a reference is an lvalue if the reference is an lvalue
16537
16538 when N_Reference =>
16539 return May_Be_Lvalue (P);
16540
16541 -- Prefix of explicit dereference is never an lvalue
16542
16543 when N_Explicit_Dereference =>
16544 return False;
16545
16546 -- Positional parameter for subprogram, entry, or accept call.
16547 -- In older versions of Ada function call arguments are never
16548 -- lvalues. In Ada 2012 functions can have in-out parameters.
16549
16550 when N_Accept_Statement
16551 | N_Entry_Call_Statement
16552 | N_Subprogram_Call
16553 =>
16554 if Nkind (P) = N_Function_Call and then Ada_Version < Ada_2012 then
16555 return False;
16556 end if;
16557
16558 -- The following mechanism is clumsy and fragile. A single flag
16559 -- set in Resolve_Actuals would be preferable ???
16560
16561 declare
16562 Proc : Entity_Id;
16563 Form : Entity_Id;
16564 Act : Node_Id;
16565
16566 begin
16567 Proc := Get_Subprogram_Entity (P);
16568
16569 if No (Proc) then
16570 return True;
16571 end if;
16572
16573 -- If we are not a list member, something is strange, so be
16574 -- conservative and return True.
16575
16576 if not Is_List_Member (N) then
16577 return True;
16578 end if;
16579
16580 -- We are going to find the right formal by stepping forward
16581 -- through the formals, as we step backwards in the actuals.
16582
16583 Form := First_Formal (Proc);
16584 Act := N;
16585 loop
16586 -- If no formal, something is weird, so be conservative and
16587 -- return True.
16588
16589 if No (Form) then
16590 return True;
16591 end if;
16592
16593 Prev (Act);
16594 exit when No (Act);
16595 Next_Formal (Form);
16596 end loop;
16597
16598 return Ekind (Form) /= E_In_Parameter;
16599 end;
16600
16601 -- Named parameter for procedure or accept call
16602
16603 when N_Parameter_Association =>
16604 declare
16605 Proc : Entity_Id;
16606 Form : Entity_Id;
16607
16608 begin
16609 Proc := Get_Subprogram_Entity (Parent (P));
16610
16611 if No (Proc) then
16612 return True;
16613 end if;
16614
16615 -- Loop through formals to find the one that matches
16616
16617 Form := First_Formal (Proc);
16618 loop
16619 -- If no matching formal, that's peculiar, some kind of
16620 -- previous error, so return True to be conservative.
16621 -- Actually happens with legal code for an unresolved call
16622 -- where we may get the wrong homonym???
16623
16624 if No (Form) then
16625 return True;
16626 end if;
16627
16628 -- Else test for match
16629
16630 if Chars (Form) = Chars (Selector_Name (P)) then
16631 return Ekind (Form) /= E_In_Parameter;
16632 end if;
16633
16634 Next_Formal (Form);
16635 end loop;
16636 end;
16637
16638 -- Test for appearing in a conversion that itself appears in an
16639 -- lvalue context, since this should be an lvalue.
16640
16641 when N_Type_Conversion =>
16642 return May_Be_Lvalue (P);
16643
16644 -- Test for appearance in object renaming declaration
16645
16646 when N_Object_Renaming_Declaration =>
16647 return True;
16648
16649 -- All other references are definitely not lvalues
16650
16651 when others =>
16652 return False;
16653 end case;
16654 end May_Be_Lvalue;
16655
16656 -----------------------
16657 -- Mark_Coextensions --
16658 -----------------------
16659
16660 procedure Mark_Coextensions (Context_Nod : Node_Id; Root_Nod : Node_Id) is
16661 Is_Dynamic : Boolean;
16662 -- Indicates whether the context causes nested coextensions to be
16663 -- dynamic or static
16664
16665 function Mark_Allocator (N : Node_Id) return Traverse_Result;
16666 -- Recognize an allocator node and label it as a dynamic coextension
16667
16668 --------------------
16669 -- Mark_Allocator --
16670 --------------------
16671
16672 function Mark_Allocator (N : Node_Id) return Traverse_Result is
16673 begin
16674 if Nkind (N) = N_Allocator then
16675 if Is_Dynamic then
16676 Set_Is_Dynamic_Coextension (N);
16677
16678 -- If the allocator expression is potentially dynamic, it may
16679 -- be expanded out of order and require dynamic allocation
16680 -- anyway, so we treat the coextension itself as dynamic.
16681 -- Potential optimization ???
16682
16683 elsif Nkind (Expression (N)) = N_Qualified_Expression
16684 and then Nkind (Expression (Expression (N))) = N_Op_Concat
16685 then
16686 Set_Is_Dynamic_Coextension (N);
16687 else
16688 Set_Is_Static_Coextension (N);
16689 end if;
16690 end if;
16691
16692 return OK;
16693 end Mark_Allocator;
16694
16695 procedure Mark_Allocators is new Traverse_Proc (Mark_Allocator);
16696
16697 -- Start of processing for Mark_Coextensions
16698
16699 begin
16700 -- An allocator that appears on the right-hand side of an assignment is
16701 -- treated as a potentially dynamic coextension when the right-hand side
16702 -- is an allocator or a qualified expression.
16703
16704 -- Obj := new ...'(new Coextension ...);
16705
16706 if Nkind (Context_Nod) = N_Assignment_Statement then
16707 Is_Dynamic :=
16708 Nkind_In (Expression (Context_Nod), N_Allocator,
16709 N_Qualified_Expression);
16710
16711 -- An allocator that appears within the expression of a simple return
16712 -- statement is treated as a potentially dynamic coextension when the
16713 -- expression is either aggregate, allocator, or qualified expression.
16714
16715 -- return (new Coextension ...);
16716 -- return new ...'(new Coextension ...);
16717
16718 elsif Nkind (Context_Nod) = N_Simple_Return_Statement then
16719 Is_Dynamic :=
16720 Nkind_In (Expression (Context_Nod), N_Aggregate,
16721 N_Allocator,
16722 N_Qualified_Expression);
16723
16724 -- An allocator that appears within the initialization expression of an
16725 -- object declaration is considered a potentially dynamic coextension
16726 -- when the initialization expression is an allocator or a qualified
16727 -- expression.
16728
16729 -- Obj : ... := new ...'(new Coextension ...);
16730
16731 -- A similar case arises when the object declaration is part of an
16732 -- extended return statement.
16733
16734 -- return Obj : ... := new ...'(new Coextension ...);
16735 -- return Obj : ... := (new Coextension ...);
16736
16737 elsif Nkind (Context_Nod) = N_Object_Declaration then
16738 Is_Dynamic :=
16739 Nkind_In (Root_Nod, N_Allocator, N_Qualified_Expression)
16740 or else
16741 Nkind (Parent (Context_Nod)) = N_Extended_Return_Statement;
16742
16743 -- This routine should not be called with constructs that cannot contain
16744 -- coextensions.
16745
16746 else
16747 raise Program_Error;
16748 end if;
16749
16750 Mark_Allocators (Root_Nod);
16751 end Mark_Coextensions;
16752
16753 ----------------------
16754 -- Needs_One_Actual --
16755 ----------------------
16756
16757 function Needs_One_Actual (E : Entity_Id) return Boolean is
16758 Formal : Entity_Id;
16759
16760 begin
16761 -- Ada 2005 or later, and formals present
16762
16763 if Ada_Version >= Ada_2005
16764 and then Present (First_Formal (E))
16765 and then No (Default_Value (First_Formal (E)))
16766 then
16767 Formal := Next_Formal (First_Formal (E));
16768 while Present (Formal) loop
16769 if No (Default_Value (Formal)) then
16770 return False;
16771 end if;
16772
16773 Next_Formal (Formal);
16774 end loop;
16775
16776 return True;
16777
16778 -- Ada 83/95 or no formals
16779
16780 else
16781 return False;
16782 end if;
16783 end Needs_One_Actual;
16784
16785 ------------------------
16786 -- New_Copy_List_Tree --
16787 ------------------------
16788
16789 function New_Copy_List_Tree (List : List_Id) return List_Id is
16790 NL : List_Id;
16791 E : Node_Id;
16792
16793 begin
16794 if List = No_List then
16795 return No_List;
16796
16797 else
16798 NL := New_List;
16799 E := First (List);
16800
16801 while Present (E) loop
16802 Append (New_Copy_Tree (E), NL);
16803 E := Next (E);
16804 end loop;
16805
16806 return NL;
16807 end if;
16808 end New_Copy_List_Tree;
16809
16810 --------------------------------------------------
16811 -- New_Copy_Tree Auxiliary Data and Subprograms --
16812 --------------------------------------------------
16813
16814 use Atree.Unchecked_Access;
16815 use Atree_Private_Part;
16816
16817 -- Our approach here requires a two pass traversal of the tree. The
16818 -- first pass visits all nodes that eventually will be copied looking
16819 -- for defining Itypes. If any defining Itypes are found, then they are
16820 -- copied, and an entry is added to the replacement map. In the second
16821 -- phase, the tree is copied, using the replacement map to replace any
16822 -- Itype references within the copied tree.
16823
16824 -- The following hash tables are used to speed up access to the map. They
16825 -- are declared at library level to avoid elaborating them for every call
16826 -- to New_Copy_Tree. This can save up to 2% of the entire compilation time
16827 -- spent in the front end.
16828
16829 subtype NCT_Header_Num is Int range 0 .. 511;
16830 -- Defines range of headers in hash tables (512 headers)
16831
16832 function New_Copy_Hash (E : Entity_Id) return NCT_Header_Num;
16833 -- Hash function used for hash operations
16834
16835 -------------------
16836 -- New_Copy_Hash --
16837 -------------------
16838
16839 function New_Copy_Hash (E : Entity_Id) return NCT_Header_Num is
16840 begin
16841 return Nat (E) mod (NCT_Header_Num'Last + 1);
16842 end New_Copy_Hash;
16843
16844 ---------------
16845 -- NCT_Assoc --
16846 ---------------
16847
16848 -- The hash table NCT_Assoc associates old entities in the table with their
16849 -- corresponding new entities (i.e. the pairs of entries presented in the
16850 -- original Map argument are Key-Element pairs).
16851
16852 package NCT_Assoc is new Simple_HTable (
16853 Header_Num => NCT_Header_Num,
16854 Element => Entity_Id,
16855 No_Element => Empty,
16856 Key => Entity_Id,
16857 Hash => New_Copy_Hash,
16858 Equal => Types."=");
16859
16860 ---------------------
16861 -- NCT_Itype_Assoc --
16862 ---------------------
16863
16864 -- The hash table NCT_Itype_Assoc contains entries only for those old
16865 -- nodes which have a non-empty Associated_Node_For_Itype set. The key
16866 -- is the associated node, and the element is the new node itself (NOT
16867 -- the associated node for the new node).
16868
16869 package NCT_Itype_Assoc is new Simple_HTable (
16870 Header_Num => NCT_Header_Num,
16871 Element => Entity_Id,
16872 No_Element => Empty,
16873 Key => Entity_Id,
16874 Hash => New_Copy_Hash,
16875 Equal => Types."=");
16876
16877 -------------------
16878 -- New_Copy_Tree --
16879 -------------------
16880
16881 function New_Copy_Tree
16882 (Source : Node_Id;
16883 Map : Elist_Id := No_Elist;
16884 New_Sloc : Source_Ptr := No_Location;
16885 New_Scope : Entity_Id := Empty) return Node_Id
16886 is
16887 EWA_Level : Nat := 0;
16888 -- By default, copying of defining identifiers is prohibited because
16889 -- this would introduce an entirely new entity into the tree. The
16890 -- exception to this general rule is declaration of constants and
16891 -- variables located in Expression_With_Action nodes.
16892
16893 EWA_Inner_Scope_Level : Nat := 0;
16894 -- Level of internal scope of defined in EWAs. Used to avoid creating
16895 -- variables for declarations located in blocks or subprograms defined
16896 -- in Expression_With_Action nodes.
16897
16898 NCT_Hash_Tables_Used : Boolean := False;
16899 -- Set to True if hash tables are in use. It is intended to speed up the
16900 -- common case, which is no hash tables in use. This can save up to 8%
16901 -- of the entire compilation time spent in the front end.
16902
16903 function Assoc (N : Node_Or_Entity_Id) return Node_Id;
16904 -- Called during second phase to map entities into their corresponding
16905 -- copies using the hash table. If the argument is not an entity, or is
16906 -- not in the hash table, then it is returned unchanged.
16907
16908 procedure Build_NCT_Hash_Tables;
16909 -- Builds hash tables
16910
16911 function Copy_Elist_With_Replacement
16912 (Old_Elist : Elist_Id) return Elist_Id;
16913 -- Called during second phase to copy element list doing replacements
16914
16915 procedure Copy_Entity_With_Replacement (New_Entity : Entity_Id);
16916 -- Called during the second phase to process a copied Entity. The actual
16917 -- copy happened during the first phase (so that we could make the entry
16918 -- in the mapping), but we still have to deal with the descendants of
16919 -- the copied Entity and copy them where necessary.
16920
16921 function Copy_List_With_Replacement (Old_List : List_Id) return List_Id;
16922 -- Called during second phase to copy list doing replacements
16923
16924 function Copy_Node_With_Replacement (Old_Node : Node_Id) return Node_Id;
16925 -- Called during second phase to copy node doing replacements
16926
16927 function In_Map (E : Entity_Id) return Boolean;
16928 -- Return True if E is one of the old entities specified in the set of
16929 -- mappings to be applied to entities in the tree (i.e. Map).
16930
16931 procedure Visit_Elist (E : Elist_Id);
16932 -- Called during first phase to visit all elements of an Elist
16933
16934 procedure Visit_Entity (Old_Entity : Entity_Id);
16935 -- Called during first phase to visit subsidiary fields of a defining
16936 -- entity which is not an itype, and also create a copy and make an
16937 -- entry in the replacement map for the new copy.
16938
16939 procedure Visit_Field (F : Union_Id; N : Node_Id);
16940 -- Visit a single field, recursing to call Visit_Node or Visit_List if
16941 -- the field is a syntactic descendant of the current node (i.e. its
16942 -- parent is Node N).
16943
16944 procedure Visit_Itype (Old_Itype : Entity_Id);
16945 -- Called during first phase to visit subsidiary fields of a defining
16946 -- Itype, and also create a copy and make an entry in the replacement
16947 -- map for the new copy.
16948
16949 procedure Visit_List (L : List_Id);
16950 -- Called during first phase to visit all elements of a List
16951
16952 procedure Visit_Node (N : Node_Or_Entity_Id);
16953 -- Called during first phase to visit a node and all its subtrees
16954
16955 -----------
16956 -- Assoc --
16957 -----------
16958
16959 function Assoc (N : Node_Or_Entity_Id) return Node_Id is
16960 Ent : Entity_Id;
16961
16962 begin
16963 if Nkind (N) not in N_Entity or else not NCT_Hash_Tables_Used then
16964 return N;
16965
16966 else
16967 Ent := NCT_Assoc.Get (Entity_Id (N));
16968
16969 if Present (Ent) then
16970 return Ent;
16971 end if;
16972 end if;
16973
16974 return N;
16975 end Assoc;
16976
16977 ---------------------------
16978 -- Build_NCT_Hash_Tables --
16979 ---------------------------
16980
16981 procedure Build_NCT_Hash_Tables is
16982 Elmt : Elmt_Id;
16983 Ent : Entity_Id;
16984
16985 begin
16986 if No (Map) then
16987 return;
16988 end if;
16989
16990 Elmt := First_Elmt (Map);
16991 while Present (Elmt) loop
16992 Ent := Node (Elmt);
16993
16994 -- Get new entity, and associate old and new
16995
16996 Next_Elmt (Elmt);
16997 NCT_Assoc.Set (Ent, Node (Elmt));
16998
16999 if Is_Type (Ent) then
17000 declare
17001 Anode : constant Entity_Id :=
17002 Associated_Node_For_Itype (Ent);
17003
17004 begin
17005 -- Enter the link between the associated node of the old
17006 -- Itype and the new Itype, for updating later when node
17007 -- is copied.
17008
17009 if Present (Anode) then
17010 NCT_Itype_Assoc.Set (Anode, Node (Elmt));
17011 end if;
17012 end;
17013 end if;
17014
17015 Next_Elmt (Elmt);
17016 end loop;
17017
17018 NCT_Hash_Tables_Used := True;
17019 end Build_NCT_Hash_Tables;
17020
17021 ---------------------------------
17022 -- Copy_Elist_With_Replacement --
17023 ---------------------------------
17024
17025 function Copy_Elist_With_Replacement
17026 (Old_Elist : Elist_Id) return Elist_Id
17027 is
17028 M : Elmt_Id;
17029 New_Elist : Elist_Id;
17030
17031 begin
17032 if No (Old_Elist) then
17033 return No_Elist;
17034
17035 else
17036 New_Elist := New_Elmt_List;
17037
17038 M := First_Elmt (Old_Elist);
17039 while Present (M) loop
17040 Append_Elmt (Copy_Node_With_Replacement (Node (M)), New_Elist);
17041 Next_Elmt (M);
17042 end loop;
17043 end if;
17044
17045 return New_Elist;
17046 end Copy_Elist_With_Replacement;
17047
17048 ----------------------------------
17049 -- Copy_Entity_With_Replacement --
17050 ----------------------------------
17051
17052 -- This routine exactly parallels its phase one analog Visit_Itype
17053
17054 procedure Copy_Entity_With_Replacement (New_Entity : Entity_Id) is
17055 begin
17056 -- Translate Next_Entity, Scope, and Etype fields, in case they
17057 -- reference entities that have been mapped into copies.
17058
17059 Set_Next_Entity (New_Entity, Assoc (Next_Entity (New_Entity)));
17060 Set_Etype (New_Entity, Assoc (Etype (New_Entity)));
17061
17062 if Present (New_Scope) then
17063 Set_Scope (New_Entity, New_Scope);
17064 else
17065 Set_Scope (New_Entity, Assoc (Scope (New_Entity)));
17066 end if;
17067
17068 -- Copy referenced fields
17069
17070 if Is_Discrete_Type (New_Entity) then
17071 Set_Scalar_Range (New_Entity,
17072 Copy_Node_With_Replacement (Scalar_Range (New_Entity)));
17073
17074 elsif Has_Discriminants (Base_Type (New_Entity)) then
17075 Set_Discriminant_Constraint (New_Entity,
17076 Copy_Elist_With_Replacement
17077 (Discriminant_Constraint (New_Entity)));
17078
17079 elsif Is_Array_Type (New_Entity) then
17080 if Present (First_Index (New_Entity)) then
17081 Set_First_Index (New_Entity,
17082 First (Copy_List_With_Replacement
17083 (List_Containing (First_Index (New_Entity)))));
17084 end if;
17085
17086 if Is_Packed (New_Entity) then
17087 Set_Packed_Array_Impl_Type (New_Entity,
17088 Copy_Node_With_Replacement
17089 (Packed_Array_Impl_Type (New_Entity)));
17090 end if;
17091 end if;
17092 end Copy_Entity_With_Replacement;
17093
17094 --------------------------------
17095 -- Copy_List_With_Replacement --
17096 --------------------------------
17097
17098 function Copy_List_With_Replacement
17099 (Old_List : List_Id) return List_Id
17100 is
17101 New_List : List_Id;
17102 E : Node_Id;
17103
17104 begin
17105 if Old_List = No_List then
17106 return No_List;
17107
17108 else
17109 New_List := Empty_List;
17110
17111 E := First (Old_List);
17112 while Present (E) loop
17113 Append (Copy_Node_With_Replacement (E), New_List);
17114 Next (E);
17115 end loop;
17116
17117 return New_List;
17118 end if;
17119 end Copy_List_With_Replacement;
17120
17121 --------------------------------
17122 -- Copy_Node_With_Replacement --
17123 --------------------------------
17124
17125 function Copy_Node_With_Replacement
17126 (Old_Node : Node_Id) return Node_Id
17127 is
17128 New_Node : Node_Id;
17129
17130 procedure Adjust_Named_Associations
17131 (Old_Node : Node_Id;
17132 New_Node : Node_Id);
17133 -- If a call node has named associations, these are chained through
17134 -- the First_Named_Actual, Next_Named_Actual links. These must be
17135 -- propagated separately to the new parameter list, because these
17136 -- are not syntactic fields.
17137
17138 function Copy_Field_With_Replacement
17139 (Field : Union_Id) return Union_Id;
17140 -- Given Field, which is a field of Old_Node, return a copy of it
17141 -- if it is a syntactic field (i.e. its parent is Node), setting
17142 -- the parent of the copy to poit to New_Node. Otherwise returns
17143 -- the field (possibly mapped if it is an entity).
17144
17145 -------------------------------
17146 -- Adjust_Named_Associations --
17147 -------------------------------
17148
17149 procedure Adjust_Named_Associations
17150 (Old_Node : Node_Id;
17151 New_Node : Node_Id)
17152 is
17153 Old_E : Node_Id;
17154 New_E : Node_Id;
17155
17156 Old_Next : Node_Id;
17157 New_Next : Node_Id;
17158
17159 begin
17160 Old_E := First (Parameter_Associations (Old_Node));
17161 New_E := First (Parameter_Associations (New_Node));
17162 while Present (Old_E) loop
17163 if Nkind (Old_E) = N_Parameter_Association
17164 and then Present (Next_Named_Actual (Old_E))
17165 then
17166 if First_Named_Actual (Old_Node) =
17167 Explicit_Actual_Parameter (Old_E)
17168 then
17169 Set_First_Named_Actual
17170 (New_Node, Explicit_Actual_Parameter (New_E));
17171 end if;
17172
17173 -- Now scan parameter list from the beginning, to locate
17174 -- next named actual, which can be out of order.
17175
17176 Old_Next := First (Parameter_Associations (Old_Node));
17177 New_Next := First (Parameter_Associations (New_Node));
17178 while Nkind (Old_Next) /= N_Parameter_Association
17179 or else Explicit_Actual_Parameter (Old_Next) /=
17180 Next_Named_Actual (Old_E)
17181 loop
17182 Next (Old_Next);
17183 Next (New_Next);
17184 end loop;
17185
17186 Set_Next_Named_Actual
17187 (New_E, Explicit_Actual_Parameter (New_Next));
17188 end if;
17189
17190 Next (Old_E);
17191 Next (New_E);
17192 end loop;
17193 end Adjust_Named_Associations;
17194
17195 ---------------------------------
17196 -- Copy_Field_With_Replacement --
17197 ---------------------------------
17198
17199 function Copy_Field_With_Replacement
17200 (Field : Union_Id) return Union_Id
17201 is
17202 begin
17203 if Field = Union_Id (Empty) then
17204 return Field;
17205
17206 elsif Field in Node_Range then
17207 declare
17208 Old_N : constant Node_Id := Node_Id (Field);
17209 New_N : Node_Id;
17210
17211 begin
17212 -- If syntactic field, as indicated by the parent pointer
17213 -- being set, then copy the referenced node recursively.
17214
17215 if Parent (Old_N) = Old_Node then
17216 New_N := Copy_Node_With_Replacement (Old_N);
17217
17218 if New_N /= Old_N then
17219 Set_Parent (New_N, New_Node);
17220 end if;
17221
17222 -- For semantic fields, update possible entity reference
17223 -- from the replacement map.
17224
17225 else
17226 New_N := Assoc (Old_N);
17227 end if;
17228
17229 return Union_Id (New_N);
17230 end;
17231
17232 elsif Field in List_Range then
17233 declare
17234 Old_L : constant List_Id := List_Id (Field);
17235 New_L : List_Id;
17236
17237 begin
17238 -- If syntactic field, as indicated by the parent pointer,
17239 -- then recursively copy the entire referenced list.
17240
17241 if Parent (Old_L) = Old_Node then
17242 New_L := Copy_List_With_Replacement (Old_L);
17243 Set_Parent (New_L, New_Node);
17244
17245 -- For semantic list, just returned unchanged
17246
17247 else
17248 New_L := Old_L;
17249 end if;
17250
17251 return Union_Id (New_L);
17252 end;
17253
17254 -- Anything other than a list or a node is returned unchanged
17255
17256 else
17257 return Field;
17258 end if;
17259 end Copy_Field_With_Replacement;
17260
17261 -- Start of processing for Copy_Node_With_Replacement
17262
17263 begin
17264 if Old_Node <= Empty_Or_Error then
17265 return Old_Node;
17266
17267 elsif Nkind (Old_Node) in N_Entity then
17268 return Assoc (Old_Node);
17269
17270 else
17271 New_Node := New_Copy (Old_Node);
17272
17273 -- If the node we are copying is the associated node of a
17274 -- previously copied Itype, then adjust the associated node
17275 -- of the copy of that Itype accordingly.
17276
17277 declare
17278 Ent : constant Entity_Id := NCT_Itype_Assoc.Get (Old_Node);
17279
17280 begin
17281 if Present (Ent) then
17282 Set_Associated_Node_For_Itype (Ent, New_Node);
17283 end if;
17284 end;
17285
17286 -- Recursively copy descendants
17287
17288 Set_Field1
17289 (New_Node, Copy_Field_With_Replacement (Field1 (New_Node)));
17290 Set_Field2
17291 (New_Node, Copy_Field_With_Replacement (Field2 (New_Node)));
17292 Set_Field3
17293 (New_Node, Copy_Field_With_Replacement (Field3 (New_Node)));
17294 Set_Field4
17295 (New_Node, Copy_Field_With_Replacement (Field4 (New_Node)));
17296 Set_Field5
17297 (New_Node, Copy_Field_With_Replacement (Field5 (New_Node)));
17298
17299 -- Adjust Sloc of new node if necessary
17300
17301 if New_Sloc /= No_Location then
17302 Set_Sloc (New_Node, New_Sloc);
17303
17304 -- If we adjust the Sloc, then we are essentially making a
17305 -- completely new node, so the Comes_From_Source flag should
17306 -- be reset to the proper default value.
17307
17308 Set_Comes_From_Source
17309 (New_Node, Default_Node.Comes_From_Source);
17310 end if;
17311
17312 -- If the node is a call and has named associations, set the
17313 -- corresponding links in the copy.
17314
17315 if Nkind_In (Old_Node, N_Entry_Call_Statement,
17316 N_Function_Call,
17317 N_Procedure_Call_Statement)
17318 and then Present (First_Named_Actual (Old_Node))
17319 then
17320 Adjust_Named_Associations (Old_Node, New_Node);
17321 end if;
17322
17323 -- Reset First_Real_Statement for Handled_Sequence_Of_Statements.
17324 -- The replacement mechanism applies to entities, and is not used
17325 -- here. Eventually we may need a more general graph-copying
17326 -- routine. For now, do a sequential search to find desired node.
17327
17328 if Nkind (Old_Node) = N_Handled_Sequence_Of_Statements
17329 and then Present (First_Real_Statement (Old_Node))
17330 then
17331 declare
17332 Old_F : constant Node_Id := First_Real_Statement (Old_Node);
17333 N1, N2 : Node_Id;
17334
17335 begin
17336 N1 := First (Statements (Old_Node));
17337 N2 := First (Statements (New_Node));
17338
17339 while N1 /= Old_F loop
17340 Next (N1);
17341 Next (N2);
17342 end loop;
17343
17344 Set_First_Real_Statement (New_Node, N2);
17345 end;
17346 end if;
17347 end if;
17348
17349 -- All done, return copied node
17350
17351 return New_Node;
17352 end Copy_Node_With_Replacement;
17353
17354 ------------
17355 -- In_Map --
17356 ------------
17357
17358 function In_Map (E : Entity_Id) return Boolean is
17359 Elmt : Elmt_Id;
17360 Ent : Entity_Id;
17361
17362 begin
17363 if Present (Map) then
17364 Elmt := First_Elmt (Map);
17365 while Present (Elmt) loop
17366 Ent := Node (Elmt);
17367
17368 if Ent = E then
17369 return True;
17370 end if;
17371
17372 Next_Elmt (Elmt);
17373 Next_Elmt (Elmt);
17374 end loop;
17375 end if;
17376
17377 return False;
17378 end In_Map;
17379
17380 -----------------
17381 -- Visit_Elist --
17382 -----------------
17383
17384 procedure Visit_Elist (E : Elist_Id) is
17385 Elmt : Elmt_Id;
17386 begin
17387 if Present (E) then
17388 Elmt := First_Elmt (E);
17389
17390 while Elmt /= No_Elmt loop
17391 Visit_Node (Node (Elmt));
17392 Next_Elmt (Elmt);
17393 end loop;
17394 end if;
17395 end Visit_Elist;
17396
17397 ------------------
17398 -- Visit_Entity --
17399 ------------------
17400
17401 procedure Visit_Entity (Old_Entity : Entity_Id) is
17402 New_E : Entity_Id;
17403
17404 begin
17405 pragma Assert (not Is_Itype (Old_Entity));
17406 pragma Assert (Nkind (Old_Entity) in N_Entity);
17407
17408 -- Restrict entity creation to declarations of constants, variables
17409 -- and subtypes. There is no need to duplicate entities declared in
17410 -- inner scopes.
17411
17412 if (not Ekind_In (Old_Entity, E_Constant, E_Variable)
17413 and then Nkind (Parent (Old_Entity)) /= N_Subtype_Declaration)
17414 or else EWA_Inner_Scope_Level > 0
17415 then
17416 return;
17417 end if;
17418
17419 New_E := New_Copy (Old_Entity);
17420
17421 -- The new entity has all the attributes of the old one, and we
17422 -- just copy the contents of the entity. However, the back-end
17423 -- needs different names for debugging purposes, so we create a
17424 -- new internal name for it in all cases.
17425
17426 Set_Chars (New_E, New_Internal_Name ('T'));
17427
17428 -- Add new association to map
17429
17430 NCT_Assoc.Set (Old_Entity, New_E);
17431 NCT_Hash_Tables_Used := True;
17432
17433 -- Visit descendants that eventually get copied
17434
17435 Visit_Field (Union_Id (Etype (Old_Entity)), Old_Entity);
17436 end Visit_Entity;
17437
17438 -----------------
17439 -- Visit_Field --
17440 -----------------
17441
17442 procedure Visit_Field (F : Union_Id; N : Node_Id) is
17443 begin
17444 if F = Union_Id (Empty) then
17445 return;
17446
17447 elsif F in Node_Range then
17448
17449 -- Copy node if it is syntactic, i.e. its parent pointer is
17450 -- set to point to the field that referenced it (certain
17451 -- Itypes will also meet this criterion, which is fine, since
17452 -- these are clearly Itypes that do need to be copied, since
17453 -- we are copying their parent.)
17454
17455 if Parent (Node_Id (F)) = N then
17456 Visit_Node (Node_Id (F));
17457 return;
17458
17459 -- Another case, if we are pointing to an Itype, then we want
17460 -- to copy it if its associated node is somewhere in the tree
17461 -- being copied.
17462
17463 -- Note: the exclusion of self-referential copies is just an
17464 -- optimization, since the search of the already copied list
17465 -- would catch it, but it is a common case (Etype pointing to
17466 -- itself for an Itype that is a base type).
17467
17468 elsif Nkind (Node_Id (F)) in N_Entity
17469 and then Is_Itype (Entity_Id (F))
17470 and then Node_Id (F) /= N
17471 then
17472 declare
17473 P : Node_Id;
17474
17475 begin
17476 P := Associated_Node_For_Itype (Node_Id (F));
17477 while Present (P) loop
17478 if P = Source then
17479 Visit_Node (Node_Id (F));
17480 return;
17481 else
17482 P := Parent (P);
17483 end if;
17484 end loop;
17485
17486 -- An Itype whose parent is not being copied definitely
17487 -- should NOT be copied, since it does not belong in any
17488 -- sense to the copied subtree.
17489
17490 return;
17491 end;
17492 end if;
17493
17494 elsif F in List_Range and then Parent (List_Id (F)) = N then
17495 Visit_List (List_Id (F));
17496 return;
17497 end if;
17498 end Visit_Field;
17499
17500 -----------------
17501 -- Visit_Itype --
17502 -----------------
17503
17504 procedure Visit_Itype (Old_Itype : Entity_Id) is
17505 New_Itype : Entity_Id;
17506 Ent : Entity_Id;
17507
17508 begin
17509 -- Itypes that describe the designated type of access to subprograms
17510 -- have the structure of subprogram declarations, with signatures,
17511 -- etc. Either we duplicate the signatures completely, or choose to
17512 -- share such itypes, which is fine because their elaboration will
17513 -- have no side effects.
17514
17515 if Ekind (Old_Itype) = E_Subprogram_Type then
17516 return;
17517 end if;
17518
17519 New_Itype := New_Copy (Old_Itype);
17520
17521 -- The new Itype has all the attributes of the old one, and we
17522 -- just copy the contents of the entity. However, the back-end
17523 -- needs different names for debugging purposes, so we create a
17524 -- new internal name for it in all cases.
17525
17526 Set_Chars (New_Itype, New_Internal_Name ('T'));
17527
17528 -- If our associated node is an entity that has already been copied,
17529 -- then set the associated node of the copy to point to the right
17530 -- copy. If we have copied an Itype that is itself the associated
17531 -- node of some previously copied Itype, then we set the right
17532 -- pointer in the other direction.
17533
17534 Ent := NCT_Assoc.Get (Associated_Node_For_Itype (Old_Itype));
17535
17536 if Present (Ent) then
17537 Set_Associated_Node_For_Itype (New_Itype, Ent);
17538 end if;
17539
17540 Ent := NCT_Itype_Assoc.Get (Old_Itype);
17541
17542 if Present (Ent) then
17543 Set_Associated_Node_For_Itype (Ent, New_Itype);
17544
17545 -- If the hash table has no association for this Itype and its
17546 -- associated node, enter one now.
17547
17548 else
17549 NCT_Itype_Assoc.Set
17550 (Associated_Node_For_Itype (Old_Itype), New_Itype);
17551 end if;
17552
17553 if Present (Freeze_Node (New_Itype)) then
17554 Set_Is_Frozen (New_Itype, False);
17555 Set_Freeze_Node (New_Itype, Empty);
17556 end if;
17557
17558 -- Add new association to map
17559
17560 NCT_Assoc.Set (Old_Itype, New_Itype);
17561 NCT_Hash_Tables_Used := True;
17562
17563 -- If a record subtype is simply copied, the entity list will be
17564 -- shared. Thus cloned_Subtype must be set to indicate the sharing.
17565
17566 if Ekind_In (Old_Itype, E_Class_Wide_Subtype, E_Record_Subtype) then
17567 Set_Cloned_Subtype (New_Itype, Old_Itype);
17568 end if;
17569
17570 -- Visit descendants that eventually get copied
17571
17572 Visit_Field (Union_Id (Etype (Old_Itype)), Old_Itype);
17573
17574 if Is_Discrete_Type (Old_Itype) then
17575 Visit_Field (Union_Id (Scalar_Range (Old_Itype)), Old_Itype);
17576
17577 elsif Has_Discriminants (Base_Type (Old_Itype)) then
17578 -- ??? This should involve call to Visit_Field
17579 Visit_Elist (Discriminant_Constraint (Old_Itype));
17580
17581 elsif Is_Array_Type (Old_Itype) then
17582 if Present (First_Index (Old_Itype)) then
17583 Visit_Field
17584 (Union_Id (List_Containing (First_Index (Old_Itype))),
17585 Old_Itype);
17586 end if;
17587
17588 if Is_Packed (Old_Itype) then
17589 Visit_Field
17590 (Union_Id (Packed_Array_Impl_Type (Old_Itype)), Old_Itype);
17591 end if;
17592 end if;
17593 end Visit_Itype;
17594
17595 ----------------
17596 -- Visit_List --
17597 ----------------
17598
17599 procedure Visit_List (L : List_Id) is
17600 N : Node_Id;
17601 begin
17602 if L /= No_List then
17603 N := First (L);
17604
17605 while Present (N) loop
17606 Visit_Node (N);
17607 Next (N);
17608 end loop;
17609 end if;
17610 end Visit_List;
17611
17612 ----------------
17613 -- Visit_Node --
17614 ----------------
17615
17616 procedure Visit_Node (N : Node_Or_Entity_Id) is
17617 begin
17618 if Nkind (N) = N_Expression_With_Actions then
17619 EWA_Level := EWA_Level + 1;
17620
17621 elsif EWA_Level > 0
17622 and then Nkind_In (N, N_Block_Statement,
17623 N_Subprogram_Body,
17624 N_Subprogram_Declaration)
17625 then
17626 EWA_Inner_Scope_Level := EWA_Inner_Scope_Level + 1;
17627
17628 -- Handle case of an Itype, which must be copied
17629
17630 elsif Nkind (N) in N_Entity and then Is_Itype (N) then
17631
17632 -- Nothing to do if already in the list. This can happen with an
17633 -- Itype entity that appears more than once in the tree. Note that
17634 -- we do not want to visit descendants in this case.
17635
17636 if Present (NCT_Assoc.Get (Entity_Id (N))) then
17637 return;
17638 end if;
17639
17640 Visit_Itype (N);
17641
17642 -- Handle defining entities in Expression_With_Action nodes
17643
17644 elsif Nkind (N) in N_Entity and then EWA_Level > 0 then
17645
17646 -- Nothing to do if already in the hash table
17647
17648 if Present (NCT_Assoc.Get (Entity_Id (N))) then
17649 return;
17650 end if;
17651
17652 Visit_Entity (N);
17653 end if;
17654
17655 -- Visit descendants
17656
17657 Visit_Field (Field1 (N), N);
17658 Visit_Field (Field2 (N), N);
17659 Visit_Field (Field3 (N), N);
17660 Visit_Field (Field4 (N), N);
17661 Visit_Field (Field5 (N), N);
17662
17663 if EWA_Level > 0
17664 and then Nkind_In (N, N_Block_Statement,
17665 N_Subprogram_Body,
17666 N_Subprogram_Declaration)
17667 then
17668 EWA_Inner_Scope_Level := EWA_Inner_Scope_Level - 1;
17669
17670 elsif Nkind (N) = N_Expression_With_Actions then
17671 EWA_Level := EWA_Level - 1;
17672 end if;
17673 end Visit_Node;
17674
17675 -- Start of processing for New_Copy_Tree
17676
17677 begin
17678 Build_NCT_Hash_Tables;
17679
17680 -- Hash table set up if required, now start phase one by visiting top
17681 -- node (we will recursively visit the descendants).
17682
17683 Visit_Node (Source);
17684
17685 -- Now the second phase of the copy can start. First we process all the
17686 -- mapped entities, copying their descendants.
17687
17688 if NCT_Hash_Tables_Used then
17689 declare
17690 Old_E : Entity_Id := Empty;
17691 New_E : Entity_Id;
17692
17693 begin
17694 NCT_Assoc.Get_First (Old_E, New_E);
17695 while Present (New_E) loop
17696
17697 -- Skip entities that were not created in the first phase
17698 -- (that is, old entities specified by the caller in the
17699 -- set of mappings to be applied to the tree).
17700
17701 if Is_Itype (New_E)
17702 or else No (Map)
17703 or else not In_Map (Old_E)
17704 then
17705 Copy_Entity_With_Replacement (New_E);
17706 end if;
17707
17708 NCT_Assoc.Get_Next (Old_E, New_E);
17709 end loop;
17710 end;
17711 end if;
17712
17713 -- Now we can copy the actual tree
17714
17715 declare
17716 Result : constant Node_Id := Copy_Node_With_Replacement (Source);
17717
17718 begin
17719 if NCT_Hash_Tables_Used then
17720 NCT_Assoc.Reset;
17721 NCT_Itype_Assoc.Reset;
17722 end if;
17723
17724 return Result;
17725 end;
17726 end New_Copy_Tree;
17727
17728 -------------------------
17729 -- New_External_Entity --
17730 -------------------------
17731
17732 function New_External_Entity
17733 (Kind : Entity_Kind;
17734 Scope_Id : Entity_Id;
17735 Sloc_Value : Source_Ptr;
17736 Related_Id : Entity_Id;
17737 Suffix : Character;
17738 Suffix_Index : Nat := 0;
17739 Prefix : Character := ' ') return Entity_Id
17740 is
17741 N : constant Entity_Id :=
17742 Make_Defining_Identifier (Sloc_Value,
17743 New_External_Name
17744 (Chars (Related_Id), Suffix, Suffix_Index, Prefix));
17745
17746 begin
17747 Set_Ekind (N, Kind);
17748 Set_Is_Internal (N, True);
17749 Append_Entity (N, Scope_Id);
17750 Set_Public_Status (N);
17751
17752 if Kind in Type_Kind then
17753 Init_Size_Align (N);
17754 end if;
17755
17756 return N;
17757 end New_External_Entity;
17758
17759 -------------------------
17760 -- New_Internal_Entity --
17761 -------------------------
17762
17763 function New_Internal_Entity
17764 (Kind : Entity_Kind;
17765 Scope_Id : Entity_Id;
17766 Sloc_Value : Source_Ptr;
17767 Id_Char : Character) return Entity_Id
17768 is
17769 N : constant Entity_Id := Make_Temporary (Sloc_Value, Id_Char);
17770
17771 begin
17772 Set_Ekind (N, Kind);
17773 Set_Is_Internal (N, True);
17774 Append_Entity (N, Scope_Id);
17775
17776 if Kind in Type_Kind then
17777 Init_Size_Align (N);
17778 end if;
17779
17780 return N;
17781 end New_Internal_Entity;
17782
17783 -----------------
17784 -- Next_Actual --
17785 -----------------
17786
17787 function Next_Actual (Actual_Id : Node_Id) return Node_Id is
17788 N : Node_Id;
17789
17790 begin
17791 -- If we are pointing at a positional parameter, it is a member of a
17792 -- node list (the list of parameters), and the next parameter is the
17793 -- next node on the list, unless we hit a parameter association, then
17794 -- we shift to using the chain whose head is the First_Named_Actual in
17795 -- the parent, and then is threaded using the Next_Named_Actual of the
17796 -- Parameter_Association. All this fiddling is because the original node
17797 -- list is in the textual call order, and what we need is the
17798 -- declaration order.
17799
17800 if Is_List_Member (Actual_Id) then
17801 N := Next (Actual_Id);
17802
17803 if Nkind (N) = N_Parameter_Association then
17804 return First_Named_Actual (Parent (Actual_Id));
17805 else
17806 return N;
17807 end if;
17808
17809 else
17810 return Next_Named_Actual (Parent (Actual_Id));
17811 end if;
17812 end Next_Actual;
17813
17814 procedure Next_Actual (Actual_Id : in out Node_Id) is
17815 begin
17816 Actual_Id := Next_Actual (Actual_Id);
17817 end Next_Actual;
17818
17819 ----------------------------------
17820 -- New_Requires_Transient_Scope --
17821 ----------------------------------
17822
17823 function New_Requires_Transient_Scope (Id : Entity_Id) return Boolean is
17824 function Caller_Known_Size_Record (Typ : Entity_Id) return Boolean;
17825 -- This is called for untagged records and protected types, with
17826 -- nondefaulted discriminants. Returns True if the size of function
17827 -- results is known at the call site, False otherwise. Returns False
17828 -- if there is a variant part that depends on the discriminants of
17829 -- this type, or if there is an array constrained by the discriminants
17830 -- of this type. ???Currently, this is overly conservative (the array
17831 -- could be nested inside some other record that is constrained by
17832 -- nondiscriminants). That is, the recursive calls are too conservative.
17833
17834 function Large_Max_Size_Mutable (Typ : Entity_Id) return Boolean;
17835 -- Returns True if Typ is a nonlimited record with defaulted
17836 -- discriminants whose max size makes it unsuitable for allocating on
17837 -- the primary stack.
17838
17839 ------------------------------
17840 -- Caller_Known_Size_Record --
17841 ------------------------------
17842
17843 function Caller_Known_Size_Record (Typ : Entity_Id) return Boolean is
17844 pragma Assert (Typ = Underlying_Type (Typ));
17845
17846 begin
17847 if Has_Variant_Part (Typ) and then not Is_Definite_Subtype (Typ) then
17848 return False;
17849 end if;
17850
17851 declare
17852 Comp : Entity_Id;
17853
17854 begin
17855 Comp := First_Entity (Typ);
17856 while Present (Comp) loop
17857
17858 -- Only look at E_Component entities. No need to look at
17859 -- E_Discriminant entities, and we must ignore internal
17860 -- subtypes generated for constrained components.
17861
17862 if Ekind (Comp) = E_Component then
17863 declare
17864 Comp_Type : constant Entity_Id :=
17865 Underlying_Type (Etype (Comp));
17866
17867 begin
17868 if Is_Record_Type (Comp_Type)
17869 or else
17870 Is_Protected_Type (Comp_Type)
17871 then
17872 if not Caller_Known_Size_Record (Comp_Type) then
17873 return False;
17874 end if;
17875
17876 elsif Is_Array_Type (Comp_Type) then
17877 if Size_Depends_On_Discriminant (Comp_Type) then
17878 return False;
17879 end if;
17880 end if;
17881 end;
17882 end if;
17883
17884 Next_Entity (Comp);
17885 end loop;
17886 end;
17887
17888 return True;
17889 end Caller_Known_Size_Record;
17890
17891 ------------------------------
17892 -- Large_Max_Size_Mutable --
17893 ------------------------------
17894
17895 function Large_Max_Size_Mutable (Typ : Entity_Id) return Boolean is
17896 pragma Assert (Typ = Underlying_Type (Typ));
17897
17898 function Is_Large_Discrete_Type (T : Entity_Id) return Boolean;
17899 -- Returns true if the discrete type T has a large range
17900
17901 ----------------------------
17902 -- Is_Large_Discrete_Type --
17903 ----------------------------
17904
17905 function Is_Large_Discrete_Type (T : Entity_Id) return Boolean is
17906 Threshold : constant Int := 16;
17907 -- Arbitrary threshold above which we consider it "large". We want
17908 -- a fairly large threshold, because these large types really
17909 -- shouldn't have default discriminants in the first place, in
17910 -- most cases.
17911
17912 begin
17913 return UI_To_Int (RM_Size (T)) > Threshold;
17914 end Is_Large_Discrete_Type;
17915
17916 -- Start of processing for Large_Max_Size_Mutable
17917
17918 begin
17919 if Is_Record_Type (Typ)
17920 and then not Is_Limited_View (Typ)
17921 and then Has_Defaulted_Discriminants (Typ)
17922 then
17923 -- Loop through the components, looking for an array whose upper
17924 -- bound(s) depends on discriminants, where both the subtype of
17925 -- the discriminant and the index subtype are too large.
17926
17927 declare
17928 Comp : Entity_Id;
17929
17930 begin
17931 Comp := First_Entity (Typ);
17932 while Present (Comp) loop
17933 if Ekind (Comp) = E_Component then
17934 declare
17935 Comp_Type : constant Entity_Id :=
17936 Underlying_Type (Etype (Comp));
17937
17938 Hi : Node_Id;
17939 Indx : Node_Id;
17940 Ityp : Entity_Id;
17941
17942 begin
17943 if Is_Array_Type (Comp_Type) then
17944 Indx := First_Index (Comp_Type);
17945
17946 while Present (Indx) loop
17947 Ityp := Etype (Indx);
17948 Hi := Type_High_Bound (Ityp);
17949
17950 if Nkind (Hi) = N_Identifier
17951 and then Ekind (Entity (Hi)) = E_Discriminant
17952 and then Is_Large_Discrete_Type (Ityp)
17953 and then Is_Large_Discrete_Type
17954 (Etype (Entity (Hi)))
17955 then
17956 return True;
17957 end if;
17958
17959 Next_Index (Indx);
17960 end loop;
17961 end if;
17962 end;
17963 end if;
17964
17965 Next_Entity (Comp);
17966 end loop;
17967 end;
17968 end if;
17969
17970 return False;
17971 end Large_Max_Size_Mutable;
17972
17973 -- Local declarations
17974
17975 Typ : constant Entity_Id := Underlying_Type (Id);
17976
17977 -- Start of processing for New_Requires_Transient_Scope
17978
17979 begin
17980 -- This is a private type which is not completed yet. This can only
17981 -- happen in a default expression (of a formal parameter or of a
17982 -- record component). Do not expand transient scope in this case.
17983
17984 if No (Typ) then
17985 return False;
17986
17987 -- Do not expand transient scope for non-existent procedure return or
17988 -- string literal types.
17989
17990 elsif Typ = Standard_Void_Type
17991 or else Ekind (Typ) = E_String_Literal_Subtype
17992 then
17993 return False;
17994
17995 -- If Typ is a generic formal incomplete type, then we want to look at
17996 -- the actual type.
17997
17998 elsif Ekind (Typ) = E_Record_Subtype
17999 and then Present (Cloned_Subtype (Typ))
18000 then
18001 return New_Requires_Transient_Scope (Cloned_Subtype (Typ));
18002
18003 -- Functions returning specific tagged types may dispatch on result, so
18004 -- their returned value is allocated on the secondary stack, even in the
18005 -- definite case. We must treat nondispatching functions the same way,
18006 -- because access-to-function types can point at both, so the calling
18007 -- conventions must be compatible. Is_Tagged_Type includes controlled
18008 -- types and class-wide types. Controlled type temporaries need
18009 -- finalization.
18010
18011 -- ???It's not clear why we need to return noncontrolled types with
18012 -- controlled components on the secondary stack.
18013
18014 elsif Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
18015 return True;
18016
18017 -- Untagged definite subtypes are known size. This includes all
18018 -- elementary [sub]types. Tasks are known size even if they have
18019 -- discriminants. So we return False here, with one exception:
18020 -- For a type like:
18021 -- type T (Last : Natural := 0) is
18022 -- X : String (1 .. Last);
18023 -- end record;
18024 -- we return True. That's because for "P(F(...));", where F returns T,
18025 -- we don't know the size of the result at the call site, so if we
18026 -- allocated it on the primary stack, we would have to allocate the
18027 -- maximum size, which is way too big.
18028
18029 elsif Is_Definite_Subtype (Typ) or else Is_Task_Type (Typ) then
18030 return Large_Max_Size_Mutable (Typ);
18031
18032 -- Indefinite (discriminated) untagged record or protected type
18033
18034 elsif Is_Record_Type (Typ) or else Is_Protected_Type (Typ) then
18035 return not Caller_Known_Size_Record (Typ);
18036
18037 -- Unconstrained array
18038
18039 else
18040 pragma Assert (Is_Array_Type (Typ) and not Is_Definite_Subtype (Typ));
18041 return True;
18042 end if;
18043 end New_Requires_Transient_Scope;
18044
18045 --------------------------
18046 -- No_Heap_Finalization --
18047 --------------------------
18048
18049 function No_Heap_Finalization (Typ : Entity_Id) return Boolean is
18050 begin
18051 if Ekind_In (Typ, E_Access_Type, E_General_Access_Type)
18052 and then Is_Library_Level_Entity (Typ)
18053 then
18054 -- A global No_Heap_Finalization pragma applies to all library-level
18055 -- named access-to-object types.
18056
18057 if Present (No_Heap_Finalization_Pragma) then
18058 return True;
18059
18060 -- The library-level named access-to-object type itself is subject to
18061 -- pragma No_Heap_Finalization.
18062
18063 elsif Present (Get_Pragma (Typ, Pragma_No_Heap_Finalization)) then
18064 return True;
18065 end if;
18066 end if;
18067
18068 return False;
18069 end No_Heap_Finalization;
18070
18071 -----------------------
18072 -- Normalize_Actuals --
18073 -----------------------
18074
18075 -- Chain actuals according to formals of subprogram. If there are no named
18076 -- associations, the chain is simply the list of Parameter Associations,
18077 -- since the order is the same as the declaration order. If there are named
18078 -- associations, then the First_Named_Actual field in the N_Function_Call
18079 -- or N_Procedure_Call_Statement node points to the Parameter_Association
18080 -- node for the parameter that comes first in declaration order. The
18081 -- remaining named parameters are then chained in declaration order using
18082 -- Next_Named_Actual.
18083
18084 -- This routine also verifies that the number of actuals is compatible with
18085 -- the number and default values of formals, but performs no type checking
18086 -- (type checking is done by the caller).
18087
18088 -- If the matching succeeds, Success is set to True and the caller proceeds
18089 -- with type-checking. If the match is unsuccessful, then Success is set to
18090 -- False, and the caller attempts a different interpretation, if there is
18091 -- one.
18092
18093 -- If the flag Report is on, the call is not overloaded, and a failure to
18094 -- match can be reported here, rather than in the caller.
18095
18096 procedure Normalize_Actuals
18097 (N : Node_Id;
18098 S : Entity_Id;
18099 Report : Boolean;
18100 Success : out Boolean)
18101 is
18102 Actuals : constant List_Id := Parameter_Associations (N);
18103 Actual : Node_Id := Empty;
18104 Formal : Entity_Id;
18105 Last : Node_Id := Empty;
18106 First_Named : Node_Id := Empty;
18107 Found : Boolean;
18108
18109 Formals_To_Match : Integer := 0;
18110 Actuals_To_Match : Integer := 0;
18111
18112 procedure Chain (A : Node_Id);
18113 -- Add named actual at the proper place in the list, using the
18114 -- Next_Named_Actual link.
18115
18116 function Reporting return Boolean;
18117 -- Determines if an error is to be reported. To report an error, we
18118 -- need Report to be True, and also we do not report errors caused
18119 -- by calls to init procs that occur within other init procs. Such
18120 -- errors must always be cascaded errors, since if all the types are
18121 -- declared correctly, the compiler will certainly build decent calls.
18122
18123 -----------
18124 -- Chain --
18125 -----------
18126
18127 procedure Chain (A : Node_Id) is
18128 begin
18129 if No (Last) then
18130
18131 -- Call node points to first actual in list
18132
18133 Set_First_Named_Actual (N, Explicit_Actual_Parameter (A));
18134
18135 else
18136 Set_Next_Named_Actual (Last, Explicit_Actual_Parameter (A));
18137 end if;
18138
18139 Last := A;
18140 Set_Next_Named_Actual (Last, Empty);
18141 end Chain;
18142
18143 ---------------
18144 -- Reporting --
18145 ---------------
18146
18147 function Reporting return Boolean is
18148 begin
18149 if not Report then
18150 return False;
18151
18152 elsif not Within_Init_Proc then
18153 return True;
18154
18155 elsif Is_Init_Proc (Entity (Name (N))) then
18156 return False;
18157
18158 else
18159 return True;
18160 end if;
18161 end Reporting;
18162
18163 -- Start of processing for Normalize_Actuals
18164
18165 begin
18166 if Is_Access_Type (S) then
18167
18168 -- The name in the call is a function call that returns an access
18169 -- to subprogram. The designated type has the list of formals.
18170
18171 Formal := First_Formal (Designated_Type (S));
18172 else
18173 Formal := First_Formal (S);
18174 end if;
18175
18176 while Present (Formal) loop
18177 Formals_To_Match := Formals_To_Match + 1;
18178 Next_Formal (Formal);
18179 end loop;
18180
18181 -- Find if there is a named association, and verify that no positional
18182 -- associations appear after named ones.
18183
18184 if Present (Actuals) then
18185 Actual := First (Actuals);
18186 end if;
18187
18188 while Present (Actual)
18189 and then Nkind (Actual) /= N_Parameter_Association
18190 loop
18191 Actuals_To_Match := Actuals_To_Match + 1;
18192 Next (Actual);
18193 end loop;
18194
18195 if No (Actual) and Actuals_To_Match = Formals_To_Match then
18196
18197 -- Most common case: positional notation, no defaults
18198
18199 Success := True;
18200 return;
18201
18202 elsif Actuals_To_Match > Formals_To_Match then
18203
18204 -- Too many actuals: will not work
18205
18206 if Reporting then
18207 if Is_Entity_Name (Name (N)) then
18208 Error_Msg_N ("too many arguments in call to&", Name (N));
18209 else
18210 Error_Msg_N ("too many arguments in call", N);
18211 end if;
18212 end if;
18213
18214 Success := False;
18215 return;
18216 end if;
18217
18218 First_Named := Actual;
18219
18220 while Present (Actual) loop
18221 if Nkind (Actual) /= N_Parameter_Association then
18222 Error_Msg_N
18223 ("positional parameters not allowed after named ones", Actual);
18224 Success := False;
18225 return;
18226
18227 else
18228 Actuals_To_Match := Actuals_To_Match + 1;
18229 end if;
18230
18231 Next (Actual);
18232 end loop;
18233
18234 if Present (Actuals) then
18235 Actual := First (Actuals);
18236 end if;
18237
18238 Formal := First_Formal (S);
18239 while Present (Formal) loop
18240
18241 -- Match the formals in order. If the corresponding actual is
18242 -- positional, nothing to do. Else scan the list of named actuals
18243 -- to find the one with the right name.
18244
18245 if Present (Actual)
18246 and then Nkind (Actual) /= N_Parameter_Association
18247 then
18248 Next (Actual);
18249 Actuals_To_Match := Actuals_To_Match - 1;
18250 Formals_To_Match := Formals_To_Match - 1;
18251
18252 else
18253 -- For named parameters, search the list of actuals to find
18254 -- one that matches the next formal name.
18255
18256 Actual := First_Named;
18257 Found := False;
18258 while Present (Actual) loop
18259 if Chars (Selector_Name (Actual)) = Chars (Formal) then
18260 Found := True;
18261 Chain (Actual);
18262 Actuals_To_Match := Actuals_To_Match - 1;
18263 Formals_To_Match := Formals_To_Match - 1;
18264 exit;
18265 end if;
18266
18267 Next (Actual);
18268 end loop;
18269
18270 if not Found then
18271 if Ekind (Formal) /= E_In_Parameter
18272 or else No (Default_Value (Formal))
18273 then
18274 if Reporting then
18275 if (Comes_From_Source (S)
18276 or else Sloc (S) = Standard_Location)
18277 and then Is_Overloadable (S)
18278 then
18279 if No (Actuals)
18280 and then
18281 Nkind_In (Parent (N), N_Procedure_Call_Statement,
18282 N_Function_Call,
18283 N_Parameter_Association)
18284 and then Ekind (S) /= E_Function
18285 then
18286 Set_Etype (N, Etype (S));
18287
18288 else
18289 Error_Msg_Name_1 := Chars (S);
18290 Error_Msg_Sloc := Sloc (S);
18291 Error_Msg_NE
18292 ("missing argument for parameter & "
18293 & "in call to % declared #", N, Formal);
18294 end if;
18295
18296 elsif Is_Overloadable (S) then
18297 Error_Msg_Name_1 := Chars (S);
18298
18299 -- Point to type derivation that generated the
18300 -- operation.
18301
18302 Error_Msg_Sloc := Sloc (Parent (S));
18303
18304 Error_Msg_NE
18305 ("missing argument for parameter & "
18306 & "in call to % (inherited) #", N, Formal);
18307
18308 else
18309 Error_Msg_NE
18310 ("missing argument for parameter &", N, Formal);
18311 end if;
18312 end if;
18313
18314 Success := False;
18315 return;
18316
18317 else
18318 Formals_To_Match := Formals_To_Match - 1;
18319 end if;
18320 end if;
18321 end if;
18322
18323 Next_Formal (Formal);
18324 end loop;
18325
18326 if Formals_To_Match = 0 and then Actuals_To_Match = 0 then
18327 Success := True;
18328 return;
18329
18330 else
18331 if Reporting then
18332
18333 -- Find some superfluous named actual that did not get
18334 -- attached to the list of associations.
18335
18336 Actual := First (Actuals);
18337 while Present (Actual) loop
18338 if Nkind (Actual) = N_Parameter_Association
18339 and then Actual /= Last
18340 and then No (Next_Named_Actual (Actual))
18341 then
18342 -- A validity check may introduce a copy of a call that
18343 -- includes an extra actual (for example for an unrelated
18344 -- accessibility check). Check that the extra actual matches
18345 -- some extra formal, which must exist already because
18346 -- subprogram must be frozen at this point.
18347
18348 if Present (Extra_Formals (S))
18349 and then not Comes_From_Source (Actual)
18350 and then Nkind (Actual) = N_Parameter_Association
18351 and then Chars (Extra_Formals (S)) =
18352 Chars (Selector_Name (Actual))
18353 then
18354 null;
18355 else
18356 Error_Msg_N
18357 ("unmatched actual & in call", Selector_Name (Actual));
18358 exit;
18359 end if;
18360 end if;
18361
18362 Next (Actual);
18363 end loop;
18364 end if;
18365
18366 Success := False;
18367 return;
18368 end if;
18369 end Normalize_Actuals;
18370
18371 --------------------------------
18372 -- Note_Possible_Modification --
18373 --------------------------------
18374
18375 procedure Note_Possible_Modification (N : Node_Id; Sure : Boolean) is
18376 Modification_Comes_From_Source : constant Boolean :=
18377 Comes_From_Source (Parent (N));
18378
18379 Ent : Entity_Id;
18380 Exp : Node_Id;
18381
18382 begin
18383 -- Loop to find referenced entity, if there is one
18384
18385 Exp := N;
18386 loop
18387 Ent := Empty;
18388
18389 if Is_Entity_Name (Exp) then
18390 Ent := Entity (Exp);
18391
18392 -- If the entity is missing, it is an undeclared identifier,
18393 -- and there is nothing to annotate.
18394
18395 if No (Ent) then
18396 return;
18397 end if;
18398
18399 elsif Nkind (Exp) = N_Explicit_Dereference then
18400 declare
18401 P : constant Node_Id := Prefix (Exp);
18402
18403 begin
18404 -- In formal verification mode, keep track of all reads and
18405 -- writes through explicit dereferences.
18406
18407 if GNATprove_Mode then
18408 SPARK_Specific.Generate_Dereference (N, 'm');
18409 end if;
18410
18411 if Nkind (P) = N_Selected_Component
18412 and then Present (Entry_Formal (Entity (Selector_Name (P))))
18413 then
18414 -- Case of a reference to an entry formal
18415
18416 Ent := Entry_Formal (Entity (Selector_Name (P)));
18417
18418 elsif Nkind (P) = N_Identifier
18419 and then Nkind (Parent (Entity (P))) = N_Object_Declaration
18420 and then Present (Expression (Parent (Entity (P))))
18421 and then Nkind (Expression (Parent (Entity (P)))) =
18422 N_Reference
18423 then
18424 -- Case of a reference to a value on which side effects have
18425 -- been removed.
18426
18427 Exp := Prefix (Expression (Parent (Entity (P))));
18428 goto Continue;
18429
18430 else
18431 return;
18432 end if;
18433 end;
18434
18435 elsif Nkind_In (Exp, N_Type_Conversion,
18436 N_Unchecked_Type_Conversion)
18437 then
18438 Exp := Expression (Exp);
18439 goto Continue;
18440
18441 elsif Nkind_In (Exp, N_Slice,
18442 N_Indexed_Component,
18443 N_Selected_Component)
18444 then
18445 -- Special check, if the prefix is an access type, then return
18446 -- since we are modifying the thing pointed to, not the prefix.
18447 -- When we are expanding, most usually the prefix is replaced
18448 -- by an explicit dereference, and this test is not needed, but
18449 -- in some cases (notably -gnatc mode and generics) when we do
18450 -- not do full expansion, we need this special test.
18451
18452 if Is_Access_Type (Etype (Prefix (Exp))) then
18453 return;
18454
18455 -- Otherwise go to prefix and keep going
18456
18457 else
18458 Exp := Prefix (Exp);
18459 goto Continue;
18460 end if;
18461
18462 -- All other cases, not a modification
18463
18464 else
18465 return;
18466 end if;
18467
18468 -- Now look for entity being referenced
18469
18470 if Present (Ent) then
18471 if Is_Object (Ent) then
18472 if Comes_From_Source (Exp)
18473 or else Modification_Comes_From_Source
18474 then
18475 -- Give warning if pragma unmodified is given and we are
18476 -- sure this is a modification.
18477
18478 if Has_Pragma_Unmodified (Ent) and then Sure then
18479
18480 -- Note that the entity may be present only as a result
18481 -- of pragma Unused.
18482
18483 if Has_Pragma_Unused (Ent) then
18484 Error_Msg_NE ("??pragma Unused given for &!", N, Ent);
18485 else
18486 Error_Msg_NE
18487 ("??pragma Unmodified given for &!", N, Ent);
18488 end if;
18489 end if;
18490
18491 Set_Never_Set_In_Source (Ent, False);
18492 end if;
18493
18494 Set_Is_True_Constant (Ent, False);
18495 Set_Current_Value (Ent, Empty);
18496 Set_Is_Known_Null (Ent, False);
18497
18498 if not Can_Never_Be_Null (Ent) then
18499 Set_Is_Known_Non_Null (Ent, False);
18500 end if;
18501
18502 -- Follow renaming chain
18503
18504 if (Ekind (Ent) = E_Variable or else Ekind (Ent) = E_Constant)
18505 and then Present (Renamed_Object (Ent))
18506 then
18507 Exp := Renamed_Object (Ent);
18508
18509 -- If the entity is the loop variable in an iteration over
18510 -- a container, retrieve container expression to indicate
18511 -- possible modification.
18512
18513 if Present (Related_Expression (Ent))
18514 and then Nkind (Parent (Related_Expression (Ent))) =
18515 N_Iterator_Specification
18516 then
18517 Exp := Original_Node (Related_Expression (Ent));
18518 end if;
18519
18520 goto Continue;
18521
18522 -- The expression may be the renaming of a subcomponent of an
18523 -- array or container. The assignment to the subcomponent is
18524 -- a modification of the container.
18525
18526 elsif Comes_From_Source (Original_Node (Exp))
18527 and then Nkind_In (Original_Node (Exp), N_Selected_Component,
18528 N_Indexed_Component)
18529 then
18530 Exp := Prefix (Original_Node (Exp));
18531 goto Continue;
18532 end if;
18533
18534 -- Generate a reference only if the assignment comes from
18535 -- source. This excludes, for example, calls to a dispatching
18536 -- assignment operation when the left-hand side is tagged. In
18537 -- GNATprove mode, we need those references also on generated
18538 -- code, as these are used to compute the local effects of
18539 -- subprograms.
18540
18541 if Modification_Comes_From_Source or GNATprove_Mode then
18542 Generate_Reference (Ent, Exp, 'm');
18543
18544 -- If the target of the assignment is the bound variable
18545 -- in an iterator, indicate that the corresponding array
18546 -- or container is also modified.
18547
18548 if Ada_Version >= Ada_2012
18549 and then Nkind (Parent (Ent)) = N_Iterator_Specification
18550 then
18551 declare
18552 Domain : constant Node_Id := Name (Parent (Ent));
18553
18554 begin
18555 -- TBD : in the full version of the construct, the
18556 -- domain of iteration can be given by an expression.
18557
18558 if Is_Entity_Name (Domain) then
18559 Generate_Reference (Entity (Domain), Exp, 'm');
18560 Set_Is_True_Constant (Entity (Domain), False);
18561 Set_Never_Set_In_Source (Entity (Domain), False);
18562 end if;
18563 end;
18564 end if;
18565 end if;
18566 end if;
18567
18568 Kill_Checks (Ent);
18569
18570 -- If we are sure this is a modification from source, and we know
18571 -- this modifies a constant, then give an appropriate warning.
18572
18573 if Sure
18574 and then Modification_Comes_From_Source
18575 and then Overlays_Constant (Ent)
18576 and then Address_Clause_Overlay_Warnings
18577 then
18578 declare
18579 Addr : constant Node_Id := Address_Clause (Ent);
18580 O_Ent : Entity_Id;
18581 Off : Boolean;
18582
18583 begin
18584 Find_Overlaid_Entity (Addr, O_Ent, Off);
18585
18586 Error_Msg_Sloc := Sloc (Addr);
18587 Error_Msg_NE
18588 ("??constant& may be modified via address clause#",
18589 N, O_Ent);
18590 end;
18591 end if;
18592
18593 return;
18594 end if;
18595
18596 <<Continue>>
18597 null;
18598 end loop;
18599 end Note_Possible_Modification;
18600
18601 -----------------
18602 -- Null_Status --
18603 -----------------
18604
18605 function Null_Status (N : Node_Id) return Null_Status_Kind is
18606 function Is_Null_Excluding_Def (Def : Node_Id) return Boolean;
18607 -- Determine whether definition Def carries a null exclusion
18608
18609 function Null_Status_Of_Entity (Id : Entity_Id) return Null_Status_Kind;
18610 -- Determine the null status of arbitrary entity Id
18611
18612 function Null_Status_Of_Type (Typ : Entity_Id) return Null_Status_Kind;
18613 -- Determine the null status of type Typ
18614
18615 ---------------------------
18616 -- Is_Null_Excluding_Def --
18617 ---------------------------
18618
18619 function Is_Null_Excluding_Def (Def : Node_Id) return Boolean is
18620 begin
18621 return
18622 Nkind_In (Def, N_Access_Definition,
18623 N_Access_Function_Definition,
18624 N_Access_Procedure_Definition,
18625 N_Access_To_Object_Definition,
18626 N_Component_Definition,
18627 N_Derived_Type_Definition)
18628 and then Null_Exclusion_Present (Def);
18629 end Is_Null_Excluding_Def;
18630
18631 ---------------------------
18632 -- Null_Status_Of_Entity --
18633 ---------------------------
18634
18635 function Null_Status_Of_Entity
18636 (Id : Entity_Id) return Null_Status_Kind
18637 is
18638 Decl : constant Node_Id := Declaration_Node (Id);
18639 Def : Node_Id;
18640
18641 begin
18642 -- The value of an imported or exported entity may be set externally
18643 -- regardless of a null exclusion. As a result, the value cannot be
18644 -- determined statically.
18645
18646 if Is_Imported (Id) or else Is_Exported (Id) then
18647 return Unknown;
18648
18649 elsif Nkind_In (Decl, N_Component_Declaration,
18650 N_Discriminant_Specification,
18651 N_Formal_Object_Declaration,
18652 N_Object_Declaration,
18653 N_Object_Renaming_Declaration,
18654 N_Parameter_Specification)
18655 then
18656 -- A component declaration yields a non-null value when either
18657 -- its component definition or access definition carries a null
18658 -- exclusion.
18659
18660 if Nkind (Decl) = N_Component_Declaration then
18661 Def := Component_Definition (Decl);
18662
18663 if Is_Null_Excluding_Def (Def) then
18664 return Is_Non_Null;
18665 end if;
18666
18667 Def := Access_Definition (Def);
18668
18669 if Present (Def) and then Is_Null_Excluding_Def (Def) then
18670 return Is_Non_Null;
18671 end if;
18672
18673 -- A formal object declaration yields a non-null value if its
18674 -- access definition carries a null exclusion. If the object is
18675 -- default initialized, then the value depends on the expression.
18676
18677 elsif Nkind (Decl) = N_Formal_Object_Declaration then
18678 Def := Access_Definition (Decl);
18679
18680 if Present (Def) and then Is_Null_Excluding_Def (Def) then
18681 return Is_Non_Null;
18682 end if;
18683
18684 -- A constant may yield a null or non-null value depending on its
18685 -- initialization expression.
18686
18687 elsif Ekind (Id) = E_Constant then
18688 return Null_Status (Constant_Value (Id));
18689
18690 -- The construct yields a non-null value when it has a null
18691 -- exclusion.
18692
18693 elsif Null_Exclusion_Present (Decl) then
18694 return Is_Non_Null;
18695
18696 -- An object renaming declaration yields a non-null value if its
18697 -- access definition carries a null exclusion. Otherwise the value
18698 -- depends on the renamed name.
18699
18700 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
18701 Def := Access_Definition (Decl);
18702
18703 if Present (Def) and then Is_Null_Excluding_Def (Def) then
18704 return Is_Non_Null;
18705
18706 else
18707 return Null_Status (Name (Decl));
18708 end if;
18709 end if;
18710 end if;
18711
18712 -- At this point the declaration of the entity does not carry a null
18713 -- exclusion and lacks an initialization expression. Check the status
18714 -- of its type.
18715
18716 return Null_Status_Of_Type (Etype (Id));
18717 end Null_Status_Of_Entity;
18718
18719 -------------------------
18720 -- Null_Status_Of_Type --
18721 -------------------------
18722
18723 function Null_Status_Of_Type (Typ : Entity_Id) return Null_Status_Kind is
18724 Curr : Entity_Id;
18725 Decl : Node_Id;
18726
18727 begin
18728 -- Traverse the type chain looking for types with null exclusion
18729
18730 Curr := Typ;
18731 while Present (Curr) and then Etype (Curr) /= Curr loop
18732 Decl := Parent (Curr);
18733
18734 -- Guard against itypes which do not always have declarations. A
18735 -- type yields a non-null value if it carries a null exclusion.
18736
18737 if Present (Decl) then
18738 if Nkind (Decl) = N_Full_Type_Declaration
18739 and then Is_Null_Excluding_Def (Type_Definition (Decl))
18740 then
18741 return Is_Non_Null;
18742
18743 elsif Nkind (Decl) = N_Subtype_Declaration
18744 and then Null_Exclusion_Present (Decl)
18745 then
18746 return Is_Non_Null;
18747 end if;
18748 end if;
18749
18750 Curr := Etype (Curr);
18751 end loop;
18752
18753 -- The type chain does not contain any null excluding types
18754
18755 return Unknown;
18756 end Null_Status_Of_Type;
18757
18758 -- Start of processing for Null_Status
18759
18760 begin
18761 -- An allocator always creates a non-null value
18762
18763 if Nkind (N) = N_Allocator then
18764 return Is_Non_Null;
18765
18766 -- Taking the 'Access of something yields a non-null value
18767
18768 elsif Nkind (N) = N_Attribute_Reference
18769 and then Nam_In (Attribute_Name (N), Name_Access,
18770 Name_Unchecked_Access,
18771 Name_Unrestricted_Access)
18772 then
18773 return Is_Non_Null;
18774
18775 -- "null" yields null
18776
18777 elsif Nkind (N) = N_Null then
18778 return Is_Null;
18779
18780 -- Check the status of the operand of a type conversion
18781
18782 elsif Nkind (N) = N_Type_Conversion then
18783 return Null_Status (Expression (N));
18784
18785 -- The input denotes a reference to an entity. Determine whether the
18786 -- entity or its type yields a null or non-null value.
18787
18788 elsif Is_Entity_Name (N) and then Present (Entity (N)) then
18789 return Null_Status_Of_Entity (Entity (N));
18790 end if;
18791
18792 -- Otherwise it is not possible to determine the null status of the
18793 -- subexpression at compile time without resorting to simple flow
18794 -- analysis.
18795
18796 return Unknown;
18797 end Null_Status;
18798
18799 --------------------------------------
18800 -- Null_To_Null_Address_Convert_OK --
18801 --------------------------------------
18802
18803 function Null_To_Null_Address_Convert_OK
18804 (N : Node_Id;
18805 Typ : Entity_Id := Empty) return Boolean
18806 is
18807 begin
18808 if not Relaxed_RM_Semantics then
18809 return False;
18810 end if;
18811
18812 if Nkind (N) = N_Null then
18813 return Present (Typ) and then Is_Descendant_Of_Address (Typ);
18814
18815 elsif Nkind_In (N, N_Op_Eq, N_Op_Ge, N_Op_Gt, N_Op_Le, N_Op_Lt, N_Op_Ne)
18816 then
18817 declare
18818 L : constant Node_Id := Left_Opnd (N);
18819 R : constant Node_Id := Right_Opnd (N);
18820
18821 begin
18822 -- We check the Etype of the complementary operand since the
18823 -- N_Null node is not decorated at this stage.
18824
18825 return
18826 ((Nkind (L) = N_Null
18827 and then Is_Descendant_Of_Address (Etype (R)))
18828 or else
18829 (Nkind (R) = N_Null
18830 and then Is_Descendant_Of_Address (Etype (L))));
18831 end;
18832 end if;
18833
18834 return False;
18835 end Null_To_Null_Address_Convert_OK;
18836
18837 -------------------------
18838 -- Object_Access_Level --
18839 -------------------------
18840
18841 -- Returns the static accessibility level of the view denoted by Obj. Note
18842 -- that the value returned is the result of a call to Scope_Depth. Only
18843 -- scope depths associated with dynamic scopes can actually be returned.
18844 -- Since only relative levels matter for accessibility checking, the fact
18845 -- that the distance between successive levels of accessibility is not
18846 -- always one is immaterial (invariant: if level(E2) is deeper than
18847 -- level(E1), then Scope_Depth(E1) < Scope_Depth(E2)).
18848
18849 function Object_Access_Level (Obj : Node_Id) return Uint is
18850 function Is_Interface_Conversion (N : Node_Id) return Boolean;
18851 -- Determine whether N is a construct of the form
18852 -- Some_Type (Operand._tag'Address)
18853 -- This construct appears in the context of dispatching calls.
18854
18855 function Reference_To (Obj : Node_Id) return Node_Id;
18856 -- An explicit dereference is created when removing side-effects from
18857 -- expressions for constraint checking purposes. In this case a local
18858 -- access type is created for it. The correct access level is that of
18859 -- the original source node. We detect this case by noting that the
18860 -- prefix of the dereference is created by an object declaration whose
18861 -- initial expression is a reference.
18862
18863 -----------------------------
18864 -- Is_Interface_Conversion --
18865 -----------------------------
18866
18867 function Is_Interface_Conversion (N : Node_Id) return Boolean is
18868 begin
18869 return Nkind (N) = N_Unchecked_Type_Conversion
18870 and then Nkind (Expression (N)) = N_Attribute_Reference
18871 and then Attribute_Name (Expression (N)) = Name_Address;
18872 end Is_Interface_Conversion;
18873
18874 ------------------
18875 -- Reference_To --
18876 ------------------
18877
18878 function Reference_To (Obj : Node_Id) return Node_Id is
18879 Pref : constant Node_Id := Prefix (Obj);
18880 begin
18881 if Is_Entity_Name (Pref)
18882 and then Nkind (Parent (Entity (Pref))) = N_Object_Declaration
18883 and then Present (Expression (Parent (Entity (Pref))))
18884 and then Nkind (Expression (Parent (Entity (Pref)))) = N_Reference
18885 then
18886 return (Prefix (Expression (Parent (Entity (Pref)))));
18887 else
18888 return Empty;
18889 end if;
18890 end Reference_To;
18891
18892 -- Local variables
18893
18894 E : Entity_Id;
18895
18896 -- Start of processing for Object_Access_Level
18897
18898 begin
18899 if Nkind (Obj) = N_Defining_Identifier
18900 or else Is_Entity_Name (Obj)
18901 then
18902 if Nkind (Obj) = N_Defining_Identifier then
18903 E := Obj;
18904 else
18905 E := Entity (Obj);
18906 end if;
18907
18908 if Is_Prival (E) then
18909 E := Prival_Link (E);
18910 end if;
18911
18912 -- If E is a type then it denotes a current instance. For this case
18913 -- we add one to the normal accessibility level of the type to ensure
18914 -- that current instances are treated as always being deeper than
18915 -- than the level of any visible named access type (see 3.10.2(21)).
18916
18917 if Is_Type (E) then
18918 return Type_Access_Level (E) + 1;
18919
18920 elsif Present (Renamed_Object (E)) then
18921 return Object_Access_Level (Renamed_Object (E));
18922
18923 -- Similarly, if E is a component of the current instance of a
18924 -- protected type, any instance of it is assumed to be at a deeper
18925 -- level than the type. For a protected object (whose type is an
18926 -- anonymous protected type) its components are at the same level
18927 -- as the type itself.
18928
18929 elsif not Is_Overloadable (E)
18930 and then Ekind (Scope (E)) = E_Protected_Type
18931 and then Comes_From_Source (Scope (E))
18932 then
18933 return Type_Access_Level (Scope (E)) + 1;
18934
18935 else
18936 -- Aliased formals of functions take their access level from the
18937 -- point of call, i.e. require a dynamic check. For static check
18938 -- purposes, this is smaller than the level of the subprogram
18939 -- itself. For procedures the aliased makes no difference.
18940
18941 if Is_Formal (E)
18942 and then Is_Aliased (E)
18943 and then Ekind (Scope (E)) = E_Function
18944 then
18945 return Type_Access_Level (Etype (E));
18946
18947 else
18948 return Scope_Depth (Enclosing_Dynamic_Scope (E));
18949 end if;
18950 end if;
18951
18952 elsif Nkind (Obj) = N_Selected_Component then
18953 if Is_Access_Type (Etype (Prefix (Obj))) then
18954 return Type_Access_Level (Etype (Prefix (Obj)));
18955 else
18956 return Object_Access_Level (Prefix (Obj));
18957 end if;
18958
18959 elsif Nkind (Obj) = N_Indexed_Component then
18960 if Is_Access_Type (Etype (Prefix (Obj))) then
18961 return Type_Access_Level (Etype (Prefix (Obj)));
18962 else
18963 return Object_Access_Level (Prefix (Obj));
18964 end if;
18965
18966 elsif Nkind (Obj) = N_Explicit_Dereference then
18967
18968 -- If the prefix is a selected access discriminant then we make a
18969 -- recursive call on the prefix, which will in turn check the level
18970 -- of the prefix object of the selected discriminant.
18971
18972 -- In Ada 2012, if the discriminant has implicit dereference and
18973 -- the context is a selected component, treat this as an object of
18974 -- unknown scope (see below). This is necessary in compile-only mode;
18975 -- otherwise expansion will already have transformed the prefix into
18976 -- a temporary.
18977
18978 if Nkind (Prefix (Obj)) = N_Selected_Component
18979 and then Ekind (Etype (Prefix (Obj))) = E_Anonymous_Access_Type
18980 and then
18981 Ekind (Entity (Selector_Name (Prefix (Obj)))) = E_Discriminant
18982 and then
18983 (not Has_Implicit_Dereference
18984 (Entity (Selector_Name (Prefix (Obj))))
18985 or else Nkind (Parent (Obj)) /= N_Selected_Component)
18986 then
18987 return Object_Access_Level (Prefix (Obj));
18988
18989 -- Detect an interface conversion in the context of a dispatching
18990 -- call. Use the original form of the conversion to find the access
18991 -- level of the operand.
18992
18993 elsif Is_Interface (Etype (Obj))
18994 and then Is_Interface_Conversion (Prefix (Obj))
18995 and then Nkind (Original_Node (Obj)) = N_Type_Conversion
18996 then
18997 return Object_Access_Level (Original_Node (Obj));
18998
18999 elsif not Comes_From_Source (Obj) then
19000 declare
19001 Ref : constant Node_Id := Reference_To (Obj);
19002 begin
19003 if Present (Ref) then
19004 return Object_Access_Level (Ref);
19005 else
19006 return Type_Access_Level (Etype (Prefix (Obj)));
19007 end if;
19008 end;
19009
19010 else
19011 return Type_Access_Level (Etype (Prefix (Obj)));
19012 end if;
19013
19014 elsif Nkind_In (Obj, N_Type_Conversion, N_Unchecked_Type_Conversion) then
19015 return Object_Access_Level (Expression (Obj));
19016
19017 elsif Nkind (Obj) = N_Function_Call then
19018
19019 -- Function results are objects, so we get either the access level of
19020 -- the function or, in the case of an indirect call, the level of the
19021 -- access-to-subprogram type. (This code is used for Ada 95, but it
19022 -- looks wrong, because it seems that we should be checking the level
19023 -- of the call itself, even for Ada 95. However, using the Ada 2005
19024 -- version of the code causes regressions in several tests that are
19025 -- compiled with -gnat95. ???)
19026
19027 if Ada_Version < Ada_2005 then
19028 if Is_Entity_Name (Name (Obj)) then
19029 return Subprogram_Access_Level (Entity (Name (Obj)));
19030 else
19031 return Type_Access_Level (Etype (Prefix (Name (Obj))));
19032 end if;
19033
19034 -- For Ada 2005, the level of the result object of a function call is
19035 -- defined to be the level of the call's innermost enclosing master.
19036 -- We determine that by querying the depth of the innermost enclosing
19037 -- dynamic scope.
19038
19039 else
19040 Return_Master_Scope_Depth_Of_Call : declare
19041 function Innermost_Master_Scope_Depth
19042 (N : Node_Id) return Uint;
19043 -- Returns the scope depth of the given node's innermost
19044 -- enclosing dynamic scope (effectively the accessibility
19045 -- level of the innermost enclosing master).
19046
19047 ----------------------------------
19048 -- Innermost_Master_Scope_Depth --
19049 ----------------------------------
19050
19051 function Innermost_Master_Scope_Depth
19052 (N : Node_Id) return Uint
19053 is
19054 Node_Par : Node_Id := Parent (N);
19055
19056 begin
19057 -- Locate the nearest enclosing node (by traversing Parents)
19058 -- that Defining_Entity can be applied to, and return the
19059 -- depth of that entity's nearest enclosing dynamic scope.
19060
19061 while Present (Node_Par) loop
19062 case Nkind (Node_Par) is
19063 when N_Abstract_Subprogram_Declaration
19064 | N_Block_Statement
19065 | N_Body_Stub
19066 | N_Component_Declaration
19067 | N_Entry_Body
19068 | N_Entry_Declaration
19069 | N_Exception_Declaration
19070 | N_Formal_Object_Declaration
19071 | N_Formal_Package_Declaration
19072 | N_Formal_Subprogram_Declaration
19073 | N_Formal_Type_Declaration
19074 | N_Full_Type_Declaration
19075 | N_Function_Specification
19076 | N_Generic_Declaration
19077 | N_Generic_Instantiation
19078 | N_Implicit_Label_Declaration
19079 | N_Incomplete_Type_Declaration
19080 | N_Loop_Parameter_Specification
19081 | N_Number_Declaration
19082 | N_Object_Declaration
19083 | N_Package_Declaration
19084 | N_Package_Specification
19085 | N_Parameter_Specification
19086 | N_Private_Extension_Declaration
19087 | N_Private_Type_Declaration
19088 | N_Procedure_Specification
19089 | N_Proper_Body
19090 | N_Protected_Type_Declaration
19091 | N_Renaming_Declaration
19092 | N_Single_Protected_Declaration
19093 | N_Single_Task_Declaration
19094 | N_Subprogram_Declaration
19095 | N_Subtype_Declaration
19096 | N_Subunit
19097 | N_Task_Type_Declaration
19098 =>
19099 return Scope_Depth
19100 (Nearest_Dynamic_Scope
19101 (Defining_Entity (Node_Par)));
19102
19103 when others =>
19104 null;
19105 end case;
19106
19107 Node_Par := Parent (Node_Par);
19108 end loop;
19109
19110 pragma Assert (False);
19111
19112 -- Should never reach the following return
19113
19114 return Scope_Depth (Current_Scope) + 1;
19115 end Innermost_Master_Scope_Depth;
19116
19117 -- Start of processing for Return_Master_Scope_Depth_Of_Call
19118
19119 begin
19120 return Innermost_Master_Scope_Depth (Obj);
19121 end Return_Master_Scope_Depth_Of_Call;
19122 end if;
19123
19124 -- For convenience we handle qualified expressions, even though they
19125 -- aren't technically object names.
19126
19127 elsif Nkind (Obj) = N_Qualified_Expression then
19128 return Object_Access_Level (Expression (Obj));
19129
19130 -- Ditto for aggregates. They have the level of the temporary that
19131 -- will hold their value.
19132
19133 elsif Nkind (Obj) = N_Aggregate then
19134 return Object_Access_Level (Current_Scope);
19135
19136 -- Otherwise return the scope level of Standard. (If there are cases
19137 -- that fall through to this point they will be treated as having
19138 -- global accessibility for now. ???)
19139
19140 else
19141 return Scope_Depth (Standard_Standard);
19142 end if;
19143 end Object_Access_Level;
19144
19145 ----------------------------------
19146 -- Old_Requires_Transient_Scope --
19147 ----------------------------------
19148
19149 function Old_Requires_Transient_Scope (Id : Entity_Id) return Boolean is
19150 Typ : constant Entity_Id := Underlying_Type (Id);
19151
19152 begin
19153 -- This is a private type which is not completed yet. This can only
19154 -- happen in a default expression (of a formal parameter or of a
19155 -- record component). Do not expand transient scope in this case.
19156
19157 if No (Typ) then
19158 return False;
19159
19160 -- Do not expand transient scope for non-existent procedure return
19161
19162 elsif Typ = Standard_Void_Type then
19163 return False;
19164
19165 -- Elementary types do not require a transient scope
19166
19167 elsif Is_Elementary_Type (Typ) then
19168 return False;
19169
19170 -- Generally, indefinite subtypes require a transient scope, since the
19171 -- back end cannot generate temporaries, since this is not a valid type
19172 -- for declaring an object. It might be possible to relax this in the
19173 -- future, e.g. by declaring the maximum possible space for the type.
19174
19175 elsif not Is_Definite_Subtype (Typ) then
19176 return True;
19177
19178 -- Functions returning tagged types may dispatch on result so their
19179 -- returned value is allocated on the secondary stack. Controlled
19180 -- type temporaries need finalization.
19181
19182 elsif Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
19183 return True;
19184
19185 -- Record type
19186
19187 elsif Is_Record_Type (Typ) then
19188 declare
19189 Comp : Entity_Id;
19190
19191 begin
19192 Comp := First_Entity (Typ);
19193 while Present (Comp) loop
19194 if Ekind (Comp) = E_Component then
19195
19196 -- ???It's not clear we need a full recursive call to
19197 -- Old_Requires_Transient_Scope here. Note that the
19198 -- following can't happen.
19199
19200 pragma Assert (Is_Definite_Subtype (Etype (Comp)));
19201 pragma Assert (not Has_Controlled_Component (Etype (Comp)));
19202
19203 if Old_Requires_Transient_Scope (Etype (Comp)) then
19204 return True;
19205 end if;
19206 end if;
19207
19208 Next_Entity (Comp);
19209 end loop;
19210 end;
19211
19212 return False;
19213
19214 -- String literal types never require transient scope
19215
19216 elsif Ekind (Typ) = E_String_Literal_Subtype then
19217 return False;
19218
19219 -- Array type. Note that we already know that this is a constrained
19220 -- array, since unconstrained arrays will fail the indefinite test.
19221
19222 elsif Is_Array_Type (Typ) then
19223
19224 -- If component type requires a transient scope, the array does too
19225
19226 if Old_Requires_Transient_Scope (Component_Type (Typ)) then
19227 return True;
19228
19229 -- Otherwise, we only need a transient scope if the size depends on
19230 -- the value of one or more discriminants.
19231
19232 else
19233 return Size_Depends_On_Discriminant (Typ);
19234 end if;
19235
19236 -- All other cases do not require a transient scope
19237
19238 else
19239 pragma Assert (Is_Protected_Type (Typ) or else Is_Task_Type (Typ));
19240 return False;
19241 end if;
19242 end Old_Requires_Transient_Scope;
19243
19244 ---------------------------------
19245 -- Original_Aspect_Pragma_Name --
19246 ---------------------------------
19247
19248 function Original_Aspect_Pragma_Name (N : Node_Id) return Name_Id is
19249 Item : Node_Id;
19250 Item_Nam : Name_Id;
19251
19252 begin
19253 pragma Assert (Nkind_In (N, N_Aspect_Specification, N_Pragma));
19254
19255 Item := N;
19256
19257 -- The pragma was generated to emulate an aspect, use the original
19258 -- aspect specification.
19259
19260 if Nkind (Item) = N_Pragma and then From_Aspect_Specification (Item) then
19261 Item := Corresponding_Aspect (Item);
19262 end if;
19263
19264 -- Retrieve the name of the aspect/pragma. Note that Pre, Pre_Class,
19265 -- Post and Post_Class rewrite their pragma identifier to preserve the
19266 -- original name.
19267 -- ??? this is kludgey
19268
19269 if Nkind (Item) = N_Pragma then
19270 Item_Nam := Chars (Original_Node (Pragma_Identifier (Item)));
19271
19272 else
19273 pragma Assert (Nkind (Item) = N_Aspect_Specification);
19274 Item_Nam := Chars (Identifier (Item));
19275 end if;
19276
19277 -- Deal with 'Class by converting the name to its _XXX form
19278
19279 if Class_Present (Item) then
19280 if Item_Nam = Name_Invariant then
19281 Item_Nam := Name_uInvariant;
19282
19283 elsif Item_Nam = Name_Post then
19284 Item_Nam := Name_uPost;
19285
19286 elsif Item_Nam = Name_Pre then
19287 Item_Nam := Name_uPre;
19288
19289 elsif Nam_In (Item_Nam, Name_Type_Invariant,
19290 Name_Type_Invariant_Class)
19291 then
19292 Item_Nam := Name_uType_Invariant;
19293
19294 -- Nothing to do for other cases (e.g. a Check that derived from
19295 -- Pre_Class and has the flag set). Also we do nothing if the name
19296 -- is already in special _xxx form.
19297
19298 end if;
19299 end if;
19300
19301 return Item_Nam;
19302 end Original_Aspect_Pragma_Name;
19303
19304 --------------------------------------
19305 -- Original_Corresponding_Operation --
19306 --------------------------------------
19307
19308 function Original_Corresponding_Operation (S : Entity_Id) return Entity_Id
19309 is
19310 Typ : constant Entity_Id := Find_Dispatching_Type (S);
19311
19312 begin
19313 -- If S is an inherited primitive S2 the original corresponding
19314 -- operation of S is the original corresponding operation of S2
19315
19316 if Present (Alias (S))
19317 and then Find_Dispatching_Type (Alias (S)) /= Typ
19318 then
19319 return Original_Corresponding_Operation (Alias (S));
19320
19321 -- If S overrides an inherited subprogram S2 the original corresponding
19322 -- operation of S is the original corresponding operation of S2
19323
19324 elsif Present (Overridden_Operation (S)) then
19325 return Original_Corresponding_Operation (Overridden_Operation (S));
19326
19327 -- otherwise it is S itself
19328
19329 else
19330 return S;
19331 end if;
19332 end Original_Corresponding_Operation;
19333
19334 -------------------
19335 -- Output_Entity --
19336 -------------------
19337
19338 procedure Output_Entity (Id : Entity_Id) is
19339 Scop : Entity_Id;
19340
19341 begin
19342 Scop := Scope (Id);
19343
19344 -- The entity may lack a scope when it is in the process of being
19345 -- analyzed. Use the current scope as an approximation.
19346
19347 if No (Scop) then
19348 Scop := Current_Scope;
19349 end if;
19350
19351 Output_Name (Chars (Id), Scop);
19352 end Output_Entity;
19353
19354 -----------------
19355 -- Output_Name --
19356 -----------------
19357
19358 procedure Output_Name (Nam : Name_Id; Scop : Entity_Id := Current_Scope) is
19359 begin
19360 Write_Str
19361 (Get_Name_String
19362 (Get_Qualified_Name
19363 (Nam => Nam,
19364 Suffix => No_Name,
19365 Scop => Scop)));
19366 Write_Eol;
19367 end Output_Name;
19368
19369 ----------------------
19370 -- Policy_In_Effect --
19371 ----------------------
19372
19373 function Policy_In_Effect (Policy : Name_Id) return Name_Id is
19374 function Policy_In_List (List : Node_Id) return Name_Id;
19375 -- Determine the mode of a policy in a N_Pragma list
19376
19377 --------------------
19378 -- Policy_In_List --
19379 --------------------
19380
19381 function Policy_In_List (List : Node_Id) return Name_Id is
19382 Arg1 : Node_Id;
19383 Arg2 : Node_Id;
19384 Prag : Node_Id;
19385
19386 begin
19387 Prag := List;
19388 while Present (Prag) loop
19389 Arg1 := First (Pragma_Argument_Associations (Prag));
19390 Arg2 := Next (Arg1);
19391
19392 Arg1 := Get_Pragma_Arg (Arg1);
19393 Arg2 := Get_Pragma_Arg (Arg2);
19394
19395 -- The current Check_Policy pragma matches the requested policy or
19396 -- appears in the single argument form (Assertion, policy_id).
19397
19398 if Nam_In (Chars (Arg1), Name_Assertion, Policy) then
19399 return Chars (Arg2);
19400 end if;
19401
19402 Prag := Next_Pragma (Prag);
19403 end loop;
19404
19405 return No_Name;
19406 end Policy_In_List;
19407
19408 -- Local variables
19409
19410 Kind : Name_Id;
19411
19412 -- Start of processing for Policy_In_Effect
19413
19414 begin
19415 if not Is_Valid_Assertion_Kind (Policy) then
19416 raise Program_Error;
19417 end if;
19418
19419 -- Inspect all policy pragmas that appear within scopes (if any)
19420
19421 Kind := Policy_In_List (Check_Policy_List);
19422
19423 -- Inspect all configuration policy pragmas (if any)
19424
19425 if Kind = No_Name then
19426 Kind := Policy_In_List (Check_Policy_List_Config);
19427 end if;
19428
19429 -- The context lacks policy pragmas, determine the mode based on whether
19430 -- assertions are enabled at the configuration level. This ensures that
19431 -- the policy is preserved when analyzing generics.
19432
19433 if Kind = No_Name then
19434 if Assertions_Enabled_Config then
19435 Kind := Name_Check;
19436 else
19437 Kind := Name_Ignore;
19438 end if;
19439 end if;
19440
19441 return Kind;
19442 end Policy_In_Effect;
19443
19444 ----------------------------------
19445 -- Predicate_Tests_On_Arguments --
19446 ----------------------------------
19447
19448 function Predicate_Tests_On_Arguments (Subp : Entity_Id) return Boolean is
19449 begin
19450 -- Always test predicates on indirect call
19451
19452 if Ekind (Subp) = E_Subprogram_Type then
19453 return True;
19454
19455 -- Do not test predicates on call to generated default Finalize, since
19456 -- we are not interested in whether something we are finalizing (and
19457 -- typically destroying) satisfies its predicates.
19458
19459 elsif Chars (Subp) = Name_Finalize
19460 and then not Comes_From_Source (Subp)
19461 then
19462 return False;
19463
19464 -- Do not test predicates on any internally generated routines
19465
19466 elsif Is_Internal_Name (Chars (Subp)) then
19467 return False;
19468
19469 -- Do not test predicates on call to Init_Proc, since if needed the
19470 -- predicate test will occur at some other point.
19471
19472 elsif Is_Init_Proc (Subp) then
19473 return False;
19474
19475 -- Do not test predicates on call to predicate function, since this
19476 -- would cause infinite recursion.
19477
19478 elsif Ekind (Subp) = E_Function
19479 and then (Is_Predicate_Function (Subp)
19480 or else
19481 Is_Predicate_Function_M (Subp))
19482 then
19483 return False;
19484
19485 -- For now, no other exceptions
19486
19487 else
19488 return True;
19489 end if;
19490 end Predicate_Tests_On_Arguments;
19491
19492 -----------------------
19493 -- Private_Component --
19494 -----------------------
19495
19496 function Private_Component (Type_Id : Entity_Id) return Entity_Id is
19497 Ancestor : constant Entity_Id := Base_Type (Type_Id);
19498
19499 function Trace_Components
19500 (T : Entity_Id;
19501 Check : Boolean) return Entity_Id;
19502 -- Recursive function that does the work, and checks against circular
19503 -- definition for each subcomponent type.
19504
19505 ----------------------
19506 -- Trace_Components --
19507 ----------------------
19508
19509 function Trace_Components
19510 (T : Entity_Id;
19511 Check : Boolean) return Entity_Id
19512 is
19513 Btype : constant Entity_Id := Base_Type (T);
19514 Component : Entity_Id;
19515 P : Entity_Id;
19516 Candidate : Entity_Id := Empty;
19517
19518 begin
19519 if Check and then Btype = Ancestor then
19520 Error_Msg_N ("circular type definition", Type_Id);
19521 return Any_Type;
19522 end if;
19523
19524 if Is_Private_Type (Btype) and then not Is_Generic_Type (Btype) then
19525 if Present (Full_View (Btype))
19526 and then Is_Record_Type (Full_View (Btype))
19527 and then not Is_Frozen (Btype)
19528 then
19529 -- To indicate that the ancestor depends on a private type, the
19530 -- current Btype is sufficient. However, to check for circular
19531 -- definition we must recurse on the full view.
19532
19533 Candidate := Trace_Components (Full_View (Btype), True);
19534
19535 if Candidate = Any_Type then
19536 return Any_Type;
19537 else
19538 return Btype;
19539 end if;
19540
19541 else
19542 return Btype;
19543 end if;
19544
19545 elsif Is_Array_Type (Btype) then
19546 return Trace_Components (Component_Type (Btype), True);
19547
19548 elsif Is_Record_Type (Btype) then
19549 Component := First_Entity (Btype);
19550 while Present (Component)
19551 and then Comes_From_Source (Component)
19552 loop
19553 -- Skip anonymous types generated by constrained components
19554
19555 if not Is_Type (Component) then
19556 P := Trace_Components (Etype (Component), True);
19557
19558 if Present (P) then
19559 if P = Any_Type then
19560 return P;
19561 else
19562 Candidate := P;
19563 end if;
19564 end if;
19565 end if;
19566
19567 Next_Entity (Component);
19568 end loop;
19569
19570 return Candidate;
19571
19572 else
19573 return Empty;
19574 end if;
19575 end Trace_Components;
19576
19577 -- Start of processing for Private_Component
19578
19579 begin
19580 return Trace_Components (Type_Id, False);
19581 end Private_Component;
19582
19583 ---------------------------
19584 -- Primitive_Names_Match --
19585 ---------------------------
19586
19587 function Primitive_Names_Match (E1, E2 : Entity_Id) return Boolean is
19588 function Non_Internal_Name (E : Entity_Id) return Name_Id;
19589 -- Given an internal name, returns the corresponding non-internal name
19590
19591 ------------------------
19592 -- Non_Internal_Name --
19593 ------------------------
19594
19595 function Non_Internal_Name (E : Entity_Id) return Name_Id is
19596 begin
19597 Get_Name_String (Chars (E));
19598 Name_Len := Name_Len - 1;
19599 return Name_Find;
19600 end Non_Internal_Name;
19601
19602 -- Start of processing for Primitive_Names_Match
19603
19604 begin
19605 pragma Assert (Present (E1) and then Present (E2));
19606
19607 return Chars (E1) = Chars (E2)
19608 or else
19609 (not Is_Internal_Name (Chars (E1))
19610 and then Is_Internal_Name (Chars (E2))
19611 and then Non_Internal_Name (E2) = Chars (E1))
19612 or else
19613 (not Is_Internal_Name (Chars (E2))
19614 and then Is_Internal_Name (Chars (E1))
19615 and then Non_Internal_Name (E1) = Chars (E2))
19616 or else
19617 (Is_Predefined_Dispatching_Operation (E1)
19618 and then Is_Predefined_Dispatching_Operation (E2)
19619 and then Same_TSS (E1, E2))
19620 or else
19621 (Is_Init_Proc (E1) and then Is_Init_Proc (E2));
19622 end Primitive_Names_Match;
19623
19624 -----------------------
19625 -- Process_End_Label --
19626 -----------------------
19627
19628 procedure Process_End_Label
19629 (N : Node_Id;
19630 Typ : Character;
19631 Ent : Entity_Id)
19632 is
19633 Loc : Source_Ptr;
19634 Nam : Node_Id;
19635 Scop : Entity_Id;
19636
19637 Label_Ref : Boolean;
19638 -- Set True if reference to end label itself is required
19639
19640 Endl : Node_Id;
19641 -- Gets set to the operator symbol or identifier that references the
19642 -- entity Ent. For the child unit case, this is the identifier from the
19643 -- designator. For other cases, this is simply Endl.
19644
19645 procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id);
19646 -- N is an identifier node that appears as a parent unit reference in
19647 -- the case where Ent is a child unit. This procedure generates an
19648 -- appropriate cross-reference entry. E is the corresponding entity.
19649
19650 -------------------------
19651 -- Generate_Parent_Ref --
19652 -------------------------
19653
19654 procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id) is
19655 begin
19656 -- If names do not match, something weird, skip reference
19657
19658 if Chars (E) = Chars (N) then
19659
19660 -- Generate the reference. We do NOT consider this as a reference
19661 -- for unreferenced symbol purposes.
19662
19663 Generate_Reference (E, N, 'r', Set_Ref => False, Force => True);
19664
19665 if Style_Check then
19666 Style.Check_Identifier (N, E);
19667 end if;
19668 end if;
19669 end Generate_Parent_Ref;
19670
19671 -- Start of processing for Process_End_Label
19672
19673 begin
19674 -- If no node, ignore. This happens in some error situations, and
19675 -- also for some internally generated structures where no end label
19676 -- references are required in any case.
19677
19678 if No (N) then
19679 return;
19680 end if;
19681
19682 -- Nothing to do if no End_Label, happens for internally generated
19683 -- constructs where we don't want an end label reference anyway. Also
19684 -- nothing to do if Endl is a string literal, which means there was
19685 -- some prior error (bad operator symbol)
19686
19687 Endl := End_Label (N);
19688
19689 if No (Endl) or else Nkind (Endl) = N_String_Literal then
19690 return;
19691 end if;
19692
19693 -- Reference node is not in extended main source unit
19694
19695 if not In_Extended_Main_Source_Unit (N) then
19696
19697 -- Generally we do not collect references except for the extended
19698 -- main source unit. The one exception is the 'e' entry for a
19699 -- package spec, where it is useful for a client to have the
19700 -- ending information to define scopes.
19701
19702 if Typ /= 'e' then
19703 return;
19704
19705 else
19706 Label_Ref := False;
19707
19708 -- For this case, we can ignore any parent references, but we
19709 -- need the package name itself for the 'e' entry.
19710
19711 if Nkind (Endl) = N_Designator then
19712 Endl := Identifier (Endl);
19713 end if;
19714 end if;
19715
19716 -- Reference is in extended main source unit
19717
19718 else
19719 Label_Ref := True;
19720
19721 -- For designator, generate references for the parent entries
19722
19723 if Nkind (Endl) = N_Designator then
19724
19725 -- Generate references for the prefix if the END line comes from
19726 -- source (otherwise we do not need these references) We climb the
19727 -- scope stack to find the expected entities.
19728
19729 if Comes_From_Source (Endl) then
19730 Nam := Name (Endl);
19731 Scop := Current_Scope;
19732 while Nkind (Nam) = N_Selected_Component loop
19733 Scop := Scope (Scop);
19734 exit when No (Scop);
19735 Generate_Parent_Ref (Selector_Name (Nam), Scop);
19736 Nam := Prefix (Nam);
19737 end loop;
19738
19739 if Present (Scop) then
19740 Generate_Parent_Ref (Nam, Scope (Scop));
19741 end if;
19742 end if;
19743
19744 Endl := Identifier (Endl);
19745 end if;
19746 end if;
19747
19748 -- If the end label is not for the given entity, then either we have
19749 -- some previous error, or this is a generic instantiation for which
19750 -- we do not need to make a cross-reference in this case anyway. In
19751 -- either case we simply ignore the call.
19752
19753 if Chars (Ent) /= Chars (Endl) then
19754 return;
19755 end if;
19756
19757 -- If label was really there, then generate a normal reference and then
19758 -- adjust the location in the end label to point past the name (which
19759 -- should almost always be the semicolon).
19760
19761 Loc := Sloc (Endl);
19762
19763 if Comes_From_Source (Endl) then
19764
19765 -- If a label reference is required, then do the style check and
19766 -- generate an l-type cross-reference entry for the label
19767
19768 if Label_Ref then
19769 if Style_Check then
19770 Style.Check_Identifier (Endl, Ent);
19771 end if;
19772
19773 Generate_Reference (Ent, Endl, 'l', Set_Ref => False);
19774 end if;
19775
19776 -- Set the location to point past the label (normally this will
19777 -- mean the semicolon immediately following the label). This is
19778 -- done for the sake of the 'e' or 't' entry generated below.
19779
19780 Get_Decoded_Name_String (Chars (Endl));
19781 Set_Sloc (Endl, Sloc (Endl) + Source_Ptr (Name_Len));
19782
19783 else
19784 -- In SPARK mode, no missing label is allowed for packages and
19785 -- subprogram bodies. Detect those cases by testing whether
19786 -- Process_End_Label was called for a body (Typ = 't') or a package.
19787
19788 if Restriction_Check_Required (SPARK_05)
19789 and then (Typ = 't' or else Ekind (Ent) = E_Package)
19790 then
19791 Error_Msg_Node_1 := Endl;
19792 Check_SPARK_05_Restriction
19793 ("`END &` required", Endl, Force => True);
19794 end if;
19795 end if;
19796
19797 -- Now generate the e/t reference
19798
19799 Generate_Reference (Ent, Endl, Typ, Set_Ref => False, Force => True);
19800
19801 -- Restore Sloc, in case modified above, since we have an identifier
19802 -- and the normal Sloc should be left set in the tree.
19803
19804 Set_Sloc (Endl, Loc);
19805 end Process_End_Label;
19806
19807 --------------------------------
19808 -- Propagate_Concurrent_Flags --
19809 --------------------------------
19810
19811 procedure Propagate_Concurrent_Flags
19812 (Typ : Entity_Id;
19813 Comp_Typ : Entity_Id)
19814 is
19815 begin
19816 if Has_Task (Comp_Typ) then
19817 Set_Has_Task (Typ);
19818 end if;
19819
19820 if Has_Protected (Comp_Typ) then
19821 Set_Has_Protected (Typ);
19822 end if;
19823
19824 if Has_Timing_Event (Comp_Typ) then
19825 Set_Has_Timing_Event (Typ);
19826 end if;
19827 end Propagate_Concurrent_Flags;
19828
19829 ------------------------------
19830 -- Propagate_DIC_Attributes --
19831 ------------------------------
19832
19833 procedure Propagate_DIC_Attributes
19834 (Typ : Entity_Id;
19835 From_Typ : Entity_Id)
19836 is
19837 DIC_Proc : Entity_Id;
19838
19839 begin
19840 if Present (Typ) and then Present (From_Typ) then
19841 pragma Assert (Is_Type (Typ) and then Is_Type (From_Typ));
19842
19843 -- Nothing to do if both the source and the destination denote the
19844 -- same type.
19845
19846 if From_Typ = Typ then
19847 return;
19848 end if;
19849
19850 DIC_Proc := DIC_Procedure (From_Typ);
19851
19852 -- The setting of the attributes is intentionally conservative. This
19853 -- prevents accidental clobbering of enabled attributes.
19854
19855 if Has_Inherited_DIC (From_Typ)
19856 and then not Has_Inherited_DIC (Typ)
19857 then
19858 Set_Has_Inherited_DIC (Typ);
19859 end if;
19860
19861 if Has_Own_DIC (From_Typ) and then not Has_Own_DIC (Typ) then
19862 Set_Has_Own_DIC (Typ);
19863 end if;
19864
19865 if Present (DIC_Proc) and then No (DIC_Procedure (Typ)) then
19866 Set_DIC_Procedure (Typ, DIC_Proc);
19867 end if;
19868 end if;
19869 end Propagate_DIC_Attributes;
19870
19871 ------------------------------------
19872 -- Propagate_Invariant_Attributes --
19873 ------------------------------------
19874
19875 procedure Propagate_Invariant_Attributes
19876 (Typ : Entity_Id;
19877 From_Typ : Entity_Id)
19878 is
19879 Full_IP : Entity_Id;
19880 Part_IP : Entity_Id;
19881
19882 begin
19883 if Present (Typ) and then Present (From_Typ) then
19884 pragma Assert (Is_Type (Typ) and then Is_Type (From_Typ));
19885
19886 -- Nothing to do if both the source and the destination denote the
19887 -- same type.
19888
19889 if From_Typ = Typ then
19890 return;
19891 end if;
19892
19893 Full_IP := Invariant_Procedure (From_Typ);
19894 Part_IP := Partial_Invariant_Procedure (From_Typ);
19895
19896 -- The setting of the attributes is intentionally conservative. This
19897 -- prevents accidental clobbering of enabled attributes.
19898
19899 if Has_Inheritable_Invariants (From_Typ)
19900 and then not Has_Inheritable_Invariants (Typ)
19901 then
19902 Set_Has_Inheritable_Invariants (Typ, True);
19903 end if;
19904
19905 if Has_Inherited_Invariants (From_Typ)
19906 and then not Has_Inherited_Invariants (Typ)
19907 then
19908 Set_Has_Inherited_Invariants (Typ, True);
19909 end if;
19910
19911 if Has_Own_Invariants (From_Typ)
19912 and then not Has_Own_Invariants (Typ)
19913 then
19914 Set_Has_Own_Invariants (Typ, True);
19915 end if;
19916
19917 if Present (Full_IP) and then No (Invariant_Procedure (Typ)) then
19918 Set_Invariant_Procedure (Typ, Full_IP);
19919 end if;
19920
19921 if Present (Part_IP) and then No (Partial_Invariant_Procedure (Typ))
19922 then
19923 Set_Partial_Invariant_Procedure (Typ, Part_IP);
19924 end if;
19925 end if;
19926 end Propagate_Invariant_Attributes;
19927
19928 ---------------------------------------
19929 -- Record_Possible_Part_Of_Reference --
19930 ---------------------------------------
19931
19932 procedure Record_Possible_Part_Of_Reference
19933 (Var_Id : Entity_Id;
19934 Ref : Node_Id)
19935 is
19936 Encap : constant Entity_Id := Encapsulating_State (Var_Id);
19937 Refs : Elist_Id;
19938
19939 begin
19940 -- The variable is a constituent of a single protected/task type. Such
19941 -- a variable acts as a component of the type and must appear within a
19942 -- specific region (SPARK RM 9.3). Instead of recording the reference,
19943 -- verify its legality now.
19944
19945 if Present (Encap) and then Is_Single_Concurrent_Object (Encap) then
19946 Check_Part_Of_Reference (Var_Id, Ref);
19947
19948 -- The variable is subject to pragma Part_Of and may eventually become a
19949 -- constituent of a single protected/task type. Record the reference to
19950 -- verify its placement when the contract of the variable is analyzed.
19951
19952 elsif Present (Get_Pragma (Var_Id, Pragma_Part_Of)) then
19953 Refs := Part_Of_References (Var_Id);
19954
19955 if No (Refs) then
19956 Refs := New_Elmt_List;
19957 Set_Part_Of_References (Var_Id, Refs);
19958 end if;
19959
19960 Append_Elmt (Ref, Refs);
19961 end if;
19962 end Record_Possible_Part_Of_Reference;
19963
19964 ----------------
19965 -- Referenced --
19966 ----------------
19967
19968 function Referenced (Id : Entity_Id; Expr : Node_Id) return Boolean is
19969 Seen : Boolean := False;
19970
19971 function Is_Reference (N : Node_Id) return Traverse_Result;
19972 -- Determine whether node N denotes a reference to Id. If this is the
19973 -- case, set global flag Seen to True and stop the traversal.
19974
19975 ------------------
19976 -- Is_Reference --
19977 ------------------
19978
19979 function Is_Reference (N : Node_Id) return Traverse_Result is
19980 begin
19981 if Is_Entity_Name (N)
19982 and then Present (Entity (N))
19983 and then Entity (N) = Id
19984 then
19985 Seen := True;
19986 return Abandon;
19987 else
19988 return OK;
19989 end if;
19990 end Is_Reference;
19991
19992 procedure Inspect_Expression is new Traverse_Proc (Is_Reference);
19993
19994 -- Start of processing for Referenced
19995
19996 begin
19997 Inspect_Expression (Expr);
19998 return Seen;
19999 end Referenced;
20000
20001 ------------------------------------
20002 -- References_Generic_Formal_Type --
20003 ------------------------------------
20004
20005 function References_Generic_Formal_Type (N : Node_Id) return Boolean is
20006
20007 function Process (N : Node_Id) return Traverse_Result;
20008 -- Process one node in search for generic formal type
20009
20010 -------------
20011 -- Process --
20012 -------------
20013
20014 function Process (N : Node_Id) return Traverse_Result is
20015 begin
20016 if Nkind (N) in N_Has_Entity then
20017 declare
20018 E : constant Entity_Id := Entity (N);
20019 begin
20020 if Present (E) then
20021 if Is_Generic_Type (E) then
20022 return Abandon;
20023 elsif Present (Etype (E))
20024 and then Is_Generic_Type (Etype (E))
20025 then
20026 return Abandon;
20027 end if;
20028 end if;
20029 end;
20030 end if;
20031
20032 return Atree.OK;
20033 end Process;
20034
20035 function Traverse is new Traverse_Func (Process);
20036 -- Traverse tree to look for generic type
20037
20038 begin
20039 if Inside_A_Generic then
20040 return Traverse (N) = Abandon;
20041 else
20042 return False;
20043 end if;
20044 end References_Generic_Formal_Type;
20045
20046 --------------------
20047 -- Remove_Homonym --
20048 --------------------
20049
20050 procedure Remove_Homonym (E : Entity_Id) is
20051 Prev : Entity_Id := Empty;
20052 H : Entity_Id;
20053
20054 begin
20055 if E = Current_Entity (E) then
20056 if Present (Homonym (E)) then
20057 Set_Current_Entity (Homonym (E));
20058 else
20059 Set_Name_Entity_Id (Chars (E), Empty);
20060 end if;
20061
20062 else
20063 H := Current_Entity (E);
20064 while Present (H) and then H /= E loop
20065 Prev := H;
20066 H := Homonym (H);
20067 end loop;
20068
20069 -- If E is not on the homonym chain, nothing to do
20070
20071 if Present (H) then
20072 Set_Homonym (Prev, Homonym (E));
20073 end if;
20074 end if;
20075 end Remove_Homonym;
20076
20077 ------------------------------
20078 -- Remove_Overloaded_Entity --
20079 ------------------------------
20080
20081 procedure Remove_Overloaded_Entity (Id : Entity_Id) is
20082 procedure Remove_Primitive_Of (Typ : Entity_Id);
20083 -- Remove primitive subprogram Id from the list of primitives that
20084 -- belong to type Typ.
20085
20086 -------------------------
20087 -- Remove_Primitive_Of --
20088 -------------------------
20089
20090 procedure Remove_Primitive_Of (Typ : Entity_Id) is
20091 Prims : Elist_Id;
20092
20093 begin
20094 if Is_Tagged_Type (Typ) then
20095 Prims := Direct_Primitive_Operations (Typ);
20096
20097 if Present (Prims) then
20098 Remove (Prims, Id);
20099 end if;
20100 end if;
20101 end Remove_Primitive_Of;
20102
20103 -- Local variables
20104
20105 Scop : constant Entity_Id := Scope (Id);
20106 Formal : Entity_Id;
20107 Prev_Id : Entity_Id;
20108
20109 -- Start of processing for Remove_Overloaded_Entity
20110
20111 begin
20112 -- Remove the entity from the homonym chain. When the entity is the
20113 -- head of the chain, associate the entry in the name table with its
20114 -- homonym effectively making it the new head of the chain.
20115
20116 if Current_Entity (Id) = Id then
20117 Set_Name_Entity_Id (Chars (Id), Homonym (Id));
20118
20119 -- Otherwise link the previous and next homonyms
20120
20121 else
20122 Prev_Id := Current_Entity (Id);
20123 while Present (Prev_Id) and then Homonym (Prev_Id) /= Id loop
20124 Prev_Id := Homonym (Prev_Id);
20125 end loop;
20126
20127 Set_Homonym (Prev_Id, Homonym (Id));
20128 end if;
20129
20130 -- Remove the entity from the scope entity chain. When the entity is
20131 -- the head of the chain, set the next entity as the new head of the
20132 -- chain.
20133
20134 if First_Entity (Scop) = Id then
20135 Prev_Id := Empty;
20136 Set_First_Entity (Scop, Next_Entity (Id));
20137
20138 -- Otherwise the entity is either in the middle of the chain or it acts
20139 -- as its tail. Traverse and link the previous and next entities.
20140
20141 else
20142 Prev_Id := First_Entity (Scop);
20143 while Present (Prev_Id) and then Next_Entity (Prev_Id) /= Id loop
20144 Next_Entity (Prev_Id);
20145 end loop;
20146
20147 Set_Next_Entity (Prev_Id, Next_Entity (Id));
20148 end if;
20149
20150 -- Handle the case where the entity acts as the tail of the scope entity
20151 -- chain.
20152
20153 if Last_Entity (Scop) = Id then
20154 Set_Last_Entity (Scop, Prev_Id);
20155 end if;
20156
20157 -- The entity denotes a primitive subprogram. Remove it from the list of
20158 -- primitives of the associated controlling type.
20159
20160 if Ekind_In (Id, E_Function, E_Procedure) and then Is_Primitive (Id) then
20161 Formal := First_Formal (Id);
20162 while Present (Formal) loop
20163 if Is_Controlling_Formal (Formal) then
20164 Remove_Primitive_Of (Etype (Formal));
20165 exit;
20166 end if;
20167
20168 Next_Formal (Formal);
20169 end loop;
20170
20171 if Ekind (Id) = E_Function and then Has_Controlling_Result (Id) then
20172 Remove_Primitive_Of (Etype (Id));
20173 end if;
20174 end if;
20175 end Remove_Overloaded_Entity;
20176
20177 ---------------------
20178 -- Rep_To_Pos_Flag --
20179 ---------------------
20180
20181 function Rep_To_Pos_Flag (E : Entity_Id; Loc : Source_Ptr) return Node_Id is
20182 begin
20183 return New_Occurrence_Of
20184 (Boolean_Literals (not Range_Checks_Suppressed (E)), Loc);
20185 end Rep_To_Pos_Flag;
20186
20187 --------------------
20188 -- Require_Entity --
20189 --------------------
20190
20191 procedure Require_Entity (N : Node_Id) is
20192 begin
20193 if Is_Entity_Name (N) and then No (Entity (N)) then
20194 if Total_Errors_Detected /= 0 then
20195 Set_Entity (N, Any_Id);
20196 else
20197 raise Program_Error;
20198 end if;
20199 end if;
20200 end Require_Entity;
20201
20202 ------------------------------
20203 -- Requires_Transient_Scope --
20204 ------------------------------
20205
20206 -- A transient scope is required when variable-sized temporaries are
20207 -- allocated on the secondary stack, or when finalization actions must be
20208 -- generated before the next instruction.
20209
20210 function Requires_Transient_Scope (Id : Entity_Id) return Boolean is
20211 Old_Result : constant Boolean := Old_Requires_Transient_Scope (Id);
20212
20213 begin
20214 if Debug_Flag_QQ then
20215 return Old_Result;
20216 end if;
20217
20218 declare
20219 New_Result : constant Boolean := New_Requires_Transient_Scope (Id);
20220
20221 begin
20222 -- Assert that we're not putting things on the secondary stack if we
20223 -- didn't before; we are trying to AVOID secondary stack when
20224 -- possible.
20225
20226 if not Old_Result then
20227 pragma Assert (not New_Result);
20228 null;
20229 end if;
20230
20231 if New_Result /= Old_Result then
20232 Results_Differ (Id, Old_Result, New_Result);
20233 end if;
20234
20235 return New_Result;
20236 end;
20237 end Requires_Transient_Scope;
20238
20239 --------------------
20240 -- Results_Differ --
20241 --------------------
20242
20243 procedure Results_Differ
20244 (Id : Entity_Id;
20245 Old_Val : Boolean;
20246 New_Val : Boolean)
20247 is
20248 begin
20249 if False then -- False to disable; True for debugging
20250 Treepr.Print_Tree_Node (Id);
20251
20252 if Old_Val = New_Val then
20253 raise Program_Error;
20254 end if;
20255 end if;
20256 end Results_Differ;
20257
20258 --------------------------
20259 -- Reset_Analyzed_Flags --
20260 --------------------------
20261
20262 procedure Reset_Analyzed_Flags (N : Node_Id) is
20263 function Clear_Analyzed (N : Node_Id) return Traverse_Result;
20264 -- Function used to reset Analyzed flags in tree. Note that we do
20265 -- not reset Analyzed flags in entities, since there is no need to
20266 -- reanalyze entities, and indeed, it is wrong to do so, since it
20267 -- can result in generating auxiliary stuff more than once.
20268
20269 --------------------
20270 -- Clear_Analyzed --
20271 --------------------
20272
20273 function Clear_Analyzed (N : Node_Id) return Traverse_Result is
20274 begin
20275 if Nkind (N) not in N_Entity then
20276 Set_Analyzed (N, False);
20277 end if;
20278
20279 return OK;
20280 end Clear_Analyzed;
20281
20282 procedure Reset_Analyzed is new Traverse_Proc (Clear_Analyzed);
20283
20284 -- Start of processing for Reset_Analyzed_Flags
20285
20286 begin
20287 Reset_Analyzed (N);
20288 end Reset_Analyzed_Flags;
20289
20290 ------------------------
20291 -- Restore_SPARK_Mode --
20292 ------------------------
20293
20294 procedure Restore_SPARK_Mode
20295 (Mode : SPARK_Mode_Type;
20296 Prag : Node_Id)
20297 is
20298 begin
20299 SPARK_Mode := Mode;
20300 SPARK_Mode_Pragma := Prag;
20301 end Restore_SPARK_Mode;
20302
20303 --------------------------------
20304 -- Returns_Unconstrained_Type --
20305 --------------------------------
20306
20307 function Returns_Unconstrained_Type (Subp : Entity_Id) return Boolean is
20308 begin
20309 return Ekind (Subp) = E_Function
20310 and then not Is_Scalar_Type (Etype (Subp))
20311 and then not Is_Access_Type (Etype (Subp))
20312 and then not Is_Constrained (Etype (Subp));
20313 end Returns_Unconstrained_Type;
20314
20315 ----------------------------
20316 -- Root_Type_Of_Full_View --
20317 ----------------------------
20318
20319 function Root_Type_Of_Full_View (T : Entity_Id) return Entity_Id is
20320 Rtyp : constant Entity_Id := Root_Type (T);
20321
20322 begin
20323 -- The root type of the full view may itself be a private type. Keep
20324 -- looking for the ultimate derivation parent.
20325
20326 if Is_Private_Type (Rtyp) and then Present (Full_View (Rtyp)) then
20327 return Root_Type_Of_Full_View (Full_View (Rtyp));
20328 else
20329 return Rtyp;
20330 end if;
20331 end Root_Type_Of_Full_View;
20332
20333 ---------------------------
20334 -- Safe_To_Capture_Value --
20335 ---------------------------
20336
20337 function Safe_To_Capture_Value
20338 (N : Node_Id;
20339 Ent : Entity_Id;
20340 Cond : Boolean := False) return Boolean
20341 is
20342 begin
20343 -- The only entities for which we track constant values are variables
20344 -- which are not renamings, constants, out parameters, and in out
20345 -- parameters, so check if we have this case.
20346
20347 -- Note: it may seem odd to track constant values for constants, but in
20348 -- fact this routine is used for other purposes than simply capturing
20349 -- the value. In particular, the setting of Known[_Non]_Null.
20350
20351 if (Ekind (Ent) = E_Variable and then No (Renamed_Object (Ent)))
20352 or else
20353 Ekind_In (Ent, E_Constant, E_Out_Parameter, E_In_Out_Parameter)
20354 then
20355 null;
20356
20357 -- For conditionals, we also allow loop parameters and all formals,
20358 -- including in parameters.
20359
20360 elsif Cond and then Ekind_In (Ent, E_Loop_Parameter, E_In_Parameter) then
20361 null;
20362
20363 -- For all other cases, not just unsafe, but impossible to capture
20364 -- Current_Value, since the above are the only entities which have
20365 -- Current_Value fields.
20366
20367 else
20368 return False;
20369 end if;
20370
20371 -- Skip if volatile or aliased, since funny things might be going on in
20372 -- these cases which we cannot necessarily track. Also skip any variable
20373 -- for which an address clause is given, or whose address is taken. Also
20374 -- never capture value of library level variables (an attempt to do so
20375 -- can occur in the case of package elaboration code).
20376
20377 if Treat_As_Volatile (Ent)
20378 or else Is_Aliased (Ent)
20379 or else Present (Address_Clause (Ent))
20380 or else Address_Taken (Ent)
20381 or else (Is_Library_Level_Entity (Ent)
20382 and then Ekind (Ent) = E_Variable)
20383 then
20384 return False;
20385 end if;
20386
20387 -- OK, all above conditions are met. We also require that the scope of
20388 -- the reference be the same as the scope of the entity, not counting
20389 -- packages and blocks and loops.
20390
20391 declare
20392 E_Scope : constant Entity_Id := Scope (Ent);
20393 R_Scope : Entity_Id;
20394
20395 begin
20396 R_Scope := Current_Scope;
20397 while R_Scope /= Standard_Standard loop
20398 exit when R_Scope = E_Scope;
20399
20400 if not Ekind_In (R_Scope, E_Package, E_Block, E_Loop) then
20401 return False;
20402 else
20403 R_Scope := Scope (R_Scope);
20404 end if;
20405 end loop;
20406 end;
20407
20408 -- We also require that the reference does not appear in a context
20409 -- where it is not sure to be executed (i.e. a conditional context
20410 -- or an exception handler). We skip this if Cond is True, since the
20411 -- capturing of values from conditional tests handles this ok.
20412
20413 if Cond then
20414 return True;
20415 end if;
20416
20417 declare
20418 Desc : Node_Id;
20419 P : Node_Id;
20420
20421 begin
20422 Desc := N;
20423
20424 -- Seems dubious that case expressions are not handled here ???
20425
20426 P := Parent (N);
20427 while Present (P) loop
20428 if Nkind (P) = N_If_Statement
20429 or else Nkind (P) = N_Case_Statement
20430 or else (Nkind (P) in N_Short_Circuit
20431 and then Desc = Right_Opnd (P))
20432 or else (Nkind (P) = N_If_Expression
20433 and then Desc /= First (Expressions (P)))
20434 or else Nkind (P) = N_Exception_Handler
20435 or else Nkind (P) = N_Selective_Accept
20436 or else Nkind (P) = N_Conditional_Entry_Call
20437 or else Nkind (P) = N_Timed_Entry_Call
20438 or else Nkind (P) = N_Asynchronous_Select
20439 then
20440 return False;
20441
20442 else
20443 Desc := P;
20444 P := Parent (P);
20445
20446 -- A special Ada 2012 case: the original node may be part
20447 -- of the else_actions of a conditional expression, in which
20448 -- case it might not have been expanded yet, and appears in
20449 -- a non-syntactic list of actions. In that case it is clearly
20450 -- not safe to save a value.
20451
20452 if No (P)
20453 and then Is_List_Member (Desc)
20454 and then No (Parent (List_Containing (Desc)))
20455 then
20456 return False;
20457 end if;
20458 end if;
20459 end loop;
20460 end;
20461
20462 -- OK, looks safe to set value
20463
20464 return True;
20465 end Safe_To_Capture_Value;
20466
20467 ---------------
20468 -- Same_Name --
20469 ---------------
20470
20471 function Same_Name (N1, N2 : Node_Id) return Boolean is
20472 K1 : constant Node_Kind := Nkind (N1);
20473 K2 : constant Node_Kind := Nkind (N2);
20474
20475 begin
20476 if (K1 = N_Identifier or else K1 = N_Defining_Identifier)
20477 and then (K2 = N_Identifier or else K2 = N_Defining_Identifier)
20478 then
20479 return Chars (N1) = Chars (N2);
20480
20481 elsif (K1 = N_Selected_Component or else K1 = N_Expanded_Name)
20482 and then (K2 = N_Selected_Component or else K2 = N_Expanded_Name)
20483 then
20484 return Same_Name (Selector_Name (N1), Selector_Name (N2))
20485 and then Same_Name (Prefix (N1), Prefix (N2));
20486
20487 else
20488 return False;
20489 end if;
20490 end Same_Name;
20491
20492 -----------------
20493 -- Same_Object --
20494 -----------------
20495
20496 function Same_Object (Node1, Node2 : Node_Id) return Boolean is
20497 N1 : constant Node_Id := Original_Node (Node1);
20498 N2 : constant Node_Id := Original_Node (Node2);
20499 -- We do the tests on original nodes, since we are most interested
20500 -- in the original source, not any expansion that got in the way.
20501
20502 K1 : constant Node_Kind := Nkind (N1);
20503 K2 : constant Node_Kind := Nkind (N2);
20504
20505 begin
20506 -- First case, both are entities with same entity
20507
20508 if K1 in N_Has_Entity and then K2 in N_Has_Entity then
20509 declare
20510 EN1 : constant Entity_Id := Entity (N1);
20511 EN2 : constant Entity_Id := Entity (N2);
20512 begin
20513 if Present (EN1) and then Present (EN2)
20514 and then (Ekind_In (EN1, E_Variable, E_Constant)
20515 or else Is_Formal (EN1))
20516 and then EN1 = EN2
20517 then
20518 return True;
20519 end if;
20520 end;
20521 end if;
20522
20523 -- Second case, selected component with same selector, same record
20524
20525 if K1 = N_Selected_Component
20526 and then K2 = N_Selected_Component
20527 and then Chars (Selector_Name (N1)) = Chars (Selector_Name (N2))
20528 then
20529 return Same_Object (Prefix (N1), Prefix (N2));
20530
20531 -- Third case, indexed component with same subscripts, same array
20532
20533 elsif K1 = N_Indexed_Component
20534 and then K2 = N_Indexed_Component
20535 and then Same_Object (Prefix (N1), Prefix (N2))
20536 then
20537 declare
20538 E1, E2 : Node_Id;
20539 begin
20540 E1 := First (Expressions (N1));
20541 E2 := First (Expressions (N2));
20542 while Present (E1) loop
20543 if not Same_Value (E1, E2) then
20544 return False;
20545 else
20546 Next (E1);
20547 Next (E2);
20548 end if;
20549 end loop;
20550
20551 return True;
20552 end;
20553
20554 -- Fourth case, slice of same array with same bounds
20555
20556 elsif K1 = N_Slice
20557 and then K2 = N_Slice
20558 and then Nkind (Discrete_Range (N1)) = N_Range
20559 and then Nkind (Discrete_Range (N2)) = N_Range
20560 and then Same_Value (Low_Bound (Discrete_Range (N1)),
20561 Low_Bound (Discrete_Range (N2)))
20562 and then Same_Value (High_Bound (Discrete_Range (N1)),
20563 High_Bound (Discrete_Range (N2)))
20564 then
20565 return Same_Name (Prefix (N1), Prefix (N2));
20566
20567 -- All other cases, not clearly the same object
20568
20569 else
20570 return False;
20571 end if;
20572 end Same_Object;
20573
20574 ---------------
20575 -- Same_Type --
20576 ---------------
20577
20578 function Same_Type (T1, T2 : Entity_Id) return Boolean is
20579 begin
20580 if T1 = T2 then
20581 return True;
20582
20583 elsif not Is_Constrained (T1)
20584 and then not Is_Constrained (T2)
20585 and then Base_Type (T1) = Base_Type (T2)
20586 then
20587 return True;
20588
20589 -- For now don't bother with case of identical constraints, to be
20590 -- fiddled with later on perhaps (this is only used for optimization
20591 -- purposes, so it is not critical to do a best possible job)
20592
20593 else
20594 return False;
20595 end if;
20596 end Same_Type;
20597
20598 ----------------
20599 -- Same_Value --
20600 ----------------
20601
20602 function Same_Value (Node1, Node2 : Node_Id) return Boolean is
20603 begin
20604 if Compile_Time_Known_Value (Node1)
20605 and then Compile_Time_Known_Value (Node2)
20606 then
20607 -- Handle properly compile-time expressions that are not
20608 -- scalar.
20609
20610 if Is_String_Type (Etype (Node1)) then
20611 return Expr_Value_S (Node1) = Expr_Value_S (Node2);
20612
20613 else
20614 return Expr_Value (Node1) = Expr_Value (Node2);
20615 end if;
20616
20617 elsif Same_Object (Node1, Node2) then
20618 return True;
20619 else
20620 return False;
20621 end if;
20622 end Same_Value;
20623
20624 --------------------
20625 -- Set_SPARK_Mode --
20626 --------------------
20627
20628 procedure Set_SPARK_Mode (Context : Entity_Id) is
20629 begin
20630 -- Do not consider illegal or partially decorated constructs
20631
20632 if Ekind (Context) = E_Void or else Error_Posted (Context) then
20633 null;
20634
20635 elsif Present (SPARK_Pragma (Context)) then
20636 Install_SPARK_Mode
20637 (Mode => Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Context)),
20638 Prag => SPARK_Pragma (Context));
20639 end if;
20640 end Set_SPARK_Mode;
20641
20642 -------------------------
20643 -- Scalar_Part_Present --
20644 -------------------------
20645
20646 function Scalar_Part_Present (T : Entity_Id) return Boolean is
20647 C : Entity_Id;
20648
20649 begin
20650 if Is_Scalar_Type (T) then
20651 return True;
20652
20653 elsif Is_Array_Type (T) then
20654 return Scalar_Part_Present (Component_Type (T));
20655
20656 elsif Is_Record_Type (T) or else Has_Discriminants (T) then
20657 C := First_Component_Or_Discriminant (T);
20658 while Present (C) loop
20659 if Scalar_Part_Present (Etype (C)) then
20660 return True;
20661 else
20662 Next_Component_Or_Discriminant (C);
20663 end if;
20664 end loop;
20665 end if;
20666
20667 return False;
20668 end Scalar_Part_Present;
20669
20670 ------------------------
20671 -- Scope_Is_Transient --
20672 ------------------------
20673
20674 function Scope_Is_Transient return Boolean is
20675 begin
20676 return Scope_Stack.Table (Scope_Stack.Last).Is_Transient;
20677 end Scope_Is_Transient;
20678
20679 ------------------
20680 -- Scope_Within --
20681 ------------------
20682
20683 function Scope_Within (Scope1, Scope2 : Entity_Id) return Boolean is
20684 Scop : Entity_Id;
20685
20686 begin
20687 Scop := Scope1;
20688 while Scop /= Standard_Standard loop
20689 Scop := Scope (Scop);
20690
20691 if Scop = Scope2 then
20692 return True;
20693 end if;
20694 end loop;
20695
20696 return False;
20697 end Scope_Within;
20698
20699 --------------------------
20700 -- Scope_Within_Or_Same --
20701 --------------------------
20702
20703 function Scope_Within_Or_Same (Scope1, Scope2 : Entity_Id) return Boolean is
20704 Scop : Entity_Id;
20705
20706 begin
20707 Scop := Scope1;
20708 while Scop /= Standard_Standard loop
20709 if Scop = Scope2 then
20710 return True;
20711 else
20712 Scop := Scope (Scop);
20713 end if;
20714 end loop;
20715
20716 return False;
20717 end Scope_Within_Or_Same;
20718
20719 --------------------
20720 -- Set_Convention --
20721 --------------------
20722
20723 procedure Set_Convention (E : Entity_Id; Val : Snames.Convention_Id) is
20724 begin
20725 Basic_Set_Convention (E, Val);
20726
20727 if Is_Type (E)
20728 and then Is_Access_Subprogram_Type (Base_Type (E))
20729 and then Has_Foreign_Convention (E)
20730 then
20731
20732 -- A pragma Convention in an instance may apply to the subtype
20733 -- created for a formal, in which case we have already verified
20734 -- that conventions of actual and formal match and there is nothing
20735 -- to flag on the subtype.
20736
20737 if In_Instance then
20738 null;
20739 else
20740 Set_Can_Use_Internal_Rep (E, False);
20741 end if;
20742 end if;
20743
20744 -- If E is an object or component, and the type of E is an anonymous
20745 -- access type with no convention set, then also set the convention of
20746 -- the anonymous access type. We do not do this for anonymous protected
20747 -- types, since protected types always have the default convention.
20748
20749 if Present (Etype (E))
20750 and then (Is_Object (E)
20751 or else Ekind (E) = E_Component
20752
20753 -- Allow E_Void (happens for pragma Convention appearing
20754 -- in the middle of a record applying to a component)
20755
20756 or else Ekind (E) = E_Void)
20757 then
20758 declare
20759 Typ : constant Entity_Id := Etype (E);
20760
20761 begin
20762 if Ekind_In (Typ, E_Anonymous_Access_Type,
20763 E_Anonymous_Access_Subprogram_Type)
20764 and then not Has_Convention_Pragma (Typ)
20765 then
20766 Basic_Set_Convention (Typ, Val);
20767 Set_Has_Convention_Pragma (Typ);
20768
20769 -- And for the access subprogram type, deal similarly with the
20770 -- designated E_Subprogram_Type if it is also internal (which
20771 -- it always is?)
20772
20773 if Ekind (Typ) = E_Anonymous_Access_Subprogram_Type then
20774 declare
20775 Dtype : constant Entity_Id := Designated_Type (Typ);
20776 begin
20777 if Ekind (Dtype) = E_Subprogram_Type
20778 and then Is_Itype (Dtype)
20779 and then not Has_Convention_Pragma (Dtype)
20780 then
20781 Basic_Set_Convention (Dtype, Val);
20782 Set_Has_Convention_Pragma (Dtype);
20783 end if;
20784 end;
20785 end if;
20786 end if;
20787 end;
20788 end if;
20789 end Set_Convention;
20790
20791 ------------------------
20792 -- Set_Current_Entity --
20793 ------------------------
20794
20795 -- The given entity is to be set as the currently visible definition of its
20796 -- associated name (i.e. the Node_Id associated with its name). All we have
20797 -- to do is to get the name from the identifier, and then set the
20798 -- associated Node_Id to point to the given entity.
20799
20800 procedure Set_Current_Entity (E : Entity_Id) is
20801 begin
20802 Set_Name_Entity_Id (Chars (E), E);
20803 end Set_Current_Entity;
20804
20805 ---------------------------
20806 -- Set_Debug_Info_Needed --
20807 ---------------------------
20808
20809 procedure Set_Debug_Info_Needed (T : Entity_Id) is
20810
20811 procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id);
20812 pragma Inline (Set_Debug_Info_Needed_If_Not_Set);
20813 -- Used to set debug info in a related node if not set already
20814
20815 --------------------------------------
20816 -- Set_Debug_Info_Needed_If_Not_Set --
20817 --------------------------------------
20818
20819 procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id) is
20820 begin
20821 if Present (E) and then not Needs_Debug_Info (E) then
20822 Set_Debug_Info_Needed (E);
20823
20824 -- For a private type, indicate that the full view also needs
20825 -- debug information.
20826
20827 if Is_Type (E)
20828 and then Is_Private_Type (E)
20829 and then Present (Full_View (E))
20830 then
20831 Set_Debug_Info_Needed (Full_View (E));
20832 end if;
20833 end if;
20834 end Set_Debug_Info_Needed_If_Not_Set;
20835
20836 -- Start of processing for Set_Debug_Info_Needed
20837
20838 begin
20839 -- Nothing to do if argument is Empty or has Debug_Info_Off set, which
20840 -- indicates that Debug_Info_Needed is never required for the entity.
20841 -- Nothing to do if entity comes from a predefined file. Library files
20842 -- are compiled without debug information, but inlined bodies of these
20843 -- routines may appear in user code, and debug information on them ends
20844 -- up complicating debugging the user code.
20845
20846 if No (T)
20847 or else Debug_Info_Off (T)
20848 then
20849 return;
20850
20851 elsif In_Inlined_Body
20852 and then Is_Predefined_File_Name
20853 (Unit_File_Name (Get_Source_Unit (Sloc (T))))
20854 then
20855 Set_Needs_Debug_Info (T, False);
20856 end if;
20857
20858 -- Set flag in entity itself. Note that we will go through the following
20859 -- circuitry even if the flag is already set on T. That's intentional,
20860 -- it makes sure that the flag will be set in subsidiary entities.
20861
20862 Set_Needs_Debug_Info (T);
20863
20864 -- Set flag on subsidiary entities if not set already
20865
20866 if Is_Object (T) then
20867 Set_Debug_Info_Needed_If_Not_Set (Etype (T));
20868
20869 elsif Is_Type (T) then
20870 Set_Debug_Info_Needed_If_Not_Set (Etype (T));
20871
20872 if Is_Record_Type (T) then
20873 declare
20874 Ent : Entity_Id := First_Entity (T);
20875 begin
20876 while Present (Ent) loop
20877 Set_Debug_Info_Needed_If_Not_Set (Ent);
20878 Next_Entity (Ent);
20879 end loop;
20880 end;
20881
20882 -- For a class wide subtype, we also need debug information
20883 -- for the equivalent type.
20884
20885 if Ekind (T) = E_Class_Wide_Subtype then
20886 Set_Debug_Info_Needed_If_Not_Set (Equivalent_Type (T));
20887 end if;
20888
20889 elsif Is_Array_Type (T) then
20890 Set_Debug_Info_Needed_If_Not_Set (Component_Type (T));
20891
20892 declare
20893 Indx : Node_Id := First_Index (T);
20894 begin
20895 while Present (Indx) loop
20896 Set_Debug_Info_Needed_If_Not_Set (Etype (Indx));
20897 Indx := Next_Index (Indx);
20898 end loop;
20899 end;
20900
20901 -- For a packed array type, we also need debug information for
20902 -- the type used to represent the packed array. Conversely, we
20903 -- also need it for the former if we need it for the latter.
20904
20905 if Is_Packed (T) then
20906 Set_Debug_Info_Needed_If_Not_Set (Packed_Array_Impl_Type (T));
20907 end if;
20908
20909 if Is_Packed_Array_Impl_Type (T) then
20910 Set_Debug_Info_Needed_If_Not_Set (Original_Array_Type (T));
20911 end if;
20912
20913 elsif Is_Access_Type (T) then
20914 Set_Debug_Info_Needed_If_Not_Set (Directly_Designated_Type (T));
20915
20916 elsif Is_Private_Type (T) then
20917 declare
20918 FV : constant Entity_Id := Full_View (T);
20919
20920 begin
20921 Set_Debug_Info_Needed_If_Not_Set (FV);
20922
20923 -- If the full view is itself a derived private type, we need
20924 -- debug information on its underlying type.
20925
20926 if Present (FV)
20927 and then Is_Private_Type (FV)
20928 and then Present (Underlying_Full_View (FV))
20929 then
20930 Set_Needs_Debug_Info (Underlying_Full_View (FV));
20931 end if;
20932 end;
20933
20934 elsif Is_Protected_Type (T) then
20935 Set_Debug_Info_Needed_If_Not_Set (Corresponding_Record_Type (T));
20936
20937 elsif Is_Scalar_Type (T) then
20938
20939 -- If the subrange bounds are materialized by dedicated constant
20940 -- objects, also include them in the debug info to make sure the
20941 -- debugger can properly use them.
20942
20943 if Present (Scalar_Range (T))
20944 and then Nkind (Scalar_Range (T)) = N_Range
20945 then
20946 declare
20947 Low_Bnd : constant Node_Id := Type_Low_Bound (T);
20948 High_Bnd : constant Node_Id := Type_High_Bound (T);
20949
20950 begin
20951 if Is_Entity_Name (Low_Bnd) then
20952 Set_Debug_Info_Needed_If_Not_Set (Entity (Low_Bnd));
20953 end if;
20954
20955 if Is_Entity_Name (High_Bnd) then
20956 Set_Debug_Info_Needed_If_Not_Set (Entity (High_Bnd));
20957 end if;
20958 end;
20959 end if;
20960 end if;
20961 end if;
20962 end Set_Debug_Info_Needed;
20963
20964 ----------------------------
20965 -- Set_Entity_With_Checks --
20966 ----------------------------
20967
20968 procedure Set_Entity_With_Checks (N : Node_Id; Val : Entity_Id) is
20969 Val_Actual : Entity_Id;
20970 Nod : Node_Id;
20971 Post_Node : Node_Id;
20972
20973 begin
20974 -- Unconditionally set the entity
20975
20976 Set_Entity (N, Val);
20977
20978 -- The node to post on is the selector in the case of an expanded name,
20979 -- and otherwise the node itself.
20980
20981 if Nkind (N) = N_Expanded_Name then
20982 Post_Node := Selector_Name (N);
20983 else
20984 Post_Node := N;
20985 end if;
20986
20987 -- Check for violation of No_Fixed_IO
20988
20989 if Restriction_Check_Required (No_Fixed_IO)
20990 and then
20991 ((RTU_Loaded (Ada_Text_IO)
20992 and then (Is_RTE (Val, RE_Decimal_IO)
20993 or else
20994 Is_RTE (Val, RE_Fixed_IO)))
20995
20996 or else
20997 (RTU_Loaded (Ada_Wide_Text_IO)
20998 and then (Is_RTE (Val, RO_WT_Decimal_IO)
20999 or else
21000 Is_RTE (Val, RO_WT_Fixed_IO)))
21001
21002 or else
21003 (RTU_Loaded (Ada_Wide_Wide_Text_IO)
21004 and then (Is_RTE (Val, RO_WW_Decimal_IO)
21005 or else
21006 Is_RTE (Val, RO_WW_Fixed_IO))))
21007
21008 -- A special extra check, don't complain about a reference from within
21009 -- the Ada.Interrupts package itself!
21010
21011 and then not In_Same_Extended_Unit (N, Val)
21012 then
21013 Check_Restriction (No_Fixed_IO, Post_Node);
21014 end if;
21015
21016 -- Remaining checks are only done on source nodes. Note that we test
21017 -- for violation of No_Fixed_IO even on non-source nodes, because the
21018 -- cases for checking violations of this restriction are instantiations
21019 -- where the reference in the instance has Comes_From_Source False.
21020
21021 if not Comes_From_Source (N) then
21022 return;
21023 end if;
21024
21025 -- Check for violation of No_Abort_Statements, which is triggered by
21026 -- call to Ada.Task_Identification.Abort_Task.
21027
21028 if Restriction_Check_Required (No_Abort_Statements)
21029 and then (Is_RTE (Val, RE_Abort_Task))
21030
21031 -- A special extra check, don't complain about a reference from within
21032 -- the Ada.Task_Identification package itself!
21033
21034 and then not In_Same_Extended_Unit (N, Val)
21035 then
21036 Check_Restriction (No_Abort_Statements, Post_Node);
21037 end if;
21038
21039 if Val = Standard_Long_Long_Integer then
21040 Check_Restriction (No_Long_Long_Integers, Post_Node);
21041 end if;
21042
21043 -- Check for violation of No_Dynamic_Attachment
21044
21045 if Restriction_Check_Required (No_Dynamic_Attachment)
21046 and then RTU_Loaded (Ada_Interrupts)
21047 and then (Is_RTE (Val, RE_Is_Reserved) or else
21048 Is_RTE (Val, RE_Is_Attached) or else
21049 Is_RTE (Val, RE_Current_Handler) or else
21050 Is_RTE (Val, RE_Attach_Handler) or else
21051 Is_RTE (Val, RE_Exchange_Handler) or else
21052 Is_RTE (Val, RE_Detach_Handler) or else
21053 Is_RTE (Val, RE_Reference))
21054
21055 -- A special extra check, don't complain about a reference from within
21056 -- the Ada.Interrupts package itself!
21057
21058 and then not In_Same_Extended_Unit (N, Val)
21059 then
21060 Check_Restriction (No_Dynamic_Attachment, Post_Node);
21061 end if;
21062
21063 -- Check for No_Implementation_Identifiers
21064
21065 if Restriction_Check_Required (No_Implementation_Identifiers) then
21066
21067 -- We have an implementation defined entity if it is marked as
21068 -- implementation defined, or is defined in a package marked as
21069 -- implementation defined. However, library packages themselves
21070 -- are excluded (we don't want to flag Interfaces itself, just
21071 -- the entities within it).
21072
21073 if (Is_Implementation_Defined (Val)
21074 or else
21075 (Present (Scope (Val))
21076 and then Is_Implementation_Defined (Scope (Val))))
21077 and then not (Ekind_In (Val, E_Package, E_Generic_Package)
21078 and then Is_Library_Level_Entity (Val))
21079 then
21080 Check_Restriction (No_Implementation_Identifiers, Post_Node);
21081 end if;
21082 end if;
21083
21084 -- Do the style check
21085
21086 if Style_Check
21087 and then not Suppress_Style_Checks (Val)
21088 and then not In_Instance
21089 then
21090 if Nkind (N) = N_Identifier then
21091 Nod := N;
21092 elsif Nkind (N) = N_Expanded_Name then
21093 Nod := Selector_Name (N);
21094 else
21095 return;
21096 end if;
21097
21098 -- A special situation arises for derived operations, where we want
21099 -- to do the check against the parent (since the Sloc of the derived
21100 -- operation points to the derived type declaration itself).
21101
21102 Val_Actual := Val;
21103 while not Comes_From_Source (Val_Actual)
21104 and then Nkind (Val_Actual) in N_Entity
21105 and then (Ekind (Val_Actual) = E_Enumeration_Literal
21106 or else Is_Subprogram_Or_Generic_Subprogram (Val_Actual))
21107 and then Present (Alias (Val_Actual))
21108 loop
21109 Val_Actual := Alias (Val_Actual);
21110 end loop;
21111
21112 -- Renaming declarations for generic actuals do not come from source,
21113 -- and have a different name from that of the entity they rename, so
21114 -- there is no style check to perform here.
21115
21116 if Chars (Nod) = Chars (Val_Actual) then
21117 Style.Check_Identifier (Nod, Val_Actual);
21118 end if;
21119 end if;
21120
21121 Set_Entity (N, Val);
21122 end Set_Entity_With_Checks;
21123
21124 ------------------------
21125 -- Set_Name_Entity_Id --
21126 ------------------------
21127
21128 procedure Set_Name_Entity_Id (Id : Name_Id; Val : Entity_Id) is
21129 begin
21130 Set_Name_Table_Int (Id, Int (Val));
21131 end Set_Name_Entity_Id;
21132
21133 ---------------------
21134 -- Set_Next_Actual --
21135 ---------------------
21136
21137 procedure Set_Next_Actual (Ass1_Id : Node_Id; Ass2_Id : Node_Id) is
21138 begin
21139 if Nkind (Parent (Ass1_Id)) = N_Parameter_Association then
21140 Set_First_Named_Actual (Parent (Ass1_Id), Ass2_Id);
21141 end if;
21142 end Set_Next_Actual;
21143
21144 ----------------------------------
21145 -- Set_Optimize_Alignment_Flags --
21146 ----------------------------------
21147
21148 procedure Set_Optimize_Alignment_Flags (E : Entity_Id) is
21149 begin
21150 if Optimize_Alignment = 'S' then
21151 Set_Optimize_Alignment_Space (E);
21152 elsif Optimize_Alignment = 'T' then
21153 Set_Optimize_Alignment_Time (E);
21154 end if;
21155 end Set_Optimize_Alignment_Flags;
21156
21157 -----------------------
21158 -- Set_Public_Status --
21159 -----------------------
21160
21161 procedure Set_Public_Status (Id : Entity_Id) is
21162 S : constant Entity_Id := Current_Scope;
21163
21164 function Within_HSS_Or_If (E : Entity_Id) return Boolean;
21165 -- Determines if E is defined within handled statement sequence or
21166 -- an if statement, returns True if so, False otherwise.
21167
21168 ----------------------
21169 -- Within_HSS_Or_If --
21170 ----------------------
21171
21172 function Within_HSS_Or_If (E : Entity_Id) return Boolean is
21173 N : Node_Id;
21174 begin
21175 N := Declaration_Node (E);
21176 loop
21177 N := Parent (N);
21178
21179 if No (N) then
21180 return False;
21181
21182 elsif Nkind_In (N, N_Handled_Sequence_Of_Statements,
21183 N_If_Statement)
21184 then
21185 return True;
21186 end if;
21187 end loop;
21188 end Within_HSS_Or_If;
21189
21190 -- Start of processing for Set_Public_Status
21191
21192 begin
21193 -- Everything in the scope of Standard is public
21194
21195 if S = Standard_Standard then
21196 Set_Is_Public (Id);
21197
21198 -- Entity is definitely not public if enclosing scope is not public
21199
21200 elsif not Is_Public (S) then
21201 return;
21202
21203 -- An object or function declaration that occurs in a handled sequence
21204 -- of statements or within an if statement is the declaration for a
21205 -- temporary object or local subprogram generated by the expander. It
21206 -- never needs to be made public and furthermore, making it public can
21207 -- cause back end problems.
21208
21209 elsif Nkind_In (Parent (Id), N_Object_Declaration,
21210 N_Function_Specification)
21211 and then Within_HSS_Or_If (Id)
21212 then
21213 return;
21214
21215 -- Entities in public packages or records are public
21216
21217 elsif Ekind (S) = E_Package or Is_Record_Type (S) then
21218 Set_Is_Public (Id);
21219
21220 -- The bounds of an entry family declaration can generate object
21221 -- declarations that are visible to the back-end, e.g. in the
21222 -- the declaration of a composite type that contains tasks.
21223
21224 elsif Is_Concurrent_Type (S)
21225 and then not Has_Completion (S)
21226 and then Nkind (Parent (Id)) = N_Object_Declaration
21227 then
21228 Set_Is_Public (Id);
21229 end if;
21230 end Set_Public_Status;
21231
21232 -----------------------------
21233 -- Set_Referenced_Modified --
21234 -----------------------------
21235
21236 procedure Set_Referenced_Modified (N : Node_Id; Out_Param : Boolean) is
21237 Pref : Node_Id;
21238
21239 begin
21240 -- Deal with indexed or selected component where prefix is modified
21241
21242 if Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
21243 Pref := Prefix (N);
21244
21245 -- If prefix is access type, then it is the designated object that is
21246 -- being modified, which means we have no entity to set the flag on.
21247
21248 if No (Etype (Pref)) or else Is_Access_Type (Etype (Pref)) then
21249 return;
21250
21251 -- Otherwise chase the prefix
21252
21253 else
21254 Set_Referenced_Modified (Pref, Out_Param);
21255 end if;
21256
21257 -- Otherwise see if we have an entity name (only other case to process)
21258
21259 elsif Is_Entity_Name (N) and then Present (Entity (N)) then
21260 Set_Referenced_As_LHS (Entity (N), not Out_Param);
21261 Set_Referenced_As_Out_Parameter (Entity (N), Out_Param);
21262 end if;
21263 end Set_Referenced_Modified;
21264
21265 ----------------------------
21266 -- Set_Scope_Is_Transient --
21267 ----------------------------
21268
21269 procedure Set_Scope_Is_Transient (V : Boolean := True) is
21270 begin
21271 Scope_Stack.Table (Scope_Stack.Last).Is_Transient := V;
21272 end Set_Scope_Is_Transient;
21273
21274 -------------------
21275 -- Set_Size_Info --
21276 -------------------
21277
21278 procedure Set_Size_Info (T1, T2 : Entity_Id) is
21279 begin
21280 -- We copy Esize, but not RM_Size, since in general RM_Size is
21281 -- subtype specific and does not get inherited by all subtypes.
21282
21283 Set_Esize (T1, Esize (T2));
21284 Set_Has_Biased_Representation (T1, Has_Biased_Representation (T2));
21285
21286 if Is_Discrete_Or_Fixed_Point_Type (T1)
21287 and then
21288 Is_Discrete_Or_Fixed_Point_Type (T2)
21289 then
21290 Set_Is_Unsigned_Type (T1, Is_Unsigned_Type (T2));
21291 end if;
21292
21293 Set_Alignment (T1, Alignment (T2));
21294 end Set_Size_Info;
21295
21296 ------------------------------
21297 -- Should_Ignore_Pragma_Par --
21298 ------------------------------
21299
21300 function Should_Ignore_Pragma_Par (Prag_Name : Name_Id) return Boolean is
21301 pragma Assert (Compiler_State = Parsing);
21302 -- This one can't work during semantic analysis, because we don't have a
21303 -- correct Current_Source_File.
21304
21305 Result : constant Boolean :=
21306 Get_Name_Table_Boolean3 (Prag_Name)
21307 and then not Is_Internal_File_Name
21308 (File_Name (Current_Source_File));
21309 begin
21310 return Result;
21311 end Should_Ignore_Pragma_Par;
21312
21313 ------------------------------
21314 -- Should_Ignore_Pragma_Sem --
21315 ------------------------------
21316
21317 function Should_Ignore_Pragma_Sem (N : Node_Id) return Boolean is
21318 pragma Assert (Compiler_State = Analyzing);
21319 Prag_Name : constant Name_Id := Pragma_Name (N);
21320 Result : constant Boolean :=
21321 Get_Name_Table_Boolean3 (Prag_Name)
21322 and then not In_Internal_Unit (N);
21323
21324 begin
21325 return Result;
21326 end Should_Ignore_Pragma_Sem;
21327
21328 --------------------
21329 -- Static_Boolean --
21330 --------------------
21331
21332 function Static_Boolean (N : Node_Id) return Uint is
21333 begin
21334 Analyze_And_Resolve (N, Standard_Boolean);
21335
21336 if N = Error
21337 or else Error_Posted (N)
21338 or else Etype (N) = Any_Type
21339 then
21340 return No_Uint;
21341 end if;
21342
21343 if Is_OK_Static_Expression (N) then
21344 if not Raises_Constraint_Error (N) then
21345 return Expr_Value (N);
21346 else
21347 return No_Uint;
21348 end if;
21349
21350 elsif Etype (N) = Any_Type then
21351 return No_Uint;
21352
21353 else
21354 Flag_Non_Static_Expr
21355 ("static boolean expression required here", N);
21356 return No_Uint;
21357 end if;
21358 end Static_Boolean;
21359
21360 --------------------
21361 -- Static_Integer --
21362 --------------------
21363
21364 function Static_Integer (N : Node_Id) return Uint is
21365 begin
21366 Analyze_And_Resolve (N, Any_Integer);
21367
21368 if N = Error
21369 or else Error_Posted (N)
21370 or else Etype (N) = Any_Type
21371 then
21372 return No_Uint;
21373 end if;
21374
21375 if Is_OK_Static_Expression (N) then
21376 if not Raises_Constraint_Error (N) then
21377 return Expr_Value (N);
21378 else
21379 return No_Uint;
21380 end if;
21381
21382 elsif Etype (N) = Any_Type then
21383 return No_Uint;
21384
21385 else
21386 Flag_Non_Static_Expr
21387 ("static integer expression required here", N);
21388 return No_Uint;
21389 end if;
21390 end Static_Integer;
21391
21392 --------------------------
21393 -- Statically_Different --
21394 --------------------------
21395
21396 function Statically_Different (E1, E2 : Node_Id) return Boolean is
21397 R1 : constant Node_Id := Get_Referenced_Object (E1);
21398 R2 : constant Node_Id := Get_Referenced_Object (E2);
21399 begin
21400 return Is_Entity_Name (R1)
21401 and then Is_Entity_Name (R2)
21402 and then Entity (R1) /= Entity (R2)
21403 and then not Is_Formal (Entity (R1))
21404 and then not Is_Formal (Entity (R2));
21405 end Statically_Different;
21406
21407 --------------------------------------
21408 -- Subject_To_Loop_Entry_Attributes --
21409 --------------------------------------
21410
21411 function Subject_To_Loop_Entry_Attributes (N : Node_Id) return Boolean is
21412 Stmt : Node_Id;
21413
21414 begin
21415 Stmt := N;
21416
21417 -- The expansion mechanism transform a loop subject to at least one
21418 -- 'Loop_Entry attribute into a conditional block. Infinite loops lack
21419 -- the conditional part.
21420
21421 if Nkind_In (Stmt, N_Block_Statement, N_If_Statement)
21422 and then Nkind (Original_Node (N)) = N_Loop_Statement
21423 then
21424 Stmt := Original_Node (N);
21425 end if;
21426
21427 return
21428 Nkind (Stmt) = N_Loop_Statement
21429 and then Present (Identifier (Stmt))
21430 and then Present (Entity (Identifier (Stmt)))
21431 and then Has_Loop_Entry_Attributes (Entity (Identifier (Stmt)));
21432 end Subject_To_Loop_Entry_Attributes;
21433
21434 -----------------------------
21435 -- Subprogram_Access_Level --
21436 -----------------------------
21437
21438 function Subprogram_Access_Level (Subp : Entity_Id) return Uint is
21439 begin
21440 if Present (Alias (Subp)) then
21441 return Subprogram_Access_Level (Alias (Subp));
21442 else
21443 return Scope_Depth (Enclosing_Dynamic_Scope (Subp));
21444 end if;
21445 end Subprogram_Access_Level;
21446
21447 -------------------------------
21448 -- Support_Atomic_Primitives --
21449 -------------------------------
21450
21451 function Support_Atomic_Primitives (Typ : Entity_Id) return Boolean is
21452 Size : Int;
21453
21454 begin
21455 -- Verify the alignment of Typ is known
21456
21457 if not Known_Alignment (Typ) then
21458 return False;
21459 end if;
21460
21461 if Known_Static_Esize (Typ) then
21462 Size := UI_To_Int (Esize (Typ));
21463
21464 -- If the Esize (Object_Size) is unknown at compile time, look at the
21465 -- RM_Size (Value_Size) which may have been set by an explicit rep item.
21466
21467 elsif Known_Static_RM_Size (Typ) then
21468 Size := UI_To_Int (RM_Size (Typ));
21469
21470 -- Otherwise, the size is considered to be unknown.
21471
21472 else
21473 return False;
21474 end if;
21475
21476 -- Check that the size of the component is 8, 16, 32, or 64 bits and
21477 -- that Typ is properly aligned.
21478
21479 case Size is
21480 when 8 | 16 | 32 | 64 =>
21481 return Size = UI_To_Int (Alignment (Typ)) * 8;
21482
21483 when others =>
21484 return False;
21485 end case;
21486 end Support_Atomic_Primitives;
21487
21488 -----------------
21489 -- Trace_Scope --
21490 -----------------
21491
21492 procedure Trace_Scope (N : Node_Id; E : Entity_Id; Msg : String) is
21493 begin
21494 if Debug_Flag_W then
21495 for J in 0 .. Scope_Stack.Last loop
21496 Write_Str (" ");
21497 end loop;
21498
21499 Write_Str (Msg);
21500 Write_Name (Chars (E));
21501 Write_Str (" from ");
21502 Write_Location (Sloc (N));
21503 Write_Eol;
21504 end if;
21505 end Trace_Scope;
21506
21507 -----------------------
21508 -- Transfer_Entities --
21509 -----------------------
21510
21511 procedure Transfer_Entities (From : Entity_Id; To : Entity_Id) is
21512 procedure Set_Public_Status_Of (Id : Entity_Id);
21513 -- Set the Is_Public attribute of arbitrary entity Id by calling routine
21514 -- Set_Public_Status. If successful and Id denotes a record type, set
21515 -- the Is_Public attribute of its fields.
21516
21517 --------------------------
21518 -- Set_Public_Status_Of --
21519 --------------------------
21520
21521 procedure Set_Public_Status_Of (Id : Entity_Id) is
21522 Field : Entity_Id;
21523
21524 begin
21525 if not Is_Public (Id) then
21526 Set_Public_Status (Id);
21527
21528 -- When the input entity is a public record type, ensure that all
21529 -- its internal fields are also exposed to the linker. The fields
21530 -- of a class-wide type are never made public.
21531
21532 if Is_Public (Id)
21533 and then Is_Record_Type (Id)
21534 and then not Is_Class_Wide_Type (Id)
21535 then
21536 Field := First_Entity (Id);
21537 while Present (Field) loop
21538 Set_Is_Public (Field);
21539 Next_Entity (Field);
21540 end loop;
21541 end if;
21542 end if;
21543 end Set_Public_Status_Of;
21544
21545 -- Local variables
21546
21547 Full_Id : Entity_Id;
21548 Id : Entity_Id;
21549
21550 -- Start of processing for Transfer_Entities
21551
21552 begin
21553 Id := First_Entity (From);
21554
21555 if Present (Id) then
21556
21557 -- Merge the entity chain of the source scope with that of the
21558 -- destination scope.
21559
21560 if Present (Last_Entity (To)) then
21561 Set_Next_Entity (Last_Entity (To), Id);
21562 else
21563 Set_First_Entity (To, Id);
21564 end if;
21565
21566 Set_Last_Entity (To, Last_Entity (From));
21567
21568 -- Inspect the entities of the source scope and update their Scope
21569 -- attribute.
21570
21571 while Present (Id) loop
21572 Set_Scope (Id, To);
21573 Set_Public_Status_Of (Id);
21574
21575 -- Handle an internally generated full view for a private type
21576
21577 if Is_Private_Type (Id)
21578 and then Present (Full_View (Id))
21579 and then Is_Itype (Full_View (Id))
21580 then
21581 Full_Id := Full_View (Id);
21582
21583 Set_Scope (Full_Id, To);
21584 Set_Public_Status_Of (Full_Id);
21585 end if;
21586
21587 Next_Entity (Id);
21588 end loop;
21589
21590 Set_First_Entity (From, Empty);
21591 Set_Last_Entity (From, Empty);
21592 end if;
21593 end Transfer_Entities;
21594
21595 -----------------------
21596 -- Type_Access_Level --
21597 -----------------------
21598
21599 function Type_Access_Level (Typ : Entity_Id) return Uint is
21600 Btyp : Entity_Id;
21601
21602 begin
21603 Btyp := Base_Type (Typ);
21604
21605 -- Ada 2005 (AI-230): For most cases of anonymous access types, we
21606 -- simply use the level where the type is declared. This is true for
21607 -- stand-alone object declarations, and for anonymous access types
21608 -- associated with components the level is the same as that of the
21609 -- enclosing composite type. However, special treatment is needed for
21610 -- the cases of access parameters, return objects of an anonymous access
21611 -- type, and, in Ada 95, access discriminants of limited types.
21612
21613 if Is_Access_Type (Btyp) then
21614 if Ekind (Btyp) = E_Anonymous_Access_Type then
21615
21616 -- If the type is a nonlocal anonymous access type (such as for
21617 -- an access parameter) we treat it as being declared at the
21618 -- library level to ensure that names such as X.all'access don't
21619 -- fail static accessibility checks.
21620
21621 if not Is_Local_Anonymous_Access (Typ) then
21622 return Scope_Depth (Standard_Standard);
21623
21624 -- If this is a return object, the accessibility level is that of
21625 -- the result subtype of the enclosing function. The test here is
21626 -- little complicated, because we have to account for extended
21627 -- return statements that have been rewritten as blocks, in which
21628 -- case we have to find and the Is_Return_Object attribute of the
21629 -- itype's associated object. It would be nice to find a way to
21630 -- simplify this test, but it doesn't seem worthwhile to add a new
21631 -- flag just for purposes of this test. ???
21632
21633 elsif Ekind (Scope (Btyp)) = E_Return_Statement
21634 or else
21635 (Is_Itype (Btyp)
21636 and then Nkind (Associated_Node_For_Itype (Btyp)) =
21637 N_Object_Declaration
21638 and then Is_Return_Object
21639 (Defining_Identifier
21640 (Associated_Node_For_Itype (Btyp))))
21641 then
21642 declare
21643 Scop : Entity_Id;
21644
21645 begin
21646 Scop := Scope (Scope (Btyp));
21647 while Present (Scop) loop
21648 exit when Ekind (Scop) = E_Function;
21649 Scop := Scope (Scop);
21650 end loop;
21651
21652 -- Treat the return object's type as having the level of the
21653 -- function's result subtype (as per RM05-6.5(5.3/2)).
21654
21655 return Type_Access_Level (Etype (Scop));
21656 end;
21657 end if;
21658 end if;
21659
21660 Btyp := Root_Type (Btyp);
21661
21662 -- The accessibility level of anonymous access types associated with
21663 -- discriminants is that of the current instance of the type, and
21664 -- that's deeper than the type itself (AARM 3.10.2 (12.3.21)).
21665
21666 -- AI-402: access discriminants have accessibility based on the
21667 -- object rather than the type in Ada 2005, so the above paragraph
21668 -- doesn't apply.
21669
21670 -- ??? Needs completion with rules from AI-416
21671
21672 if Ada_Version <= Ada_95
21673 and then Ekind (Typ) = E_Anonymous_Access_Type
21674 and then Present (Associated_Node_For_Itype (Typ))
21675 and then Nkind (Associated_Node_For_Itype (Typ)) =
21676 N_Discriminant_Specification
21677 then
21678 return Scope_Depth (Enclosing_Dynamic_Scope (Btyp)) + 1;
21679 end if;
21680 end if;
21681
21682 -- Return library level for a generic formal type. This is done because
21683 -- RM(10.3.2) says that "The statically deeper relationship does not
21684 -- apply to ... a descendant of a generic formal type". Rather than
21685 -- checking at each point where a static accessibility check is
21686 -- performed to see if we are dealing with a formal type, this rule is
21687 -- implemented by having Type_Access_Level and Deepest_Type_Access_Level
21688 -- return extreme values for a formal type; Deepest_Type_Access_Level
21689 -- returns Int'Last. By calling the appropriate function from among the
21690 -- two, we ensure that the static accessibility check will pass if we
21691 -- happen to run into a formal type. More specifically, we should call
21692 -- Deepest_Type_Access_Level instead of Type_Access_Level whenever the
21693 -- call occurs as part of a static accessibility check and the error
21694 -- case is the case where the type's level is too shallow (as opposed
21695 -- to too deep).
21696
21697 if Is_Generic_Type (Root_Type (Btyp)) then
21698 return Scope_Depth (Standard_Standard);
21699 end if;
21700
21701 return Scope_Depth (Enclosing_Dynamic_Scope (Btyp));
21702 end Type_Access_Level;
21703
21704 ------------------------------------
21705 -- Type_Without_Stream_Operation --
21706 ------------------------------------
21707
21708 function Type_Without_Stream_Operation
21709 (T : Entity_Id;
21710 Op : TSS_Name_Type := TSS_Null) return Entity_Id
21711 is
21712 BT : constant Entity_Id := Base_Type (T);
21713 Op_Missing : Boolean;
21714
21715 begin
21716 if not Restriction_Active (No_Default_Stream_Attributes) then
21717 return Empty;
21718 end if;
21719
21720 if Is_Elementary_Type (T) then
21721 if Op = TSS_Null then
21722 Op_Missing :=
21723 No (TSS (BT, TSS_Stream_Read))
21724 or else No (TSS (BT, TSS_Stream_Write));
21725
21726 else
21727 Op_Missing := No (TSS (BT, Op));
21728 end if;
21729
21730 if Op_Missing then
21731 return T;
21732 else
21733 return Empty;
21734 end if;
21735
21736 elsif Is_Array_Type (T) then
21737 return Type_Without_Stream_Operation (Component_Type (T), Op);
21738
21739 elsif Is_Record_Type (T) then
21740 declare
21741 Comp : Entity_Id;
21742 C_Typ : Entity_Id;
21743
21744 begin
21745 Comp := First_Component (T);
21746 while Present (Comp) loop
21747 C_Typ := Type_Without_Stream_Operation (Etype (Comp), Op);
21748
21749 if Present (C_Typ) then
21750 return C_Typ;
21751 end if;
21752
21753 Next_Component (Comp);
21754 end loop;
21755
21756 return Empty;
21757 end;
21758
21759 elsif Is_Private_Type (T) and then Present (Full_View (T)) then
21760 return Type_Without_Stream_Operation (Full_View (T), Op);
21761 else
21762 return Empty;
21763 end if;
21764 end Type_Without_Stream_Operation;
21765
21766 ----------------------------
21767 -- Unique_Defining_Entity --
21768 ----------------------------
21769
21770 function Unique_Defining_Entity (N : Node_Id) return Entity_Id is
21771 begin
21772 return Unique_Entity (Defining_Entity (N));
21773 end Unique_Defining_Entity;
21774
21775 -------------------
21776 -- Unique_Entity --
21777 -------------------
21778
21779 function Unique_Entity (E : Entity_Id) return Entity_Id is
21780 U : Entity_Id := E;
21781 P : Node_Id;
21782
21783 begin
21784 case Ekind (E) is
21785 when E_Constant =>
21786 if Present (Full_View (E)) then
21787 U := Full_View (E);
21788 end if;
21789
21790 when Entry_Kind =>
21791 if Nkind (Parent (E)) = N_Entry_Body then
21792 declare
21793 Prot_Item : Entity_Id;
21794 Prot_Type : Entity_Id;
21795
21796 begin
21797 if Ekind (E) = E_Entry then
21798 Prot_Type := Scope (E);
21799
21800 -- Bodies of entry families are nested within an extra scope
21801 -- that contains an entry index declaration
21802
21803 else
21804 Prot_Type := Scope (Scope (E));
21805 end if;
21806
21807 -- A protected type may be declared as a private type, in
21808 -- which case we need to get its full view.
21809
21810 if Is_Private_Type (Prot_Type) then
21811 Prot_Type := Full_View (Prot_Type);
21812 end if;
21813
21814 -- Full view may not be present on error, in which case
21815 -- return E by default.
21816
21817 if Present (Prot_Type) then
21818 pragma Assert (Ekind (Prot_Type) = E_Protected_Type);
21819
21820 -- Traverse the entity list of the protected type and
21821 -- locate an entry declaration which matches the entry
21822 -- body.
21823
21824 Prot_Item := First_Entity (Prot_Type);
21825 while Present (Prot_Item) loop
21826 if Ekind (Prot_Item) in Entry_Kind
21827 and then Corresponding_Body (Parent (Prot_Item)) = E
21828 then
21829 U := Prot_Item;
21830 exit;
21831 end if;
21832
21833 Next_Entity (Prot_Item);
21834 end loop;
21835 end if;
21836 end;
21837 end if;
21838
21839 when Formal_Kind =>
21840 if Present (Spec_Entity (E)) then
21841 U := Spec_Entity (E);
21842 end if;
21843
21844 when E_Package_Body =>
21845 P := Parent (E);
21846
21847 if Nkind (P) = N_Defining_Program_Unit_Name then
21848 P := Parent (P);
21849 end if;
21850
21851 if Nkind (P) = N_Package_Body
21852 and then Present (Corresponding_Spec (P))
21853 then
21854 U := Corresponding_Spec (P);
21855
21856 elsif Nkind (P) = N_Package_Body_Stub
21857 and then Present (Corresponding_Spec_Of_Stub (P))
21858 then
21859 U := Corresponding_Spec_Of_Stub (P);
21860 end if;
21861
21862 when E_Protected_Body =>
21863 P := Parent (E);
21864
21865 if Nkind (P) = N_Protected_Body
21866 and then Present (Corresponding_Spec (P))
21867 then
21868 U := Corresponding_Spec (P);
21869
21870 elsif Nkind (P) = N_Protected_Body_Stub
21871 and then Present (Corresponding_Spec_Of_Stub (P))
21872 then
21873 U := Corresponding_Spec_Of_Stub (P);
21874
21875 if Is_Single_Protected_Object (U) then
21876 U := Etype (U);
21877 end if;
21878 end if;
21879
21880 if Is_Private_Type (U) then
21881 U := Full_View (U);
21882 end if;
21883
21884 when E_Subprogram_Body =>
21885 P := Parent (E);
21886
21887 if Nkind (P) = N_Defining_Program_Unit_Name then
21888 P := Parent (P);
21889 end if;
21890
21891 P := Parent (P);
21892
21893 if Nkind (P) = N_Subprogram_Body
21894 and then Present (Corresponding_Spec (P))
21895 then
21896 U := Corresponding_Spec (P);
21897
21898 elsif Nkind (P) = N_Subprogram_Body_Stub
21899 and then Present (Corresponding_Spec_Of_Stub (P))
21900 then
21901 U := Corresponding_Spec_Of_Stub (P);
21902
21903 elsif Nkind (P) = N_Subprogram_Renaming_Declaration then
21904 U := Corresponding_Spec (P);
21905 end if;
21906
21907 when E_Task_Body =>
21908 P := Parent (E);
21909
21910 if Nkind (P) = N_Task_Body
21911 and then Present (Corresponding_Spec (P))
21912 then
21913 U := Corresponding_Spec (P);
21914
21915 elsif Nkind (P) = N_Task_Body_Stub
21916 and then Present (Corresponding_Spec_Of_Stub (P))
21917 then
21918 U := Corresponding_Spec_Of_Stub (P);
21919
21920 if Is_Single_Task_Object (U) then
21921 U := Etype (U);
21922 end if;
21923 end if;
21924
21925 if Is_Private_Type (U) then
21926 U := Full_View (U);
21927 end if;
21928
21929 when Type_Kind =>
21930 if Present (Full_View (E)) then
21931 U := Full_View (E);
21932 end if;
21933
21934 when others =>
21935 null;
21936 end case;
21937
21938 return U;
21939 end Unique_Entity;
21940
21941 -----------------
21942 -- Unique_Name --
21943 -----------------
21944
21945 function Unique_Name (E : Entity_Id) return String is
21946
21947 -- Names in E_Subprogram_Body or E_Package_Body entities are not
21948 -- reliable, as they may not include the overloading suffix. Instead,
21949 -- when looking for the name of E or one of its enclosing scope, we get
21950 -- the name of the corresponding Unique_Entity.
21951
21952 U : constant Entity_Id := Unique_Entity (E);
21953
21954 function This_Name return String;
21955
21956 ---------------
21957 -- This_Name --
21958 ---------------
21959
21960 function This_Name return String is
21961 begin
21962 return Get_Name_String (Chars (U));
21963 end This_Name;
21964
21965 -- Start of processing for Unique_Name
21966
21967 begin
21968 if E = Standard_Standard
21969 or else Has_Fully_Qualified_Name (E)
21970 then
21971 return This_Name;
21972
21973 elsif Ekind (E) = E_Enumeration_Literal then
21974 return Unique_Name (Etype (E)) & "__" & This_Name;
21975
21976 else
21977 declare
21978 S : constant Entity_Id := Scope (U);
21979 pragma Assert (Present (S));
21980
21981 begin
21982 -- Prefix names of predefined types with standard__, but leave
21983 -- names of user-defined packages and subprograms without prefix
21984 -- (even if technically they are nested in the Standard package).
21985
21986 if S = Standard_Standard then
21987 if Ekind (U) = E_Package or else Is_Subprogram (U) then
21988 return This_Name;
21989 else
21990 return Unique_Name (S) & "__" & This_Name;
21991 end if;
21992
21993 -- For intances of generic subprograms use the name of the related
21994 -- instace and skip the scope of its wrapper package.
21995
21996 elsif Is_Wrapper_Package (S) then
21997 pragma Assert (Scope (S) = Scope (Related_Instance (S)));
21998 -- Wrapper package and the instantiation are in the same scope
21999
22000 declare
22001 Enclosing_Name : constant String :=
22002 Unique_Name (Scope (S)) & "__" &
22003 Get_Name_String (Chars (Related_Instance (S)));
22004
22005 begin
22006 if Is_Subprogram (U)
22007 and then not Is_Generic_Actual_Subprogram (U)
22008 then
22009 return Enclosing_Name;
22010 else
22011 return Enclosing_Name & "__" & This_Name;
22012 end if;
22013 end;
22014
22015 else
22016 return Unique_Name (S) & "__" & This_Name;
22017 end if;
22018 end;
22019 end if;
22020 end Unique_Name;
22021
22022 ---------------------
22023 -- Unit_Is_Visible --
22024 ---------------------
22025
22026 function Unit_Is_Visible (U : Entity_Id) return Boolean is
22027 Curr : constant Node_Id := Cunit (Current_Sem_Unit);
22028 Curr_Entity : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
22029
22030 function Unit_In_Parent_Context (Par_Unit : Node_Id) return Boolean;
22031 -- For a child unit, check whether unit appears in a with_clause
22032 -- of a parent.
22033
22034 function Unit_In_Context (Comp_Unit : Node_Id) return Boolean;
22035 -- Scan the context clause of one compilation unit looking for a
22036 -- with_clause for the unit in question.
22037
22038 ----------------------------
22039 -- Unit_In_Parent_Context --
22040 ----------------------------
22041
22042 function Unit_In_Parent_Context (Par_Unit : Node_Id) return Boolean is
22043 begin
22044 if Unit_In_Context (Par_Unit) then
22045 return True;
22046
22047 elsif Is_Child_Unit (Defining_Entity (Unit (Par_Unit))) then
22048 return Unit_In_Parent_Context (Parent_Spec (Unit (Par_Unit)));
22049
22050 else
22051 return False;
22052 end if;
22053 end Unit_In_Parent_Context;
22054
22055 ---------------------
22056 -- Unit_In_Context --
22057 ---------------------
22058
22059 function Unit_In_Context (Comp_Unit : Node_Id) return Boolean is
22060 Clause : Node_Id;
22061
22062 begin
22063 Clause := First (Context_Items (Comp_Unit));
22064 while Present (Clause) loop
22065 if Nkind (Clause) = N_With_Clause then
22066 if Library_Unit (Clause) = U then
22067 return True;
22068
22069 -- The with_clause may denote a renaming of the unit we are
22070 -- looking for, eg. Text_IO which renames Ada.Text_IO.
22071
22072 elsif
22073 Renamed_Entity (Entity (Name (Clause))) =
22074 Defining_Entity (Unit (U))
22075 then
22076 return True;
22077 end if;
22078 end if;
22079
22080 Next (Clause);
22081 end loop;
22082
22083 return False;
22084 end Unit_In_Context;
22085
22086 -- Start of processing for Unit_Is_Visible
22087
22088 begin
22089 -- The currrent unit is directly visible
22090
22091 if Curr = U then
22092 return True;
22093
22094 elsif Unit_In_Context (Curr) then
22095 return True;
22096
22097 -- If the current unit is a body, check the context of the spec
22098
22099 elsif Nkind (Unit (Curr)) = N_Package_Body
22100 or else
22101 (Nkind (Unit (Curr)) = N_Subprogram_Body
22102 and then not Acts_As_Spec (Unit (Curr)))
22103 then
22104 if Unit_In_Context (Library_Unit (Curr)) then
22105 return True;
22106 end if;
22107 end if;
22108
22109 -- If the spec is a child unit, examine the parents
22110
22111 if Is_Child_Unit (Curr_Entity) then
22112 if Nkind (Unit (Curr)) in N_Unit_Body then
22113 return
22114 Unit_In_Parent_Context
22115 (Parent_Spec (Unit (Library_Unit (Curr))));
22116 else
22117 return Unit_In_Parent_Context (Parent_Spec (Unit (Curr)));
22118 end if;
22119
22120 else
22121 return False;
22122 end if;
22123 end Unit_Is_Visible;
22124
22125 ------------------------------
22126 -- Universal_Interpretation --
22127 ------------------------------
22128
22129 function Universal_Interpretation (Opnd : Node_Id) return Entity_Id is
22130 Index : Interp_Index;
22131 It : Interp;
22132
22133 begin
22134 -- The argument may be a formal parameter of an operator or subprogram
22135 -- with multiple interpretations, or else an expression for an actual.
22136
22137 if Nkind (Opnd) = N_Defining_Identifier
22138 or else not Is_Overloaded (Opnd)
22139 then
22140 if Etype (Opnd) = Universal_Integer
22141 or else Etype (Opnd) = Universal_Real
22142 then
22143 return Etype (Opnd);
22144 else
22145 return Empty;
22146 end if;
22147
22148 else
22149 Get_First_Interp (Opnd, Index, It);
22150 while Present (It.Typ) loop
22151 if It.Typ = Universal_Integer
22152 or else It.Typ = Universal_Real
22153 then
22154 return It.Typ;
22155 end if;
22156
22157 Get_Next_Interp (Index, It);
22158 end loop;
22159
22160 return Empty;
22161 end if;
22162 end Universal_Interpretation;
22163
22164 ---------------
22165 -- Unqualify --
22166 ---------------
22167
22168 function Unqualify (Expr : Node_Id) return Node_Id is
22169 begin
22170 -- Recurse to handle unlikely case of multiple levels of qualification
22171
22172 if Nkind (Expr) = N_Qualified_Expression then
22173 return Unqualify (Expression (Expr));
22174
22175 -- Normal case, not a qualified expression
22176
22177 else
22178 return Expr;
22179 end if;
22180 end Unqualify;
22181
22182 -----------------------
22183 -- Visible_Ancestors --
22184 -----------------------
22185
22186 function Visible_Ancestors (Typ : Entity_Id) return Elist_Id is
22187 List_1 : Elist_Id;
22188 List_2 : Elist_Id;
22189 Elmt : Elmt_Id;
22190
22191 begin
22192 pragma Assert (Is_Record_Type (Typ) and then Is_Tagged_Type (Typ));
22193
22194 -- Collect all the parents and progenitors of Typ. If the full-view of
22195 -- private parents and progenitors is available then it is used to
22196 -- generate the list of visible ancestors; otherwise their partial
22197 -- view is added to the resulting list.
22198
22199 Collect_Parents
22200 (T => Typ,
22201 List => List_1,
22202 Use_Full_View => True);
22203
22204 Collect_Interfaces
22205 (T => Typ,
22206 Ifaces_List => List_2,
22207 Exclude_Parents => True,
22208 Use_Full_View => True);
22209
22210 -- Join the two lists. Avoid duplications because an interface may
22211 -- simultaneously be parent and progenitor of a type.
22212
22213 Elmt := First_Elmt (List_2);
22214 while Present (Elmt) loop
22215 Append_Unique_Elmt (Node (Elmt), List_1);
22216 Next_Elmt (Elmt);
22217 end loop;
22218
22219 return List_1;
22220 end Visible_Ancestors;
22221
22222 ----------------------
22223 -- Within_Init_Proc --
22224 ----------------------
22225
22226 function Within_Init_Proc return Boolean is
22227 S : Entity_Id;
22228
22229 begin
22230 S := Current_Scope;
22231 while not Is_Overloadable (S) loop
22232 if S = Standard_Standard then
22233 return False;
22234 else
22235 S := Scope (S);
22236 end if;
22237 end loop;
22238
22239 return Is_Init_Proc (S);
22240 end Within_Init_Proc;
22241
22242 ------------------
22243 -- Within_Scope --
22244 ------------------
22245
22246 function Within_Scope (E : Entity_Id; S : Entity_Id) return Boolean is
22247 begin
22248 return Scope_Within_Or_Same (Scope (E), S);
22249 end Within_Scope;
22250
22251 ----------------
22252 -- Wrong_Type --
22253 ----------------
22254
22255 procedure Wrong_Type (Expr : Node_Id; Expected_Type : Entity_Id) is
22256 Found_Type : constant Entity_Id := First_Subtype (Etype (Expr));
22257 Expec_Type : constant Entity_Id := First_Subtype (Expected_Type);
22258
22259 Matching_Field : Entity_Id;
22260 -- Entity to give a more precise suggestion on how to write a one-
22261 -- element positional aggregate.
22262
22263 function Has_One_Matching_Field return Boolean;
22264 -- Determines if Expec_Type is a record type with a single component or
22265 -- discriminant whose type matches the found type or is one dimensional
22266 -- array whose component type matches the found type. In the case of
22267 -- one discriminant, we ignore the variant parts. That's not accurate,
22268 -- but good enough for the warning.
22269
22270 ----------------------------
22271 -- Has_One_Matching_Field --
22272 ----------------------------
22273
22274 function Has_One_Matching_Field return Boolean is
22275 E : Entity_Id;
22276
22277 begin
22278 Matching_Field := Empty;
22279
22280 if Is_Array_Type (Expec_Type)
22281 and then Number_Dimensions (Expec_Type) = 1
22282 and then Covers (Etype (Component_Type (Expec_Type)), Found_Type)
22283 then
22284 -- Use type name if available. This excludes multidimensional
22285 -- arrays and anonymous arrays.
22286
22287 if Comes_From_Source (Expec_Type) then
22288 Matching_Field := Expec_Type;
22289
22290 -- For an assignment, use name of target
22291
22292 elsif Nkind (Parent (Expr)) = N_Assignment_Statement
22293 and then Is_Entity_Name (Name (Parent (Expr)))
22294 then
22295 Matching_Field := Entity (Name (Parent (Expr)));
22296 end if;
22297
22298 return True;
22299
22300 elsif not Is_Record_Type (Expec_Type) then
22301 return False;
22302
22303 else
22304 E := First_Entity (Expec_Type);
22305 loop
22306 if No (E) then
22307 return False;
22308
22309 elsif not Ekind_In (E, E_Discriminant, E_Component)
22310 or else Nam_In (Chars (E), Name_uTag, Name_uParent)
22311 then
22312 Next_Entity (E);
22313
22314 else
22315 exit;
22316 end if;
22317 end loop;
22318
22319 if not Covers (Etype (E), Found_Type) then
22320 return False;
22321
22322 elsif Present (Next_Entity (E))
22323 and then (Ekind (E) = E_Component
22324 or else Ekind (Next_Entity (E)) = E_Discriminant)
22325 then
22326 return False;
22327
22328 else
22329 Matching_Field := E;
22330 return True;
22331 end if;
22332 end if;
22333 end Has_One_Matching_Field;
22334
22335 -- Start of processing for Wrong_Type
22336
22337 begin
22338 -- Don't output message if either type is Any_Type, or if a message
22339 -- has already been posted for this node. We need to do the latter
22340 -- check explicitly (it is ordinarily done in Errout), because we
22341 -- are using ! to force the output of the error messages.
22342
22343 if Expec_Type = Any_Type
22344 or else Found_Type = Any_Type
22345 or else Error_Posted (Expr)
22346 then
22347 return;
22348
22349 -- If one of the types is a Taft-Amendment type and the other it its
22350 -- completion, it must be an illegal use of a TAT in the spec, for
22351 -- which an error was already emitted. Avoid cascaded errors.
22352
22353 elsif Is_Incomplete_Type (Expec_Type)
22354 and then Has_Completion_In_Body (Expec_Type)
22355 and then Full_View (Expec_Type) = Etype (Expr)
22356 then
22357 return;
22358
22359 elsif Is_Incomplete_Type (Etype (Expr))
22360 and then Has_Completion_In_Body (Etype (Expr))
22361 and then Full_View (Etype (Expr)) = Expec_Type
22362 then
22363 return;
22364
22365 -- In an instance, there is an ongoing problem with completion of
22366 -- type derived from private types. Their structure is what Gigi
22367 -- expects, but the Etype is the parent type rather than the
22368 -- derived private type itself. Do not flag error in this case. The
22369 -- private completion is an entity without a parent, like an Itype.
22370 -- Similarly, full and partial views may be incorrect in the instance.
22371 -- There is no simple way to insure that it is consistent ???
22372
22373 -- A similar view discrepancy can happen in an inlined body, for the
22374 -- same reason: inserted body may be outside of the original package
22375 -- and only partial views are visible at the point of insertion.
22376
22377 elsif In_Instance or else In_Inlined_Body then
22378 if Etype (Etype (Expr)) = Etype (Expected_Type)
22379 and then
22380 (Has_Private_Declaration (Expected_Type)
22381 or else Has_Private_Declaration (Etype (Expr)))
22382 and then No (Parent (Expected_Type))
22383 then
22384 return;
22385
22386 elsif Nkind (Parent (Expr)) = N_Qualified_Expression
22387 and then Entity (Subtype_Mark (Parent (Expr))) = Expected_Type
22388 then
22389 return;
22390
22391 elsif Is_Private_Type (Expected_Type)
22392 and then Present (Full_View (Expected_Type))
22393 and then Covers (Full_View (Expected_Type), Etype (Expr))
22394 then
22395 return;
22396
22397 -- Conversely, type of expression may be the private one
22398
22399 elsif Is_Private_Type (Base_Type (Etype (Expr)))
22400 and then Full_View (Base_Type (Etype (Expr))) = Expected_Type
22401 then
22402 return;
22403 end if;
22404 end if;
22405
22406 -- An interesting special check. If the expression is parenthesized
22407 -- and its type corresponds to the type of the sole component of the
22408 -- expected record type, or to the component type of the expected one
22409 -- dimensional array type, then assume we have a bad aggregate attempt.
22410
22411 if Nkind (Expr) in N_Subexpr
22412 and then Paren_Count (Expr) /= 0
22413 and then Has_One_Matching_Field
22414 then
22415 Error_Msg_N ("positional aggregate cannot have one component", Expr);
22416
22417 if Present (Matching_Field) then
22418 if Is_Array_Type (Expec_Type) then
22419 Error_Msg_NE
22420 ("\write instead `&''First ='> ...`", Expr, Matching_Field);
22421 else
22422 Error_Msg_NE
22423 ("\write instead `& ='> ...`", Expr, Matching_Field);
22424 end if;
22425 end if;
22426
22427 -- Another special check, if we are looking for a pool-specific access
22428 -- type and we found an E_Access_Attribute_Type, then we have the case
22429 -- of an Access attribute being used in a context which needs a pool-
22430 -- specific type, which is never allowed. The one extra check we make
22431 -- is that the expected designated type covers the Found_Type.
22432
22433 elsif Is_Access_Type (Expec_Type)
22434 and then Ekind (Found_Type) = E_Access_Attribute_Type
22435 and then Ekind (Base_Type (Expec_Type)) /= E_General_Access_Type
22436 and then Ekind (Base_Type (Expec_Type)) /= E_Anonymous_Access_Type
22437 and then Covers
22438 (Designated_Type (Expec_Type), Designated_Type (Found_Type))
22439 then
22440 Error_Msg_N -- CODEFIX
22441 ("result must be general access type!", Expr);
22442 Error_Msg_NE -- CODEFIX
22443 ("add ALL to }!", Expr, Expec_Type);
22444
22445 -- Another special check, if the expected type is an integer type,
22446 -- but the expression is of type System.Address, and the parent is
22447 -- an addition or subtraction operation whose left operand is the
22448 -- expression in question and whose right operand is of an integral
22449 -- type, then this is an attempt at address arithmetic, so give
22450 -- appropriate message.
22451
22452 elsif Is_Integer_Type (Expec_Type)
22453 and then Is_RTE (Found_Type, RE_Address)
22454 and then Nkind_In (Parent (Expr), N_Op_Add, N_Op_Subtract)
22455 and then Expr = Left_Opnd (Parent (Expr))
22456 and then Is_Integer_Type (Etype (Right_Opnd (Parent (Expr))))
22457 then
22458 Error_Msg_N
22459 ("address arithmetic not predefined in package System",
22460 Parent (Expr));
22461 Error_Msg_N
22462 ("\possible missing with/use of System.Storage_Elements",
22463 Parent (Expr));
22464 return;
22465
22466 -- If the expected type is an anonymous access type, as for access
22467 -- parameters and discriminants, the error is on the designated types.
22468
22469 elsif Ekind (Expec_Type) = E_Anonymous_Access_Type then
22470 if Comes_From_Source (Expec_Type) then
22471 Error_Msg_NE ("expected}!", Expr, Expec_Type);
22472 else
22473 Error_Msg_NE
22474 ("expected an access type with designated}",
22475 Expr, Designated_Type (Expec_Type));
22476 end if;
22477
22478 if Is_Access_Type (Found_Type)
22479 and then not Comes_From_Source (Found_Type)
22480 then
22481 Error_Msg_NE
22482 ("\\found an access type with designated}!",
22483 Expr, Designated_Type (Found_Type));
22484 else
22485 if From_Limited_With (Found_Type) then
22486 Error_Msg_NE ("\\found incomplete}!", Expr, Found_Type);
22487 Error_Msg_Qual_Level := 99;
22488 Error_Msg_NE -- CODEFIX
22489 ("\\missing `WITH &;", Expr, Scope (Found_Type));
22490 Error_Msg_Qual_Level := 0;
22491 else
22492 Error_Msg_NE ("found}!", Expr, Found_Type);
22493 end if;
22494 end if;
22495
22496 -- Normal case of one type found, some other type expected
22497
22498 else
22499 -- If the names of the two types are the same, see if some number
22500 -- of levels of qualification will help. Don't try more than three
22501 -- levels, and if we get to standard, it's no use (and probably
22502 -- represents an error in the compiler) Also do not bother with
22503 -- internal scope names.
22504
22505 declare
22506 Expec_Scope : Entity_Id;
22507 Found_Scope : Entity_Id;
22508
22509 begin
22510 Expec_Scope := Expec_Type;
22511 Found_Scope := Found_Type;
22512
22513 for Levels in Nat range 0 .. 3 loop
22514 if Chars (Expec_Scope) /= Chars (Found_Scope) then
22515 Error_Msg_Qual_Level := Levels;
22516 exit;
22517 end if;
22518
22519 Expec_Scope := Scope (Expec_Scope);
22520 Found_Scope := Scope (Found_Scope);
22521
22522 exit when Expec_Scope = Standard_Standard
22523 or else Found_Scope = Standard_Standard
22524 or else not Comes_From_Source (Expec_Scope)
22525 or else not Comes_From_Source (Found_Scope);
22526 end loop;
22527 end;
22528
22529 if Is_Record_Type (Expec_Type)
22530 and then Present (Corresponding_Remote_Type (Expec_Type))
22531 then
22532 Error_Msg_NE ("expected}!", Expr,
22533 Corresponding_Remote_Type (Expec_Type));
22534 else
22535 Error_Msg_NE ("expected}!", Expr, Expec_Type);
22536 end if;
22537
22538 if Is_Entity_Name (Expr)
22539 and then Is_Package_Or_Generic_Package (Entity (Expr))
22540 then
22541 Error_Msg_N ("\\found package name!", Expr);
22542
22543 elsif Is_Entity_Name (Expr)
22544 and then Ekind_In (Entity (Expr), E_Procedure, E_Generic_Procedure)
22545 then
22546 if Ekind (Expec_Type) = E_Access_Subprogram_Type then
22547 Error_Msg_N
22548 ("found procedure name, possibly missing Access attribute!",
22549 Expr);
22550 else
22551 Error_Msg_N
22552 ("\\found procedure name instead of function!", Expr);
22553 end if;
22554
22555 elsif Nkind (Expr) = N_Function_Call
22556 and then Ekind (Expec_Type) = E_Access_Subprogram_Type
22557 and then Etype (Designated_Type (Expec_Type)) = Etype (Expr)
22558 and then No (Parameter_Associations (Expr))
22559 then
22560 Error_Msg_N
22561 ("found function name, possibly missing Access attribute!",
22562 Expr);
22563
22564 -- Catch common error: a prefix or infix operator which is not
22565 -- directly visible because the type isn't.
22566
22567 elsif Nkind (Expr) in N_Op
22568 and then Is_Overloaded (Expr)
22569 and then not Is_Immediately_Visible (Expec_Type)
22570 and then not Is_Potentially_Use_Visible (Expec_Type)
22571 and then not In_Use (Expec_Type)
22572 and then Has_Compatible_Type (Right_Opnd (Expr), Expec_Type)
22573 then
22574 Error_Msg_N
22575 ("operator of the type is not directly visible!", Expr);
22576
22577 elsif Ekind (Found_Type) = E_Void
22578 and then Present (Parent (Found_Type))
22579 and then Nkind (Parent (Found_Type)) = N_Full_Type_Declaration
22580 then
22581 Error_Msg_NE ("\\found premature usage of}!", Expr, Found_Type);
22582
22583 else
22584 Error_Msg_NE ("\\found}!", Expr, Found_Type);
22585 end if;
22586
22587 -- A special check for cases like M1 and M2 = 0 where M1 and M2 are
22588 -- of the same modular type, and (M1 and M2) = 0 was intended.
22589
22590 if Expec_Type = Standard_Boolean
22591 and then Is_Modular_Integer_Type (Found_Type)
22592 and then Nkind_In (Parent (Expr), N_Op_And, N_Op_Or, N_Op_Xor)
22593 and then Nkind (Right_Opnd (Parent (Expr))) in N_Op_Compare
22594 then
22595 declare
22596 Op : constant Node_Id := Right_Opnd (Parent (Expr));
22597 L : constant Node_Id := Left_Opnd (Op);
22598 R : constant Node_Id := Right_Opnd (Op);
22599
22600 begin
22601 -- The case for the message is when the left operand of the
22602 -- comparison is the same modular type, or when it is an
22603 -- integer literal (or other universal integer expression),
22604 -- which would have been typed as the modular type if the
22605 -- parens had been there.
22606
22607 if (Etype (L) = Found_Type
22608 or else
22609 Etype (L) = Universal_Integer)
22610 and then Is_Integer_Type (Etype (R))
22611 then
22612 Error_Msg_N
22613 ("\\possible missing parens for modular operation", Expr);
22614 end if;
22615 end;
22616 end if;
22617
22618 -- Reset error message qualification indication
22619
22620 Error_Msg_Qual_Level := 0;
22621 end if;
22622 end Wrong_Type;
22623
22624 --------------------------------
22625 -- Yields_Synchronized_Object --
22626 --------------------------------
22627
22628 function Yields_Synchronized_Object (Typ : Entity_Id) return Boolean is
22629 Has_Sync_Comp : Boolean := False;
22630 Id : Entity_Id;
22631
22632 begin
22633 -- An array type yields a synchronized object if its component type
22634 -- yields a synchronized object.
22635
22636 if Is_Array_Type (Typ) then
22637 return Yields_Synchronized_Object (Component_Type (Typ));
22638
22639 -- A descendant of type Ada.Synchronous_Task_Control.Suspension_Object
22640 -- yields a synchronized object by default.
22641
22642 elsif Is_Descendant_Of_Suspension_Object (Typ) then
22643 return True;
22644
22645 -- A protected type yields a synchronized object by default
22646
22647 elsif Is_Protected_Type (Typ) then
22648 return True;
22649
22650 -- A record type or type extension yields a synchronized object when its
22651 -- discriminants (if any) lack default values and all components are of
22652 -- a type that yelds a synchronized object.
22653
22654 elsif Is_Record_Type (Typ) then
22655
22656 -- Inspect all entities defined in the scope of the type, looking for
22657 -- components of a type that does not yeld a synchronized object or
22658 -- for discriminants with default values.
22659
22660 Id := First_Entity (Typ);
22661 while Present (Id) loop
22662 if Comes_From_Source (Id) then
22663 if Ekind (Id) = E_Component then
22664 if Yields_Synchronized_Object (Etype (Id)) then
22665 Has_Sync_Comp := True;
22666
22667 -- The component does not yield a synchronized object
22668
22669 else
22670 return False;
22671 end if;
22672
22673 elsif Ekind (Id) = E_Discriminant
22674 and then Present (Expression (Parent (Id)))
22675 then
22676 return False;
22677 end if;
22678 end if;
22679
22680 Next_Entity (Id);
22681 end loop;
22682
22683 -- Ensure that the parent type of a type extension yields a
22684 -- synchronized object.
22685
22686 if Etype (Typ) /= Typ
22687 and then not Yields_Synchronized_Object (Etype (Typ))
22688 then
22689 return False;
22690 end if;
22691
22692 -- If we get here, then all discriminants lack default values and all
22693 -- components are of a type that yields a synchronized object.
22694
22695 return Has_Sync_Comp;
22696
22697 -- A synchronized interface type yields a synchronized object by default
22698
22699 elsif Is_Synchronized_Interface (Typ) then
22700 return True;
22701
22702 -- A task type yelds a synchronized object by default
22703
22704 elsif Is_Task_Type (Typ) then
22705 return True;
22706
22707 -- Otherwise the type does not yield a synchronized object
22708
22709 else
22710 return False;
22711 end if;
22712 end Yields_Synchronized_Object;
22713
22714 ---------------------------
22715 -- Yields_Universal_Type --
22716 ---------------------------
22717
22718 function Yields_Universal_Type (N : Node_Id) return Boolean is
22719 begin
22720 -- Integer and real literals are of a universal type
22721
22722 if Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
22723 return True;
22724
22725 -- The values of certain attributes are of a universal type
22726
22727 elsif Nkind (N) = N_Attribute_Reference then
22728 return
22729 Universal_Type_Attribute (Get_Attribute_Id (Attribute_Name (N)));
22730
22731 -- ??? There are possibly other cases to consider
22732
22733 else
22734 return False;
22735 end if;
22736 end Yields_Universal_Type;
22737
22738 end Sem_Util;