OSDN Git Service

PR fortran/23516
[pf3gnuchains/gcc-fork.git] / gcc / ada / sem_util.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                             S E M _ U T I L                              --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-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,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, 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 ------------------------------------------------------------------------------
26
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;
62
63 package body Sem_Util is
64
65    -----------------------
66    -- Local Subprograms --
67    -----------------------
68
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.
76
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.
82
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.
86
87    --------------------------------
88    -- Add_Access_Type_To_Process --
89    --------------------------------
90
91    procedure Add_Access_Type_To_Process (E : Entity_Id; A : Entity_Id) is
92       L : Elist_Id;
93
94    begin
95       Ensure_Freeze_Node (E);
96       L := Access_Types_To_Process (Freeze_Node (E));
97
98       if No (L) then
99          L := New_Elmt_List;
100          Set_Access_Types_To_Process (Freeze_Node (E), L);
101       end if;
102
103       Append_Elmt (A, L);
104    end Add_Access_Type_To_Process;
105
106    -----------------------
107    -- Alignment_In_Bits --
108    -----------------------
109
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;
114
115    -----------------------------------------
116    -- Apply_Compile_Time_Constraint_Error --
117    -----------------------------------------
118
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)
128    is
129       Stat : constant Boolean := Is_Static_Expression (N);
130       Rtyp : Entity_Id;
131
132    begin
133       if No (Typ) then
134          Rtyp := Etype (N);
135       else
136          Rtyp := Typ;
137       end if;
138
139       Discard_Node (
140         Compile_Time_Constraint_Error (N, Msg, Ent, Loc, Warn => Warn));
141
142       if not Rep then
143          return;
144       end if;
145
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.
148
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);
155
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.
159
160       if Stat then
161          Set_Is_Static_Expression (N);
162       end if;
163    end Apply_Compile_Time_Constraint_Error;
164
165    --------------------------
166    -- Build_Actual_Subtype --
167    --------------------------
168
169    function Build_Actual_Subtype
170      (T : Entity_Id;
171       N : Node_Or_Entity_Id) return Node_Id
172    is
173       Obj : Node_Id;
174
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;
183
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;
190
191       if Is_Array_Type (T) then
192          Constraints := New_List;
193
194          for J in 1 .. Number_Dimensions (T) loop
195
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.
200
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)));
208
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)));
216
217             Append (Make_Range (Loc, Lo, Hi), Constraints);
218          end loop;
219
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 ???
223
224       elsif Has_Unknown_Discriminants (T) then
225          return T;
226
227       else
228          Constraints := New_List;
229
230          if Is_Private_Type (T) and then No (Full_View (T)) then
231
232             --  Type is a generic derived type. Inherit discriminants from
233             --  Parent type.
234
235             Disc_Type := Etype (Base_Type (T));
236          else
237             Disc_Type := T;
238          end if;
239
240          Discr := First_Discriminant (Disc_Type);
241
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;
251
252       Subt :=
253         Make_Defining_Identifier (Loc,
254           Chars => New_Internal_Name ('S'));
255       Set_Is_Internal (Subt);
256
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)));
266
267       Mark_Rewrite_Insertion (Decl);
268       return Decl;
269    end Build_Actual_Subtype;
270
271    ---------------------------------------
272    -- Build_Actual_Subtype_Of_Component --
273    ---------------------------------------
274
275    function Build_Actual_Subtype_Of_Component
276      (T : Entity_Id;
277       N : Node_Id) return Node_Id
278    is
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;
284
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.
288
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.
293
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.
297
298       -----------------------------------
299       -- Build_Actual_Array_Constraint --
300       -----------------------------------
301
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;
309
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));
315
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));
321
322             else
323                Lo := New_Copy_Tree (Old_Lo);
324
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.
330
331                Set_Analyzed (Lo, False);
332             end if;
333
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));
339
340             else
341                Hi := New_Copy_Tree (Old_Hi);
342                Set_Analyzed (Hi, False);
343             end if;
344
345             Append (Make_Range (Loc, Lo, Hi), Constraints);
346             Next_Index (Indx);
347          end loop;
348
349          return Constraints;
350       end Build_Actual_Array_Constraint;
351
352       ------------------------------------
353       -- Build_Actual_Record_Constraint --
354       ------------------------------------
355
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;
360
361       begin
362          D := First_Elmt (Discriminant_Constraint (Deaccessed_T));
363          while Present (D) loop
364
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));
369
370             else
371                D_Val := New_Copy_Tree (Node (D));
372             end if;
373
374             Append (D_Val, Constraints);
375             Next_Elmt (D);
376          end loop;
377
378          return Constraints;
379       end Build_Actual_Record_Constraint;
380
381    --  Start of processing for Build_Actual_Subtype_Of_Component
382
383    begin
384       if In_Default_Expression then
385          return Empty;
386
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.
396
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;
409
410       if Ekind (T) = E_Access_Subtype then
411          Deaccessed_T := Designated_Type (T);
412       else
413          Deaccessed_T := T;
414       end if;
415
416       if Ekind (Deaccessed_T) = E_Array_Subtype then
417          Id := First_Index (Deaccessed_T);
418
419          while Present (Id) loop
420             Indx_Type := Underlying_Type (Etype (Id));
421
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;
430
431             Next_Index (Id);
432          end loop;
433
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
440
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;
447
448             Next_Elmt (D);
449          end loop;
450       end if;
451
452       --  If none of the above, the actual and nominal subtypes are the same
453
454       return Empty;
455    end Build_Actual_Subtype_Of_Component;
456
457    -----------------------------
458    -- Build_Component_Subtype --
459    -----------------------------
460
461    function Build_Component_Subtype
462      (C   : List_Id;
463       Loc : Source_Ptr;
464       T   : Entity_Id) return Node_Id
465    is
466       Subt : Entity_Id;
467       Decl : Node_Id;
468
469    begin
470       --  Unchecked_Union components do not require component subtypes
471
472       if Is_Unchecked_Union (T) then
473          return Empty;
474       end if;
475
476       Subt :=
477         Make_Defining_Identifier (Loc,
478           Chars => New_Internal_Name ('S'));
479       Set_Is_Internal (Subt);
480
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)));
490
491       Mark_Rewrite_Insertion (Decl);
492       return Decl;
493    end Build_Component_Subtype;
494
495    --------------------------------------------
496    -- Build_Discriminal_Subtype_Of_Component --
497    --------------------------------------------
498
499    function Build_Discriminal_Subtype_Of_Component
500      (T : Entity_Id) return Node_Id
501    is
502       Loc : constant Source_Ptr := Sloc (T);
503       D   : Elmt_Id;
504       Id  : Node_Id;
505
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.
510
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.
514
515       ----------------------------------------
516       -- Build_Discriminal_Array_Constraint --
517       ----------------------------------------
518
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;
526
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));
532
533             if Denotes_Discriminant (Old_Lo) then
534                Lo := New_Occurrence_Of (Discriminal (Entity (Old_Lo)), Loc);
535
536             else
537                Lo := New_Copy_Tree (Old_Lo);
538             end if;
539
540             if Denotes_Discriminant (Old_Hi) then
541                Hi := New_Occurrence_Of (Discriminal (Entity (Old_Hi)), Loc);
542
543             else
544                Hi := New_Copy_Tree (Old_Hi);
545             end if;
546
547             Append (Make_Range (Loc, Lo, Hi), Constraints);
548             Next_Index (Indx);
549          end loop;
550
551          return Constraints;
552       end Build_Discriminal_Array_Constraint;
553
554       -----------------------------------------
555       -- Build_Discriminal_Record_Constraint --
556       -----------------------------------------
557
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;
562
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);
569
570             else
571                D_Val := New_Copy_Tree (Node (D));
572             end if;
573
574             Append (D_Val, Constraints);
575             Next_Elmt (D);
576          end loop;
577
578          return Constraints;
579       end Build_Discriminal_Record_Constraint;
580
581    --  Start of processing for Build_Discriminal_Subtype_Of_Component
582
583    begin
584       if Ekind (T) = E_Array_Subtype then
585          Id := First_Index (T);
586
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;
594
595             Next_Index (Id);
596          end loop;
597
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;
608
609             Next_Elmt (D);
610          end loop;
611       end if;
612
613       --  If none of the above, the actual and nominal subtypes are the same
614
615       return Empty;
616    end Build_Discriminal_Subtype_Of_Component;
617
618    ------------------------------
619    -- Build_Elaboration_Entity --
620    ------------------------------
621
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;
628
629    begin
630       --  Ignore if already constructed
631
632       if Present (Elaboration_Entity (Spec_Id)) then
633          return;
634       end if;
635
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.
641
642       Get_Name_String (Unit_Name (Unum));
643
644       --  Replace the %s by _E
645
646       Name_Buffer (Name_Len - 1 .. Name_Len) := "_E";
647
648       --  Replace dots by double underscore
649
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;
663
664       --  Create elaboration flag
665
666       Elab_Ent :=
667         Make_Defining_Identifier (Loc, Chars => Name_Find);
668       Set_Elaboration_Entity (Spec_Id, Elab_Ent);
669
670       if No (Declarations (Aux_Decls_Node (N))) then
671          Set_Declarations (Aux_Decls_Node (N), New_List);
672       end if;
673
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));
681
682       Append_To (Declarations (Aux_Decls_Node (N)), Decl);
683       Analyze (Decl);
684
685       --  Reset True_Constant indication, since we will indeed
686       --  assign a value to the variable in the binder main.
687
688       Set_Is_True_Constant (Elab_Ent, False);
689       Set_Current_Value    (Elab_Ent, Empty);
690
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).
694
695       Set_Has_Qualified_Name       (Elab_Ent);
696       Set_Has_Fully_Qualified_Name (Elab_Ent);
697    end Build_Elaboration_Entity;
698
699    -----------------------------------
700    -- Cannot_Raise_Constraint_Error --
701    -----------------------------------
702
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;
707
708       elsif Do_Range_Check (Expr) then
709          return False;
710
711       elsif Raises_Constraint_Error (Expr) then
712          return False;
713
714       else
715          case Nkind (Expr) is
716             when N_Identifier =>
717                return True;
718
719             when N_Expanded_Name =>
720                return True;
721
722             when N_Selected_Component =>
723                return not Do_Discriminant_Check (Expr);
724
725             when N_Attribute_Reference =>
726                if Do_Overflow_Check (Expr) then
727                   return False;
728
729                elsif No (Expressions (Expr)) then
730                   return True;
731
732                else
733                   declare
734                      N : Node_Id := First (Expressions (Expr));
735
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;
744
745                      return True;
746                   end;
747                end if;
748
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;
759
760             when N_Unchecked_Type_Conversion =>
761                return Cannot_Raise_Constraint_Error (Expression (Expr));
762
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;
770
771             when N_Op_Divide |
772                  N_Op_Mod    |
773                  N_Op_Rem
774             =>
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;
785
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
805             =>
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;
814
815             when others =>
816                return False;
817          end case;
818       end if;
819    end Cannot_Raise_Constraint_Error;
820
821    --------------------------
822    -- Check_Fully_Declared --
823    --------------------------
824
825    procedure Check_Fully_Declared (T : Entity_Id; N : Node_Id) is
826    begin
827       if Ekind (T) = E_Incomplete_Type then
828
829          --  Ada 2005 (AI-50217): If the type is available through a limited
830          --  with_clause, verify that its full view has been analyzed.
831
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;
838
839          else
840             Error_Msg_NE
841               ("premature usage of incomplete}", N, First_Subtype (T));
842          end if;
843
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
848
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.
851
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));
858
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;
865
866    ------------------------------------------
867    -- Check_Potentially_Blocking_Operation --
868    ------------------------------------------
869
870    procedure Check_Potentially_Blocking_Operation (N : Node_Id) is
871       S   : Entity_Id;
872
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.
879
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.
883
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);
889
890             return;
891          end if;
892
893          S := Scope (S);
894       end loop;
895    end Check_Potentially_Blocking_Operation;
896
897    ---------------
898    -- Check_VMS --
899    ---------------
900
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;
908
909    ----------------------------------
910    -- Collect_Primitive_Operations --
911    ----------------------------------
912
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;
922
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.
926
927       if Is_Tagged_Type (B_Type) then
928          return Primitive_Operations (B_Type);
929
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.
933
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;
944
945       Op_List := New_Elmt_List;
946
947       if B_Scope = Standard_Standard then
948          if B_Type = Standard_String then
949             Append_Elmt (Standard_Op_Concat, Op_List);
950
951          elsif B_Type = Standard_Wide_String then
952             Append_Elmt (Standard_Op_Concatw, Op_List);
953
954          else
955             null;
956          end if;
957
958       elsif (Is_Package (B_Scope)
959                and then Nkind (
960                  Parent (Declaration_Node (First_Subtype (T))))
961                    /=  N_Package_Body)
962
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.
970
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;
979
980          while Present (Id) loop
981
982             --  Note that generic formal subprograms are not
983             --  considered to be primitive operations and thus
984             --  are never inherited.
985
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;
991
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;
1000
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;
1008
1009                      Next_Formal (Formal);
1010                   end loop;
1011                end if;
1012
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.
1016
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;
1024
1025             Next_Entity (Id);
1026
1027             --  For a type declared in System, some of its operations
1028             --  may appear in  the target-specific extension to System.
1029
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;
1040
1041       return Op_List;
1042    end Collect_Primitive_Operations;
1043
1044    -----------------------------------
1045    -- Compile_Time_Constraint_Error --
1046    -----------------------------------
1047
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
1054    is
1055       Msgc : String (1 .. Msg'Length + 2);
1056       Msgl : Natural;
1057       Wmsg : Boolean;
1058       P    : Node_Id;
1059       Msgs : Boolean;
1060       Eloc : Source_Ptr;
1061
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.
1067
1068       --  No messages are generated if we already posted an error on this node
1069
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;
1076
1077          --  Make all such messages unconditional
1078
1079          Msgc (1 .. Msg'Length) := Msg;
1080          Msgc (Msg'Length + 1) := '!';
1081          Msgl := Msg'Length + 1;
1082
1083          --  Message is a warning, even in Ada 95 case
1084
1085          if Msg (Msg'Length) = '?' then
1086             Wmsg := True;
1087
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.
1091
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;
1098
1099          elsif In_Instance_Not_Visible then
1100             Msgl := Msgl + 1;
1101             Msgc (Msgl) := '?';
1102             Wmsg := True;
1103
1104          --  Otherwise we have a real error message (Ada 95 static case)
1105
1106          else
1107             Wmsg := False;
1108          end if;
1109
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.
1114
1115          Msgs := True;
1116          P := N;
1117
1118          loop
1119             P := Parent (P);
1120
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;
1130
1131             elsif Nkind (P) = N_Component_Association
1132               and then Nkind (Parent (P)) = N_Aggregate
1133             then
1134                null;  --   Keep going.
1135
1136             else
1137                exit when Nkind (P) not in N_Subexpr;
1138             end if;
1139          end loop;
1140
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;
1147
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;
1165
1166       return N;
1167    end Compile_Time_Constraint_Error;
1168
1169    -----------------------
1170    -- Conditional_Delay --
1171    -----------------------
1172
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;
1179
1180    --------------------
1181    -- Current_Entity --
1182    --------------------
1183
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.
1188
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;
1193
1194    -----------------------------
1195    -- Current_Entity_In_Scope --
1196    -----------------------------
1197
1198    function Current_Entity_In_Scope (N : Node_Id) return Entity_Id is
1199       E  : Entity_Id;
1200       CS : constant Entity_Id := Current_Scope;
1201
1202       Transient_Case : constant Boolean := Scope_Is_Transient;
1203
1204    begin
1205       E := Get_Name_Entity_Id (Chars (N));
1206
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;
1213
1214       return E;
1215    end Current_Entity_In_Scope;
1216
1217    -------------------
1218    -- Current_Scope --
1219    -------------------
1220
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;
1238
1239    ------------------------
1240    -- Current_Subprogram --
1241    ------------------------
1242
1243    function Current_Subprogram return Entity_Id is
1244       Scop : constant Entity_Id := Current_Scope;
1245
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;
1253
1254    ---------------------
1255    -- Defining_Entity --
1256    ---------------------
1257
1258    function Defining_Entity (N : Node_Id) return Entity_Id is
1259       K   : constant Node_Kind := Nkind (N);
1260       Err : Entity_Id := Empty;
1261
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
1274          =>
1275             return Defining_Entity (Specification (N));
1276
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
1309          =>
1310             return Defining_Identifier (N);
1311
1312          when N_Subunit =>
1313             return Defining_Entity (Proper_Body (N));
1314
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
1327          =>
1328             declare
1329                Nam : constant Node_Id := Defining_Unit_Name (N);
1330
1331             begin
1332                if Nkind (Nam) in N_Entity then
1333                   return Nam;
1334
1335                --  For Error, make up a name and attach to declaration
1336                --  so we can continue semantic analysis
1337
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);
1343
1344                   return Err;
1345                --  If not an entity, get defining identifier
1346
1347                else
1348                   return Defining_Identifier (Nam);
1349                end if;
1350             end;
1351
1352          when N_Block_Statement =>
1353             return Entity (Identifier (N));
1354
1355          when others =>
1356             raise Program_Error;
1357
1358       end case;
1359    end Defining_Entity;
1360
1361    --------------------------
1362    -- Denotes_Discriminant --
1363    --------------------------
1364
1365    function Denotes_Discriminant
1366      (N               : Node_Id;
1367       Check_Protected : Boolean := False) return Boolean
1368    is
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;
1378
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.
1383
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)))));
1393
1394    end Denotes_Discriminant;
1395
1396    -----------------------------
1397    -- Depends_On_Discriminant --
1398    -----------------------------
1399
1400    function Depends_On_Discriminant (N : Node_Id) return Boolean is
1401       L : Node_Id;
1402       H : Node_Id;
1403
1404    begin
1405       Get_Index_Bounds (N, L, H);
1406       return Denotes_Discriminant (L) or else Denotes_Discriminant (H);
1407    end Depends_On_Discriminant;
1408
1409    -------------------------
1410    -- Designate_Same_Unit --
1411    -------------------------
1412
1413    function Designate_Same_Unit
1414      (Name1 : Node_Id;
1415       Name2 : Node_Id) return Boolean
1416    is
1417       K1 : constant Node_Kind := Nkind (Name1);
1418       K2 : constant Node_Kind := Nkind (Name2);
1419
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.
1423
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.
1428
1429       -----------------
1430       -- Prefix_Node --
1431       -----------------
1432
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);
1437
1438          else
1439             return Prefix (N);
1440          end if;
1441       end Prefix_Node;
1442
1443       -----------------
1444       -- Select_Node --
1445       -----------------
1446
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);
1451
1452          else
1453             return Selector_Name (N);
1454          end if;
1455       end Select_Node;
1456
1457    --  Start of processing for Designate_Next_Unit
1458
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);
1467
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));
1481
1482       else
1483          return False;
1484       end if;
1485    end Designate_Same_Unit;
1486
1487    ----------------------------
1488    -- Enclosing_Generic_Body --
1489    ----------------------------
1490
1491    function Enclosing_Generic_Body
1492      (E : Entity_Id) return Node_Id
1493    is
1494       P    : Node_Id;
1495       Decl : Node_Id;
1496       Spec : Node_Id;
1497
1498    begin
1499       P := Parent (E);
1500
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);
1506
1507             if Present (Spec) then
1508                Decl := Unit_Declaration_Node (Spec);
1509
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;
1517
1518          P := Parent (P);
1519       end loop;
1520
1521       return Empty;
1522    end Enclosing_Generic_Body;
1523
1524    -------------------------------
1525    -- Enclosing_Lib_Unit_Entity --
1526    -------------------------------
1527
1528    function Enclosing_Lib_Unit_Entity return Entity_Id is
1529       Unit_Entity : Entity_Id := Current_Scope;
1530
1531    begin
1532       --  Look for enclosing library unit entity by following scope links.
1533       --  Equivalent to, but faster than indexing through the scope stack.
1534
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;
1541
1542       return Unit_Entity;
1543    end Enclosing_Lib_Unit_Entity;
1544
1545    -----------------------------
1546    -- Enclosing_Lib_Unit_Node --
1547    -----------------------------
1548
1549    function Enclosing_Lib_Unit_Node (N : Node_Id) return Node_Id is
1550       Current_Node : Node_Id := N;
1551
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;
1558
1559       if Nkind (Current_Node) /= N_Compilation_Unit then
1560          return Empty;
1561       end if;
1562
1563       return Current_Node;
1564    end Enclosing_Lib_Unit_Node;
1565
1566    --------------------------
1567    -- Enclosing_Subprogram --
1568    --------------------------
1569
1570    function Enclosing_Subprogram (E : Entity_Id) return Entity_Id is
1571       Dynamic_Scope : constant Entity_Id := Enclosing_Dynamic_Scope (E);
1572
1573    begin
1574       if Dynamic_Scope = Standard_Standard then
1575          return Empty;
1576
1577       elsif Ekind (Dynamic_Scope) = E_Subprogram_Body then
1578          return Corresponding_Spec (Parent (Parent (Dynamic_Scope)));
1579
1580       elsif Ekind (Dynamic_Scope) = E_Block then
1581          return Enclosing_Subprogram (Dynamic_Scope);
1582
1583       elsif Ekind (Dynamic_Scope) = E_Task_Type then
1584          return Get_Task_Body_Procedure (Dynamic_Scope);
1585
1586       elsif Convention (Dynamic_Scope) = Convention_Protected then
1587          return Protected_Body_Subprogram (Dynamic_Scope);
1588
1589       else
1590          return Dynamic_Scope;
1591       end if;
1592    end Enclosing_Subprogram;
1593
1594    ------------------------
1595    -- Ensure_Freeze_Node --
1596    ------------------------
1597
1598    procedure Ensure_Freeze_Node (E : Entity_Id) is
1599       FN : Node_Id;
1600
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;
1611
1612    ----------------
1613    -- Enter_Name --
1614    ----------------
1615
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;
1620
1621    begin
1622       Generate_Definition (Def_Id);
1623
1624       --  Add new name to current scope declarations. Check for duplicate
1625       --  declaration, which may or may not be a genuine error.
1626
1627       if Present (E) then
1628
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.
1632
1633          if Etype (E) = Any_Type then
1634             Set_Is_Immediately_Visible (E, False);
1635
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.
1640
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);
1645
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.
1648
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);
1653
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).
1659
1660          elsif Nkind (Parent (Def_Id)) = N_Full_Type_Declaration
1661            and then Present (Corresponding_Remote_Type (Def_Id))
1662          then
1663             null;
1664
1665          --  A controller component for a type extension overrides the
1666          --  inherited component.
1667
1668          elsif Chars (E) = Name_uController then
1669             null;
1670
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.
1674
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);
1682
1683             begin
1684                --  If E is an implicit declaration, it cannot be the first
1685                --  entity in the scope.
1686
1687                Prev := First_Entity (Current_Scope);
1688
1689                while Present (Prev)
1690                  and then Next_Entity (Prev) /= E
1691                loop
1692                   Next_Entity (Prev);
1693                end loop;
1694
1695                if No (Prev) then
1696
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.
1703
1704                   pragma Assert
1705                     (Nkind (Parent (Decl)) = N_Generic_Subprogram_Declaration);
1706                   null;
1707
1708                else
1709                   Set_Next_Entity (Prev, Next_Entity (E));
1710
1711                   if No (Next_Entity (Prev)) then
1712                      Set_Last_Entity (Current_Scope, Prev);
1713                   end if;
1714
1715                   if E = Current_Entity (E) then
1716                      Prev_Vis := Empty;
1717
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;
1724
1725                   if Present (Prev_Vis)  then
1726
1727                      --  Skip E in the visibility chain
1728
1729                      Set_Homonym (Prev_Vis, Homonym (E));
1730
1731                   else
1732                      Set_Name_Entity_Id (Chars (E), Homonym (E));
1733                   end if;
1734                end if;
1735             end;
1736
1737          --  This section of code could use a comment ???
1738
1739          elsif Present (Etype (E))
1740            and then Is_Concurrent_Type (Etype (E))
1741            and then E = Def_Id
1742          then
1743             return;
1744
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 ???
1750
1751          elsif In_Instance_Not_Visible  then
1752             null;
1753
1754          --  When compiling a package body, some child units may have become
1755          --  visible. They cannot conflict with local entities that hide them.
1756
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;
1762
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.
1766
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;
1772
1773          --  Case of genuine duplicate declaration
1774
1775          else
1776             Error_Msg_Sloc := Sloc (E);
1777
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.
1781
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);
1791
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.
1800
1801                Error_Msg_Sloc := Sloc (Def_Id);
1802                Error_Msg_N ("& conflicts with declaration#", E);
1803                return;
1804
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.
1808
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;
1816
1817             else
1818                Error_Msg_N ("& conflicts with declaration#", Def_Id);
1819
1820                --  Avoid cascaded messages with duplicate components in
1821                --  derived types.
1822
1823                if Ekind (E) = E_Component
1824                  or else Ekind (E) = E_Discriminant
1825                then
1826                   return;
1827                end if;
1828             end if;
1829
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;
1837
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!
1841
1842             if S = Standard_Standard then
1843                raise Unrecoverable_Error;
1844
1845             --  Otherwise we continue with the declaration. Having two
1846             --  identical declarations should not cause us too much trouble!
1847
1848             else
1849                null;
1850             end if;
1851          end if;
1852       end if;
1853
1854       --  If we fall through, declaration is OK , or OK enough to continue
1855
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.
1859
1860       if Ekind (Def_Id) = E_Discriminant
1861         or else Ekind (Def_Id) = E_Component
1862       then
1863          null;
1864
1865       --  If a type is already set, leave it alone (happens whey a type
1866       --  declaration is reanalyzed following a call to the optimizer)
1867
1868       elsif Present (Etype (Def_Id)) then
1869          null;
1870
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
1873
1874       else
1875          Set_Ekind (Def_Id, E_Void);
1876          Set_Etype (Def_Id, Any_Type);
1877       end if;
1878
1879       --  Inherited discriminants and components in derived record types are
1880       --  immediately visible. Itypes are not.
1881
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;
1890
1891       Set_Homonym       (Def_Id, C);
1892       Append_Entity     (Def_Id, S);
1893       Set_Public_Status (Def_Id);
1894
1895       --  Warn if new entity hides an old one
1896
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;
1908
1909    --------------------------
1910    -- Explain_Limited_Type --
1911    --------------------------
1912
1913    procedure Explain_Limited_Type (T : Entity_Id; N : Node_Id) is
1914       C : Entity_Id;
1915
1916    begin
1917       --  For array, component type must be limited
1918
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);
1924
1925       elsif Is_Record_Type (T) then
1926
1927          --  No need for extra messages if explicit limited record
1928
1929          if Is_Limited_Record (Base_Type (T)) then
1930             return;
1931          end if;
1932
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.
1936
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;
1952
1953             Next_Component (C);
1954          end loop;
1955
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;
1961
1962    -------------------------------------
1963    -- Find_Corresponding_Discriminant --
1964    -------------------------------------
1965
1966    function Find_Corresponding_Discriminant
1967      (Id  : Node_Id;
1968       Typ : Entity_Id) return Entity_Id
1969    is
1970       Par_Disc : Entity_Id;
1971       Old_Disc : Entity_Id;
1972       New_Disc : Entity_Id;
1973
1974    begin
1975       Par_Disc := Original_Record_Component (Original_Discriminant (Id));
1976
1977       --  The original type may currently be private, and the discriminant
1978       --  only appear on its full view.
1979
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;
1988
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;
1994
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;
2003
2004       --  Should always find it
2005
2006       raise Program_Error;
2007    end Find_Corresponding_Discriminant;
2008
2009    -----------------------------
2010    -- Find_Static_Alternative --
2011    -----------------------------
2012
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;
2018
2019    begin
2020       Alt := First (Alternatives (N));
2021
2022       Search : loop
2023          if Nkind (Alt) /= N_Pragma then
2024             Choice := First (Discrete_Choices (Alt));
2025
2026             while Present (Choice) loop
2027
2028                --  Others choice, always matches
2029
2030                if Nkind (Choice) = N_Others_Choice then
2031                   exit Search;
2032
2033                --  Range, check if value is in the range
2034
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));
2040
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.
2044
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));
2049
2050                --  Choice is a subtype indication
2051
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);
2056
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;
2063
2064                --  Choice is a simple expression
2065
2066                else
2067                   exit Search when Val = Expr_Value (Choice);
2068                end if;
2069
2070                Next (Choice);
2071             end loop;
2072          end if;
2073
2074          Next (Alt);
2075          pragma Assert (Present (Alt));
2076       end loop Search;
2077
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.
2083
2084       return Alt;
2085    end Find_Static_Alternative;
2086
2087    ------------------
2088    -- First_Actual --
2089    ------------------
2090
2091    function First_Actual (Node : Node_Id) return Node_Id is
2092       N : Node_Id;
2093
2094    begin
2095       if No (Parameter_Associations (Node)) then
2096          return Empty;
2097       end if;
2098
2099       N := First (Parameter_Associations (Node));
2100
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;
2107
2108    -------------------------
2109    -- Full_Qualified_Name --
2110    -------------------------
2111
2112    function Full_Qualified_Name (E : Entity_Id) return String_Id is
2113       Res : String_Id;
2114       pragma Warnings (Off, Res);
2115
2116       function Internal_Full_Qualified_Name (E : Entity_Id) return String_Id;
2117       --  Compute recursively the qualified name without NUL at the end
2118
2119       ----------------------------------
2120       -- Internal_Full_Qualified_Name --
2121       ----------------------------------
2122
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;
2126
2127       begin
2128          --  Deals properly with child units
2129
2130          if Nkind (Ent) = N_Defining_Program_Unit_Name then
2131             Ent := Defining_Identifier (Ent);
2132          end if;
2133
2134          --  Compute recursively the qualification. Only "Standard" has no
2135          --  scope.
2136
2137          if Present (Scope (Scope (Ent))) then
2138             Parent_Name := Internal_Full_Qualified_Name (Scope (Ent));
2139          end if;
2140
2141          --  Every entity should have a name except some expanded blocks
2142          --  don't bother about those.
2143
2144          if Chars (Ent) = No_Name then
2145             return Parent_Name;
2146          end if;
2147
2148          --  Add a period between Name and qualification
2149
2150          if Parent_Name /= No_String then
2151             Start_String (Parent_Name);
2152             Store_String_Char (Get_Char_Code ('.'));
2153
2154          else
2155             Start_String;
2156          end if;
2157
2158          --  Generates the entity name in upper case
2159
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;
2165
2166    --  Start of processing for Full_Qualified_Name
2167
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;
2173
2174    -----------------------
2175    -- Gather_Components --
2176    -----------------------
2177
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)
2184    is
2185       Assoc           : Node_Id;
2186       Variant         : Node_Id;
2187       Discrete_Choice : Node_Id;
2188       Comp_Item       : Node_Id;
2189
2190       Discrim       : Entity_Id;
2191       Discrim_Name  : Node_Id;
2192       Discrim_Value : Node_Id;
2193
2194    begin
2195       Report_Errors := False;
2196
2197       if No (Comp_List) or else Null_Present (Comp_List) then
2198          return;
2199
2200       elsif Present (Component_Items (Comp_List)) then
2201          Comp_Item := First (Component_Items (Comp_List));
2202
2203       else
2204          Comp_Item := Empty;
2205       end if;
2206
2207       while Present (Comp_Item) loop
2208
2209          --  Skip the tag of a tagged record, the interface tags, as well
2210          --  as all items that are not user components (anonymous types,
2211          --  rep clauses, Parent field, controller field).
2212
2213          if Nkind (Comp_Item) = N_Component_Declaration then
2214             declare
2215                Comp : constant Entity_Id := Defining_Identifier (Comp_Item);
2216             begin
2217                if not Is_Tag (Comp)
2218                  and then Chars (Comp) /= Name_uParent
2219                  and then Chars (Comp) /= Name_uController
2220                then
2221                   Append_Elmt (Comp, Into);
2222                end if;
2223             end;
2224          end if;
2225
2226          Next (Comp_Item);
2227       end loop;
2228
2229       if No (Variant_Part (Comp_List)) then
2230          return;
2231       else
2232          Discrim_Name := Name (Variant_Part (Comp_List));
2233          Variant := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
2234       end if;
2235
2236       --  Look for the discriminant that governs this variant part.
2237       --  The discriminant *must* be in the Governed_By List
2238
2239       Assoc := First (Governed_By);
2240       Find_Constraint : loop
2241          Discrim := First (Choices (Assoc));
2242          exit Find_Constraint when Chars (Discrim_Name) = Chars (Discrim)
2243            or else (Present (Corresponding_Discriminant (Entity (Discrim)))
2244                       and then
2245                     Chars (Corresponding_Discriminant (Entity (Discrim)))
2246                          = Chars  (Discrim_Name))
2247            or else Chars (Original_Record_Component (Entity (Discrim)))
2248                          = Chars (Discrim_Name);
2249
2250          if No (Next (Assoc)) then
2251             if not Is_Constrained (Typ)
2252               and then Is_Derived_Type (Typ)
2253               and then Present (Stored_Constraint (Typ))
2254             then
2255
2256                --  If the type is a tagged type with inherited discriminants,
2257                --  use the stored constraint on the parent in order to find
2258                --  the values of discriminants that are otherwise hidden by an
2259                --  explicit constraint. Renamed discriminants are handled in
2260                --  the code above.
2261
2262                --  If several parent discriminants are renamed by a single
2263                --  discriminant of the derived type, the call to obtain the
2264                --  Corresponding_Discriminant field only retrieves the last
2265                --  of them. We recover the constraint on the others from the
2266                --  Stored_Constraint as well.
2267
2268                declare
2269                   D : Entity_Id;
2270                   C : Elmt_Id;
2271
2272                begin
2273                   D := First_Discriminant (Etype (Typ));
2274                   C := First_Elmt (Stored_Constraint (Typ));
2275
2276                   while Present (D)
2277                     and then Present (C)
2278                   loop
2279                      if Chars (Discrim_Name) = Chars (D) then
2280                         if Is_Entity_Name (Node (C))
2281                           and then Entity (Node (C)) = Entity (Discrim)
2282                         then
2283                            --  D is renamed by Discrim, whose value is
2284                            --  given in Assoc.
2285
2286                            null;
2287
2288                         else
2289                            Assoc :=
2290                              Make_Component_Association (Sloc (Typ),
2291                                New_List
2292                                  (New_Occurrence_Of (D, Sloc (Typ))),
2293                                   Duplicate_Subexpr_No_Checks (Node (C)));
2294                         end if;
2295                         exit Find_Constraint;
2296                      end if;
2297
2298                      D := Next_Discriminant (D);
2299                      Next_Elmt (C);
2300                   end loop;
2301                end;
2302             end if;
2303          end if;
2304
2305          if No (Next (Assoc)) then
2306             Error_Msg_NE (" missing value for discriminant&",
2307               First (Governed_By), Discrim_Name);
2308             Report_Errors := True;
2309             return;
2310          end if;
2311
2312          Next (Assoc);
2313       end loop Find_Constraint;
2314
2315       Discrim_Value := Expression (Assoc);
2316
2317       if not Is_OK_Static_Expression (Discrim_Value) then
2318          Error_Msg_FE
2319            ("value for discriminant & must be static!",
2320             Discrim_Value, Discrim);
2321          Why_Not_Static (Discrim_Value);
2322          Report_Errors := True;
2323          return;
2324       end if;
2325
2326       Search_For_Discriminant_Value : declare
2327          Low  : Node_Id;
2328          High : Node_Id;
2329
2330          UI_High          : Uint;
2331          UI_Low           : Uint;
2332          UI_Discrim_Value : constant Uint := Expr_Value (Discrim_Value);
2333
2334       begin
2335          Find_Discrete_Value : while Present (Variant) loop
2336             Discrete_Choice := First (Discrete_Choices (Variant));
2337             while Present (Discrete_Choice) loop
2338
2339                exit Find_Discrete_Value when
2340                  Nkind (Discrete_Choice) = N_Others_Choice;
2341
2342                Get_Index_Bounds (Discrete_Choice, Low, High);
2343
2344                UI_Low  := Expr_Value (Low);
2345                UI_High := Expr_Value (High);
2346
2347                exit Find_Discrete_Value when
2348                  UI_Low <= UI_Discrim_Value
2349                    and then
2350                  UI_High >= UI_Discrim_Value;
2351
2352                Next (Discrete_Choice);
2353             end loop;
2354
2355             Next_Non_Pragma (Variant);
2356          end loop Find_Discrete_Value;
2357       end Search_For_Discriminant_Value;
2358
2359       if No (Variant) then
2360          Error_Msg_NE
2361            ("value of discriminant & is out of range", Discrim_Value, Discrim);
2362          Report_Errors := True;
2363          return;
2364       end  if;
2365
2366       --  If we have found the corresponding choice, recursively add its
2367       --  components to the Into list.
2368
2369       Gather_Components (Empty,
2370         Component_List (Variant), Governed_By, Into, Report_Errors);
2371    end Gather_Components;
2372
2373    ------------------------
2374    -- Get_Actual_Subtype --
2375    ------------------------
2376
2377    function Get_Actual_Subtype (N : Node_Id) return Entity_Id is
2378       Typ  : constant Entity_Id := Etype (N);
2379       Utyp : Entity_Id := Underlying_Type (Typ);
2380       Decl : Node_Id;
2381       Atyp : Entity_Id;
2382
2383    begin
2384       if not Present (Utyp) then
2385          Utyp := Typ;
2386       end if;
2387
2388       --  If what we have is an identifier that references a subprogram
2389       --  formal, or a variable or constant object, then we get the actual
2390       --  subtype from the referenced entity if one has been built.
2391
2392       if Nkind (N) = N_Identifier
2393         and then
2394           (Is_Formal (Entity (N))
2395             or else Ekind (Entity (N)) = E_Constant
2396             or else Ekind (Entity (N)) = E_Variable)
2397         and then Present (Actual_Subtype (Entity (N)))
2398       then
2399          return Actual_Subtype (Entity (N));
2400
2401       --  Actual subtype of unchecked union is always itself. We never need
2402       --  the "real" actual subtype. If we did, we couldn't get it anyway
2403       --  because the discriminant is not available. The restrictions on
2404       --  Unchecked_Union are designed to make sure that this is OK.
2405
2406       elsif Is_Unchecked_Union (Base_Type (Utyp)) then
2407          return Typ;
2408
2409       --  Here for the unconstrained case, we must find actual subtype
2410       --  No actual subtype is available, so we must build it on the fly.
2411
2412       --  Checking the type, not the underlying type, for constrainedness
2413       --  seems to be necessary. Maybe all the tests should be on the type???
2414
2415       elsif (not Is_Constrained (Typ))
2416            and then (Is_Array_Type (Utyp)
2417                       or else (Is_Record_Type (Utyp)
2418                                 and then Has_Discriminants (Utyp)))
2419            and then not Has_Unknown_Discriminants (Utyp)
2420            and then not (Ekind (Utyp) = E_String_Literal_Subtype)
2421       then
2422          --  Nothing to do if in default expression
2423
2424          if In_Default_Expression then
2425             return Typ;
2426
2427          elsif Is_Private_Type (Typ)
2428            and then not Has_Discriminants (Typ)
2429          then
2430             --  If the type has no discriminants, there is no subtype to
2431             --  build, even if the underlying type is discriminated.
2432
2433             return Typ;
2434
2435          --  Else build the actual subtype
2436
2437          else
2438             Decl := Build_Actual_Subtype (Typ, N);
2439             Atyp := Defining_Identifier (Decl);
2440
2441             --  If Build_Actual_Subtype generated a new declaration then use it
2442
2443             if Atyp /= Typ then
2444
2445                --  The actual subtype is an Itype, so analyze the declaration,
2446                --  but do not attach it to the tree, to get the type defined.
2447
2448                Set_Parent (Decl, N);
2449                Set_Is_Itype (Atyp);
2450                Analyze (Decl, Suppress => All_Checks);
2451                Set_Associated_Node_For_Itype (Atyp, N);
2452                Set_Has_Delayed_Freeze (Atyp, False);
2453
2454                --  We need to freeze the actual subtype immediately. This is
2455                --  needed, because otherwise this Itype will not get frozen
2456                --  at all, and it is always safe to freeze on creation because
2457                --  any associated types must be frozen at this point.
2458
2459                Freeze_Itype (Atyp, N);
2460                return Atyp;
2461
2462             --  Otherwise we did not build a declaration, so return original
2463
2464             else
2465                return Typ;
2466             end if;
2467          end if;
2468
2469       --  For all remaining cases, the actual subtype is the same as
2470       --  the nominal type.
2471
2472       else
2473          return Typ;
2474       end if;
2475    end Get_Actual_Subtype;
2476
2477    -------------------------------------
2478    -- Get_Actual_Subtype_If_Available --
2479    -------------------------------------
2480
2481    function Get_Actual_Subtype_If_Available (N : Node_Id) return Entity_Id is
2482       Typ  : constant Entity_Id := Etype (N);
2483
2484    begin
2485       --  If what we have is an identifier that references a subprogram
2486       --  formal, or a variable or constant object, then we get the actual
2487       --  subtype from the referenced entity if one has been built.
2488
2489       if Nkind (N) = N_Identifier
2490         and then
2491           (Is_Formal (Entity (N))
2492             or else Ekind (Entity (N)) = E_Constant
2493             or else Ekind (Entity (N)) = E_Variable)
2494         and then Present (Actual_Subtype (Entity (N)))
2495       then
2496          return Actual_Subtype (Entity (N));
2497
2498       --  Otherwise the Etype of N is returned unchanged
2499
2500       else
2501          return Typ;
2502       end if;
2503    end Get_Actual_Subtype_If_Available;
2504
2505    -------------------------------
2506    -- Get_Default_External_Name --
2507    -------------------------------
2508
2509    function Get_Default_External_Name (E : Node_Or_Entity_Id) return Node_Id is
2510    begin
2511       Get_Decoded_Name_String (Chars (E));
2512
2513       if Opt.External_Name_Imp_Casing = Uppercase then
2514          Set_Casing (All_Upper_Case);
2515       else
2516          Set_Casing (All_Lower_Case);
2517       end if;
2518
2519       return
2520         Make_String_Literal (Sloc (E),
2521           Strval => String_From_Name_Buffer);
2522    end Get_Default_External_Name;
2523
2524    ---------------------------
2525    -- Get_Enum_Lit_From_Pos --
2526    ---------------------------
2527
2528    function Get_Enum_Lit_From_Pos
2529      (T   : Entity_Id;
2530       Pos : Uint;
2531       Loc : Source_Ptr) return Node_Id
2532    is
2533       Lit : Node_Id;
2534
2535    begin
2536       --  In the case where the literal is of type Character, Wide_Character
2537       --  or Wide_Wide_Character or of a type derived from them, there needs
2538       --  to be some special handling since there is no explicit chain of
2539       --  literals to search. Instead, an N_Character_Literal node is created
2540       --  with the appropriate Char_Code and Chars fields.
2541
2542       if Root_Type (T) = Standard_Character
2543         or else Root_Type (T) = Standard_Wide_Character
2544         or else Root_Type (T) = Standard_Wide_Wide_Character
2545       then
2546          Set_Character_Literal_Name (UI_To_CC (Pos));
2547          return
2548            Make_Character_Literal (Loc,
2549              Chars              => Name_Find,
2550              Char_Literal_Value => Pos);
2551
2552       --  For all other cases, we have a complete table of literals, and
2553       --  we simply iterate through the chain of literal until the one
2554       --  with the desired position value is found.
2555       --
2556
2557       else
2558          Lit := First_Literal (Base_Type (T));
2559          for J in 1 .. UI_To_Int (Pos) loop
2560             Next_Literal (Lit);
2561          end loop;
2562
2563          return New_Occurrence_Of (Lit, Loc);
2564       end if;
2565    end Get_Enum_Lit_From_Pos;
2566
2567    ------------------------
2568    -- Get_Generic_Entity --
2569    ------------------------
2570
2571    function Get_Generic_Entity (N : Node_Id) return Entity_Id is
2572       Ent : constant Entity_Id := Entity (Name (N));
2573    begin
2574       if Present (Renamed_Object (Ent)) then
2575          return Renamed_Object (Ent);
2576       else
2577          return Ent;
2578       end if;
2579    end Get_Generic_Entity;
2580
2581    ----------------------
2582    -- Get_Index_Bounds --
2583    ----------------------
2584
2585    procedure Get_Index_Bounds (N : Node_Id; L, H : out Node_Id) is
2586       Kind : constant Node_Kind := Nkind (N);
2587       R    : Node_Id;
2588
2589    begin
2590       if Kind = N_Range then
2591          L := Low_Bound (N);
2592          H := High_Bound (N);
2593
2594       elsif Kind = N_Subtype_Indication then
2595          R := Range_Expression (Constraint (N));
2596
2597          if R = Error then
2598             L := Error;
2599             H := Error;
2600             return;
2601
2602          else
2603             L := Low_Bound  (Range_Expression (Constraint (N)));
2604             H := High_Bound (Range_Expression (Constraint (N)));
2605          end if;
2606
2607       elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
2608          if Error_Posted (Scalar_Range (Entity (N))) then
2609             L := Error;
2610             H := Error;
2611
2612          elsif Nkind (Scalar_Range (Entity (N))) = N_Subtype_Indication then
2613             Get_Index_Bounds (Scalar_Range (Entity (N)), L, H);
2614
2615          else
2616             L := Low_Bound  (Scalar_Range (Entity (N)));
2617             H := High_Bound (Scalar_Range (Entity (N)));
2618          end if;
2619
2620       else
2621          --  N is an expression, indicating a range with one value
2622
2623          L := N;
2624          H := N;
2625       end if;
2626    end Get_Index_Bounds;
2627
2628    ----------------------------------
2629    -- Get_Library_Unit_Name_string --
2630    ----------------------------------
2631
2632    procedure Get_Library_Unit_Name_String (Decl_Node : Node_Id) is
2633       Unit_Name_Id : constant Unit_Name_Type := Get_Unit_Name (Decl_Node);
2634
2635    begin
2636       Get_Unit_Name_String (Unit_Name_Id);
2637
2638       --  Remove seven last character (" (spec)" or " (body)")
2639
2640       Name_Len := Name_Len - 7;
2641       pragma Assert (Name_Buffer (Name_Len + 1) = ' ');
2642    end Get_Library_Unit_Name_String;
2643
2644    ------------------------
2645    -- Get_Name_Entity_Id --
2646    ------------------------
2647
2648    function Get_Name_Entity_Id (Id : Name_Id) return Entity_Id is
2649    begin
2650       return Entity_Id (Get_Name_Table_Info (Id));
2651    end Get_Name_Entity_Id;
2652
2653    ---------------------------
2654    -- Get_Referenced_Object --
2655    ---------------------------
2656
2657    function Get_Referenced_Object (N : Node_Id) return Node_Id is
2658       R   : Node_Id := N;
2659
2660    begin
2661       while Is_Entity_Name (R)
2662         and then Present (Renamed_Object (Entity (R)))
2663       loop
2664          R := Renamed_Object (Entity (R));
2665       end loop;
2666
2667       return R;
2668    end Get_Referenced_Object;
2669
2670    -------------------------
2671    -- Get_Subprogram_Body --
2672    -------------------------
2673
2674    function Get_Subprogram_Body (E : Entity_Id) return Node_Id is
2675       Decl : Node_Id;
2676
2677    begin
2678       Decl := Unit_Declaration_Node (E);
2679
2680       if Nkind (Decl) = N_Subprogram_Body then
2681          return Decl;
2682
2683       --  The below comment is bad, because it is possible for
2684       --  Nkind (Decl) to be an N_Subprogram_Body_Stub ???
2685
2686       else           --  Nkind (Decl) = N_Subprogram_Declaration
2687
2688          if Present (Corresponding_Body (Decl)) then
2689             return Unit_Declaration_Node (Corresponding_Body (Decl));
2690
2691          --  Imported subprogram case
2692
2693          else
2694             return Empty;
2695          end if;
2696       end if;
2697    end Get_Subprogram_Body;
2698
2699    -----------------------------
2700    -- Get_Task_Body_Procedure --
2701    -----------------------------
2702
2703    function Get_Task_Body_Procedure (E : Entity_Id) return Node_Id is
2704    begin
2705       --  Note: A task type may be the completion of a private type with
2706       --  discriminants. when performing elaboration checks on a task
2707       --  declaration, the current view of the type may be the private one,
2708       --  and the procedure that holds the body of the task is held in its
2709       --  underlying type.
2710
2711       return Task_Body_Procedure (Underlying_Type (Root_Type (E)));
2712    end Get_Task_Body_Procedure;
2713
2714    -----------------------
2715    -- Has_Access_Values --
2716    -----------------------
2717
2718    function Has_Access_Values (T : Entity_Id) return Boolean is
2719       Typ : constant Entity_Id := Underlying_Type (T);
2720
2721    begin
2722       --  Case of a private type which is not completed yet. This can only
2723       --  happen in the case of a generic format type appearing directly, or
2724       --  as a component of the type to which this function is being applied
2725       --  at the top level. Return False in this case, since we certainly do
2726       --  not know that the type contains access types.
2727
2728       if No (Typ) then
2729          return False;
2730
2731       elsif Is_Access_Type (Typ) then
2732          return True;
2733
2734       elsif Is_Array_Type (Typ) then
2735          return Has_Access_Values (Component_Type (Typ));
2736
2737       elsif Is_Record_Type (Typ) then
2738          declare
2739             Comp : Entity_Id;
2740
2741          begin
2742             Comp := First_Entity (Typ);
2743             while Present (Comp) loop
2744                if (Ekind (Comp) = E_Component
2745                      or else
2746                    Ekind (Comp) = E_Discriminant)
2747                  and then Has_Access_Values (Etype (Comp))
2748                then
2749                   return True;
2750                end if;
2751
2752                Next_Entity (Comp);
2753             end loop;
2754          end;
2755
2756          return False;
2757
2758       else
2759          return False;
2760       end if;
2761    end Has_Access_Values;
2762
2763    ----------------------
2764    -- Has_Declarations --
2765    ----------------------
2766
2767    function Has_Declarations (N : Node_Id) return Boolean is
2768       K : constant Node_Kind := Nkind (N);
2769    begin
2770       return    K = N_Accept_Statement
2771         or else K = N_Block_Statement
2772         or else K = N_Compilation_Unit_Aux
2773         or else K = N_Entry_Body
2774         or else K = N_Package_Body
2775         or else K = N_Protected_Body
2776         or else K = N_Subprogram_Body
2777         or else K = N_Task_Body
2778         or else K = N_Package_Specification;
2779    end Has_Declarations;
2780
2781    -------------------------------------------
2782    -- Has_Discriminant_Dependent_Constraint --
2783    -------------------------------------------
2784
2785    function Has_Discriminant_Dependent_Constraint
2786      (Comp : Entity_Id) return Boolean
2787    is
2788       Comp_Decl  : constant Node_Id := Parent (Comp);
2789       Subt_Indic : constant Node_Id :=
2790                      Subtype_Indication (Component_Definition (Comp_Decl));
2791       Constr     : Node_Id;
2792       Assn       : Node_Id;
2793
2794    begin
2795       if Nkind (Subt_Indic) = N_Subtype_Indication then
2796          Constr := Constraint (Subt_Indic);
2797
2798          if Nkind (Constr) = N_Index_Or_Discriminant_Constraint then
2799             Assn := First (Constraints (Constr));
2800             while Present (Assn) loop
2801                case Nkind (Assn) is
2802                   when N_Subtype_Indication |
2803                        N_Range              |
2804                        N_Identifier
2805                   =>
2806                      if Depends_On_Discriminant (Assn) then
2807                         return True;
2808                      end if;
2809
2810                   when N_Discriminant_Association =>
2811                      if Depends_On_Discriminant (Expression (Assn)) then
2812                         return True;
2813                      end if;
2814
2815                   when others =>
2816                      null;
2817
2818                end case;
2819
2820                Next (Assn);
2821             end loop;
2822          end if;
2823       end if;
2824
2825       return False;
2826    end Has_Discriminant_Dependent_Constraint;
2827
2828    --------------------
2829    -- Has_Infinities --
2830    --------------------
2831
2832    function Has_Infinities (E : Entity_Id) return Boolean is
2833    begin
2834       return
2835         Is_Floating_Point_Type (E)
2836           and then Nkind (Scalar_Range (E)) = N_Range
2837           and then Includes_Infinities (Scalar_Range (E));
2838    end Has_Infinities;
2839
2840    ------------------------
2841    -- Has_Null_Extension --
2842    ------------------------
2843
2844    function Has_Null_Extension (T : Entity_Id) return Boolean is
2845       B     : constant Entity_Id := Base_Type (T);
2846       Comps : Node_Id;
2847       Ext   : Node_Id;
2848
2849    begin
2850       if Nkind (Parent (B)) = N_Full_Type_Declaration
2851         and then Present (Record_Extension_Part (Type_Definition (Parent (B))))
2852       then
2853          Ext := Record_Extension_Part (Type_Definition (Parent (B)));
2854
2855          if Present (Ext) then
2856             if Null_Present (Ext) then
2857                return True;
2858             else
2859                Comps := Component_List (Ext);
2860
2861                --  The null component list is rewritten during analysis to
2862                --  include the parent component. Any other component indicates
2863                --  that the extension was not originally null.
2864
2865                return Null_Present (Comps)
2866                  or else No (Next (First (Component_Items (Comps))));
2867             end if;
2868          else
2869             return False;
2870          end if;
2871
2872       else
2873          return False;
2874       end if;
2875    end Has_Null_Extension;
2876
2877    ---------------------------
2878    -- Has_Private_Component --
2879    ---------------------------
2880
2881    function Has_Private_Component (Type_Id : Entity_Id) return Boolean is
2882       Btype     : Entity_Id := Base_Type (Type_Id);
2883       Component : Entity_Id;
2884
2885    begin
2886       if Error_Posted (Type_Id)
2887         or else Error_Posted (Btype)
2888       then
2889          return False;
2890       end if;
2891
2892       if Is_Class_Wide_Type (Btype) then
2893          Btype := Root_Type (Btype);
2894       end if;
2895
2896       if Is_Private_Type (Btype) then
2897          declare
2898             UT : constant Entity_Id := Underlying_Type (Btype);
2899          begin
2900             if No (UT) then
2901
2902                if No (Full_View (Btype)) then
2903                   return not Is_Generic_Type (Btype)
2904                     and then not Is_Generic_Type (Root_Type (Btype));
2905
2906                else
2907                   return not Is_Generic_Type (Root_Type (Full_View (Btype)));
2908                end if;
2909
2910             else
2911                return not Is_Frozen (UT) and then Has_Private_Component (UT);
2912             end if;
2913          end;
2914       elsif Is_Array_Type (Btype) then
2915          return Has_Private_Component (Component_Type (Btype));
2916
2917       elsif Is_Record_Type (Btype) then
2918
2919          Component := First_Component (Btype);
2920          while Present (Component) loop
2921
2922             if Has_Private_Component (Etype (Component)) then
2923                return True;
2924             end if;
2925
2926             Next_Component (Component);
2927          end loop;
2928
2929          return False;
2930
2931       elsif Is_Protected_Type (Btype)
2932         and then Present (Corresponding_Record_Type (Btype))
2933       then
2934          return Has_Private_Component (Corresponding_Record_Type (Btype));
2935
2936       else
2937          return False;
2938       end if;
2939    end Has_Private_Component;
2940
2941    ----------------
2942    -- Has_Stream --
2943    ----------------
2944
2945    function Has_Stream (T : Entity_Id) return Boolean is
2946       E : Entity_Id;
2947
2948    begin
2949       if No (T) then
2950          return False;
2951
2952       elsif Is_RTE (Root_Type (T), RE_Root_Stream_Type) then
2953          return True;
2954
2955       elsif Is_Array_Type (T) then
2956          return Has_Stream (Component_Type (T));
2957
2958       elsif Is_Record_Type (T) then
2959          E := First_Component (T);
2960          while Present (E) loop
2961             if Has_Stream (Etype (E)) then
2962                return True;
2963             else
2964                Next_Component (E);
2965             end if;
2966          end loop;
2967
2968          return False;
2969
2970       elsif Is_Private_Type (T) then
2971          return Has_Stream (Underlying_Type (T));
2972
2973       else
2974          return False;
2975       end if;
2976    end Has_Stream;
2977
2978    --------------------------
2979    -- Has_Tagged_Component --
2980    --------------------------
2981
2982    function Has_Tagged_Component (Typ : Entity_Id) return Boolean is
2983       Comp : Entity_Id;
2984
2985    begin
2986       if Is_Private_Type (Typ)
2987         and then Present (Underlying_Type (Typ))
2988       then
2989          return Has_Tagged_Component (Underlying_Type (Typ));
2990
2991       elsif Is_Array_Type (Typ) then
2992          return Has_Tagged_Component (Component_Type (Typ));
2993
2994       elsif Is_Tagged_Type (Typ) then
2995          return True;
2996
2997       elsif Is_Record_Type (Typ) then
2998          Comp := First_Component (Typ);
2999
3000          while Present (Comp) loop
3001             if Has_Tagged_Component (Etype (Comp)) then
3002                return True;
3003             end if;
3004
3005             Comp := Next_Component (Typ);
3006          end loop;
3007
3008          return False;
3009
3010       else
3011          return False;
3012       end if;
3013    end Has_Tagged_Component;
3014
3015    -----------------
3016    -- In_Instance --
3017    -----------------
3018
3019    function In_Instance return Boolean is
3020       S : Entity_Id := Current_Scope;
3021
3022    begin
3023       while Present (S)
3024         and then S /= Standard_Standard
3025       loop
3026          if (Ekind (S) = E_Function
3027               or else Ekind (S) = E_Package
3028               or else Ekind (S) = E_Procedure)
3029            and then Is_Generic_Instance (S)
3030          then
3031             return True;
3032          end if;
3033
3034          S := Scope (S);
3035       end loop;
3036
3037       return False;
3038    end In_Instance;
3039
3040    ----------------------
3041    -- In_Instance_Body --
3042    ----------------------
3043
3044    function In_Instance_Body return Boolean is
3045       S : Entity_Id := Current_Scope;
3046
3047    begin
3048       while Present (S)
3049         and then S /= Standard_Standard
3050       loop
3051          if (Ekind (S) = E_Function
3052               or else Ekind (S) = E_Procedure)
3053            and then Is_Generic_Instance (S)
3054          then
3055             return True;
3056
3057          elsif Ekind (S) = E_Package
3058            and then In_Package_Body (S)
3059            and then Is_Generic_Instance (S)
3060          then
3061             return True;
3062          end if;
3063
3064          S := Scope (S);
3065       end loop;
3066
3067       return False;
3068    end In_Instance_Body;
3069
3070    -----------------------------
3071    -- In_Instance_Not_Visible --
3072    -----------------------------
3073
3074    function In_Instance_Not_Visible return Boolean is
3075       S : Entity_Id := Current_Scope;
3076
3077    begin
3078       while Present (S)
3079         and then S /= Standard_Standard
3080       loop
3081          if (Ekind (S) = E_Function
3082               or else Ekind (S) = E_Procedure)
3083            and then Is_Generic_Instance (S)
3084          then
3085             return True;
3086
3087          elsif Ekind (S) = E_Package
3088            and then (In_Package_Body (S) or else In_Private_Part (S))
3089            and then Is_Generic_Instance (S)
3090          then
3091             return True;
3092          end if;
3093
3094          S := Scope (S);
3095       end loop;
3096
3097       return False;
3098    end In_Instance_Not_Visible;
3099
3100    ------------------------------
3101    -- In_Instance_Visible_Part --
3102    ------------------------------
3103
3104    function In_Instance_Visible_Part return Boolean is
3105       S : Entity_Id := Current_Scope;
3106
3107    begin
3108       while Present (S)
3109         and then S /= Standard_Standard
3110       loop
3111          if Ekind (S) = E_Package
3112            and then Is_Generic_Instance (S)
3113            and then not In_Package_Body (S)
3114            and then not In_Private_Part (S)
3115          then
3116             return True;
3117          end if;
3118
3119          S := Scope (S);
3120       end loop;
3121
3122       return False;
3123    end In_Instance_Visible_Part;
3124
3125    ----------------------
3126    -- In_Packiage_Body --
3127    ----------------------
3128
3129    function In_Package_Body return Boolean is
3130       S : Entity_Id := Current_Scope;
3131
3132    begin
3133       while Present (S)
3134         and then S /= Standard_Standard
3135       loop
3136          if Ekind (S) = E_Package
3137            and then In_Package_Body (S)
3138          then
3139             return True;
3140          else
3141             S := Scope (S);
3142          end if;
3143       end loop;
3144
3145       return False;
3146    end In_Package_Body;
3147
3148    --------------------------------------
3149    -- In_Subprogram_Or_Concurrent_Unit --
3150    --------------------------------------
3151
3152    function In_Subprogram_Or_Concurrent_Unit return Boolean is
3153       E : Entity_Id;
3154       K : Entity_Kind;
3155
3156    begin
3157       --  Use scope chain to check successively outer scopes
3158
3159       E := Current_Scope;
3160       loop
3161          K := Ekind (E);
3162
3163          if K in Subprogram_Kind
3164            or else K in Concurrent_Kind
3165            or else K in Generic_Subprogram_Kind
3166          then
3167             return True;
3168
3169          elsif E = Standard_Standard then
3170             return False;
3171          end if;
3172
3173          E := Scope (E);
3174       end loop;
3175    end In_Subprogram_Or_Concurrent_Unit;
3176
3177    ---------------------
3178    -- In_Visible_Part --
3179    ---------------------
3180
3181    function In_Visible_Part (Scope_Id : Entity_Id) return Boolean is
3182    begin
3183       return
3184         Is_Package (Scope_Id)
3185           and then In_Open_Scopes (Scope_Id)
3186           and then not In_Package_Body (Scope_Id)
3187           and then not In_Private_Part (Scope_Id);
3188    end In_Visible_Part;
3189
3190    ---------------------------------
3191    -- Insert_Explicit_Dereference --
3192    ---------------------------------
3193
3194    procedure Insert_Explicit_Dereference (N : Node_Id) is
3195       New_Prefix : constant Node_Id := Relocate_Node (N);
3196       Ent        : Entity_Id := Empty;
3197       Pref       : Node_Id;
3198       I          : Interp_Index;
3199       It         : Interp;
3200       T          : Entity_Id;
3201
3202    begin
3203       Save_Interps (N, New_Prefix);
3204       Rewrite (N,
3205         Make_Explicit_Dereference (Sloc (N), Prefix => New_Prefix));
3206
3207       Set_Etype (N, Designated_Type (Etype (New_Prefix)));
3208
3209       if Is_Overloaded (New_Prefix) then
3210
3211          --  The deference is also overloaded, and its interpretations are the
3212          --  designated types of the interpretations of the original node.
3213
3214          Set_Etype (N, Any_Type);
3215          Get_First_Interp (New_Prefix, I, It);
3216
3217          while Present (It.Nam) loop
3218             T := It.Typ;
3219
3220             if Is_Access_Type (T) then
3221                Add_One_Interp (N, Designated_Type (T), Designated_Type (T));
3222             end if;
3223
3224             Get_Next_Interp (I, It);
3225          end loop;
3226
3227          End_Interp_List;
3228
3229       else
3230          --  Prefix is unambiguous: mark the original prefix (which might
3231          --  Come_From_Source) as a reference, since the new (relocated) one
3232          --  won't be taken into account.
3233
3234          if Is_Entity_Name (New_Prefix) then
3235             Ent := Entity (New_Prefix);
3236
3237          --  For a retrieval of a subcomponent of some composite object,
3238          --  retrieve the ultimate entity if there is one.
3239
3240          elsif Nkind (New_Prefix) = N_Selected_Component
3241            or else Nkind (New_Prefix) = N_Indexed_Component
3242          then
3243             Pref := Prefix (New_Prefix);
3244
3245             while Present (Pref)
3246               and then
3247                 (Nkind (Pref) = N_Selected_Component
3248                   or else Nkind (Pref) = N_Indexed_Component)
3249             loop
3250                Pref := Prefix (Pref);
3251             end loop;
3252
3253             if Present (Pref) and then Is_Entity_Name (Pref) then
3254                Ent := Entity (Pref);
3255             end if;
3256          end if;
3257
3258          if Present (Ent) then
3259             Generate_Reference (Ent, New_Prefix);
3260          end if;
3261       end if;
3262    end Insert_Explicit_Dereference;
3263
3264    -------------------
3265    -- Is_AAMP_Float --
3266    -------------------
3267
3268    function Is_AAMP_Float (E : Entity_Id) return Boolean is
3269    begin
3270       pragma Assert (Is_Type (E));
3271
3272       return AAMP_On_Target
3273          and then Is_Floating_Point_Type (E)
3274          and then E = Base_Type (E);
3275    end Is_AAMP_Float;
3276
3277    -------------------------
3278    -- Is_Actual_Parameter --
3279    -------------------------
3280
3281    function Is_Actual_Parameter (N : Node_Id) return Boolean is
3282       PK : constant Node_Kind := Nkind (Parent (N));
3283
3284    begin
3285       case PK is
3286          when N_Parameter_Association =>
3287             return N = Explicit_Actual_Parameter (Parent (N));
3288
3289          when N_Function_Call | N_Procedure_Call_Statement =>
3290             return Is_List_Member (N)
3291               and then
3292                 List_Containing (N) = Parameter_Associations (Parent (N));
3293
3294          when others =>
3295             return False;
3296       end case;
3297    end Is_Actual_Parameter;
3298
3299    ---------------------
3300    -- Is_Aliased_View --
3301    ---------------------
3302
3303    function Is_Aliased_View (Obj : Node_Id) return Boolean is
3304       E : Entity_Id;
3305
3306    begin
3307       if Is_Entity_Name (Obj) then
3308
3309          E := Entity (Obj);
3310
3311          return
3312            (Is_Object (E)
3313              and then
3314                (Is_Aliased (E)
3315                   or else (Present (Renamed_Object (E))
3316                              and then Is_Aliased_View (Renamed_Object (E)))))
3317
3318            or else ((Is_Formal (E)
3319                       or else Ekind (E) = E_Generic_In_Out_Parameter
3320                       or else Ekind (E) = E_Generic_In_Parameter)
3321                     and then Is_Tagged_Type (Etype (E)))
3322
3323            or else ((Ekind (E) = E_Task_Type
3324                       or else Ekind (E) = E_Protected_Type)
3325                     and then In_Open_Scopes (E))
3326
3327             --  Current instance of type
3328
3329            or else (Is_Type (E) and then E = Current_Scope)
3330            or else (Is_Incomplete_Or_Private_Type (E)
3331                      and then Full_View (E) = Current_Scope);
3332
3333       elsif Nkind (Obj) = N_Selected_Component then
3334          return Is_Aliased (Entity (Selector_Name (Obj)));
3335
3336       elsif Nkind (Obj) = N_Indexed_Component then
3337          return Has_Aliased_Components (Etype (Prefix (Obj)))
3338            or else
3339              (Is_Access_Type (Etype (Prefix (Obj)))
3340                and then
3341               Has_Aliased_Components
3342                 (Designated_Type (Etype (Prefix (Obj)))));
3343
3344       elsif Nkind (Obj) = N_Unchecked_Type_Conversion
3345         or else Nkind (Obj) = N_Type_Conversion
3346       then
3347          return Is_Tagged_Type (Etype (Obj))
3348            and then Is_Aliased_View (Expression (Obj));
3349
3350       elsif Nkind (Obj) = N_Explicit_Dereference then
3351          return Nkind (Original_Node (Obj)) /= N_Function_Call;
3352
3353       else
3354          return False;
3355       end if;
3356    end Is_Aliased_View;
3357
3358    -------------------------
3359    -- Is_Ancestor_Package --
3360    -------------------------
3361
3362    function Is_Ancestor_Package
3363      (E1  : Entity_Id;
3364       E2  : Entity_Id) return Boolean
3365    is
3366       Par : Entity_Id;
3367
3368    begin
3369       Par := E2;
3370       while Present (Par)
3371         and then Par /= Standard_Standard
3372       loop
3373          if Par = E1 then
3374             return True;
3375          end if;
3376
3377          Par := Scope (Par);
3378       end loop;
3379
3380       return False;
3381    end Is_Ancestor_Package;
3382
3383    ----------------------
3384    -- Is_Atomic_Object --
3385    ----------------------
3386
3387    function Is_Atomic_Object (N : Node_Id) return Boolean is
3388
3389       function Object_Has_Atomic_Components (N : Node_Id) return Boolean;
3390       --  Determines if given object has atomic components
3391
3392       function Is_Atomic_Prefix (N : Node_Id) return Boolean;
3393       --  If prefix is an implicit dereference, examine designated type
3394
3395       function Is_Atomic_Prefix (N : Node_Id) return Boolean is
3396       begin
3397          if Is_Access_Type (Etype (N)) then
3398             return
3399               Has_Atomic_Components (Designated_Type (Etype (N)));
3400          else
3401             return Object_Has_Atomic_Components (N);
3402          end if;
3403       end Is_Atomic_Prefix;
3404
3405       function Object_Has_Atomic_Components (N : Node_Id) return Boolean is
3406       begin
3407          if Has_Atomic_Components (Etype (N))
3408            or else Is_Atomic (Etype (N))
3409          then
3410             return True;
3411
3412          elsif Is_Entity_Name (N)
3413            and then (Has_Atomic_Components (Entity (N))
3414                       or else Is_Atomic (Entity (N)))
3415          then
3416             return True;
3417
3418          elsif Nkind (N) = N_Indexed_Component
3419            or else Nkind (N) = N_Selected_Component
3420          then
3421             return Is_Atomic_Prefix (Prefix (N));
3422
3423          else
3424             return False;
3425          end if;
3426       end Object_Has_Atomic_Components;
3427
3428    --  Start of processing for Is_Atomic_Object
3429
3430    begin
3431       if Is_Atomic (Etype (N))
3432         or else (Is_Entity_Name (N) and then Is_Atomic (Entity (N)))
3433       then
3434          return True;
3435
3436       elsif Nkind (N) = N_Indexed_Component
3437         or else Nkind (N) = N_Selected_Component
3438       then
3439          return Is_Atomic_Prefix (Prefix (N));
3440
3441       else
3442          return False;
3443       end if;
3444    end Is_Atomic_Object;
3445
3446    --------------------------------------
3447    -- Is_Controlling_Limited_Procedure --
3448    --------------------------------------
3449
3450    function Is_Controlling_Limited_Procedure
3451      (Proc_Nam : Entity_Id) return Boolean
3452    is
3453       Param_Typ : Entity_Id;
3454
3455    begin
3456       --  Proc_Nam was found to be a primitive operation of a limited interface
3457
3458       if Ekind (Proc_Nam) = E_Procedure then
3459          Param_Typ := Etype (Parameter_Type (First (Parameter_Specifications (
3460            Parent (Proc_Nam)))));
3461          return
3462            Is_Interface (Param_Typ)
3463              and then Is_Limited_Record (Param_Typ);
3464
3465       --  In this case where an Itype was created, the procedure call has been
3466       --  rewritten.
3467
3468       elsif Present (Associated_Node_For_Itype (Proc_Nam))
3469         and then Present (Original_Node (Associated_Node_For_Itype (Proc_Nam)))
3470       then
3471          Param_Typ := Etype (First (Parameter_Associations (
3472            Associated_Node_For_Itype (Proc_Nam))));
3473          return
3474            Is_Interface (Param_Typ)
3475              and then Is_Limited_Record (Param_Typ);
3476       end if;
3477
3478       return False;
3479    end Is_Controlling_Limited_Procedure;
3480
3481    ----------------------------------------------
3482    -- Is_Dependent_Component_Of_Mutable_Object --
3483    ----------------------------------------------
3484
3485    function Is_Dependent_Component_Of_Mutable_Object
3486      (Object : Node_Id) return   Boolean
3487    is
3488       P           : Node_Id;
3489       Prefix_Type : Entity_Id;
3490       P_Aliased   : Boolean := False;
3491       Comp        : Entity_Id;
3492
3493       function Is_Declared_Within_Variant (Comp : Entity_Id) return Boolean;
3494       --  Returns True if and only if Comp is declared within a variant part
3495
3496       --------------------------------
3497       -- Is_Declared_Within_Variant --
3498       --------------------------------
3499
3500       function Is_Declared_Within_Variant (Comp : Entity_Id) return Boolean is
3501          Comp_Decl : constant Node_Id   := Parent (Comp);
3502          Comp_List : constant Node_Id   := Parent (Comp_Decl);
3503
3504       begin
3505          return Nkind (Parent (Comp_List)) = N_Variant;
3506       end Is_Declared_Within_Variant;
3507
3508    --  Start of processing for Is_Dependent_Component_Of_Mutable_Object
3509
3510    begin
3511       if Is_Variable (Object) then
3512
3513          if Nkind (Object) = N_Selected_Component then
3514             P := Prefix (Object);
3515             Prefix_Type := Etype (P);
3516
3517             if Is_Entity_Name (P) then
3518
3519                if Ekind (Entity (P)) = E_Generic_In_Out_Parameter then
3520                   Prefix_Type := Base_Type (Prefix_Type);
3521                end if;
3522
3523                if Is_Aliased (Entity (P)) then
3524                   P_Aliased := True;
3525                end if;
3526
3527             --  A discriminant check on a selected component may be
3528             --  expanded into a dereference when removing side-effects.
3529             --  Recover the original node and its type, which may be
3530             --  unconstrained.
3531
3532             elsif Nkind (P) = N_Explicit_Dereference
3533               and then not (Comes_From_Source (P))
3534             then
3535                P := Original_Node (P);
3536                Prefix_Type := Etype (P);
3537
3538             else
3539                --  Check for prefix being an aliased component ???
3540                null;
3541
3542             end if;
3543
3544             --  A heap object is constrained by its initial value
3545
3546             --  Ada 2005 AI-363:if the designated type is a type with a
3547             --  constrained partial view, the resulting heap object is not
3548             --  constrained, and a renaming of the component is now unsafe.
3549
3550             if Is_Access_Type (Prefix_Type)
3551               and then
3552                  not Has_Constrained_Partial_View
3553                    (Designated_Type (Prefix_Type))
3554             then
3555                return False;
3556
3557             elsif Nkind (P) = N_Explicit_Dereference
3558               and then not Has_Constrained_Partial_View (Prefix_Type)
3559             then
3560                return False;
3561             end if;
3562
3563             Comp :=
3564               Original_Record_Component (Entity (Selector_Name (Object)));
3565
3566             --  As per AI-0017, the renaming is illegal in a generic body,
3567             --  even if the subtype is indefinite.
3568
3569             if not Is_Constrained (Prefix_Type)
3570               and then (not Is_Indefinite_Subtype (Prefix_Type)
3571                          or else
3572                           (Is_Generic_Type (Prefix_Type)
3573                             and then Ekind (Current_Scope) = E_Generic_Package
3574                             and then In_Package_Body (Current_Scope)))
3575
3576               and then (Is_Declared_Within_Variant (Comp)
3577                           or else Has_Discriminant_Dependent_Constraint (Comp))
3578               and then not P_Aliased
3579             then
3580                return True;
3581
3582             else
3583                return
3584                  Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
3585
3586             end if;
3587
3588          elsif Nkind (Object) = N_Indexed_Component
3589            or else Nkind (Object) = N_Slice
3590          then
3591             return Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
3592
3593          --  A type conversion that Is_Variable is a view conversion:
3594          --  go back to the denoted object.
3595
3596          elsif Nkind (Object) = N_Type_Conversion then
3597             return
3598               Is_Dependent_Component_Of_Mutable_Object (Expression (Object));
3599          end if;
3600       end if;
3601
3602       return False;
3603    end Is_Dependent_Component_Of_Mutable_Object;
3604
3605    ---------------------
3606    -- Is_Dereferenced --
3607    ---------------------
3608
3609    function Is_Dereferenced (N : Node_Id) return Boolean is
3610       P : constant Node_Id := Parent (N);
3611    begin
3612       return
3613          (Nkind (P) = N_Selected_Component
3614             or else
3615           Nkind (P) = N_Explicit_Dereference
3616             or else
3617           Nkind (P) = N_Indexed_Component
3618             or else
3619           Nkind (P) = N_Slice)
3620         and then Prefix (P) = N;
3621    end Is_Dereferenced;
3622
3623    ----------------------
3624    -- Is_Descendent_Of --
3625    ----------------------
3626
3627    function Is_Descendent_Of (T1 : Entity_Id; T2 : Entity_Id) return Boolean is
3628       T    : Entity_Id;
3629       Etyp : Entity_Id;
3630
3631    begin
3632       pragma Assert (Nkind (T1) in N_Entity);
3633       pragma Assert (Nkind (T2) in N_Entity);
3634
3635       T := Base_Type (T1);
3636
3637       --  Immediate return if the types match
3638
3639       if T = T2 then
3640          return True;
3641
3642       --  Comment needed here ???
3643
3644       elsif Ekind (T) = E_Class_Wide_Type then
3645          return Etype (T) = T2;
3646
3647       --  All other cases
3648
3649       else
3650          loop
3651             Etyp := Etype (T);
3652
3653             --  Done if we found the type we are looking for
3654
3655             if Etyp = T2 then
3656                return True;
3657
3658             --  Done if no more derivations to check
3659
3660             elsif T = T1
3661               or else T = Etyp
3662             then
3663                return False;
3664
3665             --  Following test catches error cases resulting from prev errors
3666
3667             elsif No (Etyp) then
3668                return False;
3669
3670             elsif Is_Private_Type (T) and then Etyp = Full_View (T) then
3671                return False;
3672
3673             elsif Is_Private_Type (Etyp) and then Full_View (Etyp) = T then
3674                return False;
3675             end if;
3676
3677             T := Base_Type (Etyp);
3678          end loop;
3679       end if;
3680
3681       raise Program_Error;
3682    end Is_Descendent_Of;
3683
3684    ------------------------------
3685    -- Is_Descendent_Of_Address --
3686    ------------------------------
3687
3688    function Is_Descendent_Of_Address (T1 : Entity_Id) return Boolean is
3689    begin
3690       --  If Address has not been loaded, answer must be False
3691
3692       if not RTU_Loaded (System) then
3693          return False;
3694
3695       --  Otherwise we can get the entity we are interested in without
3696       --  causing an unwanted dependency on System, and do the test.
3697
3698       else
3699          return Is_Descendent_Of (T1, Base_Type (RTE (RE_Address)));
3700       end if;
3701    end Is_Descendent_Of_Address;
3702
3703    --------------
3704    -- Is_False --
3705    --------------
3706
3707    function Is_False (U : Uint) return Boolean is
3708    begin
3709       return (U = 0);
3710    end Is_False;
3711
3712    ---------------------------
3713    -- Is_Fixed_Model_Number --
3714    ---------------------------
3715
3716    function Is_Fixed_Model_Number (U : Ureal; T : Entity_Id) return Boolean is
3717       S : constant Ureal := Small_Value (T);
3718       M : Urealp.Save_Mark;
3719       R : Boolean;
3720
3721    begin
3722       M := Urealp.Mark;
3723       R := (U = UR_Trunc (U / S) * S);
3724       Urealp.Release (M);
3725       return R;
3726    end Is_Fixed_Model_Number;
3727
3728    -------------------------------
3729    -- Is_Fully_Initialized_Type --
3730    -------------------------------
3731
3732    function Is_Fully_Initialized_Type (Typ : Entity_Id) return Boolean is
3733    begin
3734       if Is_Scalar_Type (Typ) then
3735          return False;
3736
3737       elsif Is_Access_Type (Typ) then
3738          return True;
3739
3740       elsif Is_Array_Type (Typ) then
3741          if Is_Fully_Initialized_Type (Component_Type (Typ)) then
3742             return True;
3743          end if;
3744
3745          --  An interesting case, if we have a constrained type one of whose
3746          --  bounds is known to be null, then there are no elements to be
3747          --  initialized, so all the elements are initialized!
3748
3749          if Is_Constrained (Typ) then
3750             declare
3751                Indx     : Node_Id;
3752                Indx_Typ : Entity_Id;
3753                Lbd, Hbd : Node_Id;
3754
3755             begin
3756                Indx := First_Index (Typ);
3757                while Present (Indx) loop
3758
3759                   if Etype (Indx) = Any_Type then
3760                      return False;
3761
3762                   --  If index is a range, use directly
3763
3764                   elsif Nkind (Indx) = N_Range then
3765                      Lbd := Low_Bound  (Indx);
3766                      Hbd := High_Bound (Indx);
3767
3768                   else
3769                      Indx_Typ := Etype (Indx);
3770
3771                      if Is_Private_Type (Indx_Typ)  then
3772                         Indx_Typ := Full_View (Indx_Typ);
3773                      end if;
3774
3775                      if No (Indx_Typ) then
3776                         return False;
3777                      else
3778                         Lbd := Type_Low_Bound  (Indx_Typ);
3779                         Hbd := Type_High_Bound (Indx_Typ);
3780                      end if;
3781                   end if;
3782
3783                   if Compile_Time_Known_Value (Lbd)
3784                     and then Compile_Time_Known_Value (Hbd)
3785                   then
3786                      if Expr_Value (Hbd) < Expr_Value (Lbd) then
3787                         return True;
3788                      end if;
3789                   end if;
3790
3791                   Next_Index (Indx);
3792                end loop;
3793             end;
3794          end if;
3795
3796          --  If no null indexes, then type is not fully initialized
3797
3798          return False;
3799
3800       --  Record types
3801
3802       elsif Is_Record_Type (Typ) then
3803          if Has_Discriminants (Typ)
3804            and then
3805              Present (Discriminant_Default_Value (First_Discriminant (Typ)))
3806            and then Is_Fully_Initialized_Variant (Typ)
3807          then
3808             return True;
3809          end if;
3810
3811          --  Controlled records are considered to be fully initialized if
3812          --  there is a user defined Initialize routine. This may not be
3813          --  entirely correct, but as the spec notes, we are guessing here
3814          --  what is best from the point of view of issuing warnings.
3815
3816          if Is_Controlled (Typ) then
3817             declare
3818                Utyp : constant Entity_Id := Underlying_Type (Typ);
3819
3820             begin
3821                if Present (Utyp) then
3822                   declare
3823                      Init : constant Entity_Id :=
3824                               (Find_Prim_Op
3825                                  (Underlying_Type (Typ), Name_Initialize));
3826
3827                   begin
3828                      if Present (Init)
3829                        and then Comes_From_Source (Init)
3830                        and then not
3831                          Is_Predefined_File_Name
3832                            (File_Name (Get_Source_File_Index (Sloc (Init))))
3833                      then
3834                         return True;
3835
3836                      elsif Has_Null_Extension (Typ)
3837                         and then
3838                           Is_Fully_Initialized_Type
3839                             (Etype (Base_Type (Typ)))
3840                      then
3841                         return True;
3842                      end if;
3843                   end;
3844                end if;
3845             end;
3846          end if;
3847
3848          --  Otherwise see if all record components are initialized
3849
3850          declare
3851             Ent : Entity_Id;
3852
3853          begin
3854             Ent := First_Entity (Typ);
3855
3856             while Present (Ent) loop
3857                if Chars (Ent) = Name_uController then
3858                   null;
3859
3860                elsif Ekind (Ent) = E_Component
3861                  and then (No (Parent (Ent))
3862                              or else No (Expression (Parent (Ent))))
3863                  and then not Is_Fully_Initialized_Type (Etype (Ent))
3864                then
3865                   return False;
3866                end if;
3867
3868                Next_Entity (Ent);
3869             end loop;
3870          end;
3871
3872          --  No uninitialized components, so type is fully initialized.
3873          --  Note that this catches the case of no components as well.
3874
3875          return True;
3876
3877       elsif Is_Concurrent_Type (Typ) then
3878          return True;
3879
3880       elsif Is_Private_Type (Typ) then
3881          declare
3882             U : constant Entity_Id := Underlying_Type (Typ);
3883
3884          begin
3885             if No (U) then
3886                return False;
3887             else
3888                return Is_Fully_Initialized_Type (U);
3889             end if;
3890          end;
3891
3892       else
3893          return False;
3894       end if;
3895    end Is_Fully_Initialized_Type;
3896
3897    ----------------------------------
3898    -- Is_Fully_Initialized_Variant --
3899    ----------------------------------
3900
3901    function Is_Fully_Initialized_Variant (Typ : Entity_Id) return Boolean is
3902       Loc           : constant Source_Ptr := Sloc (Typ);
3903       Constraints   : constant List_Id    := New_List;
3904       Components    : constant Elist_Id   := New_Elmt_List;
3905       Comp_Elmt     : Elmt_Id;
3906       Comp_Id       : Node_Id;
3907       Comp_List     : Node_Id;
3908       Discr         : Entity_Id;
3909       Discr_Val     : Node_Id;
3910       Report_Errors : Boolean;
3911
3912    begin
3913       if Serious_Errors_Detected > 0 then
3914          return False;
3915       end if;
3916
3917       if Is_Record_Type (Typ)
3918         and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
3919         and then Nkind (Type_Definition (Parent (Typ))) = N_Record_Definition
3920       then
3921          Comp_List := Component_List (Type_Definition (Parent (Typ)));
3922          Discr := First_Discriminant (Typ);
3923
3924          while Present (Discr) loop
3925             if Nkind (Parent (Discr)) = N_Discriminant_Specification then
3926                Discr_Val := Expression (Parent (Discr));
3927
3928                if Present (Discr_Val)
3929                  and then Is_OK_Static_Expression (Discr_Val)
3930                then
3931                   Append_To (Constraints,
3932                     Make_Component_Association (Loc,
3933                       Choices    => New_List (New_Occurrence_Of (Discr, Loc)),
3934                       Expression => New_Copy (Discr_Val)));
3935                else
3936                   return False;
3937                end if;
3938             else
3939                return False;
3940             end if;
3941
3942             Next_Discriminant (Discr);
3943          end loop;
3944
3945          Gather_Components
3946            (Typ           => Typ,
3947             Comp_List     => Comp_List,
3948             Governed_By   => Constraints,
3949             Into          => Components,
3950             Report_Errors => Report_Errors);
3951
3952          --  Check that each component present is fully initialized
3953
3954          Comp_Elmt := First_Elmt (Components);
3955
3956          while Present (Comp_Elmt) loop
3957             Comp_Id := Node (Comp_Elmt);
3958
3959             if Ekind (Comp_Id) = E_Component
3960               and then (No (Parent (Comp_Id))
3961                          or else No (Expression (Parent (Comp_Id))))
3962               and then not Is_Fully_Initialized_Type (Etype (Comp_Id))
3963             then
3964                return False;
3965             end if;
3966
3967             Next_Elmt (Comp_Elmt);
3968          end loop;
3969
3970          return True;
3971
3972       elsif Is_Private_Type (Typ) then
3973          declare
3974             U : constant Entity_Id := Underlying_Type (Typ);
3975
3976          begin
3977             if No (U) then
3978                return False;
3979             else
3980                return Is_Fully_Initialized_Variant (U);
3981             end if;
3982          end;
3983       else
3984          return False;
3985       end if;
3986    end Is_Fully_Initialized_Variant;
3987
3988    ----------------------------
3989    -- Is_Inherited_Operation --
3990    ----------------------------
3991
3992    function Is_Inherited_Operation (E : Entity_Id) return Boolean is
3993       Kind : constant Node_Kind := Nkind (Parent (E));
3994    begin
3995       pragma Assert (Is_Overloadable (E));
3996       return Kind = N_Full_Type_Declaration
3997         or else Kind = N_Private_Extension_Declaration
3998         or else Kind = N_Subtype_Declaration
3999         or else (Ekind (E) = E_Enumeration_Literal
4000                   and then Is_Derived_Type (Etype (E)));
4001    end Is_Inherited_Operation;
4002
4003    -----------------------------
4004    -- Is_Library_Level_Entity --
4005    -----------------------------
4006
4007    function Is_Library_Level_Entity (E : Entity_Id) return Boolean is
4008    begin
4009       --  The following is a small optimization, and it also handles
4010       --  properly discriminals, which in task bodies might appear in
4011       --  expressions before the corresponding procedure has been
4012       --  created, and which therefore do not have an assigned scope.
4013
4014       if Ekind (E) in Formal_Kind then
4015          return False;
4016       end if;
4017
4018       --  Normal test is simply that the enclosing dynamic scope is Standard
4019
4020       return Enclosing_Dynamic_Scope (E) = Standard_Standard;
4021    end Is_Library_Level_Entity;
4022
4023    ---------------------------------
4024    -- Is_Local_Variable_Reference --
4025    ---------------------------------
4026
4027    function Is_Local_Variable_Reference (Expr : Node_Id) return Boolean is
4028    begin
4029       if not Is_Entity_Name (Expr) then
4030          return False;
4031
4032       else
4033          declare
4034             Ent : constant Entity_Id := Entity (Expr);
4035             Sub : constant Entity_Id := Enclosing_Subprogram (Ent);
4036
4037          begin
4038             if Ekind (Ent) /= E_Variable
4039                  and then
4040                Ekind (Ent) /= E_In_Out_Parameter
4041             then
4042                return False;
4043
4044             else
4045                return Present (Sub) and then Sub = Current_Subprogram;
4046             end if;
4047          end;
4048       end if;
4049    end Is_Local_Variable_Reference;
4050
4051    ---------------
4052    -- Is_Lvalue --
4053    ---------------
4054
4055    function Is_Lvalue (N : Node_Id) return Boolean is
4056       P : constant Node_Id := Parent (N);
4057
4058    begin
4059       case Nkind (P) is
4060
4061          --  Test left side of assignment
4062
4063          when N_Assignment_Statement =>
4064             return N = Name (P);
4065
4066          --  Test prefix of component or attribute
4067
4068          when N_Attribute_Reference  |
4069               N_Expanded_Name        |
4070               N_Explicit_Dereference |
4071               N_Indexed_Component    |
4072               N_Reference            |
4073               N_Selected_Component   |
4074               N_Slice                =>
4075             return N = Prefix (P);
4076
4077          --  Test subprogram parameter (we really should check the
4078          --  parameter mode, but it is not worth the trouble)
4079
4080          when N_Function_Call            |
4081               N_Procedure_Call_Statement |
4082               N_Accept_Statement         |
4083               N_Parameter_Association    =>
4084             return True;
4085
4086          --  Test for appearing in a conversion that itself appears
4087          --  in an lvalue context, since this should be an lvalue.
4088
4089          when N_Type_Conversion =>
4090             return Is_Lvalue (P);
4091
4092          --  Test for appearence in object renaming declaration
4093
4094          when N_Object_Renaming_Declaration =>
4095             return True;
4096
4097          --  All other references are definitely not Lvalues
4098
4099          when others =>
4100             return False;
4101
4102       end case;
4103    end Is_Lvalue;
4104
4105    -------------------------
4106    -- Is_Object_Reference --
4107    -------------------------
4108
4109    function Is_Object_Reference (N : Node_Id) return Boolean is
4110    begin
4111       if Is_Entity_Name (N) then
4112          return Is_Object (Entity (N));
4113
4114       else
4115          case Nkind (N) is
4116             when N_Indexed_Component | N_Slice =>
4117                return
4118                  Is_Object_Reference (Prefix (N))
4119                    or else Is_Access_Type (Etype (Prefix (N)));
4120
4121             --  In Ada95, a function call is a constant object; a procedure
4122             --  call is not.
4123
4124             when N_Function_Call =>
4125                return Etype (N) /= Standard_Void_Type;
4126
4127             --  A reference to the stream attribute Input is a function call
4128
4129             when N_Attribute_Reference =>
4130                return Attribute_Name (N) = Name_Input;
4131
4132             when N_Selected_Component =>
4133                return
4134                  Is_Object_Reference (Selector_Name (N))
4135                    and then
4136                      (Is_Object_Reference (Prefix (N))
4137                         or else Is_Access_Type (Etype (Prefix (N))));
4138
4139             when N_Explicit_Dereference =>
4140                return True;
4141
4142             --  A view conversion of a tagged object is an object reference
4143
4144             when N_Type_Conversion =>
4145                return Is_Tagged_Type (Etype (Subtype_Mark (N)))
4146                  and then Is_Tagged_Type (Etype (Expression (N)))
4147                  and then Is_Object_Reference (Expression (N));
4148
4149             --  An unchecked type conversion is considered to be an object if
4150             --  the operand is an object (this construction arises only as a
4151             --  result of expansion activities).
4152
4153             when N_Unchecked_Type_Conversion =>
4154                return True;
4155
4156             when others =>
4157                return False;
4158          end case;
4159       end if;
4160    end Is_Object_Reference;
4161
4162    -----------------------------------
4163    -- Is_OK_Variable_For_Out_Formal --
4164    -----------------------------------
4165
4166    function Is_OK_Variable_For_Out_Formal (AV : Node_Id) return Boolean is
4167    begin
4168       Note_Possible_Modification (AV);
4169
4170       --  We must reject parenthesized variable names. The check for
4171       --  Comes_From_Source is present because there are currently
4172       --  cases where the compiler violates this rule (e.g. passing
4173       --  a task object to its controlled Initialize routine).
4174
4175       if Paren_Count (AV) > 0 and then Comes_From_Source (AV) then
4176          return False;
4177
4178       --  A variable is always allowed
4179
4180       elsif Is_Variable (AV) then
4181          return True;
4182
4183       --  Unchecked conversions are allowed only if they come from the
4184       --  generated code, which sometimes uses unchecked conversions for
4185       --  out parameters in cases where code generation is unaffected.
4186       --  We tell source unchecked conversions by seeing if they are
4187       --  rewrites of an original UC function call, or of an explicit
4188       --  conversion of a function call.
4189
4190       elsif Nkind (AV) = N_Unchecked_Type_Conversion then
4191          if Nkind (Original_Node (AV)) = N_Function_Call then
4192             return False;
4193
4194          elsif Comes_From_Source (AV)
4195            and then Nkind (Original_Node (Expression (AV))) = N_Function_Call
4196          then
4197             return False;
4198
4199          elsif Nkind (Original_Node (AV)) = N_Type_Conversion then
4200             return Is_OK_Variable_For_Out_Formal (Expression (AV));
4201
4202          else
4203             return True;
4204          end if;
4205
4206       --  Normal type conversions are allowed if argument is a variable
4207
4208       elsif Nkind (AV) = N_Type_Conversion then
4209          if Is_Variable (Expression (AV))
4210            and then Paren_Count (Expression (AV)) = 0
4211          then
4212             Note_Possible_Modification (Expression (AV));
4213             return True;
4214
4215          --  We also allow a non-parenthesized expression that raises
4216          --  constraint error if it rewrites what used to be a variable
4217
4218          elsif Raises_Constraint_Error (Expression (AV))
4219             and then Paren_Count (Expression (AV)) = 0
4220             and then Is_Variable (Original_Node (Expression (AV)))
4221          then
4222             return True;
4223
4224          --  Type conversion of something other than a variable
4225
4226          else
4227             return False;
4228          end if;
4229
4230       --  If this node is rewritten, then test the original form, if that is
4231       --  OK, then we consider the rewritten node OK (for example, if the
4232       --  original node is a conversion, then Is_Variable will not be true
4233       --  but we still want to allow the conversion if it converts a variable).
4234
4235       elsif Original_Node (AV) /= AV then
4236          return Is_OK_Variable_For_Out_Formal (Original_Node (AV));
4237
4238       --  All other non-variables are rejected
4239
4240       else
4241          return False;
4242       end if;
4243    end Is_OK_Variable_For_Out_Formal;
4244
4245    -----------------------------------
4246    -- Is_Partially_Initialized_Type --
4247    -----------------------------------
4248
4249    function Is_Partially_Initialized_Type (Typ : Entity_Id) return Boolean is
4250    begin
4251       if Is_Scalar_Type (Typ) then
4252          return False;
4253
4254       elsif Is_Access_Type (Typ) then
4255          return True;
4256
4257       elsif Is_Array_Type (Typ) then
4258
4259          --  If component type is partially initialized, so is array type
4260
4261          if Is_Partially_Initialized_Type (Component_Type (Typ)) then
4262             return True;
4263
4264          --  Otherwise we are only partially initialized if we are fully
4265          --  initialized (this is the empty array case, no point in us
4266          --  duplicating that code here).
4267
4268          else
4269             return Is_Fully_Initialized_Type (Typ);
4270          end if;
4271
4272       elsif Is_Record_Type (Typ) then
4273
4274          --  A discriminated type is always partially initialized
4275
4276          if Has_Discriminants (Typ) then
4277             return True;
4278
4279          --  A tagged type is always partially initialized
4280
4281          elsif Is_Tagged_Type (Typ) then
4282             return True;
4283
4284          --  Case of non-discriminated record
4285
4286          else
4287             declare
4288                Ent : Entity_Id;
4289
4290                Component_Present : Boolean := False;
4291                --  Set True if at least one component is present. If no
4292                --  components are present, then record type is fully
4293                --  initialized (another odd case, like the null array).
4294
4295             begin
4296                --  Loop through components
4297
4298                Ent := First_Entity (Typ);
4299                while Present (Ent) loop
4300                   if Ekind (Ent) = E_Component then
4301                      Component_Present := True;
4302
4303                      --  If a component has an initialization expression then
4304                      --  the enclosing record type is partially initialized
4305
4306                      if Present (Parent (Ent))
4307                        and then Present (Expression (Parent (Ent)))
4308                      then
4309                         return True;
4310
4311                      --  If a component is of a type which is itself partially
4312                      --  initialized, then the enclosing record type is also.
4313
4314                      elsif Is_Partially_Initialized_Type (Etype (Ent)) then
4315                         return True;
4316                      end if;
4317                   end if;
4318
4319                   Next_Entity (Ent);
4320                end loop;
4321
4322                --  No initialized components found. If we found any components
4323                --  they were all uninitialized so the result is false.
4324
4325                if Component_Present then
4326                   return False;
4327
4328                --  But if we found no components, then all the components are
4329                --  initialized so we consider the type to be initialized.
4330
4331                else
4332                   return True;
4333                end if;
4334             end;
4335          end if;
4336
4337       --  Concurrent types are always fully initialized
4338
4339       elsif Is_Concurrent_Type (Typ) then
4340          return True;
4341
4342       --  For a private type, go to underlying type. If there is no underlying
4343       --  type then just assume this partially initialized. Not clear if this
4344       --  can happen in a non-error case, but no harm in testing for this.
4345
4346       elsif Is_Private_Type (Typ) then
4347          declare
4348             U : constant Entity_Id := Underlying_Type (Typ);
4349
4350          begin
4351             if No (U) then
4352                return True;
4353             else
4354                return Is_Partially_Initialized_Type (U);
4355             end if;
4356          end;
4357
4358       --  For any other type (are there any?) assume partially initialized
4359
4360       else
4361          return True;
4362       end if;
4363    end Is_Partially_Initialized_Type;
4364
4365    ------------------------------------
4366    -- Is_Potentially_Persistent_Type --
4367    ------------------------------------
4368
4369    function Is_Potentially_Persistent_Type (T : Entity_Id) return Boolean is
4370       Comp : Entity_Id;
4371       Indx : Node_Id;
4372
4373    begin
4374       --  For private type, test corrresponding full type
4375
4376       if Is_Private_Type (T) then
4377          return Is_Potentially_Persistent_Type (Full_View (T));
4378
4379       --  Scalar types are potentially persistent
4380
4381       elsif Is_Scalar_Type (T) then
4382          return True;
4383
4384       --  Record type is potentially persistent if not tagged and the types of
4385       --  all it components are potentially persistent, and no component has
4386       --  an initialization expression.
4387
4388       elsif Is_Record_Type (T)
4389         and then not Is_Tagged_Type (T)
4390         and then not Is_Partially_Initialized_Type (T)
4391       then
4392          Comp := First_Component (T);
4393          while Present (Comp) loop
4394             if not Is_Potentially_Persistent_Type (Etype (Comp)) then
4395                return False;
4396             else
4397                Next_Entity (Comp);
4398             end if;
4399          end loop;
4400
4401          return True;
4402
4403       --  Array type is potentially persistent if its component type is
4404       --  potentially persistent and if all its constraints are static.
4405
4406       elsif Is_Array_Type (T) then
4407          if not Is_Potentially_Persistent_Type (Component_Type (T)) then
4408             return False;
4409          end if;
4410
4411          Indx := First_Index (T);
4412          while Present (Indx) loop
4413             if not Is_OK_Static_Subtype (Etype (Indx)) then
4414                return False;
4415             else
4416                Next_Index (Indx);
4417             end if;
4418          end loop;
4419
4420          return True;
4421
4422       --  All other types are not potentially persistent
4423
4424       else
4425          return False;
4426       end if;
4427    end Is_Potentially_Persistent_Type;
4428
4429    -----------------------------
4430    -- Is_RCI_Pkg_Spec_Or_Body --
4431    -----------------------------
4432
4433    function Is_RCI_Pkg_Spec_Or_Body (Cunit : Node_Id) return Boolean is
4434
4435       function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean;
4436       --  Return True if the unit of Cunit is an RCI package declaration
4437
4438       ---------------------------
4439       -- Is_RCI_Pkg_Decl_Cunit --
4440       ---------------------------
4441
4442       function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean is
4443          The_Unit : constant Node_Id := Unit (Cunit);
4444
4445       begin
4446          if Nkind (The_Unit) /= N_Package_Declaration then
4447             return False;
4448          end if;
4449          return Is_Remote_Call_Interface (Defining_Entity (The_Unit));
4450       end Is_RCI_Pkg_Decl_Cunit;
4451
4452    --  Start of processing for Is_RCI_Pkg_Spec_Or_Body
4453
4454    begin
4455       return Is_RCI_Pkg_Decl_Cunit (Cunit)
4456         or else
4457          (Nkind (Unit (Cunit)) = N_Package_Body
4458            and then Is_RCI_Pkg_Decl_Cunit (Library_Unit (Cunit)));
4459    end Is_RCI_Pkg_Spec_Or_Body;
4460
4461    -----------------------------------------
4462    -- Is_Remote_Access_To_Class_Wide_Type --
4463    -----------------------------------------
4464
4465    function Is_Remote_Access_To_Class_Wide_Type
4466      (E : Entity_Id) return Boolean
4467    is
4468       D : Entity_Id;
4469
4470       function Comes_From_Limited_Private_Type_Declaration
4471         (E : Entity_Id) return Boolean;
4472       --  Check that the type is declared by a limited type declaration,
4473       --  or else is derived from a Remote_Type ancestor through private
4474       --  extensions.
4475
4476       -------------------------------------------------
4477       -- Comes_From_Limited_Private_Type_Declaration --
4478       -------------------------------------------------
4479
4480       function Comes_From_Limited_Private_Type_Declaration
4481         (E : Entity_Id) return Boolean
4482       is
4483          N : constant Node_Id := Declaration_Node (E);
4484
4485       begin
4486          if Nkind (N) = N_Private_Type_Declaration
4487            and then Limited_Present (N)
4488          then
4489             return True;
4490          end if;
4491
4492          if Nkind (N) = N_Private_Extension_Declaration then
4493             return
4494               Comes_From_Limited_Private_Type_Declaration (Etype (E))
4495                 or else
4496                  (Is_Remote_Types (Etype (E))
4497                     and then Is_Limited_Record (Etype (E))
4498                     and then Has_Private_Declaration (Etype (E)));
4499          end if;
4500
4501          return False;
4502       end Comes_From_Limited_Private_Type_Declaration;
4503
4504    --  Start of processing for Is_Remote_Access_To_Class_Wide_Type
4505
4506    begin
4507       if not (Is_Remote_Call_Interface (E)
4508                or else Is_Remote_Types (E))
4509         or else Ekind (E) /= E_General_Access_Type
4510       then
4511          return False;
4512       end if;
4513
4514       D := Designated_Type (E);
4515
4516       if Ekind (D) /= E_Class_Wide_Type then
4517          return False;
4518       end if;
4519
4520       return Comes_From_Limited_Private_Type_Declaration
4521                (Defining_Identifier (Parent (D)));
4522    end Is_Remote_Access_To_Class_Wide_Type;
4523
4524    -----------------------------------------
4525    -- Is_Remote_Access_To_Subprogram_Type --
4526    -----------------------------------------
4527
4528    function Is_Remote_Access_To_Subprogram_Type
4529      (E : Entity_Id) return Boolean
4530    is
4531    begin
4532       return (Ekind (E) = E_Access_Subprogram_Type
4533                 or else (Ekind (E) = E_Record_Type
4534                            and then Present (Corresponding_Remote_Type (E))))
4535         and then (Is_Remote_Call_Interface (E)
4536                    or else Is_Remote_Types (E));
4537    end Is_Remote_Access_To_Subprogram_Type;
4538
4539    --------------------
4540    -- Is_Remote_Call --
4541    --------------------
4542
4543    function Is_Remote_Call (N : Node_Id) return Boolean is
4544    begin
4545       if Nkind (N) /= N_Procedure_Call_Statement
4546         and then Nkind (N) /= N_Function_Call
4547       then
4548          --  An entry call cannot be remote
4549
4550          return False;
4551
4552       elsif Nkind (Name (N)) in N_Has_Entity
4553         and then Is_Remote_Call_Interface (Entity (Name (N)))
4554       then
4555          --  A subprogram declared in the spec of a RCI package is remote
4556
4557          return True;
4558
4559       elsif Nkind (Name (N)) = N_Explicit_Dereference
4560         and then Is_Remote_Access_To_Subprogram_Type
4561                    (Etype (Prefix (Name (N))))
4562       then
4563          --  The dereference of a RAS is a remote call
4564
4565          return True;
4566
4567       elsif Present (Controlling_Argument (N))
4568         and then Is_Remote_Access_To_Class_Wide_Type
4569           (Etype (Controlling_Argument (N)))
4570       then
4571          --  Any primitive operation call with a controlling argument of
4572          --  a RACW type is a remote call.
4573
4574          return True;
4575       end if;
4576
4577       --  All other calls are local calls
4578
4579       return False;
4580    end Is_Remote_Call;
4581
4582    ----------------------
4583    -- Is_Renamed_Entry --
4584    ----------------------
4585
4586    function Is_Renamed_Entry (Proc_Nam : Entity_Id) return Boolean is
4587       Orig_Node : Node_Id := Empty;
4588       Subp_Decl : Node_Id := Parent (Parent (Proc_Nam));
4589
4590       function Is_Entry (Nam : Node_Id) return Boolean;
4591       --  Determine whether Nam is an entry. Traverse selectors
4592       --  if there are nested selected components.
4593
4594       --------------
4595       -- Is_Entry --
4596       --------------
4597
4598       function Is_Entry (Nam : Node_Id) return Boolean is
4599       begin
4600          if Nkind (Nam) = N_Selected_Component then
4601             return Is_Entry (Selector_Name (Nam));
4602          end if;
4603
4604          return Ekind (Entity (Nam)) = E_Entry;
4605       end Is_Entry;
4606
4607    --  Start of processing for Is_Renamed_Entry
4608
4609    begin
4610       if Present (Alias (Proc_Nam)) then
4611          Subp_Decl := Parent (Parent (Alias (Proc_Nam)));
4612       end if;
4613
4614       --  Look for a rewritten subprogram renaming declaration
4615
4616       if Nkind (Subp_Decl) = N_Subprogram_Declaration
4617         and then Present (Original_Node (Subp_Decl))
4618       then
4619          Orig_Node := Original_Node (Subp_Decl);
4620       end if;
4621
4622       --  The rewritten subprogram is actually an entry
4623
4624       if Present (Orig_Node)
4625         and then Nkind (Orig_Node) = N_Subprogram_Renaming_Declaration
4626         and then Is_Entry (Name (Orig_Node))
4627       then
4628          return True;
4629       end if;
4630
4631       return False;
4632    end Is_Renamed_Entry;
4633
4634    ----------------------
4635    -- Is_Selector_Name --
4636    ----------------------
4637
4638    function Is_Selector_Name (N : Node_Id) return Boolean is
4639    begin
4640       if not Is_List_Member (N) then
4641          declare
4642             P : constant Node_Id   := Parent (N);
4643             K : constant Node_Kind := Nkind (P);
4644          begin
4645             return
4646               (K = N_Expanded_Name          or else
4647                K = N_Generic_Association    or else
4648                K = N_Parameter_Association  or else
4649                K = N_Selected_Component)
4650               and then Selector_Name (P) = N;
4651          end;
4652
4653       else
4654          declare
4655             L : constant List_Id := List_Containing (N);
4656             P : constant Node_Id := Parent (L);
4657          begin
4658             return (Nkind (P) = N_Discriminant_Association
4659                      and then Selector_Names (P) = L)
4660               or else
4661                    (Nkind (P) = N_Component_Association
4662                      and then Choices (P) = L);
4663          end;
4664       end if;
4665    end Is_Selector_Name;
4666
4667    ------------------
4668    -- Is_Statement --
4669    ------------------
4670
4671    function Is_Statement (N : Node_Id) return Boolean is
4672    begin
4673       return
4674         Nkind (N) in N_Statement_Other_Than_Procedure_Call
4675           or else Nkind (N) = N_Procedure_Call_Statement;
4676    end Is_Statement;
4677
4678    -----------------
4679    -- Is_Transfer --
4680    -----------------
4681
4682    function Is_Transfer (N : Node_Id) return Boolean is
4683       Kind : constant Node_Kind := Nkind (N);
4684
4685    begin
4686       if Kind = N_Return_Statement
4687            or else
4688          Kind = N_Goto_Statement
4689            or else
4690          Kind = N_Raise_Statement
4691            or else
4692          Kind = N_Requeue_Statement
4693       then
4694          return True;
4695
4696       elsif (Kind = N_Exit_Statement or else Kind in N_Raise_xxx_Error)
4697         and then No (Condition (N))
4698       then
4699          return True;
4700
4701       elsif Kind = N_Procedure_Call_Statement
4702         and then Is_Entity_Name (Name (N))
4703         and then Present (Entity (Name (N)))
4704         and then No_Return (Entity (Name (N)))
4705       then
4706          return True;
4707
4708       elsif Nkind (Original_Node (N)) = N_Raise_Statement then
4709          return True;
4710
4711       else
4712          return False;
4713       end if;
4714    end Is_Transfer;
4715
4716    -------------
4717    -- Is_True --
4718    -------------
4719
4720    function Is_True (U : Uint) return Boolean is
4721    begin
4722       return (U /= 0);
4723    end Is_True;
4724
4725    -----------------
4726    -- Is_Variable --
4727    -----------------
4728
4729    function Is_Variable (N : Node_Id) return Boolean is
4730
4731       Orig_Node : constant Node_Id := Original_Node (N);
4732       --  We do the test on the original node, since this is basically a
4733       --  test of syntactic categories, so it must not be disturbed by
4734       --  whatever rewriting might have occurred. For example, an aggregate,
4735       --  which is certainly NOT a variable, could be turned into a variable
4736       --  by expansion.
4737
4738       function In_Protected_Function (E : Entity_Id) return Boolean;
4739       --  Within a protected function, the private components of the
4740       --  enclosing protected type are constants. A function nested within
4741       --  a (protected) procedure is not itself protected.
4742
4743       function Is_Variable_Prefix (P : Node_Id) return Boolean;
4744       --  Prefixes can involve implicit dereferences, in which case we
4745       --  must test for the case of a reference of a constant access
4746       --  type, which can never be a variable.
4747
4748       ---------------------------
4749       -- In_Protected_Function --
4750       ---------------------------
4751
4752       function In_Protected_Function (E : Entity_Id) return Boolean is
4753          Prot : constant Entity_Id := Scope (E);
4754          S    : Entity_Id;
4755
4756       begin
4757          if not Is_Protected_Type (Prot) then
4758             return False;
4759          else
4760             S := Current_Scope;
4761             while Present (S) and then S /= Prot loop
4762                if Ekind (S) = E_Function
4763                  and then Scope (S) = Prot
4764                then
4765                   return True;
4766                end if;
4767
4768                S := Scope (S);
4769             end loop;
4770
4771             return False;
4772          end if;
4773       end In_Protected_Function;
4774
4775       ------------------------
4776       -- Is_Variable_Prefix --
4777       ------------------------
4778
4779       function Is_Variable_Prefix (P : Node_Id) return Boolean is
4780       begin
4781          if Is_Access_Type (Etype (P)) then
4782             return not Is_Access_Constant (Root_Type (Etype (P)));
4783
4784          --  For the case of an indexed component whose prefix has a packed
4785          --  array type, the prefix has been rewritten into a type conversion.
4786          --  Determine variable-ness from the converted expression.
4787
4788          elsif Nkind (P) = N_Type_Conversion
4789            and then not Comes_From_Source (P)
4790            and then Is_Array_Type (Etype (P))
4791            and then Is_Packed (Etype (P))
4792          then
4793             return Is_Variable (Expression (P));
4794
4795          else
4796             return Is_Variable (P);
4797          end if;
4798       end Is_Variable_Prefix;
4799
4800    --  Start of processing for Is_Variable
4801
4802    begin
4803       --  Definitely OK if Assignment_OK is set. Since this is something that
4804       --  only gets set for expanded nodes, the test is on N, not Orig_Node.
4805
4806       if Nkind (N) in N_Subexpr and then Assignment_OK (N) then
4807          return True;
4808
4809       --  Normally we go to the original node, but there is one exception
4810       --  where we use the rewritten node, namely when it is an explicit
4811       --  dereference. The generated code may rewrite a prefix which is an
4812       --  access type with an explicit dereference. The dereference is a
4813       --  variable, even though the original node may not be (since it could
4814       --  be a constant of the access type).
4815
4816       elsif Nkind (N) = N_Explicit_Dereference
4817         and then Nkind (Orig_Node) /= N_Explicit_Dereference
4818         and then Is_Access_Type (Etype (Orig_Node))
4819       then
4820          return Is_Variable_Prefix (Original_Node (Prefix (N)));
4821
4822       --  A function call is never a variable
4823
4824       elsif Nkind (N) = N_Function_Call then
4825          return False;
4826
4827       --  All remaining checks use the original node
4828
4829       elsif Is_Entity_Name (Orig_Node) then
4830          declare
4831             E : constant Entity_Id := Entity (Orig_Node);
4832             K : constant Entity_Kind := Ekind (E);
4833
4834          begin
4835             return (K = E_Variable
4836                       and then Nkind (Parent (E)) /= N_Exception_Handler)
4837               or else  (K = E_Component
4838                           and then not In_Protected_Function (E))
4839               or else  K = E_Out_Parameter
4840               or else  K = E_In_Out_Parameter
4841               or else  K = E_Generic_In_Out_Parameter
4842
4843                --  Current instance of type:
4844
4845               or else (Is_Type (E) and then In_Open_Scopes (E))
4846               or else (Is_Incomplete_Or_Private_Type (E)
4847                         and then In_Open_Scopes (Full_View (E)));
4848          end;
4849
4850       else
4851          case Nkind (Orig_Node) is
4852             when N_Indexed_Component | N_Slice =>
4853                return Is_Variable_Prefix (Prefix (Orig_Node));
4854
4855             when N_Selected_Component =>
4856                return Is_Variable_Prefix (Prefix (Orig_Node))
4857                  and then Is_Variable (Selector_Name (Orig_Node));
4858
4859             --  For an explicit dereference, the type of the prefix cannot
4860             --  be an access to constant or an access to subprogram.
4861
4862             when N_Explicit_Dereference =>
4863                declare
4864                   Typ : constant Entity_Id := Etype (Prefix (Orig_Node));
4865                begin
4866                   return Is_Access_Type (Typ)
4867                     and then not Is_Access_Constant (Root_Type (Typ))
4868                     and then Ekind (Typ) /= E_Access_Subprogram_Type;
4869                end;
4870
4871             --  The type conversion is the case where we do not deal with the
4872             --  context dependent special case of an actual parameter. Thus
4873             --  the type conversion is only considered a variable for the
4874             --  purposes of this routine if the target type is tagged. However,
4875             --  a type conversion is considered to be a variable if it does not
4876             --  come from source (this deals for example with the conversions
4877             --  of expressions to their actual subtypes).
4878
4879             when N_Type_Conversion =>
4880                return Is_Variable (Expression (Orig_Node))
4881                  and then
4882                    (not Comes_From_Source (Orig_Node)
4883                       or else
4884                         (Is_Tagged_Type (Etype (Subtype_Mark (Orig_Node)))
4885                           and then
4886                          Is_Tagged_Type (Etype (Expression (Orig_Node)))));
4887
4888             --  GNAT allows an unchecked type conversion as a variable. This
4889             --  only affects the generation of internal expanded code, since
4890             --  calls to instantiations of Unchecked_Conversion are never
4891             --  considered variables (since they are function calls).
4892             --  This is also true for expression actions.
4893
4894             when N_Unchecked_Type_Conversion =>
4895                return Is_Variable (Expression (Orig_Node));
4896
4897             when others =>
4898                return False;
4899          end case;
4900       end if;
4901    end Is_Variable;
4902
4903    ------------------------
4904    -- Is_Volatile_Object --
4905    ------------------------
4906
4907    function Is_Volatile_Object (N : Node_Id) return Boolean is
4908
4909       function Object_Has_Volatile_Components (N : Node_Id) return Boolean;
4910       --  Determines if given object has volatile components
4911
4912       function Is_Volatile_Prefix (N : Node_Id) return Boolean;
4913       --  If prefix is an implicit dereference, examine designated type
4914
4915       ------------------------
4916       -- Is_Volatile_Prefix --
4917       ------------------------
4918
4919       function Is_Volatile_Prefix (N : Node_Id) return Boolean is
4920          Typ  : constant Entity_Id := Etype (N);
4921
4922       begin
4923          if Is_Access_Type (Typ) then
4924             declare
4925                Dtyp : constant Entity_Id := Designated_Type (Typ);
4926
4927             begin
4928                return Is_Volatile (Dtyp)
4929                  or else Has_Volatile_Components (Dtyp);
4930             end;
4931
4932          else
4933             return Object_Has_Volatile_Components (N);
4934          end if;
4935       end Is_Volatile_Prefix;
4936
4937       ------------------------------------
4938       -- Object_Has_Volatile_Components --
4939       ------------------------------------
4940
4941       function Object_Has_Volatile_Components (N : Node_Id) return Boolean is
4942          Typ : constant Entity_Id := Etype (N);
4943
4944       begin
4945          if Is_Volatile (Typ)
4946            or else Has_Volatile_Components (Typ)
4947          then
4948             return True;
4949
4950          elsif Is_Entity_Name (N)
4951            and then (Has_Volatile_Components (Entity (N))
4952                       or else Is_Volatile (Entity (N)))
4953          then
4954             return True;
4955
4956          elsif Nkind (N) = N_Indexed_Component
4957            or else Nkind (N) = N_Selected_Component
4958          then
4959             return Is_Volatile_Prefix (Prefix (N));
4960
4961          else
4962             return False;
4963          end if;
4964       end Object_Has_Volatile_Components;
4965
4966    --  Start of processing for Is_Volatile_Object
4967
4968    begin
4969       if Is_Volatile (Etype (N))
4970         or else (Is_Entity_Name (N) and then Is_Volatile (Entity (N)))
4971       then
4972          return True;
4973
4974       elsif Nkind (N) = N_Indexed_Component
4975         or else Nkind (N) = N_Selected_Component
4976       then
4977          return Is_Volatile_Prefix (Prefix (N));
4978
4979       else
4980          return False;
4981       end if;
4982    end Is_Volatile_Object;
4983
4984    -------------------------
4985    -- Kill_Current_Values --
4986    -------------------------
4987
4988    procedure Kill_Current_Values is
4989       S : Entity_Id;
4990
4991       procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id);
4992       --  Clear current value for entity E and all entities chained to E
4993
4994       ------------------------------------------
4995       -- Kill_Current_Values_For_Entity_Chain --
4996       ------------------------------------------
4997
4998       procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id) is
4999          Ent : Entity_Id;
5000
5001       begin
5002          Ent := E;
5003          while Present (Ent) loop
5004             if Is_Object (Ent) then
5005                Set_Current_Value (Ent, Empty);
5006
5007                if not Can_Never_Be_Null (Ent) then
5008                   Set_Is_Known_Non_Null (Ent, False);
5009                end if;
5010             end if;
5011
5012             Next_Entity (Ent);
5013          end loop;
5014       end Kill_Current_Values_For_Entity_Chain;
5015
5016    --  Start of processing for Kill_Current_Values
5017
5018    begin
5019       --  Kill all saved checks, a special case of killing saved values
5020
5021       Kill_All_Checks;
5022
5023       --  Loop through relevant scopes, which includes the current scope and
5024       --  any parent scopes if the current scope is a block or a package.
5025
5026       S := Current_Scope;
5027       Scope_Loop : loop
5028
5029          --  Clear current values of all entities in current scope
5030
5031          Kill_Current_Values_For_Entity_Chain (First_Entity (S));
5032
5033          --  If scope is a package, also clear current values of all
5034          --  private entities in the scope.
5035
5036          if Ekind (S) = E_Package
5037               or else
5038             Ekind (S) = E_Generic_Package
5039               or else
5040             Is_Concurrent_Type (S)
5041          then
5042             Kill_Current_Values_For_Entity_Chain (First_Private_Entity (S));
5043          end if;
5044
5045          --  If this is a block or nested package, deal with parent
5046
5047          if Ekind (S) = E_Block
5048            or else (Ekind (S) = E_Package
5049                       and then not Is_Library_Level_Entity (S))
5050          then
5051             S := Scope (S);
5052          else
5053             exit Scope_Loop;
5054          end if;
5055       end loop Scope_Loop;
5056    end Kill_Current_Values;
5057
5058    --------------------------
5059    -- Kill_Size_Check_Code --
5060    --------------------------
5061
5062    procedure Kill_Size_Check_Code (E : Entity_Id) is
5063    begin
5064       if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
5065         and then Present (Size_Check_Code (E))
5066       then
5067          Remove (Size_Check_Code (E));
5068          Set_Size_Check_Code (E, Empty);
5069       end if;
5070    end Kill_Size_Check_Code;
5071
5072    -------------------------
5073    -- New_External_Entity --
5074    -------------------------
5075
5076    function New_External_Entity
5077      (Kind         : Entity_Kind;
5078       Scope_Id     : Entity_Id;
5079       Sloc_Value   : Source_Ptr;
5080       Related_Id   : Entity_Id;
5081       Suffix       : Character;
5082       Suffix_Index : Nat := 0;
5083       Prefix       : Character := ' ') return Entity_Id
5084    is
5085       N : constant Entity_Id :=
5086             Make_Defining_Identifier (Sloc_Value,
5087               New_External_Name
5088                 (Chars (Related_Id), Suffix, Suffix_Index, Prefix));
5089
5090    begin
5091       Set_Ekind          (N, Kind);
5092       Set_Is_Internal    (N, True);
5093       Append_Entity      (N, Scope_Id);
5094       Set_Public_Status  (N);
5095
5096       if Kind in Type_Kind then
5097          Init_Size_Align (N);
5098       end if;
5099
5100       return N;
5101    end New_External_Entity;
5102
5103    -------------------------
5104    -- New_Internal_Entity --
5105    -------------------------
5106
5107    function New_Internal_Entity
5108      (Kind       : Entity_Kind;
5109       Scope_Id   : Entity_Id;
5110       Sloc_Value : Source_Ptr;
5111       Id_Char    : Character) return Entity_Id
5112    is
5113       N : constant Entity_Id :=
5114             Make_Defining_Identifier (Sloc_Value, New_Internal_Name (Id_Char));
5115
5116    begin
5117       Set_Ekind          (N, Kind);
5118       Set_Is_Internal    (N, True);
5119       Append_Entity      (N, Scope_Id);
5120
5121       if Kind in Type_Kind then
5122          Init_Size_Align (N);
5123       end if;
5124
5125       return N;
5126    end New_Internal_Entity;
5127
5128    -----------------
5129    -- Next_Actual --
5130    -----------------
5131
5132    function Next_Actual (Actual_Id : Node_Id) return Node_Id is
5133       N  : Node_Id;
5134
5135    begin
5136       --  If we are pointing at a positional parameter, it is a member of
5137       --  a node list (the list of parameters), and the next parameter
5138       --  is the next node on the list, unless we hit a parameter
5139       --  association, in which case we shift to using the chain whose
5140       --  head is the First_Named_Actual in the parent, and then is
5141       --  threaded using the Next_Named_Actual of the Parameter_Association.
5142       --  All this fiddling is because the original node list is in the
5143       --  textual call order, and what we need is the declaration order.
5144
5145       if Is_List_Member (Actual_Id) then
5146          N := Next (Actual_Id);
5147
5148          if Nkind (N) = N_Parameter_Association then
5149             return First_Named_Actual (Parent (Actual_Id));
5150          else
5151             return N;
5152          end if;
5153
5154       else
5155          return Next_Named_Actual (Parent (Actual_Id));
5156       end if;
5157    end Next_Actual;
5158
5159    procedure Next_Actual (Actual_Id : in out Node_Id) is
5160    begin
5161       Actual_Id := Next_Actual (Actual_Id);
5162    end Next_Actual;
5163
5164    -----------------------
5165    -- Normalize_Actuals --
5166    -----------------------
5167
5168    --  Chain actuals according to formals of subprogram. If there are
5169    --  no named associations, the chain is simply the list of Parameter
5170    --  Associations, since the order is the same as the declaration order.
5171    --  If there are named associations, then the First_Named_Actual field
5172    --  in the N_Procedure_Call_Statement node or N_Function_Call node
5173    --  points to the Parameter_Association node for the parameter that
5174    --  comes first in declaration order. The remaining named parameters
5175    --  are then chained in declaration order using Next_Named_Actual.
5176
5177    --  This routine also verifies that the number of actuals is compatible
5178    --  with the number and default values of formals, but performs no type
5179    --  checking (type checking is done by the caller).
5180
5181    --  If the matching succeeds, Success is set to True, and the caller
5182    --  proceeds with type-checking. If the match is unsuccessful, then
5183    --  Success is set to False, and the caller attempts a different
5184    --  interpretation, if there is one.
5185
5186    --  If the flag Report is on, the call is not overloaded, and a failure
5187    --  to match can be reported here, rather than in the caller.
5188
5189    procedure Normalize_Actuals
5190      (N       : Node_Id;
5191       S       : Entity_Id;
5192       Report  : Boolean;
5193       Success : out Boolean)
5194    is
5195       Actuals     : constant List_Id := Parameter_Associations (N);
5196       Actual      : Node_Id   := Empty;
5197       Formal      : Entity_Id;
5198       Last        : Node_Id := Empty;
5199       First_Named : Node_Id := Empty;
5200       Found       : Boolean;
5201
5202       Formals_To_Match : Integer := 0;
5203       Actuals_To_Match : Integer := 0;
5204
5205       procedure Chain (A : Node_Id);
5206       --  Add named actual at the proper place in the list, using the
5207       --  Next_Named_Actual link.
5208
5209       function Reporting return Boolean;
5210       --  Determines if an error is to be reported. To report an error, we
5211       --  need Report to be True, and also we do not report errors caused
5212       --  by calls to init procs that occur within other init procs. Such
5213       --  errors must always be cascaded errors, since if all the types are
5214       --  declared correctly, the compiler will certainly build decent calls!
5215
5216       -----------
5217       -- Chain --
5218       -----------
5219
5220       procedure Chain (A : Node_Id) is
5221       begin
5222          if No (Last) then
5223
5224             --  Call node points to first actual in list
5225
5226             Set_First_Named_Actual (N, Explicit_Actual_Parameter (A));
5227
5228          else
5229             Set_Next_Named_Actual (Last, Explicit_Actual_Parameter (A));
5230          end if;
5231
5232          Last := A;
5233          Set_Next_Named_Actual (Last, Empty);
5234       end Chain;
5235
5236       ---------------
5237       -- Reporting --
5238       ---------------
5239
5240       function Reporting return Boolean is
5241       begin
5242          if not Report then
5243             return False;
5244
5245          elsif not Within_Init_Proc then
5246             return True;
5247
5248          elsif Is_Init_Proc (Entity (Name (N))) then
5249             return False;
5250
5251          else
5252             return True;
5253          end if;
5254       end Reporting;
5255
5256    --  Start of processing for Normalize_Actuals
5257
5258    begin
5259       if Is_Access_Type (S) then
5260
5261          --  The name in the call is a function call that returns an access
5262          --  to subprogram. The designated type has the list of formals.
5263
5264          Formal := First_Formal (Designated_Type (S));
5265       else
5266          Formal := First_Formal (S);
5267       end if;
5268
5269       while Present (Formal) loop
5270          Formals_To_Match := Formals_To_Match + 1;
5271          Next_Formal (Formal);
5272       end loop;
5273
5274       --  Find if there is a named association, and verify that no positional
5275       --  associations appear after named ones.
5276
5277       if Present (Actuals) then
5278          Actual := First (Actuals);
5279       end if;
5280
5281       while Present (Actual)
5282         and then Nkind (Actual) /= N_Parameter_Association
5283       loop
5284          Actuals_To_Match := Actuals_To_Match + 1;
5285          Next (Actual);
5286       end loop;
5287
5288       if No (Actual) and Actuals_To_Match = Formals_To_Match then
5289
5290          --  Most common case: positional notation, no defaults
5291
5292          Success := True;
5293          return;
5294
5295       elsif Actuals_To_Match > Formals_To_Match then
5296
5297          --  Too many actuals: will not work
5298
5299          if Reporting then
5300             if Is_Entity_Name (Name (N)) then
5301                Error_Msg_N ("too many arguments in call to&", Name (N));
5302             else
5303                Error_Msg_N ("too many arguments in call", N);
5304             end if;
5305          end if;
5306
5307          Success := False;
5308          return;
5309       end if;
5310
5311       First_Named := Actual;
5312
5313       while Present (Actual) loop
5314          if Nkind (Actual) /= N_Parameter_Association then
5315             Error_Msg_N
5316               ("positional parameters not allowed after named ones", Actual);
5317             Success := False;
5318             return;
5319
5320          else
5321             Actuals_To_Match := Actuals_To_Match + 1;
5322          end if;
5323
5324          Next (Actual);
5325       end loop;
5326
5327       if Present (Actuals) then
5328          Actual := First (Actuals);
5329       end if;
5330
5331       Formal := First_Formal (S);
5332       while Present (Formal) loop
5333
5334          --  Match the formals in order. If the corresponding actual
5335          --  is positional,  nothing to do. Else scan the list of named
5336          --  actuals to find the one with the right name.
5337
5338          if Present (Actual)
5339            and then Nkind (Actual) /= N_Parameter_Association
5340          then
5341             Next (Actual);
5342             Actuals_To_Match := Actuals_To_Match - 1;
5343             Formals_To_Match := Formals_To_Match - 1;
5344
5345          else
5346             --  For named parameters, search the list of actuals to find
5347             --  one that matches the next formal name.
5348
5349             Actual := First_Named;
5350             Found  := False;
5351
5352             while Present (Actual) loop
5353                if Chars (Selector_Name (Actual)) = Chars (Formal) then
5354                   Found := True;
5355                   Chain (Actual);
5356                   Actuals_To_Match := Actuals_To_Match - 1;
5357                   Formals_To_Match := Formals_To_Match - 1;
5358                   exit;
5359                end if;
5360
5361                Next (Actual);
5362             end loop;
5363
5364             if not Found then
5365                if Ekind (Formal) /= E_In_Parameter
5366                  or else No (Default_Value (Formal))
5367                then
5368                   if Reporting then
5369                      if (Comes_From_Source (S)
5370                           or else Sloc (S) = Standard_Location)
5371                        and then Is_Overloadable (S)
5372                      then
5373                         if No (Actuals)
5374                           and then
5375                            (Nkind (Parent (N)) = N_Procedure_Call_Statement
5376                              or else
5377                            (Nkind (Parent (N)) = N_Function_Call
5378                              or else
5379                             Nkind (Parent (N)) = N_Parameter_Association))
5380                           and then Ekind (S) /= E_Function
5381                         then
5382                            Set_Etype (N, Etype (S));
5383                         else
5384                            Error_Msg_Name_1 := Chars (S);
5385                            Error_Msg_Sloc := Sloc (S);
5386                            Error_Msg_NE
5387                              ("missing argument for parameter & " &
5388                                 "in call to % declared #", N, Formal);
5389                         end if;
5390
5391                      elsif Is_Overloadable (S) then
5392                         Error_Msg_Name_1 := Chars (S);
5393
5394                         --  Point to type derivation that generated the
5395                         --  operation.
5396
5397                         Error_Msg_Sloc := Sloc (Parent (S));
5398
5399                         Error_Msg_NE
5400                           ("missing argument for parameter & " &
5401                              "in call to % (inherited) #", N, Formal);
5402
5403                      else
5404                         Error_Msg_NE
5405                           ("missing argument for parameter &", N, Formal);
5406                      end if;
5407                   end if;
5408
5409                   Success := False;
5410                   return;
5411
5412                else
5413                   Formals_To_Match := Formals_To_Match - 1;
5414                end if;
5415             end if;
5416          end if;
5417
5418          Next_Formal (Formal);
5419       end loop;
5420
5421       if  Formals_To_Match = 0 and then Actuals_To_Match = 0 then
5422          Success := True;
5423          return;
5424
5425       else
5426          if Reporting then
5427
5428             --  Find some superfluous named actual that did not get
5429             --  attached to the list of associations.
5430
5431             Actual := First (Actuals);
5432
5433             while Present (Actual) loop
5434                if Nkind (Actual) = N_Parameter_Association
5435                  and then Actual /= Last
5436                  and then No (Next_Named_Actual (Actual))
5437                then
5438                   Error_Msg_N ("unmatched actual & in call",
5439                     Selector_Name (Actual));
5440                   exit;
5441                end if;
5442
5443                Next (Actual);
5444             end loop;
5445          end if;
5446
5447          Success := False;
5448          return;
5449       end if;
5450    end Normalize_Actuals;
5451
5452    --------------------------------
5453    -- Note_Possible_Modification --
5454    --------------------------------
5455
5456    procedure Note_Possible_Modification (N : Node_Id) is
5457       Modification_Comes_From_Source : constant Boolean :=
5458                                          Comes_From_Source (Parent (N));
5459
5460       Ent : Entity_Id;
5461       Exp : Node_Id;
5462
5463    begin
5464       --  Loop to find referenced entity, if there is one
5465
5466       Exp := N;
5467       loop
5468          <<Continue>>
5469          Ent := Empty;
5470
5471          if Is_Entity_Name (Exp) then
5472             Ent := Entity (Exp);
5473
5474             --  If the entity is missing, it is an undeclared identifier,
5475             --  and there is nothing to annotate.
5476
5477             if No (Ent) then
5478                return;
5479             end if;
5480
5481          elsif Nkind (Exp) = N_Explicit_Dereference then
5482             declare
5483                P : constant Node_Id := Prefix (Exp);
5484
5485             begin
5486                if Nkind (P) = N_Selected_Component
5487                  and then Present (
5488                    Entry_Formal (Entity (Selector_Name (P))))
5489                then
5490                   --  Case of a reference to an entry formal
5491
5492                   Ent := Entry_Formal (Entity (Selector_Name (P)));
5493
5494                elsif Nkind (P) = N_Identifier
5495                  and then Nkind (Parent (Entity (P))) = N_Object_Declaration
5496                  and then Present (Expression (Parent (Entity (P))))
5497                  and then Nkind (Expression (Parent (Entity (P))))
5498                    = N_Reference
5499                then
5500                   --  Case of a reference to a value on which
5501                   --  side effects have been removed.
5502
5503                   Exp := Prefix (Expression (Parent (Entity (P))));
5504
5505                else
5506                   return;
5507
5508                end if;
5509             end;
5510
5511          elsif     Nkind (Exp) = N_Type_Conversion
5512            or else Nkind (Exp) = N_Unchecked_Type_Conversion
5513          then
5514             Exp := Expression (Exp);
5515
5516          elsif     Nkind (Exp) = N_Slice
5517            or else Nkind (Exp) = N_Indexed_Component
5518            or else Nkind (Exp) = N_Selected_Component
5519          then
5520             Exp := Prefix (Exp);
5521
5522          else
5523             return;
5524
5525          end if;
5526
5527          --  Now look for entity being referenced
5528
5529          if Present (Ent) then
5530
5531             if Is_Object (Ent) then
5532                if Comes_From_Source (Exp)
5533                  or else Modification_Comes_From_Source
5534                then
5535                   Set_Never_Set_In_Source (Ent, False);
5536                end if;
5537
5538                Set_Is_True_Constant    (Ent, False);
5539                Set_Current_Value       (Ent, Empty);
5540
5541                if not Can_Never_Be_Null (Ent) then
5542                   Set_Is_Known_Non_Null (Ent, False);
5543                end if;
5544
5545                if (Ekind (Ent) = E_Variable or else Ekind (Ent) = E_Constant)
5546                  and then Present (Renamed_Object (Ent))
5547                then
5548                   Exp := Renamed_Object (Ent);
5549                   goto Continue;
5550                end if;
5551
5552                --  Generate a reference only if the assignment comes from
5553                --  source. This excludes, for example, calls to a dispatching
5554                --  assignment operation when the left-hand side is tagged.
5555
5556                if Modification_Comes_From_Source then
5557                   Generate_Reference (Ent, Exp, 'm');
5558                end if;
5559             end if;
5560
5561             Kill_Checks (Ent);
5562             return;
5563          end if;
5564       end loop;
5565    end Note_Possible_Modification;
5566
5567    -------------------------
5568    -- Object_Access_Level --
5569    -------------------------
5570
5571    function Object_Access_Level (Obj : Node_Id) return Uint is
5572       E : Entity_Id;
5573
5574    --  Returns the static accessibility level of the view denoted
5575    --  by Obj.  Note that the value returned is the result of a
5576    --  call to Scope_Depth.  Only scope depths associated with
5577    --  dynamic scopes can actually be returned.  Since only
5578    --  relative levels matter for accessibility checking, the fact
5579    --  that the distance between successive levels of accessibility
5580    --  is not always one is immaterial (invariant: if level(E2) is
5581    --  deeper than level(E1), then Scope_Depth(E1) < Scope_Depth(E2)).
5582
5583    begin
5584       if Is_Entity_Name (Obj) then
5585          E := Entity (Obj);
5586
5587          --  If E is a type then it denotes a current instance.
5588          --  For this case we add one to the normal accessibility
5589          --  level of the type to ensure that current instances
5590          --  are treated as always being deeper than than the level
5591          --  of any visible named access type (see 3.10.2(21)).
5592
5593          if Is_Type (E) then
5594             return Type_Access_Level (E) +  1;
5595
5596          elsif Present (Renamed_Object (E)) then
5597             return Object_Access_Level (Renamed_Object (E));
5598
5599          --  Similarly, if E is a component of the current instance of a
5600          --  protected type, any instance of it is assumed to be at a deeper
5601          --  level than the type. For a protected object (whose type is an
5602          --  anonymous protected type) its components are at the same level
5603          --  as the type itself.
5604
5605          elsif not Is_Overloadable (E)
5606            and then Ekind (Scope (E)) = E_Protected_Type
5607            and then Comes_From_Source (Scope (E))
5608          then
5609             return Type_Access_Level (Scope (E)) + 1;
5610
5611          else
5612             return Scope_Depth (Enclosing_Dynamic_Scope (E));
5613          end if;
5614
5615       elsif Nkind (Obj) = N_Selected_Component then
5616          if Is_Access_Type (Etype (Prefix (Obj))) then
5617             return Type_Access_Level (Etype (Prefix (Obj)));
5618          else
5619             return Object_Access_Level (Prefix (Obj));
5620          end if;
5621
5622       elsif Nkind (Obj) = N_Indexed_Component then
5623          if Is_Access_Type (Etype (Prefix (Obj))) then
5624             return Type_Access_Level (Etype (Prefix (Obj)));
5625          else
5626             return Object_Access_Level (Prefix (Obj));
5627          end if;
5628
5629       elsif Nkind (Obj) = N_Explicit_Dereference then
5630
5631          --  If the prefix is a selected access discriminant then
5632          --  we make a recursive call on the prefix, which will
5633          --  in turn check the level of the prefix object of
5634          --  the selected discriminant.
5635
5636          if Nkind (Prefix (Obj)) = N_Selected_Component
5637            and then Ekind (Etype (Prefix (Obj))) = E_Anonymous_Access_Type
5638            and then
5639              Ekind (Entity (Selector_Name (Prefix (Obj)))) = E_Discriminant
5640          then
5641             return Object_Access_Level (Prefix (Obj));
5642          else
5643             return Type_Access_Level (Etype (Prefix (Obj)));
5644          end if;
5645
5646       elsif Nkind (Obj) = N_Type_Conversion
5647         or else Nkind (Obj) = N_Unchecked_Type_Conversion
5648       then
5649          return Object_Access_Level (Expression (Obj));
5650
5651       --  Function results are objects, so we get either the access level
5652       --  of the function or, in the case of an indirect call, the level of
5653       --  of the access-to-subprogram type.
5654
5655       elsif Nkind (Obj) = N_Function_Call then
5656          if Is_Entity_Name (Name (Obj)) then
5657             return Subprogram_Access_Level (Entity (Name (Obj)));
5658          else
5659             return Type_Access_Level (Etype (Prefix (Name (Obj))));
5660          end if;
5661
5662       --  For convenience we handle qualified expressions, even though
5663       --  they aren't technically object names.
5664
5665       elsif Nkind (Obj) = N_Qualified_Expression then
5666          return Object_Access_Level (Expression (Obj));
5667
5668       --  Otherwise return the scope level of Standard.
5669       --  (If there are cases that fall through
5670       --  to this point they will be treated as
5671       --  having global accessibility for now. ???)
5672
5673       else
5674          return Scope_Depth (Standard_Standard);
5675       end if;
5676    end Object_Access_Level;
5677
5678    -----------------------
5679    -- Private_Component --
5680    -----------------------
5681
5682    function Private_Component (Type_Id : Entity_Id) return Entity_Id is
5683       Ancestor  : constant Entity_Id := Base_Type (Type_Id);
5684
5685       function Trace_Components
5686         (T     : Entity_Id;
5687          Check : Boolean) return Entity_Id;
5688       --  Recursive function that does the work, and checks against circular
5689       --  definition for each subcomponent type.
5690
5691       ----------------------
5692       -- Trace_Components --
5693       ----------------------
5694
5695       function Trace_Components
5696          (T     : Entity_Id;
5697           Check : Boolean) return Entity_Id
5698        is
5699          Btype     : constant Entity_Id := Base_Type (T);
5700          Component : Entity_Id;
5701          P         : Entity_Id;
5702          Candidate : Entity_Id := Empty;
5703
5704       begin
5705          if Check and then Btype = Ancestor then
5706             Error_Msg_N ("circular type definition", Type_Id);
5707             return Any_Type;
5708          end if;
5709
5710          if Is_Private_Type (Btype)
5711            and then not Is_Generic_Type (Btype)
5712          then
5713             if Present (Full_View (Btype))
5714               and then Is_Record_Type (Full_View (Btype))
5715               and then not Is_Frozen (Btype)
5716             then
5717                --  To indicate that the ancestor depends on a private type,
5718                --  the current Btype is sufficient. However, to check for
5719                --  circular definition we must recurse on the full view.
5720
5721                Candidate := Trace_Components (Full_View (Btype), True);
5722
5723                if Candidate = Any_Type then
5724                   return Any_Type;
5725                else
5726                   return Btype;
5727                end if;
5728
5729             else
5730                return Btype;
5731             end if;
5732
5733          elsif Is_Array_Type (Btype) then
5734             return Trace_Components (Component_Type (Btype), True);
5735
5736          elsif Is_Record_Type (Btype) then
5737             Component := First_Entity (Btype);
5738             while Present (Component) loop
5739
5740                --  Skip anonymous types generated by constrained components
5741
5742                if not Is_Type (Component) then
5743                   P := Trace_Components (Etype (Component), True);
5744
5745                   if Present (P) then
5746                      if P = Any_Type then
5747                         return P;
5748                      else
5749                         Candidate := P;
5750                      end if;
5751                   end if;
5752                end if;
5753
5754                Next_Entity (Component);
5755             end loop;
5756
5757             return Candidate;
5758
5759          else
5760             return Empty;
5761          end if;
5762       end Trace_Components;
5763
5764    --  Start of processing for Private_Component
5765
5766    begin
5767       return Trace_Components (Type_Id, False);
5768    end Private_Component;
5769
5770    -----------------------
5771    -- Process_End_Label --
5772    -----------------------
5773
5774    procedure Process_End_Label
5775      (N   : Node_Id;
5776       Typ : Character;
5777       Ent  : Entity_Id)
5778    is
5779       Loc  : Source_Ptr;
5780       Nam  : Node_Id;
5781
5782       Label_Ref : Boolean;
5783       --  Set True if reference to end label itself is required
5784
5785       Endl : Node_Id;
5786       --  Gets set to the operator symbol or identifier that references
5787       --  the entity Ent. For the child unit case, this is the identifier
5788       --  from the designator. For other cases, this is simply Endl.
5789
5790       procedure Generate_Parent_Ref (N : Node_Id);
5791       --  N is an identifier node that appears as a parent unit reference
5792       --  in the case where Ent is a child unit. This procedure generates
5793       --  an appropriate cross-reference entry.
5794
5795       -------------------------
5796       -- Generate_Parent_Ref --
5797       -------------------------
5798
5799       procedure Generate_Parent_Ref (N : Node_Id) is
5800          Parent_Ent : Entity_Id;
5801
5802       begin
5803          --  Search up scope stack. The reason we do this is that normal
5804          --  visibility analysis would not work for two reasons. First in
5805          --  some subunit cases, the entry for the parent unit may not be
5806          --  visible, and in any case there can be a local entity that
5807          --  hides the scope entity.
5808
5809          Parent_Ent := Current_Scope;
5810          while Present (Parent_Ent) loop
5811             if Chars (Parent_Ent) = Chars (N) then
5812
5813                --  Generate the reference. We do NOT consider this as a
5814                --  reference for unreferenced symbol purposes, but we do
5815                --  force a cross-reference even if the end line does not
5816                --  come from source (the caller already generated the
5817                --  appropriate Typ for this situation).
5818
5819                Generate_Reference
5820                  (Parent_Ent, N, 'r', Set_Ref => False, Force => True);
5821                Style.Check_Identifier (N, Parent_Ent);
5822                return;
5823             end if;
5824
5825             Parent_Ent := Scope (Parent_Ent);
5826          end loop;
5827
5828          --  Fall through means entity was not found -- that's odd, but
5829          --  the appropriate thing is simply to ignore and not generate
5830          --  any cross-reference for this entry.
5831
5832          return;
5833       end Generate_Parent_Ref;
5834
5835    --  Start of processing for Process_End_Label
5836
5837    begin
5838       --  If no node, ignore. This happens in some error situations,
5839       --  and also for some internally generated structures where no
5840       --  end label references are required in any case.
5841
5842       if No (N) then
5843          return;
5844       end if;
5845
5846       --  Nothing to do if no End_Label, happens for internally generated
5847       --  constructs where we don't want an end label reference anyway.
5848       --  Also nothing to do if Endl is a string literal, which means
5849       --  there was some prior error (bad operator symbol)
5850
5851       Endl := End_Label (N);
5852
5853       if No (Endl) or else Nkind (Endl) = N_String_Literal then
5854          return;
5855       end if;
5856
5857       --  Reference node is not in extended main source unit
5858
5859       if not In_Extended_Main_Source_Unit (N) then
5860
5861          --  Generally we do not collect references except for the
5862          --  extended main source unit. The one exception is the 'e'
5863          --  entry for a package spec, where it is useful for a client
5864          --  to have the ending information to define scopes.
5865
5866          if Typ /= 'e' then
5867             return;
5868
5869          else
5870             Label_Ref := False;
5871
5872             --  For this case, we can ignore any parent references,
5873             --  but we need the package name itself for the 'e' entry.
5874
5875             if Nkind (Endl) = N_Designator then
5876                Endl := Identifier (Endl);
5877             end if;
5878          end if;
5879
5880       --  Reference is in extended main source unit
5881
5882       else
5883          Label_Ref := True;
5884
5885          --  For designator, generate references for the parent entries
5886
5887          if Nkind (Endl) = N_Designator then
5888
5889             --  Generate references for the prefix if the END line comes
5890             --  from source (otherwise we do not need these references)
5891
5892             if Comes_From_Source (Endl) then
5893                Nam := Name (Endl);
5894                while Nkind (Nam) = N_Selected_Component loop
5895                   Generate_Parent_Ref (Selector_Name (Nam));
5896                   Nam := Prefix (Nam);
5897                end loop;
5898
5899                Generate_Parent_Ref (Nam);
5900             end if;
5901
5902             Endl := Identifier (Endl);
5903          end if;
5904       end if;
5905
5906       --  If the end label is not for the given entity, then either we have
5907       --  some previous error, or this is a generic instantiation for which
5908       --  we do not need to make a cross-reference in this case anyway. In
5909       --  either case we simply ignore the call.
5910
5911       if Chars (Ent) /= Chars (Endl) then
5912          return;
5913       end if;
5914
5915       --  If label was really there, then generate a normal reference
5916       --  and then adjust the location in the end label to point past
5917       --  the name (which should almost always be the semicolon).
5918
5919       Loc := Sloc (Endl);
5920
5921       if Comes_From_Source (Endl) then
5922
5923          --  If a label reference is required, then do the style check
5924          --  and generate an l-type cross-reference entry for the label
5925
5926          if Label_Ref then
5927             if Style_Check then
5928                Style.Check_Identifier (Endl, Ent);
5929             end if;
5930             Generate_Reference (Ent, Endl, 'l', Set_Ref => False);
5931          end if;
5932
5933          --  Set the location to point past the label (normally this will
5934          --  mean the semicolon immediately following the label). This is
5935          --  done for the sake of the 'e' or 't' entry generated below.
5936
5937          Get_Decoded_Name_String (Chars (Endl));
5938          Set_Sloc (Endl, Sloc (Endl) + Source_Ptr (Name_Len));
5939       end if;
5940
5941       --  Now generate the e/t reference
5942
5943       Generate_Reference (Ent, Endl, Typ, Set_Ref => False, Force => True);
5944
5945       --  Restore Sloc, in case modified above, since we have an identifier
5946       --  and the normal Sloc should be left set in the tree.
5947
5948       Set_Sloc (Endl, Loc);
5949    end Process_End_Label;
5950
5951    ------------------
5952    -- Real_Convert --
5953    ------------------
5954
5955    --  We do the conversion to get the value of the real string by using
5956    --  the scanner, see Sinput for details on use of the internal source
5957    --  buffer for scanning internal strings.
5958
5959    function Real_Convert (S : String) return Node_Id is
5960       Save_Src : constant Source_Buffer_Ptr := Source;
5961       Negative : Boolean;
5962
5963    begin
5964       Source := Internal_Source_Ptr;
5965       Scan_Ptr := 1;
5966
5967       for J in S'Range loop
5968          Source (Source_Ptr (J)) := S (J);
5969       end loop;
5970
5971       Source (S'Length + 1) := EOF;
5972
5973       if Source (Scan_Ptr) = '-' then
5974          Negative := True;
5975          Scan_Ptr := Scan_Ptr + 1;
5976       else
5977          Negative := False;
5978       end if;
5979
5980       Scan;
5981
5982       if Negative then
5983          Set_Realval (Token_Node, UR_Negate (Realval (Token_Node)));
5984       end if;
5985
5986       Source := Save_Src;
5987       return Token_Node;
5988    end Real_Convert;
5989
5990    ---------------------
5991    -- Rep_To_Pos_Flag --
5992    ---------------------
5993
5994    function Rep_To_Pos_Flag (E : Entity_Id; Loc : Source_Ptr) return Node_Id is
5995    begin
5996       return New_Occurrence_Of
5997                (Boolean_Literals (not Range_Checks_Suppressed (E)), Loc);
5998    end Rep_To_Pos_Flag;
5999
6000    --------------------
6001    -- Require_Entity --
6002    --------------------
6003
6004    procedure Require_Entity (N : Node_Id) is
6005    begin
6006       if Is_Entity_Name (N) and then No (Entity (N)) then
6007          if Total_Errors_Detected /= 0 then
6008             Set_Entity (N, Any_Id);
6009          else
6010             raise Program_Error;
6011          end if;
6012       end if;
6013    end Require_Entity;
6014
6015    ------------------------------
6016    -- Requires_Transient_Scope --
6017    ------------------------------
6018
6019    --  A transient scope is required when variable-sized temporaries are
6020    --  allocated in the primary or secondary stack, or when finalization
6021    --  actions must be generated before the next instruction.
6022
6023    function Requires_Transient_Scope (Id : Entity_Id) return Boolean is
6024       Typ : constant Entity_Id := Underlying_Type (Id);
6025
6026    --  Start of processing for Requires_Transient_Scope
6027
6028    begin
6029       --  This is a private type which is not completed yet. This can only
6030       --  happen in a default expression (of a formal parameter or of a
6031       --  record component). Do not expand transient scope in this case
6032
6033       if No (Typ) then
6034          return False;
6035
6036       --  Do not expand transient scope for non-existent procedure return
6037
6038       elsif Typ = Standard_Void_Type then
6039          return False;
6040
6041       --  Elementary types do not require a transient scope
6042
6043       elsif Is_Elementary_Type (Typ) then
6044          return False;
6045
6046       --  Generally, indefinite subtypes require a transient scope, since the
6047       --  back end cannot generate temporaries, since this is not a valid type
6048       --  for declaring an object. It might be possible to relax this in the
6049       --  future, e.g. by declaring the maximum possible space for the type.
6050
6051       elsif Is_Indefinite_Subtype (Typ) then
6052          return True;
6053
6054       --  Functions returning tagged types may dispatch on result so their
6055       --  returned value is allocated on the secondary stack. Controlled
6056       --  type temporaries need finalization.
6057
6058       elsif Is_Tagged_Type (Typ)
6059         or else Has_Controlled_Component (Typ)
6060       then
6061          return True;
6062
6063       --  Record type
6064
6065       elsif Is_Record_Type (Typ) then
6066
6067          --  In GCC 2, discriminated records always require a transient
6068          --  scope because the back end otherwise tries to allocate a
6069          --  variable length temporary for the particular variant.
6070
6071          if Opt.GCC_Version = 2
6072            and then Has_Discriminants (Typ)
6073          then
6074             return True;
6075
6076          --  For GCC 3, or for a non-discriminated record in GCC 2, we are
6077          --  OK if none of the component types requires a transient scope.
6078          --  Note that we already know that this is a definite type (i.e.
6079          --  has discriminant defaults if it is a discriminated record).
6080
6081          else
6082             declare
6083                Comp : Entity_Id;
6084             begin
6085                Comp := First_Entity (Typ);
6086                while Present (Comp) loop
6087                   if Ekind (Comp) = E_Component
6088                      and then Requires_Transient_Scope (Etype (Comp))
6089                   then
6090                      return True;
6091                   else
6092                      Next_Entity (Comp);
6093                   end if;
6094                end loop;
6095             end;
6096
6097             return False;
6098          end if;
6099
6100       --  String literal types never require transient scope
6101
6102       elsif Ekind (Typ) = E_String_Literal_Subtype then
6103          return False;
6104
6105       --  Array type. Note that we already know that this is a constrained
6106       --  array, since unconstrained arrays will fail the indefinite test.
6107
6108       elsif Is_Array_Type (Typ) then
6109
6110          --  If component type requires a transient scope, the array does too
6111
6112          if Requires_Transient_Scope (Component_Type (Typ)) then
6113             return True;
6114
6115          --  Otherwise, we only need a transient scope if the size is not
6116          --  known at compile time.
6117
6118          else
6119             return not Size_Known_At_Compile_Time (Typ);
6120          end if;
6121
6122       --  All other cases do not require a transient scope
6123
6124       else
6125          return False;
6126       end if;
6127    end Requires_Transient_Scope;
6128
6129    --------------------------
6130    -- Reset_Analyzed_Flags --
6131    --------------------------
6132
6133    procedure Reset_Analyzed_Flags (N : Node_Id) is
6134
6135       function Clear_Analyzed
6136         (N : Node_Id) return Traverse_Result;
6137       --  Function used to reset Analyzed flags in tree. Note that we do
6138       --  not reset Analyzed flags in entities, since there is no need to
6139       --  renalalyze entities, and indeed, it is wrong to do so, since it
6140       --  can result in generating auxiliary stuff more than once.
6141
6142       --------------------
6143       -- Clear_Analyzed --
6144       --------------------
6145
6146       function Clear_Analyzed
6147         (N : Node_Id) return Traverse_Result
6148       is
6149       begin
6150          if not Has_Extension (N) then
6151             Set_Analyzed (N, False);
6152          end if;
6153
6154          return OK;
6155       end Clear_Analyzed;
6156
6157       function Reset_Analyzed is
6158         new Traverse_Func (Clear_Analyzed);
6159
6160       Discard : Traverse_Result;
6161       pragma Warnings (Off, Discard);
6162
6163    --  Start of processing for Reset_Analyzed_Flags
6164
6165    begin
6166       Discard := Reset_Analyzed (N);
6167    end Reset_Analyzed_Flags;
6168
6169    ---------------------------
6170    -- Safe_To_Capture_Value --
6171    ---------------------------
6172
6173    function Safe_To_Capture_Value
6174      (N   : Node_Id;
6175       Ent : Entity_Id) return Boolean
6176    is
6177    begin
6178       --  The only entities for which we track constant values are variables,
6179       --  out parameters and in out parameters, so check if we have this case.
6180
6181       if Ekind (Ent) /= E_Variable
6182            and then
6183          Ekind (Ent) /= E_Out_Parameter
6184            and then
6185          Ekind (Ent) /= E_In_Out_Parameter
6186       then
6187          return False;
6188       end if;
6189
6190       --  Skip volatile and aliased variables, since funny things might
6191       --  be going on in these cases which we cannot necessarily track.
6192       --  Also skip any variable for which an address clause is given.
6193
6194       --  Should we have a flag Has_Address_Clause ???
6195
6196       if Treat_As_Volatile (Ent)
6197         or else Is_Aliased (Ent)
6198         or else Present (Address_Clause (Ent))
6199       then
6200          return False;
6201       end if;
6202
6203       --  OK, all above conditions are met. We also require that the scope
6204       --  of the reference be the same as the scope of the entity, not
6205       --  counting packages and blocks.
6206
6207       declare
6208          E_Scope : constant Entity_Id := Scope (Ent);
6209          R_Scope : Entity_Id;
6210
6211       begin
6212          R_Scope := Current_Scope;
6213          while R_Scope /= Standard_Standard loop
6214             exit when R_Scope = E_Scope;
6215
6216             if Ekind (R_Scope) /= E_Package
6217                  and then
6218                Ekind (R_Scope) /= E_Block
6219             then
6220                return False;
6221             else
6222                R_Scope := Scope (R_Scope);
6223             end if;
6224          end loop;
6225       end;
6226
6227       --  We also require that the reference does not appear in a context
6228       --  where it is not sure to be executed (i.e. a conditional context
6229       --  or an exception handler).
6230
6231       declare
6232          Desc : Node_Id;
6233          P    : Node_Id;
6234
6235       begin
6236          Desc := N;
6237          P    := Parent (N);
6238          while Present (P) loop
6239             if Nkind (P) = N_If_Statement
6240               or else  Nkind (P) = N_Case_Statement
6241               or else (Nkind (P) = N_And_Then and then Desc = Right_Opnd (P))
6242               or else (Nkind (P) = N_Or_Else and then Desc = Right_Opnd (P))
6243               or else  Nkind (P) = N_Exception_Handler
6244               or else  Nkind (P) = N_Selective_Accept
6245               or else  Nkind (P) = N_Conditional_Entry_Call
6246               or else  Nkind (P) = N_Timed_Entry_Call
6247               or else  Nkind (P) = N_Asynchronous_Select
6248             then
6249                return False;
6250             else
6251                Desc := P;
6252                P    := Parent (P);
6253             end if;
6254          end loop;
6255       end;
6256
6257       --  OK, looks safe to set value
6258
6259       return True;
6260    end Safe_To_Capture_Value;
6261
6262    ---------------
6263    -- Same_Name --
6264    ---------------
6265
6266    function Same_Name (N1, N2 : Node_Id) return Boolean is
6267       K1 : constant Node_Kind := Nkind (N1);
6268       K2 : constant Node_Kind := Nkind (N2);
6269
6270    begin
6271       if (K1 = N_Identifier or else K1 = N_Defining_Identifier)
6272         and then (K2 = N_Identifier or else K2 = N_Defining_Identifier)
6273       then
6274          return Chars (N1) = Chars (N2);
6275
6276       elsif (K1 = N_Selected_Component or else K1 = N_Expanded_Name)
6277         and then (K2 = N_Selected_Component or else K2 = N_Expanded_Name)
6278       then
6279          return Same_Name (Selector_Name (N1), Selector_Name (N2))
6280            and then Same_Name (Prefix (N1), Prefix (N2));
6281
6282       else
6283          return False;
6284       end if;
6285    end Same_Name;
6286
6287    ---------------
6288    -- Same_Type --
6289    ---------------
6290
6291    function Same_Type (T1, T2 : Entity_Id) return Boolean is
6292    begin
6293       if T1 = T2 then
6294          return True;
6295
6296       elsif not Is_Constrained (T1)
6297         and then not Is_Constrained (T2)
6298         and then Base_Type (T1) = Base_Type (T2)
6299       then
6300          return True;
6301
6302       --  For now don't bother with case of identical constraints, to be
6303       --  fiddled with later on perhaps (this is only used for optimization
6304       --  purposes, so it is not critical to do a best possible job)
6305
6306       else
6307          return False;
6308       end if;
6309    end Same_Type;
6310
6311    ------------------------
6312    -- Scope_Is_Transient --
6313    ------------------------
6314
6315    function Scope_Is_Transient  return Boolean is
6316    begin
6317       return Scope_Stack.Table (Scope_Stack.Last).Is_Transient;
6318    end Scope_Is_Transient;
6319
6320    ------------------
6321    -- Scope_Within --
6322    ------------------
6323
6324    function Scope_Within (Scope1, Scope2 : Entity_Id) return Boolean is
6325       Scop : Entity_Id;
6326
6327    begin
6328       Scop := Scope1;
6329       while Scop /= Standard_Standard loop
6330          Scop := Scope (Scop);
6331
6332          if Scop = Scope2 then
6333             return True;
6334          end if;
6335       end loop;
6336
6337       return False;
6338    end Scope_Within;
6339
6340    --------------------------
6341    -- Scope_Within_Or_Same --
6342    --------------------------
6343
6344    function Scope_Within_Or_Same (Scope1, Scope2 : Entity_Id) return Boolean is
6345       Scop : Entity_Id;
6346
6347    begin
6348       Scop := Scope1;
6349       while Scop /= Standard_Standard loop
6350          if Scop = Scope2 then
6351             return True;
6352          else
6353             Scop := Scope (Scop);
6354          end if;
6355       end loop;
6356
6357       return False;
6358    end Scope_Within_Or_Same;
6359
6360    ------------------------
6361    -- Set_Current_Entity --
6362    ------------------------
6363
6364    --  The given entity is to be set as the currently visible definition
6365    --  of its associated name (i.e. the Node_Id associated with its name).
6366    --  All we have to do is to get the name from the identifier, and
6367    --  then set the associated Node_Id to point to the given entity.
6368
6369    procedure Set_Current_Entity (E : Entity_Id) is
6370    begin
6371       Set_Name_Entity_Id (Chars (E), E);
6372    end Set_Current_Entity;
6373
6374    ---------------------------------
6375    -- Set_Entity_With_Style_Check --
6376    ---------------------------------
6377
6378    procedure Set_Entity_With_Style_Check (N : Node_Id; Val : Entity_Id) is
6379       Val_Actual : Entity_Id;
6380       Nod        : Node_Id;
6381
6382    begin
6383       Set_Entity (N, Val);
6384
6385       if Style_Check
6386         and then not Suppress_Style_Checks (Val)
6387         and then not In_Instance
6388       then
6389          if Nkind (N) = N_Identifier then
6390             Nod := N;
6391
6392          elsif Nkind (N) = N_Expanded_Name then
6393             Nod := Selector_Name (N);
6394
6395          else
6396             return;
6397          end if;
6398
6399          --  A special situation arises for derived operations, where we want
6400          --  to do the check against the parent (since the Sloc of the derived
6401          --  operation points to the derived type declaration itself).
6402
6403          Val_Actual := Val;
6404          while not Comes_From_Source (Val_Actual)
6405            and then Nkind (Val_Actual) in N_Entity
6406            and then (Ekind (Val_Actual) = E_Enumeration_Literal
6407                       or else Is_Subprogram (Val_Actual)
6408                       or else Is_Generic_Subprogram (Val_Actual))
6409            and then Present (Alias (Val_Actual))
6410          loop
6411             Val_Actual := Alias (Val_Actual);
6412          end loop;
6413
6414          --  Renaming declarations for generic actuals do not come from source,
6415          --  and have a different name from that of the entity they rename, so
6416          --  there is no style check to perform here.
6417
6418          if Chars (Nod) = Chars (Val_Actual) then
6419             Style.Check_Identifier (Nod, Val_Actual);
6420          end if;
6421       end if;
6422
6423       Set_Entity (N, Val);
6424    end Set_Entity_With_Style_Check;
6425
6426    ------------------------
6427    -- Set_Name_Entity_Id --
6428    ------------------------
6429
6430    procedure Set_Name_Entity_Id (Id : Name_Id; Val : Entity_Id) is
6431    begin
6432       Set_Name_Table_Info (Id, Int (Val));
6433    end Set_Name_Entity_Id;
6434
6435    ---------------------
6436    -- Set_Next_Actual --
6437    ---------------------
6438
6439    procedure Set_Next_Actual (Ass1_Id : Node_Id; Ass2_Id : Node_Id) is
6440    begin
6441       if Nkind (Parent (Ass1_Id)) = N_Parameter_Association then
6442          Set_First_Named_Actual (Parent (Ass1_Id), Ass2_Id);
6443       end if;
6444    end Set_Next_Actual;
6445
6446    -----------------------
6447    -- Set_Public_Status --
6448    -----------------------
6449
6450    procedure Set_Public_Status (Id : Entity_Id) is
6451       S : constant Entity_Id := Current_Scope;
6452
6453    begin
6454       if S = Standard_Standard
6455         or else (Is_Public (S)
6456                   and then (Ekind (S) = E_Package
6457                              or else Is_Record_Type (S)
6458                              or else Ekind (S) = E_Void))
6459       then
6460          Set_Is_Public (Id);
6461
6462       --  The bounds of an entry family declaration can generate object
6463       --  declarations that are visible to the back-end, e.g. in the
6464       --  the declaration of a composite type that contains tasks.
6465
6466       elsif Is_Public (S)
6467         and then Is_Concurrent_Type (S)
6468         and then not Has_Completion (S)
6469         and then Nkind (Parent (Id)) = N_Object_Declaration
6470       then
6471          Set_Is_Public (Id);
6472       end if;
6473    end Set_Public_Status;
6474
6475    ----------------------------
6476    -- Set_Scope_Is_Transient --
6477    ----------------------------
6478
6479    procedure Set_Scope_Is_Transient (V : Boolean := True) is
6480    begin
6481       Scope_Stack.Table (Scope_Stack.Last).Is_Transient := V;
6482    end Set_Scope_Is_Transient;
6483
6484    -------------------
6485    -- Set_Size_Info --
6486    -------------------
6487
6488    procedure Set_Size_Info (T1, T2 : Entity_Id) is
6489    begin
6490       --  We copy Esize, but not RM_Size, since in general RM_Size is
6491       --  subtype specific and does not get inherited by all subtypes.
6492
6493       Set_Esize                     (T1, Esize                     (T2));
6494       Set_Has_Biased_Representation (T1, Has_Biased_Representation (T2));
6495
6496       if Is_Discrete_Or_Fixed_Point_Type (T1)
6497            and then
6498          Is_Discrete_Or_Fixed_Point_Type (T2)
6499       then
6500          Set_Is_Unsigned_Type       (T1, Is_Unsigned_Type          (T2));
6501       end if;
6502       Set_Alignment                 (T1, Alignment                 (T2));
6503    end Set_Size_Info;
6504
6505    --------------------
6506    -- Static_Integer --
6507    --------------------
6508
6509    function Static_Integer (N : Node_Id) return Uint is
6510    begin
6511       Analyze_And_Resolve (N, Any_Integer);
6512
6513       if N = Error
6514         or else Error_Posted (N)
6515         or else Etype (N) = Any_Type
6516       then
6517          return No_Uint;
6518       end if;
6519
6520       if Is_Static_Expression (N) then
6521          if not Raises_Constraint_Error (N) then
6522             return Expr_Value (N);
6523          else
6524             return No_Uint;
6525          end if;
6526
6527       elsif Etype (N) = Any_Type then
6528          return No_Uint;
6529
6530       else
6531          Flag_Non_Static_Expr
6532            ("static integer expression required here", N);
6533          return No_Uint;
6534       end if;
6535    end Static_Integer;
6536
6537    --------------------------
6538    -- Statically_Different --
6539    --------------------------
6540
6541    function Statically_Different (E1, E2 : Node_Id) return Boolean is
6542       R1 : constant Node_Id := Get_Referenced_Object (E1);
6543       R2 : constant Node_Id := Get_Referenced_Object (E2);
6544    begin
6545       return     Is_Entity_Name (R1)
6546         and then Is_Entity_Name (R2)
6547         and then Entity (R1) /= Entity (R2)
6548         and then not Is_Formal (Entity (R1))
6549         and then not Is_Formal (Entity (R2));
6550    end Statically_Different;
6551
6552    -----------------------------
6553    -- Subprogram_Access_Level --
6554    -----------------------------
6555
6556    function Subprogram_Access_Level (Subp : Entity_Id) return Uint is
6557    begin
6558       if Present (Alias (Subp)) then
6559          return Subprogram_Access_Level (Alias (Subp));
6560       else
6561          return Scope_Depth (Enclosing_Dynamic_Scope (Subp));
6562       end if;
6563    end Subprogram_Access_Level;
6564
6565    -----------------
6566    -- Trace_Scope --
6567    -----------------
6568
6569    procedure Trace_Scope (N : Node_Id; E : Entity_Id; Msg : String) is
6570    begin
6571       if Debug_Flag_W then
6572          for J in 0 .. Scope_Stack.Last loop
6573             Write_Str ("  ");
6574          end loop;
6575
6576          Write_Str (Msg);
6577          Write_Name (Chars (E));
6578          Write_Str ("   line ");
6579          Write_Int (Int (Get_Logical_Line_Number (Sloc (N))));
6580          Write_Eol;
6581       end if;
6582    end Trace_Scope;
6583
6584    -----------------------
6585    -- Transfer_Entities --
6586    -----------------------
6587
6588    procedure Transfer_Entities (From : Entity_Id; To : Entity_Id) is
6589       Ent : Entity_Id := First_Entity (From);
6590
6591    begin
6592       if No (Ent) then
6593          return;
6594       end if;
6595
6596       if (Last_Entity (To)) = Empty then
6597          Set_First_Entity (To, Ent);
6598       else
6599          Set_Next_Entity (Last_Entity (To), Ent);
6600       end if;
6601
6602       Set_Last_Entity (To, Last_Entity (From));
6603
6604       while Present (Ent) loop
6605          Set_Scope (Ent, To);
6606
6607          if not Is_Public (Ent) then
6608             Set_Public_Status (Ent);
6609
6610             if Is_Public (Ent)
6611               and then Ekind (Ent) = E_Record_Subtype
6612
6613             then
6614                --  The components of the propagated Itype must be public
6615                --  as well.
6616
6617                declare
6618                   Comp : Entity_Id;
6619
6620                begin
6621                   Comp := First_Entity (Ent);
6622                   while Present (Comp) loop
6623                      Set_Is_Public (Comp);
6624                      Next_Entity (Comp);
6625                   end loop;
6626                end;
6627             end if;
6628          end if;
6629
6630          Next_Entity (Ent);
6631       end loop;
6632
6633       Set_First_Entity (From, Empty);
6634       Set_Last_Entity (From, Empty);
6635    end Transfer_Entities;
6636
6637    -----------------------
6638    -- Type_Access_Level --
6639    -----------------------
6640
6641    function Type_Access_Level (Typ : Entity_Id) return Uint is
6642       Btyp : Entity_Id;
6643
6644    begin
6645       --  If the type is an anonymous access type we treat it as being
6646       --  declared at the library level to ensure that names such as
6647       --  X.all'access don't fail static accessibility checks.
6648
6649       --  Ada 2005 (AI-230): In case of anonymous access types that are
6650       --  component_definition or discriminants of a nonlimited type,
6651       --  the level is the same as that of the enclosing component type.
6652
6653       Btyp := Base_Type (Typ);
6654
6655       if Ekind (Btyp) in Access_Kind then
6656          if Ekind (Btyp) = E_Anonymous_Access_Type
6657            and then not Is_Local_Anonymous_Access (Typ) -- Ada 2005 (AI-230)
6658          then
6659             return Scope_Depth (Standard_Standard);
6660          end if;
6661
6662          Btyp := Root_Type (Btyp);
6663       end if;
6664
6665       return Scope_Depth (Enclosing_Dynamic_Scope (Btyp));
6666    end Type_Access_Level;
6667
6668    --------------------------
6669    -- Unit_Declaration_Node --
6670    --------------------------
6671
6672    function Unit_Declaration_Node (Unit_Id : Entity_Id) return Node_Id is
6673       N : Node_Id := Parent (Unit_Id);
6674
6675    begin
6676       --  Predefined operators do not have a full function declaration
6677
6678       if Ekind (Unit_Id) = E_Operator then
6679          return N;
6680       end if;
6681
6682       while Nkind (N) /= N_Abstract_Subprogram_Declaration
6683         and then Nkind (N) /= N_Formal_Package_Declaration
6684         and then Nkind (N) /= N_Function_Instantiation
6685         and then Nkind (N) /= N_Generic_Package_Declaration
6686         and then Nkind (N) /= N_Generic_Subprogram_Declaration
6687         and then Nkind (N) /= N_Package_Declaration
6688         and then Nkind (N) /= N_Package_Body
6689         and then Nkind (N) /= N_Package_Instantiation
6690         and then Nkind (N) /= N_Package_Renaming_Declaration
6691         and then Nkind (N) /= N_Procedure_Instantiation
6692         and then Nkind (N) /= N_Protected_Body
6693         and then Nkind (N) /= N_Subprogram_Declaration
6694         and then Nkind (N) /= N_Subprogram_Body
6695         and then Nkind (N) /= N_Subprogram_Body_Stub
6696         and then Nkind (N) /= N_Subprogram_Renaming_Declaration
6697         and then Nkind (N) /= N_Task_Body
6698         and then Nkind (N) /= N_Task_Type_Declaration
6699         and then Nkind (N) not in N_Formal_Subprogram_Declaration
6700         and then Nkind (N) not in N_Generic_Renaming_Declaration
6701       loop
6702          N := Parent (N);
6703          pragma Assert (Present (N));
6704       end loop;
6705
6706       return N;
6707    end Unit_Declaration_Node;
6708
6709    ------------------------------
6710    -- Universal_Interpretation --
6711    ------------------------------
6712
6713    function Universal_Interpretation (Opnd : Node_Id) return Entity_Id is
6714       Index : Interp_Index;
6715       It    : Interp;
6716
6717    begin
6718       --  The argument may be a formal parameter of an operator or subprogram
6719       --  with multiple interpretations, or else an expression for an actual.
6720
6721       if Nkind (Opnd) = N_Defining_Identifier
6722         or else not Is_Overloaded (Opnd)
6723       then
6724          if Etype (Opnd) = Universal_Integer
6725            or else Etype (Opnd) = Universal_Real
6726          then
6727             return Etype (Opnd);
6728          else
6729             return Empty;
6730          end if;
6731
6732       else
6733          Get_First_Interp (Opnd, Index, It);
6734          while Present (It.Typ) loop
6735             if It.Typ = Universal_Integer
6736               or else It.Typ = Universal_Real
6737             then
6738                return It.Typ;
6739             end if;
6740
6741             Get_Next_Interp (Index, It);
6742          end loop;
6743
6744          return Empty;
6745       end if;
6746    end Universal_Interpretation;
6747
6748    ----------------------
6749    -- Within_Init_Proc --
6750    ----------------------
6751
6752    function Within_Init_Proc return Boolean is
6753       S : Entity_Id;
6754
6755    begin
6756       S := Current_Scope;
6757       while not Is_Overloadable (S) loop
6758          if S = Standard_Standard then
6759             return False;
6760          else
6761             S := Scope (S);
6762          end if;
6763       end loop;
6764
6765       return Is_Init_Proc (S);
6766    end Within_Init_Proc;
6767
6768    ----------------
6769    -- Wrong_Type --
6770    ----------------
6771
6772    procedure Wrong_Type (Expr : Node_Id; Expected_Type : Entity_Id) is
6773       Found_Type : constant Entity_Id := First_Subtype (Etype (Expr));
6774       Expec_Type : constant Entity_Id := First_Subtype (Expected_Type);
6775
6776       function Has_One_Matching_Field return Boolean;
6777       --  Determines if Expec_Type is a record type with a single component or
6778       --  discriminant whose type matches the found type or is one dimensional
6779       --  array whose component type matches the found type.
6780
6781       ----------------------------
6782       -- Has_One_Matching_Field --
6783       ----------------------------
6784
6785       function Has_One_Matching_Field return Boolean is
6786          E : Entity_Id;
6787
6788       begin
6789          if Is_Array_Type (Expec_Type)
6790            and then Number_Dimensions (Expec_Type) = 1
6791            and then
6792              Covers (Etype (Component_Type (Expec_Type)), Found_Type)
6793          then
6794             return True;
6795
6796          elsif not Is_Record_Type (Expec_Type) then
6797             return False;
6798
6799          else
6800             E := First_Entity (Expec_Type);
6801             loop
6802                if No (E) then
6803                   return False;
6804
6805                elsif (Ekind (E) /= E_Discriminant
6806                        and then Ekind (E) /= E_Component)
6807                  or else (Chars (E) = Name_uTag
6808                            or else Chars (E) = Name_uParent)
6809                then
6810                   Next_Entity (E);
6811
6812                else
6813                   exit;
6814                end if;
6815             end loop;
6816
6817             if not Covers (Etype (E), Found_Type) then
6818                return False;
6819
6820             elsif Present (Next_Entity (E)) then
6821                return False;
6822
6823             else
6824                return True;
6825             end if;
6826          end if;
6827       end Has_One_Matching_Field;
6828
6829    --  Start of processing for Wrong_Type
6830
6831    begin
6832       --  Don't output message if either type is Any_Type, or if a message
6833       --  has already been posted for this node. We need to do the latter
6834       --  check explicitly (it is ordinarily done in Errout), because we
6835       --  are using ! to force the output of the error messages.
6836
6837       if Expec_Type = Any_Type
6838         or else Found_Type = Any_Type
6839         or else Error_Posted (Expr)
6840       then
6841          return;
6842
6843       --  In  an instance, there is an ongoing problem with completion of
6844       --  type derived from private types. Their structure is what Gigi
6845       --  expects, but the  Etype is the parent type rather than the
6846       --  derived private type itself. Do not flag error in this case. The
6847       --  private completion is an entity without a parent, like an Itype.
6848       --  Similarly, full and partial views may be incorrect in the instance.
6849       --  There is no simple way to insure that it is consistent ???
6850
6851       elsif In_Instance then
6852
6853          if Etype (Etype (Expr)) = Etype (Expected_Type)
6854            and then
6855              (Has_Private_Declaration (Expected_Type)
6856                or else Has_Private_Declaration (Etype (Expr)))
6857            and then No (Parent (Expected_Type))
6858          then
6859             return;
6860          end if;
6861       end if;
6862
6863       --  An interesting special check. If the expression is parenthesized
6864       --  and its type corresponds to the type of the sole component of the
6865       --  expected record type, or to the component type of the expected one
6866       --  dimensional array type, then assume we have a bad aggregate attempt.
6867
6868       if Nkind (Expr) in N_Subexpr
6869         and then Paren_Count (Expr) /= 0
6870         and then Has_One_Matching_Field
6871       then
6872          Error_Msg_N ("positional aggregate cannot have one component", Expr);
6873
6874       --  Another special check, if we are looking for a pool-specific access
6875       --  type and we found an E_Access_Attribute_Type, then we have the case
6876       --  of an Access attribute being used in a context which needs a pool-
6877       --  specific type, which is never allowed. The one extra check we make
6878       --  is that the expected designated type covers the Found_Type.
6879
6880       elsif Is_Access_Type (Expec_Type)
6881         and then Ekind (Found_Type) = E_Access_Attribute_Type
6882         and then Ekind (Base_Type (Expec_Type)) /= E_General_Access_Type
6883         and then Ekind (Base_Type (Expec_Type)) /= E_Anonymous_Access_Type
6884         and then Covers
6885           (Designated_Type (Expec_Type), Designated_Type (Found_Type))
6886       then
6887          Error_Msg_N ("result must be general access type!", Expr);
6888          Error_Msg_NE ("add ALL to }!", Expr, Expec_Type);
6889
6890       --  If the expected type is an anonymous access type, as for access
6891       --  parameters and discriminants, the error is on the designated types.
6892
6893       elsif Ekind (Expec_Type) = E_Anonymous_Access_Type then
6894          if Comes_From_Source (Expec_Type) then
6895             Error_Msg_NE ("expected}!", Expr, Expec_Type);
6896          else
6897             Error_Msg_NE
6898               ("expected an access type with designated}",
6899                  Expr, Designated_Type (Expec_Type));
6900          end if;
6901
6902          if Is_Access_Type (Found_Type)
6903            and then not Comes_From_Source (Found_Type)
6904          then
6905             Error_Msg_NE
6906               ("found an access type with designated}!",
6907                 Expr, Designated_Type (Found_Type));
6908          else
6909             if From_With_Type (Found_Type) then
6910                Error_Msg_NE ("found incomplete}!", Expr, Found_Type);
6911                Error_Msg_NE
6912                  ("\possibly missing with_clause on&", Expr,
6913                    Scope (Found_Type));
6914             else
6915                Error_Msg_NE ("found}!", Expr, Found_Type);
6916             end if;
6917          end if;
6918
6919       --  Normal case of one type found, some other type expected
6920
6921       else
6922          --  If the names of the two types are the same, see if some
6923          --  number of levels of qualification will help. Don't try
6924          --  more than three levels, and if we get to standard, it's
6925          --  no use (and probably represents an error in the compiler)
6926          --  Also do not bother with internal scope names.
6927
6928          declare
6929             Expec_Scope : Entity_Id;
6930             Found_Scope : Entity_Id;
6931
6932          begin
6933             Expec_Scope := Expec_Type;
6934             Found_Scope := Found_Type;
6935
6936             for Levels in Int range 0 .. 3 loop
6937                if Chars (Expec_Scope) /= Chars (Found_Scope) then
6938                   Error_Msg_Qual_Level := Levels;
6939                   exit;
6940                end if;
6941
6942                Expec_Scope := Scope (Expec_Scope);
6943                Found_Scope := Scope (Found_Scope);
6944
6945                exit when Expec_Scope = Standard_Standard
6946                  or else Found_Scope = Standard_Standard
6947                  or else not Comes_From_Source (Expec_Scope)
6948                  or else not Comes_From_Source (Found_Scope);
6949             end loop;
6950          end;
6951
6952          if Is_Record_Type (Expec_Type)
6953            and then Present (Corresponding_Remote_Type (Expec_Type))
6954          then
6955             Error_Msg_NE ("expected}!", Expr,
6956                           Corresponding_Remote_Type (Expec_Type));
6957          else
6958             Error_Msg_NE ("expected}!", Expr, Expec_Type);
6959          end if;
6960
6961          if Is_Entity_Name (Expr)
6962            and then Is_Package (Entity (Expr))
6963          then
6964             Error_Msg_N ("found package name!", Expr);
6965
6966          elsif Is_Entity_Name (Expr)
6967            and then
6968              (Ekind (Entity (Expr)) = E_Procedure
6969                 or else
6970               Ekind (Entity (Expr)) = E_Generic_Procedure)
6971          then
6972             if Ekind (Expec_Type) = E_Access_Subprogram_Type then
6973                Error_Msg_N
6974                  ("found procedure name, possibly missing Access attribute!",
6975                    Expr);
6976             else
6977                Error_Msg_N ("found procedure name instead of function!", Expr);
6978             end if;
6979
6980          elsif Nkind (Expr) = N_Function_Call
6981            and then Ekind (Expec_Type) = E_Access_Subprogram_Type
6982            and then Etype (Designated_Type (Expec_Type)) = Etype (Expr)
6983            and then No (Parameter_Associations (Expr))
6984          then
6985             Error_Msg_N
6986               ("found function name, possibly missing Access attribute!",
6987                Expr);
6988
6989          --  Catch common error: a prefix or infix operator which is not
6990          --  directly visible because the type isn't.
6991
6992          elsif Nkind (Expr) in N_Op
6993             and then Is_Overloaded (Expr)
6994             and then not Is_Immediately_Visible (Expec_Type)
6995             and then not Is_Potentially_Use_Visible (Expec_Type)
6996             and then not In_Use (Expec_Type)
6997             and then Has_Compatible_Type (Right_Opnd (Expr), Expec_Type)
6998          then
6999             Error_Msg_N
7000               ("operator of the type is not directly visible!", Expr);
7001
7002          elsif Ekind (Found_Type) = E_Void
7003            and then Present (Parent (Found_Type))
7004            and then Nkind (Parent (Found_Type)) = N_Full_Type_Declaration
7005          then
7006             Error_Msg_NE ("found premature usage of}!", Expr, Found_Type);
7007
7008          else
7009             Error_Msg_NE ("found}!", Expr, Found_Type);
7010          end if;
7011
7012          Error_Msg_Qual_Level := 0;
7013       end if;
7014    end Wrong_Type;
7015
7016 end Sem_Util;