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