2006-06-30 Andrew Pinski <pinskia@gmail.com>
[official-gcc.git] / gcc / ada / sem_cat.adb
bloba888216d908f738989c0651984c567926f252662
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 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Exp_Tss; use Exp_Tss;
33 with Fname; use Fname;
34 with Lib; use Lib;
35 with Nlists; use Nlists;
36 with Opt; use Opt;
37 with Sem; use Sem;
38 with Sem_Eval; use Sem_Eval;
39 with Sem_Util; use Sem_Util;
40 with Sinfo; use Sinfo;
41 with Snames; use Snames;
42 with Stand; use Stand;
44 package body Sem_Cat is
46 -----------------------
47 -- Local Subprograms --
48 -----------------------
50 procedure Check_Categorization_Dependencies
51 (Unit_Entity : Entity_Id;
52 Depended_Entity : Entity_Id;
53 Info_Node : Node_Id;
54 Is_Subunit : Boolean);
55 -- This procedure checks that the categorization of a lib unit and that
56 -- of the depended unit satisfy dependency restrictions.
57 -- The depended_entity can be the entity in a with_clause item, in which
58 -- case Info_Node denotes that item. The depended_entity can also be the
59 -- parent unit of a child unit, in which case Info_Node is the declaration
60 -- of the child unit. The error message is posted on Info_Node, and is
61 -- specialized if Is_Subunit is true.
63 procedure Check_Non_Static_Default_Expr
64 (Type_Def : Node_Id;
65 Obj_Decl : Node_Id);
66 -- Iterate through the component list of a record definition, check
67 -- that no component is declared with a nonstatic default value.
68 -- If a nonstatic default exists, report an error on Obj_Decl.
70 -- Iterate through the component list of a record definition, check
71 -- that no component is declared with a non-static default value.
73 function Missing_Read_Write_Attributes (E : Entity_Id) return Boolean;
74 -- Return True if the entity or one of its subcomponent is an access
75 -- type which does not have user-defined Read and Write attribute.
77 function In_RCI_Declaration (N : Node_Id) return Boolean;
78 -- Determines if a declaration is within the visible part of a Remote
79 -- Call Interface compilation unit, for semantic checking purposes only,
80 -- (returns false within an instance and within the package body).
82 function In_RT_Declaration return Boolean;
83 -- Determines if current scope is within a Remote Types compilation unit,
84 -- for semantic checking purposes.
86 function Is_Non_Remote_Access_Type (E : Entity_Id) return Boolean;
87 -- Returns true if the entity is a non-remote access type
89 function In_Shared_Passive_Unit return Boolean;
90 -- Determines if current scope is within a Shared Passive compilation unit
92 function Static_Discriminant_Expr (L : List_Id) return Boolean;
93 -- Iterate through the list of discriminants to check if any of them
94 -- contains non-static default expression, which is a violation in
95 -- a preelaborated library unit.
97 procedure Validate_Remote_Access_Object_Type_Declaration (T : Entity_Id);
98 -- Check validity of declaration if RCI or RT unit. It should not contain
99 -- the declaration of an access-to-object type unless it is a
100 -- general access type that designates a class-wide limited
101 -- private type. There are also constraints about the primitive
102 -- subprograms of the class-wide type. RM E.2 (9, 13, 14)
104 function Is_Recursively_Limited_Private (E : Entity_Id) return Boolean;
105 -- Return True if E is a limited private type, or if E is a private
106 -- extension of a type whose parent verifies this property (hence the
107 -- recursive keyword).
109 ---------------------------------------
110 -- Check_Categorization_Dependencies --
111 ---------------------------------------
113 procedure Check_Categorization_Dependencies
114 (Unit_Entity : Entity_Id;
115 Depended_Entity : Entity_Id;
116 Info_Node : Node_Id;
117 Is_Subunit : Boolean)
119 N : constant Node_Id := Info_Node;
121 -- Here we define an enumeration type to represent categorization
122 -- types, ordered so that a unit with a given categorization can
123 -- only WITH units with lower or equal categorization type.
125 type Categorization is
126 (Pure,
127 Shared_Passive,
128 Remote_Types,
129 Remote_Call_Interface,
130 Preelaborated,
131 Normal);
133 Unit_Category : Categorization;
134 With_Category : Categorization;
136 function Get_Categorization (E : Entity_Id) return Categorization;
137 -- Check categorization flags from entity, and return in the form
138 -- of a corresponding enumeration value.
140 ------------------------
141 -- Get_Categorization --
142 ------------------------
144 function Get_Categorization (E : Entity_Id) return Categorization is
145 begin
146 if Is_Preelaborated (E) then
147 return Preelaborated;
149 -- Ignore Pure specification if set by pragma Pure_Function
151 elsif Is_Pure (E)
152 and then not
153 (Has_Pragma_Pure_Function (E) and not Has_Pragma_Pure (E))
154 then
155 return Pure;
157 elsif Is_Shared_Passive (E) then
158 return Shared_Passive;
160 elsif Is_Remote_Types (E) then
161 return Remote_Types;
163 elsif Is_Remote_Call_Interface (E) then
164 return Remote_Call_Interface;
166 else
167 return Normal;
168 end if;
169 end Get_Categorization;
171 -- Start of processing for Check_Categorization_Dependencies
173 begin
174 -- Intrinsic subprograms are preelaborated, so do not impose any
175 -- categorization dependencies.
177 if Is_Intrinsic_Subprogram (Depended_Entity) then
178 return;
179 end if;
181 Unit_Category := Get_Categorization (Unit_Entity);
182 With_Category := Get_Categorization (Depended_Entity);
184 -- These messages are wanings in GNAT mode, to allow it to be
185 -- judiciously turned off. Otherwise it is a real error.
187 Error_Msg_Warn := GNAT_Mode;
189 -- Check for possible error
191 if With_Category > Unit_Category then
193 -- Special case: Remote_Types and Remote_Call_Interface are allowed
194 -- to be with'ed in package body.
196 if (Unit_Category = Remote_Types
197 or else Unit_Category = Remote_Call_Interface)
198 and then In_Package_Body (Unit_Entity)
199 then
200 null;
202 -- Here we have an error
204 else
205 if Is_Subunit then
206 Error_Msg_NE
207 ("<subunit cannot depend on& " &
208 "(parent has wrong categorization)", N, Depended_Entity);
210 else
211 Error_Msg_NE
212 ("<cannot depend on& " &
213 "(wrong categorization)", N, Depended_Entity);
214 end if;
216 -- Add further explanation for common cases
218 case Unit_Category is
219 when Pure =>
220 Error_Msg_NE
221 ("\<pure unit cannot depend on non-pure unit",
222 N, Depended_Entity);
224 when Preelaborated =>
225 Error_Msg_NE
226 ("\<preelaborated unit cannot depend on " &
227 "non-preelaborated unit",
228 N, Depended_Entity);
230 when others =>
231 null;
232 end case;
233 end if;
234 end if;
235 end Check_Categorization_Dependencies;
237 -----------------------------------
238 -- Check_Non_Static_Default_Expr --
239 -----------------------------------
241 procedure Check_Non_Static_Default_Expr
242 (Type_Def : Node_Id;
243 Obj_Decl : Node_Id)
245 Recdef : Node_Id;
246 Component_Decl : Node_Id;
248 begin
249 if Nkind (Type_Def) = N_Derived_Type_Definition then
250 Recdef := Record_Extension_Part (Type_Def);
252 if No (Recdef) then
253 return;
254 end if;
256 else
257 Recdef := Type_Def;
258 end if;
260 -- Check that component declarations do not involve:
262 -- a. a non-static default expression, where the object is
263 -- declared to be default initialized.
265 -- b. a dynamic Itype (discriminants and constraints)
267 if Null_Present (Recdef) then
268 return;
269 else
270 Component_Decl := First (Component_Items (Component_List (Recdef)));
271 end if;
273 while Present (Component_Decl)
274 and then Nkind (Component_Decl) = N_Component_Declaration
275 loop
276 if Present (Expression (Component_Decl))
277 and then Nkind (Expression (Component_Decl)) /= N_Null
278 and then not Is_Static_Expression (Expression (Component_Decl))
279 then
280 Error_Msg_Sloc := Sloc (Component_Decl);
281 Error_Msg_F
282 ("object in preelaborated unit has non-static default#",
283 Obj_Decl);
285 -- Fix this later ???
287 -- elsif Has_Dynamic_Itype (Component_Decl) then
288 -- Error_Msg_N
289 -- ("dynamic type discriminant," &
290 -- " constraint in preelaborated unit",
291 -- Component_Decl);
292 end if;
294 Next (Component_Decl);
295 end loop;
296 end Check_Non_Static_Default_Expr;
298 ---------------------------
299 -- In_Preelaborated_Unit --
300 ---------------------------
302 function In_Preelaborated_Unit return Boolean is
303 Unit_Entity : constant Entity_Id := Current_Scope;
304 Unit_Kind : constant Node_Kind :=
305 Nkind (Unit (Cunit (Current_Sem_Unit)));
307 begin
308 -- There are no constraints on body of remote_call_interface or
309 -- remote_types packages..
311 return (Unit_Entity /= Standard_Standard)
312 and then (Is_Preelaborated (Unit_Entity)
313 or else Is_Pure (Unit_Entity)
314 or else Is_Shared_Passive (Unit_Entity)
315 or else
316 ((Is_Remote_Types (Unit_Entity)
317 or else Is_Remote_Call_Interface (Unit_Entity))
318 and then Ekind (Unit_Entity) = E_Package
319 and then Unit_Kind /= N_Package_Body
320 and then not In_Package_Body (Unit_Entity)
321 and then not In_Instance));
322 end In_Preelaborated_Unit;
324 ------------------
325 -- In_Pure_Unit --
326 ------------------
328 function In_Pure_Unit return Boolean is
329 begin
330 return Is_Pure (Current_Scope);
331 end In_Pure_Unit;
333 ------------------------
334 -- In_RCI_Declaration --
335 ------------------------
337 function In_RCI_Declaration (N : Node_Id) return Boolean is
338 Unit_Entity : constant Entity_Id := Current_Scope;
339 Unit_Kind : constant Node_Kind :=
340 Nkind (Unit (Cunit (Current_Sem_Unit)));
342 begin
343 -- There are no restrictions on the private part or body
344 -- of an RCI unit.
346 return Is_Remote_Call_Interface (Unit_Entity)
347 and then (Ekind (Unit_Entity) = E_Package
348 or else Ekind (Unit_Entity) = E_Generic_Package)
349 and then Unit_Kind /= N_Package_Body
350 and then List_Containing (N) =
351 Visible_Declarations
352 (Specification (Unit_Declaration_Node (Unit_Entity)))
353 and then not In_Package_Body (Unit_Entity)
354 and then not In_Instance;
355 end In_RCI_Declaration;
357 -----------------------
358 -- In_RT_Declaration --
359 -----------------------
361 function In_RT_Declaration return Boolean is
362 Unit_Entity : constant Entity_Id := Current_Scope;
363 Unit_Kind : constant Node_Kind :=
364 Nkind (Unit (Cunit (Current_Sem_Unit)));
366 begin
367 -- There are no restrictions on the body of a Remote Types unit
369 return Is_Remote_Types (Unit_Entity)
370 and then (Ekind (Unit_Entity) = E_Package
371 or else Ekind (Unit_Entity) = E_Generic_Package)
372 and then Unit_Kind /= N_Package_Body
373 and then not In_Package_Body (Unit_Entity)
374 and then not In_Instance;
375 end In_RT_Declaration;
377 ----------------------------
378 -- In_Shared_Passive_Unit --
379 ----------------------------
381 function In_Shared_Passive_Unit return Boolean is
382 Unit_Entity : constant Entity_Id := Current_Scope;
384 begin
385 return Is_Shared_Passive (Unit_Entity);
386 end In_Shared_Passive_Unit;
388 ---------------------------------------
389 -- In_Subprogram_Task_Protected_Unit --
390 ---------------------------------------
392 function In_Subprogram_Task_Protected_Unit return Boolean is
393 E : Entity_Id;
395 begin
396 -- The following is to verify that a declaration is inside
397 -- subprogram, generic subprogram, task unit, protected unit.
398 -- Used to validate if a lib. unit is Pure. RM 10.2.1(16).
400 -- Use scope chain to check successively outer scopes
402 E := Current_Scope;
403 loop
404 if Is_Subprogram (E)
405 or else
406 Is_Generic_Subprogram (E)
407 or else
408 Is_Concurrent_Type (E)
409 then
410 return True;
412 elsif E = Standard_Standard then
413 return False;
414 end if;
416 E := Scope (E);
417 end loop;
418 end In_Subprogram_Task_Protected_Unit;
420 -------------------------------
421 -- Is_Non_Remote_Access_Type --
422 -------------------------------
424 function Is_Non_Remote_Access_Type (E : Entity_Id) return Boolean is
425 begin
426 return Is_Access_Type (E)
427 and then not Is_Remote_Access_To_Class_Wide_Type (E)
428 and then not Is_Remote_Access_To_Subprogram_Type (E);
429 end Is_Non_Remote_Access_Type;
431 ------------------------------------
432 -- Is_Recursively_Limited_Private --
433 ------------------------------------
435 function Is_Recursively_Limited_Private (E : Entity_Id) return Boolean is
436 P : constant Node_Id := Parent (E);
438 begin
439 if Nkind (P) = N_Private_Type_Declaration
440 and then Is_Limited_Record (E)
441 then
442 return True;
443 elsif Nkind (P) = N_Private_Extension_Declaration then
444 return Is_Recursively_Limited_Private (Etype (E));
445 elsif Nkind (P) = N_Formal_Type_Declaration
446 and then Ekind (E) = E_Record_Type_With_Private
447 and then Is_Generic_Type (E)
448 and then Is_Limited_Record (E)
449 then
450 return True;
451 else
452 return False;
453 end if;
454 end Is_Recursively_Limited_Private;
456 ----------------------------------
457 -- Missing_Read_Write_Attribute --
458 ----------------------------------
460 function Missing_Read_Write_Attributes (E : Entity_Id) return Boolean is
461 Component : Entity_Id;
462 Component_Type : Entity_Id;
464 function Has_Read_Write_Attributes (E : Entity_Id) return Boolean;
465 -- Return True if entity has Read and Write attributes
467 -------------------------------
468 -- Has_Read_Write_Attributes --
469 -------------------------------
471 function Has_Read_Write_Attributes (E : Entity_Id) return Boolean is
472 Rep_Item : Node_Id := First_Rep_Item (E);
473 Read_Attribute : Boolean := False;
474 Write_Attribute : Boolean := False;
476 begin
477 -- We start from the declaration node and then loop until the end
478 -- of the list until we find those two attribute definition clauses.
480 while Present (Rep_Item) loop
481 if Chars (Rep_Item) = Name_Read then
482 Read_Attribute := True;
483 elsif Chars (Rep_Item) = Name_Write then
484 Write_Attribute := True;
485 end if;
487 if Read_Attribute and Write_Attribute then
488 return True;
489 end if;
491 Next_Rep_Item (Rep_Item);
492 end loop;
494 return False;
495 end Has_Read_Write_Attributes;
497 -- Start of processing for Missing_Read_Write_Attributes
499 begin
500 if Has_Read_Write_Attributes (E) then
501 return False;
502 elsif Is_Non_Remote_Access_Type (E) then
503 return True;
504 end if;
506 if Is_Record_Type (E) then
507 Component := First_Entity (E);
508 while Present (Component) loop
509 Component_Type := Etype (Component);
511 if (Is_Non_Remote_Access_Type (Component_Type)
512 or else Is_Record_Type (Component_Type))
513 and then Missing_Read_Write_Attributes (Component_Type)
514 then
515 return True;
516 end if;
518 Next_Entity (Component);
519 end loop;
520 end if;
522 return False;
523 end Missing_Read_Write_Attributes;
525 -------------------------------------
526 -- Set_Categorization_From_Pragmas --
527 -------------------------------------
529 procedure Set_Categorization_From_Pragmas (N : Node_Id) is
530 P : constant Node_Id := Parent (N);
531 S : constant Entity_Id := Current_Scope;
533 procedure Set_Parents (Visibility : Boolean);
534 -- If this is a child instance, the parents are not immediately
535 -- visible during analysis. Make them momentarily visible so that
536 -- the argument of the pragma can be resolved properly, and reset
537 -- afterwards.
539 procedure Set_Parents (Visibility : Boolean) is
540 Par : Entity_Id := Scope (S);
542 begin
543 while Present (Par) and then Par /= Standard_Standard loop
544 Set_Is_Immediately_Visible (Par, Visibility);
545 Par := Scope (Par);
546 end loop;
547 end Set_Parents;
549 begin
550 -- Deal with categorization pragmas in Pragmas of Compilation_Unit.
551 -- The purpose is to set categorization flags before analyzing the
552 -- unit itself, so as to diagnose violations of categorization as
553 -- we process each declaration, even though the pragma appears after
554 -- the unit.
556 if Nkind (P) /= N_Compilation_Unit then
557 return;
558 end if;
560 declare
561 PN : Node_Id := First (Pragmas_After (Aux_Decls_Node (P)));
563 begin
565 if Is_Child_Unit (S)
566 and then Is_Generic_Instance (S)
567 then
568 Set_Parents (True);
569 end if;
571 while Present (PN) loop
573 -- Skip implicit types that may have been introduced by
574 -- previous analysis.
576 if Nkind (PN) = N_Pragma then
578 case Get_Pragma_Id (Chars (PN)) is
579 when Pragma_All_Calls_Remote |
580 Pragma_Preelaborate |
581 Pragma_Pure |
582 Pragma_Remote_Call_Interface |
583 Pragma_Remote_Types |
584 Pragma_Shared_Passive => Analyze (PN);
585 when others => null;
586 end case;
587 end if;
589 Next (PN);
590 end loop;
591 if Is_Child_Unit (S)
592 and then Is_Generic_Instance (S)
593 then
594 Set_Parents (False);
595 end if;
597 end;
598 end Set_Categorization_From_Pragmas;
600 -----------------------------------
601 -- Set_Categorization_From_Scope --
602 -----------------------------------
604 procedure Set_Categorization_From_Scope (E : Entity_Id; Scop : Entity_Id) is
605 Declaration : Node_Id := Empty;
606 Specification : Node_Id := Empty;
608 begin
609 Set_Is_Pure (E,
610 Is_Pure (Scop) and then Is_Library_Level_Entity (E));
612 if not Is_Remote_Call_Interface (E) then
613 if Ekind (E) in Subprogram_Kind then
614 Declaration := Unit_Declaration_Node (E);
616 if False
617 or else Nkind (Declaration) = N_Subprogram_Body
618 or else Nkind (Declaration) = N_Subprogram_Renaming_Declaration
619 then
620 Specification := Corresponding_Spec (Declaration);
621 end if;
622 end if;
624 -- A subprogram body or renaming-as-body is a remote call
625 -- interface if it serves as the completion of a subprogram
626 -- declaration that is a remote call interface.
628 if Nkind (Specification) in N_Entity then
629 Set_Is_Remote_Call_Interface
630 (E, Is_Remote_Call_Interface (Specification));
632 -- A subprogram declaration is a remote call interface when it is
633 -- declared within the visible part of, or declared by, a library
634 -- unit declaration that is a remote call interface.
636 else
637 Set_Is_Remote_Call_Interface
638 (E, Is_Remote_Call_Interface (Scop)
639 and then not (In_Private_Part (Scop)
640 or else In_Package_Body (Scop)));
641 end if;
642 end if;
644 Set_Is_Remote_Types (E, Is_Remote_Types (Scop));
645 end Set_Categorization_From_Scope;
647 ------------------------------
648 -- Static_Discriminant_Expr --
649 ------------------------------
651 -- We need to accomodate a Why_Not_Static call somehow here ???
653 function Static_Discriminant_Expr (L : List_Id) return Boolean is
654 Discriminant_Spec : Node_Id;
656 begin
657 Discriminant_Spec := First (L);
658 while Present (Discriminant_Spec) loop
659 if Present (Expression (Discriminant_Spec))
660 and then not Is_Static_Expression (Expression (Discriminant_Spec))
661 then
662 return False;
663 end if;
665 Next (Discriminant_Spec);
666 end loop;
668 return True;
669 end Static_Discriminant_Expr;
671 --------------------------------------
672 -- Validate_Access_Type_Declaration --
673 --------------------------------------
675 procedure Validate_Access_Type_Declaration (T : Entity_Id; N : Node_Id) is
676 Def : constant Node_Id := Type_Definition (N);
678 begin
679 case Nkind (Def) is
681 -- Access to subprogram case
683 when N_Access_To_Subprogram_Definition =>
685 -- A pure library_item must not contain the declaration of a
686 -- named access type, except within a subprogram, generic
687 -- subprogram, task unit, or protected unit (RM 10.2.1(16)).
689 -- This test is skipped in Ada 2005 (see AI-366)
691 if Ada_Version < Ada_05
692 and then Comes_From_Source (T)
693 and then In_Pure_Unit
694 and then not In_Subprogram_Task_Protected_Unit
695 then
696 Error_Msg_N ("named access type not allowed in pure unit", T);
697 end if;
699 -- Access to object case
701 when N_Access_To_Object_Definition =>
702 if Comes_From_Source (T)
703 and then In_Pure_Unit
704 and then not In_Subprogram_Task_Protected_Unit
705 then
706 -- We can't give the message yet, since the type is not frozen
707 -- and in Ada 2005 mode, access types are allowed in pure units
708 -- if the type has no storage pool (see AI-366). So we set a
709 -- flag which will be checked at freeze time.
711 Set_Is_Pure_Unit_Access_Type (T);
712 end if;
714 -- Check for RCI or RT unit type declaration. It should not
715 -- contain the declaration of an access-to-object type unless it
716 -- is a general access type that designates a class-wide limited
717 -- private type. There are also constraints about the primitive
718 -- subprograms of the class-wide type.
720 Validate_Remote_Access_Object_Type_Declaration (T);
722 -- Check for shared passive unit type declaration. It should
723 -- not contain the declaration of access to class wide type,
724 -- access to task type and access to protected type with entry.
726 Validate_SP_Access_Object_Type_Decl (T);
728 when others =>
729 null;
730 end case;
732 -- Set categorization flag from package on entity as well, to allow
733 -- easy checks later on for required validations of RCI or RT units.
734 -- This is only done for entities that are in the original source.
736 if Comes_From_Source (T)
737 and then not (In_Package_Body (Scope (T))
738 or else In_Private_Part (Scope (T)))
739 then
740 Set_Is_Remote_Call_Interface
741 (T, Is_Remote_Call_Interface (Scope (T)));
742 Set_Is_Remote_Types
743 (T, Is_Remote_Types (Scope (T)));
744 end if;
745 end Validate_Access_Type_Declaration;
747 ----------------------------
748 -- Validate_Ancestor_Part --
749 ----------------------------
751 procedure Validate_Ancestor_Part (N : Node_Id) is
752 A : constant Node_Id := Ancestor_Part (N);
753 T : constant Entity_Id := Entity (A);
755 begin
756 if In_Preelaborated_Unit
757 and then not In_Subprogram_Or_Concurrent_Unit
758 and then (not Inside_A_Generic
759 or else Present (Enclosing_Generic_Body (N)))
760 then
761 -- We relax the restriction of 10.2.1(9) within GNAT
762 -- units to allow packages such as Ada.Strings.Unbounded
763 -- to be implemented (i.p., Null_Unbounded_String).
764 -- (There are ACVC tests that check that the restriction
765 -- is enforced, but note that AI-161, once approved,
766 -- will relax the restriction prohibiting default-
767 -- initialized objects of private and controlled
768 -- types.)
770 if Is_Private_Type (T)
771 and then not Is_Internal_File_Name
772 (Unit_File_Name (Get_Source_Unit (N)))
773 then
774 Error_Msg_N
775 ("private ancestor type not allowed in preelaborated unit", A);
777 elsif Is_Record_Type (T) then
778 if Nkind (Parent (T)) = N_Full_Type_Declaration then
779 Check_Non_Static_Default_Expr
780 (Type_Definition (Parent (T)), A);
781 end if;
782 end if;
783 end if;
784 end Validate_Ancestor_Part;
786 ----------------------------------------
787 -- Validate_Categorization_Dependency --
788 ----------------------------------------
790 procedure Validate_Categorization_Dependency
791 (N : Node_Id;
792 E : Entity_Id)
794 K : constant Node_Kind := Nkind (N);
795 P : Node_Id := Parent (N);
796 U : Entity_Id := E;
797 Is_Subunit : constant Boolean := Nkind (P) = N_Subunit;
799 begin
800 -- Only validate library units and subunits. For subunits, checks
801 -- concerning withed units apply to the parent compilation unit.
803 if Is_Subunit then
804 P := Parent (P);
805 U := Scope (E);
807 while Present (U)
808 and then not Is_Compilation_Unit (U)
809 and then not Is_Child_Unit (U)
810 loop
811 U := Scope (U);
812 end loop;
814 end if;
816 if Nkind (P) /= N_Compilation_Unit then
817 return;
818 end if;
820 -- Body of RCI unit does not need validation
822 if Is_Remote_Call_Interface (E)
823 and then (Nkind (N) = N_Package_Body
824 or else Nkind (N) = N_Subprogram_Body)
825 then
826 return;
827 end if;
829 -- Ada 2005 (AI-50217): Process explicit non-limited with_clauses
831 declare
832 Item : Node_Id;
833 Entity_Of_Withed : Entity_Id;
835 begin
836 Item := First (Context_Items (P));
838 while Present (Item) loop
839 if Nkind (Item) = N_With_Clause
840 and then not (Implicit_With (Item)
841 or else Limited_Present (Item))
842 then
843 Entity_Of_Withed := Entity (Name (Item));
844 Check_Categorization_Dependencies
845 (U, Entity_Of_Withed, Item, Is_Subunit);
846 end if;
848 Next (Item);
849 end loop;
850 end;
852 -- Child depends on parent; therefore parent should also be categorized
853 -- and satify the dependency hierarchy.
855 -- Check if N is a child spec
857 if (K in N_Generic_Declaration or else
858 K in N_Generic_Instantiation or else
859 K in N_Generic_Renaming_Declaration or else
860 K = N_Package_Declaration or else
861 K = N_Package_Renaming_Declaration or else
862 K = N_Subprogram_Declaration or else
863 K = N_Subprogram_Renaming_Declaration)
864 and then Present (Parent_Spec (N))
865 then
866 Check_Categorization_Dependencies (E, Scope (E), N, False);
868 -- Verify that public child of an RCI library unit must also be an
869 -- RCI library unit (RM E.2.3(15)).
871 if Is_Remote_Call_Interface (Scope (E))
872 and then not Private_Present (P)
873 and then not Is_Remote_Call_Interface (E)
874 then
875 Error_Msg_N ("public child of rci unit must also be rci unit", N);
876 end if;
877 end if;
878 end Validate_Categorization_Dependency;
880 --------------------------------
881 -- Validate_Controlled_Object --
882 --------------------------------
884 procedure Validate_Controlled_Object (E : Entity_Id) is
885 begin
886 -- For now, never apply this check for internal GNAT units, since we
887 -- have a number of cases in the library where we are stuck with objects
888 -- of this type, and the RM requires Preelaborate.
890 -- For similar reasons, we only do this check for source entities, since
891 -- we generate entities of this type in some situations.
893 -- Note that the 10.2.1(9) restrictions are not relevant to us anyway.
894 -- We have to enforce them for RM compatibility, but we have no trouble
895 -- accepting these objects and doing the right thing. Note that there is
896 -- no requirement that Preelaborate not actually generate any code!
898 if In_Preelaborated_Unit
899 and then not Debug_Flag_PP
900 and then Comes_From_Source (E)
901 and then not
902 Is_Internal_File_Name (Unit_File_Name (Get_Source_Unit (E)))
903 and then (not Inside_A_Generic
904 or else Present (Enclosing_Generic_Body (E)))
905 and then not Is_Protected_Type (Etype (E))
906 then
907 Error_Msg_N
908 ("library level controlled object not allowed in " &
909 "preelaborated unit", E);
910 end if;
911 end Validate_Controlled_Object;
913 --------------------------------------
914 -- Validate_Null_Statement_Sequence --
915 --------------------------------------
917 procedure Validate_Null_Statement_Sequence (N : Node_Id) is
918 Item : Node_Id;
920 begin
921 if In_Preelaborated_Unit then
922 Item := First (Statements (Handled_Statement_Sequence (N)));
924 while Present (Item) loop
925 if Nkind (Item) /= N_Label
926 and then Nkind (Item) /= N_Null_Statement
927 then
928 -- In GNAT mode, this is a warning, allowing the run-time
929 -- to judiciously bypass this error condition.
931 Error_Msg_Warn := GNAT_Mode;
932 Error_Msg_N
933 ("<statements not allowed in preelaborated unit", Item);
935 exit;
936 end if;
938 Next (Item);
939 end loop;
940 end if;
941 end Validate_Null_Statement_Sequence;
943 ---------------------------------
944 -- Validate_Object_Declaration --
945 ---------------------------------
947 procedure Validate_Object_Declaration (N : Node_Id) is
948 Id : constant Entity_Id := Defining_Identifier (N);
949 E : constant Node_Id := Expression (N);
950 Odf : constant Node_Id := Object_Definition (N);
951 T : constant Entity_Id := Etype (Id);
953 begin
954 -- Verify that any access to subprogram object does not have in its
955 -- subprogram profile access type parameters or limited parameters
956 -- without Read and Write attributes (E.2.3(13)).
958 Validate_RCI_Subprogram_Declaration (N);
960 -- Check that if we are in preelaborated elaboration code, then we
961 -- do not have an instance of a default initialized private, task or
962 -- protected object declaration which would violate (RM 10.2.1(9)).
963 -- Note that constants are never default initialized (and the test
964 -- below also filters out deferred constants). A variable is default
965 -- initialized if it does *not* have an initialization expression.
967 -- Filter out cases that are not declaration of a variable from source
969 if Nkind (N) /= N_Object_Declaration
970 or else Constant_Present (N)
971 or else not Comes_From_Source (Id)
972 then
973 return;
974 end if;
976 -- Exclude generic specs from the checks (this will get rechecked
977 -- on instantiations).
979 if Inside_A_Generic
980 and then No (Enclosing_Generic_Body (Id))
981 then
982 return;
983 end if;
985 -- Required checks for declaration that is in a preelaborated
986 -- package and is not within some subprogram.
988 if In_Preelaborated_Unit
989 and then not In_Subprogram_Or_Concurrent_Unit
990 then
991 -- Check for default initialized variable case. Note that in
992 -- accordance with (RM B.1(24)) imported objects are not
993 -- subject to default initialization.
995 if No (E) and then not Is_Imported (Id) then
996 declare
997 Ent : Entity_Id := T;
999 begin
1000 -- An array whose component type is a record with nonstatic
1001 -- default expressions is a violation, so we get the array's
1002 -- component type.
1004 if Is_Array_Type (Ent) then
1005 declare
1006 Comp_Type : Entity_Id := Component_Type (Ent);
1008 begin
1009 while Is_Array_Type (Comp_Type) loop
1010 Comp_Type := Component_Type (Comp_Type);
1011 end loop;
1013 Ent := Comp_Type;
1014 end;
1015 end if;
1017 -- Object decl. that is of record type and has no default expr.
1018 -- should check if there is any non-static default expression
1019 -- in component decl. of the record type decl.
1021 if Is_Record_Type (Ent) then
1022 if Nkind (Parent (Ent)) = N_Full_Type_Declaration then
1023 Check_Non_Static_Default_Expr
1024 (Type_Definition (Parent (Ent)), N);
1026 elsif Nkind (Odf) = N_Subtype_Indication
1027 and then not Is_Array_Type (T)
1028 and then not Is_Private_Type (T)
1029 then
1030 Check_Non_Static_Default_Expr (Type_Definition
1031 (Parent (Entity (Subtype_Mark (Odf)))), N);
1032 end if;
1033 end if;
1035 -- We relax the restriction of 10.2.1(9) within GNAT
1036 -- units. (There are ACVC tests that check that the
1037 -- restriction is enforced, but note that AI-161,
1038 -- once approved, will relax the restriction prohibiting
1039 -- default-initialized objects of private types, and
1040 -- will recommend a pragma for marking private types.)
1042 if (Is_Private_Type (Ent)
1043 or else Depends_On_Private (Ent))
1044 and then not Is_Internal_File_Name
1045 (Unit_File_Name (Get_Source_Unit (N)))
1046 then
1047 Error_Msg_N
1048 ("private object not allowed in preelaborated unit", N);
1049 return;
1051 -- Access to Task or Protected type
1053 elsif Is_Entity_Name (Odf)
1054 and then Present (Etype (Odf))
1055 and then Is_Access_Type (Etype (Odf))
1056 then
1057 Ent := Designated_Type (Etype (Odf));
1059 elsif Is_Entity_Name (Odf) then
1060 Ent := Entity (Odf);
1062 elsif Nkind (Odf) = N_Subtype_Indication then
1063 Ent := Etype (Subtype_Mark (Odf));
1065 elsif
1066 Nkind (Odf) = N_Constrained_Array_Definition
1067 then
1068 Ent := Component_Type (T);
1070 -- else
1071 -- return;
1072 end if;
1074 if Is_Task_Type (Ent)
1075 or else (Is_Protected_Type (Ent) and then Has_Entries (Ent))
1076 then
1077 Error_Msg_N
1078 ("concurrent object not allowed in preelaborated unit",
1080 return;
1081 end if;
1082 end;
1083 end if;
1085 -- Non-static discriminant not allowed in preelaborayted unit
1087 if Is_Record_Type (Etype (Id)) then
1088 declare
1089 ET : constant Entity_Id := Etype (Id);
1090 EE : constant Entity_Id := Etype (Etype (Id));
1091 PEE : Node_Id;
1093 begin
1094 if Has_Discriminants (ET)
1095 and then Present (EE)
1096 then
1097 PEE := Parent (EE);
1099 if Nkind (PEE) = N_Full_Type_Declaration
1100 and then not Static_Discriminant_Expr
1101 (Discriminant_Specifications (PEE))
1102 then
1103 Error_Msg_N
1104 ("non-static discriminant in preelaborated unit",
1105 PEE);
1106 end if;
1107 end if;
1108 end;
1109 end if;
1110 end if;
1112 -- A pure library_item must not contain the declaration of any
1113 -- variable except within a subprogram, generic subprogram, task
1114 -- unit or protected unit (RM 10.2.1(16)).
1116 if In_Pure_Unit
1117 and then not In_Subprogram_Task_Protected_Unit
1118 then
1119 Error_Msg_N ("declaration of variable not allowed in pure unit", N);
1121 -- The visible part of an RCI library unit must not contain the
1122 -- declaration of a variable (RM E.1.3(9))
1124 elsif In_RCI_Declaration (N) then
1125 Error_Msg_N ("declaration of variable not allowed in rci unit", N);
1127 -- The visible part of a Shared Passive library unit must not contain
1128 -- the declaration of a variable (RM E.2.2(7))
1130 elsif In_RT_Declaration then
1131 Error_Msg_N
1132 ("variable declaration not allowed in remote types unit", N);
1133 end if;
1135 end Validate_Object_Declaration;
1137 -------------------------------
1138 -- Validate_RCI_Declarations --
1139 -------------------------------
1141 procedure Validate_RCI_Declarations (P : Entity_Id) is
1142 E : Entity_Id;
1144 begin
1145 E := First_Entity (P);
1146 while Present (E) loop
1147 if Comes_From_Source (E) then
1148 if Is_Limited_Type (E) then
1149 Error_Msg_N
1150 ("Limited type not allowed in rci unit", Parent (E));
1151 Explain_Limited_Type (E, Parent (E));
1153 elsif Ekind (E) = E_Generic_Function
1154 or else Ekind (E) = E_Generic_Package
1155 or else Ekind (E) = E_Generic_Procedure
1156 then
1157 Error_Msg_N ("generic declaration not allowed in rci unit",
1158 Parent (E));
1160 elsif (Ekind (E) = E_Function
1161 or else Ekind (E) = E_Procedure)
1162 and then Has_Pragma_Inline (E)
1163 then
1164 Error_Msg_N
1165 ("inlined subprogram not allowed in rci unit", Parent (E));
1167 -- Inner packages that are renamings need not be checked.
1168 -- Generic RCI packages are subject to the checks, but
1169 -- entities that come from formal packages are not part of the
1170 -- visible declarations of the package and are not checked.
1172 elsif Ekind (E) = E_Package then
1173 if Present (Renamed_Entity (E)) then
1174 null;
1176 elsif Ekind (P) /= E_Generic_Package
1177 or else List_Containing (Unit_Declaration_Node (E)) /=
1178 Generic_Formal_Declarations
1179 (Unit_Declaration_Node (P))
1180 then
1181 Validate_RCI_Declarations (E);
1182 end if;
1183 end if;
1184 end if;
1186 Next_Entity (E);
1187 end loop;
1188 end Validate_RCI_Declarations;
1190 -----------------------------------------
1191 -- Validate_RCI_Subprogram_Declaration --
1192 -----------------------------------------
1194 procedure Validate_RCI_Subprogram_Declaration (N : Node_Id) is
1195 K : constant Node_Kind := Nkind (N);
1196 Profile : List_Id;
1197 Id : Node_Id;
1198 Param_Spec : Node_Id;
1199 Param_Type : Entity_Id;
1200 Base_Param_Type : Entity_Id;
1201 Base_Under_Type : Entity_Id;
1202 Type_Decl : Node_Id;
1203 Error_Node : Node_Id := N;
1205 begin
1206 -- There are two possible cases in which this procedure is called:
1208 -- 1. called from Analyze_Subprogram_Declaration.
1209 -- 2. called from Validate_Object_Declaration (access to subprogram).
1211 if not In_RCI_Declaration (N) then
1212 return;
1213 end if;
1215 if K = N_Subprogram_Declaration then
1216 Profile := Parameter_Specifications (Specification (N));
1218 else pragma Assert (K = N_Object_Declaration);
1219 Id := Defining_Identifier (N);
1221 if Nkind (Id) = N_Defining_Identifier
1222 and then Nkind (Parent (Etype (Id))) = N_Full_Type_Declaration
1223 and then Ekind (Etype (Id)) = E_Access_Subprogram_Type
1224 then
1225 Profile :=
1226 Parameter_Specifications (Type_Definition (Parent (Etype (Id))));
1227 else
1228 return;
1229 end if;
1230 end if;
1232 -- Iterate through the parameter specification list, checking that
1233 -- no access parameter and no limited type parameter in the list.
1234 -- RM E.2.3 (14)
1236 if Present (Profile) then
1237 Param_Spec := First (Profile);
1239 while Present (Param_Spec) loop
1240 Param_Type := Etype (Defining_Identifier (Param_Spec));
1241 Type_Decl := Parent (Param_Type);
1243 if Ekind (Param_Type) = E_Anonymous_Access_Type then
1245 if K = N_Subprogram_Declaration then
1246 Error_Node := Param_Spec;
1247 end if;
1249 -- Report error only if declaration is in source program
1251 if Comes_From_Source
1252 (Defining_Entity (Specification (N)))
1253 then
1254 Error_Msg_N
1255 ("subprogram in rci unit cannot have access parameter",
1256 Error_Node);
1257 end if;
1259 -- For limited private type parameter, we check only the
1260 -- private declaration and ignore full type declaration,
1261 -- unless this is the only declaration for the type, eg.
1262 -- as a limited record.
1264 elsif Is_Limited_Type (Param_Type)
1265 and then (Nkind (Type_Decl) = N_Private_Type_Declaration
1266 or else
1267 (Nkind (Type_Decl) = N_Full_Type_Declaration
1268 and then not (Has_Private_Declaration (Param_Type))
1269 and then Comes_From_Source (N)))
1270 then
1271 -- A limited parameter is legal only if user-specified Read and
1272 -- Write attributes exist for it. Second part of RM E.2.3 (14).
1274 if No (Full_View (Param_Type))
1275 and then Ekind (Param_Type) /= E_Record_Type
1276 then
1277 -- Type does not have completion yet, so if declared in in
1278 -- the current RCI scope it is illegal, and will be flagged
1279 -- subsequently.
1281 return;
1282 end if;
1284 -- In Ada 95 the rules permit using a limited type that has
1285 -- user-specified Read and Write attributes that are specified
1286 -- in the private part of the package, whereas Ada 2005
1287 -- (AI-240) revises this to require the attributes to be
1288 -- "available" (implying that the attribute clauses must be
1289 -- visible to the RCI client). The Ada 95 rules violate the
1290 -- contract model for privacy, but we support both semantics
1291 -- for now for compatibility (note that ACATS test BXE2009
1292 -- checks a case that conforms to the Ada 95 rules but is
1293 -- illegal in Ada 2005).
1295 Base_Param_Type := Base_Type (Param_Type);
1296 Base_Under_Type := Base_Type (Underlying_Type
1297 (Base_Param_Type));
1299 if (Ada_Version < Ada_05
1300 and then
1301 (No (TSS (Base_Param_Type, TSS_Stream_Read))
1302 or else
1303 No (TSS (Base_Param_Type, TSS_Stream_Write)))
1304 and then
1305 (No (TSS (Base_Under_Type, TSS_Stream_Read))
1306 or else
1307 No (TSS (Base_Under_Type, TSS_Stream_Write))))
1308 or else
1309 (Ada_Version >= Ada_05
1310 and then
1311 (No (TSS (Base_Param_Type, TSS_Stream_Read))
1312 or else
1313 No (TSS (Base_Param_Type, TSS_Stream_Write))
1314 or else
1315 Is_Hidden (TSS (Base_Param_Type, TSS_Stream_Read))
1316 or else
1317 Is_Hidden (TSS (Base_Param_Type, TSS_Stream_Write))))
1318 then
1319 if K = N_Subprogram_Declaration then
1320 Error_Node := Param_Spec;
1321 end if;
1323 if Ada_Version >= Ada_05 then
1324 Error_Msg_N
1325 ("limited parameter in rci unit "
1326 & "must have visible read/write attributes ",
1327 Error_Node);
1328 else
1329 Error_Msg_N
1330 ("limited parameter in rci unit "
1331 & "must have read/write attributes ",
1332 Error_Node);
1333 end if;
1334 Explain_Limited_Type (Param_Type, Error_Node);
1335 end if;
1336 end if;
1338 Next (Param_Spec);
1339 end loop;
1340 end if;
1341 end Validate_RCI_Subprogram_Declaration;
1343 ----------------------------------------------------
1344 -- Validate_Remote_Access_Object_Type_Declaration --
1345 ----------------------------------------------------
1347 procedure Validate_Remote_Access_Object_Type_Declaration (T : Entity_Id) is
1348 Direct_Designated_Type : Entity_Id;
1349 Desig_Type : Entity_Id;
1350 Primitive_Subprograms : Elist_Id;
1351 Subprogram : Elmt_Id;
1352 Subprogram_Node : Node_Id;
1353 Profile : List_Id;
1354 Param_Spec : Node_Id;
1355 Param_Type : Entity_Id;
1357 begin
1358 -- We are called from Analyze_Type_Declaration, and the Nkind
1359 -- of the given node is N_Access_To_Object_Definition.
1361 if not Comes_From_Source (T)
1362 or else (not In_RCI_Declaration (Parent (T))
1363 and then not In_RT_Declaration)
1364 then
1365 return;
1366 end if;
1368 -- An access definition in the private part of a Remote Types package
1369 -- may be legal if it has user-defined Read and Write attributes. This
1370 -- will be checked at the end of the package spec processing.
1372 if In_RT_Declaration and then In_Private_Part (Scope (T)) then
1373 return;
1374 end if;
1376 -- Check RCI or RT unit type declaration. It may not contain
1377 -- the declaration of an access-to-object type unless it is a
1378 -- general access type that designates a class-wide limited
1379 -- private type. There are also constraints about the primitive
1380 -- subprograms of the class-wide type (RM E.2.3(14)).
1382 if Ekind (T) /= E_General_Access_Type
1383 or else Ekind (Designated_Type (T)) /= E_Class_Wide_Type
1384 then
1385 if In_RCI_Declaration (Parent (T)) then
1386 Error_Msg_N
1387 ("access type in Remote_Call_Interface unit must be " &
1388 "general access", T);
1389 else
1390 Error_Msg_N ("access type in Remote_Types unit must be " &
1391 "general access", T);
1392 end if;
1393 Error_Msg_N ("\to class-wide type", T);
1394 return;
1395 end if;
1397 Direct_Designated_Type := Designated_Type (T);
1398 Desig_Type := Etype (Direct_Designated_Type);
1400 if not Is_Recursively_Limited_Private (Desig_Type) then
1401 Error_Msg_N
1402 ("error in designated type of remote access to class-wide type", T);
1403 Error_Msg_N
1404 ("\must be tagged limited private or private extension of type", T);
1405 return;
1406 end if;
1408 Primitive_Subprograms := Primitive_Operations (Desig_Type);
1409 Subprogram := First_Elmt (Primitive_Subprograms);
1411 while Subprogram /= No_Elmt loop
1412 Subprogram_Node := Node (Subprogram);
1414 if not Comes_From_Source (Subprogram_Node) then
1415 goto Next_Subprogram;
1416 end if;
1418 Profile := Parameter_Specifications (Parent (Subprogram_Node));
1420 -- Profile must exist, otherwise not primitive operation
1422 Param_Spec := First (Profile);
1423 while Present (Param_Spec) loop
1425 -- Now find out if this parameter is a controlling parameter
1427 Param_Type := Parameter_Type (Param_Spec);
1429 if (Nkind (Param_Type) = N_Access_Definition
1430 and then Etype (Subtype_Mark (Param_Type)) = Desig_Type)
1431 or else (Nkind (Param_Type) /= N_Access_Definition
1432 and then Etype (Param_Type) = Desig_Type)
1433 then
1434 -- It is a controlling parameter, so specific checks below
1435 -- do not apply.
1437 null;
1439 elsif
1440 Nkind (Param_Type) = N_Access_Definition
1441 then
1442 -- From RM E.2.2(14), no access parameter other than
1443 -- controlling ones may be used.
1445 Error_Msg_N
1446 ("non-controlling access parameter", Param_Spec);
1448 elsif
1449 Is_Limited_Type (Etype (Defining_Identifier (Param_Spec)))
1450 then
1451 -- Not a controlling parameter, so type must have Read
1452 -- and Write attributes.
1454 if Nkind (Param_Type) in N_Has_Etype
1455 and then Nkind (Parent (Etype (Param_Type))) =
1456 N_Private_Type_Declaration
1457 then
1458 Param_Type := Etype (Param_Type);
1460 if No (TSS (Param_Type, TSS_Stream_Read))
1461 or else
1462 No (TSS (Param_Type, TSS_Stream_Write))
1463 then
1464 Error_Msg_N
1465 ("limited formal must have Read and Write attributes",
1466 Param_Spec);
1467 Explain_Limited_Type
1468 (Etype (Defining_Identifier (Param_Spec)), Param_Spec);
1469 end if;
1470 end if;
1471 end if;
1473 -- Check next parameter in this subprogram
1475 Next (Param_Spec);
1476 end loop;
1478 <<Next_Subprogram>>
1479 Next_Elmt (Subprogram);
1480 end loop;
1482 -- Now this is an RCI unit access-to-class-wide-limited-private type
1483 -- declaration. Set the type entity to be Is_Remote_Call_Interface to
1484 -- optimize later checks by avoiding tree traversal to find out if this
1485 -- entity is inside an RCI unit.
1487 Set_Is_Remote_Call_Interface (T);
1488 end Validate_Remote_Access_Object_Type_Declaration;
1490 -----------------------------------------------
1491 -- Validate_Remote_Access_To_Class_Wide_Type --
1492 -----------------------------------------------
1494 procedure Validate_Remote_Access_To_Class_Wide_Type (N : Node_Id) is
1495 K : constant Node_Kind := Nkind (N);
1496 PK : constant Node_Kind := Nkind (Parent (N));
1497 E : Entity_Id;
1499 begin
1500 -- This subprogram enforces the checks in (RM E.2.2(8)) for certain uses
1501 -- of class-wide limited private types.
1503 -- Storage_Pool and Storage_Size are not defined for such types
1505 -- The expected type of allocator must not not be such a type.
1507 -- The actual parameter of generic instantiation must not be such a
1508 -- type if the formal parameter is of an access type.
1510 -- On entry, there are five cases
1512 -- 1. called from sem_attr Analyze_Attribute where attribute name is
1513 -- either Storage_Pool or Storage_Size.
1515 -- 2. called from exp_ch4 Expand_N_Allocator
1517 -- 3. called from sem_ch12 Analyze_Associations
1519 -- 4. called from sem_ch4 Analyze_Explicit_Dereference
1521 -- 5. called from sem_res Resolve_Actuals
1523 if K = N_Attribute_Reference then
1524 E := Etype (Prefix (N));
1526 if Is_Remote_Access_To_Class_Wide_Type (E) then
1527 Error_Msg_N ("incorrect attribute of remote operand", N);
1528 return;
1529 end if;
1531 elsif K = N_Allocator then
1532 E := Etype (N);
1534 if Is_Remote_Access_To_Class_Wide_Type (E) then
1535 Error_Msg_N ("incorrect expected remote type of allocator", N);
1536 return;
1537 end if;
1539 elsif K in N_Has_Entity then
1540 E := Entity (N);
1542 if Is_Remote_Access_To_Class_Wide_Type (E) then
1543 Error_Msg_N ("incorrect remote type generic actual", N);
1544 return;
1545 end if;
1547 -- This subprogram also enforces the checks in E.2.2(13). A value of
1548 -- such type must not be dereferenced unless as controlling operand of a
1549 -- dispatching call.
1551 elsif K = N_Explicit_Dereference
1552 and then (Comes_From_Source (N)
1553 or else (Nkind (Original_Node (N)) = N_Selected_Component
1554 and then Comes_From_Source (Original_Node (N))))
1555 then
1556 E := Etype (Prefix (N));
1558 -- If the class-wide type is not a remote one, the restrictions
1559 -- do not apply.
1561 if not Is_Remote_Access_To_Class_Wide_Type (E) then
1562 return;
1563 end if;
1565 -- If we have a true dereference that comes from source and that
1566 -- is a controlling argument for a dispatching call, accept it.
1568 if K = N_Explicit_Dereference
1569 and then Is_Actual_Parameter (N)
1570 and then Is_Controlling_Actual (N)
1571 then
1572 return;
1573 end if;
1575 -- If we are just within a procedure or function call and the
1576 -- dereference has not been analyzed, return because this procedure
1577 -- will be called again from sem_res Resolve_Actuals.
1579 if Is_Actual_Parameter (N)
1580 and then not Analyzed (N)
1581 then
1582 return;
1583 end if;
1585 -- The following is to let the compiler generated tags check pass
1586 -- through without error message. This is a bit kludgy isn't there
1587 -- some better way of making this exclusion ???
1589 if (PK = N_Selected_Component
1590 and then Present (Parent (Parent (N)))
1591 and then Nkind (Parent (Parent (N))) = N_Op_Ne)
1592 or else (PK = N_Unchecked_Type_Conversion
1593 and then Present (Parent (Parent (N)))
1594 and then
1595 Nkind (Parent (Parent (N))) = N_Selected_Component)
1596 then
1597 return;
1598 end if;
1600 -- The following code is needed for expansion of RACW Write
1601 -- attribute, since such expressions can appear in the expanded
1602 -- code.
1604 if not Comes_From_Source (N)
1605 and then
1606 (PK = N_In
1607 or else PK = N_Attribute_Reference
1608 or else
1609 (PK = N_Type_Conversion
1610 and then Present (Parent (N))
1611 and then Present (Parent (Parent (N)))
1612 and then
1613 Nkind (Parent (Parent (N))) = N_Selected_Component))
1614 then
1615 return;
1616 end if;
1618 Error_Msg_N ("incorrect remote type dereference", N);
1619 end if;
1620 end Validate_Remote_Access_To_Class_Wide_Type;
1622 ------------------------------------------
1623 -- Validate_Remote_Type_Type_Conversion --
1624 ------------------------------------------
1626 procedure Validate_Remote_Type_Type_Conversion (N : Node_Id) is
1627 S : constant Entity_Id := Etype (N);
1628 E : constant Entity_Id := Etype (Expression (N));
1630 begin
1631 -- This test is required in the case where a conversion appears inside a
1632 -- normal package, it does not necessarily have to be inside an RCI,
1633 -- Remote_Types unit (RM E.2.2(9,12)).
1635 if Is_Remote_Access_To_Subprogram_Type (E)
1636 and then not Is_Remote_Access_To_Subprogram_Type (S)
1637 then
1638 Error_Msg_N
1639 ("incorrect conversion of remote operand to local type", N);
1640 return;
1642 elsif not Is_Remote_Access_To_Subprogram_Type (E)
1643 and then Is_Remote_Access_To_Subprogram_Type (S)
1644 then
1645 Error_Msg_N
1646 ("incorrect conversion of local operand to remote type", N);
1647 return;
1649 elsif Is_Remote_Access_To_Class_Wide_Type (E)
1650 and then not Is_Remote_Access_To_Class_Wide_Type (S)
1651 then
1652 Error_Msg_N
1653 ("incorrect conversion of remote operand to local type", N);
1654 return;
1655 end if;
1657 -- If a local access type is converted into a RACW type, then the
1658 -- current unit has a pointer that may now be exported to another
1659 -- partition.
1661 if Is_Remote_Access_To_Class_Wide_Type (S)
1662 and then not Is_Remote_Access_To_Class_Wide_Type (E)
1663 then
1664 Set_Has_RACW (Current_Sem_Unit);
1665 end if;
1666 end Validate_Remote_Type_Type_Conversion;
1668 -------------------------------
1669 -- Validate_RT_RAT_Component --
1670 -------------------------------
1672 procedure Validate_RT_RAT_Component (N : Node_Id) is
1673 Spec : constant Node_Id := Specification (N);
1674 Name_U : constant Entity_Id := Defining_Entity (Spec);
1675 Typ : Entity_Id;
1676 First_Priv_Ent : constant Entity_Id := First_Private_Entity (Name_U);
1677 In_Visible_Part : Boolean := True;
1679 begin
1680 if not Is_Remote_Types (Name_U) then
1681 return;
1682 end if;
1684 Typ := First_Entity (Name_U);
1685 while Present (Typ) loop
1686 if In_Visible_Part and then Typ = First_Priv_Ent then
1687 In_Visible_Part := False;
1688 end if;
1690 if Comes_From_Source (Typ)
1691 and then Is_Type (Typ)
1692 and then (In_Visible_Part or else Has_Private_Declaration (Typ))
1693 then
1694 if Missing_Read_Write_Attributes (Typ) then
1695 if Is_Non_Remote_Access_Type (Typ) then
1696 Error_Msg_N
1697 ("non-remote access type without user-defined Read " &
1698 "and Write attributes", Typ);
1699 else
1700 Error_Msg_N
1701 ("record type containing a component of a " &
1702 "non-remote access", Typ);
1703 Error_Msg_N
1704 ("\type without Read and Write attributes " &
1705 "('R'M E.2.2(8))", Typ);
1706 end if;
1707 end if;
1708 end if;
1710 Next_Entity (Typ);
1711 end loop;
1712 end Validate_RT_RAT_Component;
1714 -----------------------------------------
1715 -- Validate_SP_Access_Object_Type_Decl --
1716 -----------------------------------------
1718 procedure Validate_SP_Access_Object_Type_Decl (T : Entity_Id) is
1719 Direct_Designated_Type : Entity_Id;
1721 function Has_Entry_Declarations (E : Entity_Id) return Boolean;
1722 -- Return true if the protected type designated by T has
1723 -- entry declarations.
1725 ----------------------------
1726 -- Has_Entry_Declarations --
1727 ----------------------------
1729 function Has_Entry_Declarations (E : Entity_Id) return Boolean is
1730 Ety : Entity_Id;
1732 begin
1733 if Nkind (Parent (E)) = N_Protected_Type_Declaration then
1734 Ety := First_Entity (E);
1735 while Present (Ety) loop
1736 if Ekind (Ety) = E_Entry then
1737 return True;
1738 end if;
1740 Next_Entity (Ety);
1741 end loop;
1742 end if;
1744 return False;
1745 end Has_Entry_Declarations;
1747 -- Start of processing for Validate_SP_Access_Object_Type_Decl
1749 begin
1750 -- We are called from Sem_Ch3.Analyze_Type_Declaration, and the
1751 -- Nkind of the given entity is N_Access_To_Object_Definition.
1753 if not Comes_From_Source (T)
1754 or else not In_Shared_Passive_Unit
1755 or else In_Subprogram_Task_Protected_Unit
1756 then
1757 return;
1758 end if;
1760 -- Check Shared Passive unit. It should not contain the declaration
1761 -- of an access-to-object type whose designated type is a class-wide
1762 -- type, task type or protected type with entry (RM E.2.1(7)).
1764 Direct_Designated_Type := Designated_Type (T);
1766 if Ekind (Direct_Designated_Type) = E_Class_Wide_Type then
1767 Error_Msg_N
1768 ("invalid access-to-class-wide type in shared passive unit", T);
1769 return;
1771 elsif Ekind (Direct_Designated_Type) in Task_Kind then
1772 Error_Msg_N
1773 ("invalid access-to-task type in shared passive unit", T);
1774 return;
1776 elsif Ekind (Direct_Designated_Type) in Protected_Kind
1777 and then Has_Entry_Declarations (Direct_Designated_Type)
1778 then
1779 Error_Msg_N
1780 ("invalid access-to-protected type in shared passive unit", T);
1781 return;
1782 end if;
1783 end Validate_SP_Access_Object_Type_Decl;
1785 ---------------------------------
1786 -- Validate_Static_Object_Name --
1787 ---------------------------------
1789 procedure Validate_Static_Object_Name (N : Node_Id) is
1790 E : Entity_Id;
1792 function Is_Primary (N : Node_Id) return Boolean;
1793 -- Determine whether node is syntactically a primary in an expression
1795 ----------------
1796 -- Is_Primary --
1797 ----------------
1799 function Is_Primary (N : Node_Id) return Boolean is
1800 K : constant Node_Kind := Nkind (Parent (N));
1802 begin
1803 case K is
1804 when N_Op | N_In | N_Not_In =>
1805 return True;
1807 when N_Aggregate
1808 | N_Component_Association
1809 | N_Index_Or_Discriminant_Constraint =>
1810 return True;
1812 when N_Attribute_Reference =>
1813 return Attribute_Name (Parent (N)) /= Name_Address
1814 and then Attribute_Name (Parent (N)) /= Name_Access
1815 and then Attribute_Name (Parent (N)) /= Name_Unchecked_Access
1816 and then
1817 Attribute_Name (Parent (N)) /= Name_Unrestricted_Access;
1819 when N_Indexed_Component =>
1820 return (N /= Prefix (Parent (N))
1821 or else Is_Primary (Parent (N)));
1823 when N_Qualified_Expression | N_Type_Conversion =>
1824 return Is_Primary (Parent (N));
1826 when N_Assignment_Statement | N_Object_Declaration =>
1827 return (N = Expression (Parent (N)));
1829 when N_Selected_Component =>
1830 return Is_Primary (Parent (N));
1832 when others =>
1833 return False;
1834 end case;
1835 end Is_Primary;
1837 -- Start of processing for Validate_Static_Object_Name
1839 begin
1840 if not In_Preelaborated_Unit
1841 or else not Comes_From_Source (N)
1842 or else In_Subprogram_Or_Concurrent_Unit
1843 or else Ekind (Current_Scope) = E_Block
1844 then
1845 return;
1847 -- Filter out cases where primary is default in a component declaration,
1848 -- discriminant specification, or actual in a record type initialization
1849 -- call.
1851 -- Initialization call of internal types
1853 elsif Nkind (Parent (N)) = N_Procedure_Call_Statement then
1855 if Present (Parent (Parent (N)))
1856 and then Nkind (Parent (Parent (N))) = N_Freeze_Entity
1857 then
1858 return;
1859 end if;
1861 if Nkind (Name (Parent (N))) = N_Identifier
1862 and then not Comes_From_Source (Entity (Name (Parent (N))))
1863 then
1864 return;
1865 end if;
1866 end if;
1868 -- Error if the name is a primary in an expression. The parent must not
1869 -- be an operator, or a selected component or an indexed component that
1870 -- is itself a primary. Entities that are actuals do not need to be
1871 -- checked, because the call itself will be diagnosed.
1873 if Is_Primary (N)
1874 and then (not Inside_A_Generic
1875 or else Present (Enclosing_Generic_Body (N)))
1876 then
1877 if Ekind (Entity (N)) = E_Variable then
1878 Flag_Non_Static_Expr
1879 ("non-static object name in preelaborated unit", N);
1881 -- We take the view that a constant defined in another preelaborated
1882 -- unit is preelaborable, even though it may have a private type and
1883 -- thus appear non-static in a client. This must be the intent of
1884 -- the language, but currently is an RM gap ???
1886 elsif Ekind (Entity (N)) = E_Constant
1887 and then not Is_Static_Expression (N)
1888 then
1889 E := Entity (N);
1891 if Is_Internal_File_Name (Unit_File_Name (Get_Source_Unit (N)))
1892 and then
1893 Enclosing_Lib_Unit_Node (N) /= Enclosing_Lib_Unit_Node (E)
1894 and then (Is_Preelaborated (Scope (E))
1895 or else Is_Pure (Scope (E))
1896 or else (Present (Renamed_Object (E))
1897 and then
1898 Is_Entity_Name (Renamed_Object (E))
1899 and then
1900 (Is_Preelaborated
1901 (Scope (Renamed_Object (E)))
1902 or else
1903 Is_Pure (Scope
1904 (Renamed_Object (E))))))
1905 then
1906 null;
1908 -- This is the error case
1910 else
1911 -- In GNAT mode, this is just a warning, to allow it to be
1912 -- judiciously turned off. Otherwise it is a real error.
1914 if GNAT_Mode then
1915 Error_Msg_N
1916 ("?non-static constant in preelaborated unit", N);
1917 else
1918 Flag_Non_Static_Expr
1919 ("non-static constant in preelaborated unit", N);
1920 end if;
1922 end if;
1923 end if;
1924 end if;
1925 end Validate_Static_Object_Name;
1927 end Sem_Cat;