OSDN Git Service

2011-12-02 Thomas Quinot <quinot@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 Ada 95 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          --  AI05-0101 (Binding Interpretation): The result type of a remote
1395          --  function must either support external streaming or be a
1396          --  controlling access result type.
1397
1398          if Has_Controlling_Result (Subp) then
1399             null;
1400
1401          elsif Ekind (Rtyp) = E_Anonymous_Access_Type then
1402             Illegal_Remote_Subp ("anonymous access result", Rtyp);
1403
1404          elsif Is_Limited_Type (Rtyp) then
1405             if No (TSS (Rtyp, TSS_Stream_Read))
1406                  or else
1407                No (TSS (Rtyp, TSS_Stream_Write))
1408             then
1409                Illegal_Remote_Subp
1410                  ("limited return type must have Read and Write attributes",
1411                      Parent (Subp));
1412                Explain_Limited_Type (Rtyp, Parent (Subp));
1413             end if;
1414
1415          --  Check that the return type supports external streaming
1416
1417          elsif No_External_Streaming (Rtyp)
1418                  and then not Error_Posted (Rtyp)
1419          then
1420             Illegal_Remote_Subp ("return type containing non-remote access "
1421               & "must have Read and Write attributes",
1422               Parent (Subp));
1423          end if;
1424       end if;
1425
1426       Param := First_Formal (Subp);
1427       while Present (Param) loop
1428
1429          --  Now find out if this parameter is a controlling parameter
1430
1431          Param_Spec := Parent (Param);
1432          Param_Type := Etype (Param);
1433
1434          if Is_Controlling_Formal (Param) then
1435
1436             --  It is a controlling parameter, so specific checks below do not
1437             --  apply.
1438
1439             null;
1440
1441          elsif Ekind_In (Param_Type, E_Anonymous_Access_Type,
1442                                      E_Anonymous_Access_Subprogram_Type)
1443          then
1444             --  From RM E.2.2(14), no anonymous access parameter other than
1445             --  controlling ones may be used (because an anonymous access
1446             --  type never supports external streaming).
1447
1448             Illegal_Remote_Subp
1449               ("non-controlling access parameter", Param_Spec);
1450
1451          elsif No_External_Streaming (Param_Type)
1452             and then not Error_Posted (Param_Type)
1453          then
1454             Illegal_Remote_Subp ("formal parameter in remote subprogram must "
1455               & "support external streaming", Param_Spec);
1456          end if;
1457
1458          --  Check next parameter in this subprogram
1459
1460          Next_Formal (Param);
1461       end loop;
1462    end Validate_RACW_Primitive;
1463
1464    ------------------------------
1465    -- Validate_RACW_Primitives --
1466    ------------------------------
1467
1468    procedure Validate_RACW_Primitives (T : Entity_Id) is
1469       Desig_Type             : Entity_Id;
1470       Primitive_Subprograms  : Elist_Id;
1471       Subprogram_Elmt        : Elmt_Id;
1472       Subprogram             : Entity_Id;
1473
1474    begin
1475       Desig_Type := Etype (Designated_Type (T));
1476
1477       --  No action needed for concurrent types
1478
1479       if Is_Concurrent_Type (Desig_Type) then
1480          return;
1481       end if;
1482
1483       Primitive_Subprograms := Primitive_Operations (Desig_Type);
1484
1485       Subprogram_Elmt := First_Elmt (Primitive_Subprograms);
1486       while Subprogram_Elmt /= No_Elmt loop
1487          Subprogram := Node (Subprogram_Elmt);
1488
1489          if Is_Predefined_Dispatching_Operation (Subprogram)
1490            or else Is_Hidden (Subprogram)
1491          then
1492             goto Next_Subprogram;
1493          end if;
1494
1495          Validate_RACW_Primitive (Subp => Subprogram, RACW => T);
1496
1497       <<Next_Subprogram>>
1498          Next_Elmt (Subprogram_Elmt);
1499       end loop;
1500    end Validate_RACW_Primitives;
1501
1502    -------------------------------
1503    -- Validate_RCI_Declarations --
1504    -------------------------------
1505
1506    procedure Validate_RCI_Declarations (P : Entity_Id) is
1507       E : Entity_Id;
1508
1509    begin
1510       E := First_Entity (P);
1511       while Present (E) loop
1512          if Comes_From_Source (E) then
1513             if Is_Limited_Type (E) then
1514                Error_Msg_N
1515                  ("limited type not allowed in rci unit", Parent (E));
1516                Explain_Limited_Type (E, Parent (E));
1517
1518             elsif Ekind_In (E, E_Generic_Function,
1519                                E_Generic_Package,
1520                                E_Generic_Procedure)
1521             then
1522                Error_Msg_N ("generic declaration not allowed in rci unit",
1523                  Parent (E));
1524
1525             elsif (Ekind (E) = E_Function or else Ekind (E) = E_Procedure)
1526               and then Has_Pragma_Inline (E)
1527             then
1528                Error_Msg_N
1529                  ("inlined subprogram not allowed in rci unit", Parent (E));
1530
1531             --  Inner packages that are renamings need not be checked. Generic
1532             --  RCI packages are subject to the checks, but entities that come
1533             --  from formal packages are not part of the visible declarations
1534             --  of the package and are not checked.
1535
1536             elsif Ekind (E) = E_Package then
1537                if Present (Renamed_Entity (E)) then
1538                   null;
1539
1540                elsif Ekind (P) /= E_Generic_Package
1541                  or else List_Containing (Unit_Declaration_Node (E)) /=
1542                            Generic_Formal_Declarations
1543                              (Unit_Declaration_Node (P))
1544                then
1545                   Validate_RCI_Declarations (E);
1546                end if;
1547             end if;
1548          end if;
1549
1550          Next_Entity (E);
1551       end loop;
1552    end Validate_RCI_Declarations;
1553
1554    -----------------------------------------
1555    -- Validate_RCI_Subprogram_Declaration --
1556    -----------------------------------------
1557
1558    procedure Validate_RCI_Subprogram_Declaration (N : Node_Id) is
1559       K               : constant Node_Kind := Nkind (N);
1560       Profile         : List_Id;
1561       Id              : Node_Id;
1562       Param_Spec      : Node_Id;
1563       Param_Type      : Entity_Id;
1564       Error_Node      : Node_Id := N;
1565
1566    begin
1567       --  This procedure enforces rules on subprogram and access to subprogram
1568       --  declarations in RCI units. These rules do not apply to expander
1569       --  generated routines, which are not remote subprograms. It is called:
1570
1571       --    1. from Analyze_Subprogram_Declaration.
1572       --    2. from Validate_Object_Declaration (access to subprogram).
1573
1574       if not (Comes_From_Source (N) and then In_RCI_Declaration (N)) then
1575          return;
1576       end if;
1577
1578       if K = N_Subprogram_Declaration then
1579          Id := Defining_Unit_Name (Specification (N));
1580          Profile := Parameter_Specifications (Specification (N));
1581
1582       else pragma Assert (K = N_Object_Declaration);
1583
1584          --  The above assertion is dubious, the visible declarations of an
1585          --  RCI unit never contain an object declaration, this should be an
1586          --  ACCESS-to-object declaration???
1587
1588          Id := Defining_Identifier (N);
1589
1590          if Nkind (Id) = N_Defining_Identifier
1591            and then Nkind (Parent (Etype (Id))) = N_Full_Type_Declaration
1592            and then Ekind (Etype (Id)) = E_Access_Subprogram_Type
1593          then
1594             Profile :=
1595               Parameter_Specifications (Type_Definition (Parent (Etype (Id))));
1596          else
1597             return;
1598          end if;
1599       end if;
1600
1601       --  Iterate through the parameter specification list, checking that
1602       --  no access parameter and no limited type parameter in the list.
1603       --  RM E.2.3(14).
1604
1605       if Present (Profile) then
1606          Param_Spec := First (Profile);
1607          while Present (Param_Spec) loop
1608             Param_Type := Etype (Defining_Identifier (Param_Spec));
1609
1610             if Ekind (Param_Type) = E_Anonymous_Access_Type then
1611                if K = N_Subprogram_Declaration then
1612                   Error_Node := Param_Spec;
1613                end if;
1614
1615                --  Report error only if declaration is in source program
1616
1617                if Comes_From_Source
1618                  (Defining_Entity (Specification (N)))
1619                then
1620                   Error_Msg_N
1621                     ("subprogram in 'R'C'I unit cannot have access parameter",
1622                       Error_Node);
1623                end if;
1624
1625             --  For a limited private type parameter, we check only the private
1626             --  declaration and ignore full type declaration, unless this is
1627             --  the only declaration for the type, e.g., as a limited record.
1628
1629             elsif No_External_Streaming (Param_Type) then
1630                if K = N_Subprogram_Declaration then
1631                   Error_Node := Param_Spec;
1632                end if;
1633
1634                Error_Msg_NE
1635                  ("formal of remote subprogram& "
1636                   & "must support external streaming",
1637                   Error_Node, Id);
1638                if Is_Limited_Type (Param_Type) then
1639                   Explain_Limited_Type (Param_Type, Error_Node);
1640                end if;
1641             end if;
1642
1643             Next (Param_Spec);
1644          end loop;
1645
1646          --  No check on return type???
1647       end if;
1648    end Validate_RCI_Subprogram_Declaration;
1649
1650    ----------------------------------------------------
1651    -- Validate_Remote_Access_Object_Type_Declaration --
1652    ----------------------------------------------------
1653
1654    procedure Validate_Remote_Access_Object_Type_Declaration (T : Entity_Id) is
1655
1656       function Is_Valid_Remote_Object_Type (E : Entity_Id) return Boolean;
1657       --  True if tagged type E is a valid candidate as the root type of the
1658       --  designated type for a RACW, i.e. a tagged limited private type, or a
1659       --  limited interface type, or a private extension of such a type.
1660
1661       ---------------------------------
1662       -- Is_Valid_Remote_Object_Type --
1663       ---------------------------------
1664
1665       function Is_Valid_Remote_Object_Type (E : Entity_Id) return Boolean is
1666          P : constant Node_Id := Parent (E);
1667
1668       begin
1669          pragma Assert (Is_Tagged_Type (E));
1670
1671          --  Simple case: a limited private type
1672
1673          if Nkind (P) = N_Private_Type_Declaration
1674            and then Is_Limited_Record (E)
1675          then
1676             return True;
1677
1678          --  AI05-0060 (Binding Interpretation): A limited interface is a legal
1679          --  ancestor for the designated type of an RACW type.
1680
1681          elsif Is_Limited_Record (E) and then Is_Limited_Interface (E) then
1682             return True;
1683
1684          --  A generic tagged limited type is a valid candidate. Limitedness
1685          --  will be checked again on the actual at instantiation point.
1686
1687          elsif Nkind (P) = N_Formal_Type_Declaration
1688            and then Ekind (E) = E_Record_Type_With_Private
1689            and then Is_Generic_Type (E)
1690            and then Is_Limited_Record (E)
1691          then
1692             return True;
1693
1694          --  A private extension declaration is a valid candidate if its parent
1695          --  type is.
1696
1697          elsif Nkind (P) = N_Private_Extension_Declaration then
1698             return Is_Valid_Remote_Object_Type (Etype (E));
1699
1700          else
1701             return False;
1702          end if;
1703       end Is_Valid_Remote_Object_Type;
1704
1705       --  Local variables
1706
1707       Direct_Designated_Type : Entity_Id;
1708       Desig_Type             : Entity_Id;
1709
1710    --  Start of processing for Validate_Remote_Access_Object_Type_Declaration
1711
1712    begin
1713       --  We are called from Analyze_Full_Type_Declaration, and the Nkind of
1714       --  the given node is N_Access_To_Object_Definition.
1715
1716       if not Comes_From_Source (T)
1717         or else (not In_RCI_Declaration (Parent (T))
1718                    and then not In_RT_Declaration)
1719       then
1720          return;
1721       end if;
1722
1723       --  An access definition in the private part of a Remote Types package
1724       --  may be legal if it has user-defined Read and Write attributes. This
1725       --  will be checked at the end of the package spec processing.
1726
1727       if In_RT_Declaration and then In_Private_Part (Scope (T)) then
1728          return;
1729       end if;
1730
1731       --  Check RCI or RT unit type declaration. It may not contain the
1732       --  declaration of an access-to-object type unless it is a general access
1733       --  type that designates a class-wide limited private type or subtype.
1734       --  There are also constraints on the primitive subprograms of the
1735       --  class-wide type (RM E.2.2(14), see Validate_RACW_Primitives).
1736
1737       if Ekind (T) /= E_General_Access_Type
1738         or else not Is_Class_Wide_Type (Designated_Type (T))
1739       then
1740          if In_RCI_Declaration (Parent (T)) then
1741             Error_Msg_N
1742               ("error in access type in Remote_Call_Interface unit", T);
1743          else
1744             Error_Msg_N
1745               ("error in access type in Remote_Types unit", T);
1746          end if;
1747
1748          Error_Msg_N ("\must be general access to class-wide type", T);
1749          return;
1750       end if;
1751
1752       Direct_Designated_Type := Designated_Type (T);
1753       Desig_Type := Etype (Direct_Designated_Type);
1754
1755       --  Why is the check below not in
1756       --  Validate_Remote_Access_To_Class_Wide_Type???
1757
1758       if not Is_Valid_Remote_Object_Type (Desig_Type) then
1759          Error_Msg_N
1760            ("error in designated type of remote access to class-wide type", T);
1761          Error_Msg_N
1762            ("\must be tagged limited private or private extension", T);
1763          return;
1764       end if;
1765    end Validate_Remote_Access_Object_Type_Declaration;
1766
1767    -----------------------------------------------
1768    -- Validate_Remote_Access_To_Class_Wide_Type --
1769    -----------------------------------------------
1770
1771    procedure Validate_Remote_Access_To_Class_Wide_Type (N : Node_Id) is
1772       K  : constant Node_Kind := Nkind (N);
1773       PK : constant Node_Kind := Nkind (Parent (N));
1774       E  : Entity_Id;
1775
1776    begin
1777       --  This subprogram enforces the checks in (RM E.2.2(8)) for certain uses
1778       --  of class-wide limited private types.
1779
1780       --    Storage_Pool and Storage_Size are not defined for such types
1781       --
1782       --    The expected type of allocator must not be such a type.
1783
1784       --    The actual parameter of generic instantiation must not be such a
1785       --    type if the formal parameter is of an access type.
1786
1787       --  On entry, there are five cases
1788
1789       --    1. called from sem_attr Analyze_Attribute where attribute name is
1790       --       either Storage_Pool or Storage_Size.
1791
1792       --    2. called from exp_ch4 Expand_N_Allocator
1793
1794       --    3. called from sem_ch12 Analyze_Associations
1795
1796       --    4. called from sem_ch4 Analyze_Explicit_Dereference
1797
1798       --    5. called from sem_res Resolve_Actuals
1799
1800       if K = N_Attribute_Reference then
1801          E := Etype (Prefix (N));
1802
1803          if Is_Remote_Access_To_Class_Wide_Type (E) then
1804             Error_Msg_N ("incorrect attribute of remote operand", N);
1805             return;
1806          end if;
1807
1808       elsif K = N_Allocator then
1809          E := Etype (N);
1810
1811          if Is_Remote_Access_To_Class_Wide_Type (E) then
1812             Error_Msg_N ("incorrect expected remote type of allocator", N);
1813             return;
1814          end if;
1815
1816       elsif K in N_Has_Entity then
1817          E := Entity (N);
1818
1819          if Is_Remote_Access_To_Class_Wide_Type (E) then
1820             Error_Msg_N ("incorrect remote type generic actual", N);
1821             return;
1822          end if;
1823
1824       --  This subprogram also enforces the checks in E.2.2(13). A value of
1825       --  such type must not be dereferenced unless as controlling operand of
1826       --  a dispatching call. Explicit dereferences not coming from source are
1827       --  exempted from this checking because the expander produces them in
1828       --  some cases (such as for tag checks on dispatching calls with multiple
1829       --  controlling operands). However we do check in the case of an implicit
1830       --  dereference that is expanded to an explicit dereference (hence the
1831       --  test of whether Original_Node (N) comes from source).
1832
1833       elsif K = N_Explicit_Dereference
1834         and then Comes_From_Source (Original_Node (N))
1835       then
1836          E := Etype (Prefix (N));
1837
1838          --  If the class-wide type is not a remote one, the restrictions
1839          --  do not apply.
1840
1841          if not Is_Remote_Access_To_Class_Wide_Type (E) then
1842             return;
1843          end if;
1844
1845          --  If we have a true dereference that comes from source and that
1846          --  is a controlling argument for a dispatching call, accept it.
1847
1848          if Is_Actual_Parameter (N)
1849            and then Is_Controlling_Actual (N)
1850          then
1851             return;
1852          end if;
1853
1854          --  If we are just within a procedure or function call and the
1855          --  dereference has not been analyzed, return because this procedure
1856          --  will be called again from sem_res Resolve_Actuals. The same can
1857          --  apply in the case of dereference that is the prefix of a selected
1858          --  component, which can be a call given in prefixed form.
1859
1860          if (Is_Actual_Parameter (N)
1861               or else PK = N_Selected_Component)
1862            and then not Analyzed (N)
1863          then
1864             return;
1865          end if;
1866
1867          --  We must allow expanded code to generate a reference to the tag of
1868          --  the designated object (may be either the actual tag, or the stub
1869          --  tag in the case of a remote object).
1870
1871          if PK = N_Selected_Component
1872            and then Is_Tag (Entity (Selector_Name (Parent (N))))
1873          then
1874             return;
1875          end if;
1876
1877          Error_Msg_N
1878            ("invalid dereference of a remote access-to-class-wide value", N);
1879       end if;
1880    end Validate_Remote_Access_To_Class_Wide_Type;
1881
1882    ------------------------------------------
1883    -- Validate_Remote_Type_Type_Conversion --
1884    ------------------------------------------
1885
1886    procedure Validate_Remote_Type_Type_Conversion (N : Node_Id) is
1887       S : constant Entity_Id := Etype (N);
1888       E : constant Entity_Id := Etype (Expression (N));
1889
1890    begin
1891       --  This test is required in the case where a conversion appears inside a
1892       --  normal package, it does not necessarily have to be inside an RCI,
1893       --  Remote_Types unit (RM E.2.2(9,12)).
1894
1895       if Is_Remote_Access_To_Subprogram_Type (E)
1896         and then not Is_Remote_Access_To_Subprogram_Type (S)
1897       then
1898          Error_Msg_N
1899            ("incorrect conversion of remote operand to local type", N);
1900          return;
1901
1902       elsif not Is_Remote_Access_To_Subprogram_Type (E)
1903         and then Is_Remote_Access_To_Subprogram_Type (S)
1904       then
1905          Error_Msg_N
1906            ("incorrect conversion of local operand to remote type", N);
1907          return;
1908
1909       elsif Is_Remote_Access_To_Class_Wide_Type (E)
1910         and then not Is_Remote_Access_To_Class_Wide_Type (S)
1911       then
1912          Error_Msg_N
1913            ("incorrect conversion of remote operand to local type", N);
1914          return;
1915       end if;
1916
1917       --  If a local access type is converted into a RACW type, then the
1918       --  current unit has a pointer that may now be exported to another
1919       --  partition.
1920
1921       if Is_Remote_Access_To_Class_Wide_Type (S)
1922         and then not Is_Remote_Access_To_Class_Wide_Type (E)
1923       then
1924          Set_Has_RACW (Current_Sem_Unit);
1925       end if;
1926    end Validate_Remote_Type_Type_Conversion;
1927
1928    -------------------------------
1929    -- Validate_RT_RAT_Component --
1930    -------------------------------
1931
1932    procedure Validate_RT_RAT_Component (N : Node_Id) is
1933       Spec           : constant Node_Id   := Specification (N);
1934       Name_U         : constant Entity_Id := Defining_Entity (Spec);
1935       Typ            : Entity_Id;
1936       U_Typ          : Entity_Id;
1937       First_Priv_Ent : constant Entity_Id := First_Private_Entity (Name_U);
1938
1939       function Stream_Attributes_Available (Typ : Entity_Id) return Boolean;
1940       --  True if any stream attribute is available for Typ
1941
1942       ---------------------------------
1943       -- Stream_Attributes_Available --
1944       ---------------------------------
1945
1946       function Stream_Attributes_Available (Typ : Entity_Id) return Boolean
1947       is
1948       begin
1949          return Stream_Attribute_Available (Typ, TSS_Stream_Read)
1950                   or else
1951                 Stream_Attribute_Available (Typ, TSS_Stream_Write)
1952                   or else
1953                 Stream_Attribute_Available (Typ, TSS_Stream_Input)
1954                   or else
1955                 Stream_Attribute_Available (Typ, TSS_Stream_Output);
1956       end Stream_Attributes_Available;
1957
1958    --  Start of processing for Validate_RT_RAT_Component
1959
1960    begin
1961       if not Is_Remote_Types (Name_U) then
1962          return;
1963       end if;
1964
1965       Typ := First_Entity (Name_U);
1966       while Present (Typ) and then Typ /= First_Priv_Ent loop
1967          U_Typ := Underlying_Type (Typ);
1968
1969          if No (U_Typ) then
1970             U_Typ := Typ;
1971          end if;
1972
1973          if Comes_From_Source (Typ) and then Is_Type (Typ) then
1974
1975             --  Check that the type can be meaningfully transmitted to another
1976             --  partition (E.2.2(8)).
1977
1978             if (Ada_Version < Ada_2005 and then Has_Non_Remote_Access (U_Typ))
1979                  or else
1980                (Stream_Attributes_Available (Typ)
1981                   and then No_External_Streaming (U_Typ))
1982             then
1983                if Is_Non_Remote_Access_Type (Typ) then
1984                   Error_Msg_N ("error in non-remote access type", U_Typ);
1985                else
1986                   Error_Msg_N
1987                     ("error in record type containing a component of a " &
1988                      "non-remote access type", U_Typ);
1989                end if;
1990
1991                if Ada_Version >= Ada_2005 then
1992                   Error_Msg_N
1993                     ("\must have visible Read and Write attribute " &
1994                      "definition clauses (RM E.2.2(8))", U_Typ);
1995                else
1996                   Error_Msg_N
1997                     ("\must have Read and Write attribute " &
1998                      "definition clauses (RM E.2.2(8))", U_Typ);
1999                end if;
2000             end if;
2001          end if;
2002
2003          Next_Entity (Typ);
2004       end loop;
2005    end Validate_RT_RAT_Component;
2006
2007    -----------------------------------------
2008    -- Validate_SP_Access_Object_Type_Decl --
2009    -----------------------------------------
2010
2011    procedure Validate_SP_Access_Object_Type_Decl (T : Entity_Id) is
2012       Direct_Designated_Type : Entity_Id;
2013
2014       function Has_Entry_Declarations (E : Entity_Id) return Boolean;
2015       --  Return true if the protected type designated by T has
2016       --  entry declarations.
2017
2018       ----------------------------
2019       -- Has_Entry_Declarations --
2020       ----------------------------
2021
2022       function Has_Entry_Declarations (E : Entity_Id) return Boolean is
2023          Ety : Entity_Id;
2024
2025       begin
2026          if Nkind (Parent (E)) = N_Protected_Type_Declaration then
2027             Ety := First_Entity (E);
2028             while Present (Ety) loop
2029                if Ekind (Ety) = E_Entry then
2030                   return True;
2031                end if;
2032
2033                Next_Entity (Ety);
2034             end loop;
2035          end if;
2036
2037          return False;
2038       end Has_Entry_Declarations;
2039
2040    --  Start of processing for Validate_SP_Access_Object_Type_Decl
2041
2042    begin
2043       --  We are called from Sem_Ch3.Analyze_Full_Type_Declaration, and the
2044       --  Nkind of the given entity is N_Access_To_Object_Definition.
2045
2046       if not Comes_From_Source (T)
2047         or else not In_Shared_Passive_Unit
2048         or else In_Subprogram_Task_Protected_Unit
2049       then
2050          return;
2051       end if;
2052
2053       --  Check Shared Passive unit. It should not contain the declaration
2054       --  of an access-to-object type whose designated type is a class-wide
2055       --  type, task type or protected type with entry (RM E.2.1(7)).
2056
2057       Direct_Designated_Type := Designated_Type (T);
2058
2059       if Ekind (Direct_Designated_Type) = E_Class_Wide_Type then
2060          Error_Msg_N
2061            ("invalid access-to-class-wide type in shared passive unit", T);
2062          return;
2063
2064       elsif Ekind (Direct_Designated_Type) in Task_Kind then
2065          Error_Msg_N
2066            ("invalid access-to-task type in shared passive unit", T);
2067          return;
2068
2069       elsif Ekind (Direct_Designated_Type) in Protected_Kind
2070         and then Has_Entry_Declarations (Direct_Designated_Type)
2071       then
2072          Error_Msg_N
2073            ("invalid access-to-protected type in shared passive unit", T);
2074          return;
2075       end if;
2076    end Validate_SP_Access_Object_Type_Decl;
2077
2078    ---------------------------------
2079    -- Validate_Static_Object_Name --
2080    ---------------------------------
2081
2082    procedure Validate_Static_Object_Name (N : Node_Id) is
2083       E : Entity_Id;
2084
2085       function Is_Primary (N : Node_Id) return Boolean;
2086       --  Determine whether node is syntactically a primary in an expression
2087       --  This function should probably be somewhere else ???
2088       --  Also it does not do what it says, e.g if N is a binary operator
2089       --  whose parent is a binary operator, Is_Primary returns True ???
2090
2091       ----------------
2092       -- Is_Primary --
2093       ----------------
2094
2095       function Is_Primary (N : Node_Id) return Boolean is
2096          K : constant Node_Kind := Nkind (Parent (N));
2097
2098       begin
2099          case K is
2100             when N_Op | N_Membership_Test =>
2101                return True;
2102
2103             when N_Aggregate
2104                | N_Component_Association
2105                | N_Index_Or_Discriminant_Constraint =>
2106                return True;
2107
2108             when N_Attribute_Reference =>
2109                return Attribute_Name (Parent (N)) /= Name_Address
2110                  and then Attribute_Name (Parent (N)) /= Name_Access
2111                  and then Attribute_Name (Parent (N)) /= Name_Unchecked_Access
2112                  and then
2113                    Attribute_Name (Parent (N)) /= Name_Unrestricted_Access;
2114
2115             when N_Indexed_Component =>
2116                return (N /= Prefix (Parent (N))
2117                  or else Is_Primary (Parent (N)));
2118
2119             when N_Qualified_Expression | N_Type_Conversion =>
2120                return Is_Primary (Parent (N));
2121
2122             when N_Assignment_Statement | N_Object_Declaration =>
2123                return (N = Expression (Parent (N)));
2124
2125             when N_Selected_Component =>
2126                return Is_Primary (Parent (N));
2127
2128             when others =>
2129                return False;
2130          end case;
2131       end Is_Primary;
2132
2133    --  Start of processing for Validate_Static_Object_Name
2134
2135    begin
2136       if not In_Preelaborated_Unit
2137         or else not Comes_From_Source (N)
2138         or else In_Subprogram_Or_Concurrent_Unit
2139         or else Ekind (Current_Scope) = E_Block
2140       then
2141          return;
2142
2143       --  Filter out cases where primary is default in a component declaration,
2144       --  discriminant specification, or actual in a record type initialization
2145       --  call.
2146
2147       --  Initialization call of internal types
2148
2149       elsif Nkind (Parent (N)) = N_Procedure_Call_Statement then
2150
2151          if Present (Parent (Parent (N)))
2152            and then Nkind (Parent (Parent (N))) = N_Freeze_Entity
2153          then
2154             return;
2155          end if;
2156
2157          if Nkind (Name (Parent (N))) = N_Identifier
2158            and then not Comes_From_Source (Entity (Name (Parent (N))))
2159          then
2160             return;
2161          end if;
2162       end if;
2163
2164       --  Error if the name is a primary in an expression. The parent must not
2165       --  be an operator, or a selected component or an indexed component that
2166       --  is itself a primary. Entities that are actuals do not need to be
2167       --  checked, because the call itself will be diagnosed.
2168
2169       if Is_Primary (N)
2170         and then (not Inside_A_Generic
2171                    or else Present (Enclosing_Generic_Body (N)))
2172       then
2173          if Ekind (Entity (N)) = E_Variable
2174            or else Ekind (Entity (N)) in Formal_Object_Kind
2175          then
2176             Flag_Non_Static_Expr
2177               ("non-static object name in preelaborated unit", N);
2178
2179          --  Give an error for a reference to a nonstatic constant, unless the
2180          --  constant is in another GNAT library unit that is preelaborable.
2181
2182          elsif Ekind (Entity (N)) = E_Constant
2183            and then not Is_Static_Expression (N)
2184          then
2185             E := Entity (N);
2186
2187             if Is_Internal_File_Name (Unit_File_Name (Get_Source_Unit (N)))
2188               and then
2189                 Enclosing_Lib_Unit_Node (N) /= Enclosing_Lib_Unit_Node (E)
2190               and then (Is_Preelaborated (Scope (E))
2191                           or else Is_Pure (Scope (E))
2192                           or else (Present (Renamed_Object (E))
2193                                      and then
2194                                        Is_Entity_Name (Renamed_Object (E))
2195                                      and then
2196                                        (Is_Preelaborated
2197                                          (Scope (Renamed_Object (E)))
2198                                             or else
2199                                               Is_Pure (Scope
2200                                                 (Renamed_Object (E))))))
2201             then
2202                null;
2203
2204             --  This is the error case
2205
2206             else
2207                --  In GNAT mode, this is just a warning, to allow it to be
2208                --  judiciously turned off. Otherwise it is a real error.
2209
2210                if GNAT_Mode then
2211                   Error_Msg_N
2212                     ("?non-static constant in preelaborated unit", N);
2213                else
2214                   Flag_Non_Static_Expr
2215                     ("non-static constant in preelaborated unit", N);
2216                end if;
2217             end if;
2218          end if;
2219       end if;
2220    end Validate_Static_Object_Name;
2221
2222 end Sem_Cat;