1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2013, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Exp_Disp; use Exp_Disp;
34 with Exp_Tss; use Exp_Tss;
35 with Exp_Util; use Exp_Util;
37 with Lib.Xref; use Lib.Xref;
38 with Namet; use Namet;
39 with Nlists; use Nlists;
40 with Nmake; use Nmake;
42 with Restrict; use Restrict;
43 with Rident; use Rident;
44 with Rtsfind; use Rtsfind;
46 with Sem_Aux; use Sem_Aux;
47 with Sem_Ch3; use Sem_Ch3;
48 with Sem_Ch6; use Sem_Ch6;
49 with Sem_Ch8; use Sem_Ch8;
50 with Sem_Ch9; use Sem_Ch9;
51 with Sem_Dim; use Sem_Dim;
52 with Sem_Disp; use Sem_Disp;
53 with Sem_Eval; use Sem_Eval;
54 with Sem_Prag; use Sem_Prag;
55 with Sem_Res; use Sem_Res;
56 with Sem_Type; use Sem_Type;
57 with Sem_Util; use Sem_Util;
58 with Sem_Warn; use Sem_Warn;
59 with Sinput; use Sinput;
60 with Snames; use Snames;
61 with Stand; use Stand;
62 with Sinfo; use Sinfo;
63 with Stringt; use Stringt;
64 with Targparm; use Targparm;
65 with Ttypes; use Ttypes;
66 with Tbuild; use Tbuild;
67 with Urealp; use Urealp;
68 with Warnsw; use Warnsw;
70 with GNAT.Heap_Sort_G;
72 package body Sem_Ch13 is
74 SSU : constant Pos := System_Storage_Unit;
75 -- Convenient short hand for commonly used constant
77 -----------------------
78 -- Local Subprograms --
79 -----------------------
81 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint);
82 -- This routine is called after setting one of the sizes of type entity
83 -- Typ to Size. The purpose is to deal with the situation of a derived
84 -- type whose inherited alignment is no longer appropriate for the new
85 -- size value. In this case, we reset the Alignment to unknown.
87 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id);
88 -- If Typ has predicates (indicated by Has_Predicates being set for Typ,
89 -- then either there are pragma Predicate entries on the rep chain for the
90 -- type (note that Predicate aspects are converted to pragma Predicate), or
91 -- there are inherited aspects from a parent type, or ancestor subtypes.
92 -- This procedure builds the spec and body for the Predicate function that
93 -- tests these predicates. N is the freeze node for the type. The spec of
94 -- the function is inserted before the freeze node, and the body of the
95 -- function is inserted after the freeze node. If the predicate expression
96 -- has at least one Raise_Expression, then this procedure also builds the
97 -- M version of the predicate function for use in membership tests.
99 procedure Build_Static_Predicate
103 -- Given a predicated type Typ, where Typ is a discrete static subtype,
104 -- whose predicate expression is Expr, tests if Expr is a static predicate,
105 -- and if so, builds the predicate range list. Nam is the name of the one
106 -- argument to the predicate function. Occurrences of the type name in the
107 -- predicate expression have been replaced by identifier references to this
108 -- name, which is unique, so any identifier with Chars matching Nam must be
109 -- a reference to the type. If the predicate is non-static, this procedure
110 -- returns doing nothing. If the predicate is static, then the predicate
111 -- list is stored in Static_Predicate (Typ), and the Expr is rewritten as
112 -- a canonicalized membership operation.
114 function Get_Alignment_Value (Expr : Node_Id) return Uint;
115 -- Given the expression for an alignment value, returns the corresponding
116 -- Uint value. If the value is inappropriate, then error messages are
117 -- posted as required, and a value of No_Uint is returned.
119 function Is_Operational_Item (N : Node_Id) return Boolean;
120 -- A specification for a stream attribute is allowed before the full type
121 -- is declared, as explained in AI-00137 and the corrigendum. Attributes
122 -- that do not specify a representation characteristic are operational
125 procedure New_Stream_Subprogram
129 Nam : TSS_Name_Type);
130 -- Create a subprogram renaming of a given stream attribute to the
131 -- designated subprogram and then in the tagged case, provide this as a
132 -- primitive operation, or in the non-tagged case make an appropriate TSS
133 -- entry. This is more properly an expansion activity than just semantics,
134 -- but the presence of user-defined stream functions for limited types is a
135 -- legality check, which is why this takes place here rather than in
136 -- exp_ch13, where it was previously. Nam indicates the name of the TSS
137 -- function to be generated.
139 -- To avoid elaboration anomalies with freeze nodes, for untagged types
140 -- we generate both a subprogram declaration and a subprogram renaming
141 -- declaration, so that the attribute specification is handled as a
142 -- renaming_as_body. For tagged types, the specification is one of the
146 with procedure Replace_Type_Reference (N : Node_Id);
147 procedure Replace_Type_References_Generic (N : Node_Id; TName : Name_Id);
148 -- This is used to scan an expression for a predicate or invariant aspect
149 -- replacing occurrences of the name TName (the name of the subtype to
150 -- which the aspect applies) with appropriate references to the parameter
151 -- of the predicate function or invariant procedure. The procedure passed
152 -- as a generic parameter does the actual replacement of node N, which is
153 -- either a simple direct reference to TName, or a selected component that
154 -- represents an appropriately qualified occurrence of TName.
160 Biased : Boolean := True);
161 -- If Biased is True, sets Has_Biased_Representation flag for E, and
162 -- outputs a warning message at node N if Warn_On_Biased_Representation is
163 -- is True. This warning inserts the string Msg to describe the construct
166 ----------------------------------------------
167 -- Table for Validate_Unchecked_Conversions --
168 ----------------------------------------------
170 -- The following table collects unchecked conversions for validation.
171 -- Entries are made by Validate_Unchecked_Conversion and then the call
172 -- to Validate_Unchecked_Conversions does the actual error checking and
173 -- posting of warnings. The reason for this delayed processing is to take
174 -- advantage of back-annotations of size and alignment values performed by
177 -- Note: the reason we store a Source_Ptr value instead of a Node_Id is
178 -- that by the time Validate_Unchecked_Conversions is called, Sprint will
179 -- already have modified all Sloc values if the -gnatD option is set.
181 type UC_Entry is record
182 Eloc : Source_Ptr; -- node used for posting warnings
183 Source : Entity_Id; -- source type for unchecked conversion
184 Target : Entity_Id; -- target type for unchecked conversion
187 package Unchecked_Conversions is new Table.Table (
188 Table_Component_Type => UC_Entry,
189 Table_Index_Type => Int,
190 Table_Low_Bound => 1,
192 Table_Increment => 200,
193 Table_Name => "Unchecked_Conversions");
195 ----------------------------------------
196 -- Table for Validate_Address_Clauses --
197 ----------------------------------------
199 -- If an address clause has the form
201 -- for X'Address use Expr
203 -- where Expr is of the form Y'Address or recursively is a reference to a
204 -- constant of either of these forms, and X and Y are entities of objects,
205 -- then if Y has a smaller alignment than X, that merits a warning about
206 -- possible bad alignment. The following table collects address clauses of
207 -- this kind. We put these in a table so that they can be checked after the
208 -- back end has completed annotation of the alignments of objects, since we
209 -- can catch more cases that way.
211 type Address_Clause_Check_Record is record
213 -- The address clause
216 -- The entity of the object overlaying Y
219 -- The entity of the object being overlaid
222 -- Whether the address is offset within Y
225 package Address_Clause_Checks is new Table.Table (
226 Table_Component_Type => Address_Clause_Check_Record,
227 Table_Index_Type => Int,
228 Table_Low_Bound => 1,
230 Table_Increment => 200,
231 Table_Name => "Address_Clause_Checks");
233 -----------------------------------------
234 -- Adjust_Record_For_Reverse_Bit_Order --
235 -----------------------------------------
237 procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is
242 -- Processing depends on version of Ada
244 -- For Ada 95, we just renumber bits within a storage unit. We do the
245 -- same for Ada 83 mode, since we recognize the Bit_Order attribute in
246 -- Ada 83, and are free to add this extension.
248 if Ada_Version < Ada_2005 then
249 Comp := First_Component_Or_Discriminant (R);
250 while Present (Comp) loop
251 CC := Component_Clause (Comp);
253 -- If component clause is present, then deal with the non-default
254 -- bit order case for Ada 95 mode.
256 -- We only do this processing for the base type, and in fact that
257 -- is important, since otherwise if there are record subtypes, we
258 -- could reverse the bits once for each subtype, which is wrong.
260 if Present (CC) and then Ekind (R) = E_Record_Type then
262 CFB : constant Uint := Component_Bit_Offset (Comp);
263 CSZ : constant Uint := Esize (Comp);
264 CLC : constant Node_Id := Component_Clause (Comp);
265 Pos : constant Node_Id := Position (CLC);
266 FB : constant Node_Id := First_Bit (CLC);
268 Storage_Unit_Offset : constant Uint :=
269 CFB / System_Storage_Unit;
271 Start_Bit : constant Uint :=
272 CFB mod System_Storage_Unit;
275 -- Cases where field goes over storage unit boundary
277 if Start_Bit + CSZ > System_Storage_Unit then
279 -- Allow multi-byte field but generate warning
281 if Start_Bit mod System_Storage_Unit = 0
282 and then CSZ mod System_Storage_Unit = 0
285 ("multi-byte field specified with non-standard"
286 & " Bit_Order??", CLC);
288 if Bytes_Big_Endian then
290 ("bytes are not reversed "
291 & "(component is big-endian)??", CLC);
294 ("bytes are not reversed "
295 & "(component is little-endian)??", CLC);
298 -- Do not allow non-contiguous field
302 ("attempt to specify non-contiguous field "
303 & "not permitted", CLC);
305 ("\caused by non-standard Bit_Order "
308 ("\consider possibility of using "
309 & "Ada 2005 mode here", CLC);
312 -- Case where field fits in one storage unit
315 -- Give warning if suspicious component clause
317 if Intval (FB) >= System_Storage_Unit
318 and then Warn_On_Reverse_Bit_Order
321 ("Bit_Order clause does not affect " &
322 "byte ordering?V?", Pos);
324 Intval (Pos) + Intval (FB) /
327 ("position normalized to ^ before bit " &
328 "order interpreted?V?", Pos);
331 -- Here is where we fix up the Component_Bit_Offset value
332 -- to account for the reverse bit order. Some examples of
333 -- what needs to be done are:
335 -- First_Bit .. Last_Bit Component_Bit_Offset
347 -- The rule is that the first bit is is obtained by
348 -- subtracting the old ending bit from storage_unit - 1.
350 Set_Component_Bit_Offset
352 (Storage_Unit_Offset * System_Storage_Unit) +
353 (System_Storage_Unit - 1) -
354 (Start_Bit + CSZ - 1));
356 Set_Normalized_First_Bit
358 Component_Bit_Offset (Comp) mod
359 System_Storage_Unit);
364 Next_Component_Or_Discriminant (Comp);
367 -- For Ada 2005, we do machine scalar processing, as fully described In
368 -- AI-133. This involves gathering all components which start at the
369 -- same byte offset and processing them together. Same approach is still
370 -- valid in later versions including Ada 2012.
374 Max_Machine_Scalar_Size : constant Uint :=
376 (Standard_Long_Long_Integer_Size);
377 -- We use this as the maximum machine scalar size
380 SSU : constant Uint := UI_From_Int (System_Storage_Unit);
383 -- This first loop through components does two things. First it
384 -- deals with the case of components with component clauses whose
385 -- length is greater than the maximum machine scalar size (either
386 -- accepting them or rejecting as needed). Second, it counts the
387 -- number of components with component clauses whose length does
388 -- not exceed this maximum for later processing.
391 Comp := First_Component_Or_Discriminant (R);
392 while Present (Comp) loop
393 CC := Component_Clause (Comp);
397 Fbit : constant Uint := Static_Integer (First_Bit (CC));
398 Lbit : constant Uint := Static_Integer (Last_Bit (CC));
401 -- Case of component with last bit >= max machine scalar
403 if Lbit >= Max_Machine_Scalar_Size then
405 -- This is allowed only if first bit is zero, and
406 -- last bit + 1 is a multiple of storage unit size.
408 if Fbit = 0 and then (Lbit + 1) mod SSU = 0 then
410 -- This is the case to give a warning if enabled
412 if Warn_On_Reverse_Bit_Order then
414 ("multi-byte field specified with "
415 & " non-standard Bit_Order?V?", CC);
417 if Bytes_Big_Endian then
419 ("\bytes are not reversed "
420 & "(component is big-endian)?V?", CC);
423 ("\bytes are not reversed "
424 & "(component is little-endian)?V?", CC);
428 -- Give error message for RM 13.5.1(10) violation
432 ("machine scalar rules not followed for&",
433 First_Bit (CC), Comp);
435 Error_Msg_Uint_1 := Lbit;
436 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
438 ("\last bit (^) exceeds maximum machine "
442 if (Lbit + 1) mod SSU /= 0 then
443 Error_Msg_Uint_1 := SSU;
445 ("\and is not a multiple of Storage_Unit (^) "
450 Error_Msg_Uint_1 := Fbit;
452 ("\and first bit (^) is non-zero "
458 -- OK case of machine scalar related component clause,
459 -- For now, just count them.
462 Num_CC := Num_CC + 1;
467 Next_Component_Or_Discriminant (Comp);
470 -- We need to sort the component clauses on the basis of the
471 -- Position values in the clause, so we can group clauses with
472 -- the same Position. together to determine the relevant machine
476 Comps : array (0 .. Num_CC) of Entity_Id;
477 -- Array to collect component and discriminant entities. The
478 -- data starts at index 1, the 0'th entry is for the sort
481 function CP_Lt (Op1, Op2 : Natural) return Boolean;
482 -- Compare routine for Sort
484 procedure CP_Move (From : Natural; To : Natural);
485 -- Move routine for Sort
487 package Sorting is new GNAT.Heap_Sort_G (CP_Move, CP_Lt);
491 -- Start and stop positions in the component list of the set of
492 -- components with the same starting position (that constitute
493 -- components in a single machine scalar).
496 -- Maximum last bit value of any component in this set
499 -- Corresponding machine scalar size
505 function CP_Lt (Op1, Op2 : Natural) return Boolean is
507 return Position (Component_Clause (Comps (Op1))) <
508 Position (Component_Clause (Comps (Op2)));
515 procedure CP_Move (From : Natural; To : Natural) is
517 Comps (To) := Comps (From);
520 -- Start of processing for Sort_CC
523 -- Collect the machine scalar relevant component clauses
526 Comp := First_Component_Or_Discriminant (R);
527 while Present (Comp) loop
529 CC : constant Node_Id := Component_Clause (Comp);
532 -- Collect only component clauses whose last bit is less
533 -- than machine scalar size. Any component clause whose
534 -- last bit exceeds this value does not take part in
535 -- machine scalar layout considerations. The test for
536 -- Error_Posted makes sure we exclude component clauses
537 -- for which we already posted an error.
540 and then not Error_Posted (Last_Bit (CC))
541 and then Static_Integer (Last_Bit (CC)) <
542 Max_Machine_Scalar_Size
544 Num_CC := Num_CC + 1;
545 Comps (Num_CC) := Comp;
549 Next_Component_Or_Discriminant (Comp);
552 -- Sort by ascending position number
554 Sorting.Sort (Num_CC);
556 -- We now have all the components whose size does not exceed
557 -- the max machine scalar value, sorted by starting position.
558 -- In this loop we gather groups of clauses starting at the
559 -- same position, to process them in accordance with AI-133.
562 while Stop < Num_CC loop
567 (Last_Bit (Component_Clause (Comps (Start))));
568 while Stop < Num_CC loop
570 (Position (Component_Clause (Comps (Stop + 1)))) =
572 (Position (Component_Clause (Comps (Stop))))
580 (Component_Clause (Comps (Stop)))));
586 -- Now we have a group of component clauses from Start to
587 -- Stop whose positions are identical, and MaxL is the
588 -- maximum last bit value of any of these components.
590 -- We need to determine the corresponding machine scalar
591 -- size. This loop assumes that machine scalar sizes are
592 -- even, and that each possible machine scalar has twice
593 -- as many bits as the next smaller one.
595 MSS := Max_Machine_Scalar_Size;
597 and then (MSS / 2) >= SSU
598 and then (MSS / 2) > MaxL
603 -- Here is where we fix up the Component_Bit_Offset value
604 -- to account for the reverse bit order. Some examples of
605 -- what needs to be done for the case of a machine scalar
608 -- First_Bit .. Last_Bit Component_Bit_Offset
620 -- The rule is that the first bit is obtained by subtracting
621 -- the old ending bit from machine scalar size - 1.
623 for C in Start .. Stop loop
625 Comp : constant Entity_Id := Comps (C);
626 CC : constant Node_Id := Component_Clause (Comp);
628 LB : constant Uint := Static_Integer (Last_Bit (CC));
629 NFB : constant Uint := MSS - Uint_1 - LB;
630 NLB : constant Uint := NFB + Esize (Comp) - 1;
631 Pos : constant Uint := Static_Integer (Position (CC));
634 if Warn_On_Reverse_Bit_Order then
635 Error_Msg_Uint_1 := MSS;
637 ("info: reverse bit order in machine " &
638 "scalar of length^?V?", First_Bit (CC));
639 Error_Msg_Uint_1 := NFB;
640 Error_Msg_Uint_2 := NLB;
642 if Bytes_Big_Endian then
644 ("\info: big-endian range for "
645 & "component & is ^ .. ^?V?",
646 First_Bit (CC), Comp);
649 ("\info: little-endian range "
650 & "for component & is ^ .. ^?V?",
651 First_Bit (CC), Comp);
655 Set_Component_Bit_Offset (Comp, Pos * SSU + NFB);
656 Set_Normalized_First_Bit (Comp, NFB mod SSU);
663 end Adjust_Record_For_Reverse_Bit_Order;
665 -------------------------------------
666 -- Alignment_Check_For_Size_Change --
667 -------------------------------------
669 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint) is
671 -- If the alignment is known, and not set by a rep clause, and is
672 -- inconsistent with the size being set, then reset it to unknown,
673 -- we assume in this case that the size overrides the inherited
674 -- alignment, and that the alignment must be recomputed.
676 if Known_Alignment (Typ)
677 and then not Has_Alignment_Clause (Typ)
678 and then Size mod (Alignment (Typ) * SSU) /= 0
680 Init_Alignment (Typ);
682 end Alignment_Check_For_Size_Change;
684 -------------------------------------
685 -- Analyze_Aspects_At_Freeze_Point --
686 -------------------------------------
688 procedure Analyze_Aspects_At_Freeze_Point (E : Entity_Id) is
693 procedure Analyze_Aspect_Default_Value (ASN : Node_Id);
694 -- This routine analyzes an Aspect_Default_[Component_]Value denoted by
695 -- the aspect specification node ASN.
697 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id);
698 -- Given an aspect specification node ASN whose expression is an
699 -- optional Boolean, this routines creates the corresponding pragma
700 -- at the freezing point.
702 ----------------------------------
703 -- Analyze_Aspect_Default_Value --
704 ----------------------------------
706 procedure Analyze_Aspect_Default_Value (ASN : Node_Id) is
707 Ent : constant Entity_Id := Entity (ASN);
708 Expr : constant Node_Id := Expression (ASN);
709 Id : constant Node_Id := Identifier (ASN);
712 Error_Msg_Name_1 := Chars (Id);
714 if not Is_Type (Ent) then
715 Error_Msg_N ("aspect% can only apply to a type", Id);
718 elsif not Is_First_Subtype (Ent) then
719 Error_Msg_N ("aspect% cannot apply to subtype", Id);
722 elsif A_Id = Aspect_Default_Value
723 and then not Is_Scalar_Type (Ent)
725 Error_Msg_N ("aspect% can only be applied to scalar type", Id);
728 elsif A_Id = Aspect_Default_Component_Value then
729 if not Is_Array_Type (Ent) then
730 Error_Msg_N ("aspect% can only be applied to array type", Id);
733 elsif not Is_Scalar_Type (Component_Type (Ent)) then
734 Error_Msg_N ("aspect% requires scalar components", Id);
739 Set_Has_Default_Aspect (Base_Type (Ent));
741 if Is_Scalar_Type (Ent) then
742 Set_Default_Aspect_Value (Ent, Expr);
744 -- Place default value of base type as well, because that is
745 -- the semantics of the aspect. It is convenient to link the
746 -- aspect to both the (possibly anonymous) base type and to
747 -- the given first subtype.
749 Set_Default_Aspect_Value (Base_Type (Ent), Expr);
752 Set_Default_Aspect_Component_Value (Ent, Expr);
754 end Analyze_Aspect_Default_Value;
756 -------------------------------------
757 -- Make_Pragma_From_Boolean_Aspect --
758 -------------------------------------
760 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id) is
761 Ident : constant Node_Id := Identifier (ASN);
762 A_Name : constant Name_Id := Chars (Ident);
763 A_Id : constant Aspect_Id := Get_Aspect_Id (A_Name);
764 Ent : constant Entity_Id := Entity (ASN);
765 Expr : constant Node_Id := Expression (ASN);
766 Loc : constant Source_Ptr := Sloc (ASN);
770 procedure Check_False_Aspect_For_Derived_Type;
771 -- This procedure checks for the case of a false aspect for a derived
772 -- type, which improperly tries to cancel an aspect inherited from
775 -----------------------------------------
776 -- Check_False_Aspect_For_Derived_Type --
777 -----------------------------------------
779 procedure Check_False_Aspect_For_Derived_Type is
783 -- We are only checking derived types
785 if not Is_Derived_Type (E) then
789 Par := Nearest_Ancestor (E);
792 when Aspect_Atomic | Aspect_Shared =>
793 if not Is_Atomic (Par) then
797 when Aspect_Atomic_Components =>
798 if not Has_Atomic_Components (Par) then
802 when Aspect_Discard_Names =>
803 if not Discard_Names (Par) then
808 if not Is_Packed (Par) then
812 when Aspect_Unchecked_Union =>
813 if not Is_Unchecked_Union (Par) then
817 when Aspect_Volatile =>
818 if not Is_Volatile (Par) then
822 when Aspect_Volatile_Components =>
823 if not Has_Volatile_Components (Par) then
831 -- Fall through means we are canceling an inherited aspect
833 Error_Msg_Name_1 := A_Name;
834 Error_Msg_NE ("derived type& inherits aspect%, cannot cancel",
838 end Check_False_Aspect_For_Derived_Type;
840 -- Start of processing for Make_Pragma_From_Boolean_Aspect
843 if Is_False (Static_Boolean (Expr)) then
844 Check_False_Aspect_For_Derived_Type;
849 Pragma_Argument_Associations => New_List (
850 Make_Pragma_Argument_Association (Sloc (Ident),
851 Expression => New_Occurrence_Of (Ent, Sloc (Ident)))),
854 Make_Identifier (Sloc (Ident), Chars (Ident)));
856 Set_From_Aspect_Specification (Prag, True);
857 Set_Corresponding_Aspect (Prag, ASN);
858 Set_Aspect_Rep_Item (ASN, Prag);
859 Set_Is_Delayed_Aspect (Prag);
860 Set_Parent (Prag, ASN);
862 end Make_Pragma_From_Boolean_Aspect;
864 -- Start of processing for Analyze_Aspects_At_Freeze_Point
867 -- Must be visible in current scope
869 if not Scope_Within_Or_Same (Current_Scope, Scope (E)) then
873 -- Look for aspect specification entries for this entity
875 ASN := First_Rep_Item (E);
876 while Present (ASN) loop
877 if Nkind (ASN) = N_Aspect_Specification
878 and then Entity (ASN) = E
879 and then Is_Delayed_Aspect (ASN)
881 A_Id := Get_Aspect_Id (ASN);
885 -- For aspects whose expression is an optional Boolean, make
886 -- the corresponding pragma at the freezing point.
888 when Boolean_Aspects |
889 Library_Unit_Aspects =>
890 Make_Pragma_From_Boolean_Aspect (ASN);
892 -- Special handling for aspects that don't correspond to
893 -- pragmas/attributes.
895 when Aspect_Default_Value |
896 Aspect_Default_Component_Value =>
897 Analyze_Aspect_Default_Value (ASN);
899 -- Ditto for iterator aspects, because the corresponding
900 -- attributes may not have been analyzed yet.
902 when Aspect_Constant_Indexing |
903 Aspect_Variable_Indexing |
904 Aspect_Default_Iterator |
905 Aspect_Iterator_Element =>
906 Analyze (Expression (ASN));
912 Ritem := Aspect_Rep_Item (ASN);
914 if Present (Ritem) then
921 end Analyze_Aspects_At_Freeze_Point;
923 -----------------------------------
924 -- Analyze_Aspect_Specifications --
925 -----------------------------------
927 procedure Analyze_Aspect_Specifications (N : Node_Id; E : Entity_Id) is
928 procedure Insert_Delayed_Pragma (Prag : Node_Id);
929 -- Insert a postcondition-like pragma into the tree depending on the
930 -- context. Prag one of the following: Pre, Post, Depends or Global.
932 ---------------------------
933 -- Insert_Delayed_Pragma --
934 ---------------------------
936 procedure Insert_Delayed_Pragma (Prag : Node_Id) is
940 -- When the context is a library unit, the pragma is added to the
941 -- Pragmas_After list.
943 if Nkind (Parent (N)) = N_Compilation_Unit then
944 Aux := Aux_Decls_Node (Parent (N));
946 if No (Pragmas_After (Aux)) then
947 Set_Pragmas_After (Aux, New_List);
950 Prepend (Prag, Pragmas_After (Aux));
952 -- Pragmas associated with subprogram bodies are inserted in the
955 elsif Nkind (N) = N_Subprogram_Body then
956 if No (Declarations (N)) then
957 Set_Declarations (N, New_List);
960 Append (Prag, Declarations (N));
965 Insert_After (N, Prag);
967 -- Analyze the pragma before analyzing the proper body of a stub.
968 -- This ensures that the pragma will appear on the proper contract
969 -- list (see N_Contract).
971 if Nkind (N) = N_Subprogram_Body_Stub then
975 end Insert_Delayed_Pragma;
983 L : constant List_Id := Aspect_Specifications (N);
985 Ins_Node : Node_Id := N;
986 -- Insert pragmas/attribute definition clause after this node when no
987 -- delayed analysis is required.
989 -- The general processing involves building an attribute definition
990 -- clause or a pragma node that corresponds to the aspect. Then in order
991 -- to delay the evaluation of this aspect to the freeze point, we attach
992 -- the corresponding pragma/attribute definition clause to the aspect
993 -- specification node, which is then placed in the Rep Item chain. In
994 -- this case we mark the entity by setting the flag Has_Delayed_Aspects
995 -- and we evaluate the rep item at the freeze point. When the aspect
996 -- doesn't have a corresponding pragma/attribute definition clause, then
997 -- its analysis is simply delayed at the freeze point.
999 -- Some special cases don't require delay analysis, thus the aspect is
1000 -- analyzed right now.
1002 -- Note that there is a special handling for Pre, Post, Test_Case,
1003 -- Contract_Cases aspects. In these cases, we do not have to worry
1004 -- about delay issues, since the pragmas themselves deal with delay
1005 -- of visibility for the expression analysis. Thus, we just insert
1006 -- the pragma after the node N.
1009 pragma Assert (Present (L));
1011 -- Loop through aspects
1013 Aspect := First (L);
1014 Aspect_Loop : while Present (Aspect) loop
1015 Analyze_One_Aspect : declare
1016 Expr : constant Node_Id := Expression (Aspect);
1017 Id : constant Node_Id := Identifier (Aspect);
1018 Loc : constant Source_Ptr := Sloc (Aspect);
1019 Nam : constant Name_Id := Chars (Id);
1020 A_Id : constant Aspect_Id := Get_Aspect_Id (Nam);
1023 Delay_Required : Boolean := True;
1024 -- Set False if delay is not required
1026 Eloc : Source_Ptr := No_Location;
1027 -- Source location of expression, modified when we split PPC's. It
1028 -- is set below when Expr is present.
1030 procedure Analyze_Aspect_External_Or_Link_Name;
1031 -- Perform analysis of the External_Name or Link_Name aspects
1033 procedure Analyze_Aspect_Implicit_Dereference;
1034 -- Perform analysis of the Implicit_Dereference aspects
1036 procedure Make_Aitem_Pragma
1037 (Pragma_Argument_Associations : List_Id;
1038 Pragma_Name : Name_Id);
1039 -- This is a wrapper for Make_Pragma used for converting aspects
1040 -- to pragmas. It takes care of Sloc (set from Loc) and building
1041 -- the pragma identifier from the given name. In addition the
1042 -- flags Class_Present and Split_PPC are set from the aspect
1043 -- node, as well as Is_Ignored. This routine also sets the
1044 -- From_Aspect_Specification in the resulting pragma node to
1045 -- True, and sets Corresponding_Aspect to point to the aspect.
1046 -- The resulting pragma is assigned to Aitem.
1048 ------------------------------------------
1049 -- Analyze_Aspect_External_Or_Link_Name --
1050 ------------------------------------------
1052 procedure Analyze_Aspect_External_Or_Link_Name is
1054 -- Verify that there is an Import/Export aspect defined for the
1055 -- entity. The processing of that aspect in turn checks that
1056 -- there is a Convention aspect declared. The pragma is
1057 -- constructed when processing the Convention aspect.
1064 while Present (A) loop
1065 exit when Nam_In (Chars (Identifier (A)), Name_Export,
1072 ("missing Import/Export for Link/External name",
1076 end Analyze_Aspect_External_Or_Link_Name;
1078 -----------------------------------------
1079 -- Analyze_Aspect_Implicit_Dereference --
1080 -----------------------------------------
1082 procedure Analyze_Aspect_Implicit_Dereference is
1084 if not Is_Type (E) or else not Has_Discriminants (E) then
1086 ("aspect must apply to a type with discriminants", N);
1093 Disc := First_Discriminant (E);
1094 while Present (Disc) loop
1095 if Chars (Expr) = Chars (Disc)
1096 and then Ekind (Etype (Disc)) =
1097 E_Anonymous_Access_Type
1099 Set_Has_Implicit_Dereference (E);
1100 Set_Has_Implicit_Dereference (Disc);
1104 Next_Discriminant (Disc);
1107 -- Error if no proper access discriminant.
1110 ("not an access discriminant of&", Expr, E);
1113 end Analyze_Aspect_Implicit_Dereference;
1115 -----------------------
1116 -- Make_Aitem_Pragma --
1117 -----------------------
1119 procedure Make_Aitem_Pragma
1120 (Pragma_Argument_Associations : List_Id;
1121 Pragma_Name : Name_Id)
1124 -- We should never get here if aspect was disabled
1126 pragma Assert (not Is_Disabled (Aspect));
1132 Pragma_Argument_Associations =>
1133 Pragma_Argument_Associations,
1134 Pragma_Identifier =>
1135 Make_Identifier (Sloc (Id), Pragma_Name),
1136 Class_Present => Class_Present (Aspect),
1137 Split_PPC => Split_PPC (Aspect));
1139 -- Set additional semantic fields
1141 if Is_Ignored (Aspect) then
1142 Set_Is_Ignored (Aitem);
1145 Set_Corresponding_Aspect (Aitem, Aspect);
1146 Set_From_Aspect_Specification (Aitem, True);
1147 end Make_Aitem_Pragma;
1149 -- Start of processing for Analyze_One_Aspect
1152 -- Skip aspect if already analyzed (not clear if this is needed)
1154 if Analyzed (Aspect) then
1158 -- Skip looking at aspect if it is totally disabled. Just mark
1159 -- it as such for later reference in the tree. This also sets
1160 -- the Is_Ignored flag appropriately.
1162 Check_Applicable_Policy (Aspect);
1164 if Is_Disabled (Aspect) then
1168 -- Set the source location of expression, used in the case of
1169 -- a failed precondition/postcondition or invariant. Note that
1170 -- the source location of the expression is not usually the best
1171 -- choice here. For example, it gets located on the last AND
1172 -- keyword in a chain of boolean expressiond AND'ed together.
1173 -- It is best to put the message on the first character of the
1174 -- assertion, which is the effect of the First_Node call here.
1176 if Present (Expr) then
1177 Eloc := Sloc (First_Node (Expr));
1180 -- Check restriction No_Implementation_Aspect_Specifications
1182 if Implementation_Defined_Aspect (A_Id) then
1184 (No_Implementation_Aspect_Specifications, Aspect);
1187 -- Check restriction No_Specification_Of_Aspect
1189 Check_Restriction_No_Specification_Of_Aspect (Aspect);
1191 -- Analyze this aspect (actual analysis is delayed till later)
1193 Set_Analyzed (Aspect);
1194 Set_Entity (Aspect, E);
1195 Ent := New_Occurrence_Of (E, Sloc (Id));
1197 -- Check for duplicate aspect. Note that the Comes_From_Source
1198 -- test allows duplicate Pre/Post's that we generate internally
1199 -- to escape being flagged here.
1201 if No_Duplicates_Allowed (A_Id) then
1203 while Anod /= Aspect loop
1204 if Comes_From_Source (Aspect)
1205 and then Same_Aspect (A_Id, Get_Aspect_Id (Anod))
1207 Error_Msg_Name_1 := Nam;
1208 Error_Msg_Sloc := Sloc (Anod);
1210 -- Case of same aspect specified twice
1212 if Class_Present (Anod) = Class_Present (Aspect) then
1213 if not Class_Present (Anod) then
1215 ("aspect% for & previously given#",
1219 ("aspect `%''Class` for & previously given#",
1229 -- Check some general restrictions on language defined aspects
1231 if not Implementation_Defined_Aspect (A_Id) then
1232 Error_Msg_Name_1 := Nam;
1234 -- Not allowed for renaming declarations
1236 if Nkind (N) in N_Renaming_Declaration then
1238 ("aspect % not allowed for renaming declaration",
1242 -- Not allowed for formal type declarations
1244 if Nkind (N) = N_Formal_Type_Declaration then
1246 ("aspect % not allowed for formal type declaration",
1251 -- Copy expression for later processing by the procedures
1252 -- Check_Aspect_At_[Freeze_Point | End_Of_Declarations]
1254 Set_Entity (Id, New_Copy_Tree (Expr));
1256 -- Processing based on specific aspect
1260 -- No_Aspect should be impossible
1263 raise Program_Error;
1265 -- Case 1: Aspects corresponding to attribute definition
1268 when Aspect_Address |
1271 Aspect_Component_Size |
1272 Aspect_Constant_Indexing |
1273 Aspect_Default_Iterator |
1274 Aspect_Dispatching_Domain |
1275 Aspect_External_Tag |
1277 Aspect_Iterator_Element |
1278 Aspect_Machine_Radix |
1279 Aspect_Object_Size |
1282 Aspect_Scalar_Storage_Order |
1285 Aspect_Simple_Storage_Pool |
1286 Aspect_Storage_Pool |
1287 Aspect_Storage_Size |
1288 Aspect_Stream_Size |
1290 Aspect_Variable_Indexing |
1293 -- Indexing aspects apply only to tagged type
1295 if (A_Id = Aspect_Constant_Indexing
1296 or else A_Id = Aspect_Variable_Indexing)
1297 and then not (Is_Type (E)
1298 and then Is_Tagged_Type (E))
1300 Error_Msg_N ("indexing applies to a tagged type", N);
1304 -- Construct the attribute definition clause
1307 Make_Attribute_Definition_Clause (Loc,
1309 Chars => Chars (Id),
1310 Expression => Relocate_Node (Expr));
1312 -- Case 2: Aspects corresponding to pragmas
1314 -- Case 2a: Aspects corresponding to pragmas with two
1315 -- arguments, where the first argument is a local name
1316 -- referring to the entity, and the second argument is the
1317 -- aspect definition expression.
1319 -- Suppress/Unsuppress
1321 when Aspect_Suppress |
1322 Aspect_Unsuppress =>
1325 (Pragma_Argument_Associations => New_List (
1326 Make_Pragma_Argument_Association (Loc,
1327 Expression => New_Occurrence_Of (E, Loc)),
1328 Make_Pragma_Argument_Association (Sloc (Expr),
1329 Expression => Relocate_Node (Expr))),
1330 Pragma_Name => Chars (Id));
1334 -- Corresponds to pragma Implemented, construct the pragma
1336 when Aspect_Synchronization =>
1339 (Pragma_Argument_Associations => New_List (
1340 Make_Pragma_Argument_Association (Loc,
1341 Expression => New_Occurrence_Of (E, Loc)),
1342 Make_Pragma_Argument_Association (Sloc (Expr),
1343 Expression => Relocate_Node (Expr))),
1344 Pragma_Name => Name_Implemented);
1346 -- No delay is required since the only values are: By_Entry
1347 -- | By_Protected_Procedure | By_Any | Optional which don't
1348 -- get analyzed anyway.
1350 Delay_Required := False;
1354 when Aspect_Attach_Handler =>
1356 (Pragma_Argument_Associations => New_List (
1357 Make_Pragma_Argument_Association (Sloc (Ent),
1359 Make_Pragma_Argument_Association (Sloc (Expr),
1360 Expression => Relocate_Node (Expr))),
1361 Pragma_Name => Name_Attach_Handler);
1363 -- Dynamic_Predicate, Predicate, Static_Predicate
1365 when Aspect_Dynamic_Predicate |
1367 Aspect_Static_Predicate =>
1369 -- Construct the pragma (always a pragma Predicate, with
1370 -- flags recording whether it is static/dynamic). We also
1371 -- set flags recording this in the type itself.
1374 (Pragma_Argument_Associations => New_List (
1375 Make_Pragma_Argument_Association (Sloc (Ent),
1377 Make_Pragma_Argument_Association (Sloc (Expr),
1378 Expression => Relocate_Node (Expr))),
1379 Pragma_Name => Name_Predicate);
1381 -- Mark type has predicates, and remember what kind of
1382 -- aspect lead to this predicate (we need this to access
1383 -- the right set of check policies later on).
1385 Set_Has_Predicates (E);
1387 if A_Id = Aspect_Dynamic_Predicate then
1388 Set_Has_Dynamic_Predicate_Aspect (E);
1389 elsif A_Id = Aspect_Static_Predicate then
1390 Set_Has_Static_Predicate_Aspect (E);
1393 -- If the type is private, indicate that its completion
1394 -- has a freeze node, because that is the one that will be
1395 -- visible at freeze time.
1397 if Is_Private_Type (E) and then Present (Full_View (E)) then
1398 Set_Has_Predicates (Full_View (E));
1400 if A_Id = Aspect_Dynamic_Predicate then
1401 Set_Has_Dynamic_Predicate_Aspect (Full_View (E));
1402 elsif A_Id = Aspect_Static_Predicate then
1403 Set_Has_Static_Predicate_Aspect (Full_View (E));
1406 Set_Has_Delayed_Aspects (Full_View (E));
1407 Ensure_Freeze_Node (Full_View (E));
1410 -- Case 2b: Aspects corresponding to pragmas with two
1411 -- arguments, where the second argument is a local name
1412 -- referring to the entity, and the first argument is the
1413 -- aspect definition expression.
1417 when Aspect_Convention =>
1419 -- The aspect may be part of the specification of an import
1420 -- or export pragma. Scan the aspect list to gather the
1421 -- other components, if any. The name of the generated
1422 -- pragma is one of Convention/Import/Export.
1434 P_Name := Chars (Id);
1436 Arg_List := New_List;
1441 while Present (A) loop
1442 A_Name := Chars (Identifier (A));
1444 if Nam_In (A_Name, Name_Import, Name_Export) then
1446 Error_Msg_N ("conflicting", A);
1453 elsif A_Name = Name_Link_Name then
1455 Make_Pragma_Argument_Association (Loc,
1457 Expression => Relocate_Node (Expression (A)));
1459 elsif A_Name = Name_External_Name then
1461 Make_Pragma_Argument_Association (Loc,
1463 Expression => Relocate_Node (Expression (A)));
1469 Arg_List := New_List (
1470 Make_Pragma_Argument_Association (Sloc (Expr),
1471 Expression => Relocate_Node (Expr)),
1472 Make_Pragma_Argument_Association (Sloc (Ent),
1473 Expression => Ent));
1475 if Present (L_Assoc) then
1476 Append_To (Arg_List, L_Assoc);
1479 if Present (E_Assoc) then
1480 Append_To (Arg_List, E_Assoc);
1484 (Pragma_Argument_Associations => Arg_List,
1485 Pragma_Name => P_Name);
1488 -- CPU, Interrupt_Priority, Priority
1490 -- These three aspects can be specified for a subprogram body,
1491 -- in which case we generate pragmas for them and insert them
1492 -- ahead of local declarations, rather than after the body.
1495 Aspect_Interrupt_Priority |
1498 if Nkind (N) = N_Subprogram_Body then
1500 (Pragma_Argument_Associations => New_List (
1501 Make_Pragma_Argument_Association (Sloc (Expr),
1502 Expression => Relocate_Node (Expr))),
1503 Pragma_Name => Chars (Id));
1507 Make_Attribute_Definition_Clause (Loc,
1509 Chars => Chars (Id),
1510 Expression => Relocate_Node (Expr));
1515 when Aspect_Warnings =>
1518 (Pragma_Argument_Associations => New_List (
1519 Make_Pragma_Argument_Association (Sloc (Expr),
1520 Expression => Relocate_Node (Expr)),
1521 Make_Pragma_Argument_Association (Loc,
1522 Expression => New_Occurrence_Of (E, Loc))),
1523 Pragma_Name => Chars (Id));
1525 -- We don't have to play the delay game here, since the only
1526 -- values are ON/OFF which don't get analyzed anyway.
1528 Delay_Required := False;
1530 -- Case 2c: Aspects corresponding to pragmas with three
1533 -- Invariant aspects have a first argument that references the
1534 -- entity, a second argument that is the expression and a third
1535 -- argument that is an appropriate message.
1537 -- Invariant, Type_Invariant
1539 when Aspect_Invariant |
1540 Aspect_Type_Invariant =>
1542 -- Analysis of the pragma will verify placement legality:
1543 -- an invariant must apply to a private type, or appear in
1544 -- the private part of a spec and apply to a completion.
1547 (Pragma_Argument_Associations => New_List (
1548 Make_Pragma_Argument_Association (Sloc (Ent),
1550 Make_Pragma_Argument_Association (Sloc (Expr),
1551 Expression => Relocate_Node (Expr))),
1552 Pragma_Name => Name_Invariant);
1554 -- Add message unless exception messages are suppressed
1556 if not Opt.Exception_Locations_Suppressed then
1557 Append_To (Pragma_Argument_Associations (Aitem),
1558 Make_Pragma_Argument_Association (Eloc,
1559 Chars => Name_Message,
1561 Make_String_Literal (Eloc,
1562 Strval => "failed invariant from "
1563 & Build_Location_String (Eloc))));
1566 -- For Invariant case, insert immediately after the entity
1567 -- declaration. We do not have to worry about delay issues
1568 -- since the pragma processing takes care of this.
1570 Delay_Required := False;
1572 -- Case 2d : Aspects that correspond to a pragma with one
1577 when Aspect_Abstract_State =>
1579 (Pragma_Argument_Associations => New_List (
1580 Make_Pragma_Argument_Association (Loc,
1581 Expression => Relocate_Node (Expr))),
1582 Pragma_Name => Name_Abstract_State);
1583 Delay_Required := False;
1587 -- Aspect Depends must be delayed because it mentions names
1588 -- of inputs and output that are classified by aspect Global.
1589 -- The aspect and pragma are treated the same way as a post
1592 when Aspect_Depends =>
1594 (Pragma_Argument_Associations => New_List (
1595 Make_Pragma_Argument_Association (Loc,
1596 Expression => Relocate_Node (Expr))),
1597 Pragma_Name => Name_Depends);
1599 -- Decorate the aspect and pragma
1601 Set_Aspect_Rep_Item (Aspect, Aitem);
1602 Set_Corresponding_Aspect (Aitem, Aspect);
1603 Set_From_Aspect_Specification (Aitem);
1604 Set_Is_Delayed_Aspect (Aitem);
1605 Set_Is_Delayed_Aspect (Aspect);
1606 Set_Parent (Aitem, Aspect);
1608 Insert_Delayed_Pragma (Aitem);
1613 -- Aspect Global must be delayed because it can mention names
1614 -- and benefit from the forward visibility rules applicable to
1615 -- aspects of subprograms. The aspect and pragma are treated
1616 -- the same way as a post condition.
1618 when Aspect_Global =>
1620 (Pragma_Argument_Associations => New_List (
1621 Make_Pragma_Argument_Association (Loc,
1622 Expression => Relocate_Node (Expr))),
1623 Pragma_Name => Name_Global);
1625 -- Decorate the aspect and pragma
1627 Set_Aspect_Rep_Item (Aspect, Aitem);
1628 Set_Corresponding_Aspect (Aitem, Aspect);
1629 Set_From_Aspect_Specification (Aitem);
1630 Set_Is_Delayed_Aspect (Aitem);
1631 Set_Is_Delayed_Aspect (Aspect);
1632 Set_Parent (Aitem, Aspect);
1634 Insert_Delayed_Pragma (Aitem);
1637 -- Relative_Deadline
1639 when Aspect_Relative_Deadline =>
1641 (Pragma_Argument_Associations => New_List (
1642 Make_Pragma_Argument_Association (Loc,
1643 Expression => Relocate_Node (Expr))),
1644 Pragma_Name => Name_Relative_Deadline);
1646 -- If the aspect applies to a task, the corresponding pragma
1647 -- must appear within its declarations, not after.
1649 if Nkind (N) = N_Task_Type_Declaration then
1655 if No (Task_Definition (N)) then
1656 Set_Task_Definition (N,
1657 Make_Task_Definition (Loc,
1658 Visible_Declarations => New_List,
1659 End_Label => Empty));
1662 Def := Task_Definition (N);
1663 V := Visible_Declarations (Def);
1664 if not Is_Empty_List (V) then
1665 Insert_Before (First (V), Aitem);
1668 Set_Visible_Declarations (Def, New_List (Aitem));
1675 -- Case 3 : Aspects that don't correspond to pragma/attribute
1676 -- definition clause.
1678 -- Case 3a: The aspects listed below don't correspond to
1679 -- pragmas/attributes but do require delayed analysis.
1681 -- Default_Value, Default_Component_Value
1683 when Aspect_Default_Value |
1684 Aspect_Default_Component_Value =>
1687 -- Case 3b: The aspects listed below don't correspond to
1688 -- pragmas/attributes and don't need delayed analysis.
1690 -- Implicit_Dereference
1692 -- For Implicit_Dereference, External_Name and Link_Name, only
1693 -- the legality checks are done during the analysis, thus no
1694 -- delay is required.
1696 when Aspect_Implicit_Dereference =>
1697 Analyze_Aspect_Implicit_Dereference;
1700 -- External_Name, Link_Name
1702 when Aspect_External_Name |
1704 Analyze_Aspect_External_Or_Link_Name;
1709 when Aspect_Dimension =>
1710 Analyze_Aspect_Dimension (N, Id, Expr);
1715 when Aspect_Dimension_System =>
1716 Analyze_Aspect_Dimension_System (N, Id, Expr);
1719 -- Case 4: Special handling for aspects
1721 -- Pre/Post/Test_Case/Contract_Cases whose corresponding
1722 -- pragmas take care of the delay.
1726 -- Aspects Pre/Post generate Precondition/Postcondition pragmas
1727 -- with a first argument that is the expression, and a second
1728 -- argument that is an informative message if the test fails.
1729 -- This is inserted right after the declaration, to get the
1730 -- required pragma placement. The processing for the pragmas
1731 -- takes care of the required delay.
1733 when Pre_Post_Aspects => declare
1737 if A_Id = Aspect_Pre or else A_Id = Aspect_Precondition then
1738 Pname := Name_Precondition;
1740 Pname := Name_Postcondition;
1743 -- If the expressions is of the form A and then B, then
1744 -- we generate separate Pre/Post aspects for the separate
1745 -- clauses. Since we allow multiple pragmas, there is no
1746 -- problem in allowing multiple Pre/Post aspects internally.
1747 -- These should be treated in reverse order (B first and
1748 -- A second) since they are later inserted just after N in
1749 -- the order they are treated. This way, the pragma for A
1750 -- ends up preceding the pragma for B, which may have an
1751 -- importance for the error raised (either constraint error
1752 -- or precondition error).
1754 -- We do not do this for Pre'Class, since we have to put
1755 -- these conditions together in a complex OR expression
1757 -- We do not do this in ASIS mode, as ASIS relies on the
1758 -- original node representing the complete expression, when
1759 -- retrieving it through the source aspect table.
1762 and then (Pname = Name_Postcondition
1763 or else not Class_Present (Aspect))
1765 while Nkind (Expr) = N_And_Then loop
1766 Insert_After (Aspect,
1767 Make_Aspect_Specification (Sloc (Left_Opnd (Expr)),
1768 Identifier => Identifier (Aspect),
1769 Expression => Relocate_Node (Left_Opnd (Expr)),
1770 Class_Present => Class_Present (Aspect),
1771 Split_PPC => True));
1772 Rewrite (Expr, Relocate_Node (Right_Opnd (Expr)));
1773 Eloc := Sloc (Expr);
1777 -- Build the precondition/postcondition pragma
1780 (Pragma_Argument_Associations => New_List (
1781 Make_Pragma_Argument_Association (Eloc,
1782 Chars => Name_Check,
1783 Expression => Relocate_Node (Expr))),
1784 Pragma_Name => Pname);
1786 -- Add message unless exception messages are suppressed
1788 if not Opt.Exception_Locations_Suppressed then
1789 Append_To (Pragma_Argument_Associations (Aitem),
1790 Make_Pragma_Argument_Association (Eloc,
1791 Chars => Name_Message,
1793 Make_String_Literal (Eloc,
1795 & Get_Name_String (Pname)
1797 & Build_Location_String (Eloc))));
1800 Set_Is_Delayed_Aspect (Aspect);
1802 -- For Pre/Post cases, insert immediately after the entity
1803 -- declaration, since that is the required pragma placement.
1804 -- Note that for these aspects, we do not have to worry
1805 -- about delay issues, since the pragmas themselves deal
1806 -- with delay of visibility for the expression analysis.
1808 Insert_Delayed_Pragma (Aitem);
1814 when Aspect_Test_Case => Test_Case : declare
1816 Comp_Expr : Node_Id;
1817 Comp_Assn : Node_Id;
1823 if Nkind (Parent (N)) = N_Compilation_Unit then
1824 Error_Msg_Name_1 := Nam;
1825 Error_Msg_N ("incorrect placement of aspect `%`", E);
1829 if Nkind (Expr) /= N_Aggregate then
1830 Error_Msg_Name_1 := Nam;
1832 ("wrong syntax for aspect `%` for &", Id, E);
1836 -- Make pragma expressions refer to the original aspect
1837 -- expressions through the Original_Node link. This is
1838 -- used in semantic analysis for ASIS mode, so that the
1839 -- original expression also gets analyzed.
1841 Comp_Expr := First (Expressions (Expr));
1842 while Present (Comp_Expr) loop
1843 New_Expr := Relocate_Node (Comp_Expr);
1844 Set_Original_Node (New_Expr, Comp_Expr);
1846 Make_Pragma_Argument_Association (Sloc (Comp_Expr),
1847 Expression => New_Expr));
1851 Comp_Assn := First (Component_Associations (Expr));
1852 while Present (Comp_Assn) loop
1853 if List_Length (Choices (Comp_Assn)) /= 1
1855 Nkind (First (Choices (Comp_Assn))) /= N_Identifier
1857 Error_Msg_Name_1 := Nam;
1859 ("wrong syntax for aspect `%` for &", Id, E);
1863 New_Expr := Relocate_Node (Expression (Comp_Assn));
1864 Set_Original_Node (New_Expr, Expression (Comp_Assn));
1866 Make_Pragma_Argument_Association (Sloc (Comp_Assn),
1867 Chars => Chars (First (Choices (Comp_Assn))),
1868 Expression => New_Expr));
1872 -- Build the test-case pragma
1875 (Pragma_Argument_Associations => Args,
1876 Pragma_Name => Nam);
1878 Delay_Required := False;
1883 when Aspect_Contract_Cases => Contract_Cases : declare
1884 Case_Guard : Node_Id;
1886 Others_Seen : Boolean := False;
1887 Post_Case : Node_Id;
1890 if Nkind (Parent (N)) = N_Compilation_Unit then
1891 Error_Msg_Name_1 := Nam;
1892 Error_Msg_N ("incorrect placement of aspect `%`", E);
1896 if Nkind (Expr) /= N_Aggregate then
1897 Error_Msg_Name_1 := Nam;
1899 ("wrong syntax for aspect `%` for &", Id, E);
1903 -- Verify the legality of individual post cases
1905 Post_Case := First (Component_Associations (Expr));
1906 while Present (Post_Case) loop
1907 if Nkind (Post_Case) /= N_Component_Association then
1908 Error_Msg_N ("wrong syntax in post case", Post_Case);
1912 -- Each post case must have exactly one case guard
1914 Case_Guard := First (Choices (Post_Case));
1915 Extra := Next (Case_Guard);
1917 if Present (Extra) then
1919 ("post case may have only one case guard", Extra);
1923 -- Check the placement of "others" (if available)
1925 if Nkind (Case_Guard) = N_Others_Choice then
1927 Error_Msg_Name_1 := Nam;
1929 ("only one others choice allowed in aspect %",
1933 Others_Seen := True;
1936 elsif Others_Seen then
1937 Error_Msg_Name_1 := Nam;
1939 ("others must be the last choice in aspect %", N);
1946 -- Transform the aspect into a pragma
1949 (Pragma_Argument_Associations => New_List (
1950 Make_Pragma_Argument_Association (Loc,
1951 Expression => Relocate_Node (Expr))),
1952 Pragma_Name => Nam);
1954 Delay_Required := False;
1957 -- Case 5: Special handling for aspects with an optional
1958 -- boolean argument.
1960 -- In the general case, the corresponding pragma cannot be
1961 -- generated yet because the evaluation of the boolean needs
1962 -- to be delayed till the freeze point.
1966 when Boolean_Aspects |
1967 Library_Unit_Aspects =>
1969 Set_Is_Boolean_Aspect (Aspect);
1971 -- Lock_Free aspect only apply to protected objects
1973 if A_Id = Aspect_Lock_Free then
1974 if Ekind (E) /= E_Protected_Type then
1975 Error_Msg_Name_1 := Nam;
1977 ("aspect % only applies to a protected object",
1981 -- Set the Uses_Lock_Free flag to True if there is no
1982 -- expression or if the expression is True. ??? The
1983 -- evaluation of this aspect should be delayed to the
1987 or else Is_True (Static_Boolean (Expr))
1989 Set_Uses_Lock_Free (E);
1992 Record_Rep_Item (E, Aspect);
1997 elsif A_Id = Aspect_Import or else A_Id = Aspect_Export then
1999 -- Verify that there is an aspect Convention that will
2000 -- incorporate the Import/Export aspect, and eventual
2001 -- Link/External names.
2008 while Present (A) loop
2009 exit when Chars (Identifier (A)) = Name_Convention;
2015 ("missing Convention aspect for Export/Import",
2023 -- This requires special handling in the case of a package
2024 -- declaration, the pragma needs to be inserted in the list
2025 -- of declarations for the associated package. There is no
2026 -- issue of visibility delay for these aspects.
2028 if A_Id in Library_Unit_Aspects
2029 and then Nkind (N) = N_Package_Declaration
2030 and then Nkind (Parent (N)) /= N_Compilation_Unit
2033 ("incorrect context for library unit aspect&", Id);
2037 -- Special handling when the aspect has no expression. In
2038 -- this case the value is considered to be True. Thus, we
2039 -- simply insert the pragma, no delay is required.
2043 (Pragma_Argument_Associations => New_List (
2044 Make_Pragma_Argument_Association (Sloc (Ent),
2045 Expression => Ent)),
2046 Pragma_Name => Chars (Id));
2048 Delay_Required := False;
2050 -- In general cases, the corresponding pragma/attribute
2051 -- definition clause will be inserted later at the freezing
2059 -- Attach the corresponding pragma/attribute definition clause to
2060 -- the aspect specification node.
2062 if Present (Aitem) then
2063 Set_From_Aspect_Specification (Aitem, True);
2066 -- Aspect Abstract_State introduces implicit declarations for all
2067 -- state abstraction entities it defines. To emulate this behavior
2068 -- insert the pragma at the start of the visible declarations of
2069 -- the related package.
2071 if Nam = Name_Abstract_State
2072 and then Nkind (N) = N_Package_Declaration
2074 if No (Visible_Declarations (Specification (N))) then
2075 Set_Visible_Declarations (Specification (N), New_List);
2078 Prepend (Aitem, Visible_Declarations (Specification (N)));
2081 -- In the context of a compilation unit, we directly put the
2082 -- pragma in the Pragmas_After list of the N_Compilation_Unit_Aux
2083 -- node (no delay is required here) except for aspects on a
2084 -- subprogram body (see below).
2086 elsif Nkind (Parent (N)) = N_Compilation_Unit
2087 and then (Present (Aitem) or else Is_Boolean_Aspect (Aspect))
2090 Aux : constant Node_Id := Aux_Decls_Node (Parent (N));
2093 pragma Assert (Nkind (Aux) = N_Compilation_Unit_Aux);
2095 -- For a Boolean aspect, create the corresponding pragma if
2096 -- no expression or if the value is True.
2098 if Is_Boolean_Aspect (Aspect) and then No (Aitem) then
2099 if Is_True (Static_Boolean (Expr)) then
2101 (Pragma_Argument_Associations => New_List (
2102 Make_Pragma_Argument_Association (Sloc (Ent),
2103 Expression => Ent)),
2104 Pragma_Name => Chars (Id));
2106 Set_From_Aspect_Specification (Aitem, True);
2107 Set_Corresponding_Aspect (Aitem, Aspect);
2114 -- If the aspect is on a subprogram body (relevant aspects
2115 -- are Inline and Priority), add the pragma in front of
2116 -- the declarations.
2118 if Nkind (N) = N_Subprogram_Body then
2119 if No (Declarations (N)) then
2120 Set_Declarations (N, New_List);
2123 Prepend (Aitem, Declarations (N));
2126 if No (Pragmas_After (Aux)) then
2127 Set_Pragmas_After (Aux, New_List);
2130 Append (Aitem, Pragmas_After (Aux));
2137 -- The evaluation of the aspect is delayed to the freezing point.
2138 -- The pragma or attribute clause if there is one is then attached
2139 -- to the aspect specification which is placed in the rep item
2142 if Delay_Required then
2143 if Present (Aitem) then
2144 Set_Is_Delayed_Aspect (Aitem);
2145 Set_Aspect_Rep_Item (Aspect, Aitem);
2146 Set_Parent (Aitem, Aspect);
2149 Set_Is_Delayed_Aspect (Aspect);
2151 -- In the case of Default_Value, link the aspect to base type
2152 -- as well, even though it appears on a first subtype. This is
2153 -- mandated by the semantics of the aspect. Do not establish
2154 -- the link when processing the base type itself as this leads
2155 -- to a rep item circularity. Verify that we are dealing with
2156 -- a scalar type to prevent cascaded errors.
2158 if A_Id = Aspect_Default_Value
2159 and then Is_Scalar_Type (E)
2160 and then Base_Type (E) /= E
2162 Set_Has_Delayed_Aspects (Base_Type (E));
2163 Record_Rep_Item (Base_Type (E), Aspect);
2166 Set_Has_Delayed_Aspects (E);
2167 Record_Rep_Item (E, Aspect);
2169 -- When delay is not required and the context is not a compilation
2170 -- unit, we simply insert the pragma/attribute definition clause
2174 Insert_After (Ins_Node, Aitem);
2177 end Analyze_One_Aspect;
2181 end loop Aspect_Loop;
2183 if Has_Delayed_Aspects (E) then
2184 Ensure_Freeze_Node (E);
2186 end Analyze_Aspect_Specifications;
2188 -----------------------
2189 -- Analyze_At_Clause --
2190 -----------------------
2192 -- An at clause is replaced by the corresponding Address attribute
2193 -- definition clause that is the preferred approach in Ada 95.
2195 procedure Analyze_At_Clause (N : Node_Id) is
2196 CS : constant Boolean := Comes_From_Source (N);
2199 -- This is an obsolescent feature
2201 Check_Restriction (No_Obsolescent_Features, N);
2203 if Warn_On_Obsolescent_Feature then
2205 ("?j?at clause is an obsolescent feature (RM J.7(2))", N);
2207 ("\?j?use address attribute definition clause instead", N);
2210 -- Rewrite as address clause
2213 Make_Attribute_Definition_Clause (Sloc (N),
2214 Name => Identifier (N),
2215 Chars => Name_Address,
2216 Expression => Expression (N)));
2218 -- We preserve Comes_From_Source, since logically the clause still comes
2219 -- from the source program even though it is changed in form.
2221 Set_Comes_From_Source (N, CS);
2223 -- Analyze rewritten clause
2225 Analyze_Attribute_Definition_Clause (N);
2226 end Analyze_At_Clause;
2228 -----------------------------------------
2229 -- Analyze_Attribute_Definition_Clause --
2230 -----------------------------------------
2232 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
2233 Loc : constant Source_Ptr := Sloc (N);
2234 Nam : constant Node_Id := Name (N);
2235 Attr : constant Name_Id := Chars (N);
2236 Expr : constant Node_Id := Expression (N);
2237 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
2240 -- The entity of Nam after it is analyzed. In the case of an incomplete
2241 -- type, this is the underlying type.
2244 -- The underlying entity to which the attribute applies. Generally this
2245 -- is the Underlying_Type of Ent, except in the case where the clause
2246 -- applies to full view of incomplete type or private type in which case
2247 -- U_Ent is just a copy of Ent.
2249 FOnly : Boolean := False;
2250 -- Reset to True for subtype specific attribute (Alignment, Size)
2251 -- and for stream attributes, i.e. those cases where in the call
2252 -- to Rep_Item_Too_Late, FOnly is set True so that only the freezing
2253 -- rules are checked. Note that the case of stream attributes is not
2254 -- clear from the RM, but see AI95-00137. Also, the RM seems to
2255 -- disallow Storage_Size for derived task types, but that is also
2256 -- clearly unintentional.
2258 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
2259 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
2260 -- definition clauses.
2262 function Duplicate_Clause return Boolean;
2263 -- This routine checks if the aspect for U_Ent being given by attribute
2264 -- definition clause N is for an aspect that has already been specified,
2265 -- and if so gives an error message. If there is a duplicate, True is
2266 -- returned, otherwise if there is no error, False is returned.
2268 procedure Check_Indexing_Functions;
2269 -- Check that the function in Constant_Indexing or Variable_Indexing
2270 -- attribute has the proper type structure. If the name is overloaded,
2271 -- check that some interpretation is legal.
2273 procedure Check_Iterator_Functions;
2274 -- Check that there is a single function in Default_Iterator attribute
2275 -- has the proper type structure.
2277 function Check_Primitive_Function (Subp : Entity_Id) return Boolean;
2278 -- Common legality check for the previous two
2280 -----------------------------------
2281 -- Analyze_Stream_TSS_Definition --
2282 -----------------------------------
2284 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
2285 Subp : Entity_Id := Empty;
2290 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
2291 -- True for Read attribute, false for other attributes
2293 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
2294 -- Return true if the entity is a subprogram with an appropriate
2295 -- profile for the attribute being defined.
2297 ----------------------
2298 -- Has_Good_Profile --
2299 ----------------------
2301 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
2303 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
2304 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
2305 (False => E_Procedure, True => E_Function);
2309 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
2313 F := First_Formal (Subp);
2316 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
2317 or else Designated_Type (Etype (F)) /=
2318 Class_Wide_Type (RTE (RE_Root_Stream_Type))
2323 if not Is_Function then
2327 Expected_Mode : constant array (Boolean) of Entity_Kind :=
2328 (False => E_In_Parameter,
2329 True => E_Out_Parameter);
2331 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
2339 Typ := Etype (Subp);
2342 return Base_Type (Typ) = Base_Type (Ent)
2343 and then No (Next_Formal (F));
2344 end Has_Good_Profile;
2346 -- Start of processing for Analyze_Stream_TSS_Definition
2351 if not Is_Type (U_Ent) then
2352 Error_Msg_N ("local name must be a subtype", Nam);
2356 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
2358 -- If Pnam is present, it can be either inherited from an ancestor
2359 -- type (in which case it is legal to redefine it for this type), or
2360 -- be a previous definition of the attribute for the same type (in
2361 -- which case it is illegal).
2363 -- In the first case, it will have been analyzed already, and we
2364 -- can check that its profile does not match the expected profile
2365 -- for a stream attribute of U_Ent. In the second case, either Pnam
2366 -- has been analyzed (and has the expected profile), or it has not
2367 -- been analyzed yet (case of a type that has not been frozen yet
2368 -- and for which the stream attribute has been set using Set_TSS).
2371 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
2373 Error_Msg_Sloc := Sloc (Pnam);
2374 Error_Msg_Name_1 := Attr;
2375 Error_Msg_N ("% attribute already defined #", Nam);
2381 if Is_Entity_Name (Expr) then
2382 if not Is_Overloaded (Expr) then
2383 if Has_Good_Profile (Entity (Expr)) then
2384 Subp := Entity (Expr);
2388 Get_First_Interp (Expr, I, It);
2389 while Present (It.Nam) loop
2390 if Has_Good_Profile (It.Nam) then
2395 Get_Next_Interp (I, It);
2400 if Present (Subp) then
2401 if Is_Abstract_Subprogram (Subp) then
2402 Error_Msg_N ("stream subprogram must not be abstract", Expr);
2406 Set_Entity (Expr, Subp);
2407 Set_Etype (Expr, Etype (Subp));
2409 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
2412 Error_Msg_Name_1 := Attr;
2413 Error_Msg_N ("incorrect expression for% attribute", Expr);
2415 end Analyze_Stream_TSS_Definition;
2417 ------------------------------
2418 -- Check_Indexing_Functions --
2419 ------------------------------
2421 procedure Check_Indexing_Functions is
2422 Indexing_Found : Boolean;
2424 procedure Check_One_Function (Subp : Entity_Id);
2425 -- Check one possible interpretation. Sets Indexing_Found True if an
2426 -- indexing function is found.
2428 ------------------------
2429 -- Check_One_Function --
2430 ------------------------
2432 procedure Check_One_Function (Subp : Entity_Id) is
2433 Default_Element : constant Node_Id :=
2434 Find_Value_Of_Aspect
2435 (Etype (First_Formal (Subp)),
2436 Aspect_Iterator_Element);
2439 if not Check_Primitive_Function (Subp)
2440 and then not Is_Overloaded (Expr)
2443 ("aspect Indexing requires a function that applies to type&",
2447 -- An indexing function must return either the default element of
2448 -- the container, or a reference type. For variable indexing it
2449 -- must be the latter.
2451 if Present (Default_Element) then
2452 Analyze (Default_Element);
2454 if Is_Entity_Name (Default_Element)
2455 and then Covers (Entity (Default_Element), Etype (Subp))
2457 Indexing_Found := True;
2462 -- For variable_indexing the return type must be a reference type
2464 if Attr = Name_Variable_Indexing
2465 and then not Has_Implicit_Dereference (Etype (Subp))
2468 ("function for indexing must return a reference type", Subp);
2471 Indexing_Found := True;
2473 end Check_One_Function;
2475 -- Start of processing for Check_Indexing_Functions
2484 if not Is_Overloaded (Expr) then
2485 Check_One_Function (Entity (Expr));
2493 Indexing_Found := False;
2494 Get_First_Interp (Expr, I, It);
2495 while Present (It.Nam) loop
2497 -- Note that analysis will have added the interpretation
2498 -- that corresponds to the dereference. We only check the
2499 -- subprogram itself.
2501 if Is_Overloadable (It.Nam) then
2502 Check_One_Function (It.Nam);
2505 Get_Next_Interp (I, It);
2508 if not Indexing_Found then
2510 ("aspect Indexing requires a function that "
2511 & "applies to type&", Expr, Ent);
2515 end Check_Indexing_Functions;
2517 ------------------------------
2518 -- Check_Iterator_Functions --
2519 ------------------------------
2521 procedure Check_Iterator_Functions is
2522 Default : Entity_Id;
2524 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean;
2525 -- Check one possible interpretation for validity
2527 ----------------------------
2528 -- Valid_Default_Iterator --
2529 ----------------------------
2531 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean is
2535 if not Check_Primitive_Function (Subp) then
2538 Formal := First_Formal (Subp);
2541 -- False if any subsequent formal has no default expression
2543 Formal := Next_Formal (Formal);
2544 while Present (Formal) loop
2545 if No (Expression (Parent (Formal))) then
2549 Next_Formal (Formal);
2552 -- True if all subsequent formals have default expressions
2555 end Valid_Default_Iterator;
2557 -- Start of processing for Check_Iterator_Functions
2562 if not Is_Entity_Name (Expr) then
2563 Error_Msg_N ("aspect Iterator must be a function name", Expr);
2566 if not Is_Overloaded (Expr) then
2567 if not Check_Primitive_Function (Entity (Expr)) then
2569 ("aspect Indexing requires a function that applies to type&",
2570 Entity (Expr), Ent);
2573 if not Valid_Default_Iterator (Entity (Expr)) then
2574 Error_Msg_N ("improper function for default iterator", Expr);
2584 Get_First_Interp (Expr, I, It);
2585 while Present (It.Nam) loop
2586 if not Check_Primitive_Function (It.Nam)
2587 or else not Valid_Default_Iterator (It.Nam)
2591 elsif Present (Default) then
2592 Error_Msg_N ("default iterator must be unique", Expr);
2598 Get_Next_Interp (I, It);
2602 if Present (Default) then
2603 Set_Entity (Expr, Default);
2604 Set_Is_Overloaded (Expr, False);
2607 end Check_Iterator_Functions;
2609 -------------------------------
2610 -- Check_Primitive_Function --
2611 -------------------------------
2613 function Check_Primitive_Function (Subp : Entity_Id) return Boolean is
2617 if Ekind (Subp) /= E_Function then
2621 if No (First_Formal (Subp)) then
2624 Ctrl := Etype (First_Formal (Subp));
2628 or else Ctrl = Class_Wide_Type (Ent)
2630 (Ekind (Ctrl) = E_Anonymous_Access_Type
2632 (Designated_Type (Ctrl) = Ent
2633 or else Designated_Type (Ctrl) = Class_Wide_Type (Ent)))
2642 end Check_Primitive_Function;
2644 ----------------------
2645 -- Duplicate_Clause --
2646 ----------------------
2648 function Duplicate_Clause return Boolean is
2652 -- Nothing to do if this attribute definition clause comes from
2653 -- an aspect specification, since we could not be duplicating an
2654 -- explicit clause, and we dealt with the case of duplicated aspects
2655 -- in Analyze_Aspect_Specifications.
2657 if From_Aspect_Specification (N) then
2661 -- Otherwise current clause may duplicate previous clause, or a
2662 -- previously given pragma or aspect specification for the same
2665 A := Get_Rep_Item (U_Ent, Chars (N), Check_Parents => False);
2668 Error_Msg_Name_1 := Chars (N);
2669 Error_Msg_Sloc := Sloc (A);
2671 Error_Msg_NE ("aspect% for & previously given#", N, U_Ent);
2676 end Duplicate_Clause;
2678 -- Start of processing for Analyze_Attribute_Definition_Clause
2681 -- The following code is a defense against recursion. Not clear that
2682 -- this can happen legitimately, but perhaps some error situations
2683 -- can cause it, and we did see this recursion during testing.
2685 if Analyzed (N) then
2688 Set_Analyzed (N, True);
2691 -- Ignore some selected attributes in CodePeer mode since they are not
2692 -- relevant in this context.
2694 if CodePeer_Mode then
2697 -- Ignore Component_Size in CodePeer mode, to avoid changing the
2698 -- internal representation of types by implicitly packing them.
2700 when Attribute_Component_Size =>
2701 Rewrite (N, Make_Null_Statement (Sloc (N)));
2709 -- Process Ignore_Rep_Clauses option
2711 if Ignore_Rep_Clauses then
2714 -- The following should be ignored. They do not affect legality
2715 -- and may be target dependent. The basic idea of -gnatI is to
2716 -- ignore any rep clauses that may be target dependent but do not
2717 -- affect legality (except possibly to be rejected because they
2718 -- are incompatible with the compilation target).
2720 when Attribute_Alignment |
2721 Attribute_Bit_Order |
2722 Attribute_Component_Size |
2723 Attribute_Machine_Radix |
2724 Attribute_Object_Size |
2726 Attribute_Stream_Size |
2727 Attribute_Value_Size =>
2728 Rewrite (N, Make_Null_Statement (Sloc (N)));
2731 -- Perhaps 'Small should not be ignored by Ignore_Rep_Clauses ???
2733 when Attribute_Small =>
2734 if Ignore_Rep_Clauses then
2735 Rewrite (N, Make_Null_Statement (Sloc (N)));
2739 -- The following should not be ignored, because in the first place
2740 -- they are reasonably portable, and should not cause problems in
2741 -- compiling code from another target, and also they do affect
2742 -- legality, e.g. failing to provide a stream attribute for a
2743 -- type may make a program illegal.
2745 when Attribute_External_Tag |
2749 Attribute_Simple_Storage_Pool |
2750 Attribute_Storage_Pool |
2751 Attribute_Storage_Size |
2755 -- Other cases are errors ("attribute& cannot be set with
2756 -- definition clause"), which will be caught below.
2764 Ent := Entity (Nam);
2766 if Rep_Item_Too_Early (Ent, N) then
2770 -- Rep clause applies to full view of incomplete type or private type if
2771 -- we have one (if not, this is a premature use of the type). However,
2772 -- certain semantic checks need to be done on the specified entity (i.e.
2773 -- the private view), so we save it in Ent.
2775 if Is_Private_Type (Ent)
2776 and then Is_Derived_Type (Ent)
2777 and then not Is_Tagged_Type (Ent)
2778 and then No (Full_View (Ent))
2780 -- If this is a private type whose completion is a derivation from
2781 -- another private type, there is no full view, and the attribute
2782 -- belongs to the type itself, not its underlying parent.
2786 elsif Ekind (Ent) = E_Incomplete_Type then
2788 -- The attribute applies to the full view, set the entity of the
2789 -- attribute definition accordingly.
2791 Ent := Underlying_Type (Ent);
2793 Set_Entity (Nam, Ent);
2796 U_Ent := Underlying_Type (Ent);
2799 -- Avoid cascaded error
2801 if Etype (Nam) = Any_Type then
2804 -- Must be declared in current scope or in case of an aspect
2805 -- specification, must be visible in current scope.
2807 elsif Scope (Ent) /= Current_Scope
2809 not (From_Aspect_Specification (N)
2810 and then Scope_Within_Or_Same (Current_Scope, Scope (Ent)))
2812 Error_Msg_N ("entity must be declared in this scope", Nam);
2815 -- Must not be a source renaming (we do have some cases where the
2816 -- expander generates a renaming, and those cases are OK, in such
2817 -- cases any attribute applies to the renamed object as well).
2819 elsif Is_Object (Ent)
2820 and then Present (Renamed_Object (Ent))
2822 -- Case of renamed object from source, this is an error
2824 if Comes_From_Source (Renamed_Object (Ent)) then
2825 Get_Name_String (Chars (N));
2826 Error_Msg_Strlen := Name_Len;
2827 Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
2829 ("~ clause not allowed for a renaming declaration "
2830 & "(RM 13.1(6))", Nam);
2833 -- For the case of a compiler generated renaming, the attribute
2834 -- definition clause applies to the renamed object created by the
2835 -- expander. The easiest general way to handle this is to create a
2836 -- copy of the attribute definition clause for this object.
2840 Make_Attribute_Definition_Clause (Loc,
2842 New_Occurrence_Of (Entity (Renamed_Object (Ent)), Loc),
2844 Expression => Duplicate_Subexpr (Expression (N))));
2847 -- If no underlying entity, use entity itself, applies to some
2848 -- previously detected error cases ???
2850 elsif No (U_Ent) then
2853 -- Cannot specify for a subtype (exception Object/Value_Size)
2855 elsif Is_Type (U_Ent)
2856 and then not Is_First_Subtype (U_Ent)
2857 and then Id /= Attribute_Object_Size
2858 and then Id /= Attribute_Value_Size
2859 and then not From_At_Mod (N)
2861 Error_Msg_N ("cannot specify attribute for subtype", Nam);
2865 Set_Entity (N, U_Ent);
2866 Check_Restriction_No_Use_Of_Attribute (N);
2868 -- Switch on particular attribute
2876 -- Address attribute definition clause
2878 when Attribute_Address => Address : begin
2880 -- A little error check, catch for X'Address use X'Address;
2882 if Nkind (Nam) = N_Identifier
2883 and then Nkind (Expr) = N_Attribute_Reference
2884 and then Attribute_Name (Expr) = Name_Address
2885 and then Nkind (Prefix (Expr)) = N_Identifier
2886 and then Chars (Nam) = Chars (Prefix (Expr))
2889 ("address for & is self-referencing", Prefix (Expr), Ent);
2893 -- Not that special case, carry on with analysis of expression
2895 Analyze_And_Resolve (Expr, RTE (RE_Address));
2897 -- Even when ignoring rep clauses we need to indicate that the
2898 -- entity has an address clause and thus it is legal to declare
2901 if Ignore_Rep_Clauses then
2902 if Ekind_In (U_Ent, E_Variable, E_Constant) then
2903 Record_Rep_Item (U_Ent, N);
2909 if Duplicate_Clause then
2912 -- Case of address clause for subprogram
2914 elsif Is_Subprogram (U_Ent) then
2915 if Has_Homonym (U_Ent) then
2917 ("address clause cannot be given " &
2918 "for overloaded subprogram",
2923 -- For subprograms, all address clauses are permitted, and we
2924 -- mark the subprogram as having a deferred freeze so that Gigi
2925 -- will not elaborate it too soon.
2927 -- Above needs more comments, what is too soon about???
2929 Set_Has_Delayed_Freeze (U_Ent);
2931 -- Case of address clause for entry
2933 elsif Ekind (U_Ent) = E_Entry then
2934 if Nkind (Parent (N)) = N_Task_Body then
2936 ("entry address must be specified in task spec", Nam);
2940 -- For entries, we require a constant address
2942 Check_Constant_Address_Clause (Expr, U_Ent);
2944 -- Special checks for task types
2946 if Is_Task_Type (Scope (U_Ent))
2947 and then Comes_From_Source (Scope (U_Ent))
2950 ("??entry address declared for entry in task type", N);
2952 ("\??only one task can be declared of this type", N);
2955 -- Entry address clauses are obsolescent
2957 Check_Restriction (No_Obsolescent_Features, N);
2959 if Warn_On_Obsolescent_Feature then
2961 ("?j?attaching interrupt to task entry is an " &
2962 "obsolescent feature (RM J.7.1)", N);
2964 ("\?j?use interrupt procedure instead", N);
2967 -- Case of an address clause for a controlled object which we
2968 -- consider to be erroneous.
2970 elsif Is_Controlled (Etype (U_Ent))
2971 or else Has_Controlled_Component (Etype (U_Ent))
2974 ("??controlled object& must not be overlaid", Nam, U_Ent);
2976 ("\??Program_Error will be raised at run time", Nam);
2977 Insert_Action (Declaration_Node (U_Ent),
2978 Make_Raise_Program_Error (Loc,
2979 Reason => PE_Overlaid_Controlled_Object));
2982 -- Case of address clause for a (non-controlled) object
2985 Ekind (U_Ent) = E_Variable
2987 Ekind (U_Ent) = E_Constant
2990 Expr : constant Node_Id := Expression (N);
2995 -- Exported variables cannot have an address clause, because
2996 -- this cancels the effect of the pragma Export.
2998 if Is_Exported (U_Ent) then
3000 ("cannot export object with address clause", Nam);
3004 Find_Overlaid_Entity (N, O_Ent, Off);
3006 -- Overlaying controlled objects is erroneous
3009 and then (Has_Controlled_Component (Etype (O_Ent))
3010 or else Is_Controlled (Etype (O_Ent)))
3013 ("??cannot overlay with controlled object", Expr);
3015 ("\??Program_Error will be raised at run time", Expr);
3016 Insert_Action (Declaration_Node (U_Ent),
3017 Make_Raise_Program_Error (Loc,
3018 Reason => PE_Overlaid_Controlled_Object));
3021 elsif Present (O_Ent)
3022 and then Ekind (U_Ent) = E_Constant
3023 and then not Is_Constant_Object (O_Ent)
3025 Error_Msg_N ("??constant overlays a variable", Expr);
3027 -- Imported variables can have an address clause, but then
3028 -- the import is pretty meaningless except to suppress
3029 -- initializations, so we do not need such variables to
3030 -- be statically allocated (and in fact it causes trouble
3031 -- if the address clause is a local value).
3033 elsif Is_Imported (U_Ent) then
3034 Set_Is_Statically_Allocated (U_Ent, False);
3037 -- We mark a possible modification of a variable with an
3038 -- address clause, since it is likely aliasing is occurring.
3040 Note_Possible_Modification (Nam, Sure => False);
3042 -- Here we are checking for explicit overlap of one variable
3043 -- by another, and if we find this then mark the overlapped
3044 -- variable as also being volatile to prevent unwanted
3045 -- optimizations. This is a significant pessimization so
3046 -- avoid it when there is an offset, i.e. when the object
3047 -- is composite; they cannot be optimized easily anyway.
3050 and then Is_Object (O_Ent)
3053 -- The following test is an expedient solution to what
3054 -- is really a problem in CodePeer. Suppressing the
3055 -- Set_Treat_As_Volatile call here prevents later
3056 -- generation (in some cases) of trees that CodePeer
3057 -- should, but currently does not, handle correctly.
3058 -- This test should probably be removed when CodePeer
3059 -- is improved, just because we want the tree CodePeer
3060 -- analyzes to match the tree for which we generate code
3061 -- as closely as is practical. ???
3063 and then not CodePeer_Mode
3065 -- ??? O_Ent might not be in current unit
3067 Set_Treat_As_Volatile (O_Ent);
3070 -- Legality checks on the address clause for initialized
3071 -- objects is deferred until the freeze point, because
3072 -- a subsequent pragma might indicate that the object
3073 -- is imported and thus not initialized. Also, the address
3074 -- clause might involve entities that have yet to be
3077 Set_Has_Delayed_Freeze (U_Ent);
3079 -- If an initialization call has been generated for this
3080 -- object, it needs to be deferred to after the freeze node
3081 -- we have just now added, otherwise GIGI will see a
3082 -- reference to the variable (as actual to the IP call)
3083 -- before its definition.
3086 Init_Call : constant Node_Id :=
3087 Remove_Init_Call (U_Ent, N);
3090 if Present (Init_Call) then
3092 -- If the init call is an expression with actions with
3093 -- null expression, just extract the actions.
3095 if Nkind (Init_Call) = N_Expression_With_Actions
3097 Nkind (Expression (Init_Call)) = N_Null_Statement
3099 Append_Freeze_Actions (U_Ent, Actions (Init_Call));
3101 -- General case: move Init_Call to freeze actions
3104 Append_Freeze_Action (U_Ent, Init_Call);
3109 if Is_Exported (U_Ent) then
3111 ("& cannot be exported if an address clause is given",
3114 ("\define and export a variable "
3115 & "that holds its address instead", Nam);
3118 -- Entity has delayed freeze, so we will generate an
3119 -- alignment check at the freeze point unless suppressed.
3121 if not Range_Checks_Suppressed (U_Ent)
3122 and then not Alignment_Checks_Suppressed (U_Ent)
3124 Set_Check_Address_Alignment (N);
3127 -- Kill the size check code, since we are not allocating
3128 -- the variable, it is somewhere else.
3130 Kill_Size_Check_Code (U_Ent);
3132 -- If the address clause is of the form:
3134 -- for Y'Address use X'Address
3138 -- Const : constant Address := X'Address;
3140 -- for Y'Address use Const;
3142 -- then we make an entry in the table for checking the size
3143 -- and alignment of the overlaying variable. We defer this
3144 -- check till after code generation to take full advantage
3145 -- of the annotation done by the back end. This entry is
3146 -- only made if the address clause comes from source.
3148 -- If the entity has a generic type, the check will be
3149 -- performed in the instance if the actual type justifies
3150 -- it, and we do not insert the clause in the table to
3151 -- prevent spurious warnings.
3153 if Address_Clause_Overlay_Warnings
3154 and then Comes_From_Source (N)
3155 and then Present (O_Ent)
3156 and then Is_Object (O_Ent)
3158 if not Is_Generic_Type (Etype (U_Ent)) then
3159 Address_Clause_Checks.Append ((N, U_Ent, O_Ent, Off));
3162 -- If variable overlays a constant view, and we are
3163 -- warning on overlays, then mark the variable as
3164 -- overlaying a constant (we will give warnings later
3165 -- if this variable is assigned).
3167 if Is_Constant_Object (O_Ent)
3168 and then Ekind (U_Ent) = E_Variable
3170 Set_Overlays_Constant (U_Ent);
3175 -- Not a valid entity for an address clause
3178 Error_Msg_N ("address cannot be given for &", Nam);
3186 -- Alignment attribute definition clause
3188 when Attribute_Alignment => Alignment : declare
3189 Align : constant Uint := Get_Alignment_Value (Expr);
3190 Max_Align : constant Uint := UI_From_Int (Maximum_Alignment);
3195 if not Is_Type (U_Ent)
3196 and then Ekind (U_Ent) /= E_Variable
3197 and then Ekind (U_Ent) /= E_Constant
3199 Error_Msg_N ("alignment cannot be given for &", Nam);
3201 elsif Duplicate_Clause then
3204 elsif Align /= No_Uint then
3205 Set_Has_Alignment_Clause (U_Ent);
3207 -- Tagged type case, check for attempt to set alignment to a
3208 -- value greater than Max_Align, and reset if so.
3210 if Is_Tagged_Type (U_Ent) and then Align > Max_Align then
3212 ("alignment for & set to Maximum_Aligment??", Nam);
3213 Set_Alignment (U_Ent, Max_Align);
3218 Set_Alignment (U_Ent, Align);
3221 -- For an array type, U_Ent is the first subtype. In that case,
3222 -- also set the alignment of the anonymous base type so that
3223 -- other subtypes (such as the itypes for aggregates of the
3224 -- type) also receive the expected alignment.
3226 if Is_Array_Type (U_Ent) then
3227 Set_Alignment (Base_Type (U_Ent), Align);
3236 -- Bit_Order attribute definition clause
3238 when Attribute_Bit_Order => Bit_Order : declare
3240 if not Is_Record_Type (U_Ent) then
3242 ("Bit_Order can only be defined for record type", Nam);
3244 elsif Duplicate_Clause then
3248 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
3250 if Etype (Expr) = Any_Type then
3253 elsif not Is_Static_Expression (Expr) then
3254 Flag_Non_Static_Expr
3255 ("Bit_Order requires static expression!", Expr);
3258 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
3259 Set_Reverse_Bit_Order (U_Ent, True);
3265 --------------------
3266 -- Component_Size --
3267 --------------------
3269 -- Component_Size attribute definition clause
3271 when Attribute_Component_Size => Component_Size_Case : declare
3272 Csize : constant Uint := Static_Integer (Expr);
3276 New_Ctyp : Entity_Id;
3280 if not Is_Array_Type (U_Ent) then
3281 Error_Msg_N ("component size requires array type", Nam);
3285 Btype := Base_Type (U_Ent);
3286 Ctyp := Component_Type (Btype);
3288 if Duplicate_Clause then
3291 elsif Rep_Item_Too_Early (Btype, N) then
3294 elsif Csize /= No_Uint then
3295 Check_Size (Expr, Ctyp, Csize, Biased);
3297 -- For the biased case, build a declaration for a subtype that
3298 -- will be used to represent the biased subtype that reflects
3299 -- the biased representation of components. We need the subtype
3300 -- to get proper conversions on referencing elements of the
3301 -- array. Note: component size clauses are ignored in VM mode.
3303 if VM_Target = No_VM then
3306 Make_Defining_Identifier (Loc,
3308 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
3311 Make_Subtype_Declaration (Loc,
3312 Defining_Identifier => New_Ctyp,
3313 Subtype_Indication =>
3314 New_Occurrence_Of (Component_Type (Btype), Loc));
3316 Set_Parent (Decl, N);
3317 Analyze (Decl, Suppress => All_Checks);
3319 Set_Has_Delayed_Freeze (New_Ctyp, False);
3320 Set_Esize (New_Ctyp, Csize);
3321 Set_RM_Size (New_Ctyp, Csize);
3322 Init_Alignment (New_Ctyp);
3323 Set_Is_Itype (New_Ctyp, True);
3324 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
3326 Set_Component_Type (Btype, New_Ctyp);
3327 Set_Biased (New_Ctyp, N, "component size clause");
3330 Set_Component_Size (Btype, Csize);
3332 -- For VM case, we ignore component size clauses
3335 -- Give a warning unless we are in GNAT mode, in which case
3336 -- the warning is suppressed since it is not useful.
3338 if not GNAT_Mode then
3340 ("component size ignored in this configuration??", N);
3344 -- Deal with warning on overridden size
3346 if Warn_On_Overridden_Size
3347 and then Has_Size_Clause (Ctyp)
3348 and then RM_Size (Ctyp) /= Csize
3351 ("component size overrides size clause for&?S?", N, Ctyp);
3354 Set_Has_Component_Size_Clause (Btype, True);
3355 Set_Has_Non_Standard_Rep (Btype, True);
3357 end Component_Size_Case;
3359 -----------------------
3360 -- Constant_Indexing --
3361 -----------------------
3363 when Attribute_Constant_Indexing =>
3364 Check_Indexing_Functions;
3370 when Attribute_CPU => CPU :
3372 -- CPU attribute definition clause not allowed except from aspect
3375 if From_Aspect_Specification (N) then
3376 if not Is_Task_Type (U_Ent) then
3377 Error_Msg_N ("CPU can only be defined for task", Nam);
3379 elsif Duplicate_Clause then
3383 -- The expression must be analyzed in the special manner
3384 -- described in "Handling of Default and Per-Object
3385 -- Expressions" in sem.ads.
3387 -- The visibility to the discriminants must be restored
3389 Push_Scope_And_Install_Discriminants (U_Ent);
3390 Preanalyze_Spec_Expression (Expr, RTE (RE_CPU_Range));
3391 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
3393 if not Is_Static_Expression (Expr) then
3394 Check_Restriction (Static_Priorities, Expr);
3400 ("attribute& cannot be set with definition clause", N);
3404 ----------------------
3405 -- Default_Iterator --
3406 ----------------------
3408 when Attribute_Default_Iterator => Default_Iterator : declare
3412 if not Is_Tagged_Type (U_Ent) then
3414 ("aspect Default_Iterator applies to tagged type", Nam);
3417 Check_Iterator_Functions;
3421 if not Is_Entity_Name (Expr)
3422 or else Ekind (Entity (Expr)) /= E_Function
3424 Error_Msg_N ("aspect Iterator must be a function", Expr);
3426 Func := Entity (Expr);
3429 if No (First_Formal (Func))
3430 or else Etype (First_Formal (Func)) /= U_Ent
3433 ("Default Iterator must be a primitive of&", Func, U_Ent);
3435 end Default_Iterator;
3437 ------------------------
3438 -- Dispatching_Domain --
3439 ------------------------
3441 when Attribute_Dispatching_Domain => Dispatching_Domain :
3443 -- Dispatching_Domain attribute definition clause not allowed
3444 -- except from aspect specification.
3446 if From_Aspect_Specification (N) then
3447 if not Is_Task_Type (U_Ent) then
3448 Error_Msg_N ("Dispatching_Domain can only be defined" &
3452 elsif Duplicate_Clause then
3456 -- The expression must be analyzed in the special manner
3457 -- described in "Handling of Default and Per-Object
3458 -- Expressions" in sem.ads.
3460 -- The visibility to the discriminants must be restored
3462 Push_Scope_And_Install_Discriminants (U_Ent);
3464 Preanalyze_Spec_Expression
3465 (Expr, RTE (RE_Dispatching_Domain));
3467 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
3472 ("attribute& cannot be set with definition clause", N);
3474 end Dispatching_Domain;
3480 when Attribute_External_Tag => External_Tag :
3482 if not Is_Tagged_Type (U_Ent) then
3483 Error_Msg_N ("should be a tagged type", Nam);
3486 if Duplicate_Clause then
3490 Analyze_And_Resolve (Expr, Standard_String);
3492 if not Is_Static_Expression (Expr) then
3493 Flag_Non_Static_Expr
3494 ("static string required for tag name!", Nam);
3497 if VM_Target = No_VM then
3498 Set_Has_External_Tag_Rep_Clause (U_Ent);
3500 Error_Msg_Name_1 := Attr;
3502 ("% attribute unsupported in this configuration", Nam);
3505 if not Is_Library_Level_Entity (U_Ent) then
3507 ("??non-unique external tag supplied for &", N, U_Ent);
3509 ("\??same external tag applies to all "
3510 & "subprogram calls", N);
3512 ("\??corresponding internal tag cannot be obtained", N);
3517 --------------------------
3518 -- Implicit_Dereference --
3519 --------------------------
3521 when Attribute_Implicit_Dereference =>
3523 -- Legality checks already performed at the point of the type
3524 -- declaration, aspect is not delayed.
3532 when Attribute_Input =>
3533 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
3534 Set_Has_Specified_Stream_Input (Ent);
3536 ------------------------
3537 -- Interrupt_Priority --
3538 ------------------------
3540 when Attribute_Interrupt_Priority => Interrupt_Priority :
3542 -- Interrupt_Priority attribute definition clause not allowed
3543 -- except from aspect specification.
3545 if From_Aspect_Specification (N) then
3546 if not (Is_Protected_Type (U_Ent)
3547 or else Is_Task_Type (U_Ent))
3550 ("Interrupt_Priority can only be defined for task" &
3551 "and protected object",
3554 elsif Duplicate_Clause then
3558 -- The expression must be analyzed in the special manner
3559 -- described in "Handling of Default and Per-Object
3560 -- Expressions" in sem.ads.
3562 -- The visibility to the discriminants must be restored
3564 Push_Scope_And_Install_Discriminants (U_Ent);
3566 Preanalyze_Spec_Expression
3567 (Expr, RTE (RE_Interrupt_Priority));
3569 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
3574 ("attribute& cannot be set with definition clause", N);
3576 end Interrupt_Priority;
3578 ----------------------
3579 -- Iterator_Element --
3580 ----------------------
3582 when Attribute_Iterator_Element =>
3585 if not Is_Entity_Name (Expr)
3586 or else not Is_Type (Entity (Expr))
3588 Error_Msg_N ("aspect Iterator_Element must be a type", Expr);
3595 -- Machine radix attribute definition clause
3597 when Attribute_Machine_Radix => Machine_Radix : declare
3598 Radix : constant Uint := Static_Integer (Expr);
3601 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
3602 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
3604 elsif Duplicate_Clause then
3607 elsif Radix /= No_Uint then
3608 Set_Has_Machine_Radix_Clause (U_Ent);
3609 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
3613 elsif Radix = 10 then
3614 Set_Machine_Radix_10 (U_Ent);
3616 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
3625 -- Object_Size attribute definition clause
3627 when Attribute_Object_Size => Object_Size : declare
3628 Size : constant Uint := Static_Integer (Expr);
3631 pragma Warnings (Off, Biased);
3634 if not Is_Type (U_Ent) then
3635 Error_Msg_N ("Object_Size cannot be given for &", Nam);
3637 elsif Duplicate_Clause then
3641 Check_Size (Expr, U_Ent, Size, Biased);
3649 UI_Mod (Size, 64) /= 0
3652 ("Object_Size must be 8, 16, 32, or multiple of 64",
3656 Set_Esize (U_Ent, Size);
3657 Set_Has_Object_Size_Clause (U_Ent);
3658 Alignment_Check_For_Size_Change (U_Ent, Size);
3666 when Attribute_Output =>
3667 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
3668 Set_Has_Specified_Stream_Output (Ent);
3674 when Attribute_Priority => Priority :
3676 -- Priority attribute definition clause not allowed except from
3677 -- aspect specification.
3679 if From_Aspect_Specification (N) then
3680 if not (Is_Protected_Type (U_Ent)
3681 or else Is_Task_Type (U_Ent)
3682 or else Ekind (U_Ent) = E_Procedure)
3685 ("Priority can only be defined for task and protected " &
3689 elsif Duplicate_Clause then
3693 -- The expression must be analyzed in the special manner
3694 -- described in "Handling of Default and Per-Object
3695 -- Expressions" in sem.ads.
3697 -- The visibility to the discriminants must be restored
3699 Push_Scope_And_Install_Discriminants (U_Ent);
3700 Preanalyze_Spec_Expression (Expr, Standard_Integer);
3701 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
3703 if not Is_Static_Expression (Expr) then
3704 Check_Restriction (Static_Priorities, Expr);
3710 ("attribute& cannot be set with definition clause", N);
3718 when Attribute_Read =>
3719 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
3720 Set_Has_Specified_Stream_Read (Ent);
3722 --------------------------
3723 -- Scalar_Storage_Order --
3724 --------------------------
3726 -- Scalar_Storage_Order attribute definition clause
3728 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order : declare
3730 if not (Is_Record_Type (U_Ent) or else Is_Array_Type (U_Ent)) then
3732 ("Scalar_Storage_Order can only be defined for "
3733 & "record or array type", Nam);
3735 elsif Duplicate_Clause then
3739 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
3741 if Etype (Expr) = Any_Type then
3744 elsif not Is_Static_Expression (Expr) then
3745 Flag_Non_Static_Expr
3746 ("Scalar_Storage_Order requires static expression!", Expr);
3748 elsif (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
3750 -- Here for the case of a non-default (i.e. non-confirming)
3751 -- Scalar_Storage_Order attribute definition.
3753 if Support_Nondefault_SSO_On_Target then
3754 Set_Reverse_Storage_Order (Base_Type (U_Ent), True);
3757 ("non-default Scalar_Storage_Order "
3758 & "not supported on target", Expr);
3762 end Scalar_Storage_Order;
3768 -- Size attribute definition clause
3770 when Attribute_Size => Size : declare
3771 Size : constant Uint := Static_Integer (Expr);
3778 if Duplicate_Clause then
3781 elsif not Is_Type (U_Ent)
3782 and then Ekind (U_Ent) /= E_Variable
3783 and then Ekind (U_Ent) /= E_Constant
3785 Error_Msg_N ("size cannot be given for &", Nam);
3787 elsif Is_Array_Type (U_Ent)
3788 and then not Is_Constrained (U_Ent)
3791 ("size cannot be given for unconstrained array", Nam);
3793 elsif Size /= No_Uint then
3794 if VM_Target /= No_VM and then not GNAT_Mode then
3796 -- Size clause is not handled properly on VM targets.
3797 -- Display a warning unless we are in GNAT mode, in which
3798 -- case this is useless.
3801 ("size clauses are ignored in this configuration??", N);
3804 if Is_Type (U_Ent) then
3807 Etyp := Etype (U_Ent);
3810 -- Check size, note that Gigi is in charge of checking that the
3811 -- size of an array or record type is OK. Also we do not check
3812 -- the size in the ordinary fixed-point case, since it is too
3813 -- early to do so (there may be subsequent small clause that
3814 -- affects the size). We can check the size if a small clause
3815 -- has already been given.
3817 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
3818 or else Has_Small_Clause (U_Ent)
3820 Check_Size (Expr, Etyp, Size, Biased);
3821 Set_Biased (U_Ent, N, "size clause", Biased);
3824 -- For types set RM_Size and Esize if possible
3826 if Is_Type (U_Ent) then
3827 Set_RM_Size (U_Ent, Size);
3829 -- For elementary types, increase Object_Size to power of 2,
3830 -- but not less than a storage unit in any case (normally
3831 -- this means it will be byte addressable).
3833 -- For all other types, nothing else to do, we leave Esize
3834 -- (object size) unset, the back end will set it from the
3835 -- size and alignment in an appropriate manner.
3837 -- In both cases, we check whether the alignment must be
3838 -- reset in the wake of the size change.
3840 if Is_Elementary_Type (U_Ent) then
3841 if Size <= System_Storage_Unit then
3842 Init_Esize (U_Ent, System_Storage_Unit);
3843 elsif Size <= 16 then
3844 Init_Esize (U_Ent, 16);
3845 elsif Size <= 32 then
3846 Init_Esize (U_Ent, 32);
3848 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
3851 Alignment_Check_For_Size_Change (U_Ent, Esize (U_Ent));
3853 Alignment_Check_For_Size_Change (U_Ent, Size);
3856 -- For objects, set Esize only
3859 if Is_Elementary_Type (Etyp) then
3860 if Size /= System_Storage_Unit
3862 Size /= System_Storage_Unit * 2
3864 Size /= System_Storage_Unit * 4
3866 Size /= System_Storage_Unit * 8
3868 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
3869 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
3871 ("size for primitive object must be a power of 2"
3872 & " in the range ^-^", N);
3876 Set_Esize (U_Ent, Size);
3879 Set_Has_Size_Clause (U_Ent);
3887 -- Small attribute definition clause
3889 when Attribute_Small => Small : declare
3890 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
3894 Analyze_And_Resolve (Expr, Any_Real);
3896 if Etype (Expr) = Any_Type then
3899 elsif not Is_Static_Expression (Expr) then
3900 Flag_Non_Static_Expr
3901 ("small requires static expression!", Expr);
3905 Small := Expr_Value_R (Expr);
3907 if Small <= Ureal_0 then
3908 Error_Msg_N ("small value must be greater than zero", Expr);
3914 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
3916 ("small requires an ordinary fixed point type", Nam);
3918 elsif Has_Small_Clause (U_Ent) then
3919 Error_Msg_N ("small already given for &", Nam);
3921 elsif Small > Delta_Value (U_Ent) then
3923 ("small value must not be greater than delta value", Nam);
3926 Set_Small_Value (U_Ent, Small);
3927 Set_Small_Value (Implicit_Base, Small);
3928 Set_Has_Small_Clause (U_Ent);
3929 Set_Has_Small_Clause (Implicit_Base);
3930 Set_Has_Non_Standard_Rep (Implicit_Base);
3938 -- Storage_Pool attribute definition clause
3940 when Attribute_Storage_Pool | Attribute_Simple_Storage_Pool => declare
3945 if Ekind (U_Ent) = E_Access_Subprogram_Type then
3947 ("storage pool cannot be given for access-to-subprogram type",
3952 Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
3955 ("storage pool can only be given for access types", Nam);
3958 elsif Is_Derived_Type (U_Ent) then
3960 ("storage pool cannot be given for a derived access type",
3963 elsif Duplicate_Clause then
3966 elsif Present (Associated_Storage_Pool (U_Ent)) then
3967 Error_Msg_N ("storage pool already given for &", Nam);
3971 if Id = Attribute_Storage_Pool then
3973 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
3975 -- In the Simple_Storage_Pool case, we allow a variable of any
3976 -- simple storage pool type, so we Resolve without imposing an
3980 Analyze_And_Resolve (Expr);
3982 if not Present (Get_Rep_Pragma
3983 (Etype (Expr), Name_Simple_Storage_Pool_Type))
3986 ("expression must be of a simple storage pool type", Expr);
3990 if not Denotes_Variable (Expr) then
3991 Error_Msg_N ("storage pool must be a variable", Expr);
3995 if Nkind (Expr) = N_Type_Conversion then
3996 T := Etype (Expression (Expr));
4001 -- The Stack_Bounded_Pool is used internally for implementing
4002 -- access types with a Storage_Size. Since it only work properly
4003 -- when used on one specific type, we need to check that it is not
4004 -- hijacked improperly:
4006 -- type T is access Integer;
4007 -- for T'Storage_Size use n;
4008 -- type Q is access Float;
4009 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
4011 if RTE_Available (RE_Stack_Bounded_Pool)
4012 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
4014 Error_Msg_N ("non-shareable internal Pool", Expr);
4018 -- If the argument is a name that is not an entity name, then
4019 -- we construct a renaming operation to define an entity of
4020 -- type storage pool.
4022 if not Is_Entity_Name (Expr)
4023 and then Is_Object_Reference (Expr)
4025 Pool := Make_Temporary (Loc, 'P', Expr);
4028 Rnode : constant Node_Id :=
4029 Make_Object_Renaming_Declaration (Loc,
4030 Defining_Identifier => Pool,
4032 New_Occurrence_Of (Etype (Expr), Loc),
4036 Insert_Before (N, Rnode);
4038 Set_Associated_Storage_Pool (U_Ent, Pool);
4041 elsif Is_Entity_Name (Expr) then
4042 Pool := Entity (Expr);
4044 -- If pool is a renamed object, get original one. This can
4045 -- happen with an explicit renaming, and within instances.
4047 while Present (Renamed_Object (Pool))
4048 and then Is_Entity_Name (Renamed_Object (Pool))
4050 Pool := Entity (Renamed_Object (Pool));
4053 if Present (Renamed_Object (Pool))
4054 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
4055 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
4057 Pool := Entity (Expression (Renamed_Object (Pool)));
4060 Set_Associated_Storage_Pool (U_Ent, Pool);
4062 elsif Nkind (Expr) = N_Type_Conversion
4063 and then Is_Entity_Name (Expression (Expr))
4064 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
4066 Pool := Entity (Expression (Expr));
4067 Set_Associated_Storage_Pool (U_Ent, Pool);
4070 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
4079 -- Storage_Size attribute definition clause
4081 when Attribute_Storage_Size => Storage_Size : declare
4082 Btype : constant Entity_Id := Base_Type (U_Ent);
4085 if Is_Task_Type (U_Ent) then
4086 Check_Restriction (No_Obsolescent_Features, N);
4088 if Warn_On_Obsolescent_Feature then
4090 ("?j?storage size clause for task is an " &
4091 "obsolescent feature (RM J.9)", N);
4092 Error_Msg_N ("\?j?use Storage_Size pragma instead", N);
4098 if not Is_Access_Type (U_Ent)
4099 and then Ekind (U_Ent) /= E_Task_Type
4101 Error_Msg_N ("storage size cannot be given for &", Nam);
4103 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
4105 ("storage size cannot be given for a derived access type",
4108 elsif Duplicate_Clause then
4112 Analyze_And_Resolve (Expr, Any_Integer);
4114 if Is_Access_Type (U_Ent) then
4115 if Present (Associated_Storage_Pool (U_Ent)) then
4116 Error_Msg_N ("storage pool already given for &", Nam);
4120 if Is_OK_Static_Expression (Expr)
4121 and then Expr_Value (Expr) = 0
4123 Set_No_Pool_Assigned (Btype);
4127 Set_Has_Storage_Size_Clause (Btype);
4135 when Attribute_Stream_Size => Stream_Size : declare
4136 Size : constant Uint := Static_Integer (Expr);
4139 if Ada_Version <= Ada_95 then
4140 Check_Restriction (No_Implementation_Attributes, N);
4143 if Duplicate_Clause then
4146 elsif Is_Elementary_Type (U_Ent) then
4147 if Size /= System_Storage_Unit
4149 Size /= System_Storage_Unit * 2
4151 Size /= System_Storage_Unit * 4
4153 Size /= System_Storage_Unit * 8
4155 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
4157 ("stream size for elementary type must be a"
4158 & " power of 2 and at least ^", N);
4160 elsif RM_Size (U_Ent) > Size then
4161 Error_Msg_Uint_1 := RM_Size (U_Ent);
4163 ("stream size for elementary type must be a"
4164 & " power of 2 and at least ^", N);
4167 Set_Has_Stream_Size_Clause (U_Ent);
4170 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
4178 -- Value_Size attribute definition clause
4180 when Attribute_Value_Size => Value_Size : declare
4181 Size : constant Uint := Static_Integer (Expr);
4185 if not Is_Type (U_Ent) then
4186 Error_Msg_N ("Value_Size cannot be given for &", Nam);
4188 elsif Duplicate_Clause then
4191 elsif Is_Array_Type (U_Ent)
4192 and then not Is_Constrained (U_Ent)
4195 ("Value_Size cannot be given for unconstrained array", Nam);
4198 if Is_Elementary_Type (U_Ent) then
4199 Check_Size (Expr, U_Ent, Size, Biased);
4200 Set_Biased (U_Ent, N, "value size clause", Biased);
4203 Set_RM_Size (U_Ent, Size);
4207 -----------------------
4208 -- Variable_Indexing --
4209 -----------------------
4211 when Attribute_Variable_Indexing =>
4212 Check_Indexing_Functions;
4218 when Attribute_Write =>
4219 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
4220 Set_Has_Specified_Stream_Write (Ent);
4222 -- All other attributes cannot be set
4226 ("attribute& cannot be set with definition clause", N);
4229 -- The test for the type being frozen must be performed after any
4230 -- expression the clause has been analyzed since the expression itself
4231 -- might cause freezing that makes the clause illegal.
4233 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
4236 end Analyze_Attribute_Definition_Clause;
4238 ----------------------------
4239 -- Analyze_Code_Statement --
4240 ----------------------------
4242 procedure Analyze_Code_Statement (N : Node_Id) is
4243 HSS : constant Node_Id := Parent (N);
4244 SBody : constant Node_Id := Parent (HSS);
4245 Subp : constant Entity_Id := Current_Scope;
4252 -- Analyze and check we get right type, note that this implements the
4253 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
4254 -- is the only way that Asm_Insn could possibly be visible.
4256 Analyze_And_Resolve (Expression (N));
4258 if Etype (Expression (N)) = Any_Type then
4260 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
4261 Error_Msg_N ("incorrect type for code statement", N);
4265 Check_Code_Statement (N);
4267 -- Make sure we appear in the handled statement sequence of a
4268 -- subprogram (RM 13.8(3)).
4270 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
4271 or else Nkind (SBody) /= N_Subprogram_Body
4274 ("code statement can only appear in body of subprogram", N);
4278 -- Do remaining checks (RM 13.8(3)) if not already done
4280 if not Is_Machine_Code_Subprogram (Subp) then
4281 Set_Is_Machine_Code_Subprogram (Subp);
4283 -- No exception handlers allowed
4285 if Present (Exception_Handlers (HSS)) then
4287 ("exception handlers not permitted in machine code subprogram",
4288 First (Exception_Handlers (HSS)));
4291 -- No declarations other than use clauses and pragmas (we allow
4292 -- certain internally generated declarations as well).
4294 Decl := First (Declarations (SBody));
4295 while Present (Decl) loop
4296 DeclO := Original_Node (Decl);
4297 if Comes_From_Source (DeclO)
4298 and not Nkind_In (DeclO, N_Pragma,
4299 N_Use_Package_Clause,
4301 N_Implicit_Label_Declaration)
4304 ("this declaration not allowed in machine code subprogram",
4311 -- No statements other than code statements, pragmas, and labels.
4312 -- Again we allow certain internally generated statements.
4314 -- In Ada 2012, qualified expressions are names, and the code
4315 -- statement is initially parsed as a procedure call.
4317 Stmt := First (Statements (HSS));
4318 while Present (Stmt) loop
4319 StmtO := Original_Node (Stmt);
4321 -- A procedure call transformed into a code statement is OK.
4323 if Ada_Version >= Ada_2012
4324 and then Nkind (StmtO) = N_Procedure_Call_Statement
4325 and then Nkind (Name (StmtO)) = N_Qualified_Expression
4329 elsif Comes_From_Source (StmtO)
4330 and then not Nkind_In (StmtO, N_Pragma,
4335 ("this statement is not allowed in machine code subprogram",
4342 end Analyze_Code_Statement;
4344 -----------------------------------------------
4345 -- Analyze_Enumeration_Representation_Clause --
4346 -----------------------------------------------
4348 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
4349 Ident : constant Node_Id := Identifier (N);
4350 Aggr : constant Node_Id := Array_Aggregate (N);
4351 Enumtype : Entity_Id;
4358 Err : Boolean := False;
4359 -- Set True to avoid cascade errors and crashes on incorrect source code
4361 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
4362 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
4363 -- Allowed range of universal integer (= allowed range of enum lit vals)
4367 -- Minimum and maximum values of entries
4370 -- Pointer to node for literal providing max value
4373 if Ignore_Rep_Clauses then
4377 -- Ignore enumeration rep clauses by default in CodePeer mode,
4378 -- unless -gnatd.I is specified, as a work around for potential false
4379 -- positive messages.
4381 if CodePeer_Mode and not Debug_Flag_Dot_II then
4385 -- First some basic error checks
4388 Enumtype := Entity (Ident);
4390 if Enumtype = Any_Type
4391 or else Rep_Item_Too_Early (Enumtype, N)
4395 Enumtype := Underlying_Type (Enumtype);
4398 if not Is_Enumeration_Type (Enumtype) then
4400 ("enumeration type required, found}",
4401 Ident, First_Subtype (Enumtype));
4405 -- Ignore rep clause on generic actual type. This will already have
4406 -- been flagged on the template as an error, and this is the safest
4407 -- way to ensure we don't get a junk cascaded message in the instance.
4409 if Is_Generic_Actual_Type (Enumtype) then
4412 -- Type must be in current scope
4414 elsif Scope (Enumtype) /= Current_Scope then
4415 Error_Msg_N ("type must be declared in this scope", Ident);
4418 -- Type must be a first subtype
4420 elsif not Is_First_Subtype (Enumtype) then
4421 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
4424 -- Ignore duplicate rep clause
4426 elsif Has_Enumeration_Rep_Clause (Enumtype) then
4427 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
4430 -- Don't allow rep clause for standard [wide_[wide_]]character
4432 elsif Is_Standard_Character_Type (Enumtype) then
4433 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
4436 -- Check that the expression is a proper aggregate (no parentheses)
4438 elsif Paren_Count (Aggr) /= 0 then
4440 ("extra parentheses surrounding aggregate not allowed",
4444 -- All tests passed, so set rep clause in place
4447 Set_Has_Enumeration_Rep_Clause (Enumtype);
4448 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
4451 -- Now we process the aggregate. Note that we don't use the normal
4452 -- aggregate code for this purpose, because we don't want any of the
4453 -- normal expansion activities, and a number of special semantic
4454 -- rules apply (including the component type being any integer type)
4456 Elit := First_Literal (Enumtype);
4458 -- First the positional entries if any
4460 if Present (Expressions (Aggr)) then
4461 Expr := First (Expressions (Aggr));
4462 while Present (Expr) loop
4464 Error_Msg_N ("too many entries in aggregate", Expr);
4468 Val := Static_Integer (Expr);
4470 -- Err signals that we found some incorrect entries processing
4471 -- the list. The final checks for completeness and ordering are
4472 -- skipped in this case.
4474 if Val = No_Uint then
4476 elsif Val < Lo or else Hi < Val then
4477 Error_Msg_N ("value outside permitted range", Expr);
4481 Set_Enumeration_Rep (Elit, Val);
4482 Set_Enumeration_Rep_Expr (Elit, Expr);
4488 -- Now process the named entries if present
4490 if Present (Component_Associations (Aggr)) then
4491 Assoc := First (Component_Associations (Aggr));
4492 while Present (Assoc) loop
4493 Choice := First (Choices (Assoc));
4495 if Present (Next (Choice)) then
4497 ("multiple choice not allowed here", Next (Choice));
4501 if Nkind (Choice) = N_Others_Choice then
4502 Error_Msg_N ("others choice not allowed here", Choice);
4505 elsif Nkind (Choice) = N_Range then
4507 -- ??? should allow zero/one element range here
4509 Error_Msg_N ("range not allowed here", Choice);
4513 Analyze_And_Resolve (Choice, Enumtype);
4515 if Error_Posted (Choice) then
4520 if Is_Entity_Name (Choice)
4521 and then Is_Type (Entity (Choice))
4523 Error_Msg_N ("subtype name not allowed here", Choice);
4526 -- ??? should allow static subtype with zero/one entry
4528 elsif Etype (Choice) = Base_Type (Enumtype) then
4529 if not Is_Static_Expression (Choice) then
4530 Flag_Non_Static_Expr
4531 ("non-static expression used for choice!", Choice);
4535 Elit := Expr_Value_E (Choice);
4537 if Present (Enumeration_Rep_Expr (Elit)) then
4539 Sloc (Enumeration_Rep_Expr (Elit));
4541 ("representation for& previously given#",
4546 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
4548 Expr := Expression (Assoc);
4549 Val := Static_Integer (Expr);
4551 if Val = No_Uint then
4554 elsif Val < Lo or else Hi < Val then
4555 Error_Msg_N ("value outside permitted range", Expr);
4559 Set_Enumeration_Rep (Elit, Val);
4569 -- Aggregate is fully processed. Now we check that a full set of
4570 -- representations was given, and that they are in range and in order.
4571 -- These checks are only done if no other errors occurred.
4577 Elit := First_Literal (Enumtype);
4578 while Present (Elit) loop
4579 if No (Enumeration_Rep_Expr (Elit)) then
4580 Error_Msg_NE ("missing representation for&!", N, Elit);
4583 Val := Enumeration_Rep (Elit);
4585 if Min = No_Uint then
4589 if Val /= No_Uint then
4590 if Max /= No_Uint and then Val <= Max then
4592 ("enumeration value for& not ordered!",
4593 Enumeration_Rep_Expr (Elit), Elit);
4596 Max_Node := Enumeration_Rep_Expr (Elit);
4600 -- If there is at least one literal whose representation is not
4601 -- equal to the Pos value, then note that this enumeration type
4602 -- has a non-standard representation.
4604 if Val /= Enumeration_Pos (Elit) then
4605 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
4612 -- Now set proper size information
4615 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
4618 if Has_Size_Clause (Enumtype) then
4620 -- All OK, if size is OK now
4622 if RM_Size (Enumtype) >= Minsize then
4626 -- Try if we can get by with biasing
4629 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
4631 -- Error message if even biasing does not work
4633 if RM_Size (Enumtype) < Minsize then
4634 Error_Msg_Uint_1 := RM_Size (Enumtype);
4635 Error_Msg_Uint_2 := Max;
4637 ("previously given size (^) is too small "
4638 & "for this value (^)", Max_Node);
4640 -- If biasing worked, indicate that we now have biased rep
4644 (Enumtype, Size_Clause (Enumtype), "size clause");
4649 Set_RM_Size (Enumtype, Minsize);
4650 Set_Enum_Esize (Enumtype);
4653 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
4654 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
4655 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
4659 -- We repeat the too late test in case it froze itself!
4661 if Rep_Item_Too_Late (Enumtype, N) then
4664 end Analyze_Enumeration_Representation_Clause;
4666 ----------------------------
4667 -- Analyze_Free_Statement --
4668 ----------------------------
4670 procedure Analyze_Free_Statement (N : Node_Id) is
4672 Analyze (Expression (N));
4673 end Analyze_Free_Statement;
4675 ---------------------------
4676 -- Analyze_Freeze_Entity --
4677 ---------------------------
4679 procedure Analyze_Freeze_Entity (N : Node_Id) is
4680 E : constant Entity_Id := Entity (N);
4683 -- Remember that we are processing a freezing entity. Required to
4684 -- ensure correct decoration of internal entities associated with
4685 -- interfaces (see New_Overloaded_Entity).
4687 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
4689 -- For tagged types covering interfaces add internal entities that link
4690 -- the primitives of the interfaces with the primitives that cover them.
4691 -- Note: These entities were originally generated only when generating
4692 -- code because their main purpose was to provide support to initialize
4693 -- the secondary dispatch tables. They are now generated also when
4694 -- compiling with no code generation to provide ASIS the relationship
4695 -- between interface primitives and tagged type primitives. They are
4696 -- also used to locate primitives covering interfaces when processing
4697 -- generics (see Derive_Subprograms).
4699 if Ada_Version >= Ada_2005
4700 and then Ekind (E) = E_Record_Type
4701 and then Is_Tagged_Type (E)
4702 and then not Is_Interface (E)
4703 and then Has_Interfaces (E)
4705 -- This would be a good common place to call the routine that checks
4706 -- overriding of interface primitives (and thus factorize calls to
4707 -- Check_Abstract_Overriding located at different contexts in the
4708 -- compiler). However, this is not possible because it causes
4709 -- spurious errors in case of late overriding.
4711 Add_Internal_Interface_Entities (E);
4716 if Ekind (E) = E_Record_Type
4717 and then Is_CPP_Class (E)
4718 and then Is_Tagged_Type (E)
4719 and then Tagged_Type_Expansion
4720 and then Expander_Active
4722 if CPP_Num_Prims (E) = 0 then
4724 -- If the CPP type has user defined components then it must import
4725 -- primitives from C++. This is required because if the C++ class
4726 -- has no primitives then the C++ compiler does not added the _tag
4727 -- component to the type.
4729 pragma Assert (Chars (First_Entity (E)) = Name_uTag);
4731 if First_Entity (E) /= Last_Entity (E) then
4733 ("'C'P'P type must import at least one primitive from C++??",
4738 -- Check that all its primitives are abstract or imported from C++.
4739 -- Check also availability of the C++ constructor.
4742 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
4744 Error_Reported : Boolean := False;
4748 Elmt := First_Elmt (Primitive_Operations (E));
4749 while Present (Elmt) loop
4750 Prim := Node (Elmt);
4752 if Comes_From_Source (Prim) then
4753 if Is_Abstract_Subprogram (Prim) then
4756 elsif not Is_Imported (Prim)
4757 or else Convention (Prim) /= Convention_CPP
4760 ("primitives of 'C'P'P types must be imported from C++ "
4761 & "or abstract??", Prim);
4763 elsif not Has_Constructors
4764 and then not Error_Reported
4766 Error_Msg_Name_1 := Chars (E);
4768 ("??'C'P'P constructor required for type %", Prim);
4769 Error_Reported := True;
4778 -- Check Ada derivation of CPP type
4781 and then Tagged_Type_Expansion
4782 and then Ekind (E) = E_Record_Type
4783 and then Etype (E) /= E
4784 and then Is_CPP_Class (Etype (E))
4785 and then CPP_Num_Prims (Etype (E)) > 0
4786 and then not Is_CPP_Class (E)
4787 and then not Has_CPP_Constructors (Etype (E))
4789 -- If the parent has C++ primitives but it has no constructor then
4790 -- check that all the primitives are overridden in this derivation;
4791 -- otherwise the constructor of the parent is needed to build the
4799 Elmt := First_Elmt (Primitive_Operations (E));
4800 while Present (Elmt) loop
4801 Prim := Node (Elmt);
4803 if not Is_Abstract_Subprogram (Prim)
4804 and then No (Interface_Alias (Prim))
4805 and then Find_Dispatching_Type (Ultimate_Alias (Prim)) /= E
4807 Error_Msg_Name_1 := Chars (Etype (E));
4809 ("'C'P'P constructor required for parent type %", E);
4818 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
4820 -- If we have a type with predicates, build predicate function
4822 if Is_Type (E) and then Has_Predicates (E) then
4823 Build_Predicate_Functions (E, N);
4826 -- If type has delayed aspects, this is where we do the preanalysis at
4827 -- the freeze point, as part of the consistent visibility check. Note
4828 -- that this must be done after calling Build_Predicate_Functions or
4829 -- Build_Invariant_Procedure since these subprograms fix occurrences of
4830 -- the subtype name in the saved expression so that they will not cause
4831 -- trouble in the preanalysis.
4833 if Has_Delayed_Aspects (E)
4834 and then Scope (E) = Current_Scope
4836 -- Retrieve the visibility to the discriminants in order to properly
4837 -- analyze the aspects.
4839 Push_Scope_And_Install_Discriminants (E);
4845 -- Look for aspect specification entries for this entity
4847 Ritem := First_Rep_Item (E);
4848 while Present (Ritem) loop
4849 if Nkind (Ritem) = N_Aspect_Specification
4850 and then Entity (Ritem) = E
4851 and then Is_Delayed_Aspect (Ritem)
4853 Check_Aspect_At_Freeze_Point (Ritem);
4856 Next_Rep_Item (Ritem);
4860 Uninstall_Discriminants_And_Pop_Scope (E);
4862 end Analyze_Freeze_Entity;
4864 ------------------------------------------
4865 -- Analyze_Record_Representation_Clause --
4866 ------------------------------------------
4868 -- Note: we check as much as we can here, but we can't do any checks
4869 -- based on the position values (e.g. overlap checks) until freeze time
4870 -- because especially in Ada 2005 (machine scalar mode), the processing
4871 -- for non-standard bit order can substantially change the positions.
4872 -- See procedure Check_Record_Representation_Clause (called from Freeze)
4873 -- for the remainder of this processing.
4875 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
4876 Ident : constant Node_Id := Identifier (N);
4881 Hbit : Uint := Uint_0;
4885 Rectype : Entity_Id;
4888 function Is_Inherited (Comp : Entity_Id) return Boolean;
4889 -- True if Comp is an inherited component in a record extension
4895 function Is_Inherited (Comp : Entity_Id) return Boolean is
4896 Comp_Base : Entity_Id;
4899 if Ekind (Rectype) = E_Record_Subtype then
4900 Comp_Base := Original_Record_Component (Comp);
4905 return Comp_Base /= Original_Record_Component (Comp_Base);
4910 Is_Record_Extension : Boolean;
4911 -- True if Rectype is a record extension
4913 CR_Pragma : Node_Id := Empty;
4914 -- Points to N_Pragma node if Complete_Representation pragma present
4916 -- Start of processing for Analyze_Record_Representation_Clause
4919 if Ignore_Rep_Clauses then
4924 Rectype := Entity (Ident);
4926 if Rectype = Any_Type or else Rep_Item_Too_Early (Rectype, N) then
4929 Rectype := Underlying_Type (Rectype);
4932 -- First some basic error checks
4934 if not Is_Record_Type (Rectype) then
4936 ("record type required, found}", Ident, First_Subtype (Rectype));
4939 elsif Scope (Rectype) /= Current_Scope then
4940 Error_Msg_N ("type must be declared in this scope", N);
4943 elsif not Is_First_Subtype (Rectype) then
4944 Error_Msg_N ("cannot give record rep clause for subtype", N);
4947 elsif Has_Record_Rep_Clause (Rectype) then
4948 Error_Msg_N ("duplicate record rep clause ignored", N);
4951 elsif Rep_Item_Too_Late (Rectype, N) then
4955 -- We know we have a first subtype, now possibly go the the anonymous
4956 -- base type to determine whether Rectype is a record extension.
4958 Recdef := Type_Definition (Declaration_Node (Base_Type (Rectype)));
4959 Is_Record_Extension :=
4960 Nkind (Recdef) = N_Derived_Type_Definition
4961 and then Present (Record_Extension_Part (Recdef));
4963 if Present (Mod_Clause (N)) then
4965 Loc : constant Source_Ptr := Sloc (N);
4966 M : constant Node_Id := Mod_Clause (N);
4967 P : constant List_Id := Pragmas_Before (M);
4971 pragma Warnings (Off, Mod_Val);
4974 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
4976 if Warn_On_Obsolescent_Feature then
4978 ("?j?mod clause is an obsolescent feature (RM J.8)", N);
4980 ("\?j?use alignment attribute definition clause instead", N);
4987 -- In ASIS_Mode mode, expansion is disabled, but we must convert
4988 -- the Mod clause into an alignment clause anyway, so that the
4989 -- back-end can compute and back-annotate properly the size and
4990 -- alignment of types that may include this record.
4992 -- This seems dubious, this destroys the source tree in a manner
4993 -- not detectable by ASIS ???
4995 if Operating_Mode = Check_Semantics and then ASIS_Mode then
4997 Make_Attribute_Definition_Clause (Loc,
4998 Name => New_Reference_To (Base_Type (Rectype), Loc),
4999 Chars => Name_Alignment,
5000 Expression => Relocate_Node (Expression (M)));
5002 Set_From_At_Mod (AtM_Nod);
5003 Insert_After (N, AtM_Nod);
5004 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
5005 Set_Mod_Clause (N, Empty);
5008 -- Get the alignment value to perform error checking
5010 Mod_Val := Get_Alignment_Value (Expression (M));
5015 -- For untagged types, clear any existing component clauses for the
5016 -- type. If the type is derived, this is what allows us to override
5017 -- a rep clause for the parent. For type extensions, the representation
5018 -- of the inherited components is inherited, so we want to keep previous
5019 -- component clauses for completeness.
5021 if not Is_Tagged_Type (Rectype) then
5022 Comp := First_Component_Or_Discriminant (Rectype);
5023 while Present (Comp) loop
5024 Set_Component_Clause (Comp, Empty);
5025 Next_Component_Or_Discriminant (Comp);
5029 -- All done if no component clauses
5031 CC := First (Component_Clauses (N));
5037 -- A representation like this applies to the base type
5039 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
5040 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
5041 Set_Has_Specified_Layout (Base_Type (Rectype));
5043 -- Process the component clauses
5045 while Present (CC) loop
5049 if Nkind (CC) = N_Pragma then
5052 -- The only pragma of interest is Complete_Representation
5054 if Pragma_Name (CC) = Name_Complete_Representation then
5058 -- Processing for real component clause
5061 Posit := Static_Integer (Position (CC));
5062 Fbit := Static_Integer (First_Bit (CC));
5063 Lbit := Static_Integer (Last_Bit (CC));
5066 and then Fbit /= No_Uint
5067 and then Lbit /= No_Uint
5071 ("position cannot be negative", Position (CC));
5075 ("first bit cannot be negative", First_Bit (CC));
5077 -- The Last_Bit specified in a component clause must not be
5078 -- less than the First_Bit minus one (RM-13.5.1(10)).
5080 elsif Lbit < Fbit - 1 then
5082 ("last bit cannot be less than first bit minus one",
5085 -- Values look OK, so find the corresponding record component
5086 -- Even though the syntax allows an attribute reference for
5087 -- implementation-defined components, GNAT does not allow the
5088 -- tag to get an explicit position.
5090 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
5091 if Attribute_Name (Component_Name (CC)) = Name_Tag then
5092 Error_Msg_N ("position of tag cannot be specified", CC);
5094 Error_Msg_N ("illegal component name", CC);
5098 Comp := First_Entity (Rectype);
5099 while Present (Comp) loop
5100 exit when Chars (Comp) = Chars (Component_Name (CC));
5106 -- Maybe component of base type that is absent from
5107 -- statically constrained first subtype.
5109 Comp := First_Entity (Base_Type (Rectype));
5110 while Present (Comp) loop
5111 exit when Chars (Comp) = Chars (Component_Name (CC));
5118 ("component clause is for non-existent field", CC);
5120 -- Ada 2012 (AI05-0026): Any name that denotes a
5121 -- discriminant of an object of an unchecked union type
5122 -- shall not occur within a record_representation_clause.
5124 -- The general restriction of using record rep clauses on
5125 -- Unchecked_Union types has now been lifted. Since it is
5126 -- possible to introduce a record rep clause which mentions
5127 -- the discriminant of an Unchecked_Union in non-Ada 2012
5128 -- code, this check is applied to all versions of the
5131 elsif Ekind (Comp) = E_Discriminant
5132 and then Is_Unchecked_Union (Rectype)
5135 ("cannot reference discriminant of unchecked union",
5136 Component_Name (CC));
5138 elsif Is_Record_Extension and then Is_Inherited (Comp) then
5140 ("component clause not allowed for inherited "
5141 & "component&", CC, Comp);
5143 elsif Present (Component_Clause (Comp)) then
5145 -- Diagnose duplicate rep clause, or check consistency
5146 -- if this is an inherited component. In a double fault,
5147 -- there may be a duplicate inconsistent clause for an
5148 -- inherited component.
5150 if Scope (Original_Record_Component (Comp)) = Rectype
5151 or else Parent (Component_Clause (Comp)) = N
5153 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
5154 Error_Msg_N ("component clause previously given#", CC);
5158 Rep1 : constant Node_Id := Component_Clause (Comp);
5160 if Intval (Position (Rep1)) /=
5161 Intval (Position (CC))
5162 or else Intval (First_Bit (Rep1)) /=
5163 Intval (First_Bit (CC))
5164 or else Intval (Last_Bit (Rep1)) /=
5165 Intval (Last_Bit (CC))
5168 ("component clause inconsistent "
5169 & "with representation of ancestor", CC);
5171 elsif Warn_On_Redundant_Constructs then
5173 ("?r?redundant confirming component clause "
5174 & "for component!", CC);
5179 -- Normal case where this is the first component clause we
5180 -- have seen for this entity, so set it up properly.
5183 -- Make reference for field in record rep clause and set
5184 -- appropriate entity field in the field identifier.
5187 (Comp, Component_Name (CC), Set_Ref => False);
5188 Set_Entity (Component_Name (CC), Comp);
5190 -- Update Fbit and Lbit to the actual bit number
5192 Fbit := Fbit + UI_From_Int (SSU) * Posit;
5193 Lbit := Lbit + UI_From_Int (SSU) * Posit;
5195 if Has_Size_Clause (Rectype)
5196 and then RM_Size (Rectype) <= Lbit
5199 ("bit number out of range of specified size",
5202 Set_Component_Clause (Comp, CC);
5203 Set_Component_Bit_Offset (Comp, Fbit);
5204 Set_Esize (Comp, 1 + (Lbit - Fbit));
5205 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
5206 Set_Normalized_Position (Comp, Fbit / SSU);
5208 if Warn_On_Overridden_Size
5209 and then Has_Size_Clause (Etype (Comp))
5210 and then RM_Size (Etype (Comp)) /= Esize (Comp)
5213 ("?S?component size overrides size clause for&",
5214 Component_Name (CC), Etype (Comp));
5217 -- This information is also set in the corresponding
5218 -- component of the base type, found by accessing the
5219 -- Original_Record_Component link if it is present.
5221 Ocomp := Original_Record_Component (Comp);
5228 (Component_Name (CC),
5234 (Comp, First_Node (CC), "component clause", Biased);
5236 if Present (Ocomp) then
5237 Set_Component_Clause (Ocomp, CC);
5238 Set_Component_Bit_Offset (Ocomp, Fbit);
5239 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
5240 Set_Normalized_Position (Ocomp, Fbit / SSU);
5241 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
5243 Set_Normalized_Position_Max
5244 (Ocomp, Normalized_Position (Ocomp));
5246 -- Note: we don't use Set_Biased here, because we
5247 -- already gave a warning above if needed, and we
5248 -- would get a duplicate for the same name here.
5250 Set_Has_Biased_Representation
5251 (Ocomp, Has_Biased_Representation (Comp));
5254 if Esize (Comp) < 0 then
5255 Error_Msg_N ("component size is negative", CC);
5266 -- Check missing components if Complete_Representation pragma appeared
5268 if Present (CR_Pragma) then
5269 Comp := First_Component_Or_Discriminant (Rectype);
5270 while Present (Comp) loop
5271 if No (Component_Clause (Comp)) then
5273 ("missing component clause for &", CR_Pragma, Comp);
5276 Next_Component_Or_Discriminant (Comp);
5279 -- Give missing components warning if required
5281 elsif Warn_On_Unrepped_Components then
5283 Num_Repped_Components : Nat := 0;
5284 Num_Unrepped_Components : Nat := 0;
5287 -- First count number of repped and unrepped components
5289 Comp := First_Component_Or_Discriminant (Rectype);
5290 while Present (Comp) loop
5291 if Present (Component_Clause (Comp)) then
5292 Num_Repped_Components := Num_Repped_Components + 1;
5294 Num_Unrepped_Components := Num_Unrepped_Components + 1;
5297 Next_Component_Or_Discriminant (Comp);
5300 -- We are only interested in the case where there is at least one
5301 -- unrepped component, and at least half the components have rep
5302 -- clauses. We figure that if less than half have them, then the
5303 -- partial rep clause is really intentional. If the component
5304 -- type has no underlying type set at this point (as for a generic
5305 -- formal type), we don't know enough to give a warning on the
5308 if Num_Unrepped_Components > 0
5309 and then Num_Unrepped_Components < Num_Repped_Components
5311 Comp := First_Component_Or_Discriminant (Rectype);
5312 while Present (Comp) loop
5313 if No (Component_Clause (Comp))
5314 and then Comes_From_Source (Comp)
5315 and then Present (Underlying_Type (Etype (Comp)))
5316 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
5317 or else Size_Known_At_Compile_Time
5318 (Underlying_Type (Etype (Comp))))
5319 and then not Has_Warnings_Off (Rectype)
5321 Error_Msg_Sloc := Sloc (Comp);
5323 ("?C?no component clause given for & declared #",
5327 Next_Component_Or_Discriminant (Comp);
5332 end Analyze_Record_Representation_Clause;
5334 -------------------------------------------
5335 -- Build_Invariant_Procedure_Declaration --
5336 -------------------------------------------
5338 function Build_Invariant_Procedure_Declaration
5339 (Typ : Entity_Id) return Node_Id
5341 Loc : constant Source_Ptr := Sloc (Typ);
5342 Object_Entity : constant Entity_Id :=
5343 Make_Defining_Identifier (Loc, New_Internal_Name ('I'));
5348 Set_Etype (Object_Entity, Typ);
5350 -- Check for duplicate definiations.
5352 if Has_Invariants (Typ) and then Present (Invariant_Procedure (Typ)) then
5357 Make_Defining_Identifier (Loc,
5358 Chars => New_External_Name (Chars (Typ), "Invariant"));
5359 Set_Has_Invariants (Typ);
5360 Set_Ekind (SId, E_Procedure);
5361 Set_Is_Invariant_Procedure (SId);
5362 Set_Invariant_Procedure (Typ, SId);
5365 Make_Procedure_Specification (Loc,
5366 Defining_Unit_Name => SId,
5367 Parameter_Specifications => New_List (
5368 Make_Parameter_Specification (Loc,
5369 Defining_Identifier => Object_Entity,
5370 Parameter_Type => New_Occurrence_Of (Typ, Loc))));
5372 return Make_Subprogram_Declaration (Loc, Specification => Spec);
5373 end Build_Invariant_Procedure_Declaration;
5375 -------------------------------
5376 -- Build_Invariant_Procedure --
5377 -------------------------------
5379 -- The procedure that is constructed here has the form
5381 -- procedure typInvariant (Ixxx : typ) is
5383 -- pragma Check (Invariant, exp, "failed invariant from xxx");
5384 -- pragma Check (Invariant, exp, "failed invariant from xxx");
5386 -- pragma Check (Invariant, exp, "failed inherited invariant from xxx");
5388 -- end typInvariant;
5390 procedure Build_Invariant_Procedure (Typ : Entity_Id; N : Node_Id) is
5391 Loc : constant Source_Ptr := Sloc (Typ);
5398 Visible_Decls : constant List_Id := Visible_Declarations (N);
5399 Private_Decls : constant List_Id := Private_Declarations (N);
5401 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean);
5402 -- Appends statements to Stmts for any invariants in the rep item chain
5403 -- of the given type. If Inherit is False, then we only process entries
5404 -- on the chain for the type Typ. If Inherit is True, then we ignore any
5405 -- Invariant aspects, but we process all Invariant'Class aspects, adding
5406 -- "inherited" to the exception message and generating an informational
5407 -- message about the inheritance of an invariant.
5409 Object_Name : Name_Id;
5410 -- Name for argument of invariant procedure
5412 Object_Entity : Node_Id;
5413 -- The entity of the formal for the procedure
5415 --------------------
5416 -- Add_Invariants --
5417 --------------------
5419 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean) is
5429 procedure Replace_Type_Reference (N : Node_Id);
5430 -- Replace a single occurrence N of the subtype name with a reference
5431 -- to the formal of the predicate function. N can be an identifier
5432 -- referencing the subtype, or a selected component, representing an
5433 -- appropriately qualified occurrence of the subtype name.
5435 procedure Replace_Type_References is
5436 new Replace_Type_References_Generic (Replace_Type_Reference);
5437 -- Traverse an expression replacing all occurrences of the subtype
5438 -- name with appropriate references to the object that is the formal
5439 -- parameter of the predicate function. Note that we must ensure
5440 -- that the type and entity information is properly set in the
5441 -- replacement node, since we will do a Preanalyze call of this
5442 -- expression without proper visibility of the procedure argument.
5444 ----------------------------
5445 -- Replace_Type_Reference --
5446 ----------------------------
5448 -- Note: See comments in Add_Predicates.Replace_Type_Reference
5449 -- regarding handling of Sloc and Comes_From_Source.
5451 procedure Replace_Type_Reference (N : Node_Id) is
5453 -- Invariant'Class, replace with T'Class (obj)
5455 if Class_Present (Ritem) then
5457 Make_Type_Conversion (Sloc (N),
5459 Make_Attribute_Reference (Sloc (N),
5460 Prefix => New_Occurrence_Of (T, Sloc (N)),
5461 Attribute_Name => Name_Class),
5462 Expression => Make_Identifier (Sloc (N), Object_Name)));
5464 Set_Entity (Expression (N), Object_Entity);
5465 Set_Etype (Expression (N), Typ);
5467 -- Invariant, replace with obj
5470 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
5471 Set_Entity (N, Object_Entity);
5475 Set_Comes_From_Source (N, True);
5476 end Replace_Type_Reference;
5478 -- Start of processing for Add_Invariants
5481 Ritem := First_Rep_Item (T);
5482 while Present (Ritem) loop
5483 if Nkind (Ritem) = N_Pragma
5484 and then Pragma_Name (Ritem) = Name_Invariant
5486 Arg1 := First (Pragma_Argument_Associations (Ritem));
5487 Arg2 := Next (Arg1);
5488 Arg3 := Next (Arg2);
5490 Arg1 := Get_Pragma_Arg (Arg1);
5491 Arg2 := Get_Pragma_Arg (Arg2);
5493 -- For Inherit case, ignore Invariant, process only Class case
5496 if not Class_Present (Ritem) then
5500 -- For Inherit false, process only item for right type
5503 if Entity (Arg1) /= Typ then
5509 Stmts := Empty_List;
5512 Exp := New_Copy_Tree (Arg2);
5514 -- Preserve sloc of original pragma Invariant
5516 Loc := Sloc (Ritem);
5518 -- We need to replace any occurrences of the name of the type
5519 -- with references to the object, converted to type'Class in
5520 -- the case of Invariant'Class aspects.
5522 Replace_Type_References (Exp, Chars (T));
5524 -- If this invariant comes from an aspect, find the aspect
5525 -- specification, and replace the saved expression because
5526 -- we need the subtype references replaced for the calls to
5527 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
5528 -- and Check_Aspect_At_End_Of_Declarations.
5530 if From_Aspect_Specification (Ritem) then
5535 -- Loop to find corresponding aspect, note that this
5536 -- must be present given the pragma is marked delayed.
5538 Aitem := Next_Rep_Item (Ritem);
5539 while Present (Aitem) loop
5540 if Nkind (Aitem) = N_Aspect_Specification
5541 and then Aspect_Rep_Item (Aitem) = Ritem
5544 (Identifier (Aitem), New_Copy_Tree (Exp));
5548 Aitem := Next_Rep_Item (Aitem);
5553 -- Now we need to preanalyze the expression to properly capture
5554 -- the visibility in the visible part. The expression will not
5555 -- be analyzed for real until the body is analyzed, but that is
5556 -- at the end of the private part and has the wrong visibility.
5558 Set_Parent (Exp, N);
5559 Preanalyze_Assert_Expression (Exp, Standard_Boolean);
5561 -- Build first two arguments for Check pragma
5564 Make_Pragma_Argument_Association (Loc,
5565 Expression => Make_Identifier (Loc, Name_Invariant)),
5566 Make_Pragma_Argument_Association (Loc,
5567 Expression => Exp));
5569 -- Add message if present in Invariant pragma
5571 if Present (Arg3) then
5572 Str := Strval (Get_Pragma_Arg (Arg3));
5574 -- If inherited case, and message starts "failed invariant",
5575 -- change it to be "failed inherited invariant".
5578 String_To_Name_Buffer (Str);
5580 if Name_Buffer (1 .. 16) = "failed invariant" then
5581 Insert_Str_In_Name_Buffer ("inherited ", 8);
5582 Str := String_From_Name_Buffer;
5587 Make_Pragma_Argument_Association (Loc,
5588 Expression => Make_String_Literal (Loc, Str)));
5591 -- Add Check pragma to list of statements
5595 Pragma_Identifier =>
5596 Make_Identifier (Loc, Name_Check),
5597 Pragma_Argument_Associations => Assoc));
5599 -- If Inherited case and option enabled, output info msg. Note
5600 -- that we know this is a case of Invariant'Class.
5602 if Inherit and Opt.List_Inherited_Aspects then
5603 Error_Msg_Sloc := Sloc (Ritem);
5605 ("?L?info: & inherits `Invariant''Class` aspect from #",
5611 Next_Rep_Item (Ritem);
5615 -- Start of processing for Build_Invariant_Procedure
5623 -- If the aspect specification exists for some view of the type, the
5624 -- declaration for the procedure has been created.
5626 if Has_Invariants (Typ) then
5627 SId := Invariant_Procedure (Typ);
5630 if Present (SId) then
5631 PDecl := Unit_Declaration_Node (SId);
5634 PDecl := Build_Invariant_Procedure_Declaration (Typ);
5637 -- Recover formal of procedure, for use in the calls to invariant
5638 -- functions (including inherited ones).
5642 (First (Parameter_Specifications (Specification (PDecl))));
5643 Object_Name := Chars (Object_Entity);
5645 -- Add invariants for the current type
5647 Add_Invariants (Typ, Inherit => False);
5649 -- Add invariants for parent types
5652 Current_Typ : Entity_Id;
5653 Parent_Typ : Entity_Id;
5658 Parent_Typ := Etype (Current_Typ);
5660 if Is_Private_Type (Parent_Typ)
5661 and then Present (Full_View (Base_Type (Parent_Typ)))
5663 Parent_Typ := Full_View (Base_Type (Parent_Typ));
5666 exit when Parent_Typ = Current_Typ;
5668 Current_Typ := Parent_Typ;
5669 Add_Invariants (Current_Typ, Inherit => True);
5673 -- Build the procedure if we generated at least one Check pragma
5675 if Stmts /= No_List then
5676 Spec := Copy_Separate_Tree (Specification (PDecl));
5679 Make_Subprogram_Body (Loc,
5680 Specification => Spec,
5681 Declarations => Empty_List,
5682 Handled_Statement_Sequence =>
5683 Make_Handled_Sequence_Of_Statements (Loc,
5684 Statements => Stmts));
5686 -- Insert procedure declaration and spec at the appropriate points.
5687 -- If declaration is already analyzed, it was processed by the
5688 -- generated pragma.
5690 if Present (Private_Decls) then
5692 -- The spec goes at the end of visible declarations, but they have
5693 -- already been analyzed, so we need to explicitly do the analyze.
5695 if not Analyzed (PDecl) then
5696 Append_To (Visible_Decls, PDecl);
5700 -- The body goes at the end of the private declarations, which we
5701 -- have not analyzed yet, so we do not need to perform an explicit
5702 -- analyze call. We skip this if there are no private declarations
5703 -- (this is an error that will be caught elsewhere);
5705 Append_To (Private_Decls, PBody);
5707 -- If the invariant appears on the full view of a type, the
5708 -- analysis of the private part is complete, and we must
5709 -- analyze the new body explicitly.
5711 if In_Private_Part (Current_Scope) then
5715 -- If there are no private declarations this may be an error that
5716 -- will be diagnosed elsewhere. However, if this is a non-private
5717 -- type that inherits invariants, it needs no completion and there
5718 -- may be no private part. In this case insert invariant procedure
5719 -- at end of current declarative list, and analyze at once, given
5720 -- that the type is about to be frozen.
5722 elsif not Is_Private_Type (Typ) then
5723 Append_To (Visible_Decls, PDecl);
5724 Append_To (Visible_Decls, PBody);
5729 end Build_Invariant_Procedure;
5731 -------------------------------
5732 -- Build_Predicate_Functions --
5733 -------------------------------
5735 -- The procedures that are constructed here have the form:
5737 -- function typPredicate (Ixxx : typ) return Boolean is
5740 -- exp1 and then exp2 and then ...
5741 -- and then typ1Predicate (typ1 (Ixxx))
5742 -- and then typ2Predicate (typ2 (Ixxx))
5744 -- end typPredicate;
5746 -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that
5747 -- this is the point at which these expressions get analyzed, providing the
5748 -- required delay, and typ1, typ2, are entities from which predicates are
5749 -- inherited. Note that we do NOT generate Check pragmas, that's because we
5750 -- use this function even if checks are off, e.g. for membership tests.
5752 -- If the expression has at least one Raise_Expression, then we also build
5753 -- the typPredicateM version of the function, in which any occurrence of a
5754 -- Raise_Expression is converted to "return False".
5756 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id) is
5757 Loc : constant Source_Ptr := Sloc (Typ);
5760 -- This is the expression for the result of the function. It is
5761 -- is build by connecting the component predicates with AND THEN.
5764 -- This is the corresponding return expression for the Predicate_M
5765 -- function. It differs in that raise expressions are marked for
5766 -- special expansion (see Process_REs).
5768 Object_Name : constant Name_Id := New_Internal_Name ('I');
5769 -- Name for argument of Predicate procedure. Note that we use the same
5770 -- name for both predicate procedure. That way the reference within the
5771 -- predicate expression is the same in both functions.
5773 Object_Entity : constant Entity_Id :=
5774 Make_Defining_Identifier (Loc, Chars => Object_Name);
5775 -- Entity for argument of Predicate procedure
5777 Object_Entity_M : constant Entity_Id :=
5778 Make_Defining_Identifier (Loc, Chars => Object_Name);
5779 -- Entity for argument of Predicate_M procedure
5781 Raise_Expression_Present : Boolean := False;
5782 -- Set True if Expr has at least one Raise_Expression
5784 procedure Add_Call (T : Entity_Id);
5785 -- Includes a call to the predicate function for type T in Expr if T
5786 -- has predicates and Predicate_Function (T) is non-empty.
5788 procedure Add_Predicates;
5789 -- Appends expressions for any Predicate pragmas in the rep item chain
5790 -- Typ to Expr. Note that we look only at items for this exact entity.
5791 -- Inheritance of predicates for the parent type is done by calling the
5792 -- Predicate_Function of the parent type, using Add_Call above.
5794 function Test_RE (N : Node_Id) return Traverse_Result;
5795 -- Used in Test_REs, tests one node for being a raise expression, and if
5796 -- so sets Raise_Expression_Present True.
5798 procedure Test_REs is new Traverse_Proc (Test_RE);
5799 -- Tests to see if Expr contains any raise expressions
5801 function Process_RE (N : Node_Id) return Traverse_Result;
5802 -- Used in Process REs, tests if node N is a raise expression, and if
5803 -- so, marks it to be converted to return False.
5805 procedure Process_REs is new Traverse_Proc (Process_RE);
5806 -- Marks any raise expressions in Expr_M to return False
5808 Dynamic_Predicate_Present : Boolean := False;
5809 -- Set True if a dynamic predicate is present, results in the entire
5810 -- predicate being considered dynamic even if it looks static
5812 Static_Predicate_Present : Node_Id := Empty;
5813 -- Set to N_Pragma node for a static predicate if one is encountered
5819 procedure Add_Call (T : Entity_Id) is
5823 if Present (T) and then Present (Predicate_Function (T)) then
5824 Set_Has_Predicates (Typ);
5826 -- Build the call to the predicate function of T
5830 (T, Convert_To (T, Make_Identifier (Loc, Object_Name)));
5832 -- Add call to evolving expression, using AND THEN if needed
5839 Left_Opnd => Relocate_Node (Expr),
5843 -- Output info message on inheritance if required. Note we do not
5844 -- give this information for generic actual types, since it is
5845 -- unwelcome noise in that case in instantiations. We also
5846 -- generally suppress the message in instantiations, and also
5847 -- if it involves internal names.
5849 if Opt.List_Inherited_Aspects
5850 and then not Is_Generic_Actual_Type (Typ)
5851 and then Instantiation_Depth (Sloc (Typ)) = 0
5852 and then not Is_Internal_Name (Chars (T))
5853 and then not Is_Internal_Name (Chars (Typ))
5855 Error_Msg_Sloc := Sloc (Predicate_Function (T));
5856 Error_Msg_Node_2 := T;
5857 Error_Msg_N ("info: & inherits predicate from & #?L?", Typ);
5862 --------------------
5863 -- Add_Predicates --
5864 --------------------
5866 procedure Add_Predicates is
5871 procedure Replace_Type_Reference (N : Node_Id);
5872 -- Replace a single occurrence N of the subtype name with a reference
5873 -- to the formal of the predicate function. N can be an identifier
5874 -- referencing the subtype, or a selected component, representing an
5875 -- appropriately qualified occurrence of the subtype name.
5877 procedure Replace_Type_References is
5878 new Replace_Type_References_Generic (Replace_Type_Reference);
5879 -- Traverse an expression changing every occurrence of an identifier
5880 -- whose name matches the name of the subtype with a reference to
5881 -- the formal parameter of the predicate function.
5883 ----------------------------
5884 -- Replace_Type_Reference --
5885 ----------------------------
5887 procedure Replace_Type_Reference (N : Node_Id) is
5889 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
5890 -- Use the Sloc of the usage name, not the defining name
5893 Set_Entity (N, Object_Entity);
5895 -- We want to treat the node as if it comes from source, so that
5896 -- ASIS will not ignore it
5898 Set_Comes_From_Source (N, True);
5899 end Replace_Type_Reference;
5901 -- Start of processing for Add_Predicates
5904 Ritem := First_Rep_Item (Typ);
5905 while Present (Ritem) loop
5906 if Nkind (Ritem) = N_Pragma
5907 and then Pragma_Name (Ritem) = Name_Predicate
5909 if Present (Corresponding_Aspect (Ritem)) then
5910 case Chars (Identifier (Corresponding_Aspect (Ritem))) is
5911 when Name_Dynamic_Predicate =>
5912 Dynamic_Predicate_Present := True;
5913 when Name_Static_Predicate =>
5914 Static_Predicate_Present := Ritem;
5920 -- Acquire arguments
5922 Arg1 := First (Pragma_Argument_Associations (Ritem));
5923 Arg2 := Next (Arg1);
5925 Arg1 := Get_Pragma_Arg (Arg1);
5926 Arg2 := Get_Pragma_Arg (Arg2);
5928 -- See if this predicate pragma is for the current type or for
5929 -- its full view. A predicate on a private completion is placed
5930 -- on the partial view beause this is the visible entity that
5933 if Entity (Arg1) = Typ
5934 or else Full_View (Entity (Arg1)) = Typ
5936 -- We have a match, this entry is for our subtype
5938 -- We need to replace any occurrences of the name of the
5939 -- type with references to the object.
5941 Replace_Type_References (Arg2, Chars (Typ));
5943 -- If this predicate comes from an aspect, find the aspect
5944 -- specification, and replace the saved expression because
5945 -- we need the subtype references replaced for the calls to
5946 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
5947 -- and Check_Aspect_At_End_Of_Declarations.
5949 if From_Aspect_Specification (Ritem) then
5954 -- Loop to find corresponding aspect, note that this
5955 -- must be present given the pragma is marked delayed.
5957 Aitem := Next_Rep_Item (Ritem);
5959 if Nkind (Aitem) = N_Aspect_Specification
5960 and then Aspect_Rep_Item (Aitem) = Ritem
5963 (Identifier (Aitem), New_Copy_Tree (Arg2));
5967 Aitem := Next_Rep_Item (Aitem);
5972 -- Now we can add the expression
5975 Expr := Relocate_Node (Arg2);
5977 -- There already was a predicate, so add to it
5982 Left_Opnd => Relocate_Node (Expr),
5983 Right_Opnd => Relocate_Node (Arg2));
5988 Next_Rep_Item (Ritem);
5996 function Process_RE (N : Node_Id) return Traverse_Result is
5998 if Nkind (N) = N_Raise_Expression then
5999 Set_Convert_To_Return_False (N);
6010 function Test_RE (N : Node_Id) return Traverse_Result is
6012 if Nkind (N) = N_Raise_Expression then
6013 Raise_Expression_Present := True;
6020 -- Start of processing for Build_Predicate_Functions
6023 -- Return if already built or if type does not have predicates
6025 if not Has_Predicates (Typ)
6026 or else Present (Predicate_Function (Typ))
6031 -- Prepare to construct predicate expression
6035 -- Add Predicates for the current type
6039 -- Add predicates for ancestor if present
6042 Atyp : constant Entity_Id := Nearest_Ancestor (Typ);
6044 if Present (Atyp) then
6049 -- Case where predicates are present
6051 if Present (Expr) then
6053 -- Test for raise expression present
6057 -- If raise expression is present, capture a copy of Expr for use
6058 -- in building the predicateM function version later on. For this
6059 -- copy we replace references to Object_Entity by Object_Entity_M.
6061 if Raise_Expression_Present then
6063 Map : constant Elist_Id := New_Elmt_List;
6065 Append_Elmt (Object_Entity, Map);
6066 Append_Elmt (Object_Entity_M, Map);
6067 Expr_M := New_Copy_Tree (Expr, Map => Map);
6071 -- Build the main predicate function
6074 SId : constant Entity_Id :=
6075 Make_Defining_Identifier (Loc,
6076 Chars => New_External_Name (Chars (Typ), "Predicate"));
6077 -- The entity for the the function spec
6079 SIdB : constant Entity_Id :=
6080 Make_Defining_Identifier (Loc,
6081 Chars => New_External_Name (Chars (Typ), "Predicate"));
6082 -- The entity for the function body
6089 -- Build function declaration
6091 Set_Ekind (SId, E_Function);
6092 Set_Is_Predicate_Function (SId);
6093 Set_Predicate_Function (Typ, SId);
6095 -- The predicate function is shared between views of a type
6097 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
6098 Set_Predicate_Function (Full_View (Typ), SId);
6102 Make_Function_Specification (Loc,
6103 Defining_Unit_Name => SId,
6104 Parameter_Specifications => New_List (
6105 Make_Parameter_Specification (Loc,
6106 Defining_Identifier => Object_Entity,
6107 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
6108 Result_Definition =>
6109 New_Occurrence_Of (Standard_Boolean, Loc));
6112 Make_Subprogram_Declaration (Loc,
6113 Specification => Spec);
6115 -- Build function body
6118 Make_Function_Specification (Loc,
6119 Defining_Unit_Name => SIdB,
6120 Parameter_Specifications => New_List (
6121 Make_Parameter_Specification (Loc,
6122 Defining_Identifier =>
6123 Make_Defining_Identifier (Loc, Object_Name),
6125 New_Occurrence_Of (Typ, Loc))),
6126 Result_Definition =>
6127 New_Occurrence_Of (Standard_Boolean, Loc));
6130 Make_Subprogram_Body (Loc,
6131 Specification => Spec,
6132 Declarations => Empty_List,
6133 Handled_Statement_Sequence =>
6134 Make_Handled_Sequence_Of_Statements (Loc,
6135 Statements => New_List (
6136 Make_Simple_Return_Statement (Loc,
6137 Expression => Expr))));
6139 -- Insert declaration before freeze node and body after
6141 Insert_Before_And_Analyze (N, FDecl);
6142 Insert_After_And_Analyze (N, FBody);
6145 -- Test for raise expressions present and if so build M version
6147 if Raise_Expression_Present then
6149 SId : constant Entity_Id :=
6150 Make_Defining_Identifier (Loc,
6151 Chars => New_External_Name (Chars (Typ), "PredicateM"));
6152 -- The entity for the the function spec
6154 SIdB : constant Entity_Id :=
6155 Make_Defining_Identifier (Loc,
6156 Chars => New_External_Name (Chars (Typ), "PredicateM"));
6157 -- The entity for the function body
6165 -- Mark any raise expressions for special expansion
6167 Process_REs (Expr_M);
6169 -- Build function declaration
6171 Set_Ekind (SId, E_Function);
6172 Set_Is_Predicate_Function_M (SId);
6173 Set_Predicate_Function_M (Typ, SId);
6175 -- The predicate function is shared between views of a type
6177 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
6178 Set_Predicate_Function_M (Full_View (Typ), SId);
6182 Make_Function_Specification (Loc,
6183 Defining_Unit_Name => SId,
6184 Parameter_Specifications => New_List (
6185 Make_Parameter_Specification (Loc,
6186 Defining_Identifier => Object_Entity_M,
6187 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
6188 Result_Definition =>
6189 New_Occurrence_Of (Standard_Boolean, Loc));
6192 Make_Subprogram_Declaration (Loc,
6193 Specification => Spec);
6195 -- Build function body
6198 Make_Function_Specification (Loc,
6199 Defining_Unit_Name => SIdB,
6200 Parameter_Specifications => New_List (
6201 Make_Parameter_Specification (Loc,
6202 Defining_Identifier =>
6203 Make_Defining_Identifier (Loc, Object_Name),
6205 New_Occurrence_Of (Typ, Loc))),
6206 Result_Definition =>
6207 New_Occurrence_Of (Standard_Boolean, Loc));
6209 -- Build the body, we declare the boolean expression before
6210 -- doing the return, because we are not really confident of
6211 -- what happens if a return appears within a return!
6214 Make_Defining_Identifier (Loc,
6215 Chars => New_Internal_Name ('B'));
6218 Make_Subprogram_Body (Loc,
6219 Specification => Spec,
6221 Declarations => New_List (
6222 Make_Object_Declaration (Loc,
6223 Defining_Identifier => BTemp,
6224 Constant_Present => True,
6225 Object_Definition =>
6226 New_Reference_To (Standard_Boolean, Loc),
6227 Expression => Expr_M)),
6229 Handled_Statement_Sequence =>
6230 Make_Handled_Sequence_Of_Statements (Loc,
6231 Statements => New_List (
6232 Make_Simple_Return_Statement (Loc,
6233 Expression => New_Reference_To (BTemp, Loc)))));
6235 -- Insert declaration before freeze node and body after
6237 Insert_Before_And_Analyze (N, FDecl);
6238 Insert_After_And_Analyze (N, FBody);
6242 -- Deal with static predicate case
6244 -- ??? We don't currently deal with real types
6245 -- ??? Why requiring that Typ is static?
6247 if Ekind (Typ) in Discrete_Kind
6248 and then Is_Static_Subtype (Typ)
6249 and then not Dynamic_Predicate_Present
6251 -- Only build the predicate for subtypes
6253 if Ekind_In (Typ, E_Enumeration_Subtype,
6254 E_Modular_Integer_Subtype,
6255 E_Signed_Integer_Subtype)
6257 Build_Static_Predicate (Typ, Expr, Object_Name);
6259 if Present (Static_Predicate_Present)
6260 and No (Static_Predicate (Typ))
6263 ("expression does not have required form for "
6264 & "static predicate",
6265 Next (First (Pragma_Argument_Associations
6266 (Static_Predicate_Present))));
6270 -- If a Static_Predicate applies on other types, that's an error:
6271 -- either the type is scalar but non-static, or it's not even a
6272 -- scalar type. We do not issue an error on generated types, as
6273 -- these may be duplicates of the same error on a source type.
6275 elsif Present (Static_Predicate_Present)
6276 and then Comes_From_Source (Typ)
6278 if Is_Scalar_Type (Typ) then
6280 ("static predicate not allowed for non-static type&",
6284 ("static predicate not allowed for non-scalar type&",
6289 end Build_Predicate_Functions;
6291 ----------------------------
6292 -- Build_Static_Predicate --
6293 ----------------------------
6295 procedure Build_Static_Predicate
6300 Loc : constant Source_Ptr := Sloc (Expr);
6302 Non_Static : exception;
6303 -- Raised if something non-static is found
6305 Btyp : constant Entity_Id := Base_Type (Typ);
6307 BLo : constant Uint := Expr_Value (Type_Low_Bound (Btyp));
6308 BHi : constant Uint := Expr_Value (Type_High_Bound (Btyp));
6309 -- Low bound and high bound value of base type of Typ
6311 TLo : constant Uint := Expr_Value (Type_Low_Bound (Typ));
6312 THi : constant Uint := Expr_Value (Type_High_Bound (Typ));
6313 -- Low bound and high bound values of static subtype Typ
6318 -- One entry in a Rlist value, a single REnt (range entry) value denotes
6319 -- one range from Lo to Hi. To represent a single value range Lo = Hi =
6322 type RList is array (Nat range <>) of REnt;
6323 -- A list of ranges. The ranges are sorted in increasing order, and are
6324 -- disjoint (there is a gap of at least one value between each range in
6325 -- the table). A value is in the set of ranges in Rlist if it lies
6326 -- within one of these ranges.
6328 False_Range : constant RList :=
6329 RList'(1 .. 0 => REnt'(No_Uint, No_Uint));
6330 -- An empty set of ranges represents a range list that can never be
6331 -- satisfied, since there are no ranges in which the value could lie,
6332 -- so it does not lie in any of them. False_Range is a canonical value
6333 -- for this empty set, but general processing should test for an Rlist
6334 -- with length zero (see Is_False predicate), since other null ranges
6335 -- may appear which must be treated as False.
6337 True_Range : constant RList := RList'(1 => REnt'(BLo, BHi));
6338 -- Range representing True, value must be in the base range
6340 function "and" (Left : RList; Right : RList) return RList;
6341 -- And's together two range lists, returning a range list. This is a set
6342 -- intersection operation.
6344 function "or" (Left : RList; Right : RList) return RList;
6345 -- Or's together two range lists, returning a range list. This is a set
6348 function "not" (Right : RList) return RList;
6349 -- Returns complement of a given range list, i.e. a range list
6350 -- representing all the values in TLo .. THi that are not in the input
6353 function Build_Val (V : Uint) return Node_Id;
6354 -- Return an analyzed N_Identifier node referencing this value, suitable
6355 -- for use as an entry in the Static_Predicate list. This node is typed
6356 -- with the base type.
6358 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id;
6359 -- Return an analyzed N_Range node referencing this range, suitable for
6360 -- use as an entry in the Static_Predicate list. This node is typed with
6363 function Get_RList (Exp : Node_Id) return RList;
6364 -- This is a recursive routine that converts the given expression into a
6365 -- list of ranges, suitable for use in building the static predicate.
6367 function Is_False (R : RList) return Boolean;
6368 pragma Inline (Is_False);
6369 -- Returns True if the given range list is empty, and thus represents a
6370 -- False list of ranges that can never be satisfied.
6372 function Is_True (R : RList) return Boolean;
6373 -- Returns True if R trivially represents the True predicate by having a
6374 -- single range from BLo to BHi.
6376 function Is_Type_Ref (N : Node_Id) return Boolean;
6377 pragma Inline (Is_Type_Ref);
6378 -- Returns if True if N is a reference to the type for the predicate in
6379 -- the expression (i.e. if it is an identifier whose Chars field matches
6380 -- the Nam given in the call).
6382 function Lo_Val (N : Node_Id) return Uint;
6383 -- Given static expression or static range from a Static_Predicate list,
6384 -- gets expression value or low bound of range.
6386 function Hi_Val (N : Node_Id) return Uint;
6387 -- Given static expression or static range from a Static_Predicate list,
6388 -- gets expression value of high bound of range.
6390 function Membership_Entry (N : Node_Id) return RList;
6391 -- Given a single membership entry (range, value, or subtype), returns
6392 -- the corresponding range list. Raises Static_Error if not static.
6394 function Membership_Entries (N : Node_Id) return RList;
6395 -- Given an element on an alternatives list of a membership operation,
6396 -- returns the range list corresponding to this entry and all following
6397 -- entries (i.e. returns the "or" of this list of values).
6399 function Stat_Pred (Typ : Entity_Id) return RList;
6400 -- Given a type, if it has a static predicate, then return the predicate
6401 -- as a range list, otherwise raise Non_Static.
6407 function "and" (Left : RList; Right : RList) return RList is
6409 -- First range of result
6411 SLeft : Nat := Left'First;
6412 -- Start of rest of left entries
6414 SRight : Nat := Right'First;
6415 -- Start of rest of right entries
6418 -- If either range is True, return the other
6420 if Is_True (Left) then
6422 elsif Is_True (Right) then
6426 -- If either range is False, return False
6428 if Is_False (Left) or else Is_False (Right) then
6432 -- Loop to remove entries at start that are disjoint, and thus just
6433 -- get discarded from the result entirely.
6436 -- If no operands left in either operand, result is false
6438 if SLeft > Left'Last or else SRight > Right'Last then
6441 -- Discard first left operand entry if disjoint with right
6443 elsif Left (SLeft).Hi < Right (SRight).Lo then
6446 -- Discard first right operand entry if disjoint with left
6448 elsif Right (SRight).Hi < Left (SLeft).Lo then
6449 SRight := SRight + 1;
6451 -- Otherwise we have an overlapping entry
6458 -- Now we have two non-null operands, and first entries overlap. The
6459 -- first entry in the result will be the overlapping part of these
6462 FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo),
6463 Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi));
6465 -- Now we can remove the entry that ended at a lower value, since its
6466 -- contribution is entirely contained in Fent.
6468 if Left (SLeft).Hi <= Right (SRight).Hi then
6471 SRight := SRight + 1;
6474 -- Compute result by concatenating this first entry with the "and" of
6475 -- the remaining parts of the left and right operands. Note that if
6476 -- either of these is empty, "and" will yield empty, so that we will
6477 -- end up with just Fent, which is what we want in that case.
6480 FEnt & (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last));
6487 function "not" (Right : RList) return RList is
6489 -- Return True if False range
6491 if Is_False (Right) then
6495 -- Return False if True range
6497 if Is_True (Right) then
6501 -- Here if not trivial case
6504 Result : RList (1 .. Right'Length + 1);
6505 -- May need one more entry for gap at beginning and end
6508 -- Number of entries stored in Result
6513 if Right (Right'First).Lo > TLo then
6515 Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1);
6518 -- Gaps between ranges
6520 for J in Right'First .. Right'Last - 1 loop
6523 REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1);
6528 if Right (Right'Last).Hi < THi then
6530 Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi);
6533 return Result (1 .. Count);
6541 function "or" (Left : RList; Right : RList) return RList is
6543 -- First range of result
6545 SLeft : Nat := Left'First;
6546 -- Start of rest of left entries
6548 SRight : Nat := Right'First;
6549 -- Start of rest of right entries
6552 -- If either range is True, return True
6554 if Is_True (Left) or else Is_True (Right) then
6558 -- If either range is False (empty), return the other
6560 if Is_False (Left) then
6562 elsif Is_False (Right) then
6566 -- Initialize result first entry from left or right operand depending
6567 -- on which starts with the lower range.
6569 if Left (SLeft).Lo < Right (SRight).Lo then
6570 FEnt := Left (SLeft);
6573 FEnt := Right (SRight);
6574 SRight := SRight + 1;
6577 -- This loop eats ranges from left and right operands that are
6578 -- contiguous with the first range we are gathering.
6581 -- Eat first entry in left operand if contiguous or overlapped by
6582 -- gathered first operand of result.
6584 if SLeft <= Left'Last
6585 and then Left (SLeft).Lo <= FEnt.Hi + 1
6587 FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi);
6590 -- Eat first entry in right operand if contiguous or overlapped by
6591 -- gathered right operand of result.
6593 elsif SRight <= Right'Last
6594 and then Right (SRight).Lo <= FEnt.Hi + 1
6596 FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi);
6597 SRight := SRight + 1;
6599 -- All done if no more entries to eat
6606 -- Obtain result as the first entry we just computed, concatenated
6607 -- to the "or" of the remaining results (if one operand is empty,
6608 -- this will just concatenate with the other
6611 FEnt & (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last));
6618 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id is
6624 Low_Bound => Build_Val (Lo),
6625 High_Bound => Build_Val (Hi));
6626 Set_Etype (Result, Btyp);
6627 Set_Analyzed (Result);
6636 function Build_Val (V : Uint) return Node_Id is
6640 if Is_Enumeration_Type (Typ) then
6641 Result := Get_Enum_Lit_From_Pos (Typ, V, Loc);
6643 Result := Make_Integer_Literal (Loc, V);
6646 Set_Etype (Result, Btyp);
6647 Set_Is_Static_Expression (Result);
6648 Set_Analyzed (Result);
6656 function Get_RList (Exp : Node_Id) return RList is
6661 -- Static expression can only be true or false
6663 if Is_OK_Static_Expression (Exp) then
6667 if Expr_Value (Exp) = 0 then
6674 -- Otherwise test node type
6682 when N_Op_And | N_And_Then =>
6683 return Get_RList (Left_Opnd (Exp))
6685 Get_RList (Right_Opnd (Exp));
6689 when N_Op_Or | N_Or_Else =>
6690 return Get_RList (Left_Opnd (Exp))
6692 Get_RList (Right_Opnd (Exp));
6697 return not Get_RList (Right_Opnd (Exp));
6699 -- Comparisons of type with static value
6701 when N_Op_Compare =>
6703 -- Type is left operand
6705 if Is_Type_Ref (Left_Opnd (Exp))
6706 and then Is_OK_Static_Expression (Right_Opnd (Exp))
6708 Val := Expr_Value (Right_Opnd (Exp));
6710 -- Typ is right operand
6712 elsif Is_Type_Ref (Right_Opnd (Exp))
6713 and then Is_OK_Static_Expression (Left_Opnd (Exp))
6715 Val := Expr_Value (Left_Opnd (Exp));
6717 -- Invert sense of comparison
6720 when N_Op_Gt => Op := N_Op_Lt;
6721 when N_Op_Lt => Op := N_Op_Gt;
6722 when N_Op_Ge => Op := N_Op_Le;
6723 when N_Op_Le => Op := N_Op_Ge;
6724 when others => null;
6727 -- Other cases are non-static
6733 -- Construct range according to comparison operation
6737 return RList'(1 => REnt'(Val, Val));
6740 return RList'(1 => REnt'(Val, BHi));
6743 return RList'(1 => REnt'(Val + 1, BHi));
6746 return RList'(1 => REnt'(BLo, Val));
6749 return RList'(1 => REnt'(BLo, Val - 1));
6752 return RList'(REnt'(BLo, Val - 1),
6753 REnt'(Val + 1, BHi));
6756 raise Program_Error;
6762 if not Is_Type_Ref (Left_Opnd (Exp)) then
6766 if Present (Right_Opnd (Exp)) then
6767 return Membership_Entry (Right_Opnd (Exp));
6769 return Membership_Entries (First (Alternatives (Exp)));
6772 -- Negative membership (NOT IN)
6775 if not Is_Type_Ref (Left_Opnd (Exp)) then
6779 if Present (Right_Opnd (Exp)) then
6780 return not Membership_Entry (Right_Opnd (Exp));
6782 return not Membership_Entries (First (Alternatives (Exp)));
6785 -- Function call, may be call to static predicate
6787 when N_Function_Call =>
6788 if Is_Entity_Name (Name (Exp)) then
6790 Ent : constant Entity_Id := Entity (Name (Exp));
6792 if Is_Predicate_Function (Ent)
6794 Is_Predicate_Function_M (Ent)
6796 return Stat_Pred (Etype (First_Formal (Ent)));
6801 -- Other function call cases are non-static
6805 -- Qualified expression, dig out the expression
6807 when N_Qualified_Expression =>
6808 return Get_RList (Expression (Exp));
6813 return (Get_RList (Left_Opnd (Exp))
6814 and not Get_RList (Right_Opnd (Exp)))
6815 or (Get_RList (Right_Opnd (Exp))
6816 and not Get_RList (Left_Opnd (Exp)));
6818 -- Any other node type is non-static
6829 function Hi_Val (N : Node_Id) return Uint is
6831 if Is_Static_Expression (N) then
6832 return Expr_Value (N);
6834 pragma Assert (Nkind (N) = N_Range);
6835 return Expr_Value (High_Bound (N));
6843 function Is_False (R : RList) return Boolean is
6845 return R'Length = 0;
6852 function Is_True (R : RList) return Boolean is
6855 and then R (R'First).Lo = BLo
6856 and then R (R'First).Hi = BHi;
6863 function Is_Type_Ref (N : Node_Id) return Boolean is
6865 return Nkind (N) = N_Identifier and then Chars (N) = Nam;
6872 function Lo_Val (N : Node_Id) return Uint is
6874 if Is_Static_Expression (N) then
6875 return Expr_Value (N);
6877 pragma Assert (Nkind (N) = N_Range);
6878 return Expr_Value (Low_Bound (N));
6882 ------------------------
6883 -- Membership_Entries --
6884 ------------------------
6886 function Membership_Entries (N : Node_Id) return RList is
6888 if No (Next (N)) then
6889 return Membership_Entry (N);
6891 return Membership_Entry (N) or Membership_Entries (Next (N));
6893 end Membership_Entries;
6895 ----------------------
6896 -- Membership_Entry --
6897 ----------------------
6899 function Membership_Entry (N : Node_Id) return RList is
6907 if Nkind (N) = N_Range then
6908 if not Is_Static_Expression (Low_Bound (N))
6910 not Is_Static_Expression (High_Bound (N))
6914 SLo := Expr_Value (Low_Bound (N));
6915 SHi := Expr_Value (High_Bound (N));
6916 return RList'(1 => REnt'(SLo, SHi));
6919 -- Static expression case
6921 elsif Is_Static_Expression (N) then
6922 Val := Expr_Value (N);
6923 return RList'(1 => REnt'(Val, Val));
6925 -- Identifier (other than static expression) case
6927 else pragma Assert (Nkind (N) = N_Identifier);
6931 if Is_Type (Entity (N)) then
6933 -- If type has predicates, process them
6935 if Has_Predicates (Entity (N)) then
6936 return Stat_Pred (Entity (N));
6938 -- For static subtype without predicates, get range
6940 elsif Is_Static_Subtype (Entity (N)) then
6941 SLo := Expr_Value (Type_Low_Bound (Entity (N)));
6942 SHi := Expr_Value (Type_High_Bound (Entity (N)));
6943 return RList'(1 => REnt'(SLo, SHi));
6945 -- Any other type makes us non-static
6951 -- Any other kind of identifier in predicate (e.g. a non-static
6952 -- expression value) means this is not a static predicate.
6958 end Membership_Entry;
6964 function Stat_Pred (Typ : Entity_Id) return RList is
6966 -- Not static if type does not have static predicates
6968 if not Has_Predicates (Typ) or else No (Static_Predicate (Typ)) then
6972 -- Otherwise we convert the predicate list to a range list
6975 Result : RList (1 .. List_Length (Static_Predicate (Typ)));
6979 P := First (Static_Predicate (Typ));
6980 for J in Result'Range loop
6981 Result (J) := REnt'(Lo_Val (P), Hi_Val (P));
6989 -- Start of processing for Build_Static_Predicate
6992 -- Now analyze the expression to see if it is a static predicate
6995 Ranges : constant RList := Get_RList (Expr);
6996 -- Range list from expression if it is static
7001 -- Convert range list into a form for the static predicate. In the
7002 -- Ranges array, we just have raw ranges, these must be converted
7003 -- to properly typed and analyzed static expressions or range nodes.
7005 -- Note: here we limit ranges to the ranges of the subtype, so that
7006 -- a predicate is always false for values outside the subtype. That
7007 -- seems fine, such values are invalid anyway, and considering them
7008 -- to fail the predicate seems allowed and friendly, and furthermore
7009 -- simplifies processing for case statements and loops.
7013 for J in Ranges'Range loop
7015 Lo : Uint := Ranges (J).Lo;
7016 Hi : Uint := Ranges (J).Hi;
7019 -- Ignore completely out of range entry
7021 if Hi < TLo or else Lo > THi then
7024 -- Otherwise process entry
7027 -- Adjust out of range value to subtype range
7037 -- Convert range into required form
7039 Append_To (Plist, Build_Range (Lo, Hi));
7044 -- Processing was successful and all entries were static, so now we
7045 -- can store the result as the predicate list.
7047 Set_Static_Predicate (Typ, Plist);
7049 -- The processing for static predicates put the expression into
7050 -- canonical form as a series of ranges. It also eliminated
7051 -- duplicates and collapsed and combined ranges. We might as well
7052 -- replace the alternatives list of the right operand of the
7053 -- membership test with the static predicate list, which will
7054 -- usually be more efficient.
7057 New_Alts : constant List_Id := New_List;
7062 Old_Node := First (Plist);
7063 while Present (Old_Node) loop
7064 New_Node := New_Copy (Old_Node);
7066 if Nkind (New_Node) = N_Range then
7067 Set_Low_Bound (New_Node, New_Copy (Low_Bound (Old_Node)));
7068 Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node)));
7071 Append_To (New_Alts, New_Node);
7075 -- If empty list, replace by False
7077 if Is_Empty_List (New_Alts) then
7078 Rewrite (Expr, New_Occurrence_Of (Standard_False, Loc));
7080 -- Else replace by set membership test
7085 Left_Opnd => Make_Identifier (Loc, Nam),
7086 Right_Opnd => Empty,
7087 Alternatives => New_Alts));
7089 -- Resolve new expression in function context
7091 Install_Formals (Predicate_Function (Typ));
7092 Push_Scope (Predicate_Function (Typ));
7093 Analyze_And_Resolve (Expr, Standard_Boolean);
7099 -- If non-static, return doing nothing
7104 end Build_Static_Predicate;
7106 -----------------------------------------
7107 -- Check_Aspect_At_End_Of_Declarations --
7108 -----------------------------------------
7110 procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id) is
7111 Ent : constant Entity_Id := Entity (ASN);
7112 Ident : constant Node_Id := Identifier (ASN);
7113 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
7115 End_Decl_Expr : constant Node_Id := Entity (Ident);
7116 -- Expression to be analyzed at end of declarations
7118 Freeze_Expr : constant Node_Id := Expression (ASN);
7119 -- Expression from call to Check_Aspect_At_Freeze_Point
7121 T : constant Entity_Id := Etype (Freeze_Expr);
7122 -- Type required for preanalyze call
7125 -- Set False if error
7127 -- On entry to this procedure, Entity (Ident) contains a copy of the
7128 -- original expression from the aspect, saved for this purpose, and
7129 -- but Expression (Ident) is a preanalyzed copy of the expression,
7130 -- preanalyzed just after the freeze point.
7132 procedure Check_Overloaded_Name;
7133 -- For aspects whose expression is simply a name, this routine checks if
7134 -- the name is overloaded or not. If so, it verifies there is an
7135 -- interpretation that matches the entity obtained at the freeze point,
7136 -- otherwise the compiler complains.
7138 ---------------------------
7139 -- Check_Overloaded_Name --
7140 ---------------------------
7142 procedure Check_Overloaded_Name is
7144 if not Is_Overloaded (End_Decl_Expr) then
7145 Err := Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
7151 Index : Interp_Index;
7155 Get_First_Interp (End_Decl_Expr, Index, It);
7156 while Present (It.Typ) loop
7157 if It.Nam = Entity (Freeze_Expr) then
7162 Get_Next_Interp (Index, It);
7166 end Check_Overloaded_Name;
7168 -- Start of processing for Check_Aspect_At_End_Of_Declarations
7171 -- Case of aspects Dimension, Dimension_System and Synchronization
7173 if A_Id = Aspect_Synchronization then
7176 -- Case of stream attributes, just have to compare entities. However,
7177 -- the expression is just a name (possibly overloaded), and there may
7178 -- be stream operations declared for unrelated types, so we just need
7179 -- to verify that one of these interpretations is the one available at
7180 -- at the freeze point.
7182 elsif A_Id = Aspect_Input or else
7183 A_Id = Aspect_Output or else
7184 A_Id = Aspect_Read or else
7187 Analyze (End_Decl_Expr);
7188 Check_Overloaded_Name;
7190 elsif A_Id = Aspect_Variable_Indexing or else
7191 A_Id = Aspect_Constant_Indexing or else
7192 A_Id = Aspect_Default_Iterator or else
7193 A_Id = Aspect_Iterator_Element
7195 -- Make type unfrozen before analysis, to prevent spurious errors
7196 -- about late attributes.
7198 Set_Is_Frozen (Ent, False);
7199 Analyze (End_Decl_Expr);
7200 Set_Is_Frozen (Ent, True);
7202 -- If the end of declarations comes before any other freeze
7203 -- point, the Freeze_Expr is not analyzed: no check needed.
7205 if Analyzed (Freeze_Expr) and then not In_Instance then
7206 Check_Overloaded_Name;
7214 -- In a generic context the aspect expressions have not been
7215 -- preanalyzed, so do it now. There are no conformance checks
7216 -- to perform in this case.
7219 Check_Aspect_At_Freeze_Point (ASN);
7222 -- The default values attributes may be defined in the private part,
7223 -- and the analysis of the expression may take place when only the
7224 -- partial view is visible. The expression must be scalar, so use
7225 -- the full view to resolve.
7227 elsif (A_Id = Aspect_Default_Value
7229 A_Id = Aspect_Default_Component_Value)
7230 and then Is_Private_Type (T)
7232 Preanalyze_Spec_Expression (End_Decl_Expr, Full_View (T));
7234 Preanalyze_Spec_Expression (End_Decl_Expr, T);
7237 Err := not Fully_Conformant_Expressions (End_Decl_Expr, Freeze_Expr);
7240 -- Output error message if error
7244 ("visibility of aspect for& changes after freeze point",
7247 ("info: & is frozen here, aspects evaluated at this point??",
7248 Freeze_Node (Ent), Ent);
7250 end Check_Aspect_At_End_Of_Declarations;
7252 ----------------------------------
7253 -- Check_Aspect_At_Freeze_Point --
7254 ----------------------------------
7256 procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id) is
7257 Ident : constant Node_Id := Identifier (ASN);
7258 -- Identifier (use Entity field to save expression)
7260 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
7262 T : Entity_Id := Empty;
7263 -- Type required for preanalyze call
7266 -- On entry to this procedure, Entity (Ident) contains a copy of the
7267 -- original expression from the aspect, saved for this purpose.
7269 -- On exit from this procedure Entity (Ident) is unchanged, still
7270 -- containing that copy, but Expression (Ident) is a preanalyzed copy
7271 -- of the expression, preanalyzed just after the freeze point.
7273 -- Make a copy of the expression to be preanalyzed
7275 Set_Expression (ASN, New_Copy_Tree (Entity (Ident)));
7277 -- Find type for preanalyze call
7281 -- No_Aspect should be impossible
7284 raise Program_Error;
7286 -- Aspects taking an optional boolean argument
7288 when Boolean_Aspects |
7289 Library_Unit_Aspects =>
7290 T := Standard_Boolean;
7292 -- Aspects corresponding to attribute definition clauses
7294 when Aspect_Address =>
7295 T := RTE (RE_Address);
7297 when Aspect_Attach_Handler =>
7298 T := RTE (RE_Interrupt_ID);
7300 when Aspect_Bit_Order | Aspect_Scalar_Storage_Order =>
7301 T := RTE (RE_Bit_Order);
7303 when Aspect_Convention =>
7307 T := RTE (RE_CPU_Range);
7309 -- Default_Component_Value is resolved with the component type
7311 when Aspect_Default_Component_Value =>
7312 T := Component_Type (Entity (ASN));
7314 -- Default_Value is resolved with the type entity in question
7316 when Aspect_Default_Value =>
7319 -- Depends is a delayed aspect because it mentiones names first
7320 -- introduced by aspect Global which is already delayed. There is
7321 -- no action to be taken with respect to the aspect itself as the
7322 -- analysis is done by the corresponding pragma.
7324 when Aspect_Depends =>
7327 when Aspect_Dispatching_Domain =>
7328 T := RTE (RE_Dispatching_Domain);
7330 when Aspect_External_Tag =>
7331 T := Standard_String;
7333 when Aspect_External_Name =>
7334 T := Standard_String;
7336 -- Global is a delayed aspect because it may reference names that
7337 -- have not been declared yet. There is no action to be taken with
7338 -- respect to the aspect itself as the reference checking is done
7339 -- on the corresponding pragma.
7341 when Aspect_Global =>
7344 when Aspect_Link_Name =>
7345 T := Standard_String;
7347 when Aspect_Priority | Aspect_Interrupt_Priority =>
7348 T := Standard_Integer;
7350 when Aspect_Relative_Deadline =>
7351 T := RTE (RE_Time_Span);
7353 when Aspect_Small =>
7354 T := Universal_Real;
7356 -- For a simple storage pool, we have to retrieve the type of the
7357 -- pool object associated with the aspect's corresponding attribute
7358 -- definition clause.
7360 when Aspect_Simple_Storage_Pool =>
7361 T := Etype (Expression (Aspect_Rep_Item (ASN)));
7363 when Aspect_Storage_Pool =>
7364 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
7366 when Aspect_Alignment |
7367 Aspect_Component_Size |
7368 Aspect_Machine_Radix |
7369 Aspect_Object_Size |
7371 Aspect_Storage_Size |
7372 Aspect_Stream_Size |
7373 Aspect_Value_Size =>
7376 when Aspect_Synchronization =>
7379 -- Special case, the expression of these aspects is just an entity
7380 -- that does not need any resolution, so just analyze.
7389 Analyze (Expression (ASN));
7392 -- Same for Iterator aspects, where the expression is a function
7393 -- name. Legality rules are checked separately.
7395 when Aspect_Constant_Indexing |
7396 Aspect_Default_Iterator |
7397 Aspect_Iterator_Element |
7398 Aspect_Variable_Indexing =>
7399 Analyze (Expression (ASN));
7402 -- Invariant/Predicate take boolean expressions
7404 when Aspect_Dynamic_Predicate |
7407 Aspect_Static_Predicate |
7408 Aspect_Type_Invariant =>
7409 T := Standard_Boolean;
7411 -- Here is the list of aspects that don't require delay analysis
7413 when Aspect_Abstract_State |
7414 Aspect_Contract_Cases |
7416 Aspect_Dimension_System |
7417 Aspect_Implicit_Dereference |
7419 Aspect_Postcondition |
7421 Aspect_Precondition |
7423 raise Program_Error;
7427 -- Do the preanalyze call
7429 Preanalyze_Spec_Expression (Expression (ASN), T);
7430 end Check_Aspect_At_Freeze_Point;
7432 -----------------------------------
7433 -- Check_Constant_Address_Clause --
7434 -----------------------------------
7436 procedure Check_Constant_Address_Clause
7440 procedure Check_At_Constant_Address (Nod : Node_Id);
7441 -- Checks that the given node N represents a name whose 'Address is
7442 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
7443 -- address value is the same at the point of declaration of U_Ent and at
7444 -- the time of elaboration of the address clause.
7446 procedure Check_Expr_Constants (Nod : Node_Id);
7447 -- Checks that Nod meets the requirements for a constant address clause
7448 -- in the sense of the enclosing procedure.
7450 procedure Check_List_Constants (Lst : List_Id);
7451 -- Check that all elements of list Lst meet the requirements for a
7452 -- constant address clause in the sense of the enclosing procedure.
7454 -------------------------------
7455 -- Check_At_Constant_Address --
7456 -------------------------------
7458 procedure Check_At_Constant_Address (Nod : Node_Id) is
7460 if Is_Entity_Name (Nod) then
7461 if Present (Address_Clause (Entity ((Nod)))) then
7463 ("invalid address clause for initialized object &!",
7466 ("address for& cannot" &
7467 " depend on another address clause! (RM 13.1(22))!",
7470 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
7471 and then Sloc (U_Ent) < Sloc (Entity (Nod))
7474 ("invalid address clause for initialized object &!",
7476 Error_Msg_Node_2 := U_Ent;
7478 ("\& must be defined before & (RM 13.1(22))!",
7482 elsif Nkind (Nod) = N_Selected_Component then
7484 T : constant Entity_Id := Etype (Prefix (Nod));
7487 if (Is_Record_Type (T)
7488 and then Has_Discriminants (T))
7491 and then Is_Record_Type (Designated_Type (T))
7492 and then Has_Discriminants (Designated_Type (T)))
7495 ("invalid address clause for initialized object &!",
7498 ("\address cannot depend on component" &
7499 " of discriminated record (RM 13.1(22))!",
7502 Check_At_Constant_Address (Prefix (Nod));
7506 elsif Nkind (Nod) = N_Indexed_Component then
7507 Check_At_Constant_Address (Prefix (Nod));
7508 Check_List_Constants (Expressions (Nod));
7511 Check_Expr_Constants (Nod);
7513 end Check_At_Constant_Address;
7515 --------------------------
7516 -- Check_Expr_Constants --
7517 --------------------------
7519 procedure Check_Expr_Constants (Nod : Node_Id) is
7520 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
7521 Ent : Entity_Id := Empty;
7524 if Nkind (Nod) in N_Has_Etype
7525 and then Etype (Nod) = Any_Type
7531 when N_Empty | N_Error =>
7534 when N_Identifier | N_Expanded_Name =>
7535 Ent := Entity (Nod);
7537 -- We need to look at the original node if it is different
7538 -- from the node, since we may have rewritten things and
7539 -- substituted an identifier representing the rewrite.
7541 if Original_Node (Nod) /= Nod then
7542 Check_Expr_Constants (Original_Node (Nod));
7544 -- If the node is an object declaration without initial
7545 -- value, some code has been expanded, and the expression
7546 -- is not constant, even if the constituents might be
7547 -- acceptable, as in A'Address + offset.
7549 if Ekind (Ent) = E_Variable
7551 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
7553 No (Expression (Declaration_Node (Ent)))
7556 ("invalid address clause for initialized object &!",
7559 -- If entity is constant, it may be the result of expanding
7560 -- a check. We must verify that its declaration appears
7561 -- before the object in question, else we also reject the
7564 elsif Ekind (Ent) = E_Constant
7565 and then In_Same_Source_Unit (Ent, U_Ent)
7566 and then Sloc (Ent) > Loc_U_Ent
7569 ("invalid address clause for initialized object &!",
7576 -- Otherwise look at the identifier and see if it is OK
7578 if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
7579 or else Is_Type (Ent)
7584 Ekind (Ent) = E_Constant
7586 Ekind (Ent) = E_In_Parameter
7588 -- This is the case where we must have Ent defined before
7589 -- U_Ent. Clearly if they are in different units this
7590 -- requirement is met since the unit containing Ent is
7591 -- already processed.
7593 if not In_Same_Source_Unit (Ent, U_Ent) then
7596 -- Otherwise location of Ent must be before the location
7597 -- of U_Ent, that's what prior defined means.
7599 elsif Sloc (Ent) < Loc_U_Ent then
7604 ("invalid address clause for initialized object &!",
7606 Error_Msg_Node_2 := U_Ent;
7608 ("\& must be defined before & (RM 13.1(22))!",
7612 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
7613 Check_Expr_Constants (Original_Node (Nod));
7617 ("invalid address clause for initialized object &!",
7620 if Comes_From_Source (Ent) then
7622 ("\reference to variable& not allowed"
7623 & " (RM 13.1(22))!", Nod, Ent);
7626 ("non-static expression not allowed"
7627 & " (RM 13.1(22))!", Nod);
7631 when N_Integer_Literal =>
7633 -- If this is a rewritten unchecked conversion, in a system
7634 -- where Address is an integer type, always use the base type
7635 -- for a literal value. This is user-friendly and prevents
7636 -- order-of-elaboration issues with instances of unchecked
7639 if Nkind (Original_Node (Nod)) = N_Function_Call then
7640 Set_Etype (Nod, Base_Type (Etype (Nod)));
7643 when N_Real_Literal |
7645 N_Character_Literal =>
7649 Check_Expr_Constants (Low_Bound (Nod));
7650 Check_Expr_Constants (High_Bound (Nod));
7652 when N_Explicit_Dereference =>
7653 Check_Expr_Constants (Prefix (Nod));
7655 when N_Indexed_Component =>
7656 Check_Expr_Constants (Prefix (Nod));
7657 Check_List_Constants (Expressions (Nod));
7660 Check_Expr_Constants (Prefix (Nod));
7661 Check_Expr_Constants (Discrete_Range (Nod));
7663 when N_Selected_Component =>
7664 Check_Expr_Constants (Prefix (Nod));
7666 when N_Attribute_Reference =>
7667 if Nam_In (Attribute_Name (Nod), Name_Address,
7669 Name_Unchecked_Access,
7670 Name_Unrestricted_Access)
7672 Check_At_Constant_Address (Prefix (Nod));
7675 Check_Expr_Constants (Prefix (Nod));
7676 Check_List_Constants (Expressions (Nod));
7680 Check_List_Constants (Component_Associations (Nod));
7681 Check_List_Constants (Expressions (Nod));
7683 when N_Component_Association =>
7684 Check_Expr_Constants (Expression (Nod));
7686 when N_Extension_Aggregate =>
7687 Check_Expr_Constants (Ancestor_Part (Nod));
7688 Check_List_Constants (Component_Associations (Nod));
7689 Check_List_Constants (Expressions (Nod));
7694 when N_Binary_Op | N_Short_Circuit | N_Membership_Test =>
7695 Check_Expr_Constants (Left_Opnd (Nod));
7696 Check_Expr_Constants (Right_Opnd (Nod));
7699 Check_Expr_Constants (Right_Opnd (Nod));
7701 when N_Type_Conversion |
7702 N_Qualified_Expression |
7704 N_Unchecked_Type_Conversion =>
7705 Check_Expr_Constants (Expression (Nod));
7707 when N_Function_Call =>
7708 if not Is_Pure (Entity (Name (Nod))) then
7710 ("invalid address clause for initialized object &!",
7714 ("\function & is not pure (RM 13.1(22))!",
7715 Nod, Entity (Name (Nod)));
7718 Check_List_Constants (Parameter_Associations (Nod));
7721 when N_Parameter_Association =>
7722 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
7726 ("invalid address clause for initialized object &!",
7729 ("\must be constant defined before& (RM 13.1(22))!",
7732 end Check_Expr_Constants;
7734 --------------------------
7735 -- Check_List_Constants --
7736 --------------------------
7738 procedure Check_List_Constants (Lst : List_Id) is
7742 if Present (Lst) then
7743 Nod1 := First (Lst);
7744 while Present (Nod1) loop
7745 Check_Expr_Constants (Nod1);
7749 end Check_List_Constants;
7751 -- Start of processing for Check_Constant_Address_Clause
7754 -- If rep_clauses are to be ignored, no need for legality checks. In
7755 -- particular, no need to pester user about rep clauses that violate
7756 -- the rule on constant addresses, given that these clauses will be
7757 -- removed by Freeze before they reach the back end.
7759 if not Ignore_Rep_Clauses then
7760 Check_Expr_Constants (Expr);
7762 end Check_Constant_Address_Clause;
7764 ----------------------------------------
7765 -- Check_Record_Representation_Clause --
7766 ----------------------------------------
7768 procedure Check_Record_Representation_Clause (N : Node_Id) is
7769 Loc : constant Source_Ptr := Sloc (N);
7770 Ident : constant Node_Id := Identifier (N);
7771 Rectype : Entity_Id;
7776 Hbit : Uint := Uint_0;
7780 Max_Bit_So_Far : Uint;
7781 -- Records the maximum bit position so far. If all field positions
7782 -- are monotonically increasing, then we can skip the circuit for
7783 -- checking for overlap, since no overlap is possible.
7785 Tagged_Parent : Entity_Id := Empty;
7786 -- This is set in the case of a derived tagged type for which we have
7787 -- Is_Fully_Repped_Tagged_Type True (indicating that all components are
7788 -- positioned by record representation clauses). In this case we must
7789 -- check for overlap between components of this tagged type, and the
7790 -- components of its parent. Tagged_Parent will point to this parent
7791 -- type. For all other cases Tagged_Parent is left set to Empty.
7793 Parent_Last_Bit : Uint;
7794 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
7795 -- last bit position for any field in the parent type. We only need to
7796 -- check overlap for fields starting below this point.
7798 Overlap_Check_Required : Boolean;
7799 -- Used to keep track of whether or not an overlap check is required
7801 Overlap_Detected : Boolean := False;
7802 -- Set True if an overlap is detected
7804 Ccount : Natural := 0;
7805 -- Number of component clauses in record rep clause
7807 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
7808 -- Given two entities for record components or discriminants, checks
7809 -- if they have overlapping component clauses and issues errors if so.
7811 procedure Find_Component;
7812 -- Finds component entity corresponding to current component clause (in
7813 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
7814 -- start/stop bits for the field. If there is no matching component or
7815 -- if the matching component does not have a component clause, then
7816 -- that's an error and Comp is set to Empty, but no error message is
7817 -- issued, since the message was already given. Comp is also set to
7818 -- Empty if the current "component clause" is in fact a pragma.
7820 -----------------------------
7821 -- Check_Component_Overlap --
7822 -----------------------------
7824 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
7825 CC1 : constant Node_Id := Component_Clause (C1_Ent);
7826 CC2 : constant Node_Id := Component_Clause (C2_Ent);
7829 if Present (CC1) and then Present (CC2) then
7831 -- Exclude odd case where we have two tag components in the same
7832 -- record, both at location zero. This seems a bit strange, but
7833 -- it seems to happen in some circumstances, perhaps on an error.
7835 if Nam_In (Chars (C1_Ent), Name_uTag, Name_uTag) then
7839 -- Here we check if the two fields overlap
7842 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
7843 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
7844 E1 : constant Uint := S1 + Esize (C1_Ent);
7845 E2 : constant Uint := S2 + Esize (C2_Ent);
7848 if E2 <= S1 or else E1 <= S2 then
7851 Error_Msg_Node_2 := Component_Name (CC2);
7852 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
7853 Error_Msg_Node_1 := Component_Name (CC1);
7855 ("component& overlaps & #", Component_Name (CC1));
7856 Overlap_Detected := True;
7860 end Check_Component_Overlap;
7862 --------------------
7863 -- Find_Component --
7864 --------------------
7866 procedure Find_Component is
7868 procedure Search_Component (R : Entity_Id);
7869 -- Search components of R for a match. If found, Comp is set
7871 ----------------------
7872 -- Search_Component --
7873 ----------------------
7875 procedure Search_Component (R : Entity_Id) is
7877 Comp := First_Component_Or_Discriminant (R);
7878 while Present (Comp) loop
7880 -- Ignore error of attribute name for component name (we
7881 -- already gave an error message for this, so no need to
7884 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
7887 exit when Chars (Comp) = Chars (Component_Name (CC));
7890 Next_Component_Or_Discriminant (Comp);
7892 end Search_Component;
7894 -- Start of processing for Find_Component
7897 -- Return with Comp set to Empty if we have a pragma
7899 if Nkind (CC) = N_Pragma then
7904 -- Search current record for matching component
7906 Search_Component (Rectype);
7908 -- If not found, maybe component of base type discriminant that is
7909 -- absent from statically constrained first subtype.
7912 Search_Component (Base_Type (Rectype));
7915 -- If no component, or the component does not reference the component
7916 -- clause in question, then there was some previous error for which
7917 -- we already gave a message, so just return with Comp Empty.
7919 if No (Comp) or else Component_Clause (Comp) /= CC then
7920 Check_Error_Detected;
7923 -- Normal case where we have a component clause
7926 Fbit := Component_Bit_Offset (Comp);
7927 Lbit := Fbit + Esize (Comp) - 1;
7931 -- Start of processing for Check_Record_Representation_Clause
7935 Rectype := Entity (Ident);
7937 if Rectype = Any_Type then
7940 Rectype := Underlying_Type (Rectype);
7943 -- See if we have a fully repped derived tagged type
7946 PS : constant Entity_Id := Parent_Subtype (Rectype);
7949 if Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
7950 Tagged_Parent := PS;
7952 -- Find maximum bit of any component of the parent type
7954 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
7955 Pcomp := First_Entity (Tagged_Parent);
7956 while Present (Pcomp) loop
7957 if Ekind_In (Pcomp, E_Discriminant, E_Component) then
7958 if Component_Bit_Offset (Pcomp) /= No_Uint
7959 and then Known_Static_Esize (Pcomp)
7964 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
7967 Next_Entity (Pcomp);
7973 -- All done if no component clauses
7975 CC := First (Component_Clauses (N));
7981 -- If a tag is present, then create a component clause that places it
7982 -- at the start of the record (otherwise gigi may place it after other
7983 -- fields that have rep clauses).
7985 Fent := First_Entity (Rectype);
7987 if Nkind (Fent) = N_Defining_Identifier
7988 and then Chars (Fent) = Name_uTag
7990 Set_Component_Bit_Offset (Fent, Uint_0);
7991 Set_Normalized_Position (Fent, Uint_0);
7992 Set_Normalized_First_Bit (Fent, Uint_0);
7993 Set_Normalized_Position_Max (Fent, Uint_0);
7994 Init_Esize (Fent, System_Address_Size);
7996 Set_Component_Clause (Fent,
7997 Make_Component_Clause (Loc,
7998 Component_Name => Make_Identifier (Loc, Name_uTag),
8000 Position => Make_Integer_Literal (Loc, Uint_0),
8001 First_Bit => Make_Integer_Literal (Loc, Uint_0),
8003 Make_Integer_Literal (Loc,
8004 UI_From_Int (System_Address_Size))));
8006 Ccount := Ccount + 1;
8009 Max_Bit_So_Far := Uint_Minus_1;
8010 Overlap_Check_Required := False;
8012 -- Process the component clauses
8014 while Present (CC) loop
8017 if Present (Comp) then
8018 Ccount := Ccount + 1;
8020 -- We need a full overlap check if record positions non-monotonic
8022 if Fbit <= Max_Bit_So_Far then
8023 Overlap_Check_Required := True;
8026 Max_Bit_So_Far := Lbit;
8028 -- Check bit position out of range of specified size
8030 if Has_Size_Clause (Rectype)
8031 and then RM_Size (Rectype) <= Lbit
8034 ("bit number out of range of specified size",
8037 -- Check for overlap with tag component
8040 if Is_Tagged_Type (Rectype)
8041 and then Fbit < System_Address_Size
8044 ("component overlaps tag field of&",
8045 Component_Name (CC), Rectype);
8046 Overlap_Detected := True;
8054 -- Check parent overlap if component might overlap parent field
8056 if Present (Tagged_Parent) and then Fbit <= Parent_Last_Bit then
8057 Pcomp := First_Component_Or_Discriminant (Tagged_Parent);
8058 while Present (Pcomp) loop
8059 if not Is_Tag (Pcomp)
8060 and then Chars (Pcomp) /= Name_uParent
8062 Check_Component_Overlap (Comp, Pcomp);
8065 Next_Component_Or_Discriminant (Pcomp);
8073 -- Now that we have processed all the component clauses, check for
8074 -- overlap. We have to leave this till last, since the components can
8075 -- appear in any arbitrary order in the representation clause.
8077 -- We do not need this check if all specified ranges were monotonic,
8078 -- as recorded by Overlap_Check_Required being False at this stage.
8080 -- This first section checks if there are any overlapping entries at
8081 -- all. It does this by sorting all entries and then seeing if there are
8082 -- any overlaps. If there are none, then that is decisive, but if there
8083 -- are overlaps, they may still be OK (they may result from fields in
8084 -- different variants).
8086 if Overlap_Check_Required then
8087 Overlap_Check1 : declare
8089 OC_Fbit : array (0 .. Ccount) of Uint;
8090 -- First-bit values for component clauses, the value is the offset
8091 -- of the first bit of the field from start of record. The zero
8092 -- entry is for use in sorting.
8094 OC_Lbit : array (0 .. Ccount) of Uint;
8095 -- Last-bit values for component clauses, the value is the offset
8096 -- of the last bit of the field from start of record. The zero
8097 -- entry is for use in sorting.
8099 OC_Count : Natural := 0;
8100 -- Count of entries in OC_Fbit and OC_Lbit
8102 function OC_Lt (Op1, Op2 : Natural) return Boolean;
8103 -- Compare routine for Sort
8105 procedure OC_Move (From : Natural; To : Natural);
8106 -- Move routine for Sort
8108 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
8114 function OC_Lt (Op1, Op2 : Natural) return Boolean is
8116 return OC_Fbit (Op1) < OC_Fbit (Op2);
8123 procedure OC_Move (From : Natural; To : Natural) is
8125 OC_Fbit (To) := OC_Fbit (From);
8126 OC_Lbit (To) := OC_Lbit (From);
8129 -- Start of processing for Overlap_Check
8132 CC := First (Component_Clauses (N));
8133 while Present (CC) loop
8135 -- Exclude component clause already marked in error
8137 if not Error_Posted (CC) then
8140 if Present (Comp) then
8141 OC_Count := OC_Count + 1;
8142 OC_Fbit (OC_Count) := Fbit;
8143 OC_Lbit (OC_Count) := Lbit;
8150 Sorting.Sort (OC_Count);
8152 Overlap_Check_Required := False;
8153 for J in 1 .. OC_Count - 1 loop
8154 if OC_Lbit (J) >= OC_Fbit (J + 1) then
8155 Overlap_Check_Required := True;
8162 -- If Overlap_Check_Required is still True, then we have to do the full
8163 -- scale overlap check, since we have at least two fields that do
8164 -- overlap, and we need to know if that is OK since they are in
8165 -- different variant, or whether we have a definite problem.
8167 if Overlap_Check_Required then
8168 Overlap_Check2 : declare
8169 C1_Ent, C2_Ent : Entity_Id;
8170 -- Entities of components being checked for overlap
8173 -- Component_List node whose Component_Items are being checked
8176 -- Component declaration for component being checked
8179 C1_Ent := First_Entity (Base_Type (Rectype));
8181 -- Loop through all components in record. For each component check
8182 -- for overlap with any of the preceding elements on the component
8183 -- list containing the component and also, if the component is in
8184 -- a variant, check against components outside the case structure.
8185 -- This latter test is repeated recursively up the variant tree.
8187 Main_Component_Loop : while Present (C1_Ent) loop
8188 if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
8189 goto Continue_Main_Component_Loop;
8192 -- Skip overlap check if entity has no declaration node. This
8193 -- happens with discriminants in constrained derived types.
8194 -- Possibly we are missing some checks as a result, but that
8195 -- does not seem terribly serious.
8197 if No (Declaration_Node (C1_Ent)) then
8198 goto Continue_Main_Component_Loop;
8201 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
8203 -- Loop through component lists that need checking. Check the
8204 -- current component list and all lists in variants above us.
8206 Component_List_Loop : loop
8208 -- If derived type definition, go to full declaration
8209 -- If at outer level, check discriminants if there are any.
8211 if Nkind (Clist) = N_Derived_Type_Definition then
8212 Clist := Parent (Clist);
8215 -- Outer level of record definition, check discriminants
8217 if Nkind_In (Clist, N_Full_Type_Declaration,
8218 N_Private_Type_Declaration)
8220 if Has_Discriminants (Defining_Identifier (Clist)) then
8222 First_Discriminant (Defining_Identifier (Clist));
8223 while Present (C2_Ent) loop
8224 exit when C1_Ent = C2_Ent;
8225 Check_Component_Overlap (C1_Ent, C2_Ent);
8226 Next_Discriminant (C2_Ent);
8230 -- Record extension case
8232 elsif Nkind (Clist) = N_Derived_Type_Definition then
8235 -- Otherwise check one component list
8238 Citem := First (Component_Items (Clist));
8239 while Present (Citem) loop
8240 if Nkind (Citem) = N_Component_Declaration then
8241 C2_Ent := Defining_Identifier (Citem);
8242 exit when C1_Ent = C2_Ent;
8243 Check_Component_Overlap (C1_Ent, C2_Ent);
8250 -- Check for variants above us (the parent of the Clist can
8251 -- be a variant, in which case its parent is a variant part,
8252 -- and the parent of the variant part is a component list
8253 -- whose components must all be checked against the current
8254 -- component for overlap).
8256 if Nkind (Parent (Clist)) = N_Variant then
8257 Clist := Parent (Parent (Parent (Clist)));
8259 -- Check for possible discriminant part in record, this
8260 -- is treated essentially as another level in the
8261 -- recursion. For this case the parent of the component
8262 -- list is the record definition, and its parent is the
8263 -- full type declaration containing the discriminant
8266 elsif Nkind (Parent (Clist)) = N_Record_Definition then
8267 Clist := Parent (Parent ((Clist)));
8269 -- If neither of these two cases, we are at the top of
8273 exit Component_List_Loop;
8275 end loop Component_List_Loop;
8277 <<Continue_Main_Component_Loop>>
8278 Next_Entity (C1_Ent);
8280 end loop Main_Component_Loop;
8284 -- The following circuit deals with warning on record holes (gaps). We
8285 -- skip this check if overlap was detected, since it makes sense for the
8286 -- programmer to fix this illegality before worrying about warnings.
8288 if not Overlap_Detected and Warn_On_Record_Holes then
8289 Record_Hole_Check : declare
8290 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
8291 -- Full declaration of record type
8293 procedure Check_Component_List
8297 -- Check component list CL for holes. The starting bit should be
8298 -- Sbit. which is zero for the main record component list and set
8299 -- appropriately for recursive calls for variants. DS is set to
8300 -- a list of discriminant specifications to be included in the
8301 -- consideration of components. It is No_List if none to consider.
8303 --------------------------
8304 -- Check_Component_List --
8305 --------------------------
8307 procedure Check_Component_List
8315 Compl := Integer (List_Length (Component_Items (CL)));
8317 if DS /= No_List then
8318 Compl := Compl + Integer (List_Length (DS));
8322 Comps : array (Natural range 0 .. Compl) of Entity_Id;
8323 -- Gather components (zero entry is for sort routine)
8325 Ncomps : Natural := 0;
8326 -- Number of entries stored in Comps (starting at Comps (1))
8329 -- One component item or discriminant specification
8332 -- Starting bit for next component
8340 function Lt (Op1, Op2 : Natural) return Boolean;
8341 -- Compare routine for Sort
8343 procedure Move (From : Natural; To : Natural);
8344 -- Move routine for Sort
8346 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
8352 function Lt (Op1, Op2 : Natural) return Boolean is
8354 return Component_Bit_Offset (Comps (Op1))
8356 Component_Bit_Offset (Comps (Op2));
8363 procedure Move (From : Natural; To : Natural) is
8365 Comps (To) := Comps (From);
8369 -- Gather discriminants into Comp
8371 if DS /= No_List then
8372 Citem := First (DS);
8373 while Present (Citem) loop
8374 if Nkind (Citem) = N_Discriminant_Specification then
8376 Ent : constant Entity_Id :=
8377 Defining_Identifier (Citem);
8379 if Ekind (Ent) = E_Discriminant then
8380 Ncomps := Ncomps + 1;
8381 Comps (Ncomps) := Ent;
8390 -- Gather component entities into Comp
8392 Citem := First (Component_Items (CL));
8393 while Present (Citem) loop
8394 if Nkind (Citem) = N_Component_Declaration then
8395 Ncomps := Ncomps + 1;
8396 Comps (Ncomps) := Defining_Identifier (Citem);
8402 -- Now sort the component entities based on the first bit.
8403 -- Note we already know there are no overlapping components.
8405 Sorting.Sort (Ncomps);
8407 -- Loop through entries checking for holes
8410 for J in 1 .. Ncomps loop
8412 Error_Msg_Uint_1 := Component_Bit_Offset (CEnt) - Nbit;
8414 if Error_Msg_Uint_1 > 0 then
8416 ("?H?^-bit gap before component&",
8417 Component_Name (Component_Clause (CEnt)), CEnt);
8420 Nbit := Component_Bit_Offset (CEnt) + Esize (CEnt);
8423 -- Process variant parts recursively if present
8425 if Present (Variant_Part (CL)) then
8426 Variant := First (Variants (Variant_Part (CL)));
8427 while Present (Variant) loop
8428 Check_Component_List
8429 (Component_List (Variant), Nbit, No_List);
8434 end Check_Component_List;
8436 -- Start of processing for Record_Hole_Check
8443 if Is_Tagged_Type (Rectype) then
8444 Sbit := UI_From_Int (System_Address_Size);
8449 if Nkind (Decl) = N_Full_Type_Declaration
8450 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
8452 Check_Component_List
8453 (Component_List (Type_Definition (Decl)),
8455 Discriminant_Specifications (Decl));
8458 end Record_Hole_Check;
8461 -- For records that have component clauses for all components, and whose
8462 -- size is less than or equal to 32, we need to know the size in the
8463 -- front end to activate possible packed array processing where the
8464 -- component type is a record.
8466 -- At this stage Hbit + 1 represents the first unused bit from all the
8467 -- component clauses processed, so if the component clauses are
8468 -- complete, then this is the length of the record.
8470 -- For records longer than System.Storage_Unit, and for those where not
8471 -- all components have component clauses, the back end determines the
8472 -- length (it may for example be appropriate to round up the size
8473 -- to some convenient boundary, based on alignment considerations, etc).
8475 if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then
8477 -- Nothing to do if at least one component has no component clause
8479 Comp := First_Component_Or_Discriminant (Rectype);
8480 while Present (Comp) loop
8481 exit when No (Component_Clause (Comp));
8482 Next_Component_Or_Discriminant (Comp);
8485 -- If we fall out of loop, all components have component clauses
8486 -- and so we can set the size to the maximum value.
8489 Set_RM_Size (Rectype, Hbit + 1);
8492 end Check_Record_Representation_Clause;
8498 procedure Check_Size
8502 Biased : out Boolean)
8504 UT : constant Entity_Id := Underlying_Type (T);
8510 -- Reject patently improper size values.
8512 if Is_Elementary_Type (T)
8513 and then Siz > UI_From_Int (Int'Last)
8515 Error_Msg_N ("Size value too large for elementary type", N);
8517 if Nkind (Original_Node (N)) = N_Op_Expon then
8519 ("\maybe '* was meant, rather than '*'*", Original_Node (N));
8523 -- Dismiss generic types
8525 if Is_Generic_Type (T)
8527 Is_Generic_Type (UT)
8529 Is_Generic_Type (Root_Type (UT))
8533 -- Guard against previous errors
8535 elsif No (UT) or else UT = Any_Type then
8536 Check_Error_Detected;
8539 -- Check case of bit packed array
8541 elsif Is_Array_Type (UT)
8542 and then Known_Static_Component_Size (UT)
8543 and then Is_Bit_Packed_Array (UT)
8551 Asiz := Component_Size (UT);
8552 Indx := First_Index (UT);
8554 Ityp := Etype (Indx);
8556 -- If non-static bound, then we are not in the business of
8557 -- trying to check the length, and indeed an error will be
8558 -- issued elsewhere, since sizes of non-static array types
8559 -- cannot be set implicitly or explicitly.
8561 if not Is_Static_Subtype (Ityp) then
8565 -- Otherwise accumulate next dimension
8567 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
8568 Expr_Value (Type_Low_Bound (Ityp)) +
8572 exit when No (Indx);
8579 Error_Msg_Uint_1 := Asiz;
8581 ("size for& too small, minimum allowed is ^", N, T);
8582 Set_Esize (T, Asiz);
8583 Set_RM_Size (T, Asiz);
8587 -- All other composite types are ignored
8589 elsif Is_Composite_Type (UT) then
8592 -- For fixed-point types, don't check minimum if type is not frozen,
8593 -- since we don't know all the characteristics of the type that can
8594 -- affect the size (e.g. a specified small) till freeze time.
8596 elsif Is_Fixed_Point_Type (UT)
8597 and then not Is_Frozen (UT)
8601 -- Cases for which a minimum check is required
8604 -- Ignore if specified size is correct for the type
8606 if Known_Esize (UT) and then Siz = Esize (UT) then
8610 -- Otherwise get minimum size
8612 M := UI_From_Int (Minimum_Size (UT));
8616 -- Size is less than minimum size, but one possibility remains
8617 -- that we can manage with the new size if we bias the type.
8619 M := UI_From_Int (Minimum_Size (UT, Biased => True));
8622 Error_Msg_Uint_1 := M;
8624 ("size for& too small, minimum allowed is ^", N, T);
8634 -------------------------
8635 -- Get_Alignment_Value --
8636 -------------------------
8638 function Get_Alignment_Value (Expr : Node_Id) return Uint is
8639 Align : constant Uint := Static_Integer (Expr);
8642 if Align = No_Uint then
8645 elsif Align <= 0 then
8646 Error_Msg_N ("alignment value must be positive", Expr);
8650 for J in Int range 0 .. 64 loop
8652 M : constant Uint := Uint_2 ** J;
8655 exit when M = Align;
8659 ("alignment value must be power of 2", Expr);
8667 end Get_Alignment_Value;
8669 -------------------------------------
8670 -- Inherit_Aspects_At_Freeze_Point --
8671 -------------------------------------
8673 procedure Inherit_Aspects_At_Freeze_Point (Typ : Entity_Id) is
8674 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8675 (Rep_Item : Node_Id) return Boolean;
8676 -- This routine checks if Rep_Item is either a pragma or an aspect
8677 -- specification node whose correponding pragma (if any) is present in
8678 -- the Rep Item chain of the entity it has been specified to.
8680 --------------------------------------------------
8681 -- Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item --
8682 --------------------------------------------------
8684 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8685 (Rep_Item : Node_Id) return Boolean
8688 return Nkind (Rep_Item) = N_Pragma
8689 or else Present_In_Rep_Item
8690 (Entity (Rep_Item), Aspect_Rep_Item (Rep_Item));
8691 end Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item;
8693 -- Start of processing for Inherit_Aspects_At_Freeze_Point
8696 -- A representation item is either subtype-specific (Size and Alignment
8697 -- clauses) or type-related (all others). Subtype-specific aspects may
8698 -- differ for different subtypes of the same type (RM 13.1.8).
8700 -- A derived type inherits each type-related representation aspect of
8701 -- its parent type that was directly specified before the declaration of
8702 -- the derived type (RM 13.1.15).
8704 -- A derived subtype inherits each subtype-specific representation
8705 -- aspect of its parent subtype that was directly specified before the
8706 -- declaration of the derived type (RM 13.1.15).
8708 -- The general processing involves inheriting a representation aspect
8709 -- from a parent type whenever the first rep item (aspect specification,
8710 -- attribute definition clause, pragma) corresponding to the given
8711 -- representation aspect in the rep item chain of Typ, if any, isn't
8712 -- directly specified to Typ but to one of its parents.
8714 -- ??? Note that, for now, just a limited number of representation
8715 -- aspects have been inherited here so far. Many of them are
8716 -- still inherited in Sem_Ch3. This will be fixed soon. Here is
8717 -- a non- exhaustive list of aspects that likely also need to
8718 -- be moved to this routine: Alignment, Component_Alignment,
8719 -- Component_Size, Machine_Radix, Object_Size, Pack, Predicates,
8720 -- Preelaborable_Initialization, RM_Size and Small.
8722 if Nkind (Parent (Typ)) = N_Private_Extension_Declaration then
8728 if not Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005, False)
8729 and then Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005)
8730 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8731 (Get_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005))
8733 Set_Is_Ada_2005_Only (Typ);
8738 if not Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012, False)
8739 and then Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012)
8740 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8741 (Get_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012))
8743 Set_Is_Ada_2012_Only (Typ);
8748 if not Has_Rep_Item (Typ, Name_Atomic, Name_Shared, False)
8749 and then Has_Rep_Pragma (Typ, Name_Atomic, Name_Shared)
8750 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8751 (Get_Rep_Item (Typ, Name_Atomic, Name_Shared))
8753 Set_Is_Atomic (Typ);
8754 Set_Treat_As_Volatile (Typ);
8755 Set_Is_Volatile (Typ);
8758 -- Default_Component_Value
8760 if Is_Array_Type (Typ)
8761 and then Has_Rep_Item (Typ, Name_Default_Component_Value, False)
8762 and then Has_Rep_Item (Typ, Name_Default_Component_Value)
8764 Set_Default_Aspect_Component_Value (Typ,
8765 Default_Aspect_Component_Value
8766 (Entity (Get_Rep_Item (Typ, Name_Default_Component_Value))));
8771 if Is_Scalar_Type (Typ)
8772 and then Has_Rep_Item (Typ, Name_Default_Value, False)
8773 and then Has_Rep_Item (Typ, Name_Default_Value)
8775 Set_Default_Aspect_Value (Typ,
8776 Default_Aspect_Value
8777 (Entity (Get_Rep_Item (Typ, Name_Default_Value))));
8782 if not Has_Rep_Item (Typ, Name_Discard_Names, False)
8783 and then Has_Rep_Item (Typ, Name_Discard_Names)
8784 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8785 (Get_Rep_Item (Typ, Name_Discard_Names))
8787 Set_Discard_Names (Typ);
8792 if not Has_Rep_Item (Typ, Name_Invariant, False)
8793 and then Has_Rep_Item (Typ, Name_Invariant)
8794 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8795 (Get_Rep_Item (Typ, Name_Invariant))
8797 Set_Has_Invariants (Typ);
8799 if Class_Present (Get_Rep_Item (Typ, Name_Invariant)) then
8800 Set_Has_Inheritable_Invariants (Typ);
8806 if not Has_Rep_Item (Typ, Name_Volatile, False)
8807 and then Has_Rep_Item (Typ, Name_Volatile)
8808 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8809 (Get_Rep_Item (Typ, Name_Volatile))
8811 Set_Treat_As_Volatile (Typ);
8812 Set_Is_Volatile (Typ);
8815 -- Inheritance for derived types only
8817 if Is_Derived_Type (Typ) then
8819 Bas_Typ : constant Entity_Id := Base_Type (Typ);
8820 Imp_Bas_Typ : constant Entity_Id := Implementation_Base_Type (Typ);
8823 -- Atomic_Components
8825 if not Has_Rep_Item (Typ, Name_Atomic_Components, False)
8826 and then Has_Rep_Item (Typ, Name_Atomic_Components)
8827 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8828 (Get_Rep_Item (Typ, Name_Atomic_Components))
8830 Set_Has_Atomic_Components (Imp_Bas_Typ);
8833 -- Volatile_Components
8835 if not Has_Rep_Item (Typ, Name_Volatile_Components, False)
8836 and then Has_Rep_Item (Typ, Name_Volatile_Components)
8837 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8838 (Get_Rep_Item (Typ, Name_Volatile_Components))
8840 Set_Has_Volatile_Components (Imp_Bas_Typ);
8843 -- Finalize_Storage_Only.
8845 if not Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only, False)
8846 and then Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only)
8848 Set_Finalize_Storage_Only (Bas_Typ);
8851 -- Universal_Aliasing
8853 if not Has_Rep_Item (Typ, Name_Universal_Aliasing, False)
8854 and then Has_Rep_Item (Typ, Name_Universal_Aliasing)
8855 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8856 (Get_Rep_Item (Typ, Name_Universal_Aliasing))
8858 Set_Universal_Aliasing (Imp_Bas_Typ);
8861 -- Record type specific aspects
8863 if Is_Record_Type (Typ) then
8867 if not Has_Rep_Item (Typ, Name_Bit_Order, False)
8868 and then Has_Rep_Item (Typ, Name_Bit_Order)
8870 Set_Reverse_Bit_Order (Bas_Typ,
8871 Reverse_Bit_Order (Entity (Name
8872 (Get_Rep_Item (Typ, Name_Bit_Order)))));
8875 -- Scalar_Storage_Order
8877 if not Has_Rep_Item (Typ, Name_Scalar_Storage_Order, False)
8878 and then Has_Rep_Item (Typ, Name_Scalar_Storage_Order)
8880 Set_Reverse_Storage_Order (Bas_Typ,
8881 Reverse_Storage_Order (Entity (Name
8882 (Get_Rep_Item (Typ, Name_Scalar_Storage_Order)))));
8887 end Inherit_Aspects_At_Freeze_Point;
8893 procedure Initialize is
8895 Address_Clause_Checks.Init;
8896 Independence_Checks.Init;
8897 Unchecked_Conversions.Init;
8900 -------------------------
8901 -- Is_Operational_Item --
8902 -------------------------
8904 function Is_Operational_Item (N : Node_Id) return Boolean is
8906 if Nkind (N) /= N_Attribute_Definition_Clause then
8911 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
8913 return Id = Attribute_Input
8914 or else Id = Attribute_Output
8915 or else Id = Attribute_Read
8916 or else Id = Attribute_Write
8917 or else Id = Attribute_External_Tag;
8920 end Is_Operational_Item;
8926 function Minimum_Size
8928 Biased : Boolean := False) return Nat
8930 Lo : Uint := No_Uint;
8931 Hi : Uint := No_Uint;
8932 LoR : Ureal := No_Ureal;
8933 HiR : Ureal := No_Ureal;
8934 LoSet : Boolean := False;
8935 HiSet : Boolean := False;
8939 R_Typ : constant Entity_Id := Root_Type (T);
8942 -- If bad type, return 0
8944 if T = Any_Type then
8947 -- For generic types, just return zero. There cannot be any legitimate
8948 -- need to know such a size, but this routine may be called with a
8949 -- generic type as part of normal processing.
8951 elsif Is_Generic_Type (R_Typ)
8952 or else R_Typ = Any_Type
8956 -- Access types. Normally an access type cannot have a size smaller
8957 -- than the size of System.Address. The exception is on VMS, where
8958 -- we have short and long addresses, and it is possible for an access
8959 -- type to have a short address size (and thus be less than the size
8960 -- of System.Address itself). We simply skip the check for VMS, and
8961 -- leave it to the back end to do the check.
8963 elsif Is_Access_Type (T) then
8964 if OpenVMS_On_Target then
8967 return System_Address_Size;
8970 -- Floating-point types
8972 elsif Is_Floating_Point_Type (T) then
8973 return UI_To_Int (Esize (R_Typ));
8977 elsif Is_Discrete_Type (T) then
8979 -- The following loop is looking for the nearest compile time known
8980 -- bounds following the ancestor subtype chain. The idea is to find
8981 -- the most restrictive known bounds information.
8985 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
8990 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
8991 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
8998 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
8999 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
9005 Ancest := Ancestor_Subtype (Ancest);
9008 Ancest := Base_Type (T);
9010 if Is_Generic_Type (Ancest) then
9016 -- Fixed-point types. We can't simply use Expr_Value to get the
9017 -- Corresponding_Integer_Value values of the bounds, since these do not
9018 -- get set till the type is frozen, and this routine can be called
9019 -- before the type is frozen. Similarly the test for bounds being static
9020 -- needs to include the case where we have unanalyzed real literals for
9023 elsif Is_Fixed_Point_Type (T) then
9025 -- The following loop is looking for the nearest compile time known
9026 -- bounds following the ancestor subtype chain. The idea is to find
9027 -- the most restrictive known bounds information.
9031 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
9035 -- Note: In the following two tests for LoSet and HiSet, it may
9036 -- seem redundant to test for N_Real_Literal here since normally
9037 -- one would assume that the test for the value being known at
9038 -- compile time includes this case. However, there is a glitch.
9039 -- If the real literal comes from folding a non-static expression,
9040 -- then we don't consider any non- static expression to be known
9041 -- at compile time if we are in configurable run time mode (needed
9042 -- in some cases to give a clearer definition of what is and what
9043 -- is not accepted). So the test is indeed needed. Without it, we
9044 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
9047 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
9048 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
9050 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
9057 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
9058 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
9060 HiR := Expr_Value_R (Type_High_Bound (Ancest));
9066 Ancest := Ancestor_Subtype (Ancest);
9069 Ancest := Base_Type (T);
9071 if Is_Generic_Type (Ancest) then
9077 Lo := UR_To_Uint (LoR / Small_Value (T));
9078 Hi := UR_To_Uint (HiR / Small_Value (T));
9080 -- No other types allowed
9083 raise Program_Error;
9086 -- Fall through with Hi and Lo set. Deal with biased case
9089 and then not Is_Fixed_Point_Type (T)
9090 and then not (Is_Enumeration_Type (T)
9091 and then Has_Non_Standard_Rep (T)))
9092 or else Has_Biased_Representation (T)
9098 -- Signed case. Note that we consider types like range 1 .. -1 to be
9099 -- signed for the purpose of computing the size, since the bounds have
9100 -- to be accommodated in the base type.
9102 if Lo < 0 or else Hi < 0 then
9106 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
9107 -- Note that we accommodate the case where the bounds cross. This
9108 -- can happen either because of the way the bounds are declared
9109 -- or because of the algorithm in Freeze_Fixed_Point_Type.
9123 -- If both bounds are positive, make sure that both are represen-
9124 -- table in the case where the bounds are crossed. This can happen
9125 -- either because of the way the bounds are declared, or because of
9126 -- the algorithm in Freeze_Fixed_Point_Type.
9132 -- S = size, (can accommodate 0 .. (2**size - 1))
9135 while Hi >= Uint_2 ** S loop
9143 ---------------------------
9144 -- New_Stream_Subprogram --
9145 ---------------------------
9147 procedure New_Stream_Subprogram
9151 Nam : TSS_Name_Type)
9153 Loc : constant Source_Ptr := Sloc (N);
9154 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
9155 Subp_Id : Entity_Id;
9156 Subp_Decl : Node_Id;
9160 Defer_Declaration : constant Boolean :=
9161 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
9162 -- For a tagged type, there is a declaration for each stream attribute
9163 -- at the freeze point, and we must generate only a completion of this
9164 -- declaration. We do the same for private types, because the full view
9165 -- might be tagged. Otherwise we generate a declaration at the point of
9166 -- the attribute definition clause.
9168 function Build_Spec return Node_Id;
9169 -- Used for declaration and renaming declaration, so that this is
9170 -- treated as a renaming_as_body.
9176 function Build_Spec return Node_Id is
9177 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
9180 T_Ref : constant Node_Id := New_Reference_To (Etyp, Loc);
9183 Subp_Id := Make_Defining_Identifier (Loc, Sname);
9185 -- S : access Root_Stream_Type'Class
9187 Formals := New_List (
9188 Make_Parameter_Specification (Loc,
9189 Defining_Identifier =>
9190 Make_Defining_Identifier (Loc, Name_S),
9192 Make_Access_Definition (Loc,
9195 Designated_Type (Etype (F)), Loc))));
9197 if Nam = TSS_Stream_Input then
9199 Make_Function_Specification (Loc,
9200 Defining_Unit_Name => Subp_Id,
9201 Parameter_Specifications => Formals,
9202 Result_Definition => T_Ref);
9207 Make_Parameter_Specification (Loc,
9208 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
9209 Out_Present => Out_P,
9210 Parameter_Type => T_Ref));
9213 Make_Procedure_Specification (Loc,
9214 Defining_Unit_Name => Subp_Id,
9215 Parameter_Specifications => Formals);
9221 -- Start of processing for New_Stream_Subprogram
9224 F := First_Formal (Subp);
9226 if Ekind (Subp) = E_Procedure then
9227 Etyp := Etype (Next_Formal (F));
9229 Etyp := Etype (Subp);
9232 -- Prepare subprogram declaration and insert it as an action on the
9233 -- clause node. The visibility for this entity is used to test for
9234 -- visibility of the attribute definition clause (in the sense of
9235 -- 8.3(23) as amended by AI-195).
9237 if not Defer_Declaration then
9239 Make_Subprogram_Declaration (Loc,
9240 Specification => Build_Spec);
9242 -- For a tagged type, there is always a visible declaration for each
9243 -- stream TSS (it is a predefined primitive operation), and the
9244 -- completion of this declaration occurs at the freeze point, which is
9245 -- not always visible at places where the attribute definition clause is
9246 -- visible. So, we create a dummy entity here for the purpose of
9247 -- tracking the visibility of the attribute definition clause itself.
9251 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
9253 Make_Object_Declaration (Loc,
9254 Defining_Identifier => Subp_Id,
9255 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
9258 Insert_Action (N, Subp_Decl);
9259 Set_Entity (N, Subp_Id);
9262 Make_Subprogram_Renaming_Declaration (Loc,
9263 Specification => Build_Spec,
9264 Name => New_Reference_To (Subp, Loc));
9266 if Defer_Declaration then
9267 Set_TSS (Base_Type (Ent), Subp_Id);
9269 Insert_Action (N, Subp_Decl);
9270 Copy_TSS (Subp_Id, Base_Type (Ent));
9272 end New_Stream_Subprogram;
9274 ------------------------
9275 -- Rep_Item_Too_Early --
9276 ------------------------
9278 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
9280 -- Cannot apply non-operational rep items to generic types
9282 if Is_Operational_Item (N) then
9286 and then Is_Generic_Type (Root_Type (T))
9288 Error_Msg_N ("representation item not allowed for generic type", N);
9292 -- Otherwise check for incomplete type
9294 if Is_Incomplete_Or_Private_Type (T)
9295 and then No (Underlying_Type (T))
9297 (Nkind (N) /= N_Pragma
9298 or else Get_Pragma_Id (N) /= Pragma_Import)
9301 ("representation item must be after full type declaration", N);
9304 -- If the type has incomplete components, a representation clause is
9305 -- illegal but stream attributes and Convention pragmas are correct.
9307 elsif Has_Private_Component (T) then
9308 if Nkind (N) = N_Pragma then
9313 ("representation item must appear after type is fully defined",
9320 end Rep_Item_Too_Early;
9322 -----------------------
9323 -- Rep_Item_Too_Late --
9324 -----------------------
9326 function Rep_Item_Too_Late
9329 FOnly : Boolean := False) return Boolean
9332 Parent_Type : Entity_Id;
9335 -- Output the too late message. Note that this is not considered a
9336 -- serious error, since the effect is simply that we ignore the
9337 -- representation clause in this case.
9343 procedure Too_Late is
9345 -- Other compilers seem more relaxed about rep items appearing too
9346 -- late. Since analysis tools typically don't care about rep items
9347 -- anyway, no reason to be too strict about this.
9349 if not Relaxed_RM_Semantics then
9350 Error_Msg_N ("|representation item appears too late!", N);
9354 -- Start of processing for Rep_Item_Too_Late
9357 -- First make sure entity is not frozen (RM 13.1(9))
9361 -- Exclude imported types, which may be frozen if they appear in a
9362 -- representation clause for a local type.
9364 and then not From_With_Type (T)
9366 -- Exclude generated entities (not coming from source). The common
9367 -- case is when we generate a renaming which prematurely freezes the
9368 -- renamed internal entity, but we still want to be able to set copies
9369 -- of attribute values such as Size/Alignment.
9371 and then Comes_From_Source (T)
9374 S := First_Subtype (T);
9376 if Present (Freeze_Node (S)) then
9378 ("??no more representation items for }", Freeze_Node (S), S);
9383 -- Check for case of non-tagged derived type whose parent either has
9384 -- primitive operations, or is a by reference type (RM 13.1(10)).
9388 and then Is_Derived_Type (T)
9389 and then not Is_Tagged_Type (T)
9391 Parent_Type := Etype (Base_Type (T));
9393 if Has_Primitive_Operations (Parent_Type) then
9396 ("primitive operations already defined for&!", N, Parent_Type);
9399 elsif Is_By_Reference_Type (Parent_Type) then
9402 ("parent type & is a by reference type!", N, Parent_Type);
9407 -- No error, link item into head of chain of rep items for the entity,
9408 -- but avoid chaining if we have an overloadable entity, and the pragma
9409 -- is one that can apply to multiple overloaded entities.
9411 if Is_Overloadable (T) and then Nkind (N) = N_Pragma then
9413 Pname : constant Name_Id := Pragma_Name (N);
9415 if Nam_In (Pname, Name_Convention, Name_Import, Name_Export,
9416 Name_External, Name_Interface)
9423 Record_Rep_Item (T, N);
9425 end Rep_Item_Too_Late;
9427 -------------------------------------
9428 -- Replace_Type_References_Generic --
9429 -------------------------------------
9431 procedure Replace_Type_References_Generic (N : Node_Id; TName : Name_Id) is
9433 function Replace_Node (N : Node_Id) return Traverse_Result;
9434 -- Processes a single node in the traversal procedure below, checking
9435 -- if node N should be replaced, and if so, doing the replacement.
9437 procedure Replace_Type_Refs is new Traverse_Proc (Replace_Node);
9438 -- This instantiation provides the body of Replace_Type_References
9444 function Replace_Node (N : Node_Id) return Traverse_Result is
9449 -- Case of identifier
9451 if Nkind (N) = N_Identifier then
9453 -- If not the type name, all done with this node
9455 if Chars (N) /= TName then
9458 -- Otherwise do the replacement and we are done with this node
9461 Replace_Type_Reference (N);
9465 -- Case of selected component (which is what a qualification
9466 -- looks like in the unanalyzed tree, which is what we have.
9468 elsif Nkind (N) = N_Selected_Component then
9470 -- If selector name is not our type, keeping going (we might
9471 -- still have an occurrence of the type in the prefix).
9473 if Nkind (Selector_Name (N)) /= N_Identifier
9474 or else Chars (Selector_Name (N)) /= TName
9478 -- Selector name is our type, check qualification
9481 -- Loop through scopes and prefixes, doing comparison
9486 -- Continue if no more scopes or scope with no name
9488 if No (S) or else Nkind (S) not in N_Has_Chars then
9492 -- Do replace if prefix is an identifier matching the
9493 -- scope that we are currently looking at.
9495 if Nkind (P) = N_Identifier
9496 and then Chars (P) = Chars (S)
9498 Replace_Type_Reference (N);
9502 -- Go check scope above us if prefix is itself of the
9503 -- form of a selected component, whose selector matches
9504 -- the scope we are currently looking at.
9506 if Nkind (P) = N_Selected_Component
9507 and then Nkind (Selector_Name (P)) = N_Identifier
9508 and then Chars (Selector_Name (P)) = Chars (S)
9513 -- For anything else, we don't have a match, so keep on
9514 -- going, there are still some weird cases where we may
9515 -- still have a replacement within the prefix.
9523 -- Continue for any other node kind
9531 Replace_Type_Refs (N);
9532 end Replace_Type_References_Generic;
9534 -------------------------
9535 -- Same_Representation --
9536 -------------------------
9538 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
9539 T1 : constant Entity_Id := Underlying_Type (Typ1);
9540 T2 : constant Entity_Id := Underlying_Type (Typ2);
9543 -- A quick check, if base types are the same, then we definitely have
9544 -- the same representation, because the subtype specific representation
9545 -- attributes (Size and Alignment) do not affect representation from
9546 -- the point of view of this test.
9548 if Base_Type (T1) = Base_Type (T2) then
9551 elsif Is_Private_Type (Base_Type (T2))
9552 and then Base_Type (T1) = Full_View (Base_Type (T2))
9557 -- Tagged types never have differing representations
9559 if Is_Tagged_Type (T1) then
9563 -- Representations are definitely different if conventions differ
9565 if Convention (T1) /= Convention (T2) then
9569 -- Representations are different if component alignments or scalar
9570 -- storage orders differ.
9572 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
9574 (Is_Record_Type (T2) or else Is_Array_Type (T2))
9576 (Component_Alignment (T1) /= Component_Alignment (T2)
9578 Reverse_Storage_Order (T1) /= Reverse_Storage_Order (T2))
9583 -- For arrays, the only real issue is component size. If we know the
9584 -- component size for both arrays, and it is the same, then that's
9585 -- good enough to know we don't have a change of representation.
9587 if Is_Array_Type (T1) then
9588 if Known_Component_Size (T1)
9589 and then Known_Component_Size (T2)
9590 and then Component_Size (T1) = Component_Size (T2)
9592 if VM_Target = No_VM then
9595 -- In VM targets the representation of arrays with aliased
9596 -- components differs from arrays with non-aliased components
9599 return Has_Aliased_Components (Base_Type (T1))
9601 Has_Aliased_Components (Base_Type (T2));
9606 -- Types definitely have same representation if neither has non-standard
9607 -- representation since default representations are always consistent.
9608 -- If only one has non-standard representation, and the other does not,
9609 -- then we consider that they do not have the same representation. They
9610 -- might, but there is no way of telling early enough.
9612 if Has_Non_Standard_Rep (T1) then
9613 if not Has_Non_Standard_Rep (T2) then
9617 return not Has_Non_Standard_Rep (T2);
9620 -- Here the two types both have non-standard representation, and we need
9621 -- to determine if they have the same non-standard representation.
9623 -- For arrays, we simply need to test if the component sizes are the
9624 -- same. Pragma Pack is reflected in modified component sizes, so this
9625 -- check also deals with pragma Pack.
9627 if Is_Array_Type (T1) then
9628 return Component_Size (T1) = Component_Size (T2);
9630 -- Tagged types always have the same representation, because it is not
9631 -- possible to specify different representations for common fields.
9633 elsif Is_Tagged_Type (T1) then
9636 -- Case of record types
9638 elsif Is_Record_Type (T1) then
9640 -- Packed status must conform
9642 if Is_Packed (T1) /= Is_Packed (T2) then
9645 -- Otherwise we must check components. Typ2 maybe a constrained
9646 -- subtype with fewer components, so we compare the components
9647 -- of the base types.
9650 Record_Case : declare
9651 CD1, CD2 : Entity_Id;
9653 function Same_Rep return Boolean;
9654 -- CD1 and CD2 are either components or discriminants. This
9655 -- function tests whether they have the same representation.
9661 function Same_Rep return Boolean is
9663 if No (Component_Clause (CD1)) then
9664 return No (Component_Clause (CD2));
9666 -- Note: at this point, component clauses have been
9667 -- normalized to the default bit order, so that the
9668 -- comparison of Component_Bit_Offsets is meaningful.
9671 Present (Component_Clause (CD2))
9673 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
9675 Esize (CD1) = Esize (CD2);
9679 -- Start of processing for Record_Case
9682 if Has_Discriminants (T1) then
9684 -- The number of discriminants may be different if the
9685 -- derived type has fewer (constrained by values). The
9686 -- invisible discriminants retain the representation of
9687 -- the original, so the discrepancy does not per se
9688 -- indicate a different representation.
9690 CD1 := First_Discriminant (T1);
9691 CD2 := First_Discriminant (T2);
9692 while Present (CD1) and then Present (CD2) loop
9693 if not Same_Rep then
9696 Next_Discriminant (CD1);
9697 Next_Discriminant (CD2);
9702 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
9703 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
9704 while Present (CD1) loop
9705 if not Same_Rep then
9708 Next_Component (CD1);
9709 Next_Component (CD2);
9717 -- For enumeration types, we must check each literal to see if the
9718 -- representation is the same. Note that we do not permit enumeration
9719 -- representation clauses for Character and Wide_Character, so these
9720 -- cases were already dealt with.
9722 elsif Is_Enumeration_Type (T1) then
9723 Enumeration_Case : declare
9727 L1 := First_Literal (T1);
9728 L2 := First_Literal (T2);
9729 while Present (L1) loop
9730 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
9739 end Enumeration_Case;
9741 -- Any other types have the same representation for these purposes
9746 end Same_Representation;
9752 procedure Set_Biased
9756 Biased : Boolean := True)
9760 Set_Has_Biased_Representation (E);
9762 if Warn_On_Biased_Representation then
9764 ("?B?" & Msg & " forces biased representation for&", N, E);
9769 --------------------
9770 -- Set_Enum_Esize --
9771 --------------------
9773 procedure Set_Enum_Esize (T : Entity_Id) is
9781 -- Find the minimum standard size (8,16,32,64) that fits
9783 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
9784 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
9787 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
9788 Sz := Standard_Character_Size; -- May be > 8 on some targets
9790 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
9793 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
9796 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
9801 if Hi < Uint_2**08 then
9802 Sz := Standard_Character_Size; -- May be > 8 on some targets
9804 elsif Hi < Uint_2**16 then
9807 elsif Hi < Uint_2**32 then
9810 else pragma Assert (Hi < Uint_2**63);
9815 -- That minimum is the proper size unless we have a foreign convention
9816 -- and the size required is 32 or less, in which case we bump the size
9817 -- up to 32. This is required for C and C++ and seems reasonable for
9818 -- all other foreign conventions.
9820 if Has_Foreign_Convention (T)
9821 and then Esize (T) < Standard_Integer_Size
9823 Init_Esize (T, Standard_Integer_Size);
9829 ------------------------------
9830 -- Validate_Address_Clauses --
9831 ------------------------------
9833 procedure Validate_Address_Clauses is
9835 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
9837 ACCR : Address_Clause_Check_Record
9838 renames Address_Clause_Checks.Table (J);
9849 -- Skip processing of this entry if warning already posted
9851 if not Address_Warning_Posted (ACCR.N) then
9852 Expr := Original_Node (Expression (ACCR.N));
9856 X_Alignment := Alignment (ACCR.X);
9857 Y_Alignment := Alignment (ACCR.Y);
9859 -- Similarly obtain sizes
9861 X_Size := Esize (ACCR.X);
9862 Y_Size := Esize (ACCR.Y);
9864 -- Check for large object overlaying smaller one
9867 and then X_Size > Uint_0
9868 and then X_Size > Y_Size
9871 ("?& overlays smaller object", ACCR.N, ACCR.X);
9873 ("\??program execution may be erroneous", ACCR.N);
9874 Error_Msg_Uint_1 := X_Size;
9876 ("\??size of & is ^", ACCR.N, ACCR.X);
9877 Error_Msg_Uint_1 := Y_Size;
9879 ("\??size of & is ^", ACCR.N, ACCR.Y);
9881 -- Check for inadequate alignment, both of the base object
9882 -- and of the offset, if any.
9884 -- Note: we do not check the alignment if we gave a size
9885 -- warning, since it would likely be redundant.
9887 elsif Y_Alignment /= Uint_0
9888 and then (Y_Alignment < X_Alignment
9891 Nkind (Expr) = N_Attribute_Reference
9893 Attribute_Name (Expr) = Name_Address
9895 Has_Compatible_Alignment
9896 (ACCR.X, Prefix (Expr))
9897 /= Known_Compatible))
9900 ("??specified address for& may be inconsistent "
9901 & "with alignment", ACCR.N, ACCR.X);
9903 ("\??program execution may be erroneous (RM 13.3(27))",
9905 Error_Msg_Uint_1 := X_Alignment;
9907 ("\??alignment of & is ^", ACCR.N, ACCR.X);
9908 Error_Msg_Uint_1 := Y_Alignment;
9910 ("\??alignment of & is ^", ACCR.N, ACCR.Y);
9911 if Y_Alignment >= X_Alignment then
9913 ("\??but offset is not multiple of alignment", ACCR.N);
9919 end Validate_Address_Clauses;
9921 ---------------------------
9922 -- Validate_Independence --
9923 ---------------------------
9925 procedure Validate_Independence is
9926 SU : constant Uint := UI_From_Int (System_Storage_Unit);
9934 procedure Check_Array_Type (Atyp : Entity_Id);
9935 -- Checks if the array type Atyp has independent components, and
9936 -- if not, outputs an appropriate set of error messages.
9938 procedure No_Independence;
9939 -- Output message that independence cannot be guaranteed
9941 function OK_Component (C : Entity_Id) return Boolean;
9942 -- Checks one component to see if it is independently accessible, and
9943 -- if so yields True, otherwise yields False if independent access
9944 -- cannot be guaranteed. This is a conservative routine, it only
9945 -- returns True if it knows for sure, it returns False if it knows
9946 -- there is a problem, or it cannot be sure there is no problem.
9948 procedure Reason_Bad_Component (C : Entity_Id);
9949 -- Outputs continuation message if a reason can be determined for
9950 -- the component C being bad.
9952 ----------------------
9953 -- Check_Array_Type --
9954 ----------------------
9956 procedure Check_Array_Type (Atyp : Entity_Id) is
9957 Ctyp : constant Entity_Id := Component_Type (Atyp);
9960 -- OK if no alignment clause, no pack, and no component size
9962 if not Has_Component_Size_Clause (Atyp)
9963 and then not Has_Alignment_Clause (Atyp)
9964 and then not Is_Packed (Atyp)
9969 -- Check actual component size
9971 if not Known_Component_Size (Atyp)
9972 or else not (Addressable (Component_Size (Atyp))
9973 and then Component_Size (Atyp) < 64)
9974 or else Component_Size (Atyp) mod Esize (Ctyp) /= 0
9978 -- Bad component size, check reason
9980 if Has_Component_Size_Clause (Atyp) then
9981 P := Get_Attribute_Definition_Clause
9982 (Atyp, Attribute_Component_Size);
9985 Error_Msg_Sloc := Sloc (P);
9986 Error_Msg_N ("\because of Component_Size clause#", N);
9991 if Is_Packed (Atyp) then
9992 P := Get_Rep_Pragma (Atyp, Name_Pack);
9995 Error_Msg_Sloc := Sloc (P);
9996 Error_Msg_N ("\because of pragma Pack#", N);
10001 -- No reason found, just return
10006 -- Array type is OK independence-wise
10009 end Check_Array_Type;
10011 ---------------------
10012 -- No_Independence --
10013 ---------------------
10015 procedure No_Independence is
10017 if Pragma_Name (N) = Name_Independent then
10018 Error_Msg_NE ("independence cannot be guaranteed for&", N, E);
10021 ("independent components cannot be guaranteed for&", N, E);
10023 end No_Independence;
10029 function OK_Component (C : Entity_Id) return Boolean is
10030 Rec : constant Entity_Id := Scope (C);
10031 Ctyp : constant Entity_Id := Etype (C);
10034 -- OK if no component clause, no Pack, and no alignment clause
10036 if No (Component_Clause (C))
10037 and then not Is_Packed (Rec)
10038 and then not Has_Alignment_Clause (Rec)
10043 -- Here we look at the actual component layout. A component is
10044 -- addressable if its size is a multiple of the Esize of the
10045 -- component type, and its starting position in the record has
10046 -- appropriate alignment, and the record itself has appropriate
10047 -- alignment to guarantee the component alignment.
10049 -- Make sure sizes are static, always assume the worst for any
10050 -- cases where we cannot check static values.
10052 if not (Known_Static_Esize (C)
10054 Known_Static_Esize (Ctyp))
10059 -- Size of component must be addressable or greater than 64 bits
10060 -- and a multiple of bytes.
10062 if not Addressable (Esize (C)) and then Esize (C) < Uint_64 then
10066 -- Check size is proper multiple
10068 if Esize (C) mod Esize (Ctyp) /= 0 then
10072 -- Check alignment of component is OK
10074 if not Known_Component_Bit_Offset (C)
10075 or else Component_Bit_Offset (C) < Uint_0
10076 or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0
10081 -- Check alignment of record type is OK
10083 if not Known_Alignment (Rec)
10084 or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
10089 -- All tests passed, component is addressable
10094 --------------------------
10095 -- Reason_Bad_Component --
10096 --------------------------
10098 procedure Reason_Bad_Component (C : Entity_Id) is
10099 Rec : constant Entity_Id := Scope (C);
10100 Ctyp : constant Entity_Id := Etype (C);
10103 -- If component clause present assume that's the problem
10105 if Present (Component_Clause (C)) then
10106 Error_Msg_Sloc := Sloc (Component_Clause (C));
10107 Error_Msg_N ("\because of Component_Clause#", N);
10111 -- If pragma Pack clause present, assume that's the problem
10113 if Is_Packed (Rec) then
10114 P := Get_Rep_Pragma (Rec, Name_Pack);
10116 if Present (P) then
10117 Error_Msg_Sloc := Sloc (P);
10118 Error_Msg_N ("\because of pragma Pack#", N);
10123 -- See if record has bad alignment clause
10125 if Has_Alignment_Clause (Rec)
10126 and then Known_Alignment (Rec)
10127 and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
10129 P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment);
10131 if Present (P) then
10132 Error_Msg_Sloc := Sloc (P);
10133 Error_Msg_N ("\because of Alignment clause#", N);
10137 -- Couldn't find a reason, so return without a message
10140 end Reason_Bad_Component;
10142 -- Start of processing for Validate_Independence
10145 for J in Independence_Checks.First .. Independence_Checks.Last loop
10146 N := Independence_Checks.Table (J).N;
10147 E := Independence_Checks.Table (J).E;
10148 IC := Pragma_Name (N) = Name_Independent_Components;
10150 -- Deal with component case
10152 if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then
10153 if not OK_Component (E) then
10155 Reason_Bad_Component (E);
10160 -- Deal with record with Independent_Components
10162 if IC and then Is_Record_Type (E) then
10163 Comp := First_Component_Or_Discriminant (E);
10164 while Present (Comp) loop
10165 if not OK_Component (Comp) then
10167 Reason_Bad_Component (Comp);
10171 Next_Component_Or_Discriminant (Comp);
10175 -- Deal with address clause case
10177 if Is_Object (E) then
10178 Addr := Address_Clause (E);
10180 if Present (Addr) then
10182 Error_Msg_Sloc := Sloc (Addr);
10183 Error_Msg_N ("\because of Address clause#", N);
10188 -- Deal with independent components for array type
10190 if IC and then Is_Array_Type (E) then
10191 Check_Array_Type (E);
10194 -- Deal with independent components for array object
10196 if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then
10197 Check_Array_Type (Etype (E));
10202 end Validate_Independence;
10204 -----------------------------------
10205 -- Validate_Unchecked_Conversion --
10206 -----------------------------------
10208 procedure Validate_Unchecked_Conversion
10210 Act_Unit : Entity_Id)
10212 Source : Entity_Id;
10213 Target : Entity_Id;
10217 -- Obtain source and target types. Note that we call Ancestor_Subtype
10218 -- here because the processing for generic instantiation always makes
10219 -- subtypes, and we want the original frozen actual types.
10221 -- If we are dealing with private types, then do the check on their
10222 -- fully declared counterparts if the full declarations have been
10223 -- encountered (they don't have to be visible, but they must exist!)
10225 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
10227 if Is_Private_Type (Source)
10228 and then Present (Underlying_Type (Source))
10230 Source := Underlying_Type (Source);
10233 Target := Ancestor_Subtype (Etype (Act_Unit));
10235 -- If either type is generic, the instantiation happens within a generic
10236 -- unit, and there is nothing to check. The proper check will happen
10237 -- when the enclosing generic is instantiated.
10239 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
10243 if Is_Private_Type (Target)
10244 and then Present (Underlying_Type (Target))
10246 Target := Underlying_Type (Target);
10249 -- Source may be unconstrained array, but not target
10251 if Is_Array_Type (Target) and then not Is_Constrained (Target) then
10253 ("unchecked conversion to unconstrained array not allowed", N);
10257 -- Warn if conversion between two different convention pointers
10259 if Is_Access_Type (Target)
10260 and then Is_Access_Type (Source)
10261 and then Convention (Target) /= Convention (Source)
10262 and then Warn_On_Unchecked_Conversion
10264 -- Give warnings for subprogram pointers only on most targets. The
10265 -- exception is VMS, where data pointers can have different lengths
10266 -- depending on the pointer convention.
10268 if Is_Access_Subprogram_Type (Target)
10269 or else Is_Access_Subprogram_Type (Source)
10270 or else OpenVMS_On_Target
10273 ("?z?conversion between pointers with different conventions!",
10278 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
10279 -- warning when compiling GNAT-related sources.
10281 if Warn_On_Unchecked_Conversion
10282 and then not In_Predefined_Unit (N)
10283 and then RTU_Loaded (Ada_Calendar)
10285 (Chars (Source) = Name_Time
10287 Chars (Target) = Name_Time)
10289 -- If Ada.Calendar is loaded and the name of one of the operands is
10290 -- Time, there is a good chance that this is Ada.Calendar.Time.
10293 Calendar_Time : constant Entity_Id :=
10294 Full_View (RTE (RO_CA_Time));
10296 pragma Assert (Present (Calendar_Time));
10298 if Source = Calendar_Time or else Target = Calendar_Time then
10300 ("?z?representation of 'Time values may change between " &
10301 "'G'N'A'T versions", N);
10306 -- Make entry in unchecked conversion table for later processing by
10307 -- Validate_Unchecked_Conversions, which will check sizes and alignments
10308 -- (using values set by the back-end where possible). This is only done
10309 -- if the appropriate warning is active.
10311 if Warn_On_Unchecked_Conversion then
10312 Unchecked_Conversions.Append
10313 (New_Val => UC_Entry'(Eloc => Sloc (N),
10315 Target => Target));
10317 -- If both sizes are known statically now, then back end annotation
10318 -- is not required to do a proper check but if either size is not
10319 -- known statically, then we need the annotation.
10321 if Known_Static_RM_Size (Source)
10323 Known_Static_RM_Size (Target)
10327 Back_Annotate_Rep_Info := True;
10331 -- If unchecked conversion to access type, and access type is declared
10332 -- in the same unit as the unchecked conversion, then set the flag
10333 -- No_Strict_Aliasing (no strict aliasing is implicit here)
10335 if Is_Access_Type (Target) and then
10336 In_Same_Source_Unit (Target, N)
10338 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
10341 -- Generate N_Validate_Unchecked_Conversion node for back end in case
10342 -- the back end needs to perform special validation checks.
10344 -- Shouldn't this be in Exp_Ch13, since the check only gets done if we
10345 -- have full expansion and the back end is called ???
10348 Make_Validate_Unchecked_Conversion (Sloc (N));
10349 Set_Source_Type (Vnode, Source);
10350 Set_Target_Type (Vnode, Target);
10352 -- If the unchecked conversion node is in a list, just insert before it.
10353 -- If not we have some strange case, not worth bothering about.
10355 if Is_List_Member (N) then
10356 Insert_After (N, Vnode);
10358 end Validate_Unchecked_Conversion;
10360 ------------------------------------
10361 -- Validate_Unchecked_Conversions --
10362 ------------------------------------
10364 procedure Validate_Unchecked_Conversions is
10366 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
10368 T : UC_Entry renames Unchecked_Conversions.Table (N);
10370 Eloc : constant Source_Ptr := T.Eloc;
10371 Source : constant Entity_Id := T.Source;
10372 Target : constant Entity_Id := T.Target;
10378 -- This validation check, which warns if we have unequal sizes for
10379 -- unchecked conversion, and thus potentially implementation
10380 -- dependent semantics, is one of the few occasions on which we
10381 -- use the official RM size instead of Esize. See description in
10382 -- Einfo "Handling of Type'Size Values" for details.
10384 if Serious_Errors_Detected = 0
10385 and then Known_Static_RM_Size (Source)
10386 and then Known_Static_RM_Size (Target)
10388 -- Don't do the check if warnings off for either type, note the
10389 -- deliberate use of OR here instead of OR ELSE to get the flag
10390 -- Warnings_Off_Used set for both types if appropriate.
10392 and then not (Has_Warnings_Off (Source)
10394 Has_Warnings_Off (Target))
10396 Source_Siz := RM_Size (Source);
10397 Target_Siz := RM_Size (Target);
10399 if Source_Siz /= Target_Siz then
10401 ("?z?types for unchecked conversion have different sizes!",
10404 if All_Errors_Mode then
10405 Error_Msg_Name_1 := Chars (Source);
10406 Error_Msg_Uint_1 := Source_Siz;
10407 Error_Msg_Name_2 := Chars (Target);
10408 Error_Msg_Uint_2 := Target_Siz;
10409 Error_Msg ("\size of % is ^, size of % is ^?z?", Eloc);
10411 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
10413 if Is_Discrete_Type (Source)
10415 Is_Discrete_Type (Target)
10417 if Source_Siz > Target_Siz then
10419 ("\?z?^ high order bits of source will "
10420 & "be ignored!", Eloc);
10422 elsif Is_Unsigned_Type (Source) then
10424 ("\?z?source will be extended with ^ high order "
10425 & "zero bits?!", Eloc);
10429 ("\?z?source will be extended with ^ high order "
10430 & "sign bits!", Eloc);
10433 elsif Source_Siz < Target_Siz then
10434 if Is_Discrete_Type (Target) then
10435 if Bytes_Big_Endian then
10437 ("\?z?target value will include ^ undefined "
10438 & "low order bits!", Eloc);
10441 ("\?z?target value will include ^ undefined "
10442 & "high order bits!", Eloc);
10447 ("\?z?^ trailing bits of target value will be "
10448 & "undefined!", Eloc);
10451 else pragma Assert (Source_Siz > Target_Siz);
10453 ("\?z?^ trailing bits of source will be ignored!",
10460 -- If both types are access types, we need to check the alignment.
10461 -- If the alignment of both is specified, we can do it here.
10463 if Serious_Errors_Detected = 0
10464 and then Ekind (Source) in Access_Kind
10465 and then Ekind (Target) in Access_Kind
10466 and then Target_Strict_Alignment
10467 and then Present (Designated_Type (Source))
10468 and then Present (Designated_Type (Target))
10471 D_Source : constant Entity_Id := Designated_Type (Source);
10472 D_Target : constant Entity_Id := Designated_Type (Target);
10475 if Known_Alignment (D_Source)
10477 Known_Alignment (D_Target)
10480 Source_Align : constant Uint := Alignment (D_Source);
10481 Target_Align : constant Uint := Alignment (D_Target);
10484 if Source_Align < Target_Align
10485 and then not Is_Tagged_Type (D_Source)
10487 -- Suppress warning if warnings suppressed on either
10488 -- type or either designated type. Note the use of
10489 -- OR here instead of OR ELSE. That is intentional,
10490 -- we would like to set flag Warnings_Off_Used in
10491 -- all types for which warnings are suppressed.
10493 and then not (Has_Warnings_Off (D_Source)
10495 Has_Warnings_Off (D_Target)
10497 Has_Warnings_Off (Source)
10499 Has_Warnings_Off (Target))
10501 Error_Msg_Uint_1 := Target_Align;
10502 Error_Msg_Uint_2 := Source_Align;
10503 Error_Msg_Node_1 := D_Target;
10504 Error_Msg_Node_2 := D_Source;
10506 ("?z?alignment of & (^) is stricter than "
10507 & "alignment of & (^)!", Eloc);
10509 ("\?z?resulting access value may have invalid "
10510 & "alignment!", Eloc);
10518 end Validate_Unchecked_Conversions;