sh.c (shift_insns_rtx, [...]): Truncate shift counts to avoid out-of-bounds array...
[official-gcc.git] / gcc / ada / sem_util.adb
blob337d1ac0cf0f1ac1a0ff7efb2aaa6634a7ddd166
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-2008, 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 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Casing; use Casing;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Errout; use Errout;
31 with Elists; use Elists;
32 with Exp_Disp; use Exp_Disp;
33 with Exp_Tss; use Exp_Tss;
34 with Exp_Util; use Exp_Util;
35 with Fname; use Fname;
36 with Freeze; use Freeze;
37 with Lib; use Lib;
38 with Lib.Xref; use Lib.Xref;
39 with Nlists; use Nlists;
40 with Output; use Output;
41 with Opt; use Opt;
42 with Rtsfind; use Rtsfind;
43 with Scans; use Scans;
44 with Scn; use Scn;
45 with Sem; use Sem;
46 with Sem_Aux; use Sem_Aux;
47 with Sem_Attr; use Sem_Attr;
48 with Sem_Ch8; use Sem_Ch8;
49 with Sem_Eval; use Sem_Eval;
50 with Sem_Res; use Sem_Res;
51 with Sem_Type; use Sem_Type;
52 with Sinfo; use Sinfo;
53 with Sinput; use Sinput;
54 with Stand; use Stand;
55 with Style;
56 with Stringt; use Stringt;
57 with Targparm; use Targparm;
58 with Tbuild; use Tbuild;
59 with Ttypes; use Ttypes;
60 with Uname; use Uname;
62 with GNAT.HTable; use GNAT.HTable;
63 package body Sem_Util is
65 ----------------------------------------
66 -- Global_Variables for New_Copy_Tree --
67 ----------------------------------------
69 -- These global variables are used by New_Copy_Tree. See description
70 -- of the body of this subprogram for details. Global variables can be
71 -- safely used by New_Copy_Tree, since there is no case of a recursive
72 -- call from the processing inside New_Copy_Tree.
74 NCT_Hash_Threshhold : constant := 20;
75 -- If there are more than this number of pairs of entries in the
76 -- map, then Hash_Tables_Used will be set, and the hash tables will
77 -- be initialized and used for the searches.
79 NCT_Hash_Tables_Used : Boolean := False;
80 -- Set to True if hash tables are in use
82 NCT_Table_Entries : Nat;
83 -- Count entries in table to see if threshhold is reached
85 NCT_Hash_Table_Setup : Boolean := False;
86 -- Set to True if hash table contains data. We set this True if we
87 -- setup the hash table with data, and leave it set permanently
88 -- from then on, this is a signal that second and subsequent users
89 -- of the hash table must clear the old entries before reuse.
91 subtype NCT_Header_Num is Int range 0 .. 511;
92 -- Defines range of headers in hash tables (512 headers)
94 -----------------------
95 -- Local Subprograms --
96 -----------------------
98 function Build_Component_Subtype
99 (C : List_Id;
100 Loc : Source_Ptr;
101 T : Entity_Id) return Node_Id;
102 -- This function builds the subtype for Build_Actual_Subtype_Of_Component
103 -- and Build_Discriminal_Subtype_Of_Component. C is a list of constraints,
104 -- Loc is the source location, T is the original subtype.
106 function Is_Fully_Initialized_Variant (Typ : Entity_Id) return Boolean;
107 -- Subsidiary to Is_Fully_Initialized_Type. For an unconstrained type
108 -- with discriminants whose default values are static, examine only the
109 -- components in the selected variant to determine whether all of them
110 -- have a default.
112 function Has_Null_Extension (T : Entity_Id) return Boolean;
113 -- T is a derived tagged type. Check whether the type extension is null.
114 -- If the parent type is fully initialized, T can be treated as such.
116 ------------------------------
117 -- Abstract_Interface_List --
118 ------------------------------
120 function Abstract_Interface_List (Typ : Entity_Id) return List_Id is
121 Nod : Node_Id;
123 begin
124 if Is_Concurrent_Type (Typ) then
126 -- If we are dealing with a synchronized subtype, go to the base
127 -- type, whose declaration has the interface list.
129 -- Shouldn't this be Declaration_Node???
131 Nod := Parent (Base_Type (Typ));
133 if Nkind (Nod) = N_Full_Type_Declaration then
134 return Empty_List;
135 end if;
137 elsif Ekind (Typ) = E_Record_Type_With_Private then
138 if Nkind (Parent (Typ)) = N_Full_Type_Declaration then
139 Nod := Type_Definition (Parent (Typ));
141 elsif Nkind (Parent (Typ)) = N_Private_Type_Declaration then
142 if Present (Full_View (Typ)) then
143 Nod := Type_Definition (Parent (Full_View (Typ)));
145 -- If the full-view is not available we cannot do anything else
146 -- here (the source has errors).
148 else
149 return Empty_List;
150 end if;
152 -- Support for generic formals with interfaces is still missing ???
154 elsif Nkind (Parent (Typ)) = N_Formal_Type_Declaration then
155 return Empty_List;
157 else
158 pragma Assert
159 (Nkind (Parent (Typ)) = N_Private_Extension_Declaration);
160 Nod := Parent (Typ);
161 end if;
163 elsif Ekind (Typ) = E_Record_Subtype then
164 Nod := Type_Definition (Parent (Etype (Typ)));
166 elsif Ekind (Typ) = E_Record_Subtype_With_Private then
168 -- Recurse, because parent may still be a private extension. Also
169 -- note that the full view of the subtype or the full view of its
170 -- base type may (both) be unavailable.
172 return Abstract_Interface_List (Etype (Typ));
174 else pragma Assert ((Ekind (Typ)) = E_Record_Type);
175 if Nkind (Parent (Typ)) = N_Formal_Type_Declaration then
176 Nod := Formal_Type_Definition (Parent (Typ));
177 else
178 Nod := Type_Definition (Parent (Typ));
179 end if;
180 end if;
182 return Interface_List (Nod);
183 end Abstract_Interface_List;
185 --------------------------------
186 -- Add_Access_Type_To_Process --
187 --------------------------------
189 procedure Add_Access_Type_To_Process (E : Entity_Id; A : Entity_Id) is
190 L : Elist_Id;
192 begin
193 Ensure_Freeze_Node (E);
194 L := Access_Types_To_Process (Freeze_Node (E));
196 if No (L) then
197 L := New_Elmt_List;
198 Set_Access_Types_To_Process (Freeze_Node (E), L);
199 end if;
201 Append_Elmt (A, L);
202 end Add_Access_Type_To_Process;
204 ----------------------------
205 -- Add_Global_Declaration --
206 ----------------------------
208 procedure Add_Global_Declaration (N : Node_Id) is
209 Aux_Node : constant Node_Id := Aux_Decls_Node (Cunit (Current_Sem_Unit));
211 begin
212 if No (Declarations (Aux_Node)) then
213 Set_Declarations (Aux_Node, New_List);
214 end if;
216 Append_To (Declarations (Aux_Node), N);
217 Analyze (N);
218 end Add_Global_Declaration;
220 -----------------------
221 -- Alignment_In_Bits --
222 -----------------------
224 function Alignment_In_Bits (E : Entity_Id) return Uint is
225 begin
226 return Alignment (E) * System_Storage_Unit;
227 end Alignment_In_Bits;
229 -----------------------------------------
230 -- Apply_Compile_Time_Constraint_Error --
231 -----------------------------------------
233 procedure Apply_Compile_Time_Constraint_Error
234 (N : Node_Id;
235 Msg : String;
236 Reason : RT_Exception_Code;
237 Ent : Entity_Id := Empty;
238 Typ : Entity_Id := Empty;
239 Loc : Source_Ptr := No_Location;
240 Rep : Boolean := True;
241 Warn : Boolean := False)
243 Stat : constant Boolean := Is_Static_Expression (N);
244 R_Stat : constant Node_Id :=
245 Make_Raise_Constraint_Error (Sloc (N), Reason => Reason);
246 Rtyp : Entity_Id;
248 begin
249 if No (Typ) then
250 Rtyp := Etype (N);
251 else
252 Rtyp := Typ;
253 end if;
255 Discard_Node
256 (Compile_Time_Constraint_Error (N, Msg, Ent, Loc, Warn => Warn));
258 if not Rep then
259 return;
260 end if;
262 -- Now we replace the node by an N_Raise_Constraint_Error node
263 -- This does not need reanalyzing, so set it as analyzed now.
265 Rewrite (N, R_Stat);
266 Set_Analyzed (N, True);
268 Set_Etype (N, Rtyp);
269 Set_Raises_Constraint_Error (N);
271 -- If the original expression was marked as static, the result is
272 -- still marked as static, but the Raises_Constraint_Error flag is
273 -- always set so that further static evaluation is not attempted.
275 if Stat then
276 Set_Is_Static_Expression (N);
277 end if;
278 end Apply_Compile_Time_Constraint_Error;
280 --------------------------
281 -- Build_Actual_Subtype --
282 --------------------------
284 function Build_Actual_Subtype
285 (T : Entity_Id;
286 N : Node_Or_Entity_Id) return Node_Id
288 Loc : Source_Ptr;
289 -- Normally Sloc (N), but may point to corresponding body in some cases
291 Constraints : List_Id;
292 Decl : Node_Id;
293 Discr : Entity_Id;
294 Hi : Node_Id;
295 Lo : Node_Id;
296 Subt : Entity_Id;
297 Disc_Type : Entity_Id;
298 Obj : Node_Id;
300 begin
301 Loc := Sloc (N);
303 if Nkind (N) = N_Defining_Identifier then
304 Obj := New_Reference_To (N, Loc);
306 -- If this is a formal parameter of a subprogram declaration, and
307 -- we are compiling the body, we want the declaration for the
308 -- actual subtype to carry the source position of the body, to
309 -- prevent anomalies in gdb when stepping through the code.
311 if Is_Formal (N) then
312 declare
313 Decl : constant Node_Id := Unit_Declaration_Node (Scope (N));
314 begin
315 if Nkind (Decl) = N_Subprogram_Declaration
316 and then Present (Corresponding_Body (Decl))
317 then
318 Loc := Sloc (Corresponding_Body (Decl));
319 end if;
320 end;
321 end if;
323 else
324 Obj := N;
325 end if;
327 if Is_Array_Type (T) then
328 Constraints := New_List;
329 for J in 1 .. Number_Dimensions (T) loop
331 -- Build an array subtype declaration with the nominal subtype and
332 -- the bounds of the actual. Add the declaration in front of the
333 -- local declarations for the subprogram, for analysis before any
334 -- reference to the formal in the body.
336 Lo :=
337 Make_Attribute_Reference (Loc,
338 Prefix =>
339 Duplicate_Subexpr_No_Checks (Obj, Name_Req => True),
340 Attribute_Name => Name_First,
341 Expressions => New_List (
342 Make_Integer_Literal (Loc, J)));
344 Hi :=
345 Make_Attribute_Reference (Loc,
346 Prefix =>
347 Duplicate_Subexpr_No_Checks (Obj, Name_Req => True),
348 Attribute_Name => Name_Last,
349 Expressions => New_List (
350 Make_Integer_Literal (Loc, J)));
352 Append (Make_Range (Loc, Lo, Hi), Constraints);
353 end loop;
355 -- If the type has unknown discriminants there is no constrained
356 -- subtype to build. This is never called for a formal or for a
357 -- lhs, so returning the type is ok ???
359 elsif Has_Unknown_Discriminants (T) then
360 return T;
362 else
363 Constraints := New_List;
365 -- Type T is a generic derived type, inherit the discriminants from
366 -- the parent type.
368 if Is_Private_Type (T)
369 and then No (Full_View (T))
371 -- T was flagged as an error if it was declared as a formal
372 -- derived type with known discriminants. In this case there
373 -- is no need to look at the parent type since T already carries
374 -- its own discriminants.
376 and then not Error_Posted (T)
377 then
378 Disc_Type := Etype (Base_Type (T));
379 else
380 Disc_Type := T;
381 end if;
383 Discr := First_Discriminant (Disc_Type);
384 while Present (Discr) loop
385 Append_To (Constraints,
386 Make_Selected_Component (Loc,
387 Prefix =>
388 Duplicate_Subexpr_No_Checks (Obj),
389 Selector_Name => New_Occurrence_Of (Discr, Loc)));
390 Next_Discriminant (Discr);
391 end loop;
392 end if;
394 Subt :=
395 Make_Defining_Identifier (Loc,
396 Chars => New_Internal_Name ('S'));
397 Set_Is_Internal (Subt);
399 Decl :=
400 Make_Subtype_Declaration (Loc,
401 Defining_Identifier => Subt,
402 Subtype_Indication =>
403 Make_Subtype_Indication (Loc,
404 Subtype_Mark => New_Reference_To (T, Loc),
405 Constraint =>
406 Make_Index_Or_Discriminant_Constraint (Loc,
407 Constraints => Constraints)));
409 Mark_Rewrite_Insertion (Decl);
410 return Decl;
411 end Build_Actual_Subtype;
413 ---------------------------------------
414 -- Build_Actual_Subtype_Of_Component --
415 ---------------------------------------
417 function Build_Actual_Subtype_Of_Component
418 (T : Entity_Id;
419 N : Node_Id) return Node_Id
421 Loc : constant Source_Ptr := Sloc (N);
422 P : constant Node_Id := Prefix (N);
423 D : Elmt_Id;
424 Id : Node_Id;
425 Indx_Type : Entity_Id;
427 Deaccessed_T : Entity_Id;
428 -- This is either a copy of T, or if T is an access type, then it is
429 -- the directly designated type of this access type.
431 function Build_Actual_Array_Constraint return List_Id;
432 -- If one or more of the bounds of the component depends on
433 -- discriminants, build actual constraint using the discriminants
434 -- of the prefix.
436 function Build_Actual_Record_Constraint return List_Id;
437 -- Similar to previous one, for discriminated components constrained
438 -- by the discriminant of the enclosing object.
440 -----------------------------------
441 -- Build_Actual_Array_Constraint --
442 -----------------------------------
444 function Build_Actual_Array_Constraint return List_Id is
445 Constraints : constant List_Id := New_List;
446 Indx : Node_Id;
447 Hi : Node_Id;
448 Lo : Node_Id;
449 Old_Hi : Node_Id;
450 Old_Lo : Node_Id;
452 begin
453 Indx := First_Index (Deaccessed_T);
454 while Present (Indx) loop
455 Old_Lo := Type_Low_Bound (Etype (Indx));
456 Old_Hi := Type_High_Bound (Etype (Indx));
458 if Denotes_Discriminant (Old_Lo) then
459 Lo :=
460 Make_Selected_Component (Loc,
461 Prefix => New_Copy_Tree (P),
462 Selector_Name => New_Occurrence_Of (Entity (Old_Lo), Loc));
464 else
465 Lo := New_Copy_Tree (Old_Lo);
467 -- The new bound will be reanalyzed in the enclosing
468 -- declaration. For literal bounds that come from a type
469 -- declaration, the type of the context must be imposed, so
470 -- insure that analysis will take place. For non-universal
471 -- types this is not strictly necessary.
473 Set_Analyzed (Lo, False);
474 end if;
476 if Denotes_Discriminant (Old_Hi) then
477 Hi :=
478 Make_Selected_Component (Loc,
479 Prefix => New_Copy_Tree (P),
480 Selector_Name => New_Occurrence_Of (Entity (Old_Hi), Loc));
482 else
483 Hi := New_Copy_Tree (Old_Hi);
484 Set_Analyzed (Hi, False);
485 end if;
487 Append (Make_Range (Loc, Lo, Hi), Constraints);
488 Next_Index (Indx);
489 end loop;
491 return Constraints;
492 end Build_Actual_Array_Constraint;
494 ------------------------------------
495 -- Build_Actual_Record_Constraint --
496 ------------------------------------
498 function Build_Actual_Record_Constraint return List_Id is
499 Constraints : constant List_Id := New_List;
500 D : Elmt_Id;
501 D_Val : Node_Id;
503 begin
504 D := First_Elmt (Discriminant_Constraint (Deaccessed_T));
505 while Present (D) loop
506 if Denotes_Discriminant (Node (D)) then
507 D_Val := Make_Selected_Component (Loc,
508 Prefix => New_Copy_Tree (P),
509 Selector_Name => New_Occurrence_Of (Entity (Node (D)), Loc));
511 else
512 D_Val := New_Copy_Tree (Node (D));
513 end if;
515 Append (D_Val, Constraints);
516 Next_Elmt (D);
517 end loop;
519 return Constraints;
520 end Build_Actual_Record_Constraint;
522 -- Start of processing for Build_Actual_Subtype_Of_Component
524 begin
525 -- Why the test for Spec_Expression mode here???
527 if In_Spec_Expression then
528 return Empty;
530 -- More comments for the rest of this body would be good ???
532 elsif Nkind (N) = N_Explicit_Dereference then
533 if Is_Composite_Type (T)
534 and then not Is_Constrained (T)
535 and then not (Is_Class_Wide_Type (T)
536 and then Is_Constrained (Root_Type (T)))
537 and then not Has_Unknown_Discriminants (T)
538 then
539 -- If the type of the dereference is already constrained, it
540 -- is an actual subtype.
542 if Is_Array_Type (Etype (N))
543 and then Is_Constrained (Etype (N))
544 then
545 return Empty;
546 else
547 Remove_Side_Effects (P);
548 return Build_Actual_Subtype (T, N);
549 end if;
550 else
551 return Empty;
552 end if;
553 end if;
555 if Ekind (T) = E_Access_Subtype then
556 Deaccessed_T := Designated_Type (T);
557 else
558 Deaccessed_T := T;
559 end if;
561 if Ekind (Deaccessed_T) = E_Array_Subtype then
562 Id := First_Index (Deaccessed_T);
563 while Present (Id) loop
564 Indx_Type := Underlying_Type (Etype (Id));
566 if Denotes_Discriminant (Type_Low_Bound (Indx_Type))
567 or else
568 Denotes_Discriminant (Type_High_Bound (Indx_Type))
569 then
570 Remove_Side_Effects (P);
571 return
572 Build_Component_Subtype
573 (Build_Actual_Array_Constraint, Loc, Base_Type (T));
574 end if;
576 Next_Index (Id);
577 end loop;
579 elsif Is_Composite_Type (Deaccessed_T)
580 and then Has_Discriminants (Deaccessed_T)
581 and then not Has_Unknown_Discriminants (Deaccessed_T)
582 then
583 D := First_Elmt (Discriminant_Constraint (Deaccessed_T));
584 while Present (D) loop
585 if Denotes_Discriminant (Node (D)) then
586 Remove_Side_Effects (P);
587 return
588 Build_Component_Subtype (
589 Build_Actual_Record_Constraint, Loc, Base_Type (T));
590 end if;
592 Next_Elmt (D);
593 end loop;
594 end if;
596 -- If none of the above, the actual and nominal subtypes are the same
598 return Empty;
599 end Build_Actual_Subtype_Of_Component;
601 -----------------------------
602 -- Build_Component_Subtype --
603 -----------------------------
605 function Build_Component_Subtype
606 (C : List_Id;
607 Loc : Source_Ptr;
608 T : Entity_Id) return Node_Id
610 Subt : Entity_Id;
611 Decl : Node_Id;
613 begin
614 -- Unchecked_Union components do not require component subtypes
616 if Is_Unchecked_Union (T) then
617 return Empty;
618 end if;
620 Subt :=
621 Make_Defining_Identifier (Loc,
622 Chars => New_Internal_Name ('S'));
623 Set_Is_Internal (Subt);
625 Decl :=
626 Make_Subtype_Declaration (Loc,
627 Defining_Identifier => Subt,
628 Subtype_Indication =>
629 Make_Subtype_Indication (Loc,
630 Subtype_Mark => New_Reference_To (Base_Type (T), Loc),
631 Constraint =>
632 Make_Index_Or_Discriminant_Constraint (Loc,
633 Constraints => C)));
635 Mark_Rewrite_Insertion (Decl);
636 return Decl;
637 end Build_Component_Subtype;
639 ---------------------------
640 -- Build_Default_Subtype --
641 ---------------------------
643 function Build_Default_Subtype
644 (T : Entity_Id;
645 N : Node_Id) return Entity_Id
647 Loc : constant Source_Ptr := Sloc (N);
648 Disc : Entity_Id;
650 begin
651 if not Has_Discriminants (T) or else Is_Constrained (T) then
652 return T;
653 end if;
655 Disc := First_Discriminant (T);
657 if No (Discriminant_Default_Value (Disc)) then
658 return T;
659 end if;
661 declare
662 Act : constant Entity_Id :=
663 Make_Defining_Identifier (Loc,
664 Chars => New_Internal_Name ('S'));
666 Constraints : constant List_Id := New_List;
667 Decl : Node_Id;
669 begin
670 while Present (Disc) loop
671 Append_To (Constraints,
672 New_Copy_Tree (Discriminant_Default_Value (Disc)));
673 Next_Discriminant (Disc);
674 end loop;
676 Decl :=
677 Make_Subtype_Declaration (Loc,
678 Defining_Identifier => Act,
679 Subtype_Indication =>
680 Make_Subtype_Indication (Loc,
681 Subtype_Mark => New_Occurrence_Of (T, Loc),
682 Constraint =>
683 Make_Index_Or_Discriminant_Constraint (Loc,
684 Constraints => Constraints)));
686 Insert_Action (N, Decl);
687 Analyze (Decl);
688 return Act;
689 end;
690 end Build_Default_Subtype;
692 --------------------------------------------
693 -- Build_Discriminal_Subtype_Of_Component --
694 --------------------------------------------
696 function Build_Discriminal_Subtype_Of_Component
697 (T : Entity_Id) return Node_Id
699 Loc : constant Source_Ptr := Sloc (T);
700 D : Elmt_Id;
701 Id : Node_Id;
703 function Build_Discriminal_Array_Constraint return List_Id;
704 -- If one or more of the bounds of the component depends on
705 -- discriminants, build actual constraint using the discriminants
706 -- of the prefix.
708 function Build_Discriminal_Record_Constraint return List_Id;
709 -- Similar to previous one, for discriminated components constrained
710 -- by the discriminant of the enclosing object.
712 ----------------------------------------
713 -- Build_Discriminal_Array_Constraint --
714 ----------------------------------------
716 function Build_Discriminal_Array_Constraint return List_Id is
717 Constraints : constant List_Id := New_List;
718 Indx : Node_Id;
719 Hi : Node_Id;
720 Lo : Node_Id;
721 Old_Hi : Node_Id;
722 Old_Lo : Node_Id;
724 begin
725 Indx := First_Index (T);
726 while Present (Indx) loop
727 Old_Lo := Type_Low_Bound (Etype (Indx));
728 Old_Hi := Type_High_Bound (Etype (Indx));
730 if Denotes_Discriminant (Old_Lo) then
731 Lo := New_Occurrence_Of (Discriminal (Entity (Old_Lo)), Loc);
733 else
734 Lo := New_Copy_Tree (Old_Lo);
735 end if;
737 if Denotes_Discriminant (Old_Hi) then
738 Hi := New_Occurrence_Of (Discriminal (Entity (Old_Hi)), Loc);
740 else
741 Hi := New_Copy_Tree (Old_Hi);
742 end if;
744 Append (Make_Range (Loc, Lo, Hi), Constraints);
745 Next_Index (Indx);
746 end loop;
748 return Constraints;
749 end Build_Discriminal_Array_Constraint;
751 -----------------------------------------
752 -- Build_Discriminal_Record_Constraint --
753 -----------------------------------------
755 function Build_Discriminal_Record_Constraint return List_Id is
756 Constraints : constant List_Id := New_List;
757 D : Elmt_Id;
758 D_Val : Node_Id;
760 begin
761 D := First_Elmt (Discriminant_Constraint (T));
762 while Present (D) loop
763 if Denotes_Discriminant (Node (D)) then
764 D_Val :=
765 New_Occurrence_Of (Discriminal (Entity (Node (D))), Loc);
767 else
768 D_Val := New_Copy_Tree (Node (D));
769 end if;
771 Append (D_Val, Constraints);
772 Next_Elmt (D);
773 end loop;
775 return Constraints;
776 end Build_Discriminal_Record_Constraint;
778 -- Start of processing for Build_Discriminal_Subtype_Of_Component
780 begin
781 if Ekind (T) = E_Array_Subtype then
782 Id := First_Index (T);
783 while Present (Id) loop
784 if Denotes_Discriminant (Type_Low_Bound (Etype (Id))) or else
785 Denotes_Discriminant (Type_High_Bound (Etype (Id)))
786 then
787 return Build_Component_Subtype
788 (Build_Discriminal_Array_Constraint, Loc, T);
789 end if;
791 Next_Index (Id);
792 end loop;
794 elsif Ekind (T) = E_Record_Subtype
795 and then Has_Discriminants (T)
796 and then not Has_Unknown_Discriminants (T)
797 then
798 D := First_Elmt (Discriminant_Constraint (T));
799 while Present (D) loop
800 if Denotes_Discriminant (Node (D)) then
801 return Build_Component_Subtype
802 (Build_Discriminal_Record_Constraint, Loc, T);
803 end if;
805 Next_Elmt (D);
806 end loop;
807 end if;
809 -- If none of the above, the actual and nominal subtypes are the same
811 return Empty;
812 end Build_Discriminal_Subtype_Of_Component;
814 ------------------------------
815 -- Build_Elaboration_Entity --
816 ------------------------------
818 procedure Build_Elaboration_Entity (N : Node_Id; Spec_Id : Entity_Id) is
819 Loc : constant Source_Ptr := Sloc (N);
820 Decl : Node_Id;
821 Elab_Ent : Entity_Id;
823 procedure Set_Package_Name (Ent : Entity_Id);
824 -- Given an entity, sets the fully qualified name of the entity in
825 -- Name_Buffer, with components separated by double underscores. This
826 -- is a recursive routine that climbs the scope chain to Standard.
828 ----------------------
829 -- Set_Package_Name --
830 ----------------------
832 procedure Set_Package_Name (Ent : Entity_Id) is
833 begin
834 if Scope (Ent) /= Standard_Standard then
835 Set_Package_Name (Scope (Ent));
837 declare
838 Nam : constant String := Get_Name_String (Chars (Ent));
839 begin
840 Name_Buffer (Name_Len + 1) := '_';
841 Name_Buffer (Name_Len + 2) := '_';
842 Name_Buffer (Name_Len + 3 .. Name_Len + Nam'Length + 2) := Nam;
843 Name_Len := Name_Len + Nam'Length + 2;
844 end;
846 else
847 Get_Name_String (Chars (Ent));
848 end if;
849 end Set_Package_Name;
851 -- Start of processing for Build_Elaboration_Entity
853 begin
854 -- Ignore if already constructed
856 if Present (Elaboration_Entity (Spec_Id)) then
857 return;
858 end if;
860 -- Construct name of elaboration entity as xxx_E, where xxx is the unit
861 -- name with dots replaced by double underscore. We have to manually
862 -- construct this name, since it will be elaborated in the outer scope,
863 -- and thus will not have the unit name automatically prepended.
865 Set_Package_Name (Spec_Id);
867 -- Append _E
869 Name_Buffer (Name_Len + 1) := '_';
870 Name_Buffer (Name_Len + 2) := 'E';
871 Name_Len := Name_Len + 2;
873 -- Create elaboration flag
875 Elab_Ent :=
876 Make_Defining_Identifier (Loc, Chars => Name_Find);
877 Set_Elaboration_Entity (Spec_Id, Elab_Ent);
879 Decl :=
880 Make_Object_Declaration (Loc,
881 Defining_Identifier => Elab_Ent,
882 Object_Definition =>
883 New_Occurrence_Of (Standard_Boolean, Loc),
884 Expression =>
885 New_Occurrence_Of (Standard_False, Loc));
887 Push_Scope (Standard_Standard);
888 Add_Global_Declaration (Decl);
889 Pop_Scope;
891 -- Reset True_Constant indication, since we will indeed assign a value
892 -- to the variable in the binder main. We also kill the Current_Value
893 -- and Last_Assignment fields for the same reason.
895 Set_Is_True_Constant (Elab_Ent, False);
896 Set_Current_Value (Elab_Ent, Empty);
897 Set_Last_Assignment (Elab_Ent, Empty);
899 -- We do not want any further qualification of the name (if we did
900 -- not do this, we would pick up the name of the generic package
901 -- in the case of a library level generic instantiation).
903 Set_Has_Qualified_Name (Elab_Ent);
904 Set_Has_Fully_Qualified_Name (Elab_Ent);
905 end Build_Elaboration_Entity;
907 -----------------------------------
908 -- Cannot_Raise_Constraint_Error --
909 -----------------------------------
911 function Cannot_Raise_Constraint_Error (Expr : Node_Id) return Boolean is
912 begin
913 if Compile_Time_Known_Value (Expr) then
914 return True;
916 elsif Do_Range_Check (Expr) then
917 return False;
919 elsif Raises_Constraint_Error (Expr) then
920 return False;
922 else
923 case Nkind (Expr) is
924 when N_Identifier =>
925 return True;
927 when N_Expanded_Name =>
928 return True;
930 when N_Selected_Component =>
931 return not Do_Discriminant_Check (Expr);
933 when N_Attribute_Reference =>
934 if Do_Overflow_Check (Expr) then
935 return False;
937 elsif No (Expressions (Expr)) then
938 return True;
940 else
941 declare
942 N : Node_Id;
944 begin
945 N := First (Expressions (Expr));
946 while Present (N) loop
947 if Cannot_Raise_Constraint_Error (N) then
948 Next (N);
949 else
950 return False;
951 end if;
952 end loop;
954 return True;
955 end;
956 end if;
958 when N_Type_Conversion =>
959 if Do_Overflow_Check (Expr)
960 or else Do_Length_Check (Expr)
961 or else Do_Tag_Check (Expr)
962 then
963 return False;
964 else
965 return
966 Cannot_Raise_Constraint_Error (Expression (Expr));
967 end if;
969 when N_Unchecked_Type_Conversion =>
970 return Cannot_Raise_Constraint_Error (Expression (Expr));
972 when N_Unary_Op =>
973 if Do_Overflow_Check (Expr) then
974 return False;
975 else
976 return
977 Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
978 end if;
980 when N_Op_Divide |
981 N_Op_Mod |
982 N_Op_Rem
984 if Do_Division_Check (Expr)
985 or else Do_Overflow_Check (Expr)
986 then
987 return False;
988 else
989 return
990 Cannot_Raise_Constraint_Error (Left_Opnd (Expr))
991 and then
992 Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
993 end if;
995 when N_Op_Add |
996 N_Op_And |
997 N_Op_Concat |
998 N_Op_Eq |
999 N_Op_Expon |
1000 N_Op_Ge |
1001 N_Op_Gt |
1002 N_Op_Le |
1003 N_Op_Lt |
1004 N_Op_Multiply |
1005 N_Op_Ne |
1006 N_Op_Or |
1007 N_Op_Rotate_Left |
1008 N_Op_Rotate_Right |
1009 N_Op_Shift_Left |
1010 N_Op_Shift_Right |
1011 N_Op_Shift_Right_Arithmetic |
1012 N_Op_Subtract |
1013 N_Op_Xor
1015 if Do_Overflow_Check (Expr) then
1016 return False;
1017 else
1018 return
1019 Cannot_Raise_Constraint_Error (Left_Opnd (Expr))
1020 and then
1021 Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
1022 end if;
1024 when others =>
1025 return False;
1026 end case;
1027 end if;
1028 end Cannot_Raise_Constraint_Error;
1030 --------------------------
1031 -- Check_Fully_Declared --
1032 --------------------------
1034 procedure Check_Fully_Declared (T : Entity_Id; N : Node_Id) is
1035 begin
1036 if Ekind (T) = E_Incomplete_Type then
1038 -- Ada 2005 (AI-50217): If the type is available through a limited
1039 -- with_clause, verify that its full view has been analyzed.
1041 if From_With_Type (T)
1042 and then Present (Non_Limited_View (T))
1043 and then Ekind (Non_Limited_View (T)) /= E_Incomplete_Type
1044 then
1045 -- The non-limited view is fully declared
1046 null;
1048 else
1049 Error_Msg_NE
1050 ("premature usage of incomplete}", N, First_Subtype (T));
1051 end if;
1053 -- Need comments for these tests ???
1055 elsif Has_Private_Component (T)
1056 and then not Is_Generic_Type (Root_Type (T))
1057 and then not In_Spec_Expression
1058 then
1059 -- Special case: if T is the anonymous type created for a single
1060 -- task or protected object, use the name of the source object.
1062 if Is_Concurrent_Type (T)
1063 and then not Comes_From_Source (T)
1064 and then Nkind (N) = N_Object_Declaration
1065 then
1066 Error_Msg_NE ("type of& has incomplete component", N,
1067 Defining_Identifier (N));
1069 else
1070 Error_Msg_NE
1071 ("premature usage of incomplete}", N, First_Subtype (T));
1072 end if;
1073 end if;
1074 end Check_Fully_Declared;
1076 -------------------------
1077 -- Check_Nested_Access --
1078 -------------------------
1080 procedure Check_Nested_Access (Ent : Entity_Id) is
1081 Scop : constant Entity_Id := Current_Scope;
1082 Current_Subp : Entity_Id;
1083 Enclosing : Entity_Id;
1085 begin
1086 -- Currently only enabled for VM back-ends for efficiency, should we
1087 -- enable it more systematically ???
1089 -- Check for Is_Imported needs commenting below ???
1091 if VM_Target /= No_VM
1092 and then (Ekind (Ent) = E_Variable
1093 or else
1094 Ekind (Ent) = E_Constant
1095 or else
1096 Ekind (Ent) = E_Loop_Parameter)
1097 and then Scope (Ent) /= Empty
1098 and then not Is_Library_Level_Entity (Ent)
1099 and then not Is_Imported (Ent)
1100 then
1101 if Is_Subprogram (Scop)
1102 or else Is_Generic_Subprogram (Scop)
1103 or else Is_Entry (Scop)
1104 then
1105 Current_Subp := Scop;
1106 else
1107 Current_Subp := Current_Subprogram;
1108 end if;
1110 Enclosing := Enclosing_Subprogram (Ent);
1112 if Enclosing /= Empty
1113 and then Enclosing /= Current_Subp
1114 then
1115 Set_Has_Up_Level_Access (Ent, True);
1116 end if;
1117 end if;
1118 end Check_Nested_Access;
1120 ------------------------------------------
1121 -- Check_Potentially_Blocking_Operation --
1122 ------------------------------------------
1124 procedure Check_Potentially_Blocking_Operation (N : Node_Id) is
1125 S : Entity_Id;
1126 begin
1127 -- N is one of the potentially blocking operations listed in 9.5.1(8).
1128 -- When pragma Detect_Blocking is active, the run time will raise
1129 -- Program_Error. Here we only issue a warning, since we generally
1130 -- support the use of potentially blocking operations in the absence
1131 -- of the pragma.
1133 -- Indirect blocking through a subprogram call cannot be diagnosed
1134 -- statically without interprocedural analysis, so we do not attempt
1135 -- to do it here.
1137 S := Scope (Current_Scope);
1138 while Present (S) and then S /= Standard_Standard loop
1139 if Is_Protected_Type (S) then
1140 Error_Msg_N
1141 ("potentially blocking operation in protected operation?", N);
1143 return;
1144 end if;
1146 S := Scope (S);
1147 end loop;
1148 end Check_Potentially_Blocking_Operation;
1150 ------------------------------
1151 -- Check_Unprotected_Access --
1152 ------------------------------
1154 procedure Check_Unprotected_Access
1155 (Context : Node_Id;
1156 Expr : Node_Id)
1158 Cont_Encl_Typ : Entity_Id;
1159 Pref_Encl_Typ : Entity_Id;
1161 function Enclosing_Protected_Type (Obj : Node_Id) return Entity_Id;
1162 -- Check whether Obj is a private component of a protected object.
1163 -- Return the protected type where the component resides, Empty
1164 -- otherwise.
1166 function Is_Public_Operation return Boolean;
1167 -- Verify that the enclosing operation is callable from outside the
1168 -- protected object, to minimize false positives.
1170 ------------------------------
1171 -- Enclosing_Protected_Type --
1172 ------------------------------
1174 function Enclosing_Protected_Type (Obj : Node_Id) return Entity_Id is
1175 begin
1176 if Is_Entity_Name (Obj) then
1177 declare
1178 Ent : Entity_Id := Entity (Obj);
1180 begin
1181 -- The object can be a renaming of a private component, use
1182 -- the original record component.
1184 if Is_Prival (Ent) then
1185 Ent := Prival_Link (Ent);
1186 end if;
1188 if Is_Protected_Type (Scope (Ent)) then
1189 return Scope (Ent);
1190 end if;
1191 end;
1192 end if;
1194 -- For indexed and selected components, recursively check the prefix
1196 if Nkind_In (Obj, N_Indexed_Component, N_Selected_Component) then
1197 return Enclosing_Protected_Type (Prefix (Obj));
1199 -- The object does not denote a protected component
1201 else
1202 return Empty;
1203 end if;
1204 end Enclosing_Protected_Type;
1206 -------------------------
1207 -- Is_Public_Operation --
1208 -------------------------
1210 function Is_Public_Operation return Boolean is
1211 S : Entity_Id;
1212 E : Entity_Id;
1214 begin
1215 S := Current_Scope;
1216 while Present (S)
1217 and then S /= Pref_Encl_Typ
1218 loop
1219 if Scope (S) = Pref_Encl_Typ then
1220 E := First_Entity (Pref_Encl_Typ);
1221 while Present (E)
1222 and then E /= First_Private_Entity (Pref_Encl_Typ)
1223 loop
1224 if E = S then
1225 return True;
1226 end if;
1227 Next_Entity (E);
1228 end loop;
1229 end if;
1231 S := Scope (S);
1232 end loop;
1234 return False;
1235 end Is_Public_Operation;
1237 -- Start of processing for Check_Unprotected_Access
1239 begin
1240 if Nkind (Expr) = N_Attribute_Reference
1241 and then Attribute_Name (Expr) = Name_Unchecked_Access
1242 then
1243 Cont_Encl_Typ := Enclosing_Protected_Type (Context);
1244 Pref_Encl_Typ := Enclosing_Protected_Type (Prefix (Expr));
1246 -- Check whether we are trying to export a protected component to a
1247 -- context with an equal or lower access level.
1249 if Present (Pref_Encl_Typ)
1250 and then No (Cont_Encl_Typ)
1251 and then Is_Public_Operation
1252 and then Scope_Depth (Pref_Encl_Typ) >=
1253 Object_Access_Level (Context)
1254 then
1255 Error_Msg_N
1256 ("?possible unprotected access to protected data", Expr);
1257 end if;
1258 end if;
1259 end Check_Unprotected_Access;
1261 ---------------
1262 -- Check_VMS --
1263 ---------------
1265 procedure Check_VMS (Construct : Node_Id) is
1266 begin
1267 if not OpenVMS_On_Target then
1268 Error_Msg_N
1269 ("this construct is allowed only in Open'V'M'S", Construct);
1270 end if;
1271 end Check_VMS;
1273 ------------------------
1274 -- Collect_Interfaces --
1275 ------------------------
1277 procedure Collect_Interfaces
1278 (T : Entity_Id;
1279 Ifaces_List : out Elist_Id;
1280 Exclude_Parents : Boolean := False;
1281 Use_Full_View : Boolean := True)
1283 procedure Collect (Typ : Entity_Id);
1284 -- Subsidiary subprogram used to traverse the whole list
1285 -- of directly and indirectly implemented interfaces
1287 -------------
1288 -- Collect --
1289 -------------
1291 procedure Collect (Typ : Entity_Id) is
1292 Ancestor : Entity_Id;
1293 Full_T : Entity_Id;
1294 Id : Node_Id;
1295 Iface : Entity_Id;
1297 begin
1298 Full_T := Typ;
1300 -- Handle private types
1302 if Use_Full_View
1303 and then Is_Private_Type (Typ)
1304 and then Present (Full_View (Typ))
1305 then
1306 Full_T := Full_View (Typ);
1307 end if;
1309 -- Include the ancestor if we are generating the whole list of
1310 -- abstract interfaces.
1312 if Etype (Full_T) /= Typ
1314 -- Protect the frontend against wrong sources. For example:
1316 -- package P is
1317 -- type A is tagged null record;
1318 -- type B is new A with private;
1319 -- type C is new A with private;
1320 -- private
1321 -- type B is new C with null record;
1322 -- type C is new B with null record;
1323 -- end P;
1325 and then Etype (Full_T) /= T
1326 then
1327 Ancestor := Etype (Full_T);
1328 Collect (Ancestor);
1330 if Is_Interface (Ancestor)
1331 and then not Exclude_Parents
1332 then
1333 Append_Unique_Elmt (Ancestor, Ifaces_List);
1334 end if;
1335 end if;
1337 -- Traverse the graph of ancestor interfaces
1339 if Is_Non_Empty_List (Abstract_Interface_List (Full_T)) then
1340 Id := First (Abstract_Interface_List (Full_T));
1341 while Present (Id) loop
1342 Iface := Etype (Id);
1344 -- Protect against wrong uses. For example:
1345 -- type I is interface;
1346 -- type O is tagged null record;
1347 -- type Wrong is new I and O with null record; -- ERROR
1349 if Is_Interface (Iface) then
1350 if Exclude_Parents
1351 and then Etype (T) /= T
1352 and then Interface_Present_In_Ancestor (Etype (T), Iface)
1353 then
1354 null;
1355 else
1356 Collect (Iface);
1357 Append_Unique_Elmt (Iface, Ifaces_List);
1358 end if;
1359 end if;
1361 Next (Id);
1362 end loop;
1363 end if;
1364 end Collect;
1366 -- Start of processing for Collect_Interfaces
1368 begin
1369 pragma Assert (Is_Tagged_Type (T) or else Is_Concurrent_Type (T));
1370 Ifaces_List := New_Elmt_List;
1371 Collect (T);
1372 end Collect_Interfaces;
1374 ----------------------------------
1375 -- Collect_Interface_Components --
1376 ----------------------------------
1378 procedure Collect_Interface_Components
1379 (Tagged_Type : Entity_Id;
1380 Components_List : out Elist_Id)
1382 procedure Collect (Typ : Entity_Id);
1383 -- Subsidiary subprogram used to climb to the parents
1385 -------------
1386 -- Collect --
1387 -------------
1389 procedure Collect (Typ : Entity_Id) is
1390 Tag_Comp : Entity_Id;
1391 Parent_Typ : Entity_Id;
1393 begin
1394 -- Handle private types
1396 if Present (Full_View (Etype (Typ))) then
1397 Parent_Typ := Full_View (Etype (Typ));
1398 else
1399 Parent_Typ := Etype (Typ);
1400 end if;
1402 if Parent_Typ /= Typ
1404 -- Protect the frontend against wrong sources. For example:
1406 -- package P is
1407 -- type A is tagged null record;
1408 -- type B is new A with private;
1409 -- type C is new A with private;
1410 -- private
1411 -- type B is new C with null record;
1412 -- type C is new B with null record;
1413 -- end P;
1415 and then Parent_Typ /= Tagged_Type
1416 then
1417 Collect (Parent_Typ);
1418 end if;
1420 -- Collect the components containing tags of secondary dispatch
1421 -- tables.
1423 Tag_Comp := Next_Tag_Component (First_Tag_Component (Typ));
1424 while Present (Tag_Comp) loop
1425 pragma Assert (Present (Related_Type (Tag_Comp)));
1426 Append_Elmt (Tag_Comp, Components_List);
1428 Tag_Comp := Next_Tag_Component (Tag_Comp);
1429 end loop;
1430 end Collect;
1432 -- Start of processing for Collect_Interface_Components
1434 begin
1435 pragma Assert (Ekind (Tagged_Type) = E_Record_Type
1436 and then Is_Tagged_Type (Tagged_Type));
1438 Components_List := New_Elmt_List;
1439 Collect (Tagged_Type);
1440 end Collect_Interface_Components;
1442 -----------------------------
1443 -- Collect_Interfaces_Info --
1444 -----------------------------
1446 procedure Collect_Interfaces_Info
1447 (T : Entity_Id;
1448 Ifaces_List : out Elist_Id;
1449 Components_List : out Elist_Id;
1450 Tags_List : out Elist_Id)
1452 Comps_List : Elist_Id;
1453 Comp_Elmt : Elmt_Id;
1454 Comp_Iface : Entity_Id;
1455 Iface_Elmt : Elmt_Id;
1456 Iface : Entity_Id;
1458 function Search_Tag (Iface : Entity_Id) return Entity_Id;
1459 -- Search for the secondary tag associated with the interface type
1460 -- Iface that is implemented by T.
1462 ----------------
1463 -- Search_Tag --
1464 ----------------
1466 function Search_Tag (Iface : Entity_Id) return Entity_Id is
1467 ADT : Elmt_Id;
1469 begin
1470 ADT := Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (T))));
1471 while Present (ADT)
1472 and then Ekind (Node (ADT)) = E_Constant
1473 and then Related_Type (Node (ADT)) /= Iface
1474 loop
1475 -- Skip the secondary dispatch tables of Iface
1477 Next_Elmt (ADT);
1478 Next_Elmt (ADT);
1479 Next_Elmt (ADT);
1480 Next_Elmt (ADT);
1481 end loop;
1483 pragma Assert (Ekind (Node (ADT)) = E_Constant);
1484 return Node (ADT);
1485 end Search_Tag;
1487 -- Start of processing for Collect_Interfaces_Info
1489 begin
1490 Collect_Interfaces (T, Ifaces_List);
1491 Collect_Interface_Components (T, Comps_List);
1493 -- Search for the record component and tag associated with each
1494 -- interface type of T.
1496 Components_List := New_Elmt_List;
1497 Tags_List := New_Elmt_List;
1499 Iface_Elmt := First_Elmt (Ifaces_List);
1500 while Present (Iface_Elmt) loop
1501 Iface := Node (Iface_Elmt);
1503 -- Associate the primary tag component and the primary dispatch table
1504 -- with all the interfaces that are parents of T
1506 if Is_Ancestor (Iface, T) then
1507 Append_Elmt (First_Tag_Component (T), Components_List);
1508 Append_Elmt (Node (First_Elmt (Access_Disp_Table (T))), Tags_List);
1510 -- Otherwise search for the tag component and secondary dispatch
1511 -- table of Iface
1513 else
1514 Comp_Elmt := First_Elmt (Comps_List);
1515 while Present (Comp_Elmt) loop
1516 Comp_Iface := Related_Type (Node (Comp_Elmt));
1518 if Comp_Iface = Iface
1519 or else Is_Ancestor (Iface, Comp_Iface)
1520 then
1521 Append_Elmt (Node (Comp_Elmt), Components_List);
1522 Append_Elmt (Search_Tag (Comp_Iface), Tags_List);
1523 exit;
1524 end if;
1526 Next_Elmt (Comp_Elmt);
1527 end loop;
1528 pragma Assert (Present (Comp_Elmt));
1529 end if;
1531 Next_Elmt (Iface_Elmt);
1532 end loop;
1533 end Collect_Interfaces_Info;
1535 ----------------------------------
1536 -- Collect_Primitive_Operations --
1537 ----------------------------------
1539 function Collect_Primitive_Operations (T : Entity_Id) return Elist_Id is
1540 B_Type : constant Entity_Id := Base_Type (T);
1541 B_Decl : constant Node_Id := Original_Node (Parent (B_Type));
1542 B_Scope : Entity_Id := Scope (B_Type);
1543 Op_List : Elist_Id;
1544 Formal : Entity_Id;
1545 Is_Prim : Boolean;
1546 Formal_Derived : Boolean := False;
1547 Id : Entity_Id;
1549 begin
1550 -- For tagged types, the primitive operations are collected as they
1551 -- are declared, and held in an explicit list which is simply returned.
1553 if Is_Tagged_Type (B_Type) then
1554 return Primitive_Operations (B_Type);
1556 -- An untagged generic type that is a derived type inherits the
1557 -- primitive operations of its parent type. Other formal types only
1558 -- have predefined operators, which are not explicitly represented.
1560 elsif Is_Generic_Type (B_Type) then
1561 if Nkind (B_Decl) = N_Formal_Type_Declaration
1562 and then Nkind (Formal_Type_Definition (B_Decl))
1563 = N_Formal_Derived_Type_Definition
1564 then
1565 Formal_Derived := True;
1566 else
1567 return New_Elmt_List;
1568 end if;
1569 end if;
1571 Op_List := New_Elmt_List;
1573 if B_Scope = Standard_Standard then
1574 if B_Type = Standard_String then
1575 Append_Elmt (Standard_Op_Concat, Op_List);
1577 elsif B_Type = Standard_Wide_String then
1578 Append_Elmt (Standard_Op_Concatw, Op_List);
1580 else
1581 null;
1582 end if;
1584 elsif (Is_Package_Or_Generic_Package (B_Scope)
1585 and then
1586 Nkind (Parent (Declaration_Node (First_Subtype (T)))) /=
1587 N_Package_Body)
1588 or else Is_Derived_Type (B_Type)
1589 then
1590 -- The primitive operations appear after the base type, except
1591 -- if the derivation happens within the private part of B_Scope
1592 -- and the type is a private type, in which case both the type
1593 -- and some primitive operations may appear before the base
1594 -- type, and the list of candidates starts after the type.
1596 if In_Open_Scopes (B_Scope)
1597 and then Scope (T) = B_Scope
1598 and then In_Private_Part (B_Scope)
1599 then
1600 Id := Next_Entity (T);
1601 else
1602 Id := Next_Entity (B_Type);
1603 end if;
1605 while Present (Id) loop
1607 -- Note that generic formal subprograms are not
1608 -- considered to be primitive operations and thus
1609 -- are never inherited.
1611 if Is_Overloadable (Id)
1612 and then Nkind (Parent (Parent (Id)))
1613 not in N_Formal_Subprogram_Declaration
1614 then
1615 Is_Prim := False;
1617 if Base_Type (Etype (Id)) = B_Type then
1618 Is_Prim := True;
1619 else
1620 Formal := First_Formal (Id);
1621 while Present (Formal) loop
1622 if Base_Type (Etype (Formal)) = B_Type then
1623 Is_Prim := True;
1624 exit;
1626 elsif Ekind (Etype (Formal)) = E_Anonymous_Access_Type
1627 and then Base_Type
1628 (Designated_Type (Etype (Formal))) = B_Type
1629 then
1630 Is_Prim := True;
1631 exit;
1632 end if;
1634 Next_Formal (Formal);
1635 end loop;
1636 end if;
1638 -- For a formal derived type, the only primitives are the
1639 -- ones inherited from the parent type. Operations appearing
1640 -- in the package declaration are not primitive for it.
1642 if Is_Prim
1643 and then (not Formal_Derived
1644 or else Present (Alias (Id)))
1645 then
1646 Append_Elmt (Id, Op_List);
1647 end if;
1648 end if;
1650 Next_Entity (Id);
1652 -- For a type declared in System, some of its operations
1653 -- may appear in the target-specific extension to System.
1655 if No (Id)
1656 and then Chars (B_Scope) = Name_System
1657 and then Scope (B_Scope) = Standard_Standard
1658 and then Present_System_Aux
1659 then
1660 B_Scope := System_Aux_Id;
1661 Id := First_Entity (System_Aux_Id);
1662 end if;
1663 end loop;
1664 end if;
1666 return Op_List;
1667 end Collect_Primitive_Operations;
1669 -----------------------------------
1670 -- Compile_Time_Constraint_Error --
1671 -----------------------------------
1673 function Compile_Time_Constraint_Error
1674 (N : Node_Id;
1675 Msg : String;
1676 Ent : Entity_Id := Empty;
1677 Loc : Source_Ptr := No_Location;
1678 Warn : Boolean := False) return Node_Id
1680 Msgc : String (1 .. Msg'Length + 2);
1681 -- Copy of message, with room for possible ? and ! at end
1683 Msgl : Natural;
1684 Wmsg : Boolean;
1685 P : Node_Id;
1686 OldP : Node_Id;
1687 Msgs : Boolean;
1688 Eloc : Source_Ptr;
1690 begin
1691 -- A static constraint error in an instance body is not a fatal error.
1692 -- we choose to inhibit the message altogether, because there is no
1693 -- obvious node (for now) on which to post it. On the other hand the
1694 -- offending node must be replaced with a constraint_error in any case.
1696 -- No messages are generated if we already posted an error on this node
1698 if not Error_Posted (N) then
1699 if Loc /= No_Location then
1700 Eloc := Loc;
1701 else
1702 Eloc := Sloc (N);
1703 end if;
1705 Msgc (1 .. Msg'Length) := Msg;
1706 Msgl := Msg'Length;
1708 -- Message is a warning, even in Ada 95 case
1710 if Msg (Msg'Last) = '?' then
1711 Wmsg := True;
1713 -- In Ada 83, all messages are warnings. In the private part and
1714 -- the body of an instance, constraint_checks are only warnings.
1715 -- We also make this a warning if the Warn parameter is set.
1717 elsif Warn
1718 or else (Ada_Version = Ada_83 and then Comes_From_Source (N))
1719 then
1720 Msgl := Msgl + 1;
1721 Msgc (Msgl) := '?';
1722 Wmsg := True;
1724 elsif In_Instance_Not_Visible then
1725 Msgl := Msgl + 1;
1726 Msgc (Msgl) := '?';
1727 Wmsg := True;
1729 -- Otherwise we have a real error message (Ada 95 static case)
1730 -- and we make this an unconditional message. Note that in the
1731 -- warning case we do not make the message unconditional, it seems
1732 -- quite reasonable to delete messages like this (about exceptions
1733 -- that will be raised) in dead code.
1735 else
1736 Wmsg := False;
1737 Msgl := Msgl + 1;
1738 Msgc (Msgl) := '!';
1739 end if;
1741 -- Should we generate a warning? The answer is not quite yes. The
1742 -- very annoying exception occurs in the case of a short circuit
1743 -- operator where the left operand is static and decisive. Climb
1744 -- parents to see if that is the case we have here. Conditional
1745 -- expressions with decisive conditions are a similar situation.
1747 Msgs := True;
1748 P := N;
1749 loop
1750 OldP := P;
1751 P := Parent (P);
1753 -- And then with False as left operand
1755 if Nkind (P) = N_And_Then
1756 and then Compile_Time_Known_Value (Left_Opnd (P))
1757 and then Is_False (Expr_Value (Left_Opnd (P)))
1758 then
1759 Msgs := False;
1760 exit;
1762 -- OR ELSE with True as left operand
1764 elsif Nkind (P) = N_Or_Else
1765 and then Compile_Time_Known_Value (Left_Opnd (P))
1766 and then Is_True (Expr_Value (Left_Opnd (P)))
1767 then
1768 Msgs := False;
1769 exit;
1771 -- Conditional expression
1773 elsif Nkind (P) = N_Conditional_Expression then
1774 declare
1775 Cond : constant Node_Id := First (Expressions (P));
1776 Texp : constant Node_Id := Next (Cond);
1777 Fexp : constant Node_Id := Next (Texp);
1779 begin
1780 if Compile_Time_Known_Value (Cond) then
1782 -- Condition is True and we are in the right operand
1784 if Is_True (Expr_Value (Cond))
1785 and then OldP = Fexp
1786 then
1787 Msgs := False;
1788 exit;
1790 -- Condition is False and we are in the left operand
1792 elsif Is_False (Expr_Value (Cond))
1793 and then OldP = Texp
1794 then
1795 Msgs := False;
1796 exit;
1797 end if;
1798 end if;
1799 end;
1801 -- Special case for component association in aggregates, where
1802 -- we want to keep climbing up to the parent aggregate.
1804 elsif Nkind (P) = N_Component_Association
1805 and then Nkind (Parent (P)) = N_Aggregate
1806 then
1807 null;
1809 -- Keep going if within subexpression
1811 else
1812 exit when Nkind (P) not in N_Subexpr;
1813 end if;
1814 end loop;
1816 if Msgs then
1817 if Present (Ent) then
1818 Error_Msg_NEL (Msgc (1 .. Msgl), N, Ent, Eloc);
1819 else
1820 Error_Msg_NEL (Msgc (1 .. Msgl), N, Etype (N), Eloc);
1821 end if;
1823 if Wmsg then
1824 if Inside_Init_Proc then
1825 Error_Msg_NEL
1826 ("\?& will be raised for objects of this type",
1827 N, Standard_Constraint_Error, Eloc);
1828 else
1829 Error_Msg_NEL
1830 ("\?& will be raised at run time",
1831 N, Standard_Constraint_Error, Eloc);
1832 end if;
1834 else
1835 Error_Msg
1836 ("\static expression fails Constraint_Check", Eloc);
1837 Set_Error_Posted (N);
1838 end if;
1839 end if;
1840 end if;
1842 return N;
1843 end Compile_Time_Constraint_Error;
1845 -----------------------
1846 -- Conditional_Delay --
1847 -----------------------
1849 procedure Conditional_Delay (New_Ent, Old_Ent : Entity_Id) is
1850 begin
1851 if Has_Delayed_Freeze (Old_Ent) and then not Is_Frozen (Old_Ent) then
1852 Set_Has_Delayed_Freeze (New_Ent);
1853 end if;
1854 end Conditional_Delay;
1856 -------------------------
1857 -- Copy_Parameter_List --
1858 -------------------------
1860 function Copy_Parameter_List (Subp_Id : Entity_Id) return List_Id is
1861 Loc : constant Source_Ptr := Sloc (Subp_Id);
1862 Plist : List_Id;
1863 Formal : Entity_Id;
1865 begin
1866 if No (First_Formal (Subp_Id)) then
1867 return No_List;
1868 else
1869 Plist := New_List;
1870 Formal := First_Formal (Subp_Id);
1871 while Present (Formal) loop
1872 Append
1873 (Make_Parameter_Specification (Loc,
1874 Defining_Identifier =>
1875 Make_Defining_Identifier (Sloc (Formal),
1876 Chars => Chars (Formal)),
1877 In_Present => In_Present (Parent (Formal)),
1878 Out_Present => Out_Present (Parent (Formal)),
1879 Parameter_Type =>
1880 New_Reference_To (Etype (Formal), Loc),
1881 Expression =>
1882 New_Copy_Tree (Expression (Parent (Formal)))),
1883 Plist);
1885 Next_Formal (Formal);
1886 end loop;
1887 end if;
1889 return Plist;
1890 end Copy_Parameter_List;
1892 --------------------
1893 -- Current_Entity --
1894 --------------------
1896 -- The currently visible definition for a given identifier is the
1897 -- one most chained at the start of the visibility chain, i.e. the
1898 -- one that is referenced by the Node_Id value of the name of the
1899 -- given identifier.
1901 function Current_Entity (N : Node_Id) return Entity_Id is
1902 begin
1903 return Get_Name_Entity_Id (Chars (N));
1904 end Current_Entity;
1906 -----------------------------
1907 -- Current_Entity_In_Scope --
1908 -----------------------------
1910 function Current_Entity_In_Scope (N : Node_Id) return Entity_Id is
1911 E : Entity_Id;
1912 CS : constant Entity_Id := Current_Scope;
1914 Transient_Case : constant Boolean := Scope_Is_Transient;
1916 begin
1917 E := Get_Name_Entity_Id (Chars (N));
1918 while Present (E)
1919 and then Scope (E) /= CS
1920 and then (not Transient_Case or else Scope (E) /= Scope (CS))
1921 loop
1922 E := Homonym (E);
1923 end loop;
1925 return E;
1926 end Current_Entity_In_Scope;
1928 -------------------
1929 -- Current_Scope --
1930 -------------------
1932 function Current_Scope return Entity_Id is
1933 begin
1934 if Scope_Stack.Last = -1 then
1935 return Standard_Standard;
1936 else
1937 declare
1938 C : constant Entity_Id :=
1939 Scope_Stack.Table (Scope_Stack.Last).Entity;
1940 begin
1941 if Present (C) then
1942 return C;
1943 else
1944 return Standard_Standard;
1945 end if;
1946 end;
1947 end if;
1948 end Current_Scope;
1950 ------------------------
1951 -- Current_Subprogram --
1952 ------------------------
1954 function Current_Subprogram return Entity_Id is
1955 Scop : constant Entity_Id := Current_Scope;
1956 begin
1957 if Is_Subprogram (Scop) or else Is_Generic_Subprogram (Scop) then
1958 return Scop;
1959 else
1960 return Enclosing_Subprogram (Scop);
1961 end if;
1962 end Current_Subprogram;
1964 ---------------------
1965 -- Defining_Entity --
1966 ---------------------
1968 function Defining_Entity (N : Node_Id) return Entity_Id is
1969 K : constant Node_Kind := Nkind (N);
1970 Err : Entity_Id := Empty;
1972 begin
1973 case K is
1974 when
1975 N_Subprogram_Declaration |
1976 N_Abstract_Subprogram_Declaration |
1977 N_Subprogram_Body |
1978 N_Package_Declaration |
1979 N_Subprogram_Renaming_Declaration |
1980 N_Subprogram_Body_Stub |
1981 N_Generic_Subprogram_Declaration |
1982 N_Generic_Package_Declaration |
1983 N_Formal_Subprogram_Declaration
1985 return Defining_Entity (Specification (N));
1987 when
1988 N_Component_Declaration |
1989 N_Defining_Program_Unit_Name |
1990 N_Discriminant_Specification |
1991 N_Entry_Body |
1992 N_Entry_Declaration |
1993 N_Entry_Index_Specification |
1994 N_Exception_Declaration |
1995 N_Exception_Renaming_Declaration |
1996 N_Formal_Object_Declaration |
1997 N_Formal_Package_Declaration |
1998 N_Formal_Type_Declaration |
1999 N_Full_Type_Declaration |
2000 N_Implicit_Label_Declaration |
2001 N_Incomplete_Type_Declaration |
2002 N_Loop_Parameter_Specification |
2003 N_Number_Declaration |
2004 N_Object_Declaration |
2005 N_Object_Renaming_Declaration |
2006 N_Package_Body_Stub |
2007 N_Parameter_Specification |
2008 N_Private_Extension_Declaration |
2009 N_Private_Type_Declaration |
2010 N_Protected_Body |
2011 N_Protected_Body_Stub |
2012 N_Protected_Type_Declaration |
2013 N_Single_Protected_Declaration |
2014 N_Single_Task_Declaration |
2015 N_Subtype_Declaration |
2016 N_Task_Body |
2017 N_Task_Body_Stub |
2018 N_Task_Type_Declaration
2020 return Defining_Identifier (N);
2022 when N_Subunit =>
2023 return Defining_Entity (Proper_Body (N));
2025 when
2026 N_Function_Instantiation |
2027 N_Function_Specification |
2028 N_Generic_Function_Renaming_Declaration |
2029 N_Generic_Package_Renaming_Declaration |
2030 N_Generic_Procedure_Renaming_Declaration |
2031 N_Package_Body |
2032 N_Package_Instantiation |
2033 N_Package_Renaming_Declaration |
2034 N_Package_Specification |
2035 N_Procedure_Instantiation |
2036 N_Procedure_Specification
2038 declare
2039 Nam : constant Node_Id := Defining_Unit_Name (N);
2041 begin
2042 if Nkind (Nam) in N_Entity then
2043 return Nam;
2045 -- For Error, make up a name and attach to declaration
2046 -- so we can continue semantic analysis
2048 elsif Nam = Error then
2049 Err :=
2050 Make_Defining_Identifier (Sloc (N),
2051 Chars => New_Internal_Name ('T'));
2052 Set_Defining_Unit_Name (N, Err);
2054 return Err;
2055 -- If not an entity, get defining identifier
2057 else
2058 return Defining_Identifier (Nam);
2059 end if;
2060 end;
2062 when N_Block_Statement =>
2063 return Entity (Identifier (N));
2065 when others =>
2066 raise Program_Error;
2068 end case;
2069 end Defining_Entity;
2071 --------------------------
2072 -- Denotes_Discriminant --
2073 --------------------------
2075 function Denotes_Discriminant
2076 (N : Node_Id;
2077 Check_Concurrent : Boolean := False) return Boolean
2079 E : Entity_Id;
2080 begin
2081 if not Is_Entity_Name (N)
2082 or else No (Entity (N))
2083 then
2084 return False;
2085 else
2086 E := Entity (N);
2087 end if;
2089 -- If we are checking for a protected type, the discriminant may have
2090 -- been rewritten as the corresponding discriminal of the original type
2091 -- or of the corresponding concurrent record, depending on whether we
2092 -- are in the spec or body of the protected type.
2094 return Ekind (E) = E_Discriminant
2095 or else
2096 (Check_Concurrent
2097 and then Ekind (E) = E_In_Parameter
2098 and then Present (Discriminal_Link (E))
2099 and then
2100 (Is_Concurrent_Type (Scope (Discriminal_Link (E)))
2101 or else
2102 Is_Concurrent_Record_Type (Scope (Discriminal_Link (E)))));
2104 end Denotes_Discriminant;
2106 ----------------------
2107 -- Denotes_Variable --
2108 ----------------------
2110 function Denotes_Variable (N : Node_Id) return Boolean is
2111 begin
2112 return Is_Variable (N) and then Paren_Count (N) = 0;
2113 end Denotes_Variable;
2115 -----------------------------
2116 -- Depends_On_Discriminant --
2117 -----------------------------
2119 function Depends_On_Discriminant (N : Node_Id) return Boolean is
2120 L : Node_Id;
2121 H : Node_Id;
2123 begin
2124 Get_Index_Bounds (N, L, H);
2125 return Denotes_Discriminant (L) or else Denotes_Discriminant (H);
2126 end Depends_On_Discriminant;
2128 -------------------------
2129 -- Designate_Same_Unit --
2130 -------------------------
2132 function Designate_Same_Unit
2133 (Name1 : Node_Id;
2134 Name2 : Node_Id) return Boolean
2136 K1 : constant Node_Kind := Nkind (Name1);
2137 K2 : constant Node_Kind := Nkind (Name2);
2139 function Prefix_Node (N : Node_Id) return Node_Id;
2140 -- Returns the parent unit name node of a defining program unit name
2141 -- or the prefix if N is a selected component or an expanded name.
2143 function Select_Node (N : Node_Id) return Node_Id;
2144 -- Returns the defining identifier node of a defining program unit
2145 -- name or the selector node if N is a selected component or an
2146 -- expanded name.
2148 -----------------
2149 -- Prefix_Node --
2150 -----------------
2152 function Prefix_Node (N : Node_Id) return Node_Id is
2153 begin
2154 if Nkind (N) = N_Defining_Program_Unit_Name then
2155 return Name (N);
2157 else
2158 return Prefix (N);
2159 end if;
2160 end Prefix_Node;
2162 -----------------
2163 -- Select_Node --
2164 -----------------
2166 function Select_Node (N : Node_Id) return Node_Id is
2167 begin
2168 if Nkind (N) = N_Defining_Program_Unit_Name then
2169 return Defining_Identifier (N);
2171 else
2172 return Selector_Name (N);
2173 end if;
2174 end Select_Node;
2176 -- Start of processing for Designate_Next_Unit
2178 begin
2179 if (K1 = N_Identifier or else
2180 K1 = N_Defining_Identifier)
2181 and then
2182 (K2 = N_Identifier or else
2183 K2 = N_Defining_Identifier)
2184 then
2185 return Chars (Name1) = Chars (Name2);
2187 elsif
2188 (K1 = N_Expanded_Name or else
2189 K1 = N_Selected_Component or else
2190 K1 = N_Defining_Program_Unit_Name)
2191 and then
2192 (K2 = N_Expanded_Name or else
2193 K2 = N_Selected_Component or else
2194 K2 = N_Defining_Program_Unit_Name)
2195 then
2196 return
2197 (Chars (Select_Node (Name1)) = Chars (Select_Node (Name2)))
2198 and then
2199 Designate_Same_Unit (Prefix_Node (Name1), Prefix_Node (Name2));
2201 else
2202 return False;
2203 end if;
2204 end Designate_Same_Unit;
2206 ----------------------------
2207 -- Enclosing_Generic_Body --
2208 ----------------------------
2210 function Enclosing_Generic_Body
2211 (N : Node_Id) return Node_Id
2213 P : Node_Id;
2214 Decl : Node_Id;
2215 Spec : Node_Id;
2217 begin
2218 P := Parent (N);
2219 while Present (P) loop
2220 if Nkind (P) = N_Package_Body
2221 or else Nkind (P) = N_Subprogram_Body
2222 then
2223 Spec := Corresponding_Spec (P);
2225 if Present (Spec) then
2226 Decl := Unit_Declaration_Node (Spec);
2228 if Nkind (Decl) = N_Generic_Package_Declaration
2229 or else Nkind (Decl) = N_Generic_Subprogram_Declaration
2230 then
2231 return P;
2232 end if;
2233 end if;
2234 end if;
2236 P := Parent (P);
2237 end loop;
2239 return Empty;
2240 end Enclosing_Generic_Body;
2242 ----------------------------
2243 -- Enclosing_Generic_Unit --
2244 ----------------------------
2246 function Enclosing_Generic_Unit
2247 (N : Node_Id) return Node_Id
2249 P : Node_Id;
2250 Decl : Node_Id;
2251 Spec : Node_Id;
2253 begin
2254 P := Parent (N);
2255 while Present (P) loop
2256 if Nkind (P) = N_Generic_Package_Declaration
2257 or else Nkind (P) = N_Generic_Subprogram_Declaration
2258 then
2259 return P;
2261 elsif Nkind (P) = N_Package_Body
2262 or else Nkind (P) = N_Subprogram_Body
2263 then
2264 Spec := Corresponding_Spec (P);
2266 if Present (Spec) then
2267 Decl := Unit_Declaration_Node (Spec);
2269 if Nkind (Decl) = N_Generic_Package_Declaration
2270 or else Nkind (Decl) = N_Generic_Subprogram_Declaration
2271 then
2272 return Decl;
2273 end if;
2274 end if;
2275 end if;
2277 P := Parent (P);
2278 end loop;
2280 return Empty;
2281 end Enclosing_Generic_Unit;
2283 -------------------------------
2284 -- Enclosing_Lib_Unit_Entity --
2285 -------------------------------
2287 function Enclosing_Lib_Unit_Entity return Entity_Id is
2288 Unit_Entity : Entity_Id;
2290 begin
2291 -- Look for enclosing library unit entity by following scope links.
2292 -- Equivalent to, but faster than indexing through the scope stack.
2294 Unit_Entity := Current_Scope;
2295 while (Present (Scope (Unit_Entity))
2296 and then Scope (Unit_Entity) /= Standard_Standard)
2297 and not Is_Child_Unit (Unit_Entity)
2298 loop
2299 Unit_Entity := Scope (Unit_Entity);
2300 end loop;
2302 return Unit_Entity;
2303 end Enclosing_Lib_Unit_Entity;
2305 -----------------------------
2306 -- Enclosing_Lib_Unit_Node --
2307 -----------------------------
2309 function Enclosing_Lib_Unit_Node (N : Node_Id) return Node_Id is
2310 Current_Node : Node_Id;
2312 begin
2313 Current_Node := N;
2314 while Present (Current_Node)
2315 and then Nkind (Current_Node) /= N_Compilation_Unit
2316 loop
2317 Current_Node := Parent (Current_Node);
2318 end loop;
2320 if Nkind (Current_Node) /= N_Compilation_Unit then
2321 return Empty;
2322 end if;
2324 return Current_Node;
2325 end Enclosing_Lib_Unit_Node;
2327 --------------------------
2328 -- Enclosing_Subprogram --
2329 --------------------------
2331 function Enclosing_Subprogram (E : Entity_Id) return Entity_Id is
2332 Dynamic_Scope : constant Entity_Id := Enclosing_Dynamic_Scope (E);
2334 begin
2335 if Dynamic_Scope = Standard_Standard then
2336 return Empty;
2338 elsif Dynamic_Scope = Empty then
2339 return Empty;
2341 elsif Ekind (Dynamic_Scope) = E_Subprogram_Body then
2342 return Corresponding_Spec (Parent (Parent (Dynamic_Scope)));
2344 elsif Ekind (Dynamic_Scope) = E_Block
2345 or else Ekind (Dynamic_Scope) = E_Return_Statement
2346 then
2347 return Enclosing_Subprogram (Dynamic_Scope);
2349 elsif Ekind (Dynamic_Scope) = E_Task_Type then
2350 return Get_Task_Body_Procedure (Dynamic_Scope);
2352 elsif Convention (Dynamic_Scope) = Convention_Protected then
2353 return Protected_Body_Subprogram (Dynamic_Scope);
2355 else
2356 return Dynamic_Scope;
2357 end if;
2358 end Enclosing_Subprogram;
2360 ------------------------
2361 -- Ensure_Freeze_Node --
2362 ------------------------
2364 procedure Ensure_Freeze_Node (E : Entity_Id) is
2365 FN : Node_Id;
2367 begin
2368 if No (Freeze_Node (E)) then
2369 FN := Make_Freeze_Entity (Sloc (E));
2370 Set_Has_Delayed_Freeze (E);
2371 Set_Freeze_Node (E, FN);
2372 Set_Access_Types_To_Process (FN, No_Elist);
2373 Set_TSS_Elist (FN, No_Elist);
2374 Set_Entity (FN, E);
2375 end if;
2376 end Ensure_Freeze_Node;
2378 ----------------
2379 -- Enter_Name --
2380 ----------------
2382 procedure Enter_Name (Def_Id : Entity_Id) is
2383 C : constant Entity_Id := Current_Entity (Def_Id);
2384 E : constant Entity_Id := Current_Entity_In_Scope (Def_Id);
2385 S : constant Entity_Id := Current_Scope;
2387 begin
2388 Generate_Definition (Def_Id);
2390 -- Add new name to current scope declarations. Check for duplicate
2391 -- declaration, which may or may not be a genuine error.
2393 if Present (E) then
2395 -- Case of previous entity entered because of a missing declaration
2396 -- or else a bad subtype indication. Best is to use the new entity,
2397 -- and make the previous one invisible.
2399 if Etype (E) = Any_Type then
2400 Set_Is_Immediately_Visible (E, False);
2402 -- Case of renaming declaration constructed for package instances.
2403 -- if there is an explicit declaration with the same identifier,
2404 -- the renaming is not immediately visible any longer, but remains
2405 -- visible through selected component notation.
2407 elsif Nkind (Parent (E)) = N_Package_Renaming_Declaration
2408 and then not Comes_From_Source (E)
2409 then
2410 Set_Is_Immediately_Visible (E, False);
2412 -- The new entity may be the package renaming, which has the same
2413 -- same name as a generic formal which has been seen already.
2415 elsif Nkind (Parent (Def_Id)) = N_Package_Renaming_Declaration
2416 and then not Comes_From_Source (Def_Id)
2417 then
2418 Set_Is_Immediately_Visible (E, False);
2420 -- For a fat pointer corresponding to a remote access to subprogram,
2421 -- we use the same identifier as the RAS type, so that the proper
2422 -- name appears in the stub. This type is only retrieved through
2423 -- the RAS type and never by visibility, and is not added to the
2424 -- visibility list (see below).
2426 elsif Nkind (Parent (Def_Id)) = N_Full_Type_Declaration
2427 and then Present (Corresponding_Remote_Type (Def_Id))
2428 then
2429 null;
2431 -- A controller component for a type extension overrides the
2432 -- inherited component.
2434 elsif Chars (E) = Name_uController then
2435 null;
2437 -- Case of an implicit operation or derived literal. The new entity
2438 -- hides the implicit one, which is removed from all visibility,
2439 -- i.e. the entity list of its scope, and homonym chain of its name.
2441 elsif (Is_Overloadable (E) and then Is_Inherited_Operation (E))
2442 or else Is_Internal (E)
2443 then
2444 declare
2445 Prev : Entity_Id;
2446 Prev_Vis : Entity_Id;
2447 Decl : constant Node_Id := Parent (E);
2449 begin
2450 -- If E is an implicit declaration, it cannot be the first
2451 -- entity in the scope.
2453 Prev := First_Entity (Current_Scope);
2454 while Present (Prev)
2455 and then Next_Entity (Prev) /= E
2456 loop
2457 Next_Entity (Prev);
2458 end loop;
2460 if No (Prev) then
2462 -- If E is not on the entity chain of the current scope,
2463 -- it is an implicit declaration in the generic formal
2464 -- part of a generic subprogram. When analyzing the body,
2465 -- the generic formals are visible but not on the entity
2466 -- chain of the subprogram. The new entity will become
2467 -- the visible one in the body.
2469 pragma Assert
2470 (Nkind (Parent (Decl)) = N_Generic_Subprogram_Declaration);
2471 null;
2473 else
2474 Set_Next_Entity (Prev, Next_Entity (E));
2476 if No (Next_Entity (Prev)) then
2477 Set_Last_Entity (Current_Scope, Prev);
2478 end if;
2480 if E = Current_Entity (E) then
2481 Prev_Vis := Empty;
2483 else
2484 Prev_Vis := Current_Entity (E);
2485 while Homonym (Prev_Vis) /= E loop
2486 Prev_Vis := Homonym (Prev_Vis);
2487 end loop;
2488 end if;
2490 if Present (Prev_Vis) then
2492 -- Skip E in the visibility chain
2494 Set_Homonym (Prev_Vis, Homonym (E));
2496 else
2497 Set_Name_Entity_Id (Chars (E), Homonym (E));
2498 end if;
2499 end if;
2500 end;
2502 -- This section of code could use a comment ???
2504 elsif Present (Etype (E))
2505 and then Is_Concurrent_Type (Etype (E))
2506 and then E = Def_Id
2507 then
2508 return;
2510 -- If the homograph is a protected component renaming, it should not
2511 -- be hiding the current entity. Such renamings are treated as weak
2512 -- declarations.
2514 elsif Is_Prival (E) then
2515 Set_Is_Immediately_Visible (E, False);
2517 -- In this case the current entity is a protected component renaming.
2518 -- Perform minimal decoration by setting the scope and return since
2519 -- the prival should not be hiding other visible entities.
2521 elsif Is_Prival (Def_Id) then
2522 Set_Scope (Def_Id, Current_Scope);
2523 return;
2525 -- Analogous to privals, the discriminal generated for an entry
2526 -- index parameter acts as a weak declaration. Perform minimal
2527 -- decoration to avoid bogus errors.
2529 elsif Is_Discriminal (Def_Id)
2530 and then Ekind (Discriminal_Link (Def_Id)) = E_Entry_Index_Parameter
2531 then
2532 Set_Scope (Def_Id, Current_Scope);
2533 return;
2535 -- In the body or private part of an instance, a type extension
2536 -- may introduce a component with the same name as that of an
2537 -- actual. The legality rule is not enforced, but the semantics
2538 -- of the full type with two components of the same name are not
2539 -- clear at this point ???
2541 elsif In_Instance_Not_Visible then
2542 null;
2544 -- When compiling a package body, some child units may have become
2545 -- visible. They cannot conflict with local entities that hide them.
2547 elsif Is_Child_Unit (E)
2548 and then In_Open_Scopes (Scope (E))
2549 and then not Is_Immediately_Visible (E)
2550 then
2551 null;
2553 -- Conversely, with front-end inlining we may compile the parent
2554 -- body first, and a child unit subsequently. The context is now
2555 -- the parent spec, and body entities are not visible.
2557 elsif Is_Child_Unit (Def_Id)
2558 and then Is_Package_Body_Entity (E)
2559 and then not In_Package_Body (Current_Scope)
2560 then
2561 null;
2563 -- Case of genuine duplicate declaration
2565 else
2566 Error_Msg_Sloc := Sloc (E);
2568 -- If the previous declaration is an incomplete type declaration
2569 -- this may be an attempt to complete it with a private type.
2570 -- The following avoids confusing cascaded errors.
2572 if Nkind (Parent (E)) = N_Incomplete_Type_Declaration
2573 and then Nkind (Parent (Def_Id)) = N_Private_Type_Declaration
2574 then
2575 Error_Msg_N
2576 ("incomplete type cannot be completed with a private " &
2577 "declaration", Parent (Def_Id));
2578 Set_Is_Immediately_Visible (E, False);
2579 Set_Full_View (E, Def_Id);
2581 -- An inherited component of a record conflicts with a new
2582 -- discriminant. The discriminant is inserted first in the scope,
2583 -- but the error should be posted on it, not on the component.
2585 elsif Ekind (E) = E_Discriminant
2586 and then Present (Scope (Def_Id))
2587 and then Scope (Def_Id) /= Current_Scope
2588 then
2589 Error_Msg_Sloc := Sloc (Def_Id);
2590 Error_Msg_N ("& conflicts with declaration#", E);
2591 return;
2593 -- If the name of the unit appears in its own context clause,
2594 -- a dummy package with the name has already been created, and
2595 -- the error emitted. Try to continue quietly.
2597 elsif Error_Posted (E)
2598 and then Sloc (E) = No_Location
2599 and then Nkind (Parent (E)) = N_Package_Specification
2600 and then Current_Scope = Standard_Standard
2601 then
2602 Set_Scope (Def_Id, Current_Scope);
2603 return;
2605 else
2606 Error_Msg_N ("& conflicts with declaration#", Def_Id);
2608 -- Avoid cascaded messages with duplicate components in
2609 -- derived types.
2611 if Ekind (E) = E_Component
2612 or else Ekind (E) = E_Discriminant
2613 then
2614 return;
2615 end if;
2616 end if;
2618 if Nkind (Parent (Parent (Def_Id))) =
2619 N_Generic_Subprogram_Declaration
2620 and then Def_Id =
2621 Defining_Entity (Specification (Parent (Parent (Def_Id))))
2622 then
2623 Error_Msg_N ("\generic units cannot be overloaded", Def_Id);
2624 end if;
2626 -- If entity is in standard, then we are in trouble, because
2627 -- it means that we have a library package with a duplicated
2628 -- name. That's hard to recover from, so abort!
2630 if S = Standard_Standard then
2631 raise Unrecoverable_Error;
2633 -- Otherwise we continue with the declaration. Having two
2634 -- identical declarations should not cause us too much trouble!
2636 else
2637 null;
2638 end if;
2639 end if;
2640 end if;
2642 -- If we fall through, declaration is OK , or OK enough to continue
2644 -- If Def_Id is a discriminant or a record component we are in the
2645 -- midst of inheriting components in a derived record definition.
2646 -- Preserve their Ekind and Etype.
2648 if Ekind (Def_Id) = E_Discriminant
2649 or else Ekind (Def_Id) = E_Component
2650 then
2651 null;
2653 -- If a type is already set, leave it alone (happens whey a type
2654 -- declaration is reanalyzed following a call to the optimizer)
2656 elsif Present (Etype (Def_Id)) then
2657 null;
2659 -- Otherwise, the kind E_Void insures that premature uses of the entity
2660 -- will be detected. Any_Type insures that no cascaded errors will occur
2662 else
2663 Set_Ekind (Def_Id, E_Void);
2664 Set_Etype (Def_Id, Any_Type);
2665 end if;
2667 -- Inherited discriminants and components in derived record types are
2668 -- immediately visible. Itypes are not.
2670 if Ekind (Def_Id) = E_Discriminant
2671 or else Ekind (Def_Id) = E_Component
2672 or else (No (Corresponding_Remote_Type (Def_Id))
2673 and then not Is_Itype (Def_Id))
2674 then
2675 Set_Is_Immediately_Visible (Def_Id);
2676 Set_Current_Entity (Def_Id);
2677 end if;
2679 Set_Homonym (Def_Id, C);
2680 Append_Entity (Def_Id, S);
2681 Set_Public_Status (Def_Id);
2683 -- Warn if new entity hides an old one
2685 if Warn_On_Hiding and then Present (C)
2687 -- Don't warn for record components since they always have a well
2688 -- defined scope which does not confuse other uses. Note that in
2689 -- some cases, Ekind has not been set yet.
2691 and then Ekind (C) /= E_Component
2692 and then Ekind (C) /= E_Discriminant
2693 and then Nkind (Parent (C)) /= N_Component_Declaration
2694 and then Ekind (Def_Id) /= E_Component
2695 and then Ekind (Def_Id) /= E_Discriminant
2696 and then Nkind (Parent (Def_Id)) /= N_Component_Declaration
2698 -- Don't warn for one character variables. It is too common to use
2699 -- such variables as locals and will just cause too many false hits.
2701 and then Length_Of_Name (Chars (C)) /= 1
2703 -- Don't warn for non-source entities
2705 and then Comes_From_Source (C)
2706 and then Comes_From_Source (Def_Id)
2708 -- Don't warn unless entity in question is in extended main source
2710 and then In_Extended_Main_Source_Unit (Def_Id)
2712 -- Finally, the hidden entity must be either immediately visible
2713 -- or use visible (from a used package)
2715 and then
2716 (Is_Immediately_Visible (C)
2717 or else
2718 Is_Potentially_Use_Visible (C))
2719 then
2720 Error_Msg_Sloc := Sloc (C);
2721 Error_Msg_N ("declaration hides &#?", Def_Id);
2722 end if;
2723 end Enter_Name;
2725 --------------------------
2726 -- Explain_Limited_Type --
2727 --------------------------
2729 procedure Explain_Limited_Type (T : Entity_Id; N : Node_Id) is
2730 C : Entity_Id;
2732 begin
2733 -- For array, component type must be limited
2735 if Is_Array_Type (T) then
2736 Error_Msg_Node_2 := T;
2737 Error_Msg_NE
2738 ("\component type& of type& is limited", N, Component_Type (T));
2739 Explain_Limited_Type (Component_Type (T), N);
2741 elsif Is_Record_Type (T) then
2743 -- No need for extra messages if explicit limited record
2745 if Is_Limited_Record (Base_Type (T)) then
2746 return;
2747 end if;
2749 -- Otherwise find a limited component. Check only components that
2750 -- come from source, or inherited components that appear in the
2751 -- source of the ancestor.
2753 C := First_Component (T);
2754 while Present (C) loop
2755 if Is_Limited_Type (Etype (C))
2756 and then
2757 (Comes_From_Source (C)
2758 or else
2759 (Present (Original_Record_Component (C))
2760 and then
2761 Comes_From_Source (Original_Record_Component (C))))
2762 then
2763 Error_Msg_Node_2 := T;
2764 Error_Msg_NE ("\component& of type& has limited type", N, C);
2765 Explain_Limited_Type (Etype (C), N);
2766 return;
2767 end if;
2769 Next_Component (C);
2770 end loop;
2772 -- The type may be declared explicitly limited, even if no component
2773 -- of it is limited, in which case we fall out of the loop.
2774 return;
2775 end if;
2776 end Explain_Limited_Type;
2778 -----------------
2779 -- Find_Actual --
2780 -----------------
2782 procedure Find_Actual
2783 (N : Node_Id;
2784 Formal : out Entity_Id;
2785 Call : out Node_Id)
2787 Parnt : constant Node_Id := Parent (N);
2788 Actual : Node_Id;
2790 begin
2791 if (Nkind (Parnt) = N_Indexed_Component
2792 or else
2793 Nkind (Parnt) = N_Selected_Component)
2794 and then N = Prefix (Parnt)
2795 then
2796 Find_Actual (Parnt, Formal, Call);
2797 return;
2799 elsif Nkind (Parnt) = N_Parameter_Association
2800 and then N = Explicit_Actual_Parameter (Parnt)
2801 then
2802 Call := Parent (Parnt);
2804 elsif Nkind (Parnt) = N_Procedure_Call_Statement then
2805 Call := Parnt;
2807 else
2808 Formal := Empty;
2809 Call := Empty;
2810 return;
2811 end if;
2813 -- If we have a call to a subprogram look for the parameter. Note that
2814 -- we exclude overloaded calls, since we don't know enough to be sure
2815 -- of giving the right answer in this case.
2817 if Is_Entity_Name (Name (Call))
2818 and then Present (Entity (Name (Call)))
2819 and then Is_Overloadable (Entity (Name (Call)))
2820 and then not Is_Overloaded (Name (Call))
2821 then
2822 -- Fall here if we are definitely a parameter
2824 Actual := First_Actual (Call);
2825 Formal := First_Formal (Entity (Name (Call)));
2826 while Present (Formal) and then Present (Actual) loop
2827 if Actual = N then
2828 return;
2829 else
2830 Actual := Next_Actual (Actual);
2831 Formal := Next_Formal (Formal);
2832 end if;
2833 end loop;
2834 end if;
2836 -- Fall through here if we did not find matching actual
2838 Formal := Empty;
2839 Call := Empty;
2840 end Find_Actual;
2842 -------------------------------------
2843 -- Find_Corresponding_Discriminant --
2844 -------------------------------------
2846 function Find_Corresponding_Discriminant
2847 (Id : Node_Id;
2848 Typ : Entity_Id) return Entity_Id
2850 Par_Disc : Entity_Id;
2851 Old_Disc : Entity_Id;
2852 New_Disc : Entity_Id;
2854 begin
2855 Par_Disc := Original_Record_Component (Original_Discriminant (Id));
2857 -- The original type may currently be private, and the discriminant
2858 -- only appear on its full view.
2860 if Is_Private_Type (Scope (Par_Disc))
2861 and then not Has_Discriminants (Scope (Par_Disc))
2862 and then Present (Full_View (Scope (Par_Disc)))
2863 then
2864 Old_Disc := First_Discriminant (Full_View (Scope (Par_Disc)));
2865 else
2866 Old_Disc := First_Discriminant (Scope (Par_Disc));
2867 end if;
2869 if Is_Class_Wide_Type (Typ) then
2870 New_Disc := First_Discriminant (Root_Type (Typ));
2871 else
2872 New_Disc := First_Discriminant (Typ);
2873 end if;
2875 while Present (Old_Disc) and then Present (New_Disc) loop
2876 if Old_Disc = Par_Disc then
2877 return New_Disc;
2878 else
2879 Next_Discriminant (Old_Disc);
2880 Next_Discriminant (New_Disc);
2881 end if;
2882 end loop;
2884 -- Should always find it
2886 raise Program_Error;
2887 end Find_Corresponding_Discriminant;
2889 --------------------------
2890 -- Find_Overlaid_Object --
2891 --------------------------
2893 function Find_Overlaid_Object (N : Node_Id) return Entity_Id is
2894 Expr : Node_Id;
2896 begin
2897 -- We are looking for one of the two following forms:
2899 -- for X'Address use Y'Address
2901 -- or
2903 -- Const : constant Address := expr;
2904 -- ...
2905 -- for X'Address use Const;
2907 -- In the second case, the expr is either Y'Address, or recursively a
2908 -- constant that eventually references Y'Address.
2910 if Nkind (N) = N_Attribute_Definition_Clause
2911 and then Chars (N) = Name_Address
2912 then
2913 -- This loop checks the form of the expression for Y'Address where Y
2914 -- is an object entity name. The first loop checks the original
2915 -- expression in the attribute definition clause. Subsequent loops
2916 -- check referenced constants.
2918 Expr := Expression (N);
2919 loop
2920 -- Check for Y'Address where Y is an object entity
2922 if Nkind (Expr) = N_Attribute_Reference
2923 and then Attribute_Name (Expr) = Name_Address
2924 and then Is_Entity_Name (Prefix (Expr))
2925 and then Is_Object (Entity (Prefix (Expr)))
2926 then
2927 return Entity (Prefix (Expr));
2929 -- Check for Const where Const is a constant entity
2931 elsif Is_Entity_Name (Expr)
2932 and then Ekind (Entity (Expr)) = E_Constant
2933 then
2934 Expr := Constant_Value (Entity (Expr));
2936 -- Anything else does not need checking
2938 else
2939 exit;
2940 end if;
2941 end loop;
2942 end if;
2944 return Empty;
2945 end Find_Overlaid_Object;
2947 -------------------------
2948 -- Find_Parameter_Type --
2949 -------------------------
2951 function Find_Parameter_Type (Param : Node_Id) return Entity_Id is
2952 begin
2953 if Nkind (Param) /= N_Parameter_Specification then
2954 return Empty;
2956 -- For an access parameter, obtain the type from the formal entity
2957 -- itself, because access to subprogram nodes do not carry a type.
2958 -- Shouldn't we always use the formal entity ???
2960 elsif Nkind (Parameter_Type (Param)) = N_Access_Definition then
2961 return Etype (Defining_Identifier (Param));
2963 else
2964 return Etype (Parameter_Type (Param));
2965 end if;
2966 end Find_Parameter_Type;
2968 -----------------------------
2969 -- Find_Static_Alternative --
2970 -----------------------------
2972 function Find_Static_Alternative (N : Node_Id) return Node_Id is
2973 Expr : constant Node_Id := Expression (N);
2974 Val : constant Uint := Expr_Value (Expr);
2975 Alt : Node_Id;
2976 Choice : Node_Id;
2978 begin
2979 Alt := First (Alternatives (N));
2981 Search : loop
2982 if Nkind (Alt) /= N_Pragma then
2983 Choice := First (Discrete_Choices (Alt));
2984 while Present (Choice) loop
2986 -- Others choice, always matches
2988 if Nkind (Choice) = N_Others_Choice then
2989 exit Search;
2991 -- Range, check if value is in the range
2993 elsif Nkind (Choice) = N_Range then
2994 exit Search when
2995 Val >= Expr_Value (Low_Bound (Choice))
2996 and then
2997 Val <= Expr_Value (High_Bound (Choice));
2999 -- Choice is a subtype name. Note that we know it must
3000 -- be a static subtype, since otherwise it would have
3001 -- been diagnosed as illegal.
3003 elsif Is_Entity_Name (Choice)
3004 and then Is_Type (Entity (Choice))
3005 then
3006 exit Search when Is_In_Range (Expr, Etype (Choice),
3007 Assume_Valid => False);
3009 -- Choice is a subtype indication
3011 elsif Nkind (Choice) = N_Subtype_Indication then
3012 declare
3013 C : constant Node_Id := Constraint (Choice);
3014 R : constant Node_Id := Range_Expression (C);
3016 begin
3017 exit Search when
3018 Val >= Expr_Value (Low_Bound (R))
3019 and then
3020 Val <= Expr_Value (High_Bound (R));
3021 end;
3023 -- Choice is a simple expression
3025 else
3026 exit Search when Val = Expr_Value (Choice);
3027 end if;
3029 Next (Choice);
3030 end loop;
3031 end if;
3033 Next (Alt);
3034 pragma Assert (Present (Alt));
3035 end loop Search;
3037 -- The above loop *must* terminate by finding a match, since
3038 -- we know the case statement is valid, and the value of the
3039 -- expression is known at compile time. When we fall out of
3040 -- the loop, Alt points to the alternative that we know will
3041 -- be selected at run time.
3043 return Alt;
3044 end Find_Static_Alternative;
3046 ------------------
3047 -- First_Actual --
3048 ------------------
3050 function First_Actual (Node : Node_Id) return Node_Id is
3051 N : Node_Id;
3053 begin
3054 if No (Parameter_Associations (Node)) then
3055 return Empty;
3056 end if;
3058 N := First (Parameter_Associations (Node));
3060 if Nkind (N) = N_Parameter_Association then
3061 return First_Named_Actual (Node);
3062 else
3063 return N;
3064 end if;
3065 end First_Actual;
3067 -------------------------
3068 -- Full_Qualified_Name --
3069 -------------------------
3071 function Full_Qualified_Name (E : Entity_Id) return String_Id is
3072 Res : String_Id;
3073 pragma Warnings (Off, Res);
3075 function Internal_Full_Qualified_Name (E : Entity_Id) return String_Id;
3076 -- Compute recursively the qualified name without NUL at the end
3078 ----------------------------------
3079 -- Internal_Full_Qualified_Name --
3080 ----------------------------------
3082 function Internal_Full_Qualified_Name (E : Entity_Id) return String_Id is
3083 Ent : Entity_Id := E;
3084 Parent_Name : String_Id := No_String;
3086 begin
3087 -- Deals properly with child units
3089 if Nkind (Ent) = N_Defining_Program_Unit_Name then
3090 Ent := Defining_Identifier (Ent);
3091 end if;
3093 -- Compute qualification recursively (only "Standard" has no scope)
3095 if Present (Scope (Scope (Ent))) then
3096 Parent_Name := Internal_Full_Qualified_Name (Scope (Ent));
3097 end if;
3099 -- Every entity should have a name except some expanded blocks
3100 -- don't bother about those.
3102 if Chars (Ent) = No_Name then
3103 return Parent_Name;
3104 end if;
3106 -- Add a period between Name and qualification
3108 if Parent_Name /= No_String then
3109 Start_String (Parent_Name);
3110 Store_String_Char (Get_Char_Code ('.'));
3112 else
3113 Start_String;
3114 end if;
3116 -- Generates the entity name in upper case
3118 Get_Decoded_Name_String (Chars (Ent));
3119 Set_All_Upper_Case;
3120 Store_String_Chars (Name_Buffer (1 .. Name_Len));
3121 return End_String;
3122 end Internal_Full_Qualified_Name;
3124 -- Start of processing for Full_Qualified_Name
3126 begin
3127 Res := Internal_Full_Qualified_Name (E);
3128 Store_String_Char (Get_Char_Code (ASCII.NUL));
3129 return End_String;
3130 end Full_Qualified_Name;
3132 -----------------------
3133 -- Gather_Components --
3134 -----------------------
3136 procedure Gather_Components
3137 (Typ : Entity_Id;
3138 Comp_List : Node_Id;
3139 Governed_By : List_Id;
3140 Into : Elist_Id;
3141 Report_Errors : out Boolean)
3143 Assoc : Node_Id;
3144 Variant : Node_Id;
3145 Discrete_Choice : Node_Id;
3146 Comp_Item : Node_Id;
3148 Discrim : Entity_Id;
3149 Discrim_Name : Node_Id;
3150 Discrim_Value : Node_Id;
3152 begin
3153 Report_Errors := False;
3155 if No (Comp_List) or else Null_Present (Comp_List) then
3156 return;
3158 elsif Present (Component_Items (Comp_List)) then
3159 Comp_Item := First (Component_Items (Comp_List));
3161 else
3162 Comp_Item := Empty;
3163 end if;
3165 while Present (Comp_Item) loop
3167 -- Skip the tag of a tagged record, the interface tags, as well
3168 -- as all items that are not user components (anonymous types,
3169 -- rep clauses, Parent field, controller field).
3171 if Nkind (Comp_Item) = N_Component_Declaration then
3172 declare
3173 Comp : constant Entity_Id := Defining_Identifier (Comp_Item);
3174 begin
3175 if not Is_Tag (Comp)
3176 and then Chars (Comp) /= Name_uParent
3177 and then Chars (Comp) /= Name_uController
3178 then
3179 Append_Elmt (Comp, Into);
3180 end if;
3181 end;
3182 end if;
3184 Next (Comp_Item);
3185 end loop;
3187 if No (Variant_Part (Comp_List)) then
3188 return;
3189 else
3190 Discrim_Name := Name (Variant_Part (Comp_List));
3191 Variant := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
3192 end if;
3194 -- Look for the discriminant that governs this variant part.
3195 -- The discriminant *must* be in the Governed_By List
3197 Assoc := First (Governed_By);
3198 Find_Constraint : loop
3199 Discrim := First (Choices (Assoc));
3200 exit Find_Constraint when Chars (Discrim_Name) = Chars (Discrim)
3201 or else (Present (Corresponding_Discriminant (Entity (Discrim)))
3202 and then
3203 Chars (Corresponding_Discriminant (Entity (Discrim)))
3204 = Chars (Discrim_Name))
3205 or else Chars (Original_Record_Component (Entity (Discrim)))
3206 = Chars (Discrim_Name);
3208 if No (Next (Assoc)) then
3209 if not Is_Constrained (Typ)
3210 and then Is_Derived_Type (Typ)
3211 and then Present (Stored_Constraint (Typ))
3212 then
3213 -- If the type is a tagged type with inherited discriminants,
3214 -- use the stored constraint on the parent in order to find
3215 -- the values of discriminants that are otherwise hidden by an
3216 -- explicit constraint. Renamed discriminants are handled in
3217 -- the code above.
3219 -- If several parent discriminants are renamed by a single
3220 -- discriminant of the derived type, the call to obtain the
3221 -- Corresponding_Discriminant field only retrieves the last
3222 -- of them. We recover the constraint on the others from the
3223 -- Stored_Constraint as well.
3225 declare
3226 D : Entity_Id;
3227 C : Elmt_Id;
3229 begin
3230 D := First_Discriminant (Etype (Typ));
3231 C := First_Elmt (Stored_Constraint (Typ));
3232 while Present (D) and then Present (C) loop
3233 if Chars (Discrim_Name) = Chars (D) then
3234 if Is_Entity_Name (Node (C))
3235 and then Entity (Node (C)) = Entity (Discrim)
3236 then
3237 -- D is renamed by Discrim, whose value is given in
3238 -- Assoc.
3240 null;
3242 else
3243 Assoc :=
3244 Make_Component_Association (Sloc (Typ),
3245 New_List
3246 (New_Occurrence_Of (D, Sloc (Typ))),
3247 Duplicate_Subexpr_No_Checks (Node (C)));
3248 end if;
3249 exit Find_Constraint;
3250 end if;
3252 Next_Discriminant (D);
3253 Next_Elmt (C);
3254 end loop;
3255 end;
3256 end if;
3257 end if;
3259 if No (Next (Assoc)) then
3260 Error_Msg_NE (" missing value for discriminant&",
3261 First (Governed_By), Discrim_Name);
3262 Report_Errors := True;
3263 return;
3264 end if;
3266 Next (Assoc);
3267 end loop Find_Constraint;
3269 Discrim_Value := Expression (Assoc);
3271 if not Is_OK_Static_Expression (Discrim_Value) then
3272 Error_Msg_FE
3273 ("value for discriminant & must be static!",
3274 Discrim_Value, Discrim);
3275 Why_Not_Static (Discrim_Value);
3276 Report_Errors := True;
3277 return;
3278 end if;
3280 Search_For_Discriminant_Value : declare
3281 Low : Node_Id;
3282 High : Node_Id;
3284 UI_High : Uint;
3285 UI_Low : Uint;
3286 UI_Discrim_Value : constant Uint := Expr_Value (Discrim_Value);
3288 begin
3289 Find_Discrete_Value : while Present (Variant) loop
3290 Discrete_Choice := First (Discrete_Choices (Variant));
3291 while Present (Discrete_Choice) loop
3293 exit Find_Discrete_Value when
3294 Nkind (Discrete_Choice) = N_Others_Choice;
3296 Get_Index_Bounds (Discrete_Choice, Low, High);
3298 UI_Low := Expr_Value (Low);
3299 UI_High := Expr_Value (High);
3301 exit Find_Discrete_Value when
3302 UI_Low <= UI_Discrim_Value
3303 and then
3304 UI_High >= UI_Discrim_Value;
3306 Next (Discrete_Choice);
3307 end loop;
3309 Next_Non_Pragma (Variant);
3310 end loop Find_Discrete_Value;
3311 end Search_For_Discriminant_Value;
3313 if No (Variant) then
3314 Error_Msg_NE
3315 ("value of discriminant & is out of range", Discrim_Value, Discrim);
3316 Report_Errors := True;
3317 return;
3318 end if;
3320 -- If we have found the corresponding choice, recursively add its
3321 -- components to the Into list.
3323 Gather_Components (Empty,
3324 Component_List (Variant), Governed_By, Into, Report_Errors);
3325 end Gather_Components;
3327 ------------------------
3328 -- Get_Actual_Subtype --
3329 ------------------------
3331 function Get_Actual_Subtype (N : Node_Id) return Entity_Id is
3332 Typ : constant Entity_Id := Etype (N);
3333 Utyp : Entity_Id := Underlying_Type (Typ);
3334 Decl : Node_Id;
3335 Atyp : Entity_Id;
3337 begin
3338 if No (Utyp) then
3339 Utyp := Typ;
3340 end if;
3342 -- If what we have is an identifier that references a subprogram
3343 -- formal, or a variable or constant object, then we get the actual
3344 -- subtype from the referenced entity if one has been built.
3346 if Nkind (N) = N_Identifier
3347 and then
3348 (Is_Formal (Entity (N))
3349 or else Ekind (Entity (N)) = E_Constant
3350 or else Ekind (Entity (N)) = E_Variable)
3351 and then Present (Actual_Subtype (Entity (N)))
3352 then
3353 return Actual_Subtype (Entity (N));
3355 -- Actual subtype of unchecked union is always itself. We never need
3356 -- the "real" actual subtype. If we did, we couldn't get it anyway
3357 -- because the discriminant is not available. The restrictions on
3358 -- Unchecked_Union are designed to make sure that this is OK.
3360 elsif Is_Unchecked_Union (Base_Type (Utyp)) then
3361 return Typ;
3363 -- Here for the unconstrained case, we must find actual subtype
3364 -- No actual subtype is available, so we must build it on the fly.
3366 -- Checking the type, not the underlying type, for constrainedness
3367 -- seems to be necessary. Maybe all the tests should be on the type???
3369 elsif (not Is_Constrained (Typ))
3370 and then (Is_Array_Type (Utyp)
3371 or else (Is_Record_Type (Utyp)
3372 and then Has_Discriminants (Utyp)))
3373 and then not Has_Unknown_Discriminants (Utyp)
3374 and then not (Ekind (Utyp) = E_String_Literal_Subtype)
3375 then
3376 -- Nothing to do if in spec expression (why not???)
3378 if In_Spec_Expression then
3379 return Typ;
3381 elsif Is_Private_Type (Typ)
3382 and then not Has_Discriminants (Typ)
3383 then
3384 -- If the type has no discriminants, there is no subtype to
3385 -- build, even if the underlying type is discriminated.
3387 return Typ;
3389 -- Else build the actual subtype
3391 else
3392 Decl := Build_Actual_Subtype (Typ, N);
3393 Atyp := Defining_Identifier (Decl);
3395 -- If Build_Actual_Subtype generated a new declaration then use it
3397 if Atyp /= Typ then
3399 -- The actual subtype is an Itype, so analyze the declaration,
3400 -- but do not attach it to the tree, to get the type defined.
3402 Set_Parent (Decl, N);
3403 Set_Is_Itype (Atyp);
3404 Analyze (Decl, Suppress => All_Checks);
3405 Set_Associated_Node_For_Itype (Atyp, N);
3406 Set_Has_Delayed_Freeze (Atyp, False);
3408 -- We need to freeze the actual subtype immediately. This is
3409 -- needed, because otherwise this Itype will not get frozen
3410 -- at all, and it is always safe to freeze on creation because
3411 -- any associated types must be frozen at this point.
3413 Freeze_Itype (Atyp, N);
3414 return Atyp;
3416 -- Otherwise we did not build a declaration, so return original
3418 else
3419 return Typ;
3420 end if;
3421 end if;
3423 -- For all remaining cases, the actual subtype is the same as
3424 -- the nominal type.
3426 else
3427 return Typ;
3428 end if;
3429 end Get_Actual_Subtype;
3431 -------------------------------------
3432 -- Get_Actual_Subtype_If_Available --
3433 -------------------------------------
3435 function Get_Actual_Subtype_If_Available (N : Node_Id) return Entity_Id is
3436 Typ : constant Entity_Id := Etype (N);
3438 begin
3439 -- If what we have is an identifier that references a subprogram
3440 -- formal, or a variable or constant object, then we get the actual
3441 -- subtype from the referenced entity if one has been built.
3443 if Nkind (N) = N_Identifier
3444 and then
3445 (Is_Formal (Entity (N))
3446 or else Ekind (Entity (N)) = E_Constant
3447 or else Ekind (Entity (N)) = E_Variable)
3448 and then Present (Actual_Subtype (Entity (N)))
3449 then
3450 return Actual_Subtype (Entity (N));
3452 -- Otherwise the Etype of N is returned unchanged
3454 else
3455 return Typ;
3456 end if;
3457 end Get_Actual_Subtype_If_Available;
3459 -------------------------------
3460 -- Get_Default_External_Name --
3461 -------------------------------
3463 function Get_Default_External_Name (E : Node_Or_Entity_Id) return Node_Id is
3464 begin
3465 Get_Decoded_Name_String (Chars (E));
3467 if Opt.External_Name_Imp_Casing = Uppercase then
3468 Set_Casing (All_Upper_Case);
3469 else
3470 Set_Casing (All_Lower_Case);
3471 end if;
3473 return
3474 Make_String_Literal (Sloc (E),
3475 Strval => String_From_Name_Buffer);
3476 end Get_Default_External_Name;
3478 ---------------------------
3479 -- Get_Enum_Lit_From_Pos --
3480 ---------------------------
3482 function Get_Enum_Lit_From_Pos
3483 (T : Entity_Id;
3484 Pos : Uint;
3485 Loc : Source_Ptr) return Node_Id
3487 Lit : Node_Id;
3489 begin
3490 -- In the case where the literal is of type Character, Wide_Character
3491 -- or Wide_Wide_Character or of a type derived from them, there needs
3492 -- to be some special handling since there is no explicit chain of
3493 -- literals to search. Instead, an N_Character_Literal node is created
3494 -- with the appropriate Char_Code and Chars fields.
3496 if Is_Standard_Character_Type (T) then
3497 Set_Character_Literal_Name (UI_To_CC (Pos));
3498 return
3499 Make_Character_Literal (Loc,
3500 Chars => Name_Find,
3501 Char_Literal_Value => Pos);
3503 -- For all other cases, we have a complete table of literals, and
3504 -- we simply iterate through the chain of literal until the one
3505 -- with the desired position value is found.
3508 else
3509 Lit := First_Literal (Base_Type (T));
3510 for J in 1 .. UI_To_Int (Pos) loop
3511 Next_Literal (Lit);
3512 end loop;
3514 return New_Occurrence_Of (Lit, Loc);
3515 end if;
3516 end Get_Enum_Lit_From_Pos;
3518 ------------------------
3519 -- Get_Generic_Entity --
3520 ------------------------
3522 function Get_Generic_Entity (N : Node_Id) return Entity_Id is
3523 Ent : constant Entity_Id := Entity (Name (N));
3524 begin
3525 if Present (Renamed_Object (Ent)) then
3526 return Renamed_Object (Ent);
3527 else
3528 return Ent;
3529 end if;
3530 end Get_Generic_Entity;
3532 ----------------------
3533 -- Get_Index_Bounds --
3534 ----------------------
3536 procedure Get_Index_Bounds (N : Node_Id; L, H : out Node_Id) is
3537 Kind : constant Node_Kind := Nkind (N);
3538 R : Node_Id;
3540 begin
3541 if Kind = N_Range then
3542 L := Low_Bound (N);
3543 H := High_Bound (N);
3545 elsif Kind = N_Subtype_Indication then
3546 R := Range_Expression (Constraint (N));
3548 if R = Error then
3549 L := Error;
3550 H := Error;
3551 return;
3553 else
3554 L := Low_Bound (Range_Expression (Constraint (N)));
3555 H := High_Bound (Range_Expression (Constraint (N)));
3556 end if;
3558 elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
3559 if Error_Posted (Scalar_Range (Entity (N))) then
3560 L := Error;
3561 H := Error;
3563 elsif Nkind (Scalar_Range (Entity (N))) = N_Subtype_Indication then
3564 Get_Index_Bounds (Scalar_Range (Entity (N)), L, H);
3566 else
3567 L := Low_Bound (Scalar_Range (Entity (N)));
3568 H := High_Bound (Scalar_Range (Entity (N)));
3569 end if;
3571 else
3572 -- N is an expression, indicating a range with one value
3574 L := N;
3575 H := N;
3576 end if;
3577 end Get_Index_Bounds;
3579 ----------------------------------
3580 -- Get_Library_Unit_Name_string --
3581 ----------------------------------
3583 procedure Get_Library_Unit_Name_String (Decl_Node : Node_Id) is
3584 Unit_Name_Id : constant Unit_Name_Type := Get_Unit_Name (Decl_Node);
3586 begin
3587 Get_Unit_Name_String (Unit_Name_Id);
3589 -- Remove seven last character (" (spec)" or " (body)")
3591 Name_Len := Name_Len - 7;
3592 pragma Assert (Name_Buffer (Name_Len + 1) = ' ');
3593 end Get_Library_Unit_Name_String;
3595 ------------------------
3596 -- Get_Name_Entity_Id --
3597 ------------------------
3599 function Get_Name_Entity_Id (Id : Name_Id) return Entity_Id is
3600 begin
3601 return Entity_Id (Get_Name_Table_Info (Id));
3602 end Get_Name_Entity_Id;
3604 -------------------
3605 -- Get_Pragma_Id --
3606 -------------------
3608 function Get_Pragma_Id (N : Node_Id) return Pragma_Id is
3609 begin
3610 return Get_Pragma_Id (Pragma_Name (N));
3611 end Get_Pragma_Id;
3613 ---------------------------
3614 -- Get_Referenced_Object --
3615 ---------------------------
3617 function Get_Referenced_Object (N : Node_Id) return Node_Id is
3618 R : Node_Id;
3620 begin
3621 R := N;
3622 while Is_Entity_Name (R)
3623 and then Present (Renamed_Object (Entity (R)))
3624 loop
3625 R := Renamed_Object (Entity (R));
3626 end loop;
3628 return R;
3629 end Get_Referenced_Object;
3631 ------------------------
3632 -- Get_Renamed_Entity --
3633 ------------------------
3635 function Get_Renamed_Entity (E : Entity_Id) return Entity_Id is
3636 R : Entity_Id;
3638 begin
3639 R := E;
3640 while Present (Renamed_Entity (R)) loop
3641 R := Renamed_Entity (R);
3642 end loop;
3644 return R;
3645 end Get_Renamed_Entity;
3647 -------------------------
3648 -- Get_Subprogram_Body --
3649 -------------------------
3651 function Get_Subprogram_Body (E : Entity_Id) return Node_Id is
3652 Decl : Node_Id;
3654 begin
3655 Decl := Unit_Declaration_Node (E);
3657 if Nkind (Decl) = N_Subprogram_Body then
3658 return Decl;
3660 -- The below comment is bad, because it is possible for
3661 -- Nkind (Decl) to be an N_Subprogram_Body_Stub ???
3663 else -- Nkind (Decl) = N_Subprogram_Declaration
3665 if Present (Corresponding_Body (Decl)) then
3666 return Unit_Declaration_Node (Corresponding_Body (Decl));
3668 -- Imported subprogram case
3670 else
3671 return Empty;
3672 end if;
3673 end if;
3674 end Get_Subprogram_Body;
3676 ---------------------------
3677 -- Get_Subprogram_Entity --
3678 ---------------------------
3680 function Get_Subprogram_Entity (Nod : Node_Id) return Entity_Id is
3681 Nam : Node_Id;
3682 Proc : Entity_Id;
3684 begin
3685 if Nkind (Nod) = N_Accept_Statement then
3686 Nam := Entry_Direct_Name (Nod);
3688 -- For an entry call, the prefix of the call is a selected component.
3689 -- Need additional code for internal calls ???
3691 elsif Nkind (Nod) = N_Entry_Call_Statement then
3692 if Nkind (Name (Nod)) = N_Selected_Component then
3693 Nam := Entity (Selector_Name (Name (Nod)));
3694 else
3695 Nam := Empty;
3696 end if;
3698 else
3699 Nam := Name (Nod);
3700 end if;
3702 if Nkind (Nam) = N_Explicit_Dereference then
3703 Proc := Etype (Prefix (Nam));
3704 elsif Is_Entity_Name (Nam) then
3705 Proc := Entity (Nam);
3706 else
3707 return Empty;
3708 end if;
3710 if Is_Object (Proc) then
3711 Proc := Etype (Proc);
3712 end if;
3714 if Ekind (Proc) = E_Access_Subprogram_Type then
3715 Proc := Directly_Designated_Type (Proc);
3716 end if;
3718 if not Is_Subprogram (Proc)
3719 and then Ekind (Proc) /= E_Subprogram_Type
3720 then
3721 return Empty;
3722 else
3723 return Proc;
3724 end if;
3725 end Get_Subprogram_Entity;
3727 -----------------------------
3728 -- Get_Task_Body_Procedure --
3729 -----------------------------
3731 function Get_Task_Body_Procedure (E : Entity_Id) return Node_Id is
3732 begin
3733 -- Note: A task type may be the completion of a private type with
3734 -- discriminants. When performing elaboration checks on a task
3735 -- declaration, the current view of the type may be the private one,
3736 -- and the procedure that holds the body of the task is held in its
3737 -- underlying type.
3739 -- This is an odd function, why not have Task_Body_Procedure do
3740 -- the following digging???
3742 return Task_Body_Procedure (Underlying_Type (Root_Type (E)));
3743 end Get_Task_Body_Procedure;
3745 -----------------------
3746 -- Has_Access_Values --
3747 -----------------------
3749 function Has_Access_Values (T : Entity_Id) return Boolean is
3750 Typ : constant Entity_Id := Underlying_Type (T);
3752 begin
3753 -- Case of a private type which is not completed yet. This can only
3754 -- happen in the case of a generic format type appearing directly, or
3755 -- as a component of the type to which this function is being applied
3756 -- at the top level. Return False in this case, since we certainly do
3757 -- not know that the type contains access types.
3759 if No (Typ) then
3760 return False;
3762 elsif Is_Access_Type (Typ) then
3763 return True;
3765 elsif Is_Array_Type (Typ) then
3766 return Has_Access_Values (Component_Type (Typ));
3768 elsif Is_Record_Type (Typ) then
3769 declare
3770 Comp : Entity_Id;
3772 begin
3773 -- Loop to Check components
3775 Comp := First_Component_Or_Discriminant (Typ);
3776 while Present (Comp) loop
3778 -- Check for access component, tag field does not count, even
3779 -- though it is implemented internally using an access type.
3781 if Has_Access_Values (Etype (Comp))
3782 and then Chars (Comp) /= Name_uTag
3783 then
3784 return True;
3785 end if;
3787 Next_Component_Or_Discriminant (Comp);
3788 end loop;
3789 end;
3791 return False;
3793 else
3794 return False;
3795 end if;
3796 end Has_Access_Values;
3798 ------------------------------
3799 -- Has_Compatible_Alignment --
3800 ------------------------------
3802 function Has_Compatible_Alignment
3803 (Obj : Entity_Id;
3804 Expr : Node_Id) return Alignment_Result
3806 function Has_Compatible_Alignment_Internal
3807 (Obj : Entity_Id;
3808 Expr : Node_Id;
3809 Default : Alignment_Result) return Alignment_Result;
3810 -- This is the internal recursive function that actually does the work.
3811 -- There is one additional parameter, which says what the result should
3812 -- be if no alignment information is found, and there is no definite
3813 -- indication of compatible alignments. At the outer level, this is set
3814 -- to Unknown, but for internal recursive calls in the case where types
3815 -- are known to be correct, it is set to Known_Compatible.
3817 ---------------------------------------
3818 -- Has_Compatible_Alignment_Internal --
3819 ---------------------------------------
3821 function Has_Compatible_Alignment_Internal
3822 (Obj : Entity_Id;
3823 Expr : Node_Id;
3824 Default : Alignment_Result) return Alignment_Result
3826 Result : Alignment_Result := Known_Compatible;
3827 -- Set to result if Problem_Prefix or Problem_Offset returns True.
3828 -- Note that once a value of Known_Incompatible is set, it is sticky
3829 -- and does not get changed to Unknown (the value in Result only gets
3830 -- worse as we go along, never better).
3832 procedure Check_Offset (Offs : Uint);
3833 -- Called when Expr is a selected or indexed component with Offs set
3834 -- to resp Component_First_Bit or Component_Size. Checks that if the
3835 -- offset is specified it is compatible with the object alignment
3836 -- requirements. The value in Result is modified accordingly.
3838 procedure Check_Prefix;
3839 -- Checks the prefix recursively in the case where the expression
3840 -- is an indexed or selected component.
3842 procedure Set_Result (R : Alignment_Result);
3843 -- If R represents a worse outcome (unknown instead of known
3844 -- compatible, or known incompatible), then set Result to R.
3846 ------------------
3847 -- Check_Offset --
3848 ------------------
3850 procedure Check_Offset (Offs : Uint) is
3851 begin
3852 -- Unspecified or zero offset is always OK
3854 if Offs = No_Uint or else Offs = Uint_0 then
3855 null;
3857 -- If we do not know required alignment, any non-zero offset is
3858 -- a potential problem (but certainly may be OK, so result is
3859 -- unknown).
3861 elsif Unknown_Alignment (Obj) then
3862 Set_Result (Unknown);
3864 -- If we know the required alignment, see if offset is compatible
3866 else
3867 if Offs mod (System_Storage_Unit * Alignment (Obj)) /= 0 then
3868 Set_Result (Known_Incompatible);
3869 end if;
3870 end if;
3871 end Check_Offset;
3873 ------------------
3874 -- Check_Prefix --
3875 ------------------
3877 procedure Check_Prefix is
3878 begin
3879 -- The subtlety here is that in doing a recursive call to check
3880 -- the prefix, we have to decide what to do in the case where we
3881 -- don't find any specific indication of an alignment problem.
3883 -- At the outer level, we normally set Unknown as the result in
3884 -- this case, since we can only set Known_Compatible if we really
3885 -- know that the alignment value is OK, but for the recursive
3886 -- call, in the case where the types match, and we have not
3887 -- specified a peculiar alignment for the object, we are only
3888 -- concerned about suspicious rep clauses, the default case does
3889 -- not affect us, since the compiler will, in the absence of such
3890 -- rep clauses, ensure that the alignment is correct.
3892 if Default = Known_Compatible
3893 or else
3894 (Etype (Obj) = Etype (Expr)
3895 and then (Unknown_Alignment (Obj)
3896 or else
3897 Alignment (Obj) = Alignment (Etype (Obj))))
3898 then
3899 Set_Result
3900 (Has_Compatible_Alignment_Internal
3901 (Obj, Prefix (Expr), Known_Compatible));
3903 -- In all other cases, we need a full check on the prefix
3905 else
3906 Set_Result
3907 (Has_Compatible_Alignment_Internal
3908 (Obj, Prefix (Expr), Unknown));
3909 end if;
3910 end Check_Prefix;
3912 ----------------
3913 -- Set_Result --
3914 ----------------
3916 procedure Set_Result (R : Alignment_Result) is
3917 begin
3918 if R > Result then
3919 Result := R;
3920 end if;
3921 end Set_Result;
3923 -- Start of processing for Has_Compatible_Alignment_Internal
3925 begin
3926 -- If Expr is a selected component, we must make sure there is no
3927 -- potentially troublesome component clause, and that the record is
3928 -- not packed.
3930 if Nkind (Expr) = N_Selected_Component then
3932 -- Packed record always generate unknown alignment
3934 if Is_Packed (Etype (Prefix (Expr))) then
3935 Set_Result (Unknown);
3936 end if;
3938 -- Check possible bad component offset and check prefix
3940 Check_Offset
3941 (Component_Bit_Offset (Entity (Selector_Name (Expr))));
3942 Check_Prefix;
3944 -- If Expr is an indexed component, we must make sure there is no
3945 -- potentially troublesome Component_Size clause and that the array
3946 -- is not bit-packed.
3948 elsif Nkind (Expr) = N_Indexed_Component then
3950 -- Bit packed array always generates unknown alignment
3952 if Is_Bit_Packed_Array (Etype (Prefix (Expr))) then
3953 Set_Result (Unknown);
3954 end if;
3956 -- Check possible bad component size and check prefix
3958 Check_Offset (Component_Size (Etype (Prefix (Expr))));
3959 Check_Prefix;
3960 end if;
3962 -- Case where we know the alignment of the object
3964 if Known_Alignment (Obj) then
3965 declare
3966 ObjA : constant Uint := Alignment (Obj);
3967 ExpA : Uint := No_Uint;
3968 SizA : Uint := No_Uint;
3970 begin
3971 -- If alignment of Obj is 1, then we are always OK
3973 if ObjA = 1 then
3974 Set_Result (Known_Compatible);
3976 -- Alignment of Obj is greater than 1, so we need to check
3978 else
3979 -- See if Expr is an object with known alignment
3981 if Is_Entity_Name (Expr)
3982 and then Known_Alignment (Entity (Expr))
3983 then
3984 ExpA := Alignment (Entity (Expr));
3986 -- Otherwise, we can use the alignment of the type of
3987 -- Expr given that we already checked for
3988 -- discombobulating rep clauses for the cases of indexed
3989 -- and selected components above.
3991 elsif Known_Alignment (Etype (Expr)) then
3992 ExpA := Alignment (Etype (Expr));
3993 end if;
3995 -- If we got an alignment, see if it is acceptable
3997 if ExpA /= No_Uint then
3998 if ExpA < ObjA then
3999 Set_Result (Known_Incompatible);
4000 end if;
4002 -- Case of Expr alignment unknown
4004 else
4005 Set_Result (Default);
4006 end if;
4008 -- See if size is given. If so, check that it is not too
4009 -- small for the required alignment.
4010 -- See if Expr is an object with known alignment
4012 if Is_Entity_Name (Expr)
4013 and then Known_Static_Esize (Entity (Expr))
4014 then
4015 SizA := Esize (Entity (Expr));
4017 -- Otherwise, we check the object size of the Expr type
4019 elsif Known_Static_Esize (Etype (Expr)) then
4020 SizA := Esize (Etype (Expr));
4021 end if;
4023 -- If we got a size, see if it is a multiple of the Obj
4024 -- alignment, if not, then the alignment cannot be
4025 -- acceptable, since the size is always a multiple of the
4026 -- alignment.
4028 if SizA /= No_Uint then
4029 if SizA mod (ObjA * Ttypes.System_Storage_Unit) /= 0 then
4030 Set_Result (Known_Incompatible);
4031 end if;
4032 end if;
4033 end if;
4034 end;
4036 -- If we can't find the result by direct comparison of alignment
4037 -- values, then there is still one case that we can determine known
4038 -- result, and that is when we can determine that the types are the
4039 -- same, and no alignments are specified. Then we known that the
4040 -- alignments are compatible, even if we don't know the alignment
4041 -- value in the front end.
4043 elsif Etype (Obj) = Etype (Expr) then
4045 -- Types are the same, but we have to check for possible size
4046 -- and alignments on the Expr object that may make the alignment
4047 -- different, even though the types are the same.
4049 if Is_Entity_Name (Expr) then
4051 -- First check alignment of the Expr object. Any alignment less
4052 -- than Maximum_Alignment is worrisome since this is the case
4053 -- where we do not know the alignment of Obj.
4055 if Known_Alignment (Entity (Expr))
4056 and then
4057 UI_To_Int (Alignment (Entity (Expr)))
4058 < Ttypes.Maximum_Alignment
4059 then
4060 Set_Result (Unknown);
4062 -- Now check size of Expr object. Any size that is not an
4063 -- even multiple of Maximum_Alignment is also worrisome
4064 -- since it may cause the alignment of the object to be less
4065 -- than the alignment of the type.
4067 elsif Known_Static_Esize (Entity (Expr))
4068 and then
4069 (UI_To_Int (Esize (Entity (Expr))) mod
4070 (Ttypes.Maximum_Alignment * Ttypes.System_Storage_Unit))
4071 /= 0
4072 then
4073 Set_Result (Unknown);
4075 -- Otherwise same type is decisive
4077 else
4078 Set_Result (Known_Compatible);
4079 end if;
4080 end if;
4082 -- Another case to deal with is when there is an explicit size or
4083 -- alignment clause when the types are not the same. If so, then the
4084 -- result is Unknown. We don't need to do this test if the Default is
4085 -- Unknown, since that result will be set in any case.
4087 elsif Default /= Unknown
4088 and then (Has_Size_Clause (Etype (Expr))
4089 or else
4090 Has_Alignment_Clause (Etype (Expr)))
4091 then
4092 Set_Result (Unknown);
4094 -- If no indication found, set default
4096 else
4097 Set_Result (Default);
4098 end if;
4100 -- Return worst result found
4102 return Result;
4103 end Has_Compatible_Alignment_Internal;
4105 -- Start of processing for Has_Compatible_Alignment
4107 begin
4108 -- If Obj has no specified alignment, then set alignment from the type
4109 -- alignment. Perhaps we should always do this, but for sure we should
4110 -- do it when there is an address clause since we can do more if the
4111 -- alignment is known.
4113 if Unknown_Alignment (Obj) then
4114 Set_Alignment (Obj, Alignment (Etype (Obj)));
4115 end if;
4117 -- Now do the internal call that does all the work
4119 return Has_Compatible_Alignment_Internal (Obj, Expr, Unknown);
4120 end Has_Compatible_Alignment;
4122 ----------------------
4123 -- Has_Declarations --
4124 ----------------------
4126 function Has_Declarations (N : Node_Id) return Boolean is
4127 K : constant Node_Kind := Nkind (N);
4128 begin
4129 return K = N_Accept_Statement
4130 or else K = N_Block_Statement
4131 or else K = N_Compilation_Unit_Aux
4132 or else K = N_Entry_Body
4133 or else K = N_Package_Body
4134 or else K = N_Protected_Body
4135 or else K = N_Subprogram_Body
4136 or else K = N_Task_Body
4137 or else K = N_Package_Specification;
4138 end Has_Declarations;
4140 -------------------------------------------
4141 -- Has_Discriminant_Dependent_Constraint --
4142 -------------------------------------------
4144 function Has_Discriminant_Dependent_Constraint
4145 (Comp : Entity_Id) return Boolean
4147 Comp_Decl : constant Node_Id := Parent (Comp);
4148 Subt_Indic : constant Node_Id :=
4149 Subtype_Indication (Component_Definition (Comp_Decl));
4150 Constr : Node_Id;
4151 Assn : Node_Id;
4153 begin
4154 if Nkind (Subt_Indic) = N_Subtype_Indication then
4155 Constr := Constraint (Subt_Indic);
4157 if Nkind (Constr) = N_Index_Or_Discriminant_Constraint then
4158 Assn := First (Constraints (Constr));
4159 while Present (Assn) loop
4160 case Nkind (Assn) is
4161 when N_Subtype_Indication |
4162 N_Range |
4163 N_Identifier
4165 if Depends_On_Discriminant (Assn) then
4166 return True;
4167 end if;
4169 when N_Discriminant_Association =>
4170 if Depends_On_Discriminant (Expression (Assn)) then
4171 return True;
4172 end if;
4174 when others =>
4175 null;
4177 end case;
4179 Next (Assn);
4180 end loop;
4181 end if;
4182 end if;
4184 return False;
4185 end Has_Discriminant_Dependent_Constraint;
4187 --------------------
4188 -- Has_Infinities --
4189 --------------------
4191 function Has_Infinities (E : Entity_Id) return Boolean is
4192 begin
4193 return
4194 Is_Floating_Point_Type (E)
4195 and then Nkind (Scalar_Range (E)) = N_Range
4196 and then Includes_Infinities (Scalar_Range (E));
4197 end Has_Infinities;
4199 --------------------
4200 -- Has_Interfaces --
4201 --------------------
4203 function Has_Interfaces
4204 (T : Entity_Id;
4205 Use_Full_View : Boolean := True) return Boolean
4207 Typ : Entity_Id;
4209 begin
4210 -- Handle concurrent types
4212 if Is_Concurrent_Type (T) then
4213 Typ := Corresponding_Record_Type (T);
4214 else
4215 Typ := T;
4216 end if;
4218 if not Present (Typ)
4219 or else not Is_Record_Type (Typ)
4220 or else not Is_Tagged_Type (Typ)
4221 then
4222 return False;
4223 end if;
4225 -- Handle private types
4227 if Use_Full_View
4228 and then Present (Full_View (Typ))
4229 then
4230 Typ := Full_View (Typ);
4231 end if;
4233 -- Handle concurrent record types
4235 if Is_Concurrent_Record_Type (Typ)
4236 and then Is_Non_Empty_List (Abstract_Interface_List (Typ))
4237 then
4238 return True;
4239 end if;
4241 loop
4242 if Is_Interface (Typ)
4243 or else
4244 (Is_Record_Type (Typ)
4245 and then Present (Interfaces (Typ))
4246 and then not Is_Empty_Elmt_List (Interfaces (Typ)))
4247 then
4248 return True;
4249 end if;
4251 exit when Etype (Typ) = Typ
4253 -- Handle private types
4255 or else (Present (Full_View (Etype (Typ)))
4256 and then Full_View (Etype (Typ)) = Typ)
4258 -- Protect the frontend against wrong source with cyclic
4259 -- derivations
4261 or else Etype (Typ) = T;
4263 -- Climb to the ancestor type handling private types
4265 if Present (Full_View (Etype (Typ))) then
4266 Typ := Full_View (Etype (Typ));
4267 else
4268 Typ := Etype (Typ);
4269 end if;
4270 end loop;
4272 return False;
4273 end Has_Interfaces;
4275 ------------------------
4276 -- Has_Null_Exclusion --
4277 ------------------------
4279 function Has_Null_Exclusion (N : Node_Id) return Boolean is
4280 begin
4281 case Nkind (N) is
4282 when N_Access_Definition |
4283 N_Access_Function_Definition |
4284 N_Access_Procedure_Definition |
4285 N_Access_To_Object_Definition |
4286 N_Allocator |
4287 N_Derived_Type_Definition |
4288 N_Function_Specification |
4289 N_Subtype_Declaration =>
4290 return Null_Exclusion_Present (N);
4292 when N_Component_Definition |
4293 N_Formal_Object_Declaration |
4294 N_Object_Renaming_Declaration =>
4295 if Present (Subtype_Mark (N)) then
4296 return Null_Exclusion_Present (N);
4297 else pragma Assert (Present (Access_Definition (N)));
4298 return Null_Exclusion_Present (Access_Definition (N));
4299 end if;
4301 when N_Discriminant_Specification =>
4302 if Nkind (Discriminant_Type (N)) = N_Access_Definition then
4303 return Null_Exclusion_Present (Discriminant_Type (N));
4304 else
4305 return Null_Exclusion_Present (N);
4306 end if;
4308 when N_Object_Declaration =>
4309 if Nkind (Object_Definition (N)) = N_Access_Definition then
4310 return Null_Exclusion_Present (Object_Definition (N));
4311 else
4312 return Null_Exclusion_Present (N);
4313 end if;
4315 when N_Parameter_Specification =>
4316 if Nkind (Parameter_Type (N)) = N_Access_Definition then
4317 return Null_Exclusion_Present (Parameter_Type (N));
4318 else
4319 return Null_Exclusion_Present (N);
4320 end if;
4322 when others =>
4323 return False;
4325 end case;
4326 end Has_Null_Exclusion;
4328 ------------------------
4329 -- Has_Null_Extension --
4330 ------------------------
4332 function Has_Null_Extension (T : Entity_Id) return Boolean is
4333 B : constant Entity_Id := Base_Type (T);
4334 Comps : Node_Id;
4335 Ext : Node_Id;
4337 begin
4338 if Nkind (Parent (B)) = N_Full_Type_Declaration
4339 and then Present (Record_Extension_Part (Type_Definition (Parent (B))))
4340 then
4341 Ext := Record_Extension_Part (Type_Definition (Parent (B)));
4343 if Present (Ext) then
4344 if Null_Present (Ext) then
4345 return True;
4346 else
4347 Comps := Component_List (Ext);
4349 -- The null component list is rewritten during analysis to
4350 -- include the parent component. Any other component indicates
4351 -- that the extension was not originally null.
4353 return Null_Present (Comps)
4354 or else No (Next (First (Component_Items (Comps))));
4355 end if;
4356 else
4357 return False;
4358 end if;
4360 else
4361 return False;
4362 end if;
4363 end Has_Null_Extension;
4365 -------------------------------
4366 -- Has_Overriding_Initialize --
4367 -------------------------------
4369 function Has_Overriding_Initialize (T : Entity_Id) return Boolean is
4370 BT : constant Entity_Id := Base_Type (T);
4371 Comp : Entity_Id;
4372 P : Elmt_Id;
4374 begin
4375 if Is_Controlled (BT) then
4377 -- For derived types, check immediate ancestor, excluding
4378 -- Controlled itself.
4380 if Is_Derived_Type (BT)
4381 and then not In_Predefined_Unit (Etype (BT))
4382 and then Has_Overriding_Initialize (Etype (BT))
4383 then
4384 return True;
4386 elsif Present (Primitive_Operations (BT)) then
4387 P := First_Elmt (Primitive_Operations (BT));
4388 while Present (P) loop
4389 if Chars (Node (P)) = Name_Initialize
4390 and then Comes_From_Source (Node (P))
4391 then
4392 return True;
4393 end if;
4395 Next_Elmt (P);
4396 end loop;
4397 end if;
4399 return False;
4401 elsif Has_Controlled_Component (BT) then
4402 Comp := First_Component (BT);
4403 while Present (Comp) loop
4404 if Has_Overriding_Initialize (Etype (Comp)) then
4405 return True;
4406 end if;
4408 Next_Component (Comp);
4409 end loop;
4411 return False;
4413 else
4414 return False;
4415 end if;
4416 end Has_Overriding_Initialize;
4418 --------------------------------------
4419 -- Has_Preelaborable_Initialization --
4420 --------------------------------------
4422 function Has_Preelaborable_Initialization (E : Entity_Id) return Boolean is
4423 Has_PE : Boolean;
4425 procedure Check_Components (E : Entity_Id);
4426 -- Check component/discriminant chain, sets Has_PE False if a component
4427 -- or discriminant does not meet the preelaborable initialization rules.
4429 ----------------------
4430 -- Check_Components --
4431 ----------------------
4433 procedure Check_Components (E : Entity_Id) is
4434 Ent : Entity_Id;
4435 Exp : Node_Id;
4437 function Is_Preelaborable_Expression (N : Node_Id) return Boolean;
4438 -- Returns True if and only if the expression denoted by N does not
4439 -- violate restrictions on preelaborable constructs (RM-10.2.1(5-9)).
4441 ---------------------------------
4442 -- Is_Preelaborable_Expression --
4443 ---------------------------------
4445 function Is_Preelaborable_Expression (N : Node_Id) return Boolean is
4446 Exp : Node_Id;
4447 Assn : Node_Id;
4448 Choice : Node_Id;
4449 Comp_Type : Entity_Id;
4450 Is_Array_Aggr : Boolean;
4452 begin
4453 if Is_Static_Expression (N) then
4454 return True;
4456 elsif Nkind (N) = N_Null then
4457 return True;
4459 -- Attributes are allowed in general, even if their prefix is a
4460 -- formal type. (It seems that certain attributes known not to be
4461 -- static might not be allowed, but there are no rules to prevent
4462 -- them.)
4464 elsif Nkind (N) = N_Attribute_Reference then
4465 return True;
4467 -- The name of a discriminant evaluated within its parent type is
4468 -- defined to be preelaborable (10.2.1(8)). Note that we test for
4469 -- names that denote discriminals as well as discriminants to
4470 -- catch references occurring within init procs.
4472 elsif Is_Entity_Name (N)
4473 and then
4474 (Ekind (Entity (N)) = E_Discriminant
4475 or else
4476 ((Ekind (Entity (N)) = E_Constant
4477 or else Ekind (Entity (N)) = E_In_Parameter)
4478 and then Present (Discriminal_Link (Entity (N)))))
4479 then
4480 return True;
4482 elsif Nkind (N) = N_Qualified_Expression then
4483 return Is_Preelaborable_Expression (Expression (N));
4485 -- For aggregates we have to check that each of the associations
4486 -- is preelaborable.
4488 elsif Nkind (N) = N_Aggregate
4489 or else Nkind (N) = N_Extension_Aggregate
4490 then
4491 Is_Array_Aggr := Is_Array_Type (Etype (N));
4493 if Is_Array_Aggr then
4494 Comp_Type := Component_Type (Etype (N));
4495 end if;
4497 -- Check the ancestor part of extension aggregates, which must
4498 -- be either the name of a type that has preelaborable init or
4499 -- an expression that is preelaborable.
4501 if Nkind (N) = N_Extension_Aggregate then
4502 declare
4503 Anc_Part : constant Node_Id := Ancestor_Part (N);
4505 begin
4506 if Is_Entity_Name (Anc_Part)
4507 and then Is_Type (Entity (Anc_Part))
4508 then
4509 if not Has_Preelaborable_Initialization
4510 (Entity (Anc_Part))
4511 then
4512 return False;
4513 end if;
4515 elsif not Is_Preelaborable_Expression (Anc_Part) then
4516 return False;
4517 end if;
4518 end;
4519 end if;
4521 -- Check positional associations
4523 Exp := First (Expressions (N));
4524 while Present (Exp) loop
4525 if not Is_Preelaborable_Expression (Exp) then
4526 return False;
4527 end if;
4529 Next (Exp);
4530 end loop;
4532 -- Check named associations
4534 Assn := First (Component_Associations (N));
4535 while Present (Assn) loop
4536 Choice := First (Choices (Assn));
4537 while Present (Choice) loop
4538 if Is_Array_Aggr then
4539 if Nkind (Choice) = N_Others_Choice then
4540 null;
4542 elsif Nkind (Choice) = N_Range then
4543 if not Is_Static_Range (Choice) then
4544 return False;
4545 end if;
4547 elsif not Is_Static_Expression (Choice) then
4548 return False;
4549 end if;
4551 else
4552 Comp_Type := Etype (Choice);
4553 end if;
4555 Next (Choice);
4556 end loop;
4558 -- If the association has a <> at this point, then we have
4559 -- to check whether the component's type has preelaborable
4560 -- initialization. Note that this only occurs when the
4561 -- association's corresponding component does not have a
4562 -- default expression, the latter case having already been
4563 -- expanded as an expression for the association.
4565 if Box_Present (Assn) then
4566 if not Has_Preelaborable_Initialization (Comp_Type) then
4567 return False;
4568 end if;
4570 -- In the expression case we check whether the expression
4571 -- is preelaborable.
4573 elsif
4574 not Is_Preelaborable_Expression (Expression (Assn))
4575 then
4576 return False;
4577 end if;
4579 Next (Assn);
4580 end loop;
4582 -- If we get here then aggregate as a whole is preelaborable
4584 return True;
4586 -- All other cases are not preelaborable
4588 else
4589 return False;
4590 end if;
4591 end Is_Preelaborable_Expression;
4593 -- Start of processing for Check_Components
4595 begin
4596 -- Loop through entities of record or protected type
4598 Ent := E;
4599 while Present (Ent) loop
4601 -- We are interested only in components and discriminants
4603 if Ekind (Ent) = E_Component
4604 or else
4605 Ekind (Ent) = E_Discriminant
4606 then
4607 -- Get default expression if any. If there is no declaration
4608 -- node, it means we have an internal entity. The parent and
4609 -- tag fields are examples of such entities. For these cases,
4610 -- we just test the type of the entity.
4612 if Present (Declaration_Node (Ent)) then
4613 Exp := Expression (Declaration_Node (Ent));
4614 else
4615 Exp := Empty;
4616 end if;
4618 -- A component has PI if it has no default expression and the
4619 -- component type has PI.
4621 if No (Exp) then
4622 if not Has_Preelaborable_Initialization (Etype (Ent)) then
4623 Has_PE := False;
4624 exit;
4625 end if;
4627 -- Require the default expression to be preelaborable
4629 elsif not Is_Preelaborable_Expression (Exp) then
4630 Has_PE := False;
4631 exit;
4632 end if;
4633 end if;
4635 Next_Entity (Ent);
4636 end loop;
4637 end Check_Components;
4639 -- Start of processing for Has_Preelaborable_Initialization
4641 begin
4642 -- Immediate return if already marked as known preelaborable init. This
4643 -- covers types for which this function has already been called once
4644 -- and returned True (in which case the result is cached), and also
4645 -- types to which a pragma Preelaborable_Initialization applies.
4647 if Known_To_Have_Preelab_Init (E) then
4648 return True;
4649 end if;
4651 -- If the type is a subtype representing a generic actual type, then
4652 -- test whether its base type has preelaborable initialization since
4653 -- the subtype representing the actual does not inherit this attribute
4654 -- from the actual or formal. (but maybe it should???)
4656 if Is_Generic_Actual_Type (E) then
4657 return Has_Preelaborable_Initialization (Base_Type (E));
4658 end if;
4660 -- All elementary types have preelaborable initialization
4662 if Is_Elementary_Type (E) then
4663 Has_PE := True;
4665 -- Array types have PI if the component type has PI
4667 elsif Is_Array_Type (E) then
4668 Has_PE := Has_Preelaborable_Initialization (Component_Type (E));
4670 -- A derived type has preelaborable initialization if its parent type
4671 -- has preelaborable initialization and (in the case of a derived record
4672 -- extension) if the non-inherited components all have preelaborable
4673 -- initialization. However, a user-defined controlled type with an
4674 -- overriding Initialize procedure does not have preelaborable
4675 -- initialization.
4677 elsif Is_Derived_Type (E) then
4679 -- If the derived type is a private extension then it doesn't have
4680 -- preelaborable initialization.
4682 if Ekind (Base_Type (E)) = E_Record_Type_With_Private then
4683 return False;
4684 end if;
4686 -- First check whether ancestor type has preelaborable initialization
4688 Has_PE := Has_Preelaborable_Initialization (Etype (Base_Type (E)));
4690 -- If OK, check extension components (if any)
4692 if Has_PE and then Is_Record_Type (E) then
4693 Check_Components (First_Entity (E));
4694 end if;
4696 -- Check specifically for 10.2.1(11.4/2) exception: a controlled type
4697 -- with a user defined Initialize procedure does not have PI.
4699 if Has_PE
4700 and then Is_Controlled (E)
4701 and then Has_Overriding_Initialize (E)
4702 then
4703 Has_PE := False;
4704 end if;
4706 -- Private types not derived from a type having preelaborable init and
4707 -- that are not marked with pragma Preelaborable_Initialization do not
4708 -- have preelaborable initialization.
4710 elsif Is_Private_Type (E) then
4711 return False;
4713 -- Record type has PI if it is non private and all components have PI
4715 elsif Is_Record_Type (E) then
4716 Has_PE := True;
4717 Check_Components (First_Entity (E));
4719 -- Protected types must not have entries, and components must meet
4720 -- same set of rules as for record components.
4722 elsif Is_Protected_Type (E) then
4723 if Has_Entries (E) then
4724 Has_PE := False;
4725 else
4726 Has_PE := True;
4727 Check_Components (First_Entity (E));
4728 Check_Components (First_Private_Entity (E));
4729 end if;
4731 -- Type System.Address always has preelaborable initialization
4733 elsif Is_RTE (E, RE_Address) then
4734 Has_PE := True;
4736 -- In all other cases, type does not have preelaborable initialization
4738 else
4739 return False;
4740 end if;
4742 -- If type has preelaborable initialization, cache result
4744 if Has_PE then
4745 Set_Known_To_Have_Preelab_Init (E);
4746 end if;
4748 return Has_PE;
4749 end Has_Preelaborable_Initialization;
4751 ---------------------------
4752 -- Has_Private_Component --
4753 ---------------------------
4755 function Has_Private_Component (Type_Id : Entity_Id) return Boolean is
4756 Btype : Entity_Id := Base_Type (Type_Id);
4757 Component : Entity_Id;
4759 begin
4760 if Error_Posted (Type_Id)
4761 or else Error_Posted (Btype)
4762 then
4763 return False;
4764 end if;
4766 if Is_Class_Wide_Type (Btype) then
4767 Btype := Root_Type (Btype);
4768 end if;
4770 if Is_Private_Type (Btype) then
4771 declare
4772 UT : constant Entity_Id := Underlying_Type (Btype);
4773 begin
4774 if No (UT) then
4775 if No (Full_View (Btype)) then
4776 return not Is_Generic_Type (Btype)
4777 and then not Is_Generic_Type (Root_Type (Btype));
4778 else
4779 return not Is_Generic_Type (Root_Type (Full_View (Btype)));
4780 end if;
4781 else
4782 return not Is_Frozen (UT) and then Has_Private_Component (UT);
4783 end if;
4784 end;
4786 elsif Is_Array_Type (Btype) then
4787 return Has_Private_Component (Component_Type (Btype));
4789 elsif Is_Record_Type (Btype) then
4790 Component := First_Component (Btype);
4791 while Present (Component) loop
4792 if Has_Private_Component (Etype (Component)) then
4793 return True;
4794 end if;
4796 Next_Component (Component);
4797 end loop;
4799 return False;
4801 elsif Is_Protected_Type (Btype)
4802 and then Present (Corresponding_Record_Type (Btype))
4803 then
4804 return Has_Private_Component (Corresponding_Record_Type (Btype));
4806 else
4807 return False;
4808 end if;
4809 end Has_Private_Component;
4811 ----------------
4812 -- Has_Stream --
4813 ----------------
4815 function Has_Stream (T : Entity_Id) return Boolean is
4816 E : Entity_Id;
4818 begin
4819 if No (T) then
4820 return False;
4822 elsif Is_RTE (Root_Type (T), RE_Root_Stream_Type) then
4823 return True;
4825 elsif Is_Array_Type (T) then
4826 return Has_Stream (Component_Type (T));
4828 elsif Is_Record_Type (T) then
4829 E := First_Component (T);
4830 while Present (E) loop
4831 if Has_Stream (Etype (E)) then
4832 return True;
4833 else
4834 Next_Component (E);
4835 end if;
4836 end loop;
4838 return False;
4840 elsif Is_Private_Type (T) then
4841 return Has_Stream (Underlying_Type (T));
4843 else
4844 return False;
4845 end if;
4846 end Has_Stream;
4848 --------------------------
4849 -- Has_Tagged_Component --
4850 --------------------------
4852 function Has_Tagged_Component (Typ : Entity_Id) return Boolean is
4853 Comp : Entity_Id;
4855 begin
4856 if Is_Private_Type (Typ)
4857 and then Present (Underlying_Type (Typ))
4858 then
4859 return Has_Tagged_Component (Underlying_Type (Typ));
4861 elsif Is_Array_Type (Typ) then
4862 return Has_Tagged_Component (Component_Type (Typ));
4864 elsif Is_Tagged_Type (Typ) then
4865 return True;
4867 elsif Is_Record_Type (Typ) then
4868 Comp := First_Component (Typ);
4869 while Present (Comp) loop
4870 if Has_Tagged_Component (Etype (Comp)) then
4871 return True;
4872 end if;
4874 Next_Component (Comp);
4875 end loop;
4877 return False;
4879 else
4880 return False;
4881 end if;
4882 end Has_Tagged_Component;
4884 --------------------------
4885 -- Implements_Interface --
4886 --------------------------
4888 function Implements_Interface
4889 (Typ_Ent : Entity_Id;
4890 Iface_Ent : Entity_Id;
4891 Exclude_Parents : Boolean := False) return Boolean
4893 Ifaces_List : Elist_Id;
4894 Elmt : Elmt_Id;
4895 Iface : Entity_Id;
4896 Typ : Entity_Id;
4898 begin
4899 if Is_Class_Wide_Type (Typ_Ent) then
4900 Typ := Etype (Typ_Ent);
4901 else
4902 Typ := Typ_Ent;
4903 end if;
4905 if Is_Class_Wide_Type (Iface_Ent) then
4906 Iface := Etype (Iface_Ent);
4907 else
4908 Iface := Iface_Ent;
4909 end if;
4911 if not Has_Interfaces (Typ) then
4912 return False;
4913 end if;
4915 Collect_Interfaces (Typ, Ifaces_List);
4917 Elmt := First_Elmt (Ifaces_List);
4918 while Present (Elmt) loop
4919 if Is_Ancestor (Node (Elmt), Typ)
4920 and then Exclude_Parents
4921 then
4922 null;
4924 elsif Node (Elmt) = Iface then
4925 return True;
4926 end if;
4928 Next_Elmt (Elmt);
4929 end loop;
4931 return False;
4932 end Implements_Interface;
4934 -----------------
4935 -- In_Instance --
4936 -----------------
4938 function In_Instance return Boolean is
4939 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4940 S : Entity_Id;
4942 begin
4943 S := Current_Scope;
4944 while Present (S)
4945 and then S /= Standard_Standard
4946 loop
4947 if (Ekind (S) = E_Function
4948 or else Ekind (S) = E_Package
4949 or else Ekind (S) = E_Procedure)
4950 and then Is_Generic_Instance (S)
4951 then
4952 -- A child instance is always compiled in the context of a parent
4953 -- instance. Nevertheless, the actuals are not analyzed in an
4954 -- instance context. We detect this case by examining the current
4955 -- compilation unit, which must be a child instance, and checking
4956 -- that it is not currently on the scope stack.
4958 if Is_Child_Unit (Curr_Unit)
4959 and then
4960 Nkind (Unit (Cunit (Current_Sem_Unit)))
4961 = N_Package_Instantiation
4962 and then not In_Open_Scopes (Curr_Unit)
4963 then
4964 return False;
4965 else
4966 return True;
4967 end if;
4968 end if;
4970 S := Scope (S);
4971 end loop;
4973 return False;
4974 end In_Instance;
4976 ----------------------
4977 -- In_Instance_Body --
4978 ----------------------
4980 function In_Instance_Body return Boolean is
4981 S : Entity_Id;
4983 begin
4984 S := Current_Scope;
4985 while Present (S)
4986 and then S /= Standard_Standard
4987 loop
4988 if (Ekind (S) = E_Function
4989 or else Ekind (S) = E_Procedure)
4990 and then Is_Generic_Instance (S)
4991 then
4992 return True;
4994 elsif Ekind (S) = E_Package
4995 and then In_Package_Body (S)
4996 and then Is_Generic_Instance (S)
4997 then
4998 return True;
4999 end if;
5001 S := Scope (S);
5002 end loop;
5004 return False;
5005 end In_Instance_Body;
5007 -----------------------------
5008 -- In_Instance_Not_Visible --
5009 -----------------------------
5011 function In_Instance_Not_Visible return Boolean is
5012 S : Entity_Id;
5014 begin
5015 S := Current_Scope;
5016 while Present (S)
5017 and then S /= Standard_Standard
5018 loop
5019 if (Ekind (S) = E_Function
5020 or else Ekind (S) = E_Procedure)
5021 and then Is_Generic_Instance (S)
5022 then
5023 return True;
5025 elsif Ekind (S) = E_Package
5026 and then (In_Package_Body (S) or else In_Private_Part (S))
5027 and then Is_Generic_Instance (S)
5028 then
5029 return True;
5030 end if;
5032 S := Scope (S);
5033 end loop;
5035 return False;
5036 end In_Instance_Not_Visible;
5038 ------------------------------
5039 -- In_Instance_Visible_Part --
5040 ------------------------------
5042 function In_Instance_Visible_Part return Boolean is
5043 S : Entity_Id;
5045 begin
5046 S := Current_Scope;
5047 while Present (S)
5048 and then S /= Standard_Standard
5049 loop
5050 if Ekind (S) = E_Package
5051 and then Is_Generic_Instance (S)
5052 and then not In_Package_Body (S)
5053 and then not In_Private_Part (S)
5054 then
5055 return True;
5056 end if;
5058 S := Scope (S);
5059 end loop;
5061 return False;
5062 end In_Instance_Visible_Part;
5064 ---------------------
5065 -- In_Package_Body --
5066 ---------------------
5068 function In_Package_Body return Boolean is
5069 S : Entity_Id;
5071 begin
5072 S := Current_Scope;
5073 while Present (S)
5074 and then S /= Standard_Standard
5075 loop
5076 if Ekind (S) = E_Package
5077 and then In_Package_Body (S)
5078 then
5079 return True;
5080 else
5081 S := Scope (S);
5082 end if;
5083 end loop;
5085 return False;
5086 end In_Package_Body;
5088 --------------------------------
5089 -- In_Parameter_Specification --
5090 --------------------------------
5092 function In_Parameter_Specification (N : Node_Id) return Boolean is
5093 PN : Node_Id;
5095 begin
5096 PN := Parent (N);
5097 while Present (PN) loop
5098 if Nkind (PN) = N_Parameter_Specification then
5099 return True;
5100 end if;
5102 PN := Parent (PN);
5103 end loop;
5105 return False;
5106 end In_Parameter_Specification;
5108 --------------------------------------
5109 -- In_Subprogram_Or_Concurrent_Unit --
5110 --------------------------------------
5112 function In_Subprogram_Or_Concurrent_Unit return Boolean is
5113 E : Entity_Id;
5114 K : Entity_Kind;
5116 begin
5117 -- Use scope chain to check successively outer scopes
5119 E := Current_Scope;
5120 loop
5121 K := Ekind (E);
5123 if K in Subprogram_Kind
5124 or else K in Concurrent_Kind
5125 or else K in Generic_Subprogram_Kind
5126 then
5127 return True;
5129 elsif E = Standard_Standard then
5130 return False;
5131 end if;
5133 E := Scope (E);
5134 end loop;
5135 end In_Subprogram_Or_Concurrent_Unit;
5137 ---------------------
5138 -- In_Visible_Part --
5139 ---------------------
5141 function In_Visible_Part (Scope_Id : Entity_Id) return Boolean is
5142 begin
5143 return
5144 Is_Package_Or_Generic_Package (Scope_Id)
5145 and then In_Open_Scopes (Scope_Id)
5146 and then not In_Package_Body (Scope_Id)
5147 and then not In_Private_Part (Scope_Id);
5148 end In_Visible_Part;
5150 ---------------------------------
5151 -- Insert_Explicit_Dereference --
5152 ---------------------------------
5154 procedure Insert_Explicit_Dereference (N : Node_Id) is
5155 New_Prefix : constant Node_Id := Relocate_Node (N);
5156 Ent : Entity_Id := Empty;
5157 Pref : Node_Id;
5158 I : Interp_Index;
5159 It : Interp;
5160 T : Entity_Id;
5162 begin
5163 Save_Interps (N, New_Prefix);
5164 Rewrite (N,
5165 Make_Explicit_Dereference (Sloc (N),
5166 Prefix => New_Prefix));
5168 Set_Etype (N, Designated_Type (Etype (New_Prefix)));
5170 if Is_Overloaded (New_Prefix) then
5172 -- The deference is also overloaded, and its interpretations are the
5173 -- designated types of the interpretations of the original node.
5175 Set_Etype (N, Any_Type);
5177 Get_First_Interp (New_Prefix, I, It);
5178 while Present (It.Nam) loop
5179 T := It.Typ;
5181 if Is_Access_Type (T) then
5182 Add_One_Interp (N, Designated_Type (T), Designated_Type (T));
5183 end if;
5185 Get_Next_Interp (I, It);
5186 end loop;
5188 End_Interp_List;
5190 else
5191 -- Prefix is unambiguous: mark the original prefix (which might
5192 -- Come_From_Source) as a reference, since the new (relocated) one
5193 -- won't be taken into account.
5195 if Is_Entity_Name (New_Prefix) then
5196 Ent := Entity (New_Prefix);
5198 -- For a retrieval of a subcomponent of some composite object,
5199 -- retrieve the ultimate entity if there is one.
5201 elsif Nkind (New_Prefix) = N_Selected_Component
5202 or else Nkind (New_Prefix) = N_Indexed_Component
5203 then
5204 Pref := Prefix (New_Prefix);
5205 while Present (Pref)
5206 and then
5207 (Nkind (Pref) = N_Selected_Component
5208 or else Nkind (Pref) = N_Indexed_Component)
5209 loop
5210 Pref := Prefix (Pref);
5211 end loop;
5213 if Present (Pref) and then Is_Entity_Name (Pref) then
5214 Ent := Entity (Pref);
5215 end if;
5216 end if;
5218 if Present (Ent) then
5219 Generate_Reference (Ent, New_Prefix);
5220 end if;
5221 end if;
5222 end Insert_Explicit_Dereference;
5224 ------------------------------------------
5225 -- Inspect_Deferred_Constant_Completion --
5226 ------------------------------------------
5228 procedure Inspect_Deferred_Constant_Completion (Decls : List_Id) is
5229 Decl : Node_Id;
5231 begin
5232 Decl := First (Decls);
5233 while Present (Decl) loop
5235 -- Deferred constant signature
5237 if Nkind (Decl) = N_Object_Declaration
5238 and then Constant_Present (Decl)
5239 and then No (Expression (Decl))
5241 -- No need to check internally generated constants
5243 and then Comes_From_Source (Decl)
5245 -- The constant is not completed. A full object declaration
5246 -- or a pragma Import complete a deferred constant.
5248 and then not Has_Completion (Defining_Identifier (Decl))
5249 then
5250 Error_Msg_N
5251 ("constant declaration requires initialization expression",
5252 Defining_Identifier (Decl));
5253 end if;
5255 Decl := Next (Decl);
5256 end loop;
5257 end Inspect_Deferred_Constant_Completion;
5259 -------------------
5260 -- Is_AAMP_Float --
5261 -------------------
5263 function Is_AAMP_Float (E : Entity_Id) return Boolean is
5264 pragma Assert (Is_Type (E));
5265 begin
5266 return AAMP_On_Target
5267 and then Is_Floating_Point_Type (E)
5268 and then E = Base_Type (E);
5269 end Is_AAMP_Float;
5271 -------------------------
5272 -- Is_Actual_Parameter --
5273 -------------------------
5275 function Is_Actual_Parameter (N : Node_Id) return Boolean is
5276 PK : constant Node_Kind := Nkind (Parent (N));
5278 begin
5279 case PK is
5280 when N_Parameter_Association =>
5281 return N = Explicit_Actual_Parameter (Parent (N));
5283 when N_Function_Call | N_Procedure_Call_Statement =>
5284 return Is_List_Member (N)
5285 and then
5286 List_Containing (N) = Parameter_Associations (Parent (N));
5288 when others =>
5289 return False;
5290 end case;
5291 end Is_Actual_Parameter;
5293 ---------------------
5294 -- Is_Aliased_View --
5295 ---------------------
5297 function Is_Aliased_View (Obj : Node_Id) return Boolean is
5298 E : Entity_Id;
5300 begin
5301 if Is_Entity_Name (Obj) then
5303 E := Entity (Obj);
5305 return
5306 (Is_Object (E)
5307 and then
5308 (Is_Aliased (E)
5309 or else (Present (Renamed_Object (E))
5310 and then Is_Aliased_View (Renamed_Object (E)))))
5312 or else ((Is_Formal (E)
5313 or else Ekind (E) = E_Generic_In_Out_Parameter
5314 or else Ekind (E) = E_Generic_In_Parameter)
5315 and then Is_Tagged_Type (Etype (E)))
5317 or else (Is_Concurrent_Type (E)
5318 and then In_Open_Scopes (E))
5320 -- Current instance of type, either directly or as rewritten
5321 -- reference to the current object.
5323 or else (Is_Entity_Name (Original_Node (Obj))
5324 and then Present (Entity (Original_Node (Obj)))
5325 and then Is_Type (Entity (Original_Node (Obj))))
5327 or else (Is_Type (E) and then E = Current_Scope)
5329 or else (Is_Incomplete_Or_Private_Type (E)
5330 and then Full_View (E) = Current_Scope);
5332 elsif Nkind (Obj) = N_Selected_Component then
5333 return Is_Aliased (Entity (Selector_Name (Obj)));
5335 elsif Nkind (Obj) = N_Indexed_Component then
5336 return Has_Aliased_Components (Etype (Prefix (Obj)))
5337 or else
5338 (Is_Access_Type (Etype (Prefix (Obj)))
5339 and then
5340 Has_Aliased_Components
5341 (Designated_Type (Etype (Prefix (Obj)))));
5343 elsif Nkind (Obj) = N_Unchecked_Type_Conversion
5344 or else Nkind (Obj) = N_Type_Conversion
5345 then
5346 return Is_Tagged_Type (Etype (Obj))
5347 and then Is_Aliased_View (Expression (Obj));
5349 elsif Nkind (Obj) = N_Explicit_Dereference then
5350 return Nkind (Original_Node (Obj)) /= N_Function_Call;
5352 else
5353 return False;
5354 end if;
5355 end Is_Aliased_View;
5357 -------------------------
5358 -- Is_Ancestor_Package --
5359 -------------------------
5361 function Is_Ancestor_Package
5362 (E1 : Entity_Id;
5363 E2 : Entity_Id) return Boolean
5365 Par : Entity_Id;
5367 begin
5368 Par := E2;
5369 while Present (Par)
5370 and then Par /= Standard_Standard
5371 loop
5372 if Par = E1 then
5373 return True;
5374 end if;
5376 Par := Scope (Par);
5377 end loop;
5379 return False;
5380 end Is_Ancestor_Package;
5382 ----------------------
5383 -- Is_Atomic_Object --
5384 ----------------------
5386 function Is_Atomic_Object (N : Node_Id) return Boolean is
5388 function Object_Has_Atomic_Components (N : Node_Id) return Boolean;
5389 -- Determines if given object has atomic components
5391 function Is_Atomic_Prefix (N : Node_Id) return Boolean;
5392 -- If prefix is an implicit dereference, examine designated type
5394 ----------------------
5395 -- Is_Atomic_Prefix --
5396 ----------------------
5398 function Is_Atomic_Prefix (N : Node_Id) return Boolean is
5399 begin
5400 if Is_Access_Type (Etype (N)) then
5401 return
5402 Has_Atomic_Components (Designated_Type (Etype (N)));
5403 else
5404 return Object_Has_Atomic_Components (N);
5405 end if;
5406 end Is_Atomic_Prefix;
5408 ----------------------------------
5409 -- Object_Has_Atomic_Components --
5410 ----------------------------------
5412 function Object_Has_Atomic_Components (N : Node_Id) return Boolean is
5413 begin
5414 if Has_Atomic_Components (Etype (N))
5415 or else Is_Atomic (Etype (N))
5416 then
5417 return True;
5419 elsif Is_Entity_Name (N)
5420 and then (Has_Atomic_Components (Entity (N))
5421 or else Is_Atomic (Entity (N)))
5422 then
5423 return True;
5425 elsif Nkind (N) = N_Indexed_Component
5426 or else Nkind (N) = N_Selected_Component
5427 then
5428 return Is_Atomic_Prefix (Prefix (N));
5430 else
5431 return False;
5432 end if;
5433 end Object_Has_Atomic_Components;
5435 -- Start of processing for Is_Atomic_Object
5437 begin
5438 if Is_Atomic (Etype (N))
5439 or else (Is_Entity_Name (N) and then Is_Atomic (Entity (N)))
5440 then
5441 return True;
5443 elsif Nkind (N) = N_Indexed_Component
5444 or else Nkind (N) = N_Selected_Component
5445 then
5446 return Is_Atomic_Prefix (Prefix (N));
5448 else
5449 return False;
5450 end if;
5451 end Is_Atomic_Object;
5453 -------------------------
5454 -- Is_Coextension_Root --
5455 -------------------------
5457 function Is_Coextension_Root (N : Node_Id) return Boolean is
5458 begin
5459 return
5460 Nkind (N) = N_Allocator
5461 and then Present (Coextensions (N))
5463 -- Anonymous access discriminants carry a list of all nested
5464 -- controlled coextensions.
5466 and then not Is_Dynamic_Coextension (N)
5467 and then not Is_Static_Coextension (N);
5468 end Is_Coextension_Root;
5470 -----------------------------
5471 -- Is_Concurrent_Interface --
5472 -----------------------------
5474 function Is_Concurrent_Interface (T : Entity_Id) return Boolean is
5475 begin
5476 return
5477 Is_Interface (T)
5478 and then
5479 (Is_Protected_Interface (T)
5480 or else Is_Synchronized_Interface (T)
5481 or else Is_Task_Interface (T));
5482 end Is_Concurrent_Interface;
5484 --------------------------------------
5485 -- Is_Controlling_Limited_Procedure --
5486 --------------------------------------
5488 function Is_Controlling_Limited_Procedure
5489 (Proc_Nam : Entity_Id) return Boolean
5491 Param_Typ : Entity_Id := Empty;
5493 begin
5494 if Ekind (Proc_Nam) = E_Procedure
5495 and then Present (Parameter_Specifications (Parent (Proc_Nam)))
5496 then
5497 Param_Typ := Etype (Parameter_Type (First (
5498 Parameter_Specifications (Parent (Proc_Nam)))));
5500 -- In this case where an Itype was created, the procedure call has been
5501 -- rewritten.
5503 elsif Present (Associated_Node_For_Itype (Proc_Nam))
5504 and then Present (Original_Node (Associated_Node_For_Itype (Proc_Nam)))
5505 and then
5506 Present (Parameter_Associations
5507 (Associated_Node_For_Itype (Proc_Nam)))
5508 then
5509 Param_Typ :=
5510 Etype (First (Parameter_Associations
5511 (Associated_Node_For_Itype (Proc_Nam))));
5512 end if;
5514 if Present (Param_Typ) then
5515 return
5516 Is_Interface (Param_Typ)
5517 and then Is_Limited_Record (Param_Typ);
5518 end if;
5520 return False;
5521 end Is_Controlling_Limited_Procedure;
5523 ----------------------------------------------
5524 -- Is_Dependent_Component_Of_Mutable_Object --
5525 ----------------------------------------------
5527 function Is_Dependent_Component_Of_Mutable_Object
5528 (Object : Node_Id) return Boolean
5530 P : Node_Id;
5531 Prefix_Type : Entity_Id;
5532 P_Aliased : Boolean := False;
5533 Comp : Entity_Id;
5535 function Is_Declared_Within_Variant (Comp : Entity_Id) return Boolean;
5536 -- Returns True if and only if Comp is declared within a variant part
5538 --------------------------------
5539 -- Is_Declared_Within_Variant --
5540 --------------------------------
5542 function Is_Declared_Within_Variant (Comp : Entity_Id) return Boolean is
5543 Comp_Decl : constant Node_Id := Parent (Comp);
5544 Comp_List : constant Node_Id := Parent (Comp_Decl);
5545 begin
5546 return Nkind (Parent (Comp_List)) = N_Variant;
5547 end Is_Declared_Within_Variant;
5549 -- Start of processing for Is_Dependent_Component_Of_Mutable_Object
5551 begin
5552 if Is_Variable (Object) then
5554 if Nkind (Object) = N_Selected_Component then
5555 P := Prefix (Object);
5556 Prefix_Type := Etype (P);
5558 if Is_Entity_Name (P) then
5560 if Ekind (Entity (P)) = E_Generic_In_Out_Parameter then
5561 Prefix_Type := Base_Type (Prefix_Type);
5562 end if;
5564 if Is_Aliased (Entity (P)) then
5565 P_Aliased := True;
5566 end if;
5568 -- A discriminant check on a selected component may be
5569 -- expanded into a dereference when removing side-effects.
5570 -- Recover the original node and its type, which may be
5571 -- unconstrained.
5573 elsif Nkind (P) = N_Explicit_Dereference
5574 and then not (Comes_From_Source (P))
5575 then
5576 P := Original_Node (P);
5577 Prefix_Type := Etype (P);
5579 else
5580 -- Check for prefix being an aliased component ???
5581 null;
5583 end if;
5585 -- A heap object is constrained by its initial value
5587 -- Ada 2005 (AI-363): Always assume the object could be mutable in
5588 -- the dereferenced case, since the access value might denote an
5589 -- unconstrained aliased object, whereas in Ada 95 the designated
5590 -- object is guaranteed to be constrained. A worst-case assumption
5591 -- has to apply in Ada 2005 because we can't tell at compile time
5592 -- whether the object is "constrained by its initial value"
5593 -- (despite the fact that 3.10.2(26/2) and 8.5.1(5/2) are
5594 -- semantic rules -- these rules are acknowledged to need fixing).
5596 if Ada_Version < Ada_05 then
5597 if Is_Access_Type (Prefix_Type)
5598 or else Nkind (P) = N_Explicit_Dereference
5599 then
5600 return False;
5601 end if;
5603 elsif Ada_Version >= Ada_05 then
5604 if Is_Access_Type (Prefix_Type) then
5606 -- If the access type is pool-specific, and there is no
5607 -- constrained partial view of the designated type, then the
5608 -- designated object is known to be constrained.
5610 if Ekind (Prefix_Type) = E_Access_Type
5611 and then not Has_Constrained_Partial_View
5612 (Designated_Type (Prefix_Type))
5613 then
5614 return False;
5616 -- Otherwise (general access type, or there is a constrained
5617 -- partial view of the designated type), we need to check
5618 -- based on the designated type.
5620 else
5621 Prefix_Type := Designated_Type (Prefix_Type);
5622 end if;
5623 end if;
5624 end if;
5626 Comp :=
5627 Original_Record_Component (Entity (Selector_Name (Object)));
5629 -- As per AI-0017, the renaming is illegal in a generic body,
5630 -- even if the subtype is indefinite.
5632 -- Ada 2005 (AI-363): In Ada 2005 an aliased object can be mutable
5634 if not Is_Constrained (Prefix_Type)
5635 and then (not Is_Indefinite_Subtype (Prefix_Type)
5636 or else
5637 (Is_Generic_Type (Prefix_Type)
5638 and then Ekind (Current_Scope) = E_Generic_Package
5639 and then In_Package_Body (Current_Scope)))
5641 and then (Is_Declared_Within_Variant (Comp)
5642 or else Has_Discriminant_Dependent_Constraint (Comp))
5643 and then (not P_Aliased or else Ada_Version >= Ada_05)
5644 then
5645 return True;
5647 else
5648 return
5649 Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
5651 end if;
5653 elsif Nkind (Object) = N_Indexed_Component
5654 or else Nkind (Object) = N_Slice
5655 then
5656 return Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
5658 -- A type conversion that Is_Variable is a view conversion:
5659 -- go back to the denoted object.
5661 elsif Nkind (Object) = N_Type_Conversion then
5662 return
5663 Is_Dependent_Component_Of_Mutable_Object (Expression (Object));
5664 end if;
5665 end if;
5667 return False;
5668 end Is_Dependent_Component_Of_Mutable_Object;
5670 ---------------------
5671 -- Is_Dereferenced --
5672 ---------------------
5674 function Is_Dereferenced (N : Node_Id) return Boolean is
5675 P : constant Node_Id := Parent (N);
5676 begin
5677 return
5678 (Nkind (P) = N_Selected_Component
5679 or else
5680 Nkind (P) = N_Explicit_Dereference
5681 or else
5682 Nkind (P) = N_Indexed_Component
5683 or else
5684 Nkind (P) = N_Slice)
5685 and then Prefix (P) = N;
5686 end Is_Dereferenced;
5688 ----------------------
5689 -- Is_Descendent_Of --
5690 ----------------------
5692 function Is_Descendent_Of (T1 : Entity_Id; T2 : Entity_Id) return Boolean is
5693 T : Entity_Id;
5694 Etyp : Entity_Id;
5696 begin
5697 pragma Assert (Nkind (T1) in N_Entity);
5698 pragma Assert (Nkind (T2) in N_Entity);
5700 T := Base_Type (T1);
5702 -- Immediate return if the types match
5704 if T = T2 then
5705 return True;
5707 -- Comment needed here ???
5709 elsif Ekind (T) = E_Class_Wide_Type then
5710 return Etype (T) = T2;
5712 -- All other cases
5714 else
5715 loop
5716 Etyp := Etype (T);
5718 -- Done if we found the type we are looking for
5720 if Etyp = T2 then
5721 return True;
5723 -- Done if no more derivations to check
5725 elsif T = T1
5726 or else T = Etyp
5727 then
5728 return False;
5730 -- Following test catches error cases resulting from prev errors
5732 elsif No (Etyp) then
5733 return False;
5735 elsif Is_Private_Type (T) and then Etyp = Full_View (T) then
5736 return False;
5738 elsif Is_Private_Type (Etyp) and then Full_View (Etyp) = T then
5739 return False;
5740 end if;
5742 T := Base_Type (Etyp);
5743 end loop;
5744 end if;
5745 end Is_Descendent_Of;
5747 --------------
5748 -- Is_False --
5749 --------------
5751 function Is_False (U : Uint) return Boolean is
5752 begin
5753 return (U = 0);
5754 end Is_False;
5756 ---------------------------
5757 -- Is_Fixed_Model_Number --
5758 ---------------------------
5760 function Is_Fixed_Model_Number (U : Ureal; T : Entity_Id) return Boolean is
5761 S : constant Ureal := Small_Value (T);
5762 M : Urealp.Save_Mark;
5763 R : Boolean;
5764 begin
5765 M := Urealp.Mark;
5766 R := (U = UR_Trunc (U / S) * S);
5767 Urealp.Release (M);
5768 return R;
5769 end Is_Fixed_Model_Number;
5771 -------------------------------
5772 -- Is_Fully_Initialized_Type --
5773 -------------------------------
5775 function Is_Fully_Initialized_Type (Typ : Entity_Id) return Boolean is
5776 begin
5777 if Is_Scalar_Type (Typ) then
5778 return False;
5780 elsif Is_Access_Type (Typ) then
5781 return True;
5783 elsif Is_Array_Type (Typ) then
5784 if Is_Fully_Initialized_Type (Component_Type (Typ)) then
5785 return True;
5786 end if;
5788 -- An interesting case, if we have a constrained type one of whose
5789 -- bounds is known to be null, then there are no elements to be
5790 -- initialized, so all the elements are initialized!
5792 if Is_Constrained (Typ) then
5793 declare
5794 Indx : Node_Id;
5795 Indx_Typ : Entity_Id;
5796 Lbd, Hbd : Node_Id;
5798 begin
5799 Indx := First_Index (Typ);
5800 while Present (Indx) loop
5801 if Etype (Indx) = Any_Type then
5802 return False;
5804 -- If index is a range, use directly
5806 elsif Nkind (Indx) = N_Range then
5807 Lbd := Low_Bound (Indx);
5808 Hbd := High_Bound (Indx);
5810 else
5811 Indx_Typ := Etype (Indx);
5813 if Is_Private_Type (Indx_Typ) then
5814 Indx_Typ := Full_View (Indx_Typ);
5815 end if;
5817 if No (Indx_Typ) or else Etype (Indx_Typ) = Any_Type then
5818 return False;
5819 else
5820 Lbd := Type_Low_Bound (Indx_Typ);
5821 Hbd := Type_High_Bound (Indx_Typ);
5822 end if;
5823 end if;
5825 if Compile_Time_Known_Value (Lbd)
5826 and then Compile_Time_Known_Value (Hbd)
5827 then
5828 if Expr_Value (Hbd) < Expr_Value (Lbd) then
5829 return True;
5830 end if;
5831 end if;
5833 Next_Index (Indx);
5834 end loop;
5835 end;
5836 end if;
5838 -- If no null indexes, then type is not fully initialized
5840 return False;
5842 -- Record types
5844 elsif Is_Record_Type (Typ) then
5845 if Has_Discriminants (Typ)
5846 and then
5847 Present (Discriminant_Default_Value (First_Discriminant (Typ)))
5848 and then Is_Fully_Initialized_Variant (Typ)
5849 then
5850 return True;
5851 end if;
5853 -- Controlled records are considered to be fully initialized if
5854 -- there is a user defined Initialize routine. This may not be
5855 -- entirely correct, but as the spec notes, we are guessing here
5856 -- what is best from the point of view of issuing warnings.
5858 if Is_Controlled (Typ) then
5859 declare
5860 Utyp : constant Entity_Id := Underlying_Type (Typ);
5862 begin
5863 if Present (Utyp) then
5864 declare
5865 Init : constant Entity_Id :=
5866 (Find_Prim_Op
5867 (Underlying_Type (Typ), Name_Initialize));
5869 begin
5870 if Present (Init)
5871 and then Comes_From_Source (Init)
5872 and then not
5873 Is_Predefined_File_Name
5874 (File_Name (Get_Source_File_Index (Sloc (Init))))
5875 then
5876 return True;
5878 elsif Has_Null_Extension (Typ)
5879 and then
5880 Is_Fully_Initialized_Type
5881 (Etype (Base_Type (Typ)))
5882 then
5883 return True;
5884 end if;
5885 end;
5886 end if;
5887 end;
5888 end if;
5890 -- Otherwise see if all record components are initialized
5892 declare
5893 Ent : Entity_Id;
5895 begin
5896 Ent := First_Entity (Typ);
5897 while Present (Ent) loop
5898 if Chars (Ent) = Name_uController then
5899 null;
5901 elsif Ekind (Ent) = E_Component
5902 and then (No (Parent (Ent))
5903 or else No (Expression (Parent (Ent))))
5904 and then not Is_Fully_Initialized_Type (Etype (Ent))
5906 -- Special VM case for tag components, which need to be
5907 -- defined in this case, but are never initialized as VMs
5908 -- are using other dispatching mechanisms. Ignore this
5909 -- uninitialized case. Note that this applies both to the
5910 -- uTag entry and the main vtable pointer (CPP_Class case).
5912 and then (VM_Target = No_VM or else not Is_Tag (Ent))
5913 then
5914 return False;
5915 end if;
5917 Next_Entity (Ent);
5918 end loop;
5919 end;
5921 -- No uninitialized components, so type is fully initialized.
5922 -- Note that this catches the case of no components as well.
5924 return True;
5926 elsif Is_Concurrent_Type (Typ) then
5927 return True;
5929 elsif Is_Private_Type (Typ) then
5930 declare
5931 U : constant Entity_Id := Underlying_Type (Typ);
5933 begin
5934 if No (U) then
5935 return False;
5936 else
5937 return Is_Fully_Initialized_Type (U);
5938 end if;
5939 end;
5941 else
5942 return False;
5943 end if;
5944 end Is_Fully_Initialized_Type;
5946 ----------------------------------
5947 -- Is_Fully_Initialized_Variant --
5948 ----------------------------------
5950 function Is_Fully_Initialized_Variant (Typ : Entity_Id) return Boolean is
5951 Loc : constant Source_Ptr := Sloc (Typ);
5952 Constraints : constant List_Id := New_List;
5953 Components : constant Elist_Id := New_Elmt_List;
5954 Comp_Elmt : Elmt_Id;
5955 Comp_Id : Node_Id;
5956 Comp_List : Node_Id;
5957 Discr : Entity_Id;
5958 Discr_Val : Node_Id;
5960 Report_Errors : Boolean;
5961 pragma Warnings (Off, Report_Errors);
5963 begin
5964 if Serious_Errors_Detected > 0 then
5965 return False;
5966 end if;
5968 if Is_Record_Type (Typ)
5969 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
5970 and then Nkind (Type_Definition (Parent (Typ))) = N_Record_Definition
5971 then
5972 Comp_List := Component_List (Type_Definition (Parent (Typ)));
5974 Discr := First_Discriminant (Typ);
5975 while Present (Discr) loop
5976 if Nkind (Parent (Discr)) = N_Discriminant_Specification then
5977 Discr_Val := Expression (Parent (Discr));
5979 if Present (Discr_Val)
5980 and then Is_OK_Static_Expression (Discr_Val)
5981 then
5982 Append_To (Constraints,
5983 Make_Component_Association (Loc,
5984 Choices => New_List (New_Occurrence_Of (Discr, Loc)),
5985 Expression => New_Copy (Discr_Val)));
5986 else
5987 return False;
5988 end if;
5989 else
5990 return False;
5991 end if;
5993 Next_Discriminant (Discr);
5994 end loop;
5996 Gather_Components
5997 (Typ => Typ,
5998 Comp_List => Comp_List,
5999 Governed_By => Constraints,
6000 Into => Components,
6001 Report_Errors => Report_Errors);
6003 -- Check that each component present is fully initialized
6005 Comp_Elmt := First_Elmt (Components);
6006 while Present (Comp_Elmt) loop
6007 Comp_Id := Node (Comp_Elmt);
6009 if Ekind (Comp_Id) = E_Component
6010 and then (No (Parent (Comp_Id))
6011 or else No (Expression (Parent (Comp_Id))))
6012 and then not Is_Fully_Initialized_Type (Etype (Comp_Id))
6013 then
6014 return False;
6015 end if;
6017 Next_Elmt (Comp_Elmt);
6018 end loop;
6020 return True;
6022 elsif Is_Private_Type (Typ) then
6023 declare
6024 U : constant Entity_Id := Underlying_Type (Typ);
6026 begin
6027 if No (U) then
6028 return False;
6029 else
6030 return Is_Fully_Initialized_Variant (U);
6031 end if;
6032 end;
6033 else
6034 return False;
6035 end if;
6036 end Is_Fully_Initialized_Variant;
6038 ----------------------------
6039 -- Is_Inherited_Operation --
6040 ----------------------------
6042 function Is_Inherited_Operation (E : Entity_Id) return Boolean is
6043 Kind : constant Node_Kind := Nkind (Parent (E));
6044 begin
6045 pragma Assert (Is_Overloadable (E));
6046 return Kind = N_Full_Type_Declaration
6047 or else Kind = N_Private_Extension_Declaration
6048 or else Kind = N_Subtype_Declaration
6049 or else (Ekind (E) = E_Enumeration_Literal
6050 and then Is_Derived_Type (Etype (E)));
6051 end Is_Inherited_Operation;
6053 -----------------------------
6054 -- Is_Library_Level_Entity --
6055 -----------------------------
6057 function Is_Library_Level_Entity (E : Entity_Id) return Boolean is
6058 begin
6059 -- The following is a small optimization, and it also properly handles
6060 -- discriminals, which in task bodies might appear in expressions before
6061 -- the corresponding procedure has been created, and which therefore do
6062 -- not have an assigned scope.
6064 if Ekind (E) in Formal_Kind then
6065 return False;
6066 end if;
6068 -- Normal test is simply that the enclosing dynamic scope is Standard
6070 return Enclosing_Dynamic_Scope (E) = Standard_Standard;
6071 end Is_Library_Level_Entity;
6073 ---------------------------------
6074 -- Is_Local_Variable_Reference --
6075 ---------------------------------
6077 function Is_Local_Variable_Reference (Expr : Node_Id) return Boolean is
6078 begin
6079 if not Is_Entity_Name (Expr) then
6080 return False;
6082 else
6083 declare
6084 Ent : constant Entity_Id := Entity (Expr);
6085 Sub : constant Entity_Id := Enclosing_Subprogram (Ent);
6086 begin
6087 if Ekind (Ent) /= E_Variable
6088 and then
6089 Ekind (Ent) /= E_In_Out_Parameter
6090 then
6091 return False;
6092 else
6093 return Present (Sub) and then Sub = Current_Subprogram;
6094 end if;
6095 end;
6096 end if;
6097 end Is_Local_Variable_Reference;
6099 -------------------------
6100 -- Is_Object_Reference --
6101 -------------------------
6103 function Is_Object_Reference (N : Node_Id) return Boolean is
6104 begin
6105 if Is_Entity_Name (N) then
6106 return Present (Entity (N)) and then Is_Object (Entity (N));
6108 else
6109 case Nkind (N) is
6110 when N_Indexed_Component | N_Slice =>
6111 return
6112 Is_Object_Reference (Prefix (N))
6113 or else Is_Access_Type (Etype (Prefix (N)));
6115 -- In Ada95, a function call is a constant object; a procedure
6116 -- call is not.
6118 when N_Function_Call =>
6119 return Etype (N) /= Standard_Void_Type;
6121 -- A reference to the stream attribute Input is a function call
6123 when N_Attribute_Reference =>
6124 return Attribute_Name (N) = Name_Input;
6126 when N_Selected_Component =>
6127 return
6128 Is_Object_Reference (Selector_Name (N))
6129 and then
6130 (Is_Object_Reference (Prefix (N))
6131 or else Is_Access_Type (Etype (Prefix (N))));
6133 when N_Explicit_Dereference =>
6134 return True;
6136 -- A view conversion of a tagged object is an object reference
6138 when N_Type_Conversion =>
6139 return Is_Tagged_Type (Etype (Subtype_Mark (N)))
6140 and then Is_Tagged_Type (Etype (Expression (N)))
6141 and then Is_Object_Reference (Expression (N));
6143 -- An unchecked type conversion is considered to be an object if
6144 -- the operand is an object (this construction arises only as a
6145 -- result of expansion activities).
6147 when N_Unchecked_Type_Conversion =>
6148 return True;
6150 when others =>
6151 return False;
6152 end case;
6153 end if;
6154 end Is_Object_Reference;
6156 -----------------------------------
6157 -- Is_OK_Variable_For_Out_Formal --
6158 -----------------------------------
6160 function Is_OK_Variable_For_Out_Formal (AV : Node_Id) return Boolean is
6161 begin
6162 Note_Possible_Modification (AV, Sure => True);
6164 -- We must reject parenthesized variable names. The check for
6165 -- Comes_From_Source is present because there are currently
6166 -- cases where the compiler violates this rule (e.g. passing
6167 -- a task object to its controlled Initialize routine).
6169 if Paren_Count (AV) > 0 and then Comes_From_Source (AV) then
6170 return False;
6172 -- A variable is always allowed
6174 elsif Is_Variable (AV) then
6175 return True;
6177 -- Unchecked conversions are allowed only if they come from the
6178 -- generated code, which sometimes uses unchecked conversions for out
6179 -- parameters in cases where code generation is unaffected. We tell
6180 -- source unchecked conversions by seeing if they are rewrites of an
6181 -- original Unchecked_Conversion function call, or of an explicit
6182 -- conversion of a function call.
6184 elsif Nkind (AV) = N_Unchecked_Type_Conversion then
6185 if Nkind (Original_Node (AV)) = N_Function_Call then
6186 return False;
6188 elsif Comes_From_Source (AV)
6189 and then Nkind (Original_Node (Expression (AV))) = N_Function_Call
6190 then
6191 return False;
6193 elsif Nkind (Original_Node (AV)) = N_Type_Conversion then
6194 return Is_OK_Variable_For_Out_Formal (Expression (AV));
6196 else
6197 return True;
6198 end if;
6200 -- Normal type conversions are allowed if argument is a variable
6202 elsif Nkind (AV) = N_Type_Conversion then
6203 if Is_Variable (Expression (AV))
6204 and then Paren_Count (Expression (AV)) = 0
6205 then
6206 Note_Possible_Modification (Expression (AV), Sure => True);
6207 return True;
6209 -- We also allow a non-parenthesized expression that raises
6210 -- constraint error if it rewrites what used to be a variable
6212 elsif Raises_Constraint_Error (Expression (AV))
6213 and then Paren_Count (Expression (AV)) = 0
6214 and then Is_Variable (Original_Node (Expression (AV)))
6215 then
6216 return True;
6218 -- Type conversion of something other than a variable
6220 else
6221 return False;
6222 end if;
6224 -- If this node is rewritten, then test the original form, if that is
6225 -- OK, then we consider the rewritten node OK (for example, if the
6226 -- original node is a conversion, then Is_Variable will not be true
6227 -- but we still want to allow the conversion if it converts a variable).
6229 elsif Original_Node (AV) /= AV then
6230 return Is_OK_Variable_For_Out_Formal (Original_Node (AV));
6232 -- All other non-variables are rejected
6234 else
6235 return False;
6236 end if;
6237 end Is_OK_Variable_For_Out_Formal;
6239 -----------------------------------
6240 -- Is_Partially_Initialized_Type --
6241 -----------------------------------
6243 function Is_Partially_Initialized_Type (Typ : Entity_Id) return Boolean is
6244 begin
6245 if Is_Scalar_Type (Typ) then
6246 return False;
6248 elsif Is_Access_Type (Typ) then
6249 return True;
6251 elsif Is_Array_Type (Typ) then
6253 -- If component type is partially initialized, so is array type
6255 if Is_Partially_Initialized_Type (Component_Type (Typ)) then
6256 return True;
6258 -- Otherwise we are only partially initialized if we are fully
6259 -- initialized (this is the empty array case, no point in us
6260 -- duplicating that code here).
6262 else
6263 return Is_Fully_Initialized_Type (Typ);
6264 end if;
6266 elsif Is_Record_Type (Typ) then
6268 -- A discriminated type is always partially initialized
6270 if Has_Discriminants (Typ) then
6271 return True;
6273 -- A tagged type is always partially initialized
6275 elsif Is_Tagged_Type (Typ) then
6276 return True;
6278 -- Case of non-discriminated record
6280 else
6281 declare
6282 Ent : Entity_Id;
6284 Component_Present : Boolean := False;
6285 -- Set True if at least one component is present. If no
6286 -- components are present, then record type is fully
6287 -- initialized (another odd case, like the null array).
6289 begin
6290 -- Loop through components
6292 Ent := First_Entity (Typ);
6293 while Present (Ent) loop
6294 if Ekind (Ent) = E_Component then
6295 Component_Present := True;
6297 -- If a component has an initialization expression then
6298 -- the enclosing record type is partially initialized
6300 if Present (Parent (Ent))
6301 and then Present (Expression (Parent (Ent)))
6302 then
6303 return True;
6305 -- If a component is of a type which is itself partially
6306 -- initialized, then the enclosing record type is also.
6308 elsif Is_Partially_Initialized_Type (Etype (Ent)) then
6309 return True;
6310 end if;
6311 end if;
6313 Next_Entity (Ent);
6314 end loop;
6316 -- No initialized components found. If we found any components
6317 -- they were all uninitialized so the result is false.
6319 if Component_Present then
6320 return False;
6322 -- But if we found no components, then all the components are
6323 -- initialized so we consider the type to be initialized.
6325 else
6326 return True;
6327 end if;
6328 end;
6329 end if;
6331 -- Concurrent types are always fully initialized
6333 elsif Is_Concurrent_Type (Typ) then
6334 return True;
6336 -- For a private type, go to underlying type. If there is no underlying
6337 -- type then just assume this partially initialized. Not clear if this
6338 -- can happen in a non-error case, but no harm in testing for this.
6340 elsif Is_Private_Type (Typ) then
6341 declare
6342 U : constant Entity_Id := Underlying_Type (Typ);
6343 begin
6344 if No (U) then
6345 return True;
6346 else
6347 return Is_Partially_Initialized_Type (U);
6348 end if;
6349 end;
6351 -- For any other type (are there any?) assume partially initialized
6353 else
6354 return True;
6355 end if;
6356 end Is_Partially_Initialized_Type;
6358 ------------------------------------
6359 -- Is_Potentially_Persistent_Type --
6360 ------------------------------------
6362 function Is_Potentially_Persistent_Type (T : Entity_Id) return Boolean is
6363 Comp : Entity_Id;
6364 Indx : Node_Id;
6366 begin
6367 -- For private type, test corresponding full type
6369 if Is_Private_Type (T) then
6370 return Is_Potentially_Persistent_Type (Full_View (T));
6372 -- Scalar types are potentially persistent
6374 elsif Is_Scalar_Type (T) then
6375 return True;
6377 -- Record type is potentially persistent if not tagged and the types of
6378 -- all it components are potentially persistent, and no component has
6379 -- an initialization expression.
6381 elsif Is_Record_Type (T)
6382 and then not Is_Tagged_Type (T)
6383 and then not Is_Partially_Initialized_Type (T)
6384 then
6385 Comp := First_Component (T);
6386 while Present (Comp) loop
6387 if not Is_Potentially_Persistent_Type (Etype (Comp)) then
6388 return False;
6389 else
6390 Next_Entity (Comp);
6391 end if;
6392 end loop;
6394 return True;
6396 -- Array type is potentially persistent if its component type is
6397 -- potentially persistent and if all its constraints are static.
6399 elsif Is_Array_Type (T) then
6400 if not Is_Potentially_Persistent_Type (Component_Type (T)) then
6401 return False;
6402 end if;
6404 Indx := First_Index (T);
6405 while Present (Indx) loop
6406 if not Is_OK_Static_Subtype (Etype (Indx)) then
6407 return False;
6408 else
6409 Next_Index (Indx);
6410 end if;
6411 end loop;
6413 return True;
6415 -- All other types are not potentially persistent
6417 else
6418 return False;
6419 end if;
6420 end Is_Potentially_Persistent_Type;
6422 ---------------------------------
6423 -- Is_Protected_Self_Reference --
6424 ---------------------------------
6426 function Is_Protected_Self_Reference (N : Node_Id) return Boolean is
6428 function In_Access_Definition (N : Node_Id) return Boolean;
6429 -- Returns true if N belongs to an access definition
6431 --------------------------
6432 -- In_Access_Definition --
6433 --------------------------
6435 function In_Access_Definition (N : Node_Id) return Boolean is
6436 P : Node_Id;
6438 begin
6439 P := Parent (N);
6440 while Present (P) loop
6441 if Nkind (P) = N_Access_Definition then
6442 return True;
6443 end if;
6445 P := Parent (P);
6446 end loop;
6448 return False;
6449 end In_Access_Definition;
6451 -- Start of processing for Is_Protected_Self_Reference
6453 begin
6454 -- Verify that prefix is analyzed and has the proper form. Note that
6455 -- the attributes Elab_Spec, Elab_Body, and UET_Address, which also
6456 -- produce the address of an entity, do not analyze their prefix
6457 -- because they denote entities that are not necessarily visible.
6458 -- Neither of them can apply to a protected type.
6460 return Ada_Version >= Ada_05
6461 and then Is_Entity_Name (N)
6462 and then Present (Entity (N))
6463 and then Is_Protected_Type (Entity (N))
6464 and then In_Open_Scopes (Entity (N))
6465 and then not In_Access_Definition (N);
6466 end Is_Protected_Self_Reference;
6468 -----------------------------
6469 -- Is_RCI_Pkg_Spec_Or_Body --
6470 -----------------------------
6472 function Is_RCI_Pkg_Spec_Or_Body (Cunit : Node_Id) return Boolean is
6474 function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean;
6475 -- Return True if the unit of Cunit is an RCI package declaration
6477 ---------------------------
6478 -- Is_RCI_Pkg_Decl_Cunit --
6479 ---------------------------
6481 function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean is
6482 The_Unit : constant Node_Id := Unit (Cunit);
6484 begin
6485 if Nkind (The_Unit) /= N_Package_Declaration then
6486 return False;
6487 end if;
6489 return Is_Remote_Call_Interface (Defining_Entity (The_Unit));
6490 end Is_RCI_Pkg_Decl_Cunit;
6492 -- Start of processing for Is_RCI_Pkg_Spec_Or_Body
6494 begin
6495 return Is_RCI_Pkg_Decl_Cunit (Cunit)
6496 or else
6497 (Nkind (Unit (Cunit)) = N_Package_Body
6498 and then Is_RCI_Pkg_Decl_Cunit (Library_Unit (Cunit)));
6499 end Is_RCI_Pkg_Spec_Or_Body;
6501 -----------------------------------------
6502 -- Is_Remote_Access_To_Class_Wide_Type --
6503 -----------------------------------------
6505 function Is_Remote_Access_To_Class_Wide_Type
6506 (E : Entity_Id) return Boolean
6508 begin
6509 -- A remote access to class-wide type is a general access to object type
6510 -- declared in the visible part of a Remote_Types or Remote_Call_
6511 -- Interface unit.
6513 return Ekind (E) = E_General_Access_Type
6514 and then (Is_Remote_Call_Interface (E) or else Is_Remote_Types (E));
6515 end Is_Remote_Access_To_Class_Wide_Type;
6517 -----------------------------------------
6518 -- Is_Remote_Access_To_Subprogram_Type --
6519 -----------------------------------------
6521 function Is_Remote_Access_To_Subprogram_Type
6522 (E : Entity_Id) return Boolean
6524 begin
6525 return (Ekind (E) = E_Access_Subprogram_Type
6526 or else (Ekind (E) = E_Record_Type
6527 and then Present (Corresponding_Remote_Type (E))))
6528 and then (Is_Remote_Call_Interface (E) or else Is_Remote_Types (E));
6529 end Is_Remote_Access_To_Subprogram_Type;
6531 --------------------
6532 -- Is_Remote_Call --
6533 --------------------
6535 function Is_Remote_Call (N : Node_Id) return Boolean is
6536 begin
6537 if Nkind (N) /= N_Procedure_Call_Statement
6538 and then Nkind (N) /= N_Function_Call
6539 then
6540 -- An entry call cannot be remote
6542 return False;
6544 elsif Nkind (Name (N)) in N_Has_Entity
6545 and then Is_Remote_Call_Interface (Entity (Name (N)))
6546 then
6547 -- A subprogram declared in the spec of a RCI package is remote
6549 return True;
6551 elsif Nkind (Name (N)) = N_Explicit_Dereference
6552 and then Is_Remote_Access_To_Subprogram_Type
6553 (Etype (Prefix (Name (N))))
6554 then
6555 -- The dereference of a RAS is a remote call
6557 return True;
6559 elsif Present (Controlling_Argument (N))
6560 and then Is_Remote_Access_To_Class_Wide_Type
6561 (Etype (Controlling_Argument (N)))
6562 then
6563 -- Any primitive operation call with a controlling argument of
6564 -- a RACW type is a remote call.
6566 return True;
6567 end if;
6569 -- All other calls are local calls
6571 return False;
6572 end Is_Remote_Call;
6574 ----------------------
6575 -- Is_Renamed_Entry --
6576 ----------------------
6578 function Is_Renamed_Entry (Proc_Nam : Entity_Id) return Boolean is
6579 Orig_Node : Node_Id := Empty;
6580 Subp_Decl : Node_Id := Parent (Parent (Proc_Nam));
6582 function Is_Entry (Nam : Node_Id) return Boolean;
6583 -- Determine whether Nam is an entry. Traverse selectors if there are
6584 -- nested selected components.
6586 --------------
6587 -- Is_Entry --
6588 --------------
6590 function Is_Entry (Nam : Node_Id) return Boolean is
6591 begin
6592 if Nkind (Nam) = N_Selected_Component then
6593 return Is_Entry (Selector_Name (Nam));
6594 end if;
6596 return Ekind (Entity (Nam)) = E_Entry;
6597 end Is_Entry;
6599 -- Start of processing for Is_Renamed_Entry
6601 begin
6602 if Present (Alias (Proc_Nam)) then
6603 Subp_Decl := Parent (Parent (Alias (Proc_Nam)));
6604 end if;
6606 -- Look for a rewritten subprogram renaming declaration
6608 if Nkind (Subp_Decl) = N_Subprogram_Declaration
6609 and then Present (Original_Node (Subp_Decl))
6610 then
6611 Orig_Node := Original_Node (Subp_Decl);
6612 end if;
6614 -- The rewritten subprogram is actually an entry
6616 if Present (Orig_Node)
6617 and then Nkind (Orig_Node) = N_Subprogram_Renaming_Declaration
6618 and then Is_Entry (Name (Orig_Node))
6619 then
6620 return True;
6621 end if;
6623 return False;
6624 end Is_Renamed_Entry;
6626 ----------------------
6627 -- Is_Selector_Name --
6628 ----------------------
6630 function Is_Selector_Name (N : Node_Id) return Boolean is
6631 begin
6632 if not Is_List_Member (N) then
6633 declare
6634 P : constant Node_Id := Parent (N);
6635 K : constant Node_Kind := Nkind (P);
6636 begin
6637 return
6638 (K = N_Expanded_Name or else
6639 K = N_Generic_Association or else
6640 K = N_Parameter_Association or else
6641 K = N_Selected_Component)
6642 and then Selector_Name (P) = N;
6643 end;
6645 else
6646 declare
6647 L : constant List_Id := List_Containing (N);
6648 P : constant Node_Id := Parent (L);
6649 begin
6650 return (Nkind (P) = N_Discriminant_Association
6651 and then Selector_Names (P) = L)
6652 or else
6653 (Nkind (P) = N_Component_Association
6654 and then Choices (P) = L);
6655 end;
6656 end if;
6657 end Is_Selector_Name;
6659 ------------------
6660 -- Is_Statement --
6661 ------------------
6663 function Is_Statement (N : Node_Id) return Boolean is
6664 begin
6665 return
6666 Nkind (N) in N_Statement_Other_Than_Procedure_Call
6667 or else Nkind (N) = N_Procedure_Call_Statement;
6668 end Is_Statement;
6670 ---------------------------------
6671 -- Is_Synchronized_Tagged_Type --
6672 ---------------------------------
6674 function Is_Synchronized_Tagged_Type (E : Entity_Id) return Boolean is
6675 Kind : constant Entity_Kind := Ekind (Base_Type (E));
6677 begin
6678 -- A task or protected type derived from an interface is a tagged type.
6679 -- Such a tagged type is called a synchronized tagged type, as are
6680 -- synchronized interfaces and private extensions whose declaration
6681 -- includes the reserved word synchronized.
6683 return (Is_Tagged_Type (E)
6684 and then (Kind = E_Task_Type
6685 or else Kind = E_Protected_Type))
6686 or else
6687 (Is_Interface (E)
6688 and then Is_Synchronized_Interface (E))
6689 or else
6690 (Ekind (E) = E_Record_Type_With_Private
6691 and then (Synchronized_Present (Parent (E))
6692 or else Is_Synchronized_Interface (Etype (E))));
6693 end Is_Synchronized_Tagged_Type;
6695 -----------------
6696 -- Is_Transfer --
6697 -----------------
6699 function Is_Transfer (N : Node_Id) return Boolean is
6700 Kind : constant Node_Kind := Nkind (N);
6702 begin
6703 if Kind = N_Simple_Return_Statement
6704 or else
6705 Kind = N_Extended_Return_Statement
6706 or else
6707 Kind = N_Goto_Statement
6708 or else
6709 Kind = N_Raise_Statement
6710 or else
6711 Kind = N_Requeue_Statement
6712 then
6713 return True;
6715 elsif (Kind = N_Exit_Statement or else Kind in N_Raise_xxx_Error)
6716 and then No (Condition (N))
6717 then
6718 return True;
6720 elsif Kind = N_Procedure_Call_Statement
6721 and then Is_Entity_Name (Name (N))
6722 and then Present (Entity (Name (N)))
6723 and then No_Return (Entity (Name (N)))
6724 then
6725 return True;
6727 elsif Nkind (Original_Node (N)) = N_Raise_Statement then
6728 return True;
6730 else
6731 return False;
6732 end if;
6733 end Is_Transfer;
6735 -------------
6736 -- Is_True --
6737 -------------
6739 function Is_True (U : Uint) return Boolean is
6740 begin
6741 return (U /= 0);
6742 end Is_True;
6744 -------------------
6745 -- Is_Value_Type --
6746 -------------------
6748 function Is_Value_Type (T : Entity_Id) return Boolean is
6749 begin
6750 return VM_Target = CLI_Target
6751 and then Chars (T) /= No_Name
6752 and then Get_Name_String (Chars (T)) = "valuetype";
6753 end Is_Value_Type;
6755 -----------------
6756 -- Is_Variable --
6757 -----------------
6759 function Is_Variable (N : Node_Id) return Boolean is
6761 Orig_Node : constant Node_Id := Original_Node (N);
6762 -- We do the test on the original node, since this is basically a
6763 -- test of syntactic categories, so it must not be disturbed by
6764 -- whatever rewriting might have occurred. For example, an aggregate,
6765 -- which is certainly NOT a variable, could be turned into a variable
6766 -- by expansion.
6768 function In_Protected_Function (E : Entity_Id) return Boolean;
6769 -- Within a protected function, the private components of the
6770 -- enclosing protected type are constants. A function nested within
6771 -- a (protected) procedure is not itself protected.
6773 function Is_Variable_Prefix (P : Node_Id) return Boolean;
6774 -- Prefixes can involve implicit dereferences, in which case we
6775 -- must test for the case of a reference of a constant access
6776 -- type, which can never be a variable.
6778 ---------------------------
6779 -- In_Protected_Function --
6780 ---------------------------
6782 function In_Protected_Function (E : Entity_Id) return Boolean is
6783 Prot : constant Entity_Id := Scope (E);
6784 S : Entity_Id;
6786 begin
6787 if not Is_Protected_Type (Prot) then
6788 return False;
6789 else
6790 S := Current_Scope;
6791 while Present (S) and then S /= Prot loop
6792 if Ekind (S) = E_Function
6793 and then Scope (S) = Prot
6794 then
6795 return True;
6796 end if;
6798 S := Scope (S);
6799 end loop;
6801 return False;
6802 end if;
6803 end In_Protected_Function;
6805 ------------------------
6806 -- Is_Variable_Prefix --
6807 ------------------------
6809 function Is_Variable_Prefix (P : Node_Id) return Boolean is
6810 begin
6811 if Is_Access_Type (Etype (P)) then
6812 return not Is_Access_Constant (Root_Type (Etype (P)));
6814 -- For the case of an indexed component whose prefix has a packed
6815 -- array type, the prefix has been rewritten into a type conversion.
6816 -- Determine variable-ness from the converted expression.
6818 elsif Nkind (P) = N_Type_Conversion
6819 and then not Comes_From_Source (P)
6820 and then Is_Array_Type (Etype (P))
6821 and then Is_Packed (Etype (P))
6822 then
6823 return Is_Variable (Expression (P));
6825 else
6826 return Is_Variable (P);
6827 end if;
6828 end Is_Variable_Prefix;
6830 -- Start of processing for Is_Variable
6832 begin
6833 -- Definitely OK if Assignment_OK is set. Since this is something that
6834 -- only gets set for expanded nodes, the test is on N, not Orig_Node.
6836 if Nkind (N) in N_Subexpr and then Assignment_OK (N) then
6837 return True;
6839 -- Normally we go to the original node, but there is one exception
6840 -- where we use the rewritten node, namely when it is an explicit
6841 -- dereference. The generated code may rewrite a prefix which is an
6842 -- access type with an explicit dereference. The dereference is a
6843 -- variable, even though the original node may not be (since it could
6844 -- be a constant of the access type).
6846 -- In Ada 2005 we have a further case to consider: the prefix may be
6847 -- a function call given in prefix notation. The original node appears
6848 -- to be a selected component, but we need to examine the call.
6850 elsif Nkind (N) = N_Explicit_Dereference
6851 and then Nkind (Orig_Node) /= N_Explicit_Dereference
6852 and then Present (Etype (Orig_Node))
6853 and then Is_Access_Type (Etype (Orig_Node))
6854 then
6855 -- Note that if the prefix is an explicit dereference that does not
6856 -- come from source, we must check for a rewritten function call in
6857 -- prefixed notation before other forms of rewriting, to prevent a
6858 -- compiler crash.
6860 return
6861 (Nkind (Orig_Node) = N_Function_Call
6862 and then not Is_Access_Constant (Etype (Prefix (N))))
6863 or else
6864 Is_Variable_Prefix (Original_Node (Prefix (N)));
6866 -- A function call is never a variable
6868 elsif Nkind (N) = N_Function_Call then
6869 return False;
6871 -- All remaining checks use the original node
6873 elsif Is_Entity_Name (Orig_Node)
6874 and then Present (Entity (Orig_Node))
6875 then
6876 declare
6877 E : constant Entity_Id := Entity (Orig_Node);
6878 K : constant Entity_Kind := Ekind (E);
6880 begin
6881 return (K = E_Variable
6882 and then Nkind (Parent (E)) /= N_Exception_Handler)
6883 or else (K = E_Component
6884 and then not In_Protected_Function (E))
6885 or else K = E_Out_Parameter
6886 or else K = E_In_Out_Parameter
6887 or else K = E_Generic_In_Out_Parameter
6889 -- Current instance of type:
6891 or else (Is_Type (E) and then In_Open_Scopes (E))
6892 or else (Is_Incomplete_Or_Private_Type (E)
6893 and then In_Open_Scopes (Full_View (E)));
6894 end;
6896 else
6897 case Nkind (Orig_Node) is
6898 when N_Indexed_Component | N_Slice =>
6899 return Is_Variable_Prefix (Prefix (Orig_Node));
6901 when N_Selected_Component =>
6902 return Is_Variable_Prefix (Prefix (Orig_Node))
6903 and then Is_Variable (Selector_Name (Orig_Node));
6905 -- For an explicit dereference, the type of the prefix cannot
6906 -- be an access to constant or an access to subprogram.
6908 when N_Explicit_Dereference =>
6909 declare
6910 Typ : constant Entity_Id := Etype (Prefix (Orig_Node));
6911 begin
6912 return Is_Access_Type (Typ)
6913 and then not Is_Access_Constant (Root_Type (Typ))
6914 and then Ekind (Typ) /= E_Access_Subprogram_Type;
6915 end;
6917 -- The type conversion is the case where we do not deal with the
6918 -- context dependent special case of an actual parameter. Thus
6919 -- the type conversion is only considered a variable for the
6920 -- purposes of this routine if the target type is tagged. However,
6921 -- a type conversion is considered to be a variable if it does not
6922 -- come from source (this deals for example with the conversions
6923 -- of expressions to their actual subtypes).
6925 when N_Type_Conversion =>
6926 return Is_Variable (Expression (Orig_Node))
6927 and then
6928 (not Comes_From_Source (Orig_Node)
6929 or else
6930 (Is_Tagged_Type (Etype (Subtype_Mark (Orig_Node)))
6931 and then
6932 Is_Tagged_Type (Etype (Expression (Orig_Node)))));
6934 -- GNAT allows an unchecked type conversion as a variable. This
6935 -- only affects the generation of internal expanded code, since
6936 -- calls to instantiations of Unchecked_Conversion are never
6937 -- considered variables (since they are function calls).
6938 -- This is also true for expression actions.
6940 when N_Unchecked_Type_Conversion =>
6941 return Is_Variable (Expression (Orig_Node));
6943 when others =>
6944 return False;
6945 end case;
6946 end if;
6947 end Is_Variable;
6949 ------------------------
6950 -- Is_Volatile_Object --
6951 ------------------------
6953 function Is_Volatile_Object (N : Node_Id) return Boolean is
6955 function Object_Has_Volatile_Components (N : Node_Id) return Boolean;
6956 -- Determines if given object has volatile components
6958 function Is_Volatile_Prefix (N : Node_Id) return Boolean;
6959 -- If prefix is an implicit dereference, examine designated type
6961 ------------------------
6962 -- Is_Volatile_Prefix --
6963 ------------------------
6965 function Is_Volatile_Prefix (N : Node_Id) return Boolean is
6966 Typ : constant Entity_Id := Etype (N);
6968 begin
6969 if Is_Access_Type (Typ) then
6970 declare
6971 Dtyp : constant Entity_Id := Designated_Type (Typ);
6973 begin
6974 return Is_Volatile (Dtyp)
6975 or else Has_Volatile_Components (Dtyp);
6976 end;
6978 else
6979 return Object_Has_Volatile_Components (N);
6980 end if;
6981 end Is_Volatile_Prefix;
6983 ------------------------------------
6984 -- Object_Has_Volatile_Components --
6985 ------------------------------------
6987 function Object_Has_Volatile_Components (N : Node_Id) return Boolean is
6988 Typ : constant Entity_Id := Etype (N);
6990 begin
6991 if Is_Volatile (Typ)
6992 or else Has_Volatile_Components (Typ)
6993 then
6994 return True;
6996 elsif Is_Entity_Name (N)
6997 and then (Has_Volatile_Components (Entity (N))
6998 or else Is_Volatile (Entity (N)))
6999 then
7000 return True;
7002 elsif Nkind (N) = N_Indexed_Component
7003 or else Nkind (N) = N_Selected_Component
7004 then
7005 return Is_Volatile_Prefix (Prefix (N));
7007 else
7008 return False;
7009 end if;
7010 end Object_Has_Volatile_Components;
7012 -- Start of processing for Is_Volatile_Object
7014 begin
7015 if Is_Volatile (Etype (N))
7016 or else (Is_Entity_Name (N) and then Is_Volatile (Entity (N)))
7017 then
7018 return True;
7020 elsif Nkind (N) = N_Indexed_Component
7021 or else Nkind (N) = N_Selected_Component
7022 then
7023 return Is_Volatile_Prefix (Prefix (N));
7025 else
7026 return False;
7027 end if;
7028 end Is_Volatile_Object;
7030 -------------------------
7031 -- Kill_Current_Values --
7032 -------------------------
7034 procedure Kill_Current_Values
7035 (Ent : Entity_Id;
7036 Last_Assignment_Only : Boolean := False)
7038 begin
7039 if Is_Assignable (Ent) then
7040 Set_Last_Assignment (Ent, Empty);
7041 end if;
7043 if not Last_Assignment_Only and then Is_Object (Ent) then
7044 Kill_Checks (Ent);
7045 Set_Current_Value (Ent, Empty);
7047 if not Can_Never_Be_Null (Ent) then
7048 Set_Is_Known_Non_Null (Ent, False);
7049 end if;
7051 Set_Is_Known_Null (Ent, False);
7052 end if;
7053 end Kill_Current_Values;
7055 procedure Kill_Current_Values (Last_Assignment_Only : Boolean := False) is
7056 S : Entity_Id;
7058 procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id);
7059 -- Clear current value for entity E and all entities chained to E
7061 ------------------------------------------
7062 -- Kill_Current_Values_For_Entity_Chain --
7063 ------------------------------------------
7065 procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id) is
7066 Ent : Entity_Id;
7067 begin
7068 Ent := E;
7069 while Present (Ent) loop
7070 Kill_Current_Values (Ent, Last_Assignment_Only);
7071 Next_Entity (Ent);
7072 end loop;
7073 end Kill_Current_Values_For_Entity_Chain;
7075 -- Start of processing for Kill_Current_Values
7077 begin
7078 -- Kill all saved checks, a special case of killing saved values
7080 if not Last_Assignment_Only then
7081 Kill_All_Checks;
7082 end if;
7084 -- Loop through relevant scopes, which includes the current scope and
7085 -- any parent scopes if the current scope is a block or a package.
7087 S := Current_Scope;
7088 Scope_Loop : loop
7090 -- Clear current values of all entities in current scope
7092 Kill_Current_Values_For_Entity_Chain (First_Entity (S));
7094 -- If scope is a package, also clear current values of all
7095 -- private entities in the scope.
7097 if Is_Package_Or_Generic_Package (S)
7098 or else Is_Concurrent_Type (S)
7099 then
7100 Kill_Current_Values_For_Entity_Chain (First_Private_Entity (S));
7101 end if;
7103 -- If this is a not a subprogram, deal with parents
7105 if not Is_Subprogram (S) then
7106 S := Scope (S);
7107 exit Scope_Loop when S = Standard_Standard;
7108 else
7109 exit Scope_Loop;
7110 end if;
7111 end loop Scope_Loop;
7112 end Kill_Current_Values;
7114 --------------------------
7115 -- Kill_Size_Check_Code --
7116 --------------------------
7118 procedure Kill_Size_Check_Code (E : Entity_Id) is
7119 begin
7120 if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
7121 and then Present (Size_Check_Code (E))
7122 then
7123 Remove (Size_Check_Code (E));
7124 Set_Size_Check_Code (E, Empty);
7125 end if;
7126 end Kill_Size_Check_Code;
7128 --------------------------
7129 -- Known_To_Be_Assigned --
7130 --------------------------
7132 function Known_To_Be_Assigned (N : Node_Id) return Boolean is
7133 P : constant Node_Id := Parent (N);
7135 begin
7136 case Nkind (P) is
7138 -- Test left side of assignment
7140 when N_Assignment_Statement =>
7141 return N = Name (P);
7143 -- Function call arguments are never lvalues
7145 when N_Function_Call =>
7146 return False;
7148 -- Positional parameter for procedure or accept call
7150 when N_Procedure_Call_Statement |
7151 N_Accept_Statement
7153 declare
7154 Proc : Entity_Id;
7155 Form : Entity_Id;
7156 Act : Node_Id;
7158 begin
7159 Proc := Get_Subprogram_Entity (P);
7161 if No (Proc) then
7162 return False;
7163 end if;
7165 -- If we are not a list member, something is strange, so
7166 -- be conservative and return False.
7168 if not Is_List_Member (N) then
7169 return False;
7170 end if;
7172 -- We are going to find the right formal by stepping forward
7173 -- through the formals, as we step backwards in the actuals.
7175 Form := First_Formal (Proc);
7176 Act := N;
7177 loop
7178 -- If no formal, something is weird, so be conservative
7179 -- and return False.
7181 if No (Form) then
7182 return False;
7183 end if;
7185 Prev (Act);
7186 exit when No (Act);
7187 Next_Formal (Form);
7188 end loop;
7190 return Ekind (Form) /= E_In_Parameter;
7191 end;
7193 -- Named parameter for procedure or accept call
7195 when N_Parameter_Association =>
7196 declare
7197 Proc : Entity_Id;
7198 Form : Entity_Id;
7200 begin
7201 Proc := Get_Subprogram_Entity (Parent (P));
7203 if No (Proc) then
7204 return False;
7205 end if;
7207 -- Loop through formals to find the one that matches
7209 Form := First_Formal (Proc);
7210 loop
7211 -- If no matching formal, that's peculiar, some kind of
7212 -- previous error, so return False to be conservative.
7214 if No (Form) then
7215 return False;
7216 end if;
7218 -- Else test for match
7220 if Chars (Form) = Chars (Selector_Name (P)) then
7221 return Ekind (Form) /= E_In_Parameter;
7222 end if;
7224 Next_Formal (Form);
7225 end loop;
7226 end;
7228 -- Test for appearing in a conversion that itself appears
7229 -- in an lvalue context, since this should be an lvalue.
7231 when N_Type_Conversion =>
7232 return Known_To_Be_Assigned (P);
7234 -- All other references are definitely not known to be modifications
7236 when others =>
7237 return False;
7239 end case;
7240 end Known_To_Be_Assigned;
7242 -------------------
7243 -- May_Be_Lvalue --
7244 -------------------
7246 function May_Be_Lvalue (N : Node_Id) return Boolean is
7247 P : constant Node_Id := Parent (N);
7249 begin
7250 case Nkind (P) is
7252 -- Test left side of assignment
7254 when N_Assignment_Statement =>
7255 return N = Name (P);
7257 -- Test prefix of component or attribute. Note that the prefix of an
7258 -- explicit or implicit dereference cannot be an l-value.
7260 when N_Attribute_Reference =>
7261 return N = Prefix (P)
7262 and then Name_Implies_Lvalue_Prefix (Attribute_Name (P));
7264 when N_Expanded_Name |
7265 N_Indexed_Component |
7266 N_Selected_Component |
7267 N_Slice =>
7268 if Is_Access_Type (Etype (N)) then
7269 return False; -- P is an implicit dereference
7270 else
7271 return N = Prefix (P);
7272 end if;
7274 when N_Reference =>
7275 return N = Prefix (P);
7277 when N_Explicit_Dereference =>
7278 return False;
7280 -- Function call arguments are never lvalues
7282 when N_Function_Call =>
7283 return False;
7285 -- Positional parameter for procedure, entry, or accept call
7287 when N_Procedure_Call_Statement |
7288 N_Entry_Call_Statement |
7289 N_Accept_Statement
7291 declare
7292 Proc : Entity_Id;
7293 Form : Entity_Id;
7294 Act : Node_Id;
7296 begin
7297 Proc := Get_Subprogram_Entity (P);
7299 if No (Proc) then
7300 return True;
7301 end if;
7303 -- If we are not a list member, something is strange, so
7304 -- be conservative and return True.
7306 if not Is_List_Member (N) then
7307 return True;
7308 end if;
7310 -- We are going to find the right formal by stepping forward
7311 -- through the formals, as we step backwards in the actuals.
7313 Form := First_Formal (Proc);
7314 Act := N;
7315 loop
7316 -- If no formal, something is weird, so be conservative
7317 -- and return True.
7319 if No (Form) then
7320 return True;
7321 end if;
7323 Prev (Act);
7324 exit when No (Act);
7325 Next_Formal (Form);
7326 end loop;
7328 return Ekind (Form) /= E_In_Parameter;
7329 end;
7331 -- Named parameter for procedure or accept call
7333 when N_Parameter_Association =>
7334 declare
7335 Proc : Entity_Id;
7336 Form : Entity_Id;
7338 begin
7339 Proc := Get_Subprogram_Entity (Parent (P));
7341 if No (Proc) then
7342 return True;
7343 end if;
7345 -- Loop through formals to find the one that matches
7347 Form := First_Formal (Proc);
7348 loop
7349 -- If no matching formal, that's peculiar, some kind of
7350 -- previous error, so return True to be conservative.
7352 if No (Form) then
7353 return True;
7354 end if;
7356 -- Else test for match
7358 if Chars (Form) = Chars (Selector_Name (P)) then
7359 return Ekind (Form) /= E_In_Parameter;
7360 end if;
7362 Next_Formal (Form);
7363 end loop;
7364 end;
7366 -- Test for appearing in a conversion that itself appears in an
7367 -- lvalue context, since this should be an lvalue.
7369 when N_Type_Conversion =>
7370 return May_Be_Lvalue (P);
7372 -- Test for appearance in object renaming declaration
7374 when N_Object_Renaming_Declaration =>
7375 return True;
7377 -- All other references are definitely not Lvalues
7379 when others =>
7380 return False;
7382 end case;
7383 end May_Be_Lvalue;
7385 -----------------------
7386 -- Mark_Coextensions --
7387 -----------------------
7389 procedure Mark_Coextensions (Context_Nod : Node_Id; Root_Nod : Node_Id) is
7390 Is_Dynamic : Boolean;
7391 -- Indicates whether the context causes nested coextensions to be
7392 -- dynamic or static
7394 function Mark_Allocator (N : Node_Id) return Traverse_Result;
7395 -- Recognize an allocator node and label it as a dynamic coextension
7397 --------------------
7398 -- Mark_Allocator --
7399 --------------------
7401 function Mark_Allocator (N : Node_Id) return Traverse_Result is
7402 begin
7403 if Nkind (N) = N_Allocator then
7404 if Is_Dynamic then
7405 Set_Is_Dynamic_Coextension (N);
7406 else
7407 Set_Is_Static_Coextension (N);
7408 end if;
7409 end if;
7411 return OK;
7412 end Mark_Allocator;
7414 procedure Mark_Allocators is new Traverse_Proc (Mark_Allocator);
7416 -- Start of processing Mark_Coextensions
7418 begin
7419 case Nkind (Context_Nod) is
7420 when N_Assignment_Statement |
7421 N_Simple_Return_Statement =>
7422 Is_Dynamic := Nkind (Expression (Context_Nod)) = N_Allocator;
7424 when N_Object_Declaration =>
7425 Is_Dynamic := Nkind (Root_Nod) = N_Allocator;
7427 -- This routine should not be called for constructs which may not
7428 -- contain coextensions.
7430 when others =>
7431 raise Program_Error;
7432 end case;
7434 Mark_Allocators (Root_Nod);
7435 end Mark_Coextensions;
7437 ----------------------
7438 -- Needs_One_Actual --
7439 ----------------------
7441 function Needs_One_Actual (E : Entity_Id) return Boolean is
7442 Formal : Entity_Id;
7444 begin
7445 if Ada_Version >= Ada_05
7446 and then Present (First_Formal (E))
7447 then
7448 Formal := Next_Formal (First_Formal (E));
7449 while Present (Formal) loop
7450 if No (Default_Value (Formal)) then
7451 return False;
7452 end if;
7454 Next_Formal (Formal);
7455 end loop;
7457 return True;
7459 else
7460 return False;
7461 end if;
7462 end Needs_One_Actual;
7464 ------------------------
7465 -- New_Copy_List_Tree --
7466 ------------------------
7468 function New_Copy_List_Tree (List : List_Id) return List_Id is
7469 NL : List_Id;
7470 E : Node_Id;
7472 begin
7473 if List = No_List then
7474 return No_List;
7476 else
7477 NL := New_List;
7478 E := First (List);
7480 while Present (E) loop
7481 Append (New_Copy_Tree (E), NL);
7482 E := Next (E);
7483 end loop;
7485 return NL;
7486 end if;
7487 end New_Copy_List_Tree;
7489 -------------------
7490 -- New_Copy_Tree --
7491 -------------------
7493 use Atree.Unchecked_Access;
7494 use Atree_Private_Part;
7496 -- Our approach here requires a two pass traversal of the tree. The
7497 -- first pass visits all nodes that eventually will be copied looking
7498 -- for defining Itypes. If any defining Itypes are found, then they are
7499 -- copied, and an entry is added to the replacement map. In the second
7500 -- phase, the tree is copied, using the replacement map to replace any
7501 -- Itype references within the copied tree.
7503 -- The following hash tables are used if the Map supplied has more
7504 -- than hash threshhold entries to speed up access to the map. If
7505 -- there are fewer entries, then the map is searched sequentially
7506 -- (because setting up a hash table for only a few entries takes
7507 -- more time than it saves.
7509 function New_Copy_Hash (E : Entity_Id) return NCT_Header_Num;
7510 -- Hash function used for hash operations
7512 -------------------
7513 -- New_Copy_Hash --
7514 -------------------
7516 function New_Copy_Hash (E : Entity_Id) return NCT_Header_Num is
7517 begin
7518 return Nat (E) mod (NCT_Header_Num'Last + 1);
7519 end New_Copy_Hash;
7521 ---------------
7522 -- NCT_Assoc --
7523 ---------------
7525 -- The hash table NCT_Assoc associates old entities in the table
7526 -- with their corresponding new entities (i.e. the pairs of entries
7527 -- presented in the original Map argument are Key-Element pairs).
7529 package NCT_Assoc is new Simple_HTable (
7530 Header_Num => NCT_Header_Num,
7531 Element => Entity_Id,
7532 No_Element => Empty,
7533 Key => Entity_Id,
7534 Hash => New_Copy_Hash,
7535 Equal => Types."=");
7537 ---------------------
7538 -- NCT_Itype_Assoc --
7539 ---------------------
7541 -- The hash table NCT_Itype_Assoc contains entries only for those
7542 -- old nodes which have a non-empty Associated_Node_For_Itype set.
7543 -- The key is the associated node, and the element is the new node
7544 -- itself (NOT the associated node for the new node).
7546 package NCT_Itype_Assoc is new Simple_HTable (
7547 Header_Num => NCT_Header_Num,
7548 Element => Entity_Id,
7549 No_Element => Empty,
7550 Key => Entity_Id,
7551 Hash => New_Copy_Hash,
7552 Equal => Types."=");
7554 -- Start of processing for New_Copy_Tree function
7556 function New_Copy_Tree
7557 (Source : Node_Id;
7558 Map : Elist_Id := No_Elist;
7559 New_Sloc : Source_Ptr := No_Location;
7560 New_Scope : Entity_Id := Empty) return Node_Id
7562 Actual_Map : Elist_Id := Map;
7563 -- This is the actual map for the copy. It is initialized with the
7564 -- given elements, and then enlarged as required for Itypes that are
7565 -- copied during the first phase of the copy operation. The visit
7566 -- procedures add elements to this map as Itypes are encountered.
7567 -- The reason we cannot use Map directly, is that it may well be
7568 -- (and normally is) initialized to No_Elist, and if we have mapped
7569 -- entities, we have to reset it to point to a real Elist.
7571 function Assoc (N : Node_Or_Entity_Id) return Node_Id;
7572 -- Called during second phase to map entities into their corresponding
7573 -- copies using Actual_Map. If the argument is not an entity, or is not
7574 -- in Actual_Map, then it is returned unchanged.
7576 procedure Build_NCT_Hash_Tables;
7577 -- Builds hash tables (number of elements >= threshold value)
7579 function Copy_Elist_With_Replacement
7580 (Old_Elist : Elist_Id) return Elist_Id;
7581 -- Called during second phase to copy element list doing replacements
7583 procedure Copy_Itype_With_Replacement (New_Itype : Entity_Id);
7584 -- Called during the second phase to process a copied Itype. The actual
7585 -- copy happened during the first phase (so that we could make the entry
7586 -- in the mapping), but we still have to deal with the descendents of
7587 -- the copied Itype and copy them where necessary.
7589 function Copy_List_With_Replacement (Old_List : List_Id) return List_Id;
7590 -- Called during second phase to copy list doing replacements
7592 function Copy_Node_With_Replacement (Old_Node : Node_Id) return Node_Id;
7593 -- Called during second phase to copy node doing replacements
7595 procedure Visit_Elist (E : Elist_Id);
7596 -- Called during first phase to visit all elements of an Elist
7598 procedure Visit_Field (F : Union_Id; N : Node_Id);
7599 -- Visit a single field, recursing to call Visit_Node or Visit_List
7600 -- if the field is a syntactic descendent of the current node (i.e.
7601 -- its parent is Node N).
7603 procedure Visit_Itype (Old_Itype : Entity_Id);
7604 -- Called during first phase to visit subsidiary fields of a defining
7605 -- Itype, and also create a copy and make an entry in the replacement
7606 -- map for the new copy.
7608 procedure Visit_List (L : List_Id);
7609 -- Called during first phase to visit all elements of a List
7611 procedure Visit_Node (N : Node_Or_Entity_Id);
7612 -- Called during first phase to visit a node and all its subtrees
7614 -----------
7615 -- Assoc --
7616 -----------
7618 function Assoc (N : Node_Or_Entity_Id) return Node_Id is
7619 E : Elmt_Id;
7620 Ent : Entity_Id;
7622 begin
7623 if not Has_Extension (N) or else No (Actual_Map) then
7624 return N;
7626 elsif NCT_Hash_Tables_Used then
7627 Ent := NCT_Assoc.Get (Entity_Id (N));
7629 if Present (Ent) then
7630 return Ent;
7631 else
7632 return N;
7633 end if;
7635 -- No hash table used, do serial search
7637 else
7638 E := First_Elmt (Actual_Map);
7639 while Present (E) loop
7640 if Node (E) = N then
7641 return Node (Next_Elmt (E));
7642 else
7643 E := Next_Elmt (Next_Elmt (E));
7644 end if;
7645 end loop;
7646 end if;
7648 return N;
7649 end Assoc;
7651 ---------------------------
7652 -- Build_NCT_Hash_Tables --
7653 ---------------------------
7655 procedure Build_NCT_Hash_Tables is
7656 Elmt : Elmt_Id;
7657 Ent : Entity_Id;
7658 begin
7659 if NCT_Hash_Table_Setup then
7660 NCT_Assoc.Reset;
7661 NCT_Itype_Assoc.Reset;
7662 end if;
7664 Elmt := First_Elmt (Actual_Map);
7665 while Present (Elmt) loop
7666 Ent := Node (Elmt);
7668 -- Get new entity, and associate old and new
7670 Next_Elmt (Elmt);
7671 NCT_Assoc.Set (Ent, Node (Elmt));
7673 if Is_Type (Ent) then
7674 declare
7675 Anode : constant Entity_Id :=
7676 Associated_Node_For_Itype (Ent);
7678 begin
7679 if Present (Anode) then
7681 -- Enter a link between the associated node of the
7682 -- old Itype and the new Itype, for updating later
7683 -- when node is copied.
7685 NCT_Itype_Assoc.Set (Anode, Node (Elmt));
7686 end if;
7687 end;
7688 end if;
7690 Next_Elmt (Elmt);
7691 end loop;
7693 NCT_Hash_Tables_Used := True;
7694 NCT_Hash_Table_Setup := True;
7695 end Build_NCT_Hash_Tables;
7697 ---------------------------------
7698 -- Copy_Elist_With_Replacement --
7699 ---------------------------------
7701 function Copy_Elist_With_Replacement
7702 (Old_Elist : Elist_Id) return Elist_Id
7704 M : Elmt_Id;
7705 New_Elist : Elist_Id;
7707 begin
7708 if No (Old_Elist) then
7709 return No_Elist;
7711 else
7712 New_Elist := New_Elmt_List;
7714 M := First_Elmt (Old_Elist);
7715 while Present (M) loop
7716 Append_Elmt (Copy_Node_With_Replacement (Node (M)), New_Elist);
7717 Next_Elmt (M);
7718 end loop;
7719 end if;
7721 return New_Elist;
7722 end Copy_Elist_With_Replacement;
7724 ---------------------------------
7725 -- Copy_Itype_With_Replacement --
7726 ---------------------------------
7728 -- This routine exactly parallels its phase one analog Visit_Itype,
7730 procedure Copy_Itype_With_Replacement (New_Itype : Entity_Id) is
7731 begin
7732 -- Translate Next_Entity, Scope and Etype fields, in case they
7733 -- reference entities that have been mapped into copies.
7735 Set_Next_Entity (New_Itype, Assoc (Next_Entity (New_Itype)));
7736 Set_Etype (New_Itype, Assoc (Etype (New_Itype)));
7738 if Present (New_Scope) then
7739 Set_Scope (New_Itype, New_Scope);
7740 else
7741 Set_Scope (New_Itype, Assoc (Scope (New_Itype)));
7742 end if;
7744 -- Copy referenced fields
7746 if Is_Discrete_Type (New_Itype) then
7747 Set_Scalar_Range (New_Itype,
7748 Copy_Node_With_Replacement (Scalar_Range (New_Itype)));
7750 elsif Has_Discriminants (Base_Type (New_Itype)) then
7751 Set_Discriminant_Constraint (New_Itype,
7752 Copy_Elist_With_Replacement
7753 (Discriminant_Constraint (New_Itype)));
7755 elsif Is_Array_Type (New_Itype) then
7756 if Present (First_Index (New_Itype)) then
7757 Set_First_Index (New_Itype,
7758 First (Copy_List_With_Replacement
7759 (List_Containing (First_Index (New_Itype)))));
7760 end if;
7762 if Is_Packed (New_Itype) then
7763 Set_Packed_Array_Type (New_Itype,
7764 Copy_Node_With_Replacement
7765 (Packed_Array_Type (New_Itype)));
7766 end if;
7767 end if;
7768 end Copy_Itype_With_Replacement;
7770 --------------------------------
7771 -- Copy_List_With_Replacement --
7772 --------------------------------
7774 function Copy_List_With_Replacement
7775 (Old_List : List_Id) return List_Id
7777 New_List : List_Id;
7778 E : Node_Id;
7780 begin
7781 if Old_List = No_List then
7782 return No_List;
7784 else
7785 New_List := Empty_List;
7787 E := First (Old_List);
7788 while Present (E) loop
7789 Append (Copy_Node_With_Replacement (E), New_List);
7790 Next (E);
7791 end loop;
7793 return New_List;
7794 end if;
7795 end Copy_List_With_Replacement;
7797 --------------------------------
7798 -- Copy_Node_With_Replacement --
7799 --------------------------------
7801 function Copy_Node_With_Replacement
7802 (Old_Node : Node_Id) return Node_Id
7804 New_Node : Node_Id;
7806 procedure Adjust_Named_Associations
7807 (Old_Node : Node_Id;
7808 New_Node : Node_Id);
7809 -- If a call node has named associations, these are chained through
7810 -- the First_Named_Actual, Next_Named_Actual links. These must be
7811 -- propagated separately to the new parameter list, because these
7812 -- are not syntactic fields.
7814 function Copy_Field_With_Replacement
7815 (Field : Union_Id) return Union_Id;
7816 -- Given Field, which is a field of Old_Node, return a copy of it
7817 -- if it is a syntactic field (i.e. its parent is Node), setting
7818 -- the parent of the copy to poit to New_Node. Otherwise returns
7819 -- the field (possibly mapped if it is an entity).
7821 -------------------------------
7822 -- Adjust_Named_Associations --
7823 -------------------------------
7825 procedure Adjust_Named_Associations
7826 (Old_Node : Node_Id;
7827 New_Node : Node_Id)
7829 Old_E : Node_Id;
7830 New_E : Node_Id;
7832 Old_Next : Node_Id;
7833 New_Next : Node_Id;
7835 begin
7836 Old_E := First (Parameter_Associations (Old_Node));
7837 New_E := First (Parameter_Associations (New_Node));
7838 while Present (Old_E) loop
7839 if Nkind (Old_E) = N_Parameter_Association
7840 and then Present (Next_Named_Actual (Old_E))
7841 then
7842 if First_Named_Actual (Old_Node)
7843 = Explicit_Actual_Parameter (Old_E)
7844 then
7845 Set_First_Named_Actual
7846 (New_Node, Explicit_Actual_Parameter (New_E));
7847 end if;
7849 -- Now scan parameter list from the beginning,to locate
7850 -- next named actual, which can be out of order.
7852 Old_Next := First (Parameter_Associations (Old_Node));
7853 New_Next := First (Parameter_Associations (New_Node));
7855 while Nkind (Old_Next) /= N_Parameter_Association
7856 or else Explicit_Actual_Parameter (Old_Next)
7857 /= Next_Named_Actual (Old_E)
7858 loop
7859 Next (Old_Next);
7860 Next (New_Next);
7861 end loop;
7863 Set_Next_Named_Actual
7864 (New_E, Explicit_Actual_Parameter (New_Next));
7865 end if;
7867 Next (Old_E);
7868 Next (New_E);
7869 end loop;
7870 end Adjust_Named_Associations;
7872 ---------------------------------
7873 -- Copy_Field_With_Replacement --
7874 ---------------------------------
7876 function Copy_Field_With_Replacement
7877 (Field : Union_Id) return Union_Id
7879 begin
7880 if Field = Union_Id (Empty) then
7881 return Field;
7883 elsif Field in Node_Range then
7884 declare
7885 Old_N : constant Node_Id := Node_Id (Field);
7886 New_N : Node_Id;
7888 begin
7889 -- If syntactic field, as indicated by the parent pointer
7890 -- being set, then copy the referenced node recursively.
7892 if Parent (Old_N) = Old_Node then
7893 New_N := Copy_Node_With_Replacement (Old_N);
7895 if New_N /= Old_N then
7896 Set_Parent (New_N, New_Node);
7897 end if;
7899 -- For semantic fields, update possible entity reference
7900 -- from the replacement map.
7902 else
7903 New_N := Assoc (Old_N);
7904 end if;
7906 return Union_Id (New_N);
7907 end;
7909 elsif Field in List_Range then
7910 declare
7911 Old_L : constant List_Id := List_Id (Field);
7912 New_L : List_Id;
7914 begin
7915 -- If syntactic field, as indicated by the parent pointer,
7916 -- then recursively copy the entire referenced list.
7918 if Parent (Old_L) = Old_Node then
7919 New_L := Copy_List_With_Replacement (Old_L);
7920 Set_Parent (New_L, New_Node);
7922 -- For semantic list, just returned unchanged
7924 else
7925 New_L := Old_L;
7926 end if;
7928 return Union_Id (New_L);
7929 end;
7931 -- Anything other than a list or a node is returned unchanged
7933 else
7934 return Field;
7935 end if;
7936 end Copy_Field_With_Replacement;
7938 -- Start of processing for Copy_Node_With_Replacement
7940 begin
7941 if Old_Node <= Empty_Or_Error then
7942 return Old_Node;
7944 elsif Has_Extension (Old_Node) then
7945 return Assoc (Old_Node);
7947 else
7948 New_Node := New_Copy (Old_Node);
7950 -- If the node we are copying is the associated node of a
7951 -- previously copied Itype, then adjust the associated node
7952 -- of the copy of that Itype accordingly.
7954 if Present (Actual_Map) then
7955 declare
7956 E : Elmt_Id;
7957 Ent : Entity_Id;
7959 begin
7960 -- Case of hash table used
7962 if NCT_Hash_Tables_Used then
7963 Ent := NCT_Itype_Assoc.Get (Old_Node);
7965 if Present (Ent) then
7966 Set_Associated_Node_For_Itype (Ent, New_Node);
7967 end if;
7969 -- Case of no hash table used
7971 else
7972 E := First_Elmt (Actual_Map);
7973 while Present (E) loop
7974 if Is_Itype (Node (E))
7975 and then
7976 Old_Node = Associated_Node_For_Itype (Node (E))
7977 then
7978 Set_Associated_Node_For_Itype
7979 (Node (Next_Elmt (E)), New_Node);
7980 end if;
7982 E := Next_Elmt (Next_Elmt (E));
7983 end loop;
7984 end if;
7985 end;
7986 end if;
7988 -- Recursively copy descendents
7990 Set_Field1
7991 (New_Node, Copy_Field_With_Replacement (Field1 (New_Node)));
7992 Set_Field2
7993 (New_Node, Copy_Field_With_Replacement (Field2 (New_Node)));
7994 Set_Field3
7995 (New_Node, Copy_Field_With_Replacement (Field3 (New_Node)));
7996 Set_Field4
7997 (New_Node, Copy_Field_With_Replacement (Field4 (New_Node)));
7998 Set_Field5
7999 (New_Node, Copy_Field_With_Replacement (Field5 (New_Node)));
8001 -- Adjust Sloc of new node if necessary
8003 if New_Sloc /= No_Location then
8004 Set_Sloc (New_Node, New_Sloc);
8006 -- If we adjust the Sloc, then we are essentially making
8007 -- a completely new node, so the Comes_From_Source flag
8008 -- should be reset to the proper default value.
8010 Nodes.Table (New_Node).Comes_From_Source :=
8011 Default_Node.Comes_From_Source;
8012 end if;
8014 -- If the node is call and has named associations,
8015 -- set the corresponding links in the copy.
8017 if (Nkind (Old_Node) = N_Function_Call
8018 or else Nkind (Old_Node) = N_Entry_Call_Statement
8019 or else
8020 Nkind (Old_Node) = N_Procedure_Call_Statement)
8021 and then Present (First_Named_Actual (Old_Node))
8022 then
8023 Adjust_Named_Associations (Old_Node, New_Node);
8024 end if;
8026 -- Reset First_Real_Statement for Handled_Sequence_Of_Statements.
8027 -- The replacement mechanism applies to entities, and is not used
8028 -- here. Eventually we may need a more general graph-copying
8029 -- routine. For now, do a sequential search to find desired node.
8031 if Nkind (Old_Node) = N_Handled_Sequence_Of_Statements
8032 and then Present (First_Real_Statement (Old_Node))
8033 then
8034 declare
8035 Old_F : constant Node_Id := First_Real_Statement (Old_Node);
8036 N1, N2 : Node_Id;
8038 begin
8039 N1 := First (Statements (Old_Node));
8040 N2 := First (Statements (New_Node));
8042 while N1 /= Old_F loop
8043 Next (N1);
8044 Next (N2);
8045 end loop;
8047 Set_First_Real_Statement (New_Node, N2);
8048 end;
8049 end if;
8050 end if;
8052 -- All done, return copied node
8054 return New_Node;
8055 end Copy_Node_With_Replacement;
8057 -----------------
8058 -- Visit_Elist --
8059 -----------------
8061 procedure Visit_Elist (E : Elist_Id) is
8062 Elmt : Elmt_Id;
8063 begin
8064 if Present (E) then
8065 Elmt := First_Elmt (E);
8067 while Elmt /= No_Elmt loop
8068 Visit_Node (Node (Elmt));
8069 Next_Elmt (Elmt);
8070 end loop;
8071 end if;
8072 end Visit_Elist;
8074 -----------------
8075 -- Visit_Field --
8076 -----------------
8078 procedure Visit_Field (F : Union_Id; N : Node_Id) is
8079 begin
8080 if F = Union_Id (Empty) then
8081 return;
8083 elsif F in Node_Range then
8085 -- Copy node if it is syntactic, i.e. its parent pointer is
8086 -- set to point to the field that referenced it (certain
8087 -- Itypes will also meet this criterion, which is fine, since
8088 -- these are clearly Itypes that do need to be copied, since
8089 -- we are copying their parent.)
8091 if Parent (Node_Id (F)) = N then
8092 Visit_Node (Node_Id (F));
8093 return;
8095 -- Another case, if we are pointing to an Itype, then we want
8096 -- to copy it if its associated node is somewhere in the tree
8097 -- being copied.
8099 -- Note: the exclusion of self-referential copies is just an
8100 -- optimization, since the search of the already copied list
8101 -- would catch it, but it is a common case (Etype pointing
8102 -- to itself for an Itype that is a base type).
8104 elsif Has_Extension (Node_Id (F))
8105 and then Is_Itype (Entity_Id (F))
8106 and then Node_Id (F) /= N
8107 then
8108 declare
8109 P : Node_Id;
8111 begin
8112 P := Associated_Node_For_Itype (Node_Id (F));
8113 while Present (P) loop
8114 if P = Source then
8115 Visit_Node (Node_Id (F));
8116 return;
8117 else
8118 P := Parent (P);
8119 end if;
8120 end loop;
8122 -- An Itype whose parent is not being copied definitely
8123 -- should NOT be copied, since it does not belong in any
8124 -- sense to the copied subtree.
8126 return;
8127 end;
8128 end if;
8130 elsif F in List_Range
8131 and then Parent (List_Id (F)) = N
8132 then
8133 Visit_List (List_Id (F));
8134 return;
8135 end if;
8136 end Visit_Field;
8138 -----------------
8139 -- Visit_Itype --
8140 -----------------
8142 procedure Visit_Itype (Old_Itype : Entity_Id) is
8143 New_Itype : Entity_Id;
8144 E : Elmt_Id;
8145 Ent : Entity_Id;
8147 begin
8148 -- Itypes that describe the designated type of access to subprograms
8149 -- have the structure of subprogram declarations, with signatures,
8150 -- etc. Either we duplicate the signatures completely, or choose to
8151 -- share such itypes, which is fine because their elaboration will
8152 -- have no side effects.
8154 if Ekind (Old_Itype) = E_Subprogram_Type then
8155 return;
8156 end if;
8158 New_Itype := New_Copy (Old_Itype);
8160 -- The new Itype has all the attributes of the old one, and
8161 -- we just copy the contents of the entity. However, the back-end
8162 -- needs different names for debugging purposes, so we create a
8163 -- new internal name for it in all cases.
8165 Set_Chars (New_Itype, New_Internal_Name ('T'));
8167 -- If our associated node is an entity that has already been copied,
8168 -- then set the associated node of the copy to point to the right
8169 -- copy. If we have copied an Itype that is itself the associated
8170 -- node of some previously copied Itype, then we set the right
8171 -- pointer in the other direction.
8173 if Present (Actual_Map) then
8175 -- Case of hash tables used
8177 if NCT_Hash_Tables_Used then
8179 Ent := NCT_Assoc.Get (Associated_Node_For_Itype (Old_Itype));
8181 if Present (Ent) then
8182 Set_Associated_Node_For_Itype (New_Itype, Ent);
8183 end if;
8185 Ent := NCT_Itype_Assoc.Get (Old_Itype);
8186 if Present (Ent) then
8187 Set_Associated_Node_For_Itype (Ent, New_Itype);
8189 -- If the hash table has no association for this Itype and
8190 -- its associated node, enter one now.
8192 else
8193 NCT_Itype_Assoc.Set
8194 (Associated_Node_For_Itype (Old_Itype), New_Itype);
8195 end if;
8197 -- Case of hash tables not used
8199 else
8200 E := First_Elmt (Actual_Map);
8201 while Present (E) loop
8202 if Associated_Node_For_Itype (Old_Itype) = Node (E) then
8203 Set_Associated_Node_For_Itype
8204 (New_Itype, Node (Next_Elmt (E)));
8205 end if;
8207 if Is_Type (Node (E))
8208 and then
8209 Old_Itype = Associated_Node_For_Itype (Node (E))
8210 then
8211 Set_Associated_Node_For_Itype
8212 (Node (Next_Elmt (E)), New_Itype);
8213 end if;
8215 E := Next_Elmt (Next_Elmt (E));
8216 end loop;
8217 end if;
8218 end if;
8220 if Present (Freeze_Node (New_Itype)) then
8221 Set_Is_Frozen (New_Itype, False);
8222 Set_Freeze_Node (New_Itype, Empty);
8223 end if;
8225 -- Add new association to map
8227 if No (Actual_Map) then
8228 Actual_Map := New_Elmt_List;
8229 end if;
8231 Append_Elmt (Old_Itype, Actual_Map);
8232 Append_Elmt (New_Itype, Actual_Map);
8234 if NCT_Hash_Tables_Used then
8235 NCT_Assoc.Set (Old_Itype, New_Itype);
8237 else
8238 NCT_Table_Entries := NCT_Table_Entries + 1;
8240 if NCT_Table_Entries > NCT_Hash_Threshhold then
8241 Build_NCT_Hash_Tables;
8242 end if;
8243 end if;
8245 -- If a record subtype is simply copied, the entity list will be
8246 -- shared. Thus cloned_Subtype must be set to indicate the sharing.
8248 if Ekind (Old_Itype) = E_Record_Subtype
8249 or else Ekind (Old_Itype) = E_Class_Wide_Subtype
8250 then
8251 Set_Cloned_Subtype (New_Itype, Old_Itype);
8252 end if;
8254 -- Visit descendents that eventually get copied
8256 Visit_Field (Union_Id (Etype (Old_Itype)), Old_Itype);
8258 if Is_Discrete_Type (Old_Itype) then
8259 Visit_Field (Union_Id (Scalar_Range (Old_Itype)), Old_Itype);
8261 elsif Has_Discriminants (Base_Type (Old_Itype)) then
8262 -- ??? This should involve call to Visit_Field
8263 Visit_Elist (Discriminant_Constraint (Old_Itype));
8265 elsif Is_Array_Type (Old_Itype) then
8266 if Present (First_Index (Old_Itype)) then
8267 Visit_Field (Union_Id (List_Containing
8268 (First_Index (Old_Itype))),
8269 Old_Itype);
8270 end if;
8272 if Is_Packed (Old_Itype) then
8273 Visit_Field (Union_Id (Packed_Array_Type (Old_Itype)),
8274 Old_Itype);
8275 end if;
8276 end if;
8277 end Visit_Itype;
8279 ----------------
8280 -- Visit_List --
8281 ----------------
8283 procedure Visit_List (L : List_Id) is
8284 N : Node_Id;
8285 begin
8286 if L /= No_List then
8287 N := First (L);
8289 while Present (N) loop
8290 Visit_Node (N);
8291 Next (N);
8292 end loop;
8293 end if;
8294 end Visit_List;
8296 ----------------
8297 -- Visit_Node --
8298 ----------------
8300 procedure Visit_Node (N : Node_Or_Entity_Id) is
8302 -- Start of processing for Visit_Node
8304 begin
8305 -- Handle case of an Itype, which must be copied
8307 if Has_Extension (N)
8308 and then Is_Itype (N)
8309 then
8310 -- Nothing to do if already in the list. This can happen with an
8311 -- Itype entity that appears more than once in the tree.
8312 -- Note that we do not want to visit descendents in this case.
8314 -- Test for already in list when hash table is used
8316 if NCT_Hash_Tables_Used then
8317 if Present (NCT_Assoc.Get (Entity_Id (N))) then
8318 return;
8319 end if;
8321 -- Test for already in list when hash table not used
8323 else
8324 declare
8325 E : Elmt_Id;
8326 begin
8327 if Present (Actual_Map) then
8328 E := First_Elmt (Actual_Map);
8329 while Present (E) loop
8330 if Node (E) = N then
8331 return;
8332 else
8333 E := Next_Elmt (Next_Elmt (E));
8334 end if;
8335 end loop;
8336 end if;
8337 end;
8338 end if;
8340 Visit_Itype (N);
8341 end if;
8343 -- Visit descendents
8345 Visit_Field (Field1 (N), N);
8346 Visit_Field (Field2 (N), N);
8347 Visit_Field (Field3 (N), N);
8348 Visit_Field (Field4 (N), N);
8349 Visit_Field (Field5 (N), N);
8350 end Visit_Node;
8352 -- Start of processing for New_Copy_Tree
8354 begin
8355 Actual_Map := Map;
8357 -- See if we should use hash table
8359 if No (Actual_Map) then
8360 NCT_Hash_Tables_Used := False;
8362 else
8363 declare
8364 Elmt : Elmt_Id;
8366 begin
8367 NCT_Table_Entries := 0;
8369 Elmt := First_Elmt (Actual_Map);
8370 while Present (Elmt) loop
8371 NCT_Table_Entries := NCT_Table_Entries + 1;
8372 Next_Elmt (Elmt);
8373 Next_Elmt (Elmt);
8374 end loop;
8376 if NCT_Table_Entries > NCT_Hash_Threshhold then
8377 Build_NCT_Hash_Tables;
8378 else
8379 NCT_Hash_Tables_Used := False;
8380 end if;
8381 end;
8382 end if;
8384 -- Hash table set up if required, now start phase one by visiting
8385 -- top node (we will recursively visit the descendents).
8387 Visit_Node (Source);
8389 -- Now the second phase of the copy can start. First we process
8390 -- all the mapped entities, copying their descendents.
8392 if Present (Actual_Map) then
8393 declare
8394 Elmt : Elmt_Id;
8395 New_Itype : Entity_Id;
8396 begin
8397 Elmt := First_Elmt (Actual_Map);
8398 while Present (Elmt) loop
8399 Next_Elmt (Elmt);
8400 New_Itype := Node (Elmt);
8401 Copy_Itype_With_Replacement (New_Itype);
8402 Next_Elmt (Elmt);
8403 end loop;
8404 end;
8405 end if;
8407 -- Now we can copy the actual tree
8409 return Copy_Node_With_Replacement (Source);
8410 end New_Copy_Tree;
8412 -------------------------
8413 -- New_External_Entity --
8414 -------------------------
8416 function New_External_Entity
8417 (Kind : Entity_Kind;
8418 Scope_Id : Entity_Id;
8419 Sloc_Value : Source_Ptr;
8420 Related_Id : Entity_Id;
8421 Suffix : Character;
8422 Suffix_Index : Nat := 0;
8423 Prefix : Character := ' ') return Entity_Id
8425 N : constant Entity_Id :=
8426 Make_Defining_Identifier (Sloc_Value,
8427 New_External_Name
8428 (Chars (Related_Id), Suffix, Suffix_Index, Prefix));
8430 begin
8431 Set_Ekind (N, Kind);
8432 Set_Is_Internal (N, True);
8433 Append_Entity (N, Scope_Id);
8434 Set_Public_Status (N);
8436 if Kind in Type_Kind then
8437 Init_Size_Align (N);
8438 end if;
8440 return N;
8441 end New_External_Entity;
8443 -------------------------
8444 -- New_Internal_Entity --
8445 -------------------------
8447 function New_Internal_Entity
8448 (Kind : Entity_Kind;
8449 Scope_Id : Entity_Id;
8450 Sloc_Value : Source_Ptr;
8451 Id_Char : Character) return Entity_Id
8453 N : constant Entity_Id :=
8454 Make_Defining_Identifier (Sloc_Value, New_Internal_Name (Id_Char));
8456 begin
8457 Set_Ekind (N, Kind);
8458 Set_Is_Internal (N, True);
8459 Append_Entity (N, Scope_Id);
8461 if Kind in Type_Kind then
8462 Init_Size_Align (N);
8463 end if;
8465 return N;
8466 end New_Internal_Entity;
8468 -----------------
8469 -- Next_Actual --
8470 -----------------
8472 function Next_Actual (Actual_Id : Node_Id) return Node_Id is
8473 N : Node_Id;
8475 begin
8476 -- If we are pointing at a positional parameter, it is a member of a
8477 -- node list (the list of parameters), and the next parameter is the
8478 -- next node on the list, unless we hit a parameter association, then
8479 -- we shift to using the chain whose head is the First_Named_Actual in
8480 -- the parent, and then is threaded using the Next_Named_Actual of the
8481 -- Parameter_Association. All this fiddling is because the original node
8482 -- list is in the textual call order, and what we need is the
8483 -- declaration order.
8485 if Is_List_Member (Actual_Id) then
8486 N := Next (Actual_Id);
8488 if Nkind (N) = N_Parameter_Association then
8489 return First_Named_Actual (Parent (Actual_Id));
8490 else
8491 return N;
8492 end if;
8494 else
8495 return Next_Named_Actual (Parent (Actual_Id));
8496 end if;
8497 end Next_Actual;
8499 procedure Next_Actual (Actual_Id : in out Node_Id) is
8500 begin
8501 Actual_Id := Next_Actual (Actual_Id);
8502 end Next_Actual;
8504 -----------------------
8505 -- Normalize_Actuals --
8506 -----------------------
8508 -- Chain actuals according to formals of subprogram. If there are no named
8509 -- associations, the chain is simply the list of Parameter Associations,
8510 -- since the order is the same as the declaration order. If there are named
8511 -- associations, then the First_Named_Actual field in the N_Function_Call
8512 -- or N_Procedure_Call_Statement node points to the Parameter_Association
8513 -- node for the parameter that comes first in declaration order. The
8514 -- remaining named parameters are then chained in declaration order using
8515 -- Next_Named_Actual.
8517 -- This routine also verifies that the number of actuals is compatible with
8518 -- the number and default values of formals, but performs no type checking
8519 -- (type checking is done by the caller).
8521 -- If the matching succeeds, Success is set to True and the caller proceeds
8522 -- with type-checking. If the match is unsuccessful, then Success is set to
8523 -- False, and the caller attempts a different interpretation, if there is
8524 -- one.
8526 -- If the flag Report is on, the call is not overloaded, and a failure to
8527 -- match can be reported here, rather than in the caller.
8529 procedure Normalize_Actuals
8530 (N : Node_Id;
8531 S : Entity_Id;
8532 Report : Boolean;
8533 Success : out Boolean)
8535 Actuals : constant List_Id := Parameter_Associations (N);
8536 Actual : Node_Id := Empty;
8537 Formal : Entity_Id;
8538 Last : Node_Id := Empty;
8539 First_Named : Node_Id := Empty;
8540 Found : Boolean;
8542 Formals_To_Match : Integer := 0;
8543 Actuals_To_Match : Integer := 0;
8545 procedure Chain (A : Node_Id);
8546 -- Add named actual at the proper place in the list, using the
8547 -- Next_Named_Actual link.
8549 function Reporting return Boolean;
8550 -- Determines if an error is to be reported. To report an error, we
8551 -- need Report to be True, and also we do not report errors caused
8552 -- by calls to init procs that occur within other init procs. Such
8553 -- errors must always be cascaded errors, since if all the types are
8554 -- declared correctly, the compiler will certainly build decent calls!
8556 -----------
8557 -- Chain --
8558 -----------
8560 procedure Chain (A : Node_Id) is
8561 begin
8562 if No (Last) then
8564 -- Call node points to first actual in list
8566 Set_First_Named_Actual (N, Explicit_Actual_Parameter (A));
8568 else
8569 Set_Next_Named_Actual (Last, Explicit_Actual_Parameter (A));
8570 end if;
8572 Last := A;
8573 Set_Next_Named_Actual (Last, Empty);
8574 end Chain;
8576 ---------------
8577 -- Reporting --
8578 ---------------
8580 function Reporting return Boolean is
8581 begin
8582 if not Report then
8583 return False;
8585 elsif not Within_Init_Proc then
8586 return True;
8588 elsif Is_Init_Proc (Entity (Name (N))) then
8589 return False;
8591 else
8592 return True;
8593 end if;
8594 end Reporting;
8596 -- Start of processing for Normalize_Actuals
8598 begin
8599 if Is_Access_Type (S) then
8601 -- The name in the call is a function call that returns an access
8602 -- to subprogram. The designated type has the list of formals.
8604 Formal := First_Formal (Designated_Type (S));
8605 else
8606 Formal := First_Formal (S);
8607 end if;
8609 while Present (Formal) loop
8610 Formals_To_Match := Formals_To_Match + 1;
8611 Next_Formal (Formal);
8612 end loop;
8614 -- Find if there is a named association, and verify that no positional
8615 -- associations appear after named ones.
8617 if Present (Actuals) then
8618 Actual := First (Actuals);
8619 end if;
8621 while Present (Actual)
8622 and then Nkind (Actual) /= N_Parameter_Association
8623 loop
8624 Actuals_To_Match := Actuals_To_Match + 1;
8625 Next (Actual);
8626 end loop;
8628 if No (Actual) and Actuals_To_Match = Formals_To_Match then
8630 -- Most common case: positional notation, no defaults
8632 Success := True;
8633 return;
8635 elsif Actuals_To_Match > Formals_To_Match then
8637 -- Too many actuals: will not work
8639 if Reporting then
8640 if Is_Entity_Name (Name (N)) then
8641 Error_Msg_N ("too many arguments in call to&", Name (N));
8642 else
8643 Error_Msg_N ("too many arguments in call", N);
8644 end if;
8645 end if;
8647 Success := False;
8648 return;
8649 end if;
8651 First_Named := Actual;
8653 while Present (Actual) loop
8654 if Nkind (Actual) /= N_Parameter_Association then
8655 Error_Msg_N
8656 ("positional parameters not allowed after named ones", Actual);
8657 Success := False;
8658 return;
8660 else
8661 Actuals_To_Match := Actuals_To_Match + 1;
8662 end if;
8664 Next (Actual);
8665 end loop;
8667 if Present (Actuals) then
8668 Actual := First (Actuals);
8669 end if;
8671 Formal := First_Formal (S);
8672 while Present (Formal) loop
8674 -- Match the formals in order. If the corresponding actual is
8675 -- positional, nothing to do. Else scan the list of named actuals
8676 -- to find the one with the right name.
8678 if Present (Actual)
8679 and then Nkind (Actual) /= N_Parameter_Association
8680 then
8681 Next (Actual);
8682 Actuals_To_Match := Actuals_To_Match - 1;
8683 Formals_To_Match := Formals_To_Match - 1;
8685 else
8686 -- For named parameters, search the list of actuals to find
8687 -- one that matches the next formal name.
8689 Actual := First_Named;
8690 Found := False;
8691 while Present (Actual) loop
8692 if Chars (Selector_Name (Actual)) = Chars (Formal) then
8693 Found := True;
8694 Chain (Actual);
8695 Actuals_To_Match := Actuals_To_Match - 1;
8696 Formals_To_Match := Formals_To_Match - 1;
8697 exit;
8698 end if;
8700 Next (Actual);
8701 end loop;
8703 if not Found then
8704 if Ekind (Formal) /= E_In_Parameter
8705 or else No (Default_Value (Formal))
8706 then
8707 if Reporting then
8708 if (Comes_From_Source (S)
8709 or else Sloc (S) = Standard_Location)
8710 and then Is_Overloadable (S)
8711 then
8712 if No (Actuals)
8713 and then
8714 (Nkind (Parent (N)) = N_Procedure_Call_Statement
8715 or else
8716 (Nkind (Parent (N)) = N_Function_Call
8717 or else
8718 Nkind (Parent (N)) = N_Parameter_Association))
8719 and then Ekind (S) /= E_Function
8720 then
8721 Set_Etype (N, Etype (S));
8722 else
8723 Error_Msg_Name_1 := Chars (S);
8724 Error_Msg_Sloc := Sloc (S);
8725 Error_Msg_NE
8726 ("missing argument for parameter & " &
8727 "in call to % declared #", N, Formal);
8728 end if;
8730 elsif Is_Overloadable (S) then
8731 Error_Msg_Name_1 := Chars (S);
8733 -- Point to type derivation that generated the
8734 -- operation.
8736 Error_Msg_Sloc := Sloc (Parent (S));
8738 Error_Msg_NE
8739 ("missing argument for parameter & " &
8740 "in call to % (inherited) #", N, Formal);
8742 else
8743 Error_Msg_NE
8744 ("missing argument for parameter &", N, Formal);
8745 end if;
8746 end if;
8748 Success := False;
8749 return;
8751 else
8752 Formals_To_Match := Formals_To_Match - 1;
8753 end if;
8754 end if;
8755 end if;
8757 Next_Formal (Formal);
8758 end loop;
8760 if Formals_To_Match = 0 and then Actuals_To_Match = 0 then
8761 Success := True;
8762 return;
8764 else
8765 if Reporting then
8767 -- Find some superfluous named actual that did not get
8768 -- attached to the list of associations.
8770 Actual := First (Actuals);
8771 while Present (Actual) loop
8772 if Nkind (Actual) = N_Parameter_Association
8773 and then Actual /= Last
8774 and then No (Next_Named_Actual (Actual))
8775 then
8776 Error_Msg_N ("unmatched actual & in call",
8777 Selector_Name (Actual));
8778 exit;
8779 end if;
8781 Next (Actual);
8782 end loop;
8783 end if;
8785 Success := False;
8786 return;
8787 end if;
8788 end Normalize_Actuals;
8790 --------------------------------
8791 -- Note_Possible_Modification --
8792 --------------------------------
8794 procedure Note_Possible_Modification (N : Node_Id; Sure : Boolean) is
8795 Modification_Comes_From_Source : constant Boolean :=
8796 Comes_From_Source (Parent (N));
8798 Ent : Entity_Id;
8799 Exp : Node_Id;
8801 begin
8802 -- Loop to find referenced entity, if there is one
8804 Exp := N;
8805 loop
8806 <<Continue>>
8807 Ent := Empty;
8809 if Is_Entity_Name (Exp) then
8810 Ent := Entity (Exp);
8812 -- If the entity is missing, it is an undeclared identifier,
8813 -- and there is nothing to annotate.
8815 if No (Ent) then
8816 return;
8817 end if;
8819 elsif Nkind (Exp) = N_Explicit_Dereference then
8820 declare
8821 P : constant Node_Id := Prefix (Exp);
8823 begin
8824 if Nkind (P) = N_Selected_Component
8825 and then Present (
8826 Entry_Formal (Entity (Selector_Name (P))))
8827 then
8828 -- Case of a reference to an entry formal
8830 Ent := Entry_Formal (Entity (Selector_Name (P)));
8832 elsif Nkind (P) = N_Identifier
8833 and then Nkind (Parent (Entity (P))) = N_Object_Declaration
8834 and then Present (Expression (Parent (Entity (P))))
8835 and then Nkind (Expression (Parent (Entity (P))))
8836 = N_Reference
8837 then
8838 -- Case of a reference to a value on which side effects have
8839 -- been removed.
8841 Exp := Prefix (Expression (Parent (Entity (P))));
8842 goto Continue;
8844 else
8845 return;
8847 end if;
8848 end;
8850 elsif Nkind (Exp) = N_Type_Conversion
8851 or else Nkind (Exp) = N_Unchecked_Type_Conversion
8852 then
8853 Exp := Expression (Exp);
8854 goto Continue;
8856 elsif Nkind (Exp) = N_Slice
8857 or else Nkind (Exp) = N_Indexed_Component
8858 or else Nkind (Exp) = N_Selected_Component
8859 then
8860 Exp := Prefix (Exp);
8861 goto Continue;
8863 else
8864 return;
8865 end if;
8867 -- Now look for entity being referenced
8869 if Present (Ent) then
8870 if Is_Object (Ent) then
8871 if Comes_From_Source (Exp)
8872 or else Modification_Comes_From_Source
8873 then
8874 if Has_Pragma_Unmodified (Ent) then
8875 Error_Msg_NE ("?pragma Unmodified given for &!", N, Ent);
8876 end if;
8878 Set_Never_Set_In_Source (Ent, False);
8879 end if;
8881 Set_Is_True_Constant (Ent, False);
8882 Set_Current_Value (Ent, Empty);
8883 Set_Is_Known_Null (Ent, False);
8885 if not Can_Never_Be_Null (Ent) then
8886 Set_Is_Known_Non_Null (Ent, False);
8887 end if;
8889 -- Follow renaming chain
8891 if (Ekind (Ent) = E_Variable or else Ekind (Ent) = E_Constant)
8892 and then Present (Renamed_Object (Ent))
8893 then
8894 Exp := Renamed_Object (Ent);
8895 goto Continue;
8896 end if;
8898 -- Generate a reference only if the assignment comes from
8899 -- source. This excludes, for example, calls to a dispatching
8900 -- assignment operation when the left-hand side is tagged.
8902 if Modification_Comes_From_Source then
8903 Generate_Reference (Ent, Exp, 'm');
8904 end if;
8906 Check_Nested_Access (Ent);
8907 end if;
8909 Kill_Checks (Ent);
8911 -- If we are sure this is a modification from source, and we know
8912 -- this modifies a constant, then give an appropriate warning.
8914 if Overlays_Constant (Ent)
8915 and then Modification_Comes_From_Source
8916 and then Sure
8917 then
8918 declare
8919 A : constant Node_Id := Address_Clause (Ent);
8920 begin
8921 if Present (A) then
8922 declare
8923 Exp : constant Node_Id := Expression (A);
8924 begin
8925 if Nkind (Exp) = N_Attribute_Reference
8926 and then Attribute_Name (Exp) = Name_Address
8927 and then Is_Entity_Name (Prefix (Exp))
8928 then
8929 Error_Msg_Sloc := Sloc (A);
8930 Error_Msg_NE
8931 ("constant& may be modified via address clause#?",
8932 N, Entity (Prefix (Exp)));
8933 end if;
8934 end;
8935 end if;
8936 end;
8937 end if;
8939 return;
8940 end if;
8941 end loop;
8942 end Note_Possible_Modification;
8944 -------------------------
8945 -- Object_Access_Level --
8946 -------------------------
8948 function Object_Access_Level (Obj : Node_Id) return Uint is
8949 E : Entity_Id;
8951 -- Returns the static accessibility level of the view denoted by Obj. Note
8952 -- that the value returned is the result of a call to Scope_Depth. Only
8953 -- scope depths associated with dynamic scopes can actually be returned.
8954 -- Since only relative levels matter for accessibility checking, the fact
8955 -- that the distance between successive levels of accessibility is not
8956 -- always one is immaterial (invariant: if level(E2) is deeper than
8957 -- level(E1), then Scope_Depth(E1) < Scope_Depth(E2)).
8959 function Reference_To (Obj : Node_Id) return Node_Id;
8960 -- An explicit dereference is created when removing side-effects from
8961 -- expressions for constraint checking purposes. In this case a local
8962 -- access type is created for it. The correct access level is that of
8963 -- the original source node. We detect this case by noting that the
8964 -- prefix of the dereference is created by an object declaration whose
8965 -- initial expression is a reference.
8967 ------------------
8968 -- Reference_To --
8969 ------------------
8971 function Reference_To (Obj : Node_Id) return Node_Id is
8972 Pref : constant Node_Id := Prefix (Obj);
8973 begin
8974 if Is_Entity_Name (Pref)
8975 and then Nkind (Parent (Entity (Pref))) = N_Object_Declaration
8976 and then Present (Expression (Parent (Entity (Pref))))
8977 and then Nkind (Expression (Parent (Entity (Pref)))) = N_Reference
8978 then
8979 return (Prefix (Expression (Parent (Entity (Pref)))));
8980 else
8981 return Empty;
8982 end if;
8983 end Reference_To;
8985 -- Start of processing for Object_Access_Level
8987 begin
8988 if Is_Entity_Name (Obj) then
8989 E := Entity (Obj);
8991 if Is_Prival (E) then
8992 E := Prival_Link (E);
8993 end if;
8995 -- If E is a type then it denotes a current instance. For this case
8996 -- we add one to the normal accessibility level of the type to ensure
8997 -- that current instances are treated as always being deeper than
8998 -- than the level of any visible named access type (see 3.10.2(21)).
9000 if Is_Type (E) then
9001 return Type_Access_Level (E) + 1;
9003 elsif Present (Renamed_Object (E)) then
9004 return Object_Access_Level (Renamed_Object (E));
9006 -- Similarly, if E is a component of the current instance of a
9007 -- protected type, any instance of it is assumed to be at a deeper
9008 -- level than the type. For a protected object (whose type is an
9009 -- anonymous protected type) its components are at the same level
9010 -- as the type itself.
9012 elsif not Is_Overloadable (E)
9013 and then Ekind (Scope (E)) = E_Protected_Type
9014 and then Comes_From_Source (Scope (E))
9015 then
9016 return Type_Access_Level (Scope (E)) + 1;
9018 else
9019 return Scope_Depth (Enclosing_Dynamic_Scope (E));
9020 end if;
9022 elsif Nkind (Obj) = N_Selected_Component then
9023 if Is_Access_Type (Etype (Prefix (Obj))) then
9024 return Type_Access_Level (Etype (Prefix (Obj)));
9025 else
9026 return Object_Access_Level (Prefix (Obj));
9027 end if;
9029 elsif Nkind (Obj) = N_Indexed_Component then
9030 if Is_Access_Type (Etype (Prefix (Obj))) then
9031 return Type_Access_Level (Etype (Prefix (Obj)));
9032 else
9033 return Object_Access_Level (Prefix (Obj));
9034 end if;
9036 elsif Nkind (Obj) = N_Explicit_Dereference then
9038 -- If the prefix is a selected access discriminant then we make a
9039 -- recursive call on the prefix, which will in turn check the level
9040 -- of the prefix object of the selected discriminant.
9042 if Nkind (Prefix (Obj)) = N_Selected_Component
9043 and then Ekind (Etype (Prefix (Obj))) = E_Anonymous_Access_Type
9044 and then
9045 Ekind (Entity (Selector_Name (Prefix (Obj)))) = E_Discriminant
9046 then
9047 return Object_Access_Level (Prefix (Obj));
9049 elsif not (Comes_From_Source (Obj)) then
9050 declare
9051 Ref : constant Node_Id := Reference_To (Obj);
9052 begin
9053 if Present (Ref) then
9054 return Object_Access_Level (Ref);
9055 else
9056 return Type_Access_Level (Etype (Prefix (Obj)));
9057 end if;
9058 end;
9060 else
9061 return Type_Access_Level (Etype (Prefix (Obj)));
9062 end if;
9064 elsif Nkind (Obj) = N_Type_Conversion
9065 or else Nkind (Obj) = N_Unchecked_Type_Conversion
9066 then
9067 return Object_Access_Level (Expression (Obj));
9069 -- Function results are objects, so we get either the access level of
9070 -- the function or, in the case of an indirect call, the level of the
9071 -- access-to-subprogram type.
9073 elsif Nkind (Obj) = N_Function_Call then
9074 if Is_Entity_Name (Name (Obj)) then
9075 return Subprogram_Access_Level (Entity (Name (Obj)));
9076 else
9077 return Type_Access_Level (Etype (Prefix (Name (Obj))));
9078 end if;
9080 -- For convenience we handle qualified expressions, even though
9081 -- they aren't technically object names.
9083 elsif Nkind (Obj) = N_Qualified_Expression then
9084 return Object_Access_Level (Expression (Obj));
9086 -- Otherwise return the scope level of Standard.
9087 -- (If there are cases that fall through
9088 -- to this point they will be treated as
9089 -- having global accessibility for now. ???)
9091 else
9092 return Scope_Depth (Standard_Standard);
9093 end if;
9094 end Object_Access_Level;
9096 -----------------------
9097 -- Private_Component --
9098 -----------------------
9100 function Private_Component (Type_Id : Entity_Id) return Entity_Id is
9101 Ancestor : constant Entity_Id := Base_Type (Type_Id);
9103 function Trace_Components
9104 (T : Entity_Id;
9105 Check : Boolean) return Entity_Id;
9106 -- Recursive function that does the work, and checks against circular
9107 -- definition for each subcomponent type.
9109 ----------------------
9110 -- Trace_Components --
9111 ----------------------
9113 function Trace_Components
9114 (T : Entity_Id;
9115 Check : Boolean) return Entity_Id
9117 Btype : constant Entity_Id := Base_Type (T);
9118 Component : Entity_Id;
9119 P : Entity_Id;
9120 Candidate : Entity_Id := Empty;
9122 begin
9123 if Check and then Btype = Ancestor then
9124 Error_Msg_N ("circular type definition", Type_Id);
9125 return Any_Type;
9126 end if;
9128 if Is_Private_Type (Btype)
9129 and then not Is_Generic_Type (Btype)
9130 then
9131 if Present (Full_View (Btype))
9132 and then Is_Record_Type (Full_View (Btype))
9133 and then not Is_Frozen (Btype)
9134 then
9135 -- To indicate that the ancestor depends on a private type, the
9136 -- current Btype is sufficient. However, to check for circular
9137 -- definition we must recurse on the full view.
9139 Candidate := Trace_Components (Full_View (Btype), True);
9141 if Candidate = Any_Type then
9142 return Any_Type;
9143 else
9144 return Btype;
9145 end if;
9147 else
9148 return Btype;
9149 end if;
9151 elsif Is_Array_Type (Btype) then
9152 return Trace_Components (Component_Type (Btype), True);
9154 elsif Is_Record_Type (Btype) then
9155 Component := First_Entity (Btype);
9156 while Present (Component) loop
9158 -- Skip anonymous types generated by constrained components
9160 if not Is_Type (Component) then
9161 P := Trace_Components (Etype (Component), True);
9163 if Present (P) then
9164 if P = Any_Type then
9165 return P;
9166 else
9167 Candidate := P;
9168 end if;
9169 end if;
9170 end if;
9172 Next_Entity (Component);
9173 end loop;
9175 return Candidate;
9177 else
9178 return Empty;
9179 end if;
9180 end Trace_Components;
9182 -- Start of processing for Private_Component
9184 begin
9185 return Trace_Components (Type_Id, False);
9186 end Private_Component;
9188 ---------------------------
9189 -- Primitive_Names_Match --
9190 ---------------------------
9192 function Primitive_Names_Match (E1, E2 : Entity_Id) return Boolean is
9194 function Non_Internal_Name (E : Entity_Id) return Name_Id;
9195 -- Given an internal name, returns the corresponding non-internal name
9197 ------------------------
9198 -- Non_Internal_Name --
9199 ------------------------
9201 function Non_Internal_Name (E : Entity_Id) return Name_Id is
9202 begin
9203 Get_Name_String (Chars (E));
9204 Name_Len := Name_Len - 1;
9205 return Name_Find;
9206 end Non_Internal_Name;
9208 -- Start of processing for Primitive_Names_Match
9210 begin
9211 pragma Assert (Present (E1) and then Present (E2));
9213 return Chars (E1) = Chars (E2)
9214 or else
9215 (not Is_Internal_Name (Chars (E1))
9216 and then Is_Internal_Name (Chars (E2))
9217 and then Non_Internal_Name (E2) = Chars (E1))
9218 or else
9219 (not Is_Internal_Name (Chars (E2))
9220 and then Is_Internal_Name (Chars (E1))
9221 and then Non_Internal_Name (E1) = Chars (E2))
9222 or else
9223 (Is_Predefined_Dispatching_Operation (E1)
9224 and then Is_Predefined_Dispatching_Operation (E2)
9225 and then Same_TSS (E1, E2))
9226 or else
9227 (Is_Init_Proc (E1) and then Is_Init_Proc (E2));
9228 end Primitive_Names_Match;
9230 -----------------------
9231 -- Process_End_Label --
9232 -----------------------
9234 procedure Process_End_Label
9235 (N : Node_Id;
9236 Typ : Character;
9237 Ent : Entity_Id)
9239 Loc : Source_Ptr;
9240 Nam : Node_Id;
9241 Scop : Entity_Id;
9243 Label_Ref : Boolean;
9244 -- Set True if reference to end label itself is required
9246 Endl : Node_Id;
9247 -- Gets set to the operator symbol or identifier that references the
9248 -- entity Ent. For the child unit case, this is the identifier from the
9249 -- designator. For other cases, this is simply Endl.
9251 procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id);
9252 -- N is an identifier node that appears as a parent unit reference in
9253 -- the case where Ent is a child unit. This procedure generates an
9254 -- appropriate cross-reference entry. E is the corresponding entity.
9256 -------------------------
9257 -- Generate_Parent_Ref --
9258 -------------------------
9260 procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id) is
9261 begin
9262 -- If names do not match, something weird, skip reference
9264 if Chars (E) = Chars (N) then
9266 -- Generate the reference. We do NOT consider this as a reference
9267 -- for unreferenced symbol purposes.
9269 Generate_Reference (E, N, 'r', Set_Ref => False, Force => True);
9271 if Style_Check then
9272 Style.Check_Identifier (N, E);
9273 end if;
9274 end if;
9275 end Generate_Parent_Ref;
9277 -- Start of processing for Process_End_Label
9279 begin
9280 -- If no node, ignore. This happens in some error situations, and
9281 -- also for some internally generated structures where no end label
9282 -- references are required in any case.
9284 if No (N) then
9285 return;
9286 end if;
9288 -- Nothing to do if no End_Label, happens for internally generated
9289 -- constructs where we don't want an end label reference anyway. Also
9290 -- nothing to do if Endl is a string literal, which means there was
9291 -- some prior error (bad operator symbol)
9293 Endl := End_Label (N);
9295 if No (Endl) or else Nkind (Endl) = N_String_Literal then
9296 return;
9297 end if;
9299 -- Reference node is not in extended main source unit
9301 if not In_Extended_Main_Source_Unit (N) then
9303 -- Generally we do not collect references except for the extended
9304 -- main source unit. The one exception is the 'e' entry for a
9305 -- package spec, where it is useful for a client to have the
9306 -- ending information to define scopes.
9308 if Typ /= 'e' then
9309 return;
9311 else
9312 Label_Ref := False;
9314 -- For this case, we can ignore any parent references, but we
9315 -- need the package name itself for the 'e' entry.
9317 if Nkind (Endl) = N_Designator then
9318 Endl := Identifier (Endl);
9319 end if;
9320 end if;
9322 -- Reference is in extended main source unit
9324 else
9325 Label_Ref := True;
9327 -- For designator, generate references for the parent entries
9329 if Nkind (Endl) = N_Designator then
9331 -- Generate references for the prefix if the END line comes from
9332 -- source (otherwise we do not need these references) We climb the
9333 -- scope stack to find the expected entities.
9335 if Comes_From_Source (Endl) then
9336 Nam := Name (Endl);
9337 Scop := Current_Scope;
9338 while Nkind (Nam) = N_Selected_Component loop
9339 Scop := Scope (Scop);
9340 exit when No (Scop);
9341 Generate_Parent_Ref (Selector_Name (Nam), Scop);
9342 Nam := Prefix (Nam);
9343 end loop;
9345 if Present (Scop) then
9346 Generate_Parent_Ref (Nam, Scope (Scop));
9347 end if;
9348 end if;
9350 Endl := Identifier (Endl);
9351 end if;
9352 end if;
9354 -- If the end label is not for the given entity, then either we have
9355 -- some previous error, or this is a generic instantiation for which
9356 -- we do not need to make a cross-reference in this case anyway. In
9357 -- either case we simply ignore the call.
9359 if Chars (Ent) /= Chars (Endl) then
9360 return;
9361 end if;
9363 -- If label was really there, then generate a normal reference and then
9364 -- adjust the location in the end label to point past the name (which
9365 -- should almost always be the semicolon).
9367 Loc := Sloc (Endl);
9369 if Comes_From_Source (Endl) then
9371 -- If a label reference is required, then do the style check and
9372 -- generate an l-type cross-reference entry for the label
9374 if Label_Ref then
9375 if Style_Check then
9376 Style.Check_Identifier (Endl, Ent);
9377 end if;
9379 Generate_Reference (Ent, Endl, 'l', Set_Ref => False);
9380 end if;
9382 -- Set the location to point past the label (normally this will
9383 -- mean the semicolon immediately following the label). This is
9384 -- done for the sake of the 'e' or 't' entry generated below.
9386 Get_Decoded_Name_String (Chars (Endl));
9387 Set_Sloc (Endl, Sloc (Endl) + Source_Ptr (Name_Len));
9388 end if;
9390 -- Now generate the e/t reference
9392 Generate_Reference (Ent, Endl, Typ, Set_Ref => False, Force => True);
9394 -- Restore Sloc, in case modified above, since we have an identifier
9395 -- and the normal Sloc should be left set in the tree.
9397 Set_Sloc (Endl, Loc);
9398 end Process_End_Label;
9400 ------------------
9401 -- Real_Convert --
9402 ------------------
9404 -- We do the conversion to get the value of the real string by using
9405 -- the scanner, see Sinput for details on use of the internal source
9406 -- buffer for scanning internal strings.
9408 function Real_Convert (S : String) return Node_Id is
9409 Save_Src : constant Source_Buffer_Ptr := Source;
9410 Negative : Boolean;
9412 begin
9413 Source := Internal_Source_Ptr;
9414 Scan_Ptr := 1;
9416 for J in S'Range loop
9417 Source (Source_Ptr (J)) := S (J);
9418 end loop;
9420 Source (S'Length + 1) := EOF;
9422 if Source (Scan_Ptr) = '-' then
9423 Negative := True;
9424 Scan_Ptr := Scan_Ptr + 1;
9425 else
9426 Negative := False;
9427 end if;
9429 Scan;
9431 if Negative then
9432 Set_Realval (Token_Node, UR_Negate (Realval (Token_Node)));
9433 end if;
9435 Source := Save_Src;
9436 return Token_Node;
9437 end Real_Convert;
9439 --------------------
9440 -- Remove_Homonym --
9441 --------------------
9443 procedure Remove_Homonym (E : Entity_Id) is
9444 Prev : Entity_Id := Empty;
9445 H : Entity_Id;
9447 begin
9448 if E = Current_Entity (E) then
9449 if Present (Homonym (E)) then
9450 Set_Current_Entity (Homonym (E));
9451 else
9452 Set_Name_Entity_Id (Chars (E), Empty);
9453 end if;
9454 else
9455 H := Current_Entity (E);
9456 while Present (H) and then H /= E loop
9457 Prev := H;
9458 H := Homonym (H);
9459 end loop;
9461 Set_Homonym (Prev, Homonym (E));
9462 end if;
9463 end Remove_Homonym;
9465 ---------------------
9466 -- Rep_To_Pos_Flag --
9467 ---------------------
9469 function Rep_To_Pos_Flag (E : Entity_Id; Loc : Source_Ptr) return Node_Id is
9470 begin
9471 return New_Occurrence_Of
9472 (Boolean_Literals (not Range_Checks_Suppressed (E)), Loc);
9473 end Rep_To_Pos_Flag;
9475 --------------------
9476 -- Require_Entity --
9477 --------------------
9479 procedure Require_Entity (N : Node_Id) is
9480 begin
9481 if Is_Entity_Name (N) and then No (Entity (N)) then
9482 if Total_Errors_Detected /= 0 then
9483 Set_Entity (N, Any_Id);
9484 else
9485 raise Program_Error;
9486 end if;
9487 end if;
9488 end Require_Entity;
9490 ------------------------------
9491 -- Requires_Transient_Scope --
9492 ------------------------------
9494 -- A transient scope is required when variable-sized temporaries are
9495 -- allocated in the primary or secondary stack, or when finalization
9496 -- actions must be generated before the next instruction.
9498 function Requires_Transient_Scope (Id : Entity_Id) return Boolean is
9499 Typ : constant Entity_Id := Underlying_Type (Id);
9501 -- Start of processing for Requires_Transient_Scope
9503 begin
9504 -- This is a private type which is not completed yet. This can only
9505 -- happen in a default expression (of a formal parameter or of a
9506 -- record component). Do not expand transient scope in this case
9508 if No (Typ) then
9509 return False;
9511 -- Do not expand transient scope for non-existent procedure return
9513 elsif Typ = Standard_Void_Type then
9514 return False;
9516 -- Elementary types do not require a transient scope
9518 elsif Is_Elementary_Type (Typ) then
9519 return False;
9521 -- Generally, indefinite subtypes require a transient scope, since the
9522 -- back end cannot generate temporaries, since this is not a valid type
9523 -- for declaring an object. It might be possible to relax this in the
9524 -- future, e.g. by declaring the maximum possible space for the type.
9526 elsif Is_Indefinite_Subtype (Typ) then
9527 return True;
9529 -- Functions returning tagged types may dispatch on result so their
9530 -- returned value is allocated on the secondary stack. Controlled
9531 -- type temporaries need finalization.
9533 elsif Is_Tagged_Type (Typ)
9534 or else Has_Controlled_Component (Typ)
9535 then
9536 return not Is_Value_Type (Typ);
9538 -- Record type
9540 elsif Is_Record_Type (Typ) then
9541 declare
9542 Comp : Entity_Id;
9543 begin
9544 Comp := First_Entity (Typ);
9545 while Present (Comp) loop
9546 if Ekind (Comp) = E_Component
9547 and then Requires_Transient_Scope (Etype (Comp))
9548 then
9549 return True;
9550 else
9551 Next_Entity (Comp);
9552 end if;
9553 end loop;
9554 end;
9556 return False;
9558 -- String literal types never require transient scope
9560 elsif Ekind (Typ) = E_String_Literal_Subtype then
9561 return False;
9563 -- Array type. Note that we already know that this is a constrained
9564 -- array, since unconstrained arrays will fail the indefinite test.
9566 elsif Is_Array_Type (Typ) then
9568 -- If component type requires a transient scope, the array does too
9570 if Requires_Transient_Scope (Component_Type (Typ)) then
9571 return True;
9573 -- Otherwise, we only need a transient scope if the size is not
9574 -- known at compile time.
9576 else
9577 return not Size_Known_At_Compile_Time (Typ);
9578 end if;
9580 -- All other cases do not require a transient scope
9582 else
9583 return False;
9584 end if;
9585 end Requires_Transient_Scope;
9587 --------------------------
9588 -- Reset_Analyzed_Flags --
9589 --------------------------
9591 procedure Reset_Analyzed_Flags (N : Node_Id) is
9593 function Clear_Analyzed (N : Node_Id) return Traverse_Result;
9594 -- Function used to reset Analyzed flags in tree. Note that we do
9595 -- not reset Analyzed flags in entities, since there is no need to
9596 -- reanalyze entities, and indeed, it is wrong to do so, since it
9597 -- can result in generating auxiliary stuff more than once.
9599 --------------------
9600 -- Clear_Analyzed --
9601 --------------------
9603 function Clear_Analyzed (N : Node_Id) return Traverse_Result is
9604 begin
9605 if not Has_Extension (N) then
9606 Set_Analyzed (N, False);
9607 end if;
9609 return OK;
9610 end Clear_Analyzed;
9612 procedure Reset_Analyzed is new Traverse_Proc (Clear_Analyzed);
9614 -- Start of processing for Reset_Analyzed_Flags
9616 begin
9617 Reset_Analyzed (N);
9618 end Reset_Analyzed_Flags;
9620 ---------------------------
9621 -- Safe_To_Capture_Value --
9622 ---------------------------
9624 function Safe_To_Capture_Value
9625 (N : Node_Id;
9626 Ent : Entity_Id;
9627 Cond : Boolean := False) return Boolean
9629 begin
9630 -- The only entities for which we track constant values are variables
9631 -- which are not renamings, constants, out parameters, and in out
9632 -- parameters, so check if we have this case.
9634 -- Note: it may seem odd to track constant values for constants, but in
9635 -- fact this routine is used for other purposes than simply capturing
9636 -- the value. In particular, the setting of Known[_Non]_Null.
9638 if (Ekind (Ent) = E_Variable and then No (Renamed_Object (Ent)))
9639 or else
9640 Ekind (Ent) = E_Constant
9641 or else
9642 Ekind (Ent) = E_Out_Parameter
9643 or else
9644 Ekind (Ent) = E_In_Out_Parameter
9645 then
9646 null;
9648 -- For conditionals, we also allow loop parameters and all formals,
9649 -- including in parameters.
9651 elsif Cond
9652 and then
9653 (Ekind (Ent) = E_Loop_Parameter
9654 or else
9655 Ekind (Ent) = E_In_Parameter)
9656 then
9657 null;
9659 -- For all other cases, not just unsafe, but impossible to capture
9660 -- Current_Value, since the above are the only entities which have
9661 -- Current_Value fields.
9663 else
9664 return False;
9665 end if;
9667 -- Skip if volatile or aliased, since funny things might be going on in
9668 -- these cases which we cannot necessarily track. Also skip any variable
9669 -- for which an address clause is given, or whose address is taken. Also
9670 -- never capture value of library level variables (an attempt to do so
9671 -- can occur in the case of package elaboration code).
9673 if Treat_As_Volatile (Ent)
9674 or else Is_Aliased (Ent)
9675 or else Present (Address_Clause (Ent))
9676 or else Address_Taken (Ent)
9677 or else (Is_Library_Level_Entity (Ent)
9678 and then Ekind (Ent) = E_Variable)
9679 then
9680 return False;
9681 end if;
9683 -- OK, all above conditions are met. We also require that the scope of
9684 -- the reference be the same as the scope of the entity, not counting
9685 -- packages and blocks and loops.
9687 declare
9688 E_Scope : constant Entity_Id := Scope (Ent);
9689 R_Scope : Entity_Id;
9691 begin
9692 R_Scope := Current_Scope;
9693 while R_Scope /= Standard_Standard loop
9694 exit when R_Scope = E_Scope;
9696 if Ekind (R_Scope) /= E_Package
9697 and then
9698 Ekind (R_Scope) /= E_Block
9699 and then
9700 Ekind (R_Scope) /= E_Loop
9701 then
9702 return False;
9703 else
9704 R_Scope := Scope (R_Scope);
9705 end if;
9706 end loop;
9707 end;
9709 -- We also require that the reference does not appear in a context
9710 -- where it is not sure to be executed (i.e. a conditional context
9711 -- or an exception handler). We skip this if Cond is True, since the
9712 -- capturing of values from conditional tests handles this ok.
9714 if Cond then
9715 return True;
9716 end if;
9718 declare
9719 Desc : Node_Id;
9720 P : Node_Id;
9722 begin
9723 Desc := N;
9725 P := Parent (N);
9726 while Present (P) loop
9727 if Nkind (P) = N_If_Statement
9728 or else Nkind (P) = N_Case_Statement
9729 or else (Nkind (P) = N_And_Then and then Desc = Right_Opnd (P))
9730 or else (Nkind (P) = N_Or_Else and then Desc = Right_Opnd (P))
9731 or else Nkind (P) = N_Exception_Handler
9732 or else Nkind (P) = N_Selective_Accept
9733 or else Nkind (P) = N_Conditional_Entry_Call
9734 or else Nkind (P) = N_Timed_Entry_Call
9735 or else Nkind (P) = N_Asynchronous_Select
9736 then
9737 return False;
9738 else
9739 Desc := P;
9740 P := Parent (P);
9741 end if;
9742 end loop;
9743 end;
9745 -- OK, looks safe to set value
9747 return True;
9748 end Safe_To_Capture_Value;
9750 ---------------
9751 -- Same_Name --
9752 ---------------
9754 function Same_Name (N1, N2 : Node_Id) return Boolean is
9755 K1 : constant Node_Kind := Nkind (N1);
9756 K2 : constant Node_Kind := Nkind (N2);
9758 begin
9759 if (K1 = N_Identifier or else K1 = N_Defining_Identifier)
9760 and then (K2 = N_Identifier or else K2 = N_Defining_Identifier)
9761 then
9762 return Chars (N1) = Chars (N2);
9764 elsif (K1 = N_Selected_Component or else K1 = N_Expanded_Name)
9765 and then (K2 = N_Selected_Component or else K2 = N_Expanded_Name)
9766 then
9767 return Same_Name (Selector_Name (N1), Selector_Name (N2))
9768 and then Same_Name (Prefix (N1), Prefix (N2));
9770 else
9771 return False;
9772 end if;
9773 end Same_Name;
9775 -----------------
9776 -- Same_Object --
9777 -----------------
9779 function Same_Object (Node1, Node2 : Node_Id) return Boolean is
9780 N1 : constant Node_Id := Original_Node (Node1);
9781 N2 : constant Node_Id := Original_Node (Node2);
9782 -- We do the tests on original nodes, since we are most interested
9783 -- in the original source, not any expansion that got in the way.
9785 K1 : constant Node_Kind := Nkind (N1);
9786 K2 : constant Node_Kind := Nkind (N2);
9788 begin
9789 -- First case, both are entities with same entity
9791 if K1 in N_Has_Entity
9792 and then K2 in N_Has_Entity
9793 and then Present (Entity (N1))
9794 and then Present (Entity (N2))
9795 and then (Ekind (Entity (N1)) = E_Variable
9796 or else
9797 Ekind (Entity (N1)) = E_Constant)
9798 and then Entity (N1) = Entity (N2)
9799 then
9800 return True;
9802 -- Second case, selected component with same selector, same record
9804 elsif K1 = N_Selected_Component
9805 and then K2 = N_Selected_Component
9806 and then Chars (Selector_Name (N1)) = Chars (Selector_Name (N2))
9807 then
9808 return Same_Object (Prefix (N1), Prefix (N2));
9810 -- Third case, indexed component with same subscripts, same array
9812 elsif K1 = N_Indexed_Component
9813 and then K2 = N_Indexed_Component
9814 and then Same_Object (Prefix (N1), Prefix (N2))
9815 then
9816 declare
9817 E1, E2 : Node_Id;
9818 begin
9819 E1 := First (Expressions (N1));
9820 E2 := First (Expressions (N2));
9821 while Present (E1) loop
9822 if not Same_Value (E1, E2) then
9823 return False;
9824 else
9825 Next (E1);
9826 Next (E2);
9827 end if;
9828 end loop;
9830 return True;
9831 end;
9833 -- Fourth case, slice of same array with same bounds
9835 elsif K1 = N_Slice
9836 and then K2 = N_Slice
9837 and then Nkind (Discrete_Range (N1)) = N_Range
9838 and then Nkind (Discrete_Range (N2)) = N_Range
9839 and then Same_Value (Low_Bound (Discrete_Range (N1)),
9840 Low_Bound (Discrete_Range (N2)))
9841 and then Same_Value (High_Bound (Discrete_Range (N1)),
9842 High_Bound (Discrete_Range (N2)))
9843 then
9844 return Same_Name (Prefix (N1), Prefix (N2));
9846 -- All other cases, not clearly the same object
9848 else
9849 return False;
9850 end if;
9851 end Same_Object;
9853 ---------------
9854 -- Same_Type --
9855 ---------------
9857 function Same_Type (T1, T2 : Entity_Id) return Boolean is
9858 begin
9859 if T1 = T2 then
9860 return True;
9862 elsif not Is_Constrained (T1)
9863 and then not Is_Constrained (T2)
9864 and then Base_Type (T1) = Base_Type (T2)
9865 then
9866 return True;
9868 -- For now don't bother with case of identical constraints, to be
9869 -- fiddled with later on perhaps (this is only used for optimization
9870 -- purposes, so it is not critical to do a best possible job)
9872 else
9873 return False;
9874 end if;
9875 end Same_Type;
9877 ----------------
9878 -- Same_Value --
9879 ----------------
9881 function Same_Value (Node1, Node2 : Node_Id) return Boolean is
9882 begin
9883 if Compile_Time_Known_Value (Node1)
9884 and then Compile_Time_Known_Value (Node2)
9885 and then Expr_Value (Node1) = Expr_Value (Node2)
9886 then
9887 return True;
9888 elsif Same_Object (Node1, Node2) then
9889 return True;
9890 else
9891 return False;
9892 end if;
9893 end Same_Value;
9895 ------------------------
9896 -- Scope_Is_Transient --
9897 ------------------------
9899 function Scope_Is_Transient return Boolean is
9900 begin
9901 return Scope_Stack.Table (Scope_Stack.Last).Is_Transient;
9902 end Scope_Is_Transient;
9904 ------------------
9905 -- Scope_Within --
9906 ------------------
9908 function Scope_Within (Scope1, Scope2 : Entity_Id) return Boolean is
9909 Scop : Entity_Id;
9911 begin
9912 Scop := Scope1;
9913 while Scop /= Standard_Standard loop
9914 Scop := Scope (Scop);
9916 if Scop = Scope2 then
9917 return True;
9918 end if;
9919 end loop;
9921 return False;
9922 end Scope_Within;
9924 --------------------------
9925 -- Scope_Within_Or_Same --
9926 --------------------------
9928 function Scope_Within_Or_Same (Scope1, Scope2 : Entity_Id) return Boolean is
9929 Scop : Entity_Id;
9931 begin
9932 Scop := Scope1;
9933 while Scop /= Standard_Standard loop
9934 if Scop = Scope2 then
9935 return True;
9936 else
9937 Scop := Scope (Scop);
9938 end if;
9939 end loop;
9941 return False;
9942 end Scope_Within_Or_Same;
9944 --------------------
9945 -- Set_Convention --
9946 --------------------
9948 procedure Set_Convention (E : Entity_Id; Val : Snames.Convention_Id) is
9949 begin
9950 Basic_Set_Convention (E, Val);
9952 if Is_Type (E)
9953 and then Is_Access_Subprogram_Type (Base_Type (E))
9954 and then Has_Foreign_Convention (E)
9955 then
9956 Set_Can_Use_Internal_Rep (E, False);
9957 end if;
9958 end Set_Convention;
9960 ------------------------
9961 -- Set_Current_Entity --
9962 ------------------------
9964 -- The given entity is to be set as the currently visible definition
9965 -- of its associated name (i.e. the Node_Id associated with its name).
9966 -- All we have to do is to get the name from the identifier, and
9967 -- then set the associated Node_Id to point to the given entity.
9969 procedure Set_Current_Entity (E : Entity_Id) is
9970 begin
9971 Set_Name_Entity_Id (Chars (E), E);
9972 end Set_Current_Entity;
9974 ---------------------------
9975 -- Set_Debug_Info_Needed --
9976 ---------------------------
9978 procedure Set_Debug_Info_Needed (T : Entity_Id) is
9980 procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id);
9981 pragma Inline (Set_Debug_Info_Needed_If_Not_Set);
9982 -- Used to set debug info in a related node if not set already
9984 --------------------------------------
9985 -- Set_Debug_Info_Needed_If_Not_Set --
9986 --------------------------------------
9988 procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id) is
9989 begin
9990 if Present (E)
9991 and then not Needs_Debug_Info (E)
9992 then
9993 Set_Debug_Info_Needed (E);
9995 -- For a private type, indicate that the full view also needs
9996 -- debug information.
9998 if Is_Type (E)
9999 and then Is_Private_Type (E)
10000 and then Present (Full_View (E))
10001 then
10002 Set_Debug_Info_Needed (Full_View (E));
10003 end if;
10004 end if;
10005 end Set_Debug_Info_Needed_If_Not_Set;
10007 -- Start of processing for Set_Debug_Info_Needed
10009 begin
10010 -- Nothing to do if argument is Empty or has Debug_Info_Off set, which
10011 -- indicates that Debug_Info_Needed is never required for the entity.
10013 if No (T)
10014 or else Debug_Info_Off (T)
10015 then
10016 return;
10017 end if;
10019 -- Set flag in entity itself. Note that we will go through the following
10020 -- circuitry even if the flag is already set on T. That's intentional,
10021 -- it makes sure that the flag will be set in subsidiary entities.
10023 Set_Needs_Debug_Info (T);
10025 -- Set flag on subsidiary entities if not set already
10027 if Is_Object (T) then
10028 Set_Debug_Info_Needed_If_Not_Set (Etype (T));
10030 elsif Is_Type (T) then
10031 Set_Debug_Info_Needed_If_Not_Set (Etype (T));
10033 if Is_Record_Type (T) then
10034 declare
10035 Ent : Entity_Id := First_Entity (T);
10036 begin
10037 while Present (Ent) loop
10038 Set_Debug_Info_Needed_If_Not_Set (Ent);
10039 Next_Entity (Ent);
10040 end loop;
10041 end;
10043 elsif Is_Array_Type (T) then
10044 Set_Debug_Info_Needed_If_Not_Set (Component_Type (T));
10046 declare
10047 Indx : Node_Id := First_Index (T);
10048 begin
10049 while Present (Indx) loop
10050 Set_Debug_Info_Needed_If_Not_Set (Etype (Indx));
10051 Indx := Next_Index (Indx);
10052 end loop;
10053 end;
10055 if Is_Packed (T) then
10056 Set_Debug_Info_Needed_If_Not_Set (Packed_Array_Type (T));
10057 end if;
10059 elsif Is_Access_Type (T) then
10060 Set_Debug_Info_Needed_If_Not_Set (Directly_Designated_Type (T));
10062 elsif Is_Private_Type (T) then
10063 Set_Debug_Info_Needed_If_Not_Set (Full_View (T));
10065 elsif Is_Protected_Type (T) then
10066 Set_Debug_Info_Needed_If_Not_Set (Corresponding_Record_Type (T));
10067 end if;
10068 end if;
10069 end Set_Debug_Info_Needed;
10071 ---------------------------------
10072 -- Set_Entity_With_Style_Check --
10073 ---------------------------------
10075 procedure Set_Entity_With_Style_Check (N : Node_Id; Val : Entity_Id) is
10076 Val_Actual : Entity_Id;
10077 Nod : Node_Id;
10079 begin
10080 Set_Entity (N, Val);
10082 if Style_Check
10083 and then not Suppress_Style_Checks (Val)
10084 and then not In_Instance
10085 then
10086 if Nkind (N) = N_Identifier then
10087 Nod := N;
10088 elsif Nkind (N) = N_Expanded_Name then
10089 Nod := Selector_Name (N);
10090 else
10091 return;
10092 end if;
10094 -- A special situation arises for derived operations, where we want
10095 -- to do the check against the parent (since the Sloc of the derived
10096 -- operation points to the derived type declaration itself).
10098 Val_Actual := Val;
10099 while not Comes_From_Source (Val_Actual)
10100 and then Nkind (Val_Actual) in N_Entity
10101 and then (Ekind (Val_Actual) = E_Enumeration_Literal
10102 or else Is_Subprogram (Val_Actual)
10103 or else Is_Generic_Subprogram (Val_Actual))
10104 and then Present (Alias (Val_Actual))
10105 loop
10106 Val_Actual := Alias (Val_Actual);
10107 end loop;
10109 -- Renaming declarations for generic actuals do not come from source,
10110 -- and have a different name from that of the entity they rename, so
10111 -- there is no style check to perform here.
10113 if Chars (Nod) = Chars (Val_Actual) then
10114 Style.Check_Identifier (Nod, Val_Actual);
10115 end if;
10116 end if;
10118 Set_Entity (N, Val);
10119 end Set_Entity_With_Style_Check;
10121 ------------------------
10122 -- Set_Name_Entity_Id --
10123 ------------------------
10125 procedure Set_Name_Entity_Id (Id : Name_Id; Val : Entity_Id) is
10126 begin
10127 Set_Name_Table_Info (Id, Int (Val));
10128 end Set_Name_Entity_Id;
10130 ---------------------
10131 -- Set_Next_Actual --
10132 ---------------------
10134 procedure Set_Next_Actual (Ass1_Id : Node_Id; Ass2_Id : Node_Id) is
10135 begin
10136 if Nkind (Parent (Ass1_Id)) = N_Parameter_Association then
10137 Set_First_Named_Actual (Parent (Ass1_Id), Ass2_Id);
10138 end if;
10139 end Set_Next_Actual;
10141 ----------------------------------
10142 -- Set_Optimize_Alignment_Flags --
10143 ----------------------------------
10145 procedure Set_Optimize_Alignment_Flags (E : Entity_Id) is
10146 begin
10147 if Optimize_Alignment = 'S' then
10148 Set_Optimize_Alignment_Space (E);
10149 elsif Optimize_Alignment = 'T' then
10150 Set_Optimize_Alignment_Time (E);
10151 end if;
10152 end Set_Optimize_Alignment_Flags;
10154 -----------------------
10155 -- Set_Public_Status --
10156 -----------------------
10158 procedure Set_Public_Status (Id : Entity_Id) is
10159 S : constant Entity_Id := Current_Scope;
10161 function Within_HSS_Or_If (E : Entity_Id) return Boolean;
10162 -- Determines if E is defined within handled statement sequence or
10163 -- an if statement, returns True if so, False otherwise.
10165 ----------------------
10166 -- Within_HSS_Or_If --
10167 ----------------------
10169 function Within_HSS_Or_If (E : Entity_Id) return Boolean is
10170 N : Node_Id;
10171 begin
10172 N := Declaration_Node (E);
10173 loop
10174 N := Parent (N);
10176 if No (N) then
10177 return False;
10179 elsif Nkind_In (N, N_Handled_Sequence_Of_Statements,
10180 N_If_Statement)
10181 then
10182 return True;
10183 end if;
10184 end loop;
10185 end Within_HSS_Or_If;
10187 -- Start of processing for Set_Public_Status
10189 begin
10190 -- Everything in the scope of Standard is public
10192 if S = Standard_Standard then
10193 Set_Is_Public (Id);
10195 -- Entity is definitely not public if enclosing scope is not public
10197 elsif not Is_Public (S) then
10198 return;
10200 -- An object or function declaration that occurs in a handled sequence
10201 -- of statements or within an if statement is the declaration for a
10202 -- temporary object or local subprogram generated by the expander. It
10203 -- never needs to be made public and furthermore, making it public can
10204 -- cause back end problems.
10206 elsif Nkind_In (Parent (Id), N_Object_Declaration,
10207 N_Function_Specification)
10208 and then Within_HSS_Or_If (Id)
10209 then
10210 return;
10212 -- Entities in public packages or records are public
10214 elsif Ekind (S) = E_Package or Is_Record_Type (S) then
10215 Set_Is_Public (Id);
10217 -- The bounds of an entry family declaration can generate object
10218 -- declarations that are visible to the back-end, e.g. in the
10219 -- the declaration of a composite type that contains tasks.
10221 elsif Is_Concurrent_Type (S)
10222 and then not Has_Completion (S)
10223 and then Nkind (Parent (Id)) = N_Object_Declaration
10224 then
10225 Set_Is_Public (Id);
10226 end if;
10227 end Set_Public_Status;
10229 -----------------------------
10230 -- Set_Referenced_Modified --
10231 -----------------------------
10233 procedure Set_Referenced_Modified (N : Node_Id; Out_Param : Boolean) is
10234 Pref : Node_Id;
10236 begin
10237 -- Deal with indexed or selected component where prefix is modified
10239 if Nkind (N) = N_Indexed_Component
10240 or else
10241 Nkind (N) = N_Selected_Component
10242 then
10243 Pref := Prefix (N);
10245 -- If prefix is access type, then it is the designated object that is
10246 -- being modified, which means we have no entity to set the flag on.
10248 if No (Etype (Pref)) or else Is_Access_Type (Etype (Pref)) then
10249 return;
10251 -- Otherwise chase the prefix
10253 else
10254 Set_Referenced_Modified (Pref, Out_Param);
10255 end if;
10257 -- Otherwise see if we have an entity name (only other case to process)
10259 elsif Is_Entity_Name (N) and then Present (Entity (N)) then
10260 Set_Referenced_As_LHS (Entity (N), not Out_Param);
10261 Set_Referenced_As_Out_Parameter (Entity (N), Out_Param);
10262 end if;
10263 end Set_Referenced_Modified;
10265 ----------------------------
10266 -- Set_Scope_Is_Transient --
10267 ----------------------------
10269 procedure Set_Scope_Is_Transient (V : Boolean := True) is
10270 begin
10271 Scope_Stack.Table (Scope_Stack.Last).Is_Transient := V;
10272 end Set_Scope_Is_Transient;
10274 -------------------
10275 -- Set_Size_Info --
10276 -------------------
10278 procedure Set_Size_Info (T1, T2 : Entity_Id) is
10279 begin
10280 -- We copy Esize, but not RM_Size, since in general RM_Size is
10281 -- subtype specific and does not get inherited by all subtypes.
10283 Set_Esize (T1, Esize (T2));
10284 Set_Has_Biased_Representation (T1, Has_Biased_Representation (T2));
10286 if Is_Discrete_Or_Fixed_Point_Type (T1)
10287 and then
10288 Is_Discrete_Or_Fixed_Point_Type (T2)
10289 then
10290 Set_Is_Unsigned_Type (T1, Is_Unsigned_Type (T2));
10291 end if;
10293 Set_Alignment (T1, Alignment (T2));
10294 end Set_Size_Info;
10296 --------------------
10297 -- Static_Integer --
10298 --------------------
10300 function Static_Integer (N : Node_Id) return Uint is
10301 begin
10302 Analyze_And_Resolve (N, Any_Integer);
10304 if N = Error
10305 or else Error_Posted (N)
10306 or else Etype (N) = Any_Type
10307 then
10308 return No_Uint;
10309 end if;
10311 if Is_Static_Expression (N) then
10312 if not Raises_Constraint_Error (N) then
10313 return Expr_Value (N);
10314 else
10315 return No_Uint;
10316 end if;
10318 elsif Etype (N) = Any_Type then
10319 return No_Uint;
10321 else
10322 Flag_Non_Static_Expr
10323 ("static integer expression required here", N);
10324 return No_Uint;
10325 end if;
10326 end Static_Integer;
10328 --------------------------
10329 -- Statically_Different --
10330 --------------------------
10332 function Statically_Different (E1, E2 : Node_Id) return Boolean is
10333 R1 : constant Node_Id := Get_Referenced_Object (E1);
10334 R2 : constant Node_Id := Get_Referenced_Object (E2);
10335 begin
10336 return Is_Entity_Name (R1)
10337 and then Is_Entity_Name (R2)
10338 and then Entity (R1) /= Entity (R2)
10339 and then not Is_Formal (Entity (R1))
10340 and then not Is_Formal (Entity (R2));
10341 end Statically_Different;
10343 -----------------------------
10344 -- Subprogram_Access_Level --
10345 -----------------------------
10347 function Subprogram_Access_Level (Subp : Entity_Id) return Uint is
10348 begin
10349 if Present (Alias (Subp)) then
10350 return Subprogram_Access_Level (Alias (Subp));
10351 else
10352 return Scope_Depth (Enclosing_Dynamic_Scope (Subp));
10353 end if;
10354 end Subprogram_Access_Level;
10356 -----------------
10357 -- Trace_Scope --
10358 -----------------
10360 procedure Trace_Scope (N : Node_Id; E : Entity_Id; Msg : String) is
10361 begin
10362 if Debug_Flag_W then
10363 for J in 0 .. Scope_Stack.Last loop
10364 Write_Str (" ");
10365 end loop;
10367 Write_Str (Msg);
10368 Write_Name (Chars (E));
10369 Write_Str (" from ");
10370 Write_Location (Sloc (N));
10371 Write_Eol;
10372 end if;
10373 end Trace_Scope;
10375 -----------------------
10376 -- Transfer_Entities --
10377 -----------------------
10379 procedure Transfer_Entities (From : Entity_Id; To : Entity_Id) is
10380 Ent : Entity_Id := First_Entity (From);
10382 begin
10383 if No (Ent) then
10384 return;
10385 end if;
10387 if (Last_Entity (To)) = Empty then
10388 Set_First_Entity (To, Ent);
10389 else
10390 Set_Next_Entity (Last_Entity (To), Ent);
10391 end if;
10393 Set_Last_Entity (To, Last_Entity (From));
10395 while Present (Ent) loop
10396 Set_Scope (Ent, To);
10398 if not Is_Public (Ent) then
10399 Set_Public_Status (Ent);
10401 if Is_Public (Ent)
10402 and then Ekind (Ent) = E_Record_Subtype
10404 then
10405 -- The components of the propagated Itype must be public
10406 -- as well.
10408 declare
10409 Comp : Entity_Id;
10410 begin
10411 Comp := First_Entity (Ent);
10412 while Present (Comp) loop
10413 Set_Is_Public (Comp);
10414 Next_Entity (Comp);
10415 end loop;
10416 end;
10417 end if;
10418 end if;
10420 Next_Entity (Ent);
10421 end loop;
10423 Set_First_Entity (From, Empty);
10424 Set_Last_Entity (From, Empty);
10425 end Transfer_Entities;
10427 -----------------------
10428 -- Type_Access_Level --
10429 -----------------------
10431 function Type_Access_Level (Typ : Entity_Id) return Uint is
10432 Btyp : Entity_Id;
10434 begin
10435 Btyp := Base_Type (Typ);
10437 -- Ada 2005 (AI-230): For most cases of anonymous access types, we
10438 -- simply use the level where the type is declared. This is true for
10439 -- stand-alone object declarations, and for anonymous access types
10440 -- associated with components the level is the same as that of the
10441 -- enclosing composite type. However, special treatment is needed for
10442 -- the cases of access parameters, return objects of an anonymous access
10443 -- type, and, in Ada 95, access discriminants of limited types.
10445 if Ekind (Btyp) in Access_Kind then
10446 if Ekind (Btyp) = E_Anonymous_Access_Type then
10448 -- If the type is a nonlocal anonymous access type (such as for
10449 -- an access parameter) we treat it as being declared at the
10450 -- library level to ensure that names such as X.all'access don't
10451 -- fail static accessibility checks.
10453 if not Is_Local_Anonymous_Access (Typ) then
10454 return Scope_Depth (Standard_Standard);
10456 -- If this is a return object, the accessibility level is that of
10457 -- the result subtype of the enclosing function. The test here is
10458 -- little complicated, because we have to account for extended
10459 -- return statements that have been rewritten as blocks, in which
10460 -- case we have to find and the Is_Return_Object attribute of the
10461 -- itype's associated object. It would be nice to find a way to
10462 -- simplify this test, but it doesn't seem worthwhile to add a new
10463 -- flag just for purposes of this test. ???
10465 elsif Ekind (Scope (Btyp)) = E_Return_Statement
10466 or else
10467 (Is_Itype (Btyp)
10468 and then Nkind (Associated_Node_For_Itype (Btyp)) =
10469 N_Object_Declaration
10470 and then Is_Return_Object
10471 (Defining_Identifier
10472 (Associated_Node_For_Itype (Btyp))))
10473 then
10474 declare
10475 Scop : Entity_Id;
10477 begin
10478 Scop := Scope (Scope (Btyp));
10479 while Present (Scop) loop
10480 exit when Ekind (Scop) = E_Function;
10481 Scop := Scope (Scop);
10482 end loop;
10484 -- Treat the return object's type as having the level of the
10485 -- function's result subtype (as per RM05-6.5(5.3/2)).
10487 return Type_Access_Level (Etype (Scop));
10488 end;
10489 end if;
10490 end if;
10492 Btyp := Root_Type (Btyp);
10494 -- The accessibility level of anonymous access types associated with
10495 -- discriminants is that of the current instance of the type, and
10496 -- that's deeper than the type itself (AARM 3.10.2 (12.3.21)).
10498 -- AI-402: access discriminants have accessibility based on the
10499 -- object rather than the type in Ada 2005, so the above paragraph
10500 -- doesn't apply.
10502 -- ??? Needs completion with rules from AI-416
10504 if Ada_Version <= Ada_95
10505 and then Ekind (Typ) = E_Anonymous_Access_Type
10506 and then Present (Associated_Node_For_Itype (Typ))
10507 and then Nkind (Associated_Node_For_Itype (Typ)) =
10508 N_Discriminant_Specification
10509 then
10510 return Scope_Depth (Enclosing_Dynamic_Scope (Btyp)) + 1;
10511 end if;
10512 end if;
10514 return Scope_Depth (Enclosing_Dynamic_Scope (Btyp));
10515 end Type_Access_Level;
10517 --------------------
10518 -- Ultimate_Alias --
10519 --------------------
10520 -- To do: add occurrences calling this new subprogram
10522 function Ultimate_Alias (Prim : Entity_Id) return Entity_Id is
10523 E : Entity_Id := Prim;
10525 begin
10526 while Present (Alias (E)) loop
10527 E := Alias (E);
10528 end loop;
10530 return E;
10531 end Ultimate_Alias;
10533 --------------------------
10534 -- Unit_Declaration_Node --
10535 --------------------------
10537 function Unit_Declaration_Node (Unit_Id : Entity_Id) return Node_Id is
10538 N : Node_Id := Parent (Unit_Id);
10540 begin
10541 -- Predefined operators do not have a full function declaration
10543 if Ekind (Unit_Id) = E_Operator then
10544 return N;
10545 end if;
10547 -- Isn't there some better way to express the following ???
10549 while Nkind (N) /= N_Abstract_Subprogram_Declaration
10550 and then Nkind (N) /= N_Formal_Package_Declaration
10551 and then Nkind (N) /= N_Function_Instantiation
10552 and then Nkind (N) /= N_Generic_Package_Declaration
10553 and then Nkind (N) /= N_Generic_Subprogram_Declaration
10554 and then Nkind (N) /= N_Package_Declaration
10555 and then Nkind (N) /= N_Package_Body
10556 and then Nkind (N) /= N_Package_Instantiation
10557 and then Nkind (N) /= N_Package_Renaming_Declaration
10558 and then Nkind (N) /= N_Procedure_Instantiation
10559 and then Nkind (N) /= N_Protected_Body
10560 and then Nkind (N) /= N_Subprogram_Declaration
10561 and then Nkind (N) /= N_Subprogram_Body
10562 and then Nkind (N) /= N_Subprogram_Body_Stub
10563 and then Nkind (N) /= N_Subprogram_Renaming_Declaration
10564 and then Nkind (N) /= N_Task_Body
10565 and then Nkind (N) /= N_Task_Type_Declaration
10566 and then Nkind (N) not in N_Formal_Subprogram_Declaration
10567 and then Nkind (N) not in N_Generic_Renaming_Declaration
10568 loop
10569 N := Parent (N);
10570 pragma Assert (Present (N));
10571 end loop;
10573 return N;
10574 end Unit_Declaration_Node;
10576 ------------------------------
10577 -- Universal_Interpretation --
10578 ------------------------------
10580 function Universal_Interpretation (Opnd : Node_Id) return Entity_Id is
10581 Index : Interp_Index;
10582 It : Interp;
10584 begin
10585 -- The argument may be a formal parameter of an operator or subprogram
10586 -- with multiple interpretations, or else an expression for an actual.
10588 if Nkind (Opnd) = N_Defining_Identifier
10589 or else not Is_Overloaded (Opnd)
10590 then
10591 if Etype (Opnd) = Universal_Integer
10592 or else Etype (Opnd) = Universal_Real
10593 then
10594 return Etype (Opnd);
10595 else
10596 return Empty;
10597 end if;
10599 else
10600 Get_First_Interp (Opnd, Index, It);
10601 while Present (It.Typ) loop
10602 if It.Typ = Universal_Integer
10603 or else It.Typ = Universal_Real
10604 then
10605 return It.Typ;
10606 end if;
10608 Get_Next_Interp (Index, It);
10609 end loop;
10611 return Empty;
10612 end if;
10613 end Universal_Interpretation;
10615 ---------------
10616 -- Unqualify --
10617 ---------------
10619 function Unqualify (Expr : Node_Id) return Node_Id is
10620 begin
10621 -- Recurse to handle unlikely case of multiple levels of qualification
10623 if Nkind (Expr) = N_Qualified_Expression then
10624 return Unqualify (Expression (Expr));
10626 -- Normal case, not a qualified expression
10628 else
10629 return Expr;
10630 end if;
10631 end Unqualify;
10633 ----------------------
10634 -- Within_Init_Proc --
10635 ----------------------
10637 function Within_Init_Proc return Boolean is
10638 S : Entity_Id;
10640 begin
10641 S := Current_Scope;
10642 while not Is_Overloadable (S) loop
10643 if S = Standard_Standard then
10644 return False;
10645 else
10646 S := Scope (S);
10647 end if;
10648 end loop;
10650 return Is_Init_Proc (S);
10651 end Within_Init_Proc;
10653 ----------------
10654 -- Wrong_Type --
10655 ----------------
10657 procedure Wrong_Type (Expr : Node_Id; Expected_Type : Entity_Id) is
10658 Found_Type : constant Entity_Id := First_Subtype (Etype (Expr));
10659 Expec_Type : constant Entity_Id := First_Subtype (Expected_Type);
10661 function Has_One_Matching_Field return Boolean;
10662 -- Determines if Expec_Type is a record type with a single component or
10663 -- discriminant whose type matches the found type or is one dimensional
10664 -- array whose component type matches the found type.
10666 ----------------------------
10667 -- Has_One_Matching_Field --
10668 ----------------------------
10670 function Has_One_Matching_Field return Boolean is
10671 E : Entity_Id;
10673 begin
10674 if Is_Array_Type (Expec_Type)
10675 and then Number_Dimensions (Expec_Type) = 1
10676 and then
10677 Covers (Etype (Component_Type (Expec_Type)), Found_Type)
10678 then
10679 return True;
10681 elsif not Is_Record_Type (Expec_Type) then
10682 return False;
10684 else
10685 E := First_Entity (Expec_Type);
10686 loop
10687 if No (E) then
10688 return False;
10690 elsif (Ekind (E) /= E_Discriminant
10691 and then Ekind (E) /= E_Component)
10692 or else (Chars (E) = Name_uTag
10693 or else Chars (E) = Name_uParent)
10694 then
10695 Next_Entity (E);
10697 else
10698 exit;
10699 end if;
10700 end loop;
10702 if not Covers (Etype (E), Found_Type) then
10703 return False;
10705 elsif Present (Next_Entity (E)) then
10706 return False;
10708 else
10709 return True;
10710 end if;
10711 end if;
10712 end Has_One_Matching_Field;
10714 -- Start of processing for Wrong_Type
10716 begin
10717 -- Don't output message if either type is Any_Type, or if a message
10718 -- has already been posted for this node. We need to do the latter
10719 -- check explicitly (it is ordinarily done in Errout), because we
10720 -- are using ! to force the output of the error messages.
10722 if Expec_Type = Any_Type
10723 or else Found_Type = Any_Type
10724 or else Error_Posted (Expr)
10725 then
10726 return;
10728 -- In an instance, there is an ongoing problem with completion of
10729 -- type derived from private types. Their structure is what Gigi
10730 -- expects, but the Etype is the parent type rather than the
10731 -- derived private type itself. Do not flag error in this case. The
10732 -- private completion is an entity without a parent, like an Itype.
10733 -- Similarly, full and partial views may be incorrect in the instance.
10734 -- There is no simple way to insure that it is consistent ???
10736 elsif In_Instance then
10737 if Etype (Etype (Expr)) = Etype (Expected_Type)
10738 and then
10739 (Has_Private_Declaration (Expected_Type)
10740 or else Has_Private_Declaration (Etype (Expr)))
10741 and then No (Parent (Expected_Type))
10742 then
10743 return;
10744 end if;
10745 end if;
10747 -- An interesting special check. If the expression is parenthesized
10748 -- and its type corresponds to the type of the sole component of the
10749 -- expected record type, or to the component type of the expected one
10750 -- dimensional array type, then assume we have a bad aggregate attempt.
10752 if Nkind (Expr) in N_Subexpr
10753 and then Paren_Count (Expr) /= 0
10754 and then Has_One_Matching_Field
10755 then
10756 Error_Msg_N ("positional aggregate cannot have one component", Expr);
10758 -- Another special check, if we are looking for a pool-specific access
10759 -- type and we found an E_Access_Attribute_Type, then we have the case
10760 -- of an Access attribute being used in a context which needs a pool-
10761 -- specific type, which is never allowed. The one extra check we make
10762 -- is that the expected designated type covers the Found_Type.
10764 elsif Is_Access_Type (Expec_Type)
10765 and then Ekind (Found_Type) = E_Access_Attribute_Type
10766 and then Ekind (Base_Type (Expec_Type)) /= E_General_Access_Type
10767 and then Ekind (Base_Type (Expec_Type)) /= E_Anonymous_Access_Type
10768 and then Covers
10769 (Designated_Type (Expec_Type), Designated_Type (Found_Type))
10770 then
10771 Error_Msg_N ("result must be general access type!", Expr);
10772 Error_Msg_NE ("add ALL to }!", Expr, Expec_Type);
10774 -- Another special check, if the expected type is an integer type,
10775 -- but the expression is of type System.Address, and the parent is
10776 -- an addition or subtraction operation whose left operand is the
10777 -- expression in question and whose right operand is of an integral
10778 -- type, then this is an attempt at address arithmetic, so give
10779 -- appropriate message.
10781 elsif Is_Integer_Type (Expec_Type)
10782 and then Is_RTE (Found_Type, RE_Address)
10783 and then (Nkind (Parent (Expr)) = N_Op_Add
10784 or else
10785 Nkind (Parent (Expr)) = N_Op_Subtract)
10786 and then Expr = Left_Opnd (Parent (Expr))
10787 and then Is_Integer_Type (Etype (Right_Opnd (Parent (Expr))))
10788 then
10789 Error_Msg_N
10790 ("address arithmetic not predefined in package System",
10791 Parent (Expr));
10792 Error_Msg_N
10793 ("\possible missing with/use of System.Storage_Elements",
10794 Parent (Expr));
10795 return;
10797 -- If the expected type is an anonymous access type, as for access
10798 -- parameters and discriminants, the error is on the designated types.
10800 elsif Ekind (Expec_Type) = E_Anonymous_Access_Type then
10801 if Comes_From_Source (Expec_Type) then
10802 Error_Msg_NE ("expected}!", Expr, Expec_Type);
10803 else
10804 Error_Msg_NE
10805 ("expected an access type with designated}",
10806 Expr, Designated_Type (Expec_Type));
10807 end if;
10809 if Is_Access_Type (Found_Type)
10810 and then not Comes_From_Source (Found_Type)
10811 then
10812 Error_Msg_NE
10813 ("\\found an access type with designated}!",
10814 Expr, Designated_Type (Found_Type));
10815 else
10816 if From_With_Type (Found_Type) then
10817 Error_Msg_NE ("\\found incomplete}!", Expr, Found_Type);
10818 Error_Msg_Qual_Level := 99;
10819 Error_Msg_NE ("\\missing `WITH &;", Expr, Scope (Found_Type));
10820 Error_Msg_Qual_Level := 0;
10821 else
10822 Error_Msg_NE ("found}!", Expr, Found_Type);
10823 end if;
10824 end if;
10826 -- Normal case of one type found, some other type expected
10828 else
10829 -- If the names of the two types are the same, see if some number
10830 -- of levels of qualification will help. Don't try more than three
10831 -- levels, and if we get to standard, it's no use (and probably
10832 -- represents an error in the compiler) Also do not bother with
10833 -- internal scope names.
10835 declare
10836 Expec_Scope : Entity_Id;
10837 Found_Scope : Entity_Id;
10839 begin
10840 Expec_Scope := Expec_Type;
10841 Found_Scope := Found_Type;
10843 for Levels in Int range 0 .. 3 loop
10844 if Chars (Expec_Scope) /= Chars (Found_Scope) then
10845 Error_Msg_Qual_Level := Levels;
10846 exit;
10847 end if;
10849 Expec_Scope := Scope (Expec_Scope);
10850 Found_Scope := Scope (Found_Scope);
10852 exit when Expec_Scope = Standard_Standard
10853 or else Found_Scope = Standard_Standard
10854 or else not Comes_From_Source (Expec_Scope)
10855 or else not Comes_From_Source (Found_Scope);
10856 end loop;
10857 end;
10859 if Is_Record_Type (Expec_Type)
10860 and then Present (Corresponding_Remote_Type (Expec_Type))
10861 then
10862 Error_Msg_NE ("expected}!", Expr,
10863 Corresponding_Remote_Type (Expec_Type));
10864 else
10865 Error_Msg_NE ("expected}!", Expr, Expec_Type);
10866 end if;
10868 if Is_Entity_Name (Expr)
10869 and then Is_Package_Or_Generic_Package (Entity (Expr))
10870 then
10871 Error_Msg_N ("\\found package name!", Expr);
10873 elsif Is_Entity_Name (Expr)
10874 and then
10875 (Ekind (Entity (Expr)) = E_Procedure
10876 or else
10877 Ekind (Entity (Expr)) = E_Generic_Procedure)
10878 then
10879 if Ekind (Expec_Type) = E_Access_Subprogram_Type then
10880 Error_Msg_N
10881 ("found procedure name, possibly missing Access attribute!",
10882 Expr);
10883 else
10884 Error_Msg_N
10885 ("\\found procedure name instead of function!", Expr);
10886 end if;
10888 elsif Nkind (Expr) = N_Function_Call
10889 and then Ekind (Expec_Type) = E_Access_Subprogram_Type
10890 and then Etype (Designated_Type (Expec_Type)) = Etype (Expr)
10891 and then No (Parameter_Associations (Expr))
10892 then
10893 Error_Msg_N
10894 ("found function name, possibly missing Access attribute!",
10895 Expr);
10897 -- Catch common error: a prefix or infix operator which is not
10898 -- directly visible because the type isn't.
10900 elsif Nkind (Expr) in N_Op
10901 and then Is_Overloaded (Expr)
10902 and then not Is_Immediately_Visible (Expec_Type)
10903 and then not Is_Potentially_Use_Visible (Expec_Type)
10904 and then not In_Use (Expec_Type)
10905 and then Has_Compatible_Type (Right_Opnd (Expr), Expec_Type)
10906 then
10907 Error_Msg_N
10908 ("operator of the type is not directly visible!", Expr);
10910 elsif Ekind (Found_Type) = E_Void
10911 and then Present (Parent (Found_Type))
10912 and then Nkind (Parent (Found_Type)) = N_Full_Type_Declaration
10913 then
10914 Error_Msg_NE ("\\found premature usage of}!", Expr, Found_Type);
10916 else
10917 Error_Msg_NE ("\\found}!", Expr, Found_Type);
10918 end if;
10920 Error_Msg_Qual_Level := 0;
10921 end if;
10922 end Wrong_Type;
10924 end Sem_Util;