1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2012, 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 Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Exp_Disp; use Exp_Disp;
33 with Exp_Tss; use Exp_Tss;
34 with Exp_Util; use Exp_Util;
36 with Lib.Xref; use Lib.Xref;
37 with Namet; use Namet;
38 with Nlists; use Nlists;
39 with Nmake; use Nmake;
41 with Restrict; use Restrict;
42 with Rident; use Rident;
43 with Rtsfind; use Rtsfind;
45 with Sem_Aux; use Sem_Aux;
46 with Sem_Ch3; use Sem_Ch3;
47 with Sem_Ch6; use Sem_Ch6;
48 with Sem_Ch8; use Sem_Ch8;
49 with Sem_Ch9; use Sem_Ch9;
50 with Sem_Dim; use Sem_Dim;
51 with Sem_Disp; use Sem_Disp;
52 with Sem_Eval; use Sem_Eval;
53 with Sem_Res; use Sem_Res;
54 with Sem_Type; use Sem_Type;
55 with Sem_Util; use Sem_Util;
56 with Sem_Warn; use Sem_Warn;
57 with Sinput; use Sinput;
58 with Snames; use Snames;
59 with Stand; use Stand;
60 with Sinfo; use Sinfo;
61 with Stringt; use Stringt;
62 with Targparm; use Targparm;
63 with Ttypes; use Ttypes;
64 with Tbuild; use Tbuild;
65 with Urealp; use Urealp;
66 with Warnsw; use Warnsw;
68 with GNAT.Heap_Sort_G;
70 package body Sem_Ch13 is
72 SSU : constant Pos := System_Storage_Unit;
73 -- Convenient short hand for commonly used constant
75 -----------------------
76 -- Local Subprograms --
77 -----------------------
79 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint);
80 -- This routine is called after setting one of the sizes of type entity
81 -- Typ to Size. The purpose is to deal with the situation of a derived
82 -- type whose inherited alignment is no longer appropriate for the new
83 -- size value. In this case, we reset the Alignment to unknown.
85 procedure Build_Predicate_Function (Typ : Entity_Id; N : Node_Id);
86 -- If Typ has predicates (indicated by Has_Predicates being set for Typ,
87 -- then either there are pragma Predicate entries on the rep chain for the
88 -- type (note that Predicate aspects are converted to pragma Predicate), or
89 -- there are inherited aspects from a parent type, or ancestor subtypes.
90 -- This procedure builds the spec and body for the Predicate function that
91 -- tests these predicates. N is the freeze node for the type. The spec of
92 -- the function is inserted before the freeze node, and the body of the
93 -- function is inserted after the freeze node.
95 procedure Build_Static_Predicate
99 -- Given a predicated type Typ, where Typ is a discrete static subtype,
100 -- whose predicate expression is Expr, tests if Expr is a static predicate,
101 -- and if so, builds the predicate range list. Nam is the name of the one
102 -- argument to the predicate function. Occurrences of the type name in the
103 -- predicate expression have been replaced by identifier references to this
104 -- name, which is unique, so any identifier with Chars matching Nam must be
105 -- a reference to the type. If the predicate is non-static, this procedure
106 -- returns doing nothing. If the predicate is static, then the predicate
107 -- list is stored in Static_Predicate (Typ), and the Expr is rewritten as
108 -- a canonicalized membership operation.
110 function Get_Alignment_Value (Expr : Node_Id) return Uint;
111 -- Given the expression for an alignment value, returns the corresponding
112 -- Uint value. If the value is inappropriate, then error messages are
113 -- posted as required, and a value of No_Uint is returned.
115 function Is_Operational_Item (N : Node_Id) return Boolean;
116 -- A specification for a stream attribute is allowed before the full type
117 -- is declared, as explained in AI-00137 and the corrigendum. Attributes
118 -- that do not specify a representation characteristic are operational
121 procedure New_Stream_Subprogram
125 Nam : TSS_Name_Type);
126 -- Create a subprogram renaming of a given stream attribute to the
127 -- designated subprogram and then in the tagged case, provide this as a
128 -- primitive operation, or in the non-tagged case make an appropriate TSS
129 -- entry. This is more properly an expansion activity than just semantics,
130 -- but the presence of user-defined stream functions for limited types is a
131 -- legality check, which is why this takes place here rather than in
132 -- exp_ch13, where it was previously. Nam indicates the name of the TSS
133 -- function to be generated.
135 -- To avoid elaboration anomalies with freeze nodes, for untagged types
136 -- we generate both a subprogram declaration and a subprogram renaming
137 -- declaration, so that the attribute specification is handled as a
138 -- renaming_as_body. For tagged types, the specification is one of the
142 with procedure Replace_Type_Reference (N : Node_Id);
143 procedure Replace_Type_References_Generic (N : Node_Id; TName : Name_Id);
144 -- This is used to scan an expression for a predicate or invariant aspect
145 -- replacing occurrences of the name TName (the name of the subtype to
146 -- which the aspect applies) with appropriate references to the parameter
147 -- of the predicate function or invariant procedure. The procedure passed
148 -- as a generic parameter does the actual replacement of node N, which is
149 -- either a simple direct reference to TName, or a selected component that
150 -- represents an appropriately qualified occurrence of TName.
156 Biased : Boolean := True);
157 -- If Biased is True, sets Has_Biased_Representation flag for E, and
158 -- outputs a warning message at node N if Warn_On_Biased_Representation is
159 -- is True. This warning inserts the string Msg to describe the construct
162 ----------------------------------------------
163 -- Table for Validate_Unchecked_Conversions --
164 ----------------------------------------------
166 -- The following table collects unchecked conversions for validation.
167 -- Entries are made by Validate_Unchecked_Conversion and then the call
168 -- to Validate_Unchecked_Conversions does the actual error checking and
169 -- posting of warnings. The reason for this delayed processing is to take
170 -- advantage of back-annotations of size and alignment values performed by
173 -- Note: the reason we store a Source_Ptr value instead of a Node_Id is
174 -- that by the time Validate_Unchecked_Conversions is called, Sprint will
175 -- already have modified all Sloc values if the -gnatD option is set.
177 type UC_Entry is record
178 Eloc : Source_Ptr; -- node used for posting warnings
179 Source : Entity_Id; -- source type for unchecked conversion
180 Target : Entity_Id; -- target type for unchecked conversion
183 package Unchecked_Conversions is new Table.Table (
184 Table_Component_Type => UC_Entry,
185 Table_Index_Type => Int,
186 Table_Low_Bound => 1,
188 Table_Increment => 200,
189 Table_Name => "Unchecked_Conversions");
191 ----------------------------------------
192 -- Table for Validate_Address_Clauses --
193 ----------------------------------------
195 -- If an address clause has the form
197 -- for X'Address use Expr
199 -- where Expr is of the form Y'Address or recursively is a reference to a
200 -- constant of either of these forms, and X and Y are entities of objects,
201 -- then if Y has a smaller alignment than X, that merits a warning about
202 -- possible bad alignment. The following table collects address clauses of
203 -- this kind. We put these in a table so that they can be checked after the
204 -- back end has completed annotation of the alignments of objects, since we
205 -- can catch more cases that way.
207 type Address_Clause_Check_Record is record
209 -- The address clause
212 -- The entity of the object overlaying Y
215 -- The entity of the object being overlaid
218 -- Whether the address is offset within Y
221 package Address_Clause_Checks is new Table.Table (
222 Table_Component_Type => Address_Clause_Check_Record,
223 Table_Index_Type => Int,
224 Table_Low_Bound => 1,
226 Table_Increment => 200,
227 Table_Name => "Address_Clause_Checks");
229 -----------------------------------------
230 -- Adjust_Record_For_Reverse_Bit_Order --
231 -----------------------------------------
233 procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is
238 -- Processing depends on version of Ada
240 -- For Ada 95, we just renumber bits within a storage unit. We do the
241 -- same for Ada 83 mode, since we recognize the Bit_Order attribute in
242 -- Ada 83, and are free to add this extension.
244 if Ada_Version < Ada_2005 then
245 Comp := First_Component_Or_Discriminant (R);
246 while Present (Comp) loop
247 CC := Component_Clause (Comp);
249 -- If component clause is present, then deal with the non-default
250 -- bit order case for Ada 95 mode.
252 -- We only do this processing for the base type, and in fact that
253 -- is important, since otherwise if there are record subtypes, we
254 -- could reverse the bits once for each subtype, which is wrong.
256 if Present (CC) and then Ekind (R) = E_Record_Type then
258 CFB : constant Uint := Component_Bit_Offset (Comp);
259 CSZ : constant Uint := Esize (Comp);
260 CLC : constant Node_Id := Component_Clause (Comp);
261 Pos : constant Node_Id := Position (CLC);
262 FB : constant Node_Id := First_Bit (CLC);
264 Storage_Unit_Offset : constant Uint :=
265 CFB / System_Storage_Unit;
267 Start_Bit : constant Uint :=
268 CFB mod System_Storage_Unit;
271 -- Cases where field goes over storage unit boundary
273 if Start_Bit + CSZ > System_Storage_Unit then
275 -- Allow multi-byte field but generate warning
277 if Start_Bit mod System_Storage_Unit = 0
278 and then CSZ mod System_Storage_Unit = 0
281 ("multi-byte field specified with non-standard"
282 & " Bit_Order?", CLC);
284 if Bytes_Big_Endian then
286 ("bytes are not reversed "
287 & "(component is big-endian)?", CLC);
290 ("bytes are not reversed "
291 & "(component is little-endian)?", CLC);
294 -- Do not allow non-contiguous field
298 ("attempt to specify non-contiguous field "
299 & "not permitted", CLC);
301 ("\caused by non-standard Bit_Order "
304 ("\consider possibility of using "
305 & "Ada 2005 mode here", CLC);
308 -- Case where field fits in one storage unit
311 -- Give warning if suspicious component clause
313 if Intval (FB) >= System_Storage_Unit
314 and then Warn_On_Reverse_Bit_Order
317 ("?Bit_Order clause does not affect " &
318 "byte ordering", Pos);
320 Intval (Pos) + Intval (FB) /
323 ("?position normalized to ^ before bit " &
324 "order interpreted", Pos);
327 -- Here is where we fix up the Component_Bit_Offset value
328 -- to account for the reverse bit order. Some examples of
329 -- what needs to be done are:
331 -- First_Bit .. Last_Bit Component_Bit_Offset
343 -- The rule is that the first bit is is obtained by
344 -- subtracting the old ending bit from storage_unit - 1.
346 Set_Component_Bit_Offset
348 (Storage_Unit_Offset * System_Storage_Unit) +
349 (System_Storage_Unit - 1) -
350 (Start_Bit + CSZ - 1));
352 Set_Normalized_First_Bit
354 Component_Bit_Offset (Comp) mod
355 System_Storage_Unit);
360 Next_Component_Or_Discriminant (Comp);
363 -- For Ada 2005, we do machine scalar processing, as fully described In
364 -- AI-133. This involves gathering all components which start at the
365 -- same byte offset and processing them together. Same approach is still
366 -- valid in later versions including Ada 2012.
370 Max_Machine_Scalar_Size : constant Uint :=
372 (Standard_Long_Long_Integer_Size);
373 -- We use this as the maximum machine scalar size
376 SSU : constant Uint := UI_From_Int (System_Storage_Unit);
379 -- This first loop through components does two things. First it
380 -- deals with the case of components with component clauses whose
381 -- length is greater than the maximum machine scalar size (either
382 -- accepting them or rejecting as needed). Second, it counts the
383 -- number of components with component clauses whose length does
384 -- not exceed this maximum for later processing.
387 Comp := First_Component_Or_Discriminant (R);
388 while Present (Comp) loop
389 CC := Component_Clause (Comp);
393 Fbit : constant Uint :=
394 Static_Integer (First_Bit (CC));
395 Lbit : constant Uint :=
396 Static_Integer (Last_Bit (CC));
399 -- Case of component with last bit >= max machine scalar
401 if Lbit >= Max_Machine_Scalar_Size then
403 -- This is allowed only if first bit is zero, and
404 -- last bit + 1 is a multiple of storage unit size.
406 if Fbit = 0 and then (Lbit + 1) mod SSU = 0 then
408 -- This is the case to give a warning if enabled
410 if Warn_On_Reverse_Bit_Order then
412 ("multi-byte field specified with "
413 & " non-standard Bit_Order?", CC);
415 if Bytes_Big_Endian then
417 ("\bytes are not reversed "
418 & "(component is big-endian)?", CC);
421 ("\bytes are not reversed "
422 & "(component is little-endian)?", CC);
426 -- Give error message for RM 13.5.1(10) violation
430 ("machine scalar rules not followed for&",
431 First_Bit (CC), Comp);
433 Error_Msg_Uint_1 := Lbit;
434 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
436 ("\last bit (^) exceeds maximum machine "
440 if (Lbit + 1) mod SSU /= 0 then
441 Error_Msg_Uint_1 := SSU;
443 ("\and is not a multiple of Storage_Unit (^) "
448 Error_Msg_Uint_1 := Fbit;
450 ("\and first bit (^) is non-zero "
456 -- OK case of machine scalar related component clause,
457 -- For now, just count them.
460 Num_CC := Num_CC + 1;
465 Next_Component_Or_Discriminant (Comp);
468 -- We need to sort the component clauses on the basis of the
469 -- Position values in the clause, so we can group clauses with
470 -- the same Position. together to determine the relevant machine
474 Comps : array (0 .. Num_CC) of Entity_Id;
475 -- Array to collect component and discriminant entities. The
476 -- data starts at index 1, the 0'th entry is for the sort
479 function CP_Lt (Op1, Op2 : Natural) return Boolean;
480 -- Compare routine for Sort
482 procedure CP_Move (From : Natural; To : Natural);
483 -- Move routine for Sort
485 package Sorting is new GNAT.Heap_Sort_G (CP_Move, CP_Lt);
489 -- Start and stop positions in the component list of the set of
490 -- components with the same starting position (that constitute
491 -- components in a single machine scalar).
494 -- Maximum last bit value of any component in this set
497 -- Corresponding machine scalar size
503 function CP_Lt (Op1, Op2 : Natural) return Boolean is
505 return Position (Component_Clause (Comps (Op1))) <
506 Position (Component_Clause (Comps (Op2)));
513 procedure CP_Move (From : Natural; To : Natural) is
515 Comps (To) := Comps (From);
518 -- Start of processing for Sort_CC
521 -- Collect the machine scalar relevant component clauses
524 Comp := First_Component_Or_Discriminant (R);
525 while Present (Comp) loop
527 CC : constant Node_Id := Component_Clause (Comp);
530 -- Collect only component clauses whose last bit is less
531 -- than machine scalar size. Any component clause whose
532 -- last bit exceeds this value does not take part in
533 -- machine scalar layout considerations. The test for
534 -- Error_Posted makes sure we exclude component clauses
535 -- for which we already posted an error.
538 and then not Error_Posted (Last_Bit (CC))
539 and then Static_Integer (Last_Bit (CC)) <
540 Max_Machine_Scalar_Size
542 Num_CC := Num_CC + 1;
543 Comps (Num_CC) := Comp;
547 Next_Component_Or_Discriminant (Comp);
550 -- Sort by ascending position number
552 Sorting.Sort (Num_CC);
554 -- We now have all the components whose size does not exceed
555 -- the max machine scalar value, sorted by starting position.
556 -- In this loop we gather groups of clauses starting at the
557 -- same position, to process them in accordance with AI-133.
560 while Stop < Num_CC loop
565 (Last_Bit (Component_Clause (Comps (Start))));
566 while Stop < Num_CC loop
568 (Position (Component_Clause (Comps (Stop + 1)))) =
570 (Position (Component_Clause (Comps (Stop))))
578 (Component_Clause (Comps (Stop)))));
584 -- Now we have a group of component clauses from Start to
585 -- Stop whose positions are identical, and MaxL is the
586 -- maximum last bit value of any of these components.
588 -- We need to determine the corresponding machine scalar
589 -- size. This loop assumes that machine scalar sizes are
590 -- even, and that each possible machine scalar has twice
591 -- as many bits as the next smaller one.
593 MSS := Max_Machine_Scalar_Size;
595 and then (MSS / 2) >= SSU
596 and then (MSS / 2) > MaxL
601 -- Here is where we fix up the Component_Bit_Offset value
602 -- to account for the reverse bit order. Some examples of
603 -- what needs to be done for the case of a machine scalar
606 -- First_Bit .. Last_Bit Component_Bit_Offset
618 -- The rule is that the first bit is obtained by subtracting
619 -- the old ending bit from machine scalar size - 1.
621 for C in Start .. Stop loop
623 Comp : constant Entity_Id := Comps (C);
624 CC : constant Node_Id := Component_Clause (Comp);
626 LB : constant Uint := Static_Integer (Last_Bit (CC));
627 NFB : constant Uint := MSS - Uint_1 - LB;
628 NLB : constant Uint := NFB + Esize (Comp) - 1;
629 Pos : constant Uint := Static_Integer (Position (CC));
632 if Warn_On_Reverse_Bit_Order then
633 Error_Msg_Uint_1 := MSS;
635 ("info: reverse bit order in machine " &
636 "scalar of length^?", First_Bit (CC));
637 Error_Msg_Uint_1 := NFB;
638 Error_Msg_Uint_2 := NLB;
640 if Bytes_Big_Endian then
642 ("?\info: big-endian range for "
643 & "component & is ^ .. ^",
644 First_Bit (CC), Comp);
647 ("?\info: little-endian range "
648 & "for component & is ^ .. ^",
649 First_Bit (CC), Comp);
653 Set_Component_Bit_Offset (Comp, Pos * SSU + NFB);
654 Set_Normalized_First_Bit (Comp, NFB mod SSU);
661 end Adjust_Record_For_Reverse_Bit_Order;
663 -------------------------------------
664 -- Alignment_Check_For_Size_Change --
665 -------------------------------------
667 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint) is
669 -- If the alignment is known, and not set by a rep clause, and is
670 -- inconsistent with the size being set, then reset it to unknown,
671 -- we assume in this case that the size overrides the inherited
672 -- alignment, and that the alignment must be recomputed.
674 if Known_Alignment (Typ)
675 and then not Has_Alignment_Clause (Typ)
676 and then Size mod (Alignment (Typ) * SSU) /= 0
678 Init_Alignment (Typ);
680 end Alignment_Check_For_Size_Change;
682 -------------------------------------
683 -- Analyze_Aspects_At_Freeze_Point --
684 -------------------------------------
686 procedure Analyze_Aspects_At_Freeze_Point (E : Entity_Id) is
691 procedure Analyze_Aspect_Default_Value (ASN : Node_Id);
692 -- This routine analyzes an Aspect_Default_[Component_]Value denoted by
693 -- the aspect specification node ASN.
695 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id);
696 -- Given an aspect specification node ASN whose expression is an
697 -- optional Boolean, this routines creates the corresponding pragma
698 -- at the freezing point.
700 ----------------------------------
701 -- Analyze_Aspect_Default_Value --
702 ----------------------------------
704 procedure Analyze_Aspect_Default_Value (ASN : Node_Id) is
705 Ent : constant Entity_Id := Entity (ASN);
706 Expr : constant Node_Id := Expression (ASN);
707 Id : constant Node_Id := Identifier (ASN);
710 Error_Msg_Name_1 := Chars (Id);
712 if not Is_Type (Ent) then
713 Error_Msg_N ("aspect% can only apply to a type", Id);
716 elsif not Is_First_Subtype (Ent) then
717 Error_Msg_N ("aspect% cannot apply to subtype", Id);
720 elsif A_Id = Aspect_Default_Value
721 and then not Is_Scalar_Type (Ent)
723 Error_Msg_N ("aspect% can only be applied to scalar type", Id);
726 elsif A_Id = Aspect_Default_Component_Value then
727 if not Is_Array_Type (Ent) then
728 Error_Msg_N ("aspect% can only be applied to array type", Id);
731 elsif not Is_Scalar_Type (Component_Type (Ent)) then
732 Error_Msg_N ("aspect% requires scalar components", Id);
737 Set_Has_Default_Aspect (Base_Type (Ent));
739 if Is_Scalar_Type (Ent) then
740 Set_Default_Aspect_Value (Ent, Expr);
742 -- Place default value of base type as well, because that is
743 -- the semantics of the aspect. It is convenient to link the
744 -- aspect to both the (possibly anonymous) base type and to
745 -- the given first subtype.
747 Set_Default_Aspect_Value (Base_Type (Ent), Expr);
750 Set_Default_Aspect_Component_Value (Ent, Expr);
752 end Analyze_Aspect_Default_Value;
754 -------------------------------------
755 -- Make_Pragma_From_Boolean_Aspect --
756 -------------------------------------
758 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id) is
759 Ident : constant Node_Id := Identifier (ASN);
760 A_Name : constant Name_Id := Chars (Ident);
761 A_Id : constant Aspect_Id := Get_Aspect_Id (A_Name);
762 Ent : constant Entity_Id := Entity (ASN);
763 Expr : constant Node_Id := Expression (ASN);
764 Loc : constant Source_Ptr := Sloc (ASN);
768 procedure Check_False_Aspect_For_Derived_Type;
769 -- This procedure checks for the case of a false aspect for a derived
770 -- type, which improperly tries to cancel an aspect inherited from
773 -----------------------------------------
774 -- Check_False_Aspect_For_Derived_Type --
775 -----------------------------------------
777 procedure Check_False_Aspect_For_Derived_Type is
781 -- We are only checking derived types
783 if not Is_Derived_Type (E) then
787 Par := Nearest_Ancestor (E);
790 when Aspect_Atomic | Aspect_Shared =>
791 if not Is_Atomic (Par) then
795 when Aspect_Atomic_Components =>
796 if not Has_Atomic_Components (Par) then
800 when Aspect_Discard_Names =>
801 if not Discard_Names (Par) then
806 if not Is_Packed (Par) then
810 when Aspect_Unchecked_Union =>
811 if not Is_Unchecked_Union (Par) then
815 when Aspect_Volatile =>
816 if not Is_Volatile (Par) then
820 when Aspect_Volatile_Components =>
821 if not Has_Volatile_Components (Par) then
829 -- Fall through means we are canceling an inherited aspect
831 Error_Msg_Name_1 := A_Name;
832 Error_Msg_NE ("derived type& inherits aspect%, cannot cancel",
836 end Check_False_Aspect_For_Derived_Type;
838 -- Start of processing for Make_Pragma_From_Boolean_Aspect
841 if Is_False (Static_Boolean (Expr)) then
842 Check_False_Aspect_For_Derived_Type;
847 Pragma_Argument_Associations => New_List (
848 New_Occurrence_Of (Ent, Sloc (Ident))),
850 Make_Identifier (Sloc (Ident), Chars (Ident)));
852 Set_From_Aspect_Specification (Prag, True);
853 Set_Corresponding_Aspect (Prag, ASN);
854 Set_Aspect_Rep_Item (ASN, Prag);
855 Set_Is_Delayed_Aspect (Prag);
856 Set_Parent (Prag, ASN);
858 end Make_Pragma_From_Boolean_Aspect;
860 -- Start of processing for Analyze_Aspects_At_Freeze_Point
863 -- Must be visible in current scope
865 if not Scope_Within_Or_Same (Current_Scope, Scope (E)) then
869 -- Look for aspect specification entries for this entity
871 ASN := First_Rep_Item (E);
872 while Present (ASN) loop
873 if Nkind (ASN) = N_Aspect_Specification
874 and then Entity (ASN) = E
875 and then Is_Delayed_Aspect (ASN)
877 A_Id := Get_Aspect_Id (Chars (Identifier (ASN)));
881 -- For aspects whose expression is an optional Boolean, make
882 -- the corresponding pragma at the freezing point.
884 when Boolean_Aspects |
885 Library_Unit_Aspects =>
886 Make_Pragma_From_Boolean_Aspect (ASN);
888 -- Special handling for aspects that don't correspond to
889 -- pragmas/attributes.
891 when Aspect_Default_Value |
892 Aspect_Default_Component_Value =>
893 Analyze_Aspect_Default_Value (ASN);
895 -- Ditto for iterator aspects, because the corresponding
896 -- attributes may not have been analyzed yet.
898 when Aspect_Constant_Indexing |
899 Aspect_Variable_Indexing |
900 Aspect_Default_Iterator |
901 Aspect_Iterator_Element =>
902 Analyze (Expression (ASN));
908 Ritem := Aspect_Rep_Item (ASN);
910 if Present (Ritem) then
917 end Analyze_Aspects_At_Freeze_Point;
919 -----------------------------------
920 -- Analyze_Aspect_Specifications --
921 -----------------------------------
923 procedure Analyze_Aspect_Specifications (N : Node_Id; E : Entity_Id) is
928 L : constant List_Id := Aspect_Specifications (N);
930 Ins_Node : Node_Id := N;
931 -- Insert pragmas/attribute definition clause after this node when no
932 -- delayed analysis is required.
934 -- The general processing involves building an attribute definition
935 -- clause or a pragma node that corresponds to the aspect. Then in order
936 -- to delay the evaluation of this aspect to the freeze point, we attach
937 -- the corresponding pragma/attribute definition clause to the aspect
938 -- specification node, which is then placed in the Rep Item chain. In
939 -- this case we mark the entity by setting the flag Has_Delayed_Aspects
940 -- and we evaluate the rep item at the freeze point. When the aspect
941 -- doesn't have a corresponding pragma/attribute definition clause, then
942 -- its analysis is simply delayed at the freeze point.
944 -- Some special cases don't require delay analysis, thus the aspect is
945 -- analyzed right now.
947 -- Note that there is a special handling for
948 -- Pre/Post/Test_Case/Contract_Case aspects. In this case, we do not
949 -- have to worry about delay issues, since the pragmas themselves deal
950 -- with delay of visibility for the expression analysis. Thus, we just
951 -- insert the pragma after the node N.
954 pragma Assert (Present (L));
956 -- Loop through aspects
959 Aspect_Loop : while Present (Aspect) loop
961 Expr : constant Node_Id := Expression (Aspect);
962 Id : constant Node_Id := Identifier (Aspect);
963 Loc : constant Source_Ptr := Sloc (Aspect);
964 Nam : constant Name_Id := Chars (Id);
965 A_Id : constant Aspect_Id := Get_Aspect_Id (Nam);
968 Delay_Required : Boolean := True;
969 -- Set False if delay is not required
971 Eloc : Source_Ptr := No_Location;
972 -- Source location of expression, modified when we split PPC's. It
973 -- is set below when Expr is present.
975 procedure Analyze_Aspect_External_Or_Link_Name;
976 -- This routine performs the analysis of the External_Name or
977 -- Link_Name aspects.
979 procedure Analyze_Aspect_Implicit_Dereference;
980 -- This routine performs the analysis of the Implicit_Dereference
983 ------------------------------------------
984 -- Analyze_Aspect_External_Or_Link_Name --
985 ------------------------------------------
987 procedure Analyze_Aspect_External_Or_Link_Name is
989 -- Verify that there is an Import/Export aspect defined for the
990 -- entity. The processing of that aspect in turn checks that
991 -- there is a Convention aspect declared. The pragma is
992 -- constructed when processing the Convention aspect.
999 while Present (A) loop
1000 exit when Chars (Identifier (A)) = Name_Export
1001 or else Chars (Identifier (A)) = Name_Import;
1007 ("Missing Import/Export for Link/External name",
1011 end Analyze_Aspect_External_Or_Link_Name;
1013 -----------------------------------------
1014 -- Analyze_Aspect_Implicit_Dereference --
1015 -----------------------------------------
1017 procedure Analyze_Aspect_Implicit_Dereference is
1019 if not Is_Type (E) or else not Has_Discriminants (E) then
1021 ("Aspect must apply to a type with discriminants", N);
1028 Disc := First_Discriminant (E);
1029 while Present (Disc) loop
1030 if Chars (Expr) = Chars (Disc)
1031 and then Ekind (Etype (Disc)) =
1032 E_Anonymous_Access_Type
1034 Set_Has_Implicit_Dereference (E);
1035 Set_Has_Implicit_Dereference (Disc);
1039 Next_Discriminant (Disc);
1042 -- Error if no proper access discriminant.
1045 ("not an access discriminant of&", Expr, E);
1048 end Analyze_Aspect_Implicit_Dereference;
1051 -- Skip aspect if already analyzed (not clear if this is needed)
1053 if Analyzed (Aspect) then
1057 -- Set the source location of expression, used in the case of
1058 -- a failed precondition/postcondition or invariant. Note that
1059 -- the source location of the expression is not usually the best
1060 -- choice here. For example, it gets located on the last AND
1061 -- keyword in a chain of boolean expressiond AND'ed together.
1062 -- It is best to put the message on the first character of the
1063 -- assertion, which is the effect of the First_Node call here.
1065 if Present (Expr) then
1066 Eloc := Sloc (First_Node (Expr));
1069 -- Check restriction No_Implementation_Aspect_Specifications
1071 if Impl_Defined_Aspects (A_Id) then
1073 (No_Implementation_Aspect_Specifications, Aspect);
1076 -- Check restriction No_Specification_Of_Aspect
1078 Check_Restriction_No_Specification_Of_Aspect (Aspect);
1080 -- Analyze this aspect
1082 Set_Analyzed (Aspect);
1083 Set_Entity (Aspect, E);
1084 Ent := New_Occurrence_Of (E, Sloc (Id));
1086 -- Check for duplicate aspect. Note that the Comes_From_Source
1087 -- test allows duplicate Pre/Post's that we generate internally
1088 -- to escape being flagged here.
1090 if No_Duplicates_Allowed (A_Id) then
1092 while Anod /= Aspect loop
1094 (A_Id, Get_Aspect_Id (Chars (Identifier (Anod))))
1095 and then Comes_From_Source (Aspect)
1097 Error_Msg_Name_1 := Nam;
1098 Error_Msg_Sloc := Sloc (Anod);
1100 -- Case of same aspect specified twice
1102 if Class_Present (Anod) = Class_Present (Aspect) then
1103 if not Class_Present (Anod) then
1105 ("aspect% for & previously given#",
1109 ("aspect `%''Class` for & previously given#",
1119 -- Check some general restrictions on language defined aspects
1121 if not Impl_Defined_Aspects (A_Id) then
1122 Error_Msg_Name_1 := Nam;
1124 -- Not allowed for renaming declarations
1126 if Nkind (N) in N_Renaming_Declaration then
1128 ("aspect % not allowed for renaming declaration",
1132 -- Not allowed for formal type declarations
1134 if Nkind (N) = N_Formal_Type_Declaration then
1136 ("aspect % not allowed for formal type declaration",
1141 -- Copy expression for later processing by the procedures
1142 -- Check_Aspect_At_[Freeze_Point | End_Of_Declarations]
1144 Set_Entity (Id, New_Copy_Tree (Expr));
1146 -- Processing based on specific aspect
1150 -- No_Aspect should be impossible
1153 raise Program_Error;
1155 -- Case 1: Aspects corresponding to attribute definition
1158 when Aspect_Address |
1161 Aspect_Component_Size |
1162 Aspect_Constant_Indexing |
1163 Aspect_Default_Iterator |
1164 Aspect_Dispatching_Domain |
1165 Aspect_External_Tag |
1167 Aspect_Iterator_Element |
1168 Aspect_Machine_Radix |
1169 Aspect_Object_Size |
1172 Aspect_Scalar_Storage_Order |
1175 Aspect_Simple_Storage_Pool |
1176 Aspect_Storage_Pool |
1177 Aspect_Storage_Size |
1178 Aspect_Stream_Size |
1180 Aspect_Variable_Indexing |
1183 -- Indexing aspects apply only to tagged type
1185 if (A_Id = Aspect_Constant_Indexing
1186 or else A_Id = Aspect_Variable_Indexing)
1187 and then not (Is_Type (E)
1188 and then Is_Tagged_Type (E))
1190 Error_Msg_N ("indexing applies to a tagged type", N);
1194 -- Construct the attribute definition clause
1197 Make_Attribute_Definition_Clause (Loc,
1199 Chars => Chars (Id),
1200 Expression => Relocate_Node (Expr));
1202 -- Case 2: Aspects cooresponding to pragmas
1204 -- Case 2a: Aspects corresponding to pragmas with two
1205 -- arguments, where the first argument is a local name
1206 -- referring to the entity, and the second argument is the
1207 -- aspect definition expression.
1209 when Aspect_Suppress |
1210 Aspect_Unsuppress =>
1212 -- Construct the pragma
1216 Pragma_Argument_Associations => New_List (
1217 New_Occurrence_Of (E, Loc),
1218 Relocate_Node (Expr)),
1219 Pragma_Identifier =>
1220 Make_Identifier (Sloc (Id), Chars (Id)));
1222 when Aspect_Synchronization =>
1224 -- The aspect corresponds to pragma Implemented.
1225 -- Construct the pragma
1229 Pragma_Argument_Associations => New_List (
1230 New_Occurrence_Of (E, Loc),
1231 Relocate_Node (Expr)),
1232 Pragma_Identifier =>
1233 Make_Identifier (Sloc (Id), Name_Implemented));
1235 -- No delay is required since the only values are: By_Entry
1236 -- | By_Protected_Procedure | By_Any | Optional which don't
1237 -- get analyzed anyway.
1239 Delay_Required := False;
1241 when Aspect_Attach_Handler =>
1244 Pragma_Identifier =>
1245 Make_Identifier (Sloc (Id), Name_Attach_Handler),
1246 Pragma_Argument_Associations =>
1247 New_List (Ent, Relocate_Node (Expr)));
1249 when Aspect_Dynamic_Predicate |
1251 Aspect_Static_Predicate =>
1253 -- Construct the pragma (always a pragma Predicate, with
1254 -- flags recording whether it is static/dynamic).
1258 Pragma_Argument_Associations =>
1259 New_List (Ent, Relocate_Node (Expr)),
1260 Class_Present => Class_Present (Aspect),
1261 Pragma_Identifier =>
1262 Make_Identifier (Sloc (Id), Name_Predicate));
1264 -- If the type is private, indicate that its completion
1265 -- has a freeze node, because that is the one that will be
1266 -- visible at freeze time.
1268 Set_Has_Predicates (E);
1270 if Is_Private_Type (E)
1271 and then Present (Full_View (E))
1273 Set_Has_Predicates (Full_View (E));
1274 Set_Has_Delayed_Aspects (Full_View (E));
1275 Ensure_Freeze_Node (Full_View (E));
1278 -- Case 2b: Aspects corresponding to pragmas with two
1279 -- arguments, where the second argument is a local name
1280 -- referring to the entity, and the first argument is the
1281 -- aspect definition expression.
1283 when Aspect_Convention =>
1285 -- The aspect may be part of the specification of an import
1286 -- or export pragma. Scan the aspect list to gather the
1287 -- other components, if any. The name of the generated
1288 -- pragma is one of Convention/Import/Export.
1300 P_Name := Chars (Id);
1302 Arg_List := New_List;
1307 while Present (A) loop
1308 A_Name := Chars (Identifier (A));
1310 if A_Name = Name_Import
1312 A_Name = Name_Export
1315 Error_Msg_N ("conflicting", A);
1322 elsif A_Name = Name_Link_Name then
1323 L_Assoc := Make_Pragma_Argument_Association (Loc,
1325 Expression => Relocate_Node (Expression (A)));
1327 elsif A_Name = Name_External_Name then
1328 E_Assoc := Make_Pragma_Argument_Association (Loc,
1330 Expression => Relocate_Node (Expression (A)));
1336 Arg_List := New_List (Relocate_Node (Expr), Ent);
1338 if Present (L_Assoc) then
1339 Append_To (Arg_List, L_Assoc);
1342 if Present (E_Assoc) then
1343 Append_To (Arg_List, E_Assoc);
1348 Pragma_Argument_Associations => Arg_List,
1349 Pragma_Identifier =>
1350 Make_Identifier (Loc, P_Name));
1353 -- The following three aspects can be specified for a
1354 -- subprogram body, in which case we generate pragmas for them
1355 -- and insert them ahead of local declarations, rather than
1359 Aspect_Interrupt_Priority |
1361 if Nkind (N) = N_Subprogram_Body then
1364 Pragma_Argument_Associations =>
1365 New_List (Relocate_Node (Expr)),
1366 Pragma_Identifier =>
1367 Make_Identifier (Sloc (Id), Chars (Id)));
1370 Make_Attribute_Definition_Clause (Loc,
1372 Chars => Chars (Id),
1373 Expression => Relocate_Node (Expr));
1376 when Aspect_Warnings =>
1378 -- Construct the pragma
1382 Pragma_Argument_Associations => New_List (
1383 Relocate_Node (Expr),
1384 New_Occurrence_Of (E, Loc)),
1385 Pragma_Identifier =>
1386 Make_Identifier (Sloc (Id), Chars (Id)),
1387 Class_Present => Class_Present (Aspect));
1389 -- We don't have to play the delay game here, since the only
1390 -- values are ON/OFF which don't get analyzed anyway.
1392 Delay_Required := False;
1394 -- Case 2c: Aspects corresponding to pragmas with three
1397 -- Invariant aspects have a first argument that references the
1398 -- entity, a second argument that is the expression and a third
1399 -- argument that is an appropriate message.
1401 when Aspect_Invariant |
1402 Aspect_Type_Invariant =>
1404 -- Analysis of the pragma will verify placement legality:
1405 -- an invariant must apply to a private type, or appear in
1406 -- the private part of a spec and apply to a completion.
1408 -- Construct the pragma
1412 Pragma_Argument_Associations =>
1413 New_List (Ent, Relocate_Node (Expr)),
1414 Class_Present => Class_Present (Aspect),
1415 Pragma_Identifier =>
1416 Make_Identifier (Sloc (Id), Name_Invariant));
1418 -- Add message unless exception messages are suppressed
1420 if not Opt.Exception_Locations_Suppressed then
1421 Append_To (Pragma_Argument_Associations (Aitem),
1422 Make_Pragma_Argument_Association (Eloc,
1423 Chars => Name_Message,
1425 Make_String_Literal (Eloc,
1426 Strval => "failed invariant from "
1427 & Build_Location_String (Eloc))));
1430 -- For Invariant case, insert immediately after the entity
1431 -- declaration. We do not have to worry about delay issues
1432 -- since the pragma processing takes care of this.
1434 Delay_Required := False;
1436 -- Case 2d : Aspects that correspond to a pragma with one
1439 when Aspect_Relative_Deadline =>
1442 Pragma_Argument_Associations =>
1444 Make_Pragma_Argument_Association (Loc,
1445 Expression => Relocate_Node (Expr))),
1446 Pragma_Identifier =>
1447 Make_Identifier (Sloc (Id), Name_Relative_Deadline));
1449 -- If the aspect applies to a task, the corresponding pragma
1450 -- must appear within its declarations, not after.
1452 if Nkind (N) = N_Task_Type_Declaration then
1458 if No (Task_Definition (N)) then
1459 Set_Task_Definition (N,
1460 Make_Task_Definition (Loc,
1461 Visible_Declarations => New_List,
1462 End_Label => Empty));
1465 Def := Task_Definition (N);
1466 V := Visible_Declarations (Def);
1467 if not Is_Empty_List (V) then
1468 Insert_Before (First (V), Aitem);
1471 Set_Visible_Declarations (Def, New_List (Aitem));
1478 -- Case 3 : Aspects that don't correspond to pragma/attribute
1479 -- definition clause.
1481 -- Case 3a: The aspects listed below don't correspond to
1482 -- pragmas/attributes but do require delayed analysis.
1484 when Aspect_Default_Value |
1485 Aspect_Default_Component_Value =>
1488 -- Case 3b: The aspects listed below don't correspond to
1489 -- pragmas/attributes and don't need delayed analysis.
1491 -- For Implicit_Dereference, External_Name and Link_Name, only
1492 -- the legality checks are done during the analysis, thus no
1493 -- delay is required.
1495 when Aspect_Implicit_Dereference =>
1496 Analyze_Aspect_Implicit_Dereference;
1499 when Aspect_External_Name |
1501 Analyze_Aspect_External_Or_Link_Name;
1504 when Aspect_Dimension =>
1505 Analyze_Aspect_Dimension (N, Id, Expr);
1508 when Aspect_Dimension_System =>
1509 Analyze_Aspect_Dimension_System (N, Id, Expr);
1512 -- Case 4: Special handling for aspects
1513 -- Pre/Post/Test_Case/Contract_Case whose corresponding pragmas
1514 -- take care of the delay.
1516 -- Aspects Pre/Post generate Precondition/Postcondition pragmas
1517 -- with a first argument that is the expression, and a second
1518 -- argument that is an informative message if the test fails.
1519 -- This is inserted right after the declaration, to get the
1520 -- required pragma placement. The processing for the pragmas
1521 -- takes care of the required delay.
1523 when Pre_Post_Aspects => declare
1527 if A_Id = Aspect_Pre or else A_Id = Aspect_Precondition then
1528 Pname := Name_Precondition;
1530 Pname := Name_Postcondition;
1533 -- If the expressions is of the form A and then B, then
1534 -- we generate separate Pre/Post aspects for the separate
1535 -- clauses. Since we allow multiple pragmas, there is no
1536 -- problem in allowing multiple Pre/Post aspects internally.
1537 -- These should be treated in reverse order (B first and
1538 -- A second) since they are later inserted just after N in
1539 -- the order they are treated. This way, the pragma for A
1540 -- ends up preceding the pragma for B, which may have an
1541 -- importance for the error raised (either constraint error
1542 -- or precondition error).
1544 -- We do not do this for Pre'Class, since we have to put
1545 -- these conditions together in a complex OR expression
1547 -- We do not do this in ASIS mode, as ASIS relies on the
1548 -- original node representing the complete expression, when
1549 -- retrieving it through the source aspect table.
1552 and then (Pname = Name_Postcondition
1553 or else not Class_Present (Aspect))
1555 while Nkind (Expr) = N_And_Then loop
1556 Insert_After (Aspect,
1557 Make_Aspect_Specification (Sloc (Left_Opnd (Expr)),
1558 Identifier => Identifier (Aspect),
1559 Expression => Relocate_Node (Left_Opnd (Expr)),
1560 Class_Present => Class_Present (Aspect),
1561 Split_PPC => True));
1562 Rewrite (Expr, Relocate_Node (Right_Opnd (Expr)));
1563 Eloc := Sloc (Expr);
1567 -- Build the precondition/postcondition pragma
1571 Pragma_Identifier =>
1572 Make_Identifier (Sloc (Id), Pname),
1573 Class_Present => Class_Present (Aspect),
1574 Split_PPC => Split_PPC (Aspect),
1575 Pragma_Argument_Associations => New_List (
1576 Make_Pragma_Argument_Association (Eloc,
1577 Chars => Name_Check,
1578 Expression => Relocate_Node (Expr))));
1580 -- Add message unless exception messages are suppressed
1582 if not Opt.Exception_Locations_Suppressed then
1583 Append_To (Pragma_Argument_Associations (Aitem),
1584 Make_Pragma_Argument_Association (Eloc,
1585 Chars => Name_Message,
1587 Make_String_Literal (Eloc,
1589 & Get_Name_String (Pname)
1591 & Build_Location_String (Eloc))));
1594 Set_From_Aspect_Specification (Aitem, True);
1595 Set_Corresponding_Aspect (Aitem, Aspect);
1596 Set_Is_Delayed_Aspect (Aspect);
1598 -- For Pre/Post cases, insert immediately after the entity
1599 -- declaration, since that is the required pragma placement.
1600 -- Note that for these aspects, we do not have to worry
1601 -- about delay issues, since the pragmas themselves deal
1602 -- with delay of visibility for the expression analysis.
1604 -- If the entity is a library-level subprogram, the pre/
1605 -- postconditions must be treated as late pragmas. Note
1606 -- that they must be prepended, not appended, to the list,
1607 -- so that split AND THEN sections are processed in the
1610 if Nkind (Parent (N)) = N_Compilation_Unit then
1612 Aux : constant Node_Id := Aux_Decls_Node (Parent (N));
1614 if No (Pragmas_After (Aux)) then
1615 Set_Pragmas_After (Aux, New_List);
1618 Prepend (Aitem, Pragmas_After (Aux));
1621 -- If it is a subprogram body, add pragmas to list of
1622 -- declarations in body.
1624 elsif Nkind (N) = N_Subprogram_Body then
1625 if No (Declarations (N)) then
1626 Set_Declarations (N, New_List);
1629 Append (Aitem, Declarations (N));
1632 Insert_After (N, Aitem);
1638 when Aspect_Contract_Case |
1642 Comp_Expr : Node_Id;
1643 Comp_Assn : Node_Id;
1649 if Nkind (Parent (N)) = N_Compilation_Unit then
1650 Error_Msg_Name_1 := Nam;
1651 Error_Msg_N ("incorrect placement of aspect `%`", E);
1655 if Nkind (Expr) /= N_Aggregate then
1656 Error_Msg_Name_1 := Nam;
1658 ("wrong syntax for aspect `%` for &", Id, E);
1662 -- Make pragma expressions refer to the original aspect
1663 -- expressions through the Original_Node link. This is
1664 -- used in semantic analysis for ASIS mode, so that the
1665 -- original expression also gets analyzed.
1667 Comp_Expr := First (Expressions (Expr));
1668 while Present (Comp_Expr) loop
1669 New_Expr := Relocate_Node (Comp_Expr);
1670 Set_Original_Node (New_Expr, Comp_Expr);
1672 (Make_Pragma_Argument_Association (Sloc (Comp_Expr),
1673 Expression => New_Expr),
1678 Comp_Assn := First (Component_Associations (Expr));
1679 while Present (Comp_Assn) loop
1680 if List_Length (Choices (Comp_Assn)) /= 1
1682 Nkind (First (Choices (Comp_Assn))) /= N_Identifier
1684 Error_Msg_Name_1 := Nam;
1686 ("wrong syntax for aspect `%` for &", Id, E);
1690 New_Expr := Relocate_Node (Expression (Comp_Assn));
1691 Set_Original_Node (New_Expr, Expression (Comp_Assn));
1692 Append (Make_Pragma_Argument_Association (
1693 Sloc => Sloc (Comp_Assn),
1694 Chars => Chars (First (Choices (Comp_Assn))),
1695 Expression => New_Expr),
1700 -- Build the contract-case or test-case pragma
1704 Pragma_Identifier =>
1705 Make_Identifier (Sloc (Id), Nam),
1706 Pragma_Argument_Associations => Args);
1708 Delay_Required := False;
1711 when Aspect_Contract_Cases => Contract_Cases : declare
1712 Case_Guard : Node_Id;
1714 Others_Seen : Boolean := False;
1715 Post_Case : Node_Id;
1718 if Nkind (Parent (N)) = N_Compilation_Unit then
1719 Error_Msg_Name_1 := Nam;
1720 Error_Msg_N ("incorrect placement of aspect `%`", E);
1724 if Nkind (Expr) /= N_Aggregate then
1725 Error_Msg_Name_1 := Nam;
1727 ("wrong syntax for aspect `%` for &", Id, E);
1731 -- Verify the legality of individual post cases
1733 Post_Case := First (Component_Associations (Expr));
1734 while Present (Post_Case) loop
1735 if Nkind (Post_Case) /= N_Component_Association then
1736 Error_Msg_N ("wrong syntax in post case", Post_Case);
1740 -- Each post case must have exactly one case guard
1742 Case_Guard := First (Choices (Post_Case));
1743 Extra := Next (Case_Guard);
1745 if Present (Extra) then
1747 ("post case may have only one case guard", Extra);
1751 -- Check the placement of "others" (if available)
1753 if Nkind (Case_Guard) = N_Others_Choice then
1755 Error_Msg_Name_1 := Nam;
1757 ("only one others choice allowed in aspect %",
1761 Others_Seen := True;
1764 elsif Others_Seen then
1765 Error_Msg_Name_1 := Nam;
1767 ("others must be the last choice in aspect %", N);
1774 -- Transform the aspect into a pragma
1778 Pragma_Identifier =>
1779 Make_Identifier (Loc, Nam),
1780 Pragma_Argument_Associations => New_List (
1781 Make_Pragma_Argument_Association (Loc,
1782 Expression => Relocate_Node (Expr))));
1784 Delay_Required := False;
1787 -- Case 5: Special handling for aspects with an optional
1788 -- boolean argument.
1790 -- In the general case, the corresponding pragma cannot be
1791 -- generated yet because the evaluation of the boolean needs to
1792 -- be delayed til the freeze point.
1794 when Boolean_Aspects |
1795 Library_Unit_Aspects =>
1797 Set_Is_Boolean_Aspect (Aspect);
1799 -- Lock_Free aspect only apply to protected objects
1801 if A_Id = Aspect_Lock_Free then
1802 if Ekind (E) /= E_Protected_Type then
1803 Error_Msg_Name_1 := Nam;
1805 ("aspect % only applies to a protected object",
1809 -- Set the Uses_Lock_Free flag to True if there is no
1810 -- expression or if the expression is True. ??? The
1811 -- evaluation of this aspect should be delayed to the
1815 or else Is_True (Static_Boolean (Expr))
1817 Set_Uses_Lock_Free (E);
1820 Record_Rep_Item (E, Aspect);
1825 elsif A_Id = Aspect_Import or else A_Id = Aspect_Export then
1827 -- Verify that there is an aspect Convention that will
1828 -- incorporate the Import/Export aspect, and eventual
1829 -- Link/External names.
1836 while Present (A) loop
1837 exit when Chars (Identifier (A)) = Name_Convention;
1843 ("missing Convention aspect for Export/Import",
1851 -- This requires special handling in the case of a package
1852 -- declaration, the pragma needs to be inserted in the list
1853 -- of declarations for the associated package. There is no
1854 -- issue of visibility delay for these aspects.
1856 if A_Id in Library_Unit_Aspects
1857 and then Nkind (N) = N_Package_Declaration
1858 and then Nkind (Parent (N)) /= N_Compilation_Unit
1861 ("incorrect context for library unit aspect&", Id);
1865 -- Special handling when the aspect has no expression. In
1866 -- this case the value is considered to be True. Thus, we
1867 -- simply insert the pragma, no delay is required.
1872 Pragma_Argument_Associations => New_List (Ent),
1873 Pragma_Identifier =>
1874 Make_Identifier (Sloc (Id), Chars (Id)));
1876 Delay_Required := False;
1878 -- In general cases, the corresponding pragma/attribute
1879 -- definition clause will be inserted later at the freezing
1887 -- Attach the corresponding pragma/attribute definition clause to
1888 -- the aspect specification node.
1890 if Present (Aitem) then
1891 Set_From_Aspect_Specification (Aitem, True);
1893 if Nkind (Aitem) = N_Pragma then
1894 Set_Corresponding_Aspect (Aitem, Aspect);
1898 -- In the context of a compilation unit, we directly put the
1899 -- pragma in the Pragmas_After list of the
1900 -- N_Compilation_Unit_Aux node (no delay is required here)
1901 -- except for aspects on a subprogram body (see below).
1903 if Nkind (Parent (N)) = N_Compilation_Unit
1904 and then (Present (Aitem) or else Is_Boolean_Aspect (Aspect))
1907 Aux : constant Node_Id := Aux_Decls_Node (Parent (N));
1910 pragma Assert (Nkind (Aux) = N_Compilation_Unit_Aux);
1912 -- For a Boolean aspect, create the corresponding pragma if
1913 -- no expression or if the value is True.
1915 if Is_Boolean_Aspect (Aspect) and then No (Aitem) then
1916 if Is_True (Static_Boolean (Expr)) then
1919 Pragma_Argument_Associations => New_List (Ent),
1920 Pragma_Identifier =>
1921 Make_Identifier (Sloc (Id), Chars (Id)));
1923 Set_From_Aspect_Specification (Aitem, True);
1924 Set_Corresponding_Aspect (Aitem, Aspect);
1931 -- If the aspect is on a subprogram body (relevant aspects
1932 -- are Inline and Priority), add the pragma in front of
1933 -- the declarations.
1935 if Nkind (N) = N_Subprogram_Body then
1936 if No (Declarations (N)) then
1937 Set_Declarations (N, New_List);
1940 Prepend (Aitem, Declarations (N));
1943 if No (Pragmas_After (Aux)) then
1944 Set_Pragmas_After (Aux, New_List);
1947 Append (Aitem, Pragmas_After (Aux));
1954 -- The evaluation of the aspect is delayed to the freezing point.
1955 -- The pragma or attribute clause if there is one is then attached
1956 -- to the aspect specification which is placed in the rep item
1959 if Delay_Required then
1960 if Present (Aitem) then
1961 Set_Is_Delayed_Aspect (Aitem);
1962 Set_Aspect_Rep_Item (Aspect, Aitem);
1963 Set_Parent (Aitem, Aspect);
1966 Set_Is_Delayed_Aspect (Aspect);
1968 -- In the case of Default_Value, link aspect to base type
1969 -- as well, even though it appears on a first subtype. This
1970 -- is mandated by the semantics of the aspect. Verify that
1971 -- this a scalar type, to prevent cascaded errors.
1973 if A_Id = Aspect_Default_Value and then Is_Scalar_Type (E) then
1974 Set_Has_Delayed_Aspects (Base_Type (E));
1975 Record_Rep_Item (Base_Type (E), Aspect);
1978 Set_Has_Delayed_Aspects (E);
1979 Record_Rep_Item (E, Aspect);
1981 -- When delay is not required and the context is not a compilation
1982 -- unit, we simply insert the pragma/attribute definition clause
1986 Insert_After (Ins_Node, Aitem);
1993 end loop Aspect_Loop;
1995 if Has_Delayed_Aspects (E) then
1996 Ensure_Freeze_Node (E);
1998 end Analyze_Aspect_Specifications;
2000 -----------------------
2001 -- Analyze_At_Clause --
2002 -----------------------
2004 -- An at clause is replaced by the corresponding Address attribute
2005 -- definition clause that is the preferred approach in Ada 95.
2007 procedure Analyze_At_Clause (N : Node_Id) is
2008 CS : constant Boolean := Comes_From_Source (N);
2011 -- This is an obsolescent feature
2013 Check_Restriction (No_Obsolescent_Features, N);
2015 if Warn_On_Obsolescent_Feature then
2017 ("at clause is an obsolescent feature (RM J.7(2))?", N);
2019 ("\use address attribute definition clause instead?", N);
2022 -- Rewrite as address clause
2025 Make_Attribute_Definition_Clause (Sloc (N),
2026 Name => Identifier (N),
2027 Chars => Name_Address,
2028 Expression => Expression (N)));
2030 -- We preserve Comes_From_Source, since logically the clause still comes
2031 -- from the source program even though it is changed in form.
2033 Set_Comes_From_Source (N, CS);
2035 -- Analyze rewritten clause
2037 Analyze_Attribute_Definition_Clause (N);
2038 end Analyze_At_Clause;
2040 -----------------------------------------
2041 -- Analyze_Attribute_Definition_Clause --
2042 -----------------------------------------
2044 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
2045 Loc : constant Source_Ptr := Sloc (N);
2046 Nam : constant Node_Id := Name (N);
2047 Attr : constant Name_Id := Chars (N);
2048 Expr : constant Node_Id := Expression (N);
2049 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
2052 -- The entity of Nam after it is analyzed. In the case of an incomplete
2053 -- type, this is the underlying type.
2056 -- The underlying entity to which the attribute applies. Generally this
2057 -- is the Underlying_Type of Ent, except in the case where the clause
2058 -- applies to full view of incomplete type or private type in which case
2059 -- U_Ent is just a copy of Ent.
2061 FOnly : Boolean := False;
2062 -- Reset to True for subtype specific attribute (Alignment, Size)
2063 -- and for stream attributes, i.e. those cases where in the call
2064 -- to Rep_Item_Too_Late, FOnly is set True so that only the freezing
2065 -- rules are checked. Note that the case of stream attributes is not
2066 -- clear from the RM, but see AI95-00137. Also, the RM seems to
2067 -- disallow Storage_Size for derived task types, but that is also
2068 -- clearly unintentional.
2070 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
2071 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
2072 -- definition clauses.
2074 function Duplicate_Clause return Boolean;
2075 -- This routine checks if the aspect for U_Ent being given by attribute
2076 -- definition clause N is for an aspect that has already been specified,
2077 -- and if so gives an error message. If there is a duplicate, True is
2078 -- returned, otherwise if there is no error, False is returned.
2080 procedure Check_Indexing_Functions;
2081 -- Check that the function in Constant_Indexing or Variable_Indexing
2082 -- attribute has the proper type structure. If the name is overloaded,
2083 -- check that some interpretation is legal.
2085 procedure Check_Iterator_Functions;
2086 -- Check that there is a single function in Default_Iterator attribute
2087 -- has the proper type structure.
2089 function Check_Primitive_Function (Subp : Entity_Id) return Boolean;
2090 -- Common legality check for the previous two
2092 -----------------------------------
2093 -- Analyze_Stream_TSS_Definition --
2094 -----------------------------------
2096 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
2097 Subp : Entity_Id := Empty;
2102 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
2103 -- True for Read attribute, false for other attributes
2105 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
2106 -- Return true if the entity is a subprogram with an appropriate
2107 -- profile for the attribute being defined.
2109 ----------------------
2110 -- Has_Good_Profile --
2111 ----------------------
2113 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
2115 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
2116 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
2117 (False => E_Procedure, True => E_Function);
2121 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
2125 F := First_Formal (Subp);
2128 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
2129 or else Designated_Type (Etype (F)) /=
2130 Class_Wide_Type (RTE (RE_Root_Stream_Type))
2135 if not Is_Function then
2139 Expected_Mode : constant array (Boolean) of Entity_Kind :=
2140 (False => E_In_Parameter,
2141 True => E_Out_Parameter);
2143 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
2151 Typ := Etype (Subp);
2154 return Base_Type (Typ) = Base_Type (Ent)
2155 and then No (Next_Formal (F));
2156 end Has_Good_Profile;
2158 -- Start of processing for Analyze_Stream_TSS_Definition
2163 if not Is_Type (U_Ent) then
2164 Error_Msg_N ("local name must be a subtype", Nam);
2168 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
2170 -- If Pnam is present, it can be either inherited from an ancestor
2171 -- type (in which case it is legal to redefine it for this type), or
2172 -- be a previous definition of the attribute for the same type (in
2173 -- which case it is illegal).
2175 -- In the first case, it will have been analyzed already, and we
2176 -- can check that its profile does not match the expected profile
2177 -- for a stream attribute of U_Ent. In the second case, either Pnam
2178 -- has been analyzed (and has the expected profile), or it has not
2179 -- been analyzed yet (case of a type that has not been frozen yet
2180 -- and for which the stream attribute has been set using Set_TSS).
2183 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
2185 Error_Msg_Sloc := Sloc (Pnam);
2186 Error_Msg_Name_1 := Attr;
2187 Error_Msg_N ("% attribute already defined #", Nam);
2193 if Is_Entity_Name (Expr) then
2194 if not Is_Overloaded (Expr) then
2195 if Has_Good_Profile (Entity (Expr)) then
2196 Subp := Entity (Expr);
2200 Get_First_Interp (Expr, I, It);
2201 while Present (It.Nam) loop
2202 if Has_Good_Profile (It.Nam) then
2207 Get_Next_Interp (I, It);
2212 if Present (Subp) then
2213 if Is_Abstract_Subprogram (Subp) then
2214 Error_Msg_N ("stream subprogram must not be abstract", Expr);
2218 Set_Entity (Expr, Subp);
2219 Set_Etype (Expr, Etype (Subp));
2221 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
2224 Error_Msg_Name_1 := Attr;
2225 Error_Msg_N ("incorrect expression for% attribute", Expr);
2227 end Analyze_Stream_TSS_Definition;
2229 ------------------------------
2230 -- Check_Indexing_Functions --
2231 ------------------------------
2233 procedure Check_Indexing_Functions is
2234 Indexing_Found : Boolean;
2236 procedure Check_One_Function (Subp : Entity_Id);
2237 -- Check one possible interpretation. Sets Indexing_Found True if an
2238 -- indexing function is found.
2240 ------------------------
2241 -- Check_One_Function --
2242 ------------------------
2244 procedure Check_One_Function (Subp : Entity_Id) is
2245 Default_Element : constant Node_Id :=
2247 (Etype (First_Formal (Subp)),
2248 Aspect_Iterator_Element);
2251 if not Check_Primitive_Function (Subp)
2252 and then not Is_Overloaded (Expr)
2255 ("aspect Indexing requires a function that applies to type&",
2259 -- An indexing function must return either the default element of
2260 -- the container, or a reference type. For variable indexing it
2261 -- must be the latter.
2263 if Present (Default_Element) then
2264 Analyze (Default_Element);
2266 if Is_Entity_Name (Default_Element)
2267 and then Covers (Entity (Default_Element), Etype (Subp))
2269 Indexing_Found := True;
2274 -- For variable_indexing the return type must be a reference type
2276 if Attr = Name_Variable_Indexing
2277 and then not Has_Implicit_Dereference (Etype (Subp))
2280 ("function for indexing must return a reference type", Subp);
2283 Indexing_Found := True;
2285 end Check_One_Function;
2287 -- Start of processing for Check_Indexing_Functions
2296 if not Is_Overloaded (Expr) then
2297 Check_One_Function (Entity (Expr));
2305 Indexing_Found := False;
2306 Get_First_Interp (Expr, I, It);
2307 while Present (It.Nam) loop
2309 -- Note that analysis will have added the interpretation
2310 -- that corresponds to the dereference. We only check the
2311 -- subprogram itself.
2313 if Is_Overloadable (It.Nam) then
2314 Check_One_Function (It.Nam);
2317 Get_Next_Interp (I, It);
2320 if not Indexing_Found then
2322 ("aspect Indexing requires a function that "
2323 & "applies to type&", Expr, Ent);
2327 end Check_Indexing_Functions;
2329 ------------------------------
2330 -- Check_Iterator_Functions --
2331 ------------------------------
2333 procedure Check_Iterator_Functions is
2334 Default : Entity_Id;
2336 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean;
2337 -- Check one possible interpretation for validity
2339 ----------------------------
2340 -- Valid_Default_Iterator --
2341 ----------------------------
2343 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean is
2347 if not Check_Primitive_Function (Subp) then
2350 Formal := First_Formal (Subp);
2353 -- False if any subsequent formal has no default expression
2355 Formal := Next_Formal (Formal);
2356 while Present (Formal) loop
2357 if No (Expression (Parent (Formal))) then
2361 Next_Formal (Formal);
2364 -- True if all subsequent formals have default expressions
2367 end Valid_Default_Iterator;
2369 -- Start of processing for Check_Iterator_Functions
2374 if not Is_Entity_Name (Expr) then
2375 Error_Msg_N ("aspect Iterator must be a function name", Expr);
2378 if not Is_Overloaded (Expr) then
2379 if not Check_Primitive_Function (Entity (Expr)) then
2381 ("aspect Indexing requires a function that applies to type&",
2382 Entity (Expr), Ent);
2385 if not Valid_Default_Iterator (Entity (Expr)) then
2386 Error_Msg_N ("improper function for default iterator", Expr);
2396 Get_First_Interp (Expr, I, It);
2397 while Present (It.Nam) loop
2398 if not Check_Primitive_Function (It.Nam)
2399 or else not Valid_Default_Iterator (It.Nam)
2403 elsif Present (Default) then
2404 Error_Msg_N ("default iterator must be unique", Expr);
2410 Get_Next_Interp (I, It);
2414 if Present (Default) then
2415 Set_Entity (Expr, Default);
2416 Set_Is_Overloaded (Expr, False);
2419 end Check_Iterator_Functions;
2421 -------------------------------
2422 -- Check_Primitive_Function --
2423 -------------------------------
2425 function Check_Primitive_Function (Subp : Entity_Id) return Boolean is
2429 if Ekind (Subp) /= E_Function then
2433 if No (First_Formal (Subp)) then
2436 Ctrl := Etype (First_Formal (Subp));
2440 or else Ctrl = Class_Wide_Type (Ent)
2442 (Ekind (Ctrl) = E_Anonymous_Access_Type
2444 (Designated_Type (Ctrl) = Ent
2445 or else Designated_Type (Ctrl) = Class_Wide_Type (Ent)))
2454 end Check_Primitive_Function;
2456 ----------------------
2457 -- Duplicate_Clause --
2458 ----------------------
2460 function Duplicate_Clause return Boolean is
2464 -- Nothing to do if this attribute definition clause comes from
2465 -- an aspect specification, since we could not be duplicating an
2466 -- explicit clause, and we dealt with the case of duplicated aspects
2467 -- in Analyze_Aspect_Specifications.
2469 if From_Aspect_Specification (N) then
2473 -- Otherwise current clause may duplicate previous clause, or a
2474 -- previously given pragma or aspect specification for the same
2477 A := Get_Rep_Item (U_Ent, Chars (N), Check_Parents => False);
2480 Error_Msg_Name_1 := Chars (N);
2481 Error_Msg_Sloc := Sloc (A);
2483 Error_Msg_NE ("aspect% for & previously given#", N, U_Ent);
2488 end Duplicate_Clause;
2490 -- Start of processing for Analyze_Attribute_Definition_Clause
2493 -- The following code is a defense against recursion. Not clear that
2494 -- this can happen legitimately, but perhaps some error situations
2495 -- can cause it, and we did see this recursion during testing.
2497 if Analyzed (N) then
2500 Set_Analyzed (N, True);
2503 -- Ignore some selected attributes in CodePeer mode since they are not
2504 -- relevant in this context.
2506 if CodePeer_Mode then
2509 -- Ignore Component_Size in CodePeer mode, to avoid changing the
2510 -- internal representation of types by implicitly packing them.
2512 when Attribute_Component_Size =>
2513 Rewrite (N, Make_Null_Statement (Sloc (N)));
2521 -- Process Ignore_Rep_Clauses option
2523 if Ignore_Rep_Clauses then
2526 -- The following should be ignored. They do not affect legality
2527 -- and may be target dependent. The basic idea of -gnatI is to
2528 -- ignore any rep clauses that may be target dependent but do not
2529 -- affect legality (except possibly to be rejected because they
2530 -- are incompatible with the compilation target).
2532 when Attribute_Alignment |
2533 Attribute_Bit_Order |
2534 Attribute_Component_Size |
2535 Attribute_Machine_Radix |
2536 Attribute_Object_Size |
2538 Attribute_Stream_Size |
2539 Attribute_Value_Size =>
2540 Rewrite (N, Make_Null_Statement (Sloc (N)));
2543 -- Perhaps 'Small should not be ignored by Ignore_Rep_Clauses ???
2545 when Attribute_Small =>
2546 if Ignore_Rep_Clauses then
2547 Rewrite (N, Make_Null_Statement (Sloc (N)));
2551 -- The following should not be ignored, because in the first place
2552 -- they are reasonably portable, and should not cause problems in
2553 -- compiling code from another target, and also they do affect
2554 -- legality, e.g. failing to provide a stream attribute for a
2555 -- type may make a program illegal.
2557 when Attribute_External_Tag |
2561 Attribute_Simple_Storage_Pool |
2562 Attribute_Storage_Pool |
2563 Attribute_Storage_Size |
2567 -- Other cases are errors ("attribute& cannot be set with
2568 -- definition clause"), which will be caught below.
2576 Ent := Entity (Nam);
2578 if Rep_Item_Too_Early (Ent, N) then
2582 -- Rep clause applies to full view of incomplete type or private type if
2583 -- we have one (if not, this is a premature use of the type). However,
2584 -- certain semantic checks need to be done on the specified entity (i.e.
2585 -- the private view), so we save it in Ent.
2587 if Is_Private_Type (Ent)
2588 and then Is_Derived_Type (Ent)
2589 and then not Is_Tagged_Type (Ent)
2590 and then No (Full_View (Ent))
2592 -- If this is a private type whose completion is a derivation from
2593 -- another private type, there is no full view, and the attribute
2594 -- belongs to the type itself, not its underlying parent.
2598 elsif Ekind (Ent) = E_Incomplete_Type then
2600 -- The attribute applies to the full view, set the entity of the
2601 -- attribute definition accordingly.
2603 Ent := Underlying_Type (Ent);
2605 Set_Entity (Nam, Ent);
2608 U_Ent := Underlying_Type (Ent);
2611 -- Avoid cascaded error
2613 if Etype (Nam) = Any_Type then
2616 -- Must be declared in current scope or in case of an aspect
2617 -- specification, must be visible in current scope.
2619 elsif Scope (Ent) /= Current_Scope
2621 not (From_Aspect_Specification (N)
2622 and then Scope_Within_Or_Same (Current_Scope, Scope (Ent)))
2624 Error_Msg_N ("entity must be declared in this scope", Nam);
2627 -- Must not be a source renaming (we do have some cases where the
2628 -- expander generates a renaming, and those cases are OK, in such
2629 -- cases any attribute applies to the renamed object as well).
2631 elsif Is_Object (Ent)
2632 and then Present (Renamed_Object (Ent))
2634 -- Case of renamed object from source, this is an error
2636 if Comes_From_Source (Renamed_Object (Ent)) then
2637 Get_Name_String (Chars (N));
2638 Error_Msg_Strlen := Name_Len;
2639 Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
2641 ("~ clause not allowed for a renaming declaration "
2642 & "(RM 13.1(6))", Nam);
2645 -- For the case of a compiler generated renaming, the attribute
2646 -- definition clause applies to the renamed object created by the
2647 -- expander. The easiest general way to handle this is to create a
2648 -- copy of the attribute definition clause for this object.
2652 Make_Attribute_Definition_Clause (Loc,
2654 New_Occurrence_Of (Entity (Renamed_Object (Ent)), Loc),
2656 Expression => Duplicate_Subexpr (Expression (N))));
2659 -- If no underlying entity, use entity itself, applies to some
2660 -- previously detected error cases ???
2662 elsif No (U_Ent) then
2665 -- Cannot specify for a subtype (exception Object/Value_Size)
2667 elsif Is_Type (U_Ent)
2668 and then not Is_First_Subtype (U_Ent)
2669 and then Id /= Attribute_Object_Size
2670 and then Id /= Attribute_Value_Size
2671 and then not From_At_Mod (N)
2673 Error_Msg_N ("cannot specify attribute for subtype", Nam);
2677 Set_Entity (N, U_Ent);
2679 -- Switch on particular attribute
2687 -- Address attribute definition clause
2689 when Attribute_Address => Address : begin
2691 -- A little error check, catch for X'Address use X'Address;
2693 if Nkind (Nam) = N_Identifier
2694 and then Nkind (Expr) = N_Attribute_Reference
2695 and then Attribute_Name (Expr) = Name_Address
2696 and then Nkind (Prefix (Expr)) = N_Identifier
2697 and then Chars (Nam) = Chars (Prefix (Expr))
2700 ("address for & is self-referencing", Prefix (Expr), Ent);
2704 -- Not that special case, carry on with analysis of expression
2706 Analyze_And_Resolve (Expr, RTE (RE_Address));
2708 -- Even when ignoring rep clauses we need to indicate that the
2709 -- entity has an address clause and thus it is legal to declare
2712 if Ignore_Rep_Clauses then
2713 if Ekind_In (U_Ent, E_Variable, E_Constant) then
2714 Record_Rep_Item (U_Ent, N);
2720 if Duplicate_Clause then
2723 -- Case of address clause for subprogram
2725 elsif Is_Subprogram (U_Ent) then
2726 if Has_Homonym (U_Ent) then
2728 ("address clause cannot be given " &
2729 "for overloaded subprogram",
2734 -- For subprograms, all address clauses are permitted, and we
2735 -- mark the subprogram as having a deferred freeze so that Gigi
2736 -- will not elaborate it too soon.
2738 -- Above needs more comments, what is too soon about???
2740 Set_Has_Delayed_Freeze (U_Ent);
2742 -- Case of address clause for entry
2744 elsif Ekind (U_Ent) = E_Entry then
2745 if Nkind (Parent (N)) = N_Task_Body then
2747 ("entry address must be specified in task spec", Nam);
2751 -- For entries, we require a constant address
2753 Check_Constant_Address_Clause (Expr, U_Ent);
2755 -- Special checks for task types
2757 if Is_Task_Type (Scope (U_Ent))
2758 and then Comes_From_Source (Scope (U_Ent))
2761 ("?entry address declared for entry in task type", N);
2763 ("\?only one task can be declared of this type", N);
2766 -- Entry address clauses are obsolescent
2768 Check_Restriction (No_Obsolescent_Features, N);
2770 if Warn_On_Obsolescent_Feature then
2772 ("attaching interrupt to task entry is an " &
2773 "obsolescent feature (RM J.7.1)?", N);
2775 ("\use interrupt procedure instead?", N);
2778 -- Case of an address clause for a controlled object which we
2779 -- consider to be erroneous.
2781 elsif Is_Controlled (Etype (U_Ent))
2782 or else Has_Controlled_Component (Etype (U_Ent))
2785 ("?controlled object& must not be overlaid", Nam, U_Ent);
2787 ("\?Program_Error will be raised at run time", Nam);
2788 Insert_Action (Declaration_Node (U_Ent),
2789 Make_Raise_Program_Error (Loc,
2790 Reason => PE_Overlaid_Controlled_Object));
2793 -- Case of address clause for a (non-controlled) object
2796 Ekind (U_Ent) = E_Variable
2798 Ekind (U_Ent) = E_Constant
2801 Expr : constant Node_Id := Expression (N);
2806 -- Exported variables cannot have an address clause, because
2807 -- this cancels the effect of the pragma Export.
2809 if Is_Exported (U_Ent) then
2811 ("cannot export object with address clause", Nam);
2815 Find_Overlaid_Entity (N, O_Ent, Off);
2817 -- Overlaying controlled objects is erroneous
2820 and then (Has_Controlled_Component (Etype (O_Ent))
2821 or else Is_Controlled (Etype (O_Ent)))
2824 ("?cannot overlay with controlled object", Expr);
2826 ("\?Program_Error will be raised at run time", Expr);
2827 Insert_Action (Declaration_Node (U_Ent),
2828 Make_Raise_Program_Error (Loc,
2829 Reason => PE_Overlaid_Controlled_Object));
2832 elsif Present (O_Ent)
2833 and then Ekind (U_Ent) = E_Constant
2834 and then not Is_Constant_Object (O_Ent)
2836 Error_Msg_N ("constant overlays a variable?", Expr);
2838 -- Imported variables can have an address clause, but then
2839 -- the import is pretty meaningless except to suppress
2840 -- initializations, so we do not need such variables to
2841 -- be statically allocated (and in fact it causes trouble
2842 -- if the address clause is a local value).
2844 elsif Is_Imported (U_Ent) then
2845 Set_Is_Statically_Allocated (U_Ent, False);
2848 -- We mark a possible modification of a variable with an
2849 -- address clause, since it is likely aliasing is occurring.
2851 Note_Possible_Modification (Nam, Sure => False);
2853 -- Here we are checking for explicit overlap of one variable
2854 -- by another, and if we find this then mark the overlapped
2855 -- variable as also being volatile to prevent unwanted
2856 -- optimizations. This is a significant pessimization so
2857 -- avoid it when there is an offset, i.e. when the object
2858 -- is composite; they cannot be optimized easily anyway.
2861 and then Is_Object (O_Ent)
2864 -- The following test is an expedient solution to what
2865 -- is really a problem in CodePeer. Suppressing the
2866 -- Set_Treat_As_Volatile call here prevents later
2867 -- generation (in some cases) of trees that CodePeer
2868 -- should, but currently does not, handle correctly.
2869 -- This test should probably be removed when CodePeer
2870 -- is improved, just because we want the tree CodePeer
2871 -- analyzes to match the tree for which we generate code
2872 -- as closely as is practical. ???
2874 and then not CodePeer_Mode
2876 -- ??? O_Ent might not be in current unit
2878 Set_Treat_As_Volatile (O_Ent);
2881 -- Legality checks on the address clause for initialized
2882 -- objects is deferred until the freeze point, because
2883 -- a subsequent pragma might indicate that the object
2884 -- is imported and thus not initialized.
2886 Set_Has_Delayed_Freeze (U_Ent);
2888 -- If an initialization call has been generated for this
2889 -- object, it needs to be deferred to after the freeze node
2890 -- we have just now added, otherwise GIGI will see a
2891 -- reference to the variable (as actual to the IP call)
2892 -- before its definition.
2895 Init_Call : constant Node_Id := Find_Init_Call (U_Ent, N);
2897 if Present (Init_Call) then
2899 Append_Freeze_Action (U_Ent, Init_Call);
2903 if Is_Exported (U_Ent) then
2905 ("& cannot be exported if an address clause is given",
2908 ("\define and export a variable " &
2909 "that holds its address instead",
2913 -- Entity has delayed freeze, so we will generate an
2914 -- alignment check at the freeze point unless suppressed.
2916 if not Range_Checks_Suppressed (U_Ent)
2917 and then not Alignment_Checks_Suppressed (U_Ent)
2919 Set_Check_Address_Alignment (N);
2922 -- Kill the size check code, since we are not allocating
2923 -- the variable, it is somewhere else.
2925 Kill_Size_Check_Code (U_Ent);
2927 -- If the address clause is of the form:
2929 -- for Y'Address use X'Address
2933 -- Const : constant Address := X'Address;
2935 -- for Y'Address use Const;
2937 -- then we make an entry in the table for checking the size
2938 -- and alignment of the overlaying variable. We defer this
2939 -- check till after code generation to take full advantage
2940 -- of the annotation done by the back end. This entry is
2941 -- only made if the address clause comes from source.
2943 -- If the entity has a generic type, the check will be
2944 -- performed in the instance if the actual type justifies
2945 -- it, and we do not insert the clause in the table to
2946 -- prevent spurious warnings.
2948 if Address_Clause_Overlay_Warnings
2949 and then Comes_From_Source (N)
2950 and then Present (O_Ent)
2951 and then Is_Object (O_Ent)
2953 if not Is_Generic_Type (Etype (U_Ent)) then
2954 Address_Clause_Checks.Append ((N, U_Ent, O_Ent, Off));
2957 -- If variable overlays a constant view, and we are
2958 -- warning on overlays, then mark the variable as
2959 -- overlaying a constant (we will give warnings later
2960 -- if this variable is assigned).
2962 if Is_Constant_Object (O_Ent)
2963 and then Ekind (U_Ent) = E_Variable
2965 Set_Overlays_Constant (U_Ent);
2970 -- Not a valid entity for an address clause
2973 Error_Msg_N ("address cannot be given for &", Nam);
2981 -- Alignment attribute definition clause
2983 when Attribute_Alignment => Alignment : declare
2984 Align : constant Uint := Get_Alignment_Value (Expr);
2985 Max_Align : constant Uint := UI_From_Int (Maximum_Alignment);
2990 if not Is_Type (U_Ent)
2991 and then Ekind (U_Ent) /= E_Variable
2992 and then Ekind (U_Ent) /= E_Constant
2994 Error_Msg_N ("alignment cannot be given for &", Nam);
2996 elsif Duplicate_Clause then
2999 elsif Align /= No_Uint then
3000 Set_Has_Alignment_Clause (U_Ent);
3002 -- Tagged type case, check for attempt to set alignment to a
3003 -- value greater than Max_Align, and reset if so.
3005 if Is_Tagged_Type (U_Ent) and then Align > Max_Align then
3007 ("?alignment for & set to Maximum_Aligment", Nam);
3008 Set_Alignment (U_Ent, Max_Align);
3013 Set_Alignment (U_Ent, Align);
3016 -- For an array type, U_Ent is the first subtype. In that case,
3017 -- also set the alignment of the anonymous base type so that
3018 -- other subtypes (such as the itypes for aggregates of the
3019 -- type) also receive the expected alignment.
3021 if Is_Array_Type (U_Ent) then
3022 Set_Alignment (Base_Type (U_Ent), Align);
3031 -- Bit_Order attribute definition clause
3033 when Attribute_Bit_Order => Bit_Order : declare
3035 if not Is_Record_Type (U_Ent) then
3037 ("Bit_Order can only be defined for record type", Nam);
3039 elsif Duplicate_Clause then
3043 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
3045 if Etype (Expr) = Any_Type then
3048 elsif not Is_Static_Expression (Expr) then
3049 Flag_Non_Static_Expr
3050 ("Bit_Order requires static expression!", Expr);
3053 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
3054 Set_Reverse_Bit_Order (U_Ent, True);
3060 --------------------
3061 -- Component_Size --
3062 --------------------
3064 -- Component_Size attribute definition clause
3066 when Attribute_Component_Size => Component_Size_Case : declare
3067 Csize : constant Uint := Static_Integer (Expr);
3071 New_Ctyp : Entity_Id;
3075 if not Is_Array_Type (U_Ent) then
3076 Error_Msg_N ("component size requires array type", Nam);
3080 Btype := Base_Type (U_Ent);
3081 Ctyp := Component_Type (Btype);
3083 if Duplicate_Clause then
3086 elsif Rep_Item_Too_Early (Btype, N) then
3089 elsif Csize /= No_Uint then
3090 Check_Size (Expr, Ctyp, Csize, Biased);
3092 -- For the biased case, build a declaration for a subtype that
3093 -- will be used to represent the biased subtype that reflects
3094 -- the biased representation of components. We need the subtype
3095 -- to get proper conversions on referencing elements of the
3096 -- array. Note: component size clauses are ignored in VM mode.
3098 if VM_Target = No_VM then
3101 Make_Defining_Identifier (Loc,
3103 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
3106 Make_Subtype_Declaration (Loc,
3107 Defining_Identifier => New_Ctyp,
3108 Subtype_Indication =>
3109 New_Occurrence_Of (Component_Type (Btype), Loc));
3111 Set_Parent (Decl, N);
3112 Analyze (Decl, Suppress => All_Checks);
3114 Set_Has_Delayed_Freeze (New_Ctyp, False);
3115 Set_Esize (New_Ctyp, Csize);
3116 Set_RM_Size (New_Ctyp, Csize);
3117 Init_Alignment (New_Ctyp);
3118 Set_Is_Itype (New_Ctyp, True);
3119 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
3121 Set_Component_Type (Btype, New_Ctyp);
3122 Set_Biased (New_Ctyp, N, "component size clause");
3125 Set_Component_Size (Btype, Csize);
3127 -- For VM case, we ignore component size clauses
3130 -- Give a warning unless we are in GNAT mode, in which case
3131 -- the warning is suppressed since it is not useful.
3133 if not GNAT_Mode then
3135 ("?component size ignored in this configuration", N);
3139 -- Deal with warning on overridden size
3141 if Warn_On_Overridden_Size
3142 and then Has_Size_Clause (Ctyp)
3143 and then RM_Size (Ctyp) /= Csize
3146 ("?component size overrides size clause for&",
3150 Set_Has_Component_Size_Clause (Btype, True);
3151 Set_Has_Non_Standard_Rep (Btype, True);
3153 end Component_Size_Case;
3155 -----------------------
3156 -- Constant_Indexing --
3157 -----------------------
3159 when Attribute_Constant_Indexing =>
3160 Check_Indexing_Functions;
3166 when Attribute_CPU => CPU :
3168 -- CPU attribute definition clause not allowed except from aspect
3171 if From_Aspect_Specification (N) then
3172 if not Is_Task_Type (U_Ent) then
3173 Error_Msg_N ("CPU can only be defined for task", Nam);
3175 elsif Duplicate_Clause then
3179 -- The expression must be analyzed in the special manner
3180 -- described in "Handling of Default and Per-Object
3181 -- Expressions" in sem.ads.
3183 -- The visibility to the discriminants must be restored
3185 Push_Scope_And_Install_Discriminants (U_Ent);
3186 Preanalyze_Spec_Expression (Expr, RTE (RE_CPU_Range));
3187 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
3189 if not Is_Static_Expression (Expr) then
3190 Check_Restriction (Static_Priorities, Expr);
3196 ("attribute& cannot be set with definition clause", N);
3200 ----------------------
3201 -- Default_Iterator --
3202 ----------------------
3204 when Attribute_Default_Iterator => Default_Iterator : declare
3208 if not Is_Tagged_Type (U_Ent) then
3210 ("aspect Default_Iterator applies to tagged type", Nam);
3213 Check_Iterator_Functions;
3217 if not Is_Entity_Name (Expr)
3218 or else Ekind (Entity (Expr)) /= E_Function
3220 Error_Msg_N ("aspect Iterator must be a function", Expr);
3222 Func := Entity (Expr);
3225 if No (First_Formal (Func))
3226 or else Etype (First_Formal (Func)) /= U_Ent
3229 ("Default Iterator must be a primitive of&", Func, U_Ent);
3231 end Default_Iterator;
3233 ------------------------
3234 -- Dispatching_Domain --
3235 ------------------------
3237 when Attribute_Dispatching_Domain => Dispatching_Domain :
3239 -- Dispatching_Domain attribute definition clause not allowed
3240 -- except from aspect specification.
3242 if From_Aspect_Specification (N) then
3243 if not Is_Task_Type (U_Ent) then
3244 Error_Msg_N ("Dispatching_Domain can only be defined" &
3248 elsif Duplicate_Clause then
3252 -- The expression must be analyzed in the special manner
3253 -- described in "Handling of Default and Per-Object
3254 -- Expressions" in sem.ads.
3256 -- The visibility to the discriminants must be restored
3258 Push_Scope_And_Install_Discriminants (U_Ent);
3260 Preanalyze_Spec_Expression
3261 (Expr, RTE (RE_Dispatching_Domain));
3263 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
3268 ("attribute& cannot be set with definition clause", N);
3270 end Dispatching_Domain;
3276 when Attribute_External_Tag => External_Tag :
3278 if not Is_Tagged_Type (U_Ent) then
3279 Error_Msg_N ("should be a tagged type", Nam);
3282 if Duplicate_Clause then
3286 Analyze_And_Resolve (Expr, Standard_String);
3288 if not Is_Static_Expression (Expr) then
3289 Flag_Non_Static_Expr
3290 ("static string required for tag name!", Nam);
3293 if VM_Target = No_VM then
3294 Set_Has_External_Tag_Rep_Clause (U_Ent);
3296 Error_Msg_Name_1 := Attr;
3298 ("% attribute unsupported in this configuration", Nam);
3301 if not Is_Library_Level_Entity (U_Ent) then
3303 ("?non-unique external tag supplied for &", N, U_Ent);
3305 ("?\same external tag applies to all subprogram calls", N);
3307 ("?\corresponding internal tag cannot be obtained", N);
3312 --------------------------
3313 -- Implicit_Dereference --
3314 --------------------------
3316 when Attribute_Implicit_Dereference =>
3318 -- Legality checks already performed at the point of the type
3319 -- declaration, aspect is not delayed.
3327 when Attribute_Input =>
3328 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
3329 Set_Has_Specified_Stream_Input (Ent);
3331 ------------------------
3332 -- Interrupt_Priority --
3333 ------------------------
3335 when Attribute_Interrupt_Priority => Interrupt_Priority :
3337 -- Interrupt_Priority attribute definition clause not allowed
3338 -- except from aspect specification.
3340 if From_Aspect_Specification (N) then
3341 if not (Is_Protected_Type (U_Ent)
3342 or else Is_Task_Type (U_Ent))
3345 ("Interrupt_Priority can only be defined for task" &
3346 "and protected object",
3349 elsif Duplicate_Clause then
3353 -- The expression must be analyzed in the special manner
3354 -- described in "Handling of Default and Per-Object
3355 -- Expressions" in sem.ads.
3357 -- The visibility to the discriminants must be restored
3359 Push_Scope_And_Install_Discriminants (U_Ent);
3361 Preanalyze_Spec_Expression
3362 (Expr, RTE (RE_Interrupt_Priority));
3364 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
3369 ("attribute& cannot be set with definition clause", N);
3371 end Interrupt_Priority;
3373 ----------------------
3374 -- Iterator_Element --
3375 ----------------------
3377 when Attribute_Iterator_Element =>
3380 if not Is_Entity_Name (Expr)
3381 or else not Is_Type (Entity (Expr))
3383 Error_Msg_N ("aspect Iterator_Element must be a type", Expr);
3390 -- Machine radix attribute definition clause
3392 when Attribute_Machine_Radix => Machine_Radix : declare
3393 Radix : constant Uint := Static_Integer (Expr);
3396 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
3397 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
3399 elsif Duplicate_Clause then
3402 elsif Radix /= No_Uint then
3403 Set_Has_Machine_Radix_Clause (U_Ent);
3404 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
3408 elsif Radix = 10 then
3409 Set_Machine_Radix_10 (U_Ent);
3411 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
3420 -- Object_Size attribute definition clause
3422 when Attribute_Object_Size => Object_Size : declare
3423 Size : constant Uint := Static_Integer (Expr);
3426 pragma Warnings (Off, Biased);
3429 if not Is_Type (U_Ent) then
3430 Error_Msg_N ("Object_Size cannot be given for &", Nam);
3432 elsif Duplicate_Clause then
3436 Check_Size (Expr, U_Ent, Size, Biased);
3444 UI_Mod (Size, 64) /= 0
3447 ("Object_Size must be 8, 16, 32, or multiple of 64",
3451 Set_Esize (U_Ent, Size);
3452 Set_Has_Object_Size_Clause (U_Ent);
3453 Alignment_Check_For_Size_Change (U_Ent, Size);
3461 when Attribute_Output =>
3462 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
3463 Set_Has_Specified_Stream_Output (Ent);
3469 when Attribute_Priority => Priority :
3471 -- Priority attribute definition clause not allowed except from
3472 -- aspect specification.
3474 if From_Aspect_Specification (N) then
3475 if not (Is_Protected_Type (U_Ent)
3476 or else Is_Task_Type (U_Ent)
3477 or else Ekind (U_Ent) = E_Procedure)
3480 ("Priority can only be defined for task and protected " &
3484 elsif Duplicate_Clause then
3488 -- The expression must be analyzed in the special manner
3489 -- described in "Handling of Default and Per-Object
3490 -- Expressions" in sem.ads.
3492 -- The visibility to the discriminants must be restored
3494 Push_Scope_And_Install_Discriminants (U_Ent);
3495 Preanalyze_Spec_Expression (Expr, Standard_Integer);
3496 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
3498 if not Is_Static_Expression (Expr) then
3499 Check_Restriction (Static_Priorities, Expr);
3505 ("attribute& cannot be set with definition clause", N);
3513 when Attribute_Read =>
3514 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
3515 Set_Has_Specified_Stream_Read (Ent);
3517 --------------------------
3518 -- Scalar_Storage_Order --
3519 --------------------------
3521 -- Scalar_Storage_Order attribute definition clause
3523 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order : declare
3525 if not (Is_Record_Type (U_Ent) or else Is_Array_Type (U_Ent)) then
3527 ("Scalar_Storage_Order can only be defined for "
3528 & "record or array type", Nam);
3530 elsif Duplicate_Clause then
3534 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
3536 if Etype (Expr) = Any_Type then
3539 elsif not Is_Static_Expression (Expr) then
3540 Flag_Non_Static_Expr
3541 ("Scalar_Storage_Order requires static expression!", Expr);
3544 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
3545 Set_Reverse_Storage_Order (Base_Type (U_Ent), True);
3549 end Scalar_Storage_Order;
3555 -- Size attribute definition clause
3557 when Attribute_Size => Size : declare
3558 Size : constant Uint := Static_Integer (Expr);
3565 if Duplicate_Clause then
3568 elsif not Is_Type (U_Ent)
3569 and then Ekind (U_Ent) /= E_Variable
3570 and then Ekind (U_Ent) /= E_Constant
3572 Error_Msg_N ("size cannot be given for &", Nam);
3574 elsif Is_Array_Type (U_Ent)
3575 and then not Is_Constrained (U_Ent)
3578 ("size cannot be given for unconstrained array", Nam);
3580 elsif Size /= No_Uint then
3581 if VM_Target /= No_VM and then not GNAT_Mode then
3583 -- Size clause is not handled properly on VM targets.
3584 -- Display a warning unless we are in GNAT mode, in which
3585 -- case this is useless.
3588 ("?size clauses are ignored in this configuration", N);
3591 if Is_Type (U_Ent) then
3594 Etyp := Etype (U_Ent);
3597 -- Check size, note that Gigi is in charge of checking that the
3598 -- size of an array or record type is OK. Also we do not check
3599 -- the size in the ordinary fixed-point case, since it is too
3600 -- early to do so (there may be subsequent small clause that
3601 -- affects the size). We can check the size if a small clause
3602 -- has already been given.
3604 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
3605 or else Has_Small_Clause (U_Ent)
3607 Check_Size (Expr, Etyp, Size, Biased);
3608 Set_Biased (U_Ent, N, "size clause", Biased);
3611 -- For types set RM_Size and Esize if possible
3613 if Is_Type (U_Ent) then
3614 Set_RM_Size (U_Ent, Size);
3616 -- For elementary types, increase Object_Size to power of 2,
3617 -- but not less than a storage unit in any case (normally
3618 -- this means it will be byte addressable).
3620 -- For all other types, nothing else to do, we leave Esize
3621 -- (object size) unset, the back end will set it from the
3622 -- size and alignment in an appropriate manner.
3624 -- In both cases, we check whether the alignment must be
3625 -- reset in the wake of the size change.
3627 if Is_Elementary_Type (U_Ent) then
3628 if Size <= System_Storage_Unit then
3629 Init_Esize (U_Ent, System_Storage_Unit);
3630 elsif Size <= 16 then
3631 Init_Esize (U_Ent, 16);
3632 elsif Size <= 32 then
3633 Init_Esize (U_Ent, 32);
3635 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
3638 Alignment_Check_For_Size_Change (U_Ent, Esize (U_Ent));
3640 Alignment_Check_For_Size_Change (U_Ent, Size);
3643 -- For objects, set Esize only
3646 if Is_Elementary_Type (Etyp) then
3647 if Size /= System_Storage_Unit
3649 Size /= System_Storage_Unit * 2
3651 Size /= System_Storage_Unit * 4
3653 Size /= System_Storage_Unit * 8
3655 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
3656 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
3658 ("size for primitive object must be a power of 2"
3659 & " in the range ^-^", N);
3663 Set_Esize (U_Ent, Size);
3666 Set_Has_Size_Clause (U_Ent);
3674 -- Small attribute definition clause
3676 when Attribute_Small => Small : declare
3677 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
3681 Analyze_And_Resolve (Expr, Any_Real);
3683 if Etype (Expr) = Any_Type then
3686 elsif not Is_Static_Expression (Expr) then
3687 Flag_Non_Static_Expr
3688 ("small requires static expression!", Expr);
3692 Small := Expr_Value_R (Expr);
3694 if Small <= Ureal_0 then
3695 Error_Msg_N ("small value must be greater than zero", Expr);
3701 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
3703 ("small requires an ordinary fixed point type", Nam);
3705 elsif Has_Small_Clause (U_Ent) then
3706 Error_Msg_N ("small already given for &", Nam);
3708 elsif Small > Delta_Value (U_Ent) then
3710 ("small value must not be greater than delta value", Nam);
3713 Set_Small_Value (U_Ent, Small);
3714 Set_Small_Value (Implicit_Base, Small);
3715 Set_Has_Small_Clause (U_Ent);
3716 Set_Has_Small_Clause (Implicit_Base);
3717 Set_Has_Non_Standard_Rep (Implicit_Base);
3725 -- Storage_Pool attribute definition clause
3727 when Attribute_Storage_Pool | Attribute_Simple_Storage_Pool => declare
3732 if Ekind (U_Ent) = E_Access_Subprogram_Type then
3734 ("storage pool cannot be given for access-to-subprogram type",
3739 Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
3742 ("storage pool can only be given for access types", Nam);
3745 elsif Is_Derived_Type (U_Ent) then
3747 ("storage pool cannot be given for a derived access type",
3750 elsif Duplicate_Clause then
3753 elsif Present (Associated_Storage_Pool (U_Ent)) then
3754 Error_Msg_N ("storage pool already given for &", Nam);
3758 if Id = Attribute_Storage_Pool then
3760 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
3762 -- In the Simple_Storage_Pool case, we allow a variable of any
3763 -- simple storage pool type, so we Resolve without imposing an
3767 Analyze_And_Resolve (Expr);
3769 if not Present (Get_Rep_Pragma
3770 (Etype (Expr), Name_Simple_Storage_Pool_Type))
3773 ("expression must be of a simple storage pool type", Expr);
3777 if not Denotes_Variable (Expr) then
3778 Error_Msg_N ("storage pool must be a variable", Expr);
3782 if Nkind (Expr) = N_Type_Conversion then
3783 T := Etype (Expression (Expr));
3788 -- The Stack_Bounded_Pool is used internally for implementing
3789 -- access types with a Storage_Size. Since it only work properly
3790 -- when used on one specific type, we need to check that it is not
3791 -- hijacked improperly:
3793 -- type T is access Integer;
3794 -- for T'Storage_Size use n;
3795 -- type Q is access Float;
3796 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
3798 if RTE_Available (RE_Stack_Bounded_Pool)
3799 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
3801 Error_Msg_N ("non-shareable internal Pool", Expr);
3805 -- If the argument is a name that is not an entity name, then
3806 -- we construct a renaming operation to define an entity of
3807 -- type storage pool.
3809 if not Is_Entity_Name (Expr)
3810 and then Is_Object_Reference (Expr)
3812 Pool := Make_Temporary (Loc, 'P', Expr);
3815 Rnode : constant Node_Id :=
3816 Make_Object_Renaming_Declaration (Loc,
3817 Defining_Identifier => Pool,
3819 New_Occurrence_Of (Etype (Expr), Loc),
3823 Insert_Before (N, Rnode);
3825 Set_Associated_Storage_Pool (U_Ent, Pool);
3828 elsif Is_Entity_Name (Expr) then
3829 Pool := Entity (Expr);
3831 -- If pool is a renamed object, get original one. This can
3832 -- happen with an explicit renaming, and within instances.
3834 while Present (Renamed_Object (Pool))
3835 and then Is_Entity_Name (Renamed_Object (Pool))
3837 Pool := Entity (Renamed_Object (Pool));
3840 if Present (Renamed_Object (Pool))
3841 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
3842 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
3844 Pool := Entity (Expression (Renamed_Object (Pool)));
3847 Set_Associated_Storage_Pool (U_Ent, Pool);
3849 elsif Nkind (Expr) = N_Type_Conversion
3850 and then Is_Entity_Name (Expression (Expr))
3851 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
3853 Pool := Entity (Expression (Expr));
3854 Set_Associated_Storage_Pool (U_Ent, Pool);
3857 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
3866 -- Storage_Size attribute definition clause
3868 when Attribute_Storage_Size => Storage_Size : declare
3869 Btype : constant Entity_Id := Base_Type (U_Ent);
3872 if Is_Task_Type (U_Ent) then
3873 Check_Restriction (No_Obsolescent_Features, N);
3875 if Warn_On_Obsolescent_Feature then
3877 ("storage size clause for task is an " &
3878 "obsolescent feature (RM J.9)?", N);
3879 Error_Msg_N ("\use Storage_Size pragma instead?", N);
3885 if not Is_Access_Type (U_Ent)
3886 and then Ekind (U_Ent) /= E_Task_Type
3888 Error_Msg_N ("storage size cannot be given for &", Nam);
3890 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
3892 ("storage size cannot be given for a derived access type",
3895 elsif Duplicate_Clause then
3899 Analyze_And_Resolve (Expr, Any_Integer);
3901 if Is_Access_Type (U_Ent) then
3902 if Present (Associated_Storage_Pool (U_Ent)) then
3903 Error_Msg_N ("storage pool already given for &", Nam);
3907 if Is_OK_Static_Expression (Expr)
3908 and then Expr_Value (Expr) = 0
3910 Set_No_Pool_Assigned (Btype);
3914 Set_Has_Storage_Size_Clause (Btype);
3922 when Attribute_Stream_Size => Stream_Size : declare
3923 Size : constant Uint := Static_Integer (Expr);
3926 if Ada_Version <= Ada_95 then
3927 Check_Restriction (No_Implementation_Attributes, N);
3930 if Duplicate_Clause then
3933 elsif Is_Elementary_Type (U_Ent) then
3934 if Size /= System_Storage_Unit
3936 Size /= System_Storage_Unit * 2
3938 Size /= System_Storage_Unit * 4
3940 Size /= System_Storage_Unit * 8
3942 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
3944 ("stream size for elementary type must be a"
3945 & " power of 2 and at least ^", N);
3947 elsif RM_Size (U_Ent) > Size then
3948 Error_Msg_Uint_1 := RM_Size (U_Ent);
3950 ("stream size for elementary type must be a"
3951 & " power of 2 and at least ^", N);
3954 Set_Has_Stream_Size_Clause (U_Ent);
3957 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
3965 -- Value_Size attribute definition clause
3967 when Attribute_Value_Size => Value_Size : declare
3968 Size : constant Uint := Static_Integer (Expr);
3972 if not Is_Type (U_Ent) then
3973 Error_Msg_N ("Value_Size cannot be given for &", Nam);
3975 elsif Duplicate_Clause then
3978 elsif Is_Array_Type (U_Ent)
3979 and then not Is_Constrained (U_Ent)
3982 ("Value_Size cannot be given for unconstrained array", Nam);
3985 if Is_Elementary_Type (U_Ent) then
3986 Check_Size (Expr, U_Ent, Size, Biased);
3987 Set_Biased (U_Ent, N, "value size clause", Biased);
3990 Set_RM_Size (U_Ent, Size);
3994 -----------------------
3995 -- Variable_Indexing --
3996 -----------------------
3998 when Attribute_Variable_Indexing =>
3999 Check_Indexing_Functions;
4005 when Attribute_Write =>
4006 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
4007 Set_Has_Specified_Stream_Write (Ent);
4009 -- All other attributes cannot be set
4013 ("attribute& cannot be set with definition clause", N);
4016 -- The test for the type being frozen must be performed after any
4017 -- expression the clause has been analyzed since the expression itself
4018 -- might cause freezing that makes the clause illegal.
4020 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
4023 end Analyze_Attribute_Definition_Clause;
4025 ----------------------------
4026 -- Analyze_Code_Statement --
4027 ----------------------------
4029 procedure Analyze_Code_Statement (N : Node_Id) is
4030 HSS : constant Node_Id := Parent (N);
4031 SBody : constant Node_Id := Parent (HSS);
4032 Subp : constant Entity_Id := Current_Scope;
4039 -- Analyze and check we get right type, note that this implements the
4040 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
4041 -- is the only way that Asm_Insn could possibly be visible.
4043 Analyze_And_Resolve (Expression (N));
4045 if Etype (Expression (N)) = Any_Type then
4047 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
4048 Error_Msg_N ("incorrect type for code statement", N);
4052 Check_Code_Statement (N);
4054 -- Make sure we appear in the handled statement sequence of a
4055 -- subprogram (RM 13.8(3)).
4057 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
4058 or else Nkind (SBody) /= N_Subprogram_Body
4061 ("code statement can only appear in body of subprogram", N);
4065 -- Do remaining checks (RM 13.8(3)) if not already done
4067 if not Is_Machine_Code_Subprogram (Subp) then
4068 Set_Is_Machine_Code_Subprogram (Subp);
4070 -- No exception handlers allowed
4072 if Present (Exception_Handlers (HSS)) then
4074 ("exception handlers not permitted in machine code subprogram",
4075 First (Exception_Handlers (HSS)));
4078 -- No declarations other than use clauses and pragmas (we allow
4079 -- certain internally generated declarations as well).
4081 Decl := First (Declarations (SBody));
4082 while Present (Decl) loop
4083 DeclO := Original_Node (Decl);
4084 if Comes_From_Source (DeclO)
4085 and not Nkind_In (DeclO, N_Pragma,
4086 N_Use_Package_Clause,
4088 N_Implicit_Label_Declaration)
4091 ("this declaration not allowed in machine code subprogram",
4098 -- No statements other than code statements, pragmas, and labels.
4099 -- Again we allow certain internally generated statements.
4101 -- In Ada 2012, qualified expressions are names, and the code
4102 -- statement is initially parsed as a procedure call.
4104 Stmt := First (Statements (HSS));
4105 while Present (Stmt) loop
4106 StmtO := Original_Node (Stmt);
4108 -- A procedure call transformed into a code statement is OK.
4110 if Ada_Version >= Ada_2012
4111 and then Nkind (StmtO) = N_Procedure_Call_Statement
4112 and then Nkind (Name (StmtO)) = N_Qualified_Expression
4116 elsif Comes_From_Source (StmtO)
4117 and then not Nkind_In (StmtO, N_Pragma,
4122 ("this statement is not allowed in machine code subprogram",
4129 end Analyze_Code_Statement;
4131 -----------------------------------------------
4132 -- Analyze_Enumeration_Representation_Clause --
4133 -----------------------------------------------
4135 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
4136 Ident : constant Node_Id := Identifier (N);
4137 Aggr : constant Node_Id := Array_Aggregate (N);
4138 Enumtype : Entity_Id;
4145 Err : Boolean := False;
4146 -- Set True to avoid cascade errors and crashes on incorrect source code
4148 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
4149 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
4150 -- Allowed range of universal integer (= allowed range of enum lit vals)
4154 -- Minimum and maximum values of entries
4157 -- Pointer to node for literal providing max value
4160 if Ignore_Rep_Clauses then
4164 -- First some basic error checks
4167 Enumtype := Entity (Ident);
4169 if Enumtype = Any_Type
4170 or else Rep_Item_Too_Early (Enumtype, N)
4174 Enumtype := Underlying_Type (Enumtype);
4177 if not Is_Enumeration_Type (Enumtype) then
4179 ("enumeration type required, found}",
4180 Ident, First_Subtype (Enumtype));
4184 -- Ignore rep clause on generic actual type. This will already have
4185 -- been flagged on the template as an error, and this is the safest
4186 -- way to ensure we don't get a junk cascaded message in the instance.
4188 if Is_Generic_Actual_Type (Enumtype) then
4191 -- Type must be in current scope
4193 elsif Scope (Enumtype) /= Current_Scope then
4194 Error_Msg_N ("type must be declared in this scope", Ident);
4197 -- Type must be a first subtype
4199 elsif not Is_First_Subtype (Enumtype) then
4200 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
4203 -- Ignore duplicate rep clause
4205 elsif Has_Enumeration_Rep_Clause (Enumtype) then
4206 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
4209 -- Don't allow rep clause for standard [wide_[wide_]]character
4211 elsif Is_Standard_Character_Type (Enumtype) then
4212 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
4215 -- Check that the expression is a proper aggregate (no parentheses)
4217 elsif Paren_Count (Aggr) /= 0 then
4219 ("extra parentheses surrounding aggregate not allowed",
4223 -- All tests passed, so set rep clause in place
4226 Set_Has_Enumeration_Rep_Clause (Enumtype);
4227 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
4230 -- Now we process the aggregate. Note that we don't use the normal
4231 -- aggregate code for this purpose, because we don't want any of the
4232 -- normal expansion activities, and a number of special semantic
4233 -- rules apply (including the component type being any integer type)
4235 Elit := First_Literal (Enumtype);
4237 -- First the positional entries if any
4239 if Present (Expressions (Aggr)) then
4240 Expr := First (Expressions (Aggr));
4241 while Present (Expr) loop
4243 Error_Msg_N ("too many entries in aggregate", Expr);
4247 Val := Static_Integer (Expr);
4249 -- Err signals that we found some incorrect entries processing
4250 -- the list. The final checks for completeness and ordering are
4251 -- skipped in this case.
4253 if Val = No_Uint then
4255 elsif Val < Lo or else Hi < Val then
4256 Error_Msg_N ("value outside permitted range", Expr);
4260 Set_Enumeration_Rep (Elit, Val);
4261 Set_Enumeration_Rep_Expr (Elit, Expr);
4267 -- Now process the named entries if present
4269 if Present (Component_Associations (Aggr)) then
4270 Assoc := First (Component_Associations (Aggr));
4271 while Present (Assoc) loop
4272 Choice := First (Choices (Assoc));
4274 if Present (Next (Choice)) then
4276 ("multiple choice not allowed here", Next (Choice));
4280 if Nkind (Choice) = N_Others_Choice then
4281 Error_Msg_N ("others choice not allowed here", Choice);
4284 elsif Nkind (Choice) = N_Range then
4286 -- ??? should allow zero/one element range here
4288 Error_Msg_N ("range not allowed here", Choice);
4292 Analyze_And_Resolve (Choice, Enumtype);
4294 if Error_Posted (Choice) then
4299 if Is_Entity_Name (Choice)
4300 and then Is_Type (Entity (Choice))
4302 Error_Msg_N ("subtype name not allowed here", Choice);
4305 -- ??? should allow static subtype with zero/one entry
4307 elsif Etype (Choice) = Base_Type (Enumtype) then
4308 if not Is_Static_Expression (Choice) then
4309 Flag_Non_Static_Expr
4310 ("non-static expression used for choice!", Choice);
4314 Elit := Expr_Value_E (Choice);
4316 if Present (Enumeration_Rep_Expr (Elit)) then
4318 Sloc (Enumeration_Rep_Expr (Elit));
4320 ("representation for& previously given#",
4325 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
4327 Expr := Expression (Assoc);
4328 Val := Static_Integer (Expr);
4330 if Val = No_Uint then
4333 elsif Val < Lo or else Hi < Val then
4334 Error_Msg_N ("value outside permitted range", Expr);
4338 Set_Enumeration_Rep (Elit, Val);
4348 -- Aggregate is fully processed. Now we check that a full set of
4349 -- representations was given, and that they are in range and in order.
4350 -- These checks are only done if no other errors occurred.
4356 Elit := First_Literal (Enumtype);
4357 while Present (Elit) loop
4358 if No (Enumeration_Rep_Expr (Elit)) then
4359 Error_Msg_NE ("missing representation for&!", N, Elit);
4362 Val := Enumeration_Rep (Elit);
4364 if Min = No_Uint then
4368 if Val /= No_Uint then
4369 if Max /= No_Uint and then Val <= Max then
4371 ("enumeration value for& not ordered!",
4372 Enumeration_Rep_Expr (Elit), Elit);
4375 Max_Node := Enumeration_Rep_Expr (Elit);
4379 -- If there is at least one literal whose representation is not
4380 -- equal to the Pos value, then note that this enumeration type
4381 -- has a non-standard representation.
4383 if Val /= Enumeration_Pos (Elit) then
4384 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
4391 -- Now set proper size information
4394 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
4397 if Has_Size_Clause (Enumtype) then
4399 -- All OK, if size is OK now
4401 if RM_Size (Enumtype) >= Minsize then
4405 -- Try if we can get by with biasing
4408 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
4410 -- Error message if even biasing does not work
4412 if RM_Size (Enumtype) < Minsize then
4413 Error_Msg_Uint_1 := RM_Size (Enumtype);
4414 Error_Msg_Uint_2 := Max;
4416 ("previously given size (^) is too small "
4417 & "for this value (^)", Max_Node);
4419 -- If biasing worked, indicate that we now have biased rep
4423 (Enumtype, Size_Clause (Enumtype), "size clause");
4428 Set_RM_Size (Enumtype, Minsize);
4429 Set_Enum_Esize (Enumtype);
4432 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
4433 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
4434 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
4438 -- We repeat the too late test in case it froze itself!
4440 if Rep_Item_Too_Late (Enumtype, N) then
4443 end Analyze_Enumeration_Representation_Clause;
4445 ----------------------------
4446 -- Analyze_Free_Statement --
4447 ----------------------------
4449 procedure Analyze_Free_Statement (N : Node_Id) is
4451 Analyze (Expression (N));
4452 end Analyze_Free_Statement;
4454 ---------------------------
4455 -- Analyze_Freeze_Entity --
4456 ---------------------------
4458 procedure Analyze_Freeze_Entity (N : Node_Id) is
4459 E : constant Entity_Id := Entity (N);
4462 -- Remember that we are processing a freezing entity. Required to
4463 -- ensure correct decoration of internal entities associated with
4464 -- interfaces (see New_Overloaded_Entity).
4466 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
4468 -- For tagged types covering interfaces add internal entities that link
4469 -- the primitives of the interfaces with the primitives that cover them.
4470 -- Note: These entities were originally generated only when generating
4471 -- code because their main purpose was to provide support to initialize
4472 -- the secondary dispatch tables. They are now generated also when
4473 -- compiling with no code generation to provide ASIS the relationship
4474 -- between interface primitives and tagged type primitives. They are
4475 -- also used to locate primitives covering interfaces when processing
4476 -- generics (see Derive_Subprograms).
4478 if Ada_Version >= Ada_2005
4479 and then Ekind (E) = E_Record_Type
4480 and then Is_Tagged_Type (E)
4481 and then not Is_Interface (E)
4482 and then Has_Interfaces (E)
4484 -- This would be a good common place to call the routine that checks
4485 -- overriding of interface primitives (and thus factorize calls to
4486 -- Check_Abstract_Overriding located at different contexts in the
4487 -- compiler). However, this is not possible because it causes
4488 -- spurious errors in case of late overriding.
4490 Add_Internal_Interface_Entities (E);
4495 if Ekind (E) = E_Record_Type
4496 and then Is_CPP_Class (E)
4497 and then Is_Tagged_Type (E)
4498 and then Tagged_Type_Expansion
4499 and then Expander_Active
4501 if CPP_Num_Prims (E) = 0 then
4503 -- If the CPP type has user defined components then it must import
4504 -- primitives from C++. This is required because if the C++ class
4505 -- has no primitives then the C++ compiler does not added the _tag
4506 -- component to the type.
4508 pragma Assert (Chars (First_Entity (E)) = Name_uTag);
4510 if First_Entity (E) /= Last_Entity (E) then
4512 ("?'C'P'P type must import at least one primitive from C++",
4517 -- Check that all its primitives are abstract or imported from C++.
4518 -- Check also availability of the C++ constructor.
4521 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
4523 Error_Reported : Boolean := False;
4527 Elmt := First_Elmt (Primitive_Operations (E));
4528 while Present (Elmt) loop
4529 Prim := Node (Elmt);
4531 if Comes_From_Source (Prim) then
4532 if Is_Abstract_Subprogram (Prim) then
4535 elsif not Is_Imported (Prim)
4536 or else Convention (Prim) /= Convention_CPP
4539 ("?primitives of 'C'P'P types must be imported from C++"
4540 & " or abstract", Prim);
4542 elsif not Has_Constructors
4543 and then not Error_Reported
4545 Error_Msg_Name_1 := Chars (E);
4547 ("?'C'P'P constructor required for type %", Prim);
4548 Error_Reported := True;
4557 -- Check Ada derivation of CPP type
4560 and then Tagged_Type_Expansion
4561 and then Ekind (E) = E_Record_Type
4562 and then Etype (E) /= E
4563 and then Is_CPP_Class (Etype (E))
4564 and then CPP_Num_Prims (Etype (E)) > 0
4565 and then not Is_CPP_Class (E)
4566 and then not Has_CPP_Constructors (Etype (E))
4568 -- If the parent has C++ primitives but it has no constructor then
4569 -- check that all the primitives are overridden in this derivation;
4570 -- otherwise the constructor of the parent is needed to build the
4578 Elmt := First_Elmt (Primitive_Operations (E));
4579 while Present (Elmt) loop
4580 Prim := Node (Elmt);
4582 if not Is_Abstract_Subprogram (Prim)
4583 and then No (Interface_Alias (Prim))
4584 and then Find_Dispatching_Type (Ultimate_Alias (Prim)) /= E
4586 Error_Msg_Name_1 := Chars (Etype (E));
4588 ("'C'P'P constructor required for parent type %", E);
4597 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
4599 -- If we have a type with predicates, build predicate function
4601 if Is_Type (E) and then Has_Predicates (E) then
4602 Build_Predicate_Function (E, N);
4605 -- If type has delayed aspects, this is where we do the preanalysis at
4606 -- the freeze point, as part of the consistent visibility check. Note
4607 -- that this must be done after calling Build_Predicate_Function or
4608 -- Build_Invariant_Procedure since these subprograms fix occurrences of
4609 -- the subtype name in the saved expression so that they will not cause
4610 -- trouble in the preanalysis.
4612 if Has_Delayed_Aspects (E)
4613 and then Scope (E) = Current_Scope
4615 -- Retrieve the visibility to the discriminants in order to properly
4616 -- analyze the aspects.
4618 Push_Scope_And_Install_Discriminants (E);
4624 -- Look for aspect specification entries for this entity
4626 Ritem := First_Rep_Item (E);
4627 while Present (Ritem) loop
4628 if Nkind (Ritem) = N_Aspect_Specification
4629 and then Entity (Ritem) = E
4630 and then Is_Delayed_Aspect (Ritem)
4632 Check_Aspect_At_Freeze_Point (Ritem);
4635 Next_Rep_Item (Ritem);
4639 Uninstall_Discriminants_And_Pop_Scope (E);
4641 end Analyze_Freeze_Entity;
4643 ------------------------------------------
4644 -- Analyze_Record_Representation_Clause --
4645 ------------------------------------------
4647 -- Note: we check as much as we can here, but we can't do any checks
4648 -- based on the position values (e.g. overlap checks) until freeze time
4649 -- because especially in Ada 2005 (machine scalar mode), the processing
4650 -- for non-standard bit order can substantially change the positions.
4651 -- See procedure Check_Record_Representation_Clause (called from Freeze)
4652 -- for the remainder of this processing.
4654 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
4655 Ident : constant Node_Id := Identifier (N);
4660 Hbit : Uint := Uint_0;
4664 Rectype : Entity_Id;
4666 CR_Pragma : Node_Id := Empty;
4667 -- Points to N_Pragma node if Complete_Representation pragma present
4670 if Ignore_Rep_Clauses then
4675 Rectype := Entity (Ident);
4677 if Rectype = Any_Type
4678 or else Rep_Item_Too_Early (Rectype, N)
4682 Rectype := Underlying_Type (Rectype);
4685 -- First some basic error checks
4687 if not Is_Record_Type (Rectype) then
4689 ("record type required, found}", Ident, First_Subtype (Rectype));
4692 elsif Scope (Rectype) /= Current_Scope then
4693 Error_Msg_N ("type must be declared in this scope", N);
4696 elsif not Is_First_Subtype (Rectype) then
4697 Error_Msg_N ("cannot give record rep clause for subtype", N);
4700 elsif Has_Record_Rep_Clause (Rectype) then
4701 Error_Msg_N ("duplicate record rep clause ignored", N);
4704 elsif Rep_Item_Too_Late (Rectype, N) then
4708 if Present (Mod_Clause (N)) then
4710 Loc : constant Source_Ptr := Sloc (N);
4711 M : constant Node_Id := Mod_Clause (N);
4712 P : constant List_Id := Pragmas_Before (M);
4716 pragma Warnings (Off, Mod_Val);
4719 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
4721 if Warn_On_Obsolescent_Feature then
4723 ("mod clause is an obsolescent feature (RM J.8)?", N);
4725 ("\use alignment attribute definition clause instead?", N);
4732 -- In ASIS_Mode mode, expansion is disabled, but we must convert
4733 -- the Mod clause into an alignment clause anyway, so that the
4734 -- back-end can compute and back-annotate properly the size and
4735 -- alignment of types that may include this record.
4737 -- This seems dubious, this destroys the source tree in a manner
4738 -- not detectable by ASIS ???
4740 if Operating_Mode = Check_Semantics and then ASIS_Mode then
4742 Make_Attribute_Definition_Clause (Loc,
4743 Name => New_Reference_To (Base_Type (Rectype), Loc),
4744 Chars => Name_Alignment,
4745 Expression => Relocate_Node (Expression (M)));
4747 Set_From_At_Mod (AtM_Nod);
4748 Insert_After (N, AtM_Nod);
4749 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
4750 Set_Mod_Clause (N, Empty);
4753 -- Get the alignment value to perform error checking
4755 Mod_Val := Get_Alignment_Value (Expression (M));
4760 -- For untagged types, clear any existing component clauses for the
4761 -- type. If the type is derived, this is what allows us to override
4762 -- a rep clause for the parent. For type extensions, the representation
4763 -- of the inherited components is inherited, so we want to keep previous
4764 -- component clauses for completeness.
4766 if not Is_Tagged_Type (Rectype) then
4767 Comp := First_Component_Or_Discriminant (Rectype);
4768 while Present (Comp) loop
4769 Set_Component_Clause (Comp, Empty);
4770 Next_Component_Or_Discriminant (Comp);
4774 -- All done if no component clauses
4776 CC := First (Component_Clauses (N));
4782 -- A representation like this applies to the base type
4784 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
4785 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
4786 Set_Has_Specified_Layout (Base_Type (Rectype));
4788 -- Process the component clauses
4790 while Present (CC) loop
4794 if Nkind (CC) = N_Pragma then
4797 -- The only pragma of interest is Complete_Representation
4799 if Pragma_Name (CC) = Name_Complete_Representation then
4803 -- Processing for real component clause
4806 Posit := Static_Integer (Position (CC));
4807 Fbit := Static_Integer (First_Bit (CC));
4808 Lbit := Static_Integer (Last_Bit (CC));
4811 and then Fbit /= No_Uint
4812 and then Lbit /= No_Uint
4816 ("position cannot be negative", Position (CC));
4820 ("first bit cannot be negative", First_Bit (CC));
4822 -- The Last_Bit specified in a component clause must not be
4823 -- less than the First_Bit minus one (RM-13.5.1(10)).
4825 elsif Lbit < Fbit - 1 then
4827 ("last bit cannot be less than first bit minus one",
4830 -- Values look OK, so find the corresponding record component
4831 -- Even though the syntax allows an attribute reference for
4832 -- implementation-defined components, GNAT does not allow the
4833 -- tag to get an explicit position.
4835 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
4836 if Attribute_Name (Component_Name (CC)) = Name_Tag then
4837 Error_Msg_N ("position of tag cannot be specified", CC);
4839 Error_Msg_N ("illegal component name", CC);
4843 Comp := First_Entity (Rectype);
4844 while Present (Comp) loop
4845 exit when Chars (Comp) = Chars (Component_Name (CC));
4851 -- Maybe component of base type that is absent from
4852 -- statically constrained first subtype.
4854 Comp := First_Entity (Base_Type (Rectype));
4855 while Present (Comp) loop
4856 exit when Chars (Comp) = Chars (Component_Name (CC));
4863 ("component clause is for non-existent field", CC);
4865 -- Ada 2012 (AI05-0026): Any name that denotes a
4866 -- discriminant of an object of an unchecked union type
4867 -- shall not occur within a record_representation_clause.
4869 -- The general restriction of using record rep clauses on
4870 -- Unchecked_Union types has now been lifted. Since it is
4871 -- possible to introduce a record rep clause which mentions
4872 -- the discriminant of an Unchecked_Union in non-Ada 2012
4873 -- code, this check is applied to all versions of the
4876 elsif Ekind (Comp) = E_Discriminant
4877 and then Is_Unchecked_Union (Rectype)
4880 ("cannot reference discriminant of unchecked union",
4881 Component_Name (CC));
4883 elsif Present (Component_Clause (Comp)) then
4885 -- Diagnose duplicate rep clause, or check consistency
4886 -- if this is an inherited component. In a double fault,
4887 -- there may be a duplicate inconsistent clause for an
4888 -- inherited component.
4890 if Scope (Original_Record_Component (Comp)) = Rectype
4891 or else Parent (Component_Clause (Comp)) = N
4893 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
4894 Error_Msg_N ("component clause previously given#", CC);
4898 Rep1 : constant Node_Id := Component_Clause (Comp);
4900 if Intval (Position (Rep1)) /=
4901 Intval (Position (CC))
4902 or else Intval (First_Bit (Rep1)) /=
4903 Intval (First_Bit (CC))
4904 or else Intval (Last_Bit (Rep1)) /=
4905 Intval (Last_Bit (CC))
4908 ("component clause inconsistent "
4909 & "with representation of ancestor", CC);
4910 elsif Warn_On_Redundant_Constructs then
4912 ("?redundant component clause "
4913 & "for inherited component!", CC);
4918 -- Normal case where this is the first component clause we
4919 -- have seen for this entity, so set it up properly.
4922 -- Make reference for field in record rep clause and set
4923 -- appropriate entity field in the field identifier.
4926 (Comp, Component_Name (CC), Set_Ref => False);
4927 Set_Entity (Component_Name (CC), Comp);
4929 -- Update Fbit and Lbit to the actual bit number
4931 Fbit := Fbit + UI_From_Int (SSU) * Posit;
4932 Lbit := Lbit + UI_From_Int (SSU) * Posit;
4934 if Has_Size_Clause (Rectype)
4935 and then RM_Size (Rectype) <= Lbit
4938 ("bit number out of range of specified size",
4941 Set_Component_Clause (Comp, CC);
4942 Set_Component_Bit_Offset (Comp, Fbit);
4943 Set_Esize (Comp, 1 + (Lbit - Fbit));
4944 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
4945 Set_Normalized_Position (Comp, Fbit / SSU);
4947 if Warn_On_Overridden_Size
4948 and then Has_Size_Clause (Etype (Comp))
4949 and then RM_Size (Etype (Comp)) /= Esize (Comp)
4952 ("?component size overrides size clause for&",
4953 Component_Name (CC), Etype (Comp));
4956 -- This information is also set in the corresponding
4957 -- component of the base type, found by accessing the
4958 -- Original_Record_Component link if it is present.
4960 Ocomp := Original_Record_Component (Comp);
4967 (Component_Name (CC),
4973 (Comp, First_Node (CC), "component clause", Biased);
4975 if Present (Ocomp) then
4976 Set_Component_Clause (Ocomp, CC);
4977 Set_Component_Bit_Offset (Ocomp, Fbit);
4978 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
4979 Set_Normalized_Position (Ocomp, Fbit / SSU);
4980 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
4982 Set_Normalized_Position_Max
4983 (Ocomp, Normalized_Position (Ocomp));
4985 -- Note: we don't use Set_Biased here, because we
4986 -- already gave a warning above if needed, and we
4987 -- would get a duplicate for the same name here.
4989 Set_Has_Biased_Representation
4990 (Ocomp, Has_Biased_Representation (Comp));
4993 if Esize (Comp) < 0 then
4994 Error_Msg_N ("component size is negative", CC);
5005 -- Check missing components if Complete_Representation pragma appeared
5007 if Present (CR_Pragma) then
5008 Comp := First_Component_Or_Discriminant (Rectype);
5009 while Present (Comp) loop
5010 if No (Component_Clause (Comp)) then
5012 ("missing component clause for &", CR_Pragma, Comp);
5015 Next_Component_Or_Discriminant (Comp);
5018 -- If no Complete_Representation pragma, warn if missing components
5020 elsif Warn_On_Unrepped_Components then
5022 Num_Repped_Components : Nat := 0;
5023 Num_Unrepped_Components : Nat := 0;
5026 -- First count number of repped and unrepped components
5028 Comp := First_Component_Or_Discriminant (Rectype);
5029 while Present (Comp) loop
5030 if Present (Component_Clause (Comp)) then
5031 Num_Repped_Components := Num_Repped_Components + 1;
5033 Num_Unrepped_Components := Num_Unrepped_Components + 1;
5036 Next_Component_Or_Discriminant (Comp);
5039 -- We are only interested in the case where there is at least one
5040 -- unrepped component, and at least half the components have rep
5041 -- clauses. We figure that if less than half have them, then the
5042 -- partial rep clause is really intentional. If the component
5043 -- type has no underlying type set at this point (as for a generic
5044 -- formal type), we don't know enough to give a warning on the
5047 if Num_Unrepped_Components > 0
5048 and then Num_Unrepped_Components < Num_Repped_Components
5050 Comp := First_Component_Or_Discriminant (Rectype);
5051 while Present (Comp) loop
5052 if No (Component_Clause (Comp))
5053 and then Comes_From_Source (Comp)
5054 and then Present (Underlying_Type (Etype (Comp)))
5055 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
5056 or else Size_Known_At_Compile_Time
5057 (Underlying_Type (Etype (Comp))))
5058 and then not Has_Warnings_Off (Rectype)
5060 Error_Msg_Sloc := Sloc (Comp);
5062 ("?no component clause given for & declared #",
5066 Next_Component_Or_Discriminant (Comp);
5071 end Analyze_Record_Representation_Clause;
5073 -------------------------------------------
5074 -- Build_Invariant_Procedure_Declaration --
5075 -------------------------------------------
5077 function Build_Invariant_Procedure_Declaration
5078 (Typ : Entity_Id) return Node_Id
5080 Loc : constant Source_Ptr := Sloc (Typ);
5081 Object_Entity : constant Entity_Id :=
5082 Make_Defining_Identifier (Loc, New_Internal_Name ('I'));
5087 Set_Etype (Object_Entity, Typ);
5089 -- Check for duplicate definiations.
5091 if Has_Invariants (Typ)
5092 and then Present (Invariant_Procedure (Typ))
5097 SId := Make_Defining_Identifier (Loc,
5098 Chars => New_External_Name (Chars (Typ), "Invariant"));
5099 Set_Has_Invariants (SId);
5100 Set_Has_Invariants (Typ);
5101 Set_Ekind (SId, E_Procedure);
5102 Set_Invariant_Procedure (Typ, SId);
5105 Make_Procedure_Specification (Loc,
5106 Defining_Unit_Name => SId,
5107 Parameter_Specifications => New_List (
5108 Make_Parameter_Specification (Loc,
5109 Defining_Identifier => Object_Entity,
5110 Parameter_Type => New_Occurrence_Of (Typ, Loc))));
5112 return Make_Subprogram_Declaration (Loc, Specification => Spec);
5113 end Build_Invariant_Procedure_Declaration;
5115 -------------------------------
5116 -- Build_Invariant_Procedure --
5117 -------------------------------
5119 -- The procedure that is constructed here has the form
5121 -- procedure typInvariant (Ixxx : typ) is
5123 -- pragma Check (Invariant, exp, "failed invariant from xxx");
5124 -- pragma Check (Invariant, exp, "failed invariant from xxx");
5126 -- pragma Check (Invariant, exp, "failed inherited invariant from xxx");
5128 -- end typInvariant;
5130 procedure Build_Invariant_Procedure (Typ : Entity_Id; N : Node_Id) is
5131 Loc : constant Source_Ptr := Sloc (Typ);
5138 Visible_Decls : constant List_Id := Visible_Declarations (N);
5139 Private_Decls : constant List_Id := Private_Declarations (N);
5141 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean);
5142 -- Appends statements to Stmts for any invariants in the rep item chain
5143 -- of the given type. If Inherit is False, then we only process entries
5144 -- on the chain for the type Typ. If Inherit is True, then we ignore any
5145 -- Invariant aspects, but we process all Invariant'Class aspects, adding
5146 -- "inherited" to the exception message and generating an informational
5147 -- message about the inheritance of an invariant.
5149 Object_Name : Name_Id;
5150 -- Name for argument of invariant procedure
5152 Object_Entity : Node_Id;
5153 -- The entity of the formal for the procedure
5155 --------------------
5156 -- Add_Invariants --
5157 --------------------
5159 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean) is
5169 procedure Replace_Type_Reference (N : Node_Id);
5170 -- Replace a single occurrence N of the subtype name with a reference
5171 -- to the formal of the predicate function. N can be an identifier
5172 -- referencing the subtype, or a selected component, representing an
5173 -- appropriately qualified occurrence of the subtype name.
5175 procedure Replace_Type_References is
5176 new Replace_Type_References_Generic (Replace_Type_Reference);
5177 -- Traverse an expression replacing all occurrences of the subtype
5178 -- name with appropriate references to the object that is the formal
5179 -- parameter of the predicate function. Note that we must ensure
5180 -- that the type and entity information is properly set in the
5181 -- replacement node, since we will do a Preanalyze call of this
5182 -- expression without proper visibility of the procedure argument.
5184 ----------------------------
5185 -- Replace_Type_Reference --
5186 ----------------------------
5188 -- Note: See comments in Add_Predicates.Replace_Type_Reference
5189 -- regarding handling of Sloc and Comes_From_Source.
5191 procedure Replace_Type_Reference (N : Node_Id) is
5193 -- Invariant'Class, replace with T'Class (obj)
5195 if Class_Present (Ritem) then
5197 Make_Type_Conversion (Sloc (N),
5199 Make_Attribute_Reference (Sloc (N),
5200 Prefix => New_Occurrence_Of (T, Sloc (N)),
5201 Attribute_Name => Name_Class),
5202 Expression => Make_Identifier (Sloc (N), Object_Name)));
5204 Set_Entity (Expression (N), Object_Entity);
5205 Set_Etype (Expression (N), Typ);
5207 -- Invariant, replace with obj
5210 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
5211 Set_Entity (N, Object_Entity);
5215 Set_Comes_From_Source (N, True);
5216 end Replace_Type_Reference;
5218 -- Start of processing for Add_Invariants
5221 Ritem := First_Rep_Item (T);
5222 while Present (Ritem) loop
5223 if Nkind (Ritem) = N_Pragma
5224 and then Pragma_Name (Ritem) = Name_Invariant
5226 Arg1 := First (Pragma_Argument_Associations (Ritem));
5227 Arg2 := Next (Arg1);
5228 Arg3 := Next (Arg2);
5230 Arg1 := Get_Pragma_Arg (Arg1);
5231 Arg2 := Get_Pragma_Arg (Arg2);
5233 -- For Inherit case, ignore Invariant, process only Class case
5236 if not Class_Present (Ritem) then
5240 -- For Inherit false, process only item for right type
5243 if Entity (Arg1) /= Typ then
5249 Stmts := Empty_List;
5252 Exp := New_Copy_Tree (Arg2);
5254 -- Preserve sloc of original pragma Invariant
5256 Loc := Sloc (Ritem);
5258 -- We need to replace any occurrences of the name of the type
5259 -- with references to the object, converted to type'Class in
5260 -- the case of Invariant'Class aspects.
5262 Replace_Type_References (Exp, Chars (T));
5264 -- If this invariant comes from an aspect, find the aspect
5265 -- specification, and replace the saved expression because
5266 -- we need the subtype references replaced for the calls to
5267 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
5268 -- and Check_Aspect_At_End_Of_Declarations.
5270 if From_Aspect_Specification (Ritem) then
5275 -- Loop to find corresponding aspect, note that this
5276 -- must be present given the pragma is marked delayed.
5278 Aitem := Next_Rep_Item (Ritem);
5279 while Present (Aitem) loop
5280 if Nkind (Aitem) = N_Aspect_Specification
5281 and then Aspect_Rep_Item (Aitem) = Ritem
5284 (Identifier (Aitem), New_Copy_Tree (Exp));
5288 Aitem := Next_Rep_Item (Aitem);
5293 -- Now we need to preanalyze the expression to properly capture
5294 -- the visibility in the visible part. The expression will not
5295 -- be analyzed for real until the body is analyzed, but that is
5296 -- at the end of the private part and has the wrong visibility.
5298 Set_Parent (Exp, N);
5299 Preanalyze_Assert_Expression (Exp, Standard_Boolean);
5301 -- Build first two arguments for Check pragma
5304 Make_Pragma_Argument_Association (Loc,
5305 Expression => Make_Identifier (Loc, Name_Invariant)),
5306 Make_Pragma_Argument_Association (Loc,
5307 Expression => Exp));
5309 -- Add message if present in Invariant pragma
5311 if Present (Arg3) then
5312 Str := Strval (Get_Pragma_Arg (Arg3));
5314 -- If inherited case, and message starts "failed invariant",
5315 -- change it to be "failed inherited invariant".
5318 String_To_Name_Buffer (Str);
5320 if Name_Buffer (1 .. 16) = "failed invariant" then
5321 Insert_Str_In_Name_Buffer ("inherited ", 8);
5322 Str := String_From_Name_Buffer;
5327 Make_Pragma_Argument_Association (Loc,
5328 Expression => Make_String_Literal (Loc, Str)));
5331 -- Add Check pragma to list of statements
5335 Pragma_Identifier =>
5336 Make_Identifier (Loc, Name_Check),
5337 Pragma_Argument_Associations => Assoc));
5339 -- If Inherited case and option enabled, output info msg. Note
5340 -- that we know this is a case of Invariant'Class.
5342 if Inherit and Opt.List_Inherited_Aspects then
5343 Error_Msg_Sloc := Sloc (Ritem);
5345 ("?info: & inherits `Invariant''Class` aspect from #",
5351 Next_Rep_Item (Ritem);
5355 -- Start of processing for Build_Invariant_Procedure
5363 -- If the aspect specification exists for some view of the type, the
5364 -- declaration for the procedure has been created.
5366 if Has_Invariants (Typ) then
5367 SId := Invariant_Procedure (Typ);
5370 if Present (SId) then
5371 PDecl := Unit_Declaration_Node (SId);
5374 PDecl := Build_Invariant_Procedure_Declaration (Typ);
5377 -- Recover formal of procedure, for use in the calls to invariant
5378 -- functions (including inherited ones).
5382 (First (Parameter_Specifications (Specification (PDecl))));
5383 Object_Name := Chars (Object_Entity);
5385 -- Add invariants for the current type
5387 Add_Invariants (Typ, Inherit => False);
5389 -- Add invariants for parent types
5392 Current_Typ : Entity_Id;
5393 Parent_Typ : Entity_Id;
5398 Parent_Typ := Etype (Current_Typ);
5400 if Is_Private_Type (Parent_Typ)
5401 and then Present (Full_View (Base_Type (Parent_Typ)))
5403 Parent_Typ := Full_View (Base_Type (Parent_Typ));
5406 exit when Parent_Typ = Current_Typ;
5408 Current_Typ := Parent_Typ;
5409 Add_Invariants (Current_Typ, Inherit => True);
5413 -- Build the procedure if we generated at least one Check pragma
5415 if Stmts /= No_List then
5416 Spec := Copy_Separate_Tree (Specification (PDecl));
5419 Make_Subprogram_Body (Loc,
5420 Specification => Spec,
5421 Declarations => Empty_List,
5422 Handled_Statement_Sequence =>
5423 Make_Handled_Sequence_Of_Statements (Loc,
5424 Statements => Stmts));
5426 -- Insert procedure declaration and spec at the appropriate points.
5427 -- If declaration is already analyzed, it was processed by the
5428 -- generated pragma.
5430 if Present (Private_Decls) then
5432 -- The spec goes at the end of visible declarations, but they have
5433 -- already been analyzed, so we need to explicitly do the analyze.
5435 if not Analyzed (PDecl) then
5436 Append_To (Visible_Decls, PDecl);
5440 -- The body goes at the end of the private declarations, which we
5441 -- have not analyzed yet, so we do not need to perform an explicit
5442 -- analyze call. We skip this if there are no private declarations
5443 -- (this is an error that will be caught elsewhere);
5445 Append_To (Private_Decls, PBody);
5447 -- If the invariant appears on the full view of a type, the
5448 -- analysis of the private part is complete, and we must
5449 -- analyze the new body explicitly.
5451 if In_Private_Part (Current_Scope) then
5455 -- If there are no private declarations this may be an error that
5456 -- will be diagnosed elsewhere. However, if this is a non-private
5457 -- type that inherits invariants, it needs no completion and there
5458 -- may be no private part. In this case insert invariant procedure
5459 -- at end of current declarative list, and analyze at once, given
5460 -- that the type is about to be frozen.
5462 elsif not Is_Private_Type (Typ) then
5463 Append_To (Visible_Decls, PDecl);
5464 Append_To (Visible_Decls, PBody);
5469 end Build_Invariant_Procedure;
5471 ------------------------------
5472 -- Build_Predicate_Function --
5473 ------------------------------
5475 -- The procedure that is constructed here has the form:
5477 -- function typPredicate (Ixxx : typ) return Boolean is
5480 -- exp1 and then exp2 and then ...
5481 -- and then typ1Predicate (typ1 (Ixxx))
5482 -- and then typ2Predicate (typ2 (Ixxx))
5484 -- end typPredicate;
5486 -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that
5487 -- this is the point at which these expressions get analyzed, providing the
5488 -- required delay, and typ1, typ2, are entities from which predicates are
5489 -- inherited. Note that we do NOT generate Check pragmas, that's because we
5490 -- use this function even if checks are off, e.g. for membership tests.
5492 procedure Build_Predicate_Function (Typ : Entity_Id; N : Node_Id) is
5493 Loc : constant Source_Ptr := Sloc (Typ);
5500 -- This is the expression for the return statement in the function. It
5501 -- is build by connecting the component predicates with AND THEN.
5503 procedure Add_Call (T : Entity_Id);
5504 -- Includes a call to the predicate function for type T in Expr if T
5505 -- has predicates and Predicate_Function (T) is non-empty.
5507 procedure Add_Predicates;
5508 -- Appends expressions for any Predicate pragmas in the rep item chain
5509 -- Typ to Expr. Note that we look only at items for this exact entity.
5510 -- Inheritance of predicates for the parent type is done by calling the
5511 -- Predicate_Function of the parent type, using Add_Call above.
5513 Object_Name : constant Name_Id := New_Internal_Name ('I');
5514 -- Name for argument of Predicate procedure
5516 Object_Entity : constant Entity_Id :=
5517 Make_Defining_Identifier (Loc, Object_Name);
5518 -- The entity for the spec entity for the argument
5520 Dynamic_Predicate_Present : Boolean := False;
5521 -- Set True if a dynamic predicate is present, results in the entire
5522 -- predicate being considered dynamic even if it looks static
5524 Static_Predicate_Present : Node_Id := Empty;
5525 -- Set to N_Pragma node for a static predicate if one is encountered.
5531 procedure Add_Call (T : Entity_Id) is
5535 if Present (T) and then Present (Predicate_Function (T)) then
5536 Set_Has_Predicates (Typ);
5538 -- Build the call to the predicate function of T
5542 (T, Convert_To (T, Make_Identifier (Loc, Object_Name)));
5544 -- Add call to evolving expression, using AND THEN if needed
5551 Left_Opnd => Relocate_Node (Expr),
5555 -- Output info message on inheritance if required. Note we do not
5556 -- give this information for generic actual types, since it is
5557 -- unwelcome noise in that case in instantiations. We also
5558 -- generally suppress the message in instantiations, and also
5559 -- if it involves internal names.
5561 if Opt.List_Inherited_Aspects
5562 and then not Is_Generic_Actual_Type (Typ)
5563 and then Instantiation_Depth (Sloc (Typ)) = 0
5564 and then not Is_Internal_Name (Chars (T))
5565 and then not Is_Internal_Name (Chars (Typ))
5567 Error_Msg_Sloc := Sloc (Predicate_Function (T));
5568 Error_Msg_Node_2 := T;
5569 Error_Msg_N ("?info: & inherits predicate from & #", Typ);
5574 --------------------
5575 -- Add_Predicates --
5576 --------------------
5578 procedure Add_Predicates is
5583 procedure Replace_Type_Reference (N : Node_Id);
5584 -- Replace a single occurrence N of the subtype name with a reference
5585 -- to the formal of the predicate function. N can be an identifier
5586 -- referencing the subtype, or a selected component, representing an
5587 -- appropriately qualified occurrence of the subtype name.
5589 procedure Replace_Type_References is
5590 new Replace_Type_References_Generic (Replace_Type_Reference);
5591 -- Traverse an expression changing every occurrence of an identifier
5592 -- whose name matches the name of the subtype with a reference to
5593 -- the formal parameter of the predicate function.
5595 ----------------------------
5596 -- Replace_Type_Reference --
5597 ----------------------------
5599 procedure Replace_Type_Reference (N : Node_Id) is
5601 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
5602 -- Use the Sloc of the usage name, not the defining name
5604 Set_Entity (N, Object_Entity);
5607 -- We want to treat the node as if it comes from source, so that
5608 -- ASIS will not ignore it
5610 Set_Comes_From_Source (N, True);
5611 end Replace_Type_Reference;
5613 -- Start of processing for Add_Predicates
5616 Ritem := First_Rep_Item (Typ);
5617 while Present (Ritem) loop
5618 if Nkind (Ritem) = N_Pragma
5619 and then Pragma_Name (Ritem) = Name_Predicate
5621 if Present (Corresponding_Aspect (Ritem)) then
5622 case Chars (Identifier (Corresponding_Aspect (Ritem))) is
5623 when Name_Dynamic_Predicate =>
5624 Dynamic_Predicate_Present := True;
5625 when Name_Static_Predicate =>
5626 Static_Predicate_Present := Ritem;
5632 -- Acquire arguments
5634 Arg1 := First (Pragma_Argument_Associations (Ritem));
5635 Arg2 := Next (Arg1);
5637 Arg1 := Get_Pragma_Arg (Arg1);
5638 Arg2 := Get_Pragma_Arg (Arg2);
5640 -- See if this predicate pragma is for the current type or for
5641 -- its full view. A predicate on a private completion is placed
5642 -- on the partial view beause this is the visible entity that
5645 if Entity (Arg1) = Typ
5646 or else Full_View (Entity (Arg1)) = Typ
5648 -- We have a match, this entry is for our subtype
5650 -- We need to replace any occurrences of the name of the
5651 -- type with references to the object.
5653 Replace_Type_References (Arg2, Chars (Typ));
5655 -- If this predicate comes from an aspect, find the aspect
5656 -- specification, and replace the saved expression because
5657 -- we need the subtype references replaced for the calls to
5658 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
5659 -- and Check_Aspect_At_End_Of_Declarations.
5661 if From_Aspect_Specification (Ritem) then
5666 -- Loop to find corresponding aspect, note that this
5667 -- must be present given the pragma is marked delayed.
5669 Aitem := Next_Rep_Item (Ritem);
5671 if Nkind (Aitem) = N_Aspect_Specification
5672 and then Aspect_Rep_Item (Aitem) = Ritem
5675 (Identifier (Aitem), New_Copy_Tree (Arg2));
5679 Aitem := Next_Rep_Item (Aitem);
5684 -- Now we can add the expression
5687 Expr := Relocate_Node (Arg2);
5689 -- There already was a predicate, so add to it
5694 Left_Opnd => Relocate_Node (Expr),
5695 Right_Opnd => Relocate_Node (Arg2));
5700 Next_Rep_Item (Ritem);
5704 -- Start of processing for Build_Predicate_Function
5707 -- Initialize for construction of statement list
5711 -- Return if already built or if type does not have predicates
5713 if not Has_Predicates (Typ)
5714 or else Present (Predicate_Function (Typ))
5719 -- Add Predicates for the current type
5723 -- Add predicates for ancestor if present
5726 Atyp : constant Entity_Id := Nearest_Ancestor (Typ);
5728 if Present (Atyp) then
5733 -- If we have predicates, build the function
5735 if Present (Expr) then
5737 -- Build function declaration
5740 Make_Defining_Identifier (Loc,
5741 Chars => New_External_Name (Chars (Typ), "Predicate"));
5742 Set_Has_Predicates (SId);
5743 Set_Ekind (SId, E_Function);
5744 Set_Predicate_Function (Typ, SId);
5746 -- The predicate function is shared between views of a type.
5748 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
5749 Set_Predicate_Function (Full_View (Typ), SId);
5753 Make_Function_Specification (Loc,
5754 Defining_Unit_Name => SId,
5755 Parameter_Specifications => New_List (
5756 Make_Parameter_Specification (Loc,
5757 Defining_Identifier => Object_Entity,
5758 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
5759 Result_Definition =>
5760 New_Occurrence_Of (Standard_Boolean, Loc));
5762 FDecl := Make_Subprogram_Declaration (Loc, Specification => Spec);
5764 -- Build function body
5767 Make_Defining_Identifier (Loc,
5768 Chars => New_External_Name (Chars (Typ), "Predicate"));
5771 Make_Function_Specification (Loc,
5772 Defining_Unit_Name => SId,
5773 Parameter_Specifications => New_List (
5774 Make_Parameter_Specification (Loc,
5775 Defining_Identifier =>
5776 Make_Defining_Identifier (Loc, Object_Name),
5778 New_Occurrence_Of (Typ, Loc))),
5779 Result_Definition =>
5780 New_Occurrence_Of (Standard_Boolean, Loc));
5783 Make_Subprogram_Body (Loc,
5784 Specification => Spec,
5785 Declarations => Empty_List,
5786 Handled_Statement_Sequence =>
5787 Make_Handled_Sequence_Of_Statements (Loc,
5788 Statements => New_List (
5789 Make_Simple_Return_Statement (Loc,
5790 Expression => Expr))));
5792 -- Insert declaration before freeze node and body after
5794 Insert_Before_And_Analyze (N, FDecl);
5795 Insert_After_And_Analyze (N, FBody);
5797 -- Deal with static predicate case
5799 if Ekind_In (Typ, E_Enumeration_Subtype,
5800 E_Modular_Integer_Subtype,
5801 E_Signed_Integer_Subtype)
5802 and then Is_Static_Subtype (Typ)
5803 and then not Dynamic_Predicate_Present
5805 Build_Static_Predicate (Typ, Expr, Object_Name);
5807 if Present (Static_Predicate_Present)
5808 and No (Static_Predicate (Typ))
5811 ("expression does not have required form for "
5812 & "static predicate",
5813 Next (First (Pragma_Argument_Associations
5814 (Static_Predicate_Present))));
5818 end Build_Predicate_Function;
5820 ----------------------------
5821 -- Build_Static_Predicate --
5822 ----------------------------
5824 procedure Build_Static_Predicate
5829 Loc : constant Source_Ptr := Sloc (Expr);
5831 Non_Static : exception;
5832 -- Raised if something non-static is found
5834 Btyp : constant Entity_Id := Base_Type (Typ);
5836 BLo : constant Uint := Expr_Value (Type_Low_Bound (Btyp));
5837 BHi : constant Uint := Expr_Value (Type_High_Bound (Btyp));
5838 -- Low bound and high bound value of base type of Typ
5840 TLo : constant Uint := Expr_Value (Type_Low_Bound (Typ));
5841 THi : constant Uint := Expr_Value (Type_High_Bound (Typ));
5842 -- Low bound and high bound values of static subtype Typ
5847 -- One entry in a Rlist value, a single REnt (range entry) value
5848 -- denotes one range from Lo to Hi. To represent a single value
5849 -- range Lo = Hi = value.
5851 type RList is array (Nat range <>) of REnt;
5852 -- A list of ranges. The ranges are sorted in increasing order,
5853 -- and are disjoint (there is a gap of at least one value between
5854 -- each range in the table). A value is in the set of ranges in
5855 -- Rlist if it lies within one of these ranges
5857 False_Range : constant RList :=
5858 RList'(1 .. 0 => REnt'(No_Uint, No_Uint));
5859 -- An empty set of ranges represents a range list that can never be
5860 -- satisfied, since there are no ranges in which the value could lie,
5861 -- so it does not lie in any of them. False_Range is a canonical value
5862 -- for this empty set, but general processing should test for an Rlist
5863 -- with length zero (see Is_False predicate), since other null ranges
5864 -- may appear which must be treated as False.
5866 True_Range : constant RList := RList'(1 => REnt'(BLo, BHi));
5867 -- Range representing True, value must be in the base range
5869 function "and" (Left, Right : RList) return RList;
5870 -- And's together two range lists, returning a range list. This is
5871 -- a set intersection operation.
5873 function "or" (Left, Right : RList) return RList;
5874 -- Or's together two range lists, returning a range list. This is a
5875 -- set union operation.
5877 function "not" (Right : RList) return RList;
5878 -- Returns complement of a given range list, i.e. a range list
5879 -- representing all the values in TLo .. THi that are not in the
5880 -- input operand Right.
5882 function Build_Val (V : Uint) return Node_Id;
5883 -- Return an analyzed N_Identifier node referencing this value, suitable
5884 -- for use as an entry in the Static_Predicate list. This node is typed
5885 -- with the base type.
5887 function Build_Range (Lo, Hi : Uint) return Node_Id;
5888 -- Return an analyzed N_Range node referencing this range, suitable
5889 -- for use as an entry in the Static_Predicate list. This node is typed
5890 -- with the base type.
5892 function Get_RList (Exp : Node_Id) return RList;
5893 -- This is a recursive routine that converts the given expression into
5894 -- a list of ranges, suitable for use in building the static predicate.
5896 function Is_False (R : RList) return Boolean;
5897 pragma Inline (Is_False);
5898 -- Returns True if the given range list is empty, and thus represents
5899 -- a False list of ranges that can never be satisfied.
5901 function Is_True (R : RList) return Boolean;
5902 -- Returns True if R trivially represents the True predicate by having
5903 -- a single range from BLo to BHi.
5905 function Is_Type_Ref (N : Node_Id) return Boolean;
5906 pragma Inline (Is_Type_Ref);
5907 -- Returns if True if N is a reference to the type for the predicate in
5908 -- the expression (i.e. if it is an identifier whose Chars field matches
5909 -- the Nam given in the call).
5911 function Lo_Val (N : Node_Id) return Uint;
5912 -- Given static expression or static range from a Static_Predicate list,
5913 -- gets expression value or low bound of range.
5915 function Hi_Val (N : Node_Id) return Uint;
5916 -- Given static expression or static range from a Static_Predicate list,
5917 -- gets expression value of high bound of range.
5919 function Membership_Entry (N : Node_Id) return RList;
5920 -- Given a single membership entry (range, value, or subtype), returns
5921 -- the corresponding range list. Raises Static_Error if not static.
5923 function Membership_Entries (N : Node_Id) return RList;
5924 -- Given an element on an alternatives list of a membership operation,
5925 -- returns the range list corresponding to this entry and all following
5926 -- entries (i.e. returns the "or" of this list of values).
5928 function Stat_Pred (Typ : Entity_Id) return RList;
5929 -- Given a type, if it has a static predicate, then return the predicate
5930 -- as a range list, otherwise raise Non_Static.
5936 function "and" (Left, Right : RList) return RList is
5938 -- First range of result
5940 SLeft : Nat := Left'First;
5941 -- Start of rest of left entries
5943 SRight : Nat := Right'First;
5944 -- Start of rest of right entries
5947 -- If either range is True, return the other
5949 if Is_True (Left) then
5951 elsif Is_True (Right) then
5955 -- If either range is False, return False
5957 if Is_False (Left) or else Is_False (Right) then
5961 -- Loop to remove entries at start that are disjoint, and thus
5962 -- just get discarded from the result entirely.
5965 -- If no operands left in either operand, result is false
5967 if SLeft > Left'Last or else SRight > Right'Last then
5970 -- Discard first left operand entry if disjoint with right
5972 elsif Left (SLeft).Hi < Right (SRight).Lo then
5975 -- Discard first right operand entry if disjoint with left
5977 elsif Right (SRight).Hi < Left (SLeft).Lo then
5978 SRight := SRight + 1;
5980 -- Otherwise we have an overlapping entry
5987 -- Now we have two non-null operands, and first entries overlap.
5988 -- The first entry in the result will be the overlapping part of
5989 -- these two entries.
5991 FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo),
5992 Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi));
5994 -- Now we can remove the entry that ended at a lower value, since
5995 -- its contribution is entirely contained in Fent.
5997 if Left (SLeft).Hi <= Right (SRight).Hi then
6000 SRight := SRight + 1;
6003 -- Compute result by concatenating this first entry with the "and"
6004 -- of the remaining parts of the left and right operands. Note that
6005 -- if either of these is empty, "and" will yield empty, so that we
6006 -- will end up with just Fent, which is what we want in that case.
6009 FEnt & (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last));
6016 function "not" (Right : RList) return RList is
6018 -- Return True if False range
6020 if Is_False (Right) then
6024 -- Return False if True range
6026 if Is_True (Right) then
6030 -- Here if not trivial case
6033 Result : RList (1 .. Right'Length + 1);
6034 -- May need one more entry for gap at beginning and end
6037 -- Number of entries stored in Result
6042 if Right (Right'First).Lo > TLo then
6044 Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1);
6047 -- Gaps between ranges
6049 for J in Right'First .. Right'Last - 1 loop
6052 REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1);
6057 if Right (Right'Last).Hi < THi then
6059 Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi);
6062 return Result (1 .. Count);
6070 function "or" (Left, Right : RList) return RList is
6072 -- First range of result
6074 SLeft : Nat := Left'First;
6075 -- Start of rest of left entries
6077 SRight : Nat := Right'First;
6078 -- Start of rest of right entries
6081 -- If either range is True, return True
6083 if Is_True (Left) or else Is_True (Right) then
6087 -- If either range is False (empty), return the other
6089 if Is_False (Left) then
6091 elsif Is_False (Right) then
6095 -- Initialize result first entry from left or right operand
6096 -- depending on which starts with the lower range.
6098 if Left (SLeft).Lo < Right (SRight).Lo then
6099 FEnt := Left (SLeft);
6102 FEnt := Right (SRight);
6103 SRight := SRight + 1;
6106 -- This loop eats ranges from left and right operands that
6107 -- are contiguous with the first range we are gathering.
6110 -- Eat first entry in left operand if contiguous or
6111 -- overlapped by gathered first operand of result.
6113 if SLeft <= Left'Last
6114 and then Left (SLeft).Lo <= FEnt.Hi + 1
6116 FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi);
6119 -- Eat first entry in right operand if contiguous or
6120 -- overlapped by gathered right operand of result.
6122 elsif SRight <= Right'Last
6123 and then Right (SRight).Lo <= FEnt.Hi + 1
6125 FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi);
6126 SRight := SRight + 1;
6128 -- All done if no more entries to eat!
6135 -- Obtain result as the first entry we just computed, concatenated
6136 -- to the "or" of the remaining results (if one operand is empty,
6137 -- this will just concatenate with the other
6140 FEnt & (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last));
6147 function Build_Range (Lo, Hi : Uint) return Node_Id is
6151 return Build_Val (Hi);
6155 Low_Bound => Build_Val (Lo),
6156 High_Bound => Build_Val (Hi));
6157 Set_Etype (Result, Btyp);
6158 Set_Analyzed (Result);
6167 function Build_Val (V : Uint) return Node_Id is
6171 if Is_Enumeration_Type (Typ) then
6172 Result := Get_Enum_Lit_From_Pos (Typ, V, Loc);
6174 Result := Make_Integer_Literal (Loc, V);
6177 Set_Etype (Result, Btyp);
6178 Set_Is_Static_Expression (Result);
6179 Set_Analyzed (Result);
6187 function Get_RList (Exp : Node_Id) return RList is
6192 -- Static expression can only be true or false
6194 if Is_OK_Static_Expression (Exp) then
6198 if Expr_Value (Exp) = 0 then
6205 -- Otherwise test node type
6213 when N_Op_And | N_And_Then =>
6214 return Get_RList (Left_Opnd (Exp))
6216 Get_RList (Right_Opnd (Exp));
6220 when N_Op_Or | N_Or_Else =>
6221 return Get_RList (Left_Opnd (Exp))
6223 Get_RList (Right_Opnd (Exp));
6228 return not Get_RList (Right_Opnd (Exp));
6230 -- Comparisons of type with static value
6232 when N_Op_Compare =>
6234 -- Type is left operand
6236 if Is_Type_Ref (Left_Opnd (Exp))
6237 and then Is_OK_Static_Expression (Right_Opnd (Exp))
6239 Val := Expr_Value (Right_Opnd (Exp));
6241 -- Typ is right operand
6243 elsif Is_Type_Ref (Right_Opnd (Exp))
6244 and then Is_OK_Static_Expression (Left_Opnd (Exp))
6246 Val := Expr_Value (Left_Opnd (Exp));
6248 -- Invert sense of comparison
6251 when N_Op_Gt => Op := N_Op_Lt;
6252 when N_Op_Lt => Op := N_Op_Gt;
6253 when N_Op_Ge => Op := N_Op_Le;
6254 when N_Op_Le => Op := N_Op_Ge;
6255 when others => null;
6258 -- Other cases are non-static
6264 -- Construct range according to comparison operation
6268 return RList'(1 => REnt'(Val, Val));
6271 return RList'(1 => REnt'(Val, BHi));
6274 return RList'(1 => REnt'(Val + 1, BHi));
6277 return RList'(1 => REnt'(BLo, Val));
6280 return RList'(1 => REnt'(BLo, Val - 1));
6283 return RList'(REnt'(BLo, Val - 1),
6284 REnt'(Val + 1, BHi));
6287 raise Program_Error;
6293 if not Is_Type_Ref (Left_Opnd (Exp)) then
6297 if Present (Right_Opnd (Exp)) then
6298 return Membership_Entry (Right_Opnd (Exp));
6300 return Membership_Entries (First (Alternatives (Exp)));
6303 -- Negative membership (NOT IN)
6306 if not Is_Type_Ref (Left_Opnd (Exp)) then
6310 if Present (Right_Opnd (Exp)) then
6311 return not Membership_Entry (Right_Opnd (Exp));
6313 return not Membership_Entries (First (Alternatives (Exp)));
6316 -- Function call, may be call to static predicate
6318 when N_Function_Call =>
6319 if Is_Entity_Name (Name (Exp)) then
6321 Ent : constant Entity_Id := Entity (Name (Exp));
6323 if Has_Predicates (Ent) then
6324 return Stat_Pred (Etype (First_Formal (Ent)));
6329 -- Other function call cases are non-static
6333 -- Qualified expression, dig out the expression
6335 when N_Qualified_Expression =>
6336 return Get_RList (Expression (Exp));
6341 return (Get_RList (Left_Opnd (Exp))
6342 and not Get_RList (Right_Opnd (Exp)))
6343 or (Get_RList (Right_Opnd (Exp))
6344 and not Get_RList (Left_Opnd (Exp)));
6346 -- Any other node type is non-static
6357 function Hi_Val (N : Node_Id) return Uint is
6359 if Is_Static_Expression (N) then
6360 return Expr_Value (N);
6362 pragma Assert (Nkind (N) = N_Range);
6363 return Expr_Value (High_Bound (N));
6371 function Is_False (R : RList) return Boolean is
6373 return R'Length = 0;
6380 function Is_True (R : RList) return Boolean is
6383 and then R (R'First).Lo = BLo
6384 and then R (R'First).Hi = BHi;
6391 function Is_Type_Ref (N : Node_Id) return Boolean is
6393 return Nkind (N) = N_Identifier and then Chars (N) = Nam;
6400 function Lo_Val (N : Node_Id) return Uint is
6402 if Is_Static_Expression (N) then
6403 return Expr_Value (N);
6405 pragma Assert (Nkind (N) = N_Range);
6406 return Expr_Value (Low_Bound (N));
6410 ------------------------
6411 -- Membership_Entries --
6412 ------------------------
6414 function Membership_Entries (N : Node_Id) return RList is
6416 if No (Next (N)) then
6417 return Membership_Entry (N);
6419 return Membership_Entry (N) or Membership_Entries (Next (N));
6421 end Membership_Entries;
6423 ----------------------
6424 -- Membership_Entry --
6425 ----------------------
6427 function Membership_Entry (N : Node_Id) return RList is
6435 if Nkind (N) = N_Range then
6436 if not Is_Static_Expression (Low_Bound (N))
6438 not Is_Static_Expression (High_Bound (N))
6442 SLo := Expr_Value (Low_Bound (N));
6443 SHi := Expr_Value (High_Bound (N));
6444 return RList'(1 => REnt'(SLo, SHi));
6447 -- Static expression case
6449 elsif Is_Static_Expression (N) then
6450 Val := Expr_Value (N);
6451 return RList'(1 => REnt'(Val, Val));
6453 -- Identifier (other than static expression) case
6455 else pragma Assert (Nkind (N) = N_Identifier);
6459 if Is_Type (Entity (N)) then
6461 -- If type has predicates, process them
6463 if Has_Predicates (Entity (N)) then
6464 return Stat_Pred (Entity (N));
6466 -- For static subtype without predicates, get range
6468 elsif Is_Static_Subtype (Entity (N)) then
6469 SLo := Expr_Value (Type_Low_Bound (Entity (N)));
6470 SHi := Expr_Value (Type_High_Bound (Entity (N)));
6471 return RList'(1 => REnt'(SLo, SHi));
6473 -- Any other type makes us non-static
6479 -- Any other kind of identifier in predicate (e.g. a non-static
6480 -- expression value) means this is not a static predicate.
6486 end Membership_Entry;
6492 function Stat_Pred (Typ : Entity_Id) return RList is
6494 -- Not static if type does not have static predicates
6496 if not Has_Predicates (Typ) or else No (Static_Predicate (Typ)) then
6500 -- Otherwise we convert the predicate list to a range list
6503 Result : RList (1 .. List_Length (Static_Predicate (Typ)));
6507 P := First (Static_Predicate (Typ));
6508 for J in Result'Range loop
6509 Result (J) := REnt'(Lo_Val (P), Hi_Val (P));
6517 -- Start of processing for Build_Static_Predicate
6520 -- Now analyze the expression to see if it is a static predicate
6523 Ranges : constant RList := Get_RList (Expr);
6524 -- Range list from expression if it is static
6529 -- Convert range list into a form for the static predicate. In the
6530 -- Ranges array, we just have raw ranges, these must be converted
6531 -- to properly typed and analyzed static expressions or range nodes.
6533 -- Note: here we limit ranges to the ranges of the subtype, so that
6534 -- a predicate is always false for values outside the subtype. That
6535 -- seems fine, such values are invalid anyway, and considering them
6536 -- to fail the predicate seems allowed and friendly, and furthermore
6537 -- simplifies processing for case statements and loops.
6541 for J in Ranges'Range loop
6543 Lo : Uint := Ranges (J).Lo;
6544 Hi : Uint := Ranges (J).Hi;
6547 -- Ignore completely out of range entry
6549 if Hi < TLo or else Lo > THi then
6552 -- Otherwise process entry
6555 -- Adjust out of range value to subtype range
6565 -- Convert range into required form
6568 Append_To (Plist, Build_Val (Lo));
6570 Append_To (Plist, Build_Range (Lo, Hi));
6576 -- Processing was successful and all entries were static, so now we
6577 -- can store the result as the predicate list.
6579 Set_Static_Predicate (Typ, Plist);
6581 -- The processing for static predicates put the expression into
6582 -- canonical form as a series of ranges. It also eliminated
6583 -- duplicates and collapsed and combined ranges. We might as well
6584 -- replace the alternatives list of the right operand of the
6585 -- membership test with the static predicate list, which will
6586 -- usually be more efficient.
6589 New_Alts : constant List_Id := New_List;
6594 Old_Node := First (Plist);
6595 while Present (Old_Node) loop
6596 New_Node := New_Copy (Old_Node);
6598 if Nkind (New_Node) = N_Range then
6599 Set_Low_Bound (New_Node, New_Copy (Low_Bound (Old_Node)));
6600 Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node)));
6603 Append_To (New_Alts, New_Node);
6607 -- If empty list, replace by False
6609 if Is_Empty_List (New_Alts) then
6610 Rewrite (Expr, New_Occurrence_Of (Standard_False, Loc));
6612 -- Else replace by set membership test
6617 Left_Opnd => Make_Identifier (Loc, Nam),
6618 Right_Opnd => Empty,
6619 Alternatives => New_Alts));
6621 -- Resolve new expression in function context
6623 Install_Formals (Predicate_Function (Typ));
6624 Push_Scope (Predicate_Function (Typ));
6625 Analyze_And_Resolve (Expr, Standard_Boolean);
6631 -- If non-static, return doing nothing
6636 end Build_Static_Predicate;
6638 -----------------------------------------
6639 -- Check_Aspect_At_End_Of_Declarations --
6640 -----------------------------------------
6642 procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id) is
6643 Ent : constant Entity_Id := Entity (ASN);
6644 Ident : constant Node_Id := Identifier (ASN);
6645 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
6647 End_Decl_Expr : constant Node_Id := Entity (Ident);
6648 -- Expression to be analyzed at end of declarations
6650 Freeze_Expr : constant Node_Id := Expression (ASN);
6651 -- Expression from call to Check_Aspect_At_Freeze_Point
6653 T : constant Entity_Id := Etype (Freeze_Expr);
6654 -- Type required for preanalyze call
6657 -- Set False if error
6659 -- On entry to this procedure, Entity (Ident) contains a copy of the
6660 -- original expression from the aspect, saved for this purpose, and
6661 -- but Expression (Ident) is a preanalyzed copy of the expression,
6662 -- preanalyzed just after the freeze point.
6664 procedure Check_Overloaded_Name;
6665 -- For aspects whose expression is simply a name, this routine checks if
6666 -- the name is overloaded or not. If so, it verifies there is an
6667 -- interpretation that matches the entity obtained at the freeze point,
6668 -- otherwise the compiler complains.
6670 ---------------------------
6671 -- Check_Overloaded_Name --
6672 ---------------------------
6674 procedure Check_Overloaded_Name is
6676 if not Is_Overloaded (End_Decl_Expr) then
6677 Err := Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
6683 Index : Interp_Index;
6687 Get_First_Interp (End_Decl_Expr, Index, It);
6688 while Present (It.Typ) loop
6689 if It.Nam = Entity (Freeze_Expr) then
6694 Get_Next_Interp (Index, It);
6698 end Check_Overloaded_Name;
6700 -- Start of processing for Check_Aspect_At_End_Of_Declarations
6703 -- Case of aspects Dimension, Dimension_System and Synchronization
6705 if A_Id = Aspect_Synchronization then
6708 -- Case of stream attributes, just have to compare entities. However,
6709 -- the expression is just a name (possibly overloaded), and there may
6710 -- be stream operations declared for unrelated types, so we just need
6711 -- to verify that one of these interpretations is the one available at
6712 -- at the freeze point.
6714 elsif A_Id = Aspect_Input or else
6715 A_Id = Aspect_Output or else
6716 A_Id = Aspect_Read or else
6719 Analyze (End_Decl_Expr);
6720 Check_Overloaded_Name;
6722 elsif A_Id = Aspect_Variable_Indexing or else
6723 A_Id = Aspect_Constant_Indexing or else
6724 A_Id = Aspect_Default_Iterator or else
6725 A_Id = Aspect_Iterator_Element
6727 -- Make type unfrozen before analysis, to prevent spurious errors
6728 -- about late attributes.
6730 Set_Is_Frozen (Ent, False);
6731 Analyze (End_Decl_Expr);
6732 Set_Is_Frozen (Ent, True);
6734 -- If the end of declarations comes before any other freeze
6735 -- point, the Freeze_Expr is not analyzed: no check needed.
6737 if Analyzed (Freeze_Expr) and then not In_Instance then
6738 Check_Overloaded_Name;
6746 -- In a generic context the aspect expressions have not been
6747 -- preanalyzed, so do it now. There are no conformance checks
6748 -- to perform in this case.
6751 Check_Aspect_At_Freeze_Point (ASN);
6754 -- The default values attributes may be defined in the private part,
6755 -- and the analysis of the expression may take place when only the
6756 -- partial view is visible. The expression must be scalar, so use
6757 -- the full view to resolve.
6759 elsif (A_Id = Aspect_Default_Value
6761 A_Id = Aspect_Default_Component_Value)
6762 and then Is_Private_Type (T)
6764 Preanalyze_Spec_Expression (End_Decl_Expr, Full_View (T));
6766 Preanalyze_Spec_Expression (End_Decl_Expr, T);
6769 Err := not Fully_Conformant_Expressions (End_Decl_Expr, Freeze_Expr);
6772 -- Output error message if error
6776 ("visibility of aspect for& changes after freeze point",
6779 ("?info: & is frozen here, aspects evaluated at this point",
6780 Freeze_Node (Ent), Ent);
6782 end Check_Aspect_At_End_Of_Declarations;
6784 ----------------------------------
6785 -- Check_Aspect_At_Freeze_Point --
6786 ----------------------------------
6788 procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id) is
6789 Ident : constant Node_Id := Identifier (ASN);
6790 -- Identifier (use Entity field to save expression)
6792 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
6794 T : Entity_Id := Empty;
6795 -- Type required for preanalyze call
6798 -- On entry to this procedure, Entity (Ident) contains a copy of the
6799 -- original expression from the aspect, saved for this purpose.
6801 -- On exit from this procedure Entity (Ident) is unchanged, still
6802 -- containing that copy, but Expression (Ident) is a preanalyzed copy
6803 -- of the expression, preanalyzed just after the freeze point.
6805 -- Make a copy of the expression to be preanalyzed
6807 Set_Expression (ASN, New_Copy_Tree (Entity (Ident)));
6809 -- Find type for preanalyze call
6813 -- No_Aspect should be impossible
6816 raise Program_Error;
6818 -- Aspects taking an optional boolean argument
6820 when Boolean_Aspects |
6821 Library_Unit_Aspects =>
6822 T := Standard_Boolean;
6824 when Aspect_Attach_Handler =>
6825 T := RTE (RE_Interrupt_ID);
6827 when Aspect_Convention =>
6830 -- Default_Value is resolved with the type entity in question
6832 when Aspect_Default_Value =>
6835 -- Default_Component_Value is resolved with the component type
6837 when Aspect_Default_Component_Value =>
6838 T := Component_Type (Entity (ASN));
6840 -- Aspects corresponding to attribute definition clauses
6842 when Aspect_Address =>
6843 T := RTE (RE_Address);
6845 when Aspect_Bit_Order | Aspect_Scalar_Storage_Order =>
6846 T := RTE (RE_Bit_Order);
6849 T := RTE (RE_CPU_Range);
6851 when Aspect_Dispatching_Domain =>
6852 T := RTE (RE_Dispatching_Domain);
6854 when Aspect_External_Tag =>
6855 T := Standard_String;
6857 when Aspect_External_Name =>
6858 T := Standard_String;
6860 when Aspect_Link_Name =>
6861 T := Standard_String;
6863 when Aspect_Priority | Aspect_Interrupt_Priority =>
6864 T := Standard_Integer;
6866 when Aspect_Relative_Deadline =>
6867 T := RTE (RE_Time_Span);
6869 when Aspect_Small =>
6870 T := Universal_Real;
6872 -- For a simple storage pool, we have to retrieve the type of the
6873 -- pool object associated with the aspect's corresponding attribute
6874 -- definition clause.
6876 when Aspect_Simple_Storage_Pool =>
6877 T := Etype (Expression (Aspect_Rep_Item (ASN)));
6879 when Aspect_Storage_Pool =>
6880 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
6882 when Aspect_Alignment |
6883 Aspect_Component_Size |
6884 Aspect_Machine_Radix |
6885 Aspect_Object_Size |
6887 Aspect_Storage_Size |
6888 Aspect_Stream_Size |
6889 Aspect_Value_Size =>
6892 when Aspect_Synchronization =>
6895 -- Special case, the expression of these aspects is just an entity
6896 -- that does not need any resolution, so just analyze.
6905 Analyze (Expression (ASN));
6908 -- Same for Iterator aspects, where the expression is a function
6909 -- name. Legality rules are checked separately.
6911 when Aspect_Constant_Indexing |
6912 Aspect_Default_Iterator |
6913 Aspect_Iterator_Element |
6914 Aspect_Variable_Indexing =>
6915 Analyze (Expression (ASN));
6918 -- Invariant/Predicate take boolean expressions
6920 when Aspect_Dynamic_Predicate |
6923 Aspect_Static_Predicate |
6924 Aspect_Type_Invariant =>
6925 T := Standard_Boolean;
6927 -- Here is the list of aspects that don't require delay analysis.
6929 when Aspect_Contract_Case |
6930 Aspect_Contract_Cases |
6932 Aspect_Dimension_System |
6933 Aspect_Implicit_Dereference |
6935 Aspect_Postcondition |
6937 Aspect_Precondition |
6939 raise Program_Error;
6943 -- Do the preanalyze call
6945 Preanalyze_Spec_Expression (Expression (ASN), T);
6946 end Check_Aspect_At_Freeze_Point;
6948 -----------------------------------
6949 -- Check_Constant_Address_Clause --
6950 -----------------------------------
6952 procedure Check_Constant_Address_Clause
6956 procedure Check_At_Constant_Address (Nod : Node_Id);
6957 -- Checks that the given node N represents a name whose 'Address is
6958 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
6959 -- address value is the same at the point of declaration of U_Ent and at
6960 -- the time of elaboration of the address clause.
6962 procedure Check_Expr_Constants (Nod : Node_Id);
6963 -- Checks that Nod meets the requirements for a constant address clause
6964 -- in the sense of the enclosing procedure.
6966 procedure Check_List_Constants (Lst : List_Id);
6967 -- Check that all elements of list Lst meet the requirements for a
6968 -- constant address clause in the sense of the enclosing procedure.
6970 -------------------------------
6971 -- Check_At_Constant_Address --
6972 -------------------------------
6974 procedure Check_At_Constant_Address (Nod : Node_Id) is
6976 if Is_Entity_Name (Nod) then
6977 if Present (Address_Clause (Entity ((Nod)))) then
6979 ("invalid address clause for initialized object &!",
6982 ("address for& cannot" &
6983 " depend on another address clause! (RM 13.1(22))!",
6986 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
6987 and then Sloc (U_Ent) < Sloc (Entity (Nod))
6990 ("invalid address clause for initialized object &!",
6992 Error_Msg_Node_2 := U_Ent;
6994 ("\& must be defined before & (RM 13.1(22))!",
6998 elsif Nkind (Nod) = N_Selected_Component then
7000 T : constant Entity_Id := Etype (Prefix (Nod));
7003 if (Is_Record_Type (T)
7004 and then Has_Discriminants (T))
7007 and then Is_Record_Type (Designated_Type (T))
7008 and then Has_Discriminants (Designated_Type (T)))
7011 ("invalid address clause for initialized object &!",
7014 ("\address cannot depend on component" &
7015 " of discriminated record (RM 13.1(22))!",
7018 Check_At_Constant_Address (Prefix (Nod));
7022 elsif Nkind (Nod) = N_Indexed_Component then
7023 Check_At_Constant_Address (Prefix (Nod));
7024 Check_List_Constants (Expressions (Nod));
7027 Check_Expr_Constants (Nod);
7029 end Check_At_Constant_Address;
7031 --------------------------
7032 -- Check_Expr_Constants --
7033 --------------------------
7035 procedure Check_Expr_Constants (Nod : Node_Id) is
7036 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
7037 Ent : Entity_Id := Empty;
7040 if Nkind (Nod) in N_Has_Etype
7041 and then Etype (Nod) = Any_Type
7047 when N_Empty | N_Error =>
7050 when N_Identifier | N_Expanded_Name =>
7051 Ent := Entity (Nod);
7053 -- We need to look at the original node if it is different
7054 -- from the node, since we may have rewritten things and
7055 -- substituted an identifier representing the rewrite.
7057 if Original_Node (Nod) /= Nod then
7058 Check_Expr_Constants (Original_Node (Nod));
7060 -- If the node is an object declaration without initial
7061 -- value, some code has been expanded, and the expression
7062 -- is not constant, even if the constituents might be
7063 -- acceptable, as in A'Address + offset.
7065 if Ekind (Ent) = E_Variable
7067 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
7069 No (Expression (Declaration_Node (Ent)))
7072 ("invalid address clause for initialized object &!",
7075 -- If entity is constant, it may be the result of expanding
7076 -- a check. We must verify that its declaration appears
7077 -- before the object in question, else we also reject the
7080 elsif Ekind (Ent) = E_Constant
7081 and then In_Same_Source_Unit (Ent, U_Ent)
7082 and then Sloc (Ent) > Loc_U_Ent
7085 ("invalid address clause for initialized object &!",
7092 -- Otherwise look at the identifier and see if it is OK
7094 if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
7095 or else Is_Type (Ent)
7100 Ekind (Ent) = E_Constant
7102 Ekind (Ent) = E_In_Parameter
7104 -- This is the case where we must have Ent defined before
7105 -- U_Ent. Clearly if they are in different units this
7106 -- requirement is met since the unit containing Ent is
7107 -- already processed.
7109 if not In_Same_Source_Unit (Ent, U_Ent) then
7112 -- Otherwise location of Ent must be before the location
7113 -- of U_Ent, that's what prior defined means.
7115 elsif Sloc (Ent) < Loc_U_Ent then
7120 ("invalid address clause for initialized object &!",
7122 Error_Msg_Node_2 := U_Ent;
7124 ("\& must be defined before & (RM 13.1(22))!",
7128 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
7129 Check_Expr_Constants (Original_Node (Nod));
7133 ("invalid address clause for initialized object &!",
7136 if Comes_From_Source (Ent) then
7138 ("\reference to variable& not allowed"
7139 & " (RM 13.1(22))!", Nod, Ent);
7142 ("non-static expression not allowed"
7143 & " (RM 13.1(22))!", Nod);
7147 when N_Integer_Literal =>
7149 -- If this is a rewritten unchecked conversion, in a system
7150 -- where Address is an integer type, always use the base type
7151 -- for a literal value. This is user-friendly and prevents
7152 -- order-of-elaboration issues with instances of unchecked
7155 if Nkind (Original_Node (Nod)) = N_Function_Call then
7156 Set_Etype (Nod, Base_Type (Etype (Nod)));
7159 when N_Real_Literal |
7161 N_Character_Literal =>
7165 Check_Expr_Constants (Low_Bound (Nod));
7166 Check_Expr_Constants (High_Bound (Nod));
7168 when N_Explicit_Dereference =>
7169 Check_Expr_Constants (Prefix (Nod));
7171 when N_Indexed_Component =>
7172 Check_Expr_Constants (Prefix (Nod));
7173 Check_List_Constants (Expressions (Nod));
7176 Check_Expr_Constants (Prefix (Nod));
7177 Check_Expr_Constants (Discrete_Range (Nod));
7179 when N_Selected_Component =>
7180 Check_Expr_Constants (Prefix (Nod));
7182 when N_Attribute_Reference =>
7183 if Attribute_Name (Nod) = Name_Address
7185 Attribute_Name (Nod) = Name_Access
7187 Attribute_Name (Nod) = Name_Unchecked_Access
7189 Attribute_Name (Nod) = Name_Unrestricted_Access
7191 Check_At_Constant_Address (Prefix (Nod));
7194 Check_Expr_Constants (Prefix (Nod));
7195 Check_List_Constants (Expressions (Nod));
7199 Check_List_Constants (Component_Associations (Nod));
7200 Check_List_Constants (Expressions (Nod));
7202 when N_Component_Association =>
7203 Check_Expr_Constants (Expression (Nod));
7205 when N_Extension_Aggregate =>
7206 Check_Expr_Constants (Ancestor_Part (Nod));
7207 Check_List_Constants (Component_Associations (Nod));
7208 Check_List_Constants (Expressions (Nod));
7213 when N_Binary_Op | N_Short_Circuit | N_Membership_Test =>
7214 Check_Expr_Constants (Left_Opnd (Nod));
7215 Check_Expr_Constants (Right_Opnd (Nod));
7218 Check_Expr_Constants (Right_Opnd (Nod));
7220 when N_Type_Conversion |
7221 N_Qualified_Expression |
7223 Check_Expr_Constants (Expression (Nod));
7225 when N_Unchecked_Type_Conversion =>
7226 Check_Expr_Constants (Expression (Nod));
7228 -- If this is a rewritten unchecked conversion, subtypes in
7229 -- this node are those created within the instance. To avoid
7230 -- order of elaboration issues, replace them with their base
7231 -- types. Note that address clauses can cause order of
7232 -- elaboration problems because they are elaborated by the
7233 -- back-end at the point of definition, and may mention
7234 -- entities declared in between (as long as everything is
7235 -- static). It is user-friendly to allow unchecked conversions
7238 if Nkind (Original_Node (Nod)) = N_Function_Call then
7239 Set_Etype (Expression (Nod),
7240 Base_Type (Etype (Expression (Nod))));
7241 Set_Etype (Nod, Base_Type (Etype (Nod)));
7244 when N_Function_Call =>
7245 if not Is_Pure (Entity (Name (Nod))) then
7247 ("invalid address clause for initialized object &!",
7251 ("\function & is not pure (RM 13.1(22))!",
7252 Nod, Entity (Name (Nod)));
7255 Check_List_Constants (Parameter_Associations (Nod));
7258 when N_Parameter_Association =>
7259 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
7263 ("invalid address clause for initialized object &!",
7266 ("\must be constant defined before& (RM 13.1(22))!",
7269 end Check_Expr_Constants;
7271 --------------------------
7272 -- Check_List_Constants --
7273 --------------------------
7275 procedure Check_List_Constants (Lst : List_Id) is
7279 if Present (Lst) then
7280 Nod1 := First (Lst);
7281 while Present (Nod1) loop
7282 Check_Expr_Constants (Nod1);
7286 end Check_List_Constants;
7288 -- Start of processing for Check_Constant_Address_Clause
7291 -- If rep_clauses are to be ignored, no need for legality checks. In
7292 -- particular, no need to pester user about rep clauses that violate
7293 -- the rule on constant addresses, given that these clauses will be
7294 -- removed by Freeze before they reach the back end.
7296 if not Ignore_Rep_Clauses then
7297 Check_Expr_Constants (Expr);
7299 end Check_Constant_Address_Clause;
7301 ----------------------------------------
7302 -- Check_Record_Representation_Clause --
7303 ----------------------------------------
7305 procedure Check_Record_Representation_Clause (N : Node_Id) is
7306 Loc : constant Source_Ptr := Sloc (N);
7307 Ident : constant Node_Id := Identifier (N);
7308 Rectype : Entity_Id;
7313 Hbit : Uint := Uint_0;
7317 Max_Bit_So_Far : Uint;
7318 -- Records the maximum bit position so far. If all field positions
7319 -- are monotonically increasing, then we can skip the circuit for
7320 -- checking for overlap, since no overlap is possible.
7322 Tagged_Parent : Entity_Id := Empty;
7323 -- This is set in the case of a derived tagged type for which we have
7324 -- Is_Fully_Repped_Tagged_Type True (indicating that all components are
7325 -- positioned by record representation clauses). In this case we must
7326 -- check for overlap between components of this tagged type, and the
7327 -- components of its parent. Tagged_Parent will point to this parent
7328 -- type. For all other cases Tagged_Parent is left set to Empty.
7330 Parent_Last_Bit : Uint;
7331 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
7332 -- last bit position for any field in the parent type. We only need to
7333 -- check overlap for fields starting below this point.
7335 Overlap_Check_Required : Boolean;
7336 -- Used to keep track of whether or not an overlap check is required
7338 Overlap_Detected : Boolean := False;
7339 -- Set True if an overlap is detected
7341 Ccount : Natural := 0;
7342 -- Number of component clauses in record rep clause
7344 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
7345 -- Given two entities for record components or discriminants, checks
7346 -- if they have overlapping component clauses and issues errors if so.
7348 procedure Find_Component;
7349 -- Finds component entity corresponding to current component clause (in
7350 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
7351 -- start/stop bits for the field. If there is no matching component or
7352 -- if the matching component does not have a component clause, then
7353 -- that's an error and Comp is set to Empty, but no error message is
7354 -- issued, since the message was already given. Comp is also set to
7355 -- Empty if the current "component clause" is in fact a pragma.
7357 -----------------------------
7358 -- Check_Component_Overlap --
7359 -----------------------------
7361 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
7362 CC1 : constant Node_Id := Component_Clause (C1_Ent);
7363 CC2 : constant Node_Id := Component_Clause (C2_Ent);
7366 if Present (CC1) and then Present (CC2) then
7368 -- Exclude odd case where we have two tag fields in the same
7369 -- record, both at location zero. This seems a bit strange, but
7370 -- it seems to happen in some circumstances, perhaps on an error.
7372 if Chars (C1_Ent) = Name_uTag
7374 Chars (C2_Ent) = Name_uTag
7379 -- Here we check if the two fields overlap
7382 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
7383 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
7384 E1 : constant Uint := S1 + Esize (C1_Ent);
7385 E2 : constant Uint := S2 + Esize (C2_Ent);
7388 if E2 <= S1 or else E1 <= S2 then
7391 Error_Msg_Node_2 := Component_Name (CC2);
7392 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
7393 Error_Msg_Node_1 := Component_Name (CC1);
7395 ("component& overlaps & #", Component_Name (CC1));
7396 Overlap_Detected := True;
7400 end Check_Component_Overlap;
7402 --------------------
7403 -- Find_Component --
7404 --------------------
7406 procedure Find_Component is
7408 procedure Search_Component (R : Entity_Id);
7409 -- Search components of R for a match. If found, Comp is set.
7411 ----------------------
7412 -- Search_Component --
7413 ----------------------
7415 procedure Search_Component (R : Entity_Id) is
7417 Comp := First_Component_Or_Discriminant (R);
7418 while Present (Comp) loop
7420 -- Ignore error of attribute name for component name (we
7421 -- already gave an error message for this, so no need to
7424 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
7427 exit when Chars (Comp) = Chars (Component_Name (CC));
7430 Next_Component_Or_Discriminant (Comp);
7432 end Search_Component;
7434 -- Start of processing for Find_Component
7437 -- Return with Comp set to Empty if we have a pragma
7439 if Nkind (CC) = N_Pragma then
7444 -- Search current record for matching component
7446 Search_Component (Rectype);
7448 -- If not found, maybe component of base type that is absent from
7449 -- statically constrained first subtype.
7452 Search_Component (Base_Type (Rectype));
7455 -- If no component, or the component does not reference the component
7456 -- clause in question, then there was some previous error for which
7457 -- we already gave a message, so just return with Comp Empty.
7459 if No (Comp) or else Component_Clause (Comp) /= CC then
7460 Check_Error_Detected;
7463 -- Normal case where we have a component clause
7466 Fbit := Component_Bit_Offset (Comp);
7467 Lbit := Fbit + Esize (Comp) - 1;
7471 -- Start of processing for Check_Record_Representation_Clause
7475 Rectype := Entity (Ident);
7477 if Rectype = Any_Type then
7480 Rectype := Underlying_Type (Rectype);
7483 -- See if we have a fully repped derived tagged type
7486 PS : constant Entity_Id := Parent_Subtype (Rectype);
7489 if Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
7490 Tagged_Parent := PS;
7492 -- Find maximum bit of any component of the parent type
7494 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
7495 Pcomp := First_Entity (Tagged_Parent);
7496 while Present (Pcomp) loop
7497 if Ekind_In (Pcomp, E_Discriminant, E_Component) then
7498 if Component_Bit_Offset (Pcomp) /= No_Uint
7499 and then Known_Static_Esize (Pcomp)
7504 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
7507 Next_Entity (Pcomp);
7513 -- All done if no component clauses
7515 CC := First (Component_Clauses (N));
7521 -- If a tag is present, then create a component clause that places it
7522 -- at the start of the record (otherwise gigi may place it after other
7523 -- fields that have rep clauses).
7525 Fent := First_Entity (Rectype);
7527 if Nkind (Fent) = N_Defining_Identifier
7528 and then Chars (Fent) = Name_uTag
7530 Set_Component_Bit_Offset (Fent, Uint_0);
7531 Set_Normalized_Position (Fent, Uint_0);
7532 Set_Normalized_First_Bit (Fent, Uint_0);
7533 Set_Normalized_Position_Max (Fent, Uint_0);
7534 Init_Esize (Fent, System_Address_Size);
7536 Set_Component_Clause (Fent,
7537 Make_Component_Clause (Loc,
7538 Component_Name => Make_Identifier (Loc, Name_uTag),
7540 Position => Make_Integer_Literal (Loc, Uint_0),
7541 First_Bit => Make_Integer_Literal (Loc, Uint_0),
7543 Make_Integer_Literal (Loc,
7544 UI_From_Int (System_Address_Size))));
7546 Ccount := Ccount + 1;
7549 Max_Bit_So_Far := Uint_Minus_1;
7550 Overlap_Check_Required := False;
7552 -- Process the component clauses
7554 while Present (CC) loop
7557 if Present (Comp) then
7558 Ccount := Ccount + 1;
7560 -- We need a full overlap check if record positions non-monotonic
7562 if Fbit <= Max_Bit_So_Far then
7563 Overlap_Check_Required := True;
7566 Max_Bit_So_Far := Lbit;
7568 -- Check bit position out of range of specified size
7570 if Has_Size_Clause (Rectype)
7571 and then RM_Size (Rectype) <= Lbit
7574 ("bit number out of range of specified size",
7577 -- Check for overlap with tag field
7580 if Is_Tagged_Type (Rectype)
7581 and then Fbit < System_Address_Size
7584 ("component overlaps tag field of&",
7585 Component_Name (CC), Rectype);
7586 Overlap_Detected := True;
7594 -- Check parent overlap if component might overlap parent field
7596 if Present (Tagged_Parent) and then Fbit <= Parent_Last_Bit then
7597 Pcomp := First_Component_Or_Discriminant (Tagged_Parent);
7598 while Present (Pcomp) loop
7599 if not Is_Tag (Pcomp)
7600 and then Chars (Pcomp) /= Name_uParent
7602 Check_Component_Overlap (Comp, Pcomp);
7605 Next_Component_Or_Discriminant (Pcomp);
7613 -- Now that we have processed all the component clauses, check for
7614 -- overlap. We have to leave this till last, since the components can
7615 -- appear in any arbitrary order in the representation clause.
7617 -- We do not need this check if all specified ranges were monotonic,
7618 -- as recorded by Overlap_Check_Required being False at this stage.
7620 -- This first section checks if there are any overlapping entries at
7621 -- all. It does this by sorting all entries and then seeing if there are
7622 -- any overlaps. If there are none, then that is decisive, but if there
7623 -- are overlaps, they may still be OK (they may result from fields in
7624 -- different variants).
7626 if Overlap_Check_Required then
7627 Overlap_Check1 : declare
7629 OC_Fbit : array (0 .. Ccount) of Uint;
7630 -- First-bit values for component clauses, the value is the offset
7631 -- of the first bit of the field from start of record. The zero
7632 -- entry is for use in sorting.
7634 OC_Lbit : array (0 .. Ccount) of Uint;
7635 -- Last-bit values for component clauses, the value is the offset
7636 -- of the last bit of the field from start of record. The zero
7637 -- entry is for use in sorting.
7639 OC_Count : Natural := 0;
7640 -- Count of entries in OC_Fbit and OC_Lbit
7642 function OC_Lt (Op1, Op2 : Natural) return Boolean;
7643 -- Compare routine for Sort
7645 procedure OC_Move (From : Natural; To : Natural);
7646 -- Move routine for Sort
7648 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
7654 function OC_Lt (Op1, Op2 : Natural) return Boolean is
7656 return OC_Fbit (Op1) < OC_Fbit (Op2);
7663 procedure OC_Move (From : Natural; To : Natural) is
7665 OC_Fbit (To) := OC_Fbit (From);
7666 OC_Lbit (To) := OC_Lbit (From);
7669 -- Start of processing for Overlap_Check
7672 CC := First (Component_Clauses (N));
7673 while Present (CC) loop
7675 -- Exclude component clause already marked in error
7677 if not Error_Posted (CC) then
7680 if Present (Comp) then
7681 OC_Count := OC_Count + 1;
7682 OC_Fbit (OC_Count) := Fbit;
7683 OC_Lbit (OC_Count) := Lbit;
7690 Sorting.Sort (OC_Count);
7692 Overlap_Check_Required := False;
7693 for J in 1 .. OC_Count - 1 loop
7694 if OC_Lbit (J) >= OC_Fbit (J + 1) then
7695 Overlap_Check_Required := True;
7702 -- If Overlap_Check_Required is still True, then we have to do the full
7703 -- scale overlap check, since we have at least two fields that do
7704 -- overlap, and we need to know if that is OK since they are in
7705 -- different variant, or whether we have a definite problem.
7707 if Overlap_Check_Required then
7708 Overlap_Check2 : declare
7709 C1_Ent, C2_Ent : Entity_Id;
7710 -- Entities of components being checked for overlap
7713 -- Component_List node whose Component_Items are being checked
7716 -- Component declaration for component being checked
7719 C1_Ent := First_Entity (Base_Type (Rectype));
7721 -- Loop through all components in record. For each component check
7722 -- for overlap with any of the preceding elements on the component
7723 -- list containing the component and also, if the component is in
7724 -- a variant, check against components outside the case structure.
7725 -- This latter test is repeated recursively up the variant tree.
7727 Main_Component_Loop : while Present (C1_Ent) loop
7728 if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
7729 goto Continue_Main_Component_Loop;
7732 -- Skip overlap check if entity has no declaration node. This
7733 -- happens with discriminants in constrained derived types.
7734 -- Possibly we are missing some checks as a result, but that
7735 -- does not seem terribly serious.
7737 if No (Declaration_Node (C1_Ent)) then
7738 goto Continue_Main_Component_Loop;
7741 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
7743 -- Loop through component lists that need checking. Check the
7744 -- current component list and all lists in variants above us.
7746 Component_List_Loop : loop
7748 -- If derived type definition, go to full declaration
7749 -- If at outer level, check discriminants if there are any.
7751 if Nkind (Clist) = N_Derived_Type_Definition then
7752 Clist := Parent (Clist);
7755 -- Outer level of record definition, check discriminants
7757 if Nkind_In (Clist, N_Full_Type_Declaration,
7758 N_Private_Type_Declaration)
7760 if Has_Discriminants (Defining_Identifier (Clist)) then
7762 First_Discriminant (Defining_Identifier (Clist));
7763 while Present (C2_Ent) loop
7764 exit when C1_Ent = C2_Ent;
7765 Check_Component_Overlap (C1_Ent, C2_Ent);
7766 Next_Discriminant (C2_Ent);
7770 -- Record extension case
7772 elsif Nkind (Clist) = N_Derived_Type_Definition then
7775 -- Otherwise check one component list
7778 Citem := First (Component_Items (Clist));
7779 while Present (Citem) loop
7780 if Nkind (Citem) = N_Component_Declaration then
7781 C2_Ent := Defining_Identifier (Citem);
7782 exit when C1_Ent = C2_Ent;
7783 Check_Component_Overlap (C1_Ent, C2_Ent);
7790 -- Check for variants above us (the parent of the Clist can
7791 -- be a variant, in which case its parent is a variant part,
7792 -- and the parent of the variant part is a component list
7793 -- whose components must all be checked against the current
7794 -- component for overlap).
7796 if Nkind (Parent (Clist)) = N_Variant then
7797 Clist := Parent (Parent (Parent (Clist)));
7799 -- Check for possible discriminant part in record, this
7800 -- is treated essentially as another level in the
7801 -- recursion. For this case the parent of the component
7802 -- list is the record definition, and its parent is the
7803 -- full type declaration containing the discriminant
7806 elsif Nkind (Parent (Clist)) = N_Record_Definition then
7807 Clist := Parent (Parent ((Clist)));
7809 -- If neither of these two cases, we are at the top of
7813 exit Component_List_Loop;
7815 end loop Component_List_Loop;
7817 <<Continue_Main_Component_Loop>>
7818 Next_Entity (C1_Ent);
7820 end loop Main_Component_Loop;
7824 -- The following circuit deals with warning on record holes (gaps). We
7825 -- skip this check if overlap was detected, since it makes sense for the
7826 -- programmer to fix this illegality before worrying about warnings.
7828 if not Overlap_Detected and Warn_On_Record_Holes then
7829 Record_Hole_Check : declare
7830 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
7831 -- Full declaration of record type
7833 procedure Check_Component_List
7837 -- Check component list CL for holes. The starting bit should be
7838 -- Sbit. which is zero for the main record component list and set
7839 -- appropriately for recursive calls for variants. DS is set to
7840 -- a list of discriminant specifications to be included in the
7841 -- consideration of components. It is No_List if none to consider.
7843 --------------------------
7844 -- Check_Component_List --
7845 --------------------------
7847 procedure Check_Component_List
7855 Compl := Integer (List_Length (Component_Items (CL)));
7857 if DS /= No_List then
7858 Compl := Compl + Integer (List_Length (DS));
7862 Comps : array (Natural range 0 .. Compl) of Entity_Id;
7863 -- Gather components (zero entry is for sort routine)
7865 Ncomps : Natural := 0;
7866 -- Number of entries stored in Comps (starting at Comps (1))
7869 -- One component item or discriminant specification
7872 -- Starting bit for next component
7880 function Lt (Op1, Op2 : Natural) return Boolean;
7881 -- Compare routine for Sort
7883 procedure Move (From : Natural; To : Natural);
7884 -- Move routine for Sort
7886 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
7892 function Lt (Op1, Op2 : Natural) return Boolean is
7894 return Component_Bit_Offset (Comps (Op1))
7896 Component_Bit_Offset (Comps (Op2));
7903 procedure Move (From : Natural; To : Natural) is
7905 Comps (To) := Comps (From);
7909 -- Gather discriminants into Comp
7911 if DS /= No_List then
7912 Citem := First (DS);
7913 while Present (Citem) loop
7914 if Nkind (Citem) = N_Discriminant_Specification then
7916 Ent : constant Entity_Id :=
7917 Defining_Identifier (Citem);
7919 if Ekind (Ent) = E_Discriminant then
7920 Ncomps := Ncomps + 1;
7921 Comps (Ncomps) := Ent;
7930 -- Gather component entities into Comp
7932 Citem := First (Component_Items (CL));
7933 while Present (Citem) loop
7934 if Nkind (Citem) = N_Component_Declaration then
7935 Ncomps := Ncomps + 1;
7936 Comps (Ncomps) := Defining_Identifier (Citem);
7942 -- Now sort the component entities based on the first bit.
7943 -- Note we already know there are no overlapping components.
7945 Sorting.Sort (Ncomps);
7947 -- Loop through entries checking for holes
7950 for J in 1 .. Ncomps loop
7952 Error_Msg_Uint_1 := Component_Bit_Offset (CEnt) - Nbit;
7954 if Error_Msg_Uint_1 > 0 then
7956 ("?^-bit gap before component&",
7957 Component_Name (Component_Clause (CEnt)), CEnt);
7960 Nbit := Component_Bit_Offset (CEnt) + Esize (CEnt);
7963 -- Process variant parts recursively if present
7965 if Present (Variant_Part (CL)) then
7966 Variant := First (Variants (Variant_Part (CL)));
7967 while Present (Variant) loop
7968 Check_Component_List
7969 (Component_List (Variant), Nbit, No_List);
7974 end Check_Component_List;
7976 -- Start of processing for Record_Hole_Check
7983 if Is_Tagged_Type (Rectype) then
7984 Sbit := UI_From_Int (System_Address_Size);
7989 if Nkind (Decl) = N_Full_Type_Declaration
7990 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
7992 Check_Component_List
7993 (Component_List (Type_Definition (Decl)),
7995 Discriminant_Specifications (Decl));
7998 end Record_Hole_Check;
8001 -- For records that have component clauses for all components, and whose
8002 -- size is less than or equal to 32, we need to know the size in the
8003 -- front end to activate possible packed array processing where the
8004 -- component type is a record.
8006 -- At this stage Hbit + 1 represents the first unused bit from all the
8007 -- component clauses processed, so if the component clauses are
8008 -- complete, then this is the length of the record.
8010 -- For records longer than System.Storage_Unit, and for those where not
8011 -- all components have component clauses, the back end determines the
8012 -- length (it may for example be appropriate to round up the size
8013 -- to some convenient boundary, based on alignment considerations, etc).
8015 if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then
8017 -- Nothing to do if at least one component has no component clause
8019 Comp := First_Component_Or_Discriminant (Rectype);
8020 while Present (Comp) loop
8021 exit when No (Component_Clause (Comp));
8022 Next_Component_Or_Discriminant (Comp);
8025 -- If we fall out of loop, all components have component clauses
8026 -- and so we can set the size to the maximum value.
8029 Set_RM_Size (Rectype, Hbit + 1);
8032 end Check_Record_Representation_Clause;
8038 procedure Check_Size
8042 Biased : out Boolean)
8044 UT : constant Entity_Id := Underlying_Type (T);
8050 -- Reject patently improper size values.
8052 if Is_Elementary_Type (T)
8053 and then Siz > UI_From_Int (Int'Last)
8055 Error_Msg_N ("Size value too large for elementary type", N);
8057 if Nkind (Original_Node (N)) = N_Op_Expon then
8059 ("\maybe '* was meant, rather than '*'*", Original_Node (N));
8063 -- Dismiss generic types
8065 if Is_Generic_Type (T)
8067 Is_Generic_Type (UT)
8069 Is_Generic_Type (Root_Type (UT))
8073 -- Guard against previous errors
8075 elsif No (UT) or else UT = Any_Type then
8076 Check_Error_Detected;
8079 -- Check case of bit packed array
8081 elsif Is_Array_Type (UT)
8082 and then Known_Static_Component_Size (UT)
8083 and then Is_Bit_Packed_Array (UT)
8091 Asiz := Component_Size (UT);
8092 Indx := First_Index (UT);
8094 Ityp := Etype (Indx);
8096 -- If non-static bound, then we are not in the business of
8097 -- trying to check the length, and indeed an error will be
8098 -- issued elsewhere, since sizes of non-static array types
8099 -- cannot be set implicitly or explicitly.
8101 if not Is_Static_Subtype (Ityp) then
8105 -- Otherwise accumulate next dimension
8107 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
8108 Expr_Value (Type_Low_Bound (Ityp)) +
8112 exit when No (Indx);
8119 Error_Msg_Uint_1 := Asiz;
8121 ("size for& too small, minimum allowed is ^", N, T);
8122 Set_Esize (T, Asiz);
8123 Set_RM_Size (T, Asiz);
8127 -- All other composite types are ignored
8129 elsif Is_Composite_Type (UT) then
8132 -- For fixed-point types, don't check minimum if type is not frozen,
8133 -- since we don't know all the characteristics of the type that can
8134 -- affect the size (e.g. a specified small) till freeze time.
8136 elsif Is_Fixed_Point_Type (UT)
8137 and then not Is_Frozen (UT)
8141 -- Cases for which a minimum check is required
8144 -- Ignore if specified size is correct for the type
8146 if Known_Esize (UT) and then Siz = Esize (UT) then
8150 -- Otherwise get minimum size
8152 M := UI_From_Int (Minimum_Size (UT));
8156 -- Size is less than minimum size, but one possibility remains
8157 -- that we can manage with the new size if we bias the type.
8159 M := UI_From_Int (Minimum_Size (UT, Biased => True));
8162 Error_Msg_Uint_1 := M;
8164 ("size for& too small, minimum allowed is ^", N, T);
8174 -------------------------
8175 -- Get_Alignment_Value --
8176 -------------------------
8178 function Get_Alignment_Value (Expr : Node_Id) return Uint is
8179 Align : constant Uint := Static_Integer (Expr);
8182 if Align = No_Uint then
8185 elsif Align <= 0 then
8186 Error_Msg_N ("alignment value must be positive", Expr);
8190 for J in Int range 0 .. 64 loop
8192 M : constant Uint := Uint_2 ** J;
8195 exit when M = Align;
8199 ("alignment value must be power of 2", Expr);
8207 end Get_Alignment_Value;
8209 -------------------------------------
8210 -- Inherit_Aspects_At_Freeze_Point --
8211 -------------------------------------
8213 procedure Inherit_Aspects_At_Freeze_Point (Typ : Entity_Id) is
8214 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8215 (Rep_Item : Node_Id) return Boolean;
8216 -- This routine checks if Rep_Item is either a pragma or an aspect
8217 -- specification node whose correponding pragma (if any) is present in
8218 -- the Rep Item chain of the entity it has been specified to.
8220 --------------------------------------------------
8221 -- Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item --
8222 --------------------------------------------------
8224 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8225 (Rep_Item : Node_Id) return Boolean
8228 return Nkind (Rep_Item) = N_Pragma
8229 or else Present_In_Rep_Item
8230 (Entity (Rep_Item), Aspect_Rep_Item (Rep_Item));
8231 end Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item;
8233 -- Start of processing for Inherit_Aspects_At_Freeze_Point
8236 -- A representation item is either subtype-specific (Size and Alignment
8237 -- clauses) or type-related (all others). Subtype-specific aspects may
8238 -- differ for different subtypes of the same type (RM 13.1.8).
8240 -- A derived type inherits each type-related representation aspect of
8241 -- its parent type that was directly specified before the declaration of
8242 -- the derived type (RM 13.1.15).
8244 -- A derived subtype inherits each subtype-specific representation
8245 -- aspect of its parent subtype that was directly specified before the
8246 -- declaration of the derived type (RM 13.1.15).
8248 -- The general processing involves inheriting a representation aspect
8249 -- from a parent type whenever the first rep item (aspect specification,
8250 -- attribute definition clause, pragma) corresponding to the given
8251 -- representation aspect in the rep item chain of Typ, if any, isn't
8252 -- directly specified to Typ but to one of its parents.
8254 -- ??? Note that, for now, just a limited number of representation
8255 -- aspects have been inherited here so far. Many of them are
8256 -- still inherited in Sem_Ch3. This will be fixed soon. Here is
8257 -- a non- exhaustive list of aspects that likely also need to
8258 -- be moved to this routine: Alignment, Component_Alignment,
8259 -- Component_Size, Machine_Radix, Object_Size, Pack, Predicates,
8260 -- Preelaborable_Initialization, RM_Size and Small.
8262 if Nkind (Parent (Typ)) = N_Private_Extension_Declaration then
8268 if not Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005, False)
8269 and then Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005)
8270 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8271 (Get_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005))
8273 Set_Is_Ada_2005_Only (Typ);
8278 if not Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012, False)
8279 and then Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012)
8280 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8281 (Get_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012))
8283 Set_Is_Ada_2012_Only (Typ);
8288 if not Has_Rep_Item (Typ, Name_Atomic, Name_Shared, False)
8289 and then Has_Rep_Pragma (Typ, Name_Atomic, Name_Shared)
8290 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8291 (Get_Rep_Item (Typ, Name_Atomic, Name_Shared))
8293 Set_Is_Atomic (Typ);
8294 Set_Treat_As_Volatile (Typ);
8295 Set_Is_Volatile (Typ);
8298 -- Default_Component_Value
8300 if Is_Array_Type (Typ)
8301 and then Has_Rep_Item (Typ, Name_Default_Component_Value, False)
8302 and then Has_Rep_Item (Typ, Name_Default_Component_Value)
8304 Set_Default_Aspect_Component_Value (Typ,
8305 Default_Aspect_Component_Value
8306 (Entity (Get_Rep_Item (Typ, Name_Default_Component_Value))));
8311 if Is_Scalar_Type (Typ)
8312 and then Has_Rep_Item (Typ, Name_Default_Value, False)
8313 and then Has_Rep_Item (Typ, Name_Default_Value)
8315 Set_Default_Aspect_Value (Typ,
8316 Default_Aspect_Value
8317 (Entity (Get_Rep_Item (Typ, Name_Default_Value))));
8322 if not Has_Rep_Item (Typ, Name_Discard_Names, False)
8323 and then Has_Rep_Item (Typ, Name_Discard_Names)
8324 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8325 (Get_Rep_Item (Typ, Name_Discard_Names))
8327 Set_Discard_Names (Typ);
8332 if not Has_Rep_Item (Typ, Name_Invariant, False)
8333 and then Has_Rep_Item (Typ, Name_Invariant)
8334 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8335 (Get_Rep_Item (Typ, Name_Invariant))
8337 Set_Has_Invariants (Typ);
8339 if Class_Present (Get_Rep_Item (Typ, Name_Invariant)) then
8340 Set_Has_Inheritable_Invariants (Typ);
8346 if not Has_Rep_Item (Typ, Name_Volatile, False)
8347 and then Has_Rep_Item (Typ, Name_Volatile)
8348 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8349 (Get_Rep_Item (Typ, Name_Volatile))
8351 Set_Treat_As_Volatile (Typ);
8352 Set_Is_Volatile (Typ);
8355 -- Inheritance for derived types only
8357 if Is_Derived_Type (Typ) then
8359 Bas_Typ : constant Entity_Id := Base_Type (Typ);
8360 Imp_Bas_Typ : constant Entity_Id := Implementation_Base_Type (Typ);
8363 -- Atomic_Components
8365 if not Has_Rep_Item (Typ, Name_Atomic_Components, False)
8366 and then Has_Rep_Item (Typ, Name_Atomic_Components)
8367 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8368 (Get_Rep_Item (Typ, Name_Atomic_Components))
8370 Set_Has_Atomic_Components (Imp_Bas_Typ);
8373 -- Volatile_Components
8375 if not Has_Rep_Item (Typ, Name_Volatile_Components, False)
8376 and then Has_Rep_Item (Typ, Name_Volatile_Components)
8377 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8378 (Get_Rep_Item (Typ, Name_Volatile_Components))
8380 Set_Has_Volatile_Components (Imp_Bas_Typ);
8383 -- Finalize_Storage_Only.
8385 if not Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only, False)
8386 and then Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only)
8388 Set_Finalize_Storage_Only (Bas_Typ);
8391 -- Universal_Aliasing
8393 if not Has_Rep_Item (Typ, Name_Universal_Aliasing, False)
8394 and then Has_Rep_Item (Typ, Name_Universal_Aliasing)
8395 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
8396 (Get_Rep_Item (Typ, Name_Universal_Aliasing))
8398 Set_Universal_Aliasing (Imp_Bas_Typ);
8401 -- Record type specific aspects
8403 if Is_Record_Type (Typ) then
8407 if not Has_Rep_Item (Typ, Name_Bit_Order, False)
8408 and then Has_Rep_Item (Typ, Name_Bit_Order)
8410 Set_Reverse_Bit_Order (Bas_Typ,
8411 Reverse_Bit_Order (Entity (Name
8412 (Get_Rep_Item (Typ, Name_Bit_Order)))));
8415 -- Scalar_Storage_Order
8417 if not Has_Rep_Item (Typ, Name_Scalar_Storage_Order, False)
8418 and then Has_Rep_Item (Typ, Name_Scalar_Storage_Order)
8420 Set_Reverse_Storage_Order (Bas_Typ,
8421 Reverse_Storage_Order (Entity (Name
8422 (Get_Rep_Item (Typ, Name_Scalar_Storage_Order)))));
8427 end Inherit_Aspects_At_Freeze_Point;
8433 procedure Initialize is
8435 Address_Clause_Checks.Init;
8436 Independence_Checks.Init;
8437 Unchecked_Conversions.Init;
8440 -------------------------
8441 -- Is_Operational_Item --
8442 -------------------------
8444 function Is_Operational_Item (N : Node_Id) return Boolean is
8446 if Nkind (N) /= N_Attribute_Definition_Clause then
8451 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
8453 return Id = Attribute_Input
8454 or else Id = Attribute_Output
8455 or else Id = Attribute_Read
8456 or else Id = Attribute_Write
8457 or else Id = Attribute_External_Tag;
8460 end Is_Operational_Item;
8466 function Minimum_Size
8468 Biased : Boolean := False) return Nat
8470 Lo : Uint := No_Uint;
8471 Hi : Uint := No_Uint;
8472 LoR : Ureal := No_Ureal;
8473 HiR : Ureal := No_Ureal;
8474 LoSet : Boolean := False;
8475 HiSet : Boolean := False;
8479 R_Typ : constant Entity_Id := Root_Type (T);
8482 -- If bad type, return 0
8484 if T = Any_Type then
8487 -- For generic types, just return zero. There cannot be any legitimate
8488 -- need to know such a size, but this routine may be called with a
8489 -- generic type as part of normal processing.
8491 elsif Is_Generic_Type (R_Typ)
8492 or else R_Typ = Any_Type
8496 -- Access types. Normally an access type cannot have a size smaller
8497 -- than the size of System.Address. The exception is on VMS, where
8498 -- we have short and long addresses, and it is possible for an access
8499 -- type to have a short address size (and thus be less than the size
8500 -- of System.Address itself). We simply skip the check for VMS, and
8501 -- leave it to the back end to do the check.
8503 elsif Is_Access_Type (T) then
8504 if OpenVMS_On_Target then
8507 return System_Address_Size;
8510 -- Floating-point types
8512 elsif Is_Floating_Point_Type (T) then
8513 return UI_To_Int (Esize (R_Typ));
8517 elsif Is_Discrete_Type (T) then
8519 -- The following loop is looking for the nearest compile time known
8520 -- bounds following the ancestor subtype chain. The idea is to find
8521 -- the most restrictive known bounds information.
8525 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
8530 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
8531 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
8538 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
8539 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
8545 Ancest := Ancestor_Subtype (Ancest);
8548 Ancest := Base_Type (T);
8550 if Is_Generic_Type (Ancest) then
8556 -- Fixed-point types. We can't simply use Expr_Value to get the
8557 -- Corresponding_Integer_Value values of the bounds, since these do not
8558 -- get set till the type is frozen, and this routine can be called
8559 -- before the type is frozen. Similarly the test for bounds being static
8560 -- needs to include the case where we have unanalyzed real literals for
8563 elsif Is_Fixed_Point_Type (T) then
8565 -- The following loop is looking for the nearest compile time known
8566 -- bounds following the ancestor subtype chain. The idea is to find
8567 -- the most restrictive known bounds information.
8571 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
8575 -- Note: In the following two tests for LoSet and HiSet, it may
8576 -- seem redundant to test for N_Real_Literal here since normally
8577 -- one would assume that the test for the value being known at
8578 -- compile time includes this case. However, there is a glitch.
8579 -- If the real literal comes from folding a non-static expression,
8580 -- then we don't consider any non- static expression to be known
8581 -- at compile time if we are in configurable run time mode (needed
8582 -- in some cases to give a clearer definition of what is and what
8583 -- is not accepted). So the test is indeed needed. Without it, we
8584 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
8587 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
8588 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
8590 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
8597 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
8598 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
8600 HiR := Expr_Value_R (Type_High_Bound (Ancest));
8606 Ancest := Ancestor_Subtype (Ancest);
8609 Ancest := Base_Type (T);
8611 if Is_Generic_Type (Ancest) then
8617 Lo := UR_To_Uint (LoR / Small_Value (T));
8618 Hi := UR_To_Uint (HiR / Small_Value (T));
8620 -- No other types allowed
8623 raise Program_Error;
8626 -- Fall through with Hi and Lo set. Deal with biased case
8629 and then not Is_Fixed_Point_Type (T)
8630 and then not (Is_Enumeration_Type (T)
8631 and then Has_Non_Standard_Rep (T)))
8632 or else Has_Biased_Representation (T)
8638 -- Signed case. Note that we consider types like range 1 .. -1 to be
8639 -- signed for the purpose of computing the size, since the bounds have
8640 -- to be accommodated in the base type.
8642 if Lo < 0 or else Hi < 0 then
8646 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
8647 -- Note that we accommodate the case where the bounds cross. This
8648 -- can happen either because of the way the bounds are declared
8649 -- or because of the algorithm in Freeze_Fixed_Point_Type.
8663 -- If both bounds are positive, make sure that both are represen-
8664 -- table in the case where the bounds are crossed. This can happen
8665 -- either because of the way the bounds are declared, or because of
8666 -- the algorithm in Freeze_Fixed_Point_Type.
8672 -- S = size, (can accommodate 0 .. (2**size - 1))
8675 while Hi >= Uint_2 ** S loop
8683 ---------------------------
8684 -- New_Stream_Subprogram --
8685 ---------------------------
8687 procedure New_Stream_Subprogram
8691 Nam : TSS_Name_Type)
8693 Loc : constant Source_Ptr := Sloc (N);
8694 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
8695 Subp_Id : Entity_Id;
8696 Subp_Decl : Node_Id;
8700 Defer_Declaration : constant Boolean :=
8701 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
8702 -- For a tagged type, there is a declaration for each stream attribute
8703 -- at the freeze point, and we must generate only a completion of this
8704 -- declaration. We do the same for private types, because the full view
8705 -- might be tagged. Otherwise we generate a declaration at the point of
8706 -- the attribute definition clause.
8708 function Build_Spec return Node_Id;
8709 -- Used for declaration and renaming declaration, so that this is
8710 -- treated as a renaming_as_body.
8716 function Build_Spec return Node_Id is
8717 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
8720 T_Ref : constant Node_Id := New_Reference_To (Etyp, Loc);
8723 Subp_Id := Make_Defining_Identifier (Loc, Sname);
8725 -- S : access Root_Stream_Type'Class
8727 Formals := New_List (
8728 Make_Parameter_Specification (Loc,
8729 Defining_Identifier =>
8730 Make_Defining_Identifier (Loc, Name_S),
8732 Make_Access_Definition (Loc,
8735 Designated_Type (Etype (F)), Loc))));
8737 if Nam = TSS_Stream_Input then
8738 Spec := Make_Function_Specification (Loc,
8739 Defining_Unit_Name => Subp_Id,
8740 Parameter_Specifications => Formals,
8741 Result_Definition => T_Ref);
8746 Make_Parameter_Specification (Loc,
8747 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
8748 Out_Present => Out_P,
8749 Parameter_Type => T_Ref));
8752 Make_Procedure_Specification (Loc,
8753 Defining_Unit_Name => Subp_Id,
8754 Parameter_Specifications => Formals);
8760 -- Start of processing for New_Stream_Subprogram
8763 F := First_Formal (Subp);
8765 if Ekind (Subp) = E_Procedure then
8766 Etyp := Etype (Next_Formal (F));
8768 Etyp := Etype (Subp);
8771 -- Prepare subprogram declaration and insert it as an action on the
8772 -- clause node. The visibility for this entity is used to test for
8773 -- visibility of the attribute definition clause (in the sense of
8774 -- 8.3(23) as amended by AI-195).
8776 if not Defer_Declaration then
8778 Make_Subprogram_Declaration (Loc,
8779 Specification => Build_Spec);
8781 -- For a tagged type, there is always a visible declaration for each
8782 -- stream TSS (it is a predefined primitive operation), and the
8783 -- completion of this declaration occurs at the freeze point, which is
8784 -- not always visible at places where the attribute definition clause is
8785 -- visible. So, we create a dummy entity here for the purpose of
8786 -- tracking the visibility of the attribute definition clause itself.
8790 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
8792 Make_Object_Declaration (Loc,
8793 Defining_Identifier => Subp_Id,
8794 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
8797 Insert_Action (N, Subp_Decl);
8798 Set_Entity (N, Subp_Id);
8801 Make_Subprogram_Renaming_Declaration (Loc,
8802 Specification => Build_Spec,
8803 Name => New_Reference_To (Subp, Loc));
8805 if Defer_Declaration then
8806 Set_TSS (Base_Type (Ent), Subp_Id);
8808 Insert_Action (N, Subp_Decl);
8809 Copy_TSS (Subp_Id, Base_Type (Ent));
8811 end New_Stream_Subprogram;
8813 ------------------------
8814 -- Rep_Item_Too_Early --
8815 ------------------------
8817 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
8819 -- Cannot apply non-operational rep items to generic types
8821 if Is_Operational_Item (N) then
8825 and then Is_Generic_Type (Root_Type (T))
8827 Error_Msg_N ("representation item not allowed for generic type", N);
8831 -- Otherwise check for incomplete type
8833 if Is_Incomplete_Or_Private_Type (T)
8834 and then No (Underlying_Type (T))
8836 (Nkind (N) /= N_Pragma
8837 or else Get_Pragma_Id (N) /= Pragma_Import)
8840 ("representation item must be after full type declaration", N);
8843 -- If the type has incomplete components, a representation clause is
8844 -- illegal but stream attributes and Convention pragmas are correct.
8846 elsif Has_Private_Component (T) then
8847 if Nkind (N) = N_Pragma then
8852 ("representation item must appear after type is fully defined",
8859 end Rep_Item_Too_Early;
8861 -----------------------
8862 -- Rep_Item_Too_Late --
8863 -----------------------
8865 function Rep_Item_Too_Late
8868 FOnly : Boolean := False) return Boolean
8871 Parent_Type : Entity_Id;
8874 -- Output the too late message. Note that this is not considered a
8875 -- serious error, since the effect is simply that we ignore the
8876 -- representation clause in this case.
8882 procedure Too_Late is
8884 Error_Msg_N ("|representation item appears too late!", N);
8887 -- Start of processing for Rep_Item_Too_Late
8890 -- First make sure entity is not frozen (RM 13.1(9))
8894 -- Exclude imported types, which may be frozen if they appear in a
8895 -- representation clause for a local type.
8897 and then not From_With_Type (T)
8899 -- Exclude generated entitiesa (not coming from source). The common
8900 -- case is when we generate a renaming which prematurely freezes the
8901 -- renamed internal entity, but we still want to be able to set copies
8902 -- of attribute values such as Size/Alignment.
8904 and then Comes_From_Source (T)
8907 S := First_Subtype (T);
8909 if Present (Freeze_Node (S)) then
8911 ("?no more representation items for }", Freeze_Node (S), S);
8916 -- Check for case of non-tagged derived type whose parent either has
8917 -- primitive operations, or is a by reference type (RM 13.1(10)).
8921 and then Is_Derived_Type (T)
8922 and then not Is_Tagged_Type (T)
8924 Parent_Type := Etype (Base_Type (T));
8926 if Has_Primitive_Operations (Parent_Type) then
8929 ("primitive operations already defined for&!", N, Parent_Type);
8932 elsif Is_By_Reference_Type (Parent_Type) then
8935 ("parent type & is a by reference type!", N, Parent_Type);
8940 -- No error, link item into head of chain of rep items for the entity,
8941 -- but avoid chaining if we have an overloadable entity, and the pragma
8942 -- is one that can apply to multiple overloaded entities.
8944 if Is_Overloadable (T) and then Nkind (N) = N_Pragma then
8946 Pname : constant Name_Id := Pragma_Name (N);
8948 if Pname = Name_Convention or else
8949 Pname = Name_Import or else
8950 Pname = Name_Export or else
8951 Pname = Name_External or else
8952 Pname = Name_Interface
8959 Record_Rep_Item (T, N);
8961 end Rep_Item_Too_Late;
8963 -------------------------------------
8964 -- Replace_Type_References_Generic --
8965 -------------------------------------
8967 procedure Replace_Type_References_Generic (N : Node_Id; TName : Name_Id) is
8969 function Replace_Node (N : Node_Id) return Traverse_Result;
8970 -- Processes a single node in the traversal procedure below, checking
8971 -- if node N should be replaced, and if so, doing the replacement.
8973 procedure Replace_Type_Refs is new Traverse_Proc (Replace_Node);
8974 -- This instantiation provides the body of Replace_Type_References
8980 function Replace_Node (N : Node_Id) return Traverse_Result is
8985 -- Case of identifier
8987 if Nkind (N) = N_Identifier then
8989 -- If not the type name, all done with this node
8991 if Chars (N) /= TName then
8994 -- Otherwise do the replacement and we are done with this node
8997 Replace_Type_Reference (N);
9001 -- Case of selected component (which is what a qualification
9002 -- looks like in the unanalyzed tree, which is what we have.
9004 elsif Nkind (N) = N_Selected_Component then
9006 -- If selector name is not our type, keeping going (we might
9007 -- still have an occurrence of the type in the prefix).
9009 if Nkind (Selector_Name (N)) /= N_Identifier
9010 or else Chars (Selector_Name (N)) /= TName
9014 -- Selector name is our type, check qualification
9017 -- Loop through scopes and prefixes, doing comparison
9022 -- Continue if no more scopes or scope with no name
9024 if No (S) or else Nkind (S) not in N_Has_Chars then
9028 -- Do replace if prefix is an identifier matching the
9029 -- scope that we are currently looking at.
9031 if Nkind (P) = N_Identifier
9032 and then Chars (P) = Chars (S)
9034 Replace_Type_Reference (N);
9038 -- Go check scope above us if prefix is itself of the
9039 -- form of a selected component, whose selector matches
9040 -- the scope we are currently looking at.
9042 if Nkind (P) = N_Selected_Component
9043 and then Nkind (Selector_Name (P)) = N_Identifier
9044 and then Chars (Selector_Name (P)) = Chars (S)
9049 -- For anything else, we don't have a match, so keep on
9050 -- going, there are still some weird cases where we may
9051 -- still have a replacement within the prefix.
9059 -- Continue for any other node kind
9067 Replace_Type_Refs (N);
9068 end Replace_Type_References_Generic;
9070 -------------------------
9071 -- Same_Representation --
9072 -------------------------
9074 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
9075 T1 : constant Entity_Id := Underlying_Type (Typ1);
9076 T2 : constant Entity_Id := Underlying_Type (Typ2);
9079 -- A quick check, if base types are the same, then we definitely have
9080 -- the same representation, because the subtype specific representation
9081 -- attributes (Size and Alignment) do not affect representation from
9082 -- the point of view of this test.
9084 if Base_Type (T1) = Base_Type (T2) then
9087 elsif Is_Private_Type (Base_Type (T2))
9088 and then Base_Type (T1) = Full_View (Base_Type (T2))
9093 -- Tagged types never have differing representations
9095 if Is_Tagged_Type (T1) then
9099 -- Representations are definitely different if conventions differ
9101 if Convention (T1) /= Convention (T2) then
9105 -- Representations are different if component alignments differ
9107 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
9109 (Is_Record_Type (T2) or else Is_Array_Type (T2))
9110 and then Component_Alignment (T1) /= Component_Alignment (T2)
9115 -- For arrays, the only real issue is component size. If we know the
9116 -- component size for both arrays, and it is the same, then that's
9117 -- good enough to know we don't have a change of representation.
9119 if Is_Array_Type (T1) then
9120 if Known_Component_Size (T1)
9121 and then Known_Component_Size (T2)
9122 and then Component_Size (T1) = Component_Size (T2)
9124 if VM_Target = No_VM then
9127 -- In VM targets the representation of arrays with aliased
9128 -- components differs from arrays with non-aliased components
9131 return Has_Aliased_Components (Base_Type (T1))
9133 Has_Aliased_Components (Base_Type (T2));
9138 -- Types definitely have same representation if neither has non-standard
9139 -- representation since default representations are always consistent.
9140 -- If only one has non-standard representation, and the other does not,
9141 -- then we consider that they do not have the same representation. They
9142 -- might, but there is no way of telling early enough.
9144 if Has_Non_Standard_Rep (T1) then
9145 if not Has_Non_Standard_Rep (T2) then
9149 return not Has_Non_Standard_Rep (T2);
9152 -- Here the two types both have non-standard representation, and we need
9153 -- to determine if they have the same non-standard representation.
9155 -- For arrays, we simply need to test if the component sizes are the
9156 -- same. Pragma Pack is reflected in modified component sizes, so this
9157 -- check also deals with pragma Pack.
9159 if Is_Array_Type (T1) then
9160 return Component_Size (T1) = Component_Size (T2);
9162 -- Tagged types always have the same representation, because it is not
9163 -- possible to specify different representations for common fields.
9165 elsif Is_Tagged_Type (T1) then
9168 -- Case of record types
9170 elsif Is_Record_Type (T1) then
9172 -- Packed status must conform
9174 if Is_Packed (T1) /= Is_Packed (T2) then
9177 -- Otherwise we must check components. Typ2 maybe a constrained
9178 -- subtype with fewer components, so we compare the components
9179 -- of the base types.
9182 Record_Case : declare
9183 CD1, CD2 : Entity_Id;
9185 function Same_Rep return Boolean;
9186 -- CD1 and CD2 are either components or discriminants. This
9187 -- function tests whether the two have the same representation
9193 function Same_Rep return Boolean is
9195 if No (Component_Clause (CD1)) then
9196 return No (Component_Clause (CD2));
9200 Present (Component_Clause (CD2))
9202 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
9204 Esize (CD1) = Esize (CD2);
9208 -- Start of processing for Record_Case
9211 if Has_Discriminants (T1) then
9213 -- The number of discriminants may be different if the
9214 -- derived type has fewer (constrained by values). The
9215 -- invisible discriminants retain the representation of
9216 -- the original, so the discrepancy does not per se
9217 -- indicate a different representation.
9219 CD1 := First_Discriminant (T1);
9220 CD2 := First_Discriminant (T2);
9221 while Present (CD1) and then Present (CD2) loop
9222 if not Same_Rep then
9225 Next_Discriminant (CD1);
9226 Next_Discriminant (CD2);
9231 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
9232 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
9233 while Present (CD1) loop
9234 if not Same_Rep then
9237 Next_Component (CD1);
9238 Next_Component (CD2);
9246 -- For enumeration types, we must check each literal to see if the
9247 -- representation is the same. Note that we do not permit enumeration
9248 -- representation clauses for Character and Wide_Character, so these
9249 -- cases were already dealt with.
9251 elsif Is_Enumeration_Type (T1) then
9252 Enumeration_Case : declare
9256 L1 := First_Literal (T1);
9257 L2 := First_Literal (T2);
9258 while Present (L1) loop
9259 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
9268 end Enumeration_Case;
9270 -- Any other types have the same representation for these purposes
9275 end Same_Representation;
9281 procedure Set_Biased
9285 Biased : Boolean := True)
9289 Set_Has_Biased_Representation (E);
9291 if Warn_On_Biased_Representation then
9293 ("?" & Msg & " forces biased representation for&", N, E);
9298 --------------------
9299 -- Set_Enum_Esize --
9300 --------------------
9302 procedure Set_Enum_Esize (T : Entity_Id) is
9310 -- Find the minimum standard size (8,16,32,64) that fits
9312 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
9313 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
9316 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
9317 Sz := Standard_Character_Size; -- May be > 8 on some targets
9319 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
9322 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
9325 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
9330 if Hi < Uint_2**08 then
9331 Sz := Standard_Character_Size; -- May be > 8 on some targets
9333 elsif Hi < Uint_2**16 then
9336 elsif Hi < Uint_2**32 then
9339 else pragma Assert (Hi < Uint_2**63);
9344 -- That minimum is the proper size unless we have a foreign convention
9345 -- and the size required is 32 or less, in which case we bump the size
9346 -- up to 32. This is required for C and C++ and seems reasonable for
9347 -- all other foreign conventions.
9349 if Has_Foreign_Convention (T)
9350 and then Esize (T) < Standard_Integer_Size
9352 Init_Esize (T, Standard_Integer_Size);
9358 ------------------------------
9359 -- Validate_Address_Clauses --
9360 ------------------------------
9362 procedure Validate_Address_Clauses is
9364 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
9366 ACCR : Address_Clause_Check_Record
9367 renames Address_Clause_Checks.Table (J);
9378 -- Skip processing of this entry if warning already posted
9380 if not Address_Warning_Posted (ACCR.N) then
9381 Expr := Original_Node (Expression (ACCR.N));
9385 X_Alignment := Alignment (ACCR.X);
9386 Y_Alignment := Alignment (ACCR.Y);
9388 -- Similarly obtain sizes
9390 X_Size := Esize (ACCR.X);
9391 Y_Size := Esize (ACCR.Y);
9393 -- Check for large object overlaying smaller one
9396 and then X_Size > Uint_0
9397 and then X_Size > Y_Size
9400 ("?& overlays smaller object", ACCR.N, ACCR.X);
9402 ("\?program execution may be erroneous", ACCR.N);
9403 Error_Msg_Uint_1 := X_Size;
9405 ("\?size of & is ^", ACCR.N, ACCR.X);
9406 Error_Msg_Uint_1 := Y_Size;
9408 ("\?size of & is ^", ACCR.N, ACCR.Y);
9410 -- Check for inadequate alignment, both of the base object
9411 -- and of the offset, if any.
9413 -- Note: we do not check the alignment if we gave a size
9414 -- warning, since it would likely be redundant.
9416 elsif Y_Alignment /= Uint_0
9417 and then (Y_Alignment < X_Alignment
9420 Nkind (Expr) = N_Attribute_Reference
9422 Attribute_Name (Expr) = Name_Address
9424 Has_Compatible_Alignment
9425 (ACCR.X, Prefix (Expr))
9426 /= Known_Compatible))
9429 ("?specified address for& may be inconsistent "
9433 ("\?program execution may be erroneous (RM 13.3(27))",
9435 Error_Msg_Uint_1 := X_Alignment;
9437 ("\?alignment of & is ^",
9439 Error_Msg_Uint_1 := Y_Alignment;
9441 ("\?alignment of & is ^",
9443 if Y_Alignment >= X_Alignment then
9445 ("\?but offset is not multiple of alignment",
9452 end Validate_Address_Clauses;
9454 ---------------------------
9455 -- Validate_Independence --
9456 ---------------------------
9458 procedure Validate_Independence is
9459 SU : constant Uint := UI_From_Int (System_Storage_Unit);
9467 procedure Check_Array_Type (Atyp : Entity_Id);
9468 -- Checks if the array type Atyp has independent components, and
9469 -- if not, outputs an appropriate set of error messages.
9471 procedure No_Independence;
9472 -- Output message that independence cannot be guaranteed
9474 function OK_Component (C : Entity_Id) return Boolean;
9475 -- Checks one component to see if it is independently accessible, and
9476 -- if so yields True, otherwise yields False if independent access
9477 -- cannot be guaranteed. This is a conservative routine, it only
9478 -- returns True if it knows for sure, it returns False if it knows
9479 -- there is a problem, or it cannot be sure there is no problem.
9481 procedure Reason_Bad_Component (C : Entity_Id);
9482 -- Outputs continuation message if a reason can be determined for
9483 -- the component C being bad.
9485 ----------------------
9486 -- Check_Array_Type --
9487 ----------------------
9489 procedure Check_Array_Type (Atyp : Entity_Id) is
9490 Ctyp : constant Entity_Id := Component_Type (Atyp);
9493 -- OK if no alignment clause, no pack, and no component size
9495 if not Has_Component_Size_Clause (Atyp)
9496 and then not Has_Alignment_Clause (Atyp)
9497 and then not Is_Packed (Atyp)
9502 -- Check actual component size
9504 if not Known_Component_Size (Atyp)
9505 or else not (Addressable (Component_Size (Atyp))
9506 and then Component_Size (Atyp) < 64)
9507 or else Component_Size (Atyp) mod Esize (Ctyp) /= 0
9511 -- Bad component size, check reason
9513 if Has_Component_Size_Clause (Atyp) then
9514 P := Get_Attribute_Definition_Clause
9515 (Atyp, Attribute_Component_Size);
9518 Error_Msg_Sloc := Sloc (P);
9519 Error_Msg_N ("\because of Component_Size clause#", N);
9524 if Is_Packed (Atyp) then
9525 P := Get_Rep_Pragma (Atyp, Name_Pack);
9528 Error_Msg_Sloc := Sloc (P);
9529 Error_Msg_N ("\because of pragma Pack#", N);
9534 -- No reason found, just return
9539 -- Array type is OK independence-wise
9542 end Check_Array_Type;
9544 ---------------------
9545 -- No_Independence --
9546 ---------------------
9548 procedure No_Independence is
9550 if Pragma_Name (N) = Name_Independent then
9552 ("independence cannot be guaranteed for&", N, E);
9555 ("independent components cannot be guaranteed for&", N, E);
9557 end No_Independence;
9563 function OK_Component (C : Entity_Id) return Boolean is
9564 Rec : constant Entity_Id := Scope (C);
9565 Ctyp : constant Entity_Id := Etype (C);
9568 -- OK if no component clause, no Pack, and no alignment clause
9570 if No (Component_Clause (C))
9571 and then not Is_Packed (Rec)
9572 and then not Has_Alignment_Clause (Rec)
9577 -- Here we look at the actual component layout. A component is
9578 -- addressable if its size is a multiple of the Esize of the
9579 -- component type, and its starting position in the record has
9580 -- appropriate alignment, and the record itself has appropriate
9581 -- alignment to guarantee the component alignment.
9583 -- Make sure sizes are static, always assume the worst for any
9584 -- cases where we cannot check static values.
9586 if not (Known_Static_Esize (C)
9588 Known_Static_Esize (Ctyp))
9593 -- Size of component must be addressable or greater than 64 bits
9594 -- and a multiple of bytes.
9596 if not Addressable (Esize (C)) and then Esize (C) < Uint_64 then
9600 -- Check size is proper multiple
9602 if Esize (C) mod Esize (Ctyp) /= 0 then
9606 -- Check alignment of component is OK
9608 if not Known_Component_Bit_Offset (C)
9609 or else Component_Bit_Offset (C) < Uint_0
9610 or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0
9615 -- Check alignment of record type is OK
9617 if not Known_Alignment (Rec)
9618 or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
9623 -- All tests passed, component is addressable
9628 --------------------------
9629 -- Reason_Bad_Component --
9630 --------------------------
9632 procedure Reason_Bad_Component (C : Entity_Id) is
9633 Rec : constant Entity_Id := Scope (C);
9634 Ctyp : constant Entity_Id := Etype (C);
9637 -- If component clause present assume that's the problem
9639 if Present (Component_Clause (C)) then
9640 Error_Msg_Sloc := Sloc (Component_Clause (C));
9641 Error_Msg_N ("\because of Component_Clause#", N);
9645 -- If pragma Pack clause present, assume that's the problem
9647 if Is_Packed (Rec) then
9648 P := Get_Rep_Pragma (Rec, Name_Pack);
9651 Error_Msg_Sloc := Sloc (P);
9652 Error_Msg_N ("\because of pragma Pack#", N);
9657 -- See if record has bad alignment clause
9659 if Has_Alignment_Clause (Rec)
9660 and then Known_Alignment (Rec)
9661 and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
9663 P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment);
9666 Error_Msg_Sloc := Sloc (P);
9667 Error_Msg_N ("\because of Alignment clause#", N);
9671 -- Couldn't find a reason, so return without a message
9674 end Reason_Bad_Component;
9676 -- Start of processing for Validate_Independence
9679 for J in Independence_Checks.First .. Independence_Checks.Last loop
9680 N := Independence_Checks.Table (J).N;
9681 E := Independence_Checks.Table (J).E;
9682 IC := Pragma_Name (N) = Name_Independent_Components;
9684 -- Deal with component case
9686 if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then
9687 if not OK_Component (E) then
9689 Reason_Bad_Component (E);
9694 -- Deal with record with Independent_Components
9696 if IC and then Is_Record_Type (E) then
9697 Comp := First_Component_Or_Discriminant (E);
9698 while Present (Comp) loop
9699 if not OK_Component (Comp) then
9701 Reason_Bad_Component (Comp);
9705 Next_Component_Or_Discriminant (Comp);
9709 -- Deal with address clause case
9711 if Is_Object (E) then
9712 Addr := Address_Clause (E);
9714 if Present (Addr) then
9716 Error_Msg_Sloc := Sloc (Addr);
9717 Error_Msg_N ("\because of Address clause#", N);
9722 -- Deal with independent components for array type
9724 if IC and then Is_Array_Type (E) then
9725 Check_Array_Type (E);
9728 -- Deal with independent components for array object
9730 if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then
9731 Check_Array_Type (Etype (E));
9736 end Validate_Independence;
9738 -----------------------------------
9739 -- Validate_Unchecked_Conversion --
9740 -----------------------------------
9742 procedure Validate_Unchecked_Conversion
9744 Act_Unit : Entity_Id)
9751 -- Obtain source and target types. Note that we call Ancestor_Subtype
9752 -- here because the processing for generic instantiation always makes
9753 -- subtypes, and we want the original frozen actual types.
9755 -- If we are dealing with private types, then do the check on their
9756 -- fully declared counterparts if the full declarations have been
9757 -- encountered (they don't have to be visible, but they must exist!)
9759 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
9761 if Is_Private_Type (Source)
9762 and then Present (Underlying_Type (Source))
9764 Source := Underlying_Type (Source);
9767 Target := Ancestor_Subtype (Etype (Act_Unit));
9769 -- If either type is generic, the instantiation happens within a generic
9770 -- unit, and there is nothing to check. The proper check will happen
9771 -- when the enclosing generic is instantiated.
9773 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
9777 if Is_Private_Type (Target)
9778 and then Present (Underlying_Type (Target))
9780 Target := Underlying_Type (Target);
9783 -- Source may be unconstrained array, but not target
9785 if Is_Array_Type (Target) and then not Is_Constrained (Target) then
9787 ("unchecked conversion to unconstrained array not allowed", N);
9791 -- Warn if conversion between two different convention pointers
9793 if Is_Access_Type (Target)
9794 and then Is_Access_Type (Source)
9795 and then Convention (Target) /= Convention (Source)
9796 and then Warn_On_Unchecked_Conversion
9798 -- Give warnings for subprogram pointers only on most targets. The
9799 -- exception is VMS, where data pointers can have different lengths
9800 -- depending on the pointer convention.
9802 if Is_Access_Subprogram_Type (Target)
9803 or else Is_Access_Subprogram_Type (Source)
9804 or else OpenVMS_On_Target
9807 ("?conversion between pointers with different conventions!", N);
9811 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
9812 -- warning when compiling GNAT-related sources.
9814 if Warn_On_Unchecked_Conversion
9815 and then not In_Predefined_Unit (N)
9816 and then RTU_Loaded (Ada_Calendar)
9818 (Chars (Source) = Name_Time
9820 Chars (Target) = Name_Time)
9822 -- If Ada.Calendar is loaded and the name of one of the operands is
9823 -- Time, there is a good chance that this is Ada.Calendar.Time.
9826 Calendar_Time : constant Entity_Id :=
9827 Full_View (RTE (RO_CA_Time));
9829 pragma Assert (Present (Calendar_Time));
9831 if Source = Calendar_Time or else Target = Calendar_Time then
9833 ("?representation of 'Time values may change between " &
9834 "'G'N'A'T versions", N);
9839 -- Make entry in unchecked conversion table for later processing by
9840 -- Validate_Unchecked_Conversions, which will check sizes and alignments
9841 -- (using values set by the back-end where possible). This is only done
9842 -- if the appropriate warning is active.
9844 if Warn_On_Unchecked_Conversion then
9845 Unchecked_Conversions.Append
9846 (New_Val => UC_Entry'(Eloc => Sloc (N),
9850 -- If both sizes are known statically now, then back end annotation
9851 -- is not required to do a proper check but if either size is not
9852 -- known statically, then we need the annotation.
9854 if Known_Static_RM_Size (Source)
9855 and then Known_Static_RM_Size (Target)
9859 Back_Annotate_Rep_Info := True;
9863 -- If unchecked conversion to access type, and access type is declared
9864 -- in the same unit as the unchecked conversion, then set the flag
9865 -- No_Strict_Aliasing (no strict aliasing is implicit here)
9867 if Is_Access_Type (Target) and then
9868 In_Same_Source_Unit (Target, N)
9870 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
9873 -- Generate N_Validate_Unchecked_Conversion node for back end in case
9874 -- the back end needs to perform special validation checks.
9876 -- Shouldn't this be in Exp_Ch13, since the check only gets done if we
9877 -- have full expansion and the back end is called ???
9880 Make_Validate_Unchecked_Conversion (Sloc (N));
9881 Set_Source_Type (Vnode, Source);
9882 Set_Target_Type (Vnode, Target);
9884 -- If the unchecked conversion node is in a list, just insert before it.
9885 -- If not we have some strange case, not worth bothering about.
9887 if Is_List_Member (N) then
9888 Insert_After (N, Vnode);
9890 end Validate_Unchecked_Conversion;
9892 ------------------------------------
9893 -- Validate_Unchecked_Conversions --
9894 ------------------------------------
9896 procedure Validate_Unchecked_Conversions is
9898 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
9900 T : UC_Entry renames Unchecked_Conversions.Table (N);
9902 Eloc : constant Source_Ptr := T.Eloc;
9903 Source : constant Entity_Id := T.Source;
9904 Target : constant Entity_Id := T.Target;
9910 -- This validation check, which warns if we have unequal sizes for
9911 -- unchecked conversion, and thus potentially implementation
9912 -- dependent semantics, is one of the few occasions on which we
9913 -- use the official RM size instead of Esize. See description in
9914 -- Einfo "Handling of Type'Size Values" for details.
9916 if Serious_Errors_Detected = 0
9917 and then Known_Static_RM_Size (Source)
9918 and then Known_Static_RM_Size (Target)
9920 -- Don't do the check if warnings off for either type, note the
9921 -- deliberate use of OR here instead of OR ELSE to get the flag
9922 -- Warnings_Off_Used set for both types if appropriate.
9924 and then not (Has_Warnings_Off (Source)
9926 Has_Warnings_Off (Target))
9928 Source_Siz := RM_Size (Source);
9929 Target_Siz := RM_Size (Target);
9931 if Source_Siz /= Target_Siz then
9933 ("?types for unchecked conversion have different sizes!",
9936 if All_Errors_Mode then
9937 Error_Msg_Name_1 := Chars (Source);
9938 Error_Msg_Uint_1 := Source_Siz;
9939 Error_Msg_Name_2 := Chars (Target);
9940 Error_Msg_Uint_2 := Target_Siz;
9941 Error_Msg ("\size of % is ^, size of % is ^?", Eloc);
9943 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
9945 if Is_Discrete_Type (Source)
9947 Is_Discrete_Type (Target)
9949 if Source_Siz > Target_Siz then
9951 ("\?^ high order bits of source will be ignored!",
9954 elsif Is_Unsigned_Type (Source) then
9956 ("\?source will be extended with ^ high order " &
9957 "zero bits?!", Eloc);
9961 ("\?source will be extended with ^ high order " &
9966 elsif Source_Siz < Target_Siz then
9967 if Is_Discrete_Type (Target) then
9968 if Bytes_Big_Endian then
9970 ("\?target value will include ^ undefined " &
9975 ("\?target value will include ^ undefined " &
9982 ("\?^ trailing bits of target value will be " &
9983 "undefined!", Eloc);
9986 else pragma Assert (Source_Siz > Target_Siz);
9988 ("\?^ trailing bits of source will be ignored!",
9995 -- If both types are access types, we need to check the alignment.
9996 -- If the alignment of both is specified, we can do it here.
9998 if Serious_Errors_Detected = 0
9999 and then Ekind (Source) in Access_Kind
10000 and then Ekind (Target) in Access_Kind
10001 and then Target_Strict_Alignment
10002 and then Present (Designated_Type (Source))
10003 and then Present (Designated_Type (Target))
10006 D_Source : constant Entity_Id := Designated_Type (Source);
10007 D_Target : constant Entity_Id := Designated_Type (Target);
10010 if Known_Alignment (D_Source)
10012 Known_Alignment (D_Target)
10015 Source_Align : constant Uint := Alignment (D_Source);
10016 Target_Align : constant Uint := Alignment (D_Target);
10019 if Source_Align < Target_Align
10020 and then not Is_Tagged_Type (D_Source)
10022 -- Suppress warning if warnings suppressed on either
10023 -- type or either designated type. Note the use of
10024 -- OR here instead of OR ELSE. That is intentional,
10025 -- we would like to set flag Warnings_Off_Used in
10026 -- all types for which warnings are suppressed.
10028 and then not (Has_Warnings_Off (D_Source)
10030 Has_Warnings_Off (D_Target)
10032 Has_Warnings_Off (Source)
10034 Has_Warnings_Off (Target))
10036 Error_Msg_Uint_1 := Target_Align;
10037 Error_Msg_Uint_2 := Source_Align;
10038 Error_Msg_Node_1 := D_Target;
10039 Error_Msg_Node_2 := D_Source;
10041 ("?alignment of & (^) is stricter than " &
10042 "alignment of & (^)!", Eloc);
10044 ("\?resulting access value may have invalid " &
10045 "alignment!", Eloc);
10053 end Validate_Unchecked_Conversions;