OSDN Git Service

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