OSDN Git Service

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