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 Inherit_Delayed_Rep_Aspects (ASN : Node_Id);
698 -- As discussed in the spec of Aspects (see Aspect_Delay declaration),
699 -- a derived type can inherit aspects from its parent which have been
700 -- specified at the time of the derivation using an aspect, as in:
702 -- type A is range 1 .. 10
703 -- with Size => Not_Defined_Yet;
707 -- Not_Defined_Yet : constant := 64;
709 -- In this example, the Size of A is considered to be specified prior
710 -- to the derivation, and thus inherited, even though the value is not
711 -- known at the time of derivation. To deal with this, we use two entity
712 -- flags. The flag Has_Derived_Rep_Aspects is set in the parent type (A
713 -- here), and then the flag May_Inherit_Delayed_Rep_Aspects is set in
714 -- the derived type (B here). If this flag is set when the derived type
715 -- is frozen, then this procedure is called to ensure proper inheritance
716 -- of all delayed aspects from the paren type. The derived type is E,
717 -- the argument to Analyze_Aspects_At_Freeze_Point. ASN is the first
718 -- aspect specification node in the Rep_Item chain for the parent type.
720 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id);
721 -- Given an aspect specification node ASN whose expression is an
722 -- optional Boolean, this routines creates the corresponding pragma
723 -- at the freezing point.
725 ----------------------------------
726 -- Analyze_Aspect_Default_Value --
727 ----------------------------------
729 procedure Analyze_Aspect_Default_Value (ASN : Node_Id) is
730 Ent : constant Entity_Id := Entity (ASN);
731 Expr : constant Node_Id := Expression (ASN);
732 Id : constant Node_Id := Identifier (ASN);
735 Error_Msg_Name_1 := Chars (Id);
737 if not Is_Type (Ent) then
738 Error_Msg_N ("aspect% can only apply to a type", Id);
741 elsif not Is_First_Subtype (Ent) then
742 Error_Msg_N ("aspect% cannot apply to subtype", Id);
745 elsif A_Id = Aspect_Default_Value
746 and then not Is_Scalar_Type (Ent)
748 Error_Msg_N ("aspect% can only be applied to scalar type", Id);
751 elsif A_Id = Aspect_Default_Component_Value then
752 if not Is_Array_Type (Ent) then
753 Error_Msg_N ("aspect% can only be applied to array type", Id);
756 elsif not Is_Scalar_Type (Component_Type (Ent)) then
757 Error_Msg_N ("aspect% requires scalar components", Id);
762 Set_Has_Default_Aspect (Base_Type (Ent));
764 if Is_Scalar_Type (Ent) then
765 Set_Default_Aspect_Value (Ent, Expr);
767 -- Place default value of base type as well, because that is
768 -- the semantics of the aspect. It is convenient to link the
769 -- aspect to both the (possibly anonymous) base type and to
770 -- the given first subtype.
772 Set_Default_Aspect_Value (Base_Type (Ent), Expr);
775 Set_Default_Aspect_Component_Value (Ent, Expr);
777 end Analyze_Aspect_Default_Value;
779 ---------------------------------
780 -- Inherit_Delayed_Rep_Aspects --
781 ---------------------------------
783 procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id) is
784 P : constant Entity_Id := Entity (ASN);
785 -- Entithy for parent type
788 -- Item from Rep_Item chain
793 -- Loop through delayed aspects for the parent type
796 while Present (N) loop
797 if Nkind (N) = N_Aspect_Specification then
798 exit when Entity (N) /= P;
800 if Is_Delayed_Aspect (N) then
801 A := Get_Aspect_Id (Chars (Identifier (N)));
803 -- Process delayed rep aspect. For Boolean attributes it is
804 -- not possible to cancel an attribute once set (the attempt
805 -- to use an aspect with xxx => False is an error) for a
806 -- derived type. So for those cases, we do not have to check
807 -- if a clause has been given for the derived type, since it
808 -- is harmless to set it again if it is already set.
814 when Aspect_Alignment =>
815 if not Has_Alignment_Clause (E) then
816 Set_Alignment (E, Alignment (P));
821 when Aspect_Atomic =>
822 if Is_Atomic (P) then
828 when Aspect_Atomic_Components =>
829 if Has_Atomic_Components (P) then
830 Set_Has_Atomic_Components (Base_Type (E));
835 when Aspect_Bit_Order =>
836 if Is_Record_Type (E)
837 and then No (Get_Attribute_Definition_Clause
838 (E, Attribute_Bit_Order))
839 and then Reverse_Bit_Order (P)
841 Set_Reverse_Bit_Order (Base_Type (E));
846 when Aspect_Component_Size =>
848 and then not Has_Component_Size_Clause (E)
851 (Base_Type (E), Component_Size (P));
856 when Aspect_Machine_Radix =>
857 if Is_Decimal_Fixed_Point_Type (E)
858 and then not Has_Machine_Radix_Clause (E)
860 Set_Machine_Radix_10 (E, Machine_Radix_10 (P));
863 -- Object_Size (also Size which also sets Object_Size)
865 when Aspect_Object_Size | Aspect_Size =>
866 if not Has_Size_Clause (E)
868 No (Get_Attribute_Definition_Clause
869 (E, Attribute_Object_Size))
871 Set_Esize (E, Esize (P));
877 if not Is_Packed (E) then
878 Set_Is_Packed (Base_Type (E));
880 if Is_Bit_Packed_Array (P) then
881 Set_Is_Bit_Packed_Array (Base_Type (E));
882 Set_Packed_Array_Type (E, Packed_Array_Type (P));
886 -- Scalar_Storage_Order
888 when Aspect_Scalar_Storage_Order =>
889 if (Is_Record_Type (E) or else Is_Array_Type (E))
890 and then No (Get_Attribute_Definition_Clause
891 (E, Attribute_Scalar_Storage_Order))
892 and then Reverse_Storage_Order (P)
894 Set_Reverse_Storage_Order (Base_Type (E));
900 if Is_Fixed_Point_Type (E)
901 and then not Has_Small_Clause (E)
903 Set_Small_Value (E, Small_Value (P));
908 when Aspect_Storage_Size =>
909 if (Is_Access_Type (E) or else Is_Task_Type (E))
910 and then not Has_Storage_Size_Clause (E)
912 Set_Storage_Size_Variable
913 (Base_Type (E), Storage_Size_Variable (P));
918 when Aspect_Value_Size =>
920 -- Value_Size is never inherited, it is either set by
921 -- default, or it is explicitly set for the derived
922 -- type. So nothing to do here.
928 when Aspect_Volatile =>
929 if Is_Volatile (P) then
933 -- Volatile_Components
935 when Aspect_Volatile_Components =>
936 if Has_Volatile_Components (P) then
937 Set_Has_Volatile_Components (Base_Type (E));
940 -- That should be all the Rep Aspects
943 pragma Assert (Aspect_Delay (A_Id) /= Rep_Aspect);
950 N := Next_Rep_Item (N);
952 end Inherit_Delayed_Rep_Aspects;
954 -------------------------------------
955 -- Make_Pragma_From_Boolean_Aspect --
956 -------------------------------------
958 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id) is
959 Ident : constant Node_Id := Identifier (ASN);
960 A_Name : constant Name_Id := Chars (Ident);
961 A_Id : constant Aspect_Id := Get_Aspect_Id (A_Name);
962 Ent : constant Entity_Id := Entity (ASN);
963 Expr : constant Node_Id := Expression (ASN);
964 Loc : constant Source_Ptr := Sloc (ASN);
968 procedure Check_False_Aspect_For_Derived_Type;
969 -- This procedure checks for the case of a false aspect for a derived
970 -- type, which improperly tries to cancel an aspect inherited from
973 -----------------------------------------
974 -- Check_False_Aspect_For_Derived_Type --
975 -----------------------------------------
977 procedure Check_False_Aspect_For_Derived_Type is
981 -- We are only checking derived types
983 if not Is_Derived_Type (E) then
987 Par := Nearest_Ancestor (E);
990 when Aspect_Atomic | Aspect_Shared =>
991 if not Is_Atomic (Par) then
995 when Aspect_Atomic_Components =>
996 if not Has_Atomic_Components (Par) then
1000 when Aspect_Discard_Names =>
1001 if not Discard_Names (Par) then
1006 if not Is_Packed (Par) then
1010 when Aspect_Unchecked_Union =>
1011 if not Is_Unchecked_Union (Par) then
1015 when Aspect_Volatile =>
1016 if not Is_Volatile (Par) then
1020 when Aspect_Volatile_Components =>
1021 if not Has_Volatile_Components (Par) then
1029 -- Fall through means we are canceling an inherited aspect
1031 Error_Msg_Name_1 := A_Name;
1033 ("derived type& inherits aspect%, cannot cancel", Expr, E);
1035 end Check_False_Aspect_For_Derived_Type;
1037 -- Start of processing for Make_Pragma_From_Boolean_Aspect
1040 -- Note that we know Expr is present, because for a missing Expr
1041 -- argument, we knew it was True and did not need to delay the
1042 -- evaluation to the freeze point.
1044 if Is_False (Static_Boolean (Expr)) then
1045 Check_False_Aspect_For_Derived_Type;
1050 Pragma_Argument_Associations => New_List (
1051 Make_Pragma_Argument_Association (Sloc (Ident),
1052 Expression => New_Occurrence_Of (Ent, Sloc (Ident)))),
1054 Pragma_Identifier =>
1055 Make_Identifier (Sloc (Ident), Chars (Ident)));
1057 Set_From_Aspect_Specification (Prag, True);
1058 Set_Corresponding_Aspect (Prag, ASN);
1059 Set_Aspect_Rep_Item (ASN, Prag);
1060 Set_Is_Delayed_Aspect (Prag);
1061 Set_Parent (Prag, ASN);
1063 end Make_Pragma_From_Boolean_Aspect;
1065 -- Start of processing for Analyze_Aspects_At_Freeze_Point
1068 -- Must be visible in current scope
1070 if not Scope_Within_Or_Same (Current_Scope, Scope (E)) then
1074 -- Look for aspect specification entries for this entity
1076 ASN := First_Rep_Item (E);
1077 while Present (ASN) loop
1078 if Nkind (ASN) = N_Aspect_Specification then
1079 exit when Entity (ASN) /= E;
1081 if Is_Delayed_Aspect (ASN) then
1082 A_Id := Get_Aspect_Id (ASN);
1086 -- For aspects whose expression is an optional Boolean, make
1087 -- the corresponding pragma at the freezing point.
1089 when Boolean_Aspects |
1090 Library_Unit_Aspects =>
1091 Make_Pragma_From_Boolean_Aspect (ASN);
1093 -- Special handling for aspects that don't correspond to
1094 -- pragmas/attributes.
1096 when Aspect_Default_Value |
1097 Aspect_Default_Component_Value =>
1098 Analyze_Aspect_Default_Value (ASN);
1100 -- Ditto for iterator aspects, because the corresponding
1101 -- attributes may not have been analyzed yet.
1103 when Aspect_Constant_Indexing |
1104 Aspect_Variable_Indexing |
1105 Aspect_Default_Iterator |
1106 Aspect_Iterator_Element =>
1107 Analyze (Expression (ASN));
1113 Ritem := Aspect_Rep_Item (ASN);
1115 if Present (Ritem) then
1121 Next_Rep_Item (ASN);
1124 -- This is where we inherit delayed rep aspects from our parent. Note
1125 -- that if we fell out of the above loop with ASN non-empty, it means
1126 -- we hit an aspect for an entity other than E, and it must be the
1127 -- type from which we were derived.
1129 if May_Inherit_Delayed_Rep_Aspects (E) then
1130 Inherit_Delayed_Rep_Aspects (ASN);
1132 end Analyze_Aspects_At_Freeze_Point;
1134 -----------------------------------
1135 -- Analyze_Aspect_Specifications --
1136 -----------------------------------
1138 procedure Analyze_Aspect_Specifications (N : Node_Id; E : Entity_Id) is
1139 procedure Decorate_Delayed_Aspect_And_Pragma
1142 -- Establish the linkages between a delayed aspect and its corresponding
1143 -- pragma. Set all delay-related flags on both constructs.
1145 procedure Insert_Delayed_Pragma (Prag : Node_Id);
1146 -- Insert a postcondition-like pragma into the tree depending on the
1147 -- context. Prag must denote one of the following: Pre, Post, Depends,
1148 -- Global or Contract_Cases.
1150 ----------------------------------------
1151 -- Decorate_Delayed_Aspect_And_Pragma --
1152 ----------------------------------------
1154 procedure Decorate_Delayed_Aspect_And_Pragma
1159 Set_Aspect_Rep_Item (Asp, Prag);
1160 Set_Corresponding_Aspect (Prag, Asp);
1161 Set_From_Aspect_Specification (Prag);
1162 Set_Is_Delayed_Aspect (Prag);
1163 Set_Is_Delayed_Aspect (Asp);
1164 Set_Parent (Prag, Asp);
1165 end Decorate_Delayed_Aspect_And_Pragma;
1167 ---------------------------
1168 -- Insert_Delayed_Pragma --
1169 ---------------------------
1171 procedure Insert_Delayed_Pragma (Prag : Node_Id) is
1175 -- When the context is a library unit, the pragma is added to the
1176 -- Pragmas_After list.
1178 if Nkind (Parent (N)) = N_Compilation_Unit then
1179 Aux := Aux_Decls_Node (Parent (N));
1181 if No (Pragmas_After (Aux)) then
1182 Set_Pragmas_After (Aux, New_List);
1185 Prepend (Prag, Pragmas_After (Aux));
1187 -- Pragmas associated with subprogram bodies are inserted in the
1188 -- declarative part.
1190 elsif Nkind (N) = N_Subprogram_Body then
1191 if No (Declarations (N)) then
1192 Set_Declarations (N, New_List);
1195 Append (Prag, Declarations (N));
1200 Insert_After (N, Prag);
1202 -- Analyze the pragma before analyzing the proper body of a stub.
1203 -- This ensures that the pragma will appear on the proper contract
1204 -- list (see N_Contract).
1206 if Nkind (N) = N_Subprogram_Body_Stub then
1210 end Insert_Delayed_Pragma;
1218 L : constant List_Id := Aspect_Specifications (N);
1220 Ins_Node : Node_Id := N;
1221 -- Insert pragmas/attribute definition clause after this node when no
1222 -- delayed analysis is required.
1224 -- Start of processing for Analyze_Aspect_Specifications
1226 -- The general processing involves building an attribute definition
1227 -- clause or a pragma node that corresponds to the aspect. Then in order
1228 -- to delay the evaluation of this aspect to the freeze point, we attach
1229 -- the corresponding pragma/attribute definition clause to the aspect
1230 -- specification node, which is then placed in the Rep Item chain. In
1231 -- this case we mark the entity by setting the flag Has_Delayed_Aspects
1232 -- and we evaluate the rep item at the freeze point. When the aspect
1233 -- doesn't have a corresponding pragma/attribute definition clause, then
1234 -- its analysis is simply delayed at the freeze point.
1236 -- Some special cases don't require delay analysis, thus the aspect is
1237 -- analyzed right now.
1239 -- Note that there is a special handling for Pre, Post, Test_Case,
1240 -- Contract_Cases aspects. In these cases, we do not have to worry
1241 -- about delay issues, since the pragmas themselves deal with delay
1242 -- of visibility for the expression analysis. Thus, we just insert
1243 -- the pragma after the node N.
1246 pragma Assert (Present (L));
1248 -- Loop through aspects
1250 Aspect := First (L);
1251 Aspect_Loop : while Present (Aspect) loop
1252 Analyze_One_Aspect : declare
1253 Expr : constant Node_Id := Expression (Aspect);
1254 Id : constant Node_Id := Identifier (Aspect);
1255 Loc : constant Source_Ptr := Sloc (Aspect);
1256 Nam : constant Name_Id := Chars (Id);
1257 A_Id : constant Aspect_Id := Get_Aspect_Id (Nam);
1260 Delay_Required : Boolean;
1261 -- Set False if delay is not required
1263 Eloc : Source_Ptr := No_Location;
1264 -- Source location of expression, modified when we split PPC's. It
1265 -- is set below when Expr is present.
1267 procedure Analyze_Aspect_External_Or_Link_Name;
1268 -- Perform analysis of the External_Name or Link_Name aspects
1270 procedure Analyze_Aspect_Implicit_Dereference;
1271 -- Perform analysis of the Implicit_Dereference aspects
1273 procedure Make_Aitem_Pragma
1274 (Pragma_Argument_Associations : List_Id;
1275 Pragma_Name : Name_Id);
1276 -- This is a wrapper for Make_Pragma used for converting aspects
1277 -- to pragmas. It takes care of Sloc (set from Loc) and building
1278 -- the pragma identifier from the given name. In addition the
1279 -- flags Class_Present and Split_PPC are set from the aspect
1280 -- node, as well as Is_Ignored. This routine also sets the
1281 -- From_Aspect_Specification in the resulting pragma node to
1282 -- True, and sets Corresponding_Aspect to point to the aspect.
1283 -- The resulting pragma is assigned to Aitem.
1285 ------------------------------------------
1286 -- Analyze_Aspect_External_Or_Link_Name --
1287 ------------------------------------------
1289 procedure Analyze_Aspect_External_Or_Link_Name is
1291 -- Verify that there is an Import/Export aspect defined for the
1292 -- entity. The processing of that aspect in turn checks that
1293 -- there is a Convention aspect declared. The pragma is
1294 -- constructed when processing the Convention aspect.
1301 while Present (A) loop
1302 exit when Nam_In (Chars (Identifier (A)), Name_Export,
1309 ("missing Import/Export for Link/External name",
1313 end Analyze_Aspect_External_Or_Link_Name;
1315 -----------------------------------------
1316 -- Analyze_Aspect_Implicit_Dereference --
1317 -----------------------------------------
1319 procedure Analyze_Aspect_Implicit_Dereference is
1321 if not Is_Type (E) or else not Has_Discriminants (E) then
1323 ("aspect must apply to a type with discriminants", N);
1330 Disc := First_Discriminant (E);
1331 while Present (Disc) loop
1332 if Chars (Expr) = Chars (Disc)
1333 and then Ekind (Etype (Disc)) =
1334 E_Anonymous_Access_Type
1336 Set_Has_Implicit_Dereference (E);
1337 Set_Has_Implicit_Dereference (Disc);
1341 Next_Discriminant (Disc);
1344 -- Error if no proper access discriminant.
1347 ("not an access discriminant of&", Expr, E);
1350 end Analyze_Aspect_Implicit_Dereference;
1352 -----------------------
1353 -- Make_Aitem_Pragma --
1354 -----------------------
1356 procedure Make_Aitem_Pragma
1357 (Pragma_Argument_Associations : List_Id;
1358 Pragma_Name : Name_Id)
1361 -- We should never get here if aspect was disabled
1363 pragma Assert (not Is_Disabled (Aspect));
1369 Pragma_Argument_Associations =>
1370 Pragma_Argument_Associations,
1371 Pragma_Identifier =>
1372 Make_Identifier (Sloc (Id), Pragma_Name),
1373 Class_Present => Class_Present (Aspect),
1374 Split_PPC => Split_PPC (Aspect));
1376 -- Set additional semantic fields
1378 if Is_Ignored (Aspect) then
1379 Set_Is_Ignored (Aitem);
1382 Set_Corresponding_Aspect (Aitem, Aspect);
1383 Set_From_Aspect_Specification (Aitem, True);
1384 end Make_Aitem_Pragma;
1386 -- Start of processing for Analyze_One_Aspect
1389 -- Skip aspect if already analyzed (not clear if this is needed)
1391 if Analyzed (Aspect) then
1395 -- Skip looking at aspect if it is totally disabled. Just mark
1396 -- it as such for later reference in the tree. This also sets
1397 -- the Is_Ignored flag appropriately.
1399 Check_Applicable_Policy (Aspect);
1401 if Is_Disabled (Aspect) then
1405 -- Set the source location of expression, used in the case of
1406 -- a failed precondition/postcondition or invariant. Note that
1407 -- the source location of the expression is not usually the best
1408 -- choice here. For example, it gets located on the last AND
1409 -- keyword in a chain of boolean expressiond AND'ed together.
1410 -- It is best to put the message on the first character of the
1411 -- assertion, which is the effect of the First_Node call here.
1413 if Present (Expr) then
1414 Eloc := Sloc (First_Node (Expr));
1417 -- Check restriction No_Implementation_Aspect_Specifications
1419 if Implementation_Defined_Aspect (A_Id) then
1421 (No_Implementation_Aspect_Specifications, Aspect);
1424 -- Check restriction No_Specification_Of_Aspect
1426 Check_Restriction_No_Specification_Of_Aspect (Aspect);
1428 -- Analyze this aspect (actual analysis is delayed till later)
1430 Set_Analyzed (Aspect);
1431 Set_Entity (Aspect, E);
1432 Ent := New_Occurrence_Of (E, Sloc (Id));
1434 -- Check for duplicate aspect. Note that the Comes_From_Source
1435 -- test allows duplicate Pre/Post's that we generate internally
1436 -- to escape being flagged here.
1438 if No_Duplicates_Allowed (A_Id) then
1440 while Anod /= Aspect loop
1441 if Comes_From_Source (Aspect)
1442 and then Same_Aspect (A_Id, Get_Aspect_Id (Anod))
1444 Error_Msg_Name_1 := Nam;
1445 Error_Msg_Sloc := Sloc (Anod);
1447 -- Case of same aspect specified twice
1449 if Class_Present (Anod) = Class_Present (Aspect) then
1450 if not Class_Present (Anod) then
1452 ("aspect% for & previously given#",
1456 ("aspect `%''Class` for & previously given#",
1466 -- Check some general restrictions on language defined aspects
1468 if not Implementation_Defined_Aspect (A_Id) then
1469 Error_Msg_Name_1 := Nam;
1471 -- Not allowed for renaming declarations
1473 if Nkind (N) in N_Renaming_Declaration then
1475 ("aspect % not allowed for renaming declaration",
1479 -- Not allowed for formal type declarations
1481 if Nkind (N) = N_Formal_Type_Declaration then
1483 ("aspect % not allowed for formal type declaration",
1488 -- Copy expression for later processing by the procedures
1489 -- Check_Aspect_At_[Freeze_Point | End_Of_Declarations]
1491 Set_Entity (Id, New_Copy_Tree (Expr));
1493 -- Set Delay_Required as appropriate to aspect
1495 case Aspect_Delay (A_Id) is
1496 when Always_Delay =>
1497 Delay_Required := True;
1500 Delay_Required := False;
1504 -- If expression has the form of an integer literal, then
1505 -- do not delay, since we know the value cannot change.
1506 -- This optimization catches most rep clause cases.
1508 if (Present (Expr) and then Nkind (Expr) = N_Integer_Literal)
1509 or else (A_Id in Boolean_Aspects and then No (Expr))
1511 Delay_Required := False;
1513 Delay_Required := True;
1514 Set_Has_Delayed_Rep_Aspects (E);
1518 -- Processing based on specific aspect
1522 -- No_Aspect should be impossible
1525 raise Program_Error;
1527 -- Case 1: Aspects corresponding to attribute definition
1530 when Aspect_Address |
1533 Aspect_Component_Size |
1534 Aspect_Constant_Indexing |
1535 Aspect_Default_Iterator |
1536 Aspect_Dispatching_Domain |
1537 Aspect_External_Tag |
1539 Aspect_Iterator_Element |
1540 Aspect_Machine_Radix |
1541 Aspect_Object_Size |
1544 Aspect_Scalar_Storage_Order |
1547 Aspect_Simple_Storage_Pool |
1548 Aspect_Storage_Pool |
1549 Aspect_Stream_Size |
1551 Aspect_Variable_Indexing |
1554 -- Indexing aspects apply only to tagged type
1556 if (A_Id = Aspect_Constant_Indexing
1558 A_Id = Aspect_Variable_Indexing)
1559 and then not (Is_Type (E)
1560 and then Is_Tagged_Type (E))
1562 Error_Msg_N ("indexing applies to a tagged type", N);
1566 -- Construct the attribute definition clause
1569 Make_Attribute_Definition_Clause (Loc,
1571 Chars => Chars (Id),
1572 Expression => Relocate_Node (Expr));
1574 -- If the address is specified, then we treat the entity as
1575 -- referenced, to avoid spurious warnings. This is analogous
1576 -- to what is done with an attribute definition clause, but
1577 -- here we don't want to generate a reference because this
1578 -- is the point of definition of the entity.
1580 if A_Id = Aspect_Address then
1584 -- Case 2: Aspects corresponding to pragmas
1586 -- Case 2a: Aspects corresponding to pragmas with two
1587 -- arguments, where the first argument is a local name
1588 -- referring to the entity, and the second argument is the
1589 -- aspect definition expression.
1591 -- Suppress/Unsuppress
1593 when Aspect_Suppress |
1594 Aspect_Unsuppress =>
1597 (Pragma_Argument_Associations => New_List (
1598 Make_Pragma_Argument_Association (Loc,
1599 Expression => New_Occurrence_Of (E, Loc)),
1600 Make_Pragma_Argument_Association (Sloc (Expr),
1601 Expression => Relocate_Node (Expr))),
1602 Pragma_Name => Chars (Id));
1606 -- Corresponds to pragma Implemented, construct the pragma
1608 when Aspect_Synchronization =>
1611 (Pragma_Argument_Associations => New_List (
1612 Make_Pragma_Argument_Association (Loc,
1613 Expression => New_Occurrence_Of (E, Loc)),
1614 Make_Pragma_Argument_Association (Sloc (Expr),
1615 Expression => Relocate_Node (Expr))),
1616 Pragma_Name => Name_Implemented);
1620 when Aspect_Attach_Handler =>
1622 (Pragma_Argument_Associations => New_List (
1623 Make_Pragma_Argument_Association (Sloc (Ent),
1625 Make_Pragma_Argument_Association (Sloc (Expr),
1626 Expression => Relocate_Node (Expr))),
1627 Pragma_Name => Name_Attach_Handler);
1629 -- Dynamic_Predicate, Predicate, Static_Predicate
1631 when Aspect_Dynamic_Predicate |
1633 Aspect_Static_Predicate =>
1635 -- Construct the pragma (always a pragma Predicate, with
1636 -- flags recording whether it is static/dynamic). We also
1637 -- set flags recording this in the type itself.
1640 (Pragma_Argument_Associations => New_List (
1641 Make_Pragma_Argument_Association (Sloc (Ent),
1643 Make_Pragma_Argument_Association (Sloc (Expr),
1644 Expression => Relocate_Node (Expr))),
1645 Pragma_Name => Name_Predicate);
1647 -- Mark type has predicates, and remember what kind of
1648 -- aspect lead to this predicate (we need this to access
1649 -- the right set of check policies later on).
1651 Set_Has_Predicates (E);
1653 if A_Id = Aspect_Dynamic_Predicate then
1654 Set_Has_Dynamic_Predicate_Aspect (E);
1655 elsif A_Id = Aspect_Static_Predicate then
1656 Set_Has_Static_Predicate_Aspect (E);
1659 -- If the type is private, indicate that its completion
1660 -- has a freeze node, because that is the one that will be
1661 -- visible at freeze time.
1663 if Is_Private_Type (E) and then Present (Full_View (E)) then
1664 Set_Has_Predicates (Full_View (E));
1666 if A_Id = Aspect_Dynamic_Predicate then
1667 Set_Has_Dynamic_Predicate_Aspect (Full_View (E));
1668 elsif A_Id = Aspect_Static_Predicate then
1669 Set_Has_Static_Predicate_Aspect (Full_View (E));
1672 Set_Has_Delayed_Aspects (Full_View (E));
1673 Ensure_Freeze_Node (Full_View (E));
1676 -- Case 2b: Aspects corresponding to pragmas with two
1677 -- arguments, where the second argument is a local name
1678 -- referring to the entity, and the first argument is the
1679 -- aspect definition expression.
1683 when Aspect_Convention =>
1685 -- The aspect may be part of the specification of an import
1686 -- or export pragma. Scan the aspect list to gather the
1687 -- other components, if any. The name of the generated
1688 -- pragma is one of Convention/Import/Export.
1700 P_Name := Chars (Id);
1702 Arg_List := New_List;
1707 while Present (A) loop
1708 A_Name := Chars (Identifier (A));
1710 if Nam_In (A_Name, Name_Import, Name_Export) then
1712 Error_Msg_N ("conflicting", A);
1719 elsif A_Name = Name_Link_Name then
1721 Make_Pragma_Argument_Association (Loc,
1723 Expression => Relocate_Node (Expression (A)));
1725 elsif A_Name = Name_External_Name then
1727 Make_Pragma_Argument_Association (Loc,
1729 Expression => Relocate_Node (Expression (A)));
1735 Arg_List := New_List (
1736 Make_Pragma_Argument_Association (Sloc (Expr),
1737 Expression => Relocate_Node (Expr)),
1738 Make_Pragma_Argument_Association (Sloc (Ent),
1739 Expression => Ent));
1741 if Present (L_Assoc) then
1742 Append_To (Arg_List, L_Assoc);
1745 if Present (E_Assoc) then
1746 Append_To (Arg_List, E_Assoc);
1750 (Pragma_Argument_Associations => Arg_List,
1751 Pragma_Name => P_Name);
1754 -- CPU, Interrupt_Priority, Priority
1756 -- These three aspects can be specified for a subprogram body,
1757 -- in which case we generate pragmas for them and insert them
1758 -- ahead of local declarations, rather than after the body.
1761 Aspect_Interrupt_Priority |
1764 if Nkind (N) = N_Subprogram_Body then
1766 (Pragma_Argument_Associations => New_List (
1767 Make_Pragma_Argument_Association (Sloc (Expr),
1768 Expression => Relocate_Node (Expr))),
1769 Pragma_Name => Chars (Id));
1773 Make_Attribute_Definition_Clause (Loc,
1775 Chars => Chars (Id),
1776 Expression => Relocate_Node (Expr));
1781 when Aspect_Warnings =>
1784 (Pragma_Argument_Associations => New_List (
1785 Make_Pragma_Argument_Association (Sloc (Expr),
1786 Expression => Relocate_Node (Expr)),
1787 Make_Pragma_Argument_Association (Loc,
1788 Expression => New_Occurrence_Of (E, Loc))),
1789 Pragma_Name => Chars (Id));
1791 -- Case 2c: Aspects corresponding to pragmas with three
1794 -- Invariant aspects have a first argument that references the
1795 -- entity, a second argument that is the expression and a third
1796 -- argument that is an appropriate message.
1798 -- Invariant, Type_Invariant
1800 when Aspect_Invariant |
1801 Aspect_Type_Invariant =>
1803 -- Analysis of the pragma will verify placement legality:
1804 -- an invariant must apply to a private type, or appear in
1805 -- the private part of a spec and apply to a completion.
1808 (Pragma_Argument_Associations => New_List (
1809 Make_Pragma_Argument_Association (Sloc (Ent),
1811 Make_Pragma_Argument_Association (Sloc (Expr),
1812 Expression => Relocate_Node (Expr))),
1813 Pragma_Name => Name_Invariant);
1815 -- Add message unless exception messages are suppressed
1817 if not Opt.Exception_Locations_Suppressed then
1818 Append_To (Pragma_Argument_Associations (Aitem),
1819 Make_Pragma_Argument_Association (Eloc,
1820 Chars => Name_Message,
1822 Make_String_Literal (Eloc,
1823 Strval => "failed invariant from "
1824 & Build_Location_String (Eloc))));
1827 -- For Invariant case, insert immediately after the entity
1828 -- declaration. We do not have to worry about delay issues
1829 -- since the pragma processing takes care of this.
1831 Delay_Required := False;
1833 -- Case 2d : Aspects that correspond to a pragma with one
1838 when Aspect_Abstract_State =>
1840 (Pragma_Argument_Associations => New_List (
1841 Make_Pragma_Argument_Association (Loc,
1842 Expression => Relocate_Node (Expr))),
1843 Pragma_Name => Name_Abstract_State);
1847 -- Aspect Depends must be delayed because it mentions names
1848 -- of inputs and output that are classified by aspect Global.
1849 -- The aspect and pragma are treated the same way as a post
1852 when Aspect_Depends =>
1854 (Pragma_Argument_Associations => New_List (
1855 Make_Pragma_Argument_Association (Loc,
1856 Expression => Relocate_Node (Expr))),
1857 Pragma_Name => Name_Depends);
1859 Decorate_Delayed_Aspect_And_Pragma (Aspect, Aitem);
1860 Insert_Delayed_Pragma (Aitem);
1865 -- Aspect Global must be delayed because it can mention names
1866 -- and benefit from the forward visibility rules applicable to
1867 -- aspects of subprograms. The aspect and pragma are treated
1868 -- the same way as a post condition.
1870 when Aspect_Global =>
1872 (Pragma_Argument_Associations => New_List (
1873 Make_Pragma_Argument_Association (Loc,
1874 Expression => Relocate_Node (Expr))),
1875 Pragma_Name => Name_Global);
1877 Decorate_Delayed_Aspect_And_Pragma (Aspect, Aitem);
1878 Insert_Delayed_Pragma (Aitem);
1883 when Aspect_SPARK_Mode =>
1885 (Pragma_Argument_Associations => New_List (
1886 Make_Pragma_Argument_Association (Loc,
1887 Expression => Relocate_Node (Expr))),
1888 Pragma_Name => Name_SPARK_Mode);
1890 -- Relative_Deadline
1892 when Aspect_Relative_Deadline =>
1894 (Pragma_Argument_Associations => New_List (
1895 Make_Pragma_Argument_Association (Loc,
1896 Expression => Relocate_Node (Expr))),
1897 Pragma_Name => Name_Relative_Deadline);
1899 -- If the aspect applies to a task, the corresponding pragma
1900 -- must appear within its declarations, not after.
1902 if Nkind (N) = N_Task_Type_Declaration then
1908 if No (Task_Definition (N)) then
1909 Set_Task_Definition (N,
1910 Make_Task_Definition (Loc,
1911 Visible_Declarations => New_List,
1912 End_Label => Empty));
1915 Def := Task_Definition (N);
1916 V := Visible_Declarations (Def);
1917 if not Is_Empty_List (V) then
1918 Insert_Before (First (V), Aitem);
1921 Set_Visible_Declarations (Def, New_List (Aitem));
1928 -- Case 3 : Aspects that don't correspond to pragma/attribute
1929 -- definition clause.
1931 -- Case 3a: The aspects listed below don't correspond to
1932 -- pragmas/attributes but do require delayed analysis.
1934 -- Default_Value, Default_Component_Value
1936 when Aspect_Default_Value |
1937 Aspect_Default_Component_Value =>
1940 -- Case 3b: The aspects listed below don't correspond to
1941 -- pragmas/attributes and don't need delayed analysis.
1943 -- Implicit_Dereference
1945 -- For Implicit_Dereference, External_Name and Link_Name, only
1946 -- the legality checks are done during the analysis, thus no
1947 -- delay is required.
1949 when Aspect_Implicit_Dereference =>
1950 Analyze_Aspect_Implicit_Dereference;
1953 -- External_Name, Link_Name
1955 when Aspect_External_Name |
1957 Analyze_Aspect_External_Or_Link_Name;
1962 when Aspect_Dimension =>
1963 Analyze_Aspect_Dimension (N, Id, Expr);
1968 when Aspect_Dimension_System =>
1969 Analyze_Aspect_Dimension_System (N, Id, Expr);
1972 -- Case 4: Aspects requiring special handling
1974 -- Pre/Post/Test_Case/Contract_Cases whose corresponding
1975 -- pragmas take care of the delay.
1979 -- Aspects Pre/Post generate Precondition/Postcondition pragmas
1980 -- with a first argument that is the expression, and a second
1981 -- argument that is an informative message if the test fails.
1982 -- This is inserted right after the declaration, to get the
1983 -- required pragma placement. The processing for the pragmas
1984 -- takes care of the required delay.
1986 when Pre_Post_Aspects => Pre_Post : declare
1990 if A_Id = Aspect_Pre or else A_Id = Aspect_Precondition then
1991 Pname := Name_Precondition;
1993 Pname := Name_Postcondition;
1996 -- If the expressions is of the form A and then B, then
1997 -- we generate separate Pre/Post aspects for the separate
1998 -- clauses. Since we allow multiple pragmas, there is no
1999 -- problem in allowing multiple Pre/Post aspects internally.
2000 -- These should be treated in reverse order (B first and
2001 -- A second) since they are later inserted just after N in
2002 -- the order they are treated. This way, the pragma for A
2003 -- ends up preceding the pragma for B, which may have an
2004 -- importance for the error raised (either constraint error
2005 -- or precondition error).
2007 -- We do not do this for Pre'Class, since we have to put
2008 -- these conditions together in a complex OR expression
2010 -- We do not do this in ASIS mode, as ASIS relies on the
2011 -- original node representing the complete expression, when
2012 -- retrieving it through the source aspect table.
2015 and then (Pname = Name_Postcondition
2016 or else not Class_Present (Aspect))
2018 while Nkind (Expr) = N_And_Then loop
2019 Insert_After (Aspect,
2020 Make_Aspect_Specification (Sloc (Left_Opnd (Expr)),
2021 Identifier => Identifier (Aspect),
2022 Expression => Relocate_Node (Left_Opnd (Expr)),
2023 Class_Present => Class_Present (Aspect),
2024 Split_PPC => True));
2025 Rewrite (Expr, Relocate_Node (Right_Opnd (Expr)));
2026 Eloc := Sloc (Expr);
2030 -- Build the precondition/postcondition pragma
2032 -- Add note about why we do NOT need Copy_Tree here ???
2035 (Pragma_Argument_Associations => New_List (
2036 Make_Pragma_Argument_Association (Eloc,
2037 Chars => Name_Check,
2038 Expression => Relocate_Node (Expr))),
2039 Pragma_Name => Pname);
2041 -- Add message unless exception messages are suppressed
2043 if not Opt.Exception_Locations_Suppressed then
2044 Append_To (Pragma_Argument_Associations (Aitem),
2045 Make_Pragma_Argument_Association (Eloc,
2046 Chars => Name_Message,
2048 Make_String_Literal (Eloc,
2050 & Get_Name_String (Pname)
2052 & Build_Location_String (Eloc))));
2055 Set_Is_Delayed_Aspect (Aspect);
2057 -- For Pre/Post cases, insert immediately after the entity
2058 -- declaration, since that is the required pragma placement.
2059 -- Note that for these aspects, we do not have to worry
2060 -- about delay issues, since the pragmas themselves deal
2061 -- with delay of visibility for the expression analysis.
2063 Insert_Delayed_Pragma (Aitem);
2069 when Aspect_Test_Case => Test_Case : declare
2071 Comp_Expr : Node_Id;
2072 Comp_Assn : Node_Id;
2078 if Nkind (Parent (N)) = N_Compilation_Unit then
2079 Error_Msg_Name_1 := Nam;
2080 Error_Msg_N ("incorrect placement of aspect `%`", E);
2084 if Nkind (Expr) /= N_Aggregate then
2085 Error_Msg_Name_1 := Nam;
2087 ("wrong syntax for aspect `%` for &", Id, E);
2091 -- Make pragma expressions refer to the original aspect
2092 -- expressions through the Original_Node link. This is
2093 -- used in semantic analysis for ASIS mode, so that the
2094 -- original expression also gets analyzed.
2096 Comp_Expr := First (Expressions (Expr));
2097 while Present (Comp_Expr) loop
2098 New_Expr := Relocate_Node (Comp_Expr);
2099 Set_Original_Node (New_Expr, Comp_Expr);
2101 Make_Pragma_Argument_Association (Sloc (Comp_Expr),
2102 Expression => New_Expr));
2106 Comp_Assn := First (Component_Associations (Expr));
2107 while Present (Comp_Assn) loop
2108 if List_Length (Choices (Comp_Assn)) /= 1
2110 Nkind (First (Choices (Comp_Assn))) /= N_Identifier
2112 Error_Msg_Name_1 := Nam;
2114 ("wrong syntax for aspect `%` for &", Id, E);
2118 New_Expr := Relocate_Node (Expression (Comp_Assn));
2119 Set_Original_Node (New_Expr, Expression (Comp_Assn));
2121 Make_Pragma_Argument_Association (Sloc (Comp_Assn),
2122 Chars => Chars (First (Choices (Comp_Assn))),
2123 Expression => New_Expr));
2127 -- Build the test-case pragma
2130 (Pragma_Argument_Associations => Args,
2131 Pragma_Name => Nam);
2136 when Aspect_Contract_Cases =>
2138 (Pragma_Argument_Associations => New_List (
2139 Make_Pragma_Argument_Association (Loc,
2140 Expression => Relocate_Node (Expr))),
2141 Pragma_Name => Nam);
2143 Decorate_Delayed_Aspect_And_Pragma (Aspect, Aitem);
2144 Insert_Delayed_Pragma (Aitem);
2147 -- Case 5: Special handling for aspects with an optional
2148 -- boolean argument.
2150 -- In the general case, the corresponding pragma cannot be
2151 -- generated yet because the evaluation of the boolean needs
2152 -- to be delayed till the freeze point.
2154 when Boolean_Aspects |
2155 Library_Unit_Aspects =>
2157 Set_Is_Boolean_Aspect (Aspect);
2159 -- Lock_Free aspect only apply to protected objects
2161 if A_Id = Aspect_Lock_Free then
2162 if Ekind (E) /= E_Protected_Type then
2163 Error_Msg_Name_1 := Nam;
2165 ("aspect % only applies to a protected object",
2169 -- Set the Uses_Lock_Free flag to True if there is no
2170 -- expression or if the expression is True. The
2171 -- evaluation of this aspect should be delayed to the
2172 -- freeze point (why???)
2175 or else Is_True (Static_Boolean (Expr))
2177 Set_Uses_Lock_Free (E);
2180 Record_Rep_Item (E, Aspect);
2185 elsif A_Id = Aspect_Import or else A_Id = Aspect_Export then
2187 -- Verify that there is an aspect Convention that will
2188 -- incorporate the Import/Export aspect, and eventual
2189 -- Link/External names.
2196 while Present (A) loop
2197 exit when Chars (Identifier (A)) = Name_Convention;
2203 ("missing Convention aspect for Export/Import",
2211 -- Library unit aspects require special handling in the case
2212 -- of a package declaration, the pragma needs to be inserted
2213 -- in the list of declarations for the associated package.
2214 -- There is no issue of visibility delay for these aspects.
2216 if A_Id in Library_Unit_Aspects
2218 Nkind_In (N, N_Package_Declaration,
2219 N_Generic_Package_Declaration)
2220 and then Nkind (Parent (N)) /= N_Compilation_Unit
2223 ("incorrect context for library unit aspect&", Id);
2227 -- Cases where we do not delay, includes all cases where
2228 -- the expression is missing other than the above cases.
2230 if not Delay_Required or else No (Expr) then
2232 (Pragma_Argument_Associations => New_List (
2233 Make_Pragma_Argument_Association (Sloc (Ent),
2234 Expression => Ent)),
2235 Pragma_Name => Chars (Id));
2236 Delay_Required := False;
2238 -- In general cases, the corresponding pragma/attribute
2239 -- definition clause will be inserted later at the freezing
2240 -- point, and we do not need to build it now
2248 -- This is special because for access types we need to generate
2249 -- an attribute definition clause. This also works for single
2250 -- task declarations, but it does not work for task type
2251 -- declarations, because we have the case where the expression
2252 -- references a discriminant of the task type. That can't use
2253 -- an attribute definition clause because we would not have
2254 -- visibility on the discriminant. For that case we must
2255 -- generate a pragma in the task definition.
2257 when Aspect_Storage_Size =>
2261 if Ekind (E) = E_Task_Type then
2263 Decl : constant Node_Id := Declaration_Node (E);
2266 pragma Assert (Nkind (Decl) = N_Task_Type_Declaration);
2268 -- If no task definition, create one
2270 if No (Task_Definition (Decl)) then
2271 Set_Task_Definition (Decl,
2272 Make_Task_Definition (Loc,
2273 Visible_Declarations => Empty_List,
2274 End_Label => Empty));
2277 -- Create a pragma and put it at the start of the
2278 -- task definition for the task type declaration.
2281 (Pragma_Argument_Associations => New_List (
2282 Make_Pragma_Argument_Association (Loc,
2283 Expression => Relocate_Node (Expr))),
2284 Pragma_Name => Name_Storage_Size);
2288 Visible_Declarations (Task_Definition (Decl)));
2292 -- All other cases, generate attribute definition
2296 Make_Attribute_Definition_Clause (Loc,
2298 Chars => Chars (Id),
2299 Expression => Relocate_Node (Expr));
2303 -- Attach the corresponding pragma/attribute definition clause to
2304 -- the aspect specification node.
2306 if Present (Aitem) then
2307 Set_From_Aspect_Specification (Aitem, True);
2310 -- Aspect Abstract_State introduces implicit declarations for all
2311 -- state abstraction entities it defines. To emulate this behavior
2312 -- insert the pragma at the start of the visible declarations of
2313 -- the related package.
2315 if Nam = Name_Abstract_State
2316 and then Nkind (N) = N_Package_Declaration
2318 if No (Visible_Declarations (Specification (N))) then
2319 Set_Visible_Declarations (Specification (N), New_List);
2322 Prepend (Aitem, Visible_Declarations (Specification (N)));
2325 -- In the context of a compilation unit, we directly put the
2326 -- pragma in the Pragmas_After list of the N_Compilation_Unit_Aux
2327 -- node (no delay is required here) except for aspects on a
2328 -- subprogram body (see below) and a generic package, for which
2329 -- we need to introduce the pragma before building the generic
2330 -- copy (see sem_ch12), and for package instantiations, where
2331 -- the library unit pragmas are better handled early.
2333 elsif Nkind (Parent (N)) = N_Compilation_Unit
2334 and then (Present (Aitem) or else Is_Boolean_Aspect (Aspect))
2337 Aux : constant Node_Id := Aux_Decls_Node (Parent (N));
2340 pragma Assert (Nkind (Aux) = N_Compilation_Unit_Aux);
2342 -- For a Boolean aspect, create the corresponding pragma if
2343 -- no expression or if the value is True.
2345 if Is_Boolean_Aspect (Aspect) and then No (Aitem) then
2346 if Is_True (Static_Boolean (Expr)) then
2348 (Pragma_Argument_Associations => New_List (
2349 Make_Pragma_Argument_Association (Sloc (Ent),
2350 Expression => Ent)),
2351 Pragma_Name => Chars (Id));
2353 Set_From_Aspect_Specification (Aitem, True);
2354 Set_Corresponding_Aspect (Aitem, Aspect);
2361 -- If the aspect is on a subprogram body (relevant aspects
2362 -- are Inline and Priority), add the pragma in front of
2363 -- the declarations.
2365 if Nkind (N) = N_Subprogram_Body then
2366 if No (Declarations (N)) then
2367 Set_Declarations (N, New_List);
2370 Prepend (Aitem, Declarations (N));
2372 elsif Nkind (N) = N_Generic_Package_Declaration then
2373 if No (Visible_Declarations (Specification (N))) then
2374 Set_Visible_Declarations (Specification (N), New_List);
2378 Visible_Declarations (Specification (N)));
2380 elsif Nkind (N) = N_Package_Instantiation then
2382 Spec : constant Node_Id :=
2383 Specification (Instance_Spec (N));
2385 if No (Visible_Declarations (Spec)) then
2386 Set_Visible_Declarations (Spec, New_List);
2389 Prepend (Aitem, Visible_Declarations (Spec));
2393 if No (Pragmas_After (Aux)) then
2394 Set_Pragmas_After (Aux, New_List);
2397 Append (Aitem, Pragmas_After (Aux));
2404 -- The evaluation of the aspect is delayed to the freezing point.
2405 -- The pragma or attribute clause if there is one is then attached
2406 -- to the aspect specification which is put in the rep item list.
2408 if Delay_Required then
2409 if Present (Aitem) then
2410 Set_Is_Delayed_Aspect (Aitem);
2411 Set_Aspect_Rep_Item (Aspect, Aitem);
2412 Set_Parent (Aitem, Aspect);
2415 Set_Is_Delayed_Aspect (Aspect);
2417 -- In the case of Default_Value, link the aspect to base type
2418 -- as well, even though it appears on a first subtype. This is
2419 -- mandated by the semantics of the aspect. Do not establish
2420 -- the link when processing the base type itself as this leads
2421 -- to a rep item circularity. Verify that we are dealing with
2422 -- a scalar type to prevent cascaded errors.
2424 if A_Id = Aspect_Default_Value
2425 and then Is_Scalar_Type (E)
2426 and then Base_Type (E) /= E
2428 Set_Has_Delayed_Aspects (Base_Type (E));
2429 Record_Rep_Item (Base_Type (E), Aspect);
2432 Set_Has_Delayed_Aspects (E);
2433 Record_Rep_Item (E, Aspect);
2435 -- When delay is not required and the context is not a compilation
2436 -- unit, we simply insert the pragma/attribute definition clause
2440 Insert_After (Ins_Node, Aitem);
2443 end Analyze_One_Aspect;
2447 end loop Aspect_Loop;
2449 if Has_Delayed_Aspects (E) then
2450 Ensure_Freeze_Node (E);
2452 end Analyze_Aspect_Specifications;
2454 -----------------------
2455 -- Analyze_At_Clause --
2456 -----------------------
2458 -- An at clause is replaced by the corresponding Address attribute
2459 -- definition clause that is the preferred approach in Ada 95.
2461 procedure Analyze_At_Clause (N : Node_Id) is
2462 CS : constant Boolean := Comes_From_Source (N);
2465 -- This is an obsolescent feature
2467 Check_Restriction (No_Obsolescent_Features, N);
2469 if Warn_On_Obsolescent_Feature then
2471 ("?j?at clause is an obsolescent feature (RM J.7(2))", N);
2473 ("\?j?use address attribute definition clause instead", N);
2476 -- Rewrite as address clause
2479 Make_Attribute_Definition_Clause (Sloc (N),
2480 Name => Identifier (N),
2481 Chars => Name_Address,
2482 Expression => Expression (N)));
2484 -- We preserve Comes_From_Source, since logically the clause still comes
2485 -- from the source program even though it is changed in form.
2487 Set_Comes_From_Source (N, CS);
2489 -- Analyze rewritten clause
2491 Analyze_Attribute_Definition_Clause (N);
2492 end Analyze_At_Clause;
2494 -----------------------------------------
2495 -- Analyze_Attribute_Definition_Clause --
2496 -----------------------------------------
2498 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
2499 Loc : constant Source_Ptr := Sloc (N);
2500 Nam : constant Node_Id := Name (N);
2501 Attr : constant Name_Id := Chars (N);
2502 Expr : constant Node_Id := Expression (N);
2503 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
2506 -- The entity of Nam after it is analyzed. In the case of an incomplete
2507 -- type, this is the underlying type.
2510 -- The underlying entity to which the attribute applies. Generally this
2511 -- is the Underlying_Type of Ent, except in the case where the clause
2512 -- applies to full view of incomplete type or private type in which case
2513 -- U_Ent is just a copy of Ent.
2515 FOnly : Boolean := False;
2516 -- Reset to True for subtype specific attribute (Alignment, Size)
2517 -- and for stream attributes, i.e. those cases where in the call
2518 -- to Rep_Item_Too_Late, FOnly is set True so that only the freezing
2519 -- rules are checked. Note that the case of stream attributes is not
2520 -- clear from the RM, but see AI95-00137. Also, the RM seems to
2521 -- disallow Storage_Size for derived task types, but that is also
2522 -- clearly unintentional.
2524 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
2525 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
2526 -- definition clauses.
2528 function Duplicate_Clause return Boolean;
2529 -- This routine checks if the aspect for U_Ent being given by attribute
2530 -- definition clause N is for an aspect that has already been specified,
2531 -- and if so gives an error message. If there is a duplicate, True is
2532 -- returned, otherwise if there is no error, False is returned.
2534 procedure Check_Indexing_Functions;
2535 -- Check that the function in Constant_Indexing or Variable_Indexing
2536 -- attribute has the proper type structure. If the name is overloaded,
2537 -- check that some interpretation is legal.
2539 procedure Check_Iterator_Functions;
2540 -- Check that there is a single function in Default_Iterator attribute
2541 -- has the proper type structure.
2543 function Check_Primitive_Function (Subp : Entity_Id) return Boolean;
2544 -- Common legality check for the previous two
2546 -----------------------------------
2547 -- Analyze_Stream_TSS_Definition --
2548 -----------------------------------
2550 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
2551 Subp : Entity_Id := Empty;
2556 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
2557 -- True for Read attribute, false for other attributes
2559 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
2560 -- Return true if the entity is a subprogram with an appropriate
2561 -- profile for the attribute being defined.
2563 ----------------------
2564 -- Has_Good_Profile --
2565 ----------------------
2567 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
2569 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
2570 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
2571 (False => E_Procedure, True => E_Function);
2575 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
2579 F := First_Formal (Subp);
2582 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
2583 or else Designated_Type (Etype (F)) /=
2584 Class_Wide_Type (RTE (RE_Root_Stream_Type))
2589 if not Is_Function then
2593 Expected_Mode : constant array (Boolean) of Entity_Kind :=
2594 (False => E_In_Parameter,
2595 True => E_Out_Parameter);
2597 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
2605 Typ := Etype (Subp);
2608 return Base_Type (Typ) = Base_Type (Ent)
2609 and then No (Next_Formal (F));
2610 end Has_Good_Profile;
2612 -- Start of processing for Analyze_Stream_TSS_Definition
2617 if not Is_Type (U_Ent) then
2618 Error_Msg_N ("local name must be a subtype", Nam);
2622 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
2624 -- If Pnam is present, it can be either inherited from an ancestor
2625 -- type (in which case it is legal to redefine it for this type), or
2626 -- be a previous definition of the attribute for the same type (in
2627 -- which case it is illegal).
2629 -- In the first case, it will have been analyzed already, and we
2630 -- can check that its profile does not match the expected profile
2631 -- for a stream attribute of U_Ent. In the second case, either Pnam
2632 -- has been analyzed (and has the expected profile), or it has not
2633 -- been analyzed yet (case of a type that has not been frozen yet
2634 -- and for which the stream attribute has been set using Set_TSS).
2637 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
2639 Error_Msg_Sloc := Sloc (Pnam);
2640 Error_Msg_Name_1 := Attr;
2641 Error_Msg_N ("% attribute already defined #", Nam);
2647 if Is_Entity_Name (Expr) then
2648 if not Is_Overloaded (Expr) then
2649 if Has_Good_Profile (Entity (Expr)) then
2650 Subp := Entity (Expr);
2654 Get_First_Interp (Expr, I, It);
2655 while Present (It.Nam) loop
2656 if Has_Good_Profile (It.Nam) then
2661 Get_Next_Interp (I, It);
2666 if Present (Subp) then
2667 if Is_Abstract_Subprogram (Subp) then
2668 Error_Msg_N ("stream subprogram must not be abstract", Expr);
2672 Set_Entity (Expr, Subp);
2673 Set_Etype (Expr, Etype (Subp));
2675 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
2678 Error_Msg_Name_1 := Attr;
2679 Error_Msg_N ("incorrect expression for% attribute", Expr);
2681 end Analyze_Stream_TSS_Definition;
2683 ------------------------------
2684 -- Check_Indexing_Functions --
2685 ------------------------------
2687 procedure Check_Indexing_Functions is
2688 Indexing_Found : Boolean;
2690 procedure Check_One_Function (Subp : Entity_Id);
2691 -- Check one possible interpretation. Sets Indexing_Found True if an
2692 -- indexing function is found.
2694 ------------------------
2695 -- Check_One_Function --
2696 ------------------------
2698 procedure Check_One_Function (Subp : Entity_Id) is
2699 Default_Element : constant Node_Id :=
2700 Find_Value_Of_Aspect
2701 (Etype (First_Formal (Subp)),
2702 Aspect_Iterator_Element);
2705 if not Check_Primitive_Function (Subp)
2706 and then not Is_Overloaded (Expr)
2709 ("aspect Indexing requires a function that applies to type&",
2713 -- An indexing function must return either the default element of
2714 -- the container, or a reference type. For variable indexing it
2715 -- must be the latter.
2717 if Present (Default_Element) then
2718 Analyze (Default_Element);
2720 if Is_Entity_Name (Default_Element)
2721 and then Covers (Entity (Default_Element), Etype (Subp))
2723 Indexing_Found := True;
2728 -- For variable_indexing the return type must be a reference type
2730 if Attr = Name_Variable_Indexing
2731 and then not Has_Implicit_Dereference (Etype (Subp))
2734 ("function for indexing must return a reference type", Subp);
2737 Indexing_Found := True;
2739 end Check_One_Function;
2741 -- Start of processing for Check_Indexing_Functions
2750 if not Is_Overloaded (Expr) then
2751 Check_One_Function (Entity (Expr));
2759 Indexing_Found := False;
2760 Get_First_Interp (Expr, I, It);
2761 while Present (It.Nam) loop
2763 -- Note that analysis will have added the interpretation
2764 -- that corresponds to the dereference. We only check the
2765 -- subprogram itself.
2767 if Is_Overloadable (It.Nam) then
2768 Check_One_Function (It.Nam);
2771 Get_Next_Interp (I, It);
2774 if not Indexing_Found then
2776 ("aspect Indexing requires a function that "
2777 & "applies to type&", Expr, Ent);
2781 end Check_Indexing_Functions;
2783 ------------------------------
2784 -- Check_Iterator_Functions --
2785 ------------------------------
2787 procedure Check_Iterator_Functions is
2788 Default : Entity_Id;
2790 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean;
2791 -- Check one possible interpretation for validity
2793 ----------------------------
2794 -- Valid_Default_Iterator --
2795 ----------------------------
2797 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean is
2801 if not Check_Primitive_Function (Subp) then
2804 Formal := First_Formal (Subp);
2807 -- False if any subsequent formal has no default expression
2809 Formal := Next_Formal (Formal);
2810 while Present (Formal) loop
2811 if No (Expression (Parent (Formal))) then
2815 Next_Formal (Formal);
2818 -- True if all subsequent formals have default expressions
2821 end Valid_Default_Iterator;
2823 -- Start of processing for Check_Iterator_Functions
2828 if not Is_Entity_Name (Expr) then
2829 Error_Msg_N ("aspect Iterator must be a function name", Expr);
2832 if not Is_Overloaded (Expr) then
2833 if not Check_Primitive_Function (Entity (Expr)) then
2835 ("aspect Indexing requires a function that applies to type&",
2836 Entity (Expr), Ent);
2839 if not Valid_Default_Iterator (Entity (Expr)) then
2840 Error_Msg_N ("improper function for default iterator", Expr);
2850 Get_First_Interp (Expr, I, It);
2851 while Present (It.Nam) loop
2852 if not Check_Primitive_Function (It.Nam)
2853 or else not Valid_Default_Iterator (It.Nam)
2857 elsif Present (Default) then
2858 Error_Msg_N ("default iterator must be unique", Expr);
2864 Get_Next_Interp (I, It);
2868 if Present (Default) then
2869 Set_Entity (Expr, Default);
2870 Set_Is_Overloaded (Expr, False);
2873 end Check_Iterator_Functions;
2875 -------------------------------
2876 -- Check_Primitive_Function --
2877 -------------------------------
2879 function Check_Primitive_Function (Subp : Entity_Id) return Boolean is
2883 if Ekind (Subp) /= E_Function then
2887 if No (First_Formal (Subp)) then
2890 Ctrl := Etype (First_Formal (Subp));
2894 or else Ctrl = Class_Wide_Type (Ent)
2896 (Ekind (Ctrl) = E_Anonymous_Access_Type
2898 (Designated_Type (Ctrl) = Ent
2899 or else Designated_Type (Ctrl) = Class_Wide_Type (Ent)))
2908 end Check_Primitive_Function;
2910 ----------------------
2911 -- Duplicate_Clause --
2912 ----------------------
2914 function Duplicate_Clause return Boolean is
2918 -- Nothing to do if this attribute definition clause comes from
2919 -- an aspect specification, since we could not be duplicating an
2920 -- explicit clause, and we dealt with the case of duplicated aspects
2921 -- in Analyze_Aspect_Specifications.
2923 if From_Aspect_Specification (N) then
2927 -- Otherwise current clause may duplicate previous clause, or a
2928 -- previously given pragma or aspect specification for the same
2931 A := Get_Rep_Item (U_Ent, Chars (N), Check_Parents => False);
2934 Error_Msg_Name_1 := Chars (N);
2935 Error_Msg_Sloc := Sloc (A);
2937 Error_Msg_NE ("aspect% for & previously given#", N, U_Ent);
2942 end Duplicate_Clause;
2944 -- Start of processing for Analyze_Attribute_Definition_Clause
2947 -- The following code is a defense against recursion. Not clear that
2948 -- this can happen legitimately, but perhaps some error situations
2949 -- can cause it, and we did see this recursion during testing.
2951 if Analyzed (N) then
2954 Set_Analyzed (N, True);
2957 -- Ignore some selected attributes in CodePeer mode since they are not
2958 -- relevant in this context.
2960 if CodePeer_Mode then
2963 -- Ignore Component_Size in CodePeer mode, to avoid changing the
2964 -- internal representation of types by implicitly packing them.
2966 when Attribute_Component_Size =>
2967 Rewrite (N, Make_Null_Statement (Sloc (N)));
2975 -- Process Ignore_Rep_Clauses option
2977 if Ignore_Rep_Clauses then
2980 -- The following should be ignored. They do not affect legality
2981 -- and may be target dependent. The basic idea of -gnatI is to
2982 -- ignore any rep clauses that may be target dependent but do not
2983 -- affect legality (except possibly to be rejected because they
2984 -- are incompatible with the compilation target).
2986 when Attribute_Alignment |
2987 Attribute_Bit_Order |
2988 Attribute_Component_Size |
2989 Attribute_Machine_Radix |
2990 Attribute_Object_Size |
2992 Attribute_Stream_Size |
2993 Attribute_Value_Size =>
2994 Rewrite (N, Make_Null_Statement (Sloc (N)));
2997 -- Perhaps 'Small should not be ignored by Ignore_Rep_Clauses ???
2999 when Attribute_Small =>
3000 if Ignore_Rep_Clauses then
3001 Rewrite (N, Make_Null_Statement (Sloc (N)));
3005 -- The following should not be ignored, because in the first place
3006 -- they are reasonably portable, and should not cause problems in
3007 -- compiling code from another target, and also they do affect
3008 -- legality, e.g. failing to provide a stream attribute for a
3009 -- type may make a program illegal.
3011 when Attribute_External_Tag |
3015 Attribute_Simple_Storage_Pool |
3016 Attribute_Storage_Pool |
3017 Attribute_Storage_Size |
3021 -- Other cases are errors ("attribute& cannot be set with
3022 -- definition clause"), which will be caught below.
3030 Ent := Entity (Nam);
3032 if Rep_Item_Too_Early (Ent, N) then
3036 -- Rep clause applies to full view of incomplete type or private type if
3037 -- we have one (if not, this is a premature use of the type). However,
3038 -- certain semantic checks need to be done on the specified entity (i.e.
3039 -- the private view), so we save it in Ent.
3041 if Is_Private_Type (Ent)
3042 and then Is_Derived_Type (Ent)
3043 and then not Is_Tagged_Type (Ent)
3044 and then No (Full_View (Ent))
3046 -- If this is a private type whose completion is a derivation from
3047 -- another private type, there is no full view, and the attribute
3048 -- belongs to the type itself, not its underlying parent.
3052 elsif Ekind (Ent) = E_Incomplete_Type then
3054 -- The attribute applies to the full view, set the entity of the
3055 -- attribute definition accordingly.
3057 Ent := Underlying_Type (Ent);
3059 Set_Entity (Nam, Ent);
3062 U_Ent := Underlying_Type (Ent);
3065 -- Avoid cascaded error
3067 if Etype (Nam) = Any_Type then
3070 -- Must be declared in current scope or in case of an aspect
3071 -- specification, must be visible in current scope.
3073 elsif Scope (Ent) /= Current_Scope
3075 not (From_Aspect_Specification (N)
3076 and then Scope_Within_Or_Same (Current_Scope, Scope (Ent)))
3078 Error_Msg_N ("entity must be declared in this scope", Nam);
3081 -- Must not be a source renaming (we do have some cases where the
3082 -- expander generates a renaming, and those cases are OK, in such
3083 -- cases any attribute applies to the renamed object as well).
3085 elsif Is_Object (Ent)
3086 and then Present (Renamed_Object (Ent))
3088 -- Case of renamed object from source, this is an error
3090 if Comes_From_Source (Renamed_Object (Ent)) then
3091 Get_Name_String (Chars (N));
3092 Error_Msg_Strlen := Name_Len;
3093 Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
3095 ("~ clause not allowed for a renaming declaration "
3096 & "(RM 13.1(6))", Nam);
3099 -- For the case of a compiler generated renaming, the attribute
3100 -- definition clause applies to the renamed object created by the
3101 -- expander. The easiest general way to handle this is to create a
3102 -- copy of the attribute definition clause for this object.
3106 Make_Attribute_Definition_Clause (Loc,
3108 New_Occurrence_Of (Entity (Renamed_Object (Ent)), Loc),
3110 Expression => Duplicate_Subexpr (Expression (N))));
3113 -- If no underlying entity, use entity itself, applies to some
3114 -- previously detected error cases ???
3116 elsif No (U_Ent) then
3119 -- Cannot specify for a subtype (exception Object/Value_Size)
3121 elsif Is_Type (U_Ent)
3122 and then not Is_First_Subtype (U_Ent)
3123 and then Id /= Attribute_Object_Size
3124 and then Id /= Attribute_Value_Size
3125 and then not From_At_Mod (N)
3127 Error_Msg_N ("cannot specify attribute for subtype", Nam);
3131 Set_Entity (N, U_Ent);
3132 Check_Restriction_No_Use_Of_Attribute (N);
3134 -- Switch on particular attribute
3142 -- Address attribute definition clause
3144 when Attribute_Address => Address : begin
3146 -- A little error check, catch for X'Address use X'Address;
3148 if Nkind (Nam) = N_Identifier
3149 and then Nkind (Expr) = N_Attribute_Reference
3150 and then Attribute_Name (Expr) = Name_Address
3151 and then Nkind (Prefix (Expr)) = N_Identifier
3152 and then Chars (Nam) = Chars (Prefix (Expr))
3155 ("address for & is self-referencing", Prefix (Expr), Ent);
3159 -- Not that special case, carry on with analysis of expression
3161 Analyze_And_Resolve (Expr, RTE (RE_Address));
3163 -- Even when ignoring rep clauses we need to indicate that the
3164 -- entity has an address clause and thus it is legal to declare
3167 if Ignore_Rep_Clauses then
3168 if Ekind_In (U_Ent, E_Variable, E_Constant) then
3169 Record_Rep_Item (U_Ent, N);
3175 if Duplicate_Clause then
3178 -- Case of address clause for subprogram
3180 elsif Is_Subprogram (U_Ent) then
3181 if Has_Homonym (U_Ent) then
3183 ("address clause cannot be given " &
3184 "for overloaded subprogram",
3189 -- For subprograms, all address clauses are permitted, and we
3190 -- mark the subprogram as having a deferred freeze so that Gigi
3191 -- will not elaborate it too soon.
3193 -- Above needs more comments, what is too soon about???
3195 Set_Has_Delayed_Freeze (U_Ent);
3197 -- Case of address clause for entry
3199 elsif Ekind (U_Ent) = E_Entry then
3200 if Nkind (Parent (N)) = N_Task_Body then
3202 ("entry address must be specified in task spec", Nam);
3206 -- For entries, we require a constant address
3208 Check_Constant_Address_Clause (Expr, U_Ent);
3210 -- Special checks for task types
3212 if Is_Task_Type (Scope (U_Ent))
3213 and then Comes_From_Source (Scope (U_Ent))
3216 ("??entry address declared for entry in task type", N);
3218 ("\??only one task can be declared of this type", N);
3221 -- Entry address clauses are obsolescent
3223 Check_Restriction (No_Obsolescent_Features, N);
3225 if Warn_On_Obsolescent_Feature then
3227 ("?j?attaching interrupt to task entry is an " &
3228 "obsolescent feature (RM J.7.1)", N);
3230 ("\?j?use interrupt procedure instead", N);
3233 -- Case of an address clause for a controlled object which we
3234 -- consider to be erroneous.
3236 elsif Is_Controlled (Etype (U_Ent))
3237 or else Has_Controlled_Component (Etype (U_Ent))
3240 ("??controlled object& must not be overlaid", Nam, U_Ent);
3242 ("\??Program_Error will be raised at run time", Nam);
3243 Insert_Action (Declaration_Node (U_Ent),
3244 Make_Raise_Program_Error (Loc,
3245 Reason => PE_Overlaid_Controlled_Object));
3248 -- Case of address clause for a (non-controlled) object
3251 Ekind (U_Ent) = E_Variable
3253 Ekind (U_Ent) = E_Constant
3256 Expr : constant Node_Id := Expression (N);
3261 -- Exported variables cannot have an address clause, because
3262 -- this cancels the effect of the pragma Export.
3264 if Is_Exported (U_Ent) then
3266 ("cannot export object with address clause", Nam);
3270 Find_Overlaid_Entity (N, O_Ent, Off);
3272 -- Overlaying controlled objects is erroneous
3275 and then (Has_Controlled_Component (Etype (O_Ent))
3276 or else Is_Controlled (Etype (O_Ent)))
3279 ("??cannot overlay with controlled object", Expr);
3281 ("\??Program_Error will be raised at run time", Expr);
3282 Insert_Action (Declaration_Node (U_Ent),
3283 Make_Raise_Program_Error (Loc,
3284 Reason => PE_Overlaid_Controlled_Object));
3287 elsif Present (O_Ent)
3288 and then Ekind (U_Ent) = E_Constant
3289 and then not Is_Constant_Object (O_Ent)
3291 Error_Msg_N ("??constant overlays a variable", Expr);
3293 -- Imported variables can have an address clause, but then
3294 -- the import is pretty meaningless except to suppress
3295 -- initializations, so we do not need such variables to
3296 -- be statically allocated (and in fact it causes trouble
3297 -- if the address clause is a local value).
3299 elsif Is_Imported (U_Ent) then
3300 Set_Is_Statically_Allocated (U_Ent, False);
3303 -- We mark a possible modification of a variable with an
3304 -- address clause, since it is likely aliasing is occurring.
3306 Note_Possible_Modification (Nam, Sure => False);
3308 -- Here we are checking for explicit overlap of one variable
3309 -- by another, and if we find this then mark the overlapped
3310 -- variable as also being volatile to prevent unwanted
3311 -- optimizations. This is a significant pessimization so
3312 -- avoid it when there is an offset, i.e. when the object
3313 -- is composite; they cannot be optimized easily anyway.
3316 and then Is_Object (O_Ent)
3319 -- The following test is an expedient solution to what
3320 -- is really a problem in CodePeer. Suppressing the
3321 -- Set_Treat_As_Volatile call here prevents later
3322 -- generation (in some cases) of trees that CodePeer
3323 -- should, but currently does not, handle correctly.
3324 -- This test should probably be removed when CodePeer
3325 -- is improved, just because we want the tree CodePeer
3326 -- analyzes to match the tree for which we generate code
3327 -- as closely as is practical. ???
3329 and then not CodePeer_Mode
3331 -- ??? O_Ent might not be in current unit
3333 Set_Treat_As_Volatile (O_Ent);
3336 -- Legality checks on the address clause for initialized
3337 -- objects is deferred until the freeze point, because
3338 -- a subsequent pragma might indicate that the object
3339 -- is imported and thus not initialized. Also, the address
3340 -- clause might involve entities that have yet to be
3343 Set_Has_Delayed_Freeze (U_Ent);
3345 -- If an initialization call has been generated for this
3346 -- object, it needs to be deferred to after the freeze node
3347 -- we have just now added, otherwise GIGI will see a
3348 -- reference to the variable (as actual to the IP call)
3349 -- before its definition.
3352 Init_Call : constant Node_Id :=
3353 Remove_Init_Call (U_Ent, N);
3356 if Present (Init_Call) then
3358 -- If the init call is an expression with actions with
3359 -- null expression, just extract the actions.
3361 if Nkind (Init_Call) = N_Expression_With_Actions
3363 Nkind (Expression (Init_Call)) = N_Null_Statement
3365 Append_Freeze_Actions (U_Ent, Actions (Init_Call));
3367 -- General case: move Init_Call to freeze actions
3370 Append_Freeze_Action (U_Ent, Init_Call);
3375 if Is_Exported (U_Ent) then
3377 ("& cannot be exported if an address clause is given",
3380 ("\define and export a variable "
3381 & "that holds its address instead", Nam);
3384 -- Entity has delayed freeze, so we will generate an
3385 -- alignment check at the freeze point unless suppressed.
3387 if not Range_Checks_Suppressed (U_Ent)
3388 and then not Alignment_Checks_Suppressed (U_Ent)
3390 Set_Check_Address_Alignment (N);
3393 -- Kill the size check code, since we are not allocating
3394 -- the variable, it is somewhere else.
3396 Kill_Size_Check_Code (U_Ent);
3398 -- If the address clause is of the form:
3400 -- for Y'Address use X'Address
3404 -- Const : constant Address := X'Address;
3406 -- for Y'Address use Const;
3408 -- then we make an entry in the table for checking the size
3409 -- and alignment of the overlaying variable. We defer this
3410 -- check till after code generation to take full advantage
3411 -- of the annotation done by the back end. This entry is
3412 -- only made if the address clause comes from source.
3414 -- If the entity has a generic type, the check will be
3415 -- performed in the instance if the actual type justifies
3416 -- it, and we do not insert the clause in the table to
3417 -- prevent spurious warnings.
3419 if Address_Clause_Overlay_Warnings
3420 and then Comes_From_Source (N)
3421 and then Present (O_Ent)
3422 and then Is_Object (O_Ent)
3424 if not Is_Generic_Type (Etype (U_Ent)) then
3425 Address_Clause_Checks.Append ((N, U_Ent, O_Ent, Off));
3428 -- If variable overlays a constant view, and we are
3429 -- warning on overlays, then mark the variable as
3430 -- overlaying a constant (we will give warnings later
3431 -- if this variable is assigned).
3433 if Is_Constant_Object (O_Ent)
3434 and then Ekind (U_Ent) = E_Variable
3436 Set_Overlays_Constant (U_Ent);
3441 -- Not a valid entity for an address clause
3444 Error_Msg_N ("address cannot be given for &", Nam);
3452 -- Alignment attribute definition clause
3454 when Attribute_Alignment => Alignment : declare
3455 Align : constant Uint := Get_Alignment_Value (Expr);
3456 Max_Align : constant Uint := UI_From_Int (Maximum_Alignment);
3461 if not Is_Type (U_Ent)
3462 and then Ekind (U_Ent) /= E_Variable
3463 and then Ekind (U_Ent) /= E_Constant
3465 Error_Msg_N ("alignment cannot be given for &", Nam);
3467 elsif Duplicate_Clause then
3470 elsif Align /= No_Uint then
3471 Set_Has_Alignment_Clause (U_Ent);
3473 -- Tagged type case, check for attempt to set alignment to a
3474 -- value greater than Max_Align, and reset if so.
3476 if Is_Tagged_Type (U_Ent) and then Align > Max_Align then
3478 ("alignment for & set to Maximum_Aligment??", Nam);
3479 Set_Alignment (U_Ent, Max_Align);
3484 Set_Alignment (U_Ent, Align);
3487 -- For an array type, U_Ent is the first subtype. In that case,
3488 -- also set the alignment of the anonymous base type so that
3489 -- other subtypes (such as the itypes for aggregates of the
3490 -- type) also receive the expected alignment.
3492 if Is_Array_Type (U_Ent) then
3493 Set_Alignment (Base_Type (U_Ent), Align);
3502 -- Bit_Order attribute definition clause
3504 when Attribute_Bit_Order => Bit_Order : declare
3506 if not Is_Record_Type (U_Ent) then
3508 ("Bit_Order can only be defined for record type", Nam);
3510 elsif Duplicate_Clause then
3514 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
3516 if Etype (Expr) = Any_Type then
3519 elsif not Is_Static_Expression (Expr) then
3520 Flag_Non_Static_Expr
3521 ("Bit_Order requires static expression!", Expr);
3524 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
3525 Set_Reverse_Bit_Order (U_Ent, True);
3531 --------------------
3532 -- Component_Size --
3533 --------------------
3535 -- Component_Size attribute definition clause
3537 when Attribute_Component_Size => Component_Size_Case : declare
3538 Csize : constant Uint := Static_Integer (Expr);
3542 New_Ctyp : Entity_Id;
3546 if not Is_Array_Type (U_Ent) then
3547 Error_Msg_N ("component size requires array type", Nam);
3551 Btype := Base_Type (U_Ent);
3552 Ctyp := Component_Type (Btype);
3554 if Duplicate_Clause then
3557 elsif Rep_Item_Too_Early (Btype, N) then
3560 elsif Csize /= No_Uint then
3561 Check_Size (Expr, Ctyp, Csize, Biased);
3563 -- For the biased case, build a declaration for a subtype that
3564 -- will be used to represent the biased subtype that reflects
3565 -- the biased representation of components. We need the subtype
3566 -- to get proper conversions on referencing elements of the
3567 -- array. Note: component size clauses are ignored in VM mode.
3569 if VM_Target = No_VM then
3572 Make_Defining_Identifier (Loc,
3574 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
3577 Make_Subtype_Declaration (Loc,
3578 Defining_Identifier => New_Ctyp,
3579 Subtype_Indication =>
3580 New_Occurrence_Of (Component_Type (Btype), Loc));
3582 Set_Parent (Decl, N);
3583 Analyze (Decl, Suppress => All_Checks);
3585 Set_Has_Delayed_Freeze (New_Ctyp, False);
3586 Set_Esize (New_Ctyp, Csize);
3587 Set_RM_Size (New_Ctyp, Csize);
3588 Init_Alignment (New_Ctyp);
3589 Set_Is_Itype (New_Ctyp, True);
3590 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
3592 Set_Component_Type (Btype, New_Ctyp);
3593 Set_Biased (New_Ctyp, N, "component size clause");
3596 Set_Component_Size (Btype, Csize);
3598 -- For VM case, we ignore component size clauses
3601 -- Give a warning unless we are in GNAT mode, in which case
3602 -- the warning is suppressed since it is not useful.
3604 if not GNAT_Mode then
3606 ("component size ignored in this configuration??", N);
3610 -- Deal with warning on overridden size
3612 if Warn_On_Overridden_Size
3613 and then Has_Size_Clause (Ctyp)
3614 and then RM_Size (Ctyp) /= Csize
3617 ("component size overrides size clause for&?S?", N, Ctyp);
3620 Set_Has_Component_Size_Clause (Btype, True);
3621 Set_Has_Non_Standard_Rep (Btype, True);
3623 end Component_Size_Case;
3625 -----------------------
3626 -- Constant_Indexing --
3627 -----------------------
3629 when Attribute_Constant_Indexing =>
3630 Check_Indexing_Functions;
3636 when Attribute_CPU => CPU :
3638 -- CPU attribute definition clause not allowed except from aspect
3641 if From_Aspect_Specification (N) then
3642 if not Is_Task_Type (U_Ent) then
3643 Error_Msg_N ("CPU can only be defined for task", Nam);
3645 elsif Duplicate_Clause then
3649 -- The expression must be analyzed in the special manner
3650 -- described in "Handling of Default and Per-Object
3651 -- Expressions" in sem.ads.
3653 -- The visibility to the discriminants must be restored
3655 Push_Scope_And_Install_Discriminants (U_Ent);
3656 Preanalyze_Spec_Expression (Expr, RTE (RE_CPU_Range));
3657 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
3659 if not Is_Static_Expression (Expr) then
3660 Check_Restriction (Static_Priorities, Expr);
3666 ("attribute& cannot be set with definition clause", N);
3670 ----------------------
3671 -- Default_Iterator --
3672 ----------------------
3674 when Attribute_Default_Iterator => Default_Iterator : declare
3678 if not Is_Tagged_Type (U_Ent) then
3680 ("aspect Default_Iterator applies to tagged type", Nam);
3683 Check_Iterator_Functions;
3687 if not Is_Entity_Name (Expr)
3688 or else Ekind (Entity (Expr)) /= E_Function
3690 Error_Msg_N ("aspect Iterator must be a function", Expr);
3692 Func := Entity (Expr);
3695 if No (First_Formal (Func))
3696 or else Etype (First_Formal (Func)) /= U_Ent
3699 ("Default Iterator must be a primitive of&", Func, U_Ent);
3701 end Default_Iterator;
3703 ------------------------
3704 -- Dispatching_Domain --
3705 ------------------------
3707 when Attribute_Dispatching_Domain => Dispatching_Domain :
3709 -- Dispatching_Domain attribute definition clause not allowed
3710 -- except from aspect specification.
3712 if From_Aspect_Specification (N) then
3713 if not Is_Task_Type (U_Ent) then
3714 Error_Msg_N ("Dispatching_Domain can only be defined" &
3718 elsif Duplicate_Clause then
3722 -- The expression must be analyzed in the special manner
3723 -- described in "Handling of Default and Per-Object
3724 -- Expressions" in sem.ads.
3726 -- The visibility to the discriminants must be restored
3728 Push_Scope_And_Install_Discriminants (U_Ent);
3730 Preanalyze_Spec_Expression
3731 (Expr, RTE (RE_Dispatching_Domain));
3733 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
3738 ("attribute& cannot be set with definition clause", N);
3740 end Dispatching_Domain;
3746 when Attribute_External_Tag => External_Tag :
3748 if not Is_Tagged_Type (U_Ent) then
3749 Error_Msg_N ("should be a tagged type", Nam);
3752 if Duplicate_Clause then
3756 Analyze_And_Resolve (Expr, Standard_String);
3758 if not Is_Static_Expression (Expr) then
3759 Flag_Non_Static_Expr
3760 ("static string required for tag name!", Nam);
3763 if VM_Target = No_VM then
3764 Set_Has_External_Tag_Rep_Clause (U_Ent);
3766 Error_Msg_Name_1 := Attr;
3768 ("% attribute unsupported in this configuration", Nam);
3771 if not Is_Library_Level_Entity (U_Ent) then
3773 ("??non-unique external tag supplied for &", N, U_Ent);
3775 ("\??same external tag applies to all "
3776 & "subprogram calls", N);
3778 ("\??corresponding internal tag cannot be obtained", N);
3783 --------------------------
3784 -- Implicit_Dereference --
3785 --------------------------
3787 when Attribute_Implicit_Dereference =>
3789 -- Legality checks already performed at the point of the type
3790 -- declaration, aspect is not delayed.
3798 when Attribute_Input =>
3799 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
3800 Set_Has_Specified_Stream_Input (Ent);
3802 ------------------------
3803 -- Interrupt_Priority --
3804 ------------------------
3806 when Attribute_Interrupt_Priority => Interrupt_Priority :
3808 -- Interrupt_Priority attribute definition clause not allowed
3809 -- except from aspect specification.
3811 if From_Aspect_Specification (N) then
3812 if not (Is_Protected_Type (U_Ent)
3813 or else Is_Task_Type (U_Ent))
3816 ("Interrupt_Priority can only be defined for task" &
3817 "and protected object",
3820 elsif Duplicate_Clause then
3824 -- The expression must be analyzed in the special manner
3825 -- described in "Handling of Default and Per-Object
3826 -- Expressions" in sem.ads.
3828 -- The visibility to the discriminants must be restored
3830 Push_Scope_And_Install_Discriminants (U_Ent);
3832 Preanalyze_Spec_Expression
3833 (Expr, RTE (RE_Interrupt_Priority));
3835 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
3840 ("attribute& cannot be set with definition clause", N);
3842 end Interrupt_Priority;
3844 ----------------------
3845 -- Iterator_Element --
3846 ----------------------
3848 when Attribute_Iterator_Element =>
3851 if not Is_Entity_Name (Expr)
3852 or else not Is_Type (Entity (Expr))
3854 Error_Msg_N ("aspect Iterator_Element must be a type", Expr);
3861 -- Machine radix attribute definition clause
3863 when Attribute_Machine_Radix => Machine_Radix : declare
3864 Radix : constant Uint := Static_Integer (Expr);
3867 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
3868 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
3870 elsif Duplicate_Clause then
3873 elsif Radix /= No_Uint then
3874 Set_Has_Machine_Radix_Clause (U_Ent);
3875 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
3879 elsif Radix = 10 then
3880 Set_Machine_Radix_10 (U_Ent);
3882 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
3891 -- Object_Size attribute definition clause
3893 when Attribute_Object_Size => Object_Size : declare
3894 Size : constant Uint := Static_Integer (Expr);
3897 pragma Warnings (Off, Biased);
3900 if not Is_Type (U_Ent) then
3901 Error_Msg_N ("Object_Size cannot be given for &", Nam);
3903 elsif Duplicate_Clause then
3907 Check_Size (Expr, U_Ent, Size, Biased);
3915 UI_Mod (Size, 64) /= 0
3918 ("Object_Size must be 8, 16, 32, or multiple of 64",
3922 Set_Esize (U_Ent, Size);
3923 Set_Has_Object_Size_Clause (U_Ent);
3924 Alignment_Check_For_Size_Change (U_Ent, Size);
3932 when Attribute_Output =>
3933 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
3934 Set_Has_Specified_Stream_Output (Ent);
3940 when Attribute_Priority => Priority :
3942 -- Priority attribute definition clause not allowed except from
3943 -- aspect specification.
3945 if From_Aspect_Specification (N) then
3946 if not (Is_Protected_Type (U_Ent)
3947 or else Is_Task_Type (U_Ent)
3948 or else Ekind (U_Ent) = E_Procedure)
3951 ("Priority can only be defined for task and protected " &
3955 elsif Duplicate_Clause then
3959 -- The expression must be analyzed in the special manner
3960 -- described in "Handling of Default and Per-Object
3961 -- Expressions" in sem.ads.
3963 -- The visibility to the discriminants must be restored
3965 Push_Scope_And_Install_Discriminants (U_Ent);
3966 Preanalyze_Spec_Expression (Expr, Standard_Integer);
3967 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
3969 if not Is_Static_Expression (Expr) then
3970 Check_Restriction (Static_Priorities, Expr);
3976 ("attribute& cannot be set with definition clause", N);
3984 when Attribute_Read =>
3985 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
3986 Set_Has_Specified_Stream_Read (Ent);
3988 --------------------------
3989 -- Scalar_Storage_Order --
3990 --------------------------
3992 -- Scalar_Storage_Order attribute definition clause
3994 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order : declare
3996 if not (Is_Record_Type (U_Ent) or else Is_Array_Type (U_Ent)) then
3998 ("Scalar_Storage_Order can only be defined for "
3999 & "record or array type", Nam);
4001 elsif Duplicate_Clause then
4005 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
4007 if Etype (Expr) = Any_Type then
4010 elsif not Is_Static_Expression (Expr) then
4011 Flag_Non_Static_Expr
4012 ("Scalar_Storage_Order requires static expression!", Expr);
4014 elsif (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
4016 -- Here for the case of a non-default (i.e. non-confirming)
4017 -- Scalar_Storage_Order attribute definition.
4019 if Support_Nondefault_SSO_On_Target then
4020 Set_Reverse_Storage_Order (Base_Type (U_Ent), True);
4023 ("non-default Scalar_Storage_Order "
4024 & "not supported on target", Expr);
4028 end Scalar_Storage_Order;
4034 -- Size attribute definition clause
4036 when Attribute_Size => Size : declare
4037 Size : constant Uint := Static_Integer (Expr);
4044 if Duplicate_Clause then
4047 elsif not Is_Type (U_Ent)
4048 and then Ekind (U_Ent) /= E_Variable
4049 and then Ekind (U_Ent) /= E_Constant
4051 Error_Msg_N ("size cannot be given for &", Nam);
4053 elsif Is_Array_Type (U_Ent)
4054 and then not Is_Constrained (U_Ent)
4057 ("size cannot be given for unconstrained array", Nam);
4059 elsif Size /= No_Uint then
4060 if VM_Target /= No_VM and then not GNAT_Mode then
4062 -- Size clause is not handled properly on VM targets.
4063 -- Display a warning unless we are in GNAT mode, in which
4064 -- case this is useless.
4067 ("size clauses are ignored in this configuration??", N);
4070 if Is_Type (U_Ent) then
4073 Etyp := Etype (U_Ent);
4076 -- Check size, note that Gigi is in charge of checking that the
4077 -- size of an array or record type is OK. Also we do not check
4078 -- the size in the ordinary fixed-point case, since it is too
4079 -- early to do so (there may be subsequent small clause that
4080 -- affects the size). We can check the size if a small clause
4081 -- has already been given.
4083 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
4084 or else Has_Small_Clause (U_Ent)
4086 Check_Size (Expr, Etyp, Size, Biased);
4087 Set_Biased (U_Ent, N, "size clause", Biased);
4090 -- For types set RM_Size and Esize if possible
4092 if Is_Type (U_Ent) then
4093 Set_RM_Size (U_Ent, Size);
4095 -- For elementary types, increase Object_Size to power of 2,
4096 -- but not less than a storage unit in any case (normally
4097 -- this means it will be byte addressable).
4099 -- For all other types, nothing else to do, we leave Esize
4100 -- (object size) unset, the back end will set it from the
4101 -- size and alignment in an appropriate manner.
4103 -- In both cases, we check whether the alignment must be
4104 -- reset in the wake of the size change.
4106 if Is_Elementary_Type (U_Ent) then
4107 if Size <= System_Storage_Unit then
4108 Init_Esize (U_Ent, System_Storage_Unit);
4109 elsif Size <= 16 then
4110 Init_Esize (U_Ent, 16);
4111 elsif Size <= 32 then
4112 Init_Esize (U_Ent, 32);
4114 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
4117 Alignment_Check_For_Size_Change (U_Ent, Esize (U_Ent));
4119 Alignment_Check_For_Size_Change (U_Ent, Size);
4122 -- For objects, set Esize only
4125 if Is_Elementary_Type (Etyp) then
4126 if Size /= System_Storage_Unit
4128 Size /= System_Storage_Unit * 2
4130 Size /= System_Storage_Unit * 4
4132 Size /= System_Storage_Unit * 8
4134 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
4135 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
4137 ("size for primitive object must be a power of 2"
4138 & " in the range ^-^", N);
4142 Set_Esize (U_Ent, Size);
4145 Set_Has_Size_Clause (U_Ent);
4153 -- Small attribute definition clause
4155 when Attribute_Small => Small : declare
4156 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
4160 Analyze_And_Resolve (Expr, Any_Real);
4162 if Etype (Expr) = Any_Type then
4165 elsif not Is_Static_Expression (Expr) then
4166 Flag_Non_Static_Expr
4167 ("small requires static expression!", Expr);
4171 Small := Expr_Value_R (Expr);
4173 if Small <= Ureal_0 then
4174 Error_Msg_N ("small value must be greater than zero", Expr);
4180 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
4182 ("small requires an ordinary fixed point type", Nam);
4184 elsif Has_Small_Clause (U_Ent) then
4185 Error_Msg_N ("small already given for &", Nam);
4187 elsif Small > Delta_Value (U_Ent) then
4189 ("small value must not be greater than delta value", Nam);
4192 Set_Small_Value (U_Ent, Small);
4193 Set_Small_Value (Implicit_Base, Small);
4194 Set_Has_Small_Clause (U_Ent);
4195 Set_Has_Small_Clause (Implicit_Base);
4196 Set_Has_Non_Standard_Rep (Implicit_Base);
4204 -- Storage_Pool attribute definition clause
4206 when Attribute_Storage_Pool | Attribute_Simple_Storage_Pool => declare
4211 if Ekind (U_Ent) = E_Access_Subprogram_Type then
4213 ("storage pool cannot be given for access-to-subprogram type",
4218 Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
4221 ("storage pool can only be given for access types", Nam);
4224 elsif Is_Derived_Type (U_Ent) then
4226 ("storage pool cannot be given for a derived access type",
4229 elsif Duplicate_Clause then
4232 elsif Present (Associated_Storage_Pool (U_Ent)) then
4233 Error_Msg_N ("storage pool already given for &", Nam);
4237 if Id = Attribute_Storage_Pool then
4239 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
4241 -- In the Simple_Storage_Pool case, we allow a variable of any
4242 -- simple storage pool type, so we Resolve without imposing an
4246 Analyze_And_Resolve (Expr);
4248 if not Present (Get_Rep_Pragma
4249 (Etype (Expr), Name_Simple_Storage_Pool_Type))
4252 ("expression must be of a simple storage pool type", Expr);
4256 if not Denotes_Variable (Expr) then
4257 Error_Msg_N ("storage pool must be a variable", Expr);
4261 if Nkind (Expr) = N_Type_Conversion then
4262 T := Etype (Expression (Expr));
4267 -- The Stack_Bounded_Pool is used internally for implementing
4268 -- access types with a Storage_Size. Since it only work properly
4269 -- when used on one specific type, we need to check that it is not
4270 -- hijacked improperly:
4272 -- type T is access Integer;
4273 -- for T'Storage_Size use n;
4274 -- type Q is access Float;
4275 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
4277 if RTE_Available (RE_Stack_Bounded_Pool)
4278 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
4280 Error_Msg_N ("non-shareable internal Pool", Expr);
4284 -- If the argument is a name that is not an entity name, then
4285 -- we construct a renaming operation to define an entity of
4286 -- type storage pool.
4288 if not Is_Entity_Name (Expr)
4289 and then Is_Object_Reference (Expr)
4291 Pool := Make_Temporary (Loc, 'P', Expr);
4294 Rnode : constant Node_Id :=
4295 Make_Object_Renaming_Declaration (Loc,
4296 Defining_Identifier => Pool,
4298 New_Occurrence_Of (Etype (Expr), Loc),
4302 Insert_Before (N, Rnode);
4304 Set_Associated_Storage_Pool (U_Ent, Pool);
4307 elsif Is_Entity_Name (Expr) then
4308 Pool := Entity (Expr);
4310 -- If pool is a renamed object, get original one. This can
4311 -- happen with an explicit renaming, and within instances.
4313 while Present (Renamed_Object (Pool))
4314 and then Is_Entity_Name (Renamed_Object (Pool))
4316 Pool := Entity (Renamed_Object (Pool));
4319 if Present (Renamed_Object (Pool))
4320 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
4321 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
4323 Pool := Entity (Expression (Renamed_Object (Pool)));
4326 Set_Associated_Storage_Pool (U_Ent, Pool);
4328 elsif Nkind (Expr) = N_Type_Conversion
4329 and then Is_Entity_Name (Expression (Expr))
4330 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
4332 Pool := Entity (Expression (Expr));
4333 Set_Associated_Storage_Pool (U_Ent, Pool);
4336 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
4345 -- Storage_Size attribute definition clause
4347 when Attribute_Storage_Size => Storage_Size : declare
4348 Btype : constant Entity_Id := Base_Type (U_Ent);
4351 if Is_Task_Type (U_Ent) then
4353 -- Check obsolescent (but never obsolescent if from aspect!)
4355 if not From_Aspect_Specification (N) then
4356 Check_Restriction (No_Obsolescent_Features, N);
4358 if Warn_On_Obsolescent_Feature then
4360 ("?j?storage size clause for task is an " &
4361 "obsolescent feature (RM J.9)", N);
4362 Error_Msg_N ("\?j?use Storage_Size pragma instead", N);
4369 if not Is_Access_Type (U_Ent)
4370 and then Ekind (U_Ent) /= E_Task_Type
4372 Error_Msg_N ("storage size cannot be given for &", Nam);
4374 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
4376 ("storage size cannot be given for a derived access type",
4379 elsif Duplicate_Clause then
4383 Analyze_And_Resolve (Expr, Any_Integer);
4385 if Is_Access_Type (U_Ent) then
4386 if Present (Associated_Storage_Pool (U_Ent)) then
4387 Error_Msg_N ("storage pool already given for &", Nam);
4391 if Is_OK_Static_Expression (Expr)
4392 and then Expr_Value (Expr) = 0
4394 Set_No_Pool_Assigned (Btype);
4398 Set_Has_Storage_Size_Clause (Btype);
4406 when Attribute_Stream_Size => Stream_Size : declare
4407 Size : constant Uint := Static_Integer (Expr);
4410 if Ada_Version <= Ada_95 then
4411 Check_Restriction (No_Implementation_Attributes, N);
4414 if Duplicate_Clause then
4417 elsif Is_Elementary_Type (U_Ent) then
4418 if Size /= System_Storage_Unit
4420 Size /= System_Storage_Unit * 2
4422 Size /= System_Storage_Unit * 4
4424 Size /= System_Storage_Unit * 8
4426 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
4428 ("stream size for elementary type must be a"
4429 & " power of 2 and at least ^", N);
4431 elsif RM_Size (U_Ent) > Size then
4432 Error_Msg_Uint_1 := RM_Size (U_Ent);
4434 ("stream size for elementary type must be a"
4435 & " power of 2 and at least ^", N);
4438 Set_Has_Stream_Size_Clause (U_Ent);
4441 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
4449 -- Value_Size attribute definition clause
4451 when Attribute_Value_Size => Value_Size : declare
4452 Size : constant Uint := Static_Integer (Expr);
4456 if not Is_Type (U_Ent) then
4457 Error_Msg_N ("Value_Size cannot be given for &", Nam);
4459 elsif Duplicate_Clause then
4462 elsif Is_Array_Type (U_Ent)
4463 and then not Is_Constrained (U_Ent)
4466 ("Value_Size cannot be given for unconstrained array", Nam);
4469 if Is_Elementary_Type (U_Ent) then
4470 Check_Size (Expr, U_Ent, Size, Biased);
4471 Set_Biased (U_Ent, N, "value size clause", Biased);
4474 Set_RM_Size (U_Ent, Size);
4478 -----------------------
4479 -- Variable_Indexing --
4480 -----------------------
4482 when Attribute_Variable_Indexing =>
4483 Check_Indexing_Functions;
4489 when Attribute_Write =>
4490 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
4491 Set_Has_Specified_Stream_Write (Ent);
4493 -- All other attributes cannot be set
4497 ("attribute& cannot be set with definition clause", N);
4500 -- The test for the type being frozen must be performed after any
4501 -- expression the clause has been analyzed since the expression itself
4502 -- might cause freezing that makes the clause illegal.
4504 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
4507 end Analyze_Attribute_Definition_Clause;
4509 ----------------------------
4510 -- Analyze_Code_Statement --
4511 ----------------------------
4513 procedure Analyze_Code_Statement (N : Node_Id) is
4514 HSS : constant Node_Id := Parent (N);
4515 SBody : constant Node_Id := Parent (HSS);
4516 Subp : constant Entity_Id := Current_Scope;
4523 -- Analyze and check we get right type, note that this implements the
4524 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
4525 -- is the only way that Asm_Insn could possibly be visible.
4527 Analyze_And_Resolve (Expression (N));
4529 if Etype (Expression (N)) = Any_Type then
4531 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
4532 Error_Msg_N ("incorrect type for code statement", N);
4536 Check_Code_Statement (N);
4538 -- Make sure we appear in the handled statement sequence of a
4539 -- subprogram (RM 13.8(3)).
4541 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
4542 or else Nkind (SBody) /= N_Subprogram_Body
4545 ("code statement can only appear in body of subprogram", N);
4549 -- Do remaining checks (RM 13.8(3)) if not already done
4551 if not Is_Machine_Code_Subprogram (Subp) then
4552 Set_Is_Machine_Code_Subprogram (Subp);
4554 -- No exception handlers allowed
4556 if Present (Exception_Handlers (HSS)) then
4558 ("exception handlers not permitted in machine code subprogram",
4559 First (Exception_Handlers (HSS)));
4562 -- No declarations other than use clauses and pragmas (we allow
4563 -- certain internally generated declarations as well).
4565 Decl := First (Declarations (SBody));
4566 while Present (Decl) loop
4567 DeclO := Original_Node (Decl);
4568 if Comes_From_Source (DeclO)
4569 and not Nkind_In (DeclO, N_Pragma,
4570 N_Use_Package_Clause,
4572 N_Implicit_Label_Declaration)
4575 ("this declaration not allowed in machine code subprogram",
4582 -- No statements other than code statements, pragmas, and labels.
4583 -- Again we allow certain internally generated statements.
4585 -- In Ada 2012, qualified expressions are names, and the code
4586 -- statement is initially parsed as a procedure call.
4588 Stmt := First (Statements (HSS));
4589 while Present (Stmt) loop
4590 StmtO := Original_Node (Stmt);
4592 -- A procedure call transformed into a code statement is OK.
4594 if Ada_Version >= Ada_2012
4595 and then Nkind (StmtO) = N_Procedure_Call_Statement
4596 and then Nkind (Name (StmtO)) = N_Qualified_Expression
4600 elsif Comes_From_Source (StmtO)
4601 and then not Nkind_In (StmtO, N_Pragma,
4606 ("this statement is not allowed in machine code subprogram",
4613 end Analyze_Code_Statement;
4615 -----------------------------------------------
4616 -- Analyze_Enumeration_Representation_Clause --
4617 -----------------------------------------------
4619 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
4620 Ident : constant Node_Id := Identifier (N);
4621 Aggr : constant Node_Id := Array_Aggregate (N);
4622 Enumtype : Entity_Id;
4629 Err : Boolean := False;
4630 -- Set True to avoid cascade errors and crashes on incorrect source code
4632 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
4633 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
4634 -- Allowed range of universal integer (= allowed range of enum lit vals)
4638 -- Minimum and maximum values of entries
4641 -- Pointer to node for literal providing max value
4644 if Ignore_Rep_Clauses then
4648 -- Ignore enumeration rep clauses by default in CodePeer mode,
4649 -- unless -gnatd.I is specified, as a work around for potential false
4650 -- positive messages.
4652 if CodePeer_Mode and not Debug_Flag_Dot_II then
4656 -- First some basic error checks
4659 Enumtype := Entity (Ident);
4661 if Enumtype = Any_Type
4662 or else Rep_Item_Too_Early (Enumtype, N)
4666 Enumtype := Underlying_Type (Enumtype);
4669 if not Is_Enumeration_Type (Enumtype) then
4671 ("enumeration type required, found}",
4672 Ident, First_Subtype (Enumtype));
4676 -- Ignore rep clause on generic actual type. This will already have
4677 -- been flagged on the template as an error, and this is the safest
4678 -- way to ensure we don't get a junk cascaded message in the instance.
4680 if Is_Generic_Actual_Type (Enumtype) then
4683 -- Type must be in current scope
4685 elsif Scope (Enumtype) /= Current_Scope then
4686 Error_Msg_N ("type must be declared in this scope", Ident);
4689 -- Type must be a first subtype
4691 elsif not Is_First_Subtype (Enumtype) then
4692 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
4695 -- Ignore duplicate rep clause
4697 elsif Has_Enumeration_Rep_Clause (Enumtype) then
4698 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
4701 -- Don't allow rep clause for standard [wide_[wide_]]character
4703 elsif Is_Standard_Character_Type (Enumtype) then
4704 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
4707 -- Check that the expression is a proper aggregate (no parentheses)
4709 elsif Paren_Count (Aggr) /= 0 then
4711 ("extra parentheses surrounding aggregate not allowed",
4715 -- All tests passed, so set rep clause in place
4718 Set_Has_Enumeration_Rep_Clause (Enumtype);
4719 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
4722 -- Now we process the aggregate. Note that we don't use the normal
4723 -- aggregate code for this purpose, because we don't want any of the
4724 -- normal expansion activities, and a number of special semantic
4725 -- rules apply (including the component type being any integer type)
4727 Elit := First_Literal (Enumtype);
4729 -- First the positional entries if any
4731 if Present (Expressions (Aggr)) then
4732 Expr := First (Expressions (Aggr));
4733 while Present (Expr) loop
4735 Error_Msg_N ("too many entries in aggregate", Expr);
4739 Val := Static_Integer (Expr);
4741 -- Err signals that we found some incorrect entries processing
4742 -- the list. The final checks for completeness and ordering are
4743 -- skipped in this case.
4745 if Val = No_Uint then
4747 elsif Val < Lo or else Hi < Val then
4748 Error_Msg_N ("value outside permitted range", Expr);
4752 Set_Enumeration_Rep (Elit, Val);
4753 Set_Enumeration_Rep_Expr (Elit, Expr);
4759 -- Now process the named entries if present
4761 if Present (Component_Associations (Aggr)) then
4762 Assoc := First (Component_Associations (Aggr));
4763 while Present (Assoc) loop
4764 Choice := First (Choices (Assoc));
4766 if Present (Next (Choice)) then
4768 ("multiple choice not allowed here", Next (Choice));
4772 if Nkind (Choice) = N_Others_Choice then
4773 Error_Msg_N ("others choice not allowed here", Choice);
4776 elsif Nkind (Choice) = N_Range then
4778 -- ??? should allow zero/one element range here
4780 Error_Msg_N ("range not allowed here", Choice);
4784 Analyze_And_Resolve (Choice, Enumtype);
4786 if Error_Posted (Choice) then
4791 if Is_Entity_Name (Choice)
4792 and then Is_Type (Entity (Choice))
4794 Error_Msg_N ("subtype name not allowed here", Choice);
4797 -- ??? should allow static subtype with zero/one entry
4799 elsif Etype (Choice) = Base_Type (Enumtype) then
4800 if not Is_Static_Expression (Choice) then
4801 Flag_Non_Static_Expr
4802 ("non-static expression used for choice!", Choice);
4806 Elit := Expr_Value_E (Choice);
4808 if Present (Enumeration_Rep_Expr (Elit)) then
4810 Sloc (Enumeration_Rep_Expr (Elit));
4812 ("representation for& previously given#",
4817 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
4819 Expr := Expression (Assoc);
4820 Val := Static_Integer (Expr);
4822 if Val = No_Uint then
4825 elsif Val < Lo or else Hi < Val then
4826 Error_Msg_N ("value outside permitted range", Expr);
4830 Set_Enumeration_Rep (Elit, Val);
4840 -- Aggregate is fully processed. Now we check that a full set of
4841 -- representations was given, and that they are in range and in order.
4842 -- These checks are only done if no other errors occurred.
4848 Elit := First_Literal (Enumtype);
4849 while Present (Elit) loop
4850 if No (Enumeration_Rep_Expr (Elit)) then
4851 Error_Msg_NE ("missing representation for&!", N, Elit);
4854 Val := Enumeration_Rep (Elit);
4856 if Min = No_Uint then
4860 if Val /= No_Uint then
4861 if Max /= No_Uint and then Val <= Max then
4863 ("enumeration value for& not ordered!",
4864 Enumeration_Rep_Expr (Elit), Elit);
4867 Max_Node := Enumeration_Rep_Expr (Elit);
4871 -- If there is at least one literal whose representation is not
4872 -- equal to the Pos value, then note that this enumeration type
4873 -- has a non-standard representation.
4875 if Val /= Enumeration_Pos (Elit) then
4876 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
4883 -- Now set proper size information
4886 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
4889 if Has_Size_Clause (Enumtype) then
4891 -- All OK, if size is OK now
4893 if RM_Size (Enumtype) >= Minsize then
4897 -- Try if we can get by with biasing
4900 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
4902 -- Error message if even biasing does not work
4904 if RM_Size (Enumtype) < Minsize then
4905 Error_Msg_Uint_1 := RM_Size (Enumtype);
4906 Error_Msg_Uint_2 := Max;
4908 ("previously given size (^) is too small "
4909 & "for this value (^)", Max_Node);
4911 -- If biasing worked, indicate that we now have biased rep
4915 (Enumtype, Size_Clause (Enumtype), "size clause");
4920 Set_RM_Size (Enumtype, Minsize);
4921 Set_Enum_Esize (Enumtype);
4924 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
4925 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
4926 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
4930 -- We repeat the too late test in case it froze itself!
4932 if Rep_Item_Too_Late (Enumtype, N) then
4935 end Analyze_Enumeration_Representation_Clause;
4937 ----------------------------
4938 -- Analyze_Free_Statement --
4939 ----------------------------
4941 procedure Analyze_Free_Statement (N : Node_Id) is
4943 Analyze (Expression (N));
4944 end Analyze_Free_Statement;
4946 ---------------------------
4947 -- Analyze_Freeze_Entity --
4948 ---------------------------
4950 procedure Analyze_Freeze_Entity (N : Node_Id) is
4951 E : constant Entity_Id := Entity (N);
4954 -- Remember that we are processing a freezing entity. Required to
4955 -- ensure correct decoration of internal entities associated with
4956 -- interfaces (see New_Overloaded_Entity).
4958 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
4960 -- For tagged types covering interfaces add internal entities that link
4961 -- the primitives of the interfaces with the primitives that cover them.
4962 -- Note: These entities were originally generated only when generating
4963 -- code because their main purpose was to provide support to initialize
4964 -- the secondary dispatch tables. They are now generated also when
4965 -- compiling with no code generation to provide ASIS the relationship
4966 -- between interface primitives and tagged type primitives. They are
4967 -- also used to locate primitives covering interfaces when processing
4968 -- generics (see Derive_Subprograms).
4970 if Ada_Version >= Ada_2005
4971 and then Ekind (E) = E_Record_Type
4972 and then Is_Tagged_Type (E)
4973 and then not Is_Interface (E)
4974 and then Has_Interfaces (E)
4976 -- This would be a good common place to call the routine that checks
4977 -- overriding of interface primitives (and thus factorize calls to
4978 -- Check_Abstract_Overriding located at different contexts in the
4979 -- compiler). However, this is not possible because it causes
4980 -- spurious errors in case of late overriding.
4982 Add_Internal_Interface_Entities (E);
4987 if Ekind (E) = E_Record_Type
4988 and then Is_CPP_Class (E)
4989 and then Is_Tagged_Type (E)
4990 and then Tagged_Type_Expansion
4991 and then Expander_Active
4993 if CPP_Num_Prims (E) = 0 then
4995 -- If the CPP type has user defined components then it must import
4996 -- primitives from C++. This is required because if the C++ class
4997 -- has no primitives then the C++ compiler does not added the _tag
4998 -- component to the type.
5000 pragma Assert (Chars (First_Entity (E)) = Name_uTag);
5002 if First_Entity (E) /= Last_Entity (E) then
5004 ("'C'P'P type must import at least one primitive from C++??",
5009 -- Check that all its primitives are abstract or imported from C++.
5010 -- Check also availability of the C++ constructor.
5013 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
5015 Error_Reported : Boolean := False;
5019 Elmt := First_Elmt (Primitive_Operations (E));
5020 while Present (Elmt) loop
5021 Prim := Node (Elmt);
5023 if Comes_From_Source (Prim) then
5024 if Is_Abstract_Subprogram (Prim) then
5027 elsif not Is_Imported (Prim)
5028 or else Convention (Prim) /= Convention_CPP
5031 ("primitives of 'C'P'P types must be imported from C++ "
5032 & "or abstract??", Prim);
5034 elsif not Has_Constructors
5035 and then not Error_Reported
5037 Error_Msg_Name_1 := Chars (E);
5039 ("??'C'P'P constructor required for type %", Prim);
5040 Error_Reported := True;
5049 -- Check Ada derivation of CPP type
5052 and then Tagged_Type_Expansion
5053 and then Ekind (E) = E_Record_Type
5054 and then Etype (E) /= E
5055 and then Is_CPP_Class (Etype (E))
5056 and then CPP_Num_Prims (Etype (E)) > 0
5057 and then not Is_CPP_Class (E)
5058 and then not Has_CPP_Constructors (Etype (E))
5060 -- If the parent has C++ primitives but it has no constructor then
5061 -- check that all the primitives are overridden in this derivation;
5062 -- otherwise the constructor of the parent is needed to build the
5070 Elmt := First_Elmt (Primitive_Operations (E));
5071 while Present (Elmt) loop
5072 Prim := Node (Elmt);
5074 if not Is_Abstract_Subprogram (Prim)
5075 and then No (Interface_Alias (Prim))
5076 and then Find_Dispatching_Type (Ultimate_Alias (Prim)) /= E
5078 Error_Msg_Name_1 := Chars (Etype (E));
5080 ("'C'P'P constructor required for parent type %", E);
5089 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
5091 -- If we have a type with predicates, build predicate function
5093 if Is_Type (E) and then Has_Predicates (E) then
5094 Build_Predicate_Functions (E, N);
5097 -- If type has delayed aspects, this is where we do the preanalysis at
5098 -- the freeze point, as part of the consistent visibility check. Note
5099 -- that this must be done after calling Build_Predicate_Functions or
5100 -- Build_Invariant_Procedure since these subprograms fix occurrences of
5101 -- the subtype name in the saved expression so that they will not cause
5102 -- trouble in the preanalysis.
5104 if Has_Delayed_Aspects (E)
5105 and then Scope (E) = Current_Scope
5107 -- Retrieve the visibility to the discriminants in order to properly
5108 -- analyze the aspects.
5110 Push_Scope_And_Install_Discriminants (E);
5116 -- Look for aspect specification entries for this entity
5118 Ritem := First_Rep_Item (E);
5119 while Present (Ritem) loop
5120 if Nkind (Ritem) = N_Aspect_Specification
5121 and then Entity (Ritem) = E
5122 and then Is_Delayed_Aspect (Ritem)
5124 Check_Aspect_At_Freeze_Point (Ritem);
5127 Next_Rep_Item (Ritem);
5131 Uninstall_Discriminants_And_Pop_Scope (E);
5133 end Analyze_Freeze_Entity;
5135 ------------------------------------------
5136 -- Analyze_Record_Representation_Clause --
5137 ------------------------------------------
5139 -- Note: we check as much as we can here, but we can't do any checks
5140 -- based on the position values (e.g. overlap checks) until freeze time
5141 -- because especially in Ada 2005 (machine scalar mode), the processing
5142 -- for non-standard bit order can substantially change the positions.
5143 -- See procedure Check_Record_Representation_Clause (called from Freeze)
5144 -- for the remainder of this processing.
5146 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
5147 Ident : constant Node_Id := Identifier (N);
5152 Hbit : Uint := Uint_0;
5156 Rectype : Entity_Id;
5159 function Is_Inherited (Comp : Entity_Id) return Boolean;
5160 -- True if Comp is an inherited component in a record extension
5166 function Is_Inherited (Comp : Entity_Id) return Boolean is
5167 Comp_Base : Entity_Id;
5170 if Ekind (Rectype) = E_Record_Subtype then
5171 Comp_Base := Original_Record_Component (Comp);
5176 return Comp_Base /= Original_Record_Component (Comp_Base);
5181 Is_Record_Extension : Boolean;
5182 -- True if Rectype is a record extension
5184 CR_Pragma : Node_Id := Empty;
5185 -- Points to N_Pragma node if Complete_Representation pragma present
5187 -- Start of processing for Analyze_Record_Representation_Clause
5190 if Ignore_Rep_Clauses then
5195 Rectype := Entity (Ident);
5197 if Rectype = Any_Type or else Rep_Item_Too_Early (Rectype, N) then
5200 Rectype := Underlying_Type (Rectype);
5203 -- First some basic error checks
5205 if not Is_Record_Type (Rectype) then
5207 ("record type required, found}", Ident, First_Subtype (Rectype));
5210 elsif Scope (Rectype) /= Current_Scope then
5211 Error_Msg_N ("type must be declared in this scope", N);
5214 elsif not Is_First_Subtype (Rectype) then
5215 Error_Msg_N ("cannot give record rep clause for subtype", N);
5218 elsif Has_Record_Rep_Clause (Rectype) then
5219 Error_Msg_N ("duplicate record rep clause ignored", N);
5222 elsif Rep_Item_Too_Late (Rectype, N) then
5226 -- We know we have a first subtype, now possibly go the the anonymous
5227 -- base type to determine whether Rectype is a record extension.
5229 Recdef := Type_Definition (Declaration_Node (Base_Type (Rectype)));
5230 Is_Record_Extension :=
5231 Nkind (Recdef) = N_Derived_Type_Definition
5232 and then Present (Record_Extension_Part (Recdef));
5234 if Present (Mod_Clause (N)) then
5236 Loc : constant Source_Ptr := Sloc (N);
5237 M : constant Node_Id := Mod_Clause (N);
5238 P : constant List_Id := Pragmas_Before (M);
5242 pragma Warnings (Off, Mod_Val);
5245 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
5247 if Warn_On_Obsolescent_Feature then
5249 ("?j?mod clause is an obsolescent feature (RM J.8)", N);
5251 ("\?j?use alignment attribute definition clause instead", N);
5258 -- In ASIS_Mode mode, expansion is disabled, but we must convert
5259 -- the Mod clause into an alignment clause anyway, so that the
5260 -- back-end can compute and back-annotate properly the size and
5261 -- alignment of types that may include this record.
5263 -- This seems dubious, this destroys the source tree in a manner
5264 -- not detectable by ASIS ???
5266 if Operating_Mode = Check_Semantics and then ASIS_Mode then
5268 Make_Attribute_Definition_Clause (Loc,
5269 Name => New_Reference_To (Base_Type (Rectype), Loc),
5270 Chars => Name_Alignment,
5271 Expression => Relocate_Node (Expression (M)));
5273 Set_From_At_Mod (AtM_Nod);
5274 Insert_After (N, AtM_Nod);
5275 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
5276 Set_Mod_Clause (N, Empty);
5279 -- Get the alignment value to perform error checking
5281 Mod_Val := Get_Alignment_Value (Expression (M));
5286 -- For untagged types, clear any existing component clauses for the
5287 -- type. If the type is derived, this is what allows us to override
5288 -- a rep clause for the parent. For type extensions, the representation
5289 -- of the inherited components is inherited, so we want to keep previous
5290 -- component clauses for completeness.
5292 if not Is_Tagged_Type (Rectype) then
5293 Comp := First_Component_Or_Discriminant (Rectype);
5294 while Present (Comp) loop
5295 Set_Component_Clause (Comp, Empty);
5296 Next_Component_Or_Discriminant (Comp);
5300 -- All done if no component clauses
5302 CC := First (Component_Clauses (N));
5308 -- A representation like this applies to the base type
5310 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
5311 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
5312 Set_Has_Specified_Layout (Base_Type (Rectype));
5314 -- Process the component clauses
5316 while Present (CC) loop
5320 if Nkind (CC) = N_Pragma then
5323 -- The only pragma of interest is Complete_Representation
5325 if Pragma_Name (CC) = Name_Complete_Representation then
5329 -- Processing for real component clause
5332 Posit := Static_Integer (Position (CC));
5333 Fbit := Static_Integer (First_Bit (CC));
5334 Lbit := Static_Integer (Last_Bit (CC));
5337 and then Fbit /= No_Uint
5338 and then Lbit /= No_Uint
5342 ("position cannot be negative", Position (CC));
5346 ("first bit cannot be negative", First_Bit (CC));
5348 -- The Last_Bit specified in a component clause must not be
5349 -- less than the First_Bit minus one (RM-13.5.1(10)).
5351 elsif Lbit < Fbit - 1 then
5353 ("last bit cannot be less than first bit minus one",
5356 -- Values look OK, so find the corresponding record component
5357 -- Even though the syntax allows an attribute reference for
5358 -- implementation-defined components, GNAT does not allow the
5359 -- tag to get an explicit position.
5361 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
5362 if Attribute_Name (Component_Name (CC)) = Name_Tag then
5363 Error_Msg_N ("position of tag cannot be specified", CC);
5365 Error_Msg_N ("illegal component name", CC);
5369 Comp := First_Entity (Rectype);
5370 while Present (Comp) loop
5371 exit when Chars (Comp) = Chars (Component_Name (CC));
5377 -- Maybe component of base type that is absent from
5378 -- statically constrained first subtype.
5380 Comp := First_Entity (Base_Type (Rectype));
5381 while Present (Comp) loop
5382 exit when Chars (Comp) = Chars (Component_Name (CC));
5389 ("component clause is for non-existent field", CC);
5391 -- Ada 2012 (AI05-0026): Any name that denotes a
5392 -- discriminant of an object of an unchecked union type
5393 -- shall not occur within a record_representation_clause.
5395 -- The general restriction of using record rep clauses on
5396 -- Unchecked_Union types has now been lifted. Since it is
5397 -- possible to introduce a record rep clause which mentions
5398 -- the discriminant of an Unchecked_Union in non-Ada 2012
5399 -- code, this check is applied to all versions of the
5402 elsif Ekind (Comp) = E_Discriminant
5403 and then Is_Unchecked_Union (Rectype)
5406 ("cannot reference discriminant of unchecked union",
5407 Component_Name (CC));
5409 elsif Is_Record_Extension and then Is_Inherited (Comp) then
5411 ("component clause not allowed for inherited "
5412 & "component&", CC, Comp);
5414 elsif Present (Component_Clause (Comp)) then
5416 -- Diagnose duplicate rep clause, or check consistency
5417 -- if this is an inherited component. In a double fault,
5418 -- there may be a duplicate inconsistent clause for an
5419 -- inherited component.
5421 if Scope (Original_Record_Component (Comp)) = Rectype
5422 or else Parent (Component_Clause (Comp)) = N
5424 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
5425 Error_Msg_N ("component clause previously given#", CC);
5429 Rep1 : constant Node_Id := Component_Clause (Comp);
5431 if Intval (Position (Rep1)) /=
5432 Intval (Position (CC))
5433 or else Intval (First_Bit (Rep1)) /=
5434 Intval (First_Bit (CC))
5435 or else Intval (Last_Bit (Rep1)) /=
5436 Intval (Last_Bit (CC))
5439 ("component clause inconsistent "
5440 & "with representation of ancestor", CC);
5442 elsif Warn_On_Redundant_Constructs then
5444 ("?r?redundant confirming component clause "
5445 & "for component!", CC);
5450 -- Normal case where this is the first component clause we
5451 -- have seen for this entity, so set it up properly.
5454 -- Make reference for field in record rep clause and set
5455 -- appropriate entity field in the field identifier.
5458 (Comp, Component_Name (CC), Set_Ref => False);
5459 Set_Entity (Component_Name (CC), Comp);
5461 -- Update Fbit and Lbit to the actual bit number
5463 Fbit := Fbit + UI_From_Int (SSU) * Posit;
5464 Lbit := Lbit + UI_From_Int (SSU) * Posit;
5466 if Has_Size_Clause (Rectype)
5467 and then RM_Size (Rectype) <= Lbit
5470 ("bit number out of range of specified size",
5473 Set_Component_Clause (Comp, CC);
5474 Set_Component_Bit_Offset (Comp, Fbit);
5475 Set_Esize (Comp, 1 + (Lbit - Fbit));
5476 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
5477 Set_Normalized_Position (Comp, Fbit / SSU);
5479 if Warn_On_Overridden_Size
5480 and then Has_Size_Clause (Etype (Comp))
5481 and then RM_Size (Etype (Comp)) /= Esize (Comp)
5484 ("?S?component size overrides size clause for&",
5485 Component_Name (CC), Etype (Comp));
5488 -- This information is also set in the corresponding
5489 -- component of the base type, found by accessing the
5490 -- Original_Record_Component link if it is present.
5492 Ocomp := Original_Record_Component (Comp);
5499 (Component_Name (CC),
5505 (Comp, First_Node (CC), "component clause", Biased);
5507 if Present (Ocomp) then
5508 Set_Component_Clause (Ocomp, CC);
5509 Set_Component_Bit_Offset (Ocomp, Fbit);
5510 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
5511 Set_Normalized_Position (Ocomp, Fbit / SSU);
5512 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
5514 Set_Normalized_Position_Max
5515 (Ocomp, Normalized_Position (Ocomp));
5517 -- Note: we don't use Set_Biased here, because we
5518 -- already gave a warning above if needed, and we
5519 -- would get a duplicate for the same name here.
5521 Set_Has_Biased_Representation
5522 (Ocomp, Has_Biased_Representation (Comp));
5525 if Esize (Comp) < 0 then
5526 Error_Msg_N ("component size is negative", CC);
5537 -- Check missing components if Complete_Representation pragma appeared
5539 if Present (CR_Pragma) then
5540 Comp := First_Component_Or_Discriminant (Rectype);
5541 while Present (Comp) loop
5542 if No (Component_Clause (Comp)) then
5544 ("missing component clause for &", CR_Pragma, Comp);
5547 Next_Component_Or_Discriminant (Comp);
5550 -- Give missing components warning if required
5552 elsif Warn_On_Unrepped_Components then
5554 Num_Repped_Components : Nat := 0;
5555 Num_Unrepped_Components : Nat := 0;
5558 -- First count number of repped and unrepped components
5560 Comp := First_Component_Or_Discriminant (Rectype);
5561 while Present (Comp) loop
5562 if Present (Component_Clause (Comp)) then
5563 Num_Repped_Components := Num_Repped_Components + 1;
5565 Num_Unrepped_Components := Num_Unrepped_Components + 1;
5568 Next_Component_Or_Discriminant (Comp);
5571 -- We are only interested in the case where there is at least one
5572 -- unrepped component, and at least half the components have rep
5573 -- clauses. We figure that if less than half have them, then the
5574 -- partial rep clause is really intentional. If the component
5575 -- type has no underlying type set at this point (as for a generic
5576 -- formal type), we don't know enough to give a warning on the
5579 if Num_Unrepped_Components > 0
5580 and then Num_Unrepped_Components < Num_Repped_Components
5582 Comp := First_Component_Or_Discriminant (Rectype);
5583 while Present (Comp) loop
5584 if No (Component_Clause (Comp))
5585 and then Comes_From_Source (Comp)
5586 and then Present (Underlying_Type (Etype (Comp)))
5587 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
5588 or else Size_Known_At_Compile_Time
5589 (Underlying_Type (Etype (Comp))))
5590 and then not Has_Warnings_Off (Rectype)
5592 Error_Msg_Sloc := Sloc (Comp);
5594 ("?C?no component clause given for & declared #",
5598 Next_Component_Or_Discriminant (Comp);
5603 end Analyze_Record_Representation_Clause;
5605 -------------------------------------------
5606 -- Build_Invariant_Procedure_Declaration --
5607 -------------------------------------------
5609 function Build_Invariant_Procedure_Declaration
5610 (Typ : Entity_Id) return Node_Id
5612 Loc : constant Source_Ptr := Sloc (Typ);
5613 Object_Entity : constant Entity_Id :=
5614 Make_Defining_Identifier (Loc, New_Internal_Name ('I'));
5619 Set_Etype (Object_Entity, Typ);
5621 -- Check for duplicate definiations.
5623 if Has_Invariants (Typ) and then Present (Invariant_Procedure (Typ)) then
5628 Make_Defining_Identifier (Loc,
5629 Chars => New_External_Name (Chars (Typ), "Invariant"));
5630 Set_Has_Invariants (Typ);
5631 Set_Ekind (SId, E_Procedure);
5632 Set_Is_Invariant_Procedure (SId);
5633 Set_Invariant_Procedure (Typ, SId);
5636 Make_Procedure_Specification (Loc,
5637 Defining_Unit_Name => SId,
5638 Parameter_Specifications => New_List (
5639 Make_Parameter_Specification (Loc,
5640 Defining_Identifier => Object_Entity,
5641 Parameter_Type => New_Occurrence_Of (Typ, Loc))));
5643 return Make_Subprogram_Declaration (Loc, Specification => Spec);
5644 end Build_Invariant_Procedure_Declaration;
5646 -------------------------------
5647 -- Build_Invariant_Procedure --
5648 -------------------------------
5650 -- The procedure that is constructed here has the form
5652 -- procedure typInvariant (Ixxx : typ) is
5654 -- pragma Check (Invariant, exp, "failed invariant from xxx");
5655 -- pragma Check (Invariant, exp, "failed invariant from xxx");
5657 -- pragma Check (Invariant, exp, "failed inherited invariant from xxx");
5659 -- end typInvariant;
5661 procedure Build_Invariant_Procedure (Typ : Entity_Id; N : Node_Id) is
5662 Loc : constant Source_Ptr := Sloc (Typ);
5669 Visible_Decls : constant List_Id := Visible_Declarations (N);
5670 Private_Decls : constant List_Id := Private_Declarations (N);
5672 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean);
5673 -- Appends statements to Stmts for any invariants in the rep item chain
5674 -- of the given type. If Inherit is False, then we only process entries
5675 -- on the chain for the type Typ. If Inherit is True, then we ignore any
5676 -- Invariant aspects, but we process all Invariant'Class aspects, adding
5677 -- "inherited" to the exception message and generating an informational
5678 -- message about the inheritance of an invariant.
5680 Object_Name : Name_Id;
5681 -- Name for argument of invariant procedure
5683 Object_Entity : Node_Id;
5684 -- The entity of the formal for the procedure
5686 --------------------
5687 -- Add_Invariants --
5688 --------------------
5690 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean) is
5700 procedure Replace_Type_Reference (N : Node_Id);
5701 -- Replace a single occurrence N of the subtype name with a reference
5702 -- to the formal of the predicate function. N can be an identifier
5703 -- referencing the subtype, or a selected component, representing an
5704 -- appropriately qualified occurrence of the subtype name.
5706 procedure Replace_Type_References is
5707 new Replace_Type_References_Generic (Replace_Type_Reference);
5708 -- Traverse an expression replacing all occurrences of the subtype
5709 -- name with appropriate references to the object that is the formal
5710 -- parameter of the predicate function. Note that we must ensure
5711 -- that the type and entity information is properly set in the
5712 -- replacement node, since we will do a Preanalyze call of this
5713 -- expression without proper visibility of the procedure argument.
5715 ----------------------------
5716 -- Replace_Type_Reference --
5717 ----------------------------
5719 -- Note: See comments in Add_Predicates.Replace_Type_Reference
5720 -- regarding handling of Sloc and Comes_From_Source.
5722 procedure Replace_Type_Reference (N : Node_Id) is
5724 -- Invariant'Class, replace with T'Class (obj)
5726 if Class_Present (Ritem) then
5728 Make_Type_Conversion (Sloc (N),
5730 Make_Attribute_Reference (Sloc (N),
5731 Prefix => New_Occurrence_Of (T, Sloc (N)),
5732 Attribute_Name => Name_Class),
5733 Expression => Make_Identifier (Sloc (N), Object_Name)));
5735 Set_Entity (Expression (N), Object_Entity);
5736 Set_Etype (Expression (N), Typ);
5738 -- Invariant, replace with obj
5741 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
5742 Set_Entity (N, Object_Entity);
5746 Set_Comes_From_Source (N, True);
5747 end Replace_Type_Reference;
5749 -- Start of processing for Add_Invariants
5752 Ritem := First_Rep_Item (T);
5753 while Present (Ritem) loop
5754 if Nkind (Ritem) = N_Pragma
5755 and then Pragma_Name (Ritem) = Name_Invariant
5757 Arg1 := First (Pragma_Argument_Associations (Ritem));
5758 Arg2 := Next (Arg1);
5759 Arg3 := Next (Arg2);
5761 Arg1 := Get_Pragma_Arg (Arg1);
5762 Arg2 := Get_Pragma_Arg (Arg2);
5764 -- For Inherit case, ignore Invariant, process only Class case
5767 if not Class_Present (Ritem) then
5771 -- For Inherit false, process only item for right type
5774 if Entity (Arg1) /= Typ then
5780 Stmts := Empty_List;
5783 Exp := New_Copy_Tree (Arg2);
5785 -- Preserve sloc of original pragma Invariant
5787 Loc := Sloc (Ritem);
5789 -- We need to replace any occurrences of the name of the type
5790 -- with references to the object, converted to type'Class in
5791 -- the case of Invariant'Class aspects.
5793 Replace_Type_References (Exp, Chars (T));
5795 -- If this invariant comes from an aspect, find the aspect
5796 -- specification, and replace the saved expression because
5797 -- we need the subtype references replaced for the calls to
5798 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
5799 -- and Check_Aspect_At_End_Of_Declarations.
5801 if From_Aspect_Specification (Ritem) then
5806 -- Loop to find corresponding aspect, note that this
5807 -- must be present given the pragma is marked delayed.
5809 Aitem := Next_Rep_Item (Ritem);
5810 while Present (Aitem) loop
5811 if Nkind (Aitem) = N_Aspect_Specification
5812 and then Aspect_Rep_Item (Aitem) = Ritem
5815 (Identifier (Aitem), New_Copy_Tree (Exp));
5819 Aitem := Next_Rep_Item (Aitem);
5824 -- Now we need to preanalyze the expression to properly capture
5825 -- the visibility in the visible part. The expression will not
5826 -- be analyzed for real until the body is analyzed, but that is
5827 -- at the end of the private part and has the wrong visibility.
5829 Set_Parent (Exp, N);
5830 Preanalyze_Assert_Expression (Exp, Standard_Boolean);
5832 -- Build first two arguments for Check pragma
5835 Make_Pragma_Argument_Association (Loc,
5836 Expression => Make_Identifier (Loc, Name_Invariant)),
5837 Make_Pragma_Argument_Association (Loc,
5838 Expression => Exp));
5840 -- Add message if present in Invariant pragma
5842 if Present (Arg3) then
5843 Str := Strval (Get_Pragma_Arg (Arg3));
5845 -- If inherited case, and message starts "failed invariant",
5846 -- change it to be "failed inherited invariant".
5849 String_To_Name_Buffer (Str);
5851 if Name_Buffer (1 .. 16) = "failed invariant" then
5852 Insert_Str_In_Name_Buffer ("inherited ", 8);
5853 Str := String_From_Name_Buffer;
5858 Make_Pragma_Argument_Association (Loc,
5859 Expression => Make_String_Literal (Loc, Str)));
5862 -- Add Check pragma to list of statements
5866 Pragma_Identifier =>
5867 Make_Identifier (Loc, Name_Check),
5868 Pragma_Argument_Associations => Assoc));
5870 -- If Inherited case and option enabled, output info msg. Note
5871 -- that we know this is a case of Invariant'Class.
5873 if Inherit and Opt.List_Inherited_Aspects then
5874 Error_Msg_Sloc := Sloc (Ritem);
5876 ("?L?info: & inherits `Invariant''Class` aspect from #",
5882 Next_Rep_Item (Ritem);
5886 -- Start of processing for Build_Invariant_Procedure
5894 -- If the aspect specification exists for some view of the type, the
5895 -- declaration for the procedure has been created.
5897 if Has_Invariants (Typ) then
5898 SId := Invariant_Procedure (Typ);
5901 if Present (SId) then
5902 PDecl := Unit_Declaration_Node (SId);
5905 PDecl := Build_Invariant_Procedure_Declaration (Typ);
5908 -- Recover formal of procedure, for use in the calls to invariant
5909 -- functions (including inherited ones).
5913 (First (Parameter_Specifications (Specification (PDecl))));
5914 Object_Name := Chars (Object_Entity);
5916 -- Add invariants for the current type
5918 Add_Invariants (Typ, Inherit => False);
5920 -- Add invariants for parent types
5923 Current_Typ : Entity_Id;
5924 Parent_Typ : Entity_Id;
5929 Parent_Typ := Etype (Current_Typ);
5931 if Is_Private_Type (Parent_Typ)
5932 and then Present (Full_View (Base_Type (Parent_Typ)))
5934 Parent_Typ := Full_View (Base_Type (Parent_Typ));
5937 exit when Parent_Typ = Current_Typ;
5939 Current_Typ := Parent_Typ;
5940 Add_Invariants (Current_Typ, Inherit => True);
5944 -- Build the procedure if we generated at least one Check pragma
5946 if Stmts /= No_List then
5947 Spec := Copy_Separate_Tree (Specification (PDecl));
5950 Make_Subprogram_Body (Loc,
5951 Specification => Spec,
5952 Declarations => Empty_List,
5953 Handled_Statement_Sequence =>
5954 Make_Handled_Sequence_Of_Statements (Loc,
5955 Statements => Stmts));
5957 -- Insert procedure declaration and spec at the appropriate points.
5958 -- If declaration is already analyzed, it was processed by the
5959 -- generated pragma.
5961 if Present (Private_Decls) then
5963 -- The spec goes at the end of visible declarations, but they have
5964 -- already been analyzed, so we need to explicitly do the analyze.
5966 if not Analyzed (PDecl) then
5967 Append_To (Visible_Decls, PDecl);
5971 -- The body goes at the end of the private declarations, which we
5972 -- have not analyzed yet, so we do not need to perform an explicit
5973 -- analyze call. We skip this if there are no private declarations
5974 -- (this is an error that will be caught elsewhere);
5976 Append_To (Private_Decls, PBody);
5978 -- If the invariant appears on the full view of a type, the
5979 -- analysis of the private part is complete, and we must
5980 -- analyze the new body explicitly.
5982 if In_Private_Part (Current_Scope) then
5986 -- If there are no private declarations this may be an error that
5987 -- will be diagnosed elsewhere. However, if this is a non-private
5988 -- type that inherits invariants, it needs no completion and there
5989 -- may be no private part. In this case insert invariant procedure
5990 -- at end of current declarative list, and analyze at once, given
5991 -- that the type is about to be frozen.
5993 elsif not Is_Private_Type (Typ) then
5994 Append_To (Visible_Decls, PDecl);
5995 Append_To (Visible_Decls, PBody);
6000 end Build_Invariant_Procedure;
6002 -------------------------------
6003 -- Build_Predicate_Functions --
6004 -------------------------------
6006 -- The procedures that are constructed here have the form:
6008 -- function typPredicate (Ixxx : typ) return Boolean is
6011 -- exp1 and then exp2 and then ...
6012 -- and then typ1Predicate (typ1 (Ixxx))
6013 -- and then typ2Predicate (typ2 (Ixxx))
6015 -- end typPredicate;
6017 -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that
6018 -- this is the point at which these expressions get analyzed, providing the
6019 -- required delay, and typ1, typ2, are entities from which predicates are
6020 -- inherited. Note that we do NOT generate Check pragmas, that's because we
6021 -- use this function even if checks are off, e.g. for membership tests.
6023 -- If the expression has at least one Raise_Expression, then we also build
6024 -- the typPredicateM version of the function, in which any occurrence of a
6025 -- Raise_Expression is converted to "return False".
6027 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id) is
6028 Loc : constant Source_Ptr := Sloc (Typ);
6031 -- This is the expression for the result of the function. It is
6032 -- is build by connecting the component predicates with AND THEN.
6035 -- This is the corresponding return expression for the Predicate_M
6036 -- function. It differs in that raise expressions are marked for
6037 -- special expansion (see Process_REs).
6039 Object_Name : constant Name_Id := New_Internal_Name ('I');
6040 -- Name for argument of Predicate procedure. Note that we use the same
6041 -- name for both predicate procedure. That way the reference within the
6042 -- predicate expression is the same in both functions.
6044 Object_Entity : constant Entity_Id :=
6045 Make_Defining_Identifier (Loc, Chars => Object_Name);
6046 -- Entity for argument of Predicate procedure
6048 Object_Entity_M : constant Entity_Id :=
6049 Make_Defining_Identifier (Loc, Chars => Object_Name);
6050 -- Entity for argument of Predicate_M procedure
6052 Raise_Expression_Present : Boolean := False;
6053 -- Set True if Expr has at least one Raise_Expression
6055 Static_Predic : Node_Id := Empty;
6056 -- Set to N_Pragma node for a static predicate if one is encountered
6058 procedure Add_Call (T : Entity_Id);
6059 -- Includes a call to the predicate function for type T in Expr if T
6060 -- has predicates and Predicate_Function (T) is non-empty.
6062 procedure Add_Predicates;
6063 -- Appends expressions for any Predicate pragmas in the rep item chain
6064 -- Typ to Expr. Note that we look only at items for this exact entity.
6065 -- Inheritance of predicates for the parent type is done by calling the
6066 -- Predicate_Function of the parent type, using Add_Call above.
6068 function Test_RE (N : Node_Id) return Traverse_Result;
6069 -- Used in Test_REs, tests one node for being a raise expression, and if
6070 -- so sets Raise_Expression_Present True.
6072 procedure Test_REs is new Traverse_Proc (Test_RE);
6073 -- Tests to see if Expr contains any raise expressions
6075 function Process_RE (N : Node_Id) return Traverse_Result;
6076 -- Used in Process REs, tests if node N is a raise expression, and if
6077 -- so, marks it to be converted to return False.
6079 procedure Process_REs is new Traverse_Proc (Process_RE);
6080 -- Marks any raise expressions in Expr_M to return False
6086 procedure Add_Call (T : Entity_Id) is
6090 if Present (T) and then Present (Predicate_Function (T)) then
6091 Set_Has_Predicates (Typ);
6093 -- Build the call to the predicate function of T
6097 (T, Convert_To (T, Make_Identifier (Loc, Object_Name)));
6099 -- Add call to evolving expression, using AND THEN if needed
6106 Left_Opnd => Relocate_Node (Expr),
6110 -- Output info message on inheritance if required. Note we do not
6111 -- give this information for generic actual types, since it is
6112 -- unwelcome noise in that case in instantiations. We also
6113 -- generally suppress the message in instantiations, and also
6114 -- if it involves internal names.
6116 if Opt.List_Inherited_Aspects
6117 and then not Is_Generic_Actual_Type (Typ)
6118 and then Instantiation_Depth (Sloc (Typ)) = 0
6119 and then not Is_Internal_Name (Chars (T))
6120 and then not Is_Internal_Name (Chars (Typ))
6122 Error_Msg_Sloc := Sloc (Predicate_Function (T));
6123 Error_Msg_Node_2 := T;
6124 Error_Msg_N ("info: & inherits predicate from & #?L?", Typ);
6129 --------------------
6130 -- Add_Predicates --
6131 --------------------
6133 procedure Add_Predicates is
6138 procedure Replace_Type_Reference (N : Node_Id);
6139 -- Replace a single occurrence N of the subtype name with a reference
6140 -- to the formal of the predicate function. N can be an identifier
6141 -- referencing the subtype, or a selected component, representing an
6142 -- appropriately qualified occurrence of the subtype name.
6144 procedure Replace_Type_References is
6145 new Replace_Type_References_Generic (Replace_Type_Reference);
6146 -- Traverse an expression changing every occurrence of an identifier
6147 -- whose name matches the name of the subtype with a reference to
6148 -- the formal parameter of the predicate function.
6150 ----------------------------
6151 -- Replace_Type_Reference --
6152 ----------------------------
6154 procedure Replace_Type_Reference (N : Node_Id) is
6156 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
6157 -- Use the Sloc of the usage name, not the defining name
6160 Set_Entity (N, Object_Entity);
6162 -- We want to treat the node as if it comes from source, so that
6163 -- ASIS will not ignore it
6165 Set_Comes_From_Source (N, True);
6166 end Replace_Type_Reference;
6168 -- Start of processing for Add_Predicates
6171 Ritem := First_Rep_Item (Typ);
6172 while Present (Ritem) loop
6173 if Nkind (Ritem) = N_Pragma
6174 and then Pragma_Name (Ritem) = Name_Predicate
6176 -- Save the static predicate of the type for diagnostics and
6177 -- error reporting purposes.
6179 if Present (Corresponding_Aspect (Ritem))
6180 and then Chars (Identifier (Corresponding_Aspect (Ritem))) =
6181 Name_Static_Predicate
6183 Static_Predic := Ritem;
6186 -- Acquire arguments
6188 Arg1 := First (Pragma_Argument_Associations (Ritem));
6189 Arg2 := Next (Arg1);
6191 Arg1 := Get_Pragma_Arg (Arg1);
6192 Arg2 := Get_Pragma_Arg (Arg2);
6194 -- See if this predicate pragma is for the current type or for
6195 -- its full view. A predicate on a private completion is placed
6196 -- on the partial view beause this is the visible entity that
6199 if Entity (Arg1) = Typ
6200 or else Full_View (Entity (Arg1)) = Typ
6202 -- We have a match, this entry is for our subtype
6204 -- We need to replace any occurrences of the name of the
6205 -- type with references to the object.
6207 Replace_Type_References (Arg2, Chars (Typ));
6209 -- If this predicate comes from an aspect, find the aspect
6210 -- specification, and replace the saved expression because
6211 -- we need the subtype references replaced for the calls to
6212 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
6213 -- and Check_Aspect_At_End_Of_Declarations.
6215 if From_Aspect_Specification (Ritem) then
6220 -- Loop to find corresponding aspect, note that this
6221 -- must be present given the pragma is marked delayed.
6223 Aitem := Next_Rep_Item (Ritem);
6225 if Nkind (Aitem) = N_Aspect_Specification
6226 and then Aspect_Rep_Item (Aitem) = Ritem
6229 (Identifier (Aitem), New_Copy_Tree (Arg2));
6233 Aitem := Next_Rep_Item (Aitem);
6238 -- Now we can add the expression
6241 Expr := Relocate_Node (Arg2);
6243 -- There already was a predicate, so add to it
6248 Left_Opnd => Relocate_Node (Expr),
6249 Right_Opnd => Relocate_Node (Arg2));
6254 Next_Rep_Item (Ritem);
6262 function Process_RE (N : Node_Id) return Traverse_Result is
6264 if Nkind (N) = N_Raise_Expression then
6265 Set_Convert_To_Return_False (N);
6276 function Test_RE (N : Node_Id) return Traverse_Result is
6278 if Nkind (N) = N_Raise_Expression then
6279 Raise_Expression_Present := True;
6286 -- Start of processing for Build_Predicate_Functions
6289 -- Return if already built or if type does not have predicates
6291 if not Has_Predicates (Typ)
6292 or else Present (Predicate_Function (Typ))
6297 -- Prepare to construct predicate expression
6301 -- Add Predicates for the current type
6305 -- Add predicates for ancestor if present
6308 Atyp : constant Entity_Id := Nearest_Ancestor (Typ);
6310 if Present (Atyp) then
6315 -- Case where predicates are present
6317 if Present (Expr) then
6319 -- Test for raise expression present
6323 -- If raise expression is present, capture a copy of Expr for use
6324 -- in building the predicateM function version later on. For this
6325 -- copy we replace references to Object_Entity by Object_Entity_M.
6327 if Raise_Expression_Present then
6329 Map : constant Elist_Id := New_Elmt_List;
6331 Append_Elmt (Object_Entity, Map);
6332 Append_Elmt (Object_Entity_M, Map);
6333 Expr_M := New_Copy_Tree (Expr, Map => Map);
6337 -- Build the main predicate function
6340 SId : constant Entity_Id :=
6341 Make_Defining_Identifier (Loc,
6342 Chars => New_External_Name (Chars (Typ), "Predicate"));
6343 -- The entity for the the function spec
6345 SIdB : constant Entity_Id :=
6346 Make_Defining_Identifier (Loc,
6347 Chars => New_External_Name (Chars (Typ), "Predicate"));
6348 -- The entity for the function body
6355 -- Build function declaration
6357 Set_Ekind (SId, E_Function);
6358 Set_Is_Predicate_Function (SId);
6359 Set_Predicate_Function (Typ, SId);
6361 -- The predicate function is shared between views of a type
6363 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
6364 Set_Predicate_Function (Full_View (Typ), SId);
6368 Make_Function_Specification (Loc,
6369 Defining_Unit_Name => SId,
6370 Parameter_Specifications => New_List (
6371 Make_Parameter_Specification (Loc,
6372 Defining_Identifier => Object_Entity,
6373 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
6374 Result_Definition =>
6375 New_Occurrence_Of (Standard_Boolean, Loc));
6378 Make_Subprogram_Declaration (Loc,
6379 Specification => Spec);
6381 -- Build function body
6384 Make_Function_Specification (Loc,
6385 Defining_Unit_Name => SIdB,
6386 Parameter_Specifications => New_List (
6387 Make_Parameter_Specification (Loc,
6388 Defining_Identifier =>
6389 Make_Defining_Identifier (Loc, Object_Name),
6391 New_Occurrence_Of (Typ, Loc))),
6392 Result_Definition =>
6393 New_Occurrence_Of (Standard_Boolean, Loc));
6396 Make_Subprogram_Body (Loc,
6397 Specification => Spec,
6398 Declarations => Empty_List,
6399 Handled_Statement_Sequence =>
6400 Make_Handled_Sequence_Of_Statements (Loc,
6401 Statements => New_List (
6402 Make_Simple_Return_Statement (Loc,
6403 Expression => Expr))));
6405 -- Insert declaration before freeze node and body after
6407 Insert_Before_And_Analyze (N, FDecl);
6408 Insert_After_And_Analyze (N, FBody);
6411 -- Test for raise expressions present and if so build M version
6413 if Raise_Expression_Present then
6415 SId : constant Entity_Id :=
6416 Make_Defining_Identifier (Loc,
6417 Chars => New_External_Name (Chars (Typ), "PredicateM"));
6418 -- The entity for the the function spec
6420 SIdB : constant Entity_Id :=
6421 Make_Defining_Identifier (Loc,
6422 Chars => New_External_Name (Chars (Typ), "PredicateM"));
6423 -- The entity for the function body
6431 -- Mark any raise expressions for special expansion
6433 Process_REs (Expr_M);
6435 -- Build function declaration
6437 Set_Ekind (SId, E_Function);
6438 Set_Is_Predicate_Function_M (SId);
6439 Set_Predicate_Function_M (Typ, SId);
6441 -- The predicate function is shared between views of a type
6443 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
6444 Set_Predicate_Function_M (Full_View (Typ), SId);
6448 Make_Function_Specification (Loc,
6449 Defining_Unit_Name => SId,
6450 Parameter_Specifications => New_List (
6451 Make_Parameter_Specification (Loc,
6452 Defining_Identifier => Object_Entity_M,
6453 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
6454 Result_Definition =>
6455 New_Occurrence_Of (Standard_Boolean, Loc));
6458 Make_Subprogram_Declaration (Loc,
6459 Specification => Spec);
6461 -- Build function body
6464 Make_Function_Specification (Loc,
6465 Defining_Unit_Name => SIdB,
6466 Parameter_Specifications => New_List (
6467 Make_Parameter_Specification (Loc,
6468 Defining_Identifier =>
6469 Make_Defining_Identifier (Loc, Object_Name),
6471 New_Occurrence_Of (Typ, Loc))),
6472 Result_Definition =>
6473 New_Occurrence_Of (Standard_Boolean, Loc));
6475 -- Build the body, we declare the boolean expression before
6476 -- doing the return, because we are not really confident of
6477 -- what happens if a return appears within a return!
6480 Make_Defining_Identifier (Loc,
6481 Chars => New_Internal_Name ('B'));
6484 Make_Subprogram_Body (Loc,
6485 Specification => Spec,
6487 Declarations => New_List (
6488 Make_Object_Declaration (Loc,
6489 Defining_Identifier => BTemp,
6490 Constant_Present => True,
6491 Object_Definition =>
6492 New_Reference_To (Standard_Boolean, Loc),
6493 Expression => Expr_M)),
6495 Handled_Statement_Sequence =>
6496 Make_Handled_Sequence_Of_Statements (Loc,
6497 Statements => New_List (
6498 Make_Simple_Return_Statement (Loc,
6499 Expression => New_Reference_To (BTemp, Loc)))));
6501 -- Insert declaration before freeze node and body after
6503 Insert_Before_And_Analyze (N, FDecl);
6504 Insert_After_And_Analyze (N, FBody);
6508 if Is_Scalar_Type (Typ) then
6510 -- Attempt to build a static predicate for a discrete or a real
6511 -- subtype. This action may fail because the actual expression may
6512 -- not be static. Note that the presence of an inherited or
6513 -- explicitly declared dynamic predicate is orthogonal to this
6514 -- check because we are only interested in the static predicate.
6516 if Ekind_In (Typ, E_Decimal_Fixed_Point_Subtype,
6517 E_Enumeration_Subtype,
6518 E_Floating_Point_Subtype,
6519 E_Modular_Integer_Subtype,
6520 E_Ordinary_Fixed_Point_Subtype,
6521 E_Signed_Integer_Subtype)
6523 Build_Static_Predicate (Typ, Expr, Object_Name);
6525 -- Emit an error when the predicate is categorized as static
6526 -- but its expression is dynamic.
6528 if Present (Static_Predic)
6529 and then No (Static_Predicate (Typ))
6532 ("expression does not have required form for "
6533 & "static predicate",
6534 Next (First (Pragma_Argument_Associations
6539 -- If a static predicate applies on other types, that's an error:
6540 -- either the type is scalar but non-static, or it's not even a
6541 -- scalar type. We do not issue an error on generated types, as
6542 -- these may be duplicates of the same error on a source type.
6544 elsif Present (Static_Predic) and then Comes_From_Source (Typ) then
6545 if Is_Scalar_Type (Typ) then
6547 ("static predicate not allowed for non-static type&",
6551 ("static predicate not allowed for non-scalar type&",
6556 end Build_Predicate_Functions;
6558 ----------------------------
6559 -- Build_Static_Predicate --
6560 ----------------------------
6562 procedure Build_Static_Predicate
6567 Loc : constant Source_Ptr := Sloc (Expr);
6569 Non_Static : exception;
6570 -- Raised if something non-static is found
6572 Btyp : constant Entity_Id := Base_Type (Typ);
6574 BLo : constant Uint := Expr_Value (Type_Low_Bound (Btyp));
6575 BHi : constant Uint := Expr_Value (Type_High_Bound (Btyp));
6576 -- Low bound and high bound value of base type of Typ
6578 TLo : constant Uint := Expr_Value (Type_Low_Bound (Typ));
6579 THi : constant Uint := Expr_Value (Type_High_Bound (Typ));
6580 -- Low bound and high bound values of static subtype Typ
6585 -- One entry in a Rlist value, a single REnt (range entry) value denotes
6586 -- one range from Lo to Hi. To represent a single value range Lo = Hi =
6589 type RList is array (Nat range <>) of REnt;
6590 -- A list of ranges. The ranges are sorted in increasing order, and are
6591 -- disjoint (there is a gap of at least one value between each range in
6592 -- the table). A value is in the set of ranges in Rlist if it lies
6593 -- within one of these ranges.
6595 False_Range : constant RList :=
6596 RList'(1 .. 0 => REnt'(No_Uint, No_Uint));
6597 -- An empty set of ranges represents a range list that can never be
6598 -- satisfied, since there are no ranges in which the value could lie,
6599 -- so it does not lie in any of them. False_Range is a canonical value
6600 -- for this empty set, but general processing should test for an Rlist
6601 -- with length zero (see Is_False predicate), since other null ranges
6602 -- may appear which must be treated as False.
6604 True_Range : constant RList := RList'(1 => REnt'(BLo, BHi));
6605 -- Range representing True, value must be in the base range
6607 function "and" (Left : RList; Right : RList) return RList;
6608 -- And's together two range lists, returning a range list. This is a set
6609 -- intersection operation.
6611 function "or" (Left : RList; Right : RList) return RList;
6612 -- Or's together two range lists, returning a range list. This is a set
6615 function "not" (Right : RList) return RList;
6616 -- Returns complement of a given range list, i.e. a range list
6617 -- representing all the values in TLo .. THi that are not in the input
6620 function Build_Val (V : Uint) return Node_Id;
6621 -- Return an analyzed N_Identifier node referencing this value, suitable
6622 -- for use as an entry in the Static_Predicate list. This node is typed
6623 -- with the base type.
6625 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id;
6626 -- Return an analyzed N_Range node referencing this range, suitable for
6627 -- use as an entry in the Static_Predicate list. This node is typed with
6630 function Get_RList (Exp : Node_Id) return RList;
6631 -- This is a recursive routine that converts the given expression into a
6632 -- list of ranges, suitable for use in building the static predicate.
6634 function Is_False (R : RList) return Boolean;
6635 pragma Inline (Is_False);
6636 -- Returns True if the given range list is empty, and thus represents a
6637 -- False list of ranges that can never be satisfied.
6639 function Is_True (R : RList) return Boolean;
6640 -- Returns True if R trivially represents the True predicate by having a
6641 -- single range from BLo to BHi.
6643 function Is_Type_Ref (N : Node_Id) return Boolean;
6644 pragma Inline (Is_Type_Ref);
6645 -- Returns if True if N is a reference to the type for the predicate in
6646 -- the expression (i.e. if it is an identifier whose Chars field matches
6647 -- the Nam given in the call).
6649 function Lo_Val (N : Node_Id) return Uint;
6650 -- Given static expression or static range from a Static_Predicate list,
6651 -- gets expression value or low bound of range.
6653 function Hi_Val (N : Node_Id) return Uint;
6654 -- Given static expression or static range from a Static_Predicate list,
6655 -- gets expression value of high bound of range.
6657 function Membership_Entry (N : Node_Id) return RList;
6658 -- Given a single membership entry (range, value, or subtype), returns
6659 -- the corresponding range list. Raises Static_Error if not static.
6661 function Membership_Entries (N : Node_Id) return RList;
6662 -- Given an element on an alternatives list of a membership operation,
6663 -- returns the range list corresponding to this entry and all following
6664 -- entries (i.e. returns the "or" of this list of values).
6666 function Stat_Pred (Typ : Entity_Id) return RList;
6667 -- Given a type, if it has a static predicate, then return the predicate
6668 -- as a range list, otherwise raise Non_Static.
6674 function "and" (Left : RList; Right : RList) return RList is
6676 -- First range of result
6678 SLeft : Nat := Left'First;
6679 -- Start of rest of left entries
6681 SRight : Nat := Right'First;
6682 -- Start of rest of right entries
6685 -- If either range is True, return the other
6687 if Is_True (Left) then
6689 elsif Is_True (Right) then
6693 -- If either range is False, return False
6695 if Is_False (Left) or else Is_False (Right) then
6699 -- Loop to remove entries at start that are disjoint, and thus just
6700 -- get discarded from the result entirely.
6703 -- If no operands left in either operand, result is false
6705 if SLeft > Left'Last or else SRight > Right'Last then
6708 -- Discard first left operand entry if disjoint with right
6710 elsif Left (SLeft).Hi < Right (SRight).Lo then
6713 -- Discard first right operand entry if disjoint with left
6715 elsif Right (SRight).Hi < Left (SLeft).Lo then
6716 SRight := SRight + 1;
6718 -- Otherwise we have an overlapping entry
6725 -- Now we have two non-null operands, and first entries overlap. The
6726 -- first entry in the result will be the overlapping part of these
6729 FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo),
6730 Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi));
6732 -- Now we can remove the entry that ended at a lower value, since its
6733 -- contribution is entirely contained in Fent.
6735 if Left (SLeft).Hi <= Right (SRight).Hi then
6738 SRight := SRight + 1;
6741 -- Compute result by concatenating this first entry with the "and" of
6742 -- the remaining parts of the left and right operands. Note that if
6743 -- either of these is empty, "and" will yield empty, so that we will
6744 -- end up with just Fent, which is what we want in that case.
6747 FEnt & (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last));
6754 function "not" (Right : RList) return RList is
6756 -- Return True if False range
6758 if Is_False (Right) then
6762 -- Return False if True range
6764 if Is_True (Right) then
6768 -- Here if not trivial case
6771 Result : RList (1 .. Right'Length + 1);
6772 -- May need one more entry for gap at beginning and end
6775 -- Number of entries stored in Result
6780 if Right (Right'First).Lo > TLo then
6782 Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1);
6785 -- Gaps between ranges
6787 for J in Right'First .. Right'Last - 1 loop
6790 REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1);
6795 if Right (Right'Last).Hi < THi then
6797 Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi);
6800 return Result (1 .. Count);
6808 function "or" (Left : RList; Right : RList) return RList is
6810 -- First range of result
6812 SLeft : Nat := Left'First;
6813 -- Start of rest of left entries
6815 SRight : Nat := Right'First;
6816 -- Start of rest of right entries
6819 -- If either range is True, return True
6821 if Is_True (Left) or else Is_True (Right) then
6825 -- If either range is False (empty), return the other
6827 if Is_False (Left) then
6829 elsif Is_False (Right) then
6833 -- Initialize result first entry from left or right operand depending
6834 -- on which starts with the lower range.
6836 if Left (SLeft).Lo < Right (SRight).Lo then
6837 FEnt := Left (SLeft);
6840 FEnt := Right (SRight);
6841 SRight := SRight + 1;
6844 -- This loop eats ranges from left and right operands that are
6845 -- contiguous with the first range we are gathering.
6848 -- Eat first entry in left operand if contiguous or overlapped by
6849 -- gathered first operand of result.
6851 if SLeft <= Left'Last
6852 and then Left (SLeft).Lo <= FEnt.Hi + 1
6854 FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi);
6857 -- Eat first entry in right operand if contiguous or overlapped by
6858 -- gathered right operand of result.
6860 elsif SRight <= Right'Last
6861 and then Right (SRight).Lo <= FEnt.Hi + 1
6863 FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi);
6864 SRight := SRight + 1;
6866 -- All done if no more entries to eat
6873 -- Obtain result as the first entry we just computed, concatenated
6874 -- to the "or" of the remaining results (if one operand is empty,
6875 -- this will just concatenate with the other
6878 FEnt & (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last));
6885 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id is
6891 Low_Bound => Build_Val (Lo),
6892 High_Bound => Build_Val (Hi));
6893 Set_Etype (Result, Btyp);
6894 Set_Analyzed (Result);
6903 function Build_Val (V : Uint) return Node_Id is
6907 if Is_Enumeration_Type (Typ) then
6908 Result := Get_Enum_Lit_From_Pos (Typ, V, Loc);
6910 Result := Make_Integer_Literal (Loc, V);
6913 Set_Etype (Result, Btyp);
6914 Set_Is_Static_Expression (Result);
6915 Set_Analyzed (Result);
6923 function Get_RList (Exp : Node_Id) return RList is
6928 -- Static expression can only be true or false
6930 if Is_OK_Static_Expression (Exp) then
6934 if Expr_Value (Exp) = 0 then
6941 -- Otherwise test node type
6949 when N_Op_And | N_And_Then =>
6950 return Get_RList (Left_Opnd (Exp))
6952 Get_RList (Right_Opnd (Exp));
6956 when N_Op_Or | N_Or_Else =>
6957 return Get_RList (Left_Opnd (Exp))
6959 Get_RList (Right_Opnd (Exp));
6964 return not Get_RList (Right_Opnd (Exp));
6966 -- Comparisons of type with static value
6968 when N_Op_Compare =>
6970 -- Type is left operand
6972 if Is_Type_Ref (Left_Opnd (Exp))
6973 and then Is_OK_Static_Expression (Right_Opnd (Exp))
6975 Val := Expr_Value (Right_Opnd (Exp));
6977 -- Typ is right operand
6979 elsif Is_Type_Ref (Right_Opnd (Exp))
6980 and then Is_OK_Static_Expression (Left_Opnd (Exp))
6982 Val := Expr_Value (Left_Opnd (Exp));
6984 -- Invert sense of comparison
6987 when N_Op_Gt => Op := N_Op_Lt;
6988 when N_Op_Lt => Op := N_Op_Gt;
6989 when N_Op_Ge => Op := N_Op_Le;
6990 when N_Op_Le => Op := N_Op_Ge;
6991 when others => null;
6994 -- Other cases are non-static
7000 -- Construct range according to comparison operation
7004 return RList'(1 => REnt'(Val, Val));
7007 return RList'(1 => REnt'(Val, BHi));
7010 return RList'(1 => REnt'(Val + 1, BHi));
7013 return RList'(1 => REnt'(BLo, Val));
7016 return RList'(1 => REnt'(BLo, Val - 1));
7019 return RList'(REnt'(BLo, Val - 1),
7020 REnt'(Val + 1, BHi));
7023 raise Program_Error;
7029 if not Is_Type_Ref (Left_Opnd (Exp)) then
7033 if Present (Right_Opnd (Exp)) then
7034 return Membership_Entry (Right_Opnd (Exp));
7036 return Membership_Entries (First (Alternatives (Exp)));
7039 -- Negative membership (NOT IN)
7042 if not Is_Type_Ref (Left_Opnd (Exp)) then
7046 if Present (Right_Opnd (Exp)) then
7047 return not Membership_Entry (Right_Opnd (Exp));
7049 return not Membership_Entries (First (Alternatives (Exp)));
7052 -- Function call, may be call to static predicate
7054 when N_Function_Call =>
7055 if Is_Entity_Name (Name (Exp)) then
7057 Ent : constant Entity_Id := Entity (Name (Exp));
7059 if Is_Predicate_Function (Ent)
7061 Is_Predicate_Function_M (Ent)
7063 return Stat_Pred (Etype (First_Formal (Ent)));
7068 -- Other function call cases are non-static
7072 -- Qualified expression, dig out the expression
7074 when N_Qualified_Expression =>
7075 return Get_RList (Expression (Exp));
7080 return (Get_RList (Left_Opnd (Exp))
7081 and not Get_RList (Right_Opnd (Exp)))
7082 or (Get_RList (Right_Opnd (Exp))
7083 and not Get_RList (Left_Opnd (Exp)));
7085 -- Any other node type is non-static
7096 function Hi_Val (N : Node_Id) return Uint is
7098 if Is_Static_Expression (N) then
7099 return Expr_Value (N);
7101 pragma Assert (Nkind (N) = N_Range);
7102 return Expr_Value (High_Bound (N));
7110 function Is_False (R : RList) return Boolean is
7112 return R'Length = 0;
7119 function Is_True (R : RList) return Boolean is
7122 and then R (R'First).Lo = BLo
7123 and then R (R'First).Hi = BHi;
7130 function Is_Type_Ref (N : Node_Id) return Boolean is
7132 return Nkind (N) = N_Identifier and then Chars (N) = Nam;
7139 function Lo_Val (N : Node_Id) return Uint is
7141 if Is_Static_Expression (N) then
7142 return Expr_Value (N);
7144 pragma Assert (Nkind (N) = N_Range);
7145 return Expr_Value (Low_Bound (N));
7149 ------------------------
7150 -- Membership_Entries --
7151 ------------------------
7153 function Membership_Entries (N : Node_Id) return RList is
7155 if No (Next (N)) then
7156 return Membership_Entry (N);
7158 return Membership_Entry (N) or Membership_Entries (Next (N));
7160 end Membership_Entries;
7162 ----------------------
7163 -- Membership_Entry --
7164 ----------------------
7166 function Membership_Entry (N : Node_Id) return RList is
7174 if Nkind (N) = N_Range then
7175 if not Is_Static_Expression (Low_Bound (N))
7177 not Is_Static_Expression (High_Bound (N))
7181 SLo := Expr_Value (Low_Bound (N));
7182 SHi := Expr_Value (High_Bound (N));
7183 return RList'(1 => REnt'(SLo, SHi));
7186 -- Static expression case
7188 elsif Is_Static_Expression (N) then
7189 Val := Expr_Value (N);
7190 return RList'(1 => REnt'(Val, Val));
7192 -- Identifier (other than static expression) case
7194 else pragma Assert (Nkind (N) = N_Identifier);
7198 if Is_Type (Entity (N)) then
7200 -- If type has predicates, process them
7202 if Has_Predicates (Entity (N)) then
7203 return Stat_Pred (Entity (N));
7205 -- For static subtype without predicates, get range
7207 elsif Is_Static_Subtype (Entity (N)) then
7208 SLo := Expr_Value (Type_Low_Bound (Entity (N)));
7209 SHi := Expr_Value (Type_High_Bound (Entity (N)));
7210 return RList'(1 => REnt'(SLo, SHi));
7212 -- Any other type makes us non-static
7218 -- Any other kind of identifier in predicate (e.g. a non-static
7219 -- expression value) means this is not a static predicate.
7225 end Membership_Entry;
7231 function Stat_Pred (Typ : Entity_Id) return RList is
7233 -- Not static if type does not have static predicates
7235 if not Has_Predicates (Typ) or else No (Static_Predicate (Typ)) then
7239 -- Otherwise we convert the predicate list to a range list
7242 Result : RList (1 .. List_Length (Static_Predicate (Typ)));
7246 P := First (Static_Predicate (Typ));
7247 for J in Result'Range loop
7248 Result (J) := REnt'(Lo_Val (P), Hi_Val (P));
7256 -- Start of processing for Build_Static_Predicate
7259 -- Now analyze the expression to see if it is a static predicate
7262 Ranges : constant RList := Get_RList (Expr);
7263 -- Range list from expression if it is static
7268 -- Convert range list into a form for the static predicate. In the
7269 -- Ranges array, we just have raw ranges, these must be converted
7270 -- to properly typed and analyzed static expressions or range nodes.
7272 -- Note: here we limit ranges to the ranges of the subtype, so that
7273 -- a predicate is always false for values outside the subtype. That
7274 -- seems fine, such values are invalid anyway, and considering them
7275 -- to fail the predicate seems allowed and friendly, and furthermore
7276 -- simplifies processing for case statements and loops.
7280 for J in Ranges'Range loop
7282 Lo : Uint := Ranges (J).Lo;
7283 Hi : Uint := Ranges (J).Hi;
7286 -- Ignore completely out of range entry
7288 if Hi < TLo or else Lo > THi then
7291 -- Otherwise process entry
7294 -- Adjust out of range value to subtype range
7304 -- Convert range into required form
7306 Append_To (Plist, Build_Range (Lo, Hi));
7311 -- Processing was successful and all entries were static, so now we
7312 -- can store the result as the predicate list.
7314 Set_Static_Predicate (Typ, Plist);
7316 -- The processing for static predicates put the expression into
7317 -- canonical form as a series of ranges. It also eliminated
7318 -- duplicates and collapsed and combined ranges. We might as well
7319 -- replace the alternatives list of the right operand of the
7320 -- membership test with the static predicate list, which will
7321 -- usually be more efficient.
7324 New_Alts : constant List_Id := New_List;
7329 Old_Node := First (Plist);
7330 while Present (Old_Node) loop
7331 New_Node := New_Copy (Old_Node);
7333 if Nkind (New_Node) = N_Range then
7334 Set_Low_Bound (New_Node, New_Copy (Low_Bound (Old_Node)));
7335 Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node)));
7338 Append_To (New_Alts, New_Node);
7342 -- If empty list, replace by False
7344 if Is_Empty_List (New_Alts) then
7345 Rewrite (Expr, New_Occurrence_Of (Standard_False, Loc));
7347 -- Else replace by set membership test
7352 Left_Opnd => Make_Identifier (Loc, Nam),
7353 Right_Opnd => Empty,
7354 Alternatives => New_Alts));
7356 -- Resolve new expression in function context
7358 Install_Formals (Predicate_Function (Typ));
7359 Push_Scope (Predicate_Function (Typ));
7360 Analyze_And_Resolve (Expr, Standard_Boolean);
7366 -- If non-static, return doing nothing
7371 end Build_Static_Predicate;
7373 -----------------------------------------
7374 -- Check_Aspect_At_End_Of_Declarations --
7375 -----------------------------------------
7377 procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id) is
7378 Ent : constant Entity_Id := Entity (ASN);
7379 Ident : constant Node_Id := Identifier (ASN);
7380 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
7382 End_Decl_Expr : constant Node_Id := Entity (Ident);
7383 -- Expression to be analyzed at end of declarations
7385 Freeze_Expr : constant Node_Id := Expression (ASN);
7386 -- Expression from call to Check_Aspect_At_Freeze_Point
7388 T : constant Entity_Id := Etype (Freeze_Expr);
7389 -- Type required for preanalyze call
7392 -- Set False if error
7394 -- On entry to this procedure, Entity (Ident) contains a copy of the
7395 -- original expression from the aspect, saved for this purpose, and
7396 -- but Expression (Ident) is a preanalyzed copy of the expression,
7397 -- preanalyzed just after the freeze point.
7399 procedure Check_Overloaded_Name;
7400 -- For aspects whose expression is simply a name, this routine checks if
7401 -- the name is overloaded or not. If so, it verifies there is an
7402 -- interpretation that matches the entity obtained at the freeze point,
7403 -- otherwise the compiler complains.
7405 ---------------------------
7406 -- Check_Overloaded_Name --
7407 ---------------------------
7409 procedure Check_Overloaded_Name is
7411 if not Is_Overloaded (End_Decl_Expr) then
7412 Err := Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
7418 Index : Interp_Index;
7422 Get_First_Interp (End_Decl_Expr, Index, It);
7423 while Present (It.Typ) loop
7424 if It.Nam = Entity (Freeze_Expr) then
7429 Get_Next_Interp (Index, It);
7433 end Check_Overloaded_Name;
7435 -- Start of processing for Check_Aspect_At_End_Of_Declarations
7438 -- Case of aspects Dimension, Dimension_System and Synchronization
7440 if A_Id = Aspect_Synchronization then
7443 -- Case of stream attributes, just have to compare entities. However,
7444 -- the expression is just a name (possibly overloaded), and there may
7445 -- be stream operations declared for unrelated types, so we just need
7446 -- to verify that one of these interpretations is the one available at
7447 -- at the freeze point.
7449 elsif A_Id = Aspect_Input or else
7450 A_Id = Aspect_Output or else
7451 A_Id = Aspect_Read or else
7454 Analyze (End_Decl_Expr);
7455 Check_Overloaded_Name;
7457 elsif A_Id = Aspect_Variable_Indexing or else
7458 A_Id = Aspect_Constant_Indexing or else
7459 A_Id = Aspect_Default_Iterator or else
7460 A_Id = Aspect_Iterator_Element
7462 -- Make type unfrozen before analysis, to prevent spurious errors
7463 -- about late attributes.
7465 Set_Is_Frozen (Ent, False);
7466 Analyze (End_Decl_Expr);
7467 Set_Is_Frozen (Ent, True);
7469 -- If the end of declarations comes before any other freeze
7470 -- point, the Freeze_Expr is not analyzed: no check needed.
7472 if Analyzed (Freeze_Expr) and then not In_Instance then
7473 Check_Overloaded_Name;
7481 -- In a generic context the aspect expressions have not been
7482 -- preanalyzed, so do it now. There are no conformance checks
7483 -- to perform in this case.
7486 Check_Aspect_At_Freeze_Point (ASN);
7489 -- The default values attributes may be defined in the private part,
7490 -- and the analysis of the expression may take place when only the
7491 -- partial view is visible. The expression must be scalar, so use
7492 -- the full view to resolve.
7494 elsif (A_Id = Aspect_Default_Value
7496 A_Id = Aspect_Default_Component_Value)
7497 and then Is_Private_Type (T)
7499 Preanalyze_Spec_Expression (End_Decl_Expr, Full_View (T));
7501 Preanalyze_Spec_Expression (End_Decl_Expr, T);
7504 Err := not Fully_Conformant_Expressions (End_Decl_Expr, Freeze_Expr);
7507 -- Output error message if error
7511 ("visibility of aspect for& changes after freeze point",
7514 ("info: & is frozen here, aspects evaluated at this point??",
7515 Freeze_Node (Ent), Ent);
7517 end Check_Aspect_At_End_Of_Declarations;
7519 ----------------------------------
7520 -- Check_Aspect_At_Freeze_Point --
7521 ----------------------------------
7523 procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id) is
7524 Ident : constant Node_Id := Identifier (ASN);
7525 -- Identifier (use Entity field to save expression)
7527 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
7529 T : Entity_Id := Empty;
7530 -- Type required for preanalyze call
7533 -- On entry to this procedure, Entity (Ident) contains a copy of the
7534 -- original expression from the aspect, saved for this purpose.
7536 -- On exit from this procedure Entity (Ident) is unchanged, still
7537 -- containing that copy, but Expression (Ident) is a preanalyzed copy
7538 -- of the expression, preanalyzed just after the freeze point.
7540 -- Make a copy of the expression to be preanalyzed
7542 Set_Expression (ASN, New_Copy_Tree (Entity (Ident)));
7544 -- Find type for preanalyze call
7548 -- No_Aspect should be impossible
7551 raise Program_Error;
7553 -- Aspects taking an optional boolean argument
7555 when Boolean_Aspects |
7556 Library_Unit_Aspects =>
7558 T := Standard_Boolean;
7560 -- Aspects corresponding to attribute definition clauses
7562 when Aspect_Address =>
7563 T := RTE (RE_Address);
7565 when Aspect_Attach_Handler =>
7566 T := RTE (RE_Interrupt_ID);
7568 when Aspect_Bit_Order | Aspect_Scalar_Storage_Order =>
7569 T := RTE (RE_Bit_Order);
7571 when Aspect_Convention =>
7575 T := RTE (RE_CPU_Range);
7577 -- Default_Component_Value is resolved with the component type
7579 when Aspect_Default_Component_Value =>
7580 T := Component_Type (Entity (ASN));
7582 -- Default_Value is resolved with the type entity in question
7584 when Aspect_Default_Value =>
7587 -- Depends is a delayed aspect because it mentiones names first
7588 -- introduced by aspect Global which is already delayed. There is
7589 -- no action to be taken with respect to the aspect itself as the
7590 -- analysis is done by the corresponding pragma.
7592 when Aspect_Depends =>
7595 when Aspect_Dispatching_Domain =>
7596 T := RTE (RE_Dispatching_Domain);
7598 when Aspect_External_Tag =>
7599 T := Standard_String;
7601 when Aspect_External_Name =>
7602 T := Standard_String;
7604 -- Global is a delayed aspect because it may reference names that
7605 -- have not been declared yet. There is no action to be taken with
7606 -- respect to the aspect itself as the reference checking is done
7607 -- on the corresponding pragma.
7609 when Aspect_Global =>
7612 when Aspect_Link_Name =>
7613 T := Standard_String;
7615 when Aspect_Priority | Aspect_Interrupt_Priority =>
7616 T := Standard_Integer;
7618 when Aspect_Relative_Deadline =>
7619 T := RTE (RE_Time_Span);
7621 when Aspect_Small =>
7622 T := Universal_Real;
7624 -- For a simple storage pool, we have to retrieve the type of the
7625 -- pool object associated with the aspect's corresponding attribute
7626 -- definition clause.
7628 when Aspect_Simple_Storage_Pool =>
7629 T := Etype (Expression (Aspect_Rep_Item (ASN)));
7631 when Aspect_Storage_Pool =>
7632 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
7634 when Aspect_Alignment |
7635 Aspect_Component_Size |
7636 Aspect_Machine_Radix |
7637 Aspect_Object_Size |
7639 Aspect_Storage_Size |
7640 Aspect_Stream_Size |
7641 Aspect_Value_Size =>
7644 when Aspect_Synchronization =>
7647 -- Special case, the expression of these aspects is just an entity
7648 -- that does not need any resolution, so just analyze.
7657 Analyze (Expression (ASN));
7660 -- Same for Iterator aspects, where the expression is a function
7661 -- name. Legality rules are checked separately.
7663 when Aspect_Constant_Indexing |
7664 Aspect_Default_Iterator |
7665 Aspect_Iterator_Element |
7666 Aspect_Variable_Indexing =>
7667 Analyze (Expression (ASN));
7670 -- Invariant/Predicate take boolean expressions
7672 when Aspect_Dynamic_Predicate |
7675 Aspect_Static_Predicate |
7676 Aspect_Type_Invariant =>
7677 T := Standard_Boolean;
7679 -- Here is the list of aspects that don't require delay analysis
7681 when Aspect_Abstract_State |
7682 Aspect_Contract_Cases |
7684 Aspect_Dimension_System |
7685 Aspect_Implicit_Dereference |
7687 Aspect_Postcondition |
7689 Aspect_Precondition |
7692 raise Program_Error;
7696 -- Do the preanalyze call
7698 Preanalyze_Spec_Expression (Expression (ASN), T);
7699 end Check_Aspect_At_Freeze_Point;
7701 -----------------------------------
7702 -- Check_Constant_Address_Clause --
7703 -----------------------------------
7705 procedure Check_Constant_Address_Clause
7709 procedure Check_At_Constant_Address (Nod : Node_Id);
7710 -- Checks that the given node N represents a name whose 'Address is
7711 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
7712 -- address value is the same at the point of declaration of U_Ent and at
7713 -- the time of elaboration of the address clause.
7715 procedure Check_Expr_Constants (Nod : Node_Id);
7716 -- Checks that Nod meets the requirements for a constant address clause
7717 -- in the sense of the enclosing procedure.
7719 procedure Check_List_Constants (Lst : List_Id);
7720 -- Check that all elements of list Lst meet the requirements for a
7721 -- constant address clause in the sense of the enclosing procedure.
7723 -------------------------------
7724 -- Check_At_Constant_Address --
7725 -------------------------------
7727 procedure Check_At_Constant_Address (Nod : Node_Id) is
7729 if Is_Entity_Name (Nod) then
7730 if Present (Address_Clause (Entity ((Nod)))) then
7732 ("invalid address clause for initialized object &!",
7735 ("address for& cannot" &
7736 " depend on another address clause! (RM 13.1(22))!",
7739 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
7740 and then Sloc (U_Ent) < Sloc (Entity (Nod))
7743 ("invalid address clause for initialized object &!",
7745 Error_Msg_Node_2 := U_Ent;
7747 ("\& must be defined before & (RM 13.1(22))!",
7751 elsif Nkind (Nod) = N_Selected_Component then
7753 T : constant Entity_Id := Etype (Prefix (Nod));
7756 if (Is_Record_Type (T)
7757 and then Has_Discriminants (T))
7760 and then Is_Record_Type (Designated_Type (T))
7761 and then Has_Discriminants (Designated_Type (T)))
7764 ("invalid address clause for initialized object &!",
7767 ("\address cannot depend on component" &
7768 " of discriminated record (RM 13.1(22))!",
7771 Check_At_Constant_Address (Prefix (Nod));
7775 elsif Nkind (Nod) = N_Indexed_Component then
7776 Check_At_Constant_Address (Prefix (Nod));
7777 Check_List_Constants (Expressions (Nod));
7780 Check_Expr_Constants (Nod);
7782 end Check_At_Constant_Address;
7784 --------------------------
7785 -- Check_Expr_Constants --
7786 --------------------------
7788 procedure Check_Expr_Constants (Nod : Node_Id) is
7789 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
7790 Ent : Entity_Id := Empty;
7793 if Nkind (Nod) in N_Has_Etype
7794 and then Etype (Nod) = Any_Type
7800 when N_Empty | N_Error =>
7803 when N_Identifier | N_Expanded_Name =>
7804 Ent := Entity (Nod);
7806 -- We need to look at the original node if it is different
7807 -- from the node, since we may have rewritten things and
7808 -- substituted an identifier representing the rewrite.
7810 if Original_Node (Nod) /= Nod then
7811 Check_Expr_Constants (Original_Node (Nod));
7813 -- If the node is an object declaration without initial
7814 -- value, some code has been expanded, and the expression
7815 -- is not constant, even if the constituents might be
7816 -- acceptable, as in A'Address + offset.
7818 if Ekind (Ent) = E_Variable
7820 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
7822 No (Expression (Declaration_Node (Ent)))
7825 ("invalid address clause for initialized object &!",
7828 -- If entity is constant, it may be the result of expanding
7829 -- a check. We must verify that its declaration appears
7830 -- before the object in question, else we also reject the
7833 elsif Ekind (Ent) = E_Constant
7834 and then In_Same_Source_Unit (Ent, U_Ent)
7835 and then Sloc (Ent) > Loc_U_Ent
7838 ("invalid address clause for initialized object &!",
7845 -- Otherwise look at the identifier and see if it is OK
7847 if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
7848 or else Is_Type (Ent)
7853 Ekind (Ent) = E_Constant
7855 Ekind (Ent) = E_In_Parameter
7857 -- This is the case where we must have Ent defined before
7858 -- U_Ent. Clearly if they are in different units this
7859 -- requirement is met since the unit containing Ent is
7860 -- already processed.
7862 if not In_Same_Source_Unit (Ent, U_Ent) then
7865 -- Otherwise location of Ent must be before the location
7866 -- of U_Ent, that's what prior defined means.
7868 elsif Sloc (Ent) < Loc_U_Ent then
7873 ("invalid address clause for initialized object &!",
7875 Error_Msg_Node_2 := U_Ent;
7877 ("\& must be defined before & (RM 13.1(22))!",
7881 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
7882 Check_Expr_Constants (Original_Node (Nod));
7886 ("invalid address clause for initialized object &!",
7889 if Comes_From_Source (Ent) then
7891 ("\reference to variable& not allowed"
7892 & " (RM 13.1(22))!", Nod, Ent);
7895 ("non-static expression not allowed"
7896 & " (RM 13.1(22))!", Nod);
7900 when N_Integer_Literal =>
7902 -- If this is a rewritten unchecked conversion, in a system
7903 -- where Address is an integer type, always use the base type
7904 -- for a literal value. This is user-friendly and prevents
7905 -- order-of-elaboration issues with instances of unchecked
7908 if Nkind (Original_Node (Nod)) = N_Function_Call then
7909 Set_Etype (Nod, Base_Type (Etype (Nod)));
7912 when N_Real_Literal |
7914 N_Character_Literal =>
7918 Check_Expr_Constants (Low_Bound (Nod));
7919 Check_Expr_Constants (High_Bound (Nod));
7921 when N_Explicit_Dereference =>
7922 Check_Expr_Constants (Prefix (Nod));
7924 when N_Indexed_Component =>
7925 Check_Expr_Constants (Prefix (Nod));
7926 Check_List_Constants (Expressions (Nod));
7929 Check_Expr_Constants (Prefix (Nod));
7930 Check_Expr_Constants (Discrete_Range (Nod));
7932 when N_Selected_Component =>
7933 Check_Expr_Constants (Prefix (Nod));
7935 when N_Attribute_Reference =>
7936 if Nam_In (Attribute_Name (Nod), Name_Address,
7938 Name_Unchecked_Access,
7939 Name_Unrestricted_Access)
7941 Check_At_Constant_Address (Prefix (Nod));
7944 Check_Expr_Constants (Prefix (Nod));
7945 Check_List_Constants (Expressions (Nod));
7949 Check_List_Constants (Component_Associations (Nod));
7950 Check_List_Constants (Expressions (Nod));
7952 when N_Component_Association =>
7953 Check_Expr_Constants (Expression (Nod));
7955 when N_Extension_Aggregate =>
7956 Check_Expr_Constants (Ancestor_Part (Nod));
7957 Check_List_Constants (Component_Associations (Nod));
7958 Check_List_Constants (Expressions (Nod));
7963 when N_Binary_Op | N_Short_Circuit | N_Membership_Test =>
7964 Check_Expr_Constants (Left_Opnd (Nod));
7965 Check_Expr_Constants (Right_Opnd (Nod));
7968 Check_Expr_Constants (Right_Opnd (Nod));
7970 when N_Type_Conversion |
7971 N_Qualified_Expression |
7973 N_Unchecked_Type_Conversion =>
7974 Check_Expr_Constants (Expression (Nod));
7976 when N_Function_Call =>
7977 if not Is_Pure (Entity (Name (Nod))) then
7979 ("invalid address clause for initialized object &!",
7983 ("\function & is not pure (RM 13.1(22))!",
7984 Nod, Entity (Name (Nod)));
7987 Check_List_Constants (Parameter_Associations (Nod));
7990 when N_Parameter_Association =>
7991 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
7995 ("invalid address clause for initialized object &!",
7998 ("\must be constant defined before& (RM 13.1(22))!",
8001 end Check_Expr_Constants;
8003 --------------------------
8004 -- Check_List_Constants --
8005 --------------------------
8007 procedure Check_List_Constants (Lst : List_Id) is
8011 if Present (Lst) then
8012 Nod1 := First (Lst);
8013 while Present (Nod1) loop
8014 Check_Expr_Constants (Nod1);
8018 end Check_List_Constants;
8020 -- Start of processing for Check_Constant_Address_Clause
8023 -- If rep_clauses are to be ignored, no need for legality checks. In
8024 -- particular, no need to pester user about rep clauses that violate
8025 -- the rule on constant addresses, given that these clauses will be
8026 -- removed by Freeze before they reach the back end.
8028 if not Ignore_Rep_Clauses then
8029 Check_Expr_Constants (Expr);
8031 end Check_Constant_Address_Clause;
8033 ----------------------------------------
8034 -- Check_Record_Representation_Clause --
8035 ----------------------------------------
8037 procedure Check_Record_Representation_Clause (N : Node_Id) is
8038 Loc : constant Source_Ptr := Sloc (N);
8039 Ident : constant Node_Id := Identifier (N);
8040 Rectype : Entity_Id;
8045 Hbit : Uint := Uint_0;
8049 Max_Bit_So_Far : Uint;
8050 -- Records the maximum bit position so far. If all field positions
8051 -- are monotonically increasing, then we can skip the circuit for
8052 -- checking for overlap, since no overlap is possible.
8054 Tagged_Parent : Entity_Id := Empty;
8055 -- This is set in the case of a derived tagged type for which we have
8056 -- Is_Fully_Repped_Tagged_Type True (indicating that all components are
8057 -- positioned by record representation clauses). In this case we must
8058 -- check for overlap between components of this tagged type, and the
8059 -- components of its parent. Tagged_Parent will point to this parent
8060 -- type. For all other cases Tagged_Parent is left set to Empty.
8062 Parent_Last_Bit : Uint;
8063 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
8064 -- last bit position for any field in the parent type. We only need to
8065 -- check overlap for fields starting below this point.
8067 Overlap_Check_Required : Boolean;
8068 -- Used to keep track of whether or not an overlap check is required
8070 Overlap_Detected : Boolean := False;
8071 -- Set True if an overlap is detected
8073 Ccount : Natural := 0;
8074 -- Number of component clauses in record rep clause
8076 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
8077 -- Given two entities for record components or discriminants, checks
8078 -- if they have overlapping component clauses and issues errors if so.
8080 procedure Find_Component;
8081 -- Finds component entity corresponding to current component clause (in
8082 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
8083 -- start/stop bits for the field. If there is no matching component or
8084 -- if the matching component does not have a component clause, then
8085 -- that's an error and Comp is set to Empty, but no error message is
8086 -- issued, since the message was already given. Comp is also set to
8087 -- Empty if the current "component clause" is in fact a pragma.
8089 -----------------------------
8090 -- Check_Component_Overlap --
8091 -----------------------------
8093 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
8094 CC1 : constant Node_Id := Component_Clause (C1_Ent);
8095 CC2 : constant Node_Id := Component_Clause (C2_Ent);
8098 if Present (CC1) and then Present (CC2) then
8100 -- Exclude odd case where we have two tag components in the same
8101 -- record, both at location zero. This seems a bit strange, but
8102 -- it seems to happen in some circumstances, perhaps on an error.
8104 if Nam_In (Chars (C1_Ent), Name_uTag, Name_uTag) then
8108 -- Here we check if the two fields overlap
8111 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
8112 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
8113 E1 : constant Uint := S1 + Esize (C1_Ent);
8114 E2 : constant Uint := S2 + Esize (C2_Ent);
8117 if E2 <= S1 or else E1 <= S2 then
8120 Error_Msg_Node_2 := Component_Name (CC2);
8121 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
8122 Error_Msg_Node_1 := Component_Name (CC1);
8124 ("component& overlaps & #", Component_Name (CC1));
8125 Overlap_Detected := True;
8129 end Check_Component_Overlap;
8131 --------------------
8132 -- Find_Component --
8133 --------------------
8135 procedure Find_Component is
8137 procedure Search_Component (R : Entity_Id);
8138 -- Search components of R for a match. If found, Comp is set
8140 ----------------------
8141 -- Search_Component --
8142 ----------------------
8144 procedure Search_Component (R : Entity_Id) is
8146 Comp := First_Component_Or_Discriminant (R);
8147 while Present (Comp) loop
8149 -- Ignore error of attribute name for component name (we
8150 -- already gave an error message for this, so no need to
8153 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
8156 exit when Chars (Comp) = Chars (Component_Name (CC));
8159 Next_Component_Or_Discriminant (Comp);
8161 end Search_Component;
8163 -- Start of processing for Find_Component
8166 -- Return with Comp set to Empty if we have a pragma
8168 if Nkind (CC) = N_Pragma then
8173 -- Search current record for matching component
8175 Search_Component (Rectype);
8177 -- If not found, maybe component of base type discriminant that is
8178 -- absent from statically constrained first subtype.
8181 Search_Component (Base_Type (Rectype));
8184 -- If no component, or the component does not reference the component
8185 -- clause in question, then there was some previous error for which
8186 -- we already gave a message, so just return with Comp Empty.
8188 if No (Comp) or else Component_Clause (Comp) /= CC then
8189 Check_Error_Detected;
8192 -- Normal case where we have a component clause
8195 Fbit := Component_Bit_Offset (Comp);
8196 Lbit := Fbit + Esize (Comp) - 1;
8200 -- Start of processing for Check_Record_Representation_Clause
8204 Rectype := Entity (Ident);
8206 if Rectype = Any_Type then
8209 Rectype := Underlying_Type (Rectype);
8212 -- See if we have a fully repped derived tagged type
8215 PS : constant Entity_Id := Parent_Subtype (Rectype);
8218 if Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
8219 Tagged_Parent := PS;
8221 -- Find maximum bit of any component of the parent type
8223 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
8224 Pcomp := First_Entity (Tagged_Parent);
8225 while Present (Pcomp) loop
8226 if Ekind_In (Pcomp, E_Discriminant, E_Component) then
8227 if Component_Bit_Offset (Pcomp) /= No_Uint
8228 and then Known_Static_Esize (Pcomp)
8233 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
8236 Next_Entity (Pcomp);
8242 -- All done if no component clauses
8244 CC := First (Component_Clauses (N));
8250 -- If a tag is present, then create a component clause that places it
8251 -- at the start of the record (otherwise gigi may place it after other
8252 -- fields that have rep clauses).
8254 Fent := First_Entity (Rectype);
8256 if Nkind (Fent) = N_Defining_Identifier
8257 and then Chars (Fent) = Name_uTag
8259 Set_Component_Bit_Offset (Fent, Uint_0);
8260 Set_Normalized_Position (Fent, Uint_0);
8261 Set_Normalized_First_Bit (Fent, Uint_0);
8262 Set_Normalized_Position_Max (Fent, Uint_0);
8263 Init_Esize (Fent, System_Address_Size);
8265 Set_Component_Clause (Fent,
8266 Make_Component_Clause (Loc,
8267 Component_Name => Make_Identifier (Loc, Name_uTag),
8269 Position => Make_Integer_Literal (Loc, Uint_0),
8270 First_Bit => Make_Integer_Literal (Loc, Uint_0),
8272 Make_Integer_Literal (Loc,
8273 UI_From_Int (System_Address_Size))));
8275 Ccount := Ccount + 1;
8278 Max_Bit_So_Far := Uint_Minus_1;
8279 Overlap_Check_Required := False;
8281 -- Process the component clauses
8283 while Present (CC) loop
8286 if Present (Comp) then
8287 Ccount := Ccount + 1;
8289 -- We need a full overlap check if record positions non-monotonic
8291 if Fbit <= Max_Bit_So_Far then
8292 Overlap_Check_Required := True;
8295 Max_Bit_So_Far := Lbit;
8297 -- Check bit position out of range of specified size
8299 if Has_Size_Clause (Rectype)
8300 and then RM_Size (Rectype) <= Lbit
8303 ("bit number out of range of specified size",
8306 -- Check for overlap with tag component
8309 if Is_Tagged_Type (Rectype)
8310 and then Fbit < System_Address_Size
8313 ("component overlaps tag field of&",
8314 Component_Name (CC), Rectype);
8315 Overlap_Detected := True;
8323 -- Check parent overlap if component might overlap parent field
8325 if Present (Tagged_Parent) and then Fbit <= Parent_Last_Bit then
8326 Pcomp := First_Component_Or_Discriminant (Tagged_Parent);
8327 while Present (Pcomp) loop
8328 if not Is_Tag (Pcomp)
8329 and then Chars (Pcomp) /= Name_uParent
8331 Check_Component_Overlap (Comp, Pcomp);
8334 Next_Component_Or_Discriminant (Pcomp);
8342 -- Now that we have processed all the component clauses, check for
8343 -- overlap. We have to leave this till last, since the components can
8344 -- appear in any arbitrary order in the representation clause.
8346 -- We do not need this check if all specified ranges were monotonic,
8347 -- as recorded by Overlap_Check_Required being False at this stage.
8349 -- This first section checks if there are any overlapping entries at
8350 -- all. It does this by sorting all entries and then seeing if there are
8351 -- any overlaps. If there are none, then that is decisive, but if there
8352 -- are overlaps, they may still be OK (they may result from fields in
8353 -- different variants).
8355 if Overlap_Check_Required then
8356 Overlap_Check1 : declare
8358 OC_Fbit : array (0 .. Ccount) of Uint;
8359 -- First-bit values for component clauses, the value is the offset
8360 -- of the first bit of the field from start of record. The zero
8361 -- entry is for use in sorting.
8363 OC_Lbit : array (0 .. Ccount) of Uint;
8364 -- Last-bit values for component clauses, the value is the offset
8365 -- of the last bit of the field from start of record. The zero
8366 -- entry is for use in sorting.
8368 OC_Count : Natural := 0;
8369 -- Count of entries in OC_Fbit and OC_Lbit
8371 function OC_Lt (Op1, Op2 : Natural) return Boolean;
8372 -- Compare routine for Sort
8374 procedure OC_Move (From : Natural; To : Natural);
8375 -- Move routine for Sort
8377 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
8383 function OC_Lt (Op1, Op2 : Natural) return Boolean is
8385 return OC_Fbit (Op1) < OC_Fbit (Op2);
8392 procedure OC_Move (From : Natural; To : Natural) is
8394 OC_Fbit (To) := OC_Fbit (From);
8395 OC_Lbit (To) := OC_Lbit (From);
8398 -- Start of processing for Overlap_Check
8401 CC := First (Component_Clauses (N));
8402 while Present (CC) loop
8404 -- Exclude component clause already marked in error
8406 if not Error_Posted (CC) then
8409 if Present (Comp) then
8410 OC_Count := OC_Count + 1;
8411 OC_Fbit (OC_Count) := Fbit;
8412 OC_Lbit (OC_Count) := Lbit;
8419 Sorting.Sort (OC_Count);
8421 Overlap_Check_Required := False;
8422 for J in 1 .. OC_Count - 1 loop
8423 if OC_Lbit (J) >= OC_Fbit (J + 1) then
8424 Overlap_Check_Required := True;
8431 -- If Overlap_Check_Required is still True, then we have to do the full
8432 -- scale overlap check, since we have at least two fields that do
8433 -- overlap, and we need to know if that is OK since they are in
8434 -- different variant, or whether we have a definite problem.
8436 if Overlap_Check_Required then
8437 Overlap_Check2 : declare
8438 C1_Ent, C2_Ent : Entity_Id;
8439 -- Entities of components being checked for overlap
8442 -- Component_List node whose Component_Items are being checked
8445 -- Component declaration for component being checked
8448 C1_Ent := First_Entity (Base_Type (Rectype));
8450 -- Loop through all components in record. For each component check
8451 -- for overlap with any of the preceding elements on the component
8452 -- list containing the component and also, if the component is in
8453 -- a variant, check against components outside the case structure.
8454 -- This latter test is repeated recursively up the variant tree.
8456 Main_Component_Loop : while Present (C1_Ent) loop
8457 if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
8458 goto Continue_Main_Component_Loop;
8461 -- Skip overlap check if entity has no declaration node. This
8462 -- happens with discriminants in constrained derived types.
8463 -- Possibly we are missing some checks as a result, but that
8464 -- does not seem terribly serious.
8466 if No (Declaration_Node (C1_Ent)) then
8467 goto Continue_Main_Component_Loop;
8470 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
8472 -- Loop through component lists that need checking. Check the
8473 -- current component list and all lists in variants above us.
8475 Component_List_Loop : loop
8477 -- If derived type definition, go to full declaration
8478 -- If at outer level, check discriminants if there are any.
8480 if Nkind (Clist) = N_Derived_Type_Definition then
8481 Clist := Parent (Clist);
8484 -- Outer level of record definition, check discriminants
8486 if Nkind_In (Clist, N_Full_Type_Declaration,
8487 N_Private_Type_Declaration)
8489 if Has_Discriminants (Defining_Identifier (Clist)) then
8491 First_Discriminant (Defining_Identifier (Clist));
8492 while Present (C2_Ent) loop
8493 exit when C1_Ent = C2_Ent;
8494 Check_Component_Overlap (C1_Ent, C2_Ent);
8495 Next_Discriminant (C2_Ent);
8499 -- Record extension case
8501 elsif Nkind (Clist) = N_Derived_Type_Definition then
8504 -- Otherwise check one component list
8507 Citem := First (Component_Items (Clist));
8508 while Present (Citem) loop
8509 if Nkind (Citem) = N_Component_Declaration then
8510 C2_Ent := Defining_Identifier (Citem);
8511 exit when C1_Ent = C2_Ent;
8512 Check_Component_Overlap (C1_Ent, C2_Ent);
8519 -- Check for variants above us (the parent of the Clist can
8520 -- be a variant, in which case its parent is a variant part,
8521 -- and the parent of the variant part is a component list
8522 -- whose components must all be checked against the current
8523 -- component for overlap).
8525 if Nkind (Parent (Clist)) = N_Variant then
8526 Clist := Parent (Parent (Parent (Clist)));
8528 -- Check for possible discriminant part in record, this
8529 -- is treated essentially as another level in the
8530 -- recursion. For this case the parent of the component
8531 -- list is the record definition, and its parent is the
8532 -- full type declaration containing the discriminant
8535 elsif Nkind (Parent (Clist)) = N_Record_Definition then
8536 Clist := Parent (Parent ((Clist)));
8538 -- If neither of these two cases, we are at the top of
8542 exit Component_List_Loop;
8544 end loop Component_List_Loop;
8546 <<Continue_Main_Component_Loop>>
8547 Next_Entity (C1_Ent);
8549 end loop Main_Component_Loop;
8553 -- The following circuit deals with warning on record holes (gaps). We
8554 -- skip this check if overlap was detected, since it makes sense for the
8555 -- programmer to fix this illegality before worrying about warnings.
8557 if not Overlap_Detected and Warn_On_Record_Holes then
8558 Record_Hole_Check : declare
8559 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
8560 -- Full declaration of record type
8562 procedure Check_Component_List
8566 -- Check component list CL for holes. The starting bit should be
8567 -- Sbit. which is zero for the main record component list and set
8568 -- appropriately for recursive calls for variants. DS is set to
8569 -- a list of discriminant specifications to be included in the
8570 -- consideration of components. It is No_List if none to consider.
8572 --------------------------
8573 -- Check_Component_List --
8574 --------------------------
8576 procedure Check_Component_List
8584 Compl := Integer (List_Length (Component_Items (CL)));
8586 if DS /= No_List then
8587 Compl := Compl + Integer (List_Length (DS));
8591 Comps : array (Natural range 0 .. Compl) of Entity_Id;
8592 -- Gather components (zero entry is for sort routine)
8594 Ncomps : Natural := 0;
8595 -- Number of entries stored in Comps (starting at Comps (1))
8598 -- One component item or discriminant specification
8601 -- Starting bit for next component
8609 function Lt (Op1, Op2 : Natural) return Boolean;
8610 -- Compare routine for Sort
8612 procedure Move (From : Natural; To : Natural);
8613 -- Move routine for Sort
8615 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
8621 function Lt (Op1, Op2 : Natural) return Boolean is
8623 return Component_Bit_Offset (Comps (Op1))
8625 Component_Bit_Offset (Comps (Op2));
8632 procedure Move (From : Natural; To : Natural) is
8634 Comps (To) := Comps (From);
8638 -- Gather discriminants into Comp
8640 if DS /= No_List then
8641 Citem := First (DS);
8642 while Present (Citem) loop
8643 if Nkind (Citem) = N_Discriminant_Specification then
8645 Ent : constant Entity_Id :=
8646 Defining_Identifier (Citem);
8648 if Ekind (Ent) = E_Discriminant then
8649 Ncomps := Ncomps + 1;
8650 Comps (Ncomps) := Ent;
8659 -- Gather component entities into Comp
8661 Citem := First (Component_Items (CL));
8662 while Present (Citem) loop
8663 if Nkind (Citem) = N_Component_Declaration then
8664 Ncomps := Ncomps + 1;
8665 Comps (Ncomps) := Defining_Identifier (Citem);
8671 -- Now sort the component entities based on the first bit.
8672 -- Note we already know there are no overlapping components.
8674 Sorting.Sort (Ncomps);
8676 -- Loop through entries checking for holes
8679 for J in 1 .. Ncomps loop
8681 Error_Msg_Uint_1 := Component_Bit_Offset (CEnt) - Nbit;
8683 if Error_Msg_Uint_1 > 0 then
8685 ("?H?^-bit gap before component&",
8686 Component_Name (Component_Clause (CEnt)), CEnt);
8689 Nbit := Component_Bit_Offset (CEnt) + Esize (CEnt);
8692 -- Process variant parts recursively if present
8694 if Present (Variant_Part (CL)) then
8695 Variant := First (Variants (Variant_Part (CL)));
8696 while Present (Variant) loop
8697 Check_Component_List
8698 (Component_List (Variant), Nbit, No_List);
8703 end Check_Component_List;
8705 -- Start of processing for Record_Hole_Check
8712 if Is_Tagged_Type (Rectype) then
8713 Sbit := UI_From_Int (System_Address_Size);
8718 if Nkind (Decl) = N_Full_Type_Declaration
8719 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
8721 Check_Component_List
8722 (Component_List (Type_Definition (Decl)),
8724 Discriminant_Specifications (Decl));
8727 end Record_Hole_Check;
8730 -- For records that have component clauses for all components, and whose
8731 -- size is less than or equal to 32, we need to know the size in the
8732 -- front end to activate possible packed array processing where the
8733 -- component type is a record.
8735 -- At this stage Hbit + 1 represents the first unused bit from all the
8736 -- component clauses processed, so if the component clauses are
8737 -- complete, then this is the length of the record.
8739 -- For records longer than System.Storage_Unit, and for those where not
8740 -- all components have component clauses, the back end determines the
8741 -- length (it may for example be appropriate to round up the size
8742 -- to some convenient boundary, based on alignment considerations, etc).
8744 if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then
8746 -- Nothing to do if at least one component has no component clause
8748 Comp := First_Component_Or_Discriminant (Rectype);
8749 while Present (Comp) loop
8750 exit when No (Component_Clause (Comp));
8751 Next_Component_Or_Discriminant (Comp);
8754 -- If we fall out of loop, all components have component clauses
8755 -- and so we can set the size to the maximum value.
8758 Set_RM_Size (Rectype, Hbit + 1);
8761 end Check_Record_Representation_Clause;
8767 procedure Check_Size
8771 Biased : out Boolean)
8773 UT : constant Entity_Id := Underlying_Type (T);
8779 -- Reject patently improper size values.
8781 if Is_Elementary_Type (T)
8782 and then Siz > UI_From_Int (Int'Last)
8784 Error_Msg_N ("Size value too large for elementary type", N);
8786 if Nkind (Original_Node (N)) = N_Op_Expon then
8788 ("\maybe '* was meant, rather than '*'*", Original_Node (N));
8792 -- Dismiss generic types
8794 if Is_Generic_Type (T)
8796 Is_Generic_Type (UT)
8798 Is_Generic_Type (Root_Type (UT))
8802 -- Guard against previous errors
8804 elsif No (UT) or else UT = Any_Type then
8805 Check_Error_Detected;
8808 -- Check case of bit packed array
8810 elsif Is_Array_Type (UT)
8811 and then Known_Static_Component_Size (UT)
8812 and then Is_Bit_Packed_Array (UT)
8820 Asiz := Component_Size (UT);
8821 Indx := First_Index (UT);
8823 Ityp := Etype (Indx);
8825 -- If non-static bound, then we are not in the business of
8826 -- trying to check the length, and indeed an error will be
8827 -- issued elsewhere, since sizes of non-static array types
8828 -- cannot be set implicitly or explicitly.
8830 if not Is_Static_Subtype (Ityp) then
8834 -- Otherwise accumulate next dimension
8836 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
8837 Expr_Value (Type_Low_Bound (Ityp)) +
8841 exit when No (Indx);
8848 Error_Msg_Uint_1 := Asiz;
8850 ("size for& too small, minimum allowed is ^", N, T);
8851 Set_Esize (T, Asiz);
8852 Set_RM_Size (T, Asiz);
8856 -- All other composite types are ignored
8858 elsif Is_Composite_Type (UT) then
8861 -- For fixed-point types, don't check minimum if type is not frozen,
8862 -- since we don't know all the characteristics of the type that can
8863 -- affect the size (e.g. a specified small) till freeze time.
8865 elsif Is_Fixed_Point_Type (UT)
8866 and then not Is_Frozen (UT)
8870 -- Cases for which a minimum check is required
8873 -- Ignore if specified size is correct for the type
8875 if Known_Esize (UT) and then Siz = Esize (UT) then
8879 -- Otherwise get minimum size
8881 M := UI_From_Int (Minimum_Size (UT));
8885 -- Size is less than minimum size, but one possibility remains
8886 -- that we can manage with the new size if we bias the type.
8888 M := UI_From_Int (Minimum_Size (UT, Biased => True));
8891 Error_Msg_Uint_1 := M;
8893 ("size for& too small, minimum allowed is ^", N, T);
8903 -------------------------
8904 -- Get_Alignment_Value --
8905 -------------------------
8907 function Get_Alignment_Value (Expr : Node_Id) return Uint is
8908 Align : constant Uint := Static_Integer (Expr);
8911 if Align = No_Uint then
8914 elsif Align <= 0 then
8915 Error_Msg_N ("alignment value must be positive", Expr);
8919 for J in Int range 0 .. 64 loop
8921 M : constant Uint := Uint_2 ** J;
8924 exit when M = Align;
8928 ("alignment value must be power of 2", Expr);
8936 end Get_Alignment_Value;
8938 -------------------------------------
8939 -- Inherit_Aspects_At_Freeze_Point --
8940 -------------------------------------
8942 procedure Inherit_Aspects_At_Freeze_Point (Typ : Entity_Id) is
8944 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8945 (Rep_Item : Node_Id) return Boolean;
8946 -- This routine checks if Rep_Item is either a pragma or an aspect
8947 -- specification node whose correponding pragma (if any) is present in
8948 -- the Rep Item chain of the entity it has been specified to.
8950 --------------------------------------------------
8951 -- Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item --
8952 --------------------------------------------------
8954 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8955 (Rep_Item : Node_Id) return Boolean
8958 return Nkind (Rep_Item) = N_Pragma
8959 or else Present_In_Rep_Item
8960 (Entity (Rep_Item), Aspect_Rep_Item (Rep_Item));
8961 end Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item;
8963 -- Start of processing for Inherit_Aspects_At_Freeze_Point
8966 -- A representation item is either subtype-specific (Size and Alignment
8967 -- clauses) or type-related (all others). Subtype-specific aspects may
8968 -- differ for different subtypes of the same type (RM 13.1.8).
8970 -- A derived type inherits each type-related representation aspect of
8971 -- its parent type that was directly specified before the declaration of
8972 -- the derived type (RM 13.1.15).
8974 -- A derived subtype inherits each subtype-specific representation
8975 -- aspect of its parent subtype that was directly specified before the
8976 -- declaration of the derived type (RM 13.1.15).
8978 -- The general processing involves inheriting a representation aspect
8979 -- from a parent type whenever the first rep item (aspect specification,
8980 -- attribute definition clause, pragma) corresponding to the given
8981 -- representation aspect in the rep item chain of Typ, if any, isn't
8982 -- directly specified to Typ but to one of its parents.
8984 -- ??? Note that, for now, just a limited number of representation
8985 -- aspects have been inherited here so far. Many of them are
8986 -- still inherited in Sem_Ch3. This will be fixed soon. Here is
8987 -- a non- exhaustive list of aspects that likely also need to
8988 -- be moved to this routine: Alignment, Component_Alignment,
8989 -- Component_Size, Machine_Radix, Object_Size, Pack, Predicates,
8990 -- Preelaborable_Initialization, RM_Size and Small.
8992 if Nkind (Parent (Typ)) = N_Private_Extension_Declaration then
8998 if not Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005, False)
8999 and then Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005)
9000 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
9001 (Get_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005))
9003 Set_Is_Ada_2005_Only (Typ);
9008 if not Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012, False)
9009 and then Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012)
9010 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
9011 (Get_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012))
9013 Set_Is_Ada_2012_Only (Typ);
9018 if not Has_Rep_Item (Typ, Name_Atomic, Name_Shared, False)
9019 and then Has_Rep_Pragma (Typ, Name_Atomic, Name_Shared)
9020 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
9021 (Get_Rep_Item (Typ, Name_Atomic, Name_Shared))
9023 Set_Is_Atomic (Typ);
9024 Set_Treat_As_Volatile (Typ);
9025 Set_Is_Volatile (Typ);
9028 -- Default_Component_Value
9030 if Is_Array_Type (Typ)
9031 and then Has_Rep_Item (Typ, Name_Default_Component_Value, False)
9032 and then Has_Rep_Item (Typ, Name_Default_Component_Value)
9034 Set_Default_Aspect_Component_Value (Typ,
9035 Default_Aspect_Component_Value
9036 (Entity (Get_Rep_Item (Typ, Name_Default_Component_Value))));
9041 if Is_Scalar_Type (Typ)
9042 and then Has_Rep_Item (Typ, Name_Default_Value, False)
9043 and then Has_Rep_Item (Typ, Name_Default_Value)
9045 Set_Default_Aspect_Value (Typ,
9046 Default_Aspect_Value
9047 (Entity (Get_Rep_Item (Typ, Name_Default_Value))));
9052 if not Has_Rep_Item (Typ, Name_Discard_Names, False)
9053 and then Has_Rep_Item (Typ, Name_Discard_Names)
9054 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
9055 (Get_Rep_Item (Typ, Name_Discard_Names))
9057 Set_Discard_Names (Typ);
9062 if not Has_Rep_Item (Typ, Name_Invariant, False)
9063 and then Has_Rep_Item (Typ, Name_Invariant)
9064 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
9065 (Get_Rep_Item (Typ, Name_Invariant))
9067 Set_Has_Invariants (Typ);
9069 if Class_Present (Get_Rep_Item (Typ, Name_Invariant)) then
9070 Set_Has_Inheritable_Invariants (Typ);
9076 if not Has_Rep_Item (Typ, Name_Volatile, False)
9077 and then Has_Rep_Item (Typ, Name_Volatile)
9078 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
9079 (Get_Rep_Item (Typ, Name_Volatile))
9081 Set_Treat_As_Volatile (Typ);
9082 Set_Is_Volatile (Typ);
9085 -- Inheritance for derived types only
9087 if Is_Derived_Type (Typ) then
9089 Bas_Typ : constant Entity_Id := Base_Type (Typ);
9090 Imp_Bas_Typ : constant Entity_Id := Implementation_Base_Type (Typ);
9093 -- Atomic_Components
9095 if not Has_Rep_Item (Typ, Name_Atomic_Components, False)
9096 and then Has_Rep_Item (Typ, Name_Atomic_Components)
9097 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
9098 (Get_Rep_Item (Typ, Name_Atomic_Components))
9100 Set_Has_Atomic_Components (Imp_Bas_Typ);
9103 -- Volatile_Components
9105 if not Has_Rep_Item (Typ, Name_Volatile_Components, False)
9106 and then Has_Rep_Item (Typ, Name_Volatile_Components)
9107 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
9108 (Get_Rep_Item (Typ, Name_Volatile_Components))
9110 Set_Has_Volatile_Components (Imp_Bas_Typ);
9113 -- Finalize_Storage_Only.
9115 if not Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only, False)
9116 and then Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only)
9118 Set_Finalize_Storage_Only (Bas_Typ);
9121 -- Universal_Aliasing
9123 if not Has_Rep_Item (Typ, Name_Universal_Aliasing, False)
9124 and then Has_Rep_Item (Typ, Name_Universal_Aliasing)
9125 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
9126 (Get_Rep_Item (Typ, Name_Universal_Aliasing))
9128 Set_Universal_Aliasing (Imp_Bas_Typ);
9131 -- Record type specific aspects
9133 if Is_Record_Type (Typ) then
9137 if not Has_Rep_Item (Typ, Name_Bit_Order, False)
9138 and then Has_Rep_Item (Typ, Name_Bit_Order)
9140 Set_Reverse_Bit_Order (Bas_Typ,
9141 Reverse_Bit_Order (Entity (Name
9142 (Get_Rep_Item (Typ, Name_Bit_Order)))));
9145 -- Scalar_Storage_Order
9147 if not Has_Rep_Item (Typ, Name_Scalar_Storage_Order, False)
9148 and then Has_Rep_Item (Typ, Name_Scalar_Storage_Order)
9150 Set_Reverse_Storage_Order (Bas_Typ,
9151 Reverse_Storage_Order (Entity (Name
9152 (Get_Rep_Item (Typ, Name_Scalar_Storage_Order)))));
9157 end Inherit_Aspects_At_Freeze_Point;
9163 procedure Initialize is
9165 Address_Clause_Checks.Init;
9166 Independence_Checks.Init;
9167 Unchecked_Conversions.Init;
9170 -------------------------
9171 -- Is_Operational_Item --
9172 -------------------------
9174 function Is_Operational_Item (N : Node_Id) return Boolean is
9176 if Nkind (N) /= N_Attribute_Definition_Clause then
9181 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
9183 return Id = Attribute_Input
9184 or else Id = Attribute_Output
9185 or else Id = Attribute_Read
9186 or else Id = Attribute_Write
9187 or else Id = Attribute_External_Tag;
9190 end Is_Operational_Item;
9196 function Minimum_Size
9198 Biased : Boolean := False) return Nat
9200 Lo : Uint := No_Uint;
9201 Hi : Uint := No_Uint;
9202 LoR : Ureal := No_Ureal;
9203 HiR : Ureal := No_Ureal;
9204 LoSet : Boolean := False;
9205 HiSet : Boolean := False;
9209 R_Typ : constant Entity_Id := Root_Type (T);
9212 -- If bad type, return 0
9214 if T = Any_Type then
9217 -- For generic types, just return zero. There cannot be any legitimate
9218 -- need to know such a size, but this routine may be called with a
9219 -- generic type as part of normal processing.
9221 elsif Is_Generic_Type (R_Typ)
9222 or else R_Typ = Any_Type
9226 -- Access types. Normally an access type cannot have a size smaller
9227 -- than the size of System.Address. The exception is on VMS, where
9228 -- we have short and long addresses, and it is possible for an access
9229 -- type to have a short address size (and thus be less than the size
9230 -- of System.Address itself). We simply skip the check for VMS, and
9231 -- leave it to the back end to do the check.
9233 elsif Is_Access_Type (T) then
9234 if OpenVMS_On_Target then
9237 return System_Address_Size;
9240 -- Floating-point types
9242 elsif Is_Floating_Point_Type (T) then
9243 return UI_To_Int (Esize (R_Typ));
9247 elsif Is_Discrete_Type (T) then
9249 -- The following loop is looking for the nearest compile time known
9250 -- bounds following the ancestor subtype chain. The idea is to find
9251 -- the most restrictive known bounds information.
9255 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
9260 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
9261 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
9268 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
9269 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
9275 Ancest := Ancestor_Subtype (Ancest);
9278 Ancest := Base_Type (T);
9280 if Is_Generic_Type (Ancest) then
9286 -- Fixed-point types. We can't simply use Expr_Value to get the
9287 -- Corresponding_Integer_Value values of the bounds, since these do not
9288 -- get set till the type is frozen, and this routine can be called
9289 -- before the type is frozen. Similarly the test for bounds being static
9290 -- needs to include the case where we have unanalyzed real literals for
9293 elsif Is_Fixed_Point_Type (T) then
9295 -- The following loop is looking for the nearest compile time known
9296 -- bounds following the ancestor subtype chain. The idea is to find
9297 -- the most restrictive known bounds information.
9301 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
9305 -- Note: In the following two tests for LoSet and HiSet, it may
9306 -- seem redundant to test for N_Real_Literal here since normally
9307 -- one would assume that the test for the value being known at
9308 -- compile time includes this case. However, there is a glitch.
9309 -- If the real literal comes from folding a non-static expression,
9310 -- then we don't consider any non- static expression to be known
9311 -- at compile time if we are in configurable run time mode (needed
9312 -- in some cases to give a clearer definition of what is and what
9313 -- is not accepted). So the test is indeed needed. Without it, we
9314 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
9317 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
9318 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
9320 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
9327 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
9328 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
9330 HiR := Expr_Value_R (Type_High_Bound (Ancest));
9336 Ancest := Ancestor_Subtype (Ancest);
9339 Ancest := Base_Type (T);
9341 if Is_Generic_Type (Ancest) then
9347 Lo := UR_To_Uint (LoR / Small_Value (T));
9348 Hi := UR_To_Uint (HiR / Small_Value (T));
9350 -- No other types allowed
9353 raise Program_Error;
9356 -- Fall through with Hi and Lo set. Deal with biased case
9359 and then not Is_Fixed_Point_Type (T)
9360 and then not (Is_Enumeration_Type (T)
9361 and then Has_Non_Standard_Rep (T)))
9362 or else Has_Biased_Representation (T)
9368 -- Signed case. Note that we consider types like range 1 .. -1 to be
9369 -- signed for the purpose of computing the size, since the bounds have
9370 -- to be accommodated in the base type.
9372 if Lo < 0 or else Hi < 0 then
9376 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
9377 -- Note that we accommodate the case where the bounds cross. This
9378 -- can happen either because of the way the bounds are declared
9379 -- or because of the algorithm in Freeze_Fixed_Point_Type.
9393 -- If both bounds are positive, make sure that both are represen-
9394 -- table in the case where the bounds are crossed. This can happen
9395 -- either because of the way the bounds are declared, or because of
9396 -- the algorithm in Freeze_Fixed_Point_Type.
9402 -- S = size, (can accommodate 0 .. (2**size - 1))
9405 while Hi >= Uint_2 ** S loop
9413 ---------------------------
9414 -- New_Stream_Subprogram --
9415 ---------------------------
9417 procedure New_Stream_Subprogram
9421 Nam : TSS_Name_Type)
9423 Loc : constant Source_Ptr := Sloc (N);
9424 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
9425 Subp_Id : Entity_Id;
9426 Subp_Decl : Node_Id;
9430 Defer_Declaration : constant Boolean :=
9431 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
9432 -- For a tagged type, there is a declaration for each stream attribute
9433 -- at the freeze point, and we must generate only a completion of this
9434 -- declaration. We do the same for private types, because the full view
9435 -- might be tagged. Otherwise we generate a declaration at the point of
9436 -- the attribute definition clause.
9438 function Build_Spec return Node_Id;
9439 -- Used for declaration and renaming declaration, so that this is
9440 -- treated as a renaming_as_body.
9446 function Build_Spec return Node_Id is
9447 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
9450 T_Ref : constant Node_Id := New_Reference_To (Etyp, Loc);
9453 Subp_Id := Make_Defining_Identifier (Loc, Sname);
9455 -- S : access Root_Stream_Type'Class
9457 Formals := New_List (
9458 Make_Parameter_Specification (Loc,
9459 Defining_Identifier =>
9460 Make_Defining_Identifier (Loc, Name_S),
9462 Make_Access_Definition (Loc,
9465 Designated_Type (Etype (F)), Loc))));
9467 if Nam = TSS_Stream_Input then
9469 Make_Function_Specification (Loc,
9470 Defining_Unit_Name => Subp_Id,
9471 Parameter_Specifications => Formals,
9472 Result_Definition => T_Ref);
9477 Make_Parameter_Specification (Loc,
9478 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
9479 Out_Present => Out_P,
9480 Parameter_Type => T_Ref));
9483 Make_Procedure_Specification (Loc,
9484 Defining_Unit_Name => Subp_Id,
9485 Parameter_Specifications => Formals);
9491 -- Start of processing for New_Stream_Subprogram
9494 F := First_Formal (Subp);
9496 if Ekind (Subp) = E_Procedure then
9497 Etyp := Etype (Next_Formal (F));
9499 Etyp := Etype (Subp);
9502 -- Prepare subprogram declaration and insert it as an action on the
9503 -- clause node. The visibility for this entity is used to test for
9504 -- visibility of the attribute definition clause (in the sense of
9505 -- 8.3(23) as amended by AI-195).
9507 if not Defer_Declaration then
9509 Make_Subprogram_Declaration (Loc,
9510 Specification => Build_Spec);
9512 -- For a tagged type, there is always a visible declaration for each
9513 -- stream TSS (it is a predefined primitive operation), and the
9514 -- completion of this declaration occurs at the freeze point, which is
9515 -- not always visible at places where the attribute definition clause is
9516 -- visible. So, we create a dummy entity here for the purpose of
9517 -- tracking the visibility of the attribute definition clause itself.
9521 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
9523 Make_Object_Declaration (Loc,
9524 Defining_Identifier => Subp_Id,
9525 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
9528 Insert_Action (N, Subp_Decl);
9529 Set_Entity (N, Subp_Id);
9532 Make_Subprogram_Renaming_Declaration (Loc,
9533 Specification => Build_Spec,
9534 Name => New_Reference_To (Subp, Loc));
9536 if Defer_Declaration then
9537 Set_TSS (Base_Type (Ent), Subp_Id);
9539 Insert_Action (N, Subp_Decl);
9540 Copy_TSS (Subp_Id, Base_Type (Ent));
9542 end New_Stream_Subprogram;
9544 ------------------------
9545 -- Rep_Item_Too_Early --
9546 ------------------------
9548 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
9550 -- Cannot apply non-operational rep items to generic types
9552 if Is_Operational_Item (N) then
9556 and then Is_Generic_Type (Root_Type (T))
9558 Error_Msg_N ("representation item not allowed for generic type", N);
9562 -- Otherwise check for incomplete type
9564 if Is_Incomplete_Or_Private_Type (T)
9565 and then No (Underlying_Type (T))
9567 (Nkind (N) /= N_Pragma
9568 or else Get_Pragma_Id (N) /= Pragma_Import)
9571 ("representation item must be after full type declaration", N);
9574 -- If the type has incomplete components, a representation clause is
9575 -- illegal but stream attributes and Convention pragmas are correct.
9577 elsif Has_Private_Component (T) then
9578 if Nkind (N) = N_Pragma then
9583 ("representation item must appear after type is fully defined",
9590 end Rep_Item_Too_Early;
9592 -----------------------
9593 -- Rep_Item_Too_Late --
9594 -----------------------
9596 function Rep_Item_Too_Late
9599 FOnly : Boolean := False) return Boolean
9602 Parent_Type : Entity_Id;
9605 -- Output the too late message. Note that this is not considered a
9606 -- serious error, since the effect is simply that we ignore the
9607 -- representation clause in this case.
9613 procedure Too_Late is
9615 -- Other compilers seem more relaxed about rep items appearing too
9616 -- late. Since analysis tools typically don't care about rep items
9617 -- anyway, no reason to be too strict about this.
9619 if not Relaxed_RM_Semantics then
9620 Error_Msg_N ("|representation item appears too late!", N);
9624 -- Start of processing for Rep_Item_Too_Late
9627 -- First make sure entity is not frozen (RM 13.1(9))
9631 -- Exclude imported types, which may be frozen if they appear in a
9632 -- representation clause for a local type.
9634 and then not From_With_Type (T)
9636 -- Exclude generated entities (not coming from source). The common
9637 -- case is when we generate a renaming which prematurely freezes the
9638 -- renamed internal entity, but we still want to be able to set copies
9639 -- of attribute values such as Size/Alignment.
9641 and then Comes_From_Source (T)
9644 S := First_Subtype (T);
9646 if Present (Freeze_Node (S)) then
9648 ("??no more representation items for }", Freeze_Node (S), S);
9653 -- Check for case of non-tagged derived type whose parent either has
9654 -- primitive operations, or is a by reference type (RM 13.1(10)).
9658 and then Is_Derived_Type (T)
9659 and then not Is_Tagged_Type (T)
9661 Parent_Type := Etype (Base_Type (T));
9663 if Has_Primitive_Operations (Parent_Type) then
9666 ("primitive operations already defined for&!", N, Parent_Type);
9669 elsif Is_By_Reference_Type (Parent_Type) then
9672 ("parent type & is a by reference type!", N, Parent_Type);
9677 -- No error, link item into head of chain of rep items for the entity,
9678 -- but avoid chaining if we have an overloadable entity, and the pragma
9679 -- is one that can apply to multiple overloaded entities.
9681 if Is_Overloadable (T) and then Nkind (N) = N_Pragma then
9683 Pname : constant Name_Id := Pragma_Name (N);
9685 if Nam_In (Pname, Name_Convention, Name_Import, Name_Export,
9686 Name_External, Name_Interface)
9693 Record_Rep_Item (T, N);
9695 end Rep_Item_Too_Late;
9697 -------------------------------------
9698 -- Replace_Type_References_Generic --
9699 -------------------------------------
9701 procedure Replace_Type_References_Generic (N : Node_Id; TName : Name_Id) is
9703 function Replace_Node (N : Node_Id) return Traverse_Result;
9704 -- Processes a single node in the traversal procedure below, checking
9705 -- if node N should be replaced, and if so, doing the replacement.
9707 procedure Replace_Type_Refs is new Traverse_Proc (Replace_Node);
9708 -- This instantiation provides the body of Replace_Type_References
9714 function Replace_Node (N : Node_Id) return Traverse_Result is
9719 -- Case of identifier
9721 if Nkind (N) = N_Identifier then
9723 -- If not the type name, all done with this node
9725 if Chars (N) /= TName then
9728 -- Otherwise do the replacement and we are done with this node
9731 Replace_Type_Reference (N);
9735 -- Case of selected component (which is what a qualification
9736 -- looks like in the unanalyzed tree, which is what we have.
9738 elsif Nkind (N) = N_Selected_Component then
9740 -- If selector name is not our type, keeping going (we might
9741 -- still have an occurrence of the type in the prefix).
9743 if Nkind (Selector_Name (N)) /= N_Identifier
9744 or else Chars (Selector_Name (N)) /= TName
9748 -- Selector name is our type, check qualification
9751 -- Loop through scopes and prefixes, doing comparison
9756 -- Continue if no more scopes or scope with no name
9758 if No (S) or else Nkind (S) not in N_Has_Chars then
9762 -- Do replace if prefix is an identifier matching the
9763 -- scope that we are currently looking at.
9765 if Nkind (P) = N_Identifier
9766 and then Chars (P) = Chars (S)
9768 Replace_Type_Reference (N);
9772 -- Go check scope above us if prefix is itself of the
9773 -- form of a selected component, whose selector matches
9774 -- the scope we are currently looking at.
9776 if Nkind (P) = N_Selected_Component
9777 and then Nkind (Selector_Name (P)) = N_Identifier
9778 and then Chars (Selector_Name (P)) = Chars (S)
9783 -- For anything else, we don't have a match, so keep on
9784 -- going, there are still some weird cases where we may
9785 -- still have a replacement within the prefix.
9793 -- Continue for any other node kind
9801 Replace_Type_Refs (N);
9802 end Replace_Type_References_Generic;
9804 -------------------------
9805 -- Same_Representation --
9806 -------------------------
9808 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
9809 T1 : constant Entity_Id := Underlying_Type (Typ1);
9810 T2 : constant Entity_Id := Underlying_Type (Typ2);
9813 -- A quick check, if base types are the same, then we definitely have
9814 -- the same representation, because the subtype specific representation
9815 -- attributes (Size and Alignment) do not affect representation from
9816 -- the point of view of this test.
9818 if Base_Type (T1) = Base_Type (T2) then
9821 elsif Is_Private_Type (Base_Type (T2))
9822 and then Base_Type (T1) = Full_View (Base_Type (T2))
9827 -- Tagged types never have differing representations
9829 if Is_Tagged_Type (T1) then
9833 -- Representations are definitely different if conventions differ
9835 if Convention (T1) /= Convention (T2) then
9839 -- Representations are different if component alignments or scalar
9840 -- storage orders differ.
9842 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
9844 (Is_Record_Type (T2) or else Is_Array_Type (T2))
9846 (Component_Alignment (T1) /= Component_Alignment (T2)
9848 Reverse_Storage_Order (T1) /= Reverse_Storage_Order (T2))
9853 -- For arrays, the only real issue is component size. If we know the
9854 -- component size for both arrays, and it is the same, then that's
9855 -- good enough to know we don't have a change of representation.
9857 if Is_Array_Type (T1) then
9858 if Known_Component_Size (T1)
9859 and then Known_Component_Size (T2)
9860 and then Component_Size (T1) = Component_Size (T2)
9862 if VM_Target = No_VM then
9865 -- In VM targets the representation of arrays with aliased
9866 -- components differs from arrays with non-aliased components
9869 return Has_Aliased_Components (Base_Type (T1))
9871 Has_Aliased_Components (Base_Type (T2));
9876 -- Types definitely have same representation if neither has non-standard
9877 -- representation since default representations are always consistent.
9878 -- If only one has non-standard representation, and the other does not,
9879 -- then we consider that they do not have the same representation. They
9880 -- might, but there is no way of telling early enough.
9882 if Has_Non_Standard_Rep (T1) then
9883 if not Has_Non_Standard_Rep (T2) then
9887 return not Has_Non_Standard_Rep (T2);
9890 -- Here the two types both have non-standard representation, and we need
9891 -- to determine if they have the same non-standard representation.
9893 -- For arrays, we simply need to test if the component sizes are the
9894 -- same. Pragma Pack is reflected in modified component sizes, so this
9895 -- check also deals with pragma Pack.
9897 if Is_Array_Type (T1) then
9898 return Component_Size (T1) = Component_Size (T2);
9900 -- Tagged types always have the same representation, because it is not
9901 -- possible to specify different representations for common fields.
9903 elsif Is_Tagged_Type (T1) then
9906 -- Case of record types
9908 elsif Is_Record_Type (T1) then
9910 -- Packed status must conform
9912 if Is_Packed (T1) /= Is_Packed (T2) then
9915 -- Otherwise we must check components. Typ2 maybe a constrained
9916 -- subtype with fewer components, so we compare the components
9917 -- of the base types.
9920 Record_Case : declare
9921 CD1, CD2 : Entity_Id;
9923 function Same_Rep return Boolean;
9924 -- CD1 and CD2 are either components or discriminants. This
9925 -- function tests whether they have the same representation.
9931 function Same_Rep return Boolean is
9933 if No (Component_Clause (CD1)) then
9934 return No (Component_Clause (CD2));
9936 -- Note: at this point, component clauses have been
9937 -- normalized to the default bit order, so that the
9938 -- comparison of Component_Bit_Offsets is meaningful.
9941 Present (Component_Clause (CD2))
9943 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
9945 Esize (CD1) = Esize (CD2);
9949 -- Start of processing for Record_Case
9952 if Has_Discriminants (T1) then
9954 -- The number of discriminants may be different if the
9955 -- derived type has fewer (constrained by values). The
9956 -- invisible discriminants retain the representation of
9957 -- the original, so the discrepancy does not per se
9958 -- indicate a different representation.
9960 CD1 := First_Discriminant (T1);
9961 CD2 := First_Discriminant (T2);
9962 while Present (CD1) and then Present (CD2) loop
9963 if not Same_Rep then
9966 Next_Discriminant (CD1);
9967 Next_Discriminant (CD2);
9972 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
9973 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
9974 while Present (CD1) loop
9975 if not Same_Rep then
9978 Next_Component (CD1);
9979 Next_Component (CD2);
9987 -- For enumeration types, we must check each literal to see if the
9988 -- representation is the same. Note that we do not permit enumeration
9989 -- representation clauses for Character and Wide_Character, so these
9990 -- cases were already dealt with.
9992 elsif Is_Enumeration_Type (T1) then
9993 Enumeration_Case : declare
9997 L1 := First_Literal (T1);
9998 L2 := First_Literal (T2);
9999 while Present (L1) loop
10000 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
10009 end Enumeration_Case;
10011 -- Any other types have the same representation for these purposes
10016 end Same_Representation;
10022 procedure Set_Biased
10026 Biased : Boolean := True)
10030 Set_Has_Biased_Representation (E);
10032 if Warn_On_Biased_Representation then
10034 ("?B?" & Msg & " forces biased representation for&", N, E);
10039 --------------------
10040 -- Set_Enum_Esize --
10041 --------------------
10043 procedure Set_Enum_Esize (T : Entity_Id) is
10049 Init_Alignment (T);
10051 -- Find the minimum standard size (8,16,32,64) that fits
10053 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
10054 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
10057 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
10058 Sz := Standard_Character_Size; -- May be > 8 on some targets
10060 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
10063 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
10066 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
10071 if Hi < Uint_2**08 then
10072 Sz := Standard_Character_Size; -- May be > 8 on some targets
10074 elsif Hi < Uint_2**16 then
10077 elsif Hi < Uint_2**32 then
10080 else pragma Assert (Hi < Uint_2**63);
10085 -- That minimum is the proper size unless we have a foreign convention
10086 -- and the size required is 32 or less, in which case we bump the size
10087 -- up to 32. This is required for C and C++ and seems reasonable for
10088 -- all other foreign conventions.
10090 if Has_Foreign_Convention (T)
10091 and then Esize (T) < Standard_Integer_Size
10093 Init_Esize (T, Standard_Integer_Size);
10095 Init_Esize (T, Sz);
10097 end Set_Enum_Esize;
10099 ------------------------------
10100 -- Validate_Address_Clauses --
10101 ------------------------------
10103 procedure Validate_Address_Clauses is
10105 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
10107 ACCR : Address_Clause_Check_Record
10108 renames Address_Clause_Checks.Table (J);
10112 X_Alignment : Uint;
10113 Y_Alignment : Uint;
10119 -- Skip processing of this entry if warning already posted
10121 if not Address_Warning_Posted (ACCR.N) then
10122 Expr := Original_Node (Expression (ACCR.N));
10126 X_Alignment := Alignment (ACCR.X);
10127 Y_Alignment := Alignment (ACCR.Y);
10129 -- Similarly obtain sizes
10131 X_Size := Esize (ACCR.X);
10132 Y_Size := Esize (ACCR.Y);
10134 -- Check for large object overlaying smaller one
10137 and then X_Size > Uint_0
10138 and then X_Size > Y_Size
10141 ("?& overlays smaller object", ACCR.N, ACCR.X);
10143 ("\??program execution may be erroneous", ACCR.N);
10144 Error_Msg_Uint_1 := X_Size;
10146 ("\??size of & is ^", ACCR.N, ACCR.X);
10147 Error_Msg_Uint_1 := Y_Size;
10149 ("\??size of & is ^", ACCR.N, ACCR.Y);
10151 -- Check for inadequate alignment, both of the base object
10152 -- and of the offset, if any.
10154 -- Note: we do not check the alignment if we gave a size
10155 -- warning, since it would likely be redundant.
10157 elsif Y_Alignment /= Uint_0
10158 and then (Y_Alignment < X_Alignment
10161 Nkind (Expr) = N_Attribute_Reference
10163 Attribute_Name (Expr) = Name_Address
10165 Has_Compatible_Alignment
10166 (ACCR.X, Prefix (Expr))
10167 /= Known_Compatible))
10170 ("??specified address for& may be inconsistent "
10171 & "with alignment", ACCR.N, ACCR.X);
10173 ("\??program execution may be erroneous (RM 13.3(27))",
10175 Error_Msg_Uint_1 := X_Alignment;
10177 ("\??alignment of & is ^", ACCR.N, ACCR.X);
10178 Error_Msg_Uint_1 := Y_Alignment;
10180 ("\??alignment of & is ^", ACCR.N, ACCR.Y);
10181 if Y_Alignment >= X_Alignment then
10183 ("\??but offset is not multiple of alignment", ACCR.N);
10189 end Validate_Address_Clauses;
10191 ---------------------------
10192 -- Validate_Independence --
10193 ---------------------------
10195 procedure Validate_Independence is
10196 SU : constant Uint := UI_From_Int (System_Storage_Unit);
10204 procedure Check_Array_Type (Atyp : Entity_Id);
10205 -- Checks if the array type Atyp has independent components, and
10206 -- if not, outputs an appropriate set of error messages.
10208 procedure No_Independence;
10209 -- Output message that independence cannot be guaranteed
10211 function OK_Component (C : Entity_Id) return Boolean;
10212 -- Checks one component to see if it is independently accessible, and
10213 -- if so yields True, otherwise yields False if independent access
10214 -- cannot be guaranteed. This is a conservative routine, it only
10215 -- returns True if it knows for sure, it returns False if it knows
10216 -- there is a problem, or it cannot be sure there is no problem.
10218 procedure Reason_Bad_Component (C : Entity_Id);
10219 -- Outputs continuation message if a reason can be determined for
10220 -- the component C being bad.
10222 ----------------------
10223 -- Check_Array_Type --
10224 ----------------------
10226 procedure Check_Array_Type (Atyp : Entity_Id) is
10227 Ctyp : constant Entity_Id := Component_Type (Atyp);
10230 -- OK if no alignment clause, no pack, and no component size
10232 if not Has_Component_Size_Clause (Atyp)
10233 and then not Has_Alignment_Clause (Atyp)
10234 and then not Is_Packed (Atyp)
10239 -- Check actual component size
10241 if not Known_Component_Size (Atyp)
10242 or else not (Addressable (Component_Size (Atyp))
10243 and then Component_Size (Atyp) < 64)
10244 or else Component_Size (Atyp) mod Esize (Ctyp) /= 0
10248 -- Bad component size, check reason
10250 if Has_Component_Size_Clause (Atyp) then
10251 P := Get_Attribute_Definition_Clause
10252 (Atyp, Attribute_Component_Size);
10254 if Present (P) then
10255 Error_Msg_Sloc := Sloc (P);
10256 Error_Msg_N ("\because of Component_Size clause#", N);
10261 if Is_Packed (Atyp) then
10262 P := Get_Rep_Pragma (Atyp, Name_Pack);
10264 if Present (P) then
10265 Error_Msg_Sloc := Sloc (P);
10266 Error_Msg_N ("\because of pragma Pack#", N);
10271 -- No reason found, just return
10276 -- Array type is OK independence-wise
10279 end Check_Array_Type;
10281 ---------------------
10282 -- No_Independence --
10283 ---------------------
10285 procedure No_Independence is
10287 if Pragma_Name (N) = Name_Independent then
10288 Error_Msg_NE ("independence cannot be guaranteed for&", N, E);
10291 ("independent components cannot be guaranteed for&", N, E);
10293 end No_Independence;
10299 function OK_Component (C : Entity_Id) return Boolean is
10300 Rec : constant Entity_Id := Scope (C);
10301 Ctyp : constant Entity_Id := Etype (C);
10304 -- OK if no component clause, no Pack, and no alignment clause
10306 if No (Component_Clause (C))
10307 and then not Is_Packed (Rec)
10308 and then not Has_Alignment_Clause (Rec)
10313 -- Here we look at the actual component layout. A component is
10314 -- addressable if its size is a multiple of the Esize of the
10315 -- component type, and its starting position in the record has
10316 -- appropriate alignment, and the record itself has appropriate
10317 -- alignment to guarantee the component alignment.
10319 -- Make sure sizes are static, always assume the worst for any
10320 -- cases where we cannot check static values.
10322 if not (Known_Static_Esize (C)
10324 Known_Static_Esize (Ctyp))
10329 -- Size of component must be addressable or greater than 64 bits
10330 -- and a multiple of bytes.
10332 if not Addressable (Esize (C)) and then Esize (C) < Uint_64 then
10336 -- Check size is proper multiple
10338 if Esize (C) mod Esize (Ctyp) /= 0 then
10342 -- Check alignment of component is OK
10344 if not Known_Component_Bit_Offset (C)
10345 or else Component_Bit_Offset (C) < Uint_0
10346 or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0
10351 -- Check alignment of record type is OK
10353 if not Known_Alignment (Rec)
10354 or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
10359 -- All tests passed, component is addressable
10364 --------------------------
10365 -- Reason_Bad_Component --
10366 --------------------------
10368 procedure Reason_Bad_Component (C : Entity_Id) is
10369 Rec : constant Entity_Id := Scope (C);
10370 Ctyp : constant Entity_Id := Etype (C);
10373 -- If component clause present assume that's the problem
10375 if Present (Component_Clause (C)) then
10376 Error_Msg_Sloc := Sloc (Component_Clause (C));
10377 Error_Msg_N ("\because of Component_Clause#", N);
10381 -- If pragma Pack clause present, assume that's the problem
10383 if Is_Packed (Rec) then
10384 P := Get_Rep_Pragma (Rec, Name_Pack);
10386 if Present (P) then
10387 Error_Msg_Sloc := Sloc (P);
10388 Error_Msg_N ("\because of pragma Pack#", N);
10393 -- See if record has bad alignment clause
10395 if Has_Alignment_Clause (Rec)
10396 and then Known_Alignment (Rec)
10397 and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
10399 P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment);
10401 if Present (P) then
10402 Error_Msg_Sloc := Sloc (P);
10403 Error_Msg_N ("\because of Alignment clause#", N);
10407 -- Couldn't find a reason, so return without a message
10410 end Reason_Bad_Component;
10412 -- Start of processing for Validate_Independence
10415 for J in Independence_Checks.First .. Independence_Checks.Last loop
10416 N := Independence_Checks.Table (J).N;
10417 E := Independence_Checks.Table (J).E;
10418 IC := Pragma_Name (N) = Name_Independent_Components;
10420 -- Deal with component case
10422 if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then
10423 if not OK_Component (E) then
10425 Reason_Bad_Component (E);
10430 -- Deal with record with Independent_Components
10432 if IC and then Is_Record_Type (E) then
10433 Comp := First_Component_Or_Discriminant (E);
10434 while Present (Comp) loop
10435 if not OK_Component (Comp) then
10437 Reason_Bad_Component (Comp);
10441 Next_Component_Or_Discriminant (Comp);
10445 -- Deal with address clause case
10447 if Is_Object (E) then
10448 Addr := Address_Clause (E);
10450 if Present (Addr) then
10452 Error_Msg_Sloc := Sloc (Addr);
10453 Error_Msg_N ("\because of Address clause#", N);
10458 -- Deal with independent components for array type
10460 if IC and then Is_Array_Type (E) then
10461 Check_Array_Type (E);
10464 -- Deal with independent components for array object
10466 if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then
10467 Check_Array_Type (Etype (E));
10472 end Validate_Independence;
10474 -----------------------------------
10475 -- Validate_Unchecked_Conversion --
10476 -----------------------------------
10478 procedure Validate_Unchecked_Conversion
10480 Act_Unit : Entity_Id)
10482 Source : Entity_Id;
10483 Target : Entity_Id;
10487 -- Obtain source and target types. Note that we call Ancestor_Subtype
10488 -- here because the processing for generic instantiation always makes
10489 -- subtypes, and we want the original frozen actual types.
10491 -- If we are dealing with private types, then do the check on their
10492 -- fully declared counterparts if the full declarations have been
10493 -- encountered (they don't have to be visible, but they must exist!)
10495 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
10497 if Is_Private_Type (Source)
10498 and then Present (Underlying_Type (Source))
10500 Source := Underlying_Type (Source);
10503 Target := Ancestor_Subtype (Etype (Act_Unit));
10505 -- If either type is generic, the instantiation happens within a generic
10506 -- unit, and there is nothing to check. The proper check will happen
10507 -- when the enclosing generic is instantiated.
10509 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
10513 if Is_Private_Type (Target)
10514 and then Present (Underlying_Type (Target))
10516 Target := Underlying_Type (Target);
10519 -- Source may be unconstrained array, but not target
10521 if Is_Array_Type (Target) and then not Is_Constrained (Target) then
10523 ("unchecked conversion to unconstrained array not allowed", N);
10527 -- Warn if conversion between two different convention pointers
10529 if Is_Access_Type (Target)
10530 and then Is_Access_Type (Source)
10531 and then Convention (Target) /= Convention (Source)
10532 and then Warn_On_Unchecked_Conversion
10534 -- Give warnings for subprogram pointers only on most targets. The
10535 -- exception is VMS, where data pointers can have different lengths
10536 -- depending on the pointer convention.
10538 if Is_Access_Subprogram_Type (Target)
10539 or else Is_Access_Subprogram_Type (Source)
10540 or else OpenVMS_On_Target
10543 ("?z?conversion between pointers with different conventions!",
10548 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
10549 -- warning when compiling GNAT-related sources.
10551 if Warn_On_Unchecked_Conversion
10552 and then not In_Predefined_Unit (N)
10553 and then RTU_Loaded (Ada_Calendar)
10555 (Chars (Source) = Name_Time
10557 Chars (Target) = Name_Time)
10559 -- If Ada.Calendar is loaded and the name of one of the operands is
10560 -- Time, there is a good chance that this is Ada.Calendar.Time.
10563 Calendar_Time : constant Entity_Id :=
10564 Full_View (RTE (RO_CA_Time));
10566 pragma Assert (Present (Calendar_Time));
10568 if Source = Calendar_Time or else Target = Calendar_Time then
10570 ("?z?representation of 'Time values may change between " &
10571 "'G'N'A'T versions", N);
10576 -- Make entry in unchecked conversion table for later processing by
10577 -- Validate_Unchecked_Conversions, which will check sizes and alignments
10578 -- (using values set by the back-end where possible). This is only done
10579 -- if the appropriate warning is active.
10581 if Warn_On_Unchecked_Conversion then
10582 Unchecked_Conversions.Append
10583 (New_Val => UC_Entry'(Eloc => Sloc (N),
10585 Target => Target));
10587 -- If both sizes are known statically now, then back end annotation
10588 -- is not required to do a proper check but if either size is not
10589 -- known statically, then we need the annotation.
10591 if Known_Static_RM_Size (Source)
10593 Known_Static_RM_Size (Target)
10597 Back_Annotate_Rep_Info := True;
10601 -- If unchecked conversion to access type, and access type is declared
10602 -- in the same unit as the unchecked conversion, then set the flag
10603 -- No_Strict_Aliasing (no strict aliasing is implicit here)
10605 if Is_Access_Type (Target) and then
10606 In_Same_Source_Unit (Target, N)
10608 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
10611 -- Generate N_Validate_Unchecked_Conversion node for back end in case
10612 -- the back end needs to perform special validation checks.
10614 -- Shouldn't this be in Exp_Ch13, since the check only gets done if we
10615 -- have full expansion and the back end is called ???
10618 Make_Validate_Unchecked_Conversion (Sloc (N));
10619 Set_Source_Type (Vnode, Source);
10620 Set_Target_Type (Vnode, Target);
10622 -- If the unchecked conversion node is in a list, just insert before it.
10623 -- If not we have some strange case, not worth bothering about.
10625 if Is_List_Member (N) then
10626 Insert_After (N, Vnode);
10628 end Validate_Unchecked_Conversion;
10630 ------------------------------------
10631 -- Validate_Unchecked_Conversions --
10632 ------------------------------------
10634 procedure Validate_Unchecked_Conversions is
10636 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
10638 T : UC_Entry renames Unchecked_Conversions.Table (N);
10640 Eloc : constant Source_Ptr := T.Eloc;
10641 Source : constant Entity_Id := T.Source;
10642 Target : constant Entity_Id := T.Target;
10648 -- This validation check, which warns if we have unequal sizes for
10649 -- unchecked conversion, and thus potentially implementation
10650 -- dependent semantics, is one of the few occasions on which we
10651 -- use the official RM size instead of Esize. See description in
10652 -- Einfo "Handling of Type'Size Values" for details.
10654 if Serious_Errors_Detected = 0
10655 and then Known_Static_RM_Size (Source)
10656 and then Known_Static_RM_Size (Target)
10658 -- Don't do the check if warnings off for either type, note the
10659 -- deliberate use of OR here instead of OR ELSE to get the flag
10660 -- Warnings_Off_Used set for both types if appropriate.
10662 and then not (Has_Warnings_Off (Source)
10664 Has_Warnings_Off (Target))
10666 Source_Siz := RM_Size (Source);
10667 Target_Siz := RM_Size (Target);
10669 if Source_Siz /= Target_Siz then
10671 ("?z?types for unchecked conversion have different sizes!",
10674 if All_Errors_Mode then
10675 Error_Msg_Name_1 := Chars (Source);
10676 Error_Msg_Uint_1 := Source_Siz;
10677 Error_Msg_Name_2 := Chars (Target);
10678 Error_Msg_Uint_2 := Target_Siz;
10679 Error_Msg ("\size of % is ^, size of % is ^?z?", Eloc);
10681 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
10683 if Is_Discrete_Type (Source)
10685 Is_Discrete_Type (Target)
10687 if Source_Siz > Target_Siz then
10689 ("\?z?^ high order bits of source will "
10690 & "be ignored!", Eloc);
10692 elsif Is_Unsigned_Type (Source) then
10694 ("\?z?source will be extended with ^ high order "
10695 & "zero bits?!", Eloc);
10699 ("\?z?source will be extended with ^ high order "
10700 & "sign bits!", Eloc);
10703 elsif Source_Siz < Target_Siz then
10704 if Is_Discrete_Type (Target) then
10705 if Bytes_Big_Endian then
10707 ("\?z?target value will include ^ undefined "
10708 & "low order bits!", Eloc);
10711 ("\?z?target value will include ^ undefined "
10712 & "high order bits!", Eloc);
10717 ("\?z?^ trailing bits of target value will be "
10718 & "undefined!", Eloc);
10721 else pragma Assert (Source_Siz > Target_Siz);
10723 ("\?z?^ trailing bits of source will be ignored!",
10730 -- If both types are access types, we need to check the alignment.
10731 -- If the alignment of both is specified, we can do it here.
10733 if Serious_Errors_Detected = 0
10734 and then Ekind (Source) in Access_Kind
10735 and then Ekind (Target) in Access_Kind
10736 and then Target_Strict_Alignment
10737 and then Present (Designated_Type (Source))
10738 and then Present (Designated_Type (Target))
10741 D_Source : constant Entity_Id := Designated_Type (Source);
10742 D_Target : constant Entity_Id := Designated_Type (Target);
10745 if Known_Alignment (D_Source)
10747 Known_Alignment (D_Target)
10750 Source_Align : constant Uint := Alignment (D_Source);
10751 Target_Align : constant Uint := Alignment (D_Target);
10754 if Source_Align < Target_Align
10755 and then not Is_Tagged_Type (D_Source)
10757 -- Suppress warning if warnings suppressed on either
10758 -- type or either designated type. Note the use of
10759 -- OR here instead of OR ELSE. That is intentional,
10760 -- we would like to set flag Warnings_Off_Used in
10761 -- all types for which warnings are suppressed.
10763 and then not (Has_Warnings_Off (D_Source)
10765 Has_Warnings_Off (D_Target)
10767 Has_Warnings_Off (Source)
10769 Has_Warnings_Off (Target))
10771 Error_Msg_Uint_1 := Target_Align;
10772 Error_Msg_Uint_2 := Source_Align;
10773 Error_Msg_Node_1 := D_Target;
10774 Error_Msg_Node_2 := D_Source;
10776 ("?z?alignment of & (^) is stricter than "
10777 & "alignment of & (^)!", Eloc);
10779 ("\?z?resulting access value may have invalid "
10780 & "alignment!", Eloc);
10788 end Validate_Unchecked_Conversions;