2005-03-23 Daniel Berlin <dberlin@dberlin.org>
[official-gcc.git] / gcc / ada / sem_util.adb
blob00fc1a19a5932e2b56f404ef2ca9edb360cafe2b
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-2005, 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Casing; use Casing;
29 with Checks; use Checks;
30 with Debug; use Debug;
31 with Errout; use Errout;
32 with Elists; use Elists;
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 Namet; use Namet;
40 with Nlists; use Nlists;
41 with Nmake; use Nmake;
42 with Output; use Output;
43 with Opt; use Opt;
44 with Rtsfind; use Rtsfind;
45 with Scans; use Scans;
46 with Scn; use Scn;
47 with Sem; use Sem;
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 Snames; use Snames;
55 with Stand; use Stand;
56 with Style;
57 with Stringt; use Stringt;
58 with Targparm; use Targparm;
59 with Tbuild; use Tbuild;
60 with Ttypes; use Ttypes;
61 with Uname; use Uname;
63 package body Sem_Util is
65 -----------------------
66 -- Local Subprograms --
67 -----------------------
69 function Build_Component_Subtype
70 (C : List_Id;
71 Loc : Source_Ptr;
72 T : Entity_Id) return Node_Id;
73 -- This function builds the subtype for Build_Actual_Subtype_Of_Component
74 -- and Build_Discriminal_Subtype_Of_Component. C is a list of constraints,
75 -- Loc is the source location, T is the original subtype.
77 function Is_Fully_Initialized_Variant (Typ : Entity_Id) return Boolean;
78 -- Subsidiary to Is_Fully_Initialized_Type. For an unconstrained type
79 -- with discriminants whose default values are static, examine only the
80 -- components in the selected variant to determine whether all of them
81 -- have a default.
83 function Has_Null_Extension (T : Entity_Id) return Boolean;
84 -- T is a derived tagged type. Check whether the type extension is null.
85 -- If the parent type is fully initialized, T can be treated as such.
87 --------------------------------
88 -- Add_Access_Type_To_Process --
89 --------------------------------
91 procedure Add_Access_Type_To_Process (E : Entity_Id; A : Entity_Id) is
92 L : Elist_Id;
94 begin
95 Ensure_Freeze_Node (E);
96 L := Access_Types_To_Process (Freeze_Node (E));
98 if No (L) then
99 L := New_Elmt_List;
100 Set_Access_Types_To_Process (Freeze_Node (E), L);
101 end if;
103 Append_Elmt (A, L);
104 end Add_Access_Type_To_Process;
106 -----------------------
107 -- Alignment_In_Bits --
108 -----------------------
110 function Alignment_In_Bits (E : Entity_Id) return Uint is
111 begin
112 return Alignment (E) * System_Storage_Unit;
113 end Alignment_In_Bits;
115 -----------------------------------------
116 -- Apply_Compile_Time_Constraint_Error --
117 -----------------------------------------
119 procedure Apply_Compile_Time_Constraint_Error
120 (N : Node_Id;
121 Msg : String;
122 Reason : RT_Exception_Code;
123 Ent : Entity_Id := Empty;
124 Typ : Entity_Id := Empty;
125 Loc : Source_Ptr := No_Location;
126 Rep : Boolean := True;
127 Warn : Boolean := False)
129 Stat : constant Boolean := Is_Static_Expression (N);
130 Rtyp : Entity_Id;
132 begin
133 if No (Typ) then
134 Rtyp := Etype (N);
135 else
136 Rtyp := Typ;
137 end if;
139 Discard_Node (
140 Compile_Time_Constraint_Error (N, Msg, Ent, Loc, Warn => Warn));
142 if not Rep then
143 return;
144 end if;
146 -- Now we replace the node by an N_Raise_Constraint_Error node
147 -- This does not need reanalyzing, so set it as analyzed now.
149 Rewrite (N,
150 Make_Raise_Constraint_Error (Sloc (N),
151 Reason => Reason));
152 Set_Analyzed (N, True);
153 Set_Etype (N, Rtyp);
154 Set_Raises_Constraint_Error (N);
156 -- If the original expression was marked as static, the result is
157 -- still marked as static, but the Raises_Constraint_Error flag is
158 -- always set so that further static evaluation is not attempted.
160 if Stat then
161 Set_Is_Static_Expression (N);
162 end if;
163 end Apply_Compile_Time_Constraint_Error;
165 --------------------------
166 -- Build_Actual_Subtype --
167 --------------------------
169 function Build_Actual_Subtype
170 (T : Entity_Id;
171 N : Node_Or_Entity_Id) return Node_Id
173 Obj : Node_Id;
175 Loc : constant Source_Ptr := Sloc (N);
176 Constraints : List_Id;
177 Decl : Node_Id;
178 Discr : Entity_Id;
179 Hi : Node_Id;
180 Lo : Node_Id;
181 Subt : Entity_Id;
182 Disc_Type : Entity_Id;
184 begin
185 if Nkind (N) = N_Defining_Identifier then
186 Obj := New_Reference_To (N, Loc);
187 else
188 Obj := N;
189 end if;
191 if Is_Array_Type (T) then
192 Constraints := New_List;
194 for J in 1 .. Number_Dimensions (T) loop
196 -- Build an array subtype declaration with the nominal
197 -- subtype and the bounds of the actual. Add the declaration
198 -- in front of the local declarations for the subprogram, for
199 -- analysis before any reference to the formal in the body.
201 Lo :=
202 Make_Attribute_Reference (Loc,
203 Prefix =>
204 Duplicate_Subexpr_No_Checks (Obj, Name_Req => True),
205 Attribute_Name => Name_First,
206 Expressions => New_List (
207 Make_Integer_Literal (Loc, J)));
209 Hi :=
210 Make_Attribute_Reference (Loc,
211 Prefix =>
212 Duplicate_Subexpr_No_Checks (Obj, Name_Req => True),
213 Attribute_Name => Name_Last,
214 Expressions => New_List (
215 Make_Integer_Literal (Loc, J)));
217 Append (Make_Range (Loc, Lo, Hi), Constraints);
218 end loop;
220 -- If the type has unknown discriminants there is no constrained
221 -- subtype to build. This is never called for a formal or for a
222 -- lhs, so returning the type is ok ???
224 elsif Has_Unknown_Discriminants (T) then
225 return T;
227 else
228 Constraints := New_List;
230 if Is_Private_Type (T) and then No (Full_View (T)) then
232 -- Type is a generic derived type. Inherit discriminants from
233 -- Parent type.
235 Disc_Type := Etype (Base_Type (T));
236 else
237 Disc_Type := T;
238 end if;
240 Discr := First_Discriminant (Disc_Type);
242 while Present (Discr) loop
243 Append_To (Constraints,
244 Make_Selected_Component (Loc,
245 Prefix =>
246 Duplicate_Subexpr_No_Checks (Obj),
247 Selector_Name => New_Occurrence_Of (Discr, Loc)));
248 Next_Discriminant (Discr);
249 end loop;
250 end if;
252 Subt :=
253 Make_Defining_Identifier (Loc,
254 Chars => New_Internal_Name ('S'));
255 Set_Is_Internal (Subt);
257 Decl :=
258 Make_Subtype_Declaration (Loc,
259 Defining_Identifier => Subt,
260 Subtype_Indication =>
261 Make_Subtype_Indication (Loc,
262 Subtype_Mark => New_Reference_To (T, Loc),
263 Constraint =>
264 Make_Index_Or_Discriminant_Constraint (Loc,
265 Constraints => Constraints)));
267 Mark_Rewrite_Insertion (Decl);
268 return Decl;
269 end Build_Actual_Subtype;
271 ---------------------------------------
272 -- Build_Actual_Subtype_Of_Component --
273 ---------------------------------------
275 function Build_Actual_Subtype_Of_Component
276 (T : Entity_Id;
277 N : Node_Id) return Node_Id
279 Loc : constant Source_Ptr := Sloc (N);
280 P : constant Node_Id := Prefix (N);
281 D : Elmt_Id;
282 Id : Node_Id;
283 Indx_Type : Entity_Id;
285 Deaccessed_T : Entity_Id;
286 -- This is either a copy of T, or if T is an access type, then it is
287 -- the directly designated type of this access type.
289 function Build_Actual_Array_Constraint return List_Id;
290 -- If one or more of the bounds of the component depends on
291 -- discriminants, build actual constraint using the discriminants
292 -- of the prefix.
294 function Build_Actual_Record_Constraint return List_Id;
295 -- Similar to previous one, for discriminated components constrained
296 -- by the discriminant of the enclosing object.
298 -----------------------------------
299 -- Build_Actual_Array_Constraint --
300 -----------------------------------
302 function Build_Actual_Array_Constraint return List_Id is
303 Constraints : constant List_Id := New_List;
304 Indx : Node_Id;
305 Hi : Node_Id;
306 Lo : Node_Id;
307 Old_Hi : Node_Id;
308 Old_Lo : Node_Id;
310 begin
311 Indx := First_Index (Deaccessed_T);
312 while Present (Indx) loop
313 Old_Lo := Type_Low_Bound (Etype (Indx));
314 Old_Hi := Type_High_Bound (Etype (Indx));
316 if Denotes_Discriminant (Old_Lo) then
317 Lo :=
318 Make_Selected_Component (Loc,
319 Prefix => New_Copy_Tree (P),
320 Selector_Name => New_Occurrence_Of (Entity (Old_Lo), Loc));
322 else
323 Lo := New_Copy_Tree (Old_Lo);
325 -- The new bound will be reanalyzed in the enclosing
326 -- declaration. For literal bounds that come from a type
327 -- declaration, the type of the context must be imposed, so
328 -- insure that analysis will take place. For non-universal
329 -- types this is not strictly necessary.
331 Set_Analyzed (Lo, False);
332 end if;
334 if Denotes_Discriminant (Old_Hi) then
335 Hi :=
336 Make_Selected_Component (Loc,
337 Prefix => New_Copy_Tree (P),
338 Selector_Name => New_Occurrence_Of (Entity (Old_Hi), Loc));
340 else
341 Hi := New_Copy_Tree (Old_Hi);
342 Set_Analyzed (Hi, False);
343 end if;
345 Append (Make_Range (Loc, Lo, Hi), Constraints);
346 Next_Index (Indx);
347 end loop;
349 return Constraints;
350 end Build_Actual_Array_Constraint;
352 ------------------------------------
353 -- Build_Actual_Record_Constraint --
354 ------------------------------------
356 function Build_Actual_Record_Constraint return List_Id is
357 Constraints : constant List_Id := New_List;
358 D : Elmt_Id;
359 D_Val : Node_Id;
361 begin
362 D := First_Elmt (Discriminant_Constraint (Deaccessed_T));
363 while Present (D) loop
365 if Denotes_Discriminant (Node (D)) then
366 D_Val := Make_Selected_Component (Loc,
367 Prefix => New_Copy_Tree (P),
368 Selector_Name => New_Occurrence_Of (Entity (Node (D)), Loc));
370 else
371 D_Val := New_Copy_Tree (Node (D));
372 end if;
374 Append (D_Val, Constraints);
375 Next_Elmt (D);
376 end loop;
378 return Constraints;
379 end Build_Actual_Record_Constraint;
381 -- Start of processing for Build_Actual_Subtype_Of_Component
383 begin
384 if In_Default_Expression then
385 return Empty;
387 elsif Nkind (N) = N_Explicit_Dereference then
388 if Is_Composite_Type (T)
389 and then not Is_Constrained (T)
390 and then not (Is_Class_Wide_Type (T)
391 and then Is_Constrained (Root_Type (T)))
392 and then not Has_Unknown_Discriminants (T)
393 then
394 -- If the type of the dereference is already constrained, it
395 -- is an actual subtype.
397 if Is_Array_Type (Etype (N))
398 and then Is_Constrained (Etype (N))
399 then
400 return Empty;
401 else
402 Remove_Side_Effects (P);
403 return Build_Actual_Subtype (T, N);
404 end if;
405 else
406 return Empty;
407 end if;
408 end if;
410 if Ekind (T) = E_Access_Subtype then
411 Deaccessed_T := Designated_Type (T);
412 else
413 Deaccessed_T := T;
414 end if;
416 if Ekind (Deaccessed_T) = E_Array_Subtype then
417 Id := First_Index (Deaccessed_T);
419 while Present (Id) loop
420 Indx_Type := Underlying_Type (Etype (Id));
422 if Denotes_Discriminant (Type_Low_Bound (Indx_Type)) or else
423 Denotes_Discriminant (Type_High_Bound (Indx_Type))
424 then
425 Remove_Side_Effects (P);
426 return
427 Build_Component_Subtype (
428 Build_Actual_Array_Constraint, Loc, Base_Type (T));
429 end if;
431 Next_Index (Id);
432 end loop;
434 elsif Is_Composite_Type (Deaccessed_T)
435 and then Has_Discriminants (Deaccessed_T)
436 and then not Has_Unknown_Discriminants (Deaccessed_T)
437 then
438 D := First_Elmt (Discriminant_Constraint (Deaccessed_T));
439 while Present (D) loop
441 if Denotes_Discriminant (Node (D)) then
442 Remove_Side_Effects (P);
443 return
444 Build_Component_Subtype (
445 Build_Actual_Record_Constraint, Loc, Base_Type (T));
446 end if;
448 Next_Elmt (D);
449 end loop;
450 end if;
452 -- If none of the above, the actual and nominal subtypes are the same
454 return Empty;
455 end Build_Actual_Subtype_Of_Component;
457 -----------------------------
458 -- Build_Component_Subtype --
459 -----------------------------
461 function Build_Component_Subtype
462 (C : List_Id;
463 Loc : Source_Ptr;
464 T : Entity_Id) return Node_Id
466 Subt : Entity_Id;
467 Decl : Node_Id;
469 begin
470 -- Unchecked_Union components do not require component subtypes
472 if Is_Unchecked_Union (T) then
473 return Empty;
474 end if;
476 Subt :=
477 Make_Defining_Identifier (Loc,
478 Chars => New_Internal_Name ('S'));
479 Set_Is_Internal (Subt);
481 Decl :=
482 Make_Subtype_Declaration (Loc,
483 Defining_Identifier => Subt,
484 Subtype_Indication =>
485 Make_Subtype_Indication (Loc,
486 Subtype_Mark => New_Reference_To (Base_Type (T), Loc),
487 Constraint =>
488 Make_Index_Or_Discriminant_Constraint (Loc,
489 Constraints => C)));
491 Mark_Rewrite_Insertion (Decl);
492 return Decl;
493 end Build_Component_Subtype;
495 --------------------------------------------
496 -- Build_Discriminal_Subtype_Of_Component --
497 --------------------------------------------
499 function Build_Discriminal_Subtype_Of_Component
500 (T : Entity_Id) return Node_Id
502 Loc : constant Source_Ptr := Sloc (T);
503 D : Elmt_Id;
504 Id : Node_Id;
506 function Build_Discriminal_Array_Constraint return List_Id;
507 -- If one or more of the bounds of the component depends on
508 -- discriminants, build actual constraint using the discriminants
509 -- of the prefix.
511 function Build_Discriminal_Record_Constraint return List_Id;
512 -- Similar to previous one, for discriminated components constrained
513 -- by the discriminant of the enclosing object.
515 ----------------------------------------
516 -- Build_Discriminal_Array_Constraint --
517 ----------------------------------------
519 function Build_Discriminal_Array_Constraint return List_Id is
520 Constraints : constant List_Id := New_List;
521 Indx : Node_Id;
522 Hi : Node_Id;
523 Lo : Node_Id;
524 Old_Hi : Node_Id;
525 Old_Lo : Node_Id;
527 begin
528 Indx := First_Index (T);
529 while Present (Indx) loop
530 Old_Lo := Type_Low_Bound (Etype (Indx));
531 Old_Hi := Type_High_Bound (Etype (Indx));
533 if Denotes_Discriminant (Old_Lo) then
534 Lo := New_Occurrence_Of (Discriminal (Entity (Old_Lo)), Loc);
536 else
537 Lo := New_Copy_Tree (Old_Lo);
538 end if;
540 if Denotes_Discriminant (Old_Hi) then
541 Hi := New_Occurrence_Of (Discriminal (Entity (Old_Hi)), Loc);
543 else
544 Hi := New_Copy_Tree (Old_Hi);
545 end if;
547 Append (Make_Range (Loc, Lo, Hi), Constraints);
548 Next_Index (Indx);
549 end loop;
551 return Constraints;
552 end Build_Discriminal_Array_Constraint;
554 -----------------------------------------
555 -- Build_Discriminal_Record_Constraint --
556 -----------------------------------------
558 function Build_Discriminal_Record_Constraint return List_Id is
559 Constraints : constant List_Id := New_List;
560 D : Elmt_Id;
561 D_Val : Node_Id;
563 begin
564 D := First_Elmt (Discriminant_Constraint (T));
565 while Present (D) loop
566 if Denotes_Discriminant (Node (D)) then
567 D_Val :=
568 New_Occurrence_Of (Discriminal (Entity (Node (D))), Loc);
570 else
571 D_Val := New_Copy_Tree (Node (D));
572 end if;
574 Append (D_Val, Constraints);
575 Next_Elmt (D);
576 end loop;
578 return Constraints;
579 end Build_Discriminal_Record_Constraint;
581 -- Start of processing for Build_Discriminal_Subtype_Of_Component
583 begin
584 if Ekind (T) = E_Array_Subtype then
585 Id := First_Index (T);
587 while Present (Id) loop
588 if Denotes_Discriminant (Type_Low_Bound (Etype (Id))) or else
589 Denotes_Discriminant (Type_High_Bound (Etype (Id)))
590 then
591 return Build_Component_Subtype
592 (Build_Discriminal_Array_Constraint, Loc, T);
593 end if;
595 Next_Index (Id);
596 end loop;
598 elsif Ekind (T) = E_Record_Subtype
599 and then Has_Discriminants (T)
600 and then not Has_Unknown_Discriminants (T)
601 then
602 D := First_Elmt (Discriminant_Constraint (T));
603 while Present (D) loop
604 if Denotes_Discriminant (Node (D)) then
605 return Build_Component_Subtype
606 (Build_Discriminal_Record_Constraint, Loc, T);
607 end if;
609 Next_Elmt (D);
610 end loop;
611 end if;
613 -- If none of the above, the actual and nominal subtypes are the same
615 return Empty;
616 end Build_Discriminal_Subtype_Of_Component;
618 ------------------------------
619 -- Build_Elaboration_Entity --
620 ------------------------------
622 procedure Build_Elaboration_Entity (N : Node_Id; Spec_Id : Entity_Id) is
623 Loc : constant Source_Ptr := Sloc (N);
624 Unum : constant Unit_Number_Type := Get_Source_Unit (Loc);
625 Decl : Node_Id;
626 P : Natural;
627 Elab_Ent : Entity_Id;
629 begin
630 -- Ignore if already constructed
632 if Present (Elaboration_Entity (Spec_Id)) then
633 return;
634 end if;
636 -- Construct name of elaboration entity as xxx_E, where xxx
637 -- is the unit name with dots replaced by double underscore.
638 -- We have to manually construct this name, since it will
639 -- be elaborated in the outer scope, and thus will not have
640 -- the unit name automatically prepended.
642 Get_Name_String (Unit_Name (Unum));
644 -- Replace the %s by _E
646 Name_Buffer (Name_Len - 1 .. Name_Len) := "_E";
648 -- Replace dots by double underscore
650 P := 2;
651 while P < Name_Len - 2 loop
652 if Name_Buffer (P) = '.' then
653 Name_Buffer (P + 2 .. Name_Len + 1) :=
654 Name_Buffer (P + 1 .. Name_Len);
655 Name_Len := Name_Len + 1;
656 Name_Buffer (P) := '_';
657 Name_Buffer (P + 1) := '_';
658 P := P + 3;
659 else
660 P := P + 1;
661 end if;
662 end loop;
664 -- Create elaboration flag
666 Elab_Ent :=
667 Make_Defining_Identifier (Loc, Chars => Name_Find);
668 Set_Elaboration_Entity (Spec_Id, Elab_Ent);
670 if No (Declarations (Aux_Decls_Node (N))) then
671 Set_Declarations (Aux_Decls_Node (N), New_List);
672 end if;
674 Decl :=
675 Make_Object_Declaration (Loc,
676 Defining_Identifier => Elab_Ent,
677 Object_Definition =>
678 New_Occurrence_Of (Standard_Boolean, Loc),
679 Expression =>
680 New_Occurrence_Of (Standard_False, Loc));
682 Append_To (Declarations (Aux_Decls_Node (N)), Decl);
683 Analyze (Decl);
685 -- Reset True_Constant indication, since we will indeed
686 -- assign a value to the variable in the binder main.
688 Set_Is_True_Constant (Elab_Ent, False);
689 Set_Current_Value (Elab_Ent, Empty);
691 -- We do not want any further qualification of the name (if we did
692 -- not do this, we would pick up the name of the generic package
693 -- in the case of a library level generic instantiation).
695 Set_Has_Qualified_Name (Elab_Ent);
696 Set_Has_Fully_Qualified_Name (Elab_Ent);
697 end Build_Elaboration_Entity;
699 -----------------------------------
700 -- Cannot_Raise_Constraint_Error --
701 -----------------------------------
703 function Cannot_Raise_Constraint_Error (Expr : Node_Id) return Boolean is
704 begin
705 if Compile_Time_Known_Value (Expr) then
706 return True;
708 elsif Do_Range_Check (Expr) then
709 return False;
711 elsif Raises_Constraint_Error (Expr) then
712 return False;
714 else
715 case Nkind (Expr) is
716 when N_Identifier =>
717 return True;
719 when N_Expanded_Name =>
720 return True;
722 when N_Selected_Component =>
723 return not Do_Discriminant_Check (Expr);
725 when N_Attribute_Reference =>
726 if Do_Overflow_Check (Expr) then
727 return False;
729 elsif No (Expressions (Expr)) then
730 return True;
732 else
733 declare
734 N : Node_Id := First (Expressions (Expr));
736 begin
737 while Present (N) loop
738 if Cannot_Raise_Constraint_Error (N) then
739 Next (N);
740 else
741 return False;
742 end if;
743 end loop;
745 return True;
746 end;
747 end if;
749 when N_Type_Conversion =>
750 if Do_Overflow_Check (Expr)
751 or else Do_Length_Check (Expr)
752 or else Do_Tag_Check (Expr)
753 then
754 return False;
755 else
756 return
757 Cannot_Raise_Constraint_Error (Expression (Expr));
758 end if;
760 when N_Unchecked_Type_Conversion =>
761 return Cannot_Raise_Constraint_Error (Expression (Expr));
763 when N_Unary_Op =>
764 if Do_Overflow_Check (Expr) then
765 return False;
766 else
767 return
768 Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
769 end if;
771 when N_Op_Divide |
772 N_Op_Mod |
773 N_Op_Rem
775 if Do_Division_Check (Expr)
776 or else Do_Overflow_Check (Expr)
777 then
778 return False;
779 else
780 return
781 Cannot_Raise_Constraint_Error (Left_Opnd (Expr))
782 and then
783 Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
784 end if;
786 when N_Op_Add |
787 N_Op_And |
788 N_Op_Concat |
789 N_Op_Eq |
790 N_Op_Expon |
791 N_Op_Ge |
792 N_Op_Gt |
793 N_Op_Le |
794 N_Op_Lt |
795 N_Op_Multiply |
796 N_Op_Ne |
797 N_Op_Or |
798 N_Op_Rotate_Left |
799 N_Op_Rotate_Right |
800 N_Op_Shift_Left |
801 N_Op_Shift_Right |
802 N_Op_Shift_Right_Arithmetic |
803 N_Op_Subtract |
804 N_Op_Xor
806 if Do_Overflow_Check (Expr) then
807 return False;
808 else
809 return
810 Cannot_Raise_Constraint_Error (Left_Opnd (Expr))
811 and then
812 Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
813 end if;
815 when others =>
816 return False;
817 end case;
818 end if;
819 end Cannot_Raise_Constraint_Error;
821 --------------------------
822 -- Check_Fully_Declared --
823 --------------------------
825 procedure Check_Fully_Declared (T : Entity_Id; N : Node_Id) is
826 begin
827 if Ekind (T) = E_Incomplete_Type then
829 -- Ada 2005 (AI-50217): If the type is available through a limited
830 -- with_clause, verify that its full view has been analyzed.
832 if From_With_Type (T)
833 and then Present (Non_Limited_View (T))
834 and then Ekind (Non_Limited_View (T)) /= E_Incomplete_Type
835 then
836 -- The non-limited view is fully declared
837 null;
839 else
840 Error_Msg_NE
841 ("premature usage of incomplete}", N, First_Subtype (T));
842 end if;
844 elsif Has_Private_Component (T)
845 and then not Is_Generic_Type (Root_Type (T))
846 and then not In_Default_Expression
847 then
849 -- Special case: if T is the anonymous type created for a single
850 -- task or protected object, use the name of the source object.
852 if Is_Concurrent_Type (T)
853 and then not Comes_From_Source (T)
854 and then Nkind (N) = N_Object_Declaration
855 then
856 Error_Msg_NE ("type of& has incomplete component", N,
857 Defining_Identifier (N));
859 else
860 Error_Msg_NE
861 ("premature usage of incomplete}", N, First_Subtype (T));
862 end if;
863 end if;
864 end Check_Fully_Declared;
866 ------------------------------------------
867 -- Check_Potentially_Blocking_Operation --
868 ------------------------------------------
870 procedure Check_Potentially_Blocking_Operation (N : Node_Id) is
871 S : Entity_Id;
873 begin
874 -- N is one of the potentially blocking operations listed in 9.5.1(8).
875 -- When pragma Detect_Blocking is active, the run time will raise
876 -- Program_Error. Here we only issue a warning, since we generally
877 -- support the use of potentially blocking operations in the absence
878 -- of the pragma.
880 -- Indirect blocking through a subprogram call cannot be diagnosed
881 -- statically without interprocedural analysis, so we do not attempt
882 -- to do it here.
884 S := Scope (Current_Scope);
885 while Present (S) and then S /= Standard_Standard loop
886 if Is_Protected_Type (S) then
887 Error_Msg_N
888 ("potentially blocking operation in protected operation?", N);
890 return;
891 end if;
893 S := Scope (S);
894 end loop;
895 end Check_Potentially_Blocking_Operation;
897 ---------------
898 -- Check_VMS --
899 ---------------
901 procedure Check_VMS (Construct : Node_Id) is
902 begin
903 if not OpenVMS_On_Target then
904 Error_Msg_N
905 ("this construct is allowed only in Open'V'M'S", Construct);
906 end if;
907 end Check_VMS;
909 ----------------------------------
910 -- Collect_Primitive_Operations --
911 ----------------------------------
913 function Collect_Primitive_Operations (T : Entity_Id) return Elist_Id is
914 B_Type : constant Entity_Id := Base_Type (T);
915 B_Decl : constant Node_Id := Original_Node (Parent (B_Type));
916 B_Scope : Entity_Id := Scope (B_Type);
917 Op_List : Elist_Id;
918 Formal : Entity_Id;
919 Is_Prim : Boolean;
920 Formal_Derived : Boolean := False;
921 Id : Entity_Id;
923 begin
924 -- For tagged types, the primitive operations are collected as they
925 -- are declared, and held in an explicit list which is simply returned.
927 if Is_Tagged_Type (B_Type) then
928 return Primitive_Operations (B_Type);
930 -- An untagged generic type that is a derived type inherits the
931 -- primitive operations of its parent type. Other formal types only
932 -- have predefined operators, which are not explicitly represented.
934 elsif Is_Generic_Type (B_Type) then
935 if Nkind (B_Decl) = N_Formal_Type_Declaration
936 and then Nkind (Formal_Type_Definition (B_Decl))
937 = N_Formal_Derived_Type_Definition
938 then
939 Formal_Derived := True;
940 else
941 return New_Elmt_List;
942 end if;
943 end if;
945 Op_List := New_Elmt_List;
947 if B_Scope = Standard_Standard then
948 if B_Type = Standard_String then
949 Append_Elmt (Standard_Op_Concat, Op_List);
951 elsif B_Type = Standard_Wide_String then
952 Append_Elmt (Standard_Op_Concatw, Op_List);
954 else
955 null;
956 end if;
958 elsif (Is_Package (B_Scope)
959 and then Nkind (
960 Parent (Declaration_Node (First_Subtype (T))))
961 /= N_Package_Body)
963 or else Is_Derived_Type (B_Type)
964 then
965 -- The primitive operations appear after the base type, except
966 -- if the derivation happens within the private part of B_Scope
967 -- and the type is a private type, in which case both the type
968 -- and some primitive operations may appear before the base
969 -- type, and the list of candidates starts after the type.
971 if In_Open_Scopes (B_Scope)
972 and then Scope (T) = B_Scope
973 and then In_Private_Part (B_Scope)
974 then
975 Id := Next_Entity (T);
976 else
977 Id := Next_Entity (B_Type);
978 end if;
980 while Present (Id) loop
982 -- Note that generic formal subprograms are not
983 -- considered to be primitive operations and thus
984 -- are never inherited.
986 if Is_Overloadable (Id)
987 and then Nkind (Parent (Parent (Id)))
988 not in N_Formal_Subprogram_Declaration
989 then
990 Is_Prim := False;
992 if Base_Type (Etype (Id)) = B_Type then
993 Is_Prim := True;
994 else
995 Formal := First_Formal (Id);
996 while Present (Formal) loop
997 if Base_Type (Etype (Formal)) = B_Type then
998 Is_Prim := True;
999 exit;
1001 elsif Ekind (Etype (Formal)) = E_Anonymous_Access_Type
1002 and then Base_Type
1003 (Designated_Type (Etype (Formal))) = B_Type
1004 then
1005 Is_Prim := True;
1006 exit;
1007 end if;
1009 Next_Formal (Formal);
1010 end loop;
1011 end if;
1013 -- For a formal derived type, the only primitives are the
1014 -- ones inherited from the parent type. Operations appearing
1015 -- in the package declaration are not primitive for it.
1017 if Is_Prim
1018 and then (not Formal_Derived
1019 or else Present (Alias (Id)))
1020 then
1021 Append_Elmt (Id, Op_List);
1022 end if;
1023 end if;
1025 Next_Entity (Id);
1027 -- For a type declared in System, some of its operations
1028 -- may appear in the target-specific extension to System.
1030 if No (Id)
1031 and then Chars (B_Scope) = Name_System
1032 and then Scope (B_Scope) = Standard_Standard
1033 and then Present_System_Aux
1034 then
1035 B_Scope := System_Aux_Id;
1036 Id := First_Entity (System_Aux_Id);
1037 end if;
1038 end loop;
1039 end if;
1041 return Op_List;
1042 end Collect_Primitive_Operations;
1044 -----------------------------------
1045 -- Compile_Time_Constraint_Error --
1046 -----------------------------------
1048 function Compile_Time_Constraint_Error
1049 (N : Node_Id;
1050 Msg : String;
1051 Ent : Entity_Id := Empty;
1052 Loc : Source_Ptr := No_Location;
1053 Warn : Boolean := False) return Node_Id
1055 Msgc : String (1 .. Msg'Length + 2);
1056 Msgl : Natural;
1057 Wmsg : Boolean;
1058 P : Node_Id;
1059 Msgs : Boolean;
1060 Eloc : Source_Ptr;
1062 begin
1063 -- A static constraint error in an instance body is not a fatal error.
1064 -- we choose to inhibit the message altogether, because there is no
1065 -- obvious node (for now) on which to post it. On the other hand the
1066 -- offending node must be replaced with a constraint_error in any case.
1068 -- No messages are generated if we already posted an error on this node
1070 if not Error_Posted (N) then
1071 if Loc /= No_Location then
1072 Eloc := Loc;
1073 else
1074 Eloc := Sloc (N);
1075 end if;
1077 -- Make all such messages unconditional
1079 Msgc (1 .. Msg'Length) := Msg;
1080 Msgc (Msg'Length + 1) := '!';
1081 Msgl := Msg'Length + 1;
1083 -- Message is a warning, even in Ada 95 case
1085 if Msg (Msg'Length) = '?' then
1086 Wmsg := True;
1088 -- In Ada 83, all messages are warnings. In the private part and
1089 -- the body of an instance, constraint_checks are only warnings.
1090 -- We also make this a warning if the Warn parameter is set.
1092 elsif Warn
1093 or else (Ada_Version = Ada_83 and then Comes_From_Source (N))
1094 then
1095 Msgl := Msgl + 1;
1096 Msgc (Msgl) := '?';
1097 Wmsg := True;
1099 elsif In_Instance_Not_Visible then
1100 Msgl := Msgl + 1;
1101 Msgc (Msgl) := '?';
1102 Wmsg := True;
1104 -- Otherwise we have a real error message (Ada 95 static case)
1106 else
1107 Wmsg := False;
1108 end if;
1110 -- Should we generate a warning? The answer is not quite yes. The
1111 -- very annoying exception occurs in the case of a short circuit
1112 -- operator where the left operand is static and decisive. Climb
1113 -- parents to see if that is the case we have here.
1115 Msgs := True;
1116 P := N;
1118 loop
1119 P := Parent (P);
1121 if (Nkind (P) = N_And_Then
1122 and then Compile_Time_Known_Value (Left_Opnd (P))
1123 and then Is_False (Expr_Value (Left_Opnd (P))))
1124 or else (Nkind (P) = N_Or_Else
1125 and then Compile_Time_Known_Value (Left_Opnd (P))
1126 and then Is_True (Expr_Value (Left_Opnd (P))))
1127 then
1128 Msgs := False;
1129 exit;
1131 elsif Nkind (P) = N_Component_Association
1132 and then Nkind (Parent (P)) = N_Aggregate
1133 then
1134 null; -- Keep going.
1136 else
1137 exit when Nkind (P) not in N_Subexpr;
1138 end if;
1139 end loop;
1141 if Msgs then
1142 if Present (Ent) then
1143 Error_Msg_NEL (Msgc (1 .. Msgl), N, Ent, Eloc);
1144 else
1145 Error_Msg_NEL (Msgc (1 .. Msgl), N, Etype (N), Eloc);
1146 end if;
1148 if Wmsg then
1149 if Inside_Init_Proc then
1150 Error_Msg_NEL
1151 ("\& will be raised for objects of this type!?",
1152 N, Standard_Constraint_Error, Eloc);
1153 else
1154 Error_Msg_NEL
1155 ("\& will be raised at run time!?",
1156 N, Standard_Constraint_Error, Eloc);
1157 end if;
1158 else
1159 Error_Msg_NEL
1160 ("\static expression raises&!",
1161 N, Standard_Constraint_Error, Eloc);
1162 end if;
1163 end if;
1164 end if;
1166 return N;
1167 end Compile_Time_Constraint_Error;
1169 -----------------------
1170 -- Conditional_Delay --
1171 -----------------------
1173 procedure Conditional_Delay (New_Ent, Old_Ent : Entity_Id) is
1174 begin
1175 if Has_Delayed_Freeze (Old_Ent) and then not Is_Frozen (Old_Ent) then
1176 Set_Has_Delayed_Freeze (New_Ent);
1177 end if;
1178 end Conditional_Delay;
1180 --------------------
1181 -- Current_Entity --
1182 --------------------
1184 -- The currently visible definition for a given identifier is the
1185 -- one most chained at the start of the visibility chain, i.e. the
1186 -- one that is referenced by the Node_Id value of the name of the
1187 -- given identifier.
1189 function Current_Entity (N : Node_Id) return Entity_Id is
1190 begin
1191 return Get_Name_Entity_Id (Chars (N));
1192 end Current_Entity;
1194 -----------------------------
1195 -- Current_Entity_In_Scope --
1196 -----------------------------
1198 function Current_Entity_In_Scope (N : Node_Id) return Entity_Id is
1199 E : Entity_Id;
1200 CS : constant Entity_Id := Current_Scope;
1202 Transient_Case : constant Boolean := Scope_Is_Transient;
1204 begin
1205 E := Get_Name_Entity_Id (Chars (N));
1207 while Present (E)
1208 and then Scope (E) /= CS
1209 and then (not Transient_Case or else Scope (E) /= Scope (CS))
1210 loop
1211 E := Homonym (E);
1212 end loop;
1214 return E;
1215 end Current_Entity_In_Scope;
1217 -------------------
1218 -- Current_Scope --
1219 -------------------
1221 function Current_Scope return Entity_Id is
1222 begin
1223 if Scope_Stack.Last = -1 then
1224 return Standard_Standard;
1225 else
1226 declare
1227 C : constant Entity_Id :=
1228 Scope_Stack.Table (Scope_Stack.Last).Entity;
1229 begin
1230 if Present (C) then
1231 return C;
1232 else
1233 return Standard_Standard;
1234 end if;
1235 end;
1236 end if;
1237 end Current_Scope;
1239 ------------------------
1240 -- Current_Subprogram --
1241 ------------------------
1243 function Current_Subprogram return Entity_Id is
1244 Scop : constant Entity_Id := Current_Scope;
1246 begin
1247 if Is_Subprogram (Scop) or else Is_Generic_Subprogram (Scop) then
1248 return Scop;
1249 else
1250 return Enclosing_Subprogram (Scop);
1251 end if;
1252 end Current_Subprogram;
1254 ---------------------
1255 -- Defining_Entity --
1256 ---------------------
1258 function Defining_Entity (N : Node_Id) return Entity_Id is
1259 K : constant Node_Kind := Nkind (N);
1260 Err : Entity_Id := Empty;
1262 begin
1263 case K is
1264 when
1265 N_Subprogram_Declaration |
1266 N_Abstract_Subprogram_Declaration |
1267 N_Subprogram_Body |
1268 N_Package_Declaration |
1269 N_Subprogram_Renaming_Declaration |
1270 N_Subprogram_Body_Stub |
1271 N_Generic_Subprogram_Declaration |
1272 N_Generic_Package_Declaration |
1273 N_Formal_Subprogram_Declaration
1275 return Defining_Entity (Specification (N));
1277 when
1278 N_Component_Declaration |
1279 N_Defining_Program_Unit_Name |
1280 N_Discriminant_Specification |
1281 N_Entry_Body |
1282 N_Entry_Declaration |
1283 N_Entry_Index_Specification |
1284 N_Exception_Declaration |
1285 N_Exception_Renaming_Declaration |
1286 N_Formal_Object_Declaration |
1287 N_Formal_Package_Declaration |
1288 N_Formal_Type_Declaration |
1289 N_Full_Type_Declaration |
1290 N_Implicit_Label_Declaration |
1291 N_Incomplete_Type_Declaration |
1292 N_Loop_Parameter_Specification |
1293 N_Number_Declaration |
1294 N_Object_Declaration |
1295 N_Object_Renaming_Declaration |
1296 N_Package_Body_Stub |
1297 N_Parameter_Specification |
1298 N_Private_Extension_Declaration |
1299 N_Private_Type_Declaration |
1300 N_Protected_Body |
1301 N_Protected_Body_Stub |
1302 N_Protected_Type_Declaration |
1303 N_Single_Protected_Declaration |
1304 N_Single_Task_Declaration |
1305 N_Subtype_Declaration |
1306 N_Task_Body |
1307 N_Task_Body_Stub |
1308 N_Task_Type_Declaration
1310 return Defining_Identifier (N);
1312 when N_Subunit =>
1313 return Defining_Entity (Proper_Body (N));
1315 when
1316 N_Function_Instantiation |
1317 N_Function_Specification |
1318 N_Generic_Function_Renaming_Declaration |
1319 N_Generic_Package_Renaming_Declaration |
1320 N_Generic_Procedure_Renaming_Declaration |
1321 N_Package_Body |
1322 N_Package_Instantiation |
1323 N_Package_Renaming_Declaration |
1324 N_Package_Specification |
1325 N_Procedure_Instantiation |
1326 N_Procedure_Specification
1328 declare
1329 Nam : constant Node_Id := Defining_Unit_Name (N);
1331 begin
1332 if Nkind (Nam) in N_Entity then
1333 return Nam;
1335 -- For Error, make up a name and attach to declaration
1336 -- so we can continue semantic analysis
1338 elsif Nam = Error then
1339 Err :=
1340 Make_Defining_Identifier (Sloc (N),
1341 Chars => New_Internal_Name ('T'));
1342 Set_Defining_Unit_Name (N, Err);
1344 return Err;
1345 -- If not an entity, get defining identifier
1347 else
1348 return Defining_Identifier (Nam);
1349 end if;
1350 end;
1352 when N_Block_Statement =>
1353 return Entity (Identifier (N));
1355 when others =>
1356 raise Program_Error;
1358 end case;
1359 end Defining_Entity;
1361 --------------------------
1362 -- Denotes_Discriminant --
1363 --------------------------
1365 function Denotes_Discriminant
1366 (N : Node_Id;
1367 Check_Protected : Boolean := False) return Boolean
1369 E : Entity_Id;
1370 begin
1371 if not Is_Entity_Name (N)
1372 or else No (Entity (N))
1373 then
1374 return False;
1375 else
1376 E := Entity (N);
1377 end if;
1379 -- If we are checking for a protected type, the discriminant may have
1380 -- been rewritten as the corresponding discriminal of the original type
1381 -- or of the corresponding concurrent record, depending on whether we
1382 -- are in the spec or body of the protected type.
1384 return Ekind (E) = E_Discriminant
1385 or else
1386 (Check_Protected
1387 and then Ekind (E) = E_In_Parameter
1388 and then Present (Discriminal_Link (E))
1389 and then
1390 (Is_Protected_Type (Scope (Discriminal_Link (E)))
1391 or else
1392 Is_Concurrent_Record_Type (Scope (Discriminal_Link (E)))));
1394 end Denotes_Discriminant;
1396 -----------------------------
1397 -- Depends_On_Discriminant --
1398 -----------------------------
1400 function Depends_On_Discriminant (N : Node_Id) return Boolean is
1401 L : Node_Id;
1402 H : Node_Id;
1404 begin
1405 Get_Index_Bounds (N, L, H);
1406 return Denotes_Discriminant (L) or else Denotes_Discriminant (H);
1407 end Depends_On_Discriminant;
1409 -------------------------
1410 -- Designate_Same_Unit --
1411 -------------------------
1413 function Designate_Same_Unit
1414 (Name1 : Node_Id;
1415 Name2 : Node_Id) return Boolean
1417 K1 : constant Node_Kind := Nkind (Name1);
1418 K2 : constant Node_Kind := Nkind (Name2);
1420 function Prefix_Node (N : Node_Id) return Node_Id;
1421 -- Returns the parent unit name node of a defining program unit name
1422 -- or the prefix if N is a selected component or an expanded name.
1424 function Select_Node (N : Node_Id) return Node_Id;
1425 -- Returns the defining identifier node of a defining program unit
1426 -- name or the selector node if N is a selected component or an
1427 -- expanded name.
1429 -----------------
1430 -- Prefix_Node --
1431 -----------------
1433 function Prefix_Node (N : Node_Id) return Node_Id is
1434 begin
1435 if Nkind (N) = N_Defining_Program_Unit_Name then
1436 return Name (N);
1438 else
1439 return Prefix (N);
1440 end if;
1441 end Prefix_Node;
1443 -----------------
1444 -- Select_Node --
1445 -----------------
1447 function Select_Node (N : Node_Id) return Node_Id is
1448 begin
1449 if Nkind (N) = N_Defining_Program_Unit_Name then
1450 return Defining_Identifier (N);
1452 else
1453 return Selector_Name (N);
1454 end if;
1455 end Select_Node;
1457 -- Start of processing for Designate_Next_Unit
1459 begin
1460 if (K1 = N_Identifier or else
1461 K1 = N_Defining_Identifier)
1462 and then
1463 (K2 = N_Identifier or else
1464 K2 = N_Defining_Identifier)
1465 then
1466 return Chars (Name1) = Chars (Name2);
1468 elsif
1469 (K1 = N_Expanded_Name or else
1470 K1 = N_Selected_Component or else
1471 K1 = N_Defining_Program_Unit_Name)
1472 and then
1473 (K2 = N_Expanded_Name or else
1474 K2 = N_Selected_Component or else
1475 K2 = N_Defining_Program_Unit_Name)
1476 then
1477 return
1478 (Chars (Select_Node (Name1)) = Chars (Select_Node (Name2)))
1479 and then
1480 Designate_Same_Unit (Prefix_Node (Name1), Prefix_Node (Name2));
1482 else
1483 return False;
1484 end if;
1485 end Designate_Same_Unit;
1487 ----------------------------
1488 -- Enclosing_Generic_Body --
1489 ----------------------------
1491 function Enclosing_Generic_Body
1492 (E : Entity_Id) return Node_Id
1494 P : Node_Id;
1495 Decl : Node_Id;
1496 Spec : Node_Id;
1498 begin
1499 P := Parent (E);
1501 while Present (P) loop
1502 if Nkind (P) = N_Package_Body
1503 or else Nkind (P) = N_Subprogram_Body
1504 then
1505 Spec := Corresponding_Spec (P);
1507 if Present (Spec) then
1508 Decl := Unit_Declaration_Node (Spec);
1510 if Nkind (Decl) = N_Generic_Package_Declaration
1511 or else Nkind (Decl) = N_Generic_Subprogram_Declaration
1512 then
1513 return P;
1514 end if;
1515 end if;
1516 end if;
1518 P := Parent (P);
1519 end loop;
1521 return Empty;
1522 end Enclosing_Generic_Body;
1524 -------------------------------
1525 -- Enclosing_Lib_Unit_Entity --
1526 -------------------------------
1528 function Enclosing_Lib_Unit_Entity return Entity_Id is
1529 Unit_Entity : Entity_Id := Current_Scope;
1531 begin
1532 -- Look for enclosing library unit entity by following scope links.
1533 -- Equivalent to, but faster than indexing through the scope stack.
1535 while (Present (Scope (Unit_Entity))
1536 and then Scope (Unit_Entity) /= Standard_Standard)
1537 and not Is_Child_Unit (Unit_Entity)
1538 loop
1539 Unit_Entity := Scope (Unit_Entity);
1540 end loop;
1542 return Unit_Entity;
1543 end Enclosing_Lib_Unit_Entity;
1545 -----------------------------
1546 -- Enclosing_Lib_Unit_Node --
1547 -----------------------------
1549 function Enclosing_Lib_Unit_Node (N : Node_Id) return Node_Id is
1550 Current_Node : Node_Id := N;
1552 begin
1553 while Present (Current_Node)
1554 and then Nkind (Current_Node) /= N_Compilation_Unit
1555 loop
1556 Current_Node := Parent (Current_Node);
1557 end loop;
1559 if Nkind (Current_Node) /= N_Compilation_Unit then
1560 return Empty;
1561 end if;
1563 return Current_Node;
1564 end Enclosing_Lib_Unit_Node;
1566 --------------------------
1567 -- Enclosing_Subprogram --
1568 --------------------------
1570 function Enclosing_Subprogram (E : Entity_Id) return Entity_Id is
1571 Dynamic_Scope : constant Entity_Id := Enclosing_Dynamic_Scope (E);
1573 begin
1574 if Dynamic_Scope = Standard_Standard then
1575 return Empty;
1577 elsif Ekind (Dynamic_Scope) = E_Subprogram_Body then
1578 return Corresponding_Spec (Parent (Parent (Dynamic_Scope)));
1580 elsif Ekind (Dynamic_Scope) = E_Block then
1581 return Enclosing_Subprogram (Dynamic_Scope);
1583 elsif Ekind (Dynamic_Scope) = E_Task_Type then
1584 return Get_Task_Body_Procedure (Dynamic_Scope);
1586 elsif Convention (Dynamic_Scope) = Convention_Protected then
1587 return Protected_Body_Subprogram (Dynamic_Scope);
1589 else
1590 return Dynamic_Scope;
1591 end if;
1592 end Enclosing_Subprogram;
1594 ------------------------
1595 -- Ensure_Freeze_Node --
1596 ------------------------
1598 procedure Ensure_Freeze_Node (E : Entity_Id) is
1599 FN : Node_Id;
1601 begin
1602 if No (Freeze_Node (E)) then
1603 FN := Make_Freeze_Entity (Sloc (E));
1604 Set_Has_Delayed_Freeze (E);
1605 Set_Freeze_Node (E, FN);
1606 Set_Access_Types_To_Process (FN, No_Elist);
1607 Set_TSS_Elist (FN, No_Elist);
1608 Set_Entity (FN, E);
1609 end if;
1610 end Ensure_Freeze_Node;
1612 ----------------
1613 -- Enter_Name --
1614 ----------------
1616 procedure Enter_Name (Def_Id : Node_Id) is
1617 C : constant Entity_Id := Current_Entity (Def_Id);
1618 E : constant Entity_Id := Current_Entity_In_Scope (Def_Id);
1619 S : constant Entity_Id := Current_Scope;
1621 begin
1622 Generate_Definition (Def_Id);
1624 -- Add new name to current scope declarations. Check for duplicate
1625 -- declaration, which may or may not be a genuine error.
1627 if Present (E) then
1629 -- Case of previous entity entered because of a missing declaration
1630 -- or else a bad subtype indication. Best is to use the new entity,
1631 -- and make the previous one invisible.
1633 if Etype (E) = Any_Type then
1634 Set_Is_Immediately_Visible (E, False);
1636 -- Case of renaming declaration constructed for package instances.
1637 -- if there is an explicit declaration with the same identifier,
1638 -- the renaming is not immediately visible any longer, but remains
1639 -- visible through selected component notation.
1641 elsif Nkind (Parent (E)) = N_Package_Renaming_Declaration
1642 and then not Comes_From_Source (E)
1643 then
1644 Set_Is_Immediately_Visible (E, False);
1646 -- The new entity may be the package renaming, which has the same
1647 -- same name as a generic formal which has been seen already.
1649 elsif Nkind (Parent (Def_Id)) = N_Package_Renaming_Declaration
1650 and then not Comes_From_Source (Def_Id)
1651 then
1652 Set_Is_Immediately_Visible (E, False);
1654 -- For a fat pointer corresponding to a remote access to subprogram,
1655 -- we use the same identifier as the RAS type, so that the proper
1656 -- name appears in the stub. This type is only retrieved through
1657 -- the RAS type and never by visibility, and is not added to the
1658 -- visibility list (see below).
1660 elsif Nkind (Parent (Def_Id)) = N_Full_Type_Declaration
1661 and then Present (Corresponding_Remote_Type (Def_Id))
1662 then
1663 null;
1665 -- A controller component for a type extension overrides the
1666 -- inherited component.
1668 elsif Chars (E) = Name_uController then
1669 null;
1671 -- Case of an implicit operation or derived literal. The new entity
1672 -- hides the implicit one, which is removed from all visibility,
1673 -- i.e. the entity list of its scope, and homonym chain of its name.
1675 elsif (Is_Overloadable (E) and then Is_Inherited_Operation (E))
1676 or else Is_Internal (E)
1677 then
1678 declare
1679 Prev : Entity_Id;
1680 Prev_Vis : Entity_Id;
1681 Decl : constant Node_Id := Parent (E);
1683 begin
1684 -- If E is an implicit declaration, it cannot be the first
1685 -- entity in the scope.
1687 Prev := First_Entity (Current_Scope);
1689 while Present (Prev)
1690 and then Next_Entity (Prev) /= E
1691 loop
1692 Next_Entity (Prev);
1693 end loop;
1695 if No (Prev) then
1697 -- If E is not on the entity chain of the current scope,
1698 -- it is an implicit declaration in the generic formal
1699 -- part of a generic subprogram. When analyzing the body,
1700 -- the generic formals are visible but not on the entity
1701 -- chain of the subprogram. The new entity will become
1702 -- the visible one in the body.
1704 pragma Assert
1705 (Nkind (Parent (Decl)) = N_Generic_Subprogram_Declaration);
1706 null;
1708 else
1709 Set_Next_Entity (Prev, Next_Entity (E));
1711 if No (Next_Entity (Prev)) then
1712 Set_Last_Entity (Current_Scope, Prev);
1713 end if;
1715 if E = Current_Entity (E) then
1716 Prev_Vis := Empty;
1718 else
1719 Prev_Vis := Current_Entity (E);
1720 while Homonym (Prev_Vis) /= E loop
1721 Prev_Vis := Homonym (Prev_Vis);
1722 end loop;
1723 end if;
1725 if Present (Prev_Vis) then
1727 -- Skip E in the visibility chain
1729 Set_Homonym (Prev_Vis, Homonym (E));
1731 else
1732 Set_Name_Entity_Id (Chars (E), Homonym (E));
1733 end if;
1734 end if;
1735 end;
1737 -- This section of code could use a comment ???
1739 elsif Present (Etype (E))
1740 and then Is_Concurrent_Type (Etype (E))
1741 and then E = Def_Id
1742 then
1743 return;
1745 -- In the body or private part of an instance, a type extension
1746 -- may introduce a component with the same name as that of an
1747 -- actual. The legality rule is not enforced, but the semantics
1748 -- of the full type with two components of the same name are not
1749 -- clear at this point ???
1751 elsif In_Instance_Not_Visible then
1752 null;
1754 -- When compiling a package body, some child units may have become
1755 -- visible. They cannot conflict with local entities that hide them.
1757 elsif Is_Child_Unit (E)
1758 and then In_Open_Scopes (Scope (E))
1759 and then not Is_Immediately_Visible (E)
1760 then
1761 null;
1763 -- Conversely, with front-end inlining we may compile the parent
1764 -- body first, and a child unit subsequently. The context is now
1765 -- the parent spec, and body entities are not visible.
1767 elsif Is_Child_Unit (Def_Id)
1768 and then Is_Package_Body_Entity (E)
1769 and then not In_Package_Body (Current_Scope)
1770 then
1771 null;
1773 -- Case of genuine duplicate declaration
1775 else
1776 Error_Msg_Sloc := Sloc (E);
1778 -- If the previous declaration is an incomplete type declaration
1779 -- this may be an attempt to complete it with a private type.
1780 -- The following avoids confusing cascaded errors.
1782 if Nkind (Parent (E)) = N_Incomplete_Type_Declaration
1783 and then Nkind (Parent (Def_Id)) = N_Private_Type_Declaration
1784 then
1785 Error_Msg_N
1786 ("incomplete type cannot be completed" &
1787 " with a private declaration",
1788 Parent (Def_Id));
1789 Set_Is_Immediately_Visible (E, False);
1790 Set_Full_View (E, Def_Id);
1792 elsif Ekind (E) = E_Discriminant
1793 and then Present (Scope (Def_Id))
1794 and then Scope (Def_Id) /= Current_Scope
1795 then
1796 -- An inherited component of a record conflicts with
1797 -- a new discriminant. The discriminant is inserted first
1798 -- in the scope, but the error should be posted on it, not
1799 -- on the component.
1801 Error_Msg_Sloc := Sloc (Def_Id);
1802 Error_Msg_N ("& conflicts with declaration#", E);
1803 return;
1805 -- If the name of the unit appears in its own context clause,
1806 -- a dummy package with the name has already been created, and
1807 -- the error emitted. Try to continue quietly.
1809 elsif Error_Posted (E)
1810 and then Sloc (E) = No_Location
1811 and then Nkind (Parent (E)) = N_Package_Specification
1812 and then Current_Scope = Standard_Standard
1813 then
1814 Set_Scope (Def_Id, Current_Scope);
1815 return;
1817 else
1818 Error_Msg_N ("& conflicts with declaration#", Def_Id);
1820 -- Avoid cascaded messages with duplicate components in
1821 -- derived types.
1823 if Ekind (E) = E_Component
1824 or else Ekind (E) = E_Discriminant
1825 then
1826 return;
1827 end if;
1828 end if;
1830 if Nkind (Parent (Parent (Def_Id)))
1831 = N_Generic_Subprogram_Declaration
1832 and then Def_Id =
1833 Defining_Entity (Specification (Parent (Parent (Def_Id))))
1834 then
1835 Error_Msg_N ("\generic units cannot be overloaded", Def_Id);
1836 end if;
1838 -- If entity is in standard, then we are in trouble, because
1839 -- it means that we have a library package with a duplicated
1840 -- name. That's hard to recover from, so abort!
1842 if S = Standard_Standard then
1843 raise Unrecoverable_Error;
1845 -- Otherwise we continue with the declaration. Having two
1846 -- identical declarations should not cause us too much trouble!
1848 else
1849 null;
1850 end if;
1851 end if;
1852 end if;
1854 -- If we fall through, declaration is OK , or OK enough to continue
1856 -- If Def_Id is a discriminant or a record component we are in the
1857 -- midst of inheriting components in a derived record definition.
1858 -- Preserve their Ekind and Etype.
1860 if Ekind (Def_Id) = E_Discriminant
1861 or else Ekind (Def_Id) = E_Component
1862 then
1863 null;
1865 -- If a type is already set, leave it alone (happens whey a type
1866 -- declaration is reanalyzed following a call to the optimizer)
1868 elsif Present (Etype (Def_Id)) then
1869 null;
1871 -- Otherwise, the kind E_Void insures that premature uses of the entity
1872 -- will be detected. Any_Type insures that no cascaded errors will occur
1874 else
1875 Set_Ekind (Def_Id, E_Void);
1876 Set_Etype (Def_Id, Any_Type);
1877 end if;
1879 -- Inherited discriminants and components in derived record types are
1880 -- immediately visible. Itypes are not.
1882 if Ekind (Def_Id) = E_Discriminant
1883 or else Ekind (Def_Id) = E_Component
1884 or else (No (Corresponding_Remote_Type (Def_Id))
1885 and then not Is_Itype (Def_Id))
1886 then
1887 Set_Is_Immediately_Visible (Def_Id);
1888 Set_Current_Entity (Def_Id);
1889 end if;
1891 Set_Homonym (Def_Id, C);
1892 Append_Entity (Def_Id, S);
1893 Set_Public_Status (Def_Id);
1895 -- Warn if new entity hides an old one
1897 if Warn_On_Hiding
1898 and then Present (C)
1899 and then Length_Of_Name (Chars (C)) /= 1
1900 and then Comes_From_Source (C)
1901 and then Comes_From_Source (Def_Id)
1902 and then In_Extended_Main_Source_Unit (Def_Id)
1903 then
1904 Error_Msg_Sloc := Sloc (C);
1905 Error_Msg_N ("declaration hides &#?", Def_Id);
1906 end if;
1907 end Enter_Name;
1909 --------------------------
1910 -- Explain_Limited_Type --
1911 --------------------------
1913 procedure Explain_Limited_Type (T : Entity_Id; N : Node_Id) is
1914 C : Entity_Id;
1916 begin
1917 -- For array, component type must be limited
1919 if Is_Array_Type (T) then
1920 Error_Msg_Node_2 := T;
1921 Error_Msg_NE
1922 ("component type& of type& is limited", N, Component_Type (T));
1923 Explain_Limited_Type (Component_Type (T), N);
1925 elsif Is_Record_Type (T) then
1927 -- No need for extra messages if explicit limited record
1929 if Is_Limited_Record (Base_Type (T)) then
1930 return;
1931 end if;
1933 -- Otherwise find a limited component. Check only components that
1934 -- come from source, or inherited components that appear in the
1935 -- source of the ancestor.
1937 C := First_Component (T);
1938 while Present (C) loop
1939 if Is_Limited_Type (Etype (C))
1940 and then
1941 (Comes_From_Source (C)
1942 or else
1943 (Present (Original_Record_Component (C))
1944 and then
1945 Comes_From_Source (Original_Record_Component (C))))
1946 then
1947 Error_Msg_Node_2 := T;
1948 Error_Msg_NE ("\component& of type& has limited type", N, C);
1949 Explain_Limited_Type (Etype (C), N);
1950 return;
1951 end if;
1953 Next_Component (C);
1954 end loop;
1956 -- The type may be declared explicitly limited, even if no component
1957 -- of it is limited, in which case we fall out of the loop.
1958 return;
1959 end if;
1960 end Explain_Limited_Type;
1962 -------------------------------------
1963 -- Find_Corresponding_Discriminant --
1964 -------------------------------------
1966 function Find_Corresponding_Discriminant
1967 (Id : Node_Id;
1968 Typ : Entity_Id) return Entity_Id
1970 Par_Disc : Entity_Id;
1971 Old_Disc : Entity_Id;
1972 New_Disc : Entity_Id;
1974 begin
1975 Par_Disc := Original_Record_Component (Original_Discriminant (Id));
1977 -- The original type may currently be private, and the discriminant
1978 -- only appear on its full view.
1980 if Is_Private_Type (Scope (Par_Disc))
1981 and then not Has_Discriminants (Scope (Par_Disc))
1982 and then Present (Full_View (Scope (Par_Disc)))
1983 then
1984 Old_Disc := First_Discriminant (Full_View (Scope (Par_Disc)));
1985 else
1986 Old_Disc := First_Discriminant (Scope (Par_Disc));
1987 end if;
1989 if Is_Class_Wide_Type (Typ) then
1990 New_Disc := First_Discriminant (Root_Type (Typ));
1991 else
1992 New_Disc := First_Discriminant (Typ);
1993 end if;
1995 while Present (Old_Disc) and then Present (New_Disc) loop
1996 if Old_Disc = Par_Disc then
1997 return New_Disc;
1998 else
1999 Next_Discriminant (Old_Disc);
2000 Next_Discriminant (New_Disc);
2001 end if;
2002 end loop;
2004 -- Should always find it
2006 raise Program_Error;
2007 end Find_Corresponding_Discriminant;
2009 -----------------------------
2010 -- Find_Static_Alternative --
2011 -----------------------------
2013 function Find_Static_Alternative (N : Node_Id) return Node_Id is
2014 Expr : constant Node_Id := Expression (N);
2015 Val : constant Uint := Expr_Value (Expr);
2016 Alt : Node_Id;
2017 Choice : Node_Id;
2019 begin
2020 Alt := First (Alternatives (N));
2022 Search : loop
2023 if Nkind (Alt) /= N_Pragma then
2024 Choice := First (Discrete_Choices (Alt));
2026 while Present (Choice) loop
2028 -- Others choice, always matches
2030 if Nkind (Choice) = N_Others_Choice then
2031 exit Search;
2033 -- Range, check if value is in the range
2035 elsif Nkind (Choice) = N_Range then
2036 exit Search when
2037 Val >= Expr_Value (Low_Bound (Choice))
2038 and then
2039 Val <= Expr_Value (High_Bound (Choice));
2041 -- Choice is a subtype name. Note that we know it must
2042 -- be a static subtype, since otherwise it would have
2043 -- been diagnosed as illegal.
2045 elsif Is_Entity_Name (Choice)
2046 and then Is_Type (Entity (Choice))
2047 then
2048 exit Search when Is_In_Range (Expr, Etype (Choice));
2050 -- Choice is a subtype indication
2052 elsif Nkind (Choice) = N_Subtype_Indication then
2053 declare
2054 C : constant Node_Id := Constraint (Choice);
2055 R : constant Node_Id := Range_Expression (C);
2057 begin
2058 exit Search when
2059 Val >= Expr_Value (Low_Bound (R))
2060 and then
2061 Val <= Expr_Value (High_Bound (R));
2062 end;
2064 -- Choice is a simple expression
2066 else
2067 exit Search when Val = Expr_Value (Choice);
2068 end if;
2070 Next (Choice);
2071 end loop;
2072 end if;
2074 Next (Alt);
2075 pragma Assert (Present (Alt));
2076 end loop Search;
2078 -- The above loop *must* terminate by finding a match, since
2079 -- we know the case statement is valid, and the value of the
2080 -- expression is known at compile time. When we fall out of
2081 -- the loop, Alt points to the alternative that we know will
2082 -- be selected at run time.
2084 return Alt;
2085 end Find_Static_Alternative;
2087 ------------------
2088 -- First_Actual --
2089 ------------------
2091 function First_Actual (Node : Node_Id) return Node_Id is
2092 N : Node_Id;
2094 begin
2095 if No (Parameter_Associations (Node)) then
2096 return Empty;
2097 end if;
2099 N := First (Parameter_Associations (Node));
2101 if Nkind (N) = N_Parameter_Association then
2102 return First_Named_Actual (Node);
2103 else
2104 return N;
2105 end if;
2106 end First_Actual;
2108 -------------------------
2109 -- Full_Qualified_Name --
2110 -------------------------
2112 function Full_Qualified_Name (E : Entity_Id) return String_Id is
2113 Res : String_Id;
2114 pragma Warnings (Off, Res);
2116 function Internal_Full_Qualified_Name (E : Entity_Id) return String_Id;
2117 -- Compute recursively the qualified name without NUL at the end
2119 ----------------------------------
2120 -- Internal_Full_Qualified_Name --
2121 ----------------------------------
2123 function Internal_Full_Qualified_Name (E : Entity_Id) return String_Id is
2124 Ent : Entity_Id := E;
2125 Parent_Name : String_Id := No_String;
2127 begin
2128 -- Deals properly with child units
2130 if Nkind (Ent) = N_Defining_Program_Unit_Name then
2131 Ent := Defining_Identifier (Ent);
2132 end if;
2134 -- Compute recursively the qualification. Only "Standard" has no
2135 -- scope.
2137 if Present (Scope (Scope (Ent))) then
2138 Parent_Name := Internal_Full_Qualified_Name (Scope (Ent));
2139 end if;
2141 -- Every entity should have a name except some expanded blocks
2142 -- don't bother about those.
2144 if Chars (Ent) = No_Name then
2145 return Parent_Name;
2146 end if;
2148 -- Add a period between Name and qualification
2150 if Parent_Name /= No_String then
2151 Start_String (Parent_Name);
2152 Store_String_Char (Get_Char_Code ('.'));
2154 else
2155 Start_String;
2156 end if;
2158 -- Generates the entity name in upper case
2160 Get_Name_String (Chars (Ent));
2161 Set_All_Upper_Case;
2162 Store_String_Chars (Name_Buffer (1 .. Name_Len));
2163 return End_String;
2164 end Internal_Full_Qualified_Name;
2166 -- Start of processing for Full_Qualified_Name
2168 begin
2169 Res := Internal_Full_Qualified_Name (E);
2170 Store_String_Char (Get_Char_Code (ASCII.nul));
2171 return End_String;
2172 end Full_Qualified_Name;
2174 -----------------------
2175 -- Gather_Components --
2176 -----------------------
2178 procedure Gather_Components
2179 (Typ : Entity_Id;
2180 Comp_List : Node_Id;
2181 Governed_By : List_Id;
2182 Into : Elist_Id;
2183 Report_Errors : out Boolean)
2185 Assoc : Node_Id;
2186 Variant : Node_Id;
2187 Discrete_Choice : Node_Id;
2188 Comp_Item : Node_Id;
2190 Discrim : Entity_Id;
2191 Discrim_Name : Node_Id;
2192 Discrim_Value : Node_Id;
2194 begin
2195 Report_Errors := False;
2197 if No (Comp_List) or else Null_Present (Comp_List) then
2198 return;
2200 elsif Present (Component_Items (Comp_List)) then
2201 Comp_Item := First (Component_Items (Comp_List));
2203 else
2204 Comp_Item := Empty;
2205 end if;
2207 while Present (Comp_Item) loop
2209 -- Skip the tag of a tagged record, as well as all items
2210 -- that are not user components (anonymous types, rep clauses,
2211 -- Parent field, controller field).
2213 if Nkind (Comp_Item) = N_Component_Declaration
2214 and then Chars (Defining_Identifier (Comp_Item)) /= Name_uTag
2215 and then Chars (Defining_Identifier (Comp_Item)) /= Name_uParent
2216 and then Chars (Defining_Identifier (Comp_Item)) /= Name_uController
2217 then
2218 Append_Elmt (Defining_Identifier (Comp_Item), Into);
2219 end if;
2221 Next (Comp_Item);
2222 end loop;
2224 if No (Variant_Part (Comp_List)) then
2225 return;
2226 else
2227 Discrim_Name := Name (Variant_Part (Comp_List));
2228 Variant := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
2229 end if;
2231 -- Look for the discriminant that governs this variant part.
2232 -- The discriminant *must* be in the Governed_By List
2234 Assoc := First (Governed_By);
2235 Find_Constraint : loop
2236 Discrim := First (Choices (Assoc));
2237 exit Find_Constraint when Chars (Discrim_Name) = Chars (Discrim)
2238 or else (Present (Corresponding_Discriminant (Entity (Discrim)))
2239 and then
2240 Chars (Corresponding_Discriminant (Entity (Discrim)))
2241 = Chars (Discrim_Name))
2242 or else Chars (Original_Record_Component (Entity (Discrim)))
2243 = Chars (Discrim_Name);
2245 if No (Next (Assoc)) then
2246 if not Is_Constrained (Typ)
2247 and then Is_Derived_Type (Typ)
2248 and then Present (Stored_Constraint (Typ))
2249 then
2251 -- If the type is a tagged type with inherited discriminants,
2252 -- use the stored constraint on the parent in order to find
2253 -- the values of discriminants that are otherwise hidden by an
2254 -- explicit constraint. Renamed discriminants are handled in
2255 -- the code above.
2257 -- If several parent discriminants are renamed by a single
2258 -- discriminant of the derived type, the call to obtain the
2259 -- Corresponding_Discriminant field only retrieves the last
2260 -- of them. We recover the constraint on the others from the
2261 -- Stored_Constraint as well.
2263 declare
2264 D : Entity_Id;
2265 C : Elmt_Id;
2267 begin
2268 D := First_Discriminant (Etype (Typ));
2269 C := First_Elmt (Stored_Constraint (Typ));
2271 while Present (D)
2272 and then Present (C)
2273 loop
2274 if Chars (Discrim_Name) = Chars (D) then
2275 if Is_Entity_Name (Node (C))
2276 and then Entity (Node (C)) = Entity (Discrim)
2277 then
2278 -- D is renamed by Discrim, whose value is
2279 -- given in Assoc.
2281 null;
2283 else
2284 Assoc :=
2285 Make_Component_Association (Sloc (Typ),
2286 New_List
2287 (New_Occurrence_Of (D, Sloc (Typ))),
2288 Duplicate_Subexpr_No_Checks (Node (C)));
2289 end if;
2290 exit Find_Constraint;
2291 end if;
2293 D := Next_Discriminant (D);
2294 Next_Elmt (C);
2295 end loop;
2296 end;
2297 end if;
2298 end if;
2300 if No (Next (Assoc)) then
2301 Error_Msg_NE (" missing value for discriminant&",
2302 First (Governed_By), Discrim_Name);
2303 Report_Errors := True;
2304 return;
2305 end if;
2307 Next (Assoc);
2308 end loop Find_Constraint;
2310 Discrim_Value := Expression (Assoc);
2312 if not Is_OK_Static_Expression (Discrim_Value) then
2313 Error_Msg_FE
2314 ("value for discriminant & must be static!",
2315 Discrim_Value, Discrim);
2316 Why_Not_Static (Discrim_Value);
2317 Report_Errors := True;
2318 return;
2319 end if;
2321 Search_For_Discriminant_Value : declare
2322 Low : Node_Id;
2323 High : Node_Id;
2325 UI_High : Uint;
2326 UI_Low : Uint;
2327 UI_Discrim_Value : constant Uint := Expr_Value (Discrim_Value);
2329 begin
2330 Find_Discrete_Value : while Present (Variant) loop
2331 Discrete_Choice := First (Discrete_Choices (Variant));
2332 while Present (Discrete_Choice) loop
2334 exit Find_Discrete_Value when
2335 Nkind (Discrete_Choice) = N_Others_Choice;
2337 Get_Index_Bounds (Discrete_Choice, Low, High);
2339 UI_Low := Expr_Value (Low);
2340 UI_High := Expr_Value (High);
2342 exit Find_Discrete_Value when
2343 UI_Low <= UI_Discrim_Value
2344 and then
2345 UI_High >= UI_Discrim_Value;
2347 Next (Discrete_Choice);
2348 end loop;
2350 Next_Non_Pragma (Variant);
2351 end loop Find_Discrete_Value;
2352 end Search_For_Discriminant_Value;
2354 if No (Variant) then
2355 Error_Msg_NE
2356 ("value of discriminant & is out of range", Discrim_Value, Discrim);
2357 Report_Errors := True;
2358 return;
2359 end if;
2361 -- If we have found the corresponding choice, recursively add its
2362 -- components to the Into list.
2364 Gather_Components (Empty,
2365 Component_List (Variant), Governed_By, Into, Report_Errors);
2366 end Gather_Components;
2368 ------------------------
2369 -- Get_Actual_Subtype --
2370 ------------------------
2372 function Get_Actual_Subtype (N : Node_Id) return Entity_Id is
2373 Typ : constant Entity_Id := Etype (N);
2374 Utyp : Entity_Id := Underlying_Type (Typ);
2375 Decl : Node_Id;
2376 Atyp : Entity_Id;
2378 begin
2379 if not Present (Utyp) then
2380 Utyp := Typ;
2381 end if;
2383 -- If what we have is an identifier that references a subprogram
2384 -- formal, or a variable or constant object, then we get the actual
2385 -- subtype from the referenced entity if one has been built.
2387 if Nkind (N) = N_Identifier
2388 and then
2389 (Is_Formal (Entity (N))
2390 or else Ekind (Entity (N)) = E_Constant
2391 or else Ekind (Entity (N)) = E_Variable)
2392 and then Present (Actual_Subtype (Entity (N)))
2393 then
2394 return Actual_Subtype (Entity (N));
2396 -- Actual subtype of unchecked union is always itself. We never need
2397 -- the "real" actual subtype. If we did, we couldn't get it anyway
2398 -- because the discriminant is not available. The restrictions on
2399 -- Unchecked_Union are designed to make sure that this is OK.
2401 elsif Is_Unchecked_Union (Base_Type (Utyp)) then
2402 return Typ;
2404 -- Here for the unconstrained case, we must find actual subtype
2405 -- No actual subtype is available, so we must build it on the fly.
2407 -- Checking the type, not the underlying type, for constrainedness
2408 -- seems to be necessary. Maybe all the tests should be on the type???
2410 elsif (not Is_Constrained (Typ))
2411 and then (Is_Array_Type (Utyp)
2412 or else (Is_Record_Type (Utyp)
2413 and then Has_Discriminants (Utyp)))
2414 and then not Has_Unknown_Discriminants (Utyp)
2415 and then not (Ekind (Utyp) = E_String_Literal_Subtype)
2416 then
2417 -- Nothing to do if in default expression
2419 if In_Default_Expression then
2420 return Typ;
2422 elsif Is_Private_Type (Typ)
2423 and then not Has_Discriminants (Typ)
2424 then
2425 -- If the type has no discriminants, there is no subtype to
2426 -- build, even if the underlying type is discriminated.
2428 return Typ;
2430 -- Else build the actual subtype
2432 else
2433 Decl := Build_Actual_Subtype (Typ, N);
2434 Atyp := Defining_Identifier (Decl);
2436 -- If Build_Actual_Subtype generated a new declaration then use it
2438 if Atyp /= Typ then
2440 -- The actual subtype is an Itype, so analyze the declaration,
2441 -- but do not attach it to the tree, to get the type defined.
2443 Set_Parent (Decl, N);
2444 Set_Is_Itype (Atyp);
2445 Analyze (Decl, Suppress => All_Checks);
2446 Set_Associated_Node_For_Itype (Atyp, N);
2447 Set_Has_Delayed_Freeze (Atyp, False);
2449 -- We need to freeze the actual subtype immediately. This is
2450 -- needed, because otherwise this Itype will not get frozen
2451 -- at all, and it is always safe to freeze on creation because
2452 -- any associated types must be frozen at this point.
2454 Freeze_Itype (Atyp, N);
2455 return Atyp;
2457 -- Otherwise we did not build a declaration, so return original
2459 else
2460 return Typ;
2461 end if;
2462 end if;
2464 -- For all remaining cases, the actual subtype is the same as
2465 -- the nominal type.
2467 else
2468 return Typ;
2469 end if;
2470 end Get_Actual_Subtype;
2472 -------------------------------------
2473 -- Get_Actual_Subtype_If_Available --
2474 -------------------------------------
2476 function Get_Actual_Subtype_If_Available (N : Node_Id) return Entity_Id is
2477 Typ : constant Entity_Id := Etype (N);
2479 begin
2480 -- If what we have is an identifier that references a subprogram
2481 -- formal, or a variable or constant object, then we get the actual
2482 -- subtype from the referenced entity if one has been built.
2484 if Nkind (N) = N_Identifier
2485 and then
2486 (Is_Formal (Entity (N))
2487 or else Ekind (Entity (N)) = E_Constant
2488 or else Ekind (Entity (N)) = E_Variable)
2489 and then Present (Actual_Subtype (Entity (N)))
2490 then
2491 return Actual_Subtype (Entity (N));
2493 -- Otherwise the Etype of N is returned unchanged
2495 else
2496 return Typ;
2497 end if;
2498 end Get_Actual_Subtype_If_Available;
2500 -------------------------------
2501 -- Get_Default_External_Name --
2502 -------------------------------
2504 function Get_Default_External_Name (E : Node_Or_Entity_Id) return Node_Id is
2505 begin
2506 Get_Decoded_Name_String (Chars (E));
2508 if Opt.External_Name_Imp_Casing = Uppercase then
2509 Set_Casing (All_Upper_Case);
2510 else
2511 Set_Casing (All_Lower_Case);
2512 end if;
2514 return
2515 Make_String_Literal (Sloc (E),
2516 Strval => String_From_Name_Buffer);
2517 end Get_Default_External_Name;
2519 ---------------------------
2520 -- Get_Enum_Lit_From_Pos --
2521 ---------------------------
2523 function Get_Enum_Lit_From_Pos
2524 (T : Entity_Id;
2525 Pos : Uint;
2526 Loc : Source_Ptr) return Node_Id
2528 Lit : Node_Id;
2530 begin
2531 -- In the case where the literal is of type Character, Wide_Character
2532 -- or Wide_Wide_Character or of a type derived from them, there needs
2533 -- to be some special handling since there is no explicit chain of
2534 -- literals to search. Instead, an N_Character_Literal node is created
2535 -- with the appropriate Char_Code and Chars fields.
2537 if Root_Type (T) = Standard_Character
2538 or else Root_Type (T) = Standard_Wide_Character
2539 or else Root_Type (T) = Standard_Wide_Wide_Character
2540 then
2541 Set_Character_Literal_Name (UI_To_CC (Pos));
2542 return
2543 Make_Character_Literal (Loc,
2544 Chars => Name_Find,
2545 Char_Literal_Value => Pos);
2547 -- For all other cases, we have a complete table of literals, and
2548 -- we simply iterate through the chain of literal until the one
2549 -- with the desired position value is found.
2552 else
2553 Lit := First_Literal (Base_Type (T));
2554 for J in 1 .. UI_To_Int (Pos) loop
2555 Next_Literal (Lit);
2556 end loop;
2558 return New_Occurrence_Of (Lit, Loc);
2559 end if;
2560 end Get_Enum_Lit_From_Pos;
2562 ------------------------
2563 -- Get_Generic_Entity --
2564 ------------------------
2566 function Get_Generic_Entity (N : Node_Id) return Entity_Id is
2567 Ent : constant Entity_Id := Entity (Name (N));
2568 begin
2569 if Present (Renamed_Object (Ent)) then
2570 return Renamed_Object (Ent);
2571 else
2572 return Ent;
2573 end if;
2574 end Get_Generic_Entity;
2576 ----------------------
2577 -- Get_Index_Bounds --
2578 ----------------------
2580 procedure Get_Index_Bounds (N : Node_Id; L, H : out Node_Id) is
2581 Kind : constant Node_Kind := Nkind (N);
2582 R : Node_Id;
2584 begin
2585 if Kind = N_Range then
2586 L := Low_Bound (N);
2587 H := High_Bound (N);
2589 elsif Kind = N_Subtype_Indication then
2590 R := Range_Expression (Constraint (N));
2592 if R = Error then
2593 L := Error;
2594 H := Error;
2595 return;
2597 else
2598 L := Low_Bound (Range_Expression (Constraint (N)));
2599 H := High_Bound (Range_Expression (Constraint (N)));
2600 end if;
2602 elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
2603 if Error_Posted (Scalar_Range (Entity (N))) then
2604 L := Error;
2605 H := Error;
2607 elsif Nkind (Scalar_Range (Entity (N))) = N_Subtype_Indication then
2608 Get_Index_Bounds (Scalar_Range (Entity (N)), L, H);
2610 else
2611 L := Low_Bound (Scalar_Range (Entity (N)));
2612 H := High_Bound (Scalar_Range (Entity (N)));
2613 end if;
2615 else
2616 -- N is an expression, indicating a range with one value
2618 L := N;
2619 H := N;
2620 end if;
2621 end Get_Index_Bounds;
2623 ----------------------------------
2624 -- Get_Library_Unit_Name_string --
2625 ----------------------------------
2627 procedure Get_Library_Unit_Name_String (Decl_Node : Node_Id) is
2628 Unit_Name_Id : constant Unit_Name_Type := Get_Unit_Name (Decl_Node);
2630 begin
2631 Get_Unit_Name_String (Unit_Name_Id);
2633 -- Remove seven last character (" (spec)" or " (body)")
2635 Name_Len := Name_Len - 7;
2636 pragma Assert (Name_Buffer (Name_Len + 1) = ' ');
2637 end Get_Library_Unit_Name_String;
2639 ------------------------
2640 -- Get_Name_Entity_Id --
2641 ------------------------
2643 function Get_Name_Entity_Id (Id : Name_Id) return Entity_Id is
2644 begin
2645 return Entity_Id (Get_Name_Table_Info (Id));
2646 end Get_Name_Entity_Id;
2648 ---------------------------
2649 -- Get_Referenced_Object --
2650 ---------------------------
2652 function Get_Referenced_Object (N : Node_Id) return Node_Id is
2653 R : Node_Id := N;
2655 begin
2656 while Is_Entity_Name (R)
2657 and then Present (Renamed_Object (Entity (R)))
2658 loop
2659 R := Renamed_Object (Entity (R));
2660 end loop;
2662 return R;
2663 end Get_Referenced_Object;
2665 -------------------------
2666 -- Get_Subprogram_Body --
2667 -------------------------
2669 function Get_Subprogram_Body (E : Entity_Id) return Node_Id is
2670 Decl : Node_Id;
2672 begin
2673 Decl := Unit_Declaration_Node (E);
2675 if Nkind (Decl) = N_Subprogram_Body then
2676 return Decl;
2678 -- The below comment is bad, because it is possible for
2679 -- Nkind (Decl) to be an N_Subprogram_Body_Stub ???
2681 else -- Nkind (Decl) = N_Subprogram_Declaration
2683 if Present (Corresponding_Body (Decl)) then
2684 return Unit_Declaration_Node (Corresponding_Body (Decl));
2686 -- Imported subprogram case
2688 else
2689 return Empty;
2690 end if;
2691 end if;
2692 end Get_Subprogram_Body;
2694 -----------------------------
2695 -- Get_Task_Body_Procedure --
2696 -----------------------------
2698 function Get_Task_Body_Procedure (E : Entity_Id) return Node_Id is
2699 begin
2700 -- Note: A task type may be the completion of a private type with
2701 -- discriminants. when performing elaboration checks on a task
2702 -- declaration, the current view of the type may be the private one,
2703 -- and the procedure that holds the body of the task is held in its
2704 -- underlying type.
2706 return Task_Body_Procedure (Underlying_Type (Root_Type (E)));
2707 end Get_Task_Body_Procedure;
2709 -----------------------
2710 -- Has_Access_Values --
2711 -----------------------
2713 function Has_Access_Values (T : Entity_Id) return Boolean is
2714 Typ : constant Entity_Id := Underlying_Type (T);
2716 begin
2717 -- Case of a private type which is not completed yet. This can only
2718 -- happen in the case of a generic format type appearing directly, or
2719 -- as a component of the type to which this function is being applied
2720 -- at the top level. Return False in this case, since we certainly do
2721 -- not know that the type contains access types.
2723 if No (Typ) then
2724 return False;
2726 elsif Is_Access_Type (Typ) then
2727 return True;
2729 elsif Is_Array_Type (Typ) then
2730 return Has_Access_Values (Component_Type (Typ));
2732 elsif Is_Record_Type (Typ) then
2733 declare
2734 Comp : Entity_Id;
2736 begin
2737 Comp := First_Entity (Typ);
2738 while Present (Comp) loop
2739 if (Ekind (Comp) = E_Component
2740 or else
2741 Ekind (Comp) = E_Discriminant)
2742 and then Has_Access_Values (Etype (Comp))
2743 then
2744 return True;
2745 end if;
2747 Next_Entity (Comp);
2748 end loop;
2749 end;
2751 return False;
2753 else
2754 return False;
2755 end if;
2756 end Has_Access_Values;
2758 ----------------------
2759 -- Has_Declarations --
2760 ----------------------
2762 function Has_Declarations (N : Node_Id) return Boolean is
2763 K : constant Node_Kind := Nkind (N);
2764 begin
2765 return K = N_Accept_Statement
2766 or else K = N_Block_Statement
2767 or else K = N_Compilation_Unit_Aux
2768 or else K = N_Entry_Body
2769 or else K = N_Package_Body
2770 or else K = N_Protected_Body
2771 or else K = N_Subprogram_Body
2772 or else K = N_Task_Body
2773 or else K = N_Package_Specification;
2774 end Has_Declarations;
2776 --------------------
2777 -- Has_Infinities --
2778 --------------------
2780 function Has_Infinities (E : Entity_Id) return Boolean is
2781 begin
2782 return
2783 Is_Floating_Point_Type (E)
2784 and then Nkind (Scalar_Range (E)) = N_Range
2785 and then Includes_Infinities (Scalar_Range (E));
2786 end Has_Infinities;
2788 ------------------------
2789 -- Has_Null_Extension --
2790 ------------------------
2792 function Has_Null_Extension (T : Entity_Id) return Boolean is
2793 B : constant Entity_Id := Base_Type (T);
2794 Comps : Node_Id;
2795 Ext : Node_Id;
2797 begin
2798 if Nkind (Parent (B)) = N_Full_Type_Declaration
2799 and then Present (Record_Extension_Part (Type_Definition (Parent (B))))
2800 then
2801 Ext := Record_Extension_Part (Type_Definition (Parent (B)));
2803 if Present (Ext) then
2804 if Null_Present (Ext) then
2805 return True;
2806 else
2807 Comps := Component_List (Ext);
2809 -- The null component list is rewritten during analysis to
2810 -- include the parent component. Any other component indicates
2811 -- that the extension was not originally null.
2813 return Null_Present (Comps)
2814 or else No (Next (First (Component_Items (Comps))));
2815 end if;
2816 else
2817 return False;
2818 end if;
2820 else
2821 return False;
2822 end if;
2823 end Has_Null_Extension;
2825 ---------------------------
2826 -- Has_Private_Component --
2827 ---------------------------
2829 function Has_Private_Component (Type_Id : Entity_Id) return Boolean is
2830 Btype : Entity_Id := Base_Type (Type_Id);
2831 Component : Entity_Id;
2833 begin
2834 if Error_Posted (Type_Id)
2835 or else Error_Posted (Btype)
2836 then
2837 return False;
2838 end if;
2840 if Is_Class_Wide_Type (Btype) then
2841 Btype := Root_Type (Btype);
2842 end if;
2844 if Is_Private_Type (Btype) then
2845 declare
2846 UT : constant Entity_Id := Underlying_Type (Btype);
2847 begin
2848 if No (UT) then
2850 if No (Full_View (Btype)) then
2851 return not Is_Generic_Type (Btype)
2852 and then not Is_Generic_Type (Root_Type (Btype));
2854 else
2855 return not Is_Generic_Type (Root_Type (Full_View (Btype)));
2856 end if;
2858 else
2859 return not Is_Frozen (UT) and then Has_Private_Component (UT);
2860 end if;
2861 end;
2862 elsif Is_Array_Type (Btype) then
2863 return Has_Private_Component (Component_Type (Btype));
2865 elsif Is_Record_Type (Btype) then
2867 Component := First_Component (Btype);
2868 while Present (Component) loop
2870 if Has_Private_Component (Etype (Component)) then
2871 return True;
2872 end if;
2874 Next_Component (Component);
2875 end loop;
2877 return False;
2879 elsif Is_Protected_Type (Btype)
2880 and then Present (Corresponding_Record_Type (Btype))
2881 then
2882 return Has_Private_Component (Corresponding_Record_Type (Btype));
2884 else
2885 return False;
2886 end if;
2887 end Has_Private_Component;
2889 ----------------
2890 -- Has_Stream --
2891 ----------------
2893 function Has_Stream (T : Entity_Id) return Boolean is
2894 E : Entity_Id;
2896 begin
2897 if No (T) then
2898 return False;
2900 elsif Is_RTE (Root_Type (T), RE_Root_Stream_Type) then
2901 return True;
2903 elsif Is_Array_Type (T) then
2904 return Has_Stream (Component_Type (T));
2906 elsif Is_Record_Type (T) then
2907 E := First_Component (T);
2908 while Present (E) loop
2909 if Has_Stream (Etype (E)) then
2910 return True;
2911 else
2912 Next_Component (E);
2913 end if;
2914 end loop;
2916 return False;
2918 elsif Is_Private_Type (T) then
2919 return Has_Stream (Underlying_Type (T));
2921 else
2922 return False;
2923 end if;
2924 end Has_Stream;
2926 --------------------------
2927 -- Has_Tagged_Component --
2928 --------------------------
2930 function Has_Tagged_Component (Typ : Entity_Id) return Boolean is
2931 Comp : Entity_Id;
2933 begin
2934 if Is_Private_Type (Typ)
2935 and then Present (Underlying_Type (Typ))
2936 then
2937 return Has_Tagged_Component (Underlying_Type (Typ));
2939 elsif Is_Array_Type (Typ) then
2940 return Has_Tagged_Component (Component_Type (Typ));
2942 elsif Is_Tagged_Type (Typ) then
2943 return True;
2945 elsif Is_Record_Type (Typ) then
2946 Comp := First_Component (Typ);
2948 while Present (Comp) loop
2949 if Has_Tagged_Component (Etype (Comp)) then
2950 return True;
2951 end if;
2953 Comp := Next_Component (Typ);
2954 end loop;
2956 return False;
2958 else
2959 return False;
2960 end if;
2961 end Has_Tagged_Component;
2963 -----------------
2964 -- In_Instance --
2965 -----------------
2967 function In_Instance return Boolean is
2968 S : Entity_Id := Current_Scope;
2970 begin
2971 while Present (S)
2972 and then S /= Standard_Standard
2973 loop
2974 if (Ekind (S) = E_Function
2975 or else Ekind (S) = E_Package
2976 or else Ekind (S) = E_Procedure)
2977 and then Is_Generic_Instance (S)
2978 then
2979 return True;
2980 end if;
2982 S := Scope (S);
2983 end loop;
2985 return False;
2986 end In_Instance;
2988 ----------------------
2989 -- In_Instance_Body --
2990 ----------------------
2992 function In_Instance_Body return Boolean is
2993 S : Entity_Id := Current_Scope;
2995 begin
2996 while Present (S)
2997 and then S /= Standard_Standard
2998 loop
2999 if (Ekind (S) = E_Function
3000 or else Ekind (S) = E_Procedure)
3001 and then Is_Generic_Instance (S)
3002 then
3003 return True;
3005 elsif Ekind (S) = E_Package
3006 and then In_Package_Body (S)
3007 and then Is_Generic_Instance (S)
3008 then
3009 return True;
3010 end if;
3012 S := Scope (S);
3013 end loop;
3015 return False;
3016 end In_Instance_Body;
3018 -----------------------------
3019 -- In_Instance_Not_Visible --
3020 -----------------------------
3022 function In_Instance_Not_Visible return Boolean is
3023 S : Entity_Id := Current_Scope;
3025 begin
3026 while Present (S)
3027 and then S /= Standard_Standard
3028 loop
3029 if (Ekind (S) = E_Function
3030 or else Ekind (S) = E_Procedure)
3031 and then Is_Generic_Instance (S)
3032 then
3033 return True;
3035 elsif Ekind (S) = E_Package
3036 and then (In_Package_Body (S) or else In_Private_Part (S))
3037 and then Is_Generic_Instance (S)
3038 then
3039 return True;
3040 end if;
3042 S := Scope (S);
3043 end loop;
3045 return False;
3046 end In_Instance_Not_Visible;
3048 ------------------------------
3049 -- In_Instance_Visible_Part --
3050 ------------------------------
3052 function In_Instance_Visible_Part return Boolean is
3053 S : Entity_Id := Current_Scope;
3055 begin
3056 while Present (S)
3057 and then S /= Standard_Standard
3058 loop
3059 if Ekind (S) = E_Package
3060 and then Is_Generic_Instance (S)
3061 and then not In_Package_Body (S)
3062 and then not In_Private_Part (S)
3063 then
3064 return True;
3065 end if;
3067 S := Scope (S);
3068 end loop;
3070 return False;
3071 end In_Instance_Visible_Part;
3073 ----------------------
3074 -- In_Packiage_Body --
3075 ----------------------
3077 function In_Package_Body return Boolean is
3078 S : Entity_Id := Current_Scope;
3080 begin
3081 while Present (S)
3082 and then S /= Standard_Standard
3083 loop
3084 if Ekind (S) = E_Package
3085 and then In_Package_Body (S)
3086 then
3087 return True;
3088 else
3089 S := Scope (S);
3090 end if;
3091 end loop;
3093 return False;
3094 end In_Package_Body;
3096 --------------------------------------
3097 -- In_Subprogram_Or_Concurrent_Unit --
3098 --------------------------------------
3100 function In_Subprogram_Or_Concurrent_Unit return Boolean is
3101 E : Entity_Id;
3102 K : Entity_Kind;
3104 begin
3105 -- Use scope chain to check successively outer scopes
3107 E := Current_Scope;
3108 loop
3109 K := Ekind (E);
3111 if K in Subprogram_Kind
3112 or else K in Concurrent_Kind
3113 or else K in Generic_Subprogram_Kind
3114 then
3115 return True;
3117 elsif E = Standard_Standard then
3118 return False;
3119 end if;
3121 E := Scope (E);
3122 end loop;
3123 end In_Subprogram_Or_Concurrent_Unit;
3125 ---------------------
3126 -- In_Visible_Part --
3127 ---------------------
3129 function In_Visible_Part (Scope_Id : Entity_Id) return Boolean is
3130 begin
3131 return
3132 Is_Package (Scope_Id)
3133 and then In_Open_Scopes (Scope_Id)
3134 and then not In_Package_Body (Scope_Id)
3135 and then not In_Private_Part (Scope_Id);
3136 end In_Visible_Part;
3138 ---------------------------------
3139 -- Insert_Explicit_Dereference --
3140 ---------------------------------
3142 procedure Insert_Explicit_Dereference (N : Node_Id) is
3143 New_Prefix : constant Node_Id := Relocate_Node (N);
3144 Ent : Entity_Id := Empty;
3145 Pref : Node_Id;
3146 I : Interp_Index;
3147 It : Interp;
3148 T : Entity_Id;
3150 begin
3151 Save_Interps (N, New_Prefix);
3152 Rewrite (N,
3153 Make_Explicit_Dereference (Sloc (N), Prefix => New_Prefix));
3155 Set_Etype (N, Designated_Type (Etype (New_Prefix)));
3157 if Is_Overloaded (New_Prefix) then
3159 -- The deference is also overloaded, and its interpretations are the
3160 -- designated types of the interpretations of the original node.
3162 Set_Etype (N, Any_Type);
3163 Get_First_Interp (New_Prefix, I, It);
3165 while Present (It.Nam) loop
3166 T := It.Typ;
3168 if Is_Access_Type (T) then
3169 Add_One_Interp (N, Designated_Type (T), Designated_Type (T));
3170 end if;
3172 Get_Next_Interp (I, It);
3173 end loop;
3175 End_Interp_List;
3177 else
3178 -- Prefix is unambiguous: mark the original prefix (which might
3179 -- Come_From_Source) as a reference, since the new (relocated) one
3180 -- won't be taken into account.
3182 if Is_Entity_Name (New_Prefix) then
3183 Ent := Entity (New_Prefix);
3185 -- For a retrieval of a subcomponent of some composite object,
3186 -- retrieve the ultimate entity if there is one.
3188 elsif Nkind (New_Prefix) = N_Selected_Component
3189 or else Nkind (New_Prefix) = N_Indexed_Component
3190 then
3191 Pref := Prefix (New_Prefix);
3193 while Present (Pref)
3194 and then
3195 (Nkind (Pref) = N_Selected_Component
3196 or else Nkind (Pref) = N_Indexed_Component)
3197 loop
3198 Pref := Prefix (Pref);
3199 end loop;
3201 if Present (Pref) and then Is_Entity_Name (Pref) then
3202 Ent := Entity (Pref);
3203 end if;
3204 end if;
3206 if Present (Ent) then
3207 Generate_Reference (Ent, New_Prefix);
3208 end if;
3209 end if;
3210 end Insert_Explicit_Dereference;
3212 -------------------
3213 -- Is_AAMP_Float --
3214 -------------------
3216 function Is_AAMP_Float (E : Entity_Id) return Boolean is
3217 begin
3218 pragma Assert (Is_Type (E));
3220 return AAMP_On_Target
3221 and then Is_Floating_Point_Type (E)
3222 and then E = Base_Type (E);
3223 end Is_AAMP_Float;
3225 -------------------------
3226 -- Is_Actual_Parameter --
3227 -------------------------
3229 function Is_Actual_Parameter (N : Node_Id) return Boolean is
3230 PK : constant Node_Kind := Nkind (Parent (N));
3232 begin
3233 case PK is
3234 when N_Parameter_Association =>
3235 return N = Explicit_Actual_Parameter (Parent (N));
3237 when N_Function_Call | N_Procedure_Call_Statement =>
3238 return Is_List_Member (N)
3239 and then
3240 List_Containing (N) = Parameter_Associations (Parent (N));
3242 when others =>
3243 return False;
3244 end case;
3245 end Is_Actual_Parameter;
3247 ---------------------
3248 -- Is_Aliased_View --
3249 ---------------------
3251 function Is_Aliased_View (Obj : Node_Id) return Boolean is
3252 E : Entity_Id;
3254 begin
3255 if Is_Entity_Name (Obj) then
3257 E := Entity (Obj);
3259 return
3260 (Is_Object (E)
3261 and then
3262 (Is_Aliased (E)
3263 or else (Present (Renamed_Object (E))
3264 and then Is_Aliased_View (Renamed_Object (E)))))
3266 or else ((Is_Formal (E)
3267 or else Ekind (E) = E_Generic_In_Out_Parameter
3268 or else Ekind (E) = E_Generic_In_Parameter)
3269 and then Is_Tagged_Type (Etype (E)))
3271 or else ((Ekind (E) = E_Task_Type
3272 or else Ekind (E) = E_Protected_Type)
3273 and then In_Open_Scopes (E))
3275 -- Current instance of type
3277 or else (Is_Type (E) and then E = Current_Scope)
3278 or else (Is_Incomplete_Or_Private_Type (E)
3279 and then Full_View (E) = Current_Scope);
3281 elsif Nkind (Obj) = N_Selected_Component then
3282 return Is_Aliased (Entity (Selector_Name (Obj)));
3284 elsif Nkind (Obj) = N_Indexed_Component then
3285 return Has_Aliased_Components (Etype (Prefix (Obj)))
3286 or else
3287 (Is_Access_Type (Etype (Prefix (Obj)))
3288 and then
3289 Has_Aliased_Components
3290 (Designated_Type (Etype (Prefix (Obj)))));
3292 elsif Nkind (Obj) = N_Unchecked_Type_Conversion
3293 or else Nkind (Obj) = N_Type_Conversion
3294 then
3295 return Is_Tagged_Type (Etype (Obj))
3296 and then Is_Aliased_View (Expression (Obj));
3298 elsif Nkind (Obj) = N_Explicit_Dereference then
3299 return Nkind (Original_Node (Obj)) /= N_Function_Call;
3301 else
3302 return False;
3303 end if;
3304 end Is_Aliased_View;
3306 -------------------------
3307 -- Is_Ancestor_Package --
3308 -------------------------
3310 function Is_Ancestor_Package
3311 (E1 : Entity_Id;
3312 E2 : Entity_Id) return Boolean
3314 Par : Entity_Id;
3316 begin
3317 Par := E2;
3318 while Present (Par)
3319 and then Par /= Standard_Standard
3320 loop
3321 if Par = E1 then
3322 return True;
3323 end if;
3325 Par := Scope (Par);
3326 end loop;
3328 return False;
3329 end Is_Ancestor_Package;
3331 ----------------------
3332 -- Is_Atomic_Object --
3333 ----------------------
3335 function Is_Atomic_Object (N : Node_Id) return Boolean is
3337 function Object_Has_Atomic_Components (N : Node_Id) return Boolean;
3338 -- Determines if given object has atomic components
3340 function Is_Atomic_Prefix (N : Node_Id) return Boolean;
3341 -- If prefix is an implicit dereference, examine designated type
3343 function Is_Atomic_Prefix (N : Node_Id) return Boolean is
3344 begin
3345 if Is_Access_Type (Etype (N)) then
3346 return
3347 Has_Atomic_Components (Designated_Type (Etype (N)));
3348 else
3349 return Object_Has_Atomic_Components (N);
3350 end if;
3351 end Is_Atomic_Prefix;
3353 function Object_Has_Atomic_Components (N : Node_Id) return Boolean is
3354 begin
3355 if Has_Atomic_Components (Etype (N))
3356 or else Is_Atomic (Etype (N))
3357 then
3358 return True;
3360 elsif Is_Entity_Name (N)
3361 and then (Has_Atomic_Components (Entity (N))
3362 or else Is_Atomic (Entity (N)))
3363 then
3364 return True;
3366 elsif Nkind (N) = N_Indexed_Component
3367 or else Nkind (N) = N_Selected_Component
3368 then
3369 return Is_Atomic_Prefix (Prefix (N));
3371 else
3372 return False;
3373 end if;
3374 end Object_Has_Atomic_Components;
3376 -- Start of processing for Is_Atomic_Object
3378 begin
3379 if Is_Atomic (Etype (N))
3380 or else (Is_Entity_Name (N) and then Is_Atomic (Entity (N)))
3381 then
3382 return True;
3384 elsif Nkind (N) = N_Indexed_Component
3385 or else Nkind (N) = N_Selected_Component
3386 then
3387 return Is_Atomic_Prefix (Prefix (N));
3389 else
3390 return False;
3391 end if;
3392 end Is_Atomic_Object;
3394 ----------------------------------------------
3395 -- Is_Dependent_Component_Of_Mutable_Object --
3396 ----------------------------------------------
3398 function Is_Dependent_Component_Of_Mutable_Object
3399 (Object : Node_Id) return Boolean
3401 P : Node_Id;
3402 Prefix_Type : Entity_Id;
3403 P_Aliased : Boolean := False;
3404 Comp : Entity_Id;
3406 function Has_Dependent_Constraint (Comp : Entity_Id) return Boolean;
3407 -- Returns True if and only if Comp has a constrained subtype
3408 -- that depends on a discriminant.
3410 function Is_Declared_Within_Variant (Comp : Entity_Id) return Boolean;
3411 -- Returns True if and only if Comp is declared within a variant part
3413 ------------------------------
3414 -- Has_Dependent_Constraint --
3415 ------------------------------
3417 function Has_Dependent_Constraint (Comp : Entity_Id) return Boolean is
3418 Comp_Decl : constant Node_Id := Parent (Comp);
3419 Subt_Indic : constant Node_Id :=
3420 Subtype_Indication (Component_Definition (Comp_Decl));
3421 Constr : Node_Id;
3422 Assn : Node_Id;
3424 begin
3425 if Nkind (Subt_Indic) = N_Subtype_Indication then
3426 Constr := Constraint (Subt_Indic);
3428 if Nkind (Constr) = N_Index_Or_Discriminant_Constraint then
3429 Assn := First (Constraints (Constr));
3430 while Present (Assn) loop
3431 case Nkind (Assn) is
3432 when N_Subtype_Indication |
3433 N_Range |
3434 N_Identifier
3436 if Depends_On_Discriminant (Assn) then
3437 return True;
3438 end if;
3440 when N_Discriminant_Association =>
3441 if Depends_On_Discriminant (Expression (Assn)) then
3442 return True;
3443 end if;
3445 when others =>
3446 null;
3448 end case;
3450 Next (Assn);
3451 end loop;
3452 end if;
3453 end if;
3455 return False;
3456 end Has_Dependent_Constraint;
3458 --------------------------------
3459 -- Is_Declared_Within_Variant --
3460 --------------------------------
3462 function Is_Declared_Within_Variant (Comp : Entity_Id) return Boolean is
3463 Comp_Decl : constant Node_Id := Parent (Comp);
3464 Comp_List : constant Node_Id := Parent (Comp_Decl);
3466 begin
3467 return Nkind (Parent (Comp_List)) = N_Variant;
3468 end Is_Declared_Within_Variant;
3470 -- Start of processing for Is_Dependent_Component_Of_Mutable_Object
3472 begin
3473 if Is_Variable (Object) then
3475 if Nkind (Object) = N_Selected_Component then
3476 P := Prefix (Object);
3477 Prefix_Type := Etype (P);
3479 if Is_Entity_Name (P) then
3481 if Ekind (Entity (P)) = E_Generic_In_Out_Parameter then
3482 Prefix_Type := Base_Type (Prefix_Type);
3483 end if;
3485 if Is_Aliased (Entity (P)) then
3486 P_Aliased := True;
3487 end if;
3489 -- A discriminant check on a selected component may be
3490 -- expanded into a dereference when removing side-effects.
3491 -- Recover the original node and its type, which may be
3492 -- unconstrained.
3494 elsif Nkind (P) = N_Explicit_Dereference
3495 and then not (Comes_From_Source (P))
3496 then
3497 P := Original_Node (P);
3498 Prefix_Type := Etype (P);
3500 else
3501 -- Check for prefix being an aliased component ???
3502 null;
3504 end if;
3506 if Is_Access_Type (Prefix_Type)
3507 or else Nkind (P) = N_Explicit_Dereference
3508 then
3509 return False;
3510 end if;
3512 Comp :=
3513 Original_Record_Component (Entity (Selector_Name (Object)));
3515 -- As per AI-0017, the renaming is illegal in a generic body,
3516 -- even if the subtype is indefinite.
3518 if not Is_Constrained (Prefix_Type)
3519 and then (not Is_Indefinite_Subtype (Prefix_Type)
3520 or else
3521 (Is_Generic_Type (Prefix_Type)
3522 and then Ekind (Current_Scope) = E_Generic_Package
3523 and then In_Package_Body (Current_Scope)))
3525 and then (Is_Declared_Within_Variant (Comp)
3526 or else Has_Dependent_Constraint (Comp))
3527 and then not P_Aliased
3528 then
3529 return True;
3531 else
3532 return
3533 Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
3535 end if;
3537 elsif Nkind (Object) = N_Indexed_Component
3538 or else Nkind (Object) = N_Slice
3539 then
3540 return Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
3542 -- A type conversion that Is_Variable is a view conversion:
3543 -- go back to the denoted object.
3545 elsif Nkind (Object) = N_Type_Conversion then
3546 return
3547 Is_Dependent_Component_Of_Mutable_Object (Expression (Object));
3548 end if;
3549 end if;
3551 return False;
3552 end Is_Dependent_Component_Of_Mutable_Object;
3554 ---------------------
3555 -- Is_Dereferenced --
3556 ---------------------
3558 function Is_Dereferenced (N : Node_Id) return Boolean is
3559 P : constant Node_Id := Parent (N);
3560 begin
3561 return
3562 (Nkind (P) = N_Selected_Component
3563 or else
3564 Nkind (P) = N_Explicit_Dereference
3565 or else
3566 Nkind (P) = N_Indexed_Component
3567 or else
3568 Nkind (P) = N_Slice)
3569 and then Prefix (P) = N;
3570 end Is_Dereferenced;
3572 ----------------------
3573 -- Is_Descendent_Of --
3574 ----------------------
3576 function Is_Descendent_Of (T1 : Entity_Id; T2 : Entity_Id) return Boolean is
3577 T : Entity_Id;
3578 Etyp : Entity_Id;
3580 begin
3581 pragma Assert (Nkind (T1) in N_Entity);
3582 pragma Assert (Nkind (T2) in N_Entity);
3584 T := Base_Type (T1);
3586 -- Immediate return if the types match
3588 if T = T2 then
3589 return True;
3591 -- Comment needed here ???
3593 elsif Ekind (T) = E_Class_Wide_Type then
3594 return Etype (T) = T2;
3596 -- All other cases
3598 else
3599 loop
3600 Etyp := Etype (T);
3602 -- Done if we found the type we are looking for
3604 if Etyp = T2 then
3605 return True;
3607 -- Done if no more derivations to check
3609 elsif T = T1
3610 or else T = Etyp
3611 then
3612 return False;
3614 -- Following test catches error cases resulting from prev errors
3616 elsif No (Etyp) then
3617 return False;
3619 elsif Is_Private_Type (T) and then Etyp = Full_View (T) then
3620 return False;
3622 elsif Is_Private_Type (Etyp) and then Full_View (Etyp) = T then
3623 return False;
3624 end if;
3626 T := Base_Type (Etyp);
3627 end loop;
3628 end if;
3630 raise Program_Error;
3631 end Is_Descendent_Of;
3633 ------------------------------
3634 -- Is_Descendent_Of_Address --
3635 ------------------------------
3637 function Is_Descendent_Of_Address (T1 : Entity_Id) return Boolean is
3638 begin
3639 -- If Address has not been loaded, answer must be False
3641 if not RTU_Loaded (System) then
3642 return False;
3644 -- Otherwise we can get the entity we are interested in without
3645 -- causing an unwanted dependency on System, and do the test.
3647 else
3648 return Is_Descendent_Of (T1, Base_Type (RTE (RE_Address)));
3649 end if;
3650 end Is_Descendent_Of_Address;
3652 --------------
3653 -- Is_False --
3654 --------------
3656 function Is_False (U : Uint) return Boolean is
3657 begin
3658 return (U = 0);
3659 end Is_False;
3661 ---------------------------
3662 -- Is_Fixed_Model_Number --
3663 ---------------------------
3665 function Is_Fixed_Model_Number (U : Ureal; T : Entity_Id) return Boolean is
3666 S : constant Ureal := Small_Value (T);
3667 M : Urealp.Save_Mark;
3668 R : Boolean;
3670 begin
3671 M := Urealp.Mark;
3672 R := (U = UR_Trunc (U / S) * S);
3673 Urealp.Release (M);
3674 return R;
3675 end Is_Fixed_Model_Number;
3677 -------------------------------
3678 -- Is_Fully_Initialized_Type --
3679 -------------------------------
3681 function Is_Fully_Initialized_Type (Typ : Entity_Id) return Boolean is
3682 begin
3683 if Is_Scalar_Type (Typ) then
3684 return False;
3686 elsif Is_Access_Type (Typ) then
3687 return True;
3689 elsif Is_Array_Type (Typ) then
3690 if Is_Fully_Initialized_Type (Component_Type (Typ)) then
3691 return True;
3692 end if;
3694 -- An interesting case, if we have a constrained type one of whose
3695 -- bounds is known to be null, then there are no elements to be
3696 -- initialized, so all the elements are initialized!
3698 if Is_Constrained (Typ) then
3699 declare
3700 Indx : Node_Id;
3701 Indx_Typ : Entity_Id;
3702 Lbd, Hbd : Node_Id;
3704 begin
3705 Indx := First_Index (Typ);
3706 while Present (Indx) loop
3708 if Etype (Indx) = Any_Type then
3709 return False;
3711 -- If index is a range, use directly
3713 elsif Nkind (Indx) = N_Range then
3714 Lbd := Low_Bound (Indx);
3715 Hbd := High_Bound (Indx);
3717 else
3718 Indx_Typ := Etype (Indx);
3720 if Is_Private_Type (Indx_Typ) then
3721 Indx_Typ := Full_View (Indx_Typ);
3722 end if;
3724 if No (Indx_Typ) then
3725 return False;
3726 else
3727 Lbd := Type_Low_Bound (Indx_Typ);
3728 Hbd := Type_High_Bound (Indx_Typ);
3729 end if;
3730 end if;
3732 if Compile_Time_Known_Value (Lbd)
3733 and then Compile_Time_Known_Value (Hbd)
3734 then
3735 if Expr_Value (Hbd) < Expr_Value (Lbd) then
3736 return True;
3737 end if;
3738 end if;
3740 Next_Index (Indx);
3741 end loop;
3742 end;
3743 end if;
3745 -- If no null indexes, then type is not fully initialized
3747 return False;
3749 -- Record types
3751 elsif Is_Record_Type (Typ) then
3752 if Has_Discriminants (Typ)
3753 and then
3754 Present (Discriminant_Default_Value (First_Discriminant (Typ)))
3755 and then Is_Fully_Initialized_Variant (Typ)
3756 then
3757 return True;
3758 end if;
3760 -- Controlled records are considered to be fully initialized if
3761 -- there is a user defined Initialize routine. This may not be
3762 -- entirely correct, but as the spec notes, we are guessing here
3763 -- what is best from the point of view of issuing warnings.
3765 if Is_Controlled (Typ) then
3766 declare
3767 Utyp : constant Entity_Id := Underlying_Type (Typ);
3769 begin
3770 if Present (Utyp) then
3771 declare
3772 Init : constant Entity_Id :=
3773 (Find_Prim_Op
3774 (Underlying_Type (Typ), Name_Initialize));
3776 begin
3777 if Present (Init)
3778 and then Comes_From_Source (Init)
3779 and then not
3780 Is_Predefined_File_Name
3781 (File_Name (Get_Source_File_Index (Sloc (Init))))
3782 then
3783 return True;
3785 elsif Has_Null_Extension (Typ)
3786 and then
3787 Is_Fully_Initialized_Type
3788 (Etype (Base_Type (Typ)))
3789 then
3790 return True;
3791 end if;
3792 end;
3793 end if;
3794 end;
3795 end if;
3797 -- Otherwise see if all record components are initialized
3799 declare
3800 Ent : Entity_Id;
3802 begin
3803 Ent := First_Entity (Typ);
3805 while Present (Ent) loop
3806 if Chars (Ent) = Name_uController then
3807 null;
3809 elsif Ekind (Ent) = E_Component
3810 and then (No (Parent (Ent))
3811 or else No (Expression (Parent (Ent))))
3812 and then not Is_Fully_Initialized_Type (Etype (Ent))
3813 then
3814 return False;
3815 end if;
3817 Next_Entity (Ent);
3818 end loop;
3819 end;
3821 -- No uninitialized components, so type is fully initialized.
3822 -- Note that this catches the case of no components as well.
3824 return True;
3826 elsif Is_Concurrent_Type (Typ) then
3827 return True;
3829 elsif Is_Private_Type (Typ) then
3830 declare
3831 U : constant Entity_Id := Underlying_Type (Typ);
3833 begin
3834 if No (U) then
3835 return False;
3836 else
3837 return Is_Fully_Initialized_Type (U);
3838 end if;
3839 end;
3841 else
3842 return False;
3843 end if;
3844 end Is_Fully_Initialized_Type;
3846 ----------------------------------
3847 -- Is_Fully_Initialized_Variant --
3848 ----------------------------------
3850 function Is_Fully_Initialized_Variant (Typ : Entity_Id) return Boolean is
3851 Loc : constant Source_Ptr := Sloc (Typ);
3852 Constraints : constant List_Id := New_List;
3853 Components : constant Elist_Id := New_Elmt_List;
3854 Comp_Elmt : Elmt_Id;
3855 Comp_Id : Node_Id;
3856 Comp_List : Node_Id;
3857 Discr : Entity_Id;
3858 Discr_Val : Node_Id;
3859 Report_Errors : Boolean;
3861 begin
3862 if Serious_Errors_Detected > 0 then
3863 return False;
3864 end if;
3866 if Is_Record_Type (Typ)
3867 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
3868 and then Nkind (Type_Definition (Parent (Typ))) = N_Record_Definition
3869 then
3870 Comp_List := Component_List (Type_Definition (Parent (Typ)));
3871 Discr := First_Discriminant (Typ);
3873 while Present (Discr) loop
3874 if Nkind (Parent (Discr)) = N_Discriminant_Specification then
3875 Discr_Val := Expression (Parent (Discr));
3877 if Present (Discr_Val)
3878 and then Is_OK_Static_Expression (Discr_Val)
3879 then
3880 Append_To (Constraints,
3881 Make_Component_Association (Loc,
3882 Choices => New_List (New_Occurrence_Of (Discr, Loc)),
3883 Expression => New_Copy (Discr_Val)));
3884 else
3885 return False;
3886 end if;
3887 else
3888 return False;
3889 end if;
3891 Next_Discriminant (Discr);
3892 end loop;
3894 Gather_Components
3895 (Typ => Typ,
3896 Comp_List => Comp_List,
3897 Governed_By => Constraints,
3898 Into => Components,
3899 Report_Errors => Report_Errors);
3901 -- Check that each component present is fully initialized
3903 Comp_Elmt := First_Elmt (Components);
3905 while Present (Comp_Elmt) loop
3906 Comp_Id := Node (Comp_Elmt);
3908 if Ekind (Comp_Id) = E_Component
3909 and then (No (Parent (Comp_Id))
3910 or else No (Expression (Parent (Comp_Id))))
3911 and then not Is_Fully_Initialized_Type (Etype (Comp_Id))
3912 then
3913 return False;
3914 end if;
3916 Next_Elmt (Comp_Elmt);
3917 end loop;
3919 return True;
3921 elsif Is_Private_Type (Typ) then
3922 declare
3923 U : constant Entity_Id := Underlying_Type (Typ);
3925 begin
3926 if No (U) then
3927 return False;
3928 else
3929 return Is_Fully_Initialized_Variant (U);
3930 end if;
3931 end;
3932 else
3933 return False;
3934 end if;
3935 end Is_Fully_Initialized_Variant;
3937 ----------------------------
3938 -- Is_Inherited_Operation --
3939 ----------------------------
3941 function Is_Inherited_Operation (E : Entity_Id) return Boolean is
3942 Kind : constant Node_Kind := Nkind (Parent (E));
3943 begin
3944 pragma Assert (Is_Overloadable (E));
3945 return Kind = N_Full_Type_Declaration
3946 or else Kind = N_Private_Extension_Declaration
3947 or else Kind = N_Subtype_Declaration
3948 or else (Ekind (E) = E_Enumeration_Literal
3949 and then Is_Derived_Type (Etype (E)));
3950 end Is_Inherited_Operation;
3952 -----------------------------
3953 -- Is_Library_Level_Entity --
3954 -----------------------------
3956 function Is_Library_Level_Entity (E : Entity_Id) return Boolean is
3957 begin
3958 -- The following is a small optimization, and it also handles
3959 -- properly discriminals, which in task bodies might appear in
3960 -- expressions before the corresponding procedure has been
3961 -- created, and which therefore do not have an assigned scope.
3963 if Ekind (E) in Formal_Kind then
3964 return False;
3965 end if;
3967 -- Normal test is simply that the enclosing dynamic scope is Standard
3969 return Enclosing_Dynamic_Scope (E) = Standard_Standard;
3970 end Is_Library_Level_Entity;
3972 ---------------------------------
3973 -- Is_Local_Variable_Reference --
3974 ---------------------------------
3976 function Is_Local_Variable_Reference (Expr : Node_Id) return Boolean is
3977 begin
3978 if not Is_Entity_Name (Expr) then
3979 return False;
3981 else
3982 declare
3983 Ent : constant Entity_Id := Entity (Expr);
3984 Sub : constant Entity_Id := Enclosing_Subprogram (Ent);
3986 begin
3987 if Ekind (Ent) /= E_Variable
3988 and then
3989 Ekind (Ent) /= E_In_Out_Parameter
3990 then
3991 return False;
3993 else
3994 return Present (Sub) and then Sub = Current_Subprogram;
3995 end if;
3996 end;
3997 end if;
3998 end Is_Local_Variable_Reference;
4000 ---------------
4001 -- Is_Lvalue --
4002 ---------------
4004 function Is_Lvalue (N : Node_Id) return Boolean is
4005 P : constant Node_Id := Parent (N);
4007 begin
4008 case Nkind (P) is
4010 -- Test left side of assignment
4012 when N_Assignment_Statement =>
4013 return N = Name (P);
4015 -- Test prefix of component or attribute
4017 when N_Attribute_Reference |
4018 N_Expanded_Name |
4019 N_Explicit_Dereference |
4020 N_Indexed_Component |
4021 N_Reference |
4022 N_Selected_Component |
4023 N_Slice =>
4024 return N = Prefix (P);
4026 -- Test subprogram parameter (we really should check the
4027 -- parameter mode, but it is not worth the trouble)
4029 when N_Function_Call |
4030 N_Procedure_Call_Statement |
4031 N_Accept_Statement |
4032 N_Parameter_Association =>
4033 return True;
4035 -- Test for appearing in a conversion that itself appears
4036 -- in an lvalue context, since this should be an lvalue.
4038 when N_Type_Conversion =>
4039 return Is_Lvalue (P);
4041 -- Test for appearence in object renaming declaration
4043 when N_Object_Renaming_Declaration =>
4044 return True;
4046 -- All other references are definitely not Lvalues
4048 when others =>
4049 return False;
4051 end case;
4052 end Is_Lvalue;
4054 -------------------------
4055 -- Is_Object_Reference --
4056 -------------------------
4058 function Is_Object_Reference (N : Node_Id) return Boolean is
4059 begin
4060 if Is_Entity_Name (N) then
4061 return Is_Object (Entity (N));
4063 else
4064 case Nkind (N) is
4065 when N_Indexed_Component | N_Slice =>
4066 return Is_Object_Reference (Prefix (N));
4068 -- In Ada95, a function call is a constant object
4070 when N_Function_Call =>
4071 return True;
4073 -- A reference to the stream attribute Input is a function call
4075 when N_Attribute_Reference =>
4076 return Attribute_Name (N) = Name_Input;
4078 when N_Selected_Component =>
4079 return
4080 Is_Object_Reference (Selector_Name (N))
4081 and then Is_Object_Reference (Prefix (N));
4083 when N_Explicit_Dereference =>
4084 return True;
4086 -- A view conversion of a tagged object is an object reference
4088 when N_Type_Conversion =>
4089 return Is_Tagged_Type (Etype (Subtype_Mark (N)))
4090 and then Is_Tagged_Type (Etype (Expression (N)))
4091 and then Is_Object_Reference (Expression (N));
4093 -- An unchecked type conversion is considered to be an object if
4094 -- the operand is an object (this construction arises only as a
4095 -- result of expansion activities).
4097 when N_Unchecked_Type_Conversion =>
4098 return True;
4100 when others =>
4101 return False;
4102 end case;
4103 end if;
4104 end Is_Object_Reference;
4106 -----------------------------------
4107 -- Is_OK_Variable_For_Out_Formal --
4108 -----------------------------------
4110 function Is_OK_Variable_For_Out_Formal (AV : Node_Id) return Boolean is
4111 begin
4112 Note_Possible_Modification (AV);
4114 -- We must reject parenthesized variable names. The check for
4115 -- Comes_From_Source is present because there are currently
4116 -- cases where the compiler violates this rule (e.g. passing
4117 -- a task object to its controlled Initialize routine).
4119 if Paren_Count (AV) > 0 and then Comes_From_Source (AV) then
4120 return False;
4122 -- A variable is always allowed
4124 elsif Is_Variable (AV) then
4125 return True;
4127 -- Unchecked conversions are allowed only if they come from the
4128 -- generated code, which sometimes uses unchecked conversions for
4129 -- out parameters in cases where code generation is unaffected.
4130 -- We tell source unchecked conversions by seeing if they are
4131 -- rewrites of an original UC function call, or of an explicit
4132 -- conversion of a function call.
4134 elsif Nkind (AV) = N_Unchecked_Type_Conversion then
4135 if Nkind (Original_Node (AV)) = N_Function_Call then
4136 return False;
4138 elsif Comes_From_Source (AV)
4139 and then Nkind (Original_Node (Expression (AV))) = N_Function_Call
4140 then
4141 return False;
4143 elsif Nkind (Original_Node (AV)) = N_Type_Conversion then
4144 return Is_OK_Variable_For_Out_Formal (Expression (AV));
4146 else
4147 return True;
4148 end if;
4150 -- Normal type conversions are allowed if argument is a variable
4152 elsif Nkind (AV) = N_Type_Conversion then
4153 if Is_Variable (Expression (AV))
4154 and then Paren_Count (Expression (AV)) = 0
4155 then
4156 Note_Possible_Modification (Expression (AV));
4157 return True;
4159 -- We also allow a non-parenthesized expression that raises
4160 -- constraint error if it rewrites what used to be a variable
4162 elsif Raises_Constraint_Error (Expression (AV))
4163 and then Paren_Count (Expression (AV)) = 0
4164 and then Is_Variable (Original_Node (Expression (AV)))
4165 then
4166 return True;
4168 -- Type conversion of something other than a variable
4170 else
4171 return False;
4172 end if;
4174 -- If this node is rewritten, then test the original form, if that is
4175 -- OK, then we consider the rewritten node OK (for example, if the
4176 -- original node is a conversion, then Is_Variable will not be true
4177 -- but we still want to allow the conversion if it converts a variable).
4179 elsif Original_Node (AV) /= AV then
4180 return Is_OK_Variable_For_Out_Formal (Original_Node (AV));
4182 -- All other non-variables are rejected
4184 else
4185 return False;
4186 end if;
4187 end Is_OK_Variable_For_Out_Formal;
4189 -----------------------------------
4190 -- Is_Partially_Initialized_Type --
4191 -----------------------------------
4193 function Is_Partially_Initialized_Type (Typ : Entity_Id) return Boolean is
4194 begin
4195 if Is_Scalar_Type (Typ) then
4196 return False;
4198 elsif Is_Access_Type (Typ) then
4199 return True;
4201 elsif Is_Array_Type (Typ) then
4203 -- If component type is partially initialized, so is array type
4205 if Is_Partially_Initialized_Type (Component_Type (Typ)) then
4206 return True;
4208 -- Otherwise we are only partially initialized if we are fully
4209 -- initialized (this is the empty array case, no point in us
4210 -- duplicating that code here).
4212 else
4213 return Is_Fully_Initialized_Type (Typ);
4214 end if;
4216 elsif Is_Record_Type (Typ) then
4218 -- A discriminated type is always partially initialized
4220 if Has_Discriminants (Typ) then
4221 return True;
4223 -- A tagged type is always partially initialized
4225 elsif Is_Tagged_Type (Typ) then
4226 return True;
4228 -- Case of non-discriminated record
4230 else
4231 declare
4232 Ent : Entity_Id;
4234 Component_Present : Boolean := False;
4235 -- Set True if at least one component is present. If no
4236 -- components are present, then record type is fully
4237 -- initialized (another odd case, like the null array).
4239 begin
4240 -- Loop through components
4242 Ent := First_Entity (Typ);
4243 while Present (Ent) loop
4244 if Ekind (Ent) = E_Component then
4245 Component_Present := True;
4247 -- If a component has an initialization expression then
4248 -- the enclosing record type is partially initialized
4250 if Present (Parent (Ent))
4251 and then Present (Expression (Parent (Ent)))
4252 then
4253 return True;
4255 -- If a component is of a type which is itself partially
4256 -- initialized, then the enclosing record type is also.
4258 elsif Is_Partially_Initialized_Type (Etype (Ent)) then
4259 return True;
4260 end if;
4261 end if;
4263 Next_Entity (Ent);
4264 end loop;
4266 -- No initialized components found. If we found any components
4267 -- they were all uninitialized so the result is false.
4269 if Component_Present then
4270 return False;
4272 -- But if we found no components, then all the components are
4273 -- initialized so we consider the type to be initialized.
4275 else
4276 return True;
4277 end if;
4278 end;
4279 end if;
4281 -- Concurrent types are always fully initialized
4283 elsif Is_Concurrent_Type (Typ) then
4284 return True;
4286 -- For a private type, go to underlying type. If there is no underlying
4287 -- type then just assume this partially initialized. Not clear if this
4288 -- can happen in a non-error case, but no harm in testing for this.
4290 elsif Is_Private_Type (Typ) then
4291 declare
4292 U : constant Entity_Id := Underlying_Type (Typ);
4294 begin
4295 if No (U) then
4296 return True;
4297 else
4298 return Is_Partially_Initialized_Type (U);
4299 end if;
4300 end;
4302 -- For any other type (are there any?) assume partially initialized
4304 else
4305 return True;
4306 end if;
4307 end Is_Partially_Initialized_Type;
4309 -----------------------------
4310 -- Is_RCI_Pkg_Spec_Or_Body --
4311 -----------------------------
4313 function Is_RCI_Pkg_Spec_Or_Body (Cunit : Node_Id) return Boolean is
4315 function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean;
4316 -- Return True if the unit of Cunit is an RCI package declaration
4318 ---------------------------
4319 -- Is_RCI_Pkg_Decl_Cunit --
4320 ---------------------------
4322 function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean is
4323 The_Unit : constant Node_Id := Unit (Cunit);
4325 begin
4326 if Nkind (The_Unit) /= N_Package_Declaration then
4327 return False;
4328 end if;
4329 return Is_Remote_Call_Interface (Defining_Entity (The_Unit));
4330 end Is_RCI_Pkg_Decl_Cunit;
4332 -- Start of processing for Is_RCI_Pkg_Spec_Or_Body
4334 begin
4335 return Is_RCI_Pkg_Decl_Cunit (Cunit)
4336 or else
4337 (Nkind (Unit (Cunit)) = N_Package_Body
4338 and then Is_RCI_Pkg_Decl_Cunit (Library_Unit (Cunit)));
4339 end Is_RCI_Pkg_Spec_Or_Body;
4341 -----------------------------------------
4342 -- Is_Remote_Access_To_Class_Wide_Type --
4343 -----------------------------------------
4345 function Is_Remote_Access_To_Class_Wide_Type
4346 (E : Entity_Id) return Boolean
4348 D : Entity_Id;
4350 function Comes_From_Limited_Private_Type_Declaration
4351 (E : Entity_Id) return Boolean;
4352 -- Check that the type is declared by a limited type declaration,
4353 -- or else is derived from a Remote_Type ancestor through private
4354 -- extensions.
4356 -------------------------------------------------
4357 -- Comes_From_Limited_Private_Type_Declaration --
4358 -------------------------------------------------
4360 function Comes_From_Limited_Private_Type_Declaration
4361 (E : Entity_Id) return Boolean
4363 N : constant Node_Id := Declaration_Node (E);
4365 begin
4366 if Nkind (N) = N_Private_Type_Declaration
4367 and then Limited_Present (N)
4368 then
4369 return True;
4370 end if;
4372 if Nkind (N) = N_Private_Extension_Declaration then
4373 return
4374 Comes_From_Limited_Private_Type_Declaration (Etype (E))
4375 or else
4376 (Is_Remote_Types (Etype (E))
4377 and then Is_Limited_Record (Etype (E))
4378 and then Has_Private_Declaration (Etype (E)));
4379 end if;
4381 return False;
4382 end Comes_From_Limited_Private_Type_Declaration;
4384 -- Start of processing for Is_Remote_Access_To_Class_Wide_Type
4386 begin
4387 if not (Is_Remote_Call_Interface (E)
4388 or else Is_Remote_Types (E))
4389 or else Ekind (E) /= E_General_Access_Type
4390 then
4391 return False;
4392 end if;
4394 D := Designated_Type (E);
4396 if Ekind (D) /= E_Class_Wide_Type then
4397 return False;
4398 end if;
4400 return Comes_From_Limited_Private_Type_Declaration
4401 (Defining_Identifier (Parent (D)));
4402 end Is_Remote_Access_To_Class_Wide_Type;
4404 -----------------------------------------
4405 -- Is_Remote_Access_To_Subprogram_Type --
4406 -----------------------------------------
4408 function Is_Remote_Access_To_Subprogram_Type
4409 (E : Entity_Id) return Boolean
4411 begin
4412 return (Ekind (E) = E_Access_Subprogram_Type
4413 or else (Ekind (E) = E_Record_Type
4414 and then Present (Corresponding_Remote_Type (E))))
4415 and then (Is_Remote_Call_Interface (E)
4416 or else Is_Remote_Types (E));
4417 end Is_Remote_Access_To_Subprogram_Type;
4419 --------------------
4420 -- Is_Remote_Call --
4421 --------------------
4423 function Is_Remote_Call (N : Node_Id) return Boolean is
4424 begin
4425 if Nkind (N) /= N_Procedure_Call_Statement
4426 and then Nkind (N) /= N_Function_Call
4427 then
4428 -- An entry call cannot be remote
4430 return False;
4432 elsif Nkind (Name (N)) in N_Has_Entity
4433 and then Is_Remote_Call_Interface (Entity (Name (N)))
4434 then
4435 -- A subprogram declared in the spec of a RCI package is remote
4437 return True;
4439 elsif Nkind (Name (N)) = N_Explicit_Dereference
4440 and then Is_Remote_Access_To_Subprogram_Type
4441 (Etype (Prefix (Name (N))))
4442 then
4443 -- The dereference of a RAS is a remote call
4445 return True;
4447 elsif Present (Controlling_Argument (N))
4448 and then Is_Remote_Access_To_Class_Wide_Type
4449 (Etype (Controlling_Argument (N)))
4450 then
4451 -- Any primitive operation call with a controlling argument of
4452 -- a RACW type is a remote call.
4454 return True;
4455 end if;
4457 -- All other calls are local calls
4459 return False;
4460 end Is_Remote_Call;
4462 ----------------------
4463 -- Is_Selector_Name --
4464 ----------------------
4466 function Is_Selector_Name (N : Node_Id) return Boolean is
4467 begin
4468 if not Is_List_Member (N) then
4469 declare
4470 P : constant Node_Id := Parent (N);
4471 K : constant Node_Kind := Nkind (P);
4472 begin
4473 return
4474 (K = N_Expanded_Name or else
4475 K = N_Generic_Association or else
4476 K = N_Parameter_Association or else
4477 K = N_Selected_Component)
4478 and then Selector_Name (P) = N;
4479 end;
4481 else
4482 declare
4483 L : constant List_Id := List_Containing (N);
4484 P : constant Node_Id := Parent (L);
4485 begin
4486 return (Nkind (P) = N_Discriminant_Association
4487 and then Selector_Names (P) = L)
4488 or else
4489 (Nkind (P) = N_Component_Association
4490 and then Choices (P) = L);
4491 end;
4492 end if;
4493 end Is_Selector_Name;
4495 ------------------
4496 -- Is_Statement --
4497 ------------------
4499 function Is_Statement (N : Node_Id) return Boolean is
4500 begin
4501 return
4502 Nkind (N) in N_Statement_Other_Than_Procedure_Call
4503 or else Nkind (N) = N_Procedure_Call_Statement;
4504 end Is_Statement;
4506 -----------------
4507 -- Is_Transfer --
4508 -----------------
4510 function Is_Transfer (N : Node_Id) return Boolean is
4511 Kind : constant Node_Kind := Nkind (N);
4513 begin
4514 if Kind = N_Return_Statement
4515 or else
4516 Kind = N_Goto_Statement
4517 or else
4518 Kind = N_Raise_Statement
4519 or else
4520 Kind = N_Requeue_Statement
4521 then
4522 return True;
4524 elsif (Kind = N_Exit_Statement or else Kind in N_Raise_xxx_Error)
4525 and then No (Condition (N))
4526 then
4527 return True;
4529 elsif Kind = N_Procedure_Call_Statement
4530 and then Is_Entity_Name (Name (N))
4531 and then Present (Entity (Name (N)))
4532 and then No_Return (Entity (Name (N)))
4533 then
4534 return True;
4536 elsif Nkind (Original_Node (N)) = N_Raise_Statement then
4537 return True;
4539 else
4540 return False;
4541 end if;
4542 end Is_Transfer;
4544 -------------
4545 -- Is_True --
4546 -------------
4548 function Is_True (U : Uint) return Boolean is
4549 begin
4550 return (U /= 0);
4551 end Is_True;
4553 -----------------
4554 -- Is_Variable --
4555 -----------------
4557 function Is_Variable (N : Node_Id) return Boolean is
4559 Orig_Node : constant Node_Id := Original_Node (N);
4560 -- We do the test on the original node, since this is basically a
4561 -- test of syntactic categories, so it must not be disturbed by
4562 -- whatever rewriting might have occurred. For example, an aggregate,
4563 -- which is certainly NOT a variable, could be turned into a variable
4564 -- by expansion.
4566 function In_Protected_Function (E : Entity_Id) return Boolean;
4567 -- Within a protected function, the private components of the
4568 -- enclosing protected type are constants. A function nested within
4569 -- a (protected) procedure is not itself protected.
4571 function Is_Variable_Prefix (P : Node_Id) return Boolean;
4572 -- Prefixes can involve implicit dereferences, in which case we
4573 -- must test for the case of a reference of a constant access
4574 -- type, which can never be a variable.
4576 ---------------------------
4577 -- In_Protected_Function --
4578 ---------------------------
4580 function In_Protected_Function (E : Entity_Id) return Boolean is
4581 Prot : constant Entity_Id := Scope (E);
4582 S : Entity_Id;
4584 begin
4585 if not Is_Protected_Type (Prot) then
4586 return False;
4587 else
4588 S := Current_Scope;
4589 while Present (S) and then S /= Prot loop
4590 if Ekind (S) = E_Function
4591 and then Scope (S) = Prot
4592 then
4593 return True;
4594 end if;
4596 S := Scope (S);
4597 end loop;
4599 return False;
4600 end if;
4601 end In_Protected_Function;
4603 ------------------------
4604 -- Is_Variable_Prefix --
4605 ------------------------
4607 function Is_Variable_Prefix (P : Node_Id) return Boolean is
4608 begin
4609 if Is_Access_Type (Etype (P)) then
4610 return not Is_Access_Constant (Root_Type (Etype (P)));
4612 -- For the case of an indexed component whose prefix has a packed
4613 -- array type, the prefix has been rewritten into a type conversion.
4614 -- Determine variable-ness from the converted expression.
4616 elsif Nkind (P) = N_Type_Conversion
4617 and then not Comes_From_Source (P)
4618 and then Is_Array_Type (Etype (P))
4619 and then Is_Packed (Etype (P))
4620 then
4621 return Is_Variable (Expression (P));
4623 else
4624 return Is_Variable (P);
4625 end if;
4626 end Is_Variable_Prefix;
4628 -- Start of processing for Is_Variable
4630 begin
4631 -- Definitely OK if Assignment_OK is set. Since this is something that
4632 -- only gets set for expanded nodes, the test is on N, not Orig_Node.
4634 if Nkind (N) in N_Subexpr and then Assignment_OK (N) then
4635 return True;
4637 -- Normally we go to the original node, but there is one exception
4638 -- where we use the rewritten node, namely when it is an explicit
4639 -- dereference. The generated code may rewrite a prefix which is an
4640 -- access type with an explicit dereference. The dereference is a
4641 -- variable, even though the original node may not be (since it could
4642 -- be a constant of the access type).
4644 elsif Nkind (N) = N_Explicit_Dereference
4645 and then Nkind (Orig_Node) /= N_Explicit_Dereference
4646 and then Is_Access_Type (Etype (Orig_Node))
4647 then
4648 return Is_Variable_Prefix (Original_Node (Prefix (N)));
4650 -- A function call is never a variable
4652 elsif Nkind (N) = N_Function_Call then
4653 return False;
4655 -- All remaining checks use the original node
4657 elsif Is_Entity_Name (Orig_Node) then
4658 declare
4659 E : constant Entity_Id := Entity (Orig_Node);
4660 K : constant Entity_Kind := Ekind (E);
4662 begin
4663 return (K = E_Variable
4664 and then Nkind (Parent (E)) /= N_Exception_Handler)
4665 or else (K = E_Component
4666 and then not In_Protected_Function (E))
4667 or else K = E_Out_Parameter
4668 or else K = E_In_Out_Parameter
4669 or else K = E_Generic_In_Out_Parameter
4671 -- Current instance of type:
4673 or else (Is_Type (E) and then In_Open_Scopes (E))
4674 or else (Is_Incomplete_Or_Private_Type (E)
4675 and then In_Open_Scopes (Full_View (E)));
4676 end;
4678 else
4679 case Nkind (Orig_Node) is
4680 when N_Indexed_Component | N_Slice =>
4681 return Is_Variable_Prefix (Prefix (Orig_Node));
4683 when N_Selected_Component =>
4684 return Is_Variable_Prefix (Prefix (Orig_Node))
4685 and then Is_Variable (Selector_Name (Orig_Node));
4687 -- For an explicit dereference, the type of the prefix cannot
4688 -- be an access to constant or an access to subprogram.
4690 when N_Explicit_Dereference =>
4691 declare
4692 Typ : constant Entity_Id := Etype (Prefix (Orig_Node));
4693 begin
4694 return Is_Access_Type (Typ)
4695 and then not Is_Access_Constant (Root_Type (Typ))
4696 and then Ekind (Typ) /= E_Access_Subprogram_Type;
4697 end;
4699 -- The type conversion is the case where we do not deal with the
4700 -- context dependent special case of an actual parameter. Thus
4701 -- the type conversion is only considered a variable for the
4702 -- purposes of this routine if the target type is tagged. However,
4703 -- a type conversion is considered to be a variable if it does not
4704 -- come from source (this deals for example with the conversions
4705 -- of expressions to their actual subtypes).
4707 when N_Type_Conversion =>
4708 return Is_Variable (Expression (Orig_Node))
4709 and then
4710 (not Comes_From_Source (Orig_Node)
4711 or else
4712 (Is_Tagged_Type (Etype (Subtype_Mark (Orig_Node)))
4713 and then
4714 Is_Tagged_Type (Etype (Expression (Orig_Node)))));
4716 -- GNAT allows an unchecked type conversion as a variable. This
4717 -- only affects the generation of internal expanded code, since
4718 -- calls to instantiations of Unchecked_Conversion are never
4719 -- considered variables (since they are function calls).
4720 -- This is also true for expression actions.
4722 when N_Unchecked_Type_Conversion =>
4723 return Is_Variable (Expression (Orig_Node));
4725 when others =>
4726 return False;
4727 end case;
4728 end if;
4729 end Is_Variable;
4731 ------------------------
4732 -- Is_Volatile_Object --
4733 ------------------------
4735 function Is_Volatile_Object (N : Node_Id) return Boolean is
4737 function Object_Has_Volatile_Components (N : Node_Id) return Boolean;
4738 -- Determines if given object has volatile components
4740 function Is_Volatile_Prefix (N : Node_Id) return Boolean;
4741 -- If prefix is an implicit dereference, examine designated type
4743 ------------------------
4744 -- Is_Volatile_Prefix --
4745 ------------------------
4747 function Is_Volatile_Prefix (N : Node_Id) return Boolean is
4748 Typ : constant Entity_Id := Etype (N);
4750 begin
4751 if Is_Access_Type (Typ) then
4752 declare
4753 Dtyp : constant Entity_Id := Designated_Type (Typ);
4755 begin
4756 return Is_Volatile (Dtyp)
4757 or else Has_Volatile_Components (Dtyp);
4758 end;
4760 else
4761 return Object_Has_Volatile_Components (N);
4762 end if;
4763 end Is_Volatile_Prefix;
4765 ------------------------------------
4766 -- Object_Has_Volatile_Components --
4767 ------------------------------------
4769 function Object_Has_Volatile_Components (N : Node_Id) return Boolean is
4770 Typ : constant Entity_Id := Etype (N);
4772 begin
4773 if Is_Volatile (Typ)
4774 or else Has_Volatile_Components (Typ)
4775 then
4776 return True;
4778 elsif Is_Entity_Name (N)
4779 and then (Has_Volatile_Components (Entity (N))
4780 or else Is_Volatile (Entity (N)))
4781 then
4782 return True;
4784 elsif Nkind (N) = N_Indexed_Component
4785 or else Nkind (N) = N_Selected_Component
4786 then
4787 return Is_Volatile_Prefix (Prefix (N));
4789 else
4790 return False;
4791 end if;
4792 end Object_Has_Volatile_Components;
4794 -- Start of processing for Is_Volatile_Object
4796 begin
4797 if Is_Volatile (Etype (N))
4798 or else (Is_Entity_Name (N) and then Is_Volatile (Entity (N)))
4799 then
4800 return True;
4802 elsif Nkind (N) = N_Indexed_Component
4803 or else Nkind (N) = N_Selected_Component
4804 then
4805 return Is_Volatile_Prefix (Prefix (N));
4807 else
4808 return False;
4809 end if;
4810 end Is_Volatile_Object;
4812 -------------------------
4813 -- Kill_Current_Values --
4814 -------------------------
4816 procedure Kill_Current_Values is
4817 S : Entity_Id;
4819 procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id);
4820 -- Clear current value for entity E and all entities chained to E
4822 ------------------------------------------
4823 -- Kill_Current_Values_For_Entity_Chain --
4824 ------------------------------------------
4826 procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id) is
4827 Ent : Entity_Id;
4829 begin
4830 Ent := E;
4831 while Present (Ent) loop
4832 if Is_Object (Ent) then
4833 Set_Current_Value (Ent, Empty);
4835 if not Can_Never_Be_Null (Ent) then
4836 Set_Is_Known_Non_Null (Ent, False);
4837 end if;
4838 end if;
4840 Next_Entity (Ent);
4841 end loop;
4842 end Kill_Current_Values_For_Entity_Chain;
4844 -- Start of processing for Kill_Current_Values
4846 begin
4847 -- Kill all saved checks, a special case of killing saved values
4849 Kill_All_Checks;
4851 -- Loop through relevant scopes, which includes the current scope and
4852 -- any parent scopes if the current scope is a block or a package.
4854 S := Current_Scope;
4855 Scope_Loop : loop
4857 -- Clear current values of all entities in current scope
4859 Kill_Current_Values_For_Entity_Chain (First_Entity (S));
4861 -- If scope is a package, also clear current values of all
4862 -- private entities in the scope.
4864 if Ekind (S) = E_Package
4865 or else
4866 Ekind (S) = E_Generic_Package
4867 or else
4868 Is_Concurrent_Type (S)
4869 then
4870 Kill_Current_Values_For_Entity_Chain (First_Private_Entity (S));
4871 end if;
4873 -- If this is a block or nested package, deal with parent
4875 if Ekind (S) = E_Block
4876 or else (Ekind (S) = E_Package
4877 and then not Is_Library_Level_Entity (S))
4878 then
4879 S := Scope (S);
4880 else
4881 exit Scope_Loop;
4882 end if;
4883 end loop Scope_Loop;
4884 end Kill_Current_Values;
4886 --------------------------
4887 -- Kill_Size_Check_Code --
4888 --------------------------
4890 procedure Kill_Size_Check_Code (E : Entity_Id) is
4891 begin
4892 if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
4893 and then Present (Size_Check_Code (E))
4894 then
4895 Remove (Size_Check_Code (E));
4896 Set_Size_Check_Code (E, Empty);
4897 end if;
4898 end Kill_Size_Check_Code;
4900 -------------------------
4901 -- New_External_Entity --
4902 -------------------------
4904 function New_External_Entity
4905 (Kind : Entity_Kind;
4906 Scope_Id : Entity_Id;
4907 Sloc_Value : Source_Ptr;
4908 Related_Id : Entity_Id;
4909 Suffix : Character;
4910 Suffix_Index : Nat := 0;
4911 Prefix : Character := ' ') return Entity_Id
4913 N : constant Entity_Id :=
4914 Make_Defining_Identifier (Sloc_Value,
4915 New_External_Name
4916 (Chars (Related_Id), Suffix, Suffix_Index, Prefix));
4918 begin
4919 Set_Ekind (N, Kind);
4920 Set_Is_Internal (N, True);
4921 Append_Entity (N, Scope_Id);
4922 Set_Public_Status (N);
4924 if Kind in Type_Kind then
4925 Init_Size_Align (N);
4926 end if;
4928 return N;
4929 end New_External_Entity;
4931 -------------------------
4932 -- New_Internal_Entity --
4933 -------------------------
4935 function New_Internal_Entity
4936 (Kind : Entity_Kind;
4937 Scope_Id : Entity_Id;
4938 Sloc_Value : Source_Ptr;
4939 Id_Char : Character) return Entity_Id
4941 N : constant Entity_Id :=
4942 Make_Defining_Identifier (Sloc_Value, New_Internal_Name (Id_Char));
4944 begin
4945 Set_Ekind (N, Kind);
4946 Set_Is_Internal (N, True);
4947 Append_Entity (N, Scope_Id);
4949 if Kind in Type_Kind then
4950 Init_Size_Align (N);
4951 end if;
4953 return N;
4954 end New_Internal_Entity;
4956 -----------------
4957 -- Next_Actual --
4958 -----------------
4960 function Next_Actual (Actual_Id : Node_Id) return Node_Id is
4961 N : Node_Id;
4963 begin
4964 -- If we are pointing at a positional parameter, it is a member of
4965 -- a node list (the list of parameters), and the next parameter
4966 -- is the next node on the list, unless we hit a parameter
4967 -- association, in which case we shift to using the chain whose
4968 -- head is the First_Named_Actual in the parent, and then is
4969 -- threaded using the Next_Named_Actual of the Parameter_Association.
4970 -- All this fiddling is because the original node list is in the
4971 -- textual call order, and what we need is the declaration order.
4973 if Is_List_Member (Actual_Id) then
4974 N := Next (Actual_Id);
4976 if Nkind (N) = N_Parameter_Association then
4977 return First_Named_Actual (Parent (Actual_Id));
4978 else
4979 return N;
4980 end if;
4982 else
4983 return Next_Named_Actual (Parent (Actual_Id));
4984 end if;
4985 end Next_Actual;
4987 procedure Next_Actual (Actual_Id : in out Node_Id) is
4988 begin
4989 Actual_Id := Next_Actual (Actual_Id);
4990 end Next_Actual;
4992 -----------------------
4993 -- Normalize_Actuals --
4994 -----------------------
4996 -- Chain actuals according to formals of subprogram. If there are
4997 -- no named associations, the chain is simply the list of Parameter
4998 -- Associations, since the order is the same as the declaration order.
4999 -- If there are named associations, then the First_Named_Actual field
5000 -- in the N_Procedure_Call_Statement node or N_Function_Call node
5001 -- points to the Parameter_Association node for the parameter that
5002 -- comes first in declaration order. The remaining named parameters
5003 -- are then chained in declaration order using Next_Named_Actual.
5005 -- This routine also verifies that the number of actuals is compatible
5006 -- with the number and default values of formals, but performs no type
5007 -- checking (type checking is done by the caller).
5009 -- If the matching succeeds, Success is set to True, and the caller
5010 -- proceeds with type-checking. If the match is unsuccessful, then
5011 -- Success is set to False, and the caller attempts a different
5012 -- interpretation, if there is one.
5014 -- If the flag Report is on, the call is not overloaded, and a failure
5015 -- to match can be reported here, rather than in the caller.
5017 procedure Normalize_Actuals
5018 (N : Node_Id;
5019 S : Entity_Id;
5020 Report : Boolean;
5021 Success : out Boolean)
5023 Actuals : constant List_Id := Parameter_Associations (N);
5024 Actual : Node_Id := Empty;
5025 Formal : Entity_Id;
5026 Last : Node_Id := Empty;
5027 First_Named : Node_Id := Empty;
5028 Found : Boolean;
5030 Formals_To_Match : Integer := 0;
5031 Actuals_To_Match : Integer := 0;
5033 procedure Chain (A : Node_Id);
5034 -- Add named actual at the proper place in the list, using the
5035 -- Next_Named_Actual link.
5037 function Reporting return Boolean;
5038 -- Determines if an error is to be reported. To report an error, we
5039 -- need Report to be True, and also we do not report errors caused
5040 -- by calls to init procs that occur within other init procs. Such
5041 -- errors must always be cascaded errors, since if all the types are
5042 -- declared correctly, the compiler will certainly build decent calls!
5044 -----------
5045 -- Chain --
5046 -----------
5048 procedure Chain (A : Node_Id) is
5049 begin
5050 if No (Last) then
5052 -- Call node points to first actual in list
5054 Set_First_Named_Actual (N, Explicit_Actual_Parameter (A));
5056 else
5057 Set_Next_Named_Actual (Last, Explicit_Actual_Parameter (A));
5058 end if;
5060 Last := A;
5061 Set_Next_Named_Actual (Last, Empty);
5062 end Chain;
5064 ---------------
5065 -- Reporting --
5066 ---------------
5068 function Reporting return Boolean is
5069 begin
5070 if not Report then
5071 return False;
5073 elsif not Within_Init_Proc then
5074 return True;
5076 elsif Is_Init_Proc (Entity (Name (N))) then
5077 return False;
5079 else
5080 return True;
5081 end if;
5082 end Reporting;
5084 -- Start of processing for Normalize_Actuals
5086 begin
5087 if Is_Access_Type (S) then
5089 -- The name in the call is a function call that returns an access
5090 -- to subprogram. The designated type has the list of formals.
5092 Formal := First_Formal (Designated_Type (S));
5093 else
5094 Formal := First_Formal (S);
5095 end if;
5097 while Present (Formal) loop
5098 Formals_To_Match := Formals_To_Match + 1;
5099 Next_Formal (Formal);
5100 end loop;
5102 -- Find if there is a named association, and verify that no positional
5103 -- associations appear after named ones.
5105 if Present (Actuals) then
5106 Actual := First (Actuals);
5107 end if;
5109 while Present (Actual)
5110 and then Nkind (Actual) /= N_Parameter_Association
5111 loop
5112 Actuals_To_Match := Actuals_To_Match + 1;
5113 Next (Actual);
5114 end loop;
5116 if No (Actual) and Actuals_To_Match = Formals_To_Match then
5118 -- Most common case: positional notation, no defaults
5120 Success := True;
5121 return;
5123 elsif Actuals_To_Match > Formals_To_Match then
5125 -- Too many actuals: will not work
5127 if Reporting then
5128 if Is_Entity_Name (Name (N)) then
5129 Error_Msg_N ("too many arguments in call to&", Name (N));
5130 else
5131 Error_Msg_N ("too many arguments in call", N);
5132 end if;
5133 end if;
5135 Success := False;
5136 return;
5137 end if;
5139 First_Named := Actual;
5141 while Present (Actual) loop
5142 if Nkind (Actual) /= N_Parameter_Association then
5143 Error_Msg_N
5144 ("positional parameters not allowed after named ones", Actual);
5145 Success := False;
5146 return;
5148 else
5149 Actuals_To_Match := Actuals_To_Match + 1;
5150 end if;
5152 Next (Actual);
5153 end loop;
5155 if Present (Actuals) then
5156 Actual := First (Actuals);
5157 end if;
5159 Formal := First_Formal (S);
5160 while Present (Formal) loop
5162 -- Match the formals in order. If the corresponding actual
5163 -- is positional, nothing to do. Else scan the list of named
5164 -- actuals to find the one with the right name.
5166 if Present (Actual)
5167 and then Nkind (Actual) /= N_Parameter_Association
5168 then
5169 Next (Actual);
5170 Actuals_To_Match := Actuals_To_Match - 1;
5171 Formals_To_Match := Formals_To_Match - 1;
5173 else
5174 -- For named parameters, search the list of actuals to find
5175 -- one that matches the next formal name.
5177 Actual := First_Named;
5178 Found := False;
5180 while Present (Actual) loop
5181 if Chars (Selector_Name (Actual)) = Chars (Formal) then
5182 Found := True;
5183 Chain (Actual);
5184 Actuals_To_Match := Actuals_To_Match - 1;
5185 Formals_To_Match := Formals_To_Match - 1;
5186 exit;
5187 end if;
5189 Next (Actual);
5190 end loop;
5192 if not Found then
5193 if Ekind (Formal) /= E_In_Parameter
5194 or else No (Default_Value (Formal))
5195 then
5196 if Reporting then
5197 if (Comes_From_Source (S)
5198 or else Sloc (S) = Standard_Location)
5199 and then Is_Overloadable (S)
5200 then
5201 if No (Actuals)
5202 and then
5203 (Nkind (Parent (N)) = N_Procedure_Call_Statement
5204 or else
5205 (Nkind (Parent (N)) = N_Function_Call
5206 or else
5207 Nkind (Parent (N)) = N_Parameter_Association))
5208 and then Ekind (S) /= E_Function
5209 then
5210 Set_Etype (N, Etype (S));
5211 else
5212 Error_Msg_Name_1 := Chars (S);
5213 Error_Msg_Sloc := Sloc (S);
5214 Error_Msg_NE
5215 ("missing argument for parameter & " &
5216 "in call to % declared #", N, Formal);
5217 end if;
5219 elsif Is_Overloadable (S) then
5220 Error_Msg_Name_1 := Chars (S);
5222 -- Point to type derivation that generated the
5223 -- operation.
5225 Error_Msg_Sloc := Sloc (Parent (S));
5227 Error_Msg_NE
5228 ("missing argument for parameter & " &
5229 "in call to % (inherited) #", N, Formal);
5231 else
5232 Error_Msg_NE
5233 ("missing argument for parameter &", N, Formal);
5234 end if;
5235 end if;
5237 Success := False;
5238 return;
5240 else
5241 Formals_To_Match := Formals_To_Match - 1;
5242 end if;
5243 end if;
5244 end if;
5246 Next_Formal (Formal);
5247 end loop;
5249 if Formals_To_Match = 0 and then Actuals_To_Match = 0 then
5250 Success := True;
5251 return;
5253 else
5254 if Reporting then
5256 -- Find some superfluous named actual that did not get
5257 -- attached to the list of associations.
5259 Actual := First (Actuals);
5261 while Present (Actual) loop
5262 if Nkind (Actual) = N_Parameter_Association
5263 and then Actual /= Last
5264 and then No (Next_Named_Actual (Actual))
5265 then
5266 Error_Msg_N ("unmatched actual & in call",
5267 Selector_Name (Actual));
5268 exit;
5269 end if;
5271 Next (Actual);
5272 end loop;
5273 end if;
5275 Success := False;
5276 return;
5277 end if;
5278 end Normalize_Actuals;
5280 --------------------------------
5281 -- Note_Possible_Modification --
5282 --------------------------------
5284 procedure Note_Possible_Modification (N : Node_Id) is
5285 Modification_Comes_From_Source : constant Boolean :=
5286 Comes_From_Source (Parent (N));
5288 Ent : Entity_Id;
5289 Exp : Node_Id;
5291 begin
5292 -- Loop to find referenced entity, if there is one
5294 Exp := N;
5295 loop
5296 <<Continue>>
5297 Ent := Empty;
5299 if Is_Entity_Name (Exp) then
5300 Ent := Entity (Exp);
5302 -- If the entity is missing, it is an undeclared identifier,
5303 -- and there is nothing to annotate.
5305 if No (Ent) then
5306 return;
5307 end if;
5309 elsif Nkind (Exp) = N_Explicit_Dereference then
5310 declare
5311 P : constant Node_Id := Prefix (Exp);
5313 begin
5314 if Nkind (P) = N_Selected_Component
5315 and then Present (
5316 Entry_Formal (Entity (Selector_Name (P))))
5317 then
5318 -- Case of a reference to an entry formal
5320 Ent := Entry_Formal (Entity (Selector_Name (P)));
5322 elsif Nkind (P) = N_Identifier
5323 and then Nkind (Parent (Entity (P))) = N_Object_Declaration
5324 and then Present (Expression (Parent (Entity (P))))
5325 and then Nkind (Expression (Parent (Entity (P))))
5326 = N_Reference
5327 then
5328 -- Case of a reference to a value on which
5329 -- side effects have been removed.
5331 Exp := Prefix (Expression (Parent (Entity (P))));
5333 else
5334 return;
5336 end if;
5337 end;
5339 elsif Nkind (Exp) = N_Type_Conversion
5340 or else Nkind (Exp) = N_Unchecked_Type_Conversion
5341 then
5342 Exp := Expression (Exp);
5344 elsif Nkind (Exp) = N_Slice
5345 or else Nkind (Exp) = N_Indexed_Component
5346 or else Nkind (Exp) = N_Selected_Component
5347 then
5348 Exp := Prefix (Exp);
5350 else
5351 return;
5353 end if;
5355 -- Now look for entity being referenced
5357 if Present (Ent) then
5359 if Is_Object (Ent) then
5360 if Comes_From_Source (Exp)
5361 or else Modification_Comes_From_Source
5362 then
5363 Set_Never_Set_In_Source (Ent, False);
5364 end if;
5366 Set_Is_True_Constant (Ent, False);
5367 Set_Current_Value (Ent, Empty);
5369 if not Can_Never_Be_Null (Ent) then
5370 Set_Is_Known_Non_Null (Ent, False);
5371 end if;
5373 if (Ekind (Ent) = E_Variable or else Ekind (Ent) = E_Constant)
5374 and then Present (Renamed_Object (Ent))
5375 then
5376 Exp := Renamed_Object (Ent);
5377 goto Continue;
5378 end if;
5380 -- Generate a reference only if the assignment comes from
5381 -- source. This excludes, for example, calls to a dispatching
5382 -- assignment operation when the left-hand side is tagged.
5384 if Modification_Comes_From_Source then
5385 Generate_Reference (Ent, Exp, 'm');
5386 end if;
5387 end if;
5389 Kill_Checks (Ent);
5390 return;
5391 end if;
5392 end loop;
5393 end Note_Possible_Modification;
5395 -------------------------
5396 -- Object_Access_Level --
5397 -------------------------
5399 function Object_Access_Level (Obj : Node_Id) return Uint is
5400 E : Entity_Id;
5402 -- Returns the static accessibility level of the view denoted
5403 -- by Obj. Note that the value returned is the result of a
5404 -- call to Scope_Depth. Only scope depths associated with
5405 -- dynamic scopes can actually be returned. Since only
5406 -- relative levels matter for accessibility checking, the fact
5407 -- that the distance between successive levels of accessibility
5408 -- is not always one is immaterial (invariant: if level(E2) is
5409 -- deeper than level(E1), then Scope_Depth(E1) < Scope_Depth(E2)).
5411 begin
5412 if Is_Entity_Name (Obj) then
5413 E := Entity (Obj);
5415 -- If E is a type then it denotes a current instance.
5416 -- For this case we add one to the normal accessibility
5417 -- level of the type to ensure that current instances
5418 -- are treated as always being deeper than than the level
5419 -- of any visible named access type (see 3.10.2(21)).
5421 if Is_Type (E) then
5422 return Type_Access_Level (E) + 1;
5424 elsif Present (Renamed_Object (E)) then
5425 return Object_Access_Level (Renamed_Object (E));
5427 -- Similarly, if E is a component of the current instance of a
5428 -- protected type, any instance of it is assumed to be at a deeper
5429 -- level than the type. For a protected object (whose type is an
5430 -- anonymous protected type) its components are at the same level
5431 -- as the type itself.
5433 elsif not Is_Overloadable (E)
5434 and then Ekind (Scope (E)) = E_Protected_Type
5435 and then Comes_From_Source (Scope (E))
5436 then
5437 return Type_Access_Level (Scope (E)) + 1;
5439 else
5440 return Scope_Depth (Enclosing_Dynamic_Scope (E));
5441 end if;
5443 elsif Nkind (Obj) = N_Selected_Component then
5444 if Is_Access_Type (Etype (Prefix (Obj))) then
5445 return Type_Access_Level (Etype (Prefix (Obj)));
5446 else
5447 return Object_Access_Level (Prefix (Obj));
5448 end if;
5450 elsif Nkind (Obj) = N_Indexed_Component then
5451 if Is_Access_Type (Etype (Prefix (Obj))) then
5452 return Type_Access_Level (Etype (Prefix (Obj)));
5453 else
5454 return Object_Access_Level (Prefix (Obj));
5455 end if;
5457 elsif Nkind (Obj) = N_Explicit_Dereference then
5459 -- If the prefix is a selected access discriminant then
5460 -- we make a recursive call on the prefix, which will
5461 -- in turn check the level of the prefix object of
5462 -- the selected discriminant.
5464 if Nkind (Prefix (Obj)) = N_Selected_Component
5465 and then Ekind (Etype (Prefix (Obj))) = E_Anonymous_Access_Type
5466 and then
5467 Ekind (Entity (Selector_Name (Prefix (Obj)))) = E_Discriminant
5468 then
5469 return Object_Access_Level (Prefix (Obj));
5470 else
5471 return Type_Access_Level (Etype (Prefix (Obj)));
5472 end if;
5474 elsif Nkind (Obj) = N_Type_Conversion
5475 or else Nkind (Obj) = N_Unchecked_Type_Conversion
5476 then
5477 return Object_Access_Level (Expression (Obj));
5479 -- Function results are objects, so we get either the access level
5480 -- of the function or, in the case of an indirect call, the level of
5481 -- of the access-to-subprogram type.
5483 elsif Nkind (Obj) = N_Function_Call then
5484 if Is_Entity_Name (Name (Obj)) then
5485 return Subprogram_Access_Level (Entity (Name (Obj)));
5486 else
5487 return Type_Access_Level (Etype (Prefix (Name (Obj))));
5488 end if;
5490 -- For convenience we handle qualified expressions, even though
5491 -- they aren't technically object names.
5493 elsif Nkind (Obj) = N_Qualified_Expression then
5494 return Object_Access_Level (Expression (Obj));
5496 -- Otherwise return the scope level of Standard.
5497 -- (If there are cases that fall through
5498 -- to this point they will be treated as
5499 -- having global accessibility for now. ???)
5501 else
5502 return Scope_Depth (Standard_Standard);
5503 end if;
5504 end Object_Access_Level;
5506 -----------------------
5507 -- Private_Component --
5508 -----------------------
5510 function Private_Component (Type_Id : Entity_Id) return Entity_Id is
5511 Ancestor : constant Entity_Id := Base_Type (Type_Id);
5513 function Trace_Components
5514 (T : Entity_Id;
5515 Check : Boolean) return Entity_Id;
5516 -- Recursive function that does the work, and checks against circular
5517 -- definition for each subcomponent type.
5519 ----------------------
5520 -- Trace_Components --
5521 ----------------------
5523 function Trace_Components
5524 (T : Entity_Id;
5525 Check : Boolean) return Entity_Id
5527 Btype : constant Entity_Id := Base_Type (T);
5528 Component : Entity_Id;
5529 P : Entity_Id;
5530 Candidate : Entity_Id := Empty;
5532 begin
5533 if Check and then Btype = Ancestor then
5534 Error_Msg_N ("circular type definition", Type_Id);
5535 return Any_Type;
5536 end if;
5538 if Is_Private_Type (Btype)
5539 and then not Is_Generic_Type (Btype)
5540 then
5541 if Present (Full_View (Btype))
5542 and then Is_Record_Type (Full_View (Btype))
5543 and then not Is_Frozen (Btype)
5544 then
5545 -- To indicate that the ancestor depends on a private type,
5546 -- the current Btype is sufficient. However, to check for
5547 -- circular definition we must recurse on the full view.
5549 Candidate := Trace_Components (Full_View (Btype), True);
5551 if Candidate = Any_Type then
5552 return Any_Type;
5553 else
5554 return Btype;
5555 end if;
5557 else
5558 return Btype;
5559 end if;
5561 elsif Is_Array_Type (Btype) then
5562 return Trace_Components (Component_Type (Btype), True);
5564 elsif Is_Record_Type (Btype) then
5565 Component := First_Entity (Btype);
5566 while Present (Component) loop
5568 -- Skip anonymous types generated by constrained components
5570 if not Is_Type (Component) then
5571 P := Trace_Components (Etype (Component), True);
5573 if Present (P) then
5574 if P = Any_Type then
5575 return P;
5576 else
5577 Candidate := P;
5578 end if;
5579 end if;
5580 end if;
5582 Next_Entity (Component);
5583 end loop;
5585 return Candidate;
5587 else
5588 return Empty;
5589 end if;
5590 end Trace_Components;
5592 -- Start of processing for Private_Component
5594 begin
5595 return Trace_Components (Type_Id, False);
5596 end Private_Component;
5598 -----------------------
5599 -- Process_End_Label --
5600 -----------------------
5602 procedure Process_End_Label
5603 (N : Node_Id;
5604 Typ : Character;
5605 Ent : Entity_Id)
5607 Loc : Source_Ptr;
5608 Nam : Node_Id;
5610 Label_Ref : Boolean;
5611 -- Set True if reference to end label itself is required
5613 Endl : Node_Id;
5614 -- Gets set to the operator symbol or identifier that references
5615 -- the entity Ent. For the child unit case, this is the identifier
5616 -- from the designator. For other cases, this is simply Endl.
5618 procedure Generate_Parent_Ref (N : Node_Id);
5619 -- N is an identifier node that appears as a parent unit reference
5620 -- in the case where Ent is a child unit. This procedure generates
5621 -- an appropriate cross-reference entry.
5623 -------------------------
5624 -- Generate_Parent_Ref --
5625 -------------------------
5627 procedure Generate_Parent_Ref (N : Node_Id) is
5628 Parent_Ent : Entity_Id;
5630 begin
5631 -- Search up scope stack. The reason we do this is that normal
5632 -- visibility analysis would not work for two reasons. First in
5633 -- some subunit cases, the entry for the parent unit may not be
5634 -- visible, and in any case there can be a local entity that
5635 -- hides the scope entity.
5637 Parent_Ent := Current_Scope;
5638 while Present (Parent_Ent) loop
5639 if Chars (Parent_Ent) = Chars (N) then
5641 -- Generate the reference. We do NOT consider this as a
5642 -- reference for unreferenced symbol purposes, but we do
5643 -- force a cross-reference even if the end line does not
5644 -- come from source (the caller already generated the
5645 -- appropriate Typ for this situation).
5647 Generate_Reference
5648 (Parent_Ent, N, 'r', Set_Ref => False, Force => True);
5649 Style.Check_Identifier (N, Parent_Ent);
5650 return;
5651 end if;
5653 Parent_Ent := Scope (Parent_Ent);
5654 end loop;
5656 -- Fall through means entity was not found -- that's odd, but
5657 -- the appropriate thing is simply to ignore and not generate
5658 -- any cross-reference for this entry.
5660 return;
5661 end Generate_Parent_Ref;
5663 -- Start of processing for Process_End_Label
5665 begin
5666 -- If no node, ignore. This happens in some error situations,
5667 -- and also for some internally generated structures where no
5668 -- end label references are required in any case.
5670 if No (N) then
5671 return;
5672 end if;
5674 -- Nothing to do if no End_Label, happens for internally generated
5675 -- constructs where we don't want an end label reference anyway.
5676 -- Also nothing to do if Endl is a string literal, which means
5677 -- there was some prior error (bad operator symbol)
5679 Endl := End_Label (N);
5681 if No (Endl) or else Nkind (Endl) = N_String_Literal then
5682 return;
5683 end if;
5685 -- Reference node is not in extended main source unit
5687 if not In_Extended_Main_Source_Unit (N) then
5689 -- Generally we do not collect references except for the
5690 -- extended main source unit. The one exception is the 'e'
5691 -- entry for a package spec, where it is useful for a client
5692 -- to have the ending information to define scopes.
5694 if Typ /= 'e' then
5695 return;
5697 else
5698 Label_Ref := False;
5700 -- For this case, we can ignore any parent references,
5701 -- but we need the package name itself for the 'e' entry.
5703 if Nkind (Endl) = N_Designator then
5704 Endl := Identifier (Endl);
5705 end if;
5706 end if;
5708 -- Reference is in extended main source unit
5710 else
5711 Label_Ref := True;
5713 -- For designator, generate references for the parent entries
5715 if Nkind (Endl) = N_Designator then
5717 -- Generate references for the prefix if the END line comes
5718 -- from source (otherwise we do not need these references)
5720 if Comes_From_Source (Endl) then
5721 Nam := Name (Endl);
5722 while Nkind (Nam) = N_Selected_Component loop
5723 Generate_Parent_Ref (Selector_Name (Nam));
5724 Nam := Prefix (Nam);
5725 end loop;
5727 Generate_Parent_Ref (Nam);
5728 end if;
5730 Endl := Identifier (Endl);
5731 end if;
5732 end if;
5734 -- If the end label is not for the given entity, then either we have
5735 -- some previous error, or this is a generic instantiation for which
5736 -- we do not need to make a cross-reference in this case anyway. In
5737 -- either case we simply ignore the call.
5739 if Chars (Ent) /= Chars (Endl) then
5740 return;
5741 end if;
5743 -- If label was really there, then generate a normal reference
5744 -- and then adjust the location in the end label to point past
5745 -- the name (which should almost always be the semicolon).
5747 Loc := Sloc (Endl);
5749 if Comes_From_Source (Endl) then
5751 -- If a label reference is required, then do the style check
5752 -- and generate an l-type cross-reference entry for the label
5754 if Label_Ref then
5755 if Style_Check then
5756 Style.Check_Identifier (Endl, Ent);
5757 end if;
5758 Generate_Reference (Ent, Endl, 'l', Set_Ref => False);
5759 end if;
5761 -- Set the location to point past the label (normally this will
5762 -- mean the semicolon immediately following the label). This is
5763 -- done for the sake of the 'e' or 't' entry generated below.
5765 Get_Decoded_Name_String (Chars (Endl));
5766 Set_Sloc (Endl, Sloc (Endl) + Source_Ptr (Name_Len));
5767 end if;
5769 -- Now generate the e/t reference
5771 Generate_Reference (Ent, Endl, Typ, Set_Ref => False, Force => True);
5773 -- Restore Sloc, in case modified above, since we have an identifier
5774 -- and the normal Sloc should be left set in the tree.
5776 Set_Sloc (Endl, Loc);
5777 end Process_End_Label;
5779 ------------------
5780 -- Real_Convert --
5781 ------------------
5783 -- We do the conversion to get the value of the real string by using
5784 -- the scanner, see Sinput for details on use of the internal source
5785 -- buffer for scanning internal strings.
5787 function Real_Convert (S : String) return Node_Id is
5788 Save_Src : constant Source_Buffer_Ptr := Source;
5789 Negative : Boolean;
5791 begin
5792 Source := Internal_Source_Ptr;
5793 Scan_Ptr := 1;
5795 for J in S'Range loop
5796 Source (Source_Ptr (J)) := S (J);
5797 end loop;
5799 Source (S'Length + 1) := EOF;
5801 if Source (Scan_Ptr) = '-' then
5802 Negative := True;
5803 Scan_Ptr := Scan_Ptr + 1;
5804 else
5805 Negative := False;
5806 end if;
5808 Scan;
5810 if Negative then
5811 Set_Realval (Token_Node, UR_Negate (Realval (Token_Node)));
5812 end if;
5814 Source := Save_Src;
5815 return Token_Node;
5816 end Real_Convert;
5818 ---------------------
5819 -- Rep_To_Pos_Flag --
5820 ---------------------
5822 function Rep_To_Pos_Flag (E : Entity_Id; Loc : Source_Ptr) return Node_Id is
5823 begin
5824 return New_Occurrence_Of
5825 (Boolean_Literals (not Range_Checks_Suppressed (E)), Loc);
5826 end Rep_To_Pos_Flag;
5828 --------------------
5829 -- Require_Entity --
5830 --------------------
5832 procedure Require_Entity (N : Node_Id) is
5833 begin
5834 if Is_Entity_Name (N) and then No (Entity (N)) then
5835 if Total_Errors_Detected /= 0 then
5836 Set_Entity (N, Any_Id);
5837 else
5838 raise Program_Error;
5839 end if;
5840 end if;
5841 end Require_Entity;
5843 ------------------------------
5844 -- Requires_Transient_Scope --
5845 ------------------------------
5847 -- A transient scope is required when variable-sized temporaries are
5848 -- allocated in the primary or secondary stack, or when finalization
5849 -- actions must be generated before the next instruction.
5851 function Requires_Transient_Scope (Id : Entity_Id) return Boolean is
5852 Typ : constant Entity_Id := Underlying_Type (Id);
5854 -- Start of processing for Requires_Transient_Scope
5856 begin
5857 -- This is a private type which is not completed yet. This can only
5858 -- happen in a default expression (of a formal parameter or of a
5859 -- record component). Do not expand transient scope in this case
5861 if No (Typ) then
5862 return False;
5864 -- Do not expand transient scope for non-existent procedure return
5866 elsif Typ = Standard_Void_Type then
5867 return False;
5869 -- Elementary types do not require a transient scope
5871 elsif Is_Elementary_Type (Typ) then
5872 return False;
5874 -- Generally, indefinite subtypes require a transient scope, since the
5875 -- back end cannot generate temporaries, since this is not a valid type
5876 -- for declaring an object. It might be possible to relax this in the
5877 -- future, e.g. by declaring the maximum possible space for the type.
5879 elsif Is_Indefinite_Subtype (Typ) then
5880 return True;
5882 -- Functions returning tagged types may dispatch on result so their
5883 -- returned value is allocated on the secondary stack. Controlled
5884 -- type temporaries need finalization.
5886 elsif Is_Tagged_Type (Typ)
5887 or else Has_Controlled_Component (Typ)
5888 then
5889 return True;
5891 -- Record type
5893 elsif Is_Record_Type (Typ) then
5895 -- In GCC 2, discriminated records always require a transient
5896 -- scope because the back end otherwise tries to allocate a
5897 -- variable length temporary for the particular variant.
5899 if Opt.GCC_Version = 2
5900 and then Has_Discriminants (Typ)
5901 then
5902 return True;
5904 -- For GCC 3, or for a non-discriminated record in GCC 2, we are
5905 -- OK if none of the component types requires a transient scope.
5906 -- Note that we already know that this is a definite type (i.e.
5907 -- has discriminant defaults if it is a discriminated record).
5909 else
5910 declare
5911 Comp : Entity_Id;
5912 begin
5913 Comp := First_Entity (Typ);
5914 while Present (Comp) loop
5915 if Ekind (Comp) = E_Component
5916 and then Requires_Transient_Scope (Etype (Comp))
5917 then
5918 return True;
5919 else
5920 Next_Entity (Comp);
5921 end if;
5922 end loop;
5923 end;
5925 return False;
5926 end if;
5928 -- String literal types never require transient scope
5930 elsif Ekind (Typ) = E_String_Literal_Subtype then
5931 return False;
5933 -- Array type. Note that we already know that this is a constrained
5934 -- array, since unconstrained arrays will fail the indefinite test.
5936 elsif Is_Array_Type (Typ) then
5938 -- If component type requires a transient scope, the array does too
5940 if Requires_Transient_Scope (Component_Type (Typ)) then
5941 return True;
5943 -- Otherwise, we only need a transient scope if the size is not
5944 -- known at compile time.
5946 else
5947 return not Size_Known_At_Compile_Time (Typ);
5948 end if;
5950 -- All other cases do not require a transient scope
5952 else
5953 return False;
5954 end if;
5955 end Requires_Transient_Scope;
5957 --------------------------
5958 -- Reset_Analyzed_Flags --
5959 --------------------------
5961 procedure Reset_Analyzed_Flags (N : Node_Id) is
5963 function Clear_Analyzed
5964 (N : Node_Id) return Traverse_Result;
5965 -- Function used to reset Analyzed flags in tree. Note that we do
5966 -- not reset Analyzed flags in entities, since there is no need to
5967 -- renalalyze entities, and indeed, it is wrong to do so, since it
5968 -- can result in generating auxiliary stuff more than once.
5970 --------------------
5971 -- Clear_Analyzed --
5972 --------------------
5974 function Clear_Analyzed
5975 (N : Node_Id) return Traverse_Result
5977 begin
5978 if not Has_Extension (N) then
5979 Set_Analyzed (N, False);
5980 end if;
5982 return OK;
5983 end Clear_Analyzed;
5985 function Reset_Analyzed is
5986 new Traverse_Func (Clear_Analyzed);
5988 Discard : Traverse_Result;
5989 pragma Warnings (Off, Discard);
5991 -- Start of processing for Reset_Analyzed_Flags
5993 begin
5994 Discard := Reset_Analyzed (N);
5995 end Reset_Analyzed_Flags;
5997 ---------------------------
5998 -- Safe_To_Capture_Value --
5999 ---------------------------
6001 function Safe_To_Capture_Value
6002 (N : Node_Id;
6003 Ent : Entity_Id) return Boolean
6005 begin
6006 -- The only entities for which we track constant values are variables,
6007 -- out parameters and in out parameters, so check if we have this case.
6009 if Ekind (Ent) /= E_Variable
6010 and then
6011 Ekind (Ent) /= E_Out_Parameter
6012 and then
6013 Ekind (Ent) /= E_In_Out_Parameter
6014 then
6015 return False;
6016 end if;
6018 -- Skip volatile and aliased variables, since funny things might
6019 -- be going on in these cases which we cannot necessarily track.
6021 if Treat_As_Volatile (Ent) or else Is_Aliased (Ent) then
6022 return False;
6023 end if;
6025 -- OK, all above conditions are met. We also require that the scope
6026 -- of the reference be the same as the scope of the entity, not
6027 -- counting packages and blocks.
6029 declare
6030 E_Scope : constant Entity_Id := Scope (Ent);
6031 R_Scope : Entity_Id;
6033 begin
6034 R_Scope := Current_Scope;
6035 while R_Scope /= Standard_Standard loop
6036 exit when R_Scope = E_Scope;
6038 if Ekind (R_Scope) /= E_Package
6039 and then
6040 Ekind (R_Scope) /= E_Block
6041 then
6042 return False;
6043 else
6044 R_Scope := Scope (R_Scope);
6045 end if;
6046 end loop;
6047 end;
6049 -- We also require that the reference does not appear in a context
6050 -- where it is not sure to be executed (i.e. a conditional context
6051 -- or an exception handler).
6053 declare
6054 P : Node_Id;
6056 begin
6057 P := Parent (N);
6058 while Present (P) loop
6059 if Nkind (P) = N_If_Statement
6060 or else
6061 Nkind (P) = N_Case_Statement
6062 or else
6063 Nkind (P) = N_Exception_Handler
6064 or else
6065 Nkind (P) = N_Selective_Accept
6066 or else
6067 Nkind (P) = N_Conditional_Entry_Call
6068 or else
6069 Nkind (P) = N_Timed_Entry_Call
6070 or else
6071 Nkind (P) = N_Asynchronous_Select
6072 then
6073 return False;
6074 else
6075 P := Parent (P);
6076 end if;
6077 end loop;
6078 end;
6080 -- OK, looks safe to set value
6082 return True;
6083 end Safe_To_Capture_Value;
6085 ---------------
6086 -- Same_Name --
6087 ---------------
6089 function Same_Name (N1, N2 : Node_Id) return Boolean is
6090 K1 : constant Node_Kind := Nkind (N1);
6091 K2 : constant Node_Kind := Nkind (N2);
6093 begin
6094 if (K1 = N_Identifier or else K1 = N_Defining_Identifier)
6095 and then (K2 = N_Identifier or else K2 = N_Defining_Identifier)
6096 then
6097 return Chars (N1) = Chars (N2);
6099 elsif (K1 = N_Selected_Component or else K1 = N_Expanded_Name)
6100 and then (K2 = N_Selected_Component or else K2 = N_Expanded_Name)
6101 then
6102 return Same_Name (Selector_Name (N1), Selector_Name (N2))
6103 and then Same_Name (Prefix (N1), Prefix (N2));
6105 else
6106 return False;
6107 end if;
6108 end Same_Name;
6110 ---------------
6111 -- Same_Type --
6112 ---------------
6114 function Same_Type (T1, T2 : Entity_Id) return Boolean is
6115 begin
6116 if T1 = T2 then
6117 return True;
6119 elsif not Is_Constrained (T1)
6120 and then not Is_Constrained (T2)
6121 and then Base_Type (T1) = Base_Type (T2)
6122 then
6123 return True;
6125 -- For now don't bother with case of identical constraints, to be
6126 -- fiddled with later on perhaps (this is only used for optimization
6127 -- purposes, so it is not critical to do a best possible job)
6129 else
6130 return False;
6131 end if;
6132 end Same_Type;
6134 ------------------------
6135 -- Scope_Is_Transient --
6136 ------------------------
6138 function Scope_Is_Transient return Boolean is
6139 begin
6140 return Scope_Stack.Table (Scope_Stack.Last).Is_Transient;
6141 end Scope_Is_Transient;
6143 ------------------
6144 -- Scope_Within --
6145 ------------------
6147 function Scope_Within (Scope1, Scope2 : Entity_Id) return Boolean is
6148 Scop : Entity_Id;
6150 begin
6151 Scop := Scope1;
6152 while Scop /= Standard_Standard loop
6153 Scop := Scope (Scop);
6155 if Scop = Scope2 then
6156 return True;
6157 end if;
6158 end loop;
6160 return False;
6161 end Scope_Within;
6163 --------------------------
6164 -- Scope_Within_Or_Same --
6165 --------------------------
6167 function Scope_Within_Or_Same (Scope1, Scope2 : Entity_Id) return Boolean is
6168 Scop : Entity_Id;
6170 begin
6171 Scop := Scope1;
6172 while Scop /= Standard_Standard loop
6173 if Scop = Scope2 then
6174 return True;
6175 else
6176 Scop := Scope (Scop);
6177 end if;
6178 end loop;
6180 return False;
6181 end Scope_Within_Or_Same;
6183 ------------------------
6184 -- Set_Current_Entity --
6185 ------------------------
6187 -- The given entity is to be set as the currently visible definition
6188 -- of its associated name (i.e. the Node_Id associated with its name).
6189 -- All we have to do is to get the name from the identifier, and
6190 -- then set the associated Node_Id to point to the given entity.
6192 procedure Set_Current_Entity (E : Entity_Id) is
6193 begin
6194 Set_Name_Entity_Id (Chars (E), E);
6195 end Set_Current_Entity;
6197 ---------------------------------
6198 -- Set_Entity_With_Style_Check --
6199 ---------------------------------
6201 procedure Set_Entity_With_Style_Check (N : Node_Id; Val : Entity_Id) is
6202 Val_Actual : Entity_Id;
6203 Nod : Node_Id;
6205 begin
6206 Set_Entity (N, Val);
6208 if Style_Check
6209 and then not Suppress_Style_Checks (Val)
6210 and then not In_Instance
6211 then
6212 if Nkind (N) = N_Identifier then
6213 Nod := N;
6215 elsif Nkind (N) = N_Expanded_Name then
6216 Nod := Selector_Name (N);
6218 else
6219 return;
6220 end if;
6222 Val_Actual := Val;
6224 -- A special situation arises for derived operations, where we want
6225 -- to do the check against the parent (since the Sloc of the derived
6226 -- operation points to the derived type declaration itself).
6228 while not Comes_From_Source (Val_Actual)
6229 and then Nkind (Val_Actual) in N_Entity
6230 and then (Ekind (Val_Actual) = E_Enumeration_Literal
6231 or else Is_Subprogram (Val_Actual)
6232 or else Is_Generic_Subprogram (Val_Actual))
6233 and then Present (Alias (Val_Actual))
6234 loop
6235 Val_Actual := Alias (Val_Actual);
6236 end loop;
6238 -- Renaming declarations for generic actuals do not come from source,
6239 -- and have a different name from that of the entity they rename, so
6240 -- there is no style check to perform here.
6242 if Chars (Nod) = Chars (Val_Actual) then
6243 Style.Check_Identifier (Nod, Val_Actual);
6244 end if;
6245 end if;
6247 Set_Entity (N, Val);
6248 end Set_Entity_With_Style_Check;
6250 ------------------------
6251 -- Set_Name_Entity_Id --
6252 ------------------------
6254 procedure Set_Name_Entity_Id (Id : Name_Id; Val : Entity_Id) is
6255 begin
6256 Set_Name_Table_Info (Id, Int (Val));
6257 end Set_Name_Entity_Id;
6259 ---------------------
6260 -- Set_Next_Actual --
6261 ---------------------
6263 procedure Set_Next_Actual (Ass1_Id : Node_Id; Ass2_Id : Node_Id) is
6264 begin
6265 if Nkind (Parent (Ass1_Id)) = N_Parameter_Association then
6266 Set_First_Named_Actual (Parent (Ass1_Id), Ass2_Id);
6267 end if;
6268 end Set_Next_Actual;
6270 -----------------------
6271 -- Set_Public_Status --
6272 -----------------------
6274 procedure Set_Public_Status (Id : Entity_Id) is
6275 S : constant Entity_Id := Current_Scope;
6277 begin
6278 if S = Standard_Standard
6279 or else (Is_Public (S)
6280 and then (Ekind (S) = E_Package
6281 or else Is_Record_Type (S)
6282 or else Ekind (S) = E_Void))
6283 then
6284 Set_Is_Public (Id);
6286 -- The bounds of an entry family declaration can generate object
6287 -- declarations that are visible to the back-end, e.g. in the
6288 -- the declaration of a composite type that contains tasks.
6290 elsif Is_Public (S)
6291 and then Is_Concurrent_Type (S)
6292 and then not Has_Completion (S)
6293 and then Nkind (Parent (Id)) = N_Object_Declaration
6294 then
6295 Set_Is_Public (Id);
6296 end if;
6297 end Set_Public_Status;
6299 ----------------------------
6300 -- Set_Scope_Is_Transient --
6301 ----------------------------
6303 procedure Set_Scope_Is_Transient (V : Boolean := True) is
6304 begin
6305 Scope_Stack.Table (Scope_Stack.Last).Is_Transient := V;
6306 end Set_Scope_Is_Transient;
6308 -------------------
6309 -- Set_Size_Info --
6310 -------------------
6312 procedure Set_Size_Info (T1, T2 : Entity_Id) is
6313 begin
6314 -- We copy Esize, but not RM_Size, since in general RM_Size is
6315 -- subtype specific and does not get inherited by all subtypes.
6317 Set_Esize (T1, Esize (T2));
6318 Set_Has_Biased_Representation (T1, Has_Biased_Representation (T2));
6320 if Is_Discrete_Or_Fixed_Point_Type (T1)
6321 and then
6322 Is_Discrete_Or_Fixed_Point_Type (T2)
6323 then
6324 Set_Is_Unsigned_Type (T1, Is_Unsigned_Type (T2));
6325 end if;
6326 Set_Alignment (T1, Alignment (T2));
6327 end Set_Size_Info;
6329 --------------------
6330 -- Static_Integer --
6331 --------------------
6333 function Static_Integer (N : Node_Id) return Uint is
6334 begin
6335 Analyze_And_Resolve (N, Any_Integer);
6337 if N = Error
6338 or else Error_Posted (N)
6339 or else Etype (N) = Any_Type
6340 then
6341 return No_Uint;
6342 end if;
6344 if Is_Static_Expression (N) then
6345 if not Raises_Constraint_Error (N) then
6346 return Expr_Value (N);
6347 else
6348 return No_Uint;
6349 end if;
6351 elsif Etype (N) = Any_Type then
6352 return No_Uint;
6354 else
6355 Flag_Non_Static_Expr
6356 ("static integer expression required here", N);
6357 return No_Uint;
6358 end if;
6359 end Static_Integer;
6361 --------------------------
6362 -- Statically_Different --
6363 --------------------------
6365 function Statically_Different (E1, E2 : Node_Id) return Boolean is
6366 R1 : constant Node_Id := Get_Referenced_Object (E1);
6367 R2 : constant Node_Id := Get_Referenced_Object (E2);
6368 begin
6369 return Is_Entity_Name (R1)
6370 and then Is_Entity_Name (R2)
6371 and then Entity (R1) /= Entity (R2)
6372 and then not Is_Formal (Entity (R1))
6373 and then not Is_Formal (Entity (R2));
6374 end Statically_Different;
6376 -----------------------------
6377 -- Subprogram_Access_Level --
6378 -----------------------------
6380 function Subprogram_Access_Level (Subp : Entity_Id) return Uint is
6381 begin
6382 if Present (Alias (Subp)) then
6383 return Subprogram_Access_Level (Alias (Subp));
6384 else
6385 return Scope_Depth (Enclosing_Dynamic_Scope (Subp));
6386 end if;
6387 end Subprogram_Access_Level;
6389 -----------------
6390 -- Trace_Scope --
6391 -----------------
6393 procedure Trace_Scope (N : Node_Id; E : Entity_Id; Msg : String) is
6394 begin
6395 if Debug_Flag_W then
6396 for J in 0 .. Scope_Stack.Last loop
6397 Write_Str (" ");
6398 end loop;
6400 Write_Str (Msg);
6401 Write_Name (Chars (E));
6402 Write_Str (" line ");
6403 Write_Int (Int (Get_Logical_Line_Number (Sloc (N))));
6404 Write_Eol;
6405 end if;
6406 end Trace_Scope;
6408 -----------------------
6409 -- Transfer_Entities --
6410 -----------------------
6412 procedure Transfer_Entities (From : Entity_Id; To : Entity_Id) is
6413 Ent : Entity_Id := First_Entity (From);
6415 begin
6416 if No (Ent) then
6417 return;
6418 end if;
6420 if (Last_Entity (To)) = Empty then
6421 Set_First_Entity (To, Ent);
6422 else
6423 Set_Next_Entity (Last_Entity (To), Ent);
6424 end if;
6426 Set_Last_Entity (To, Last_Entity (From));
6428 while Present (Ent) loop
6429 Set_Scope (Ent, To);
6431 if not Is_Public (Ent) then
6432 Set_Public_Status (Ent);
6434 if Is_Public (Ent)
6435 and then Ekind (Ent) = E_Record_Subtype
6437 then
6438 -- The components of the propagated Itype must be public
6439 -- as well.
6441 declare
6442 Comp : Entity_Id;
6444 begin
6445 Comp := First_Entity (Ent);
6447 while Present (Comp) loop
6448 Set_Is_Public (Comp);
6449 Next_Entity (Comp);
6450 end loop;
6451 end;
6452 end if;
6453 end if;
6455 Next_Entity (Ent);
6456 end loop;
6458 Set_First_Entity (From, Empty);
6459 Set_Last_Entity (From, Empty);
6460 end Transfer_Entities;
6462 -----------------------
6463 -- Type_Access_Level --
6464 -----------------------
6466 function Type_Access_Level (Typ : Entity_Id) return Uint is
6467 Btyp : Entity_Id;
6469 begin
6470 -- If the type is an anonymous access type we treat it as being
6471 -- declared at the library level to ensure that names such as
6472 -- X.all'access don't fail static accessibility checks.
6474 -- Ada 2005 (AI-230): In case of anonymous access types that are
6475 -- component_definition or discriminants of a nonlimited type,
6476 -- the level is the same as that of the enclosing component type.
6478 Btyp := Base_Type (Typ);
6479 if Ekind (Btyp) in Access_Kind then
6480 if Ekind (Btyp) = E_Anonymous_Access_Type
6481 and then not Is_Array_Type (Scope (Btyp)) -- Ada 2005 (AI-230)
6482 and then Ekind (Scope (Btyp)) /= E_Record_Type -- Ada 2005 (AI-230)
6483 then
6484 return Scope_Depth (Standard_Standard);
6485 end if;
6487 Btyp := Root_Type (Btyp);
6488 end if;
6490 return Scope_Depth (Enclosing_Dynamic_Scope (Btyp));
6491 end Type_Access_Level;
6493 --------------------------
6494 -- Unit_Declaration_Node --
6495 --------------------------
6497 function Unit_Declaration_Node (Unit_Id : Entity_Id) return Node_Id is
6498 N : Node_Id := Parent (Unit_Id);
6500 begin
6501 -- Predefined operators do not have a full function declaration
6503 if Ekind (Unit_Id) = E_Operator then
6504 return N;
6505 end if;
6507 while Nkind (N) /= N_Abstract_Subprogram_Declaration
6508 and then Nkind (N) /= N_Formal_Package_Declaration
6509 and then Nkind (N) /= N_Function_Instantiation
6510 and then Nkind (N) /= N_Generic_Package_Declaration
6511 and then Nkind (N) /= N_Generic_Subprogram_Declaration
6512 and then Nkind (N) /= N_Package_Declaration
6513 and then Nkind (N) /= N_Package_Body
6514 and then Nkind (N) /= N_Package_Instantiation
6515 and then Nkind (N) /= N_Package_Renaming_Declaration
6516 and then Nkind (N) /= N_Procedure_Instantiation
6517 and then Nkind (N) /= N_Protected_Body
6518 and then Nkind (N) /= N_Subprogram_Declaration
6519 and then Nkind (N) /= N_Subprogram_Body
6520 and then Nkind (N) /= N_Subprogram_Body_Stub
6521 and then Nkind (N) /= N_Subprogram_Renaming_Declaration
6522 and then Nkind (N) /= N_Task_Body
6523 and then Nkind (N) /= N_Task_Type_Declaration
6524 and then Nkind (N) not in N_Formal_Subprogram_Declaration
6525 and then Nkind (N) not in N_Generic_Renaming_Declaration
6526 loop
6527 N := Parent (N);
6528 pragma Assert (Present (N));
6529 end loop;
6531 return N;
6532 end Unit_Declaration_Node;
6534 ------------------------------
6535 -- Universal_Interpretation --
6536 ------------------------------
6538 function Universal_Interpretation (Opnd : Node_Id) return Entity_Id is
6539 Index : Interp_Index;
6540 It : Interp;
6542 begin
6543 -- The argument may be a formal parameter of an operator or subprogram
6544 -- with multiple interpretations, or else an expression for an actual.
6546 if Nkind (Opnd) = N_Defining_Identifier
6547 or else not Is_Overloaded (Opnd)
6548 then
6549 if Etype (Opnd) = Universal_Integer
6550 or else Etype (Opnd) = Universal_Real
6551 then
6552 return Etype (Opnd);
6553 else
6554 return Empty;
6555 end if;
6557 else
6558 Get_First_Interp (Opnd, Index, It);
6560 while Present (It.Typ) loop
6562 if It.Typ = Universal_Integer
6563 or else It.Typ = Universal_Real
6564 then
6565 return It.Typ;
6566 end if;
6568 Get_Next_Interp (Index, It);
6569 end loop;
6571 return Empty;
6572 end if;
6573 end Universal_Interpretation;
6575 ----------------------
6576 -- Within_Init_Proc --
6577 ----------------------
6579 function Within_Init_Proc return Boolean is
6580 S : Entity_Id;
6582 begin
6583 S := Current_Scope;
6584 while not Is_Overloadable (S) loop
6585 if S = Standard_Standard then
6586 return False;
6587 else
6588 S := Scope (S);
6589 end if;
6590 end loop;
6592 return Is_Init_Proc (S);
6593 end Within_Init_Proc;
6595 ----------------
6596 -- Wrong_Type --
6597 ----------------
6599 procedure Wrong_Type (Expr : Node_Id; Expected_Type : Entity_Id) is
6600 Found_Type : constant Entity_Id := First_Subtype (Etype (Expr));
6601 Expec_Type : constant Entity_Id := First_Subtype (Expected_Type);
6603 function Has_One_Matching_Field return Boolean;
6604 -- Determines if Expec_Type is a record type with a single component or
6605 -- discriminant whose type matches the found type or is one dimensional
6606 -- array whose component type matches the found type.
6608 ----------------------------
6609 -- Has_One_Matching_Field --
6610 ----------------------------
6612 function Has_One_Matching_Field return Boolean is
6613 E : Entity_Id;
6615 begin
6616 if Is_Array_Type (Expec_Type)
6617 and then Number_Dimensions (Expec_Type) = 1
6618 and then
6619 Covers (Etype (Component_Type (Expec_Type)), Found_Type)
6620 then
6621 return True;
6623 elsif not Is_Record_Type (Expec_Type) then
6624 return False;
6626 else
6627 E := First_Entity (Expec_Type);
6628 loop
6629 if No (E) then
6630 return False;
6632 elsif (Ekind (E) /= E_Discriminant
6633 and then Ekind (E) /= E_Component)
6634 or else (Chars (E) = Name_uTag
6635 or else Chars (E) = Name_uParent)
6636 then
6637 Next_Entity (E);
6639 else
6640 exit;
6641 end if;
6642 end loop;
6644 if not Covers (Etype (E), Found_Type) then
6645 return False;
6647 elsif Present (Next_Entity (E)) then
6648 return False;
6650 else
6651 return True;
6652 end if;
6653 end if;
6654 end Has_One_Matching_Field;
6656 -- Start of processing for Wrong_Type
6658 begin
6659 -- Don't output message if either type is Any_Type, or if a message
6660 -- has already been posted for this node. We need to do the latter
6661 -- check explicitly (it is ordinarily done in Errout), because we
6662 -- are using ! to force the output of the error messages.
6664 if Expec_Type = Any_Type
6665 or else Found_Type = Any_Type
6666 or else Error_Posted (Expr)
6667 then
6668 return;
6670 -- In an instance, there is an ongoing problem with completion of
6671 -- type derived from private types. Their structure is what Gigi
6672 -- expects, but the Etype is the parent type rather than the
6673 -- derived private type itself. Do not flag error in this case. The
6674 -- private completion is an entity without a parent, like an Itype.
6675 -- Similarly, full and partial views may be incorrect in the instance.
6676 -- There is no simple way to insure that it is consistent ???
6678 elsif In_Instance then
6680 if Etype (Etype (Expr)) = Etype (Expected_Type)
6681 and then
6682 (Has_Private_Declaration (Expected_Type)
6683 or else Has_Private_Declaration (Etype (Expr)))
6684 and then No (Parent (Expected_Type))
6685 then
6686 return;
6687 end if;
6688 end if;
6690 -- An interesting special check. If the expression is parenthesized
6691 -- and its type corresponds to the type of the sole component of the
6692 -- expected record type, or to the component type of the expected one
6693 -- dimensional array type, then assume we have a bad aggregate attempt.
6695 if Nkind (Expr) in N_Subexpr
6696 and then Paren_Count (Expr) /= 0
6697 and then Has_One_Matching_Field
6698 then
6699 Error_Msg_N ("positional aggregate cannot have one component", Expr);
6701 -- Another special check, if we are looking for a pool-specific access
6702 -- type and we found an E_Access_Attribute_Type, then we have the case
6703 -- of an Access attribute being used in a context which needs a pool-
6704 -- specific type, which is never allowed. The one extra check we make
6705 -- is that the expected designated type covers the Found_Type.
6707 elsif Is_Access_Type (Expec_Type)
6708 and then Ekind (Found_Type) = E_Access_Attribute_Type
6709 and then Ekind (Base_Type (Expec_Type)) /= E_General_Access_Type
6710 and then Ekind (Base_Type (Expec_Type)) /= E_Anonymous_Access_Type
6711 and then Covers
6712 (Designated_Type (Expec_Type), Designated_Type (Found_Type))
6713 then
6714 Error_Msg_N ("result must be general access type!", Expr);
6715 Error_Msg_NE ("add ALL to }!", Expr, Expec_Type);
6717 -- If the expected type is an anonymous access type, as for access
6718 -- parameters and discriminants, the error is on the designated types.
6720 elsif Ekind (Expec_Type) = E_Anonymous_Access_Type then
6721 if Comes_From_Source (Expec_Type) then
6722 Error_Msg_NE ("expected}!", Expr, Expec_Type);
6723 else
6724 Error_Msg_NE
6725 ("expected an access type with designated}",
6726 Expr, Designated_Type (Expec_Type));
6727 end if;
6729 if Is_Access_Type (Found_Type)
6730 and then not Comes_From_Source (Found_Type)
6731 then
6732 Error_Msg_NE
6733 ("found an access type with designated}!",
6734 Expr, Designated_Type (Found_Type));
6735 else
6736 if From_With_Type (Found_Type) then
6737 Error_Msg_NE ("found incomplete}!", Expr, Found_Type);
6738 Error_Msg_NE
6739 ("\possibly missing with_clause on&", Expr,
6740 Scope (Found_Type));
6741 else
6742 Error_Msg_NE ("found}!", Expr, Found_Type);
6743 end if;
6744 end if;
6746 -- Normal case of one type found, some other type expected
6748 else
6749 -- If the names of the two types are the same, see if some
6750 -- number of levels of qualification will help. Don't try
6751 -- more than three levels, and if we get to standard, it's
6752 -- no use (and probably represents an error in the compiler)
6753 -- Also do not bother with internal scope names.
6755 declare
6756 Expec_Scope : Entity_Id;
6757 Found_Scope : Entity_Id;
6759 begin
6760 Expec_Scope := Expec_Type;
6761 Found_Scope := Found_Type;
6763 for Levels in Int range 0 .. 3 loop
6764 if Chars (Expec_Scope) /= Chars (Found_Scope) then
6765 Error_Msg_Qual_Level := Levels;
6766 exit;
6767 end if;
6769 Expec_Scope := Scope (Expec_Scope);
6770 Found_Scope := Scope (Found_Scope);
6772 exit when Expec_Scope = Standard_Standard
6773 or else
6774 Found_Scope = Standard_Standard
6775 or else
6776 not Comes_From_Source (Expec_Scope)
6777 or else
6778 not Comes_From_Source (Found_Scope);
6779 end loop;
6780 end;
6782 Error_Msg_NE ("expected}!", Expr, Expec_Type);
6784 if Is_Entity_Name (Expr)
6785 and then Is_Package (Entity (Expr))
6786 then
6787 Error_Msg_N ("found package name!", Expr);
6789 elsif Is_Entity_Name (Expr)
6790 and then
6791 (Ekind (Entity (Expr)) = E_Procedure
6792 or else
6793 Ekind (Entity (Expr)) = E_Generic_Procedure)
6794 then
6795 if Ekind (Expec_Type) = E_Access_Subprogram_Type then
6796 Error_Msg_N
6797 ("found procedure name, possibly missing Access attribute!",
6798 Expr);
6799 else
6800 Error_Msg_N ("found procedure name instead of function!", Expr);
6801 end if;
6803 elsif Nkind (Expr) = N_Function_Call
6804 and then Ekind (Expec_Type) = E_Access_Subprogram_Type
6805 and then Etype (Designated_Type (Expec_Type)) = Etype (Expr)
6806 and then No (Parameter_Associations (Expr))
6807 then
6808 Error_Msg_N
6809 ("found function name, possibly missing Access attribute!",
6810 Expr);
6812 -- Catch common error: a prefix or infix operator which is not
6813 -- directly visible because the type isn't.
6815 elsif Nkind (Expr) in N_Op
6816 and then Is_Overloaded (Expr)
6817 and then not Is_Immediately_Visible (Expec_Type)
6818 and then not Is_Potentially_Use_Visible (Expec_Type)
6819 and then not In_Use (Expec_Type)
6820 and then Has_Compatible_Type (Right_Opnd (Expr), Expec_Type)
6821 then
6822 Error_Msg_N
6823 ("operator of the type is not directly visible!", Expr);
6825 elsif Ekind (Found_Type) = E_Void
6826 and then Present (Parent (Found_Type))
6827 and then Nkind (Parent (Found_Type)) = N_Full_Type_Declaration
6828 then
6829 Error_Msg_NE ("found premature usage of}!", Expr, Found_Type);
6831 else
6832 Error_Msg_NE ("found}!", Expr, Found_Type);
6833 end if;
6835 Error_Msg_Qual_Level := 0;
6836 end if;
6837 end Wrong_Type;
6839 end Sem_Util;