OSDN Git Service

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