OSDN Git Service

* sem_util.adb (Defining_Entity): in case of error, attach created
[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             else
1606                Error_Msg_N ("& conflicts with declaration#", Def_Id);
1607
1608                --  Avoid cascaded messages with duplicate components in
1609                --  derived types.
1610
1611                if Ekind (E) = E_Component
1612                  or else Ekind (E) = E_Discriminant
1613                then
1614                   return;
1615                end if;
1616             end if;
1617
1618             if Nkind (Parent (Parent (Def_Id)))
1619                  = N_Generic_Subprogram_Declaration
1620               and then Def_Id =
1621                 Defining_Entity (Specification (Parent (Parent (Def_Id))))
1622             then
1623                Error_Msg_N ("\generic units cannot be overloaded", Def_Id);
1624             end if;
1625
1626             --  If entity is in standard, then we are in trouble, because
1627             --  it means that we have a library package with a duplicated
1628             --  name. That's hard to recover from, so abort!
1629
1630             if S = Standard_Standard then
1631                raise Unrecoverable_Error;
1632
1633             --  Otherwise we continue with the declaration. Having two
1634             --  identical declarations should not cause us too much trouble!
1635
1636             else
1637                null;
1638             end if;
1639          end if;
1640       end if;
1641
1642       --  If we fall through, declaration is OK , or OK enough to continue
1643
1644       --  If Def_Id is a discriminant or a record component we are in the
1645       --  midst of inheriting components in a derived record definition.
1646       --  Preserve their Ekind and Etype.
1647
1648       if Ekind (Def_Id) = E_Discriminant
1649         or else Ekind (Def_Id) = E_Component
1650       then
1651          null;
1652
1653       --  If a type is already set, leave it alone (happens whey a type
1654       --  declaration is reanalyzed following a call to the optimizer)
1655
1656       elsif Present (Etype (Def_Id)) then
1657          null;
1658
1659       --  Otherwise, the kind E_Void insures that premature uses of the entity
1660       --  will be detected. Any_Type insures that no cascaded errors will occur
1661
1662       else
1663          Set_Ekind (Def_Id, E_Void);
1664          Set_Etype (Def_Id, Any_Type);
1665       end if;
1666
1667       --  Inherited discriminants and components in derived record types are
1668       --  immediately visible. Itypes are not.
1669
1670       if Ekind (Def_Id) = E_Discriminant
1671         or else Ekind (Def_Id) = E_Component
1672         or else (No (Corresponding_Remote_Type (Def_Id))
1673                  and then not Is_Itype (Def_Id))
1674       then
1675          Set_Is_Immediately_Visible (Def_Id);
1676          Set_Current_Entity         (Def_Id);
1677       end if;
1678
1679       Set_Homonym       (Def_Id, C);
1680       Append_Entity     (Def_Id, S);
1681       Set_Public_Status (Def_Id);
1682
1683       --  Warn if new entity hides an old one
1684
1685       if Warn_On_Hiding
1686         and then Length_Of_Name (Chars (C)) /= 1
1687         and then Present (C)
1688         and then Comes_From_Source (C)
1689         and then Comes_From_Source (Def_Id)
1690         and then In_Extended_Main_Source_Unit (Def_Id)
1691       then
1692          Error_Msg_Sloc := Sloc (C);
1693          Error_Msg_N ("declaration hides &#?", Def_Id);
1694       end if;
1695
1696    end Enter_Name;
1697
1698    -------------------------------------
1699    -- Find_Corresponding_Discriminant --
1700    -------------------------------------
1701
1702    function Find_Corresponding_Discriminant
1703      (Id   : Node_Id;
1704       Typ  : Entity_Id)
1705       return Entity_Id
1706    is
1707       Par_Disc : Entity_Id;
1708       Old_Disc : Entity_Id;
1709       New_Disc : Entity_Id;
1710
1711    begin
1712       Par_Disc := Original_Record_Component (Original_Discriminant (Id));
1713       Old_Disc := First_Discriminant (Scope (Par_Disc));
1714
1715       if Is_Class_Wide_Type (Typ) then
1716          New_Disc := First_Discriminant (Root_Type (Typ));
1717       else
1718          New_Disc := First_Discriminant (Typ);
1719       end if;
1720
1721       while Present (Old_Disc) and then Present (New_Disc) loop
1722          if Old_Disc = Par_Disc  then
1723             return New_Disc;
1724          else
1725             Next_Discriminant (Old_Disc);
1726             Next_Discriminant (New_Disc);
1727          end if;
1728       end loop;
1729
1730       --  Should always find it
1731
1732       raise Program_Error;
1733    end Find_Corresponding_Discriminant;
1734
1735    ------------------
1736    -- First_Actual --
1737    ------------------
1738
1739    function First_Actual (Node : Node_Id) return Node_Id is
1740       N : Node_Id;
1741
1742    begin
1743       if No (Parameter_Associations (Node)) then
1744          return Empty;
1745       end if;
1746
1747       N := First (Parameter_Associations (Node));
1748
1749       if Nkind (N) = N_Parameter_Association then
1750          return First_Named_Actual (Node);
1751       else
1752          return N;
1753       end if;
1754    end First_Actual;
1755
1756    -------------------------
1757    -- Full_Qualified_Name --
1758    -------------------------
1759
1760    function Full_Qualified_Name (E : Entity_Id) return String_Id is
1761
1762       Res : String_Id;
1763
1764       function Internal_Full_Qualified_Name (E : Entity_Id) return String_Id;
1765       --  Compute recursively the qualified name without NUL at the end.
1766
1767       function Internal_Full_Qualified_Name (E : Entity_Id) return String_Id is
1768          Ent         : Entity_Id := E;
1769          Parent_Name : String_Id := No_String;
1770
1771       begin
1772          --  Deals properly with child units
1773
1774          if Nkind (Ent) = N_Defining_Program_Unit_Name then
1775             Ent := Defining_Identifier (Ent);
1776          end if;
1777
1778          --  Compute recursively the qualification. Only "Standard" has no
1779          --  scope.
1780
1781          if Present (Scope (Scope (Ent))) then
1782             Parent_Name := Internal_Full_Qualified_Name (Scope (Ent));
1783          end if;
1784
1785          --  Every entity should have a name except some expanded blocks
1786          --  don't bother about those.
1787
1788          if Chars (Ent) = No_Name then
1789             return Parent_Name;
1790          end if;
1791
1792          --  Add a period between Name and qualification
1793
1794          if Parent_Name /= No_String then
1795             Start_String (Parent_Name);
1796             Store_String_Char (Get_Char_Code ('.'));
1797
1798          else
1799             Start_String;
1800          end if;
1801
1802          --  Generates the entity name in upper case
1803
1804          Get_Name_String (Chars (Ent));
1805          Set_All_Upper_Case;
1806          Store_String_Chars (Name_Buffer (1 .. Name_Len));
1807          return End_String;
1808       end Internal_Full_Qualified_Name;
1809
1810    begin
1811       Res := Internal_Full_Qualified_Name (E);
1812       Store_String_Char (Get_Char_Code (ASCII.nul));
1813       return End_String;
1814    end Full_Qualified_Name;
1815
1816    -----------------------
1817    -- Gather_Components --
1818    -----------------------
1819
1820    procedure Gather_Components
1821      (Typ           : Entity_Id;
1822       Comp_List     : Node_Id;
1823       Governed_By   : List_Id;
1824       Into          : Elist_Id;
1825       Report_Errors : out Boolean)
1826    is
1827       Assoc           : Node_Id;
1828       Variant         : Node_Id;
1829       Discrete_Choice : Node_Id;
1830       Comp_Item       : Node_Id;
1831
1832       Discrim       : Entity_Id;
1833       Discrim_Name  : Node_Id;
1834       Discrim_Value : Node_Id;
1835
1836    begin
1837       Report_Errors := False;
1838
1839       if No (Comp_List) or else Null_Present (Comp_List) then
1840          return;
1841
1842       elsif Present (Component_Items (Comp_List)) then
1843          Comp_Item := First (Component_Items (Comp_List));
1844
1845       else
1846          Comp_Item := Empty;
1847       end if;
1848
1849       while Present (Comp_Item) loop
1850
1851          --  Skip the tag of a tagged record, as well as all items
1852          --  that are not user components (anonymous types, rep clauses,
1853          --  Parent field, controller field).
1854
1855          if Nkind (Comp_Item) = N_Component_Declaration
1856            and then Chars (Defining_Identifier (Comp_Item)) /= Name_uTag
1857            and then Chars (Defining_Identifier (Comp_Item)) /= Name_uParent
1858            and then Chars (Defining_Identifier (Comp_Item)) /= Name_uController
1859          then
1860             Append_Elmt (Defining_Identifier (Comp_Item), Into);
1861          end if;
1862
1863          Next (Comp_Item);
1864       end loop;
1865
1866       if No (Variant_Part (Comp_List)) then
1867          return;
1868       else
1869          Discrim_Name := Name (Variant_Part (Comp_List));
1870          Variant := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
1871       end if;
1872
1873       --  Look for the discriminant that governs this variant part.
1874       --  The discriminant *must* be in the Governed_By List
1875
1876       Assoc := First (Governed_By);
1877       Find_Constraint : loop
1878          Discrim := First (Choices (Assoc));
1879          exit Find_Constraint when Chars (Discrim_Name) = Chars (Discrim)
1880            or else (Present (Corresponding_Discriminant (Entity (Discrim)))
1881                       and then
1882                     Chars (Corresponding_Discriminant (Entity (Discrim)))
1883                          = Chars  (Discrim_Name))
1884            or else Chars (Original_Record_Component (Entity (Discrim)))
1885                          = Chars (Discrim_Name);
1886
1887          if No (Next (Assoc)) then
1888             if not Is_Constrained (Typ)
1889               and then Is_Derived_Type (Typ)
1890               and then Present (Girder_Constraint (Typ))
1891             then
1892
1893                --  If the type is a tagged type with inherited discriminants,
1894                --  use the girder constraint on the parent in order to find
1895                --  the values of discriminants that are otherwise hidden by an
1896                --  explicit constraint. Renamed discriminants are handled in
1897                --  the code above.
1898
1899                declare
1900                   D : Entity_Id;
1901                   C : Elmt_Id;
1902
1903                begin
1904                   D := First_Discriminant (Etype (Typ));
1905                   C := First_Elmt (Girder_Constraint (Typ));
1906
1907                   while Present (D)
1908                     and then Present (C)
1909                   loop
1910                      if Chars (Discrim_Name) = Chars (D) then
1911                         Assoc :=
1912                           Make_Component_Association (Sloc (Typ),
1913                             New_List
1914                               (New_Occurrence_Of (D, Sloc (Typ))),
1915                             Duplicate_Subexpr (Node (C)));
1916                         exit Find_Constraint;
1917                      end if;
1918
1919                      D := Next_Discriminant (D);
1920                      Next_Elmt (C);
1921                   end loop;
1922                end;
1923             end if;
1924          end if;
1925
1926          if No (Next (Assoc)) then
1927             Error_Msg_NE (" missing value for discriminant&",
1928               First (Governed_By), Discrim_Name);
1929             Report_Errors := True;
1930             return;
1931          end if;
1932
1933          Next (Assoc);
1934       end loop Find_Constraint;
1935
1936       Discrim_Value := Expression (Assoc);
1937
1938       if not Is_OK_Static_Expression (Discrim_Value) then
1939          Error_Msg_NE
1940            ("value for discriminant & must be static", Discrim_Value, Discrim);
1941          Report_Errors := True;
1942          return;
1943       end if;
1944
1945       Search_For_Discriminant_Value : declare
1946          Low  : Node_Id;
1947          High : Node_Id;
1948
1949          UI_High          : Uint;
1950          UI_Low           : Uint;
1951          UI_Discrim_Value : constant Uint := Expr_Value (Discrim_Value);
1952
1953       begin
1954          Find_Discrete_Value : while Present (Variant) loop
1955             Discrete_Choice := First (Discrete_Choices (Variant));
1956             while Present (Discrete_Choice) loop
1957
1958                exit Find_Discrete_Value when
1959                  Nkind (Discrete_Choice) = N_Others_Choice;
1960
1961                Get_Index_Bounds (Discrete_Choice, Low, High);
1962
1963                UI_Low  := Expr_Value (Low);
1964                UI_High := Expr_Value (High);
1965
1966                exit Find_Discrete_Value when
1967                  UI_Low <= UI_Discrim_Value
1968                    and then
1969                  UI_High >= UI_Discrim_Value;
1970
1971                Next (Discrete_Choice);
1972             end loop;
1973
1974             Next_Non_Pragma (Variant);
1975          end loop Find_Discrete_Value;
1976       end Search_For_Discriminant_Value;
1977
1978       if No (Variant) then
1979          Error_Msg_NE
1980            ("value of discriminant & is out of range", Discrim_Value, Discrim);
1981          Report_Errors := True;
1982          return;
1983       end  if;
1984
1985       --  If we have found the corresponding choice, recursively add its
1986       --  components to the Into list.
1987
1988       Gather_Components (Empty,
1989         Component_List (Variant), Governed_By, Into, Report_Errors);
1990    end Gather_Components;
1991
1992    ------------------------
1993    -- Get_Actual_Subtype --
1994    ------------------------
1995
1996    function Get_Actual_Subtype (N : Node_Id) return Entity_Id is
1997       Typ  : constant Entity_Id := Etype (N);
1998       Utyp : Entity_Id := Underlying_Type (Typ);
1999       Decl : Node_Id;
2000       Atyp : Entity_Id;
2001
2002    begin
2003       if not Present (Utyp) then
2004          Utyp := Typ;
2005       end if;
2006
2007       --  If what we have is an identifier that references a subprogram
2008       --  formal, or a variable or constant object, then we get the actual
2009       --  subtype from the referenced entity if one has been built.
2010
2011       if Nkind (N) = N_Identifier
2012         and then
2013           (Is_Formal (Entity (N))
2014             or else Ekind (Entity (N)) = E_Constant
2015             or else Ekind (Entity (N)) = E_Variable)
2016         and then Present (Actual_Subtype (Entity (N)))
2017       then
2018          return Actual_Subtype (Entity (N));
2019
2020       --  Actual subtype of unchecked union is always itself. We never need
2021       --  the "real" actual subtype. If we did, we couldn't get it anyway
2022       --  because the discriminant is not available. The restrictions on
2023       --  Unchecked_Union are designed to make sure that this is OK.
2024
2025       elsif Is_Unchecked_Union (Utyp) then
2026          return Typ;
2027
2028       --  Here for the unconstrained case, we must find actual subtype
2029       --  No actual subtype is available, so we must build it on the fly.
2030
2031       --  Checking the type, not the underlying type, for constrainedness
2032       --  seems to be necessary. Maybe all the tests should be on the type???
2033
2034       elsif (not Is_Constrained (Typ))
2035            and then (Is_Array_Type (Utyp)
2036                       or else (Is_Record_Type (Utyp)
2037                                 and then Has_Discriminants (Utyp)))
2038            and then not Has_Unknown_Discriminants (Utyp)
2039            and then not (Ekind (Utyp) = E_String_Literal_Subtype)
2040       then
2041          --  Nothing to do if in default expression
2042
2043          if In_Default_Expression then
2044             return Typ;
2045
2046          --  Else build the actual subtype
2047
2048          else
2049             Decl := Build_Actual_Subtype (Typ, N);
2050             Atyp := Defining_Identifier (Decl);
2051
2052             --  If Build_Actual_Subtype generated a new declaration then use it
2053
2054             if Atyp /= Typ then
2055
2056                --  The actual subtype is an Itype, so analyze the declaration,
2057                --  but do not attach it to the tree, to get the type defined.
2058
2059                Set_Parent (Decl, N);
2060                Set_Is_Itype (Atyp);
2061                Analyze (Decl, Suppress => All_Checks);
2062                Set_Associated_Node_For_Itype (Atyp, N);
2063                Set_Has_Delayed_Freeze (Atyp, False);
2064
2065                --  We need to freeze the actual subtype immediately. This is
2066                --  needed, because otherwise this Itype will not get frozen
2067                --  at all, and it is always safe to freeze on creation because
2068                --  any associated types must be frozen at this point.
2069
2070                Freeze_Itype (Atyp, N);
2071                return Atyp;
2072
2073             --  Otherwise we did not build a declaration, so return original
2074
2075             else
2076                return Typ;
2077             end if;
2078          end if;
2079
2080       --  For all remaining cases, the actual subtype is the same as
2081       --  the nominal type.
2082
2083       else
2084          return Typ;
2085       end if;
2086    end Get_Actual_Subtype;
2087
2088    -------------------------------------
2089    -- Get_Actual_Subtype_If_Available --
2090    -------------------------------------
2091
2092    function Get_Actual_Subtype_If_Available (N : Node_Id) return Entity_Id is
2093       Typ  : constant Entity_Id := Etype (N);
2094
2095    begin
2096       --  If what we have is an identifier that references a subprogram
2097       --  formal, or a variable or constant object, then we get the actual
2098       --  subtype from the referenced entity if one has been built.
2099
2100       if Nkind (N) = N_Identifier
2101         and then
2102           (Is_Formal (Entity (N))
2103             or else Ekind (Entity (N)) = E_Constant
2104             or else Ekind (Entity (N)) = E_Variable)
2105         and then Present (Actual_Subtype (Entity (N)))
2106       then
2107          return Actual_Subtype (Entity (N));
2108
2109       --  Otherwise the Etype of N is returned unchanged
2110
2111       else
2112          return Typ;
2113       end if;
2114    end Get_Actual_Subtype_If_Available;
2115
2116    -------------------------------
2117    -- Get_Default_External_Name --
2118    -------------------------------
2119
2120    function Get_Default_External_Name (E : Node_Or_Entity_Id) return Node_Id is
2121    begin
2122       Get_Decoded_Name_String (Chars (E));
2123
2124       if Opt.External_Name_Imp_Casing = Uppercase then
2125          Set_Casing (All_Upper_Case);
2126       else
2127          Set_Casing (All_Lower_Case);
2128       end if;
2129
2130       return
2131         Make_String_Literal (Sloc (E),
2132           Strval => String_From_Name_Buffer);
2133
2134    end Get_Default_External_Name;
2135
2136    ---------------------------
2137    -- Get_Enum_Lit_From_Pos --
2138    ---------------------------
2139
2140    function Get_Enum_Lit_From_Pos
2141      (T    : Entity_Id;
2142       Pos  : Uint;
2143       Loc  : Source_Ptr)
2144       return Node_Id
2145    is
2146       Lit : Node_Id;
2147       P   : constant Nat := UI_To_Int (Pos);
2148
2149    begin
2150       --  In the case where the literal is either of type Wide_Character
2151       --  or Character or of a type derived from them, there needs to be
2152       --  some special handling since there is no explicit chain of
2153       --  literals to search. Instead, an N_Character_Literal node is
2154       --  created with the appropriate Char_Code and Chars fields.
2155
2156       if Root_Type (T) = Standard_Character
2157         or else Root_Type (T) = Standard_Wide_Character
2158       then
2159          Set_Character_Literal_Name (Char_Code (P));
2160          return
2161            Make_Character_Literal (Loc,
2162              Chars => Name_Find,
2163              Char_Literal_Value => Char_Code (P));
2164
2165       --  For all other cases, we have a complete table of literals, and
2166       --  we simply iterate through the chain of literal until the one
2167       --  with the desired position value is found.
2168       --
2169
2170       else
2171          Lit := First_Literal (Base_Type (T));
2172          for J in 1 .. P loop
2173             Next_Literal (Lit);
2174          end loop;
2175
2176          return New_Occurrence_Of (Lit, Loc);
2177       end if;
2178    end Get_Enum_Lit_From_Pos;
2179
2180    ----------------------
2181    -- Get_Index_Bounds --
2182    ----------------------
2183
2184    procedure Get_Index_Bounds (N : Node_Id; L, H : out Node_Id) is
2185       Kind : constant Node_Kind := Nkind (N);
2186       R    : Node_Id;
2187
2188    begin
2189       if Kind = N_Range then
2190          L := Low_Bound (N);
2191          H := High_Bound (N);
2192
2193       elsif Kind = N_Subtype_Indication then
2194          R := Range_Expression (Constraint (N));
2195
2196          if R = Error then
2197             L := Error;
2198             H := Error;
2199             return;
2200
2201          else
2202             L := Low_Bound  (Range_Expression (Constraint (N)));
2203             H := High_Bound (Range_Expression (Constraint (N)));
2204          end if;
2205
2206       elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
2207          if Error_Posted (Scalar_Range (Entity (N))) then
2208             L := Error;
2209             H := Error;
2210
2211          elsif Nkind (Scalar_Range (Entity (N))) = N_Subtype_Indication then
2212             Get_Index_Bounds (Scalar_Range (Entity (N)), L, H);
2213
2214          else
2215             L := Low_Bound  (Scalar_Range (Entity (N)));
2216             H := High_Bound (Scalar_Range (Entity (N)));
2217          end if;
2218
2219       else
2220          --  N is an expression, indicating a range with one value.
2221
2222          L := N;
2223          H := N;
2224       end if;
2225    end Get_Index_Bounds;
2226
2227    ------------------------
2228    -- Get_Name_Entity_Id --
2229    ------------------------
2230
2231    function Get_Name_Entity_Id (Id : Name_Id) return Entity_Id is
2232    begin
2233       return Entity_Id (Get_Name_Table_Info (Id));
2234    end Get_Name_Entity_Id;
2235
2236    ---------------------------
2237    -- Get_Referenced_Object --
2238    ---------------------------
2239
2240    function Get_Referenced_Object (N : Node_Id) return Node_Id is
2241       R   : Node_Id := N;
2242
2243    begin
2244       while Is_Entity_Name (R)
2245         and then Present (Renamed_Object (Entity (R)))
2246       loop
2247          R := Renamed_Object (Entity (R));
2248       end loop;
2249
2250       return R;
2251    end Get_Referenced_Object;
2252
2253    -------------------------
2254    -- Get_Subprogram_Body --
2255    -------------------------
2256
2257    function Get_Subprogram_Body (E : Entity_Id) return Node_Id is
2258       Decl : Node_Id;
2259
2260    begin
2261       Decl := Unit_Declaration_Node (E);
2262
2263       if Nkind (Decl) = N_Subprogram_Body then
2264          return Decl;
2265
2266       else           --  Nkind (Decl) = N_Subprogram_Declaration
2267
2268          if Present (Corresponding_Body (Decl)) then
2269             return Unit_Declaration_Node (Corresponding_Body (Decl));
2270
2271          else        --  imported subprogram.
2272             return Empty;
2273          end if;
2274       end if;
2275    end Get_Subprogram_Body;
2276
2277    -----------------------------
2278    -- Get_Task_Body_Procedure --
2279    -----------------------------
2280
2281    function Get_Task_Body_Procedure (E : Entity_Id) return Node_Id is
2282    begin
2283       return Task_Body_Procedure (Declaration_Node (Root_Type (E)));
2284    end Get_Task_Body_Procedure;
2285
2286    --------------------
2287    -- Has_Infinities --
2288    --------------------
2289
2290    function Has_Infinities (E : Entity_Id) return Boolean is
2291    begin
2292       return
2293         Is_Floating_Point_Type (E)
2294           and then Nkind (Scalar_Range (E)) = N_Range
2295           and then Includes_Infinities (Scalar_Range (E));
2296    end Has_Infinities;
2297
2298    ---------------------------
2299    -- Has_Private_Component --
2300    ---------------------------
2301
2302    function Has_Private_Component (Type_Id : Entity_Id) return Boolean is
2303       Btype     : Entity_Id := Base_Type (Type_Id);
2304       Component : Entity_Id;
2305
2306    begin
2307       if Error_Posted (Type_Id)
2308         or else Error_Posted (Btype)
2309       then
2310          return False;
2311       end if;
2312
2313       if Is_Class_Wide_Type (Btype) then
2314          Btype := Root_Type (Btype);
2315       end if;
2316
2317       if Is_Private_Type (Btype) then
2318          declare
2319             UT : constant Entity_Id := Underlying_Type (Btype);
2320          begin
2321             if No (UT) then
2322
2323                if No (Full_View (Btype)) then
2324                   return not Is_Generic_Type (Btype)
2325                     and then not Is_Generic_Type (Root_Type (Btype));
2326
2327                else
2328                   return not Is_Generic_Type (Root_Type (Full_View (Btype)));
2329                end if;
2330
2331             else
2332                return not Is_Frozen (UT) and then Has_Private_Component (UT);
2333             end if;
2334          end;
2335       elsif Is_Array_Type (Btype) then
2336          return Has_Private_Component (Component_Type (Btype));
2337
2338       elsif Is_Record_Type (Btype) then
2339
2340          Component := First_Component (Btype);
2341          while Present (Component) loop
2342
2343             if Has_Private_Component (Etype (Component)) then
2344                return True;
2345             end if;
2346
2347             Next_Component (Component);
2348          end loop;
2349
2350          return False;
2351
2352       elsif Is_Protected_Type (Btype)
2353         and then Present (Corresponding_Record_Type (Btype))
2354       then
2355          return Has_Private_Component (Corresponding_Record_Type (Btype));
2356
2357       else
2358          return False;
2359       end if;
2360    end Has_Private_Component;
2361
2362    --------------------------
2363    -- Has_Tagged_Component --
2364    --------------------------
2365
2366    function Has_Tagged_Component (Typ : Entity_Id) return Boolean is
2367       Comp : Entity_Id;
2368
2369    begin
2370       if Is_Private_Type (Typ)
2371         and then Present (Underlying_Type (Typ))
2372       then
2373          return Has_Tagged_Component (Underlying_Type (Typ));
2374
2375       elsif Is_Array_Type (Typ) then
2376          return Has_Tagged_Component (Component_Type (Typ));
2377
2378       elsif Is_Tagged_Type (Typ) then
2379          return True;
2380
2381       elsif Is_Record_Type (Typ) then
2382          Comp := First_Component (Typ);
2383
2384          while Present (Comp) loop
2385             if Has_Tagged_Component (Etype (Comp)) then
2386                return True;
2387             end if;
2388
2389             Comp := Next_Component (Typ);
2390          end loop;
2391
2392          return False;
2393
2394       else
2395          return False;
2396       end if;
2397    end Has_Tagged_Component;
2398
2399    -----------------
2400    -- In_Instance --
2401    -----------------
2402
2403    function In_Instance return Boolean is
2404       S : Entity_Id := Current_Scope;
2405
2406    begin
2407       while Present (S)
2408         and then S /= Standard_Standard
2409       loop
2410          if (Ekind (S) = E_Function
2411               or else Ekind (S) = E_Package
2412               or else Ekind (S) = E_Procedure)
2413            and then Is_Generic_Instance (S)
2414          then
2415             return True;
2416          end if;
2417
2418          S := Scope (S);
2419       end loop;
2420
2421       return False;
2422    end In_Instance;
2423
2424    ----------------------
2425    -- In_Instance_Body --
2426    ----------------------
2427
2428    function In_Instance_Body return Boolean is
2429       S : Entity_Id := Current_Scope;
2430
2431    begin
2432       while Present (S)
2433         and then S /= Standard_Standard
2434       loop
2435          if (Ekind (S) = E_Function
2436               or else Ekind (S) = E_Procedure)
2437            and then Is_Generic_Instance (S)
2438          then
2439             return True;
2440
2441          elsif Ekind (S) = E_Package
2442            and then In_Package_Body (S)
2443            and then Is_Generic_Instance (S)
2444          then
2445             return True;
2446          end if;
2447
2448          S := Scope (S);
2449       end loop;
2450
2451       return False;
2452    end In_Instance_Body;
2453
2454    -----------------------------
2455    -- In_Instance_Not_Visible --
2456    -----------------------------
2457
2458    function In_Instance_Not_Visible return Boolean is
2459       S : Entity_Id := Current_Scope;
2460
2461    begin
2462       while Present (S)
2463         and then S /= Standard_Standard
2464       loop
2465          if (Ekind (S) = E_Function
2466               or else Ekind (S) = E_Procedure)
2467            and then Is_Generic_Instance (S)
2468          then
2469             return True;
2470
2471          elsif Ekind (S) = E_Package
2472            and then (In_Package_Body (S) or else In_Private_Part (S))
2473            and then Is_Generic_Instance (S)
2474          then
2475             return True;
2476          end if;
2477
2478          S := Scope (S);
2479       end loop;
2480
2481       return False;
2482    end In_Instance_Not_Visible;
2483
2484    ------------------------------
2485    -- In_Instance_Visible_Part --
2486    ------------------------------
2487
2488    function In_Instance_Visible_Part return Boolean is
2489       S : Entity_Id := Current_Scope;
2490
2491    begin
2492       while Present (S)
2493         and then S /= Standard_Standard
2494       loop
2495          if Ekind (S) = E_Package
2496            and then Is_Generic_Instance (S)
2497            and then not In_Package_Body (S)
2498            and then not In_Private_Part (S)
2499          then
2500             return True;
2501          end if;
2502
2503          S := Scope (S);
2504       end loop;
2505
2506       return False;
2507    end In_Instance_Visible_Part;
2508
2509    --------------------------------------
2510    -- In_Subprogram_Or_Concurrent_Unit --
2511    --------------------------------------
2512
2513    function In_Subprogram_Or_Concurrent_Unit return Boolean is
2514       E : Entity_Id;
2515       K : Entity_Kind;
2516
2517    begin
2518       --  Use scope chain to check successively outer scopes
2519
2520       E := Current_Scope;
2521       loop
2522          K := Ekind (E);
2523
2524          if K in Subprogram_Kind
2525            or else K in Concurrent_Kind
2526            or else K = E_Generic_Procedure
2527            or else K = E_Generic_Function
2528          then
2529             return True;
2530
2531          elsif E = Standard_Standard then
2532             return False;
2533          end if;
2534
2535          E := Scope (E);
2536       end loop;
2537
2538    end In_Subprogram_Or_Concurrent_Unit;
2539
2540    ---------------------
2541    -- In_Visible_Part --
2542    ---------------------
2543
2544    function In_Visible_Part (Scope_Id : Entity_Id) return Boolean is
2545    begin
2546       return
2547         Is_Package (Scope_Id)
2548           and then In_Open_Scopes (Scope_Id)
2549           and then not In_Package_Body (Scope_Id)
2550           and then not In_Private_Part (Scope_Id);
2551    end In_Visible_Part;
2552
2553    -------------------
2554    -- Is_AAMP_Float --
2555    -------------------
2556
2557    function Is_AAMP_Float (E : Entity_Id) return Boolean is
2558    begin
2559       pragma Assert (Is_Type (E));
2560
2561       return AAMP_On_Target
2562          and then Is_Floating_Point_Type (E)
2563          and then E = Base_Type (E);
2564    end Is_AAMP_Float;
2565
2566    -------------------------
2567    -- Is_Actual_Parameter --
2568    -------------------------
2569
2570    function Is_Actual_Parameter (N : Node_Id) return Boolean is
2571       PK : constant Node_Kind := Nkind (Parent (N));
2572
2573    begin
2574       case PK is
2575          when N_Parameter_Association =>
2576             return N = Explicit_Actual_Parameter (Parent (N));
2577
2578          when N_Function_Call | N_Procedure_Call_Statement =>
2579             return Is_List_Member (N)
2580               and then
2581                 List_Containing (N) = Parameter_Associations (Parent (N));
2582
2583          when others =>
2584             return False;
2585       end case;
2586    end Is_Actual_Parameter;
2587
2588    ---------------------
2589    -- Is_Aliased_View --
2590    ---------------------
2591
2592    function Is_Aliased_View (Obj : Node_Id) return Boolean is
2593       E : Entity_Id;
2594
2595    begin
2596       if Is_Entity_Name (Obj) then
2597
2598          --  Shouldn't we check that we really have an object here?
2599          --  If we do, then a-caldel.adb blows up mysteriously ???
2600
2601          E := Entity (Obj);
2602
2603          return Is_Aliased (E)
2604            or else (Present (Renamed_Object (E))
2605                      and then Is_Aliased_View (Renamed_Object (E)))
2606
2607            or else ((Is_Formal (E)
2608                       or else Ekind (E) = E_Generic_In_Out_Parameter
2609                       or else Ekind (E) = E_Generic_In_Parameter)
2610                     and then Is_Tagged_Type (Etype (E)))
2611
2612            or else ((Ekind (E) = E_Task_Type or else
2613                      Ekind (E) = E_Protected_Type)
2614                     and then In_Open_Scopes (E))
2615
2616             --  Current instance of type
2617
2618            or else (Is_Type (E) and then E = Current_Scope)
2619            or else (Is_Incomplete_Or_Private_Type (E)
2620                      and then Full_View (E) = Current_Scope);
2621
2622       elsif Nkind (Obj) = N_Selected_Component then
2623          return Is_Aliased (Entity (Selector_Name (Obj)));
2624
2625       elsif Nkind (Obj) = N_Indexed_Component then
2626          return Has_Aliased_Components (Etype (Prefix (Obj)))
2627            or else
2628              (Is_Access_Type (Etype (Prefix (Obj)))
2629                and then
2630               Has_Aliased_Components
2631                 (Designated_Type (Etype (Prefix (Obj)))));
2632
2633       elsif Nkind (Obj) = N_Unchecked_Type_Conversion
2634         or else Nkind (Obj) = N_Type_Conversion
2635       then
2636          return Is_Tagged_Type (Etype (Obj))
2637            or else Is_Aliased_View (Expression (Obj));
2638
2639       elsif Nkind (Obj) = N_Explicit_Dereference then
2640          return Nkind (Original_Node (Obj)) /= N_Function_Call;
2641
2642       else
2643          return False;
2644       end if;
2645    end Is_Aliased_View;
2646
2647    ----------------------
2648    -- Is_Atomic_Object --
2649    ----------------------
2650
2651    function Is_Atomic_Object (N : Node_Id) return Boolean is
2652
2653       function Object_Has_Atomic_Components (N : Node_Id) return Boolean;
2654       --  Determines if given object has atomic components
2655
2656       function Is_Atomic_Prefix (N : Node_Id) return Boolean;
2657       --  If prefix is an implicit dereference, examine designated type.
2658
2659       function Is_Atomic_Prefix (N : Node_Id) return Boolean is
2660       begin
2661          if Is_Access_Type (Etype (N)) then
2662             return
2663               Has_Atomic_Components (Designated_Type (Etype (N)));
2664          else
2665             return Object_Has_Atomic_Components (N);
2666          end if;
2667       end Is_Atomic_Prefix;
2668
2669       function Object_Has_Atomic_Components (N : Node_Id) return Boolean is
2670       begin
2671          if Has_Atomic_Components (Etype (N))
2672            or else Is_Atomic (Etype (N))
2673          then
2674             return True;
2675
2676          elsif Is_Entity_Name (N)
2677            and then (Has_Atomic_Components (Entity (N))
2678                       or else Is_Atomic (Entity (N)))
2679          then
2680             return True;
2681
2682          elsif Nkind (N) = N_Indexed_Component
2683            or else Nkind (N) = N_Selected_Component
2684          then
2685             return Is_Atomic_Prefix (Prefix (N));
2686
2687          else
2688             return False;
2689          end if;
2690       end Object_Has_Atomic_Components;
2691
2692    --  Start of processing for Is_Atomic_Object
2693
2694    begin
2695       if Is_Atomic (Etype (N))
2696         or else (Is_Entity_Name (N) and then Is_Atomic (Entity (N)))
2697       then
2698          return True;
2699
2700       elsif Nkind (N) = N_Indexed_Component
2701         or else Nkind (N) = N_Selected_Component
2702       then
2703          return Is_Atomic_Prefix (Prefix (N));
2704
2705       else
2706          return False;
2707       end if;
2708    end Is_Atomic_Object;
2709
2710    ----------------------------------------------
2711    -- Is_Dependent_Component_Of_Mutable_Object --
2712    ----------------------------------------------
2713
2714    function Is_Dependent_Component_Of_Mutable_Object
2715      (Object : Node_Id)
2716       return   Boolean
2717    is
2718       P           : Node_Id;
2719       Prefix_Type : Entity_Id;
2720       P_Aliased   : Boolean := False;
2721       Comp        : Entity_Id;
2722
2723       function Has_Dependent_Constraint (Comp : Entity_Id) return Boolean;
2724       --  Returns True if and only if Comp has a constrained subtype
2725       --  that depends on a discriminant.
2726
2727       function Is_Declared_Within_Variant (Comp : Entity_Id) return Boolean;
2728       --  Returns True if and only if Comp is declared within a variant part.
2729
2730       ------------------------------
2731       -- Has_Dependent_Constraint --
2732       ------------------------------
2733
2734       function Has_Dependent_Constraint (Comp : Entity_Id) return Boolean is
2735          Comp_Decl  : constant Node_Id   := Parent (Comp);
2736          Subt_Indic : constant Node_Id   := Subtype_Indication (Comp_Decl);
2737          Constr     : Node_Id;
2738          Assn       : Node_Id;
2739
2740       begin
2741          if Nkind (Subt_Indic) = N_Subtype_Indication then
2742             Constr := Constraint (Subt_Indic);
2743
2744             if Nkind (Constr) = N_Index_Or_Discriminant_Constraint then
2745                Assn := First (Constraints (Constr));
2746                while Present (Assn) loop
2747                   case Nkind (Assn) is
2748                      when N_Subtype_Indication |
2749                           N_Range              |
2750                           N_Identifier
2751                      =>
2752                         if Depends_On_Discriminant (Assn) then
2753                            return True;
2754                         end if;
2755
2756                      when N_Discriminant_Association =>
2757                         if Depends_On_Discriminant (Expression (Assn)) then
2758                            return True;
2759                         end if;
2760
2761                      when others =>
2762                         null;
2763
2764                   end case;
2765
2766                   Next (Assn);
2767                end loop;
2768             end if;
2769          end if;
2770
2771          return False;
2772       end Has_Dependent_Constraint;
2773
2774       --------------------------------
2775       -- Is_Declared_Within_Variant --
2776       --------------------------------
2777
2778       function Is_Declared_Within_Variant (Comp : Entity_Id) return Boolean is
2779          Comp_Decl : constant Node_Id   := Parent (Comp);
2780          Comp_List : constant Node_Id   := Parent (Comp_Decl);
2781
2782       begin
2783          return Nkind (Parent (Comp_List)) = N_Variant;
2784       end Is_Declared_Within_Variant;
2785
2786    --  Start of processing for Is_Dependent_Component_Of_Mutable_Object
2787
2788    begin
2789       if Is_Variable (Object) then
2790
2791          if Nkind (Object) = N_Selected_Component then
2792             P := Prefix (Object);
2793             Prefix_Type := Etype (P);
2794
2795             if Is_Entity_Name (P) then
2796
2797                if Ekind (Entity (P)) = E_Generic_In_Out_Parameter then
2798                   Prefix_Type := Base_Type (Prefix_Type);
2799                end if;
2800
2801                if Is_Aliased (Entity (P)) then
2802                   P_Aliased := True;
2803                end if;
2804
2805             else
2806                --  Check for prefix being an aliased component ???
2807                null;
2808             end if;
2809
2810             if Is_Access_Type (Prefix_Type)
2811               or else Nkind (P) = N_Explicit_Dereference
2812             then
2813                return False;
2814             end if;
2815
2816             Comp :=
2817               Original_Record_Component (Entity (Selector_Name (Object)));
2818
2819             if not Is_Constrained (Prefix_Type)
2820               and then not Is_Indefinite_Subtype (Prefix_Type)
2821               and then (Is_Declared_Within_Variant (Comp)
2822                           or else Has_Dependent_Constraint (Comp))
2823               and then not P_Aliased
2824             then
2825                return True;
2826
2827             else
2828                return
2829                  Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
2830
2831             end if;
2832
2833          elsif Nkind (Object) = N_Indexed_Component
2834            or else Nkind (Object) = N_Slice
2835          then
2836             return Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
2837          end if;
2838       end if;
2839
2840       return False;
2841    end Is_Dependent_Component_Of_Mutable_Object;
2842
2843    --------------
2844    -- Is_False --
2845    --------------
2846
2847    function Is_False (U : Uint) return Boolean is
2848    begin
2849       return (U = 0);
2850    end Is_False;
2851
2852    ---------------------------
2853    -- Is_Fixed_Model_Number --
2854    ---------------------------
2855
2856    function Is_Fixed_Model_Number (U : Ureal; T : Entity_Id) return Boolean is
2857       S : constant Ureal := Small_Value (T);
2858       M : Urealp.Save_Mark;
2859       R : Boolean;
2860
2861    begin
2862       M := Urealp.Mark;
2863       R := (U = UR_Trunc (U / S) * S);
2864       Urealp.Release (M);
2865       return R;
2866    end Is_Fixed_Model_Number;
2867
2868    -------------------------------
2869    -- Is_Fully_Initialized_Type --
2870    -------------------------------
2871
2872    function Is_Fully_Initialized_Type (Typ : Entity_Id) return Boolean is
2873    begin
2874       if Is_Scalar_Type (Typ) then
2875          return False;
2876
2877       elsif Is_Access_Type (Typ) then
2878          return True;
2879
2880       elsif Is_Array_Type (Typ) then
2881          if Is_Fully_Initialized_Type (Component_Type (Typ)) then
2882             return True;
2883          end if;
2884
2885          --  An interesting case, if we have a constrained type one of whose
2886          --  bounds is known to be null, then there are no elements to be
2887          --  initialized, so all the elements are initialized!
2888
2889          if Is_Constrained (Typ) then
2890             declare
2891                Indx     : Node_Id;
2892                Indx_Typ : Entity_Id;
2893                Lbd, Hbd : Node_Id;
2894
2895             begin
2896                Indx := First_Index (Typ);
2897                while Present (Indx) loop
2898
2899                   if Etype (Indx) = Any_Type then
2900                      return False;
2901
2902                   --  If index is a range, use directly.
2903
2904                   elsif Nkind (Indx) = N_Range then
2905                      Lbd := Low_Bound  (Indx);
2906                      Hbd := High_Bound (Indx);
2907
2908                   else
2909                      Indx_Typ := Etype (Indx);
2910
2911                      if Is_Private_Type (Indx_Typ)  then
2912                         Indx_Typ := Full_View (Indx_Typ);
2913                      end if;
2914
2915                      if No (Indx_Typ) then
2916                         return False;
2917                      else
2918                         Lbd := Type_Low_Bound  (Indx_Typ);
2919                         Hbd := Type_High_Bound (Indx_Typ);
2920                      end if;
2921                   end if;
2922
2923                   if Compile_Time_Known_Value (Lbd)
2924                     and then Compile_Time_Known_Value (Hbd)
2925                   then
2926                      if Expr_Value (Hbd) < Expr_Value (Lbd) then
2927                         return True;
2928                      end if;
2929                   end if;
2930
2931                   Next_Index (Indx);
2932                end loop;
2933             end;
2934          end if;
2935
2936          return False;
2937
2938       elsif Is_Record_Type (Typ) then
2939          declare
2940             Ent : Entity_Id;
2941
2942          begin
2943             Ent := First_Entity (Typ);
2944
2945             while Present (Ent) loop
2946                if Ekind (Ent) = E_Component
2947                  and then (No (Parent (Ent))
2948                              or else No (Expression (Parent (Ent))))
2949                  and then not Is_Fully_Initialized_Type (Etype (Ent))
2950                then
2951                   return False;
2952                end if;
2953
2954                Next_Entity (Ent);
2955             end loop;
2956          end;
2957
2958          return True;
2959
2960       elsif Is_Concurrent_Type (Typ) then
2961          return True;
2962
2963       elsif Is_Private_Type (Typ) then
2964          declare
2965             U : constant Entity_Id := Underlying_Type (Typ);
2966
2967          begin
2968             if No (U) then
2969                return False;
2970             else
2971                return Is_Fully_Initialized_Type (U);
2972             end if;
2973          end;
2974
2975       else
2976          return False;
2977       end if;
2978    end Is_Fully_Initialized_Type;
2979
2980    ----------------------------
2981    -- Is_Inherited_Operation --
2982    ----------------------------
2983
2984    function Is_Inherited_Operation (E : Entity_Id) return Boolean is
2985       Kind : constant Node_Kind := Nkind (Parent (E));
2986
2987    begin
2988       pragma Assert (Is_Overloadable (E));
2989       return Kind = N_Full_Type_Declaration
2990         or else Kind = N_Private_Extension_Declaration
2991         or else Kind = N_Subtype_Declaration
2992         or else (Ekind (E) = E_Enumeration_Literal
2993                   and then Is_Derived_Type (Etype (E)));
2994    end Is_Inherited_Operation;
2995
2996    -----------------------------
2997    -- Is_Library_Level_Entity --
2998    -----------------------------
2999
3000    function Is_Library_Level_Entity (E : Entity_Id) return Boolean is
3001    begin
3002       return Enclosing_Dynamic_Scope (E) = Standard_Standard;
3003    end Is_Library_Level_Entity;
3004
3005    ---------------------------------
3006    -- Is_Local_Variable_Reference --
3007    ---------------------------------
3008
3009    function Is_Local_Variable_Reference (Expr : Node_Id) return Boolean is
3010    begin
3011       if not Is_Entity_Name (Expr) then
3012          return False;
3013
3014       else
3015          declare
3016             Ent : constant Entity_Id := Entity (Expr);
3017             Sub : constant Entity_Id := Enclosing_Subprogram (Ent);
3018
3019          begin
3020             if Ekind (Ent) /= E_Variable
3021                  and then
3022                Ekind (Ent) /= E_In_Out_Parameter
3023             then
3024                return False;
3025
3026             else
3027                return Present (Sub) and then Sub = Current_Subprogram;
3028             end if;
3029          end;
3030       end if;
3031    end Is_Local_Variable_Reference;
3032
3033    -------------------------
3034    -- Is_Object_Reference --
3035    -------------------------
3036
3037    function Is_Object_Reference (N : Node_Id) return Boolean is
3038    begin
3039       if Is_Entity_Name (N) then
3040          return Is_Object (Entity (N));
3041
3042       else
3043          case Nkind (N) is
3044             when N_Indexed_Component | N_Slice =>
3045                return True;
3046
3047             --  In Ada95, a function call is a constant object.
3048
3049             when N_Function_Call =>
3050                return True;
3051
3052             when N_Selected_Component =>
3053                return Is_Object_Reference (Selector_Name (N));
3054
3055             when N_Explicit_Dereference =>
3056                return True;
3057
3058             --  An unchecked type conversion is considered to be an object if
3059             --  the operand is an object (this construction arises only as a
3060             --  result of expansion activities).
3061
3062             when N_Unchecked_Type_Conversion =>
3063                return True;
3064
3065             when others =>
3066                return False;
3067          end case;
3068       end if;
3069    end Is_Object_Reference;
3070
3071    -----------------------------------
3072    -- Is_OK_Variable_For_Out_Formal --
3073    -----------------------------------
3074
3075    function Is_OK_Variable_For_Out_Formal (AV : Node_Id) return Boolean is
3076    begin
3077       Note_Possible_Modification (AV);
3078
3079       --  We must reject parenthesized variable names. The check for
3080       --  Comes_From_Source is present because there are currently
3081       --  cases where the compiler violates this rule (e.g. passing
3082       --  a task object to its controlled Initialize routine).
3083
3084       if Paren_Count (AV) > 0 and then Comes_From_Source (AV) then
3085          return False;
3086
3087       --  A variable is always allowed
3088
3089       elsif Is_Variable (AV) then
3090          return True;
3091
3092       --  Unchecked conversions are allowed only if they come from the
3093       --  generated code, which sometimes uses unchecked conversions for
3094       --  out parameters in cases where code generation is unaffected.
3095       --  We tell source unchecked conversions by seeing if they are
3096       --  rewrites of an original UC function call, or of an explicit
3097       --  conversion of a function call.
3098
3099       elsif Nkind (AV) = N_Unchecked_Type_Conversion then
3100          if Nkind (Original_Node (AV)) = N_Function_Call then
3101             return False;
3102
3103          elsif Comes_From_Source (AV)
3104            and then Nkind (Original_Node (Expression (AV))) = N_Function_Call
3105          then
3106             return False;
3107
3108          else
3109             return True;
3110          end if;
3111
3112       --  Normal type conversions are allowed if argument is a variable
3113
3114       elsif Nkind (AV) = N_Type_Conversion then
3115          if Is_Variable (Expression (AV))
3116            and then Paren_Count (Expression (AV)) = 0
3117          then
3118             Note_Possible_Modification (Expression (AV));
3119             return True;
3120
3121          --  We also allow a non-parenthesized expression that raises
3122          --  constraint error if it rewrites what used to be a variable
3123
3124          elsif Raises_Constraint_Error (Expression (AV))
3125             and then Paren_Count (Expression (AV)) = 0
3126             and then Is_Variable (Original_Node (Expression (AV)))
3127          then
3128             return True;
3129
3130          --  Type conversion of something other than a variable
3131
3132          else
3133             return False;
3134          end if;
3135
3136       --  If this node is rewritten, then test the original form, if that is
3137       --  OK, then we consider the rewritten node OK (for example, if the
3138       --  original node is a conversion, then Is_Variable will not be true
3139       --  but we still want to allow the conversion if it converts a variable.
3140
3141       elsif Original_Node (AV) /= AV then
3142          return Is_OK_Variable_For_Out_Formal (Original_Node (AV));
3143
3144       --  All other non-variables are rejected
3145
3146       else
3147          return False;
3148       end if;
3149    end Is_OK_Variable_For_Out_Formal;
3150
3151    -----------------------------
3152    -- Is_RCI_Pkg_Spec_Or_Body --
3153    -----------------------------
3154
3155    function Is_RCI_Pkg_Spec_Or_Body (Cunit : Node_Id) return Boolean is
3156
3157       function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean;
3158       --  Return True if the unit of Cunit is an RCI package declaration
3159
3160       ---------------------------
3161       -- Is_RCI_Pkg_Decl_Cunit --
3162       ---------------------------
3163
3164       function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean is
3165          The_Unit : constant Node_Id := Unit (Cunit);
3166
3167       begin
3168          if Nkind (The_Unit) /= N_Package_Declaration then
3169             return False;
3170          end if;
3171          return Is_Remote_Call_Interface (Defining_Entity (The_Unit));
3172       end Is_RCI_Pkg_Decl_Cunit;
3173
3174    --  Start of processing for Is_RCI_Pkg_Spec_Or_Body
3175
3176    begin
3177       return Is_RCI_Pkg_Decl_Cunit (Cunit)
3178         or else
3179          (Nkind (Unit (Cunit)) = N_Package_Body
3180            and then Is_RCI_Pkg_Decl_Cunit (Library_Unit (Cunit)));
3181    end Is_RCI_Pkg_Spec_Or_Body;
3182
3183    -----------------------------------------
3184    -- Is_Remote_Access_To_Class_Wide_Type --
3185    -----------------------------------------
3186
3187    function Is_Remote_Access_To_Class_Wide_Type
3188      (E    : Entity_Id)
3189       return Boolean
3190    is
3191       D : Entity_Id;
3192
3193       function Comes_From_Limited_Private_Type_Declaration
3194         (E    : Entity_Id)
3195          return Boolean;
3196       --  Check if the original declaration is a limited private one and
3197       --  if all the derivations have been using private extensions.
3198
3199       -------------------------------------------------
3200       -- Comes_From_Limited_Private_Type_Declaration --
3201       -------------------------------------------------
3202
3203       function Comes_From_Limited_Private_Type_Declaration (E : in Entity_Id)
3204         return Boolean
3205       is
3206          N : constant Node_Id := Declaration_Node (E);
3207       begin
3208          if Nkind (N) = N_Private_Type_Declaration
3209            and then Limited_Present (N)
3210          then
3211             return True;
3212          end if;
3213
3214          if Nkind (N) = N_Private_Extension_Declaration then
3215             return Comes_From_Limited_Private_Type_Declaration (Etype (E));
3216          end if;
3217
3218          return False;
3219       end Comes_From_Limited_Private_Type_Declaration;
3220
3221    --  Start of processing for Is_Remote_Access_To_Class_Wide_Type
3222
3223    begin
3224       if not (Is_Remote_Call_Interface (E)
3225                or else Is_Remote_Types (E))
3226         or else Ekind (E) /= E_General_Access_Type
3227       then
3228          return False;
3229       end if;
3230
3231       D := Designated_Type (E);
3232
3233       if Ekind (D) /= E_Class_Wide_Type then
3234          return False;
3235       end if;
3236
3237       return Comes_From_Limited_Private_Type_Declaration
3238                (Defining_Identifier (Parent (D)));
3239    end Is_Remote_Access_To_Class_Wide_Type;
3240
3241    -----------------------------------------
3242    -- Is_Remote_Access_To_Subprogram_Type --
3243    -----------------------------------------
3244
3245    function Is_Remote_Access_To_Subprogram_Type
3246      (E    : Entity_Id)
3247       return Boolean
3248    is
3249    begin
3250       return (Ekind (E) = E_Access_Subprogram_Type
3251                 or else (Ekind (E) = E_Record_Type
3252                            and then Present (Corresponding_Remote_Type (E))))
3253         and then (Is_Remote_Call_Interface (E)
3254                    or else Is_Remote_Types (E));
3255    end Is_Remote_Access_To_Subprogram_Type;
3256
3257    --------------------
3258    -- Is_Remote_Call --
3259    --------------------
3260
3261    function Is_Remote_Call (N : Node_Id) return Boolean is
3262    begin
3263       if Nkind (N) /= N_Procedure_Call_Statement
3264         and then Nkind (N) /= N_Function_Call
3265       then
3266          --  An entry call cannot be remote
3267
3268          return False;
3269
3270       elsif Nkind (Name (N)) in N_Has_Entity
3271         and then Is_Remote_Call_Interface (Entity (Name (N)))
3272       then
3273          --  A subprogram declared in the spec of a RCI package is remote
3274
3275          return True;
3276
3277       elsif Nkind (Name (N)) = N_Explicit_Dereference
3278         and then Is_Remote_Access_To_Subprogram_Type
3279           (Etype (Prefix (Name (N))))
3280       then
3281          --  The dereference of a RAS is a remote call
3282
3283          return True;
3284
3285       elsif Present (Controlling_Argument (N))
3286         and then Is_Remote_Access_To_Class_Wide_Type
3287           (Etype (Controlling_Argument (N)))
3288       then
3289          --  Any primitive operation call with a controlling argument of
3290          --  a RACW type is a remote call.
3291
3292          return True;
3293       end if;
3294
3295       --  All other calls are local calls
3296
3297       return False;
3298    end Is_Remote_Call;
3299
3300    ----------------------
3301    -- Is_Selector_Name --
3302    ----------------------
3303
3304    function Is_Selector_Name (N : Node_Id) return Boolean is
3305
3306    begin
3307       if not Is_List_Member (N) then
3308          declare
3309             P : constant Node_Id   := Parent (N);
3310             K : constant Node_Kind := Nkind (P);
3311
3312          begin
3313             return
3314               (K = N_Expanded_Name          or else
3315                K = N_Generic_Association    or else
3316                K = N_Parameter_Association  or else
3317                K = N_Selected_Component)
3318               and then Selector_Name (P) = N;
3319          end;
3320
3321       else
3322          declare
3323             L : constant List_Id := List_Containing (N);
3324             P : constant Node_Id := Parent (L);
3325
3326          begin
3327             return (Nkind (P) = N_Discriminant_Association
3328                      and then Selector_Names (P) = L)
3329               or else
3330                    (Nkind (P) = N_Component_Association
3331                      and then Choices (P) = L);
3332          end;
3333       end if;
3334    end Is_Selector_Name;
3335
3336    ------------------
3337    -- Is_Statement --
3338    ------------------
3339
3340    function Is_Statement (N : Node_Id) return Boolean is
3341    begin
3342       return
3343         Nkind (N) in N_Statement_Other_Than_Procedure_Call
3344           or else Nkind (N) = N_Procedure_Call_Statement;
3345    end Is_Statement;
3346
3347    -----------------
3348    -- Is_Transfer --
3349    -----------------
3350
3351    function Is_Transfer (N : Node_Id) return Boolean is
3352       Kind : constant Node_Kind := Nkind (N);
3353
3354    begin
3355       if Kind = N_Return_Statement
3356            or else
3357          Kind = N_Goto_Statement
3358            or else
3359          Kind = N_Raise_Statement
3360            or else
3361          Kind = N_Requeue_Statement
3362       then
3363          return True;
3364
3365       elsif (Kind = N_Exit_Statement or else Kind in N_Raise_xxx_Error)
3366         and then No (Condition (N))
3367       then
3368          return True;
3369
3370       elsif Kind = N_Procedure_Call_Statement
3371         and then Is_Entity_Name (Name (N))
3372         and then Present (Entity (Name (N)))
3373         and then No_Return (Entity (Name (N)))
3374       then
3375          return True;
3376
3377       elsif Nkind (Original_Node (N)) = N_Raise_Statement then
3378          return True;
3379
3380       else
3381          return False;
3382       end if;
3383    end Is_Transfer;
3384
3385    -------------
3386    -- Is_True --
3387    -------------
3388
3389    function Is_True (U : Uint) return Boolean is
3390    begin
3391       return (U /= 0);
3392    end Is_True;
3393
3394    -----------------
3395    -- Is_Variable --
3396    -----------------
3397
3398    function Is_Variable (N : Node_Id) return Boolean is
3399
3400       Orig_Node : constant Node_Id := Original_Node (N);
3401       --  We do the test on the original node, since this is basically a
3402       --  test of syntactic categories, so it must not be disturbed by
3403       --  whatever rewriting might have occurred. For example, an aggregate,
3404       --  which is certainly NOT a variable, could be turned into a variable
3405       --  by expansion.
3406
3407       function In_Protected_Function (E : Entity_Id) return Boolean;
3408       --  Within a protected function, the private components of the
3409       --  enclosing protected type are constants. A function nested within
3410       --  a (protected) procedure is not itself protected.
3411
3412       function Is_Variable_Prefix (P : Node_Id) return Boolean;
3413       --  Prefixes can involve implicit dereferences, in which case we
3414       --  must test for the case of a reference of a constant access
3415       --  type, which can never be a variable.
3416
3417       function In_Protected_Function (E : Entity_Id) return Boolean is
3418          Prot : constant Entity_Id := Scope (E);
3419          S    : Entity_Id;
3420
3421       begin
3422          if not Is_Protected_Type (Prot) then
3423             return False;
3424          else
3425             S := Current_Scope;
3426
3427             while Present (S) and then S /= Prot loop
3428
3429                if Ekind (S) = E_Function
3430                  and then Scope (S) = Prot
3431                then
3432                   return True;
3433                end if;
3434
3435                S := Scope (S);
3436             end loop;
3437
3438             return False;
3439          end if;
3440       end In_Protected_Function;
3441
3442       function Is_Variable_Prefix (P : Node_Id) return Boolean is
3443       begin
3444          if Is_Access_Type (Etype (P)) then
3445             return not Is_Access_Constant (Root_Type (Etype (P)));
3446          else
3447             return Is_Variable (P);
3448          end if;
3449       end Is_Variable_Prefix;
3450
3451    --  Start of processing for Is_Variable
3452
3453    begin
3454       --  Definitely OK if Assignment_OK is set. Since this is something that
3455       --  only gets set for expanded nodes, the test is on N, not Orig_Node.
3456
3457       if Nkind (N) in N_Subexpr and then Assignment_OK (N) then
3458          return True;
3459
3460       --  Normally we go to the original node, but there is one exception
3461       --  where we use the rewritten node, namely when it is an explicit
3462       --  dereference. The generated code may rewrite a prefix which is an
3463       --  access type with an explicit dereference. The dereference is a
3464       --  variable, even though the original node may not be (since it could
3465       --  be a constant of the access type).
3466
3467       elsif Nkind (N) = N_Explicit_Dereference
3468         and then Nkind (Orig_Node) /= N_Explicit_Dereference
3469         and then Is_Access_Type (Etype (Orig_Node))
3470       then
3471          return Is_Variable_Prefix (Original_Node (Prefix (N)));
3472
3473       --  All remaining checks use the original node
3474
3475       elsif Is_Entity_Name (Orig_Node) then
3476          declare
3477             E : constant Entity_Id := Entity (Orig_Node);
3478             K : constant Entity_Kind := Ekind (E);
3479
3480          begin
3481             return (K = E_Variable
3482                       and then Nkind (Parent (E)) /= N_Exception_Handler)
3483               or else  (K = E_Component
3484                           and then not In_Protected_Function (E))
3485               or else  K = E_Out_Parameter
3486               or else  K = E_In_Out_Parameter
3487               or else  K = E_Generic_In_Out_Parameter
3488
3489                --  Current instance of type:
3490
3491               or else (Is_Type (E) and then In_Open_Scopes (E))
3492               or else (Is_Incomplete_Or_Private_Type (E)
3493                         and then In_Open_Scopes (Full_View (E)));
3494          end;
3495
3496       else
3497          case Nkind (Orig_Node) is
3498             when N_Indexed_Component | N_Slice =>
3499                return Is_Variable_Prefix (Prefix (Orig_Node));
3500
3501             when N_Selected_Component =>
3502                return Is_Variable_Prefix (Prefix (Orig_Node))
3503                  and then Is_Variable (Selector_Name (Orig_Node));
3504
3505             --  For an explicit dereference, we must check whether the type
3506             --  is ACCESS CONSTANT, since if it is, then it is not a variable.
3507
3508             when N_Explicit_Dereference =>
3509                return Is_Access_Type (Etype (Prefix (Orig_Node)))
3510                  and then not
3511                    Is_Access_Constant (Root_Type (Etype (Prefix (Orig_Node))));
3512
3513             --  The type conversion is the case where we do not deal with the
3514             --  context dependent special case of an actual parameter. Thus
3515             --  the type conversion is only considered a variable for the
3516             --  purposes of this routine if the target type is tagged. However,
3517             --  a type conversion is considered to be a variable if it does not
3518             --  come from source (this deals for example with the conversions
3519             --  of expressions to their actual subtypes).
3520
3521             when N_Type_Conversion =>
3522                return Is_Variable (Expression (Orig_Node))
3523                  and then
3524                    (not Comes_From_Source (Orig_Node)
3525                       or else
3526                         (Is_Tagged_Type (Etype (Subtype_Mark (Orig_Node)))
3527                           and then
3528                          Is_Tagged_Type (Etype (Expression (Orig_Node)))));
3529
3530             --  GNAT allows an unchecked type conversion as a variable. This
3531             --  only affects the generation of internal expanded code, since
3532             --  calls to instantiations of Unchecked_Conversion are never
3533             --  considered variables (since they are function calls).
3534             --  This is also true for expression actions.
3535
3536             when N_Unchecked_Type_Conversion =>
3537                return Is_Variable (Expression (Orig_Node));
3538
3539             when others =>
3540                return False;
3541          end case;
3542       end if;
3543    end Is_Variable;
3544
3545    ------------------------
3546    -- Is_Volatile_Object --
3547    ------------------------
3548
3549    function Is_Volatile_Object (N : Node_Id) return Boolean is
3550
3551       function Object_Has_Volatile_Components (N : Node_Id) return Boolean;
3552       --  Determines if given object has volatile components
3553
3554       function Is_Volatile_Prefix (N : Node_Id) return Boolean;
3555       --  If prefix is an implicit dereference, examine designated type.
3556
3557       function Is_Volatile_Prefix (N : Node_Id) return Boolean is
3558       begin
3559          if Is_Access_Type (Etype (N)) then
3560             return Has_Volatile_Components (Designated_Type (Etype (N)));
3561          else
3562             return Object_Has_Volatile_Components (N);
3563          end if;
3564       end Is_Volatile_Prefix;
3565
3566       function Object_Has_Volatile_Components (N : Node_Id) return Boolean is
3567       begin
3568          if Is_Volatile (Etype (N))
3569            or else Has_Volatile_Components (Etype (N))
3570          then
3571             return True;
3572
3573          elsif Is_Entity_Name (N)
3574            and then (Has_Volatile_Components (Entity (N))
3575                       or else Is_Volatile (Entity (N)))
3576          then
3577             return True;
3578
3579          elsif Nkind (N) = N_Indexed_Component
3580            or else Nkind (N) = N_Selected_Component
3581          then
3582             return Is_Volatile_Prefix (Prefix (N));
3583
3584          else
3585             return False;
3586          end if;
3587       end Object_Has_Volatile_Components;
3588
3589    --  Start of processing for Is_Volatile_Object
3590
3591    begin
3592       if Is_Volatile (Etype (N))
3593         or else (Is_Entity_Name (N) and then Is_Volatile (Entity (N)))
3594       then
3595          return True;
3596
3597       elsif Nkind (N) = N_Indexed_Component
3598         or else Nkind (N) = N_Selected_Component
3599       then
3600          return Is_Volatile_Prefix (Prefix (N));
3601
3602       else
3603          return False;
3604       end if;
3605    end Is_Volatile_Object;
3606
3607    --------------------------
3608    -- Kill_Size_Check_Code --
3609    --------------------------
3610
3611    procedure Kill_Size_Check_Code (E : Entity_Id) is
3612    begin
3613       if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
3614         and then Present (Size_Check_Code (E))
3615       then
3616          Remove (Size_Check_Code (E));
3617          Set_Size_Check_Code (E, Empty);
3618       end if;
3619    end Kill_Size_Check_Code;
3620
3621    -------------------------
3622    -- New_External_Entity --
3623    -------------------------
3624
3625    function New_External_Entity
3626      (Kind         : Entity_Kind;
3627       Scope_Id     : Entity_Id;
3628       Sloc_Value   : Source_Ptr;
3629       Related_Id   : Entity_Id;
3630       Suffix       : Character;
3631       Suffix_Index : Nat := 0;
3632       Prefix       : Character := ' ')
3633       return         Entity_Id
3634    is
3635       N : constant Entity_Id :=
3636             Make_Defining_Identifier (Sloc_Value,
3637               New_External_Name
3638                 (Chars (Related_Id), Suffix, Suffix_Index, Prefix));
3639
3640    begin
3641       Set_Ekind          (N, Kind);
3642       Set_Is_Internal    (N, True);
3643       Append_Entity      (N, Scope_Id);
3644       Set_Public_Status  (N);
3645
3646       if Kind in Type_Kind then
3647          Init_Size_Align (N);
3648       end if;
3649
3650       return N;
3651    end New_External_Entity;
3652
3653    -------------------------
3654    -- New_Internal_Entity --
3655    -------------------------
3656
3657    function New_Internal_Entity
3658      (Kind       : Entity_Kind;
3659       Scope_Id   : Entity_Id;
3660       Sloc_Value : Source_Ptr;
3661       Id_Char    : Character)
3662       return       Entity_Id
3663    is
3664       N : constant Entity_Id :=
3665             Make_Defining_Identifier (Sloc_Value, New_Internal_Name (Id_Char));
3666
3667    begin
3668       Set_Ekind          (N, Kind);
3669       Set_Is_Internal    (N, True);
3670       Append_Entity      (N, Scope_Id);
3671
3672       if Kind in Type_Kind then
3673          Init_Size_Align (N);
3674       end if;
3675
3676       return N;
3677    end New_Internal_Entity;
3678
3679    -----------------
3680    -- Next_Actual --
3681    -----------------
3682
3683    function Next_Actual (Actual_Id : Node_Id) return Node_Id is
3684       N  : Node_Id;
3685
3686    begin
3687       --  If we are pointing at a positional parameter, it is a member of
3688       --  a node list (the list of parameters), and the next parameter
3689       --  is the next node on the list, unless we hit a parameter
3690       --  association, in which case we shift to using the chain whose
3691       --  head is the First_Named_Actual in the parent, and then is
3692       --  threaded using the Next_Named_Actual of the Parameter_Association.
3693       --  All this fiddling is because the original node list is in the
3694       --  textual call order, and what we need is the declaration order.
3695
3696       if Is_List_Member (Actual_Id) then
3697          N := Next (Actual_Id);
3698
3699          if Nkind (N) = N_Parameter_Association then
3700             return First_Named_Actual (Parent (Actual_Id));
3701          else
3702             return N;
3703          end if;
3704
3705       else
3706          return Next_Named_Actual (Parent (Actual_Id));
3707       end if;
3708    end Next_Actual;
3709
3710    procedure Next_Actual (Actual_Id : in out Node_Id) is
3711    begin
3712       Actual_Id := Next_Actual (Actual_Id);
3713    end Next_Actual;
3714
3715    -----------------------
3716    -- Normalize_Actuals --
3717    -----------------------
3718
3719    --  Chain actuals according to formals of subprogram. If there are
3720    --  no named associations, the chain is simply the list of Parameter
3721    --  Associations, since the order is the same as the declaration order.
3722    --  If there are named associations, then the First_Named_Actual field
3723    --  in the N_Procedure_Call_Statement node or N_Function_Call node
3724    --  points to the Parameter_Association node for the parameter that
3725    --  comes first in declaration order. The remaining named parameters
3726    --  are then chained in declaration order using Next_Named_Actual.
3727
3728    --  This routine also verifies that the number of actuals is compatible
3729    --  with the number and default values of formals, but performs no type
3730    --  checking (type checking is done by the caller).
3731
3732    --  If the matching succeeds, Success is set to True, and the caller
3733    --  proceeds with type-checking. If the match is unsuccessful, then
3734    --  Success is set to False, and the caller attempts a different
3735    --  interpretation, if there is one.
3736
3737    --  If the flag Report is on, the call is not overloaded, and a failure
3738    --  to match can be reported here, rather than in the caller.
3739
3740    procedure Normalize_Actuals
3741      (N       : Node_Id;
3742       S       : Entity_Id;
3743       Report  : Boolean;
3744       Success : out Boolean)
3745    is
3746       Actuals     : constant List_Id := Parameter_Associations (N);
3747       Actual      : Node_Id   := Empty;
3748       Formal      : Entity_Id;
3749       Last        : Node_Id := Empty;
3750       First_Named : Node_Id := Empty;
3751       Found       : Boolean;
3752
3753       Formals_To_Match : Integer := 0;
3754       Actuals_To_Match : Integer := 0;
3755
3756       procedure Chain (A : Node_Id);
3757       --  Add named actual at the proper place in the list, using the
3758       --  Next_Named_Actual link.
3759
3760       function Reporting return Boolean;
3761       --  Determines if an error is to be reported. To report an error, we
3762       --  need Report to be True, and also we do not report errors caused
3763       --  by calls to Init_Proc's that occur within other Init_Proc's. Such
3764       --  errors must always be cascaded errors, since if all the types are
3765       --  declared correctly, the compiler will certainly build decent calls!
3766
3767       procedure Chain (A : Node_Id) is
3768       begin
3769          if No (Last) then
3770
3771             --  Call node points to first actual in list.
3772
3773             Set_First_Named_Actual (N, Explicit_Actual_Parameter (A));
3774
3775          else
3776             Set_Next_Named_Actual (Last, Explicit_Actual_Parameter (A));
3777          end if;
3778
3779          Last := A;
3780          Set_Next_Named_Actual (Last, Empty);
3781       end Chain;
3782
3783       function Reporting return Boolean is
3784       begin
3785          if not Report then
3786             return False;
3787
3788          elsif not Within_Init_Proc then
3789             return True;
3790
3791          elsif Chars (Entity (Name (N))) = Name_uInit_Proc then
3792             return False;
3793
3794          else
3795             return True;
3796          end if;
3797       end Reporting;
3798
3799    --  Start of processing for Normalize_Actuals
3800
3801    begin
3802       if Is_Access_Type (S) then
3803
3804          --  The name in the call is a function call that returns an access
3805          --  to subprogram. The designated type has the list of formals.
3806
3807          Formal := First_Formal (Designated_Type (S));
3808       else
3809          Formal := First_Formal (S);
3810       end if;
3811
3812       while Present (Formal) loop
3813          Formals_To_Match := Formals_To_Match + 1;
3814          Next_Formal (Formal);
3815       end loop;
3816
3817       --  Find if there is a named association, and verify that no positional
3818       --  associations appear after named ones.
3819
3820       if Present (Actuals) then
3821          Actual := First (Actuals);
3822       end if;
3823
3824       while Present (Actual)
3825         and then Nkind (Actual) /= N_Parameter_Association
3826       loop
3827          Actuals_To_Match := Actuals_To_Match + 1;
3828          Next (Actual);
3829       end loop;
3830
3831       if No (Actual) and Actuals_To_Match = Formals_To_Match then
3832
3833          --  Most common case: positional notation, no defaults
3834
3835          Success := True;
3836          return;
3837
3838       elsif Actuals_To_Match > Formals_To_Match then
3839
3840          --  Too many actuals: will not work.
3841
3842          if Reporting then
3843             Error_Msg_N ("too many arguments in call", N);
3844          end if;
3845
3846          Success := False;
3847          return;
3848       end if;
3849
3850       First_Named := Actual;
3851
3852       while Present (Actual) loop
3853          if Nkind (Actual) /= N_Parameter_Association then
3854             Error_Msg_N
3855               ("positional parameters not allowed after named ones", Actual);
3856             Success := False;
3857             return;
3858
3859          else
3860             Actuals_To_Match := Actuals_To_Match + 1;
3861          end if;
3862
3863          Next (Actual);
3864       end loop;
3865
3866       if Present (Actuals) then
3867          Actual := First (Actuals);
3868       end if;
3869
3870       Formal := First_Formal (S);
3871
3872       while Present (Formal) loop
3873
3874          --  Match the formals in order. If the corresponding actual
3875          --  is positional,  nothing to do. Else scan the list of named
3876          --  actuals to find the one with the right name.
3877
3878          if Present (Actual)
3879            and then Nkind (Actual) /= N_Parameter_Association
3880          then
3881             Next (Actual);
3882             Actuals_To_Match := Actuals_To_Match - 1;
3883             Formals_To_Match := Formals_To_Match - 1;
3884
3885          else
3886             --  For named parameters, search the list of actuals to find
3887             --  one that matches the next formal name.
3888
3889             Actual := First_Named;
3890             Found  := False;
3891
3892             while Present (Actual) loop
3893                if Chars (Selector_Name (Actual)) = Chars (Formal) then
3894                   Found := True;
3895                   Chain (Actual);
3896                   Actuals_To_Match := Actuals_To_Match - 1;
3897                   Formals_To_Match := Formals_To_Match - 1;
3898                   exit;
3899                end if;
3900
3901                Next (Actual);
3902             end loop;
3903
3904             if not Found then
3905                if Ekind (Formal) /= E_In_Parameter
3906                  or else No (Default_Value (Formal))
3907                then
3908                   if Reporting then
3909                      if Comes_From_Source (S)
3910                        and then Is_Overloadable (S)
3911                      then
3912                         Error_Msg_Name_1 := Chars (S);
3913                         Error_Msg_Sloc := Sloc (S);
3914                         Error_Msg_NE
3915                           ("missing argument for parameter & " &
3916                              "in call to % declared #", N, Formal);
3917                      else
3918                         Error_Msg_NE
3919                           ("missing argument for parameter &", N, Formal);
3920                      end if;
3921                   end if;
3922
3923                   Success := False;
3924                   return;
3925
3926                else
3927                   Formals_To_Match := Formals_To_Match - 1;
3928                end if;
3929             end if;
3930          end if;
3931
3932          Next_Formal (Formal);
3933       end loop;
3934
3935       if  Formals_To_Match = 0 and then Actuals_To_Match = 0 then
3936          Success := True;
3937          return;
3938
3939       else
3940          if Reporting then
3941
3942             --  Find some superfluous named actual that did not get
3943             --  attached to the list of associations.
3944
3945             Actual := First (Actuals);
3946
3947             while Present (Actual) loop
3948
3949                if Nkind (Actual) = N_Parameter_Association
3950                  and then Actual /= Last
3951                  and then No (Next_Named_Actual (Actual))
3952                then
3953                   Error_Msg_N ("Unmatched actual in call",  Actual);
3954                   exit;
3955                end if;
3956
3957                Next (Actual);
3958             end loop;
3959          end if;
3960
3961          Success := False;
3962          return;
3963       end if;
3964    end Normalize_Actuals;
3965
3966    --------------------------------
3967    -- Note_Possible_Modification --
3968    --------------------------------
3969
3970    procedure Note_Possible_Modification (N : Node_Id) is
3971       Ent : Entity_Id;
3972       Exp : Node_Id;
3973
3974       procedure Set_Ref (E : Entity_Id; N : Node_Id);
3975       --  Internal routine to note modification on entity E by node N
3976
3977       procedure Set_Ref (E : Entity_Id; N : Node_Id) is
3978       begin
3979          Set_Not_Source_Assigned (E, False);
3980          Set_Is_True_Constant (E, False);
3981          Generate_Reference (E, N, 'm');
3982       end Set_Ref;
3983
3984    --  Start of processing for Note_Possible_Modification
3985
3986    begin
3987       --  Loop to find referenced entity, if there is one
3988
3989       Exp := N;
3990       loop
3991          --  Test for node rewritten as dereference (e.g. accept parameter)
3992
3993          if Nkind (Exp) = N_Explicit_Dereference
3994            and then Is_Entity_Name (Original_Node (Exp))
3995          then
3996             Set_Ref (Entity (Original_Node (Exp)), Original_Node (Exp));
3997             return;
3998
3999          elsif Is_Entity_Name (Exp) then
4000             Ent := Entity (Exp);
4001
4002             if (Ekind (Ent) = E_Variable or else Ekind (Ent) = E_Constant)
4003               and then Present (Renamed_Object (Ent))
4004             then
4005                Exp := Renamed_Object (Ent);
4006
4007             else
4008                Set_Ref (Ent, Exp);
4009                return;
4010             end if;
4011
4012          elsif     Nkind (Exp) = N_Type_Conversion
4013            or else Nkind (Exp) = N_Unchecked_Type_Conversion
4014          then
4015             Exp := Expression (Exp);
4016
4017          elsif     Nkind (Exp) = N_Slice
4018            or else Nkind (Exp) = N_Indexed_Component
4019            or else Nkind (Exp) = N_Selected_Component
4020          then
4021             Exp := Prefix (Exp);
4022
4023          else
4024             return;
4025          end if;
4026       end loop;
4027    end Note_Possible_Modification;
4028
4029    -------------------------
4030    -- Object_Access_Level --
4031    -------------------------
4032
4033    function Object_Access_Level (Obj : Node_Id) return Uint is
4034       E : Entity_Id;
4035
4036    --  Returns the static accessibility level of the view denoted
4037    --  by Obj.  Note that the value returned is the result of a
4038    --  call to Scope_Depth.  Only scope depths associated with
4039    --  dynamic scopes can actually be returned.  Since only
4040    --  relative levels matter for accessibility checking, the fact
4041    --  that the distance between successive levels of accessibility
4042    --  is not always one is immaterial (invariant: if level(E2) is
4043    --  deeper than level(E1), then Scope_Depth(E1) < Scope_Depth(E2)).
4044
4045    begin
4046       if Is_Entity_Name (Obj) then
4047          E := Entity (Obj);
4048
4049          --  If E is a type then it denotes a current instance.
4050          --  For this case we add one to the normal accessibility
4051          --  level of the type to ensure that current instances
4052          --  are treated as always being deeper than than the level
4053          --  of any visible named access type (see 3.10.2(21)).
4054
4055          if Is_Type (E) then
4056             return Type_Access_Level (E) +  1;
4057
4058          elsif Present (Renamed_Object (E)) then
4059             return Object_Access_Level (Renamed_Object (E));
4060
4061          --  Similarly, if E is a component of the current instance of a
4062          --  protected type, any instance of it is assumed to be at a deeper
4063          --  level than the type. For a protected object (whose type is an
4064          --  anonymous protected type) its components are at the same level
4065          --  as the type itself.
4066
4067          elsif not Is_Overloadable (E)
4068            and then Ekind (Scope (E)) = E_Protected_Type
4069            and then Comes_From_Source (Scope (E))
4070          then
4071             return Type_Access_Level (Scope (E)) + 1;
4072
4073          else
4074             return Scope_Depth (Enclosing_Dynamic_Scope (E));
4075          end if;
4076
4077       elsif Nkind (Obj) = N_Selected_Component then
4078          if Is_Access_Type (Etype (Prefix (Obj))) then
4079             return Type_Access_Level (Etype (Prefix (Obj)));
4080          else
4081             return Object_Access_Level (Prefix (Obj));
4082          end if;
4083
4084       elsif Nkind (Obj) = N_Indexed_Component then
4085          if Is_Access_Type (Etype (Prefix (Obj))) then
4086             return Type_Access_Level (Etype (Prefix (Obj)));
4087          else
4088             return Object_Access_Level (Prefix (Obj));
4089          end if;
4090
4091       elsif Nkind (Obj) = N_Explicit_Dereference then
4092
4093          --  If the prefix is a selected access discriminant then
4094          --  we make a recursive call on the prefix, which will
4095          --  in turn check the level of the prefix object of
4096          --  the selected discriminant.
4097
4098          if Nkind (Prefix (Obj)) = N_Selected_Component
4099            and then Ekind (Etype (Prefix (Obj))) = E_Anonymous_Access_Type
4100            and then
4101              Ekind (Entity (Selector_Name (Prefix (Obj)))) = E_Discriminant
4102          then
4103             return Object_Access_Level (Prefix (Obj));
4104          else
4105             return Type_Access_Level (Etype (Prefix (Obj)));
4106          end if;
4107
4108       elsif Nkind (Obj) = N_Type_Conversion then
4109          return Object_Access_Level (Expression (Obj));
4110
4111       --  Function results are objects, so we get either the access level
4112       --  of the function or, in the case of an indirect call, the level of
4113       --  of the access-to-subprogram type.
4114
4115       elsif Nkind (Obj) = N_Function_Call then
4116          if Is_Entity_Name (Name (Obj)) then
4117             return Subprogram_Access_Level (Entity (Name (Obj)));
4118          else
4119             return Type_Access_Level (Etype (Prefix (Name (Obj))));
4120          end if;
4121
4122       --  For convenience we handle qualified expressions, even though
4123       --  they aren't technically object names.
4124
4125       elsif Nkind (Obj) = N_Qualified_Expression then
4126          return Object_Access_Level (Expression (Obj));
4127
4128       --  Otherwise return the scope level of Standard.
4129       --  (If there are cases that fall through
4130       --  to this point they will be treated as
4131       --  having global accessibility for now. ???)
4132
4133       else
4134          return Scope_Depth (Standard_Standard);
4135       end if;
4136    end Object_Access_Level;
4137
4138    -----------------------
4139    -- Private_Component --
4140    -----------------------
4141
4142    function Private_Component (Type_Id : Entity_Id) return Entity_Id is
4143       Ancestor  : constant Entity_Id := Base_Type (Type_Id);
4144
4145       function Trace_Components
4146         (T     : Entity_Id;
4147          Check : Boolean)
4148          return  Entity_Id;
4149       --  Recursive function that does the work, and checks against circular
4150       --  definition for each subcomponent type.
4151
4152       ----------------------
4153       -- Trace_Components --
4154       ----------------------
4155
4156       function Trace_Components
4157          (T     : Entity_Id;
4158           Check : Boolean) return Entity_Id
4159        is
4160          Btype     : constant Entity_Id := Base_Type (T);
4161          Component : Entity_Id;
4162          P         : Entity_Id;
4163          Candidate : Entity_Id := Empty;
4164
4165       begin
4166          if Check and then Btype = Ancestor then
4167             Error_Msg_N ("circular type definition", Type_Id);
4168             return Any_Type;
4169          end if;
4170
4171          if Is_Private_Type (Btype)
4172            and then not Is_Generic_Type (Btype)
4173          then
4174             return Btype;
4175
4176          elsif Is_Array_Type (Btype) then
4177             return Trace_Components (Component_Type (Btype), True);
4178
4179          elsif Is_Record_Type (Btype) then
4180             Component := First_Entity (Btype);
4181             while Present (Component) loop
4182
4183                --  skip anonymous types generated by constrained components.
4184
4185                if not Is_Type (Component) then
4186                   P := Trace_Components (Etype (Component), True);
4187
4188                   if Present (P) then
4189                      if P = Any_Type then
4190                         return P;
4191                      else
4192                         Candidate := P;
4193                      end if;
4194                   end if;
4195                end if;
4196
4197                Next_Entity (Component);
4198             end loop;
4199
4200             return Candidate;
4201
4202          else
4203             return Empty;
4204          end if;
4205       end Trace_Components;
4206
4207    --  Start of processing for Private_Component
4208
4209    begin
4210       return Trace_Components (Type_Id, False);
4211    end Private_Component;
4212
4213    -----------------------
4214    -- Process_End_Label --
4215    -----------------------
4216
4217    procedure Process_End_Label (N : Node_Id; Typ  : Character) is
4218       Loc  : Source_Ptr;
4219       Nam  : Node_Id;
4220       Ctyp : Entity_Id;
4221
4222       Label_Ref : Boolean;
4223       --  Set True if reference to end label itself is required
4224
4225       Endl : Node_Id;
4226       --  Gets set to the operator symbol or identifier that references
4227       --  the entity Ent. For the child unit case, this is the identifier
4228       --  from the designator. For other cases, this is simply Endl.
4229
4230       Ent : Entity_Id;
4231       --  This is the entity for the construct to which the End_Label applies
4232
4233       procedure Generate_Parent_Ref (N : Node_Id);
4234       --  N is an identifier node that appears as a parent unit reference
4235       --  in the case where Ent is a child unit. This procedure generates
4236       --  an appropriate cross-reference entry.
4237
4238       procedure Generate_Parent_Ref (N : Node_Id) is
4239          Parent_Ent : Entity_Id;
4240
4241       begin
4242          --  Search up scope stack. The reason we do this is that normal
4243          --  visibility analysis would not work for two reasons. First in
4244          --  some subunit cases, the entry for the parent unit may not be
4245          --  visible, and in any case there can be a local entity that
4246          --  hides the scope entity.
4247
4248          Parent_Ent := Current_Scope;
4249          while Present (Parent_Ent) loop
4250             if Chars (Parent_Ent) = Chars (N) then
4251
4252                --  Generate the reference. We do NOT consider this as a
4253                --  reference for unreferenced symbol purposes, but we do
4254                --  force a cross-reference even if the end line does not
4255                --  come from source (the caller already generated the
4256                --  appropriate Typ for this situation).
4257
4258                Generate_Reference
4259                  (Parent_Ent, N, 'r', Set_Ref => False, Force => True);
4260                Style.Check_Identifier (N, Parent_Ent);
4261                return;
4262             end if;
4263
4264             Parent_Ent := Scope (Parent_Ent);
4265          end loop;
4266
4267          --  Fall through means entity was not found -- that's odd, but
4268          --  the appropriate thing is simply to ignore and not generate
4269          --  any cross-reference for this entry.
4270
4271          return;
4272       end Generate_Parent_Ref;
4273
4274    --  Start of processing for Process_End_Label
4275
4276    begin
4277       --  If no node, ignore. This happens in some error situations,
4278       --  and also for some internally generated structures where no
4279       --  end label references are required in any case.
4280
4281       if No (N) then
4282          return;
4283       end if;
4284
4285       --  Nothing to do if no End_Label, happens for internally generated
4286       --  constructs where we don't want an end label reference anyway.
4287       --  Also nothing to do if Endl is a string literal, which means
4288       --  there was some prior error (bad operator symbol)
4289
4290       Endl := End_Label (N);
4291
4292       if No (Endl) or else Nkind (Endl) = N_String_Literal then
4293          return;
4294       end if;
4295
4296       --  Reference node is not in extended main source unit
4297
4298       if not In_Extended_Main_Source_Unit (N) then
4299
4300          --  Generally we do not collect references except for the
4301          --  extended main source unit. The one exception is the 'e'
4302          --  entry for a package spec, where it is useful for a client
4303          --  to have the ending information to define scopes.
4304
4305          if Typ /= 'e' then
4306             return;
4307
4308          else
4309             Label_Ref := False;
4310
4311             --  For this case, we can ignore any parent references,
4312             --  but we need the package name itself for the 'e' entry.
4313
4314             if Nkind (Endl) = N_Designator then
4315                Endl := Identifier (Endl);
4316             end if;
4317          end if;
4318
4319       --  Reference is in extended main source unit
4320
4321       else
4322          Label_Ref := True;
4323
4324          --  For designator, generate references for the parent entries
4325
4326          if Nkind (Endl) = N_Designator then
4327
4328             --  Generate references for the prefix if the END line comes
4329             --  from source (otherwise we do not need these references)
4330
4331             if Comes_From_Source (Endl) then
4332                Nam := Name (Endl);
4333                while Nkind (Nam) = N_Selected_Component loop
4334                   Generate_Parent_Ref (Selector_Name (Nam));
4335                   Nam := Prefix (Nam);
4336                end loop;
4337
4338                Generate_Parent_Ref (Nam);
4339             end if;
4340
4341             Endl := Identifier (Endl);
4342          end if;
4343       end if;
4344
4345       --  Locate the entity to which the end label applies. Most of the
4346       --  time this is simply the current scope containing the construct.
4347
4348       Ent := Current_Scope;
4349
4350       if Chars (Ent) = Chars (Endl) then
4351          null;
4352
4353       --  But in the case of single tasks and single protected objects,
4354       --  the current scope is the anonymous task or protected type and
4355       --  what we want is the object. There is no direct link so what we
4356       --  do is search ahead in the entity chain for the object with the
4357       --  matching type and name. In practice it is almost certain to be
4358       --  the very next entity on the chain, so this is not inefficient.
4359
4360       else
4361          Ctyp := Etype (Ent);
4362          loop
4363             Next_Entity (Ent);
4364
4365             --  If we don't find the entry we are looking for, that's
4366             --  odd, perhaps results from some error condition? Anyway
4367             --  the appropriate thing is just to abandon the attempt.
4368
4369             if No (Ent) then
4370                return;
4371
4372             --  Exit if we find the entity we are looking for
4373
4374             elsif Etype (Ent) = Ctyp
4375               and then Chars (Ent) = Chars (Endl)
4376             then
4377                exit;
4378             end if;
4379          end loop;
4380       end if;
4381
4382       --  If label was really there, then generate a normal reference
4383       --  and then adjust the location in the end label to point past
4384       --  the name (which should almost always be the semicolon).
4385
4386       Loc := Sloc (Endl);
4387
4388       if Comes_From_Source (Endl) then
4389
4390          --  If a label reference is required, then do the style check
4391          --  and generate a normal cross-reference entry for the label
4392
4393          if Label_Ref then
4394             Style.Check_Identifier (Endl, Ent);
4395             Generate_Reference (Ent, Endl, 'r', Set_Ref => False);
4396          end if;
4397
4398          --  Set the location to point past the label (normally this will
4399          --  mean the semicolon immediately following the label). This is
4400          --  done for the sake of the 'e' or 't' entry generated below.
4401
4402          Get_Decoded_Name_String (Chars (Endl));
4403          Set_Sloc (Endl, Sloc (Endl) + Source_Ptr (Name_Len));
4404       end if;
4405
4406       --  Now generate the e/t reference
4407
4408       Generate_Reference (Ent, Endl, Typ, Set_Ref => False, Force => True);
4409
4410       --  Restore Sloc, in case modified above, since we have an identifier
4411       --  and the normal Sloc should be left set in the tree.
4412
4413       Set_Sloc (Endl, Loc);
4414    end Process_End_Label;
4415
4416    ------------------
4417    -- Real_Convert --
4418    ------------------
4419
4420    --  We do the conversion to get the value of the real string by using
4421    --  the scanner, see Sinput for details on use of the internal source
4422    --  buffer for scanning internal strings.
4423
4424    function Real_Convert (S : String) return Node_Id is
4425       Save_Src : constant Source_Buffer_Ptr := Source;
4426       Negative : Boolean;
4427
4428    begin
4429       Source := Internal_Source_Ptr;
4430       Scan_Ptr := 1;
4431
4432       for J in S'Range loop
4433          Source (Source_Ptr (J)) := S (J);
4434       end loop;
4435
4436       Source (S'Length + 1) := EOF;
4437
4438       if Source (Scan_Ptr) = '-' then
4439          Negative := True;
4440          Scan_Ptr := Scan_Ptr + 1;
4441       else
4442          Negative := False;
4443       end if;
4444
4445       Scan;
4446
4447       if Negative then
4448          Set_Realval (Token_Node, UR_Negate (Realval (Token_Node)));
4449       end if;
4450
4451       Source := Save_Src;
4452       return Token_Node;
4453    end Real_Convert;
4454
4455    ------------------------------
4456    -- Requires_Transient_Scope --
4457    ------------------------------
4458
4459    --  A transient scope is required when variable-sized temporaries are
4460    --  allocated in the primary or secondary stack, or when finalization
4461    --  actions must be generated before the next instruction
4462
4463    function Requires_Transient_Scope (Id : Entity_Id) return Boolean is
4464       Typ : constant Entity_Id := Underlying_Type (Id);
4465
4466    begin
4467       --  This is a private type which is not completed yet. This can only
4468       --  happen in a default expression (of a formal parameter or of a
4469       --  record component). Do not expand transient scope in this case
4470
4471       if No (Typ) then
4472          return False;
4473
4474       elsif Typ = Standard_Void_Type then
4475          return False;
4476
4477       --  The back-end has trouble allocating variable-size temporaries so
4478       --  we generate them in the front-end and need a transient scope to
4479       --  reclaim them properly
4480
4481       elsif not Size_Known_At_Compile_Time (Typ) then
4482          return True;
4483
4484       --  Unconstrained discriminated records always require a variable
4485       --  length temporary, since the length may depend on the variant.
4486
4487       elsif Is_Record_Type (Typ)
4488         and then Has_Discriminants (Typ)
4489         and then not Is_Constrained (Typ)
4490       then
4491          return True;
4492
4493       --  Functions returning tagged types may dispatch on result so their
4494       --  returned value is allocated on the secondary stack. Controlled
4495       --  type temporaries need finalization.
4496
4497       elsif Is_Tagged_Type (Typ)
4498         or else Has_Controlled_Component (Typ)
4499       then
4500          return True;
4501
4502       --  Unconstrained array types are returned on the secondary stack
4503
4504       elsif Is_Array_Type (Typ) then
4505          return not Is_Constrained (Typ);
4506       end if;
4507
4508       return False;
4509    end Requires_Transient_Scope;
4510
4511    --------------------------
4512    -- Reset_Analyzed_Flags --
4513    --------------------------
4514
4515    procedure Reset_Analyzed_Flags (N : Node_Id) is
4516
4517       function Clear_Analyzed
4518         (N    : Node_Id)
4519          return Traverse_Result;
4520       --  Function used to reset Analyzed flags in tree. Note that we do
4521       --  not reset Analyzed flags in entities, since there is no need to
4522       --  renalalyze entities, and indeed, it is wrong to do so, since it
4523       --  can result in generating auxiliary stuff more than once.
4524
4525       function Clear_Analyzed
4526         (N    : Node_Id)
4527          return Traverse_Result
4528       is
4529       begin
4530          if not Has_Extension (N) then
4531             Set_Analyzed (N, False);
4532          end if;
4533
4534          return OK;
4535       end Clear_Analyzed;
4536
4537       function Reset_Analyzed is
4538         new Traverse_Func (Clear_Analyzed);
4539
4540       Discard : Traverse_Result;
4541
4542    --  Start of processing for Reset_Analyzed_Flags
4543
4544    begin
4545       Discard := Reset_Analyzed (N);
4546    end Reset_Analyzed_Flags;
4547
4548    ---------------
4549    -- Same_Name --
4550    ---------------
4551
4552    function Same_Name (N1, N2 : Node_Id) return Boolean is
4553       K1 : constant Node_Kind := Nkind (N1);
4554       K2 : constant Node_Kind := Nkind (N2);
4555
4556    begin
4557       if (K1 = N_Identifier or else K1 = N_Defining_Identifier)
4558         and then (K2 = N_Identifier or else K2 = N_Defining_Identifier)
4559       then
4560          return Chars (N1) = Chars (N2);
4561
4562       elsif (K1 = N_Selected_Component or else K1 = N_Expanded_Name)
4563         and then (K2 = N_Selected_Component or else K2 = N_Expanded_Name)
4564       then
4565          return Same_Name (Selector_Name (N1), Selector_Name (N2))
4566            and then Same_Name (Prefix (N1), Prefix (N2));
4567
4568       else
4569          return False;
4570       end if;
4571    end Same_Name;
4572
4573    ---------------
4574    -- Same_Type --
4575    ---------------
4576
4577    function Same_Type (T1, T2 : Entity_Id) return Boolean is
4578    begin
4579       if T1 = T2 then
4580          return True;
4581
4582       elsif not Is_Constrained (T1)
4583         and then not Is_Constrained (T2)
4584         and then Base_Type (T1) = Base_Type (T2)
4585       then
4586          return True;
4587
4588       --  For now don't bother with case of identical constraints, to be
4589       --  fiddled with later on perhaps (this is only used for optimization
4590       --  purposes, so it is not critical to do a best possible job)
4591
4592       else
4593          return False;
4594       end if;
4595    end Same_Type;
4596
4597    ------------------------
4598    -- Scope_Is_Transient --
4599    ------------------------
4600
4601    function Scope_Is_Transient  return Boolean is
4602    begin
4603       return Scope_Stack.Table (Scope_Stack.Last).Is_Transient;
4604    end Scope_Is_Transient;
4605
4606    ------------------
4607    -- Scope_Within --
4608    ------------------
4609
4610    function Scope_Within (Scope1, Scope2 : Entity_Id) return Boolean is
4611       Scop : Entity_Id;
4612
4613    begin
4614       Scop := Scope1;
4615       while Scop /= Standard_Standard loop
4616          Scop := Scope (Scop);
4617
4618          if Scop = Scope2 then
4619             return True;
4620          end if;
4621       end loop;
4622
4623       return False;
4624    end Scope_Within;
4625
4626    --------------------------
4627    -- Scope_Within_Or_Same --
4628    --------------------------
4629
4630    function Scope_Within_Or_Same (Scope1, Scope2 : Entity_Id) return Boolean is
4631       Scop : Entity_Id;
4632
4633    begin
4634       Scop := Scope1;
4635       while Scop /= Standard_Standard loop
4636          if Scop = Scope2 then
4637             return True;
4638          else
4639             Scop := Scope (Scop);
4640          end if;
4641       end loop;
4642
4643       return False;
4644    end Scope_Within_Or_Same;
4645
4646    ------------------------
4647    -- Set_Current_Entity --
4648    ------------------------
4649
4650    --  The given entity is to be set as the currently visible definition
4651    --  of its associated name (i.e. the Node_Id associated with its name).
4652    --  All we have to do is to get the name from the identifier, and
4653    --  then set the associated Node_Id to point to the given entity.
4654
4655    procedure Set_Current_Entity (E : Entity_Id) is
4656    begin
4657       Set_Name_Entity_Id (Chars (E), E);
4658    end Set_Current_Entity;
4659
4660    ---------------------------------
4661    -- Set_Entity_With_Style_Check --
4662    ---------------------------------
4663
4664    procedure Set_Entity_With_Style_Check (N : Node_Id; Val : Entity_Id) is
4665       Val_Actual : Entity_Id;
4666       Nod        : Node_Id;
4667
4668    begin
4669       Set_Entity (N, Val);
4670
4671       if Style_Check
4672         and then not Suppress_Style_Checks (Val)
4673         and then not In_Instance
4674       then
4675          if Nkind (N) = N_Identifier then
4676             Nod := N;
4677
4678          elsif Nkind (N) = N_Expanded_Name then
4679             Nod := Selector_Name (N);
4680
4681          else
4682             return;
4683          end if;
4684
4685          Val_Actual := Val;
4686
4687          --  A special situation arises for derived operations, where we want
4688          --  to do the check against the parent (since the Sloc of the derived
4689          --  operation points to the derived type declaration itself).
4690
4691          while not Comes_From_Source (Val_Actual)
4692            and then Nkind (Val_Actual) in N_Entity
4693            and then (Ekind (Val_Actual) = E_Enumeration_Literal
4694                       or else Ekind (Val_Actual) = E_Function
4695                       or else Ekind (Val_Actual) = E_Generic_Function
4696                       or else Ekind (Val_Actual) = E_Procedure
4697                       or else Ekind (Val_Actual) = E_Generic_Procedure)
4698            and then Present (Alias (Val_Actual))
4699          loop
4700             Val_Actual := Alias (Val_Actual);
4701          end loop;
4702
4703          --  Renaming declarations for generic actuals do not come from source,
4704          --  and have a different name from that of the entity they rename, so
4705          --  there is no style check to perform here.
4706
4707          if Chars (Nod) = Chars (Val_Actual) then
4708             Style.Check_Identifier (Nod, Val_Actual);
4709          end if;
4710
4711       end if;
4712
4713       Set_Entity (N, Val);
4714    end Set_Entity_With_Style_Check;
4715
4716    ------------------------
4717    -- Set_Name_Entity_Id --
4718    ------------------------
4719
4720    procedure Set_Name_Entity_Id (Id : Name_Id; Val : Entity_Id) is
4721    begin
4722       Set_Name_Table_Info (Id, Int (Val));
4723    end Set_Name_Entity_Id;
4724
4725    ---------------------
4726    -- Set_Next_Actual --
4727    ---------------------
4728
4729    procedure Set_Next_Actual (Ass1_Id : Node_Id; Ass2_Id : Node_Id) is
4730    begin
4731       if Nkind (Parent (Ass1_Id)) = N_Parameter_Association then
4732          Set_First_Named_Actual (Parent (Ass1_Id), Ass2_Id);
4733       end if;
4734    end Set_Next_Actual;
4735
4736    -----------------------
4737    -- Set_Public_Status --
4738    -----------------------
4739
4740    procedure Set_Public_Status (Id : Entity_Id) is
4741       S : constant Entity_Id := Current_Scope;
4742
4743    begin
4744       if S = Standard_Standard
4745         or else (Is_Public (S)
4746                   and then (Ekind (S) = E_Package
4747                              or else Is_Record_Type (S)
4748                              or else Ekind (S) = E_Void))
4749       then
4750          Set_Is_Public (Id);
4751
4752       --  The bounds of an entry family declaration can generate object
4753       --  declarations that are visible to the back-end, e.g. in the
4754       --  the declaration of a composite type that contains tasks.
4755
4756       elsif Is_Public (S)
4757         and then Is_Concurrent_Type (S)
4758         and then not Has_Completion (S)
4759         and then Nkind (Parent (Id)) = N_Object_Declaration
4760       then
4761          Set_Is_Public (Id);
4762       end if;
4763    end Set_Public_Status;
4764
4765    ----------------------------
4766    -- Set_Scope_Is_Transient --
4767    ----------------------------
4768
4769    procedure Set_Scope_Is_Transient (V : Boolean := True) is
4770    begin
4771       Scope_Stack.Table (Scope_Stack.Last).Is_Transient := V;
4772    end Set_Scope_Is_Transient;
4773
4774    -------------------
4775    -- Set_Size_Info --
4776    -------------------
4777
4778    procedure Set_Size_Info (T1, T2 : Entity_Id) is
4779    begin
4780       --  We copy Esize, but not RM_Size, since in general RM_Size is
4781       --  subtype specific and does not get inherited by all subtypes.
4782
4783       Set_Esize                     (T1, Esize                     (T2));
4784       Set_Has_Biased_Representation (T1, Has_Biased_Representation (T2));
4785
4786       if Is_Discrete_Or_Fixed_Point_Type (T1)
4787            and then
4788          Is_Discrete_Or_Fixed_Point_Type (T2)
4789       then
4790          Set_Is_Unsigned_Type       (T1, Is_Unsigned_Type          (T2));
4791       end if;
4792
4793       Set_Alignment                 (T1, Alignment                 (T2));
4794    end Set_Size_Info;
4795
4796    --------------------
4797    -- Static_Integer --
4798    --------------------
4799
4800    function Static_Integer (N : Node_Id) return Uint is
4801    begin
4802       Analyze_And_Resolve (N, Any_Integer);
4803
4804       if N = Error
4805         or else Error_Posted (N)
4806         or else Etype (N) = Any_Type
4807       then
4808          return No_Uint;
4809       end if;
4810
4811       if Is_Static_Expression (N) then
4812          if not Raises_Constraint_Error (N) then
4813             return Expr_Value (N);
4814          else
4815             return No_Uint;
4816          end if;
4817
4818       elsif Etype (N) = Any_Type then
4819          return No_Uint;
4820
4821       else
4822          Error_Msg_N ("static integer expression required here", N);
4823          return No_Uint;
4824       end if;
4825    end Static_Integer;
4826
4827    --------------------------
4828    -- Statically_Different --
4829    --------------------------
4830
4831    function Statically_Different (E1, E2 : Node_Id) return Boolean is
4832       R1 : constant Node_Id := Get_Referenced_Object (E1);
4833       R2 : constant Node_Id := Get_Referenced_Object (E2);
4834
4835    begin
4836       return     Is_Entity_Name (R1)
4837         and then Is_Entity_Name (R2)
4838         and then Entity (R1) /= Entity (R2)
4839         and then not Is_Formal (Entity (R1))
4840         and then not Is_Formal (Entity (R2));
4841    end Statically_Different;
4842
4843    -----------------------------
4844    -- Subprogram_Access_Level --
4845    -----------------------------
4846
4847    function Subprogram_Access_Level (Subp : Entity_Id) return Uint is
4848    begin
4849       if Present (Alias (Subp)) then
4850          return Subprogram_Access_Level (Alias (Subp));
4851       else
4852          return Scope_Depth (Enclosing_Dynamic_Scope (Subp));
4853       end if;
4854    end Subprogram_Access_Level;
4855
4856    -----------------
4857    -- Trace_Scope --
4858    -----------------
4859
4860    procedure Trace_Scope (N : Node_Id; E : Entity_Id; Msg : String) is
4861    begin
4862       if Debug_Flag_W then
4863          for J in 0 .. Scope_Stack.Last loop
4864             Write_Str ("  ");
4865          end loop;
4866
4867          Write_Str (Msg);
4868          Write_Name (Chars (E));
4869          Write_Str ("   line ");
4870          Write_Int (Int (Get_Logical_Line_Number (Sloc (N))));
4871          Write_Eol;
4872       end if;
4873    end Trace_Scope;
4874
4875    -----------------------
4876    -- Transfer_Entities --
4877    -----------------------
4878
4879    procedure Transfer_Entities (From : Entity_Id; To : Entity_Id) is
4880       Ent      : Entity_Id := First_Entity (From);
4881
4882    begin
4883       if No (Ent) then
4884          return;
4885       end if;
4886
4887       if (Last_Entity (To)) = Empty then
4888          Set_First_Entity (To, Ent);
4889       else
4890          Set_Next_Entity (Last_Entity (To), Ent);
4891       end if;
4892
4893       Set_Last_Entity (To, Last_Entity (From));
4894
4895       while Present (Ent) loop
4896          Set_Scope (Ent, To);
4897
4898          if not Is_Public (Ent) then
4899             Set_Public_Status (Ent);
4900
4901             if Is_Public (Ent)
4902               and then Ekind (Ent) = E_Record_Subtype
4903
4904             then
4905                --  The components of the propagated Itype must be public
4906                --  as well.
4907
4908                declare
4909                   Comp : Entity_Id;
4910
4911                begin
4912                   Comp := First_Entity (Ent);
4913
4914                   while Present (Comp) loop
4915                      Set_Is_Public (Comp);
4916                      Next_Entity (Comp);
4917                   end loop;
4918                end;
4919             end if;
4920          end if;
4921
4922          Next_Entity (Ent);
4923       end loop;
4924
4925       Set_First_Entity (From, Empty);
4926       Set_Last_Entity (From, Empty);
4927    end Transfer_Entities;
4928
4929    -----------------------
4930    -- Type_Access_Level --
4931    -----------------------
4932
4933    function Type_Access_Level (Typ : Entity_Id) return Uint is
4934       Btyp : Entity_Id := Base_Type (Typ);
4935
4936    begin
4937       --  If the type is an anonymous access type we treat it as being
4938       --  declared at the library level to ensure that names such as
4939       --  X.all'access don't fail static accessibility checks.
4940
4941       if Ekind (Btyp) in Access_Kind then
4942          if Ekind (Btyp) = E_Anonymous_Access_Type then
4943             return Scope_Depth (Standard_Standard);
4944          end if;
4945
4946          Btyp := Root_Type (Btyp);
4947       end if;
4948
4949       return Scope_Depth (Enclosing_Dynamic_Scope (Btyp));
4950    end Type_Access_Level;
4951
4952    --------------------------
4953    -- Unit_Declaration_Node --
4954    --------------------------
4955
4956    function Unit_Declaration_Node (Unit_Id : Entity_Id) return Node_Id is
4957       N : Node_Id := Parent (Unit_Id);
4958
4959    begin
4960       --  Predefined operators do not have a full function declaration.
4961
4962       if Ekind (Unit_Id) = E_Operator then
4963          return N;
4964       end if;
4965
4966       while Nkind (N) /= N_Abstract_Subprogram_Declaration
4967         and then Nkind (N) /= N_Formal_Package_Declaration
4968         and then Nkind (N) /= N_Formal_Subprogram_Declaration
4969         and then Nkind (N) /= N_Function_Instantiation
4970         and then Nkind (N) /= N_Generic_Package_Declaration
4971         and then Nkind (N) /= N_Generic_Subprogram_Declaration
4972         and then Nkind (N) /= N_Package_Declaration
4973         and then Nkind (N) /= N_Package_Body
4974         and then Nkind (N) /= N_Package_Instantiation
4975         and then Nkind (N) /= N_Package_Renaming_Declaration
4976         and then Nkind (N) /= N_Procedure_Instantiation
4977         and then Nkind (N) /= N_Subprogram_Declaration
4978         and then Nkind (N) /= N_Subprogram_Body
4979         and then Nkind (N) /= N_Subprogram_Body_Stub
4980         and then Nkind (N) /= N_Subprogram_Renaming_Declaration
4981         and then Nkind (N) /= N_Task_Body
4982         and then Nkind (N) /= N_Task_Type_Declaration
4983         and then Nkind (N) not in N_Generic_Renaming_Declaration
4984       loop
4985          N := Parent (N);
4986          pragma Assert (Present (N));
4987       end loop;
4988
4989       return N;
4990    end Unit_Declaration_Node;
4991
4992    ----------------------
4993    -- Within_Init_Proc --
4994    ----------------------
4995
4996    function Within_Init_Proc return Boolean is
4997       S : Entity_Id;
4998
4999    begin
5000       S := Current_Scope;
5001       while not Is_Overloadable (S) loop
5002          if S = Standard_Standard then
5003             return False;
5004          else
5005             S := Scope (S);
5006          end if;
5007       end loop;
5008
5009       return Chars (S) = Name_uInit_Proc;
5010    end Within_Init_Proc;
5011
5012    ----------------
5013    -- Wrong_Type --
5014    ----------------
5015
5016    procedure Wrong_Type (Expr : Node_Id; Expected_Type : Entity_Id) is
5017       Found_Type : constant Entity_Id := First_Subtype (Etype (Expr));
5018       Expec_Type : constant Entity_Id := First_Subtype (Expected_Type);
5019
5020       function Has_One_Matching_Field return Boolean;
5021       --  Determines whether Expec_Type is a record type with a single
5022       --  component or discriminant whose type matches the found type or
5023       --  is a one dimensional array whose component type matches the
5024       --  found type.
5025
5026       function Has_One_Matching_Field return Boolean is
5027          E : Entity_Id;
5028
5029       begin
5030          if Is_Array_Type (Expec_Type)
5031            and then Number_Dimensions (Expec_Type) = 1
5032            and then
5033              Covers (Etype (Component_Type (Expec_Type)), Found_Type)
5034          then
5035             return True;
5036
5037          elsif not Is_Record_Type (Expec_Type) then
5038             return False;
5039
5040          else
5041             E := First_Entity (Expec_Type);
5042
5043             loop
5044                if No (E) then
5045                   return False;
5046
5047                elsif (Ekind (E) /= E_Discriminant
5048                        and then Ekind (E) /= E_Component)
5049                  or else (Chars (E) = Name_uTag
5050                            or else Chars (E) = Name_uParent)
5051                then
5052                   Next_Entity (E);
5053
5054                else
5055                   exit;
5056                end if;
5057             end loop;
5058
5059             if not Covers (Etype (E), Found_Type) then
5060                return False;
5061
5062             elsif Present (Next_Entity (E)) then
5063                return False;
5064
5065             else
5066                return True;
5067             end if;
5068          end if;
5069       end Has_One_Matching_Field;
5070
5071    --  Start of processing for Wrong_Type
5072
5073    begin
5074       --  Don't output message if either type is Any_Type, or if a message
5075       --  has already been posted for this node. We need to do the latter
5076       --  check explicitly (it is ordinarily done in Errout), because we
5077       --  are using ! to force the output of the error messages.
5078
5079       if Expec_Type = Any_Type
5080         or else Found_Type = Any_Type
5081         or else Error_Posted (Expr)
5082       then
5083          return;
5084
5085       --  In  an instance, there is an ongoing problem with completion of
5086       --  type derived from private types. Their structure is what Gigi
5087       --  expects, but the  Etype is the parent type rather than the
5088       --  derived private type itself. Do not flag error in this case. The
5089       --  private completion is an entity without a parent, like an Itype.
5090       --  Similarly, full and partial views may be incorrect in the instance.
5091       --  There is no simple way to insure that it is consistent ???
5092
5093       elsif In_Instance then
5094
5095          if Etype (Etype (Expr)) = Etype (Expected_Type)
5096            and then No (Parent (Expected_Type))
5097          then
5098             return;
5099          end if;
5100       end if;
5101
5102       --  An interesting special check. If the expression is parenthesized
5103       --  and its type corresponds to the type of the sole component of the
5104       --  expected record type, or to the component type of the expected one
5105       --  dimensional array type, then assume we have a bad aggregate attempt.
5106
5107       if Nkind (Expr) in N_Subexpr
5108         and then Paren_Count (Expr) /= 0
5109         and then Has_One_Matching_Field
5110       then
5111          Error_Msg_N ("positional aggregate cannot have one component", Expr);
5112
5113       --  Another special check, if we are looking for a pool-specific access
5114       --  type and we found an E_Access_Attribute_Type, then we have the case
5115       --  of an Access attribute being used in a context which needs a pool-
5116       --  specific type, which is never allowed. The one extra check we make
5117       --  is that the expected designated type covers the Found_Type.
5118
5119       elsif Is_Access_Type (Expec_Type)
5120         and then Ekind (Found_Type) = E_Access_Attribute_Type
5121         and then Ekind (Base_Type (Expec_Type)) /= E_General_Access_Type
5122         and then Ekind (Base_Type (Expec_Type)) /= E_Anonymous_Access_Type
5123         and then Covers
5124           (Designated_Type (Expec_Type), Designated_Type (Found_Type))
5125       then
5126          Error_Msg_N ("result must be general access type!", Expr);
5127          Error_Msg_NE ("add ALL to }!", Expr, Expec_Type);
5128
5129       --  If the expected type is an anonymous access type, as for access
5130       --  parameters and discriminants, the error is on the designated types.
5131
5132       elsif Ekind (Expec_Type) = E_Anonymous_Access_Type then
5133          if Comes_From_Source (Expec_Type) then
5134             Error_Msg_NE ("expected}!", Expr, Expec_Type);
5135          else
5136             Error_Msg_NE
5137               ("expected an access type with designated}",
5138                  Expr, Designated_Type (Expec_Type));
5139          end if;
5140
5141          if Is_Access_Type (Found_Type)
5142            and then not Comes_From_Source (Found_Type)
5143          then
5144             Error_Msg_NE
5145               ("found an access type with designated}!",
5146                 Expr, Designated_Type (Found_Type));
5147          else
5148             if From_With_Type (Found_Type) then
5149                Error_Msg_NE ("found incomplete}!", Expr, Found_Type);
5150                Error_Msg_NE
5151                  ("\possibly missing with_clause on&", Expr,
5152                    Scope (Found_Type));
5153             else
5154                Error_Msg_NE ("found}!", Expr, Found_Type);
5155             end if;
5156          end if;
5157
5158       --  Normal case of one type found, some other type expected
5159
5160       else
5161          --  If the names of the two types are the same, see if some
5162          --  number of levels of qualification will help. Don't try
5163          --  more than three levels, and if we get to standard, it's
5164          --  no use (and probably represents an error in the compiler)
5165          --  Also do not bother with internal scope names.
5166
5167          declare
5168             Expec_Scope : Entity_Id;
5169             Found_Scope : Entity_Id;
5170
5171          begin
5172             Expec_Scope := Expec_Type;
5173             Found_Scope := Found_Type;
5174
5175             for Levels in Int range 0 .. 3 loop
5176                if Chars (Expec_Scope) /= Chars (Found_Scope) then
5177                   Error_Msg_Qual_Level := Levels;
5178                   exit;
5179                end if;
5180
5181                Expec_Scope := Scope (Expec_Scope);
5182                Found_Scope := Scope (Found_Scope);
5183
5184                exit when Expec_Scope = Standard_Standard
5185                            or else
5186                          Found_Scope = Standard_Standard
5187                            or else
5188                          not Comes_From_Source (Expec_Scope)
5189                            or else
5190                          not Comes_From_Source (Found_Scope);
5191             end loop;
5192          end;
5193
5194          Error_Msg_NE ("expected}!", Expr, Expec_Type);
5195
5196          if Is_Entity_Name (Expr)
5197            and then Is_Package (Entity (Expr))
5198          then
5199             Error_Msg_N ("found package name!", Expr);
5200
5201          elsif Is_Entity_Name (Expr)
5202            and then
5203              (Ekind (Entity (Expr)) = E_Procedure
5204                 or else
5205               Ekind (Entity (Expr)) = E_Generic_Procedure)
5206          then
5207             Error_Msg_N ("found procedure name instead of function!", Expr);
5208
5209          --  catch common error: a prefix or infix operator which is not
5210          --  directly visible because the type isn't.
5211
5212          elsif Nkind (Expr) in N_Op
5213             and then Is_Overloaded (Expr)
5214             and then not Is_Immediately_Visible (Expec_Type)
5215             and then not Is_Potentially_Use_Visible (Expec_Type)
5216             and then not In_Use (Expec_Type)
5217             and then Has_Compatible_Type (Right_Opnd (Expr), Expec_Type)
5218          then
5219             Error_Msg_N (
5220               "operator of the type is not directly visible!", Expr);
5221
5222          else
5223             Error_Msg_NE ("found}!", Expr, Found_Type);
5224          end if;
5225
5226          Error_Msg_Qual_Level := 0;
5227       end if;
5228    end Wrong_Type;
5229
5230 end Sem_Util;