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