OSDN Git Service

* config/arm/crti.asm: Give _init and _fini function type.
[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-2004, 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 2,  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 COPYING.  If not, write --
19 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
20 -- MA 02111-1307, USA.                                                      --
21 --                                                                          --
22 -- GNAT was originally developed  by the GNAT team at  New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
24 --                                                                          --
25 ------------------------------------------------------------------------------
26
27 with Atree;    use Atree;
28 with Debug;    use Debug;
29 with Einfo;    use Einfo;
30 with Elists;   use Elists;
31 with Errout;   use Errout;
32 with Exp_Tss;  use Exp_Tss;
33 with Fname;    use Fname;
34 with Lib;      use Lib;
35 with Nlists;   use Nlists;
36 with Sem;      use Sem;
37 with Sem_Eval; use Sem_Eval;
38 with Sem_Util; use Sem_Util;
39 with Sinfo;    use Sinfo;
40 with Snames;   use Snames;
41 with Stand;    use Stand;
42
43 package body Sem_Cat is
44
45    -----------------------
46    -- Local Subprograms --
47    -----------------------
48
49    procedure Check_Categorization_Dependencies
50      (Unit_Entity     : Entity_Id;
51       Depended_Entity : Entity_Id;
52       Info_Node       : Node_Id;
53       Is_Subunit      : Boolean);
54    --  This procedure checks that the categorization of a lib unit and that
55    --  of the depended unit satisfy dependency restrictions.
56    --  The depended_entity can be the entity in a with_clause item, in which
57    --  case Info_Node denotes that item. The depended_entity can also be the
58    --  parent unit of a child unit, in which case Info_Node is the declaration
59    --  of the child unit.  The error message is posted on Info_Node, and is
60    --  specialized if Is_Subunit is true.
61
62    procedure Check_Non_Static_Default_Expr
63      (Type_Def : Node_Id;
64       Obj_Decl : Node_Id);
65    --  Iterate through the component list of a record definition, check
66    --  that no component is declared with a nonstatic default value.
67    --  If a nonstatic default exists, report an error on Obj_Decl.
68
69    --  Iterate through the component list of a record definition, check
70    --  that no component is declared with a non-static default value.
71
72    function Missing_Read_Write_Attributes (E : Entity_Id) return Boolean;
73    --  Return True if the entity or one of its subcomponent is an access
74    --  type which does not have user-defined Read and Write attribute.
75
76    function In_RCI_Declaration (N : Node_Id) return Boolean;
77    --  Determines if a declaration is  within the visible part of  a Remote
78    --  Call Interface compilation unit, for semantic checking purposes only,
79    --  (returns false within an instance and within the package body).
80
81    function In_RT_Declaration return Boolean;
82    --  Determines if current scope is within a Remote Types compilation unit,
83    --  for semantic checking purposes.
84
85    function Is_Non_Remote_Access_Type (E : Entity_Id) return Boolean;
86    --  Returns true if the entity is a non-remote access type
87
88    function In_Shared_Passive_Unit return Boolean;
89    --  Determines if current scope is within a Shared Passive compilation unit
90
91    function Static_Discriminant_Expr (L : List_Id) return Boolean;
92    --  Iterate through the list of discriminants to check if any of them
93    --  contains non-static default expression, which is a violation in
94    --  a preelaborated library unit.
95
96    procedure Validate_Remote_Access_Object_Type_Declaration (T : Entity_Id);
97    --  Check validity of declaration if RCI or RT unit. It should not contain
98    --  the declaration of an access-to-object type unless it is a
99    --  general access type that designates a class-wide limited
100    --  private type. There are also constraints about the primitive
101    --  subprograms of the class-wide type. RM E.2 (9, 13, 14)
102
103    function Is_Recursively_Limited_Private (E : Entity_Id) return Boolean;
104    --  Return True if E is a limited private type, or if E is a private
105    --  extension of a type whose parent verifies this property (hence the
106    --  recursive keyword).
107
108    ---------------------------------------
109    -- Check_Categorization_Dependencies --
110    ---------------------------------------
111
112    procedure Check_Categorization_Dependencies
113      (Unit_Entity     : Entity_Id;
114       Depended_Entity : Entity_Id;
115       Info_Node       : Node_Id;
116       Is_Subunit      : Boolean)
117    is
118       N : constant Node_Id := Info_Node;
119
120       type Categorization is
121          (Pure, Shared_Passive, Remote_Types,
122            Remote_Call_Interface, Pre_Elaborated, Normal);
123
124       Unit_Category : Categorization;
125       With_Category : Categorization;
126
127       function Get_Categorization (E : Entity_Id) return Categorization;
128       --  Check categorization flags from entity, and return in the form
129       --  of a corresponding enumeration value.
130
131       ------------------------
132       -- Get_Categorization --
133       ------------------------
134
135       function Get_Categorization (E : Entity_Id) return Categorization is
136       begin
137          if Is_Preelaborated (E) then
138             return Pre_Elaborated;
139          elsif Is_Pure (E) then
140             return Pure;
141          elsif Is_Shared_Passive (E) then
142             return Shared_Passive;
143          elsif Is_Remote_Types (E) then
144             return Remote_Types;
145          elsif Is_Remote_Call_Interface (E) then
146             return Remote_Call_Interface;
147          else
148             return Normal;
149          end if;
150       end Get_Categorization;
151
152    --  Start of processing for Check_Categorization_Dependencies
153
154    begin
155       --  Intrinsic subprograms are preelaborated, so do not impose any
156       --  categorization dependencies.
157
158       if Is_Intrinsic_Subprogram (Depended_Entity) then
159          return;
160       end if;
161
162       Unit_Category := Get_Categorization (Unit_Entity);
163       With_Category := Get_Categorization (Depended_Entity);
164
165       if With_Category > Unit_Category then
166
167          if (Unit_Category = Remote_Types
168                  or else Unit_Category = Remote_Call_Interface)
169            and then In_Package_Body (Unit_Entity)
170          then
171             null;
172
173          elsif Is_Subunit then
174             Error_Msg_NE ("subunit cannot depend on&"
175               & " (parent has wrong categorization)", N, Depended_Entity);
176          else
177             Error_Msg_NE ("current unit cannot depend on&"
178               & " (wrong categorization)", N, Depended_Entity);
179          end if;
180       end if;
181
182    end Check_Categorization_Dependencies;
183
184    -----------------------------------
185    -- Check_Non_Static_Default_Expr --
186    -----------------------------------
187
188    procedure Check_Non_Static_Default_Expr
189      (Type_Def : Node_Id;
190       Obj_Decl : Node_Id)
191    is
192       Recdef         : Node_Id;
193       Component_Decl : Node_Id;
194
195    begin
196       if Nkind (Type_Def) = N_Derived_Type_Definition then
197          Recdef := Record_Extension_Part (Type_Def);
198
199          if No (Recdef) then
200             return;
201          end if;
202
203       else
204          Recdef := Type_Def;
205       end if;
206
207       --  Check that component declarations do not involve:
208
209       --    a. a non-static default expression, where the object is
210       --       declared to be default initialized.
211
212       --    b. a dynamic Itype (discriminants and constraints)
213
214       if Null_Present (Recdef) then
215          return;
216       else
217          Component_Decl := First (Component_Items (Component_List (Recdef)));
218       end if;
219
220       while Present (Component_Decl)
221         and then Nkind (Component_Decl) = N_Component_Declaration
222       loop
223          if Present (Expression (Component_Decl))
224            and then Nkind (Expression (Component_Decl)) /= N_Null
225            and then not Is_Static_Expression (Expression (Component_Decl))
226          then
227             Error_Msg_Sloc := Sloc (Component_Decl);
228             Error_Msg_F
229               ("object in preelaborated unit has non-static default#",
230                Obj_Decl);
231
232          --  Fix this later ???
233
234          --  elsif Has_Dynamic_Itype (Component_Decl) then
235          --     Error_Msg_N
236          --       ("dynamic type discriminant," &
237          --        " constraint in preelaborated unit",
238          --        Component_Decl);
239          end if;
240
241          Next (Component_Decl);
242       end loop;
243    end Check_Non_Static_Default_Expr;
244
245    ---------------------------
246    -- In_Preelaborated_Unit --
247    ---------------------------
248
249    function In_Preelaborated_Unit return Boolean is
250       Unit_Entity : constant Entity_Id := Current_Scope;
251       Unit_Kind   : constant Node_Kind :=
252                       Nkind (Unit (Cunit (Current_Sem_Unit)));
253
254    begin
255       --  There are no constraints on body of remote_call_interface or
256       --  remote_types packages..
257
258       return (Unit_Entity /= Standard_Standard)
259         and then (Is_Preelaborated (Unit_Entity)
260                     or else Is_Pure (Unit_Entity)
261                     or else Is_Shared_Passive (Unit_Entity)
262                     or else
263                       ((Is_Remote_Types (Unit_Entity)
264                                or else Is_Remote_Call_Interface (Unit_Entity))
265                          and then Ekind (Unit_Entity) = E_Package
266                          and then Unit_Kind /= N_Package_Body
267                          and then not In_Package_Body (Unit_Entity)
268                          and then not In_Instance));
269    end In_Preelaborated_Unit;
270
271    ------------------
272    -- In_Pure_Unit --
273    ------------------
274
275    function In_Pure_Unit return Boolean is
276    begin
277       return Is_Pure (Current_Scope);
278    end In_Pure_Unit;
279
280    ------------------------
281    -- In_RCI_Declaration --
282    ------------------------
283
284    function In_RCI_Declaration (N : Node_Id) return Boolean is
285       Unit_Entity : constant Entity_Id := Current_Scope;
286       Unit_Kind   : constant Node_Kind :=
287                       Nkind (Unit (Cunit (Current_Sem_Unit)));
288
289    begin
290       --  There are no restrictions on the private part or body
291       --  of an RCI unit.
292
293       return Is_Remote_Call_Interface (Unit_Entity)
294         and then (Ekind (Unit_Entity) = E_Package
295                   or else Ekind (Unit_Entity) = E_Generic_Package)
296         and then Unit_Kind /= N_Package_Body
297         and then List_Containing (N) =
298                   Visible_Declarations
299                     (Specification (Unit_Declaration_Node (Unit_Entity)))
300         and then not In_Package_Body (Unit_Entity)
301         and then not In_Instance;
302    end In_RCI_Declaration;
303
304    -----------------------
305    -- In_RT_Declaration --
306    -----------------------
307
308    function In_RT_Declaration return Boolean is
309       Unit_Entity : constant Entity_Id := Current_Scope;
310       Unit_Kind   : constant Node_Kind :=
311                       Nkind (Unit (Cunit (Current_Sem_Unit)));
312
313    begin
314       --  There are no restrictions on the body of a Remote Types unit.
315
316       return Is_Remote_Types (Unit_Entity)
317         and then (Ekind (Unit_Entity) = E_Package
318                    or else Ekind (Unit_Entity) = E_Generic_Package)
319         and then Unit_Kind /= N_Package_Body
320         and then not In_Package_Body (Unit_Entity)
321         and then not In_Instance;
322    end In_RT_Declaration;
323
324    ----------------------------
325    -- In_Shared_Passive_Unit --
326    ----------------------------
327
328    function In_Shared_Passive_Unit return Boolean is
329       Unit_Entity : constant Entity_Id := Current_Scope;
330
331    begin
332       return Is_Shared_Passive (Unit_Entity);
333    end In_Shared_Passive_Unit;
334
335    ---------------------------------------
336    -- In_Subprogram_Task_Protected_Unit --
337    ---------------------------------------
338
339    function In_Subprogram_Task_Protected_Unit return Boolean is
340       E : Entity_Id;
341
342    begin
343       --  The following is to verify that a declaration is inside
344       --  subprogram, generic subprogram, task unit, protected unit.
345       --  Used to validate if a lib. unit is Pure. RM 10.2.1(16).
346
347       --  Use scope chain to check successively outer scopes
348
349       E := Current_Scope;
350       loop
351          if Is_Subprogram (E)
352               or else
353             Is_Generic_Subprogram (E)
354               or else
355             Is_Concurrent_Type (E)
356          then
357             return True;
358
359          elsif E = Standard_Standard then
360             return False;
361          end if;
362
363          E := Scope (E);
364       end loop;
365    end In_Subprogram_Task_Protected_Unit;
366
367    -------------------------------
368    -- Is_Non_Remote_Access_Type --
369    -------------------------------
370
371    function Is_Non_Remote_Access_Type (E : Entity_Id) return Boolean is
372    begin
373       return Is_Access_Type (E)
374         and then not Is_Remote_Access_To_Class_Wide_Type (E)
375         and then not Is_Remote_Access_To_Subprogram_Type (E);
376    end Is_Non_Remote_Access_Type;
377
378    ------------------------------------
379    -- Is_Recursively_Limited_Private --
380    ------------------------------------
381
382    function Is_Recursively_Limited_Private (E : Entity_Id) return Boolean is
383       P : constant Node_Id := Parent (E);
384
385    begin
386       if Nkind (P) = N_Private_Type_Declaration
387         and then Is_Limited_Record (E)
388       then
389          return True;
390       elsif Nkind (P) = N_Private_Extension_Declaration then
391          return Is_Recursively_Limited_Private (Etype (E));
392       elsif Nkind (P) = N_Formal_Type_Declaration
393         and then Ekind (E) = E_Record_Type_With_Private
394         and then Is_Generic_Type (E)
395         and then Is_Limited_Record (E)
396       then
397          return True;
398       else
399          return False;
400       end if;
401    end Is_Recursively_Limited_Private;
402
403    ----------------------------------
404    -- Missing_Read_Write_Attribute --
405    ----------------------------------
406
407    function Missing_Read_Write_Attributes (E : Entity_Id) return Boolean is
408       Component      : Entity_Id;
409       Component_Type : Entity_Id;
410
411       function Has_Read_Write_Attributes (E : Entity_Id) return Boolean;
412       --  Return True if entity has Read and Write attributes
413
414       -------------------------------
415       -- Has_Read_Write_Attributes --
416       -------------------------------
417
418       function Has_Read_Write_Attributes (E : Entity_Id) return Boolean is
419          Rep_Item        : Node_Id := First_Rep_Item (E);
420          Read_Attribute  : Boolean := False;
421          Write_Attribute : Boolean := False;
422
423       begin
424          --  We start from the declaration node and then loop until the end
425          --  of the list until we find those two attribute definition clauses.
426
427          while Present (Rep_Item) loop
428             if Chars (Rep_Item) = Name_Read then
429                Read_Attribute := True;
430             elsif Chars (Rep_Item) = Name_Write then
431                Write_Attribute := True;
432             end if;
433
434             if Read_Attribute and Write_Attribute then
435                return True;
436             end if;
437
438             Next_Rep_Item (Rep_Item);
439          end loop;
440
441          return False;
442       end Has_Read_Write_Attributes;
443
444    --  Start of processing for Missing_Read_Write_Attributes
445
446    begin
447       if Has_Read_Write_Attributes (E) then
448          return False;
449       elsif Is_Non_Remote_Access_Type (E) then
450          return True;
451       end if;
452
453       if Is_Record_Type (E) then
454          Component := First_Entity (E);
455          while Present (Component) loop
456             Component_Type := Etype (Component);
457
458             if (Is_Non_Remote_Access_Type (Component_Type)
459                 or else Is_Record_Type (Component_Type))
460               and then Missing_Read_Write_Attributes (Component_Type)
461             then
462                return True;
463             end if;
464
465             Next_Entity (Component);
466          end loop;
467       end if;
468
469       return False;
470    end Missing_Read_Write_Attributes;
471
472    -------------------------------------
473    -- Set_Categorization_From_Pragmas --
474    -------------------------------------
475
476    procedure Set_Categorization_From_Pragmas (N : Node_Id) is
477       P   : constant Node_Id := Parent (N);
478       S   : constant Entity_Id := Current_Scope;
479
480       procedure Set_Parents (Visibility : Boolean);
481          --  If this is a child instance, the parents are not immediately
482          --  visible during analysis. Make them momentarily visible so that
483          --  the argument of the pragma can be resolved properly, and reset
484          --  afterwards.
485
486       procedure Set_Parents (Visibility : Boolean) is
487          Par : Entity_Id := Scope (S);
488
489       begin
490          while Present (Par) and then Par /= Standard_Standard loop
491             Set_Is_Immediately_Visible (Par, Visibility);
492             Par := Scope (Par);
493          end loop;
494       end Set_Parents;
495
496    begin
497       --  Deal with categorization pragmas in Pragmas of Compilation_Unit.
498       --  The purpose is to set categorization flags before analyzing the
499       --  unit itself, so as to diagnose violations of categorization as
500       --  we process each declaration, even though the pragma appears after
501       --  the unit.
502
503       if Nkind (P) /= N_Compilation_Unit then
504          return;
505       end if;
506
507       declare
508          PN : Node_Id := First (Pragmas_After (Aux_Decls_Node (P)));
509
510       begin
511
512          if Is_Child_Unit (S)
513            and then Is_Generic_Instance (S)
514          then
515             Set_Parents (True);
516          end if;
517
518          while Present (PN) loop
519
520             --  Skip implicit types that may have been introduced by
521             --  previous analysis.
522
523             if Nkind (PN) = N_Pragma then
524
525                case Get_Pragma_Id (Chars (PN)) is
526                   when Pragma_All_Calls_Remote   |
527                     Pragma_Preelaborate          |
528                     Pragma_Pure                  |
529                     Pragma_Remote_Call_Interface |
530                     Pragma_Remote_Types          |
531                     Pragma_Shared_Passive        => Analyze (PN);
532                   when others                    => null;
533                end case;
534             end if;
535
536             Next (PN);
537          end loop;
538          if Is_Child_Unit (S)
539            and then Is_Generic_Instance (S)
540          then
541             Set_Parents (False);
542          end if;
543
544       end;
545    end Set_Categorization_From_Pragmas;
546
547    -----------------------------------
548    -- Set_Categorization_From_Scope --
549    -----------------------------------
550
551    procedure Set_Categorization_From_Scope (E : Entity_Id; Scop : Entity_Id) is
552       Declaration   : Node_Id := Empty;
553       Specification : Node_Id := Empty;
554
555    begin
556       Set_Is_Pure (E,
557         Is_Pure (Scop) and then Is_Library_Level_Entity (E));
558
559       if not Is_Remote_Call_Interface (E) then
560          if Ekind (E) in Subprogram_Kind then
561             Declaration := Unit_Declaration_Node (E);
562
563             if False
564               or else Nkind (Declaration) = N_Subprogram_Body
565               or else Nkind (Declaration) = N_Subprogram_Renaming_Declaration
566             then
567                Specification := Corresponding_Spec (Declaration);
568             end if;
569          end if;
570
571          --  A subprogram body or renaming-as-body is a remote call
572          --  interface if it serves as the completion of a subprogram
573          --  declaration that is a remote call interface.
574
575          if Nkind (Specification) in N_Entity then
576             Set_Is_Remote_Call_Interface
577               (E, Is_Remote_Call_Interface (Specification));
578
579          --  A subprogram declaration is a remote call interface when it is
580          --  declared within the visible part of, or declared by, a library
581          --  unit declaration that is a remote call interface.
582
583          else
584             Set_Is_Remote_Call_Interface
585               (E, Is_Remote_Call_Interface (Scop)
586                     and then not (In_Private_Part (Scop)
587                                     or else In_Package_Body (Scop)));
588          end if;
589       end if;
590
591       Set_Is_Remote_Types (E, Is_Remote_Types (Scop));
592    end Set_Categorization_From_Scope;
593
594    ------------------------------
595    -- Static_Discriminant_Expr --
596    ------------------------------
597
598    --  We need to accomodate a Why_Not_Static call somehow here ???
599
600    function Static_Discriminant_Expr (L : List_Id) return Boolean is
601       Discriminant_Spec : Node_Id;
602
603    begin
604       Discriminant_Spec := First (L);
605       while Present (Discriminant_Spec) loop
606          if Present (Expression (Discriminant_Spec))
607            and then not Is_Static_Expression (Expression (Discriminant_Spec))
608          then
609             return False;
610          end if;
611
612          Next (Discriminant_Spec);
613       end loop;
614
615       return True;
616    end Static_Discriminant_Expr;
617
618    --------------------------------------
619    -- Validate_Access_Type_Declaration --
620    --------------------------------------
621
622    procedure Validate_Access_Type_Declaration (T : Entity_Id; N : Node_Id) is
623       Def : constant Node_Id := Type_Definition (N);
624
625    begin
626       case Nkind (Def) is
627          when N_Access_To_Subprogram_Definition =>
628
629             --  A pure library_item must not contain the declaration of a
630             --  named access type, except within a subprogram, generic
631             --  subprogram, task unit, or protected unit (RM 10.2.1(16)).
632
633             if Comes_From_Source (T)
634                and then In_Pure_Unit
635                and then not In_Subprogram_Task_Protected_Unit
636             then
637                Error_Msg_N ("named access type not allowed in pure unit", T);
638             end if;
639
640          when N_Access_To_Object_Definition =>
641
642             if Comes_From_Source (T)
643               and then In_Pure_Unit
644               and then not In_Subprogram_Task_Protected_Unit
645             then
646                Error_Msg_N
647                  ("named access type not allowed in pure unit", T);
648             end if;
649
650             --  Check for RCI or RT unit type declaration. It should not
651             --  contain the declaration of an access-to-object type unless it
652             --  is a general access type that designates a class-wide limited
653             --  private type. There are also constraints about the primitive
654             --  subprograms of the class-wide type.
655
656             Validate_Remote_Access_Object_Type_Declaration (T);
657
658             --  Check for shared passive unit type declaration. It should
659             --  not contain the declaration of access to class wide type,
660             --  access to task type and access to protected type with entry.
661
662             Validate_SP_Access_Object_Type_Decl (T);
663
664          when others => null;
665       end case;
666
667       --  Set categorization flag from package on entity as well, to allow
668       --  easy checks later on for required validations of RCI or RT units.
669       --  This is only done for entities that are in the original source.
670
671       if Comes_From_Source (T)
672         and then not (In_Package_Body (Scope (T))
673                         or else In_Private_Part (Scope (T)))
674       then
675          Set_Is_Remote_Call_Interface
676            (T, Is_Remote_Call_Interface (Scope (T)));
677          Set_Is_Remote_Types
678            (T, Is_Remote_Types (Scope (T)));
679       end if;
680    end Validate_Access_Type_Declaration;
681
682    ----------------------------
683    -- Validate_Ancestor_Part --
684    ----------------------------
685
686    procedure Validate_Ancestor_Part (N : Node_Id) is
687       A : constant Node_Id   := Ancestor_Part (N);
688       T : constant Entity_Id := Entity (A);
689
690    begin
691       if In_Preelaborated_Unit
692         and then not In_Subprogram_Or_Concurrent_Unit
693         and then (not Inside_A_Generic
694                    or else Present (Enclosing_Generic_Body (N)))
695       then
696          --  We relax the restriction of 10.2.1(9) within GNAT
697          --  units to allow packages such as Ada.Strings.Unbounded
698          --  to be implemented (i.p., Null_Unbounded_String).
699          --  (There are ACVC tests that check that the restriction
700          --  is enforced, but note that AI-161, once approved,
701          --  will relax the restriction prohibiting default-
702          --  initialized objects of private and controlled
703          --  types.)
704
705          if Is_Private_Type (T)
706            and then not Is_Internal_File_Name
707                           (Unit_File_Name (Get_Source_Unit (N)))
708          then
709             Error_Msg_N
710               ("private ancestor type not allowed in preelaborated unit", A);
711
712          elsif Is_Record_Type (T) then
713             if Nkind (Parent (T)) = N_Full_Type_Declaration then
714                Check_Non_Static_Default_Expr
715                  (Type_Definition (Parent (T)), A);
716             end if;
717          end if;
718       end if;
719    end Validate_Ancestor_Part;
720
721    ----------------------------------------
722    -- Validate_Categorization_Dependency --
723    ----------------------------------------
724
725    procedure Validate_Categorization_Dependency
726      (N : Node_Id;
727       E : Entity_Id)
728    is
729       K          : constant Node_Kind := Nkind (N);
730       P          : Node_Id            := Parent (N);
731       U          : Entity_Id := E;
732       Is_Subunit : constant Boolean := Nkind (P) = N_Subunit;
733
734    begin
735       --  Only validate library units and subunits. For subunits, checks
736       --  concerning withed units apply to the parent compilation unit.
737
738       if Is_Subunit then
739          P := Parent (P);
740          U := Scope (E);
741
742          while Present (U)
743            and then not Is_Compilation_Unit (U)
744            and then not Is_Child_Unit (U)
745          loop
746             U := Scope (U);
747          end loop;
748
749       end if;
750
751       if Nkind (P) /= N_Compilation_Unit then
752          return;
753       end if;
754
755       --  Body of RCI unit does not need validation.
756
757       if Is_Remote_Call_Interface (E)
758         and then (Nkind (N) = N_Package_Body
759                    or else Nkind (N) = N_Subprogram_Body)
760       then
761          return;
762       end if;
763
764       --  Ada 2005 (AI-50217): Process explicit non-limited with_clauses
765
766       declare
767          Item             : Node_Id;
768          Entity_Of_Withed : Entity_Id;
769
770       begin
771          Item := First (Context_Items (P));
772
773          while Present (Item) loop
774             if Nkind (Item) = N_With_Clause
775               and then not (Implicit_With (Item)
776                               or else Limited_Present (Item))
777             then
778                Entity_Of_Withed := Entity (Name (Item));
779                Check_Categorization_Dependencies
780                 (U, Entity_Of_Withed, Item, Is_Subunit);
781             end if;
782
783             Next (Item);
784          end loop;
785       end;
786
787       --  Child depends on parent; therefore parent should also
788       --  be categorized and satify the dependency hierarchy.
789
790       --  Check if N is a child spec.
791
792       if (K in N_Generic_Declaration              or else
793           K in N_Generic_Instantiation            or else
794           K in N_Generic_Renaming_Declaration     or else
795           K =  N_Package_Declaration              or else
796           K =  N_Package_Renaming_Declaration     or else
797           K =  N_Subprogram_Declaration           or else
798           K =  N_Subprogram_Renaming_Declaration)
799         and then Present (Parent_Spec (N))
800       then
801          Check_Categorization_Dependencies (E, Scope (E), N, False);
802
803          --  Verify that public child of an RCI library unit
804          --  must also be an RCI library unit (RM E.2.3(15)).
805
806          if Is_Remote_Call_Interface (Scope (E))
807            and then not Private_Present (P)
808            and then not Is_Remote_Call_Interface (E)
809          then
810             Error_Msg_N ("public child of rci unit must also be rci unit", N);
811          end if;
812       end if;
813    end Validate_Categorization_Dependency;
814
815    --------------------------------
816    -- Validate_Controlled_Object --
817    --------------------------------
818
819    procedure Validate_Controlled_Object (E : Entity_Id) is
820    begin
821       --  For now, never apply this check for internal GNAT units, since we
822       --  have a number of cases in the library where we are stuck with objects
823       --  of this type, and the RM requires Preelaborate.
824
825       --  For similar reasons, we only do this check for source entities, since
826       --  we generate entities of this type in some situations.
827
828       --  Note that the 10.2.1(9) restrictions are not relevant to us anyway.
829       --  We have to enforce them for RM compatibility, but we have no trouble
830       --  accepting these objects and doing the right thing. Note that there is
831       --  no requirement that Preelaborate not actually generate any code!
832
833       if In_Preelaborated_Unit
834         and then not Debug_Flag_PP
835         and then Comes_From_Source (E)
836         and then not
837           Is_Internal_File_Name (Unit_File_Name (Get_Source_Unit (E)))
838         and then (not Inside_A_Generic
839                    or else Present (Enclosing_Generic_Body (E)))
840         and then not Is_Protected_Type (Etype (E))
841       then
842          Error_Msg_N
843            ("library level controlled object not allowed in " &
844             "preelaborated unit", E);
845       end if;
846    end Validate_Controlled_Object;
847
848    --------------------------------------
849    -- Validate_Null_Statement_Sequence --
850    --------------------------------------
851
852    procedure Validate_Null_Statement_Sequence (N : Node_Id) is
853       Item : Node_Id;
854
855    begin
856       if In_Preelaborated_Unit then
857          Item := First (Statements (Handled_Statement_Sequence (N)));
858
859          while Present (Item) loop
860             if Nkind (Item) /= N_Label
861               and then Nkind (Item) /= N_Null_Statement
862             then
863                Error_Msg_N
864                  ("statements not allowed in preelaborated unit", Item);
865                exit;
866             end if;
867
868             Next (Item);
869          end loop;
870       end if;
871    end Validate_Null_Statement_Sequence;
872
873    ---------------------------------
874    -- Validate_Object_Declaration --
875    ---------------------------------
876
877    procedure Validate_Object_Declaration (N : Node_Id) is
878       Id  : constant Entity_Id  := Defining_Identifier (N);
879       E   : constant Node_Id    := Expression (N);
880       Odf : constant Node_Id    := Object_Definition (N);
881       T   : constant Entity_Id  := Etype (Id);
882
883    begin
884       --  Verify that any access to subprogram object does not have in its
885       --  subprogram profile access type parameters or limited parameters
886       --  without Read and Write attributes (E.2.3(13)).
887
888       Validate_RCI_Subprogram_Declaration (N);
889
890       --  Check that if we are in preelaborated elaboration code, then we
891       --  do not have an instance of a default initialized private, task or
892       --  protected object declaration which would violate (RM 10.2.1(9)).
893       --  Note that constants are never default initialized (and the test
894       --  below also filters out deferred constants). A variable is default
895       --  initialized if it does *not* have an initialization expression.
896
897       --  Filter out cases that are not declaration of a variable from source
898
899       if Nkind (N) /= N_Object_Declaration
900         or else Constant_Present (N)
901         or else not Comes_From_Source (Id)
902       then
903          return;
904       end if;
905
906       --  Exclude generic specs from the checks (this will get rechecked
907       --  on instantiations).
908
909       if Inside_A_Generic
910         and then not Present (Enclosing_Generic_Body (Id))
911       then
912          return;
913       end if;
914
915       --  Required checks for declaration that is in a preelaborated
916       --  package and is not within some subprogram.
917
918       if In_Preelaborated_Unit
919         and then not In_Subprogram_Or_Concurrent_Unit
920       then
921          --  Check for default initialized variable case. Note that in
922          --  accordance with (RM B.1(24)) imported objects are not
923          --  subject to default initialization.
924
925          if No (E) and then not Is_Imported (Id) then
926             declare
927                Ent : Entity_Id := T;
928
929             begin
930                --  An array whose component type is a record with nonstatic
931                --  default expressions is a violation, so we get the array's
932                --  component type.
933
934                if Is_Array_Type (Ent) then
935                   declare
936                      Comp_Type : Entity_Id := Component_Type (Ent);
937
938                   begin
939                      while Is_Array_Type (Comp_Type) loop
940                         Comp_Type := Component_Type (Comp_Type);
941                      end loop;
942
943                      Ent := Comp_Type;
944                   end;
945                end if;
946
947                --  Object decl. that is of record type and has no default expr.
948                --  should check if there is any non-static default expression
949                --  in component decl. of the record type decl.
950
951                if Is_Record_Type (Ent) then
952                   if Nkind (Parent (Ent)) = N_Full_Type_Declaration then
953                      Check_Non_Static_Default_Expr
954                        (Type_Definition (Parent (Ent)), N);
955
956                   elsif Nkind (Odf) = N_Subtype_Indication
957                     and then not Is_Array_Type (T)
958                     and then not Is_Private_Type (T)
959                   then
960                      Check_Non_Static_Default_Expr (Type_Definition
961                        (Parent (Entity (Subtype_Mark (Odf)))), N);
962                   end if;
963                end if;
964
965                --  We relax the restriction of 10.2.1(9) within GNAT
966                --  units. (There are ACVC tests that check that the
967                --  restriction is enforced, but note that AI-161,
968                --  once approved, will relax the restriction prohibiting
969                --  default-initialized objects of private types, and
970                --  will recommend a pragma for marking private types.)
971
972                if (Is_Private_Type (Ent)
973                     or else Depends_On_Private (Ent))
974                  and then not Is_Internal_File_Name
975                                 (Unit_File_Name (Get_Source_Unit (N)))
976                then
977                   Error_Msg_N
978                     ("private object not allowed in preelaborated unit", N);
979                   return;
980
981                --  Access to Task or Protected type
982
983                elsif Is_Entity_Name (Odf)
984                  and then Present (Etype (Odf))
985                  and then Is_Access_Type (Etype (Odf))
986                then
987                   Ent := Designated_Type (Etype (Odf));
988
989                elsif Is_Entity_Name (Odf) then
990                   Ent := Entity (Odf);
991
992                elsif Nkind (Odf) = N_Subtype_Indication then
993                   Ent := Etype (Subtype_Mark (Odf));
994
995                elsif
996                   Nkind (Odf) = N_Constrained_Array_Definition
997                then
998                   Ent := Component_Type (T);
999
1000                --  else
1001                --     return;
1002                end if;
1003
1004                if Is_Task_Type (Ent)
1005                  or else (Is_Protected_Type (Ent) and then Has_Entries (Ent))
1006                then
1007                   Error_Msg_N
1008                     ("concurrent object not allowed in preelaborated unit",
1009                      N);
1010                   return;
1011                end if;
1012             end;
1013          end if;
1014
1015          --  Non-static discriminant not allowed in preelaborayted unit
1016
1017          if Is_Record_Type (Etype (Id)) then
1018             declare
1019                ET  : constant Entity_Id := Etype (Id);
1020                EE  : constant Entity_Id := Etype (Etype (Id));
1021                PEE : Node_Id;
1022
1023             begin
1024                if Has_Discriminants (ET)
1025                  and then Present (EE)
1026                then
1027                   PEE := Parent (EE);
1028
1029                   if Nkind (PEE) = N_Full_Type_Declaration
1030                     and then not Static_Discriminant_Expr
1031                                   (Discriminant_Specifications (PEE))
1032                   then
1033                      Error_Msg_N
1034                        ("non-static discriminant in preelaborated unit",
1035                         PEE);
1036                   end if;
1037                end if;
1038             end;
1039          end if;
1040       end if;
1041
1042       --  A pure library_item must not contain the declaration of any
1043       --  variable except within  a subprogram, generic subprogram, task
1044       --  unit or protected unit (RM 10.2.1(16)).
1045
1046       if In_Pure_Unit
1047         and then not In_Subprogram_Task_Protected_Unit
1048       then
1049          Error_Msg_N ("declaration of variable not allowed in pure unit", N);
1050
1051       --  The visible part of an RCI library unit must not contain the
1052       --  declaration of a variable (RM E.1.3(9))
1053
1054       elsif In_RCI_Declaration (N) then
1055          Error_Msg_N ("declaration of variable not allowed in rci unit", N);
1056
1057       --  The visible part of a Shared Passive library unit must not contain
1058       --  the declaration of a variable (RM E.2.2(7))
1059
1060       elsif In_RT_Declaration then
1061          Error_Msg_N
1062            ("variable declaration not allowed in remote types unit", N);
1063       end if;
1064
1065    end Validate_Object_Declaration;
1066
1067    -------------------------------
1068    -- Validate_RCI_Declarations --
1069    -------------------------------
1070
1071    procedure Validate_RCI_Declarations (P : Entity_Id) is
1072       E : Entity_Id;
1073
1074    begin
1075       E := First_Entity (P);
1076       while Present (E) loop
1077          if Comes_From_Source (E) then
1078             if Is_Limited_Type (E) then
1079                Error_Msg_N
1080                  ("Limited type not allowed in rci unit", Parent (E));
1081                Explain_Limited_Type (E, Parent (E));
1082
1083             elsif Ekind (E) = E_Generic_Function
1084               or else Ekind (E) = E_Generic_Package
1085               or else Ekind (E) = E_Generic_Procedure
1086             then
1087                Error_Msg_N ("generic declaration not allowed in rci unit",
1088                  Parent (E));
1089
1090             elsif (Ekind (E) = E_Function
1091                     or else Ekind (E) = E_Procedure)
1092               and then Has_Pragma_Inline (E)
1093             then
1094                Error_Msg_N
1095                  ("inlined subprogram not allowed in rci unit", Parent (E));
1096
1097             --  Inner packages that are renamings need not be checked.
1098             --  Generic RCI packages are subject to the checks, but
1099             --  entities that come from formal packages are not part of the
1100             --  visible declarations of the package and are not checked.
1101
1102             elsif Ekind (E) = E_Package then
1103                if Present (Renamed_Entity (E)) then
1104                   null;
1105
1106                elsif Ekind (P) /= E_Generic_Package
1107                  or else List_Containing (Unit_Declaration_Node (E)) /=
1108                            Generic_Formal_Declarations
1109                              (Unit_Declaration_Node (P))
1110                then
1111                   Validate_RCI_Declarations (E);
1112                end if;
1113             end if;
1114          end if;
1115
1116          Next_Entity (E);
1117       end loop;
1118    end Validate_RCI_Declarations;
1119
1120    -----------------------------------------
1121    -- Validate_RCI_Subprogram_Declaration --
1122    -----------------------------------------
1123
1124    procedure Validate_RCI_Subprogram_Declaration (N : Node_Id) is
1125       K               : constant Node_Kind := Nkind (N);
1126       Profile         : List_Id;
1127       Id              : Node_Id;
1128       Param_Spec      : Node_Id;
1129       Param_Type      : Entity_Id;
1130       Base_Param_Type : Entity_Id;
1131       Type_Decl       : Node_Id;
1132       Error_Node      : Node_Id := N;
1133
1134    begin
1135       --  There are two possible cases in which this procedure is called:
1136
1137       --    1. called from Analyze_Subprogram_Declaration.
1138       --    2. called from Validate_Object_Declaration (access to subprogram).
1139
1140       if not In_RCI_Declaration (N) then
1141          return;
1142       end if;
1143
1144       if K = N_Subprogram_Declaration then
1145          Profile := Parameter_Specifications (Specification (N));
1146
1147       else pragma Assert (K = N_Object_Declaration);
1148          Id := Defining_Identifier (N);
1149
1150          if Nkind (Id) = N_Defining_Identifier
1151            and then Nkind (Parent (Etype (Id))) = N_Full_Type_Declaration
1152            and then Ekind (Etype (Id)) = E_Access_Subprogram_Type
1153          then
1154             Profile :=
1155               Parameter_Specifications (Type_Definition (Parent (Etype (Id))));
1156          else
1157             return;
1158          end if;
1159       end if;
1160
1161       --  Iterate through the parameter specification list, checking that
1162       --  no access parameter and no limited type parameter in the list.
1163       --  RM E.2.3 (14)
1164
1165       if Present (Profile) then
1166          Param_Spec := First (Profile);
1167
1168          while Present (Param_Spec) loop
1169             Param_Type := Etype (Defining_Identifier (Param_Spec));
1170             Type_Decl  := Parent (Param_Type);
1171
1172             if Ekind (Param_Type) = E_Anonymous_Access_Type then
1173
1174                if K = N_Subprogram_Declaration then
1175                   Error_Node := Param_Spec;
1176                end if;
1177
1178                --  Report error only if declaration is in source program.
1179
1180                if Comes_From_Source
1181                  (Defining_Entity (Specification (N)))
1182                then
1183                   Error_Msg_N
1184                     ("subprogram in rci unit cannot have access parameter",
1185                       Error_Node);
1186                end if;
1187
1188             --  For limited private type parameter, we check only the
1189             --  private declaration and ignore full type declaration,
1190             --  unless this is the only declaration for the type, eg.
1191             --  as a limited record.
1192
1193             elsif Is_Limited_Type (Param_Type)
1194               and then (Nkind (Type_Decl) = N_Private_Type_Declaration
1195                          or else
1196                         (Nkind (Type_Decl) = N_Full_Type_Declaration
1197                           and then not (Has_Private_Declaration (Param_Type))
1198                           and then Comes_From_Source (N)))
1199             then
1200                --  A limited parameter is legal only if user-specified
1201                --  Read and Write attributes exist for it.
1202                --  second part of RM E.2.3 (14)
1203
1204                if No (Full_View (Param_Type))
1205                  and then Ekind (Param_Type) /= E_Record_Type
1206                then
1207                   --  Type does not have completion yet, so if declared in
1208                   --  in the current RCI scope it is illegal, and will be
1209                   --  flagged subsequently.
1210                   return;
1211                end if;
1212
1213                Base_Param_Type := Base_Type (Underlying_Type (Param_Type));
1214
1215                if No (TSS (Base_Param_Type, TSS_Stream_Read))
1216                     or else
1217                   No (TSS (Base_Param_Type, TSS_Stream_Write))
1218                then
1219                   if K = N_Subprogram_Declaration then
1220                      Error_Node := Param_Spec;
1221                   end if;
1222
1223                   Error_Msg_N
1224                     ("limited parameter in rci unit "
1225                        & "must have read/write attributes ", Error_Node);
1226                   Explain_Limited_Type (Param_Type, Error_Node);
1227                end if;
1228             end if;
1229
1230             Next (Param_Spec);
1231          end loop;
1232       end if;
1233    end Validate_RCI_Subprogram_Declaration;
1234
1235    ----------------------------------------------------
1236    -- Validate_Remote_Access_Object_Type_Declaration --
1237    ----------------------------------------------------
1238
1239    procedure Validate_Remote_Access_Object_Type_Declaration (T : Entity_Id) is
1240       Direct_Designated_Type : Entity_Id;
1241       Desig_Type             : Entity_Id;
1242       Primitive_Subprograms  : Elist_Id;
1243       Subprogram             : Elmt_Id;
1244       Subprogram_Node        : Node_Id;
1245       Profile                : List_Id;
1246       Param_Spec             : Node_Id;
1247       Param_Type             : Entity_Id;
1248
1249    begin
1250       --  We are called from Analyze_Type_Declaration, and the Nkind
1251       --  of the given node is N_Access_To_Object_Definition.
1252
1253       if not Comes_From_Source (T)
1254         or else (not In_RCI_Declaration (Parent (T))
1255                    and then not In_RT_Declaration)
1256       then
1257          return;
1258       end if;
1259
1260       --  An access definition in the private part of a Remote Types package
1261       --  may be legal if it has user-defined Read and Write attributes. This
1262       --  will be checked at the end of the package spec processing.
1263
1264       if In_RT_Declaration and then In_Private_Part (Scope (T)) then
1265          return;
1266       end if;
1267
1268       --  Check RCI or RT unit type declaration. It may not contain
1269       --  the declaration of an access-to-object type unless it is a
1270       --  general access type that designates a class-wide limited
1271       --  private type. There are also constraints about the primitive
1272       --  subprograms of the class-wide type (RM E.2.3(14)).
1273
1274       if Ekind (T) /= E_General_Access_Type
1275         or else Ekind (Designated_Type (T)) /= E_Class_Wide_Type
1276       then
1277          if In_RCI_Declaration (Parent (T)) then
1278             Error_Msg_N
1279               ("access type in Remote_Call_Interface unit must be " &
1280                "general access", T);
1281          else
1282             Error_Msg_N ("access type in Remote_Types unit must be " &
1283               "general access", T);
1284          end if;
1285          Error_Msg_N ("\to class-wide type", T);
1286          return;
1287       end if;
1288
1289       Direct_Designated_Type := Designated_Type (T);
1290       Desig_Type := Etype (Direct_Designated_Type);
1291
1292       if not Is_Recursively_Limited_Private (Desig_Type) then
1293          Error_Msg_N
1294            ("error in designated type of remote access to class-wide type", T);
1295          Error_Msg_N
1296            ("\must be tagged limited private or private extension of type", T);
1297          return;
1298       end if;
1299
1300       Primitive_Subprograms := Primitive_Operations (Desig_Type);
1301       Subprogram            := First_Elmt (Primitive_Subprograms);
1302
1303       while Subprogram /= No_Elmt loop
1304          Subprogram_Node := Node (Subprogram);
1305
1306          if not Comes_From_Source (Subprogram_Node) then
1307             goto Next_Subprogram;
1308          end if;
1309
1310          Profile := Parameter_Specifications (Parent (Subprogram_Node));
1311
1312          --  Profile must exist, otherwise not primitive operation
1313
1314          Param_Spec := First (Profile);
1315
1316          while Present (Param_Spec) loop
1317
1318             --  Now find out if this parameter is a controlling parameter
1319
1320             Param_Type := Parameter_Type (Param_Spec);
1321
1322             if (Nkind (Param_Type) = N_Access_Definition
1323                   and then Etype (Subtype_Mark (Param_Type)) = Desig_Type)
1324               or else (Nkind (Param_Type) /= N_Access_Definition
1325                         and then Etype (Param_Type) = Desig_Type)
1326             then
1327                --  It is a controlling parameter, so specific checks below
1328                --  do not apply.
1329
1330                null;
1331
1332             elsif
1333               Nkind (Param_Type) = N_Access_Definition
1334             then
1335                --  From RM E.2.2(14), no access parameter other than
1336                --  controlling ones may be used.
1337
1338                Error_Msg_N
1339                  ("non-controlling access parameter", Param_Spec);
1340
1341             elsif
1342               Is_Limited_Type (Etype (Defining_Identifier (Param_Spec)))
1343             then
1344                --  Not a controlling parameter, so type must have Read
1345                --  and Write attributes.
1346
1347                if Nkind (Param_Type) in N_Has_Etype
1348                  and then Nkind (Parent (Etype (Param_Type))) =
1349                           N_Private_Type_Declaration
1350                then
1351                   Param_Type := Etype (Param_Type);
1352
1353                   if No (TSS (Param_Type, TSS_Stream_Read))
1354                        or else
1355                      No (TSS (Param_Type, TSS_Stream_Write))
1356                   then
1357                      Error_Msg_N
1358                        ("limited formal must have Read and Write attributes",
1359                          Param_Spec);
1360                      Explain_Limited_Type
1361                        (Etype (Defining_Identifier (Param_Spec)), Param_Spec);
1362                   end if;
1363                end if;
1364             end if;
1365
1366             --  Check next parameter in this subprogram
1367
1368             Next (Param_Spec);
1369          end loop;
1370
1371          <<Next_Subprogram>>
1372             Next_Elmt (Subprogram);
1373       end loop;
1374
1375       --  Now this is an RCI unit access-to-class-wide-limited-private type
1376       --  declaration. Set the type entity to be Is_Remote_Call_Interface to
1377       --  optimize later checks by avoiding tree traversal to find out if this
1378       --  entity is inside an RCI unit.
1379
1380       Set_Is_Remote_Call_Interface (T);
1381
1382    end Validate_Remote_Access_Object_Type_Declaration;
1383
1384    -----------------------------------------------
1385    -- Validate_Remote_Access_To_Class_Wide_Type --
1386    -----------------------------------------------
1387
1388    procedure Validate_Remote_Access_To_Class_Wide_Type (N : Node_Id) is
1389       K  : constant Node_Kind := Nkind (N);
1390       PK : constant Node_Kind := Nkind (Parent (N));
1391       E  : Entity_Id;
1392
1393    begin
1394       --  This subprogram enforces the checks in (RM E.2.2(8)) for
1395       --  certain uses of class-wide limited private types.
1396
1397       --    Storage_Pool and Storage_Size are not defined for such types
1398       --
1399       --    The expected type of allocator must not not be such a type.
1400
1401       --    The actual parameter of generic instantiation must not
1402       --    be such a type if the formal parameter is of an access type.
1403
1404       --  On entry, there are five cases
1405
1406       --    1. called from sem_attr Analyze_Attribute where attribute
1407       --       name is either Storage_Pool or Storage_Size.
1408
1409       --    2. called from exp_ch4 Expand_N_Allocator
1410
1411       --    3. called from sem_ch12 Analyze_Associations
1412
1413       --    4. called from sem_ch4 Analyze_Explicit_Dereference
1414
1415       --    5. called from sem_res Resolve_Actuals
1416
1417       if K = N_Attribute_Reference then
1418          E := Etype (Prefix (N));
1419
1420          if Is_Remote_Access_To_Class_Wide_Type (E) then
1421             Error_Msg_N ("incorrect attribute of remote operand", N);
1422             return;
1423          end if;
1424
1425       elsif K = N_Allocator then
1426          E := Etype (N);
1427
1428          if Is_Remote_Access_To_Class_Wide_Type (E) then
1429             Error_Msg_N ("incorrect expected remote type of allocator", N);
1430             return;
1431          end if;
1432
1433       elsif K in N_Has_Entity then
1434          E := Entity (N);
1435
1436          if Is_Remote_Access_To_Class_Wide_Type (E) then
1437             Error_Msg_N ("incorrect remote type generic actual", N);
1438             return;
1439          end if;
1440
1441       --  This subprogram also enforces the checks in E.2.2(13).
1442       --  A value of such type must not be dereferenced unless as a
1443       --  controlling operand of a dispatching call.
1444
1445       elsif K = N_Explicit_Dereference
1446         and then (Comes_From_Source (N)
1447                     or else (Nkind (Original_Node (N)) = N_Selected_Component
1448                                and then Comes_From_Source (Original_Node (N))))
1449       then
1450          E := Etype (Prefix (N));
1451
1452          --  If the class-wide type is not a remote one, the restrictions
1453          --  do not apply.
1454
1455          if not Is_Remote_Access_To_Class_Wide_Type (E) then
1456             return;
1457          end if;
1458
1459          --  If we have a true dereference that comes from source and that
1460          --  is a controlling argument for a dispatching call, accept it.
1461
1462          if K = N_Explicit_Dereference
1463            and then Is_Actual_Parameter (N)
1464            and then Is_Controlling_Actual (N)
1465          then
1466             return;
1467          end if;
1468
1469          --  If we are just within a procedure or function call and the
1470          --  dereference has not been analyzed, return because this
1471          --  procedure will be called again from sem_res Resolve_Actuals.
1472
1473          if Is_Actual_Parameter (N)
1474            and then not Analyzed (N)
1475          then
1476             return;
1477          end if;
1478
1479          --  The following is to let the compiler generated tags check
1480          --  pass through without error message. This is a bit kludgy
1481          --  isn't there some better way of making this exclusion ???
1482
1483          if (PK = N_Selected_Component
1484               and then Present (Parent (Parent (N)))
1485               and then Nkind (Parent (Parent (N))) = N_Op_Ne)
1486            or else (PK = N_Unchecked_Type_Conversion
1487                     and then Present (Parent (Parent (N)))
1488                     and then
1489                       Nkind (Parent (Parent (N))) = N_Selected_Component)
1490          then
1491             return;
1492          end if;
1493
1494          --  The following code is needed for expansion of RACW Write
1495          --  attribute, since such expressions can appear in the expanded
1496          --  code.
1497
1498          if not Comes_From_Source (N)
1499            and then
1500            (PK = N_In
1501             or else PK = N_Attribute_Reference
1502             or else
1503               (PK = N_Type_Conversion
1504                and then Present (Parent (N))
1505                and then Present (Parent (Parent (N)))
1506                and then
1507                  Nkind (Parent (Parent (N))) = N_Selected_Component))
1508          then
1509             return;
1510          end if;
1511
1512          Error_Msg_N ("incorrect remote type dereference", N);
1513       end if;
1514    end Validate_Remote_Access_To_Class_Wide_Type;
1515
1516    ------------------------------------------
1517    -- Validate_Remote_Type_Type_Conversion --
1518    ------------------------------------------
1519
1520    procedure Validate_Remote_Type_Type_Conversion (N : Node_Id) is
1521       S : constant Entity_Id := Etype (N);
1522       E : constant Entity_Id := Etype (Expression (N));
1523
1524    begin
1525       --  This test is required in the case where a conversion appears
1526       --  inside a normal package, it does not necessarily have to be
1527       --  inside an RCI, Remote_Types unit (RM E.2.2(9,12)).
1528
1529       if Is_Remote_Access_To_Subprogram_Type (E)
1530         and then not Is_Remote_Access_To_Subprogram_Type (S)
1531       then
1532          Error_Msg_N ("incorrect conversion of remote operand", N);
1533          return;
1534
1535       elsif Is_Remote_Access_To_Class_Wide_Type (E)
1536         and then not Is_Remote_Access_To_Class_Wide_Type (S)
1537       then
1538          Error_Msg_N ("incorrect conversion of remote operand", N);
1539          return;
1540       end if;
1541
1542       --  If a local access type is converted into a RACW type, then the
1543       --  current unit has a pointer that may now be exported to another
1544       --  partition.
1545
1546       if Is_Remote_Access_To_Class_Wide_Type (S)
1547         and then not Is_Remote_Access_To_Class_Wide_Type (E)
1548       then
1549          Set_Has_RACW (Current_Sem_Unit);
1550       end if;
1551    end Validate_Remote_Type_Type_Conversion;
1552
1553    -------------------------------
1554    -- Validate_RT_RAT_Component --
1555    -------------------------------
1556
1557    procedure Validate_RT_RAT_Component (N : Node_Id) is
1558       Spec            : constant Node_Id   := Specification (N);
1559       Name_U          : constant Entity_Id := Defining_Entity (Spec);
1560       Typ             : Entity_Id;
1561       First_Priv_Ent  : constant Entity_Id := First_Private_Entity (Name_U);
1562       In_Visible_Part : Boolean            := True;
1563
1564    begin
1565       if not Is_Remote_Types (Name_U) then
1566          return;
1567       end if;
1568
1569       Typ := First_Entity (Name_U);
1570       while Present (Typ) loop
1571          if In_Visible_Part and then Typ = First_Priv_Ent then
1572             In_Visible_Part := False;
1573          end if;
1574
1575          if Comes_From_Source (Typ)
1576            and then Is_Type (Typ)
1577            and then (In_Visible_Part or else Has_Private_Declaration (Typ))
1578          then
1579             if Missing_Read_Write_Attributes (Typ) then
1580                if Is_Non_Remote_Access_Type (Typ) then
1581                   Error_Msg_N
1582                     ("non-remote access type without user-defined Read " &
1583                      "and Write attributes", Typ);
1584                else
1585                   Error_Msg_N
1586                     ("record type containing a component of a " &
1587                      "non-remote access", Typ);
1588                   Error_Msg_N
1589                     ("\type without Read and Write attributes " &
1590                      "('R'M E.2.2(8))", Typ);
1591                end if;
1592             end if;
1593          end if;
1594
1595          Next_Entity (Typ);
1596       end loop;
1597    end Validate_RT_RAT_Component;
1598
1599    -----------------------------------------
1600    -- Validate_SP_Access_Object_Type_Decl --
1601    -----------------------------------------
1602
1603    procedure Validate_SP_Access_Object_Type_Decl (T : Entity_Id) is
1604       Direct_Designated_Type : Entity_Id;
1605
1606       function Has_Entry_Declarations (E : Entity_Id) return Boolean;
1607       --  Return true if the protected type designated by T has
1608       --  entry declarations.
1609
1610       function Has_Entry_Declarations (E : Entity_Id) return Boolean is
1611          Ety : Entity_Id;
1612
1613       begin
1614          if Nkind (Parent (E)) = N_Protected_Type_Declaration then
1615             Ety := First_Entity (E);
1616             while Present (Ety) loop
1617                if Ekind (Ety) = E_Entry then
1618                   return True;
1619                end if;
1620
1621                Next_Entity (Ety);
1622             end loop;
1623          end if;
1624
1625          return False;
1626       end Has_Entry_Declarations;
1627
1628    --  Start of processing for Validate_SP_Access_Object_Type_Decl
1629
1630    begin
1631       --  We are called from Sem_Ch3.Analyze_Type_Declaration, and the
1632       --  Nkind of the given entity is N_Access_To_Object_Definition.
1633
1634       if not Comes_From_Source (T)
1635         or else not In_Shared_Passive_Unit
1636         or else In_Subprogram_Task_Protected_Unit
1637       then
1638          return;
1639       end if;
1640
1641       --  Check Shared Passive unit. It should not contain the declaration
1642       --  of an access-to-object type whose designated type is a class-wide
1643       --  type, task type or protected type with entry (RM E.2.1(7)).
1644
1645       Direct_Designated_Type := Designated_Type (T);
1646
1647       if Ekind (Direct_Designated_Type) = E_Class_Wide_Type then
1648          Error_Msg_N
1649            ("invalid access-to-class-wide type in shared passive unit", T);
1650          return;
1651
1652       elsif Ekind (Direct_Designated_Type) in Task_Kind then
1653          Error_Msg_N
1654            ("invalid access-to-task type in shared passive unit", T);
1655          return;
1656
1657       elsif Ekind (Direct_Designated_Type) in Protected_Kind
1658         and then Has_Entry_Declarations (Direct_Designated_Type)
1659       then
1660          Error_Msg_N
1661            ("invalid access-to-protected type in shared passive unit", T);
1662          return;
1663       end if;
1664    end Validate_SP_Access_Object_Type_Decl;
1665
1666    ---------------------------------
1667    -- Validate_Static_Object_Name --
1668    ---------------------------------
1669
1670    procedure Validate_Static_Object_Name (N : Node_Id) is
1671       E : Entity_Id;
1672
1673       function Is_Primary (N : Node_Id) return Boolean;
1674       --  Determine whether node is syntactically a primary in an expression.
1675
1676       function Is_Primary (N : Node_Id) return Boolean is
1677          K : constant Node_Kind := Nkind (Parent (N));
1678
1679       begin
1680          case K is
1681
1682             when N_Op | N_In | N_Not_In =>
1683                return True;
1684
1685             when N_Aggregate
1686                | N_Component_Association
1687                | N_Index_Or_Discriminant_Constraint =>
1688                return True;
1689
1690             when N_Attribute_Reference =>
1691                return Attribute_Name (Parent (N)) /= Name_Address
1692                  and then Attribute_Name (Parent (N)) /= Name_Access
1693                  and then Attribute_Name (Parent (N)) /= Name_Unchecked_Access
1694                  and then
1695                    Attribute_Name (Parent (N)) /= Name_Unrestricted_Access;
1696
1697             when N_Indexed_Component =>
1698                return (N /= Prefix (Parent (N))
1699                  or else Is_Primary (Parent (N)));
1700
1701             when N_Qualified_Expression | N_Type_Conversion =>
1702                return Is_Primary (Parent (N));
1703
1704             when N_Assignment_Statement | N_Object_Declaration =>
1705                return (N = Expression (Parent (N)));
1706
1707             when N_Selected_Component =>
1708                return Is_Primary (Parent (N));
1709
1710             when others =>
1711                return False;
1712          end case;
1713       end Is_Primary;
1714
1715    --  Start of processing for Validate_Static_Object_Name
1716
1717    begin
1718       if not In_Preelaborated_Unit
1719         or else not Comes_From_Source (N)
1720         or else In_Subprogram_Or_Concurrent_Unit
1721         or else Ekind (Current_Scope) = E_Block
1722       then
1723          return;
1724
1725       --  Filter out cases where primary is default in a component
1726       --  declaration, discriminant specification, or actual in a record
1727       --  type initialization call.
1728
1729       --  Initialization call of internal types.
1730
1731       elsif Nkind (Parent (N)) = N_Procedure_Call_Statement then
1732
1733          if Present (Parent (Parent (N)))
1734            and then Nkind (Parent (Parent (N))) = N_Freeze_Entity
1735          then
1736             return;
1737          end if;
1738
1739          if Nkind (Name (Parent (N))) = N_Identifier
1740            and then not Comes_From_Source (Entity (Name (Parent (N))))
1741          then
1742             return;
1743          end if;
1744       end if;
1745
1746       --  Error if the name is a primary in an expression. The parent must not
1747       --  be an operator, or a selected component or an indexed component that
1748       --  is itself a primary. Entities that are actuals do not need to be
1749       --  checked, because the call itself will be diagnosed.
1750
1751       if Is_Primary (N)
1752         and then (not Inside_A_Generic
1753                    or else Present (Enclosing_Generic_Body (N)))
1754       then
1755          if Ekind (Entity (N)) = E_Variable then
1756             Flag_Non_Static_Expr
1757               ("non-static object name in preelaborated unit", N);
1758
1759          --  We take the view that a constant defined in another preelaborated
1760          --  unit is preelaborable, even though it may have a private type and
1761          --  thus appear non-static in a client. This must be the intent of
1762          --  the language, but currently is an RM gap.
1763
1764          elsif Ekind (Entity (N)) = E_Constant
1765            and then not Is_Static_Expression (N)
1766          then
1767             E := Entity (N);
1768
1769             if Is_Internal_File_Name (Unit_File_Name (Get_Source_Unit (N)))
1770               and then
1771                 Enclosing_Lib_Unit_Node (N) /= Enclosing_Lib_Unit_Node (E)
1772               and then (Is_Preelaborated (Scope (E))
1773                           or else Is_Pure (Scope (E))
1774                           or else (Present (Renamed_Object (E))
1775                                      and then
1776                                        Is_Entity_Name (Renamed_Object (E))
1777                                      and then
1778                                        (Is_Preelaborated
1779                                          (Scope (Renamed_Object (E)))
1780                                             or else
1781                                               Is_Pure (Scope
1782                                                 (Renamed_Object (E))))))
1783             then
1784                null;
1785             else
1786                Flag_Non_Static_Expr
1787                  ("non-static constant in preelaborated unit", N);
1788             end if;
1789          end if;
1790       end if;
1791    end Validate_Static_Object_Name;
1792
1793 end Sem_Cat;