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