OSDN Git Service

2011-08-05 Hristian Kirtchev <kirtchev@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / sem_cat.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                              S E M _ C A T                               --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2011, Free Software Foundation, Inc.         --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 3,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license.          --
20 --                                                                          --
21 -- GNAT was originally developed  by the GNAT team at  New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
23 --                                                                          --
24 ------------------------------------------------------------------------------
25
26 with Atree;    use Atree;
27 with Debug;    use Debug;
28 with Einfo;    use Einfo;
29 with Elists;   use Elists;
30 with Errout;   use Errout;
31 with Exp_Disp; use Exp_Disp;
32 with Fname;    use Fname;
33 with Lib;      use Lib;
34 with Namet;    use Namet;
35 with Nlists;   use Nlists;
36 with Opt;      use Opt;
37 with Sem;      use Sem;
38 with Sem_Attr; use Sem_Attr;
39 with Sem_Aux;  use Sem_Aux;
40 with Sem_Eval; use Sem_Eval;
41 with Sem_Util; use Sem_Util;
42 with Sinfo;    use Sinfo;
43 with Snames;   use Snames;
44 with Stand;    use Stand;
45
46 package body Sem_Cat is
47
48    -----------------------
49    -- Local Subprograms --
50    -----------------------
51
52    procedure Check_Categorization_Dependencies
53      (Unit_Entity     : Entity_Id;
54       Depended_Entity : Entity_Id;
55       Info_Node       : Node_Id;
56       Is_Subunit      : Boolean);
57    --  This procedure checks that the categorization of a lib unit and that
58    --  of the depended unit satisfy dependency restrictions.
59    --  The depended_entity can be the entity in a with_clause item, in which
60    --  case Info_Node denotes that item. The depended_entity can also be the
61    --  parent unit of a child unit, in which case Info_Node is the declaration
62    --  of the child unit.  The error message is posted on Info_Node, and is
63    --  specialized if Is_Subunit is true.
64
65    procedure Check_Non_Static_Default_Expr
66      (Type_Def : Node_Id;
67       Obj_Decl : Node_Id);
68    --  Iterate through the component list of a record definition, check
69    --  that no component is declared with a nonstatic default value.
70    --  If a nonstatic default exists, report an error on Obj_Decl.
71
72    function Has_Read_Write_Attributes (E : Entity_Id) return Boolean;
73    --  Return True if entity has attribute definition clauses for Read and
74    --  Write attributes that are visible at some place.
75
76    function Is_Non_Remote_Access_Type (E : Entity_Id) return Boolean;
77    --  Returns true if the entity is a type whose full view is a non-remote
78    --  access type, for the purpose of enforcing E.2.2(8) rules.
79
80    function Has_Non_Remote_Access (Typ : Entity_Id) return Boolean;
81    --  Return true if Typ or the type of any of its subcomponents is a non
82    --  remote access type and doesn't have user-defined stream attributes.
83
84    function No_External_Streaming (E : Entity_Id) return Boolean;
85    --  Return True if the entity or one of its subcomponents does not support
86    --  external streaming.
87
88    function In_RCI_Declaration (N : Node_Id) return Boolean;
89    --  Determines if a declaration is  within the visible part of a Remote
90    --  Call Interface compilation unit, for semantic checking purposes only
91    --  (returns false within an instance and within the package body).
92
93    function In_RT_Declaration return Boolean;
94    --  Determines if current scope is within the declaration of a Remote Types
95    --  unit, for semantic checking purposes.
96
97    function In_Shared_Passive_Unit return Boolean;
98    --  Determines if current scope is within a Shared Passive compilation unit
99
100    function Static_Discriminant_Expr (L : List_Id) return Boolean;
101    --  Iterate through the list of discriminants to check if any of them
102    --  contains non-static default expression, which is a violation in
103    --  a preelaborated library unit.
104
105    procedure Validate_Remote_Access_Object_Type_Declaration (T : Entity_Id);
106    --  Check validity of declaration if RCI or RT unit. It should not contain
107    --  the declaration of an access-to-object type unless it is a general
108    --  access type that designates a class-wide limited private type. There are
109    --  also constraints about the primitive subprograms of the class-wide type.
110    --  RM E.2 (9, 13, 14)
111
112    procedure Validate_RACW_Primitive
113      (Subp : Entity_Id;
114       RACW : Entity_Id);
115    --  Check legality of the declaration of primitive Subp of the designated
116    --  type of the given RACW type.
117
118    ---------------------------------------
119    -- Check_Categorization_Dependencies --
120    ---------------------------------------
121
122    procedure Check_Categorization_Dependencies
123      (Unit_Entity     : Entity_Id;
124       Depended_Entity : Entity_Id;
125       Info_Node       : Node_Id;
126       Is_Subunit      : Boolean)
127    is
128       N   : constant Node_Id := Info_Node;
129       Err : Boolean;
130
131       --  Here we define an enumeration type to represent categorization types,
132       --  ordered so that a unit with a given categorization can only WITH
133       --  units with lower or equal categorization type.
134
135       type Categorization is
136         (Pure,
137          Shared_Passive,
138          Remote_Types,
139          Remote_Call_Interface,
140          Normal);
141
142       function Get_Categorization (E : Entity_Id) return Categorization;
143       --  Check categorization flags from entity, and return in the form
144       --  of the lowest value of the Categorization type that applies to E.
145
146       ------------------------
147       -- Get_Categorization --
148       ------------------------
149
150       function Get_Categorization (E : Entity_Id) return Categorization is
151       begin
152          --  Get the lowest categorization that corresponds to E. Note that
153          --  nothing prevents several (different) categorization pragmas
154          --  to apply to the same library unit, in which case the unit has
155          --  all associated categories, so we need to be careful here to
156          --  check pragmas in proper Categorization order in order to
157          --  return the lowest applicable value.
158
159          --  Ignore Pure specification if set by pragma Pure_Function
160
161          if Is_Pure (E)
162            and then not
163              (Has_Pragma_Pure_Function (E) and not Has_Pragma_Pure (E))
164          then
165             return Pure;
166
167          elsif Is_Shared_Passive (E) then
168             return Shared_Passive;
169
170          elsif Is_Remote_Types (E) then
171             return Remote_Types;
172
173          elsif Is_Remote_Call_Interface (E) then
174             return Remote_Call_Interface;
175
176          else
177             return Normal;
178          end if;
179       end Get_Categorization;
180
181       Unit_Category : Categorization;
182       With_Category : Categorization;
183
184    --  Start of processing for Check_Categorization_Dependencies
185
186    begin
187       --  Intrinsic subprograms are preelaborated, so do not impose any
188       --  categorization dependencies.
189
190       if Is_Intrinsic_Subprogram (Depended_Entity) then
191          return;
192       end if;
193
194       --  First check 10.2.1 (11/1) rules on preelaborate packages
195
196       if Is_Preelaborated (Unit_Entity)
197         and then not Is_Preelaborated (Depended_Entity)
198         and then not Is_Pure (Depended_Entity)
199       then
200          Err := True;
201       else
202          Err := False;
203       end if;
204
205       --  Check categorization rules of RM E.2(5)
206
207       Unit_Category := Get_Categorization (Unit_Entity);
208       With_Category := Get_Categorization (Depended_Entity);
209
210       if With_Category > Unit_Category then
211
212          --  Special case: Remote_Types and Remote_Call_Interface are allowed
213          --  to WITH anything in the package body, per (RM E.2(5)).
214
215          if (Unit_Category = Remote_Types
216                or else Unit_Category = Remote_Call_Interface)
217            and then In_Package_Body (Unit_Entity)
218          then
219             null;
220
221          --  Special case: Remote_Types can depend on Preelaborated per
222          --  Ada 2005 AI 0206.
223
224          elsif Unit_Category = Remote_Types
225            and then Is_Preelaborated (Depended_Entity)
226          then
227             null;
228
229          --  All other cases, we do have an error
230
231          else
232             Err := True;
233          end if;
234       end if;
235
236       --  Here if we have an error
237
238       if Err then
239
240          --  These messages are warnings in GNAT mode or if the -gnateP switch
241          --  was set. Otherwise these are real errors for real illegalities.
242
243          --  The reason we suppress these errors in GNAT mode is that the run-
244          --  time has several instances of violations of the categorization
245          --  errors (e.g. Pure units withing Preelaborate units. All these
246          --  violations are harmless in the cases where we intend them, and
247          --  we suppress the warnings with Warnings (Off). In cases where we
248          --  do not intend the violation, warnings are errors in GNAT mode
249          --  anyway, so we will still get an error.
250
251          Error_Msg_Warn :=
252            Treat_Categorization_Errors_As_Warnings or GNAT_Mode;
253
254          --  Don't give error if main unit is not an internal unit, and the
255          --  unit generating the message is an internal unit. This is the
256          --  situation in which such messages would be ignored in any case,
257          --  so it is convenient not to generate them (since it causes
258          --  annoying interference with debugging).
259
260          if Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit))
261            and then not Is_Internal_File_Name (Unit_File_Name (Main_Unit))
262          then
263             return;
264
265          --  Subunit case
266
267          elsif Is_Subunit then
268             Error_Msg_NE
269               ("<subunit cannot depend on& " &
270                "(parent has wrong categorization)", N, Depended_Entity);
271
272          --  Normal unit, not subunit
273
274          else
275             Error_Msg_NE
276               ("<cannot depend on& " &
277                "(wrong categorization)", N, Depended_Entity);
278          end if;
279
280          --  Add further explanation for Pure/Preelaborate common cases
281
282          if Unit_Category = Pure then
283             Error_Msg_NE
284               ("\<pure unit cannot depend on non-pure unit",
285                N, Depended_Entity);
286
287          elsif Is_Preelaborated (Unit_Entity)
288            and then not Is_Preelaborated (Depended_Entity)
289            and then not Is_Pure (Depended_Entity)
290          then
291             Error_Msg_NE
292               ("\<preelaborated unit cannot depend on "
293                & "non-preelaborated unit",
294                N, Depended_Entity);
295          end if;
296       end if;
297    end Check_Categorization_Dependencies;
298
299    -----------------------------------
300    -- Check_Non_Static_Default_Expr --
301    -----------------------------------
302
303    procedure Check_Non_Static_Default_Expr
304      (Type_Def : Node_Id;
305       Obj_Decl : Node_Id)
306    is
307       Recdef         : Node_Id;
308       Component_Decl : Node_Id;
309
310    begin
311       if Nkind (Type_Def) = N_Derived_Type_Definition then
312          Recdef := Record_Extension_Part (Type_Def);
313
314          if No (Recdef) then
315             return;
316          end if;
317
318       else
319          Recdef := Type_Def;
320       end if;
321
322       --  Check that component declarations do not involve:
323
324       --    a. a non-static default expression, where the object is
325       --       declared to be default initialized.
326
327       --    b. a dynamic Itype (discriminants and constraints)
328
329       if Null_Present (Recdef) then
330          return;
331       else
332          Component_Decl := First (Component_Items (Component_List (Recdef)));
333       end if;
334
335       while Present (Component_Decl)
336         and then Nkind (Component_Decl) = N_Component_Declaration
337       loop
338          if Present (Expression (Component_Decl))
339            and then Nkind (Expression (Component_Decl)) /= N_Null
340            and then not Is_Static_Expression (Expression (Component_Decl))
341          then
342             Error_Msg_Sloc := Sloc (Component_Decl);
343             Error_Msg_F
344               ("object in preelaborated unit has non-static default#",
345                Obj_Decl);
346
347          --  Fix this later ???
348
349          --  elsif Has_Dynamic_Itype (Component_Decl) then
350          --     Error_Msg_N
351          --       ("dynamic type discriminant," &
352          --        " constraint in preelaborated unit",
353          --        Component_Decl);
354          end if;
355
356          Next (Component_Decl);
357       end loop;
358    end Check_Non_Static_Default_Expr;
359
360    ---------------------------
361    -- Has_Non_Remote_Access --
362    ---------------------------
363
364    function Has_Non_Remote_Access (Typ : Entity_Id) return Boolean is
365       Component : Entity_Id;
366       Comp_Type : Entity_Id;
367       U_Typ     : constant Entity_Id := Underlying_Type (Typ);
368
369    begin
370       if No (U_Typ) then
371          return False;
372
373       elsif Has_Read_Write_Attributes (Typ)
374         or else Has_Read_Write_Attributes (U_Typ)
375       then
376          return False;
377
378       elsif Is_Non_Remote_Access_Type (U_Typ) then
379          return True;
380       end if;
381
382       if Is_Record_Type (U_Typ) then
383          Component := First_Entity (U_Typ);
384          while Present (Component) loop
385             if not Is_Tag (Component) then
386                Comp_Type := Etype (Component);
387
388                if Has_Non_Remote_Access (Comp_Type) then
389                   return True;
390                end if;
391             end if;
392
393             Next_Entity (Component);
394          end loop;
395
396       elsif Is_Array_Type (U_Typ) then
397          return Has_Non_Remote_Access (Component_Type (U_Typ));
398
399       end if;
400
401       return False;
402    end Has_Non_Remote_Access;
403
404    -------------------------------
405    -- Has_Read_Write_Attributes --
406    -------------------------------
407
408    function Has_Read_Write_Attributes (E : Entity_Id) return Boolean is
409    begin
410       return True
411         and then Has_Stream_Attribute_Definition (E,
412                    TSS_Stream_Read,  At_Any_Place => True)
413         and then Has_Stream_Attribute_Definition (E,
414                    TSS_Stream_Write, At_Any_Place => True);
415    end Has_Read_Write_Attributes;
416
417    -------------------------------------
418    -- Has_Stream_Attribute_Definition --
419    -------------------------------------
420
421    function Has_Stream_Attribute_Definition
422      (Typ          : Entity_Id;
423       Nam          : TSS_Name_Type;
424       At_Any_Place : Boolean := False) return Boolean
425    is
426       Rep_Item  : Node_Id;
427       Full_Type : Entity_Id := Typ;
428
429    begin
430       --  In the case of a type derived from a private view, any specified
431       --  stream attributes will be attached to the derived type's underlying
432       --  type rather the derived type entity itself (which is itself private).
433
434       if Is_Private_Type (Typ)
435         and then Is_Derived_Type (Typ)
436         and then Present (Full_View (Typ))
437       then
438          Full_Type := Underlying_Type (Typ);
439       end if;
440
441       --  We start from the declaration node and then loop until the end of
442       --  the list until we find the requested attribute definition clause.
443       --  In Ada 2005 mode, clauses are ignored if they are not currently
444       --  visible (this is tested using the corresponding Entity, which is
445       --  inserted by the expander at the point where the clause occurs),
446       --  unless At_Any_Place is true.
447
448       Rep_Item := First_Rep_Item (Full_Type);
449       while Present (Rep_Item) loop
450          if Nkind (Rep_Item) = N_Attribute_Definition_Clause then
451             case Chars (Rep_Item) is
452                when Name_Read =>
453                   exit when Nam = TSS_Stream_Read;
454
455                when Name_Write =>
456                   exit when Nam = TSS_Stream_Write;
457
458                when Name_Input =>
459                   exit when Nam = TSS_Stream_Input;
460
461                when Name_Output =>
462                   exit when Nam = TSS_Stream_Output;
463
464                when others =>
465                   null;
466
467             end case;
468          end if;
469
470          Next_Rep_Item (Rep_Item);
471       end loop;
472
473       --  If At_Any_Place is true, return True if the attribute is available
474       --  at any place; if it is false, return True only if the attribute is
475       --  currently visible.
476
477       return Present (Rep_Item)
478         and then (Ada_Version < Ada_2005
479                    or else At_Any_Place
480                    or else not Is_Hidden (Entity (Rep_Item)));
481    end Has_Stream_Attribute_Definition;
482
483    ---------------------------
484    -- In_Preelaborated_Unit --
485    ---------------------------
486
487    function In_Preelaborated_Unit return Boolean is
488       Unit_Entity : constant Entity_Id := Current_Scope;
489       Unit_Kind   : constant Node_Kind :=
490                       Nkind (Unit (Cunit (Current_Sem_Unit)));
491
492    begin
493       --  There are no constraints on body of remote_call_interface or
494       --  remote_types packages.
495
496       return (Unit_Entity /= Standard_Standard)
497         and then (Is_Preelaborated (Unit_Entity)
498                     or else Is_Pure (Unit_Entity)
499                     or else Is_Shared_Passive (Unit_Entity)
500                     or else
501                       ((Is_Remote_Types (Unit_Entity)
502                                or else Is_Remote_Call_Interface (Unit_Entity))
503                          and then Ekind (Unit_Entity) = E_Package
504                          and then Unit_Kind /= N_Package_Body
505                          and then not In_Package_Body (Unit_Entity)
506                          and then not In_Instance));
507    end In_Preelaborated_Unit;
508
509    ------------------
510    -- In_Pure_Unit --
511    ------------------
512
513    function In_Pure_Unit return Boolean is
514    begin
515       return Is_Pure (Current_Scope);
516    end In_Pure_Unit;
517
518    ------------------------
519    -- In_RCI_Declaration --
520    ------------------------
521
522    function In_RCI_Declaration (N : Node_Id) return Boolean is
523       Unit_Entity : constant Entity_Id := Current_Scope;
524       Unit_Kind   : constant Node_Kind :=
525                       Nkind (Unit (Cunit (Current_Sem_Unit)));
526
527    begin
528       --  There are no restrictions on the private part or body
529       --  of an RCI unit.
530
531       return Is_Remote_Call_Interface (Unit_Entity)
532         and then Is_Package_Or_Generic_Package (Unit_Entity)
533         and then Unit_Kind /= N_Package_Body
534         and then List_Containing (N) =
535                   Visible_Declarations
536                     (Specification (Unit_Declaration_Node (Unit_Entity)))
537         and then not In_Package_Body (Unit_Entity)
538         and then not In_Instance;
539
540       --  What about the case of a nested package in the visible part???
541       --  This case is missed by the List_Containing check above???
542    end In_RCI_Declaration;
543
544    -----------------------
545    -- In_RT_Declaration --
546    -----------------------
547
548    function In_RT_Declaration return Boolean is
549       Unit_Entity : constant Entity_Id := Current_Scope;
550       Unit_Kind   : constant Node_Kind :=
551                       Nkind (Unit (Cunit (Current_Sem_Unit)));
552
553    begin
554       --  There are no restrictions on the body of a Remote Types unit
555
556       return Is_Remote_Types (Unit_Entity)
557         and then Is_Package_Or_Generic_Package (Unit_Entity)
558         and then Unit_Kind /= N_Package_Body
559         and then not In_Package_Body (Unit_Entity)
560         and then not In_Instance;
561    end In_RT_Declaration;
562
563    ----------------------------
564    -- In_Shared_Passive_Unit --
565    ----------------------------
566
567    function In_Shared_Passive_Unit return Boolean is
568       Unit_Entity : constant Entity_Id := Current_Scope;
569
570    begin
571       return Is_Shared_Passive (Unit_Entity);
572    end In_Shared_Passive_Unit;
573
574    ---------------------------------------
575    -- In_Subprogram_Task_Protected_Unit --
576    ---------------------------------------
577
578    function In_Subprogram_Task_Protected_Unit return Boolean is
579       E : Entity_Id;
580
581    begin
582       --  The following is to verify that a declaration is inside
583       --  subprogram, generic subprogram, task unit, protected unit.
584       --  Used to validate if a lib. unit is Pure. RM 10.2.1(16).
585
586       --  Use scope chain to check successively outer scopes
587
588       E := Current_Scope;
589       loop
590          if Is_Subprogram (E)
591               or else
592             Is_Generic_Subprogram (E)
593               or else
594             Is_Concurrent_Type (E)
595          then
596             return True;
597
598          elsif E = Standard_Standard then
599             return False;
600          end if;
601
602          E := Scope (E);
603       end loop;
604    end In_Subprogram_Task_Protected_Unit;
605
606    -------------------------------
607    -- Is_Non_Remote_Access_Type --
608    -------------------------------
609
610    function Is_Non_Remote_Access_Type (E : Entity_Id) return Boolean is
611       U_E : constant Entity_Id := Underlying_Type (E);
612    begin
613       if No (U_E) then
614
615          --  This case arises for the case of a generic formal type, in which
616          --  case E.2.2(8) rules will be enforced at instantiation time.
617
618          return False;
619       end if;
620
621       return Is_Access_Type (U_E)
622         and then not Is_Remote_Access_To_Class_Wide_Type (U_E)
623         and then not Is_Remote_Access_To_Subprogram_Type (U_E);
624    end Is_Non_Remote_Access_Type;
625
626    ---------------------------
627    -- No_External_Streaming --
628    ---------------------------
629
630    function No_External_Streaming (E : Entity_Id) return Boolean is
631       U_E : constant Entity_Id := Underlying_Type (E);
632
633    begin
634       if No (U_E) then
635          return False;
636
637       elsif Has_Read_Write_Attributes (E) then
638
639          --  Note: availability of stream attributes is tested on E, not U_E.
640          --  There may be stream attributes defined on U_E that are not visible
641          --  at the place where support of external streaming is tested.
642
643          return False;
644
645       elsif Has_Non_Remote_Access (U_E) then
646          return True;
647       end if;
648
649       return Is_Limited_Type (E);
650    end No_External_Streaming;
651
652    -------------------------------------
653    -- Set_Categorization_From_Pragmas --
654    -------------------------------------
655
656    procedure Set_Categorization_From_Pragmas (N : Node_Id) is
657       P   : constant Node_Id := Parent (N);
658       S   : constant Entity_Id := Current_Scope;
659
660       procedure Set_Parents (Visibility : Boolean);
661          --  If this is a child instance, the parents are not immediately
662          --  visible during analysis. Make them momentarily visible so that
663          --  the argument of the pragma can be resolved properly, and reset
664          --  afterwards.
665
666       -----------------
667       -- Set_Parents --
668       -----------------
669
670       procedure Set_Parents (Visibility : Boolean) is
671          Par : Entity_Id;
672       begin
673          Par := Scope (S);
674          while Present (Par) and then Par /= Standard_Standard loop
675             Set_Is_Immediately_Visible (Par, Visibility);
676             Par := Scope (Par);
677          end loop;
678       end Set_Parents;
679
680    --  Start of processing for Set_Categorization_From_Pragmas
681
682    begin
683       --  Deal with categorization pragmas in Pragmas of Compilation_Unit.
684       --  The purpose is to set categorization flags before analyzing the
685       --  unit itself, so as to diagnose violations of categorization as
686       --  we process each declaration, even though the pragma appears after
687       --  the unit.
688
689       if Nkind (P) /= N_Compilation_Unit then
690          return;
691       end if;
692
693       declare
694          PN : Node_Id;
695
696       begin
697          if Is_Child_Unit (S)
698            and then Is_Generic_Instance (S)
699          then
700             Set_Parents (True);
701          end if;
702
703          PN := First (Pragmas_After (Aux_Decls_Node (P)));
704          while Present (PN) loop
705
706             --  Skip implicit types that may have been introduced by
707             --  previous analysis.
708
709             if Nkind (PN) = N_Pragma then
710                case Get_Pragma_Id (PN) is
711                   when Pragma_All_Calls_Remote   |
712                     Pragma_Preelaborate          |
713                     Pragma_Pure                  |
714                     Pragma_Remote_Call_Interface |
715                     Pragma_Remote_Types          |
716                     Pragma_Shared_Passive        => Analyze (PN);
717                   when others                    => null;
718                end case;
719             end if;
720
721             Next (PN);
722          end loop;
723
724          if Is_Child_Unit (S)
725            and then Is_Generic_Instance (S)
726          then
727             Set_Parents (False);
728          end if;
729       end;
730    end Set_Categorization_From_Pragmas;
731
732    -----------------------------------
733    -- Set_Categorization_From_Scope --
734    -----------------------------------
735
736    procedure Set_Categorization_From_Scope (E : Entity_Id; Scop : Entity_Id) is
737       Declaration   : Node_Id := Empty;
738       Specification : Node_Id := Empty;
739
740    begin
741       Set_Is_Pure (E,
742         Is_Pure (Scop) and then Is_Library_Level_Entity (E));
743
744       if not Is_Remote_Call_Interface (E) then
745          if Ekind (E) in Subprogram_Kind then
746             Declaration := Unit_Declaration_Node (E);
747
748             if Nkind (Declaration) = N_Subprogram_Body
749                  or else
750                Nkind (Declaration) = N_Subprogram_Renaming_Declaration
751             then
752                Specification := Corresponding_Spec (Declaration);
753             end if;
754          end if;
755
756          --  A subprogram body or renaming-as-body is a remote call
757          --  interface if it serves as the completion of a subprogram
758          --  declaration that is a remote call interface.
759
760          if Nkind (Specification) in N_Entity then
761             Set_Is_Remote_Call_Interface
762               (E, Is_Remote_Call_Interface (Specification));
763
764          --  A subprogram declaration is a remote call interface when it is
765          --  declared within the visible part of, or declared by, a library
766          --  unit declaration that is a remote call interface.
767
768          else
769             Set_Is_Remote_Call_Interface
770               (E, Is_Remote_Call_Interface (Scop)
771                     and then not (In_Private_Part (Scop)
772                                     or else In_Package_Body (Scop)));
773          end if;
774       end if;
775
776       Set_Is_Remote_Types
777         (E, Is_Remote_Types (Scop)
778               and then not (In_Private_Part (Scop)
779                               or else In_Package_Body (Scop)));
780    end Set_Categorization_From_Scope;
781
782    ------------------------------
783    -- Static_Discriminant_Expr --
784    ------------------------------
785
786    --  We need to accommodate a Why_Not_Static call somehow here ???
787
788    function Static_Discriminant_Expr (L : List_Id) return Boolean is
789       Discriminant_Spec : Node_Id;
790
791    begin
792       Discriminant_Spec := First (L);
793       while Present (Discriminant_Spec) loop
794          if Present (Expression (Discriminant_Spec))
795            and then not Is_Static_Expression (Expression (Discriminant_Spec))
796          then
797             return False;
798          end if;
799
800          Next (Discriminant_Spec);
801       end loop;
802
803       return True;
804    end Static_Discriminant_Expr;
805
806    --------------------------------------
807    -- Validate_Access_Type_Declaration --
808    --------------------------------------
809
810    procedure Validate_Access_Type_Declaration (T : Entity_Id; N : Node_Id) is
811       Def : constant Node_Id := Type_Definition (N);
812
813    begin
814       case Nkind (Def) is
815
816          --  Access to subprogram case
817
818          when N_Access_To_Subprogram_Definition =>
819
820             --  A pure library_item must not contain the declaration of a
821             --  named access type, except within a subprogram, generic
822             --  subprogram, task unit, or protected unit (RM 10.2.1(16)).
823
824             --  This test is skipped in Ada 2005 (see AI-366)
825
826             if Ada_Version < Ada_2005
827               and then Comes_From_Source (T)
828               and then In_Pure_Unit
829               and then not In_Subprogram_Task_Protected_Unit
830             then
831                Error_Msg_N ("named access type not allowed in pure unit", T);
832             end if;
833
834          --  Access to object case
835
836          when N_Access_To_Object_Definition =>
837             if Comes_From_Source (T)
838               and then In_Pure_Unit
839               and then not In_Subprogram_Task_Protected_Unit
840             then
841                --  We can't give the message yet, since the type is not frozen
842                --  and in Ada 2005 mode, access types are allowed in pure units
843                --  if the type has no storage pool (see AI-366). So we set a
844                --  flag which will be checked at freeze time.
845
846                Set_Is_Pure_Unit_Access_Type (T);
847             end if;
848
849             --  Check for RCI or RT unit type declaration: declaration of an
850             --  access-to-object type is illegal unless it is a general access
851             --  type that designates a class-wide limited private type.
852             --  Note that constraints on the primitive subprograms of the
853             --  designated tagged type are not enforced here but in
854             --  Validate_RACW_Primitives, which is done separately because the
855             --  designated type might not be frozen (and therefore its
856             --  primitive operations might not be completely known) at the
857             --  point of the RACW declaration.
858
859             Validate_Remote_Access_Object_Type_Declaration (T);
860
861             --  Check for shared passive unit type declaration. It should
862             --  not contain the declaration of access to class wide type,
863             --  access to task type and access to protected type with entry.
864
865             Validate_SP_Access_Object_Type_Decl (T);
866
867          when others =>
868             null;
869       end case;
870
871       --  Set categorization flag from package on entity as well, to allow
872       --  easy checks later on for required validations of RCI or RT units.
873       --  This is only done for entities that are in the original source.
874
875       if Comes_From_Source (T)
876         and then not (In_Package_Body (Scope (T))
877                         or else In_Private_Part (Scope (T)))
878       then
879          Set_Is_Remote_Call_Interface
880            (T, Is_Remote_Call_Interface (Scope (T)));
881          Set_Is_Remote_Types
882            (T, Is_Remote_Types (Scope (T)));
883       end if;
884    end Validate_Access_Type_Declaration;
885
886    ----------------------------
887    -- Validate_Ancestor_Part --
888    ----------------------------
889
890    procedure Validate_Ancestor_Part (N : Node_Id) is
891       A : constant Node_Id   := Ancestor_Part (N);
892       T : constant Entity_Id := Entity (A);
893
894    begin
895       if In_Preelaborated_Unit
896         and then not In_Subprogram_Or_Concurrent_Unit
897         and then (not Inside_A_Generic
898                    or else Present (Enclosing_Generic_Body (N)))
899       then
900          --  If the type is private, it must have the Ada 2005 pragma
901          --  Has_Preelaborable_Initialization.
902          --  The check is omitted within predefined units. This is probably
903          --  obsolete code to fix the Ada95 weakness in this area ???
904
905          if Is_Private_Type (T)
906            and then not Has_Pragma_Preelab_Init (T)
907            and then not Is_Internal_File_Name
908                           (Unit_File_Name (Get_Source_Unit (N)))
909          then
910             Error_Msg_N
911               ("private ancestor type not allowed in preelaborated unit", A);
912
913          elsif Is_Record_Type (T) then
914             if Nkind (Parent (T)) = N_Full_Type_Declaration then
915                Check_Non_Static_Default_Expr
916                  (Type_Definition (Parent (T)), A);
917             end if;
918          end if;
919       end if;
920    end Validate_Ancestor_Part;
921
922    ----------------------------------------
923    -- Validate_Categorization_Dependency --
924    ----------------------------------------
925
926    procedure Validate_Categorization_Dependency
927      (N : Node_Id;
928       E : Entity_Id)
929    is
930       K          : constant Node_Kind := Nkind (N);
931       P          : Node_Id            := Parent (N);
932       U          : Entity_Id := E;
933       Is_Subunit : constant Boolean := Nkind (P) = N_Subunit;
934
935    begin
936       --  Only validate library units and subunits. For subunits, checks
937       --  concerning withed units apply to the parent compilation unit.
938
939       if Is_Subunit then
940          P := Parent (P);
941          U := Scope (E);
942
943          while Present (U)
944            and then not Is_Compilation_Unit (U)
945            and then not Is_Child_Unit (U)
946          loop
947             U := Scope (U);
948          end loop;
949       end if;
950
951       if Nkind (P) /= N_Compilation_Unit then
952          return;
953       end if;
954
955       --  Body of RCI unit does not need validation
956
957       if Is_Remote_Call_Interface (E)
958         and then (Nkind (N) = N_Package_Body
959                    or else Nkind (N) = N_Subprogram_Body)
960       then
961          return;
962       end if;
963
964       --  Ada 2005 (AI-50217): Process explicit non-limited with_clauses
965
966       declare
967          Item             : Node_Id;
968          Entity_Of_Withed : Entity_Id;
969
970       begin
971          Item := First (Context_Items (P));
972          while Present (Item) loop
973             if Nkind (Item) = N_With_Clause
974               and then not (Implicit_With (Item)
975                               or else Limited_Present (Item))
976             then
977                Entity_Of_Withed := Entity (Name (Item));
978                Check_Categorization_Dependencies
979                  (U, Entity_Of_Withed, Item, Is_Subunit);
980             end if;
981
982             Next (Item);
983          end loop;
984       end;
985
986       --  Child depends on parent; therefore parent should also be categorized
987       --  and satisfy the dependency hierarchy.
988
989       --  Check if N is a child spec
990
991       if (K in N_Generic_Declaration              or else
992           K in N_Generic_Instantiation            or else
993           K in N_Generic_Renaming_Declaration     or else
994           K =  N_Package_Declaration              or else
995           K =  N_Package_Renaming_Declaration     or else
996           K =  N_Subprogram_Declaration           or else
997           K =  N_Subprogram_Renaming_Declaration)
998         and then Present (Parent_Spec (N))
999       then
1000          Check_Categorization_Dependencies (E, Scope (E), N, False);
1001
1002          --  Verify that public child of an RCI library unit must also be an
1003          --  RCI library unit (RM E.2.3(15)).
1004
1005          if Is_Remote_Call_Interface (Scope (E))
1006            and then not Private_Present (P)
1007            and then not Is_Remote_Call_Interface (E)
1008          then
1009             Error_Msg_N ("public child of rci unit must also be rci unit", N);
1010          end if;
1011       end if;
1012    end Validate_Categorization_Dependency;
1013
1014    --------------------------------
1015    -- Validate_Controlled_Object --
1016    --------------------------------
1017
1018    procedure Validate_Controlled_Object (E : Entity_Id) is
1019    begin
1020       --  Don't need this check in Ada 2005 mode, where this is all taken
1021       --  care of by the mechanism for Preelaborable Initialization.
1022
1023       if Ada_Version >= Ada_2005 then
1024          return;
1025       end if;
1026
1027       --  For now, never apply this check for internal GNAT units, since we
1028       --  have a number of cases in the library where we are stuck with objects
1029       --  of this type, and the RM requires Preelaborate.
1030
1031       --  For similar reasons, we only do this check for source entities, since
1032       --  we generate entities of this type in some situations.
1033
1034       --  Note that the 10.2.1(9) restrictions are not relevant to us anyway.
1035       --  We have to enforce them for RM compatibility, but we have no trouble
1036       --  accepting these objects and doing the right thing. Note that there is
1037       --  no requirement that Preelaborate not actually generate any code!
1038
1039       if In_Preelaborated_Unit
1040         and then not Debug_Flag_PP
1041         and then Comes_From_Source (E)
1042         and then not
1043           Is_Internal_File_Name (Unit_File_Name (Get_Source_Unit (E)))
1044         and then (not Inside_A_Generic
1045                    or else Present (Enclosing_Generic_Body (E)))
1046         and then not Is_Protected_Type (Etype (E))
1047       then
1048          Error_Msg_N
1049            ("library level controlled object not allowed in " &
1050             "preelaborated unit", E);
1051       end if;
1052    end Validate_Controlled_Object;
1053
1054    --------------------------------------
1055    -- Validate_Null_Statement_Sequence --
1056    --------------------------------------
1057
1058    procedure Validate_Null_Statement_Sequence (N : Node_Id) is
1059       Item : Node_Id;
1060
1061    begin
1062       if In_Preelaborated_Unit then
1063          Item := First (Statements (Handled_Statement_Sequence (N)));
1064          while Present (Item) loop
1065             if Nkind (Item) /= N_Label
1066               and then Nkind (Item) /= N_Null_Statement
1067             then
1068                --  In GNAT mode, this is a warning, allowing the run-time
1069                --  to judiciously bypass this error condition.
1070
1071                Error_Msg_Warn := GNAT_Mode;
1072                Error_Msg_N
1073                  ("<statements not allowed in preelaborated unit", Item);
1074
1075                exit;
1076             end if;
1077
1078             Next (Item);
1079          end loop;
1080       end if;
1081    end Validate_Null_Statement_Sequence;
1082
1083    ---------------------------------
1084    -- Validate_Object_Declaration --
1085    ---------------------------------
1086
1087    procedure Validate_Object_Declaration (N : Node_Id) is
1088       Id  : constant Entity_Id  := Defining_Identifier (N);
1089       E   : constant Node_Id    := Expression (N);
1090       Odf : constant Node_Id    := Object_Definition (N);
1091       T   : constant Entity_Id  := Etype (Id);
1092
1093    begin
1094       --  Verify that any access to subprogram object does not have in its
1095       --  subprogram profile access type parameters or limited parameters
1096       --  without Read and Write attributes (E.2.3(13)).
1097
1098       Validate_RCI_Subprogram_Declaration (N);
1099
1100       --  Check that if we are in preelaborated elaboration code, then we
1101       --  do not have an instance of a default initialized private, task or
1102       --  protected object declaration which would violate (RM 10.2.1(9)).
1103       --  Note that constants are never default initialized (and the test
1104       --  below also filters out deferred constants). A variable is default
1105       --  initialized if it does *not* have an initialization expression.
1106
1107       --  Filter out cases that are not declaration of a variable from source
1108
1109       if Nkind (N) /= N_Object_Declaration
1110         or else Constant_Present (N)
1111         or else not Comes_From_Source (Id)
1112       then
1113          return;
1114       end if;
1115
1116       --  Exclude generic specs from the checks (this will get rechecked
1117       --  on instantiations).
1118
1119       if Inside_A_Generic and then No (Enclosing_Generic_Body (Id)) then
1120          return;
1121       end if;
1122
1123       --  Required checks for declaration that is in a preelaborated package
1124       --  and is not within some subprogram.
1125
1126       if In_Preelaborated_Unit
1127         and then not In_Subprogram_Or_Concurrent_Unit
1128       then
1129          --  Check for default initialized variable case. Note that in
1130          --  accordance with (RM B.1(24)) imported objects are not subject to
1131          --  default initialization.
1132          --  If the initialization does not come from source and is an
1133          --  aggregate, it is a static initialization that replaces an
1134          --  implicit call, and must be treated as such.
1135
1136          if Present (E)
1137            and then (Comes_From_Source (E) or else Nkind (E) /= N_Aggregate)
1138          then
1139             null;
1140
1141          elsif Is_Imported (Id) then
1142             null;
1143
1144          else
1145             declare
1146                Ent : Entity_Id := T;
1147
1148             begin
1149                --  An array whose component type is a record with nonstatic
1150                --  default expressions is a violation, so we get the array's
1151                --  component type.
1152
1153                if Is_Array_Type (Ent) then
1154                   declare
1155                      Comp_Type : Entity_Id;
1156
1157                   begin
1158                      Comp_Type := Component_Type (Ent);
1159                      while Is_Array_Type (Comp_Type) loop
1160                         Comp_Type := Component_Type (Comp_Type);
1161                      end loop;
1162
1163                      Ent := Comp_Type;
1164                   end;
1165                end if;
1166
1167                --  Object decl. that is of record type and has no default expr.
1168                --  should check if there is any non-static default expression
1169                --  in component decl. of the record type decl.
1170
1171                if Is_Record_Type (Ent) then
1172                   if Nkind (Parent (Ent)) = N_Full_Type_Declaration then
1173                      Check_Non_Static_Default_Expr
1174                        (Type_Definition (Parent (Ent)), N);
1175
1176                   elsif Nkind (Odf) = N_Subtype_Indication
1177                     and then not Is_Array_Type (T)
1178                     and then not Is_Private_Type (T)
1179                   then
1180                      Check_Non_Static_Default_Expr (Type_Definition
1181                        (Parent (Entity (Subtype_Mark (Odf)))), N);
1182                   end if;
1183                end if;
1184
1185                --  Check for invalid use of private object. Note that Ada 2005
1186                --  AI-161 modifies the rules for Ada 2005, including the use of
1187                --  the new pragma Preelaborable_Initialization.
1188
1189                if Is_Private_Type (Ent)
1190                  or else Depends_On_Private (Ent)
1191                then
1192                   --  Case where type has preelaborable initialization which
1193                   --  means that a pragma Preelaborable_Initialization was
1194                   --  given for the private type.
1195
1196                   if Has_Preelaborable_Initialization (Ent) then
1197
1198                      --  But for the predefined units, we will ignore this
1199                      --  status unless we are in Ada 2005 mode since we want
1200                      --  Ada 95 compatible behavior, in which the entities
1201                      --  marked with this pragma in the predefined library are
1202                      --  not treated specially.
1203
1204                      if Ada_Version < Ada_2005 then
1205                         Error_Msg_N
1206                           ("private object not allowed in preelaborated unit",
1207                            N);
1208                         Error_Msg_N ("\(would be legal in Ada 2005 mode)", N);
1209                      end if;
1210
1211                   --  Type does not have preelaborable initialization
1212
1213                   else
1214                      --  We allow this when compiling in GNAT mode to make life
1215                      --  easier for some cases where it would otherwise be hard
1216                      --  to be exactly valid Ada.
1217
1218                      if not GNAT_Mode then
1219                         Error_Msg_N
1220                           ("private object not allowed in preelaborated unit",
1221                            N);
1222
1223                         --  Add a message if it would help to provide a pragma
1224                         --  Preelaborable_Initialization on the type of the
1225                         --  object (which would make it legal in Ada 2005).
1226
1227                         --  If the type has no full view (generic type, or
1228                         --  previous error), the warning does not apply.
1229
1230                         if Is_Private_Type (Ent)
1231                           and then Present (Full_View (Ent))
1232                           and then
1233                             Has_Preelaborable_Initialization (Full_View (Ent))
1234                         then
1235                            Error_Msg_Sloc := Sloc (Ent);
1236
1237                            if Ada_Version >= Ada_2005 then
1238                               Error_Msg_NE
1239                                 ("\would be legal if pragma Preelaborable_" &
1240                                  "Initialization given for & #", N, Ent);
1241                            else
1242                               Error_Msg_NE
1243                                 ("\would be legal in Ada 2005 if pragma " &
1244                                  "Preelaborable_Initialization given for & #",
1245                                  N, Ent);
1246                            end if;
1247                         end if;
1248                      end if;
1249                   end if;
1250
1251                --  Access to Task or Protected type
1252
1253                elsif Is_Entity_Name (Odf)
1254                  and then Present (Etype (Odf))
1255                  and then Is_Access_Type (Etype (Odf))
1256                then
1257                   Ent := Designated_Type (Etype (Odf));
1258
1259                elsif Is_Entity_Name (Odf) then
1260                   Ent := Entity (Odf);
1261
1262                elsif Nkind (Odf) = N_Subtype_Indication then
1263                   Ent := Etype (Subtype_Mark (Odf));
1264
1265                elsif Nkind (Odf) = N_Constrained_Array_Definition then
1266                   Ent := Component_Type (T);
1267                end if;
1268
1269                if Is_Task_Type (Ent)
1270                  or else (Is_Protected_Type (Ent) and then Has_Entries (Ent))
1271                then
1272                   Error_Msg_N
1273                     ("concurrent object not allowed in preelaborated unit",
1274                      N);
1275                   return;
1276                end if;
1277             end;
1278          end if;
1279
1280          --  Non-static discriminants not allowed in preelaborated unit.
1281          --  Objects of a controlled type with a user-defined Initialize
1282          --  are forbidden as well.
1283
1284          if Is_Record_Type (Etype (Id)) then
1285             declare
1286                ET  : constant Entity_Id := Etype (Id);
1287                EE  : constant Entity_Id := Etype (Etype (Id));
1288                PEE : Node_Id;
1289
1290             begin
1291                if Has_Discriminants (ET)
1292                  and then Present (EE)
1293                then
1294                   PEE := Parent (EE);
1295
1296                   if Nkind (PEE) = N_Full_Type_Declaration
1297                     and then not Static_Discriminant_Expr
1298                                    (Discriminant_Specifications (PEE))
1299                   then
1300                      Error_Msg_N
1301                        ("non-static discriminant in preelaborated unit",
1302                         PEE);
1303                   end if;
1304                end if;
1305
1306                --  For controlled type or type with controlled component, check
1307                --  preelaboration flag, as there may be a non-null Initialize
1308                --  primitive. For language versions earlier than Ada 2005,
1309                --  there is no notion of preelaborable initialization, and
1310                --  Validate_Controlled_Object is used to enforce rules for
1311                --  controlled objects.
1312
1313                if (Is_Controlled (ET) or else Has_Controlled_Component (ET))
1314                     and then Ada_Version >= Ada_2005
1315                     and then not Has_Preelaborable_Initialization (ET)
1316                then
1317                   Error_Msg_NE
1318                     ("controlled type& does not have"
1319                       & " preelaborable initialization", N, ET);
1320                end if;
1321             end;
1322
1323          end if;
1324       end if;
1325
1326       --  A pure library_item must not contain the declaration of any variable
1327       --  except within a subprogram, generic subprogram, task unit, or
1328       --  protected unit (RM 10.2.1(16)).
1329
1330       if In_Pure_Unit and then not In_Subprogram_Task_Protected_Unit then
1331          Error_Msg_N ("declaration of variable not allowed in pure unit", N);
1332
1333       --  The visible part of an RCI library unit must not contain the
1334       --  declaration of a variable (RM E.1.3(9))
1335
1336       elsif In_RCI_Declaration (N) then
1337          Error_Msg_N ("visible variable not allowed in 'R'C'I unit", N);
1338
1339       --  The visible part of a Shared Passive library unit must not contain
1340       --  the declaration of a variable (RM E.2.2(7))
1341
1342       elsif In_RT_Declaration and then not In_Private_Part (Id) then
1343          Error_Msg_N
1344            ("visible variable not allowed in remote types unit", N);
1345       end if;
1346
1347    end Validate_Object_Declaration;
1348
1349    -----------------------------
1350    -- Validate_RACW_Primitive --
1351    -----------------------------
1352
1353    procedure Validate_RACW_Primitive
1354      (Subp : Entity_Id;
1355       RACW : Entity_Id)
1356    is
1357       procedure Illegal_Remote_Subp (Msg : String; N : Node_Id);
1358       --  Diagnose illegality on N. If RACW is present, report the error on it
1359       --  rather than on N.
1360
1361       -------------------------
1362       -- Illegal_Remote_Subp --
1363       -------------------------
1364
1365       procedure Illegal_Remote_Subp (Msg : String; N : Node_Id) is
1366       begin
1367          if Present (RACW) then
1368             if not Error_Posted (RACW) then
1369                Error_Msg_N
1370                  ("illegal remote access to class-wide type&", RACW);
1371             end if;
1372
1373             Error_Msg_Sloc := Sloc (N);
1374             Error_Msg_NE ("\\" & Msg & " in primitive& #", RACW, Subp);
1375
1376          else
1377             Error_Msg_NE (Msg & " in remote subprogram&", N, Subp);
1378          end if;
1379       end Illegal_Remote_Subp;
1380
1381       Rtyp       : Entity_Id;
1382       Param      : Node_Id;
1383       Param_Spec : Node_Id;
1384       Param_Type : Entity_Id;
1385
1386    --  Start of processing for Validate_RACW_Primitive
1387
1388    begin
1389       --  Check return type
1390
1391       if Ekind (Subp) = E_Function then
1392          Rtyp := Etype (Subp);
1393
1394          if Has_Controlling_Result (Subp) then
1395             null;
1396
1397          elsif Ekind (Rtyp) = E_Anonymous_Access_Type then
1398             Illegal_Remote_Subp ("anonymous access result", Rtyp);
1399
1400          elsif Is_Limited_Type (Rtyp) then
1401             if No (TSS (Rtyp, TSS_Stream_Read))
1402                  or else
1403                No (TSS (Rtyp, TSS_Stream_Write))
1404             then
1405                Illegal_Remote_Subp
1406                  ("limited return type must have Read and Write attributes",
1407                      Parent (Subp));
1408                Explain_Limited_Type (Rtyp, Parent (Subp));
1409
1410             --  Check that the return type supports external streaming.
1411             --  Note that the language of the standard (E.2.2(14)) does not
1412             --  explicitly mention that case, but it really does not make
1413             --  sense to return a value containing a local access type.
1414
1415             elsif No_External_Streaming (Rtyp)
1416                     and then not Error_Posted (Rtyp)
1417             then
1418                Illegal_Remote_Subp ("return type containing non-remote access "
1419                  & "must have Read and Write attributes",
1420                  Parent (Subp));
1421             end if;
1422          end if;
1423       end if;
1424
1425       Param := First_Formal (Subp);
1426       while Present (Param) loop
1427
1428          --  Now find out if this parameter is a controlling parameter
1429
1430          Param_Spec := Parent (Param);
1431          Param_Type := Etype (Param);
1432
1433          if Is_Controlling_Formal (Param) then
1434
1435             --  It is a controlling parameter, so specific checks below do not
1436             --  apply.
1437
1438             null;
1439
1440          elsif Ekind_In (Param_Type, E_Anonymous_Access_Type,
1441                                      E_Anonymous_Access_Subprogram_Type)
1442          then
1443             --  From RM E.2.2(14), no anonymous access parameter other than
1444             --  controlling ones may be used (because an anonymous access
1445             --  type never supports external streaming).
1446
1447             Illegal_Remote_Subp
1448               ("non-controlling access parameter", Param_Spec);
1449
1450          elsif No_External_Streaming (Param_Type)
1451             and then not Error_Posted (Param_Type)
1452          then
1453             Illegal_Remote_Subp ("formal parameter in remote subprogram must "
1454               & "support external streaming", Param_Spec);
1455          end if;
1456
1457          --  Check next parameter in this subprogram
1458
1459          Next_Formal (Param);
1460       end loop;
1461    end Validate_RACW_Primitive;
1462
1463    ------------------------------
1464    -- Validate_RACW_Primitives --
1465    ------------------------------
1466
1467    procedure Validate_RACW_Primitives (T : Entity_Id) is
1468       Desig_Type             : Entity_Id;
1469       Primitive_Subprograms  : Elist_Id;
1470       Subprogram_Elmt        : Elmt_Id;
1471       Subprogram             : Entity_Id;
1472
1473    begin
1474       Desig_Type := Etype (Designated_Type (T));
1475
1476       --  No action needed for concurrent types
1477
1478       if Is_Concurrent_Type (Desig_Type) then
1479          return;
1480       end if;
1481
1482       Primitive_Subprograms := Primitive_Operations (Desig_Type);
1483
1484       Subprogram_Elmt := First_Elmt (Primitive_Subprograms);
1485       while Subprogram_Elmt /= No_Elmt loop
1486          Subprogram := Node (Subprogram_Elmt);
1487
1488          if Is_Predefined_Dispatching_Operation (Subprogram)
1489            or else Is_Hidden (Subprogram)
1490          then
1491             goto Next_Subprogram;
1492          end if;
1493
1494          Validate_RACW_Primitive (Subp => Subprogram, RACW => T);
1495
1496       <<Next_Subprogram>>
1497          Next_Elmt (Subprogram_Elmt);
1498       end loop;
1499    end Validate_RACW_Primitives;
1500
1501    -------------------------------
1502    -- Validate_RCI_Declarations --
1503    -------------------------------
1504
1505    procedure Validate_RCI_Declarations (P : Entity_Id) is
1506       E : Entity_Id;
1507
1508    begin
1509       E := First_Entity (P);
1510       while Present (E) loop
1511          if Comes_From_Source (E) then
1512             if Is_Limited_Type (E) then
1513                Error_Msg_N
1514                  ("limited type not allowed in rci unit", Parent (E));
1515                Explain_Limited_Type (E, Parent (E));
1516
1517             elsif Ekind_In (E, E_Generic_Function,
1518                                E_Generic_Package,
1519                                E_Generic_Procedure)
1520             then
1521                Error_Msg_N ("generic declaration not allowed in rci unit",
1522                  Parent (E));
1523
1524             elsif (Ekind (E) = E_Function or else Ekind (E) = E_Procedure)
1525               and then Has_Pragma_Inline (E)
1526             then
1527                Error_Msg_N
1528                  ("inlined subprogram not allowed in rci unit", Parent (E));
1529
1530             --  Inner packages that are renamings need not be checked. Generic
1531             --  RCI packages are subject to the checks, but entities that come
1532             --  from formal packages are not part of the visible declarations
1533             --  of the package and are not checked.
1534
1535             elsif Ekind (E) = E_Package then
1536                if Present (Renamed_Entity (E)) then
1537                   null;
1538
1539                elsif Ekind (P) /= E_Generic_Package
1540                  or else List_Containing (Unit_Declaration_Node (E)) /=
1541                            Generic_Formal_Declarations
1542                              (Unit_Declaration_Node (P))
1543                then
1544                   Validate_RCI_Declarations (E);
1545                end if;
1546             end if;
1547          end if;
1548
1549          Next_Entity (E);
1550       end loop;
1551    end Validate_RCI_Declarations;
1552
1553    -----------------------------------------
1554    -- Validate_RCI_Subprogram_Declaration --
1555    -----------------------------------------
1556
1557    procedure Validate_RCI_Subprogram_Declaration (N : Node_Id) is
1558       K               : constant Node_Kind := Nkind (N);
1559       Profile         : List_Id;
1560       Id              : Node_Id;
1561       Param_Spec      : Node_Id;
1562       Param_Type      : Entity_Id;
1563       Error_Node      : Node_Id := N;
1564
1565    begin
1566       --  This procedure enforces rules on subprogram and access to subprogram
1567       --  declarations in RCI units. These rules do not apply to expander
1568       --  generated routines, which are not remote subprograms. It is called:
1569
1570       --    1. from Analyze_Subprogram_Declaration.
1571       --    2. from Validate_Object_Declaration (access to subprogram).
1572
1573       if not (Comes_From_Source (N) and then In_RCI_Declaration (N)) then
1574          return;
1575       end if;
1576
1577       if K = N_Subprogram_Declaration then
1578          Id := Defining_Unit_Name (Specification (N));
1579          Profile := Parameter_Specifications (Specification (N));
1580
1581       else pragma Assert (K = N_Object_Declaration);
1582
1583          --  The above assertion is dubious, the visible declarations of an
1584          --  RCI unit never contain an object declaration, this should be an
1585          --  ACCESS-to-object declaration???
1586
1587          Id := Defining_Identifier (N);
1588
1589          if Nkind (Id) = N_Defining_Identifier
1590            and then Nkind (Parent (Etype (Id))) = N_Full_Type_Declaration
1591            and then Ekind (Etype (Id)) = E_Access_Subprogram_Type
1592          then
1593             Profile :=
1594               Parameter_Specifications (Type_Definition (Parent (Etype (Id))));
1595          else
1596             return;
1597          end if;
1598       end if;
1599
1600       --  Iterate through the parameter specification list, checking that
1601       --  no access parameter and no limited type parameter in the list.
1602       --  RM E.2.3(14).
1603
1604       if Present (Profile) then
1605          Param_Spec := First (Profile);
1606          while Present (Param_Spec) loop
1607             Param_Type := Etype (Defining_Identifier (Param_Spec));
1608
1609             if Ekind (Param_Type) = E_Anonymous_Access_Type then
1610                if K = N_Subprogram_Declaration then
1611                   Error_Node := Param_Spec;
1612                end if;
1613
1614                --  Report error only if declaration is in source program
1615
1616                if Comes_From_Source
1617                  (Defining_Entity (Specification (N)))
1618                then
1619                   Error_Msg_N
1620                     ("subprogram in 'R'C'I unit cannot have access parameter",
1621                       Error_Node);
1622                end if;
1623
1624             --  For a limited private type parameter, we check only the private
1625             --  declaration and ignore full type declaration, unless this is
1626             --  the only declaration for the type, e.g., as a limited record.
1627
1628             elsif No_External_Streaming (Param_Type) then
1629                if K = N_Subprogram_Declaration then
1630                   Error_Node := Param_Spec;
1631                end if;
1632
1633                Error_Msg_NE
1634                  ("formal of remote subprogram& "
1635                   & "must support external streaming",
1636                   Error_Node, Id);
1637                if Is_Limited_Type (Param_Type) then
1638                   Explain_Limited_Type (Param_Type, Error_Node);
1639                end if;
1640             end if;
1641
1642             Next (Param_Spec);
1643          end loop;
1644
1645          --  No check on return type???
1646       end if;
1647    end Validate_RCI_Subprogram_Declaration;
1648
1649    ----------------------------------------------------
1650    -- Validate_Remote_Access_Object_Type_Declaration --
1651    ----------------------------------------------------
1652
1653    procedure Validate_Remote_Access_Object_Type_Declaration (T : Entity_Id) is
1654
1655       function Is_Valid_Remote_Object_Type (E : Entity_Id) return Boolean;
1656       --  True if tagged type E is a valid candidate as the root type of the
1657       --  designated type for a RACW, i.e. a tagged limited private type, or a
1658       --  limited interface type, or a private extension of such a type.
1659
1660       ---------------------------------
1661       -- Is_Valid_Remote_Object_Type --
1662       ---------------------------------
1663
1664       function Is_Valid_Remote_Object_Type (E : Entity_Id) return Boolean is
1665          P : constant Node_Id := Parent (E);
1666
1667       begin
1668          pragma Assert (Is_Tagged_Type (E));
1669
1670          --  Simple case: a limited private type
1671
1672          if Nkind (P) = N_Private_Type_Declaration
1673            and then Is_Limited_Record (E)
1674          then
1675             return True;
1676
1677          --  A limited interface is not currently a legal ancestor for the
1678          --  designated type of an RACW type, because a type that implements
1679          --  such an interface need not be limited. However, the ARG seems to
1680          --  incline towards allowing an access to classwide limited interface
1681          --  type as a remote access type, as resolved in AI05-060. But note
1682          --  that the expansion circuitry for RACWs that designate classwide
1683          --  interfaces is not complete yet.
1684
1685          elsif Is_Limited_Record (E) and then Is_Limited_Interface (E) then
1686             return True;
1687
1688          --  A generic tagged limited type is a valid candidate. Limitedness
1689          --  will be checked again on the actual at instantiation point.
1690
1691          elsif Nkind (P) = N_Formal_Type_Declaration
1692            and then Ekind (E) = E_Record_Type_With_Private
1693            and then Is_Generic_Type (E)
1694            and then Is_Limited_Record (E)
1695          then
1696             return True;
1697
1698          --  A private extension declaration is a valid candidate if its parent
1699          --  type is.
1700
1701          elsif Nkind (P) = N_Private_Extension_Declaration then
1702             return Is_Valid_Remote_Object_Type (Etype (E));
1703
1704          else
1705             return False;
1706          end if;
1707       end Is_Valid_Remote_Object_Type;
1708
1709       --  Local variables
1710
1711       Direct_Designated_Type : Entity_Id;
1712       Desig_Type             : Entity_Id;
1713
1714    --  Start of processing for Validate_Remote_Access_Object_Type_Declaration
1715
1716    begin
1717       --  We are called from Analyze_Full_Type_Declaration, and the Nkind of
1718       --  the given node is N_Access_To_Object_Definition.
1719
1720       if not Comes_From_Source (T)
1721         or else (not In_RCI_Declaration (Parent (T))
1722                    and then not In_RT_Declaration)
1723       then
1724          return;
1725       end if;
1726
1727       --  An access definition in the private part of a Remote Types package
1728       --  may be legal if it has user-defined Read and Write attributes. This
1729       --  will be checked at the end of the package spec processing.
1730
1731       if In_RT_Declaration and then In_Private_Part (Scope (T)) then
1732          return;
1733       end if;
1734
1735       --  Check RCI or RT unit type declaration. It may not contain the
1736       --  declaration of an access-to-object type unless it is a general access
1737       --  type that designates a class-wide limited private type or subtype.
1738       --  There are also constraints on the primitive subprograms of the
1739       --  class-wide type (RM E.2.2(14), see Validate_RACW_Primitives).
1740
1741       if Ekind (T) /= E_General_Access_Type
1742         or else not Is_Class_Wide_Type (Designated_Type (T))
1743       then
1744          if In_RCI_Declaration (Parent (T)) then
1745             Error_Msg_N
1746               ("error in access type in Remote_Call_Interface unit", T);
1747          else
1748             Error_Msg_N
1749               ("error in access type in Remote_Types unit", T);
1750          end if;
1751
1752          Error_Msg_N ("\must be general access to class-wide type", T);
1753          return;
1754       end if;
1755
1756       Direct_Designated_Type := Designated_Type (T);
1757       Desig_Type := Etype (Direct_Designated_Type);
1758
1759       --  Why is the check below not in
1760       --  Validate_Remote_Access_To_Class_Wide_Type???
1761
1762       if not Is_Valid_Remote_Object_Type (Desig_Type) then
1763          Error_Msg_N
1764            ("error in designated type of remote access to class-wide type", T);
1765          Error_Msg_N
1766            ("\must be tagged limited private or private extension", T);
1767          return;
1768       end if;
1769    end Validate_Remote_Access_Object_Type_Declaration;
1770
1771    -----------------------------------------------
1772    -- Validate_Remote_Access_To_Class_Wide_Type --
1773    -----------------------------------------------
1774
1775    procedure Validate_Remote_Access_To_Class_Wide_Type (N : Node_Id) is
1776       K  : constant Node_Kind := Nkind (N);
1777       PK : constant Node_Kind := Nkind (Parent (N));
1778       E  : Entity_Id;
1779
1780    begin
1781       --  This subprogram enforces the checks in (RM E.2.2(8)) for certain uses
1782       --  of class-wide limited private types.
1783
1784       --    Storage_Pool and Storage_Size are not defined for such types
1785       --
1786       --    The expected type of allocator must not be such a type.
1787
1788       --    The actual parameter of generic instantiation must not be such a
1789       --    type if the formal parameter is of an access type.
1790
1791       --  On entry, there are five cases
1792
1793       --    1. called from sem_attr Analyze_Attribute where attribute name is
1794       --       either Storage_Pool or Storage_Size.
1795
1796       --    2. called from exp_ch4 Expand_N_Allocator
1797
1798       --    3. called from sem_ch12 Analyze_Associations
1799
1800       --    4. called from sem_ch4 Analyze_Explicit_Dereference
1801
1802       --    5. called from sem_res Resolve_Actuals
1803
1804       if K = N_Attribute_Reference then
1805          E := Etype (Prefix (N));
1806
1807          if Is_Remote_Access_To_Class_Wide_Type (E) then
1808             Error_Msg_N ("incorrect attribute of remote operand", N);
1809             return;
1810          end if;
1811
1812       elsif K = N_Allocator then
1813          E := Etype (N);
1814
1815          if Is_Remote_Access_To_Class_Wide_Type (E) then
1816             Error_Msg_N ("incorrect expected remote type of allocator", N);
1817             return;
1818          end if;
1819
1820       elsif K in N_Has_Entity then
1821          E := Entity (N);
1822
1823          if Is_Remote_Access_To_Class_Wide_Type (E) then
1824             Error_Msg_N ("incorrect remote type generic actual", N);
1825             return;
1826          end if;
1827
1828       --  This subprogram also enforces the checks in E.2.2(13). A value of
1829       --  such type must not be dereferenced unless as controlling operand of
1830       --  a dispatching call. Explicit dereferences not coming from source are
1831       --  exempted from this checking because the expander produces them in
1832       --  some cases (such as for tag checks on dispatching calls with multiple
1833       --  controlling operands). However we do check in the case of an implicit
1834       --  dereference that is expanded to an explicit dereference (hence the
1835       --  test of whether Original_Node (N) comes from source).
1836
1837       elsif K = N_Explicit_Dereference
1838         and then Comes_From_Source (Original_Node (N))
1839       then
1840          E := Etype (Prefix (N));
1841
1842          --  If the class-wide type is not a remote one, the restrictions
1843          --  do not apply.
1844
1845          if not Is_Remote_Access_To_Class_Wide_Type (E) then
1846             return;
1847          end if;
1848
1849          --  If we have a true dereference that comes from source and that
1850          --  is a controlling argument for a dispatching call, accept it.
1851
1852          if Is_Actual_Parameter (N)
1853            and then Is_Controlling_Actual (N)
1854          then
1855             return;
1856          end if;
1857
1858          --  If we are just within a procedure or function call and the
1859          --  dereference has not been analyzed, return because this procedure
1860          --  will be called again from sem_res Resolve_Actuals. The same can
1861          --  apply in the case of dereference that is the prefix of a selected
1862          --  component, which can be a call given in prefixed form.
1863
1864          if (Is_Actual_Parameter (N)
1865               or else PK = N_Selected_Component)
1866            and then not Analyzed (N)
1867          then
1868             return;
1869          end if;
1870
1871          --  We must allow expanded code to generate a reference to the tag of
1872          --  the designated object (may be either the actual tag, or the stub
1873          --  tag in the case of a remote object).
1874
1875          if PK = N_Selected_Component
1876            and then Is_Tag (Entity (Selector_Name (Parent (N))))
1877          then
1878             return;
1879          end if;
1880
1881          Error_Msg_N
1882            ("invalid dereference of a remote access-to-class-wide value", N);
1883       end if;
1884    end Validate_Remote_Access_To_Class_Wide_Type;
1885
1886    ------------------------------------------
1887    -- Validate_Remote_Type_Type_Conversion --
1888    ------------------------------------------
1889
1890    procedure Validate_Remote_Type_Type_Conversion (N : Node_Id) is
1891       S : constant Entity_Id := Etype (N);
1892       E : constant Entity_Id := Etype (Expression (N));
1893
1894    begin
1895       --  This test is required in the case where a conversion appears inside a
1896       --  normal package, it does not necessarily have to be inside an RCI,
1897       --  Remote_Types unit (RM E.2.2(9,12)).
1898
1899       if Is_Remote_Access_To_Subprogram_Type (E)
1900         and then not Is_Remote_Access_To_Subprogram_Type (S)
1901       then
1902          Error_Msg_N
1903            ("incorrect conversion of remote operand to local type", N);
1904          return;
1905
1906       elsif not Is_Remote_Access_To_Subprogram_Type (E)
1907         and then Is_Remote_Access_To_Subprogram_Type (S)
1908       then
1909          Error_Msg_N
1910            ("incorrect conversion of local operand to remote type", N);
1911          return;
1912
1913       elsif Is_Remote_Access_To_Class_Wide_Type (E)
1914         and then not Is_Remote_Access_To_Class_Wide_Type (S)
1915       then
1916          Error_Msg_N
1917            ("incorrect conversion of remote operand to local type", N);
1918          return;
1919       end if;
1920
1921       --  If a local access type is converted into a RACW type, then the
1922       --  current unit has a pointer that may now be exported to another
1923       --  partition.
1924
1925       if Is_Remote_Access_To_Class_Wide_Type (S)
1926         and then not Is_Remote_Access_To_Class_Wide_Type (E)
1927       then
1928          Set_Has_RACW (Current_Sem_Unit);
1929       end if;
1930    end Validate_Remote_Type_Type_Conversion;
1931
1932    -------------------------------
1933    -- Validate_RT_RAT_Component --
1934    -------------------------------
1935
1936    procedure Validate_RT_RAT_Component (N : Node_Id) is
1937       Spec           : constant Node_Id   := Specification (N);
1938       Name_U         : constant Entity_Id := Defining_Entity (Spec);
1939       Typ            : Entity_Id;
1940       U_Typ          : Entity_Id;
1941       First_Priv_Ent : constant Entity_Id := First_Private_Entity (Name_U);
1942
1943       function Stream_Attributes_Available (Typ : Entity_Id) return Boolean;
1944       --  True if any stream attribute is available for Typ
1945
1946       ---------------------------------
1947       -- Stream_Attributes_Available --
1948       ---------------------------------
1949
1950       function Stream_Attributes_Available (Typ : Entity_Id) return Boolean
1951       is
1952       begin
1953          return Stream_Attribute_Available (Typ, TSS_Stream_Read)
1954                   or else
1955                 Stream_Attribute_Available (Typ, TSS_Stream_Write)
1956                   or else
1957                 Stream_Attribute_Available (Typ, TSS_Stream_Input)
1958                   or else
1959                 Stream_Attribute_Available (Typ, TSS_Stream_Output);
1960       end Stream_Attributes_Available;
1961
1962    --  Start of processing for Validate_RT_RAT_Component
1963
1964    begin
1965       if not Is_Remote_Types (Name_U) then
1966          return;
1967       end if;
1968
1969       Typ := First_Entity (Name_U);
1970       while Present (Typ) and then Typ /= First_Priv_Ent loop
1971          U_Typ := Underlying_Type (Typ);
1972
1973          if No (U_Typ) then
1974             U_Typ := Typ;
1975          end if;
1976
1977          if Comes_From_Source (Typ) and then Is_Type (Typ) then
1978
1979             --  Check that the type can be meaningfully transmitted to another
1980             --  partition (E.2.2(8)).
1981
1982             if (Ada_Version < Ada_2005 and then Has_Non_Remote_Access (U_Typ))
1983                  or else
1984                (Stream_Attributes_Available (Typ)
1985                   and then No_External_Streaming (U_Typ))
1986             then
1987                if Is_Non_Remote_Access_Type (Typ) then
1988                   Error_Msg_N ("error in non-remote access type", U_Typ);
1989                else
1990                   Error_Msg_N
1991                     ("error in record type containing a component of a " &
1992                      "non-remote access type", U_Typ);
1993                end if;
1994
1995                if Ada_Version >= Ada_2005 then
1996                   Error_Msg_N
1997                     ("\must have visible Read and Write attribute " &
1998                      "definition clauses (RM E.2.2(8))", U_Typ);
1999                else
2000                   Error_Msg_N
2001                     ("\must have Read and Write attribute " &
2002                      "definition clauses (RM E.2.2(8))", U_Typ);
2003                end if;
2004             end if;
2005          end if;
2006
2007          Next_Entity (Typ);
2008       end loop;
2009    end Validate_RT_RAT_Component;
2010
2011    -----------------------------------------
2012    -- Validate_SP_Access_Object_Type_Decl --
2013    -----------------------------------------
2014
2015    procedure Validate_SP_Access_Object_Type_Decl (T : Entity_Id) is
2016       Direct_Designated_Type : Entity_Id;
2017
2018       function Has_Entry_Declarations (E : Entity_Id) return Boolean;
2019       --  Return true if the protected type designated by T has
2020       --  entry declarations.
2021
2022       ----------------------------
2023       -- Has_Entry_Declarations --
2024       ----------------------------
2025
2026       function Has_Entry_Declarations (E : Entity_Id) return Boolean is
2027          Ety : Entity_Id;
2028
2029       begin
2030          if Nkind (Parent (E)) = N_Protected_Type_Declaration then
2031             Ety := First_Entity (E);
2032             while Present (Ety) loop
2033                if Ekind (Ety) = E_Entry then
2034                   return True;
2035                end if;
2036
2037                Next_Entity (Ety);
2038             end loop;
2039          end if;
2040
2041          return False;
2042       end Has_Entry_Declarations;
2043
2044    --  Start of processing for Validate_SP_Access_Object_Type_Decl
2045
2046    begin
2047       --  We are called from Sem_Ch3.Analyze_Full_Type_Declaration, and the
2048       --  Nkind of the given entity is N_Access_To_Object_Definition.
2049
2050       if not Comes_From_Source (T)
2051         or else not In_Shared_Passive_Unit
2052         or else In_Subprogram_Task_Protected_Unit
2053       then
2054          return;
2055       end if;
2056
2057       --  Check Shared Passive unit. It should not contain the declaration
2058       --  of an access-to-object type whose designated type is a class-wide
2059       --  type, task type or protected type with entry (RM E.2.1(7)).
2060
2061       Direct_Designated_Type := Designated_Type (T);
2062
2063       if Ekind (Direct_Designated_Type) = E_Class_Wide_Type then
2064          Error_Msg_N
2065            ("invalid access-to-class-wide type in shared passive unit", T);
2066          return;
2067
2068       elsif Ekind (Direct_Designated_Type) in Task_Kind then
2069          Error_Msg_N
2070            ("invalid access-to-task type in shared passive unit", T);
2071          return;
2072
2073       elsif Ekind (Direct_Designated_Type) in Protected_Kind
2074         and then Has_Entry_Declarations (Direct_Designated_Type)
2075       then
2076          Error_Msg_N
2077            ("invalid access-to-protected type in shared passive unit", T);
2078          return;
2079       end if;
2080    end Validate_SP_Access_Object_Type_Decl;
2081
2082    ---------------------------------
2083    -- Validate_Static_Object_Name --
2084    ---------------------------------
2085
2086    procedure Validate_Static_Object_Name (N : Node_Id) is
2087       E : Entity_Id;
2088
2089       function Is_Primary (N : Node_Id) return Boolean;
2090       --  Determine whether node is syntactically a primary in an expression
2091       --  This function should probably be somewhere else ???
2092       --  Also it does not do what it says, e.g if N is a binary operator
2093       --  whose parent is a binary operator, Is_Primary returns True ???
2094
2095       ----------------
2096       -- Is_Primary --
2097       ----------------
2098
2099       function Is_Primary (N : Node_Id) return Boolean is
2100          K : constant Node_Kind := Nkind (Parent (N));
2101
2102       begin
2103          case K is
2104             when N_Op | N_Membership_Test =>
2105                return True;
2106
2107             when N_Aggregate
2108                | N_Component_Association
2109                | N_Index_Or_Discriminant_Constraint =>
2110                return True;
2111
2112             when N_Attribute_Reference =>
2113                return Attribute_Name (Parent (N)) /= Name_Address
2114                  and then Attribute_Name (Parent (N)) /= Name_Access
2115                  and then Attribute_Name (Parent (N)) /= Name_Unchecked_Access
2116                  and then
2117                    Attribute_Name (Parent (N)) /= Name_Unrestricted_Access;
2118
2119             when N_Indexed_Component =>
2120                return (N /= Prefix (Parent (N))
2121                  or else Is_Primary (Parent (N)));
2122
2123             when N_Qualified_Expression | N_Type_Conversion =>
2124                return Is_Primary (Parent (N));
2125
2126             when N_Assignment_Statement | N_Object_Declaration =>
2127                return (N = Expression (Parent (N)));
2128
2129             when N_Selected_Component =>
2130                return Is_Primary (Parent (N));
2131
2132             when others =>
2133                return False;
2134          end case;
2135       end Is_Primary;
2136
2137    --  Start of processing for Validate_Static_Object_Name
2138
2139    begin
2140       if not In_Preelaborated_Unit
2141         or else not Comes_From_Source (N)
2142         or else In_Subprogram_Or_Concurrent_Unit
2143         or else Ekind (Current_Scope) = E_Block
2144       then
2145          return;
2146
2147       --  Filter out cases where primary is default in a component declaration,
2148       --  discriminant specification, or actual in a record type initialization
2149       --  call.
2150
2151       --  Initialization call of internal types
2152
2153       elsif Nkind (Parent (N)) = N_Procedure_Call_Statement then
2154
2155          if Present (Parent (Parent (N)))
2156            and then Nkind (Parent (Parent (N))) = N_Freeze_Entity
2157          then
2158             return;
2159          end if;
2160
2161          if Nkind (Name (Parent (N))) = N_Identifier
2162            and then not Comes_From_Source (Entity (Name (Parent (N))))
2163          then
2164             return;
2165          end if;
2166       end if;
2167
2168       --  Error if the name is a primary in an expression. The parent must not
2169       --  be an operator, or a selected component or an indexed component that
2170       --  is itself a primary. Entities that are actuals do not need to be
2171       --  checked, because the call itself will be diagnosed.
2172
2173       if Is_Primary (N)
2174         and then (not Inside_A_Generic
2175                    or else Present (Enclosing_Generic_Body (N)))
2176       then
2177          if Ekind (Entity (N)) = E_Variable
2178            or else Ekind (Entity (N)) in Formal_Object_Kind
2179          then
2180             Flag_Non_Static_Expr
2181               ("non-static object name in preelaborated unit", N);
2182
2183          --  Give an error for a reference to a nonstatic constant, unless the
2184          --  constant is in another GNAT library unit that is preelaborable.
2185
2186          elsif Ekind (Entity (N)) = E_Constant
2187            and then not Is_Static_Expression (N)
2188          then
2189             E := Entity (N);
2190
2191             if Is_Internal_File_Name (Unit_File_Name (Get_Source_Unit (N)))
2192               and then
2193                 Enclosing_Lib_Unit_Node (N) /= Enclosing_Lib_Unit_Node (E)
2194               and then (Is_Preelaborated (Scope (E))
2195                           or else Is_Pure (Scope (E))
2196                           or else (Present (Renamed_Object (E))
2197                                      and then
2198                                        Is_Entity_Name (Renamed_Object (E))
2199                                      and then
2200                                        (Is_Preelaborated
2201                                          (Scope (Renamed_Object (E)))
2202                                             or else
2203                                               Is_Pure (Scope
2204                                                 (Renamed_Object (E))))))
2205             then
2206                null;
2207
2208             --  This is the error case
2209
2210             else
2211                --  In GNAT mode, this is just a warning, to allow it to be
2212                --  judiciously turned off. Otherwise it is a real error.
2213
2214                if GNAT_Mode then
2215                   Error_Msg_N
2216                     ("?non-static constant in preelaborated unit", N);
2217                else
2218                   Flag_Non_Static_Expr
2219                     ("non-static constant in preelaborated unit", N);
2220                end if;
2221             end if;
2222          end if;
2223       end if;
2224    end Validate_Static_Object_Name;
2225
2226 end Sem_Cat;