OSDN Git Service

* sem_ch3.adb (Find_Type_Of_Subtype_Indic): If subtype indication
[pf3gnuchains/gcc-fork.git] / gcc / ada / sem_util.adb
1 -----------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                             S E M _ U T I L                              --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --                            $Revision$
10 --                                                                          --
11 --          Copyright (C) 1992-2001, Free Software Foundation, Inc.         --
12 --                                                                          --
13 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
14 -- terms of the  GNU General Public License as published  by the Free Soft- --
15 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
16 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
19 -- for  more details.  You should have  received  a copy of the GNU General --
20 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
21 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
22 -- MA 02111-1307, USA.                                                      --
23 --                                                                          --
24 -- GNAT was originally developed  by the GNAT team at  New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
26 --                                                                          --
27 ------------------------------------------------------------------------------
28
29 with Atree;    use Atree;
30 with Casing;   use Casing;
31 with Debug;    use Debug;
32 with Errout;   use Errout;
33 with Elists;   use Elists;
34 with Exp_Util; use Exp_Util;
35 with Freeze;   use Freeze;
36 with Lib;      use Lib;
37 with Lib.Xref; use Lib.Xref;
38 with Namet;    use Namet;
39 with Nlists;   use Nlists;
40 with Nmake;    use Nmake;
41 with Output;   use Output;
42 with Opt;      use Opt;
43 with Restrict; use Restrict;
44 with Scans;    use Scans;
45 with Scn;      use Scn;
46 with Sem;      use Sem;
47 with Sem_Ch8;  use Sem_Ch8;
48 with Sem_Eval; use Sem_Eval;
49 with Sem_Res;  use Sem_Res;
50 with Sem_Type; use Sem_Type;
51 with Sinfo;    use Sinfo;
52 with Sinput;   use Sinput;
53 with Snames;   use Snames;
54 with Stand;    use Stand;
55 with Style;
56 with Stringt;  use Stringt;
57 with Targparm; use Targparm;
58 with Tbuild;   use Tbuild;
59 with Ttypes;   use Ttypes;
60
61 package body Sem_Util is
62
63    -----------------------
64    -- Local Subprograms --
65    -----------------------
66
67    function Build_Component_Subtype
68      (C    : List_Id;
69       Loc  : Source_Ptr;
70       T    : Entity_Id)
71       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    --------------------------------
77    -- Add_Access_Type_To_Process --
78    --------------------------------
79
80    procedure Add_Access_Type_To_Process (E : Entity_Id; A : Entity_Id)
81    is
82       L : Elist_Id;
83    begin
84       Ensure_Freeze_Node (E);
85       L := Access_Types_To_Process (Freeze_Node (E));
86
87       if No (L) then
88          L := New_Elmt_List;
89          Set_Access_Types_To_Process (Freeze_Node (E), L);
90       end if;
91
92       Append_Elmt (A, L);
93    end Add_Access_Type_To_Process;
94
95    -----------------------
96    -- Alignment_In_Bits --
97    -----------------------
98
99    function Alignment_In_Bits (E : Entity_Id) return Uint is
100    begin
101       return Alignment (E) * System_Storage_Unit;
102    end Alignment_In_Bits;
103
104    -----------------------------------------
105    -- Apply_Compile_Time_Constraint_Error --
106    -----------------------------------------
107
108    procedure Apply_Compile_Time_Constraint_Error
109      (N   : Node_Id;
110       Msg : String;
111       Ent : Entity_Id  := Empty;
112       Typ : Entity_Id  := Empty;
113       Loc : Source_Ptr := No_Location;
114       Rep : Boolean    := True)
115    is
116       Stat : constant Boolean := Is_Static_Expression (N);
117       Rtyp : Entity_Id;
118
119    begin
120       if No (Typ) then
121          Rtyp := Etype (N);
122       else
123          Rtyp := Typ;
124       end if;
125
126       if No (Compile_Time_Constraint_Error (N, Msg, Ent, Loc))
127         or else not Rep
128       then
129          return;
130       end if;
131
132       --  Now we replace the node by an N_Raise_Constraint_Error node
133       --  This does not need reanalyzing, so set it as analyzed now.
134
135       Rewrite (N, Make_Raise_Constraint_Error (Sloc (N)));
136       Set_Analyzed (N, True);
137       Set_Etype (N, Rtyp);
138       Set_Raises_Constraint_Error (N);
139
140       --  If the original expression was marked as static, the result is
141       --  still marked as static, but the Raises_Constraint_Error flag is
142       --  always set so that further static evaluation is not attempted.
143
144       if Stat then
145          Set_Is_Static_Expression (N);
146       end if;
147    end Apply_Compile_Time_Constraint_Error;
148
149    --------------------------
150    -- Build_Actual_Subtype --
151    --------------------------
152
153    function Build_Actual_Subtype
154      (T    : Entity_Id;
155       N    : Node_Or_Entity_Id)
156       return Node_Id
157    is
158       Obj : Node_Id;
159
160       Loc         : constant Source_Ptr := Sloc (N);
161       Constraints : List_Id;
162       Decl        : Node_Id;
163       Discr       : Entity_Id;
164       Hi          : Node_Id;
165       Lo          : Node_Id;
166       Subt        : Entity_Id;
167       Disc_Type   : Entity_Id;
168
169    begin
170       if Nkind (N) = N_Defining_Identifier then
171          Obj := New_Reference_To (N, Loc);
172       else
173          Obj := N;
174       end if;
175
176       if Is_Array_Type (T) then
177          Constraints := New_List;
178
179          for J in 1 .. Number_Dimensions (T) loop
180
181             --  Build an array subtype declaration with the nominal
182             --  subtype and the bounds of the actual. Add the declaration
183             --  in front of the local declarations for the subprogram,for
184             --  analysis before any reference to the formal in the body.
185
186             Lo :=
187               Make_Attribute_Reference (Loc,
188                 Prefix         => Duplicate_Subexpr (Obj, Name_Req => True),
189                 Attribute_Name => Name_First,
190                 Expressions    => New_List (
191                   Make_Integer_Literal (Loc, J)));
192
193             Hi :=
194               Make_Attribute_Reference (Loc,
195                 Prefix         => Duplicate_Subexpr (Obj, Name_Req => True),
196                 Attribute_Name => Name_Last,
197                 Expressions    => New_List (
198                   Make_Integer_Literal (Loc, J)));
199
200             Append (Make_Range (Loc, Lo, Hi), Constraints);
201          end loop;
202
203       --  If the type has unknown discriminants there is no constrained
204       --  subtype to build.
205
206       elsif Has_Unknown_Discriminants (T) then
207          return T;
208
209       else
210          Constraints := New_List;
211
212          if Is_Private_Type (T) and then No (Full_View (T)) then
213
214             --  Type is a generic derived type. Inherit discriminants from
215             --  Parent type.
216
217             Disc_Type := Etype (Base_Type (T));
218          else
219             Disc_Type := T;
220          end if;
221
222          Discr := First_Discriminant (Disc_Type);
223
224          while Present (Discr) loop
225             Append_To (Constraints,
226               Make_Selected_Component (Loc,
227                 Prefix => Duplicate_Subexpr (Obj),
228                 Selector_Name => New_Occurrence_Of (Discr, Loc)));
229             Next_Discriminant (Discr);
230          end loop;
231       end if;
232
233       Subt :=
234         Make_Defining_Identifier (Loc,
235           Chars => New_Internal_Name ('S'));
236       Set_Is_Internal (Subt);
237
238       Decl :=
239         Make_Subtype_Declaration (Loc,
240           Defining_Identifier => Subt,
241           Subtype_Indication =>
242             Make_Subtype_Indication (Loc,
243               Subtype_Mark => New_Reference_To (T,  Loc),
244               Constraint  =>
245                 Make_Index_Or_Discriminant_Constraint (Loc,
246                   Constraints => Constraints)));
247
248       Mark_Rewrite_Insertion (Decl);
249       return Decl;
250    end Build_Actual_Subtype;
251
252    ---------------------------------------
253    -- Build_Actual_Subtype_Of_Component --
254    ---------------------------------------
255
256    function Build_Actual_Subtype_Of_Component
257      (T    : Entity_Id;
258       N    : Node_Id)
259       return Node_Id
260    is
261       Loc       : constant Source_Ptr := Sloc (N);
262       P         : constant Node_Id    := Prefix (N);
263       D         : Elmt_Id;
264       Id        : Node_Id;
265       Indx_Type : Entity_Id;
266
267       Deaccessed_T : Entity_Id;
268       --  This is either a copy of T, or if T is an access type, then it is
269       --  the directly designated type of this access type.
270
271       function Build_Actual_Array_Constraint return List_Id;
272       --  If one or more of the bounds of the component depends on
273       --  discriminants, build  actual constraint using the discriminants
274       --  of the prefix.
275
276       function Build_Actual_Record_Constraint return List_Id;
277       --  Similar to previous one, for discriminated components constrained
278       --  by the discriminant of the enclosing object.
279
280       -----------------------------------
281       -- Build_Actual_Array_Constraint --
282       -----------------------------------
283
284       function Build_Actual_Array_Constraint return List_Id is
285          Constraints : List_Id := New_List;
286          Indx        : Node_Id;
287          Hi          : Node_Id;
288          Lo          : Node_Id;
289          Old_Hi      : Node_Id;
290          Old_Lo      : Node_Id;
291
292       begin
293          Indx := First_Index (Deaccessed_T);
294          while Present (Indx) loop
295             Old_Lo := Type_Low_Bound  (Etype (Indx));
296             Old_Hi := Type_High_Bound (Etype (Indx));
297
298             if Denotes_Discriminant (Old_Lo) then
299                Lo :=
300                  Make_Selected_Component (Loc,
301                    Prefix => New_Copy_Tree (P),
302                    Selector_Name => New_Occurrence_Of (Entity (Old_Lo), Loc));
303
304             else
305                Lo := New_Copy_Tree (Old_Lo);
306
307                --  The new bound will be reanalyzed in the enclosing
308                --  declaration. For literal bounds that come from a type
309                --  declaration, the type of the context must be imposed, so
310                --  insure that analysis will take place. For non-universal
311                --  types this is not strictly necessary.
312
313                Set_Analyzed (Lo, False);
314             end if;
315
316             if Denotes_Discriminant (Old_Hi) then
317                Hi :=
318                  Make_Selected_Component (Loc,
319                    Prefix => New_Copy_Tree (P),
320                    Selector_Name => New_Occurrence_Of (Entity (Old_Hi), Loc));
321
322             else
323                Hi := New_Copy_Tree (Old_Hi);
324                Set_Analyzed (Hi, False);
325             end if;
326
327             Append (Make_Range (Loc, Lo, Hi), Constraints);
328             Next_Index (Indx);
329          end loop;
330
331          return Constraints;
332       end Build_Actual_Array_Constraint;
333
334       ------------------------------------
335       -- Build_Actual_Record_Constraint --
336       ------------------------------------
337
338       function Build_Actual_Record_Constraint return List_Id is
339          Constraints : List_Id := New_List;
340          D           : Elmt_Id;
341          D_Val       : Node_Id;
342
343       begin
344          D := First_Elmt (Discriminant_Constraint (Deaccessed_T));
345          while Present (D) loop
346
347             if Denotes_Discriminant (Node (D)) then
348                D_Val :=  Make_Selected_Component (Loc,
349                  Prefix => New_Copy_Tree (P),
350                 Selector_Name => New_Occurrence_Of (Entity (Node (D)), Loc));
351
352             else
353                D_Val := New_Copy_Tree (Node (D));
354             end if;
355
356             Append (D_Val, Constraints);
357             Next_Elmt (D);
358          end loop;
359
360          return Constraints;
361       end Build_Actual_Record_Constraint;
362
363    --  Start of processing for Build_Actual_Subtype_Of_Component
364
365    begin
366       if Nkind (N) = N_Explicit_Dereference then
367          if Is_Composite_Type (T)
368            and then not Is_Constrained (T)
369            and then not (Is_Class_Wide_Type (T)
370                           and then Is_Constrained (Root_Type (T)))
371            and then not Has_Unknown_Discriminants (T)
372          then
373             --  If the type of the dereference is already constrained, it
374             --  is an actual subtype.
375
376             if Is_Array_Type (Etype (N))
377               and then Is_Constrained (Etype (N))
378             then
379                return Empty;
380             else
381                Remove_Side_Effects (P);
382                return Build_Actual_Subtype (T, N);
383             end if;
384          else
385             return Empty;
386          end if;
387       end if;
388
389       if Ekind (T) = E_Access_Subtype then
390          Deaccessed_T := Designated_Type (T);
391       else
392          Deaccessed_T := T;
393       end if;
394
395       if Ekind (Deaccessed_T) = E_Array_Subtype then
396
397          Id := First_Index (Deaccessed_T);
398          Indx_Type := Underlying_Type (Etype (Id));
399
400          while Present (Id) loop
401
402             if Denotes_Discriminant (Type_Low_Bound  (Indx_Type)) or else
403                Denotes_Discriminant (Type_High_Bound (Indx_Type))
404             then
405                Remove_Side_Effects (P);
406                return
407                  Build_Component_Subtype (
408                    Build_Actual_Array_Constraint, Loc, Base_Type (T));
409             end if;
410
411             Next_Index (Id);
412          end loop;
413
414       elsif Is_Composite_Type (Deaccessed_T)
415         and then Has_Discriminants (Deaccessed_T)
416         and then not Has_Unknown_Discriminants (Deaccessed_T)
417       then
418          D := First_Elmt (Discriminant_Constraint (Deaccessed_T));
419          while Present (D) loop
420
421             if Denotes_Discriminant (Node (D)) then
422                Remove_Side_Effects (P);
423                return
424                  Build_Component_Subtype (
425                    Build_Actual_Record_Constraint, Loc, Base_Type (T));
426             end if;
427
428             Next_Elmt (D);
429          end loop;
430       end if;
431
432       --  If none of the above, the actual and nominal subtypes are the same.
433
434       return Empty;
435
436    end Build_Actual_Subtype_Of_Component;
437
438    -----------------------------
439    -- Build_Component_Subtype --
440    -----------------------------
441
442    function Build_Component_Subtype
443      (C    : List_Id;
444       Loc  : Source_Ptr;
445       T    : Entity_Id)
446       return Node_Id
447    is
448       Subt : Entity_Id;
449       Decl : Node_Id;
450
451    begin
452       Subt :=
453         Make_Defining_Identifier (Loc,
454           Chars => New_Internal_Name ('S'));
455       Set_Is_Internal (Subt);
456
457       Decl :=
458         Make_Subtype_Declaration (Loc,
459           Defining_Identifier => Subt,
460           Subtype_Indication =>
461             Make_Subtype_Indication (Loc,
462               Subtype_Mark => New_Reference_To (Base_Type (T),  Loc),
463               Constraint  =>
464                 Make_Index_Or_Discriminant_Constraint (Loc,
465                   Constraints => C)));
466
467       Mark_Rewrite_Insertion (Decl);
468       return Decl;
469    end Build_Component_Subtype;
470
471    --------------------------------------------
472    -- Build_Discriminal_Subtype_Of_Component --
473    --------------------------------------------
474
475    function Build_Discriminal_Subtype_Of_Component
476      (T    : Entity_Id)
477       return Node_Id
478    is
479       Loc : constant Source_Ptr := Sloc (T);
480       D   : Elmt_Id;
481       Id  : Node_Id;
482
483       function Build_Discriminal_Array_Constraint return List_Id;
484       --  If one or more of the bounds of the component depends on
485       --  discriminants, build  actual constraint using the discriminants
486       --  of the prefix.
487
488       function Build_Discriminal_Record_Constraint return List_Id;
489       --  Similar to previous one, for discriminated components constrained
490       --  by the discriminant of the enclosing object.
491
492       ----------------------------------------
493       -- Build_Discriminal_Array_Constraint --
494       ----------------------------------------
495
496       function Build_Discriminal_Array_Constraint return List_Id is
497          Constraints : List_Id := New_List;
498          Indx        : Node_Id;
499          Hi          : Node_Id;
500          Lo          : Node_Id;
501          Old_Hi      : Node_Id;
502          Old_Lo      : Node_Id;
503
504       begin
505          Indx := First_Index (T);
506          while Present (Indx) loop
507             Old_Lo := Type_Low_Bound  (Etype (Indx));
508             Old_Hi := Type_High_Bound (Etype (Indx));
509
510             if Denotes_Discriminant (Old_Lo) then
511                Lo := New_Occurrence_Of (Discriminal (Entity (Old_Lo)), Loc);
512
513             else
514                Lo := New_Copy_Tree (Old_Lo);
515             end if;
516
517             if Denotes_Discriminant (Old_Hi) then
518                Hi := New_Occurrence_Of (Discriminal (Entity (Old_Hi)), Loc);
519
520             else
521                Hi := New_Copy_Tree (Old_Hi);
522             end if;
523
524             Append (Make_Range (Loc, Lo, Hi), Constraints);
525             Next_Index (Indx);
526          end loop;
527
528          return Constraints;
529       end Build_Discriminal_Array_Constraint;
530
531       -----------------------------------------
532       -- Build_Discriminal_Record_Constraint --
533       -----------------------------------------
534
535       function Build_Discriminal_Record_Constraint return List_Id is
536          Constraints     : List_Id := New_List;
537          D     : Elmt_Id;
538          D_Val : Node_Id;
539
540       begin
541          D := First_Elmt (Discriminant_Constraint (T));
542          while Present (D) loop
543
544             if Denotes_Discriminant (Node (D)) then
545                D_Val :=
546                  New_Occurrence_Of (Discriminal (Entity (Node (D))), Loc);
547
548             else
549                D_Val := New_Copy_Tree (Node (D));
550             end if;
551
552             Append (D_Val, Constraints);
553             Next_Elmt (D);
554          end loop;
555
556          return Constraints;
557       end Build_Discriminal_Record_Constraint;
558
559    --  Start of processing for Build_Discriminal_Subtype_Of_Component
560
561    begin
562       if Ekind (T) = E_Array_Subtype then
563
564          Id := First_Index (T);
565
566          while Present (Id) loop
567
568             if Denotes_Discriminant (Type_Low_Bound  (Etype (Id))) or else
569                Denotes_Discriminant (Type_High_Bound (Etype (Id)))
570             then
571                return Build_Component_Subtype
572                  (Build_Discriminal_Array_Constraint, Loc, T);
573             end if;
574
575             Next_Index (Id);
576          end loop;
577
578       elsif Ekind (T) = E_Record_Subtype
579         and then Has_Discriminants (T)
580         and then not Has_Unknown_Discriminants (T)
581       then
582          D := First_Elmt (Discriminant_Constraint (T));
583          while Present (D) loop
584
585             if Denotes_Discriminant (Node (D)) then
586                return Build_Component_Subtype
587                  (Build_Discriminal_Record_Constraint, Loc, T);
588             end if;
589
590             Next_Elmt (D);
591          end loop;
592       end if;
593
594       --  If none of the above, the actual and nominal subtypes are the same.
595
596       return Empty;
597
598    end Build_Discriminal_Subtype_Of_Component;
599
600    ------------------------------
601    -- Build_Elaboration_Entity --
602    ------------------------------
603
604    procedure Build_Elaboration_Entity (N : Node_Id; Spec_Id : Entity_Id) is
605       Loc       : constant Source_Ptr       := Sloc (N);
606       Unum      : constant Unit_Number_Type := Get_Source_Unit (Loc);
607       Decl      : Node_Id;
608       P         : Natural;
609       Elab_Ent  : Entity_Id;
610
611    begin
612       --  Ignore if already constructed
613
614       if Present (Elaboration_Entity (Spec_Id)) then
615          return;
616       end if;
617
618       --  Construct name of elaboration entity as xxx_E, where xxx
619       --  is the unit name with dots replaced by double underscore.
620       --  We have to manually construct this name, since it will
621       --  be elaborated in the outer scope, and thus will not have
622       --  the unit name automatically prepended.
623
624       Get_Name_String (Unit_Name (Unum));
625
626       --  Replace the %s by _E
627
628       Name_Buffer (Name_Len - 1 .. Name_Len) := "_E";
629
630       --  Replace dots by double underscore
631
632       P := 2;
633       while P < Name_Len - 2 loop
634          if Name_Buffer (P) = '.' then
635             Name_Buffer (P + 2 .. Name_Len + 1) :=
636               Name_Buffer (P + 1 .. Name_Len);
637             Name_Len := Name_Len + 1;
638             Name_Buffer (P) := '_';
639             Name_Buffer (P + 1) := '_';
640             P := P + 3;
641          else
642             P := P + 1;
643          end if;
644       end loop;
645
646       --  Create elaboration flag
647
648       Elab_Ent :=
649         Make_Defining_Identifier (Loc, Chars => Name_Find);
650       Set_Elaboration_Entity (Spec_Id, Elab_Ent);
651
652       if No (Declarations (Aux_Decls_Node (N))) then
653          Set_Declarations (Aux_Decls_Node (N), New_List);
654       end if;
655
656       Decl :=
657          Make_Object_Declaration (Loc,
658            Defining_Identifier => Elab_Ent,
659            Object_Definition   =>
660              New_Occurrence_Of (Standard_Boolean, Loc),
661            Expression          =>
662              New_Occurrence_Of (Standard_False, Loc));
663
664       Append_To (Declarations (Aux_Decls_Node (N)), Decl);
665       Analyze (Decl);
666
667       --  Reset True_Constant indication, since we will indeed
668       --  assign a value to the variable in the binder main.
669
670       Set_Is_True_Constant (Elab_Ent, False);
671
672       --  We do not want any further qualification of the name (if we did
673       --  not do this, we would pick up the name of the generic package
674       --  in the case of a library level generic instantiation).
675
676       Set_Has_Qualified_Name       (Elab_Ent);
677       Set_Has_Fully_Qualified_Name (Elab_Ent);
678    end Build_Elaboration_Entity;
679
680    --------------------------
681    -- Check_Fully_Declared --
682    --------------------------
683
684    procedure Check_Fully_Declared (T : Entity_Id; N : Node_Id) is
685    begin
686       if Ekind (T) = E_Incomplete_Type then
687          Error_Msg_NE
688            ("premature usage of incomplete}", N, First_Subtype (T));
689
690       elsif Has_Private_Component (T)
691         and then not Is_Generic_Type (Root_Type (T))
692         and then not In_Default_Expression
693       then
694          Error_Msg_NE
695            ("premature usage of incomplete}", N, First_Subtype (T));
696       end if;
697    end Check_Fully_Declared;
698
699    ------------------------------------------
700    -- Check_Potentially_Blocking_Operation --
701    ------------------------------------------
702
703    procedure Check_Potentially_Blocking_Operation (N : Node_Id) is
704       S   : Entity_Id;
705       Loc : constant Source_Ptr := Sloc (N);
706
707    begin
708       --  N is one of the potentially blocking operations listed in
709       --  9.5.1 (8). When using the Ravenscar profile, raise Program_Error
710       --  before N if the context is a protected action. Otherwise, only issue
711       --  a warning, since some users are relying on blocking operations
712       --  inside protected objects.
713       --  Indirect blocking through a subprogram call
714       --  cannot be diagnosed statically without interprocedural analysis,
715       --  so we do not attempt to do it here.
716
717       S := Scope (Current_Scope);
718
719       while Present (S) and then S /= Standard_Standard loop
720          if Is_Protected_Type (S) then
721             if Restricted_Profile then
722                Insert_Before (N,
723                   Make_Raise_Statement (Loc,
724                    Name => New_Occurrence_Of (Standard_Program_Error, Loc)));
725                Error_Msg_N ("potentially blocking operation, " &
726                  " Program Error will be raised at run time?", N);
727
728             else
729                Error_Msg_N
730                  ("potentially blocking operation in protected operation?", N);
731             end if;
732
733             return;
734          end if;
735
736          S := Scope (S);
737       end loop;
738    end Check_Potentially_Blocking_Operation;
739
740    ---------------
741    -- Check_VMS --
742    ---------------
743
744    procedure Check_VMS (Construct : Node_Id) is
745    begin
746       if not OpenVMS_On_Target then
747          Error_Msg_N
748            ("this construct is allowed only in Open'V'M'S", Construct);
749       end if;
750    end Check_VMS;
751
752    ----------------------------------
753    -- Collect_Primitive_Operations --
754    ----------------------------------
755
756    function Collect_Primitive_Operations (T : Entity_Id) return Elist_Id is
757       B_Type         : constant Entity_Id := Base_Type (T);
758       B_Decl         : constant Node_Id   := Original_Node (Parent (B_Type));
759       B_Scope        : Entity_Id          := Scope (B_Type);
760       Op_List        : Elist_Id;
761       Formal         : Entity_Id;
762       Is_Prim        : Boolean;
763       Formal_Derived : Boolean := False;
764       Id             : Entity_Id;
765
766    begin
767       --  For tagged types, the primitive operations are collected as they
768       --  are declared, and held in an explicit list which is simply returned.
769
770       if Is_Tagged_Type (B_Type) then
771          return Primitive_Operations (B_Type);
772
773       --  An untagged generic type that is a derived type inherits the
774       --  primitive operations of its parent type. Other formal types only
775       --  have predefined operators, which are not explicitly represented.
776
777       elsif Is_Generic_Type (B_Type) then
778          if Nkind (B_Decl) = N_Formal_Type_Declaration
779            and then Nkind (Formal_Type_Definition (B_Decl))
780              = N_Formal_Derived_Type_Definition
781          then
782             Formal_Derived := True;
783          else
784             return New_Elmt_List;
785          end if;
786       end if;
787
788       Op_List := New_Elmt_List;
789
790       if B_Scope = Standard_Standard then
791          if B_Type = Standard_String then
792             Append_Elmt (Standard_Op_Concat, Op_List);
793
794          elsif B_Type = Standard_Wide_String then
795             Append_Elmt (Standard_Op_Concatw, Op_List);
796
797          else
798             null;
799          end if;
800
801       elsif (Is_Package (B_Scope)
802                and then Nkind (
803                  Parent (Declaration_Node (First_Subtype (T))))
804                    /=  N_Package_Body)
805
806         or else Is_Derived_Type (B_Type)
807       then
808          --  The primitive operations appear after the base type, except
809          --  if the derivation happens within the private part of B_Scope
810          --  and the type is a private type, in which case both the type
811          --  and some primitive operations may appear before the base
812          --  type, and the list of candidates starts after the type.
813
814          if In_Open_Scopes (B_Scope)
815            and then Scope (T) = B_Scope
816            and then In_Private_Part (B_Scope)
817          then
818             Id := Next_Entity (T);
819          else
820             Id := Next_Entity (B_Type);
821          end if;
822
823          while Present (Id) loop
824
825             --  Note that generic formal subprograms are not
826             --  considered to be primitive operations and thus
827             --  are never inherited.
828
829             if Is_Overloadable (Id)
830               and then Nkind (Parent (Parent (Id)))
831                          /= N_Formal_Subprogram_Declaration
832             then
833                Is_Prim := False;
834
835                if Base_Type (Etype (Id)) = B_Type then
836                   Is_Prim := True;
837                else
838                   Formal := First_Formal (Id);
839                   while Present (Formal) loop
840                      if Base_Type (Etype (Formal)) = B_Type then
841                         Is_Prim := True;
842                         exit;
843
844                      elsif Ekind (Etype (Formal)) = E_Anonymous_Access_Type
845                        and then Base_Type
846                          (Designated_Type (Etype (Formal))) = B_Type
847                      then
848                         Is_Prim := True;
849                         exit;
850                      end if;
851
852                      Next_Formal (Formal);
853                   end loop;
854                end if;
855
856                --  For a formal derived type, the only primitives are the
857                --  ones inherited from the parent type. Operations appearing
858                --  in the package declaration are not primitive for it.
859
860                if Is_Prim
861                  and then (not Formal_Derived
862                             or else Present (Alias (Id)))
863                then
864                   Append_Elmt (Id, Op_List);
865                end if;
866             end if;
867
868             Next_Entity (Id);
869
870             --  For a type declared in System, some of its operations
871             --  may appear in  the target-specific extension to System.
872
873             if No (Id)
874               and then Chars (B_Scope) = Name_System
875               and then Scope (B_Scope) = Standard_Standard
876               and then Present_System_Aux
877             then
878                B_Scope := System_Aux_Id;
879                Id := First_Entity (System_Aux_Id);
880             end if;
881
882          end loop;
883
884       end if;
885
886       return Op_List;
887    end Collect_Primitive_Operations;
888
889    -----------------------------------
890    -- Compile_Time_Constraint_Error --
891    -----------------------------------
892
893    function Compile_Time_Constraint_Error
894      (N    : Node_Id;
895       Msg  : String;
896       Ent  : Entity_Id  := Empty;
897       Loc  : Source_Ptr := No_Location)
898       return Node_Id
899    is
900       Msgc : String (1 .. Msg'Length + 2);
901       Msgl : Natural;
902       Warn : Boolean;
903       P    : Node_Id;
904       Msgs : Boolean;
905
906    begin
907       --  A static constraint error in an instance body is not a fatal error.
908       --  we choose to inhibit the message altogether, because there is no
909       --  obvious node (for now) on which to post it. On the other hand the
910       --  offending node must be replaced with a constraint_error in any case.
911
912       --  No messages are generated if we already posted an error on this node
913
914       if not Error_Posted (N) then
915
916          --  Make all such messages unconditional
917
918          Msgc (1 .. Msg'Length) := Msg;
919          Msgc (Msg'Length + 1) := '!';
920          Msgl := Msg'Length + 1;
921
922          --  Message is a warning, even in Ada 95 case
923
924          if Msg (Msg'Length) = '?' then
925             Warn := True;
926
927          --  In Ada 83, all messages are warnings. In the private part and
928          --  the body of an instance, constraint_checks are only warnings.
929
930          elsif Ada_83 and then Comes_From_Source (N) then
931
932             Msgl := Msgl + 1;
933             Msgc (Msgl) := '?';
934             Warn := True;
935
936          elsif In_Instance_Not_Visible then
937
938             Msgl := Msgl + 1;
939             Msgc (Msgl) := '?';
940             Warn := True;
941             Warn_On_Instance := True;
942
943          --  Otherwise we have a real error message (Ada 95 static case)
944
945          else
946             Warn := False;
947          end if;
948
949          --  Should we generate a warning? The answer is not quite yes. The
950          --  very annoying exception occurs in the case of a short circuit
951          --  operator where the left operand is static and decisive. Climb
952          --  parents to see if that is the case we have here.
953
954          Msgs := True;
955          P := N;
956
957          loop
958             P := Parent (P);
959
960             if (Nkind (P) = N_And_Then
961                 and then Compile_Time_Known_Value (Left_Opnd (P))
962                 and then Is_False (Expr_Value (Left_Opnd (P))))
963               or else (Nkind (P) = N_Or_Else
964                 and then Compile_Time_Known_Value (Left_Opnd (P))
965                 and then Is_True (Expr_Value (Left_Opnd (P))))
966             then
967                Msgs := False;
968                exit;
969
970             elsif Nkind (P) = N_Component_Association
971               and then Nkind (Parent (P)) = N_Aggregate
972             then
973                null;  --   Keep going.
974
975             else
976                exit when Nkind (P) not in N_Subexpr;
977             end if;
978          end loop;
979
980          if Msgs then
981             if Present (Ent) then
982                Error_Msg_NE (Msgc (1 .. Msgl), N, Ent);
983             else
984                Error_Msg_NE (Msgc (1 .. Msgl), N, Etype (N));
985             end if;
986
987             if Warn then
988                if Inside_Init_Proc then
989                   Error_Msg_NE
990                     ("\& will be raised for objects of this type!?",
991                      N, Standard_Constraint_Error);
992                else
993                   Error_Msg_NE
994                     ("\& will be raised at run time!?",
995                      N, Standard_Constraint_Error);
996                end if;
997             else
998                Error_Msg_NE
999                  ("\static expression raises&!",
1000                   N, Standard_Constraint_Error);
1001             end if;
1002          end if;
1003       end if;
1004
1005       return N;
1006    end Compile_Time_Constraint_Error;
1007
1008    -----------------------
1009    -- Conditional_Delay --
1010    -----------------------
1011
1012    procedure Conditional_Delay (New_Ent, Old_Ent : Entity_Id) is
1013    begin
1014       if Has_Delayed_Freeze (Old_Ent) and then not Is_Frozen (Old_Ent) then
1015          Set_Has_Delayed_Freeze (New_Ent);
1016       end if;
1017    end Conditional_Delay;
1018
1019    --------------------
1020    -- Current_Entity --
1021    --------------------
1022
1023    --  The currently visible definition for a given identifier is the
1024    --  one most chained at the start of the visibility chain, i.e. the
1025    --  one that is referenced by the Node_Id value of the name of the
1026    --  given identifier.
1027
1028    function Current_Entity (N : Node_Id) return Entity_Id is
1029    begin
1030       return Get_Name_Entity_Id (Chars (N));
1031    end Current_Entity;
1032
1033    -----------------------------
1034    -- Current_Entity_In_Scope --
1035    -----------------------------
1036
1037    function Current_Entity_In_Scope (N : Node_Id) return Entity_Id is
1038       E  : Entity_Id;
1039       CS : constant Entity_Id := Current_Scope;
1040
1041       Transient_Case : constant Boolean := Scope_Is_Transient;
1042
1043    begin
1044       E := Get_Name_Entity_Id (Chars (N));
1045
1046       while Present (E)
1047         and then Scope (E) /= CS
1048         and then (not Transient_Case or else Scope (E) /= Scope (CS))
1049       loop
1050          E := Homonym (E);
1051       end loop;
1052
1053       return E;
1054    end Current_Entity_In_Scope;
1055
1056    -------------------
1057    -- Current_Scope --
1058    -------------------
1059
1060    function Current_Scope return Entity_Id is
1061    begin
1062       if Scope_Stack.Last = -1 then
1063          return Standard_Standard;
1064       else
1065          declare
1066             C : constant Entity_Id :=
1067                   Scope_Stack.Table (Scope_Stack.Last).Entity;
1068          begin
1069             if Present (C) then
1070                return C;
1071             else
1072                return Standard_Standard;
1073             end if;
1074          end;
1075       end if;
1076    end Current_Scope;
1077
1078    ------------------------
1079    -- Current_Subprogram --
1080    ------------------------
1081
1082    function Current_Subprogram return Entity_Id is
1083       Scop : constant Entity_Id := Current_Scope;
1084
1085    begin
1086       if Ekind (Scop) = E_Function
1087            or else
1088          Ekind (Scop) = E_Procedure
1089            or else
1090          Ekind (Scop) = E_Generic_Function
1091            or else
1092          Ekind (Scop) = E_Generic_Procedure
1093       then
1094          return Scop;
1095
1096       else
1097          return Enclosing_Subprogram (Scop);
1098       end if;
1099    end Current_Subprogram;
1100
1101    ---------------------
1102    -- Defining_Entity --
1103    ---------------------
1104
1105    function Defining_Entity (N : Node_Id) return Entity_Id is
1106       K   : constant Node_Kind := Nkind (N);
1107       Err : Entity_Id := Empty;
1108
1109    begin
1110       case K is
1111          when
1112            N_Subprogram_Declaration                 |
1113            N_Abstract_Subprogram_Declaration        |
1114            N_Subprogram_Body                        |
1115            N_Package_Declaration                    |
1116            N_Subprogram_Renaming_Declaration        |
1117            N_Subprogram_Body_Stub                   |
1118            N_Generic_Subprogram_Declaration         |
1119            N_Generic_Package_Declaration            |
1120            N_Formal_Subprogram_Declaration
1121          =>
1122             return Defining_Entity (Specification (N));
1123
1124          when
1125            N_Component_Declaration                  |
1126            N_Defining_Program_Unit_Name             |
1127            N_Discriminant_Specification             |
1128            N_Entry_Body                             |
1129            N_Entry_Declaration                      |
1130            N_Entry_Index_Specification              |
1131            N_Exception_Declaration                  |
1132            N_Exception_Renaming_Declaration         |
1133            N_Formal_Object_Declaration              |
1134            N_Formal_Package_Declaration             |
1135            N_Formal_Type_Declaration                |
1136            N_Full_Type_Declaration                  |
1137            N_Implicit_Label_Declaration             |
1138            N_Incomplete_Type_Declaration            |
1139            N_Loop_Parameter_Specification           |
1140            N_Number_Declaration                     |
1141            N_Object_Declaration                     |
1142            N_Object_Renaming_Declaration            |
1143            N_Package_Body_Stub                      |
1144            N_Parameter_Specification                |
1145            N_Private_Extension_Declaration          |
1146            N_Private_Type_Declaration               |
1147            N_Protected_Body                         |
1148            N_Protected_Body_Stub                    |
1149            N_Protected_Type_Declaration             |
1150            N_Single_Protected_Declaration           |
1151            N_Single_Task_Declaration                |
1152            N_Subtype_Declaration                    |
1153            N_Task_Body                              |
1154            N_Task_Body_Stub                         |
1155            N_Task_Type_Declaration
1156          =>
1157             return Defining_Identifier (N);
1158
1159          when N_Subunit =>
1160             return Defining_Entity (Proper_Body (N));
1161
1162          when
1163            N_Function_Instantiation                 |
1164            N_Function_Specification                 |
1165            N_Generic_Function_Renaming_Declaration  |
1166            N_Generic_Package_Renaming_Declaration   |
1167            N_Generic_Procedure_Renaming_Declaration |
1168            N_Package_Body                           |
1169            N_Package_Instantiation                  |
1170            N_Package_Renaming_Declaration           |
1171            N_Package_Specification                  |
1172            N_Procedure_Instantiation                |
1173            N_Procedure_Specification
1174          =>
1175             declare
1176                Nam : constant Node_Id := Defining_Unit_Name (N);
1177
1178             begin
1179                if Nkind (Nam) in N_Entity then
1180                   return Nam;
1181
1182                --  For Error, make up a name and attach to declaration
1183                --  so we can continue semantic analysis
1184
1185                elsif Nam = Error then
1186                   Err :=
1187                     Make_Defining_Identifier (Sloc (N),
1188                       Chars => New_Internal_Name ('T'));
1189                   Set_Defining_Unit_Name (N, Err);
1190
1191                   return Err;
1192                --  If not an entity, get defining identifier
1193
1194                else
1195                   return Defining_Identifier (Nam);
1196                end if;
1197             end;
1198
1199          when N_Block_Statement =>
1200             return Entity (Identifier (N));
1201
1202          when others =>
1203             raise Program_Error;
1204
1205       end case;
1206    end Defining_Entity;
1207
1208    --------------------------
1209    -- Denotes_Discriminant --
1210    --------------------------
1211
1212    function Denotes_Discriminant (N : Node_Id) return Boolean is
1213    begin
1214       return Is_Entity_Name (N)
1215         and then Present (Entity (N))
1216         and then Ekind (Entity (N)) = E_Discriminant;
1217    end Denotes_Discriminant;
1218
1219    -----------------------------
1220    -- Depends_On_Discriminant --
1221    -----------------------------
1222
1223    function Depends_On_Discriminant (N : Node_Id) return Boolean is
1224       L : Node_Id;
1225       H : Node_Id;
1226
1227    begin
1228       Get_Index_Bounds (N, L, H);
1229       return Denotes_Discriminant (L) or else Denotes_Discriminant (H);
1230    end Depends_On_Discriminant;
1231
1232    -------------------------
1233    -- Designate_Same_Unit --
1234    -------------------------
1235
1236    function Designate_Same_Unit
1237      (Name1 : Node_Id;
1238       Name2 : Node_Id)
1239       return  Boolean
1240    is
1241       K1 : Node_Kind := Nkind (Name1);
1242       K2 : Node_Kind := Nkind (Name2);
1243
1244       function Prefix_Node (N : Node_Id) return Node_Id;
1245       --  Returns the parent unit name node of a defining program unit name
1246       --  or the prefix if N is a selected component or an expanded name.
1247
1248       function Select_Node (N : Node_Id) return Node_Id;
1249       --  Returns the defining identifier node of a defining program unit
1250       --  name or  the selector node if N is a selected component or an
1251       --  expanded name.
1252
1253       function Prefix_Node (N : Node_Id) return Node_Id is
1254       begin
1255          if Nkind (N) = N_Defining_Program_Unit_Name then
1256             return Name (N);
1257
1258          else
1259             return Prefix (N);
1260          end if;
1261       end Prefix_Node;
1262
1263       function Select_Node (N : Node_Id) return Node_Id is
1264       begin
1265          if Nkind (N) = N_Defining_Program_Unit_Name then
1266             return Defining_Identifier (N);
1267
1268          else
1269             return Selector_Name (N);
1270          end if;
1271       end Select_Node;
1272
1273    --  Start of processing for Designate_Next_Unit
1274
1275    begin
1276       if (K1 = N_Identifier or else
1277           K1 = N_Defining_Identifier)
1278         and then
1279          (K2 = N_Identifier or else
1280           K2 = N_Defining_Identifier)
1281       then
1282          return Chars (Name1) = Chars (Name2);
1283
1284       elsif
1285          (K1 = N_Expanded_Name      or else
1286           K1 = N_Selected_Component or else
1287           K1 = N_Defining_Program_Unit_Name)
1288         and then
1289          (K2 = N_Expanded_Name      or else
1290           K2 = N_Selected_Component or else
1291           K2 = N_Defining_Program_Unit_Name)
1292       then
1293          return
1294            (Chars (Select_Node (Name1)) = Chars (Select_Node (Name2)))
1295              and then
1296                Designate_Same_Unit (Prefix_Node (Name1), Prefix_Node (Name2));
1297
1298       else
1299          return False;
1300       end if;
1301    end Designate_Same_Unit;
1302
1303    ----------------------------
1304    -- Enclosing_Generic_Body --
1305    ----------------------------
1306
1307    function Enclosing_Generic_Body
1308      (E    : Entity_Id)
1309       return Node_Id
1310    is
1311       P    : Node_Id;
1312       Decl : Node_Id;
1313       Spec : Node_Id;
1314
1315    begin
1316       P := Parent (E);
1317
1318       while Present (P) loop
1319          if Nkind (P) = N_Package_Body
1320            or else Nkind (P) = N_Subprogram_Body
1321          then
1322             Spec := Corresponding_Spec (P);
1323
1324             if Present (Spec) then
1325                Decl := Unit_Declaration_Node (Spec);
1326
1327                if Nkind (Decl) = N_Generic_Package_Declaration
1328                  or else Nkind (Decl) = N_Generic_Subprogram_Declaration
1329                then
1330                   return P;
1331                end if;
1332             end if;
1333          end if;
1334
1335          P := Parent (P);
1336       end loop;
1337
1338       return Empty;
1339    end Enclosing_Generic_Body;
1340
1341    -------------------------------
1342    -- Enclosing_Lib_Unit_Entity --
1343    -------------------------------
1344
1345    function Enclosing_Lib_Unit_Entity return Entity_Id is
1346       Unit_Entity : Entity_Id := Current_Scope;
1347
1348    begin
1349       --  Look for enclosing library unit entity by following scope links.
1350       --  Equivalent to, but faster than indexing through the scope stack.
1351
1352       while (Present (Scope (Unit_Entity))
1353         and then Scope (Unit_Entity) /= Standard_Standard)
1354         and not Is_Child_Unit (Unit_Entity)
1355       loop
1356          Unit_Entity := Scope (Unit_Entity);
1357       end loop;
1358
1359       return Unit_Entity;
1360    end Enclosing_Lib_Unit_Entity;
1361
1362    -----------------------------
1363    -- Enclosing_Lib_Unit_Node --
1364    -----------------------------
1365
1366    function Enclosing_Lib_Unit_Node (N : Node_Id) return Node_Id is
1367       Current_Node : Node_Id := N;
1368
1369    begin
1370       while Present (Current_Node)
1371         and then Nkind (Current_Node) /= N_Compilation_Unit
1372       loop
1373          Current_Node := Parent (Current_Node);
1374       end loop;
1375
1376       if Nkind (Current_Node) /= N_Compilation_Unit then
1377          return Empty;
1378       end if;
1379
1380       return Current_Node;
1381    end Enclosing_Lib_Unit_Node;
1382
1383    --------------------------
1384    -- Enclosing_Subprogram --
1385    --------------------------
1386
1387    function Enclosing_Subprogram (E : Entity_Id) return Entity_Id is
1388       Dynamic_Scope : constant Entity_Id := Enclosing_Dynamic_Scope (E);
1389
1390    begin
1391       if Dynamic_Scope = Standard_Standard then
1392          return Empty;
1393
1394       elsif Ekind (Dynamic_Scope) = E_Subprogram_Body then
1395          return Corresponding_Spec (Parent (Parent (Dynamic_Scope)));
1396
1397       elsif Ekind (Dynamic_Scope) = E_Block then
1398          return Enclosing_Subprogram (Dynamic_Scope);
1399
1400       elsif Ekind (Dynamic_Scope) = E_Task_Type then
1401          return Get_Task_Body_Procedure (Dynamic_Scope);
1402
1403       elsif Convention (Dynamic_Scope) = Convention_Protected then
1404          return Protected_Body_Subprogram (Dynamic_Scope);
1405
1406       else
1407          return Dynamic_Scope;
1408       end if;
1409    end Enclosing_Subprogram;
1410
1411    ------------------------
1412    -- Ensure_Freeze_Node --
1413    ------------------------
1414
1415    procedure Ensure_Freeze_Node (E : Entity_Id) is
1416       FN : Node_Id;
1417
1418    begin
1419       if No (Freeze_Node (E)) then
1420          FN := Make_Freeze_Entity (Sloc (E));
1421          Set_Has_Delayed_Freeze (E);
1422          Set_Freeze_Node (E, FN);
1423          Set_Access_Types_To_Process (FN, No_Elist);
1424          Set_TSS_Elist (FN, No_Elist);
1425          Set_Entity (FN, E);
1426       end if;
1427    end Ensure_Freeze_Node;
1428
1429    ----------------
1430    -- Enter_Name --
1431    ----------------
1432
1433    procedure Enter_Name (Def_Id : Node_Id) is
1434       C : constant Entity_Id := Current_Entity (Def_Id);
1435       E : constant Entity_Id := Current_Entity_In_Scope (Def_Id);
1436       S : constant Entity_Id := Current_Scope;
1437
1438    begin
1439       Generate_Definition (Def_Id);
1440
1441       --  Add new name to current scope declarations. Check for duplicate
1442       --  declaration, which may or may not be a genuine error.
1443
1444       if Present (E) then
1445
1446          --  Case of previous entity entered because of a missing declaration
1447          --  or else a bad subtype indication. Best is to use the new entity,
1448          --  and make the previous one invisible.
1449
1450          if Etype (E) = Any_Type then
1451             Set_Is_Immediately_Visible (E, False);
1452
1453          --  Case of renaming declaration constructed for package instances.
1454          --  if there is an explicit declaration with the same identifier,
1455          --  the renaming is not immediately visible any longer, but remains
1456          --  visible through selected component notation.
1457
1458          elsif Nkind (Parent (E)) = N_Package_Renaming_Declaration
1459            and then not Comes_From_Source (E)
1460          then
1461             Set_Is_Immediately_Visible (E, False);
1462
1463          --  The new entity may be the package renaming, which has the same
1464          --  same name as a generic formal which has been seen already.
1465
1466          elsif Nkind (Parent (Def_Id)) = N_Package_Renaming_Declaration
1467             and then not Comes_From_Source (Def_Id)
1468          then
1469             Set_Is_Immediately_Visible (E, False);
1470
1471          --  For a fat pointer corresponding to a remote access to subprogram,
1472          --  we use the same identifier as the RAS type, so that the proper
1473          --  name appears in the stub. This type is only retrieved through
1474          --  the RAS type and never by visibility, and is not added to the
1475          --  visibility list (see below).
1476
1477          elsif Nkind (Parent (Def_Id)) = N_Full_Type_Declaration
1478            and then Present (Corresponding_Remote_Type (Def_Id))
1479          then
1480             null;
1481
1482          --  A controller component for a type extension overrides the
1483          --  inherited component.
1484
1485          elsif Chars (E) = Name_uController then
1486             null;
1487
1488          --  Case of an implicit operation or derived literal. The new entity
1489          --  hides the implicit one,  which is removed from all visibility,
1490          --  i.e. the entity list of its scope, and homonym chain of its name.
1491
1492          elsif (Is_Overloadable (E) and then Present (Alias (E)))
1493            or else Is_Internal (E)
1494            or else (Ekind (E) = E_Enumeration_Literal
1495                      and then Is_Derived_Type (Etype (E)))
1496          then
1497             declare
1498                Prev     : Entity_Id;
1499                Prev_Vis : Entity_Id;
1500
1501             begin
1502                --  If E is an implicit declaration, it cannot be the first
1503                --  entity in the scope.
1504
1505                Prev := First_Entity (Current_Scope);
1506
1507                while Next_Entity (Prev) /= E loop
1508                   Next_Entity (Prev);
1509                end loop;
1510
1511                Set_Next_Entity (Prev, Next_Entity (E));
1512
1513                if No (Next_Entity (Prev)) then
1514                   Set_Last_Entity (Current_Scope, Prev);
1515                end if;
1516
1517                if E = Current_Entity (E) then
1518                      Prev_Vis := Empty;
1519                else
1520                   Prev_Vis := Current_Entity (E);
1521                   while Homonym (Prev_Vis) /= E loop
1522                      Prev_Vis := Homonym (Prev_Vis);
1523                   end loop;
1524                end if;
1525
1526                if Present (Prev_Vis)  then
1527
1528                   --  Skip E in the visibility chain
1529
1530                   Set_Homonym (Prev_Vis, Homonym (E));
1531
1532                else
1533                   Set_Name_Entity_Id (Chars (E), Homonym (E));
1534                end if;
1535             end;
1536
1537          --  This section of code could use a comment ???
1538
1539          elsif Present (Etype (E))
1540            and then Is_Concurrent_Type (Etype (E))
1541            and then E = Def_Id
1542          then
1543             return;
1544
1545          --  In the body or private part of an instance, a type extension
1546          --  may introduce a component with the same name as that of an
1547          --  actual. The legality rule is not enforced, but the semantics
1548          --  of the full type with two components of the same name are not
1549          --  clear at this point ???
1550
1551          elsif In_Instance_Not_Visible  then
1552             null;
1553
1554          --  When compiling a package body, some child units may have become
1555          --  visible. They cannot conflict with local entities that hide them.
1556
1557          elsif Is_Child_Unit (E)
1558            and then In_Open_Scopes (Scope (E))
1559            and then not Is_Immediately_Visible (E)
1560          then
1561             null;
1562
1563          --  Conversely, with front-end inlining we may compile the parent
1564          --  body first, and a child unit subsequently. The context is now
1565          --  the parent spec, and body entities are not visible.
1566
1567          elsif Is_Child_Unit (Def_Id)
1568            and then Is_Package_Body_Entity (E)
1569            and then not In_Package_Body (Current_Scope)
1570          then
1571             null;
1572
1573          --  Case of genuine duplicate declaration
1574
1575          else
1576             Error_Msg_Sloc := Sloc (E);
1577
1578             --  If the previous declaration is an incomplete type declaration
1579             --  this may be an attempt to complete it with a private type.
1580             --  The following avoids confusing cascaded errors.
1581
1582             if Nkind (Parent (E)) = N_Incomplete_Type_Declaration
1583               and then Nkind (Parent (Def_Id)) = N_Private_Type_Declaration
1584             then
1585                Error_Msg_N
1586                  ("incomplete type cannot be completed" &
1587                         " with a private declaration",
1588                     Parent (Def_Id));
1589                Set_Is_Immediately_Visible (E, False);
1590                Set_Full_View (E, Def_Id);
1591
1592             elsif Ekind (E) = E_Discriminant
1593               and then Present (Scope (Def_Id))
1594               and then Scope (Def_Id) /= Current_Scope
1595             then
1596                --  An inherited component of a record conflicts with
1597                --  a new discriminant. The discriminant is inserted first
1598                --  in the scope, but the error should be posted on it, not
1599                --  on the component.
1600
1601                Error_Msg_Sloc := Sloc (Def_Id);
1602                Error_Msg_N ("& conflicts with declaration#", E);
1603                return;
1604
1605             --  If the name of the unit appears in its own context clause,
1606             --  a dummy package with the name has already been created, and
1607             --  the error emitted. Try to continue quietly.
1608
1609             elsif Error_Posted (E)
1610               and then Sloc (E) = No_Location
1611               and then Nkind (Parent (E)) = N_Package_Specification
1612               and then Current_Scope = Standard_Standard
1613             then
1614                Set_Scope (Def_Id, Current_Scope);
1615                return;
1616
1617             else
1618                Error_Msg_N ("& conflicts with declaration#", Def_Id);
1619
1620                --  Avoid cascaded messages with duplicate components in
1621                --  derived types.
1622
1623                if Ekind (E) = E_Component
1624                  or else Ekind (E) = E_Discriminant
1625                then
1626                   return;
1627                end if;
1628             end if;
1629
1630             if Nkind (Parent (Parent (Def_Id)))
1631                  = N_Generic_Subprogram_Declaration
1632               and then Def_Id =
1633                 Defining_Entity (Specification (Parent (Parent (Def_Id))))
1634             then
1635                Error_Msg_N ("\generic units cannot be overloaded", Def_Id);
1636             end if;
1637
1638             --  If entity is in standard, then we are in trouble, because
1639             --  it means that we have a library package with a duplicated
1640             --  name. That's hard to recover from, so abort!
1641
1642             if S = Standard_Standard then
1643                raise Unrecoverable_Error;
1644
1645             --  Otherwise we continue with the declaration. Having two
1646             --  identical declarations should not cause us too much trouble!
1647
1648             else
1649                null;
1650             end if;
1651          end if;
1652       end if;
1653
1654       --  If we fall through, declaration is OK , or OK enough to continue
1655
1656       --  If Def_Id is a discriminant or a record component we are in the
1657       --  midst of inheriting components in a derived record definition.
1658       --  Preserve their Ekind and Etype.
1659
1660       if Ekind (Def_Id) = E_Discriminant
1661         or else Ekind (Def_Id) = E_Component
1662       then
1663          null;
1664
1665       --  If a type is already set, leave it alone (happens whey a type
1666       --  declaration is reanalyzed following a call to the optimizer)
1667
1668       elsif Present (Etype (Def_Id)) then
1669          null;
1670
1671       --  Otherwise, the kind E_Void insures that premature uses of the entity
1672       --  will be detected. Any_Type insures that no cascaded errors will occur
1673
1674       else
1675          Set_Ekind (Def_Id, E_Void);
1676          Set_Etype (Def_Id, Any_Type);
1677       end if;
1678
1679       --  Inherited discriminants and components in derived record types are
1680       --  immediately visible. Itypes are not.
1681
1682       if Ekind (Def_Id) = E_Discriminant
1683         or else Ekind (Def_Id) = E_Component
1684         or else (No (Corresponding_Remote_Type (Def_Id))
1685                  and then not Is_Itype (Def_Id))
1686       then
1687          Set_Is_Immediately_Visible (Def_Id);
1688          Set_Current_Entity         (Def_Id);
1689       end if;
1690
1691       Set_Homonym       (Def_Id, C);
1692       Append_Entity     (Def_Id, S);
1693       Set_Public_Status (Def_Id);
1694
1695       --  Warn if new entity hides an old one
1696
1697       if Warn_On_Hiding
1698         and then Length_Of_Name (Chars (C)) /= 1
1699         and then Present (C)
1700         and then Comes_From_Source (C)
1701         and then Comes_From_Source (Def_Id)
1702         and then In_Extended_Main_Source_Unit (Def_Id)
1703       then
1704          Error_Msg_Sloc := Sloc (C);
1705          Error_Msg_N ("declaration hides &#?", Def_Id);
1706       end if;
1707
1708    end Enter_Name;
1709
1710    -------------------------------------
1711    -- Find_Corresponding_Discriminant --
1712    -------------------------------------
1713
1714    function Find_Corresponding_Discriminant
1715      (Id   : Node_Id;
1716       Typ  : Entity_Id)
1717       return Entity_Id
1718    is
1719       Par_Disc : Entity_Id;
1720       Old_Disc : Entity_Id;
1721       New_Disc : Entity_Id;
1722
1723    begin
1724       Par_Disc := Original_Record_Component (Original_Discriminant (Id));
1725       Old_Disc := First_Discriminant (Scope (Par_Disc));
1726
1727       if Is_Class_Wide_Type (Typ) then
1728          New_Disc := First_Discriminant (Root_Type (Typ));
1729       else
1730          New_Disc := First_Discriminant (Typ);
1731       end if;
1732
1733       while Present (Old_Disc) and then Present (New_Disc) loop
1734          if Old_Disc = Par_Disc  then
1735             return New_Disc;
1736          else
1737             Next_Discriminant (Old_Disc);
1738             Next_Discriminant (New_Disc);
1739          end if;
1740       end loop;
1741
1742       --  Should always find it
1743
1744       raise Program_Error;
1745    end Find_Corresponding_Discriminant;
1746
1747    ------------------
1748    -- First_Actual --
1749    ------------------
1750
1751    function First_Actual (Node : Node_Id) return Node_Id is
1752       N : Node_Id;
1753
1754    begin
1755       if No (Parameter_Associations (Node)) then
1756          return Empty;
1757       end if;
1758
1759       N := First (Parameter_Associations (Node));
1760
1761       if Nkind (N) = N_Parameter_Association then
1762          return First_Named_Actual (Node);
1763       else
1764          return N;
1765       end if;
1766    end First_Actual;
1767
1768    -------------------------
1769    -- Full_Qualified_Name --
1770    -------------------------
1771
1772    function Full_Qualified_Name (E : Entity_Id) return String_Id is
1773
1774       Res : String_Id;
1775
1776       function Internal_Full_Qualified_Name (E : Entity_Id) return String_Id;
1777       --  Compute recursively the qualified name without NUL at the end.
1778
1779       function Internal_Full_Qualified_Name (E : Entity_Id) return String_Id is
1780          Ent         : Entity_Id := E;
1781          Parent_Name : String_Id := No_String;
1782
1783       begin
1784          --  Deals properly with child units
1785
1786          if Nkind (Ent) = N_Defining_Program_Unit_Name then
1787             Ent := Defining_Identifier (Ent);
1788          end if;
1789
1790          --  Compute recursively the qualification. Only "Standard" has no
1791          --  scope.
1792
1793          if Present (Scope (Scope (Ent))) then
1794             Parent_Name := Internal_Full_Qualified_Name (Scope (Ent));
1795          end if;
1796
1797          --  Every entity should have a name except some expanded blocks
1798          --  don't bother about those.
1799
1800          if Chars (Ent) = No_Name then
1801             return Parent_Name;
1802          end if;
1803
1804          --  Add a period between Name and qualification
1805
1806          if Parent_Name /= No_String then
1807             Start_String (Parent_Name);
1808             Store_String_Char (Get_Char_Code ('.'));
1809
1810          else
1811             Start_String;
1812          end if;
1813
1814          --  Generates the entity name in upper case
1815
1816          Get_Name_String (Chars (Ent));
1817          Set_All_Upper_Case;
1818          Store_String_Chars (Name_Buffer (1 .. Name_Len));
1819          return End_String;
1820       end Internal_Full_Qualified_Name;
1821
1822    begin
1823       Res := Internal_Full_Qualified_Name (E);
1824       Store_String_Char (Get_Char_Code (ASCII.nul));
1825       return End_String;
1826    end Full_Qualified_Name;
1827
1828    -----------------------
1829    -- Gather_Components --
1830    -----------------------
1831
1832    procedure Gather_Components
1833      (Typ           : Entity_Id;
1834       Comp_List     : Node_Id;
1835       Governed_By   : List_Id;
1836       Into          : Elist_Id;
1837       Report_Errors : out Boolean)
1838    is
1839       Assoc           : Node_Id;
1840       Variant         : Node_Id;
1841       Discrete_Choice : Node_Id;
1842       Comp_Item       : Node_Id;
1843
1844       Discrim       : Entity_Id;
1845       Discrim_Name  : Node_Id;
1846       Discrim_Value : Node_Id;
1847
1848    begin
1849       Report_Errors := False;
1850
1851       if No (Comp_List) or else Null_Present (Comp_List) then
1852          return;
1853
1854       elsif Present (Component_Items (Comp_List)) then
1855          Comp_Item := First (Component_Items (Comp_List));
1856
1857       else
1858          Comp_Item := Empty;
1859       end if;
1860
1861       while Present (Comp_Item) loop
1862
1863          --  Skip the tag of a tagged record, as well as all items
1864          --  that are not user components (anonymous types, rep clauses,
1865          --  Parent field, controller field).
1866
1867          if Nkind (Comp_Item) = N_Component_Declaration
1868            and then Chars (Defining_Identifier (Comp_Item)) /= Name_uTag
1869            and then Chars (Defining_Identifier (Comp_Item)) /= Name_uParent
1870            and then Chars (Defining_Identifier (Comp_Item)) /= Name_uController
1871          then
1872             Append_Elmt (Defining_Identifier (Comp_Item), Into);
1873          end if;
1874
1875          Next (Comp_Item);
1876       end loop;
1877
1878       if No (Variant_Part (Comp_List)) then
1879          return;
1880       else
1881          Discrim_Name := Name (Variant_Part (Comp_List));
1882          Variant := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
1883       end if;
1884
1885       --  Look for the discriminant that governs this variant part.
1886       --  The discriminant *must* be in the Governed_By List
1887
1888       Assoc := First (Governed_By);
1889       Find_Constraint : loop
1890          Discrim := First (Choices (Assoc));
1891          exit Find_Constraint when Chars (Discrim_Name) = Chars (Discrim)
1892            or else (Present (Corresponding_Discriminant (Entity (Discrim)))
1893                       and then
1894                     Chars (Corresponding_Discriminant (Entity (Discrim)))
1895                          = Chars  (Discrim_Name))
1896            or else Chars (Original_Record_Component (Entity (Discrim)))
1897                          = Chars (Discrim_Name);
1898
1899          if No (Next (Assoc)) then
1900             if not Is_Constrained (Typ)
1901               and then Is_Derived_Type (Typ)
1902               and then Present (Girder_Constraint (Typ))
1903             then
1904
1905                --  If the type is a tagged type with inherited discriminants,
1906                --  use the girder constraint on the parent in order to find
1907                --  the values of discriminants that are otherwise hidden by an
1908                --  explicit constraint. Renamed discriminants are handled in
1909                --  the code above.
1910
1911                declare
1912                   D : Entity_Id;
1913                   C : Elmt_Id;
1914
1915                begin
1916                   D := First_Discriminant (Etype (Typ));
1917                   C := First_Elmt (Girder_Constraint (Typ));
1918
1919                   while Present (D)
1920                     and then Present (C)
1921                   loop
1922                      if Chars (Discrim_Name) = Chars (D) then
1923                         Assoc :=
1924                           Make_Component_Association (Sloc (Typ),
1925                             New_List
1926                               (New_Occurrence_Of (D, Sloc (Typ))),
1927                             Duplicate_Subexpr (Node (C)));
1928                         exit Find_Constraint;
1929                      end if;
1930
1931                      D := Next_Discriminant (D);
1932                      Next_Elmt (C);
1933                   end loop;
1934                end;
1935             end if;
1936          end if;
1937
1938          if No (Next (Assoc)) then
1939             Error_Msg_NE (" missing value for discriminant&",
1940               First (Governed_By), Discrim_Name);
1941             Report_Errors := True;
1942             return;
1943          end if;
1944
1945          Next (Assoc);
1946       end loop Find_Constraint;
1947
1948       Discrim_Value := Expression (Assoc);
1949
1950       if not Is_OK_Static_Expression (Discrim_Value) then
1951          Error_Msg_NE
1952            ("value for discriminant & must be static", Discrim_Value, Discrim);
1953          Report_Errors := True;
1954          return;
1955       end if;
1956
1957       Search_For_Discriminant_Value : declare
1958          Low  : Node_Id;
1959          High : Node_Id;
1960
1961          UI_High          : Uint;
1962          UI_Low           : Uint;
1963          UI_Discrim_Value : constant Uint := Expr_Value (Discrim_Value);
1964
1965       begin
1966          Find_Discrete_Value : while Present (Variant) loop
1967             Discrete_Choice := First (Discrete_Choices (Variant));
1968             while Present (Discrete_Choice) loop
1969
1970                exit Find_Discrete_Value when
1971                  Nkind (Discrete_Choice) = N_Others_Choice;
1972
1973                Get_Index_Bounds (Discrete_Choice, Low, High);
1974
1975                UI_Low  := Expr_Value (Low);
1976                UI_High := Expr_Value (High);
1977
1978                exit Find_Discrete_Value when
1979                  UI_Low <= UI_Discrim_Value
1980                    and then
1981                  UI_High >= UI_Discrim_Value;
1982
1983                Next (Discrete_Choice);
1984             end loop;
1985
1986             Next_Non_Pragma (Variant);
1987          end loop Find_Discrete_Value;
1988       end Search_For_Discriminant_Value;
1989
1990       if No (Variant) then
1991          Error_Msg_NE
1992            ("value of discriminant & is out of range", Discrim_Value, Discrim);
1993          Report_Errors := True;
1994          return;
1995       end  if;
1996
1997       --  If we have found the corresponding choice, recursively add its
1998       --  components to the Into list.
1999
2000       Gather_Components (Empty,
2001         Component_List (Variant), Governed_By, Into, Report_Errors);
2002    end Gather_Components;
2003
2004    ------------------------
2005    -- Get_Actual_Subtype --
2006    ------------------------
2007
2008    function Get_Actual_Subtype (N : Node_Id) return Entity_Id is
2009       Typ  : constant Entity_Id := Etype (N);
2010       Utyp : Entity_Id := Underlying_Type (Typ);
2011       Decl : Node_Id;
2012       Atyp : Entity_Id;
2013
2014    begin
2015       if not Present (Utyp) then
2016          Utyp := Typ;
2017       end if;
2018
2019       --  If what we have is an identifier that references a subprogram
2020       --  formal, or a variable or constant object, then we get the actual
2021       --  subtype from the referenced entity if one has been built.
2022
2023       if Nkind (N) = N_Identifier
2024         and then
2025           (Is_Formal (Entity (N))
2026             or else Ekind (Entity (N)) = E_Constant
2027             or else Ekind (Entity (N)) = E_Variable)
2028         and then Present (Actual_Subtype (Entity (N)))
2029       then
2030          return Actual_Subtype (Entity (N));
2031
2032       --  Actual subtype of unchecked union is always itself. We never need
2033       --  the "real" actual subtype. If we did, we couldn't get it anyway
2034       --  because the discriminant is not available. The restrictions on
2035       --  Unchecked_Union are designed to make sure that this is OK.
2036
2037       elsif Is_Unchecked_Union (Utyp) then
2038          return Typ;
2039
2040       --  Here for the unconstrained case, we must find actual subtype
2041       --  No actual subtype is available, so we must build it on the fly.
2042
2043       --  Checking the type, not the underlying type, for constrainedness
2044       --  seems to be necessary. Maybe all the tests should be on the type???
2045
2046       elsif (not Is_Constrained (Typ))
2047            and then (Is_Array_Type (Utyp)
2048                       or else (Is_Record_Type (Utyp)
2049                                 and then Has_Discriminants (Utyp)))
2050            and then not Has_Unknown_Discriminants (Utyp)
2051            and then not (Ekind (Utyp) = E_String_Literal_Subtype)
2052       then
2053          --  Nothing to do if in default expression
2054
2055          if In_Default_Expression then
2056             return Typ;
2057
2058          --  Else build the actual subtype
2059
2060          else
2061             Decl := Build_Actual_Subtype (Typ, N);
2062             Atyp := Defining_Identifier (Decl);
2063
2064             --  If Build_Actual_Subtype generated a new declaration then use it
2065
2066             if Atyp /= Typ then
2067
2068                --  The actual subtype is an Itype, so analyze the declaration,
2069                --  but do not attach it to the tree, to get the type defined.
2070
2071                Set_Parent (Decl, N);
2072                Set_Is_Itype (Atyp);
2073                Analyze (Decl, Suppress => All_Checks);
2074                Set_Associated_Node_For_Itype (Atyp, N);
2075                Set_Has_Delayed_Freeze (Atyp, False);
2076
2077                --  We need to freeze the actual subtype immediately. This is
2078                --  needed, because otherwise this Itype will not get frozen
2079                --  at all, and it is always safe to freeze on creation because
2080                --  any associated types must be frozen at this point.
2081
2082                Freeze_Itype (Atyp, N);
2083                return Atyp;
2084
2085             --  Otherwise we did not build a declaration, so return original
2086
2087             else
2088                return Typ;
2089             end if;
2090          end if;
2091
2092       --  For all remaining cases, the actual subtype is the same as
2093       --  the nominal type.
2094
2095       else
2096          return Typ;
2097       end if;
2098    end Get_Actual_Subtype;
2099
2100    -------------------------------------
2101    -- Get_Actual_Subtype_If_Available --
2102    -------------------------------------
2103
2104    function Get_Actual_Subtype_If_Available (N : Node_Id) return Entity_Id is
2105       Typ  : constant Entity_Id := Etype (N);
2106
2107    begin
2108       --  If what we have is an identifier that references a subprogram
2109       --  formal, or a variable or constant object, then we get the actual
2110       --  subtype from the referenced entity if one has been built.
2111
2112       if Nkind (N) = N_Identifier
2113         and then
2114           (Is_Formal (Entity (N))
2115             or else Ekind (Entity (N)) = E_Constant
2116             or else Ekind (Entity (N)) = E_Variable)
2117         and then Present (Actual_Subtype (Entity (N)))
2118       then
2119          return Actual_Subtype (Entity (N));
2120
2121       --  Otherwise the Etype of N is returned unchanged
2122
2123       else
2124          return Typ;
2125       end if;
2126    end Get_Actual_Subtype_If_Available;
2127
2128    -------------------------------
2129    -- Get_Default_External_Name --
2130    -------------------------------
2131
2132    function Get_Default_External_Name (E : Node_Or_Entity_Id) return Node_Id is
2133    begin
2134       Get_Decoded_Name_String (Chars (E));
2135
2136       if Opt.External_Name_Imp_Casing = Uppercase then
2137          Set_Casing (All_Upper_Case);
2138       else
2139          Set_Casing (All_Lower_Case);
2140       end if;
2141
2142       return
2143         Make_String_Literal (Sloc (E),
2144           Strval => String_From_Name_Buffer);
2145
2146    end Get_Default_External_Name;
2147
2148    ---------------------------
2149    -- Get_Enum_Lit_From_Pos --
2150    ---------------------------
2151
2152    function Get_Enum_Lit_From_Pos
2153      (T    : Entity_Id;
2154       Pos  : Uint;
2155       Loc  : Source_Ptr)
2156       return Node_Id
2157    is
2158       Lit : Node_Id;
2159       P   : constant Nat := UI_To_Int (Pos);
2160
2161    begin
2162       --  In the case where the literal is either of type Wide_Character
2163       --  or Character or of a type derived from them, there needs to be
2164       --  some special handling since there is no explicit chain of
2165       --  literals to search. Instead, an N_Character_Literal node is
2166       --  created with the appropriate Char_Code and Chars fields.
2167
2168       if Root_Type (T) = Standard_Character
2169         or else Root_Type (T) = Standard_Wide_Character
2170       then
2171          Set_Character_Literal_Name (Char_Code (P));
2172          return
2173            Make_Character_Literal (Loc,
2174              Chars => Name_Find,
2175              Char_Literal_Value => Char_Code (P));
2176
2177       --  For all other cases, we have a complete table of literals, and
2178       --  we simply iterate through the chain of literal until the one
2179       --  with the desired position value is found.
2180       --
2181
2182       else
2183          Lit := First_Literal (Base_Type (T));
2184          for J in 1 .. P loop
2185             Next_Literal (Lit);
2186          end loop;
2187
2188          return New_Occurrence_Of (Lit, Loc);
2189       end if;
2190    end Get_Enum_Lit_From_Pos;
2191
2192    ----------------------
2193    -- Get_Index_Bounds --
2194    ----------------------
2195
2196    procedure Get_Index_Bounds (N : Node_Id; L, H : out Node_Id) is
2197       Kind : constant Node_Kind := Nkind (N);
2198       R    : Node_Id;
2199
2200    begin
2201       if Kind = N_Range then
2202          L := Low_Bound (N);
2203          H := High_Bound (N);
2204
2205       elsif Kind = N_Subtype_Indication then
2206          R := Range_Expression (Constraint (N));
2207
2208          if R = Error then
2209             L := Error;
2210             H := Error;
2211             return;
2212
2213          else
2214             L := Low_Bound  (Range_Expression (Constraint (N)));
2215             H := High_Bound (Range_Expression (Constraint (N)));
2216          end if;
2217
2218       elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
2219          if Error_Posted (Scalar_Range (Entity (N))) then
2220             L := Error;
2221             H := Error;
2222
2223          elsif Nkind (Scalar_Range (Entity (N))) = N_Subtype_Indication then
2224             Get_Index_Bounds (Scalar_Range (Entity (N)), L, H);
2225
2226          else
2227             L := Low_Bound  (Scalar_Range (Entity (N)));
2228             H := High_Bound (Scalar_Range (Entity (N)));
2229          end if;
2230
2231       else
2232          --  N is an expression, indicating a range with one value.
2233
2234          L := N;
2235          H := N;
2236       end if;
2237    end Get_Index_Bounds;
2238
2239    ------------------------
2240    -- Get_Name_Entity_Id --
2241    ------------------------
2242
2243    function Get_Name_Entity_Id (Id : Name_Id) return Entity_Id is
2244    begin
2245       return Entity_Id (Get_Name_Table_Info (Id));
2246    end Get_Name_Entity_Id;
2247
2248    ---------------------------
2249    -- Get_Referenced_Object --
2250    ---------------------------
2251
2252    function Get_Referenced_Object (N : Node_Id) return Node_Id is
2253       R   : Node_Id := N;
2254
2255    begin
2256       while Is_Entity_Name (R)
2257         and then Present (Renamed_Object (Entity (R)))
2258       loop
2259          R := Renamed_Object (Entity (R));
2260       end loop;
2261
2262       return R;
2263    end Get_Referenced_Object;
2264
2265    -------------------------
2266    -- Get_Subprogram_Body --
2267    -------------------------
2268
2269    function Get_Subprogram_Body (E : Entity_Id) return Node_Id is
2270       Decl : Node_Id;
2271
2272    begin
2273       Decl := Unit_Declaration_Node (E);
2274
2275       if Nkind (Decl) = N_Subprogram_Body then
2276          return Decl;
2277
2278       else           --  Nkind (Decl) = N_Subprogram_Declaration
2279
2280          if Present (Corresponding_Body (Decl)) then
2281             return Unit_Declaration_Node (Corresponding_Body (Decl));
2282
2283          else        --  imported subprogram.
2284             return Empty;
2285          end if;
2286       end if;
2287    end Get_Subprogram_Body;
2288
2289    -----------------------------
2290    -- Get_Task_Body_Procedure --
2291    -----------------------------
2292
2293    function Get_Task_Body_Procedure (E : Entity_Id) return Node_Id is
2294    begin
2295       return Task_Body_Procedure (Declaration_Node (Root_Type (E)));
2296    end Get_Task_Body_Procedure;
2297
2298    --------------------
2299    -- Has_Infinities --
2300    --------------------
2301
2302    function Has_Infinities (E : Entity_Id) return Boolean is
2303    begin
2304       return
2305         Is_Floating_Point_Type (E)
2306           and then Nkind (Scalar_Range (E)) = N_Range
2307           and then Includes_Infinities (Scalar_Range (E));
2308    end Has_Infinities;
2309
2310    ---------------------------
2311    -- Has_Private_Component --
2312    ---------------------------
2313
2314    function Has_Private_Component (Type_Id : Entity_Id) return Boolean is
2315       Btype     : Entity_Id := Base_Type (Type_Id);
2316       Component : Entity_Id;
2317
2318    begin
2319       if Error_Posted (Type_Id)
2320         or else Error_Posted (Btype)
2321       then
2322          return False;
2323       end if;
2324
2325       if Is_Class_Wide_Type (Btype) then
2326          Btype := Root_Type (Btype);
2327       end if;
2328
2329       if Is_Private_Type (Btype) then
2330          declare
2331             UT : constant Entity_Id := Underlying_Type (Btype);
2332          begin
2333             if No (UT) then
2334
2335                if No (Full_View (Btype)) then
2336                   return not Is_Generic_Type (Btype)
2337                     and then not Is_Generic_Type (Root_Type (Btype));
2338
2339                else
2340                   return not Is_Generic_Type (Root_Type (Full_View (Btype)));
2341                end if;
2342
2343             else
2344                return not Is_Frozen (UT) and then Has_Private_Component (UT);
2345             end if;
2346          end;
2347       elsif Is_Array_Type (Btype) then
2348          return Has_Private_Component (Component_Type (Btype));
2349
2350       elsif Is_Record_Type (Btype) then
2351
2352          Component := First_Component (Btype);
2353          while Present (Component) loop
2354
2355             if Has_Private_Component (Etype (Component)) then
2356                return True;
2357             end if;
2358
2359             Next_Component (Component);
2360          end loop;
2361
2362          return False;
2363
2364       elsif Is_Protected_Type (Btype)
2365         and then Present (Corresponding_Record_Type (Btype))
2366       then
2367          return Has_Private_Component (Corresponding_Record_Type (Btype));
2368
2369       else
2370          return False;
2371       end if;
2372    end Has_Private_Component;
2373
2374    --------------------------
2375    -- Has_Tagged_Component --
2376    --------------------------
2377
2378    function Has_Tagged_Component (Typ : Entity_Id) return Boolean is
2379       Comp : Entity_Id;
2380
2381    begin
2382       if Is_Private_Type (Typ)
2383         and then Present (Underlying_Type (Typ))
2384       then
2385          return Has_Tagged_Component (Underlying_Type (Typ));
2386
2387       elsif Is_Array_Type (Typ) then
2388          return Has_Tagged_Component (Component_Type (Typ));
2389
2390       elsif Is_Tagged_Type (Typ) then
2391          return True;
2392
2393       elsif Is_Record_Type (Typ) then
2394          Comp := First_Component (Typ);
2395
2396          while Present (Comp) loop
2397             if Has_Tagged_Component (Etype (Comp)) then
2398                return True;
2399             end if;
2400
2401             Comp := Next_Component (Typ);
2402          end loop;
2403
2404          return False;
2405
2406       else
2407          return False;
2408       end if;
2409    end Has_Tagged_Component;
2410
2411    -----------------
2412    -- In_Instance --
2413    -----------------
2414
2415    function In_Instance return Boolean is
2416       S : Entity_Id := Current_Scope;
2417
2418    begin
2419       while Present (S)
2420         and then S /= Standard_Standard
2421       loop
2422          if (Ekind (S) = E_Function
2423               or else Ekind (S) = E_Package
2424               or else Ekind (S) = E_Procedure)
2425            and then Is_Generic_Instance (S)
2426          then
2427             return True;
2428          end if;
2429
2430          S := Scope (S);
2431       end loop;
2432
2433       return False;
2434    end In_Instance;
2435
2436    ----------------------
2437    -- In_Instance_Body --
2438    ----------------------
2439
2440    function In_Instance_Body return Boolean is
2441       S : Entity_Id := Current_Scope;
2442
2443    begin
2444       while Present (S)
2445         and then S /= Standard_Standard
2446       loop
2447          if (Ekind (S) = E_Function
2448               or else Ekind (S) = E_Procedure)
2449            and then Is_Generic_Instance (S)
2450          then
2451             return True;
2452
2453          elsif Ekind (S) = E_Package
2454            and then In_Package_Body (S)
2455            and then Is_Generic_Instance (S)
2456          then
2457             return True;
2458          end if;
2459
2460          S := Scope (S);
2461       end loop;
2462
2463       return False;
2464    end In_Instance_Body;
2465
2466    -----------------------------
2467    -- In_Instance_Not_Visible --
2468    -----------------------------
2469
2470    function In_Instance_Not_Visible return Boolean is
2471       S : Entity_Id := Current_Scope;
2472
2473    begin
2474       while Present (S)
2475         and then S /= Standard_Standard
2476       loop
2477          if (Ekind (S) = E_Function
2478               or else Ekind (S) = E_Procedure)
2479            and then Is_Generic_Instance (S)
2480          then
2481             return True;
2482
2483          elsif Ekind (S) = E_Package
2484            and then (In_Package_Body (S) or else In_Private_Part (S))
2485            and then Is_Generic_Instance (S)
2486          then
2487             return True;
2488          end if;
2489
2490          S := Scope (S);
2491       end loop;
2492
2493       return False;
2494    end In_Instance_Not_Visible;
2495
2496    ------------------------------
2497    -- In_Instance_Visible_Part --
2498    ------------------------------
2499
2500    function In_Instance_Visible_Part return Boolean is
2501       S : Entity_Id := Current_Scope;
2502
2503    begin
2504       while Present (S)
2505         and then S /= Standard_Standard
2506       loop
2507          if Ekind (S) = E_Package
2508            and then Is_Generic_Instance (S)
2509            and then not In_Package_Body (S)
2510            and then not In_Private_Part (S)
2511          then
2512             return True;
2513          end if;
2514
2515          S := Scope (S);
2516       end loop;
2517
2518       return False;
2519    end In_Instance_Visible_Part;
2520
2521    --------------------------------------
2522    -- In_Subprogram_Or_Concurrent_Unit --
2523    --------------------------------------
2524
2525    function In_Subprogram_Or_Concurrent_Unit return Boolean is
2526       E : Entity_Id;
2527       K : Entity_Kind;
2528
2529    begin
2530       --  Use scope chain to check successively outer scopes
2531
2532       E := Current_Scope;
2533       loop
2534          K := Ekind (E);
2535
2536          if K in Subprogram_Kind
2537            or else K in Concurrent_Kind
2538            or else K = E_Generic_Procedure
2539            or else K = E_Generic_Function
2540          then
2541             return True;
2542
2543          elsif E = Standard_Standard then
2544             return False;
2545          end if;
2546
2547          E := Scope (E);
2548       end loop;
2549
2550    end In_Subprogram_Or_Concurrent_Unit;
2551
2552    ---------------------
2553    -- In_Visible_Part --
2554    ---------------------
2555
2556    function In_Visible_Part (Scope_Id : Entity_Id) return Boolean is
2557    begin
2558       return
2559         Is_Package (Scope_Id)
2560           and then In_Open_Scopes (Scope_Id)
2561           and then not In_Package_Body (Scope_Id)
2562           and then not In_Private_Part (Scope_Id);
2563    end In_Visible_Part;
2564
2565    -------------------
2566    -- Is_AAMP_Float --
2567    -------------------
2568
2569    function Is_AAMP_Float (E : Entity_Id) return Boolean is
2570    begin
2571       pragma Assert (Is_Type (E));
2572
2573       return AAMP_On_Target
2574          and then Is_Floating_Point_Type (E)
2575          and then E = Base_Type (E);
2576    end Is_AAMP_Float;
2577
2578    -------------------------
2579    -- Is_Actual_Parameter --
2580    -------------------------
2581
2582    function Is_Actual_Parameter (N : Node_Id) return Boolean is
2583       PK : constant Node_Kind := Nkind (Parent (N));
2584
2585    begin
2586       case PK is
2587          when N_Parameter_Association =>
2588             return N = Explicit_Actual_Parameter (Parent (N));
2589
2590          when N_Function_Call | N_Procedure_Call_Statement =>
2591             return Is_List_Member (N)
2592               and then
2593                 List_Containing (N) = Parameter_Associations (Parent (N));
2594
2595          when others =>
2596             return False;
2597       end case;
2598    end Is_Actual_Parameter;
2599
2600    ---------------------
2601    -- Is_Aliased_View --
2602    ---------------------
2603
2604    function Is_Aliased_View (Obj : Node_Id) return Boolean is
2605       E : Entity_Id;
2606
2607    begin
2608       if Is_Entity_Name (Obj) then
2609
2610          --  Shouldn't we check that we really have an object here?
2611          --  If we do, then a-caldel.adb blows up mysteriously ???
2612
2613          E := Entity (Obj);
2614
2615          return Is_Aliased (E)
2616            or else (Present (Renamed_Object (E))
2617                      and then Is_Aliased_View (Renamed_Object (E)))
2618
2619            or else ((Is_Formal (E)
2620                       or else Ekind (E) = E_Generic_In_Out_Parameter
2621                       or else Ekind (E) = E_Generic_In_Parameter)
2622                     and then Is_Tagged_Type (Etype (E)))
2623
2624            or else ((Ekind (E) = E_Task_Type or else
2625                      Ekind (E) = E_Protected_Type)
2626                     and then In_Open_Scopes (E))
2627
2628             --  Current instance of type
2629
2630            or else (Is_Type (E) and then E = Current_Scope)
2631            or else (Is_Incomplete_Or_Private_Type (E)
2632                      and then Full_View (E) = Current_Scope);
2633
2634       elsif Nkind (Obj) = N_Selected_Component then
2635          return Is_Aliased (Entity (Selector_Name (Obj)));
2636
2637       elsif Nkind (Obj) = N_Indexed_Component then
2638          return Has_Aliased_Components (Etype (Prefix (Obj)))
2639            or else
2640              (Is_Access_Type (Etype (Prefix (Obj)))
2641                and then
2642               Has_Aliased_Components
2643                 (Designated_Type (Etype (Prefix (Obj)))));
2644
2645       elsif Nkind (Obj) = N_Unchecked_Type_Conversion
2646         or else Nkind (Obj) = N_Type_Conversion
2647       then
2648          return Is_Tagged_Type (Etype (Obj))
2649            or else Is_Aliased_View (Expression (Obj));
2650
2651       elsif Nkind (Obj) = N_Explicit_Dereference then
2652          return Nkind (Original_Node (Obj)) /= N_Function_Call;
2653
2654       else
2655          return False;
2656       end if;
2657    end Is_Aliased_View;
2658
2659    ----------------------
2660    -- Is_Atomic_Object --
2661    ----------------------
2662
2663    function Is_Atomic_Object (N : Node_Id) return Boolean is
2664
2665       function Object_Has_Atomic_Components (N : Node_Id) return Boolean;
2666       --  Determines if given object has atomic components
2667
2668       function Is_Atomic_Prefix (N : Node_Id) return Boolean;
2669       --  If prefix is an implicit dereference, examine designated type.
2670
2671       function Is_Atomic_Prefix (N : Node_Id) return Boolean is
2672       begin
2673          if Is_Access_Type (Etype (N)) then
2674             return
2675               Has_Atomic_Components (Designated_Type (Etype (N)));
2676          else
2677             return Object_Has_Atomic_Components (N);
2678          end if;
2679       end Is_Atomic_Prefix;
2680
2681       function Object_Has_Atomic_Components (N : Node_Id) return Boolean is
2682       begin
2683          if Has_Atomic_Components (Etype (N))
2684            or else Is_Atomic (Etype (N))
2685          then
2686             return True;
2687
2688          elsif Is_Entity_Name (N)
2689            and then (Has_Atomic_Components (Entity (N))
2690                       or else Is_Atomic (Entity (N)))
2691          then
2692             return True;
2693
2694          elsif Nkind (N) = N_Indexed_Component
2695            or else Nkind (N) = N_Selected_Component
2696          then
2697             return Is_Atomic_Prefix (Prefix (N));
2698
2699          else
2700             return False;
2701          end if;
2702       end Object_Has_Atomic_Components;
2703
2704    --  Start of processing for Is_Atomic_Object
2705
2706    begin
2707       if Is_Atomic (Etype (N))
2708         or else (Is_Entity_Name (N) and then Is_Atomic (Entity (N)))
2709       then
2710          return True;
2711
2712       elsif Nkind (N) = N_Indexed_Component
2713         or else Nkind (N) = N_Selected_Component
2714       then
2715          return Is_Atomic_Prefix (Prefix (N));
2716
2717       else
2718          return False;
2719       end if;
2720    end Is_Atomic_Object;
2721
2722    ----------------------------------------------
2723    -- Is_Dependent_Component_Of_Mutable_Object --
2724    ----------------------------------------------
2725
2726    function Is_Dependent_Component_Of_Mutable_Object
2727      (Object : Node_Id)
2728       return   Boolean
2729    is
2730       P           : Node_Id;
2731       Prefix_Type : Entity_Id;
2732       P_Aliased   : Boolean := False;
2733       Comp        : Entity_Id;
2734
2735       function Has_Dependent_Constraint (Comp : Entity_Id) return Boolean;
2736       --  Returns True if and only if Comp has a constrained subtype
2737       --  that depends on a discriminant.
2738
2739       function Is_Declared_Within_Variant (Comp : Entity_Id) return Boolean;
2740       --  Returns True if and only if Comp is declared within a variant part.
2741
2742       ------------------------------
2743       -- Has_Dependent_Constraint --
2744       ------------------------------
2745
2746       function Has_Dependent_Constraint (Comp : Entity_Id) return Boolean is
2747          Comp_Decl  : constant Node_Id   := Parent (Comp);
2748          Subt_Indic : constant Node_Id   := Subtype_Indication (Comp_Decl);
2749          Constr     : Node_Id;
2750          Assn       : Node_Id;
2751
2752       begin
2753          if Nkind (Subt_Indic) = N_Subtype_Indication then
2754             Constr := Constraint (Subt_Indic);
2755
2756             if Nkind (Constr) = N_Index_Or_Discriminant_Constraint then
2757                Assn := First (Constraints (Constr));
2758                while Present (Assn) loop
2759                   case Nkind (Assn) is
2760                      when N_Subtype_Indication |
2761                           N_Range              |
2762                           N_Identifier
2763                      =>
2764                         if Depends_On_Discriminant (Assn) then
2765                            return True;
2766                         end if;
2767
2768                      when N_Discriminant_Association =>
2769                         if Depends_On_Discriminant (Expression (Assn)) then
2770                            return True;
2771                         end if;
2772
2773                      when others =>
2774                         null;
2775
2776                   end case;
2777
2778                   Next (Assn);
2779                end loop;
2780             end if;
2781          end if;
2782
2783          return False;
2784       end Has_Dependent_Constraint;
2785
2786       --------------------------------
2787       -- Is_Declared_Within_Variant --
2788       --------------------------------
2789
2790       function Is_Declared_Within_Variant (Comp : Entity_Id) return Boolean is
2791          Comp_Decl : constant Node_Id   := Parent (Comp);
2792          Comp_List : constant Node_Id   := Parent (Comp_Decl);
2793
2794       begin
2795          return Nkind (Parent (Comp_List)) = N_Variant;
2796       end Is_Declared_Within_Variant;
2797
2798    --  Start of processing for Is_Dependent_Component_Of_Mutable_Object
2799
2800    begin
2801       if Is_Variable (Object) then
2802
2803          if Nkind (Object) = N_Selected_Component then
2804             P := Prefix (Object);
2805             Prefix_Type := Etype (P);
2806
2807             if Is_Entity_Name (P) then
2808
2809                if Ekind (Entity (P)) = E_Generic_In_Out_Parameter then
2810                   Prefix_Type := Base_Type (Prefix_Type);
2811                end if;
2812
2813                if Is_Aliased (Entity (P)) then
2814                   P_Aliased := True;
2815                end if;
2816
2817             else
2818                --  Check for prefix being an aliased component ???
2819                null;
2820             end if;
2821
2822             if Is_Access_Type (Prefix_Type)
2823               or else Nkind (P) = N_Explicit_Dereference
2824             then
2825                return False;
2826             end if;
2827
2828             Comp :=
2829               Original_Record_Component (Entity (Selector_Name (Object)));
2830
2831             if not Is_Constrained (Prefix_Type)
2832               and then not Is_Indefinite_Subtype (Prefix_Type)
2833               and then (Is_Declared_Within_Variant (Comp)
2834                           or else Has_Dependent_Constraint (Comp))
2835               and then not P_Aliased
2836             then
2837                return True;
2838
2839             else
2840                return
2841                  Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
2842
2843             end if;
2844
2845          elsif Nkind (Object) = N_Indexed_Component
2846            or else Nkind (Object) = N_Slice
2847          then
2848             return Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
2849          end if;
2850       end if;
2851
2852       return False;
2853    end Is_Dependent_Component_Of_Mutable_Object;
2854
2855    --------------
2856    -- Is_False --
2857    --------------
2858
2859    function Is_False (U : Uint) return Boolean is
2860    begin
2861       return (U = 0);
2862    end Is_False;
2863
2864    ---------------------------
2865    -- Is_Fixed_Model_Number --
2866    ---------------------------
2867
2868    function Is_Fixed_Model_Number (U : Ureal; T : Entity_Id) return Boolean is
2869       S : constant Ureal := Small_Value (T);
2870       M : Urealp.Save_Mark;
2871       R : Boolean;
2872
2873    begin
2874       M := Urealp.Mark;
2875       R := (U = UR_Trunc (U / S) * S);
2876       Urealp.Release (M);
2877       return R;
2878    end Is_Fixed_Model_Number;
2879
2880    -------------------------------
2881    -- Is_Fully_Initialized_Type --
2882    -------------------------------
2883
2884    function Is_Fully_Initialized_Type (Typ : Entity_Id) return Boolean is
2885    begin
2886       if Is_Scalar_Type (Typ) then
2887          return False;
2888
2889       elsif Is_Access_Type (Typ) then
2890          return True;
2891
2892       elsif Is_Array_Type (Typ) then
2893          if Is_Fully_Initialized_Type (Component_Type (Typ)) then
2894             return True;
2895          end if;
2896
2897          --  An interesting case, if we have a constrained type one of whose
2898          --  bounds is known to be null, then there are no elements to be
2899          --  initialized, so all the elements are initialized!
2900
2901          if Is_Constrained (Typ) then
2902             declare
2903                Indx     : Node_Id;
2904                Indx_Typ : Entity_Id;
2905                Lbd, Hbd : Node_Id;
2906
2907             begin
2908                Indx := First_Index (Typ);
2909                while Present (Indx) loop
2910
2911                   if Etype (Indx) = Any_Type then
2912                      return False;
2913
2914                   --  If index is a range, use directly.
2915
2916                   elsif Nkind (Indx) = N_Range then
2917                      Lbd := Low_Bound  (Indx);
2918                      Hbd := High_Bound (Indx);
2919
2920                   else
2921                      Indx_Typ := Etype (Indx);
2922
2923                      if Is_Private_Type (Indx_Typ)  then
2924                         Indx_Typ := Full_View (Indx_Typ);
2925                      end if;
2926
2927                      if No (Indx_Typ) then
2928                         return False;
2929                      else
2930                         Lbd := Type_Low_Bound  (Indx_Typ);
2931                         Hbd := Type_High_Bound (Indx_Typ);
2932                      end if;
2933                   end if;
2934
2935                   if Compile_Time_Known_Value (Lbd)
2936                     and then Compile_Time_Known_Value (Hbd)
2937                   then
2938                      if Expr_Value (Hbd) < Expr_Value (Lbd) then
2939                         return True;
2940                      end if;
2941                   end if;
2942
2943                   Next_Index (Indx);
2944                end loop;
2945             end;
2946          end if;
2947
2948          return False;
2949
2950       elsif Is_Record_Type (Typ) then
2951          declare
2952             Ent : Entity_Id;
2953
2954          begin
2955             Ent := First_Entity (Typ);
2956
2957             while Present (Ent) loop
2958                if Ekind (Ent) = E_Component
2959                  and then (No (Parent (Ent))
2960                              or else No (Expression (Parent (Ent))))
2961                  and then not Is_Fully_Initialized_Type (Etype (Ent))
2962                then
2963                   return False;
2964                end if;
2965
2966                Next_Entity (Ent);
2967             end loop;
2968          end;
2969
2970          return True;
2971
2972       elsif Is_Concurrent_Type (Typ) then
2973          return True;
2974
2975       elsif Is_Private_Type (Typ) then
2976          declare
2977             U : constant Entity_Id := Underlying_Type (Typ);
2978
2979          begin
2980             if No (U) then
2981                return False;
2982             else
2983                return Is_Fully_Initialized_Type (U);
2984             end if;
2985          end;
2986
2987       else
2988          return False;
2989       end if;
2990    end Is_Fully_Initialized_Type;
2991
2992    ----------------------------
2993    -- Is_Inherited_Operation --
2994    ----------------------------
2995
2996    function Is_Inherited_Operation (E : Entity_Id) return Boolean is
2997       Kind : constant Node_Kind := Nkind (Parent (E));
2998
2999    begin
3000       pragma Assert (Is_Overloadable (E));
3001       return Kind = N_Full_Type_Declaration
3002         or else Kind = N_Private_Extension_Declaration
3003         or else Kind = N_Subtype_Declaration
3004         or else (Ekind (E) = E_Enumeration_Literal
3005                   and then Is_Derived_Type (Etype (E)));
3006    end Is_Inherited_Operation;
3007
3008    -----------------------------
3009    -- Is_Library_Level_Entity --
3010    -----------------------------
3011
3012    function Is_Library_Level_Entity (E : Entity_Id) return Boolean is
3013    begin
3014       return Enclosing_Dynamic_Scope (E) = Standard_Standard;
3015    end Is_Library_Level_Entity;
3016
3017    ---------------------------------
3018    -- Is_Local_Variable_Reference --
3019    ---------------------------------
3020
3021    function Is_Local_Variable_Reference (Expr : Node_Id) return Boolean is
3022    begin
3023       if not Is_Entity_Name (Expr) then
3024          return False;
3025
3026       else
3027          declare
3028             Ent : constant Entity_Id := Entity (Expr);
3029             Sub : constant Entity_Id := Enclosing_Subprogram (Ent);
3030
3031          begin
3032             if Ekind (Ent) /= E_Variable
3033                  and then
3034                Ekind (Ent) /= E_In_Out_Parameter
3035             then
3036                return False;
3037
3038             else
3039                return Present (Sub) and then Sub = Current_Subprogram;
3040             end if;
3041          end;
3042       end if;
3043    end Is_Local_Variable_Reference;
3044
3045    -------------------------
3046    -- Is_Object_Reference --
3047    -------------------------
3048
3049    function Is_Object_Reference (N : Node_Id) return Boolean is
3050    begin
3051       if Is_Entity_Name (N) then
3052          return Is_Object (Entity (N));
3053
3054       else
3055          case Nkind (N) is
3056             when N_Indexed_Component | N_Slice =>
3057                return True;
3058
3059             --  In Ada95, a function call is a constant object.
3060
3061             when N_Function_Call =>
3062                return True;
3063
3064             when N_Selected_Component =>
3065                return Is_Object_Reference (Selector_Name (N));
3066
3067             when N_Explicit_Dereference =>
3068                return True;
3069
3070             --  An unchecked type conversion is considered to be an object if
3071             --  the operand is an object (this construction arises only as a
3072             --  result of expansion activities).
3073
3074             when N_Unchecked_Type_Conversion =>
3075                return True;
3076
3077             when others =>
3078                return False;
3079          end case;
3080       end if;
3081    end Is_Object_Reference;
3082
3083    -----------------------------------
3084    -- Is_OK_Variable_For_Out_Formal --
3085    -----------------------------------
3086
3087    function Is_OK_Variable_For_Out_Formal (AV : Node_Id) return Boolean is
3088    begin
3089       Note_Possible_Modification (AV);
3090
3091       --  We must reject parenthesized variable names. The check for
3092       --  Comes_From_Source is present because there are currently
3093       --  cases where the compiler violates this rule (e.g. passing
3094       --  a task object to its controlled Initialize routine).
3095
3096       if Paren_Count (AV) > 0 and then Comes_From_Source (AV) then
3097          return False;
3098
3099       --  A variable is always allowed
3100
3101       elsif Is_Variable (AV) then
3102          return True;
3103
3104       --  Unchecked conversions are allowed only if they come from the
3105       --  generated code, which sometimes uses unchecked conversions for
3106       --  out parameters in cases where code generation is unaffected.
3107       --  We tell source unchecked conversions by seeing if they are
3108       --  rewrites of an original UC function call, or of an explicit
3109       --  conversion of a function call.
3110
3111       elsif Nkind (AV) = N_Unchecked_Type_Conversion then
3112          if Nkind (Original_Node (AV)) = N_Function_Call then
3113             return False;
3114
3115          elsif Comes_From_Source (AV)
3116            and then Nkind (Original_Node (Expression (AV))) = N_Function_Call
3117          then
3118             return False;
3119
3120          else
3121             return True;
3122          end if;
3123
3124       --  Normal type conversions are allowed if argument is a variable
3125
3126       elsif Nkind (AV) = N_Type_Conversion then
3127          if Is_Variable (Expression (AV))
3128            and then Paren_Count (Expression (AV)) = 0
3129          then
3130             Note_Possible_Modification (Expression (AV));
3131             return True;
3132
3133          --  We also allow a non-parenthesized expression that raises
3134          --  constraint error if it rewrites what used to be a variable
3135
3136          elsif Raises_Constraint_Error (Expression (AV))
3137             and then Paren_Count (Expression (AV)) = 0
3138             and then Is_Variable (Original_Node (Expression (AV)))
3139          then
3140             return True;
3141
3142          --  Type conversion of something other than a variable
3143
3144          else
3145             return False;
3146          end if;
3147
3148       --  If this node is rewritten, then test the original form, if that is
3149       --  OK, then we consider the rewritten node OK (for example, if the
3150       --  original node is a conversion, then Is_Variable will not be true
3151       --  but we still want to allow the conversion if it converts a variable.
3152
3153       elsif Original_Node (AV) /= AV then
3154          return Is_OK_Variable_For_Out_Formal (Original_Node (AV));
3155
3156       --  All other non-variables are rejected
3157
3158       else
3159          return False;
3160       end if;
3161    end Is_OK_Variable_For_Out_Formal;
3162
3163    -----------------------------
3164    -- Is_RCI_Pkg_Spec_Or_Body --
3165    -----------------------------
3166
3167    function Is_RCI_Pkg_Spec_Or_Body (Cunit : Node_Id) return Boolean is
3168
3169       function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean;
3170       --  Return True if the unit of Cunit is an RCI package declaration
3171
3172       ---------------------------
3173       -- Is_RCI_Pkg_Decl_Cunit --
3174       ---------------------------
3175
3176       function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean is
3177          The_Unit : constant Node_Id := Unit (Cunit);
3178
3179       begin
3180          if Nkind (The_Unit) /= N_Package_Declaration then
3181             return False;
3182          end if;
3183          return Is_Remote_Call_Interface (Defining_Entity (The_Unit));
3184       end Is_RCI_Pkg_Decl_Cunit;
3185
3186    --  Start of processing for Is_RCI_Pkg_Spec_Or_Body
3187
3188    begin
3189       return Is_RCI_Pkg_Decl_Cunit (Cunit)
3190         or else
3191          (Nkind (Unit (Cunit)) = N_Package_Body
3192            and then Is_RCI_Pkg_Decl_Cunit (Library_Unit (Cunit)));
3193    end Is_RCI_Pkg_Spec_Or_Body;
3194
3195    -----------------------------------------
3196    -- Is_Remote_Access_To_Class_Wide_Type --
3197    -----------------------------------------
3198
3199    function Is_Remote_Access_To_Class_Wide_Type
3200      (E    : Entity_Id)
3201       return Boolean
3202    is
3203       D : Entity_Id;
3204
3205       function Comes_From_Limited_Private_Type_Declaration
3206         (E    : Entity_Id)
3207          return Boolean;
3208       --  Check if the original declaration is a limited private one and
3209       --  if all the derivations have been using private extensions.
3210
3211       -------------------------------------------------
3212       -- Comes_From_Limited_Private_Type_Declaration --
3213       -------------------------------------------------
3214
3215       function Comes_From_Limited_Private_Type_Declaration (E : in Entity_Id)
3216         return Boolean
3217       is
3218          N : constant Node_Id := Declaration_Node (E);
3219       begin
3220          if Nkind (N) = N_Private_Type_Declaration
3221            and then Limited_Present (N)
3222          then
3223             return True;
3224          end if;
3225
3226          if Nkind (N) = N_Private_Extension_Declaration then
3227             return Comes_From_Limited_Private_Type_Declaration (Etype (E));
3228          end if;
3229
3230          return False;
3231       end Comes_From_Limited_Private_Type_Declaration;
3232
3233    --  Start of processing for Is_Remote_Access_To_Class_Wide_Type
3234
3235    begin
3236       if not (Is_Remote_Call_Interface (E)
3237                or else Is_Remote_Types (E))
3238         or else Ekind (E) /= E_General_Access_Type
3239       then
3240          return False;
3241       end if;
3242
3243       D := Designated_Type (E);
3244
3245       if Ekind (D) /= E_Class_Wide_Type then
3246          return False;
3247       end if;
3248
3249       return Comes_From_Limited_Private_Type_Declaration
3250                (Defining_Identifier (Parent (D)));
3251    end Is_Remote_Access_To_Class_Wide_Type;
3252
3253    -----------------------------------------
3254    -- Is_Remote_Access_To_Subprogram_Type --
3255    -----------------------------------------
3256
3257    function Is_Remote_Access_To_Subprogram_Type
3258      (E    : Entity_Id)
3259       return Boolean
3260    is
3261    begin
3262       return (Ekind (E) = E_Access_Subprogram_Type
3263                 or else (Ekind (E) = E_Record_Type
3264                            and then Present (Corresponding_Remote_Type (E))))
3265         and then (Is_Remote_Call_Interface (E)
3266                    or else Is_Remote_Types (E));
3267    end Is_Remote_Access_To_Subprogram_Type;
3268
3269    --------------------
3270    -- Is_Remote_Call --
3271    --------------------
3272
3273    function Is_Remote_Call (N : Node_Id) return Boolean is
3274    begin
3275       if Nkind (N) /= N_Procedure_Call_Statement
3276         and then Nkind (N) /= N_Function_Call
3277       then
3278          --  An entry call cannot be remote
3279
3280          return False;
3281
3282       elsif Nkind (Name (N)) in N_Has_Entity
3283         and then Is_Remote_Call_Interface (Entity (Name (N)))
3284       then
3285          --  A subprogram declared in the spec of a RCI package is remote
3286
3287          return True;
3288
3289       elsif Nkind (Name (N)) = N_Explicit_Dereference
3290         and then Is_Remote_Access_To_Subprogram_Type
3291           (Etype (Prefix (Name (N))))
3292       then
3293          --  The dereference of a RAS is a remote call
3294
3295          return True;
3296
3297       elsif Present (Controlling_Argument (N))
3298         and then Is_Remote_Access_To_Class_Wide_Type
3299           (Etype (Controlling_Argument (N)))
3300       then
3301          --  Any primitive operation call with a controlling argument of
3302          --  a RACW type is a remote call.
3303
3304          return True;
3305       end if;
3306
3307       --  All other calls are local calls
3308
3309       return False;
3310    end Is_Remote_Call;
3311
3312    ----------------------
3313    -- Is_Selector_Name --
3314    ----------------------
3315
3316    function Is_Selector_Name (N : Node_Id) return Boolean is
3317
3318    begin
3319       if not Is_List_Member (N) then
3320          declare
3321             P : constant Node_Id   := Parent (N);
3322             K : constant Node_Kind := Nkind (P);
3323
3324          begin
3325             return
3326               (K = N_Expanded_Name          or else
3327                K = N_Generic_Association    or else
3328                K = N_Parameter_Association  or else
3329                K = N_Selected_Component)
3330               and then Selector_Name (P) = N;
3331          end;
3332
3333       else
3334          declare
3335             L : constant List_Id := List_Containing (N);
3336             P : constant Node_Id := Parent (L);
3337
3338          begin
3339             return (Nkind (P) = N_Discriminant_Association
3340                      and then Selector_Names (P) = L)
3341               or else
3342                    (Nkind (P) = N_Component_Association
3343                      and then Choices (P) = L);
3344          end;
3345       end if;
3346    end Is_Selector_Name;
3347
3348    ------------------
3349    -- Is_Statement --
3350    ------------------
3351
3352    function Is_Statement (N : Node_Id) return Boolean is
3353    begin
3354       return
3355         Nkind (N) in N_Statement_Other_Than_Procedure_Call
3356           or else Nkind (N) = N_Procedure_Call_Statement;
3357    end Is_Statement;
3358
3359    -----------------
3360    -- Is_Transfer --
3361    -----------------
3362
3363    function Is_Transfer (N : Node_Id) return Boolean is
3364       Kind : constant Node_Kind := Nkind (N);
3365
3366    begin
3367       if Kind = N_Return_Statement
3368            or else
3369          Kind = N_Goto_Statement
3370            or else
3371          Kind = N_Raise_Statement
3372            or else
3373          Kind = N_Requeue_Statement
3374       then
3375          return True;
3376
3377       elsif (Kind = N_Exit_Statement or else Kind in N_Raise_xxx_Error)
3378         and then No (Condition (N))
3379       then
3380          return True;
3381
3382       elsif Kind = N_Procedure_Call_Statement
3383         and then Is_Entity_Name (Name (N))
3384         and then Present (Entity (Name (N)))
3385         and then No_Return (Entity (Name (N)))
3386       then
3387          return True;
3388
3389       elsif Nkind (Original_Node (N)) = N_Raise_Statement then
3390          return True;
3391
3392       else
3393          return False;
3394       end if;
3395    end Is_Transfer;
3396
3397    -------------
3398    -- Is_True --
3399    -------------
3400
3401    function Is_True (U : Uint) return Boolean is
3402    begin
3403       return (U /= 0);
3404    end Is_True;
3405
3406    -----------------
3407    -- Is_Variable --
3408    -----------------
3409
3410    function Is_Variable (N : Node_Id) return Boolean is
3411
3412       Orig_Node : constant Node_Id := Original_Node (N);
3413       --  We do the test on the original node, since this is basically a
3414       --  test of syntactic categories, so it must not be disturbed by
3415       --  whatever rewriting might have occurred. For example, an aggregate,
3416       --  which is certainly NOT a variable, could be turned into a variable
3417       --  by expansion.
3418
3419       function In_Protected_Function (E : Entity_Id) return Boolean;
3420       --  Within a protected function, the private components of the
3421       --  enclosing protected type are constants. A function nested within
3422       --  a (protected) procedure is not itself protected.
3423
3424       function Is_Variable_Prefix (P : Node_Id) return Boolean;
3425       --  Prefixes can involve implicit dereferences, in which case we
3426       --  must test for the case of a reference of a constant access
3427       --  type, which can never be a variable.
3428
3429       function In_Protected_Function (E : Entity_Id) return Boolean is
3430          Prot : constant Entity_Id := Scope (E);
3431          S    : Entity_Id;
3432
3433       begin
3434          if not Is_Protected_Type (Prot) then
3435             return False;
3436          else
3437             S := Current_Scope;
3438
3439             while Present (S) and then S /= Prot loop
3440
3441                if Ekind (S) = E_Function
3442                  and then Scope (S) = Prot
3443                then
3444                   return True;
3445                end if;
3446
3447                S := Scope (S);
3448             end loop;
3449
3450             return False;
3451          end if;
3452       end In_Protected_Function;
3453
3454       function Is_Variable_Prefix (P : Node_Id) return Boolean is
3455       begin
3456          if Is_Access_Type (Etype (P)) then
3457             return not Is_Access_Constant (Root_Type (Etype (P)));
3458          else
3459             return Is_Variable (P);
3460          end if;
3461       end Is_Variable_Prefix;
3462
3463    --  Start of processing for Is_Variable
3464
3465    begin
3466       --  Definitely OK if Assignment_OK is set. Since this is something that
3467       --  only gets set for expanded nodes, the test is on N, not Orig_Node.
3468
3469       if Nkind (N) in N_Subexpr and then Assignment_OK (N) then
3470          return True;
3471
3472       --  Normally we go to the original node, but there is one exception
3473       --  where we use the rewritten node, namely when it is an explicit
3474       --  dereference. The generated code may rewrite a prefix which is an
3475       --  access type with an explicit dereference. The dereference is a
3476       --  variable, even though the original node may not be (since it could
3477       --  be a constant of the access type).
3478
3479       elsif Nkind (N) = N_Explicit_Dereference
3480         and then Nkind (Orig_Node) /= N_Explicit_Dereference
3481         and then Is_Access_Type (Etype (Orig_Node))
3482       then
3483          return Is_Variable_Prefix (Original_Node (Prefix (N)));
3484
3485       --  All remaining checks use the original node
3486
3487       elsif Is_Entity_Name (Orig_Node) then
3488          declare
3489             E : constant Entity_Id := Entity (Orig_Node);
3490             K : constant Entity_Kind := Ekind (E);
3491
3492          begin
3493             return (K = E_Variable
3494                       and then Nkind (Parent (E)) /= N_Exception_Handler)
3495               or else  (K = E_Component
3496                           and then not In_Protected_Function (E))
3497               or else  K = E_Out_Parameter
3498               or else  K = E_In_Out_Parameter
3499               or else  K = E_Generic_In_Out_Parameter
3500
3501                --  Current instance of type:
3502
3503               or else (Is_Type (E) and then In_Open_Scopes (E))
3504               or else (Is_Incomplete_Or_Private_Type (E)
3505                         and then In_Open_Scopes (Full_View (E)));
3506          end;
3507
3508       else
3509          case Nkind (Orig_Node) is
3510             when N_Indexed_Component | N_Slice =>
3511                return Is_Variable_Prefix (Prefix (Orig_Node));
3512
3513             when N_Selected_Component =>
3514                return Is_Variable_Prefix (Prefix (Orig_Node))
3515                  and then Is_Variable (Selector_Name (Orig_Node));
3516
3517             --  For an explicit dereference, we must check whether the type
3518             --  is ACCESS CONSTANT, since if it is, then it is not a variable.
3519
3520             when N_Explicit_Dereference =>
3521                return Is_Access_Type (Etype (Prefix (Orig_Node)))
3522                  and then not
3523                    Is_Access_Constant (Root_Type (Etype (Prefix (Orig_Node))));
3524
3525             --  The type conversion is the case where we do not deal with the
3526             --  context dependent special case of an actual parameter. Thus
3527             --  the type conversion is only considered a variable for the
3528             --  purposes of this routine if the target type is tagged. However,
3529             --  a type conversion is considered to be a variable if it does not
3530             --  come from source (this deals for example with the conversions
3531             --  of expressions to their actual subtypes).
3532
3533             when N_Type_Conversion =>
3534                return Is_Variable (Expression (Orig_Node))
3535                  and then
3536                    (not Comes_From_Source (Orig_Node)
3537                       or else
3538                         (Is_Tagged_Type (Etype (Subtype_Mark (Orig_Node)))
3539                           and then
3540                          Is_Tagged_Type (Etype (Expression (Orig_Node)))));
3541
3542             --  GNAT allows an unchecked type conversion as a variable. This
3543             --  only affects the generation of internal expanded code, since
3544             --  calls to instantiations of Unchecked_Conversion are never
3545             --  considered variables (since they are function calls).
3546             --  This is also true for expression actions.
3547
3548             when N_Unchecked_Type_Conversion =>
3549                return Is_Variable (Expression (Orig_Node));
3550
3551             when others =>
3552                return False;
3553          end case;
3554       end if;
3555    end Is_Variable;
3556
3557    ------------------------
3558    -- Is_Volatile_Object --
3559    ------------------------
3560
3561    function Is_Volatile_Object (N : Node_Id) return Boolean is
3562
3563       function Object_Has_Volatile_Components (N : Node_Id) return Boolean;
3564       --  Determines if given object has volatile components
3565
3566       function Is_Volatile_Prefix (N : Node_Id) return Boolean;
3567       --  If prefix is an implicit dereference, examine designated type.
3568
3569       function Is_Volatile_Prefix (N : Node_Id) return Boolean is
3570       begin
3571          if Is_Access_Type (Etype (N)) then
3572             return Has_Volatile_Components (Designated_Type (Etype (N)));
3573          else
3574             return Object_Has_Volatile_Components (N);
3575          end if;
3576       end Is_Volatile_Prefix;
3577
3578       function Object_Has_Volatile_Components (N : Node_Id) return Boolean is
3579       begin
3580          if Is_Volatile (Etype (N))
3581            or else Has_Volatile_Components (Etype (N))
3582          then
3583             return True;
3584
3585          elsif Is_Entity_Name (N)
3586            and then (Has_Volatile_Components (Entity (N))
3587                       or else Is_Volatile (Entity (N)))
3588          then
3589             return True;
3590
3591          elsif Nkind (N) = N_Indexed_Component
3592            or else Nkind (N) = N_Selected_Component
3593          then
3594             return Is_Volatile_Prefix (Prefix (N));
3595
3596          else
3597             return False;
3598          end if;
3599       end Object_Has_Volatile_Components;
3600
3601    --  Start of processing for Is_Volatile_Object
3602
3603    begin
3604       if Is_Volatile (Etype (N))
3605         or else (Is_Entity_Name (N) and then Is_Volatile (Entity (N)))
3606       then
3607          return True;
3608
3609       elsif Nkind (N) = N_Indexed_Component
3610         or else Nkind (N) = N_Selected_Component
3611       then
3612          return Is_Volatile_Prefix (Prefix (N));
3613
3614       else
3615          return False;
3616       end if;
3617    end Is_Volatile_Object;
3618
3619    --------------------------
3620    -- Kill_Size_Check_Code --
3621    --------------------------
3622
3623    procedure Kill_Size_Check_Code (E : Entity_Id) is
3624    begin
3625       if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
3626         and then Present (Size_Check_Code (E))
3627       then
3628          Remove (Size_Check_Code (E));
3629          Set_Size_Check_Code (E, Empty);
3630       end if;
3631    end Kill_Size_Check_Code;
3632
3633    -------------------------
3634    -- New_External_Entity --
3635    -------------------------
3636
3637    function New_External_Entity
3638      (Kind         : Entity_Kind;
3639       Scope_Id     : Entity_Id;
3640       Sloc_Value   : Source_Ptr;
3641       Related_Id   : Entity_Id;
3642       Suffix       : Character;
3643       Suffix_Index : Nat := 0;
3644       Prefix       : Character := ' ')
3645       return         Entity_Id
3646    is
3647       N : constant Entity_Id :=
3648             Make_Defining_Identifier (Sloc_Value,
3649               New_External_Name
3650                 (Chars (Related_Id), Suffix, Suffix_Index, Prefix));
3651
3652    begin
3653       Set_Ekind          (N, Kind);
3654       Set_Is_Internal    (N, True);
3655       Append_Entity      (N, Scope_Id);
3656       Set_Public_Status  (N);
3657
3658       if Kind in Type_Kind then
3659          Init_Size_Align (N);
3660       end if;
3661
3662       return N;
3663    end New_External_Entity;
3664
3665    -------------------------
3666    -- New_Internal_Entity --
3667    -------------------------
3668
3669    function New_Internal_Entity
3670      (Kind       : Entity_Kind;
3671       Scope_Id   : Entity_Id;
3672       Sloc_Value : Source_Ptr;
3673       Id_Char    : Character)
3674       return       Entity_Id
3675    is
3676       N : constant Entity_Id :=
3677             Make_Defining_Identifier (Sloc_Value, New_Internal_Name (Id_Char));
3678
3679    begin
3680       Set_Ekind          (N, Kind);
3681       Set_Is_Internal    (N, True);
3682       Append_Entity      (N, Scope_Id);
3683
3684       if Kind in Type_Kind then
3685          Init_Size_Align (N);
3686       end if;
3687
3688       return N;
3689    end New_Internal_Entity;
3690
3691    -----------------
3692    -- Next_Actual --
3693    -----------------
3694
3695    function Next_Actual (Actual_Id : Node_Id) return Node_Id is
3696       N  : Node_Id;
3697
3698    begin
3699       --  If we are pointing at a positional parameter, it is a member of
3700       --  a node list (the list of parameters), and the next parameter
3701       --  is the next node on the list, unless we hit a parameter
3702       --  association, in which case we shift to using the chain whose
3703       --  head is the First_Named_Actual in the parent, and then is
3704       --  threaded using the Next_Named_Actual of the Parameter_Association.
3705       --  All this fiddling is because the original node list is in the
3706       --  textual call order, and what we need is the declaration order.
3707
3708       if Is_List_Member (Actual_Id) then
3709          N := Next (Actual_Id);
3710
3711          if Nkind (N) = N_Parameter_Association then
3712             return First_Named_Actual (Parent (Actual_Id));
3713          else
3714             return N;
3715          end if;
3716
3717       else
3718          return Next_Named_Actual (Parent (Actual_Id));
3719       end if;
3720    end Next_Actual;
3721
3722    procedure Next_Actual (Actual_Id : in out Node_Id) is
3723    begin
3724       Actual_Id := Next_Actual (Actual_Id);
3725    end Next_Actual;
3726
3727    -----------------------
3728    -- Normalize_Actuals --
3729    -----------------------
3730
3731    --  Chain actuals according to formals of subprogram. If there are
3732    --  no named associations, the chain is simply the list of Parameter
3733    --  Associations, since the order is the same as the declaration order.
3734    --  If there are named associations, then the First_Named_Actual field
3735    --  in the N_Procedure_Call_Statement node or N_Function_Call node
3736    --  points to the Parameter_Association node for the parameter that
3737    --  comes first in declaration order. The remaining named parameters
3738    --  are then chained in declaration order using Next_Named_Actual.
3739
3740    --  This routine also verifies that the number of actuals is compatible
3741    --  with the number and default values of formals, but performs no type
3742    --  checking (type checking is done by the caller).
3743
3744    --  If the matching succeeds, Success is set to True, and the caller
3745    --  proceeds with type-checking. If the match is unsuccessful, then
3746    --  Success is set to False, and the caller attempts a different
3747    --  interpretation, if there is one.
3748
3749    --  If the flag Report is on, the call is not overloaded, and a failure
3750    --  to match can be reported here, rather than in the caller.
3751
3752    procedure Normalize_Actuals
3753      (N       : Node_Id;
3754       S       : Entity_Id;
3755       Report  : Boolean;
3756       Success : out Boolean)
3757    is
3758       Actuals     : constant List_Id := Parameter_Associations (N);
3759       Actual      : Node_Id   := Empty;
3760       Formal      : Entity_Id;
3761       Last        : Node_Id := Empty;
3762       First_Named : Node_Id := Empty;
3763       Found       : Boolean;
3764
3765       Formals_To_Match : Integer := 0;
3766       Actuals_To_Match : Integer := 0;
3767
3768       procedure Chain (A : Node_Id);
3769       --  Add named actual at the proper place in the list, using the
3770       --  Next_Named_Actual link.
3771
3772       function Reporting return Boolean;
3773       --  Determines if an error is to be reported. To report an error, we
3774       --  need Report to be True, and also we do not report errors caused
3775       --  by calls to Init_Proc's that occur within other Init_Proc's. Such
3776       --  errors must always be cascaded errors, since if all the types are
3777       --  declared correctly, the compiler will certainly build decent calls!
3778
3779       procedure Chain (A : Node_Id) is
3780       begin
3781          if No (Last) then
3782
3783             --  Call node points to first actual in list.
3784
3785             Set_First_Named_Actual (N, Explicit_Actual_Parameter (A));
3786
3787          else
3788             Set_Next_Named_Actual (Last, Explicit_Actual_Parameter (A));
3789          end if;
3790
3791          Last := A;
3792          Set_Next_Named_Actual (Last, Empty);
3793       end Chain;
3794
3795       function Reporting return Boolean is
3796       begin
3797          if not Report then
3798             return False;
3799
3800          elsif not Within_Init_Proc then
3801             return True;
3802
3803          elsif Chars (Entity (Name (N))) = Name_uInit_Proc then
3804             return False;
3805
3806          else
3807             return True;
3808          end if;
3809       end Reporting;
3810
3811    --  Start of processing for Normalize_Actuals
3812
3813    begin
3814       if Is_Access_Type (S) then
3815
3816          --  The name in the call is a function call that returns an access
3817          --  to subprogram. The designated type has the list of formals.
3818
3819          Formal := First_Formal (Designated_Type (S));
3820       else
3821          Formal := First_Formal (S);
3822       end if;
3823
3824       while Present (Formal) loop
3825          Formals_To_Match := Formals_To_Match + 1;
3826          Next_Formal (Formal);
3827       end loop;
3828
3829       --  Find if there is a named association, and verify that no positional
3830       --  associations appear after named ones.
3831
3832       if Present (Actuals) then
3833          Actual := First (Actuals);
3834       end if;
3835
3836       while Present (Actual)
3837         and then Nkind (Actual) /= N_Parameter_Association
3838       loop
3839          Actuals_To_Match := Actuals_To_Match + 1;
3840          Next (Actual);
3841       end loop;
3842
3843       if No (Actual) and Actuals_To_Match = Formals_To_Match then
3844
3845          --  Most common case: positional notation, no defaults
3846
3847          Success := True;
3848          return;
3849
3850       elsif Actuals_To_Match > Formals_To_Match then
3851
3852          --  Too many actuals: will not work.
3853
3854          if Reporting then
3855             Error_Msg_N ("too many arguments in call", N);
3856          end if;
3857
3858          Success := False;
3859          return;
3860       end if;
3861
3862       First_Named := Actual;
3863
3864       while Present (Actual) loop
3865          if Nkind (Actual) /= N_Parameter_Association then
3866             Error_Msg_N
3867               ("positional parameters not allowed after named ones", Actual);
3868             Success := False;
3869             return;
3870
3871          else
3872             Actuals_To_Match := Actuals_To_Match + 1;
3873          end if;
3874
3875          Next (Actual);
3876       end loop;
3877
3878       if Present (Actuals) then
3879          Actual := First (Actuals);
3880       end if;
3881
3882       Formal := First_Formal (S);
3883
3884       while Present (Formal) loop
3885
3886          --  Match the formals in order. If the corresponding actual
3887          --  is positional,  nothing to do. Else scan the list of named
3888          --  actuals to find the one with the right name.
3889
3890          if Present (Actual)
3891            and then Nkind (Actual) /= N_Parameter_Association
3892          then
3893             Next (Actual);
3894             Actuals_To_Match := Actuals_To_Match - 1;
3895             Formals_To_Match := Formals_To_Match - 1;
3896
3897          else
3898             --  For named parameters, search the list of actuals to find
3899             --  one that matches the next formal name.
3900
3901             Actual := First_Named;
3902             Found  := False;
3903
3904             while Present (Actual) loop
3905                if Chars (Selector_Name (Actual)) = Chars (Formal) then
3906                   Found := True;
3907                   Chain (Actual);
3908                   Actuals_To_Match := Actuals_To_Match - 1;
3909                   Formals_To_Match := Formals_To_Match - 1;
3910                   exit;
3911                end if;
3912
3913                Next (Actual);
3914             end loop;
3915
3916             if not Found then
3917                if Ekind (Formal) /= E_In_Parameter
3918                  or else No (Default_Value (Formal))
3919                then
3920                   if Reporting then
3921                      if Comes_From_Source (S)
3922                        and then Is_Overloadable (S)
3923                      then
3924                         Error_Msg_Name_1 := Chars (S);
3925                         Error_Msg_Sloc := Sloc (S);
3926                         Error_Msg_NE
3927                           ("missing argument for parameter & " &
3928                              "in call to % declared #", N, Formal);
3929                      else
3930                         Error_Msg_NE
3931                           ("missing argument for parameter &", N, Formal);
3932                      end if;
3933                   end if;
3934
3935                   Success := False;
3936                   return;
3937
3938                else
3939                   Formals_To_Match := Formals_To_Match - 1;
3940                end if;
3941             end if;
3942          end if;
3943
3944          Next_Formal (Formal);
3945       end loop;
3946
3947       if  Formals_To_Match = 0 and then Actuals_To_Match = 0 then
3948          Success := True;
3949          return;
3950
3951       else
3952          if Reporting then
3953
3954             --  Find some superfluous named actual that did not get
3955             --  attached to the list of associations.
3956
3957             Actual := First (Actuals);
3958
3959             while Present (Actual) loop
3960
3961                if Nkind (Actual) = N_Parameter_Association
3962                  and then Actual /= Last
3963                  and then No (Next_Named_Actual (Actual))
3964                then
3965                   Error_Msg_N ("Unmatched actual in call",  Actual);
3966                   exit;
3967                end if;
3968
3969                Next (Actual);
3970             end loop;
3971          end if;
3972
3973          Success := False;
3974          return;
3975       end if;
3976    end Normalize_Actuals;
3977
3978    --------------------------------
3979    -- Note_Possible_Modification --
3980    --------------------------------
3981
3982    procedure Note_Possible_Modification (N : Node_Id) is
3983       Ent : Entity_Id;
3984       Exp : Node_Id;
3985
3986       procedure Set_Ref (E : Entity_Id; N : Node_Id);
3987       --  Internal routine to note modification on entity E by node N
3988
3989       procedure Set_Ref (E : Entity_Id; N : Node_Id) is
3990       begin
3991          Set_Not_Source_Assigned (E, False);
3992          Set_Is_True_Constant (E, False);
3993          Generate_Reference (E, N, 'm');
3994       end Set_Ref;
3995
3996    --  Start of processing for Note_Possible_Modification
3997
3998    begin
3999       --  Loop to find referenced entity, if there is one
4000
4001       Exp := N;
4002       loop
4003          --  Test for node rewritten as dereference (e.g. accept parameter)
4004
4005          if Nkind (Exp) = N_Explicit_Dereference
4006            and then Is_Entity_Name (Original_Node (Exp))
4007          then
4008             Set_Ref (Entity (Original_Node (Exp)), Original_Node (Exp));
4009             return;
4010
4011          elsif Is_Entity_Name (Exp) then
4012             Ent := Entity (Exp);
4013
4014             if (Ekind (Ent) = E_Variable or else Ekind (Ent) = E_Constant)
4015               and then Present (Renamed_Object (Ent))
4016             then
4017                Exp := Renamed_Object (Ent);
4018
4019             else
4020                Set_Ref (Ent, Exp);
4021                return;
4022             end if;
4023
4024          elsif     Nkind (Exp) = N_Type_Conversion
4025            or else Nkind (Exp) = N_Unchecked_Type_Conversion
4026          then
4027             Exp := Expression (Exp);
4028
4029          elsif     Nkind (Exp) = N_Slice
4030            or else Nkind (Exp) = N_Indexed_Component
4031            or else Nkind (Exp) = N_Selected_Component
4032          then
4033             Exp := Prefix (Exp);
4034
4035          else
4036             return;
4037          end if;
4038       end loop;
4039    end Note_Possible_Modification;
4040
4041    -------------------------
4042    -- Object_Access_Level --
4043    -------------------------
4044
4045    function Object_Access_Level (Obj : Node_Id) return Uint is
4046       E : Entity_Id;
4047
4048    --  Returns the static accessibility level of the view denoted
4049    --  by Obj.  Note that the value returned is the result of a
4050    --  call to Scope_Depth.  Only scope depths associated with
4051    --  dynamic scopes can actually be returned.  Since only
4052    --  relative levels matter for accessibility checking, the fact
4053    --  that the distance between successive levels of accessibility
4054    --  is not always one is immaterial (invariant: if level(E2) is
4055    --  deeper than level(E1), then Scope_Depth(E1) < Scope_Depth(E2)).
4056
4057    begin
4058       if Is_Entity_Name (Obj) then
4059          E := Entity (Obj);
4060
4061          --  If E is a type then it denotes a current instance.
4062          --  For this case we add one to the normal accessibility
4063          --  level of the type to ensure that current instances
4064          --  are treated as always being deeper than than the level
4065          --  of any visible named access type (see 3.10.2(21)).
4066
4067          if Is_Type (E) then
4068             return Type_Access_Level (E) +  1;
4069
4070          elsif Present (Renamed_Object (E)) then
4071             return Object_Access_Level (Renamed_Object (E));
4072
4073          --  Similarly, if E is a component of the current instance of a
4074          --  protected type, any instance of it is assumed to be at a deeper
4075          --  level than the type. For a protected object (whose type is an
4076          --  anonymous protected type) its components are at the same level
4077          --  as the type itself.
4078
4079          elsif not Is_Overloadable (E)
4080            and then Ekind (Scope (E)) = E_Protected_Type
4081            and then Comes_From_Source (Scope (E))
4082          then
4083             return Type_Access_Level (Scope (E)) + 1;
4084
4085          else
4086             return Scope_Depth (Enclosing_Dynamic_Scope (E));
4087          end if;
4088
4089       elsif Nkind (Obj) = N_Selected_Component then
4090          if Is_Access_Type (Etype (Prefix (Obj))) then
4091             return Type_Access_Level (Etype (Prefix (Obj)));
4092          else
4093             return Object_Access_Level (Prefix (Obj));
4094          end if;
4095
4096       elsif Nkind (Obj) = N_Indexed_Component then
4097          if Is_Access_Type (Etype (Prefix (Obj))) then
4098             return Type_Access_Level (Etype (Prefix (Obj)));
4099          else
4100             return Object_Access_Level (Prefix (Obj));
4101          end if;
4102
4103       elsif Nkind (Obj) = N_Explicit_Dereference then
4104
4105          --  If the prefix is a selected access discriminant then
4106          --  we make a recursive call on the prefix, which will
4107          --  in turn check the level of the prefix object of
4108          --  the selected discriminant.
4109
4110          if Nkind (Prefix (Obj)) = N_Selected_Component
4111            and then Ekind (Etype (Prefix (Obj))) = E_Anonymous_Access_Type
4112            and then
4113              Ekind (Entity (Selector_Name (Prefix (Obj)))) = E_Discriminant
4114          then
4115             return Object_Access_Level (Prefix (Obj));
4116          else
4117             return Type_Access_Level (Etype (Prefix (Obj)));
4118          end if;
4119
4120       elsif Nkind (Obj) = N_Type_Conversion then
4121          return Object_Access_Level (Expression (Obj));
4122
4123       --  Function results are objects, so we get either the access level
4124       --  of the function or, in the case of an indirect call, the level of
4125       --  of the access-to-subprogram type.
4126
4127       elsif Nkind (Obj) = N_Function_Call then
4128          if Is_Entity_Name (Name (Obj)) then
4129             return Subprogram_Access_Level (Entity (Name (Obj)));
4130          else
4131             return Type_Access_Level (Etype (Prefix (Name (Obj))));
4132          end if;
4133
4134       --  For convenience we handle qualified expressions, even though
4135       --  they aren't technically object names.
4136
4137       elsif Nkind (Obj) = N_Qualified_Expression then
4138          return Object_Access_Level (Expression (Obj));
4139
4140       --  Otherwise return the scope level of Standard.
4141       --  (If there are cases that fall through
4142       --  to this point they will be treated as
4143       --  having global accessibility for now. ???)
4144
4145       else
4146          return Scope_Depth (Standard_Standard);
4147       end if;
4148    end Object_Access_Level;
4149
4150    -----------------------
4151    -- Private_Component --
4152    -----------------------
4153
4154    function Private_Component (Type_Id : Entity_Id) return Entity_Id is
4155       Ancestor  : constant Entity_Id := Base_Type (Type_Id);
4156
4157       function Trace_Components
4158         (T     : Entity_Id;
4159          Check : Boolean)
4160          return  Entity_Id;
4161       --  Recursive function that does the work, and checks against circular
4162       --  definition for each subcomponent type.
4163
4164       ----------------------
4165       -- Trace_Components --
4166       ----------------------
4167
4168       function Trace_Components
4169          (T     : Entity_Id;
4170           Check : Boolean) return Entity_Id
4171        is
4172          Btype     : constant Entity_Id := Base_Type (T);
4173          Component : Entity_Id;
4174          P         : Entity_Id;
4175          Candidate : Entity_Id := Empty;
4176
4177       begin
4178          if Check and then Btype = Ancestor then
4179             Error_Msg_N ("circular type definition", Type_Id);
4180             return Any_Type;
4181          end if;
4182
4183          if Is_Private_Type (Btype)
4184            and then not Is_Generic_Type (Btype)
4185          then
4186             return Btype;
4187
4188          elsif Is_Array_Type (Btype) then
4189             return Trace_Components (Component_Type (Btype), True);
4190
4191          elsif Is_Record_Type (Btype) then
4192             Component := First_Entity (Btype);
4193             while Present (Component) loop
4194
4195                --  skip anonymous types generated by constrained components.
4196
4197                if not Is_Type (Component) then
4198                   P := Trace_Components (Etype (Component), True);
4199
4200                   if Present (P) then
4201                      if P = Any_Type then
4202                         return P;
4203                      else
4204                         Candidate := P;
4205                      end if;
4206                   end if;
4207                end if;
4208
4209                Next_Entity (Component);
4210             end loop;
4211
4212             return Candidate;
4213
4214          else
4215             return Empty;
4216          end if;
4217       end Trace_Components;
4218
4219    --  Start of processing for Private_Component
4220
4221    begin
4222       return Trace_Components (Type_Id, False);
4223    end Private_Component;
4224
4225    -----------------------
4226    -- Process_End_Label --
4227    -----------------------
4228
4229    procedure Process_End_Label (N : Node_Id; Typ  : Character) is
4230       Loc  : Source_Ptr;
4231       Nam  : Node_Id;
4232       Ctyp : Entity_Id;
4233
4234       Label_Ref : Boolean;
4235       --  Set True if reference to end label itself is required
4236
4237       Endl : Node_Id;
4238       --  Gets set to the operator symbol or identifier that references
4239       --  the entity Ent. For the child unit case, this is the identifier
4240       --  from the designator. For other cases, this is simply Endl.
4241
4242       Ent : Entity_Id;
4243       --  This is the entity for the construct to which the End_Label applies
4244
4245       procedure Generate_Parent_Ref (N : Node_Id);
4246       --  N is an identifier node that appears as a parent unit reference
4247       --  in the case where Ent is a child unit. This procedure generates
4248       --  an appropriate cross-reference entry.
4249
4250       procedure Generate_Parent_Ref (N : Node_Id) is
4251          Parent_Ent : Entity_Id;
4252
4253       begin
4254          --  Search up scope stack. The reason we do this is that normal
4255          --  visibility analysis would not work for two reasons. First in
4256          --  some subunit cases, the entry for the parent unit may not be
4257          --  visible, and in any case there can be a local entity that
4258          --  hides the scope entity.
4259
4260          Parent_Ent := Current_Scope;
4261          while Present (Parent_Ent) loop
4262             if Chars (Parent_Ent) = Chars (N) then
4263
4264                --  Generate the reference. We do NOT consider this as a
4265                --  reference for unreferenced symbol purposes, but we do
4266                --  force a cross-reference even if the end line does not
4267                --  come from source (the caller already generated the
4268                --  appropriate Typ for this situation).
4269
4270                Generate_Reference
4271                  (Parent_Ent, N, 'r', Set_Ref => False, Force => True);
4272                Style.Check_Identifier (N, Parent_Ent);
4273                return;
4274             end if;
4275
4276             Parent_Ent := Scope (Parent_Ent);
4277          end loop;
4278
4279          --  Fall through means entity was not found -- that's odd, but
4280          --  the appropriate thing is simply to ignore and not generate
4281          --  any cross-reference for this entry.
4282
4283          return;
4284       end Generate_Parent_Ref;
4285
4286    --  Start of processing for Process_End_Label
4287
4288    begin
4289       --  If no node, ignore. This happens in some error situations,
4290       --  and also for some internally generated structures where no
4291       --  end label references are required in any case.
4292
4293       if No (N) then
4294          return;
4295       end if;
4296
4297       --  Nothing to do if no End_Label, happens for internally generated
4298       --  constructs where we don't want an end label reference anyway.
4299       --  Also nothing to do if Endl is a string literal, which means
4300       --  there was some prior error (bad operator symbol)
4301
4302       Endl := End_Label (N);
4303
4304       if No (Endl) or else Nkind (Endl) = N_String_Literal then
4305          return;
4306       end if;
4307
4308       --  Reference node is not in extended main source unit
4309
4310       if not In_Extended_Main_Source_Unit (N) then
4311
4312          --  Generally we do not collect references except for the
4313          --  extended main source unit. The one exception is the 'e'
4314          --  entry for a package spec, where it is useful for a client
4315          --  to have the ending information to define scopes.
4316
4317          if Typ /= 'e' then
4318             return;
4319
4320          else
4321             Label_Ref := False;
4322
4323             --  For this case, we can ignore any parent references,
4324             --  but we need the package name itself for the 'e' entry.
4325
4326             if Nkind (Endl) = N_Designator then
4327                Endl := Identifier (Endl);
4328             end if;
4329          end if;
4330
4331       --  Reference is in extended main source unit
4332
4333       else
4334          Label_Ref := True;
4335
4336          --  For designator, generate references for the parent entries
4337
4338          if Nkind (Endl) = N_Designator then
4339
4340             --  Generate references for the prefix if the END line comes
4341             --  from source (otherwise we do not need these references)
4342
4343             if Comes_From_Source (Endl) then
4344                Nam := Name (Endl);
4345                while Nkind (Nam) = N_Selected_Component loop
4346                   Generate_Parent_Ref (Selector_Name (Nam));
4347                   Nam := Prefix (Nam);
4348                end loop;
4349
4350                Generate_Parent_Ref (Nam);
4351             end if;
4352
4353             Endl := Identifier (Endl);
4354          end if;
4355       end if;
4356
4357       --  Locate the entity to which the end label applies. Most of the
4358       --  time this is simply the current scope containing the construct.
4359
4360       Ent := Current_Scope;
4361
4362       if Chars (Ent) = Chars (Endl) then
4363          null;
4364
4365       --  But in the case of single tasks and single protected objects,
4366       --  the current scope is the anonymous task or protected type and
4367       --  what we want is the object. There is no direct link so what we
4368       --  do is search ahead in the entity chain for the object with the
4369       --  matching type and name. In practice it is almost certain to be
4370       --  the very next entity on the chain, so this is not inefficient.
4371
4372       else
4373          Ctyp := Etype (Ent);
4374          loop
4375             Next_Entity (Ent);
4376
4377             --  If we don't find the entry we are looking for, that's
4378             --  odd, perhaps results from some error condition? Anyway
4379             --  the appropriate thing is just to abandon the attempt.
4380
4381             if No (Ent) then
4382                return;
4383
4384             --  Exit if we find the entity we are looking for
4385
4386             elsif Etype (Ent) = Ctyp
4387               and then Chars (Ent) = Chars (Endl)
4388             then
4389                exit;
4390             end if;
4391          end loop;
4392       end if;
4393
4394       --  If label was really there, then generate a normal reference
4395       --  and then adjust the location in the end label to point past
4396       --  the name (which should almost always be the semicolon).
4397
4398       Loc := Sloc (Endl);
4399
4400       if Comes_From_Source (Endl) then
4401
4402          --  If a label reference is required, then do the style check
4403          --  and generate a normal cross-reference entry for the label
4404
4405          if Label_Ref then
4406             Style.Check_Identifier (Endl, Ent);
4407             Generate_Reference (Ent, Endl, 'r', Set_Ref => False);
4408          end if;
4409
4410          --  Set the location to point past the label (normally this will
4411          --  mean the semicolon immediately following the label). This is
4412          --  done for the sake of the 'e' or 't' entry generated below.
4413
4414          Get_Decoded_Name_String (Chars (Endl));
4415          Set_Sloc (Endl, Sloc (Endl) + Source_Ptr (Name_Len));
4416       end if;
4417
4418       --  Now generate the e/t reference
4419
4420       Generate_Reference (Ent, Endl, Typ, Set_Ref => False, Force => True);
4421
4422       --  Restore Sloc, in case modified above, since we have an identifier
4423       --  and the normal Sloc should be left set in the tree.
4424
4425       Set_Sloc (Endl, Loc);
4426    end Process_End_Label;
4427
4428    ------------------
4429    -- Real_Convert --
4430    ------------------
4431
4432    --  We do the conversion to get the value of the real string by using
4433    --  the scanner, see Sinput for details on use of the internal source
4434    --  buffer for scanning internal strings.
4435
4436    function Real_Convert (S : String) return Node_Id is
4437       Save_Src : constant Source_Buffer_Ptr := Source;
4438       Negative : Boolean;
4439
4440    begin
4441       Source := Internal_Source_Ptr;
4442       Scan_Ptr := 1;
4443
4444       for J in S'Range loop
4445          Source (Source_Ptr (J)) := S (J);
4446       end loop;
4447
4448       Source (S'Length + 1) := EOF;
4449
4450       if Source (Scan_Ptr) = '-' then
4451          Negative := True;
4452          Scan_Ptr := Scan_Ptr + 1;
4453       else
4454          Negative := False;
4455       end if;
4456
4457       Scan;
4458
4459       if Negative then
4460          Set_Realval (Token_Node, UR_Negate (Realval (Token_Node)));
4461       end if;
4462
4463       Source := Save_Src;
4464       return Token_Node;
4465    end Real_Convert;
4466
4467    ------------------------------
4468    -- Requires_Transient_Scope --
4469    ------------------------------
4470
4471    --  A transient scope is required when variable-sized temporaries are
4472    --  allocated in the primary or secondary stack, or when finalization
4473    --  actions must be generated before the next instruction
4474
4475    function Requires_Transient_Scope (Id : Entity_Id) return Boolean is
4476       Typ : constant Entity_Id := Underlying_Type (Id);
4477
4478    begin
4479       --  This is a private type which is not completed yet. This can only
4480       --  happen in a default expression (of a formal parameter or of a
4481       --  record component). Do not expand transient scope in this case
4482
4483       if No (Typ) then
4484          return False;
4485
4486       elsif Typ = Standard_Void_Type then
4487          return False;
4488
4489       --  The back-end has trouble allocating variable-size temporaries so
4490       --  we generate them in the front-end and need a transient scope to
4491       --  reclaim them properly
4492
4493       elsif not Size_Known_At_Compile_Time (Typ) then
4494          return True;
4495
4496       --  Unconstrained discriminated records always require a variable
4497       --  length temporary, since the length may depend on the variant.
4498
4499       elsif Is_Record_Type (Typ)
4500         and then Has_Discriminants (Typ)
4501         and then not Is_Constrained (Typ)
4502       then
4503          return True;
4504
4505       --  Functions returning tagged types may dispatch on result so their
4506       --  returned value is allocated on the secondary stack. Controlled
4507       --  type temporaries need finalization.
4508
4509       elsif Is_Tagged_Type (Typ)
4510         or else Has_Controlled_Component (Typ)
4511       then
4512          return True;
4513
4514       --  Unconstrained array types are returned on the secondary stack
4515
4516       elsif Is_Array_Type (Typ) then
4517          return not Is_Constrained (Typ);
4518       end if;
4519
4520       return False;
4521    end Requires_Transient_Scope;
4522
4523    --------------------------
4524    -- Reset_Analyzed_Flags --
4525    --------------------------
4526
4527    procedure Reset_Analyzed_Flags (N : Node_Id) is
4528
4529       function Clear_Analyzed
4530         (N    : Node_Id)
4531          return Traverse_Result;
4532       --  Function used to reset Analyzed flags in tree. Note that we do
4533       --  not reset Analyzed flags in entities, since there is no need to
4534       --  renalalyze entities, and indeed, it is wrong to do so, since it
4535       --  can result in generating auxiliary stuff more than once.
4536
4537       function Clear_Analyzed
4538         (N    : Node_Id)
4539          return Traverse_Result
4540       is
4541       begin
4542          if not Has_Extension (N) then
4543             Set_Analyzed (N, False);
4544          end if;
4545
4546          return OK;
4547       end Clear_Analyzed;
4548
4549       function Reset_Analyzed is
4550         new Traverse_Func (Clear_Analyzed);
4551
4552       Discard : Traverse_Result;
4553
4554    --  Start of processing for Reset_Analyzed_Flags
4555
4556    begin
4557       Discard := Reset_Analyzed (N);
4558    end Reset_Analyzed_Flags;
4559
4560    ---------------
4561    -- Same_Name --
4562    ---------------
4563
4564    function Same_Name (N1, N2 : Node_Id) return Boolean is
4565       K1 : constant Node_Kind := Nkind (N1);
4566       K2 : constant Node_Kind := Nkind (N2);
4567
4568    begin
4569       if (K1 = N_Identifier or else K1 = N_Defining_Identifier)
4570         and then (K2 = N_Identifier or else K2 = N_Defining_Identifier)
4571       then
4572          return Chars (N1) = Chars (N2);
4573
4574       elsif (K1 = N_Selected_Component or else K1 = N_Expanded_Name)
4575         and then (K2 = N_Selected_Component or else K2 = N_Expanded_Name)
4576       then
4577          return Same_Name (Selector_Name (N1), Selector_Name (N2))
4578            and then Same_Name (Prefix (N1), Prefix (N2));
4579
4580       else
4581          return False;
4582       end if;
4583    end Same_Name;
4584
4585    ---------------
4586    -- Same_Type --
4587    ---------------
4588
4589    function Same_Type (T1, T2 : Entity_Id) return Boolean is
4590    begin
4591       if T1 = T2 then
4592          return True;
4593
4594       elsif not Is_Constrained (T1)
4595         and then not Is_Constrained (T2)
4596         and then Base_Type (T1) = Base_Type (T2)
4597       then
4598          return True;
4599
4600       --  For now don't bother with case of identical constraints, to be
4601       --  fiddled with later on perhaps (this is only used for optimization
4602       --  purposes, so it is not critical to do a best possible job)
4603
4604       else
4605          return False;
4606       end if;
4607    end Same_Type;
4608
4609    ------------------------
4610    -- Scope_Is_Transient --
4611    ------------------------
4612
4613    function Scope_Is_Transient  return Boolean is
4614    begin
4615       return Scope_Stack.Table (Scope_Stack.Last).Is_Transient;
4616    end Scope_Is_Transient;
4617
4618    ------------------
4619    -- Scope_Within --
4620    ------------------
4621
4622    function Scope_Within (Scope1, Scope2 : Entity_Id) return Boolean is
4623       Scop : Entity_Id;
4624
4625    begin
4626       Scop := Scope1;
4627       while Scop /= Standard_Standard loop
4628          Scop := Scope (Scop);
4629
4630          if Scop = Scope2 then
4631             return True;
4632          end if;
4633       end loop;
4634
4635       return False;
4636    end Scope_Within;
4637
4638    --------------------------
4639    -- Scope_Within_Or_Same --
4640    --------------------------
4641
4642    function Scope_Within_Or_Same (Scope1, Scope2 : Entity_Id) return Boolean is
4643       Scop : Entity_Id;
4644
4645    begin
4646       Scop := Scope1;
4647       while Scop /= Standard_Standard loop
4648          if Scop = Scope2 then
4649             return True;
4650          else
4651             Scop := Scope (Scop);
4652          end if;
4653       end loop;
4654
4655       return False;
4656    end Scope_Within_Or_Same;
4657
4658    ------------------------
4659    -- Set_Current_Entity --
4660    ------------------------
4661
4662    --  The given entity is to be set as the currently visible definition
4663    --  of its associated name (i.e. the Node_Id associated with its name).
4664    --  All we have to do is to get the name from the identifier, and
4665    --  then set the associated Node_Id to point to the given entity.
4666
4667    procedure Set_Current_Entity (E : Entity_Id) is
4668    begin
4669       Set_Name_Entity_Id (Chars (E), E);
4670    end Set_Current_Entity;
4671
4672    ---------------------------------
4673    -- Set_Entity_With_Style_Check --
4674    ---------------------------------
4675
4676    procedure Set_Entity_With_Style_Check (N : Node_Id; Val : Entity_Id) is
4677       Val_Actual : Entity_Id;
4678       Nod        : Node_Id;
4679
4680    begin
4681       Set_Entity (N, Val);
4682
4683       if Style_Check
4684         and then not Suppress_Style_Checks (Val)
4685         and then not In_Instance
4686       then
4687          if Nkind (N) = N_Identifier then
4688             Nod := N;
4689
4690          elsif Nkind (N) = N_Expanded_Name then
4691             Nod := Selector_Name (N);
4692
4693          else
4694             return;
4695          end if;
4696
4697          Val_Actual := Val;
4698
4699          --  A special situation arises for derived operations, where we want
4700          --  to do the check against the parent (since the Sloc of the derived
4701          --  operation points to the derived type declaration itself).
4702
4703          while not Comes_From_Source (Val_Actual)
4704            and then Nkind (Val_Actual) in N_Entity
4705            and then (Ekind (Val_Actual) = E_Enumeration_Literal
4706                       or else Ekind (Val_Actual) = E_Function
4707                       or else Ekind (Val_Actual) = E_Generic_Function
4708                       or else Ekind (Val_Actual) = E_Procedure
4709                       or else Ekind (Val_Actual) = E_Generic_Procedure)
4710            and then Present (Alias (Val_Actual))
4711          loop
4712             Val_Actual := Alias (Val_Actual);
4713          end loop;
4714
4715          --  Renaming declarations for generic actuals do not come from source,
4716          --  and have a different name from that of the entity they rename, so
4717          --  there is no style check to perform here.
4718
4719          if Chars (Nod) = Chars (Val_Actual) then
4720             Style.Check_Identifier (Nod, Val_Actual);
4721          end if;
4722
4723       end if;
4724
4725       Set_Entity (N, Val);
4726    end Set_Entity_With_Style_Check;
4727
4728    ------------------------
4729    -- Set_Name_Entity_Id --
4730    ------------------------
4731
4732    procedure Set_Name_Entity_Id (Id : Name_Id; Val : Entity_Id) is
4733    begin
4734       Set_Name_Table_Info (Id, Int (Val));
4735    end Set_Name_Entity_Id;
4736
4737    ---------------------
4738    -- Set_Next_Actual --
4739    ---------------------
4740
4741    procedure Set_Next_Actual (Ass1_Id : Node_Id; Ass2_Id : Node_Id) is
4742    begin
4743       if Nkind (Parent (Ass1_Id)) = N_Parameter_Association then
4744          Set_First_Named_Actual (Parent (Ass1_Id), Ass2_Id);
4745       end if;
4746    end Set_Next_Actual;
4747
4748    -----------------------
4749    -- Set_Public_Status --
4750    -----------------------
4751
4752    procedure Set_Public_Status (Id : Entity_Id) is
4753       S : constant Entity_Id := Current_Scope;
4754
4755    begin
4756       if S = Standard_Standard
4757         or else (Is_Public (S)
4758                   and then (Ekind (S) = E_Package
4759                              or else Is_Record_Type (S)
4760                              or else Ekind (S) = E_Void))
4761       then
4762          Set_Is_Public (Id);
4763
4764       --  The bounds of an entry family declaration can generate object
4765       --  declarations that are visible to the back-end, e.g. in the
4766       --  the declaration of a composite type that contains tasks.
4767
4768       elsif Is_Public (S)
4769         and then Is_Concurrent_Type (S)
4770         and then not Has_Completion (S)
4771         and then Nkind (Parent (Id)) = N_Object_Declaration
4772       then
4773          Set_Is_Public (Id);
4774       end if;
4775    end Set_Public_Status;
4776
4777    ----------------------------
4778    -- Set_Scope_Is_Transient --
4779    ----------------------------
4780
4781    procedure Set_Scope_Is_Transient (V : Boolean := True) is
4782    begin
4783       Scope_Stack.Table (Scope_Stack.Last).Is_Transient := V;
4784    end Set_Scope_Is_Transient;
4785
4786    -------------------
4787    -- Set_Size_Info --
4788    -------------------
4789
4790    procedure Set_Size_Info (T1, T2 : Entity_Id) is
4791    begin
4792       --  We copy Esize, but not RM_Size, since in general RM_Size is
4793       --  subtype specific and does not get inherited by all subtypes.
4794
4795       Set_Esize                     (T1, Esize                     (T2));
4796       Set_Has_Biased_Representation (T1, Has_Biased_Representation (T2));
4797
4798       if Is_Discrete_Or_Fixed_Point_Type (T1)
4799            and then
4800          Is_Discrete_Or_Fixed_Point_Type (T2)
4801       then
4802          Set_Is_Unsigned_Type       (T1, Is_Unsigned_Type          (T2));
4803       end if;
4804
4805       Set_Alignment                 (T1, Alignment                 (T2));
4806    end Set_Size_Info;
4807
4808    --------------------
4809    -- Static_Integer --
4810    --------------------
4811
4812    function Static_Integer (N : Node_Id) return Uint is
4813    begin
4814       Analyze_And_Resolve (N, Any_Integer);
4815
4816       if N = Error
4817         or else Error_Posted (N)
4818         or else Etype (N) = Any_Type
4819       then
4820          return No_Uint;
4821       end if;
4822
4823       if Is_Static_Expression (N) then
4824          if not Raises_Constraint_Error (N) then
4825             return Expr_Value (N);
4826          else
4827             return No_Uint;
4828          end if;
4829
4830       elsif Etype (N) = Any_Type then
4831          return No_Uint;
4832
4833       else
4834          Error_Msg_N ("static integer expression required here", N);
4835          return No_Uint;
4836       end if;
4837    end Static_Integer;
4838
4839    --------------------------
4840    -- Statically_Different --
4841    --------------------------
4842
4843    function Statically_Different (E1, E2 : Node_Id) return Boolean is
4844       R1 : constant Node_Id := Get_Referenced_Object (E1);
4845       R2 : constant Node_Id := Get_Referenced_Object (E2);
4846
4847    begin
4848       return     Is_Entity_Name (R1)
4849         and then Is_Entity_Name (R2)
4850         and then Entity (R1) /= Entity (R2)
4851         and then not Is_Formal (Entity (R1))
4852         and then not Is_Formal (Entity (R2));
4853    end Statically_Different;
4854
4855    -----------------------------
4856    -- Subprogram_Access_Level --
4857    -----------------------------
4858
4859    function Subprogram_Access_Level (Subp : Entity_Id) return Uint is
4860    begin
4861       if Present (Alias (Subp)) then
4862          return Subprogram_Access_Level (Alias (Subp));
4863       else
4864          return Scope_Depth (Enclosing_Dynamic_Scope (Subp));
4865       end if;
4866    end Subprogram_Access_Level;
4867
4868    -----------------
4869    -- Trace_Scope --
4870    -----------------
4871
4872    procedure Trace_Scope (N : Node_Id; E : Entity_Id; Msg : String) is
4873    begin
4874       if Debug_Flag_W then
4875          for J in 0 .. Scope_Stack.Last loop
4876             Write_Str ("  ");
4877          end loop;
4878
4879          Write_Str (Msg);
4880          Write_Name (Chars (E));
4881          Write_Str ("   line ");
4882          Write_Int (Int (Get_Logical_Line_Number (Sloc (N))));
4883          Write_Eol;
4884       end if;
4885    end Trace_Scope;
4886
4887    -----------------------
4888    -- Transfer_Entities --
4889    -----------------------
4890
4891    procedure Transfer_Entities (From : Entity_Id; To : Entity_Id) is
4892       Ent      : Entity_Id := First_Entity (From);
4893
4894    begin
4895       if No (Ent) then
4896          return;
4897       end if;
4898
4899       if (Last_Entity (To)) = Empty then
4900          Set_First_Entity (To, Ent);
4901       else
4902          Set_Next_Entity (Last_Entity (To), Ent);
4903       end if;
4904
4905       Set_Last_Entity (To, Last_Entity (From));
4906
4907       while Present (Ent) loop
4908          Set_Scope (Ent, To);
4909
4910          if not Is_Public (Ent) then
4911             Set_Public_Status (Ent);
4912
4913             if Is_Public (Ent)
4914               and then Ekind (Ent) = E_Record_Subtype
4915
4916             then
4917                --  The components of the propagated Itype must be public
4918                --  as well.
4919
4920                declare
4921                   Comp : Entity_Id;
4922
4923                begin
4924                   Comp := First_Entity (Ent);
4925
4926                   while Present (Comp) loop
4927                      Set_Is_Public (Comp);
4928                      Next_Entity (Comp);
4929                   end loop;
4930                end;
4931             end if;
4932          end if;
4933
4934          Next_Entity (Ent);
4935       end loop;
4936
4937       Set_First_Entity (From, Empty);
4938       Set_Last_Entity (From, Empty);
4939    end Transfer_Entities;
4940
4941    -----------------------
4942    -- Type_Access_Level --
4943    -----------------------
4944
4945    function Type_Access_Level (Typ : Entity_Id) return Uint is
4946       Btyp : Entity_Id := Base_Type (Typ);
4947
4948    begin
4949       --  If the type is an anonymous access type we treat it as being
4950       --  declared at the library level to ensure that names such as
4951       --  X.all'access don't fail static accessibility checks.
4952
4953       if Ekind (Btyp) in Access_Kind then
4954          if Ekind (Btyp) = E_Anonymous_Access_Type then
4955             return Scope_Depth (Standard_Standard);
4956          end if;
4957
4958          Btyp := Root_Type (Btyp);
4959       end if;
4960
4961       return Scope_Depth (Enclosing_Dynamic_Scope (Btyp));
4962    end Type_Access_Level;
4963
4964    --------------------------
4965    -- Unit_Declaration_Node --
4966    --------------------------
4967
4968    function Unit_Declaration_Node (Unit_Id : Entity_Id) return Node_Id is
4969       N : Node_Id := Parent (Unit_Id);
4970
4971    begin
4972       --  Predefined operators do not have a full function declaration.
4973
4974       if Ekind (Unit_Id) = E_Operator then
4975          return N;
4976       end if;
4977
4978       while Nkind (N) /= N_Abstract_Subprogram_Declaration
4979         and then Nkind (N) /= N_Formal_Package_Declaration
4980         and then Nkind (N) /= N_Formal_Subprogram_Declaration
4981         and then Nkind (N) /= N_Function_Instantiation
4982         and then Nkind (N) /= N_Generic_Package_Declaration
4983         and then Nkind (N) /= N_Generic_Subprogram_Declaration
4984         and then Nkind (N) /= N_Package_Declaration
4985         and then Nkind (N) /= N_Package_Body
4986         and then Nkind (N) /= N_Package_Instantiation
4987         and then Nkind (N) /= N_Package_Renaming_Declaration
4988         and then Nkind (N) /= N_Procedure_Instantiation
4989         and then Nkind (N) /= N_Subprogram_Declaration
4990         and then Nkind (N) /= N_Subprogram_Body
4991         and then Nkind (N) /= N_Subprogram_Body_Stub
4992         and then Nkind (N) /= N_Subprogram_Renaming_Declaration
4993         and then Nkind (N) /= N_Task_Body
4994         and then Nkind (N) /= N_Task_Type_Declaration
4995         and then Nkind (N) not in N_Generic_Renaming_Declaration
4996       loop
4997          N := Parent (N);
4998          pragma Assert (Present (N));
4999       end loop;
5000
5001       return N;
5002    end Unit_Declaration_Node;
5003
5004    ----------------------
5005    -- Within_Init_Proc --
5006    ----------------------
5007
5008    function Within_Init_Proc return Boolean is
5009       S : Entity_Id;
5010
5011    begin
5012       S := Current_Scope;
5013       while not Is_Overloadable (S) loop
5014          if S = Standard_Standard then
5015             return False;
5016          else
5017             S := Scope (S);
5018          end if;
5019       end loop;
5020
5021       return Chars (S) = Name_uInit_Proc;
5022    end Within_Init_Proc;
5023
5024    ----------------
5025    -- Wrong_Type --
5026    ----------------
5027
5028    procedure Wrong_Type (Expr : Node_Id; Expected_Type : Entity_Id) is
5029       Found_Type : constant Entity_Id := First_Subtype (Etype (Expr));
5030       Expec_Type : constant Entity_Id := First_Subtype (Expected_Type);
5031
5032       function Has_One_Matching_Field return Boolean;
5033       --  Determines whether Expec_Type is a record type with a single
5034       --  component or discriminant whose type matches the found type or
5035       --  is a one dimensional array whose component type matches the
5036       --  found type.
5037
5038       function Has_One_Matching_Field return Boolean is
5039          E : Entity_Id;
5040
5041       begin
5042          if Is_Array_Type (Expec_Type)
5043            and then Number_Dimensions (Expec_Type) = 1
5044            and then
5045              Covers (Etype (Component_Type (Expec_Type)), Found_Type)
5046          then
5047             return True;
5048
5049          elsif not Is_Record_Type (Expec_Type) then
5050             return False;
5051
5052          else
5053             E := First_Entity (Expec_Type);
5054
5055             loop
5056                if No (E) then
5057                   return False;
5058
5059                elsif (Ekind (E) /= E_Discriminant
5060                        and then Ekind (E) /= E_Component)
5061                  or else (Chars (E) = Name_uTag
5062                            or else Chars (E) = Name_uParent)
5063                then
5064                   Next_Entity (E);
5065
5066                else
5067                   exit;
5068                end if;
5069             end loop;
5070
5071             if not Covers (Etype (E), Found_Type) then
5072                return False;
5073
5074             elsif Present (Next_Entity (E)) then
5075                return False;
5076
5077             else
5078                return True;
5079             end if;
5080          end if;
5081       end Has_One_Matching_Field;
5082
5083    --  Start of processing for Wrong_Type
5084
5085    begin
5086       --  Don't output message if either type is Any_Type, or if a message
5087       --  has already been posted for this node. We need to do the latter
5088       --  check explicitly (it is ordinarily done in Errout), because we
5089       --  are using ! to force the output of the error messages.
5090
5091       if Expec_Type = Any_Type
5092         or else Found_Type = Any_Type
5093         or else Error_Posted (Expr)
5094       then
5095          return;
5096
5097       --  In  an instance, there is an ongoing problem with completion of
5098       --  type derived from private types. Their structure is what Gigi
5099       --  expects, but the  Etype is the parent type rather than the
5100       --  derived private type itself. Do not flag error in this case. The
5101       --  private completion is an entity without a parent, like an Itype.
5102       --  Similarly, full and partial views may be incorrect in the instance.
5103       --  There is no simple way to insure that it is consistent ???
5104
5105       elsif In_Instance then
5106
5107          if Etype (Etype (Expr)) = Etype (Expected_Type)
5108            and then No (Parent (Expected_Type))
5109          then
5110             return;
5111          end if;
5112       end if;
5113
5114       --  An interesting special check. If the expression is parenthesized
5115       --  and its type corresponds to the type of the sole component of the
5116       --  expected record type, or to the component type of the expected one
5117       --  dimensional array type, then assume we have a bad aggregate attempt.
5118
5119       if Nkind (Expr) in N_Subexpr
5120         and then Paren_Count (Expr) /= 0
5121         and then Has_One_Matching_Field
5122       then
5123          Error_Msg_N ("positional aggregate cannot have one component", Expr);
5124
5125       --  Another special check, if we are looking for a pool-specific access
5126       --  type and we found an E_Access_Attribute_Type, then we have the case
5127       --  of an Access attribute being used in a context which needs a pool-
5128       --  specific type, which is never allowed. The one extra check we make
5129       --  is that the expected designated type covers the Found_Type.
5130
5131       elsif Is_Access_Type (Expec_Type)
5132         and then Ekind (Found_Type) = E_Access_Attribute_Type
5133         and then Ekind (Base_Type (Expec_Type)) /= E_General_Access_Type
5134         and then Ekind (Base_Type (Expec_Type)) /= E_Anonymous_Access_Type
5135         and then Covers
5136           (Designated_Type (Expec_Type), Designated_Type (Found_Type))
5137       then
5138          Error_Msg_N ("result must be general access type!", Expr);
5139          Error_Msg_NE ("add ALL to }!", Expr, Expec_Type);
5140
5141       --  If the expected type is an anonymous access type, as for access
5142       --  parameters and discriminants, the error is on the designated types.
5143
5144       elsif Ekind (Expec_Type) = E_Anonymous_Access_Type then
5145          if Comes_From_Source (Expec_Type) then
5146             Error_Msg_NE ("expected}!", Expr, Expec_Type);
5147          else
5148             Error_Msg_NE
5149               ("expected an access type with designated}",
5150                  Expr, Designated_Type (Expec_Type));
5151          end if;
5152
5153          if Is_Access_Type (Found_Type)
5154            and then not Comes_From_Source (Found_Type)
5155          then
5156             Error_Msg_NE
5157               ("found an access type with designated}!",
5158                 Expr, Designated_Type (Found_Type));
5159          else
5160             if From_With_Type (Found_Type) then
5161                Error_Msg_NE ("found incomplete}!", Expr, Found_Type);
5162                Error_Msg_NE
5163                  ("\possibly missing with_clause on&", Expr,
5164                    Scope (Found_Type));
5165             else
5166                Error_Msg_NE ("found}!", Expr, Found_Type);
5167             end if;
5168          end if;
5169
5170       --  Normal case of one type found, some other type expected
5171
5172       else
5173          --  If the names of the two types are the same, see if some
5174          --  number of levels of qualification will help. Don't try
5175          --  more than three levels, and if we get to standard, it's
5176          --  no use (and probably represents an error in the compiler)
5177          --  Also do not bother with internal scope names.
5178
5179          declare
5180             Expec_Scope : Entity_Id;
5181             Found_Scope : Entity_Id;
5182
5183          begin
5184             Expec_Scope := Expec_Type;
5185             Found_Scope := Found_Type;
5186
5187             for Levels in Int range 0 .. 3 loop
5188                if Chars (Expec_Scope) /= Chars (Found_Scope) then
5189                   Error_Msg_Qual_Level := Levels;
5190                   exit;
5191                end if;
5192
5193                Expec_Scope := Scope (Expec_Scope);
5194                Found_Scope := Scope (Found_Scope);
5195
5196                exit when Expec_Scope = Standard_Standard
5197                            or else
5198                          Found_Scope = Standard_Standard
5199                            or else
5200                          not Comes_From_Source (Expec_Scope)
5201                            or else
5202                          not Comes_From_Source (Found_Scope);
5203             end loop;
5204          end;
5205
5206          Error_Msg_NE ("expected}!", Expr, Expec_Type);
5207
5208          if Is_Entity_Name (Expr)
5209            and then Is_Package (Entity (Expr))
5210          then
5211             Error_Msg_N ("found package name!", Expr);
5212
5213          elsif Is_Entity_Name (Expr)
5214            and then
5215              (Ekind (Entity (Expr)) = E_Procedure
5216                 or else
5217               Ekind (Entity (Expr)) = E_Generic_Procedure)
5218          then
5219             Error_Msg_N ("found procedure name instead of function!", Expr);
5220
5221          --  catch common error: a prefix or infix operator which is not
5222          --  directly visible because the type isn't.
5223
5224          elsif Nkind (Expr) in N_Op
5225             and then Is_Overloaded (Expr)
5226             and then not Is_Immediately_Visible (Expec_Type)
5227             and then not Is_Potentially_Use_Visible (Expec_Type)
5228             and then not In_Use (Expec_Type)
5229             and then Has_Compatible_Type (Right_Opnd (Expr), Expec_Type)
5230          then
5231             Error_Msg_N (
5232               "operator of the type is not directly visible!", Expr);
5233
5234          else
5235             Error_Msg_NE ("found}!", Expr, Found_Type);
5236          end if;
5237
5238          Error_Msg_Qual_Level := 0;
5239       end if;
5240    end Wrong_Type;
5241
5242 end Sem_Util;