1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2014, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Exp_Disp; use Exp_Disp;
34 with Exp_Tss; use Exp_Tss;
35 with Exp_Util; use Exp_Util;
37 with Lib.Xref; use Lib.Xref;
38 with Namet; use Namet;
39 with Nlists; use Nlists;
40 with Nmake; use Nmake;
42 with Restrict; use Restrict;
43 with Rident; use Rident;
44 with Rtsfind; use Rtsfind;
46 with Sem_Aux; use Sem_Aux;
47 with Sem_Case; use Sem_Case;
48 with Sem_Ch3; use Sem_Ch3;
49 with Sem_Ch6; use Sem_Ch6;
50 with Sem_Ch8; use Sem_Ch8;
51 with Sem_Dim; use Sem_Dim;
52 with Sem_Disp; use Sem_Disp;
53 with Sem_Eval; use Sem_Eval;
54 with Sem_Prag; use Sem_Prag;
55 with Sem_Res; use Sem_Res;
56 with Sem_Type; use Sem_Type;
57 with Sem_Util; use Sem_Util;
58 with Sem_Warn; use Sem_Warn;
59 with Sinput; use Sinput;
60 with Snames; use Snames;
61 with Stand; use Stand;
62 with Sinfo; use Sinfo;
63 with Stringt; use Stringt;
64 with Targparm; use Targparm;
65 with Ttypes; use Ttypes;
66 with Tbuild; use Tbuild;
67 with Urealp; use Urealp;
68 with Warnsw; use Warnsw;
70 with GNAT.Heap_Sort_G;
72 package body Sem_Ch13 is
74 SSU : constant Pos := System_Storage_Unit;
75 -- Convenient short hand for commonly used constant
77 -----------------------
78 -- Local Subprograms --
79 -----------------------
81 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint);
82 -- This routine is called after setting one of the sizes of type entity
83 -- Typ to Size. The purpose is to deal with the situation of a derived
84 -- type whose inherited alignment is no longer appropriate for the new
85 -- size value. In this case, we reset the Alignment to unknown.
87 procedure Build_Discrete_Static_Predicate
91 -- Given a predicated type Typ, where Typ is a discrete static subtype,
92 -- whose predicate expression is Expr, tests if Expr is a static predicate,
93 -- and if so, builds the predicate range list. Nam is the name of the one
94 -- argument to the predicate function. Occurrences of the type name in the
95 -- predicate expression have been replaced by identifier references to this
96 -- name, which is unique, so any identifier with Chars matching Nam must be
97 -- a reference to the type. If the predicate is non-static, this procedure
98 -- returns doing nothing. If the predicate is static, then the predicate
99 -- list is stored in Static_Predicate (Typ), and the Expr is rewritten as
100 -- a canonicalized membership operation.
102 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id);
103 -- If Typ has predicates (indicated by Has_Predicates being set for Typ),
104 -- then either there are pragma Predicate entries on the rep chain for the
105 -- type (note that Predicate aspects are converted to pragma Predicate), or
106 -- there are inherited aspects from a parent type, or ancestor subtypes.
107 -- This procedure builds the spec and body for the Predicate function that
108 -- tests these predicates. N is the freeze node for the type. The spec of
109 -- the function is inserted before the freeze node, and the body of the
110 -- function is inserted after the freeze node. If the predicate expression
111 -- has at least one Raise_Expression, then this procedure also builds the
112 -- M version of the predicate function for use in membership tests.
114 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id);
115 -- Called if both Storage_Pool and Storage_Size attribute definition
116 -- clauses (SP and SS) are present for entity Ent. Issue error message.
118 procedure Freeze_Entity_Checks (N : Node_Id);
119 -- Called from Analyze_Freeze_Entity and Analyze_Generic_Freeze Entity
120 -- to generate appropriate semantic checks that are delayed until this
121 -- point (they had to be delayed this long for cases of delayed aspects,
122 -- e.g. analysis of statically predicated subtypes in choices, for which
123 -- we have to be sure the subtypes in question are frozen before checking.
125 function Get_Alignment_Value (Expr : Node_Id) return Uint;
126 -- Given the expression for an alignment value, returns the corresponding
127 -- Uint value. If the value is inappropriate, then error messages are
128 -- posted as required, and a value of No_Uint is returned.
130 function Is_Operational_Item (N : Node_Id) return Boolean;
131 -- A specification for a stream attribute is allowed before the full type
132 -- is declared, as explained in AI-00137 and the corrigendum. Attributes
133 -- that do not specify a representation characteristic are operational
136 procedure New_Stream_Subprogram
140 Nam : TSS_Name_Type);
141 -- Create a subprogram renaming of a given stream attribute to the
142 -- designated subprogram and then in the tagged case, provide this as a
143 -- primitive operation, or in the non-tagged case make an appropriate TSS
144 -- entry. This is more properly an expansion activity than just semantics,
145 -- but the presence of user-defined stream functions for limited types is a
146 -- legality check, which is why this takes place here rather than in
147 -- exp_ch13, where it was previously. Nam indicates the name of the TSS
148 -- function to be generated.
150 -- To avoid elaboration anomalies with freeze nodes, for untagged types
151 -- we generate both a subprogram declaration and a subprogram renaming
152 -- declaration, so that the attribute specification is handled as a
153 -- renaming_as_body. For tagged types, the specification is one of the
157 with procedure Replace_Type_Reference (N : Node_Id);
158 procedure Replace_Type_References_Generic (N : Node_Id; TName : Name_Id);
159 -- This is used to scan an expression for a predicate or invariant aspect
160 -- replacing occurrences of the name TName (the name of the subtype to
161 -- which the aspect applies) with appropriate references to the parameter
162 -- of the predicate function or invariant procedure. The procedure passed
163 -- as a generic parameter does the actual replacement of node N, which is
164 -- either a simple direct reference to TName, or a selected component that
165 -- represents an appropriately qualified occurrence of TName.
167 procedure Resolve_Iterable_Operation
172 -- If the name of a primitive operation for an Iterable aspect is
173 -- overloaded, resolve according to required signature.
179 Biased : Boolean := True);
180 -- If Biased is True, sets Has_Biased_Representation flag for E, and
181 -- outputs a warning message at node N if Warn_On_Biased_Representation is
182 -- is True. This warning inserts the string Msg to describe the construct
185 ----------------------------------------------
186 -- Table for Validate_Unchecked_Conversions --
187 ----------------------------------------------
189 -- The following table collects unchecked conversions for validation.
190 -- Entries are made by Validate_Unchecked_Conversion and then the call
191 -- to Validate_Unchecked_Conversions does the actual error checking and
192 -- posting of warnings. The reason for this delayed processing is to take
193 -- advantage of back-annotations of size and alignment values performed by
196 -- Note: the reason we store a Source_Ptr value instead of a Node_Id is
197 -- that by the time Validate_Unchecked_Conversions is called, Sprint will
198 -- already have modified all Sloc values if the -gnatD option is set.
200 type UC_Entry is record
201 Eloc : Source_Ptr; -- node used for posting warnings
202 Source : Entity_Id; -- source type for unchecked conversion
203 Target : Entity_Id; -- target type for unchecked conversion
204 Act_Unit : Entity_Id; -- actual function instantiated
207 package Unchecked_Conversions is new Table.Table (
208 Table_Component_Type => UC_Entry,
209 Table_Index_Type => Int,
210 Table_Low_Bound => 1,
212 Table_Increment => 200,
213 Table_Name => "Unchecked_Conversions");
215 ----------------------------------------
216 -- Table for Validate_Address_Clauses --
217 ----------------------------------------
219 -- If an address clause has the form
221 -- for X'Address use Expr
223 -- where Expr is of the form Y'Address or recursively is a reference to a
224 -- constant of either of these forms, and X and Y are entities of objects,
225 -- then if Y has a smaller alignment than X, that merits a warning about
226 -- possible bad alignment. The following table collects address clauses of
227 -- this kind. We put these in a table so that they can be checked after the
228 -- back end has completed annotation of the alignments of objects, since we
229 -- can catch more cases that way.
231 type Address_Clause_Check_Record is record
233 -- The address clause
236 -- The entity of the object overlaying Y
239 -- The entity of the object being overlaid
242 -- Whether the address is offset within Y
245 package Address_Clause_Checks is new Table.Table (
246 Table_Component_Type => Address_Clause_Check_Record,
247 Table_Index_Type => Int,
248 Table_Low_Bound => 1,
250 Table_Increment => 200,
251 Table_Name => "Address_Clause_Checks");
253 -----------------------------------------
254 -- Adjust_Record_For_Reverse_Bit_Order --
255 -----------------------------------------
257 procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is
262 -- Processing depends on version of Ada
264 -- For Ada 95, we just renumber bits within a storage unit. We do the
265 -- same for Ada 83 mode, since we recognize the Bit_Order attribute in
266 -- Ada 83, and are free to add this extension.
268 if Ada_Version < Ada_2005 then
269 Comp := First_Component_Or_Discriminant (R);
270 while Present (Comp) loop
271 CC := Component_Clause (Comp);
273 -- If component clause is present, then deal with the non-default
274 -- bit order case for Ada 95 mode.
276 -- We only do this processing for the base type, and in fact that
277 -- is important, since otherwise if there are record subtypes, we
278 -- could reverse the bits once for each subtype, which is wrong.
280 if Present (CC) and then Ekind (R) = E_Record_Type then
282 CFB : constant Uint := Component_Bit_Offset (Comp);
283 CSZ : constant Uint := Esize (Comp);
284 CLC : constant Node_Id := Component_Clause (Comp);
285 Pos : constant Node_Id := Position (CLC);
286 FB : constant Node_Id := First_Bit (CLC);
288 Storage_Unit_Offset : constant Uint :=
289 CFB / System_Storage_Unit;
291 Start_Bit : constant Uint :=
292 CFB mod System_Storage_Unit;
295 -- Cases where field goes over storage unit boundary
297 if Start_Bit + CSZ > System_Storage_Unit then
299 -- Allow multi-byte field but generate warning
301 if Start_Bit mod System_Storage_Unit = 0
302 and then CSZ mod System_Storage_Unit = 0
305 ("info: multi-byte field specified with "
306 & "non-standard Bit_Order?V?", CLC);
308 if Bytes_Big_Endian then
310 ("\bytes are not reversed "
311 & "(component is big-endian)?V?", CLC);
314 ("\bytes are not reversed "
315 & "(component is little-endian)?V?", CLC);
318 -- Do not allow non-contiguous field
322 ("attempt to specify non-contiguous field "
323 & "not permitted", CLC);
325 ("\caused by non-standard Bit_Order "
328 ("\consider possibility of using "
329 & "Ada 2005 mode here", CLC);
332 -- Case where field fits in one storage unit
335 -- Give warning if suspicious component clause
337 if Intval (FB) >= System_Storage_Unit
338 and then Warn_On_Reverse_Bit_Order
341 ("info: Bit_Order clause does not affect " &
342 "byte ordering?V?", Pos);
344 Intval (Pos) + Intval (FB) /
347 ("info: position normalized to ^ before bit " &
348 "order interpreted?V?", Pos);
351 -- Here is where we fix up the Component_Bit_Offset value
352 -- to account for the reverse bit order. Some examples of
353 -- what needs to be done are:
355 -- First_Bit .. Last_Bit Component_Bit_Offset
367 -- The rule is that the first bit is is obtained by
368 -- subtracting the old ending bit from storage_unit - 1.
370 Set_Component_Bit_Offset
372 (Storage_Unit_Offset * System_Storage_Unit) +
373 (System_Storage_Unit - 1) -
374 (Start_Bit + CSZ - 1));
376 Set_Normalized_First_Bit
378 Component_Bit_Offset (Comp) mod
379 System_Storage_Unit);
384 Next_Component_Or_Discriminant (Comp);
387 -- For Ada 2005, we do machine scalar processing, as fully described In
388 -- AI-133. This involves gathering all components which start at the
389 -- same byte offset and processing them together. Same approach is still
390 -- valid in later versions including Ada 2012.
394 Max_Machine_Scalar_Size : constant Uint :=
396 (Standard_Long_Long_Integer_Size);
397 -- We use this as the maximum machine scalar size
400 SSU : constant Uint := UI_From_Int (System_Storage_Unit);
403 -- This first loop through components does two things. First it
404 -- deals with the case of components with component clauses whose
405 -- length is greater than the maximum machine scalar size (either
406 -- accepting them or rejecting as needed). Second, it counts the
407 -- number of components with component clauses whose length does
408 -- not exceed this maximum for later processing.
411 Comp := First_Component_Or_Discriminant (R);
412 while Present (Comp) loop
413 CC := Component_Clause (Comp);
417 Fbit : constant Uint := Static_Integer (First_Bit (CC));
418 Lbit : constant Uint := Static_Integer (Last_Bit (CC));
421 -- Case of component with last bit >= max machine scalar
423 if Lbit >= Max_Machine_Scalar_Size then
425 -- This is allowed only if first bit is zero, and
426 -- last bit + 1 is a multiple of storage unit size.
428 if Fbit = 0 and then (Lbit + 1) mod SSU = 0 then
430 -- This is the case to give a warning if enabled
432 if Warn_On_Reverse_Bit_Order then
434 ("info: multi-byte field specified with "
435 & " non-standard Bit_Order?V?", CC);
437 if Bytes_Big_Endian then
439 ("\bytes are not reversed "
440 & "(component is big-endian)?V?", CC);
443 ("\bytes are not reversed "
444 & "(component is little-endian)?V?", CC);
448 -- Give error message for RM 13.5.1(10) violation
452 ("machine scalar rules not followed for&",
453 First_Bit (CC), Comp);
455 Error_Msg_Uint_1 := Lbit;
456 Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
458 ("\last bit (^) exceeds maximum machine "
462 if (Lbit + 1) mod SSU /= 0 then
463 Error_Msg_Uint_1 := SSU;
465 ("\and is not a multiple of Storage_Unit (^) "
470 Error_Msg_Uint_1 := Fbit;
472 ("\and first bit (^) is non-zero "
478 -- OK case of machine scalar related component clause,
479 -- For now, just count them.
482 Num_CC := Num_CC + 1;
487 Next_Component_Or_Discriminant (Comp);
490 -- We need to sort the component clauses on the basis of the
491 -- Position values in the clause, so we can group clauses with
492 -- the same Position together to determine the relevant machine
496 Comps : array (0 .. Num_CC) of Entity_Id;
497 -- Array to collect component and discriminant entities. The
498 -- data starts at index 1, the 0'th entry is for the sort
501 function CP_Lt (Op1, Op2 : Natural) return Boolean;
502 -- Compare routine for Sort
504 procedure CP_Move (From : Natural; To : Natural);
505 -- Move routine for Sort
507 package Sorting is new GNAT.Heap_Sort_G (CP_Move, CP_Lt);
511 -- Start and stop positions in the component list of the set of
512 -- components with the same starting position (that constitute
513 -- components in a single machine scalar).
516 -- Maximum last bit value of any component in this set
519 -- Corresponding machine scalar size
525 function CP_Lt (Op1, Op2 : Natural) return Boolean is
527 return Position (Component_Clause (Comps (Op1))) <
528 Position (Component_Clause (Comps (Op2)));
535 procedure CP_Move (From : Natural; To : Natural) is
537 Comps (To) := Comps (From);
540 -- Start of processing for Sort_CC
543 -- Collect the machine scalar relevant component clauses
546 Comp := First_Component_Or_Discriminant (R);
547 while Present (Comp) loop
549 CC : constant Node_Id := Component_Clause (Comp);
552 -- Collect only component clauses whose last bit is less
553 -- than machine scalar size. Any component clause whose
554 -- last bit exceeds this value does not take part in
555 -- machine scalar layout considerations. The test for
556 -- Error_Posted makes sure we exclude component clauses
557 -- for which we already posted an error.
560 and then not Error_Posted (Last_Bit (CC))
561 and then Static_Integer (Last_Bit (CC)) <
562 Max_Machine_Scalar_Size
564 Num_CC := Num_CC + 1;
565 Comps (Num_CC) := Comp;
569 Next_Component_Or_Discriminant (Comp);
572 -- Sort by ascending position number
574 Sorting.Sort (Num_CC);
576 -- We now have all the components whose size does not exceed
577 -- the max machine scalar value, sorted by starting position.
578 -- In this loop we gather groups of clauses starting at the
579 -- same position, to process them in accordance with AI-133.
582 while Stop < Num_CC loop
587 (Last_Bit (Component_Clause (Comps (Start))));
588 while Stop < Num_CC loop
590 (Position (Component_Clause (Comps (Stop + 1)))) =
592 (Position (Component_Clause (Comps (Stop))))
600 (Component_Clause (Comps (Stop)))));
606 -- Now we have a group of component clauses from Start to
607 -- Stop whose positions are identical, and MaxL is the
608 -- maximum last bit value of any of these components.
610 -- We need to determine the corresponding machine scalar
611 -- size. This loop assumes that machine scalar sizes are
612 -- even, and that each possible machine scalar has twice
613 -- as many bits as the next smaller one.
615 MSS := Max_Machine_Scalar_Size;
617 and then (MSS / 2) >= SSU
618 and then (MSS / 2) > MaxL
623 -- Here is where we fix up the Component_Bit_Offset value
624 -- to account for the reverse bit order. Some examples of
625 -- what needs to be done for the case of a machine scalar
628 -- First_Bit .. Last_Bit Component_Bit_Offset
640 -- The rule is that the first bit is obtained by subtracting
641 -- the old ending bit from machine scalar size - 1.
643 for C in Start .. Stop loop
645 Comp : constant Entity_Id := Comps (C);
646 CC : constant Node_Id := Component_Clause (Comp);
648 LB : constant Uint := Static_Integer (Last_Bit (CC));
649 NFB : constant Uint := MSS - Uint_1 - LB;
650 NLB : constant Uint := NFB + Esize (Comp) - 1;
651 Pos : constant Uint := Static_Integer (Position (CC));
654 if Warn_On_Reverse_Bit_Order then
655 Error_Msg_Uint_1 := MSS;
657 ("info: reverse bit order in machine " &
658 "scalar of length^?V?", First_Bit (CC));
659 Error_Msg_Uint_1 := NFB;
660 Error_Msg_Uint_2 := NLB;
662 if Bytes_Big_Endian then
664 ("\big-endian range for component "
665 & "& is ^ .. ^?V?", First_Bit (CC), Comp);
668 ("\little-endian range for component"
669 & "& is ^ .. ^?V?", First_Bit (CC), Comp);
673 Set_Component_Bit_Offset (Comp, Pos * SSU + NFB);
674 Set_Normalized_First_Bit (Comp, NFB mod SSU);
681 end Adjust_Record_For_Reverse_Bit_Order;
683 -------------------------------------
684 -- Alignment_Check_For_Size_Change --
685 -------------------------------------
687 procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint) is
689 -- If the alignment is known, and not set by a rep clause, and is
690 -- inconsistent with the size being set, then reset it to unknown,
691 -- we assume in this case that the size overrides the inherited
692 -- alignment, and that the alignment must be recomputed.
694 if Known_Alignment (Typ)
695 and then not Has_Alignment_Clause (Typ)
696 and then Size mod (Alignment (Typ) * SSU) /= 0
698 Init_Alignment (Typ);
700 end Alignment_Check_For_Size_Change;
702 -------------------------------------
703 -- Analyze_Aspects_At_Freeze_Point --
704 -------------------------------------
706 procedure Analyze_Aspects_At_Freeze_Point (E : Entity_Id) is
711 procedure Analyze_Aspect_Default_Value (ASN : Node_Id);
712 -- This routine analyzes an Aspect_Default_[Component_]Value denoted by
713 -- the aspect specification node ASN.
715 procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id);
716 -- As discussed in the spec of Aspects (see Aspect_Delay declaration),
717 -- a derived type can inherit aspects from its parent which have been
718 -- specified at the time of the derivation using an aspect, as in:
720 -- type A is range 1 .. 10
721 -- with Size => Not_Defined_Yet;
725 -- Not_Defined_Yet : constant := 64;
727 -- In this example, the Size of A is considered to be specified prior
728 -- to the derivation, and thus inherited, even though the value is not
729 -- known at the time of derivation. To deal with this, we use two entity
730 -- flags. The flag Has_Derived_Rep_Aspects is set in the parent type (A
731 -- here), and then the flag May_Inherit_Delayed_Rep_Aspects is set in
732 -- the derived type (B here). If this flag is set when the derived type
733 -- is frozen, then this procedure is called to ensure proper inheritance
734 -- of all delayed aspects from the parent type. The derived type is E,
735 -- the argument to Analyze_Aspects_At_Freeze_Point. ASN is the first
736 -- aspect specification node in the Rep_Item chain for the parent type.
738 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id);
739 -- Given an aspect specification node ASN whose expression is an
740 -- optional Boolean, this routines creates the corresponding pragma
741 -- at the freezing point.
743 ----------------------------------
744 -- Analyze_Aspect_Default_Value --
745 ----------------------------------
747 procedure Analyze_Aspect_Default_Value (ASN : Node_Id) is
748 Ent : constant Entity_Id := Entity (ASN);
749 Expr : constant Node_Id := Expression (ASN);
750 Id : constant Node_Id := Identifier (ASN);
753 Error_Msg_Name_1 := Chars (Id);
755 if not Is_Type (Ent) then
756 Error_Msg_N ("aspect% can only apply to a type", Id);
759 elsif not Is_First_Subtype (Ent) then
760 Error_Msg_N ("aspect% cannot apply to subtype", Id);
763 elsif A_Id = Aspect_Default_Value
764 and then not Is_Scalar_Type (Ent)
766 Error_Msg_N ("aspect% can only be applied to scalar type", Id);
769 elsif A_Id = Aspect_Default_Component_Value then
770 if not Is_Array_Type (Ent) then
771 Error_Msg_N ("aspect% can only be applied to array type", Id);
774 elsif not Is_Scalar_Type (Component_Type (Ent)) then
775 Error_Msg_N ("aspect% requires scalar components", Id);
780 Set_Has_Default_Aspect (Base_Type (Ent));
782 if Is_Scalar_Type (Ent) then
783 Set_Default_Aspect_Value (Base_Type (Ent), Expr);
785 Set_Default_Aspect_Component_Value (Base_Type (Ent), Expr);
787 end Analyze_Aspect_Default_Value;
789 ---------------------------------
790 -- Inherit_Delayed_Rep_Aspects --
791 ---------------------------------
793 procedure Inherit_Delayed_Rep_Aspects (ASN : Node_Id) is
794 P : constant Entity_Id := Entity (ASN);
795 -- Entithy for parent type
798 -- Item from Rep_Item chain
803 -- Loop through delayed aspects for the parent type
806 while Present (N) loop
807 if Nkind (N) = N_Aspect_Specification then
808 exit when Entity (N) /= P;
810 if Is_Delayed_Aspect (N) then
811 A := Get_Aspect_Id (Chars (Identifier (N)));
813 -- Process delayed rep aspect. For Boolean attributes it is
814 -- not possible to cancel an attribute once set (the attempt
815 -- to use an aspect with xxx => False is an error) for a
816 -- derived type. So for those cases, we do not have to check
817 -- if a clause has been given for the derived type, since it
818 -- is harmless to set it again if it is already set.
824 when Aspect_Alignment =>
825 if not Has_Alignment_Clause (E) then
826 Set_Alignment (E, Alignment (P));
831 when Aspect_Atomic =>
832 if Is_Atomic (P) then
838 when Aspect_Atomic_Components =>
839 if Has_Atomic_Components (P) then
840 Set_Has_Atomic_Components (Base_Type (E));
845 when Aspect_Bit_Order =>
846 if Is_Record_Type (E)
847 and then No (Get_Attribute_Definition_Clause
848 (E, Attribute_Bit_Order))
849 and then Reverse_Bit_Order (P)
851 Set_Reverse_Bit_Order (Base_Type (E));
856 when Aspect_Component_Size =>
858 and then not Has_Component_Size_Clause (E)
861 (Base_Type (E), Component_Size (P));
866 when Aspect_Machine_Radix =>
867 if Is_Decimal_Fixed_Point_Type (E)
868 and then not Has_Machine_Radix_Clause (E)
870 Set_Machine_Radix_10 (E, Machine_Radix_10 (P));
873 -- Object_Size (also Size which also sets Object_Size)
875 when Aspect_Object_Size | Aspect_Size =>
876 if not Has_Size_Clause (E)
878 No (Get_Attribute_Definition_Clause
879 (E, Attribute_Object_Size))
881 Set_Esize (E, Esize (P));
887 if not Is_Packed (E) then
888 Set_Is_Packed (Base_Type (E));
890 if Is_Bit_Packed_Array (P) then
891 Set_Is_Bit_Packed_Array (Base_Type (E));
892 Set_Packed_Array_Type (E, Packed_Array_Type (P));
896 -- Scalar_Storage_Order
898 when Aspect_Scalar_Storage_Order =>
899 if (Is_Record_Type (E) or else Is_Array_Type (E))
900 and then No (Get_Attribute_Definition_Clause
901 (E, Attribute_Scalar_Storage_Order))
902 and then Reverse_Storage_Order (P)
904 Set_Reverse_Storage_Order (Base_Type (E));
910 if Is_Fixed_Point_Type (E)
911 and then not Has_Small_Clause (E)
913 Set_Small_Value (E, Small_Value (P));
918 when Aspect_Storage_Size =>
919 if (Is_Access_Type (E) or else Is_Task_Type (E))
920 and then not Has_Storage_Size_Clause (E)
922 Set_Storage_Size_Variable
923 (Base_Type (E), Storage_Size_Variable (P));
928 when Aspect_Value_Size =>
930 -- Value_Size is never inherited, it is either set by
931 -- default, or it is explicitly set for the derived
932 -- type. So nothing to do here.
938 when Aspect_Volatile =>
939 if Is_Volatile (P) then
943 -- Volatile_Components
945 when Aspect_Volatile_Components =>
946 if Has_Volatile_Components (P) then
947 Set_Has_Volatile_Components (Base_Type (E));
950 -- That should be all the Rep Aspects
953 pragma Assert (Aspect_Delay (A_Id) /= Rep_Aspect);
960 N := Next_Rep_Item (N);
962 end Inherit_Delayed_Rep_Aspects;
964 -------------------------------------
965 -- Make_Pragma_From_Boolean_Aspect --
966 -------------------------------------
968 procedure Make_Pragma_From_Boolean_Aspect (ASN : Node_Id) is
969 Ident : constant Node_Id := Identifier (ASN);
970 A_Name : constant Name_Id := Chars (Ident);
971 A_Id : constant Aspect_Id := Get_Aspect_Id (A_Name);
972 Ent : constant Entity_Id := Entity (ASN);
973 Expr : constant Node_Id := Expression (ASN);
974 Loc : constant Source_Ptr := Sloc (ASN);
978 procedure Check_False_Aspect_For_Derived_Type;
979 -- This procedure checks for the case of a false aspect for a derived
980 -- type, which improperly tries to cancel an aspect inherited from
983 -----------------------------------------
984 -- Check_False_Aspect_For_Derived_Type --
985 -----------------------------------------
987 procedure Check_False_Aspect_For_Derived_Type is
991 -- We are only checking derived types
993 if not Is_Derived_Type (E) then
997 Par := Nearest_Ancestor (E);
1000 when Aspect_Atomic | Aspect_Shared =>
1001 if not Is_Atomic (Par) then
1005 when Aspect_Atomic_Components =>
1006 if not Has_Atomic_Components (Par) then
1010 when Aspect_Discard_Names =>
1011 if not Discard_Names (Par) then
1016 if not Is_Packed (Par) then
1020 when Aspect_Unchecked_Union =>
1021 if not Is_Unchecked_Union (Par) then
1025 when Aspect_Volatile =>
1026 if not Is_Volatile (Par) then
1030 when Aspect_Volatile_Components =>
1031 if not Has_Volatile_Components (Par) then
1039 -- Fall through means we are canceling an inherited aspect
1041 Error_Msg_Name_1 := A_Name;
1043 ("derived type& inherits aspect%, cannot cancel", Expr, E);
1045 end Check_False_Aspect_For_Derived_Type;
1047 -- Start of processing for Make_Pragma_From_Boolean_Aspect
1050 -- Note that we know Expr is present, because for a missing Expr
1051 -- argument, we knew it was True and did not need to delay the
1052 -- evaluation to the freeze point.
1054 if Is_False (Static_Boolean (Expr)) then
1055 Check_False_Aspect_For_Derived_Type;
1060 Pragma_Argument_Associations => New_List (
1061 Make_Pragma_Argument_Association (Sloc (Ident),
1062 Expression => New_Occurrence_Of (Ent, Sloc (Ident)))),
1064 Pragma_Identifier =>
1065 Make_Identifier (Sloc (Ident), Chars (Ident)));
1067 Set_From_Aspect_Specification (Prag, True);
1068 Set_Corresponding_Aspect (Prag, ASN);
1069 Set_Aspect_Rep_Item (ASN, Prag);
1070 Set_Is_Delayed_Aspect (Prag);
1071 Set_Parent (Prag, ASN);
1073 end Make_Pragma_From_Boolean_Aspect;
1075 -- Start of processing for Analyze_Aspects_At_Freeze_Point
1078 -- Must be visible in current scope
1080 if not Scope_Within_Or_Same (Current_Scope, Scope (E)) then
1084 -- Look for aspect specification entries for this entity
1086 ASN := First_Rep_Item (E);
1087 while Present (ASN) loop
1088 if Nkind (ASN) = N_Aspect_Specification then
1089 exit when Entity (ASN) /= E;
1091 if Is_Delayed_Aspect (ASN) then
1092 A_Id := Get_Aspect_Id (ASN);
1096 -- For aspects whose expression is an optional Boolean, make
1097 -- the corresponding pragma at the freezing point.
1099 when Boolean_Aspects |
1100 Library_Unit_Aspects =>
1101 Make_Pragma_From_Boolean_Aspect (ASN);
1103 -- Special handling for aspects that don't correspond to
1104 -- pragmas/attributes.
1106 when Aspect_Default_Value |
1107 Aspect_Default_Component_Value =>
1108 Analyze_Aspect_Default_Value (ASN);
1110 -- Ditto for iterator aspects, because the corresponding
1111 -- attributes may not have been analyzed yet.
1113 when Aspect_Constant_Indexing |
1114 Aspect_Variable_Indexing |
1115 Aspect_Default_Iterator |
1116 Aspect_Iterator_Element =>
1117 Analyze (Expression (ASN));
1119 when Aspect_Iterable =>
1120 Validate_Iterable_Aspect (E, ASN);
1126 Ritem := Aspect_Rep_Item (ASN);
1128 if Present (Ritem) then
1134 Next_Rep_Item (ASN);
1137 -- This is where we inherit delayed rep aspects from our parent. Note
1138 -- that if we fell out of the above loop with ASN non-empty, it means
1139 -- we hit an aspect for an entity other than E, and it must be the
1140 -- type from which we were derived.
1142 if May_Inherit_Delayed_Rep_Aspects (E) then
1143 Inherit_Delayed_Rep_Aspects (ASN);
1145 end Analyze_Aspects_At_Freeze_Point;
1147 -----------------------------------
1148 -- Analyze_Aspect_Specifications --
1149 -----------------------------------
1151 procedure Analyze_Aspect_Specifications (N : Node_Id; E : Entity_Id) is
1152 procedure Decorate_Aspect_And_Pragma
1155 Delayed : Boolean := False);
1156 -- Establish the linkages between an aspect and its corresponding
1157 -- pragma. Flag Delayed should be set when both constructs are delayed.
1159 procedure Insert_After_SPARK_Mode
1163 -- Subsidiary to the analysis of aspects Abstract_State, Initializes and
1164 -- Initial_Condition. Insert node Prag before node Ins_Nod. If Ins_Nod
1165 -- denotes pragma SPARK_Mode, then SPARK_Mode is skipped. Decls is the
1166 -- associated declarative list where Prag is to reside.
1168 procedure Insert_Delayed_Pragma (Prag : Node_Id);
1169 -- Insert a postcondition-like pragma into the tree depending on the
1170 -- context. Prag must denote one of the following: Pre, Post, Depends,
1171 -- Global or Contract_Cases. This procedure is also used for the case
1172 -- of Attach_Handler which has similar requirements for placement.
1174 --------------------------------
1175 -- Decorate_Aspect_And_Pragma --
1176 --------------------------------
1178 procedure Decorate_Aspect_And_Pragma
1181 Delayed : Boolean := False)
1184 Set_Aspect_Rep_Item (Asp, Prag);
1185 Set_Corresponding_Aspect (Prag, Asp);
1186 Set_From_Aspect_Specification (Prag);
1187 Set_Is_Delayed_Aspect (Prag, Delayed);
1188 Set_Is_Delayed_Aspect (Asp, Delayed);
1189 Set_Parent (Prag, Asp);
1190 end Decorate_Aspect_And_Pragma;
1192 -----------------------------
1193 -- Insert_After_SPARK_Mode --
1194 -----------------------------
1196 procedure Insert_After_SPARK_Mode
1201 Decl : Node_Id := Ins_Nod;
1207 and then Nkind (Decl) = N_Pragma
1208 and then Pragma_Name (Decl) = Name_SPARK_Mode
1210 Decl := Next (Decl);
1213 if Present (Decl) then
1214 Insert_Before (Decl, Prag);
1216 -- Aitem acts as the last declaration
1219 Append_To (Decls, Prag);
1221 end Insert_After_SPARK_Mode;
1223 ---------------------------
1224 -- Insert_Delayed_Pragma --
1225 ---------------------------
1227 procedure Insert_Delayed_Pragma (Prag : Node_Id) is
1231 -- When the context is a library unit, the pragma is added to the
1232 -- Pragmas_After list.
1234 if Nkind (Parent (N)) = N_Compilation_Unit then
1235 Aux := Aux_Decls_Node (Parent (N));
1237 if No (Pragmas_After (Aux)) then
1238 Set_Pragmas_After (Aux, New_List);
1241 Prepend (Prag, Pragmas_After (Aux));
1243 -- Pragmas associated with subprogram bodies are inserted in the
1244 -- declarative part.
1246 elsif Nkind (N) = N_Subprogram_Body then
1247 if No (Declarations (N)) then
1248 Set_Declarations (N, New_List (Prag));
1254 -- There may be several aspects associated with the body;
1255 -- preserve the ordering of the corresponding pragmas.
1257 D := First (Declarations (N));
1258 while Present (D) loop
1259 exit when Nkind (D) /= N_Pragma
1260 or else not From_Aspect_Specification (D);
1265 Append (Prag, Declarations (N));
1267 Insert_Before (D, Prag);
1275 Insert_After (N, Prag);
1277 end Insert_Delayed_Pragma;
1285 L : constant List_Id := Aspect_Specifications (N);
1287 Ins_Node : Node_Id := N;
1288 -- Insert pragmas/attribute definition clause after this node when no
1289 -- delayed analysis is required.
1291 -- Start of processing for Analyze_Aspect_Specifications
1293 -- The general processing involves building an attribute definition
1294 -- clause or a pragma node that corresponds to the aspect. Then in order
1295 -- to delay the evaluation of this aspect to the freeze point, we attach
1296 -- the corresponding pragma/attribute definition clause to the aspect
1297 -- specification node, which is then placed in the Rep Item chain. In
1298 -- this case we mark the entity by setting the flag Has_Delayed_Aspects
1299 -- and we evaluate the rep item at the freeze point. When the aspect
1300 -- doesn't have a corresponding pragma/attribute definition clause, then
1301 -- its analysis is simply delayed at the freeze point.
1303 -- Some special cases don't require delay analysis, thus the aspect is
1304 -- analyzed right now.
1306 -- Note that there is a special handling for Pre, Post, Test_Case,
1307 -- Contract_Cases aspects. In these cases, we do not have to worry
1308 -- about delay issues, since the pragmas themselves deal with delay
1309 -- of visibility for the expression analysis. Thus, we just insert
1310 -- the pragma after the node N.
1313 pragma Assert (Present (L));
1315 -- Loop through aspects
1317 Aspect := First (L);
1318 Aspect_Loop : while Present (Aspect) loop
1319 Analyze_One_Aspect : declare
1320 Expr : constant Node_Id := Expression (Aspect);
1321 Id : constant Node_Id := Identifier (Aspect);
1322 Loc : constant Source_Ptr := Sloc (Aspect);
1323 Nam : constant Name_Id := Chars (Id);
1324 A_Id : constant Aspect_Id := Get_Aspect_Id (Nam);
1327 Delay_Required : Boolean;
1328 -- Set False if delay is not required
1330 Eloc : Source_Ptr := No_Location;
1331 -- Source location of expression, modified when we split PPC's. It
1332 -- is set below when Expr is present.
1334 procedure Analyze_Aspect_External_Or_Link_Name;
1335 -- Perform analysis of the External_Name or Link_Name aspects
1337 procedure Analyze_Aspect_Implicit_Dereference;
1338 -- Perform analysis of the Implicit_Dereference aspects
1340 procedure Make_Aitem_Pragma
1341 (Pragma_Argument_Associations : List_Id;
1342 Pragma_Name : Name_Id);
1343 -- This is a wrapper for Make_Pragma used for converting aspects
1344 -- to pragmas. It takes care of Sloc (set from Loc) and building
1345 -- the pragma identifier from the given name. In addition the
1346 -- flags Class_Present and Split_PPC are set from the aspect
1347 -- node, as well as Is_Ignored. This routine also sets the
1348 -- From_Aspect_Specification in the resulting pragma node to
1349 -- True, and sets Corresponding_Aspect to point to the aspect.
1350 -- The resulting pragma is assigned to Aitem.
1352 ------------------------------------------
1353 -- Analyze_Aspect_External_Or_Link_Name --
1354 ------------------------------------------
1356 procedure Analyze_Aspect_External_Or_Link_Name is
1358 -- Verify that there is an Import/Export aspect defined for the
1359 -- entity. The processing of that aspect in turn checks that
1360 -- there is a Convention aspect declared. The pragma is
1361 -- constructed when processing the Convention aspect.
1368 while Present (A) loop
1369 exit when Nam_In (Chars (Identifier (A)), Name_Export,
1376 ("missing Import/Export for Link/External name",
1380 end Analyze_Aspect_External_Or_Link_Name;
1382 -----------------------------------------
1383 -- Analyze_Aspect_Implicit_Dereference --
1384 -----------------------------------------
1386 procedure Analyze_Aspect_Implicit_Dereference is
1388 if not Is_Type (E) or else not Has_Discriminants (E) then
1390 ("aspect must apply to a type with discriminants", N);
1397 Disc := First_Discriminant (E);
1398 while Present (Disc) loop
1399 if Chars (Expr) = Chars (Disc)
1400 and then Ekind (Etype (Disc)) =
1401 E_Anonymous_Access_Type
1403 Set_Has_Implicit_Dereference (E);
1404 Set_Has_Implicit_Dereference (Disc);
1408 Next_Discriminant (Disc);
1411 -- Error if no proper access discriminant.
1414 ("not an access discriminant of&", Expr, E);
1417 end Analyze_Aspect_Implicit_Dereference;
1419 -----------------------
1420 -- Make_Aitem_Pragma --
1421 -----------------------
1423 procedure Make_Aitem_Pragma
1424 (Pragma_Argument_Associations : List_Id;
1425 Pragma_Name : Name_Id)
1427 Args : List_Id := Pragma_Argument_Associations;
1430 -- We should never get here if aspect was disabled
1432 pragma Assert (not Is_Disabled (Aspect));
1434 -- Certain aspects allow for an optional name or expression. Do
1435 -- not generate a pragma with empty argument association list.
1437 if No (Args) or else No (Expression (First (Args))) then
1445 Pragma_Argument_Associations => Args,
1446 Pragma_Identifier =>
1447 Make_Identifier (Sloc (Id), Pragma_Name),
1448 Class_Present => Class_Present (Aspect),
1449 Split_PPC => Split_PPC (Aspect));
1451 -- Set additional semantic fields
1453 if Is_Ignored (Aspect) then
1454 Set_Is_Ignored (Aitem);
1455 elsif Is_Checked (Aspect) then
1456 Set_Is_Checked (Aitem);
1459 Set_Corresponding_Aspect (Aitem, Aspect);
1460 Set_From_Aspect_Specification (Aitem, True);
1461 end Make_Aitem_Pragma;
1463 -- Start of processing for Analyze_One_Aspect
1466 -- Skip aspect if already analyzed (not clear if this is needed)
1468 if Analyzed (Aspect) then
1472 -- Skip looking at aspect if it is totally disabled. Just mark it
1473 -- as such for later reference in the tree. This also sets the
1474 -- Is_Ignored and Is_Checked flags appropriately.
1476 Check_Applicable_Policy (Aspect);
1478 if Is_Disabled (Aspect) then
1482 -- Set the source location of expression, used in the case of
1483 -- a failed precondition/postcondition or invariant. Note that
1484 -- the source location of the expression is not usually the best
1485 -- choice here. For example, it gets located on the last AND
1486 -- keyword in a chain of boolean expressiond AND'ed together.
1487 -- It is best to put the message on the first character of the
1488 -- assertion, which is the effect of the First_Node call here.
1490 if Present (Expr) then
1491 Eloc := Sloc (First_Node (Expr));
1494 -- Check restriction No_Implementation_Aspect_Specifications
1496 if Implementation_Defined_Aspect (A_Id) then
1498 (No_Implementation_Aspect_Specifications, Aspect);
1501 -- Check restriction No_Specification_Of_Aspect
1503 Check_Restriction_No_Specification_Of_Aspect (Aspect);
1505 -- Mark aspect analyzed (actual analysis is delayed till later)
1507 Set_Analyzed (Aspect);
1508 Set_Entity (Aspect, E);
1509 Ent := New_Occurrence_Of (E, Sloc (Id));
1511 -- Check for duplicate aspect. Note that the Comes_From_Source
1512 -- test allows duplicate Pre/Post's that we generate internally
1513 -- to escape being flagged here.
1515 if No_Duplicates_Allowed (A_Id) then
1517 while Anod /= Aspect loop
1518 if Comes_From_Source (Aspect)
1519 and then Same_Aspect (A_Id, Get_Aspect_Id (Anod))
1521 Error_Msg_Name_1 := Nam;
1522 Error_Msg_Sloc := Sloc (Anod);
1524 -- Case of same aspect specified twice
1526 if Class_Present (Anod) = Class_Present (Aspect) then
1527 if not Class_Present (Anod) then
1529 ("aspect% for & previously given#",
1533 ("aspect `%''Class` for & previously given#",
1543 -- Check some general restrictions on language defined aspects
1545 if not Implementation_Defined_Aspect (A_Id) then
1546 Error_Msg_Name_1 := Nam;
1548 -- Not allowed for renaming declarations
1550 if Nkind (N) in N_Renaming_Declaration then
1552 ("aspect % not allowed for renaming declaration",
1556 -- Not allowed for formal type declarations
1558 if Nkind (N) = N_Formal_Type_Declaration then
1560 ("aspect % not allowed for formal type declaration",
1565 -- Copy expression for later processing by the procedures
1566 -- Check_Aspect_At_[Freeze_Point | End_Of_Declarations]
1568 Set_Entity (Id, New_Copy_Tree (Expr));
1570 -- Set Delay_Required as appropriate to aspect
1572 case Aspect_Delay (A_Id) is
1573 when Always_Delay =>
1574 Delay_Required := True;
1577 Delay_Required := False;
1581 -- If expression has the form of an integer literal, then
1582 -- do not delay, since we know the value cannot change.
1583 -- This optimization catches most rep clause cases.
1585 if (Present (Expr) and then Nkind (Expr) = N_Integer_Literal)
1586 or else (A_Id in Boolean_Aspects and then No (Expr))
1588 Delay_Required := False;
1590 Delay_Required := True;
1591 Set_Has_Delayed_Rep_Aspects (E);
1595 -- Processing based on specific aspect
1599 -- No_Aspect should be impossible
1602 raise Program_Error;
1604 -- Case 1: Aspects corresponding to attribute definition
1607 when Aspect_Address |
1610 Aspect_Component_Size |
1611 Aspect_Constant_Indexing |
1612 Aspect_Default_Iterator |
1613 Aspect_Dispatching_Domain |
1614 Aspect_External_Tag |
1617 Aspect_Iterator_Element |
1618 Aspect_Machine_Radix |
1619 Aspect_Object_Size |
1622 Aspect_Scalar_Storage_Order |
1625 Aspect_Simple_Storage_Pool |
1626 Aspect_Storage_Pool |
1627 Aspect_Stream_Size |
1629 Aspect_Variable_Indexing |
1632 -- Indexing aspects apply only to tagged type
1634 if (A_Id = Aspect_Constant_Indexing
1636 A_Id = Aspect_Variable_Indexing)
1637 and then not (Is_Type (E)
1638 and then Is_Tagged_Type (E))
1640 Error_Msg_N ("indexing applies to a tagged type", N);
1644 -- For the case of aspect Address, we don't consider that we
1645 -- know the entity is never set in the source, since it is
1646 -- is likely aliasing is occurring.
1648 -- Note: one might think that the analysis of the resulting
1649 -- attribute definition clause would take care of that, but
1650 -- that's not the case since it won't be from source.
1652 if A_Id = Aspect_Address then
1653 Set_Never_Set_In_Source (E, False);
1656 -- Construct the attribute definition clause
1659 Make_Attribute_Definition_Clause (Loc,
1661 Chars => Chars (Id),
1662 Expression => Relocate_Node (Expr));
1664 -- If the address is specified, then we treat the entity as
1665 -- referenced, to avoid spurious warnings. This is analogous
1666 -- to what is done with an attribute definition clause, but
1667 -- here we don't want to generate a reference because this
1668 -- is the point of definition of the entity.
1670 if A_Id = Aspect_Address then
1674 -- Case 2: Aspects corresponding to pragmas
1676 -- Case 2a: Aspects corresponding to pragmas with two
1677 -- arguments, where the first argument is a local name
1678 -- referring to the entity, and the second argument is the
1679 -- aspect definition expression.
1681 -- Linker_Section/Suppress/Unsuppress
1683 when Aspect_Linker_Section |
1685 Aspect_Unsuppress =>
1688 (Pragma_Argument_Associations => New_List (
1689 Make_Pragma_Argument_Association (Loc,
1690 Expression => New_Occurrence_Of (E, Loc)),
1691 Make_Pragma_Argument_Association (Sloc (Expr),
1692 Expression => Relocate_Node (Expr))),
1693 Pragma_Name => Chars (Id));
1697 -- Corresponds to pragma Implemented, construct the pragma
1699 when Aspect_Synchronization =>
1701 (Pragma_Argument_Associations => New_List (
1702 Make_Pragma_Argument_Association (Loc,
1703 Expression => New_Occurrence_Of (E, Loc)),
1704 Make_Pragma_Argument_Association (Sloc (Expr),
1705 Expression => Relocate_Node (Expr))),
1706 Pragma_Name => Name_Implemented);
1710 when Aspect_Attach_Handler =>
1712 (Pragma_Argument_Associations => New_List (
1713 Make_Pragma_Argument_Association (Sloc (Ent),
1715 Make_Pragma_Argument_Association (Sloc (Expr),
1716 Expression => Relocate_Node (Expr))),
1717 Pragma_Name => Name_Attach_Handler);
1719 -- We need to insert this pragma into the tree to get proper
1720 -- processing and to look valid from a placement viewpoint.
1722 Insert_Delayed_Pragma (Aitem);
1725 -- Dynamic_Predicate, Predicate, Static_Predicate
1727 when Aspect_Dynamic_Predicate |
1729 Aspect_Static_Predicate =>
1731 -- Construct the pragma (always a pragma Predicate, with
1732 -- flags recording whether it is static/dynamic). We also
1733 -- set flags recording this in the type itself.
1736 (Pragma_Argument_Associations => New_List (
1737 Make_Pragma_Argument_Association (Sloc (Ent),
1739 Make_Pragma_Argument_Association (Sloc (Expr),
1740 Expression => Relocate_Node (Expr))),
1741 Pragma_Name => Name_Predicate);
1743 -- Mark type has predicates, and remember what kind of
1744 -- aspect lead to this predicate (we need this to access
1745 -- the right set of check policies later on).
1747 Set_Has_Predicates (E);
1749 if A_Id = Aspect_Dynamic_Predicate then
1750 Set_Has_Dynamic_Predicate_Aspect (E);
1751 elsif A_Id = Aspect_Static_Predicate then
1752 Set_Has_Static_Predicate_Aspect (E);
1755 -- If the type is private, indicate that its completion
1756 -- has a freeze node, because that is the one that will
1757 -- be visible at freeze time.
1759 if Is_Private_Type (E) and then Present (Full_View (E)) then
1760 Set_Has_Predicates (Full_View (E));
1762 if A_Id = Aspect_Dynamic_Predicate then
1763 Set_Has_Dynamic_Predicate_Aspect (Full_View (E));
1764 elsif A_Id = Aspect_Static_Predicate then
1765 Set_Has_Static_Predicate_Aspect (Full_View (E));
1768 Set_Has_Delayed_Aspects (Full_View (E));
1769 Ensure_Freeze_Node (Full_View (E));
1772 -- Case 2b: Aspects corresponding to pragmas with two
1773 -- arguments, where the second argument is a local name
1774 -- referring to the entity, and the first argument is the
1775 -- aspect definition expression.
1779 when Aspect_Convention =>
1781 -- The aspect may be part of the specification of an import
1782 -- or export pragma. Scan the aspect list to gather the
1783 -- other components, if any. The name of the generated
1784 -- pragma is one of Convention/Import/Export.
1796 P_Name := Chars (Id);
1798 Arg_List := New_List;
1803 while Present (A) loop
1804 A_Name := Chars (Identifier (A));
1806 if Nam_In (A_Name, Name_Import, Name_Export) then
1808 Error_Msg_N ("conflicting", A);
1815 elsif A_Name = Name_Link_Name then
1817 Make_Pragma_Argument_Association (Loc,
1819 Expression => Relocate_Node (Expression (A)));
1821 elsif A_Name = Name_External_Name then
1823 Make_Pragma_Argument_Association (Loc,
1825 Expression => Relocate_Node (Expression (A)));
1831 Arg_List := New_List (
1832 Make_Pragma_Argument_Association (Sloc (Expr),
1833 Expression => Relocate_Node (Expr)),
1834 Make_Pragma_Argument_Association (Sloc (Ent),
1835 Expression => Ent));
1837 if Present (L_Assoc) then
1838 Append_To (Arg_List, L_Assoc);
1841 if Present (E_Assoc) then
1842 Append_To (Arg_List, E_Assoc);
1846 (Pragma_Argument_Associations => Arg_List,
1847 Pragma_Name => P_Name);
1850 -- CPU, Interrupt_Priority, Priority
1852 -- These three aspects can be specified for a subprogram spec
1853 -- or body, in which case we analyze the expression and export
1854 -- the value of the aspect.
1856 -- Previously, we generated an equivalent pragma for bodies
1857 -- (note that the specs cannot contain these pragmas). The
1858 -- pragma was inserted ahead of local declarations, rather than
1859 -- after the body. This leads to a certain duplication between
1860 -- the processing performed for the aspect and the pragma, but
1861 -- given the straightforward handling required it is simpler
1862 -- to duplicate than to translate the aspect in the spec into
1863 -- a pragma in the declarative part of the body.
1866 Aspect_Interrupt_Priority |
1869 if Nkind_In (N, N_Subprogram_Body,
1870 N_Subprogram_Declaration)
1872 -- Analyze the aspect expression
1874 Analyze_And_Resolve (Expr, Standard_Integer);
1876 -- Interrupt_Priority aspect not allowed for main
1877 -- subprograms. ARM D.1 does not forbid this explicitly,
1878 -- but ARM J.15.11 (6/3) does not permit pragma
1879 -- Interrupt_Priority for subprograms.
1881 if A_Id = Aspect_Interrupt_Priority then
1883 ("Interrupt_Priority aspect cannot apply to "
1884 & "subprogram", Expr);
1886 -- The expression must be static
1888 elsif not Is_Static_Expression (Expr) then
1889 Flag_Non_Static_Expr
1890 ("aspect requires static expression!", Expr);
1892 -- Check whether this is the main subprogram. Issue a
1893 -- warning only if it is obviously not a main program
1894 -- (when it has parameters or when the subprogram is
1895 -- within a package).
1897 elsif Present (Parameter_Specifications
1898 (Specification (N)))
1899 or else not Is_Compilation_Unit (Defining_Entity (N))
1901 -- See ARM D.1 (14/3) and D.16 (12/3)
1904 ("aspect applied to subprogram other than the "
1905 & "main subprogram has no effect??", Expr);
1907 -- Otherwise check in range and export the value
1909 -- For the CPU aspect
1911 elsif A_Id = Aspect_CPU then
1912 if Is_In_Range (Expr, RTE (RE_CPU_Range)) then
1914 -- Value is correct so we export the value to make
1915 -- it available at execution time.
1918 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
1922 ("main subprogram CPU is out of range", Expr);
1925 -- For the Priority aspect
1927 elsif A_Id = Aspect_Priority then
1928 if Is_In_Range (Expr, RTE (RE_Priority)) then
1930 -- Value is correct so we export the value to make
1931 -- it available at execution time.
1934 (Main_Unit, UI_To_Int (Expr_Value (Expr)));
1936 -- Ignore pragma if Relaxed_RM_Semantics to support
1937 -- other targets/non GNAT compilers.
1939 elsif not Relaxed_RM_Semantics then
1941 ("main subprogram priority is out of range",
1946 -- Load an arbitrary entity from System.Tasking.Stages
1947 -- or System.Tasking.Restricted.Stages (depending on
1948 -- the supported profile) to make sure that one of these
1949 -- packages is implicitly with'ed, since we need to have
1950 -- the tasking run time active for the pragma Priority to
1951 -- have any effect. Previously with with'ed the package
1952 -- System.Tasking, but this package does not trigger the
1953 -- required initialization of the run-time library.
1956 Discard : Entity_Id;
1957 pragma Warnings (Off, Discard);
1959 if Restricted_Profile then
1960 Discard := RTE (RE_Activate_Restricted_Tasks);
1962 Discard := RTE (RE_Activate_Tasks);
1966 -- Handling for these Aspects in subprograms is complete
1973 -- Pass the aspect as an attribute
1976 Make_Attribute_Definition_Clause (Loc,
1978 Chars => Chars (Id),
1979 Expression => Relocate_Node (Expr));
1984 when Aspect_Warnings =>
1986 (Pragma_Argument_Associations => New_List (
1987 Make_Pragma_Argument_Association (Sloc (Expr),
1988 Expression => Relocate_Node (Expr)),
1989 Make_Pragma_Argument_Association (Loc,
1990 Expression => New_Occurrence_Of (E, Loc))),
1991 Pragma_Name => Chars (Id));
1993 -- Case 2c: Aspects corresponding to pragmas with three
1996 -- Invariant aspects have a first argument that references the
1997 -- entity, a second argument that is the expression and a third
1998 -- argument that is an appropriate message.
2000 -- Invariant, Type_Invariant
2002 when Aspect_Invariant |
2003 Aspect_Type_Invariant =>
2005 -- Analysis of the pragma will verify placement legality:
2006 -- an invariant must apply to a private type, or appear in
2007 -- the private part of a spec and apply to a completion.
2010 (Pragma_Argument_Associations => New_List (
2011 Make_Pragma_Argument_Association (Sloc (Ent),
2013 Make_Pragma_Argument_Association (Sloc (Expr),
2014 Expression => Relocate_Node (Expr))),
2015 Pragma_Name => Name_Invariant);
2017 -- Add message unless exception messages are suppressed
2019 if not Opt.Exception_Locations_Suppressed then
2020 Append_To (Pragma_Argument_Associations (Aitem),
2021 Make_Pragma_Argument_Association (Eloc,
2022 Chars => Name_Message,
2024 Make_String_Literal (Eloc,
2025 Strval => "failed invariant from "
2026 & Build_Location_String (Eloc))));
2029 -- For Invariant case, insert immediately after the entity
2030 -- declaration. We do not have to worry about delay issues
2031 -- since the pragma processing takes care of this.
2033 Delay_Required := False;
2035 -- Case 2d : Aspects that correspond to a pragma with one
2040 -- Aspect Abstract_State introduces implicit declarations for
2041 -- all state abstraction entities it defines. To emulate this
2042 -- behavior, insert the pragma at the beginning of the visible
2043 -- declarations of the related package so that it is analyzed
2046 when Aspect_Abstract_State => Abstract_State : declare
2047 Context : Node_Id := N;
2052 -- When aspect Abstract_State appears on a generic package,
2053 -- it is propageted to the package instance. The context in
2054 -- this case is the instance spec.
2056 if Nkind (Context) = N_Package_Instantiation then
2057 Context := Instance_Spec (Context);
2060 if Nkind_In (Context, N_Generic_Package_Declaration,
2061 N_Package_Declaration)
2064 (Pragma_Argument_Associations => New_List (
2065 Make_Pragma_Argument_Association (Loc,
2066 Expression => Relocate_Node (Expr))),
2067 Pragma_Name => Name_Abstract_State);
2068 Decorate_Aspect_And_Pragma (Aspect, Aitem);
2070 Decls := Visible_Declarations (Specification (Context));
2072 -- In general pragma Abstract_State must be at the top
2073 -- of the existing visible declarations to emulate its
2074 -- source counterpart. The only exception to this is a
2075 -- generic instance in which case the pragma must be
2076 -- inserted after the association renamings.
2078 if Present (Decls) then
2079 Decl := First (Decls);
2081 -- The visible declarations of a generic instance have
2082 -- the following structure:
2084 -- <renamings of generic formals>
2085 -- <renamings of internally-generated spec and body>
2086 -- <first source declaration>
2088 -- The pragma must be inserted before the first source
2089 -- declaration, skip the instance "header".
2091 if Is_Generic_Instance (Defining_Entity (Context)) then
2092 while Present (Decl)
2093 and then not Comes_From_Source (Decl)
2095 Decl := Next (Decl);
2099 -- Pragma Abstract_State must be inserted after pragma
2100 -- SPARK_Mode in the tree. This ensures that any error
2101 -- messages dependent on SPARK_Mode will be properly
2102 -- enabled/suppressed.
2104 Insert_After_SPARK_Mode
2109 -- Otherwise the pragma forms a new declarative list
2112 Set_Visible_Declarations
2113 (Specification (Context), New_List (Aitem));
2118 ("aspect & must apply to a package declaration",
2127 -- Aspect Depends must be delayed because it mentions names
2128 -- of inputs and output that are classified by aspect Global.
2129 -- The aspect and pragma are treated the same way as a post
2132 when Aspect_Depends =>
2134 (Pragma_Argument_Associations => New_List (
2135 Make_Pragma_Argument_Association (Loc,
2136 Expression => Relocate_Node (Expr))),
2137 Pragma_Name => Name_Depends);
2139 Decorate_Aspect_And_Pragma
2140 (Aspect, Aitem, Delayed => True);
2141 Insert_Delayed_Pragma (Aitem);
2146 -- Aspect Global must be delayed because it can mention names
2147 -- and benefit from the forward visibility rules applicable to
2148 -- aspects of subprograms. The aspect and pragma are treated
2149 -- the same way as a post condition.
2151 when Aspect_Global =>
2153 (Pragma_Argument_Associations => New_List (
2154 Make_Pragma_Argument_Association (Loc,
2155 Expression => Relocate_Node (Expr))),
2156 Pragma_Name => Name_Global);
2158 Decorate_Aspect_And_Pragma
2159 (Aspect, Aitem, Delayed => True);
2160 Insert_Delayed_Pragma (Aitem);
2163 -- Initial_Condition
2165 -- Aspect Initial_Condition covers the visible declarations of
2166 -- a package and all hidden states through functions. As such,
2167 -- it must be evaluated at the end of the said declarations.
2169 when Aspect_Initial_Condition => Initial_Condition : declare
2170 Context : Node_Id := N;
2174 -- When aspect Abstract_State appears on a generic package,
2175 -- it is propageted to the package instance. The context in
2176 -- this case is the instance spec.
2178 if Nkind (Context) = N_Package_Instantiation then
2179 Context := Instance_Spec (Context);
2182 if Nkind_In (Context, N_Generic_Package_Declaration,
2183 N_Package_Declaration)
2185 Decls := Visible_Declarations (Specification (Context));
2188 (Pragma_Argument_Associations => New_List (
2189 Make_Pragma_Argument_Association (Loc,
2190 Expression => Relocate_Node (Expr))),
2192 Name_Initial_Condition);
2194 Decorate_Aspect_And_Pragma
2195 (Aspect, Aitem, Delayed => True);
2199 Set_Visible_Declarations (Context, Decls);
2202 -- When aspects Abstract_State, Initial_Condition and
2203 -- Initializes are out of order, ensure that pragma
2204 -- SPARK_Mode is always at the top of the declarative
2205 -- list to properly enable/suppress errors.
2207 Insert_After_SPARK_Mode
2209 Ins_Nod => First (Decls),
2214 ("aspect & must apply to a package declaration",
2219 end Initial_Condition;
2223 -- Aspect Initializes coverts the visible declarations of a
2224 -- package. As such, it must be evaluated at the end of the
2225 -- said declarations.
2227 when Aspect_Initializes => Initializes : declare
2228 Context : Node_Id := N;
2232 -- When aspect Initializes appears on a generic package,
2233 -- it is propageted to the package instance. The context
2234 -- in this case is the instance spec.
2236 if Nkind (Context) = N_Package_Instantiation then
2237 Context := Instance_Spec (Context);
2240 if Nkind_In (Context, N_Generic_Package_Declaration,
2241 N_Package_Declaration)
2243 Decls := Visible_Declarations (Specification (Context));
2246 (Pragma_Argument_Associations => New_List (
2247 Make_Pragma_Argument_Association (Loc,
2248 Expression => Relocate_Node (Expr))),
2249 Pragma_Name => Name_Initializes);
2251 Decorate_Aspect_And_Pragma
2252 (Aspect, Aitem, Delayed => True);
2256 Set_Visible_Declarations (Context, Decls);
2259 -- When aspects Abstract_State, Initial_Condition and
2260 -- Initializes are out of order, ensure that pragma
2261 -- SPARK_Mode is always at the top of the declarative
2262 -- list to properly enable/suppress errors.
2264 Insert_After_SPARK_Mode
2266 Ins_Nod => First (Decls),
2271 ("aspect & must apply to a package declaration",
2280 when Aspect_Part_Of =>
2281 if Nkind_In (N, N_Object_Declaration,
2282 N_Package_Instantiation)
2285 (Pragma_Argument_Associations => New_List (
2286 Make_Pragma_Argument_Association (Loc,
2287 Expression => Relocate_Node (Expr))),
2288 Pragma_Name => Name_Part_Of);
2292 ("aspect & must apply to a variable or package "
2293 & "instantiation", Aspect, Id);
2298 when Aspect_SPARK_Mode => SPARK_Mode : declare
2303 (Pragma_Argument_Associations => New_List (
2304 Make_Pragma_Argument_Association (Loc,
2305 Expression => Relocate_Node (Expr))),
2306 Pragma_Name => Name_SPARK_Mode);
2308 -- When the aspect appears on a package body, insert the
2309 -- generated pragma at the top of the body declarations to
2310 -- emulate the behavior of a source pragma.
2312 if Nkind (N) = N_Package_Body then
2313 Decorate_Aspect_And_Pragma (Aspect, Aitem);
2315 Decls := Declarations (N);
2319 Set_Declarations (N, Decls);
2322 Prepend_To (Decls, Aitem);
2325 -- When the aspect is associated with package declaration,
2326 -- insert the generated pragma at the top of the visible
2327 -- declarations to emulate the behavior of a source pragma.
2329 elsif Nkind (N) = N_Package_Declaration then
2330 Decorate_Aspect_And_Pragma (Aspect, Aitem);
2332 Decls := Visible_Declarations (Specification (N));
2336 Set_Visible_Declarations (Specification (N), Decls);
2339 Prepend_To (Decls, Aitem);
2346 -- Aspect Refined_Depends must be delayed because it can
2347 -- mention state refinements introduced by aspect Refined_State
2348 -- and further classified by aspect Refined_Global. Since both
2349 -- those aspects are delayed, so is Refined_Depends.
2351 when Aspect_Refined_Depends =>
2353 (Pragma_Argument_Associations => New_List (
2354 Make_Pragma_Argument_Association (Loc,
2355 Expression => Relocate_Node (Expr))),
2356 Pragma_Name => Name_Refined_Depends);
2358 Decorate_Aspect_And_Pragma
2359 (Aspect, Aitem, Delayed => True);
2360 Insert_Delayed_Pragma (Aitem);
2365 -- Aspect Refined_Global must be delayed because it can mention
2366 -- state refinements introduced by aspect Refined_State. Since
2367 -- Refined_State is already delayed due to forward references,
2368 -- so is Refined_Global.
2370 when Aspect_Refined_Global =>
2372 (Pragma_Argument_Associations => New_List (
2373 Make_Pragma_Argument_Association (Loc,
2374 Expression => Relocate_Node (Expr))),
2375 Pragma_Name => Name_Refined_Global);
2377 Decorate_Aspect_And_Pragma (Aspect, Aitem, Delayed => True);
2378 Insert_Delayed_Pragma (Aitem);
2383 when Aspect_Refined_Post =>
2385 (Pragma_Argument_Associations => New_List (
2386 Make_Pragma_Argument_Association (Loc,
2387 Expression => Relocate_Node (Expr))),
2388 Pragma_Name => Name_Refined_Post);
2392 when Aspect_Refined_State => Refined_State : declare
2397 -- The corresponding pragma for Refined_State is inserted in
2398 -- the declarations of the related package body. This action
2399 -- synchronizes both the source and from-aspect versions of
2402 if Nkind (N) = N_Package_Body then
2404 (Pragma_Argument_Associations => New_List (
2405 Make_Pragma_Argument_Association (Loc,
2406 Expression => Relocate_Node (Expr))),
2407 Pragma_Name => Name_Refined_State);
2408 Decorate_Aspect_And_Pragma (Aspect, Aitem);
2410 Decls := Declarations (N);
2412 -- When the package body is subject to pragma SPARK_Mode,
2413 -- insert pragma Refined_State after SPARK_Mode.
2415 if Present (Decls) then
2416 Decl := First (Decls);
2418 if Nkind (Decl) = N_Pragma
2419 and then Pragma_Name (Decl) = Name_SPARK_Mode
2421 Insert_After (Decl, Aitem);
2423 -- The related package body lacks SPARK_Mode, the
2424 -- corresponding pragma must be the first declaration.
2427 Prepend_To (Decls, Aitem);
2430 -- Otherwise the pragma forms a new declarative list
2433 Set_Declarations (N, New_List (Aitem));
2438 ("aspect & must apply to a package body", Aspect, Id);
2444 -- Relative_Deadline
2446 when Aspect_Relative_Deadline =>
2448 (Pragma_Argument_Associations => New_List (
2449 Make_Pragma_Argument_Association (Loc,
2450 Expression => Relocate_Node (Expr))),
2451 Pragma_Name => Name_Relative_Deadline);
2453 -- If the aspect applies to a task, the corresponding pragma
2454 -- must appear within its declarations, not after.
2456 if Nkind (N) = N_Task_Type_Declaration then
2462 if No (Task_Definition (N)) then
2463 Set_Task_Definition (N,
2464 Make_Task_Definition (Loc,
2465 Visible_Declarations => New_List,
2466 End_Label => Empty));
2469 Def := Task_Definition (N);
2470 V := Visible_Declarations (Def);
2471 if not Is_Empty_List (V) then
2472 Insert_Before (First (V), Aitem);
2475 Set_Visible_Declarations (Def, New_List (Aitem));
2482 -- Case 2e: Annotate aspect
2484 when Aspect_Annotate =>
2491 -- The argument can be a single identifier
2493 if Nkind (Expr) = N_Identifier then
2495 -- One level of parens is allowed
2497 if Paren_Count (Expr) > 1 then
2498 Error_Msg_F ("extra parentheses ignored", Expr);
2501 Set_Paren_Count (Expr, 0);
2503 -- Add the single item to the list
2505 Args := New_List (Expr);
2507 -- Otherwise we must have an aggregate
2509 elsif Nkind (Expr) = N_Aggregate then
2511 -- Must be positional
2513 if Present (Component_Associations (Expr)) then
2515 ("purely positional aggregate required", Expr);
2519 -- Must not be parenthesized
2521 if Paren_Count (Expr) /= 0 then
2522 Error_Msg_F ("extra parentheses ignored", Expr);
2525 -- List of arguments is list of aggregate expressions
2527 Args := Expressions (Expr);
2529 -- Anything else is illegal
2532 Error_Msg_F ("wrong form for Annotate aspect", Expr);
2536 -- Prepare pragma arguments
2539 Arg := First (Args);
2540 while Present (Arg) loop
2542 Make_Pragma_Argument_Association (Sloc (Arg),
2543 Expression => Relocate_Node (Arg)));
2548 Make_Pragma_Argument_Association (Sloc (Ent),
2549 Chars => Name_Entity,
2550 Expression => Ent));
2553 (Pragma_Argument_Associations => Pargs,
2554 Pragma_Name => Name_Annotate);
2557 -- Case 3 : Aspects that don't correspond to pragma/attribute
2558 -- definition clause.
2560 -- Case 3a: The aspects listed below don't correspond to
2561 -- pragmas/attributes but do require delayed analysis.
2563 -- Default_Value, Default_Component_Value
2565 when Aspect_Default_Value |
2566 Aspect_Default_Component_Value =>
2569 -- Case 3b: The aspects listed below don't correspond to
2570 -- pragmas/attributes and don't need delayed analysis.
2572 -- Implicit_Dereference
2574 -- For Implicit_Dereference, External_Name and Link_Name, only
2575 -- the legality checks are done during the analysis, thus no
2576 -- delay is required.
2578 when Aspect_Implicit_Dereference =>
2579 Analyze_Aspect_Implicit_Dereference;
2582 -- External_Name, Link_Name
2584 when Aspect_External_Name |
2586 Analyze_Aspect_External_Or_Link_Name;
2591 when Aspect_Dimension =>
2592 Analyze_Aspect_Dimension (N, Id, Expr);
2597 when Aspect_Dimension_System =>
2598 Analyze_Aspect_Dimension_System (N, Id, Expr);
2601 -- Case 4: Aspects requiring special handling
2603 -- Pre/Post/Test_Case/Contract_Cases whose corresponding
2604 -- pragmas take care of the delay.
2608 -- Aspects Pre/Post generate Precondition/Postcondition pragmas
2609 -- with a first argument that is the expression, and a second
2610 -- argument that is an informative message if the test fails.
2611 -- This is inserted right after the declaration, to get the
2612 -- required pragma placement. The processing for the pragmas
2613 -- takes care of the required delay.
2615 when Pre_Post_Aspects => Pre_Post : declare
2619 if A_Id = Aspect_Pre or else A_Id = Aspect_Precondition then
2620 Pname := Name_Precondition;
2622 Pname := Name_Postcondition;
2625 -- If the expressions is of the form A and then B, then
2626 -- we generate separate Pre/Post aspects for the separate
2627 -- clauses. Since we allow multiple pragmas, there is no
2628 -- problem in allowing multiple Pre/Post aspects internally.
2629 -- These should be treated in reverse order (B first and
2630 -- A second) since they are later inserted just after N in
2631 -- the order they are treated. This way, the pragma for A
2632 -- ends up preceding the pragma for B, which may have an
2633 -- importance for the error raised (either constraint error
2634 -- or precondition error).
2636 -- We do not do this for Pre'Class, since we have to put
2637 -- these conditions together in a complex OR expression
2639 -- We do not do this in ASIS mode, as ASIS relies on the
2640 -- original node representing the complete expression, when
2641 -- retrieving it through the source aspect table.
2644 and then (Pname = Name_Postcondition
2645 or else not Class_Present (Aspect))
2647 while Nkind (Expr) = N_And_Then loop
2648 Insert_After (Aspect,
2649 Make_Aspect_Specification (Sloc (Left_Opnd (Expr)),
2650 Identifier => Identifier (Aspect),
2651 Expression => Relocate_Node (Left_Opnd (Expr)),
2652 Class_Present => Class_Present (Aspect),
2653 Split_PPC => True));
2654 Rewrite (Expr, Relocate_Node (Right_Opnd (Expr)));
2655 Eloc := Sloc (Expr);
2659 -- Build the precondition/postcondition pragma
2661 -- Add note about why we do NOT need Copy_Tree here ???
2664 (Pragma_Argument_Associations => New_List (
2665 Make_Pragma_Argument_Association (Eloc,
2666 Chars => Name_Check,
2667 Expression => Relocate_Node (Expr))),
2668 Pragma_Name => Pname);
2670 -- Add message unless exception messages are suppressed
2672 if not Opt.Exception_Locations_Suppressed then
2673 Append_To (Pragma_Argument_Associations (Aitem),
2674 Make_Pragma_Argument_Association (Eloc,
2675 Chars => Name_Message,
2677 Make_String_Literal (Eloc,
2679 & Get_Name_String (Pname)
2681 & Build_Location_String (Eloc))));
2684 Set_Is_Delayed_Aspect (Aspect);
2686 -- For Pre/Post cases, insert immediately after the entity
2687 -- declaration, since that is the required pragma placement.
2688 -- Note that for these aspects, we do not have to worry
2689 -- about delay issues, since the pragmas themselves deal
2690 -- with delay of visibility for the expression analysis.
2692 Insert_Delayed_Pragma (Aitem);
2698 when Aspect_Test_Case => Test_Case : declare
2700 Comp_Expr : Node_Id;
2701 Comp_Assn : Node_Id;
2707 if Nkind (Parent (N)) = N_Compilation_Unit then
2708 Error_Msg_Name_1 := Nam;
2709 Error_Msg_N ("incorrect placement of aspect `%`", E);
2713 if Nkind (Expr) /= N_Aggregate then
2714 Error_Msg_Name_1 := Nam;
2716 ("wrong syntax for aspect `%` for &", Id, E);
2720 -- Make pragma expressions refer to the original aspect
2721 -- expressions through the Original_Node link. This is
2722 -- used in semantic analysis for ASIS mode, so that the
2723 -- original expression also gets analyzed.
2725 Comp_Expr := First (Expressions (Expr));
2726 while Present (Comp_Expr) loop
2727 New_Expr := Relocate_Node (Comp_Expr);
2728 Set_Original_Node (New_Expr, Comp_Expr);
2730 Make_Pragma_Argument_Association (Sloc (Comp_Expr),
2731 Expression => New_Expr));
2735 Comp_Assn := First (Component_Associations (Expr));
2736 while Present (Comp_Assn) loop
2737 if List_Length (Choices (Comp_Assn)) /= 1
2739 Nkind (First (Choices (Comp_Assn))) /= N_Identifier
2741 Error_Msg_Name_1 := Nam;
2743 ("wrong syntax for aspect `%` for &", Id, E);
2747 New_Expr := Relocate_Node (Expression (Comp_Assn));
2748 Set_Original_Node (New_Expr, Expression (Comp_Assn));
2750 Make_Pragma_Argument_Association (Sloc (Comp_Assn),
2751 Chars => Chars (First (Choices (Comp_Assn))),
2752 Expression => New_Expr));
2756 -- Build the test-case pragma
2759 (Pragma_Argument_Associations => Args,
2760 Pragma_Name => Nam);
2765 when Aspect_Contract_Cases =>
2767 (Pragma_Argument_Associations => New_List (
2768 Make_Pragma_Argument_Association (Loc,
2769 Expression => Relocate_Node (Expr))),
2770 Pragma_Name => Nam);
2772 Decorate_Aspect_And_Pragma
2773 (Aspect, Aitem, Delayed => True);
2774 Insert_Delayed_Pragma (Aitem);
2777 -- Case 5: Special handling for aspects with an optional
2778 -- boolean argument.
2780 -- In the general case, the corresponding pragma cannot be
2781 -- generated yet because the evaluation of the boolean needs
2782 -- to be delayed till the freeze point.
2784 when Boolean_Aspects |
2785 Library_Unit_Aspects =>
2787 Set_Is_Boolean_Aspect (Aspect);
2789 -- Lock_Free aspect only apply to protected objects
2791 if A_Id = Aspect_Lock_Free then
2792 if Ekind (E) /= E_Protected_Type then
2793 Error_Msg_Name_1 := Nam;
2795 ("aspect % only applies to a protected object",
2799 -- Set the Uses_Lock_Free flag to True if there is no
2800 -- expression or if the expression is True. The
2801 -- evaluation of this aspect should be delayed to the
2802 -- freeze point (why???)
2805 or else Is_True (Static_Boolean (Expr))
2807 Set_Uses_Lock_Free (E);
2810 Record_Rep_Item (E, Aspect);
2815 elsif A_Id = Aspect_Import or else A_Id = Aspect_Export then
2817 -- For the case of aspects Import and Export, we don't
2818 -- consider that we know the entity is never set in the
2819 -- source, since it is is likely modified outside the
2822 -- Note: one might think that the analysis of the
2823 -- resulting pragma would take care of that, but
2824 -- that's not the case since it won't be from source.
2826 if Ekind (E) = E_Variable then
2827 Set_Never_Set_In_Source (E, False);
2830 -- In older versions of Ada the corresponding pragmas
2831 -- specified a Convention. In Ada 2012 the convention
2832 -- is specified as a separate aspect, and it is optional,
2833 -- given that it defaults to Convention_Ada. The code
2834 -- that verifed that there was a matching convention
2840 -- Library unit aspects require special handling in the case
2841 -- of a package declaration, the pragma needs to be inserted
2842 -- in the list of declarations for the associated package.
2843 -- There is no issue of visibility delay for these aspects.
2845 if A_Id in Library_Unit_Aspects
2847 Nkind_In (N, N_Package_Declaration,
2848 N_Generic_Package_Declaration)
2849 and then Nkind (Parent (N)) /= N_Compilation_Unit
2852 ("incorrect context for library unit aspect&", Id);
2856 -- Cases where we do not delay, includes all cases where
2857 -- the expression is missing other than the above cases.
2859 if not Delay_Required or else No (Expr) then
2861 (Pragma_Argument_Associations => New_List (
2862 Make_Pragma_Argument_Association (Sloc (Ent),
2863 Expression => Ent)),
2864 Pragma_Name => Chars (Id));
2865 Delay_Required := False;
2867 -- In general cases, the corresponding pragma/attribute
2868 -- definition clause will be inserted later at the freezing
2869 -- point, and we do not need to build it now
2877 -- This is special because for access types we need to generate
2878 -- an attribute definition clause. This also works for single
2879 -- task declarations, but it does not work for task type
2880 -- declarations, because we have the case where the expression
2881 -- references a discriminant of the task type. That can't use
2882 -- an attribute definition clause because we would not have
2883 -- visibility on the discriminant. For that case we must
2884 -- generate a pragma in the task definition.
2886 when Aspect_Storage_Size =>
2890 if Ekind (E) = E_Task_Type then
2892 Decl : constant Node_Id := Declaration_Node (E);
2895 pragma Assert (Nkind (Decl) = N_Task_Type_Declaration);
2897 -- If no task definition, create one
2899 if No (Task_Definition (Decl)) then
2900 Set_Task_Definition (Decl,
2901 Make_Task_Definition (Loc,
2902 Visible_Declarations => Empty_List,
2903 End_Label => Empty));
2906 -- Create a pragma and put it at the start of the
2907 -- task definition for the task type declaration.
2910 (Pragma_Argument_Associations => New_List (
2911 Make_Pragma_Argument_Association (Loc,
2912 Expression => Relocate_Node (Expr))),
2913 Pragma_Name => Name_Storage_Size);
2917 Visible_Declarations (Task_Definition (Decl)));
2921 -- All other cases, generate attribute definition
2925 Make_Attribute_Definition_Clause (Loc,
2927 Chars => Chars (Id),
2928 Expression => Relocate_Node (Expr));
2932 -- Attach the corresponding pragma/attribute definition clause to
2933 -- the aspect specification node.
2935 if Present (Aitem) then
2936 Set_From_Aspect_Specification (Aitem, True);
2939 -- In the context of a compilation unit, we directly put the
2940 -- pragma in the Pragmas_After list of the N_Compilation_Unit_Aux
2941 -- node (no delay is required here) except for aspects on a
2942 -- subprogram body (see below) and a generic package, for which
2943 -- we need to introduce the pragma before building the generic
2944 -- copy (see sem_ch12), and for package instantiations, where
2945 -- the library unit pragmas are better handled early.
2947 if Nkind (Parent (N)) = N_Compilation_Unit
2948 and then (Present (Aitem) or else Is_Boolean_Aspect (Aspect))
2951 Aux : constant Node_Id := Aux_Decls_Node (Parent (N));
2954 pragma Assert (Nkind (Aux) = N_Compilation_Unit_Aux);
2956 -- For a Boolean aspect, create the corresponding pragma if
2957 -- no expression or if the value is True.
2959 if Is_Boolean_Aspect (Aspect) and then No (Aitem) then
2960 if Is_True (Static_Boolean (Expr)) then
2962 (Pragma_Argument_Associations => New_List (
2963 Make_Pragma_Argument_Association (Sloc (Ent),
2964 Expression => Ent)),
2965 Pragma_Name => Chars (Id));
2967 Set_From_Aspect_Specification (Aitem, True);
2968 Set_Corresponding_Aspect (Aitem, Aspect);
2975 -- If the aspect is on a subprogram body (relevant aspect
2976 -- is Inline), add the pragma in front of the declarations.
2978 if Nkind (N) = N_Subprogram_Body then
2979 if No (Declarations (N)) then
2980 Set_Declarations (N, New_List);
2983 Prepend (Aitem, Declarations (N));
2985 elsif Nkind (N) = N_Generic_Package_Declaration then
2986 if No (Visible_Declarations (Specification (N))) then
2987 Set_Visible_Declarations (Specification (N), New_List);
2991 Visible_Declarations (Specification (N)));
2993 elsif Nkind (N) = N_Package_Instantiation then
2995 Spec : constant Node_Id :=
2996 Specification (Instance_Spec (N));
2998 if No (Visible_Declarations (Spec)) then
2999 Set_Visible_Declarations (Spec, New_List);
3002 Prepend (Aitem, Visible_Declarations (Spec));
3006 if No (Pragmas_After (Aux)) then
3007 Set_Pragmas_After (Aux, New_List);
3010 Append (Aitem, Pragmas_After (Aux));
3017 -- The evaluation of the aspect is delayed to the freezing point.
3018 -- The pragma or attribute clause if there is one is then attached
3019 -- to the aspect specification which is put in the rep item list.
3021 if Delay_Required then
3022 if Present (Aitem) then
3023 Set_Is_Delayed_Aspect (Aitem);
3024 Set_Aspect_Rep_Item (Aspect, Aitem);
3025 Set_Parent (Aitem, Aspect);
3028 Set_Is_Delayed_Aspect (Aspect);
3030 -- In the case of Default_Value, link the aspect to base type
3031 -- as well, even though it appears on a first subtype. This is
3032 -- mandated by the semantics of the aspect. Do not establish
3033 -- the link when processing the base type itself as this leads
3034 -- to a rep item circularity. Verify that we are dealing with
3035 -- a scalar type to prevent cascaded errors.
3037 if A_Id = Aspect_Default_Value
3038 and then Is_Scalar_Type (E)
3039 and then Base_Type (E) /= E
3041 Set_Has_Delayed_Aspects (Base_Type (E));
3042 Record_Rep_Item (Base_Type (E), Aspect);
3045 Set_Has_Delayed_Aspects (E);
3046 Record_Rep_Item (E, Aspect);
3048 -- When delay is not required and the context is a package or a
3049 -- subprogram body, insert the pragma in the body declarations.
3051 elsif Nkind_In (N, N_Package_Body, N_Subprogram_Body) then
3052 if No (Declarations (N)) then
3053 Set_Declarations (N, New_List);
3056 -- The pragma is added before source declarations
3058 Prepend_To (Declarations (N), Aitem);
3060 -- When delay is not required and the context is not a compilation
3061 -- unit, we simply insert the pragma/attribute definition clause
3065 Insert_After (Ins_Node, Aitem);
3068 end Analyze_One_Aspect;
3072 end loop Aspect_Loop;
3074 if Has_Delayed_Aspects (E) then
3075 Ensure_Freeze_Node (E);
3077 end Analyze_Aspect_Specifications;
3079 -----------------------
3080 -- Analyze_At_Clause --
3081 -----------------------
3083 -- An at clause is replaced by the corresponding Address attribute
3084 -- definition clause that is the preferred approach in Ada 95.
3086 procedure Analyze_At_Clause (N : Node_Id) is
3087 CS : constant Boolean := Comes_From_Source (N);
3090 -- This is an obsolescent feature
3092 Check_Restriction (No_Obsolescent_Features, N);
3094 if Warn_On_Obsolescent_Feature then
3096 ("?j?at clause is an obsolescent feature (RM J.7(2))", N);
3098 ("\?j?use address attribute definition clause instead", N);
3101 -- Rewrite as address clause
3104 Make_Attribute_Definition_Clause (Sloc (N),
3105 Name => Identifier (N),
3106 Chars => Name_Address,
3107 Expression => Expression (N)));
3109 -- We preserve Comes_From_Source, since logically the clause still comes
3110 -- from the source program even though it is changed in form.
3112 Set_Comes_From_Source (N, CS);
3114 -- Analyze rewritten clause
3116 Analyze_Attribute_Definition_Clause (N);
3117 end Analyze_At_Clause;
3119 -----------------------------------------
3120 -- Analyze_Attribute_Definition_Clause --
3121 -----------------------------------------
3123 procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
3124 Loc : constant Source_Ptr := Sloc (N);
3125 Nam : constant Node_Id := Name (N);
3126 Attr : constant Name_Id := Chars (N);
3127 Expr : constant Node_Id := Expression (N);
3128 Id : constant Attribute_Id := Get_Attribute_Id (Attr);
3131 -- The entity of Nam after it is analyzed. In the case of an incomplete
3132 -- type, this is the underlying type.
3135 -- The underlying entity to which the attribute applies. Generally this
3136 -- is the Underlying_Type of Ent, except in the case where the clause
3137 -- applies to full view of incomplete type or private type in which case
3138 -- U_Ent is just a copy of Ent.
3140 FOnly : Boolean := False;
3141 -- Reset to True for subtype specific attribute (Alignment, Size)
3142 -- and for stream attributes, i.e. those cases where in the call
3143 -- to Rep_Item_Too_Late, FOnly is set True so that only the freezing
3144 -- rules are checked. Note that the case of stream attributes is not
3145 -- clear from the RM, but see AI95-00137. Also, the RM seems to
3146 -- disallow Storage_Size for derived task types, but that is also
3147 -- clearly unintentional.
3149 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
3150 -- Common processing for 'Read, 'Write, 'Input and 'Output attribute
3151 -- definition clauses.
3153 function Duplicate_Clause return Boolean;
3154 -- This routine checks if the aspect for U_Ent being given by attribute
3155 -- definition clause N is for an aspect that has already been specified,
3156 -- and if so gives an error message. If there is a duplicate, True is
3157 -- returned, otherwise if there is no error, False is returned.
3159 procedure Check_Indexing_Functions;
3160 -- Check that the function in Constant_Indexing or Variable_Indexing
3161 -- attribute has the proper type structure. If the name is overloaded,
3162 -- check that some interpretation is legal.
3164 procedure Check_Iterator_Functions;
3165 -- Check that there is a single function in Default_Iterator attribute
3166 -- has the proper type structure.
3168 function Check_Primitive_Function (Subp : Entity_Id) return Boolean;
3169 -- Common legality check for the previous two
3171 -----------------------------------
3172 -- Analyze_Stream_TSS_Definition --
3173 -----------------------------------
3175 procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
3176 Subp : Entity_Id := Empty;
3181 Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
3182 -- True for Read attribute, false for other attributes
3184 function Has_Good_Profile (Subp : Entity_Id) return Boolean;
3185 -- Return true if the entity is a subprogram with an appropriate
3186 -- profile for the attribute being defined.
3188 ----------------------
3189 -- Has_Good_Profile --
3190 ----------------------
3192 function Has_Good_Profile (Subp : Entity_Id) return Boolean is
3194 Is_Function : constant Boolean := (TSS_Nam = TSS_Stream_Input);
3195 Expected_Ekind : constant array (Boolean) of Entity_Kind :=
3196 (False => E_Procedure, True => E_Function);
3200 if Ekind (Subp) /= Expected_Ekind (Is_Function) then
3204 F := First_Formal (Subp);
3207 or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
3208 or else Designated_Type (Etype (F)) /=
3209 Class_Wide_Type (RTE (RE_Root_Stream_Type))
3214 if not Is_Function then
3218 Expected_Mode : constant array (Boolean) of Entity_Kind :=
3219 (False => E_In_Parameter,
3220 True => E_Out_Parameter);
3222 if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
3230 Typ := Etype (Subp);
3233 -- Verify that the prefix of the attribute and the local name
3234 -- for the type of the formal match.
3236 if Base_Type (Typ) /= Base_Type (Ent)
3237 or else Present ((Next_Formal (F)))
3241 elsif not Is_Scalar_Type (Typ)
3242 and then not Is_First_Subtype (Typ)
3243 and then not Is_Class_Wide_Type (Typ)
3250 end Has_Good_Profile;
3252 -- Start of processing for Analyze_Stream_TSS_Definition
3257 if not Is_Type (U_Ent) then
3258 Error_Msg_N ("local name must be a subtype", Nam);
3261 elsif not Is_First_Subtype (U_Ent) then
3262 Error_Msg_N ("local name must be a first subtype", Nam);
3266 Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
3268 -- If Pnam is present, it can be either inherited from an ancestor
3269 -- type (in which case it is legal to redefine it for this type), or
3270 -- be a previous definition of the attribute for the same type (in
3271 -- which case it is illegal).
3273 -- In the first case, it will have been analyzed already, and we
3274 -- can check that its profile does not match the expected profile
3275 -- for a stream attribute of U_Ent. In the second case, either Pnam
3276 -- has been analyzed (and has the expected profile), or it has not
3277 -- been analyzed yet (case of a type that has not been frozen yet
3278 -- and for which the stream attribute has been set using Set_TSS).
3281 and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
3283 Error_Msg_Sloc := Sloc (Pnam);
3284 Error_Msg_Name_1 := Attr;
3285 Error_Msg_N ("% attribute already defined #", Nam);
3291 if Is_Entity_Name (Expr) then
3292 if not Is_Overloaded (Expr) then
3293 if Has_Good_Profile (Entity (Expr)) then
3294 Subp := Entity (Expr);
3298 Get_First_Interp (Expr, I, It);
3299 while Present (It.Nam) loop
3300 if Has_Good_Profile (It.Nam) then
3305 Get_Next_Interp (I, It);
3310 if Present (Subp) then
3311 if Is_Abstract_Subprogram (Subp) then
3312 Error_Msg_N ("stream subprogram must not be abstract", Expr);
3315 -- Test for stream subprogram for interface type being non-null
3317 elsif Is_Interface (U_Ent)
3318 and then not Inside_A_Generic
3319 and then Ekind (Subp) = E_Procedure
3323 (Unit_Declaration_Node (Ultimate_Alias (Subp))))
3326 ("stream subprogram for interface type "
3327 & "must be null procedure", Expr);
3330 Set_Entity (Expr, Subp);
3331 Set_Etype (Expr, Etype (Subp));
3333 New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
3336 Error_Msg_Name_1 := Attr;
3337 Error_Msg_N ("incorrect expression for% attribute", Expr);
3339 end Analyze_Stream_TSS_Definition;
3341 ------------------------------
3342 -- Check_Indexing_Functions --
3343 ------------------------------
3345 procedure Check_Indexing_Functions is
3346 Indexing_Found : Boolean;
3348 procedure Check_One_Function (Subp : Entity_Id);
3349 -- Check one possible interpretation. Sets Indexing_Found True if an
3350 -- indexing function is found.
3352 ------------------------
3353 -- Check_One_Function --
3354 ------------------------
3356 procedure Check_One_Function (Subp : Entity_Id) is
3357 Default_Element : constant Node_Id :=
3358 Find_Value_Of_Aspect
3359 (Etype (First_Formal (Subp)),
3360 Aspect_Iterator_Element);
3363 if not Check_Primitive_Function (Subp)
3364 and then not Is_Overloaded (Expr)
3367 ("aspect Indexing requires a function that applies to type&",
3371 -- An indexing function must return either the default element of
3372 -- the container, or a reference type. For variable indexing it
3373 -- must be the latter.
3375 if Present (Default_Element) then
3376 Analyze (Default_Element);
3378 if Is_Entity_Name (Default_Element)
3379 and then Covers (Entity (Default_Element), Etype (Subp))
3381 Indexing_Found := True;
3386 -- For variable_indexing the return type must be a reference type
3388 if Attr = Name_Variable_Indexing
3389 and then not Has_Implicit_Dereference (Etype (Subp))
3392 ("function for indexing must return a reference type", Subp);
3395 Indexing_Found := True;
3397 end Check_One_Function;
3399 -- Start of processing for Check_Indexing_Functions
3408 if not Is_Overloaded (Expr) then
3409 Check_One_Function (Entity (Expr));
3417 Indexing_Found := False;
3418 Get_First_Interp (Expr, I, It);
3419 while Present (It.Nam) loop
3421 -- Note that analysis will have added the interpretation
3422 -- that corresponds to the dereference. We only check the
3423 -- subprogram itself.
3425 if Is_Overloadable (It.Nam) then
3426 Check_One_Function (It.Nam);
3429 Get_Next_Interp (I, It);
3432 if not Indexing_Found then
3434 ("aspect Indexing requires a function that "
3435 & "applies to type&", Expr, Ent);
3439 end Check_Indexing_Functions;
3441 ------------------------------
3442 -- Check_Iterator_Functions --
3443 ------------------------------
3445 procedure Check_Iterator_Functions is
3446 Default : Entity_Id;
3448 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean;
3449 -- Check one possible interpretation for validity
3451 ----------------------------
3452 -- Valid_Default_Iterator --
3453 ----------------------------
3455 function Valid_Default_Iterator (Subp : Entity_Id) return Boolean is
3459 if not Check_Primitive_Function (Subp) then
3462 Formal := First_Formal (Subp);
3465 -- False if any subsequent formal has no default expression
3467 Formal := Next_Formal (Formal);
3468 while Present (Formal) loop
3469 if No (Expression (Parent (Formal))) then
3473 Next_Formal (Formal);
3476 -- True if all subsequent formals have default expressions
3479 end Valid_Default_Iterator;
3481 -- Start of processing for Check_Iterator_Functions
3486 if not Is_Entity_Name (Expr) then
3487 Error_Msg_N ("aspect Iterator must be a function name", Expr);
3490 if not Is_Overloaded (Expr) then
3491 if not Check_Primitive_Function (Entity (Expr)) then
3493 ("aspect Indexing requires a function that applies to type&",
3494 Entity (Expr), Ent);
3497 if not Valid_Default_Iterator (Entity (Expr)) then
3498 Error_Msg_N ("improper function for default iterator", Expr);
3508 Get_First_Interp (Expr, I, It);
3509 while Present (It.Nam) loop
3510 if not Check_Primitive_Function (It.Nam)
3511 or else not Valid_Default_Iterator (It.Nam)
3515 elsif Present (Default) then
3516 Error_Msg_N ("default iterator must be unique", Expr);
3522 Get_Next_Interp (I, It);
3526 if Present (Default) then
3527 Set_Entity (Expr, Default);
3528 Set_Is_Overloaded (Expr, False);
3531 end Check_Iterator_Functions;
3533 -------------------------------
3534 -- Check_Primitive_Function --
3535 -------------------------------
3537 function Check_Primitive_Function (Subp : Entity_Id) return Boolean is
3541 if Ekind (Subp) /= E_Function then
3545 if No (First_Formal (Subp)) then
3548 Ctrl := Etype (First_Formal (Subp));
3552 or else Ctrl = Class_Wide_Type (Ent)
3554 (Ekind (Ctrl) = E_Anonymous_Access_Type
3556 (Designated_Type (Ctrl) = Ent
3557 or else Designated_Type (Ctrl) = Class_Wide_Type (Ent)))
3566 end Check_Primitive_Function;
3568 ----------------------
3569 -- Duplicate_Clause --
3570 ----------------------
3572 function Duplicate_Clause return Boolean is
3576 -- Nothing to do if this attribute definition clause comes from
3577 -- an aspect specification, since we could not be duplicating an
3578 -- explicit clause, and we dealt with the case of duplicated aspects
3579 -- in Analyze_Aspect_Specifications.
3581 if From_Aspect_Specification (N) then
3585 -- Otherwise current clause may duplicate previous clause, or a
3586 -- previously given pragma or aspect specification for the same
3589 A := Get_Rep_Item (U_Ent, Chars (N), Check_Parents => False);
3592 Error_Msg_Name_1 := Chars (N);
3593 Error_Msg_Sloc := Sloc (A);
3595 Error_Msg_NE ("aspect% for & previously given#", N, U_Ent);
3600 end Duplicate_Clause;
3602 -- Start of processing for Analyze_Attribute_Definition_Clause
3605 -- The following code is a defense against recursion. Not clear that
3606 -- this can happen legitimately, but perhaps some error situations
3607 -- can cause it, and we did see this recursion during testing.
3609 if Analyzed (N) then
3612 Set_Analyzed (N, True);
3615 -- Ignore some selected attributes in CodePeer mode since they are not
3616 -- relevant in this context.
3618 if CodePeer_Mode then
3621 -- Ignore Component_Size in CodePeer mode, to avoid changing the
3622 -- internal representation of types by implicitly packing them.
3624 when Attribute_Component_Size =>
3625 Rewrite (N, Make_Null_Statement (Sloc (N)));
3633 -- Process Ignore_Rep_Clauses option
3635 if Ignore_Rep_Clauses then
3638 -- The following should be ignored. They do not affect legality
3639 -- and may be target dependent. The basic idea of -gnatI is to
3640 -- ignore any rep clauses that may be target dependent but do not
3641 -- affect legality (except possibly to be rejected because they
3642 -- are incompatible with the compilation target).
3644 when Attribute_Alignment |
3645 Attribute_Bit_Order |
3646 Attribute_Component_Size |
3647 Attribute_Machine_Radix |
3648 Attribute_Object_Size |
3651 Attribute_Stream_Size |
3652 Attribute_Value_Size =>
3653 Kill_Rep_Clause (N);
3656 -- The following should not be ignored, because in the first place
3657 -- they are reasonably portable, and should not cause problems in
3658 -- compiling code from another target, and also they do affect
3659 -- legality, e.g. failing to provide a stream attribute for a
3660 -- type may make a program illegal.
3662 when Attribute_External_Tag |
3666 Attribute_Simple_Storage_Pool |
3667 Attribute_Storage_Pool |
3668 Attribute_Storage_Size |
3672 -- We do not do anything here with address clauses, they will be
3673 -- removed by Freeze later on, but for now, it works better to
3674 -- keep then in the tree.
3676 when Attribute_Address =>
3679 -- Other cases are errors ("attribute& cannot be set with
3680 -- definition clause"), which will be caught below.
3688 Ent := Entity (Nam);
3690 if Rep_Item_Too_Early (Ent, N) then
3694 -- Rep clause applies to full view of incomplete type or private type if
3695 -- we have one (if not, this is a premature use of the type). However,
3696 -- certain semantic checks need to be done on the specified entity (i.e.
3697 -- the private view), so we save it in Ent.
3699 if Is_Private_Type (Ent)
3700 and then Is_Derived_Type (Ent)
3701 and then not Is_Tagged_Type (Ent)
3702 and then No (Full_View (Ent))
3704 -- If this is a private type whose completion is a derivation from
3705 -- another private type, there is no full view, and the attribute
3706 -- belongs to the type itself, not its underlying parent.
3710 elsif Ekind (Ent) = E_Incomplete_Type then
3712 -- The attribute applies to the full view, set the entity of the
3713 -- attribute definition accordingly.
3715 Ent := Underlying_Type (Ent);
3717 Set_Entity (Nam, Ent);
3720 U_Ent := Underlying_Type (Ent);
3723 -- Avoid cascaded error
3725 if Etype (Nam) = Any_Type then
3728 -- Must be declared in current scope or in case of an aspect
3729 -- specification, must be visible in current scope.
3731 elsif Scope (Ent) /= Current_Scope
3733 not (From_Aspect_Specification (N)
3734 and then Scope_Within_Or_Same (Current_Scope, Scope (Ent)))
3736 Error_Msg_N ("entity must be declared in this scope", Nam);
3739 -- Must not be a source renaming (we do have some cases where the
3740 -- expander generates a renaming, and those cases are OK, in such
3741 -- cases any attribute applies to the renamed object as well).
3743 elsif Is_Object (Ent)
3744 and then Present (Renamed_Object (Ent))
3746 -- Case of renamed object from source, this is an error
3748 if Comes_From_Source (Renamed_Object (Ent)) then
3749 Get_Name_String (Chars (N));
3750 Error_Msg_Strlen := Name_Len;
3751 Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
3753 ("~ clause not allowed for a renaming declaration "
3754 & "(RM 13.1(6))", Nam);
3757 -- For the case of a compiler generated renaming, the attribute
3758 -- definition clause applies to the renamed object created by the
3759 -- expander. The easiest general way to handle this is to create a
3760 -- copy of the attribute definition clause for this object.
3762 elsif Is_Entity_Name (Renamed_Object (Ent)) then
3764 Make_Attribute_Definition_Clause (Loc,
3766 New_Occurrence_Of (Entity (Renamed_Object (Ent)), Loc),
3768 Expression => Duplicate_Subexpr (Expression (N))));
3770 -- If the renamed object is not an entity, it must be a dereference
3771 -- of an unconstrained function call, and we must introduce a new
3772 -- declaration to capture the expression. This is needed in the case
3773 -- of 'Alignment, where the original declaration must be rewritten.
3777 (Nkind (Renamed_Object (Ent)) = N_Explicit_Dereference);
3781 -- If no underlying entity, use entity itself, applies to some
3782 -- previously detected error cases ???
3784 elsif No (U_Ent) then
3787 -- Cannot specify for a subtype (exception Object/Value_Size)
3789 elsif Is_Type (U_Ent)
3790 and then not Is_First_Subtype (U_Ent)
3791 and then Id /= Attribute_Object_Size
3792 and then Id /= Attribute_Value_Size
3793 and then not From_At_Mod (N)
3795 Error_Msg_N ("cannot specify attribute for subtype", Nam);
3799 Set_Entity (N, U_Ent);
3800 Check_Restriction_No_Use_Of_Attribute (N);
3802 -- Switch on particular attribute
3810 -- Address attribute definition clause
3812 when Attribute_Address => Address : begin
3814 -- A little error check, catch for X'Address use X'Address;
3816 if Nkind (Nam) = N_Identifier
3817 and then Nkind (Expr) = N_Attribute_Reference
3818 and then Attribute_Name (Expr) = Name_Address
3819 and then Nkind (Prefix (Expr)) = N_Identifier
3820 and then Chars (Nam) = Chars (Prefix (Expr))
3823 ("address for & is self-referencing", Prefix (Expr), Ent);
3827 -- Not that special case, carry on with analysis of expression
3829 Analyze_And_Resolve (Expr, RTE (RE_Address));
3831 -- Even when ignoring rep clauses we need to indicate that the
3832 -- entity has an address clause and thus it is legal to declare
3833 -- it imported. Freeze will get rid of the address clause later.
3835 if Ignore_Rep_Clauses then
3836 if Ekind_In (U_Ent, E_Variable, E_Constant) then
3837 Record_Rep_Item (U_Ent, N);
3843 if Duplicate_Clause then
3846 -- Case of address clause for subprogram
3848 elsif Is_Subprogram (U_Ent) then
3849 if Has_Homonym (U_Ent) then
3851 ("address clause cannot be given " &
3852 "for overloaded subprogram",
3857 -- For subprograms, all address clauses are permitted, and we
3858 -- mark the subprogram as having a deferred freeze so that Gigi
3859 -- will not elaborate it too soon.
3861 -- Above needs more comments, what is too soon about???
3863 Set_Has_Delayed_Freeze (U_Ent);
3865 -- Case of address clause for entry
3867 elsif Ekind (U_Ent) = E_Entry then
3868 if Nkind (Parent (N)) = N_Task_Body then
3870 ("entry address must be specified in task spec", Nam);
3874 -- For entries, we require a constant address
3876 Check_Constant_Address_Clause (Expr, U_Ent);
3878 -- Special checks for task types
3880 if Is_Task_Type (Scope (U_Ent))
3881 and then Comes_From_Source (Scope (U_Ent))
3884 ("??entry address declared for entry in task type", N);
3886 ("\??only one task can be declared of this type", N);
3889 -- Entry address clauses are obsolescent
3891 Check_Restriction (No_Obsolescent_Features, N);
3893 if Warn_On_Obsolescent_Feature then
3895 ("?j?attaching interrupt to task entry is an " &
3896 "obsolescent feature (RM J.7.1)", N);
3898 ("\?j?use interrupt procedure instead", N);
3901 -- Case of an address clause for a controlled object which we
3902 -- consider to be erroneous.
3904 elsif Is_Controlled (Etype (U_Ent))
3905 or else Has_Controlled_Component (Etype (U_Ent))
3908 ("??controlled object& must not be overlaid", Nam, U_Ent);
3910 ("\??Program_Error will be raised at run time", Nam);
3911 Insert_Action (Declaration_Node (U_Ent),
3912 Make_Raise_Program_Error (Loc,
3913 Reason => PE_Overlaid_Controlled_Object));
3916 -- Case of address clause for a (non-controlled) object
3919 Ekind (U_Ent) = E_Variable
3921 Ekind (U_Ent) = E_Constant
3924 Expr : constant Node_Id := Expression (N);
3929 -- Exported variables cannot have an address clause, because
3930 -- this cancels the effect of the pragma Export.
3932 if Is_Exported (U_Ent) then
3934 ("cannot export object with address clause", Nam);
3938 Find_Overlaid_Entity (N, O_Ent, Off);
3940 -- Overlaying controlled objects is erroneous
3943 and then (Has_Controlled_Component (Etype (O_Ent))
3944 or else Is_Controlled (Etype (O_Ent)))
3947 ("??cannot overlay with controlled object", Expr);
3949 ("\??Program_Error will be raised at run time", Expr);
3950 Insert_Action (Declaration_Node (U_Ent),
3951 Make_Raise_Program_Error (Loc,
3952 Reason => PE_Overlaid_Controlled_Object));
3955 elsif Present (O_Ent)
3956 and then Ekind (U_Ent) = E_Constant
3957 and then not Is_Constant_Object (O_Ent)
3959 Error_Msg_N ("??constant overlays a variable", Expr);
3961 -- Imported variables can have an address clause, but then
3962 -- the import is pretty meaningless except to suppress
3963 -- initializations, so we do not need such variables to
3964 -- be statically allocated (and in fact it causes trouble
3965 -- if the address clause is a local value).
3967 elsif Is_Imported (U_Ent) then
3968 Set_Is_Statically_Allocated (U_Ent, False);
3971 -- We mark a possible modification of a variable with an
3972 -- address clause, since it is likely aliasing is occurring.
3974 Note_Possible_Modification (Nam, Sure => False);
3976 -- Here we are checking for explicit overlap of one variable
3977 -- by another, and if we find this then mark the overlapped
3978 -- variable as also being volatile to prevent unwanted
3979 -- optimizations. This is a significant pessimization so
3980 -- avoid it when there is an offset, i.e. when the object
3981 -- is composite; they cannot be optimized easily anyway.
3984 and then Is_Object (O_Ent)
3987 -- The following test is an expedient solution to what
3988 -- is really a problem in CodePeer. Suppressing the
3989 -- Set_Treat_As_Volatile call here prevents later
3990 -- generation (in some cases) of trees that CodePeer
3991 -- should, but currently does not, handle correctly.
3992 -- This test should probably be removed when CodePeer
3993 -- is improved, just because we want the tree CodePeer
3994 -- analyzes to match the tree for which we generate code
3995 -- as closely as is practical. ???
3997 and then not CodePeer_Mode
3999 -- ??? O_Ent might not be in current unit
4001 Set_Treat_As_Volatile (O_Ent);
4004 -- Legality checks on the address clause for initialized
4005 -- objects is deferred until the freeze point, because
4006 -- a subsequent pragma might indicate that the object
4007 -- is imported and thus not initialized. Also, the address
4008 -- clause might involve entities that have yet to be
4011 Set_Has_Delayed_Freeze (U_Ent);
4013 -- If an initialization call has been generated for this
4014 -- object, it needs to be deferred to after the freeze node
4015 -- we have just now added, otherwise GIGI will see a
4016 -- reference to the variable (as actual to the IP call)
4017 -- before its definition.
4020 Init_Call : constant Node_Id :=
4021 Remove_Init_Call (U_Ent, N);
4024 if Present (Init_Call) then
4025 Append_Freeze_Action (U_Ent, Init_Call);
4027 -- Reset Initialization_Statements pointer so that
4028 -- if there is a pragma Import further down, it can
4029 -- clear any default initialization.
4031 Set_Initialization_Statements (U_Ent, Init_Call);
4035 if Is_Exported (U_Ent) then
4037 ("& cannot be exported if an address clause is given",
4040 ("\define and export a variable "
4041 & "that holds its address instead", Nam);
4044 -- Entity has delayed freeze, so we will generate an
4045 -- alignment check at the freeze point unless suppressed.
4047 if not Range_Checks_Suppressed (U_Ent)
4048 and then not Alignment_Checks_Suppressed (U_Ent)
4050 Set_Check_Address_Alignment (N);
4053 -- Kill the size check code, since we are not allocating
4054 -- the variable, it is somewhere else.
4056 Kill_Size_Check_Code (U_Ent);
4058 -- If the address clause is of the form:
4060 -- for Y'Address use X'Address
4064 -- Const : constant Address := X'Address;
4066 -- for Y'Address use Const;
4068 -- then we make an entry in the table for checking the size
4069 -- and alignment of the overlaying variable. We defer this
4070 -- check till after code generation to take full advantage
4071 -- of the annotation done by the back end.
4073 -- If the entity has a generic type, the check will be
4074 -- performed in the instance if the actual type justifies
4075 -- it, and we do not insert the clause in the table to
4076 -- prevent spurious warnings.
4078 -- Note: we used to test Comes_From_Source and only give
4079 -- this warning for source entities, but we have removed
4080 -- this test. It really seems bogus to generate overlays
4081 -- that would trigger this warning in generated code.
4082 -- Furthermore, by removing the test, we handle the
4083 -- aspect case properly.
4085 if Address_Clause_Overlay_Warnings
4086 and then Present (O_Ent)
4087 and then Is_Object (O_Ent)
4089 if not Is_Generic_Type (Etype (U_Ent)) then
4090 Address_Clause_Checks.Append ((N, U_Ent, O_Ent, Off));
4093 -- If variable overlays a constant view, and we are
4094 -- warning on overlays, then mark the variable as
4095 -- overlaying a constant (we will give warnings later
4096 -- if this variable is assigned).
4098 if Is_Constant_Object (O_Ent)
4099 and then Ekind (U_Ent) = E_Variable
4101 Set_Overlays_Constant (U_Ent);
4106 -- Not a valid entity for an address clause
4109 Error_Msg_N ("address cannot be given for &", Nam);
4117 -- Alignment attribute definition clause
4119 when Attribute_Alignment => Alignment : declare
4120 Align : constant Uint := Get_Alignment_Value (Expr);
4121 Max_Align : constant Uint := UI_From_Int (Maximum_Alignment);
4126 if not Is_Type (U_Ent)
4127 and then Ekind (U_Ent) /= E_Variable
4128 and then Ekind (U_Ent) /= E_Constant
4130 Error_Msg_N ("alignment cannot be given for &", Nam);
4132 elsif Duplicate_Clause then
4135 elsif Align /= No_Uint then
4136 Set_Has_Alignment_Clause (U_Ent);
4138 -- Tagged type case, check for attempt to set alignment to a
4139 -- value greater than Max_Align, and reset if so.
4141 if Is_Tagged_Type (U_Ent) and then Align > Max_Align then
4143 ("alignment for & set to Maximum_Aligment??", Nam);
4144 Set_Alignment (U_Ent, Max_Align);
4149 Set_Alignment (U_Ent, Align);
4152 -- For an array type, U_Ent is the first subtype. In that case,
4153 -- also set the alignment of the anonymous base type so that
4154 -- other subtypes (such as the itypes for aggregates of the
4155 -- type) also receive the expected alignment.
4157 if Is_Array_Type (U_Ent) then
4158 Set_Alignment (Base_Type (U_Ent), Align);
4167 -- Bit_Order attribute definition clause
4169 when Attribute_Bit_Order => Bit_Order : declare
4171 if not Is_Record_Type (U_Ent) then
4173 ("Bit_Order can only be defined for record type", Nam);
4175 elsif Duplicate_Clause then
4179 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
4181 if Etype (Expr) = Any_Type then
4184 elsif not Is_Static_Expression (Expr) then
4185 Flag_Non_Static_Expr
4186 ("Bit_Order requires static expression!", Expr);
4189 if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
4190 Set_Reverse_Bit_Order (U_Ent, True);
4196 --------------------
4197 -- Component_Size --
4198 --------------------
4200 -- Component_Size attribute definition clause
4202 when Attribute_Component_Size => Component_Size_Case : declare
4203 Csize : constant Uint := Static_Integer (Expr);
4207 New_Ctyp : Entity_Id;
4211 if not Is_Array_Type (U_Ent) then
4212 Error_Msg_N ("component size requires array type", Nam);
4216 Btype := Base_Type (U_Ent);
4217 Ctyp := Component_Type (Btype);
4219 if Duplicate_Clause then
4222 elsif Rep_Item_Too_Early (Btype, N) then
4225 elsif Csize /= No_Uint then
4226 Check_Size (Expr, Ctyp, Csize, Biased);
4228 -- For the biased case, build a declaration for a subtype that
4229 -- will be used to represent the biased subtype that reflects
4230 -- the biased representation of components. We need the subtype
4231 -- to get proper conversions on referencing elements of the
4232 -- array. Note: component size clauses are ignored in VM mode.
4234 if VM_Target = No_VM then
4237 Make_Defining_Identifier (Loc,
4239 New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
4242 Make_Subtype_Declaration (Loc,
4243 Defining_Identifier => New_Ctyp,
4244 Subtype_Indication =>
4245 New_Occurrence_Of (Component_Type (Btype), Loc));
4247 Set_Parent (Decl, N);
4248 Analyze (Decl, Suppress => All_Checks);
4250 Set_Has_Delayed_Freeze (New_Ctyp, False);
4251 Set_Esize (New_Ctyp, Csize);
4252 Set_RM_Size (New_Ctyp, Csize);
4253 Init_Alignment (New_Ctyp);
4254 Set_Is_Itype (New_Ctyp, True);
4255 Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
4257 Set_Component_Type (Btype, New_Ctyp);
4258 Set_Biased (New_Ctyp, N, "component size clause");
4261 Set_Component_Size (Btype, Csize);
4263 -- For VM case, we ignore component size clauses
4266 -- Give a warning unless we are in GNAT mode, in which case
4267 -- the warning is suppressed since it is not useful.
4269 if not GNAT_Mode then
4271 ("component size ignored in this configuration??", N);
4275 -- Deal with warning on overridden size
4277 if Warn_On_Overridden_Size
4278 and then Has_Size_Clause (Ctyp)
4279 and then RM_Size (Ctyp) /= Csize
4282 ("component size overrides size clause for&?S?", N, Ctyp);
4285 Set_Has_Component_Size_Clause (Btype, True);
4286 Set_Has_Non_Standard_Rep (Btype, True);
4288 end Component_Size_Case;
4290 -----------------------
4291 -- Constant_Indexing --
4292 -----------------------
4294 when Attribute_Constant_Indexing =>
4295 Check_Indexing_Functions;
4301 when Attribute_CPU => CPU :
4303 -- CPU attribute definition clause not allowed except from aspect
4306 if From_Aspect_Specification (N) then
4307 if not Is_Task_Type (U_Ent) then
4308 Error_Msg_N ("CPU can only be defined for task", Nam);
4310 elsif Duplicate_Clause then
4314 -- The expression must be analyzed in the special manner
4315 -- described in "Handling of Default and Per-Object
4316 -- Expressions" in sem.ads.
4318 -- The visibility to the discriminants must be restored
4320 Push_Scope_And_Install_Discriminants (U_Ent);
4321 Preanalyze_Spec_Expression (Expr, RTE (RE_CPU_Range));
4322 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
4324 if not Is_Static_Expression (Expr) then
4325 Check_Restriction (Static_Priorities, Expr);
4331 ("attribute& cannot be set with definition clause", N);
4335 ----------------------
4336 -- Default_Iterator --
4337 ----------------------
4339 when Attribute_Default_Iterator => Default_Iterator : declare
4343 if not Is_Tagged_Type (U_Ent) then
4345 ("aspect Default_Iterator applies to tagged type", Nam);
4348 Check_Iterator_Functions;
4352 if not Is_Entity_Name (Expr)
4353 or else Ekind (Entity (Expr)) /= E_Function
4355 Error_Msg_N ("aspect Iterator must be a function", Expr);
4357 Func := Entity (Expr);
4360 if No (First_Formal (Func))
4361 or else Etype (First_Formal (Func)) /= U_Ent
4364 ("Default Iterator must be a primitive of&", Func, U_Ent);
4366 end Default_Iterator;
4368 ------------------------
4369 -- Dispatching_Domain --
4370 ------------------------
4372 when Attribute_Dispatching_Domain => Dispatching_Domain :
4374 -- Dispatching_Domain attribute definition clause not allowed
4375 -- except from aspect specification.
4377 if From_Aspect_Specification (N) then
4378 if not Is_Task_Type (U_Ent) then
4379 Error_Msg_N ("Dispatching_Domain can only be defined" &
4383 elsif Duplicate_Clause then
4387 -- The expression must be analyzed in the special manner
4388 -- described in "Handling of Default and Per-Object
4389 -- Expressions" in sem.ads.
4391 -- The visibility to the discriminants must be restored
4393 Push_Scope_And_Install_Discriminants (U_Ent);
4395 Preanalyze_Spec_Expression
4396 (Expr, RTE (RE_Dispatching_Domain));
4398 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
4403 ("attribute& cannot be set with definition clause", N);
4405 end Dispatching_Domain;
4411 when Attribute_External_Tag => External_Tag :
4413 if not Is_Tagged_Type (U_Ent) then
4414 Error_Msg_N ("should be a tagged type", Nam);
4417 if Duplicate_Clause then
4421 Analyze_And_Resolve (Expr, Standard_String);
4423 if not Is_Static_Expression (Expr) then
4424 Flag_Non_Static_Expr
4425 ("static string required for tag name!", Nam);
4428 if VM_Target /= No_VM then
4429 Error_Msg_Name_1 := Attr;
4431 ("% attribute unsupported in this configuration", Nam);
4434 if not Is_Library_Level_Entity (U_Ent) then
4436 ("??non-unique external tag supplied for &", N, U_Ent);
4438 ("\??same external tag applies to all "
4439 & "subprogram calls", N);
4441 ("\??corresponding internal tag cannot be obtained", N);
4446 --------------------------
4447 -- Implicit_Dereference --
4448 --------------------------
4450 when Attribute_Implicit_Dereference =>
4452 -- Legality checks already performed at the point of the type
4453 -- declaration, aspect is not delayed.
4461 when Attribute_Input =>
4462 Analyze_Stream_TSS_Definition (TSS_Stream_Input);
4463 Set_Has_Specified_Stream_Input (Ent);
4465 ------------------------
4466 -- Interrupt_Priority --
4467 ------------------------
4469 when Attribute_Interrupt_Priority => Interrupt_Priority :
4471 -- Interrupt_Priority attribute definition clause not allowed
4472 -- except from aspect specification.
4474 if From_Aspect_Specification (N) then
4475 if not (Is_Protected_Type (U_Ent)
4476 or else Is_Task_Type (U_Ent))
4479 ("Interrupt_Priority can only be defined for task" &
4480 "and protected object",
4483 elsif Duplicate_Clause then
4487 -- The expression must be analyzed in the special manner
4488 -- described in "Handling of Default and Per-Object
4489 -- Expressions" in sem.ads.
4491 -- The visibility to the discriminants must be restored
4493 Push_Scope_And_Install_Discriminants (U_Ent);
4495 Preanalyze_Spec_Expression
4496 (Expr, RTE (RE_Interrupt_Priority));
4498 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
4503 ("attribute& cannot be set with definition clause", N);
4505 end Interrupt_Priority;
4511 when Attribute_Iterable =>
4514 if Nkind (Expr) /= N_Aggregate then
4515 Error_Msg_N ("aspect Iterable must be an aggregate", Expr);
4522 Assoc := First (Component_Associations (Expr));
4523 while Present (Assoc) loop
4524 if not Is_Entity_Name (Expression (Assoc)) then
4525 Error_Msg_N ("value must be a function", Assoc);
4532 ----------------------
4533 -- Iterator_Element --
4534 ----------------------
4536 when Attribute_Iterator_Element =>
4539 if not Is_Entity_Name (Expr)
4540 or else not Is_Type (Entity (Expr))
4542 Error_Msg_N ("aspect Iterator_Element must be a type", Expr);
4549 -- Machine radix attribute definition clause
4551 when Attribute_Machine_Radix => Machine_Radix : declare
4552 Radix : constant Uint := Static_Integer (Expr);
4555 if not Is_Decimal_Fixed_Point_Type (U_Ent) then
4556 Error_Msg_N ("decimal fixed-point type expected for &", Nam);
4558 elsif Duplicate_Clause then
4561 elsif Radix /= No_Uint then
4562 Set_Has_Machine_Radix_Clause (U_Ent);
4563 Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
4567 elsif Radix = 10 then
4568 Set_Machine_Radix_10 (U_Ent);
4570 Error_Msg_N ("machine radix value must be 2 or 10", Expr);
4579 -- Object_Size attribute definition clause
4581 when Attribute_Object_Size => Object_Size : declare
4582 Size : constant Uint := Static_Integer (Expr);
4585 pragma Warnings (Off, Biased);
4588 if not Is_Type (U_Ent) then
4589 Error_Msg_N ("Object_Size cannot be given for &", Nam);
4591 elsif Duplicate_Clause then
4595 Check_Size (Expr, U_Ent, Size, Biased);
4597 if Is_Scalar_Type (U_Ent) then
4598 if Size /= 8 and then Size /= 16 and then Size /= 32
4599 and then UI_Mod (Size, 64) /= 0
4602 ("Object_Size must be 8, 16, 32, or multiple of 64",
4606 elsif Size mod 8 /= 0 then
4607 Error_Msg_N ("Object_Size must be a multiple of 8", Expr);
4610 Set_Esize (U_Ent, Size);
4611 Set_Has_Object_Size_Clause (U_Ent);
4612 Alignment_Check_For_Size_Change (U_Ent, Size);
4620 when Attribute_Output =>
4621 Analyze_Stream_TSS_Definition (TSS_Stream_Output);
4622 Set_Has_Specified_Stream_Output (Ent);
4628 when Attribute_Priority => Priority :
4630 -- Priority attribute definition clause not allowed except from
4631 -- aspect specification.
4633 if From_Aspect_Specification (N) then
4634 if not (Is_Protected_Type (U_Ent)
4635 or else Is_Task_Type (U_Ent)
4636 or else Ekind (U_Ent) = E_Procedure)
4639 ("Priority can only be defined for task and protected " &
4643 elsif Duplicate_Clause then
4647 -- The expression must be analyzed in the special manner
4648 -- described in "Handling of Default and Per-Object
4649 -- Expressions" in sem.ads.
4651 -- The visibility to the discriminants must be restored
4653 Push_Scope_And_Install_Discriminants (U_Ent);
4654 Preanalyze_Spec_Expression (Expr, Standard_Integer);
4655 Uninstall_Discriminants_And_Pop_Scope (U_Ent);
4657 if not Is_Static_Expression (Expr) then
4658 Check_Restriction (Static_Priorities, Expr);
4664 ("attribute& cannot be set with definition clause", N);
4672 when Attribute_Read =>
4673 Analyze_Stream_TSS_Definition (TSS_Stream_Read);
4674 Set_Has_Specified_Stream_Read (Ent);
4676 --------------------------
4677 -- Scalar_Storage_Order --
4678 --------------------------
4680 -- Scalar_Storage_Order attribute definition clause
4682 when Attribute_Scalar_Storage_Order => Scalar_Storage_Order : declare
4684 if not (Is_Record_Type (U_Ent) or else Is_Array_Type (U_Ent)) then
4686 ("Scalar_Storage_Order can only be defined for "
4687 & "record or array type", Nam);
4689 elsif Duplicate_Clause then
4693 Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
4695 if Etype (Expr) = Any_Type then
4698 elsif not Is_Static_Expression (Expr) then
4699 Flag_Non_Static_Expr
4700 ("Scalar_Storage_Order requires static expression!", Expr);
4702 elsif (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
4704 -- Here for the case of a non-default (i.e. non-confirming)
4705 -- Scalar_Storage_Order attribute definition.
4707 if Support_Nondefault_SSO_On_Target then
4708 Set_Reverse_Storage_Order (Base_Type (U_Ent), True);
4711 ("non-default Scalar_Storage_Order "
4712 & "not supported on target", Expr);
4716 end Scalar_Storage_Order;
4722 -- Size attribute definition clause
4724 when Attribute_Size => Size : declare
4725 Size : constant Uint := Static_Integer (Expr);
4732 if Duplicate_Clause then
4735 elsif not Is_Type (U_Ent)
4736 and then Ekind (U_Ent) /= E_Variable
4737 and then Ekind (U_Ent) /= E_Constant
4739 Error_Msg_N ("size cannot be given for &", Nam);
4741 elsif Is_Array_Type (U_Ent)
4742 and then not Is_Constrained (U_Ent)
4745 ("size cannot be given for unconstrained array", Nam);
4747 elsif Size /= No_Uint then
4748 if VM_Target /= No_VM and then not GNAT_Mode then
4750 -- Size clause is not handled properly on VM targets.
4751 -- Display a warning unless we are in GNAT mode, in which
4752 -- case this is useless.
4755 ("size clauses are ignored in this configuration??", N);
4758 if Is_Type (U_Ent) then
4761 Etyp := Etype (U_Ent);
4764 -- Check size, note that Gigi is in charge of checking that the
4765 -- size of an array or record type is OK. Also we do not check
4766 -- the size in the ordinary fixed-point case, since it is too
4767 -- early to do so (there may be subsequent small clause that
4768 -- affects the size). We can check the size if a small clause
4769 -- has already been given.
4771 if not Is_Ordinary_Fixed_Point_Type (U_Ent)
4772 or else Has_Small_Clause (U_Ent)
4774 Check_Size (Expr, Etyp, Size, Biased);
4775 Set_Biased (U_Ent, N, "size clause", Biased);
4778 -- For types set RM_Size and Esize if possible
4780 if Is_Type (U_Ent) then
4781 Set_RM_Size (U_Ent, Size);
4783 -- For elementary types, increase Object_Size to power of 2,
4784 -- but not less than a storage unit in any case (normally
4785 -- this means it will be byte addressable).
4787 -- For all other types, nothing else to do, we leave Esize
4788 -- (object size) unset, the back end will set it from the
4789 -- size and alignment in an appropriate manner.
4791 -- In both cases, we check whether the alignment must be
4792 -- reset in the wake of the size change.
4794 if Is_Elementary_Type (U_Ent) then
4795 if Size <= System_Storage_Unit then
4796 Init_Esize (U_Ent, System_Storage_Unit);
4797 elsif Size <= 16 then
4798 Init_Esize (U_Ent, 16);
4799 elsif Size <= 32 then
4800 Init_Esize (U_Ent, 32);
4802 Set_Esize (U_Ent, (Size + 63) / 64 * 64);
4805 Alignment_Check_For_Size_Change (U_Ent, Esize (U_Ent));
4807 Alignment_Check_For_Size_Change (U_Ent, Size);
4810 -- For objects, set Esize only
4813 if Is_Elementary_Type (Etyp) then
4814 if Size /= System_Storage_Unit
4816 Size /= System_Storage_Unit * 2
4818 Size /= System_Storage_Unit * 4
4820 Size /= System_Storage_Unit * 8
4822 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
4823 Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
4825 ("size for primitive object must be a power of 2"
4826 & " in the range ^-^", N);
4830 Set_Esize (U_Ent, Size);
4833 Set_Has_Size_Clause (U_Ent);
4841 -- Small attribute definition clause
4843 when Attribute_Small => Small : declare
4844 Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
4848 Analyze_And_Resolve (Expr, Any_Real);
4850 if Etype (Expr) = Any_Type then
4853 elsif not Is_Static_Expression (Expr) then
4854 Flag_Non_Static_Expr
4855 ("small requires static expression!", Expr);
4859 Small := Expr_Value_R (Expr);
4861 if Small <= Ureal_0 then
4862 Error_Msg_N ("small value must be greater than zero", Expr);
4868 if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
4870 ("small requires an ordinary fixed point type", Nam);
4872 elsif Has_Small_Clause (U_Ent) then
4873 Error_Msg_N ("small already given for &", Nam);
4875 elsif Small > Delta_Value (U_Ent) then
4877 ("small value must not be greater than delta value", Nam);
4880 Set_Small_Value (U_Ent, Small);
4881 Set_Small_Value (Implicit_Base, Small);
4882 Set_Has_Small_Clause (U_Ent);
4883 Set_Has_Small_Clause (Implicit_Base);
4884 Set_Has_Non_Standard_Rep (Implicit_Base);
4892 -- Storage_Pool attribute definition clause
4894 when Attribute_Storage_Pool | Attribute_Simple_Storage_Pool => declare
4899 if Ekind (U_Ent) = E_Access_Subprogram_Type then
4901 ("storage pool cannot be given for access-to-subprogram type",
4906 Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
4909 ("storage pool can only be given for access types", Nam);
4912 elsif Is_Derived_Type (U_Ent) then
4914 ("storage pool cannot be given for a derived access type",
4917 elsif Duplicate_Clause then
4920 elsif Present (Associated_Storage_Pool (U_Ent)) then
4921 Error_Msg_N ("storage pool already given for &", Nam);
4925 -- Check for Storage_Size previously given
4928 SS : constant Node_Id :=
4929 Get_Attribute_Definition_Clause
4930 (U_Ent, Attribute_Storage_Size);
4932 if Present (SS) then
4933 Check_Pool_Size_Clash (U_Ent, N, SS);
4937 -- Storage_Pool case
4939 if Id = Attribute_Storage_Pool then
4941 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
4943 -- In the Simple_Storage_Pool case, we allow a variable of any
4944 -- simple storage pool type, so we Resolve without imposing an
4948 Analyze_And_Resolve (Expr);
4950 if not Present (Get_Rep_Pragma
4951 (Etype (Expr), Name_Simple_Storage_Pool_Type))
4954 ("expression must be of a simple storage pool type", Expr);
4958 if not Denotes_Variable (Expr) then
4959 Error_Msg_N ("storage pool must be a variable", Expr);
4963 if Nkind (Expr) = N_Type_Conversion then
4964 T := Etype (Expression (Expr));
4969 -- The Stack_Bounded_Pool is used internally for implementing
4970 -- access types with a Storage_Size. Since it only work properly
4971 -- when used on one specific type, we need to check that it is not
4972 -- hijacked improperly:
4974 -- type T is access Integer;
4975 -- for T'Storage_Size use n;
4976 -- type Q is access Float;
4977 -- for Q'Storage_Size use T'Storage_Size; -- incorrect
4979 if RTE_Available (RE_Stack_Bounded_Pool)
4980 and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
4982 Error_Msg_N ("non-shareable internal Pool", Expr);
4986 -- If the argument is a name that is not an entity name, then
4987 -- we construct a renaming operation to define an entity of
4988 -- type storage pool.
4990 if not Is_Entity_Name (Expr)
4991 and then Is_Object_Reference (Expr)
4993 Pool := Make_Temporary (Loc, 'P', Expr);
4996 Rnode : constant Node_Id :=
4997 Make_Object_Renaming_Declaration (Loc,
4998 Defining_Identifier => Pool,
5000 New_Occurrence_Of (Etype (Expr), Loc),
5004 -- If the attribute definition clause comes from an aspect
5005 -- clause, then insert the renaming before the associated
5006 -- entity's declaration, since the attribute clause has
5007 -- not yet been appended to the declaration list.
5009 if From_Aspect_Specification (N) then
5010 Insert_Before (Parent (Entity (N)), Rnode);
5012 Insert_Before (N, Rnode);
5016 Set_Associated_Storage_Pool (U_Ent, Pool);
5019 elsif Is_Entity_Name (Expr) then
5020 Pool := Entity (Expr);
5022 -- If pool is a renamed object, get original one. This can
5023 -- happen with an explicit renaming, and within instances.
5025 while Present (Renamed_Object (Pool))
5026 and then Is_Entity_Name (Renamed_Object (Pool))
5028 Pool := Entity (Renamed_Object (Pool));
5031 if Present (Renamed_Object (Pool))
5032 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
5033 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
5035 Pool := Entity (Expression (Renamed_Object (Pool)));
5038 Set_Associated_Storage_Pool (U_Ent, Pool);
5040 elsif Nkind (Expr) = N_Type_Conversion
5041 and then Is_Entity_Name (Expression (Expr))
5042 and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
5044 Pool := Entity (Expression (Expr));
5045 Set_Associated_Storage_Pool (U_Ent, Pool);
5048 Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
5057 -- Storage_Size attribute definition clause
5059 when Attribute_Storage_Size => Storage_Size : declare
5060 Btype : constant Entity_Id := Base_Type (U_Ent);
5063 if Is_Task_Type (U_Ent) then
5065 -- Check obsolescent (but never obsolescent if from aspect)
5067 if not From_Aspect_Specification (N) then
5068 Check_Restriction (No_Obsolescent_Features, N);
5070 if Warn_On_Obsolescent_Feature then
5072 ("?j?storage size clause for task is an " &
5073 "obsolescent feature (RM J.9)", N);
5074 Error_Msg_N ("\?j?use Storage_Size pragma instead", N);
5081 if not Is_Access_Type (U_Ent)
5082 and then Ekind (U_Ent) /= E_Task_Type
5084 Error_Msg_N ("storage size cannot be given for &", Nam);
5086 elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
5088 ("storage size cannot be given for a derived access type",
5091 elsif Duplicate_Clause then
5095 Analyze_And_Resolve (Expr, Any_Integer);
5097 if Is_Access_Type (U_Ent) then
5099 -- Check for Storage_Pool previously given
5102 SP : constant Node_Id :=
5103 Get_Attribute_Definition_Clause
5104 (U_Ent, Attribute_Storage_Pool);
5107 if Present (SP) then
5108 Check_Pool_Size_Clash (U_Ent, SP, N);
5112 -- Special case of for x'Storage_Size use 0
5114 if Is_OK_Static_Expression (Expr)
5115 and then Expr_Value (Expr) = 0
5117 Set_No_Pool_Assigned (Btype);
5121 Set_Has_Storage_Size_Clause (Btype);
5129 when Attribute_Stream_Size => Stream_Size : declare
5130 Size : constant Uint := Static_Integer (Expr);
5133 if Ada_Version <= Ada_95 then
5134 Check_Restriction (No_Implementation_Attributes, N);
5137 if Duplicate_Clause then
5140 elsif Is_Elementary_Type (U_Ent) then
5141 if Size /= System_Storage_Unit
5143 Size /= System_Storage_Unit * 2
5145 Size /= System_Storage_Unit * 4
5147 Size /= System_Storage_Unit * 8
5149 Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
5151 ("stream size for elementary type must be a"
5152 & " power of 2 and at least ^", N);
5154 elsif RM_Size (U_Ent) > Size then
5155 Error_Msg_Uint_1 := RM_Size (U_Ent);
5157 ("stream size for elementary type must be a"
5158 & " power of 2 and at least ^", N);
5161 Set_Has_Stream_Size_Clause (U_Ent);
5164 Error_Msg_N ("Stream_Size cannot be given for &", Nam);
5172 -- Value_Size attribute definition clause
5174 when Attribute_Value_Size => Value_Size : declare
5175 Size : constant Uint := Static_Integer (Expr);
5179 if not Is_Type (U_Ent) then
5180 Error_Msg_N ("Value_Size cannot be given for &", Nam);
5182 elsif Duplicate_Clause then
5185 elsif Is_Array_Type (U_Ent)
5186 and then not Is_Constrained (U_Ent)
5189 ("Value_Size cannot be given for unconstrained array", Nam);
5192 if Is_Elementary_Type (U_Ent) then
5193 Check_Size (Expr, U_Ent, Size, Biased);
5194 Set_Biased (U_Ent, N, "value size clause", Biased);
5197 Set_RM_Size (U_Ent, Size);
5201 -----------------------
5202 -- Variable_Indexing --
5203 -----------------------
5205 when Attribute_Variable_Indexing =>
5206 Check_Indexing_Functions;
5212 when Attribute_Write =>
5213 Analyze_Stream_TSS_Definition (TSS_Stream_Write);
5214 Set_Has_Specified_Stream_Write (Ent);
5216 -- All other attributes cannot be set
5220 ("attribute& cannot be set with definition clause", N);
5223 -- The test for the type being frozen must be performed after any
5224 -- expression the clause has been analyzed since the expression itself
5225 -- might cause freezing that makes the clause illegal.
5227 if Rep_Item_Too_Late (U_Ent, N, FOnly) then
5230 end Analyze_Attribute_Definition_Clause;
5232 ----------------------------
5233 -- Analyze_Code_Statement --
5234 ----------------------------
5236 procedure Analyze_Code_Statement (N : Node_Id) is
5237 HSS : constant Node_Id := Parent (N);
5238 SBody : constant Node_Id := Parent (HSS);
5239 Subp : constant Entity_Id := Current_Scope;
5246 -- Analyze and check we get right type, note that this implements the
5247 -- requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
5248 -- is the only way that Asm_Insn could possibly be visible.
5250 Analyze_And_Resolve (Expression (N));
5252 if Etype (Expression (N)) = Any_Type then
5254 elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
5255 Error_Msg_N ("incorrect type for code statement", N);
5259 Check_Code_Statement (N);
5261 -- Make sure we appear in the handled statement sequence of a
5262 -- subprogram (RM 13.8(3)).
5264 if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
5265 or else Nkind (SBody) /= N_Subprogram_Body
5268 ("code statement can only appear in body of subprogram", N);
5272 -- Do remaining checks (RM 13.8(3)) if not already done
5274 if not Is_Machine_Code_Subprogram (Subp) then
5275 Set_Is_Machine_Code_Subprogram (Subp);
5277 -- No exception handlers allowed
5279 if Present (Exception_Handlers (HSS)) then
5281 ("exception handlers not permitted in machine code subprogram",
5282 First (Exception_Handlers (HSS)));
5285 -- No declarations other than use clauses and pragmas (we allow
5286 -- certain internally generated declarations as well).
5288 Decl := First (Declarations (SBody));
5289 while Present (Decl) loop
5290 DeclO := Original_Node (Decl);
5291 if Comes_From_Source (DeclO)
5292 and not Nkind_In (DeclO, N_Pragma,
5293 N_Use_Package_Clause,
5295 N_Implicit_Label_Declaration)
5298 ("this declaration not allowed in machine code subprogram",
5305 -- No statements other than code statements, pragmas, and labels.
5306 -- Again we allow certain internally generated statements.
5308 -- In Ada 2012, qualified expressions are names, and the code
5309 -- statement is initially parsed as a procedure call.
5311 Stmt := First (Statements (HSS));
5312 while Present (Stmt) loop
5313 StmtO := Original_Node (Stmt);
5315 -- A procedure call transformed into a code statement is OK.
5317 if Ada_Version >= Ada_2012
5318 and then Nkind (StmtO) = N_Procedure_Call_Statement
5319 and then Nkind (Name (StmtO)) = N_Qualified_Expression
5323 elsif Comes_From_Source (StmtO)
5324 and then not Nkind_In (StmtO, N_Pragma,
5329 ("this statement is not allowed in machine code subprogram",
5336 end Analyze_Code_Statement;
5338 -----------------------------------------------
5339 -- Analyze_Enumeration_Representation_Clause --
5340 -----------------------------------------------
5342 procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
5343 Ident : constant Node_Id := Identifier (N);
5344 Aggr : constant Node_Id := Array_Aggregate (N);
5345 Enumtype : Entity_Id;
5352 Err : Boolean := False;
5353 -- Set True to avoid cascade errors and crashes on incorrect source code
5355 Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
5356 Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
5357 -- Allowed range of universal integer (= allowed range of enum lit vals)
5361 -- Minimum and maximum values of entries
5364 -- Pointer to node for literal providing max value
5367 if Ignore_Rep_Clauses then
5368 Kill_Rep_Clause (N);
5372 -- Ignore enumeration rep clauses by default in CodePeer mode,
5373 -- unless -gnatd.I is specified, as a work around for potential false
5374 -- positive messages.
5376 if CodePeer_Mode and not Debug_Flag_Dot_II then
5380 -- First some basic error checks
5383 Enumtype := Entity (Ident);
5385 if Enumtype = Any_Type
5386 or else Rep_Item_Too_Early (Enumtype, N)
5390 Enumtype := Underlying_Type (Enumtype);
5393 if not Is_Enumeration_Type (Enumtype) then
5395 ("enumeration type required, found}",
5396 Ident, First_Subtype (Enumtype));
5400 -- Ignore rep clause on generic actual type. This will already have
5401 -- been flagged on the template as an error, and this is the safest
5402 -- way to ensure we don't get a junk cascaded message in the instance.
5404 if Is_Generic_Actual_Type (Enumtype) then
5407 -- Type must be in current scope
5409 elsif Scope (Enumtype) /= Current_Scope then
5410 Error_Msg_N ("type must be declared in this scope", Ident);
5413 -- Type must be a first subtype
5415 elsif not Is_First_Subtype (Enumtype) then
5416 Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
5419 -- Ignore duplicate rep clause
5421 elsif Has_Enumeration_Rep_Clause (Enumtype) then
5422 Error_Msg_N ("duplicate enumeration rep clause ignored", N);
5425 -- Don't allow rep clause for standard [wide_[wide_]]character
5427 elsif Is_Standard_Character_Type (Enumtype) then
5428 Error_Msg_N ("enumeration rep clause not allowed for this type", N);
5431 -- Check that the expression is a proper aggregate (no parentheses)
5433 elsif Paren_Count (Aggr) /= 0 then
5435 ("extra parentheses surrounding aggregate not allowed",
5439 -- All tests passed, so set rep clause in place
5442 Set_Has_Enumeration_Rep_Clause (Enumtype);
5443 Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
5446 -- Now we process the aggregate. Note that we don't use the normal
5447 -- aggregate code for this purpose, because we don't want any of the
5448 -- normal expansion activities, and a number of special semantic
5449 -- rules apply (including the component type being any integer type)
5451 Elit := First_Literal (Enumtype);
5453 -- First the positional entries if any
5455 if Present (Expressions (Aggr)) then
5456 Expr := First (Expressions (Aggr));
5457 while Present (Expr) loop
5459 Error_Msg_N ("too many entries in aggregate", Expr);
5463 Val := Static_Integer (Expr);
5465 -- Err signals that we found some incorrect entries processing
5466 -- the list. The final checks for completeness and ordering are
5467 -- skipped in this case.
5469 if Val = No_Uint then
5471 elsif Val < Lo or else Hi < Val then
5472 Error_Msg_N ("value outside permitted range", Expr);
5476 Set_Enumeration_Rep (Elit, Val);
5477 Set_Enumeration_Rep_Expr (Elit, Expr);
5483 -- Now process the named entries if present
5485 if Present (Component_Associations (Aggr)) then
5486 Assoc := First (Component_Associations (Aggr));
5487 while Present (Assoc) loop
5488 Choice := First (Choices (Assoc));
5490 if Present (Next (Choice)) then
5492 ("multiple choice not allowed here", Next (Choice));
5496 if Nkind (Choice) = N_Others_Choice then
5497 Error_Msg_N ("others choice not allowed here", Choice);
5500 elsif Nkind (Choice) = N_Range then
5502 -- ??? should allow zero/one element range here
5504 Error_Msg_N ("range not allowed here", Choice);
5508 Analyze_And_Resolve (Choice, Enumtype);
5510 if Error_Posted (Choice) then
5515 if Is_Entity_Name (Choice)
5516 and then Is_Type (Entity (Choice))
5518 Error_Msg_N ("subtype name not allowed here", Choice);
5521 -- ??? should allow static subtype with zero/one entry
5523 elsif Etype (Choice) = Base_Type (Enumtype) then
5524 if not Is_Static_Expression (Choice) then
5525 Flag_Non_Static_Expr
5526 ("non-static expression used for choice!", Choice);
5530 Elit := Expr_Value_E (Choice);
5532 if Present (Enumeration_Rep_Expr (Elit)) then
5534 Sloc (Enumeration_Rep_Expr (Elit));
5536 ("representation for& previously given#",
5541 Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
5543 Expr := Expression (Assoc);
5544 Val := Static_Integer (Expr);
5546 if Val = No_Uint then
5549 elsif Val < Lo or else Hi < Val then
5550 Error_Msg_N ("value outside permitted range", Expr);
5554 Set_Enumeration_Rep (Elit, Val);
5564 -- Aggregate is fully processed. Now we check that a full set of
5565 -- representations was given, and that they are in range and in order.
5566 -- These checks are only done if no other errors occurred.
5572 Elit := First_Literal (Enumtype);
5573 while Present (Elit) loop
5574 if No (Enumeration_Rep_Expr (Elit)) then
5575 Error_Msg_NE ("missing representation for&!", N, Elit);
5578 Val := Enumeration_Rep (Elit);
5580 if Min = No_Uint then
5584 if Val /= No_Uint then
5585 if Max /= No_Uint and then Val <= Max then
5587 ("enumeration value for& not ordered!",
5588 Enumeration_Rep_Expr (Elit), Elit);
5591 Max_Node := Enumeration_Rep_Expr (Elit);
5595 -- If there is at least one literal whose representation is not
5596 -- equal to the Pos value, then note that this enumeration type
5597 -- has a non-standard representation.
5599 if Val /= Enumeration_Pos (Elit) then
5600 Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
5607 -- Now set proper size information
5610 Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
5613 if Has_Size_Clause (Enumtype) then
5615 -- All OK, if size is OK now
5617 if RM_Size (Enumtype) >= Minsize then
5621 -- Try if we can get by with biasing
5624 UI_From_Int (Minimum_Size (Enumtype, Biased => True));
5626 -- Error message if even biasing does not work
5628 if RM_Size (Enumtype) < Minsize then
5629 Error_Msg_Uint_1 := RM_Size (Enumtype);
5630 Error_Msg_Uint_2 := Max;
5632 ("previously given size (^) is too small "
5633 & "for this value (^)", Max_Node);
5635 -- If biasing worked, indicate that we now have biased rep
5639 (Enumtype, Size_Clause (Enumtype), "size clause");
5644 Set_RM_Size (Enumtype, Minsize);
5645 Set_Enum_Esize (Enumtype);
5648 Set_RM_Size (Base_Type (Enumtype), RM_Size (Enumtype));
5649 Set_Esize (Base_Type (Enumtype), Esize (Enumtype));
5650 Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
5654 -- We repeat the too late test in case it froze itself
5656 if Rep_Item_Too_Late (Enumtype, N) then
5659 end Analyze_Enumeration_Representation_Clause;
5661 ----------------------------
5662 -- Analyze_Free_Statement --
5663 ----------------------------
5665 procedure Analyze_Free_Statement (N : Node_Id) is
5667 Analyze (Expression (N));
5668 end Analyze_Free_Statement;
5670 ---------------------------
5671 -- Analyze_Freeze_Entity --
5672 ---------------------------
5674 procedure Analyze_Freeze_Entity (N : Node_Id) is
5676 Freeze_Entity_Checks (N);
5677 end Analyze_Freeze_Entity;
5679 -----------------------------------
5680 -- Analyze_Freeze_Generic_Entity --
5681 -----------------------------------
5683 procedure Analyze_Freeze_Generic_Entity (N : Node_Id) is
5685 Freeze_Entity_Checks (N);
5686 end Analyze_Freeze_Generic_Entity;
5688 ------------------------------------------
5689 -- Analyze_Record_Representation_Clause --
5690 ------------------------------------------
5692 -- Note: we check as much as we can here, but we can't do any checks
5693 -- based on the position values (e.g. overlap checks) until freeze time
5694 -- because especially in Ada 2005 (machine scalar mode), the processing
5695 -- for non-standard bit order can substantially change the positions.
5696 -- See procedure Check_Record_Representation_Clause (called from Freeze)
5697 -- for the remainder of this processing.
5699 procedure Analyze_Record_Representation_Clause (N : Node_Id) is
5700 Ident : constant Node_Id := Identifier (N);
5705 Hbit : Uint := Uint_0;
5709 Rectype : Entity_Id;
5712 function Is_Inherited (Comp : Entity_Id) return Boolean;
5713 -- True if Comp is an inherited component in a record extension
5719 function Is_Inherited (Comp : Entity_Id) return Boolean is
5720 Comp_Base : Entity_Id;
5723 if Ekind (Rectype) = E_Record_Subtype then
5724 Comp_Base := Original_Record_Component (Comp);
5729 return Comp_Base /= Original_Record_Component (Comp_Base);
5734 Is_Record_Extension : Boolean;
5735 -- True if Rectype is a record extension
5737 CR_Pragma : Node_Id := Empty;
5738 -- Points to N_Pragma node if Complete_Representation pragma present
5740 -- Start of processing for Analyze_Record_Representation_Clause
5743 if Ignore_Rep_Clauses then
5744 Kill_Rep_Clause (N);
5749 Rectype := Entity (Ident);
5751 if Rectype = Any_Type or else Rep_Item_Too_Early (Rectype, N) then
5754 Rectype := Underlying_Type (Rectype);
5757 -- First some basic error checks
5759 if not Is_Record_Type (Rectype) then
5761 ("record type required, found}", Ident, First_Subtype (Rectype));
5764 elsif Scope (Rectype) /= Current_Scope then
5765 Error_Msg_N ("type must be declared in this scope", N);
5768 elsif not Is_First_Subtype (Rectype) then
5769 Error_Msg_N ("cannot give record rep clause for subtype", N);
5772 elsif Has_Record_Rep_Clause (Rectype) then
5773 Error_Msg_N ("duplicate record rep clause ignored", N);
5776 elsif Rep_Item_Too_Late (Rectype, N) then
5780 -- We know we have a first subtype, now possibly go the the anonymous
5781 -- base type to determine whether Rectype is a record extension.
5783 Recdef := Type_Definition (Declaration_Node (Base_Type (Rectype)));
5784 Is_Record_Extension :=
5785 Nkind (Recdef) = N_Derived_Type_Definition
5786 and then Present (Record_Extension_Part (Recdef));
5788 if Present (Mod_Clause (N)) then
5790 Loc : constant Source_Ptr := Sloc (N);
5791 M : constant Node_Id := Mod_Clause (N);
5792 P : constant List_Id := Pragmas_Before (M);
5796 pragma Warnings (Off, Mod_Val);
5799 Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
5801 if Warn_On_Obsolescent_Feature then
5803 ("?j?mod clause is an obsolescent feature (RM J.8)", N);
5805 ("\?j?use alignment attribute definition clause instead", N);
5812 -- In ASIS_Mode mode, expansion is disabled, but we must convert
5813 -- the Mod clause into an alignment clause anyway, so that the
5814 -- back-end can compute and back-annotate properly the size and
5815 -- alignment of types that may include this record.
5817 -- This seems dubious, this destroys the source tree in a manner
5818 -- not detectable by ASIS ???
5820 if Operating_Mode = Check_Semantics and then ASIS_Mode then
5822 Make_Attribute_Definition_Clause (Loc,
5823 Name => New_Occurrence_Of (Base_Type (Rectype), Loc),
5824 Chars => Name_Alignment,
5825 Expression => Relocate_Node (Expression (M)));
5827 Set_From_At_Mod (AtM_Nod);
5828 Insert_After (N, AtM_Nod);
5829 Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
5830 Set_Mod_Clause (N, Empty);
5833 -- Get the alignment value to perform error checking
5835 Mod_Val := Get_Alignment_Value (Expression (M));
5840 -- For untagged types, clear any existing component clauses for the
5841 -- type. If the type is derived, this is what allows us to override
5842 -- a rep clause for the parent. For type extensions, the representation
5843 -- of the inherited components is inherited, so we want to keep previous
5844 -- component clauses for completeness.
5846 if not Is_Tagged_Type (Rectype) then
5847 Comp := First_Component_Or_Discriminant (Rectype);
5848 while Present (Comp) loop
5849 Set_Component_Clause (Comp, Empty);
5850 Next_Component_Or_Discriminant (Comp);
5854 -- All done if no component clauses
5856 CC := First (Component_Clauses (N));
5862 -- A representation like this applies to the base type
5864 Set_Has_Record_Rep_Clause (Base_Type (Rectype));
5865 Set_Has_Non_Standard_Rep (Base_Type (Rectype));
5866 Set_Has_Specified_Layout (Base_Type (Rectype));
5868 -- Process the component clauses
5870 while Present (CC) loop
5874 if Nkind (CC) = N_Pragma then
5877 -- The only pragma of interest is Complete_Representation
5879 if Pragma_Name (CC) = Name_Complete_Representation then
5883 -- Processing for real component clause
5886 Posit := Static_Integer (Position (CC));
5887 Fbit := Static_Integer (First_Bit (CC));
5888 Lbit := Static_Integer (Last_Bit (CC));
5891 and then Fbit /= No_Uint
5892 and then Lbit /= No_Uint
5896 ("position cannot be negative", Position (CC));
5900 ("first bit cannot be negative", First_Bit (CC));
5902 -- The Last_Bit specified in a component clause must not be
5903 -- less than the First_Bit minus one (RM-13.5.1(10)).
5905 elsif Lbit < Fbit - 1 then
5907 ("last bit cannot be less than first bit minus one",
5910 -- Values look OK, so find the corresponding record component
5911 -- Even though the syntax allows an attribute reference for
5912 -- implementation-defined components, GNAT does not allow the
5913 -- tag to get an explicit position.
5915 elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
5916 if Attribute_Name (Component_Name (CC)) = Name_Tag then
5917 Error_Msg_N ("position of tag cannot be specified", CC);
5919 Error_Msg_N ("illegal component name", CC);
5923 Comp := First_Entity (Rectype);
5924 while Present (Comp) loop
5925 exit when Chars (Comp) = Chars (Component_Name (CC));
5931 -- Maybe component of base type that is absent from
5932 -- statically constrained first subtype.
5934 Comp := First_Entity (Base_Type (Rectype));
5935 while Present (Comp) loop
5936 exit when Chars (Comp) = Chars (Component_Name (CC));
5943 ("component clause is for non-existent field", CC);
5945 -- Ada 2012 (AI05-0026): Any name that denotes a
5946 -- discriminant of an object of an unchecked union type
5947 -- shall not occur within a record_representation_clause.
5949 -- The general restriction of using record rep clauses on
5950 -- Unchecked_Union types has now been lifted. Since it is
5951 -- possible to introduce a record rep clause which mentions
5952 -- the discriminant of an Unchecked_Union in non-Ada 2012
5953 -- code, this check is applied to all versions of the
5956 elsif Ekind (Comp) = E_Discriminant
5957 and then Is_Unchecked_Union (Rectype)
5960 ("cannot reference discriminant of unchecked union",
5961 Component_Name (CC));
5963 elsif Is_Record_Extension and then Is_Inherited (Comp) then
5965 ("component clause not allowed for inherited "
5966 & "component&", CC, Comp);
5968 elsif Present (Component_Clause (Comp)) then
5970 -- Diagnose duplicate rep clause, or check consistency
5971 -- if this is an inherited component. In a double fault,
5972 -- there may be a duplicate inconsistent clause for an
5973 -- inherited component.
5975 if Scope (Original_Record_Component (Comp)) = Rectype
5976 or else Parent (Component_Clause (Comp)) = N
5978 Error_Msg_Sloc := Sloc (Component_Clause (Comp));
5979 Error_Msg_N ("component clause previously given#", CC);
5983 Rep1 : constant Node_Id := Component_Clause (Comp);
5985 if Intval (Position (Rep1)) /=
5986 Intval (Position (CC))
5987 or else Intval (First_Bit (Rep1)) /=
5988 Intval (First_Bit (CC))
5989 or else Intval (Last_Bit (Rep1)) /=
5990 Intval (Last_Bit (CC))
5993 ("component clause inconsistent "
5994 & "with representation of ancestor", CC);
5996 elsif Warn_On_Redundant_Constructs then
5998 ("?r?redundant confirming component clause "
5999 & "for component!", CC);
6004 -- Normal case where this is the first component clause we
6005 -- have seen for this entity, so set it up properly.
6008 -- Make reference for field in record rep clause and set
6009 -- appropriate entity field in the field identifier.
6012 (Comp, Component_Name (CC), Set_Ref => False);
6013 Set_Entity (Component_Name (CC), Comp);
6015 -- Update Fbit and Lbit to the actual bit number
6017 Fbit := Fbit + UI_From_Int (SSU) * Posit;
6018 Lbit := Lbit + UI_From_Int (SSU) * Posit;
6020 if Has_Size_Clause (Rectype)
6021 and then RM_Size (Rectype) <= Lbit
6024 ("bit number out of range of specified size",
6027 Set_Component_Clause (Comp, CC);
6028 Set_Component_Bit_Offset (Comp, Fbit);
6029 Set_Esize (Comp, 1 + (Lbit - Fbit));
6030 Set_Normalized_First_Bit (Comp, Fbit mod SSU);
6031 Set_Normalized_Position (Comp, Fbit / SSU);
6033 if Warn_On_Overridden_Size
6034 and then Has_Size_Clause (Etype (Comp))
6035 and then RM_Size (Etype (Comp)) /= Esize (Comp)
6038 ("?S?component size overrides size clause for&",
6039 Component_Name (CC), Etype (Comp));
6042 -- This information is also set in the corresponding
6043 -- component of the base type, found by accessing the
6044 -- Original_Record_Component link if it is present.
6046 Ocomp := Original_Record_Component (Comp);
6053 (Component_Name (CC),
6059 (Comp, First_Node (CC), "component clause", Biased);
6061 if Present (Ocomp) then
6062 Set_Component_Clause (Ocomp, CC);
6063 Set_Component_Bit_Offset (Ocomp, Fbit);
6064 Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
6065 Set_Normalized_Position (Ocomp, Fbit / SSU);
6066 Set_Esize (Ocomp, 1 + (Lbit - Fbit));
6068 Set_Normalized_Position_Max
6069 (Ocomp, Normalized_Position (Ocomp));
6071 -- Note: we don't use Set_Biased here, because we
6072 -- already gave a warning above if needed, and we
6073 -- would get a duplicate for the same name here.
6075 Set_Has_Biased_Representation
6076 (Ocomp, Has_Biased_Representation (Comp));
6079 if Esize (Comp) < 0 then
6080 Error_Msg_N ("component size is negative", CC);
6091 -- Check missing components if Complete_Representation pragma appeared
6093 if Present (CR_Pragma) then
6094 Comp := First_Component_Or_Discriminant (Rectype);
6095 while Present (Comp) loop
6096 if No (Component_Clause (Comp)) then
6098 ("missing component clause for &", CR_Pragma, Comp);
6101 Next_Component_Or_Discriminant (Comp);
6104 -- Give missing components warning if required
6106 elsif Warn_On_Unrepped_Components then
6108 Num_Repped_Components : Nat := 0;
6109 Num_Unrepped_Components : Nat := 0;
6112 -- First count number of repped and unrepped components
6114 Comp := First_Component_Or_Discriminant (Rectype);
6115 while Present (Comp) loop
6116 if Present (Component_Clause (Comp)) then
6117 Num_Repped_Components := Num_Repped_Components + 1;
6119 Num_Unrepped_Components := Num_Unrepped_Components + 1;
6122 Next_Component_Or_Discriminant (Comp);
6125 -- We are only interested in the case where there is at least one
6126 -- unrepped component, and at least half the components have rep
6127 -- clauses. We figure that if less than half have them, then the
6128 -- partial rep clause is really intentional. If the component
6129 -- type has no underlying type set at this point (as for a generic
6130 -- formal type), we don't know enough to give a warning on the
6133 if Num_Unrepped_Components > 0
6134 and then Num_Unrepped_Components < Num_Repped_Components
6136 Comp := First_Component_Or_Discriminant (Rectype);
6137 while Present (Comp) loop
6138 if No (Component_Clause (Comp))
6139 and then Comes_From_Source (Comp)
6140 and then Present (Underlying_Type (Etype (Comp)))
6141 and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
6142 or else Size_Known_At_Compile_Time
6143 (Underlying_Type (Etype (Comp))))
6144 and then not Has_Warnings_Off (Rectype)
6146 Error_Msg_Sloc := Sloc (Comp);
6148 ("?C?no component clause given for & declared #",
6152 Next_Component_Or_Discriminant (Comp);
6157 end Analyze_Record_Representation_Clause;
6159 -------------------------------------
6160 -- Build_Discrete_Static_Predicate --
6161 -------------------------------------
6163 procedure Build_Discrete_Static_Predicate
6168 Loc : constant Source_Ptr := Sloc (Expr);
6170 Non_Static : exception;
6171 -- Raised if something non-static is found
6173 Btyp : constant Entity_Id := Base_Type (Typ);
6175 BLo : constant Uint := Expr_Value (Type_Low_Bound (Btyp));
6176 BHi : constant Uint := Expr_Value (Type_High_Bound (Btyp));
6177 -- Low bound and high bound value of base type of Typ
6179 TLo : constant Uint := Expr_Value (Type_Low_Bound (Typ));
6180 THi : constant Uint := Expr_Value (Type_High_Bound (Typ));
6181 -- Low bound and high bound values of static subtype Typ
6186 -- One entry in a Rlist value, a single REnt (range entry) value denotes
6187 -- one range from Lo to Hi. To represent a single value range Lo = Hi =
6190 type RList is array (Nat range <>) of REnt;
6191 -- A list of ranges. The ranges are sorted in increasing order, and are
6192 -- disjoint (there is a gap of at least one value between each range in
6193 -- the table). A value is in the set of ranges in Rlist if it lies
6194 -- within one of these ranges.
6196 False_Range : constant RList :=
6197 RList'(1 .. 0 => REnt'(No_Uint, No_Uint));
6198 -- An empty set of ranges represents a range list that can never be
6199 -- satisfied, since there are no ranges in which the value could lie,
6200 -- so it does not lie in any of them. False_Range is a canonical value
6201 -- for this empty set, but general processing should test for an Rlist
6202 -- with length zero (see Is_False predicate), since other null ranges
6203 -- may appear which must be treated as False.
6205 True_Range : constant RList := RList'(1 => REnt'(BLo, BHi));
6206 -- Range representing True, value must be in the base range
6208 function "and" (Left : RList; Right : RList) return RList;
6209 -- And's together two range lists, returning a range list. This is a set
6210 -- intersection operation.
6212 function "or" (Left : RList; Right : RList) return RList;
6213 -- Or's together two range lists, returning a range list. This is a set
6216 function "not" (Right : RList) return RList;
6217 -- Returns complement of a given range list, i.e. a range list
6218 -- representing all the values in TLo .. THi that are not in the input
6221 function Build_Val (V : Uint) return Node_Id;
6222 -- Return an analyzed N_Identifier node referencing this value, suitable
6223 -- for use as an entry in the Static_Predicate list. This node is typed
6224 -- with the base type.
6226 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id;
6227 -- Return an analyzed N_Range node referencing this range, suitable for
6228 -- use as an entry in the Static_Predicate list. This node is typed with
6231 function Get_RList (Exp : Node_Id) return RList;
6232 -- This is a recursive routine that converts the given expression into a
6233 -- list of ranges, suitable for use in building the static predicate.
6235 function Is_False (R : RList) return Boolean;
6236 pragma Inline (Is_False);
6237 -- Returns True if the given range list is empty, and thus represents a
6238 -- False list of ranges that can never be satisfied.
6240 function Is_True (R : RList) return Boolean;
6241 -- Returns True if R trivially represents the True predicate by having a
6242 -- single range from BLo to BHi.
6244 function Is_Type_Ref (N : Node_Id) return Boolean;
6245 pragma Inline (Is_Type_Ref);
6246 -- Returns if True if N is a reference to the type for the predicate in
6247 -- the expression (i.e. if it is an identifier whose Chars field matches
6248 -- the Nam given in the call).
6250 function Lo_Val (N : Node_Id) return Uint;
6251 -- Given static expression or static range from a Static_Predicate list,
6252 -- gets expression value or low bound of range.
6254 function Hi_Val (N : Node_Id) return Uint;
6255 -- Given static expression or static range from a Static_Predicate list,
6256 -- gets expression value of high bound of range.
6258 function Membership_Entry (N : Node_Id) return RList;
6259 -- Given a single membership entry (range, value, or subtype), returns
6260 -- the corresponding range list. Raises Static_Error if not static.
6262 function Membership_Entries (N : Node_Id) return RList;
6263 -- Given an element on an alternatives list of a membership operation,
6264 -- returns the range list corresponding to this entry and all following
6265 -- entries (i.e. returns the "or" of this list of values).
6267 function Stat_Pred (Typ : Entity_Id) return RList;
6268 -- Given a type, if it has a static predicate, then return the predicate
6269 -- as a range list, otherwise raise Non_Static.
6275 function "and" (Left : RList; Right : RList) return RList is
6277 -- First range of result
6279 SLeft : Nat := Left'First;
6280 -- Start of rest of left entries
6282 SRight : Nat := Right'First;
6283 -- Start of rest of right entries
6286 -- If either range is True, return the other
6288 if Is_True (Left) then
6290 elsif Is_True (Right) then
6294 -- If either range is False, return False
6296 if Is_False (Left) or else Is_False (Right) then
6300 -- Loop to remove entries at start that are disjoint, and thus just
6301 -- get discarded from the result entirely.
6304 -- If no operands left in either operand, result is false
6306 if SLeft > Left'Last or else SRight > Right'Last then
6309 -- Discard first left operand entry if disjoint with right
6311 elsif Left (SLeft).Hi < Right (SRight).Lo then
6314 -- Discard first right operand entry if disjoint with left
6316 elsif Right (SRight).Hi < Left (SLeft).Lo then
6317 SRight := SRight + 1;
6319 -- Otherwise we have an overlapping entry
6326 -- Now we have two non-null operands, and first entries overlap. The
6327 -- first entry in the result will be the overlapping part of these
6330 FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo),
6331 Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi));
6333 -- Now we can remove the entry that ended at a lower value, since its
6334 -- contribution is entirely contained in Fent.
6336 if Left (SLeft).Hi <= Right (SRight).Hi then
6339 SRight := SRight + 1;
6342 -- Compute result by concatenating this first entry with the "and" of
6343 -- the remaining parts of the left and right operands. Note that if
6344 -- either of these is empty, "and" will yield empty, so that we will
6345 -- end up with just Fent, which is what we want in that case.
6348 FEnt & (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last));
6355 function "not" (Right : RList) return RList is
6357 -- Return True if False range
6359 if Is_False (Right) then
6363 -- Return False if True range
6365 if Is_True (Right) then
6369 -- Here if not trivial case
6372 Result : RList (1 .. Right'Length + 1);
6373 -- May need one more entry for gap at beginning and end
6376 -- Number of entries stored in Result
6381 if Right (Right'First).Lo > TLo then
6383 Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1);
6386 -- Gaps between ranges
6388 for J in Right'First .. Right'Last - 1 loop
6390 Result (Count) := REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1);
6395 if Right (Right'Last).Hi < THi then
6397 Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi);
6400 return Result (1 .. Count);
6408 function "or" (Left : RList; Right : RList) return RList is
6410 -- First range of result
6412 SLeft : Nat := Left'First;
6413 -- Start of rest of left entries
6415 SRight : Nat := Right'First;
6416 -- Start of rest of right entries
6419 -- If either range is True, return True
6421 if Is_True (Left) or else Is_True (Right) then
6425 -- If either range is False (empty), return the other
6427 if Is_False (Left) then
6429 elsif Is_False (Right) then
6433 -- Initialize result first entry from left or right operand depending
6434 -- on which starts with the lower range.
6436 if Left (SLeft).Lo < Right (SRight).Lo then
6437 FEnt := Left (SLeft);
6440 FEnt := Right (SRight);
6441 SRight := SRight + 1;
6444 -- This loop eats ranges from left and right operands that are
6445 -- contiguous with the first range we are gathering.
6448 -- Eat first entry in left operand if contiguous or overlapped by
6449 -- gathered first operand of result.
6451 if SLeft <= Left'Last
6452 and then Left (SLeft).Lo <= FEnt.Hi + 1
6454 FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi);
6457 -- Eat first entry in right operand if contiguous or overlapped by
6458 -- gathered right operand of result.
6460 elsif SRight <= Right'Last
6461 and then Right (SRight).Lo <= FEnt.Hi + 1
6463 FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi);
6464 SRight := SRight + 1;
6466 -- All done if no more entries to eat
6473 -- Obtain result as the first entry we just computed, concatenated
6474 -- to the "or" of the remaining results (if one operand is empty,
6475 -- this will just concatenate with the other
6478 FEnt & (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last));
6485 function Build_Range (Lo : Uint; Hi : Uint) return Node_Id is
6490 Low_Bound => Build_Val (Lo),
6491 High_Bound => Build_Val (Hi));
6492 Set_Etype (Result, Btyp);
6493 Set_Analyzed (Result);
6501 function Build_Val (V : Uint) return Node_Id is
6505 if Is_Enumeration_Type (Typ) then
6506 Result := Get_Enum_Lit_From_Pos (Typ, V, Loc);
6508 Result := Make_Integer_Literal (Loc, V);
6511 Set_Etype (Result, Btyp);
6512 Set_Is_Static_Expression (Result);
6513 Set_Analyzed (Result);
6521 function Get_RList (Exp : Node_Id) return RList is
6526 -- Static expression can only be true or false
6528 if Is_OK_Static_Expression (Exp) then
6529 if Expr_Value (Exp) = 0 then
6536 -- Otherwise test node type
6544 when N_Op_And | N_And_Then =>
6545 return Get_RList (Left_Opnd (Exp))
6547 Get_RList (Right_Opnd (Exp));
6551 when N_Op_Or | N_Or_Else =>
6552 return Get_RList (Left_Opnd (Exp))
6554 Get_RList (Right_Opnd (Exp));
6559 return not Get_RList (Right_Opnd (Exp));
6561 -- Comparisons of type with static value
6563 when N_Op_Compare =>
6565 -- Type is left operand
6567 if Is_Type_Ref (Left_Opnd (Exp))
6568 and then Is_OK_Static_Expression (Right_Opnd (Exp))
6570 Val := Expr_Value (Right_Opnd (Exp));
6572 -- Typ is right operand
6574 elsif Is_Type_Ref (Right_Opnd (Exp))
6575 and then Is_OK_Static_Expression (Left_Opnd (Exp))
6577 Val := Expr_Value (Left_Opnd (Exp));
6579 -- Invert sense of comparison
6582 when N_Op_Gt => Op := N_Op_Lt;
6583 when N_Op_Lt => Op := N_Op_Gt;
6584 when N_Op_Ge => Op := N_Op_Le;
6585 when N_Op_Le => Op := N_Op_Ge;
6586 when others => null;
6589 -- Other cases are non-static
6595 -- Construct range according to comparison operation
6599 return RList'(1 => REnt'(Val, Val));
6602 return RList'(1 => REnt'(Val, BHi));
6605 return RList'(1 => REnt'(Val + 1, BHi));
6608 return RList'(1 => REnt'(BLo, Val));
6611 return RList'(1 => REnt'(BLo, Val - 1));
6614 return RList'(REnt'(BLo, Val - 1), REnt'(Val + 1, BHi));
6617 raise Program_Error;
6623 if not Is_Type_Ref (Left_Opnd (Exp)) then
6627 if Present (Right_Opnd (Exp)) then
6628 return Membership_Entry (Right_Opnd (Exp));
6630 return Membership_Entries (First (Alternatives (Exp)));
6633 -- Negative membership (NOT IN)
6636 if not Is_Type_Ref (Left_Opnd (Exp)) then
6640 if Present (Right_Opnd (Exp)) then
6641 return not Membership_Entry (Right_Opnd (Exp));
6643 return not Membership_Entries (First (Alternatives (Exp)));
6646 -- Function call, may be call to static predicate
6648 when N_Function_Call =>
6649 if Is_Entity_Name (Name (Exp)) then
6651 Ent : constant Entity_Id := Entity (Name (Exp));
6653 if Is_Predicate_Function (Ent)
6655 Is_Predicate_Function_M (Ent)
6657 return Stat_Pred (Etype (First_Formal (Ent)));
6662 -- Other function call cases are non-static
6666 -- Qualified expression, dig out the expression
6668 when N_Qualified_Expression =>
6669 return Get_RList (Expression (Exp));
6671 when N_Case_Expression =>
6678 if not Is_Entity_Name (Expression (Expr))
6679 or else Etype (Expression (Expr)) /= Typ
6682 ("expression must denaote subtype", Expression (Expr));
6686 -- Collect discrete choices in all True alternatives
6688 Choices := New_List;
6689 Alt := First (Alternatives (Exp));
6690 while Present (Alt) loop
6691 Dep := Expression (Alt);
6693 if not Is_Static_Expression (Dep) then
6696 elsif Is_True (Expr_Value (Dep)) then
6697 Append_List_To (Choices,
6698 New_Copy_List (Discrete_Choices (Alt)));
6704 return Membership_Entries (First (Choices));
6707 -- Expression with actions: if no actions, dig out expression
6709 when N_Expression_With_Actions =>
6710 if Is_Empty_List (Actions (Exp)) then
6711 return Get_RList (Expression (Exp));
6719 return (Get_RList (Left_Opnd (Exp))
6720 and not Get_RList (Right_Opnd (Exp)))
6721 or (Get_RList (Right_Opnd (Exp))
6722 and not Get_RList (Left_Opnd (Exp)));
6724 -- Any other node type is non-static
6735 function Hi_Val (N : Node_Id) return Uint is
6737 if Is_Static_Expression (N) then
6738 return Expr_Value (N);
6740 pragma Assert (Nkind (N) = N_Range);
6741 return Expr_Value (High_Bound (N));
6749 function Is_False (R : RList) return Boolean is
6751 return R'Length = 0;
6758 function Is_True (R : RList) return Boolean is
6761 and then R (R'First).Lo = BLo
6762 and then R (R'First).Hi = BHi;
6769 function Is_Type_Ref (N : Node_Id) return Boolean is
6771 return Nkind (N) = N_Identifier and then Chars (N) = Nam;
6778 function Lo_Val (N : Node_Id) return Uint is
6780 if Is_Static_Expression (N) then
6781 return Expr_Value (N);
6783 pragma Assert (Nkind (N) = N_Range);
6784 return Expr_Value (Low_Bound (N));
6788 ------------------------
6789 -- Membership_Entries --
6790 ------------------------
6792 function Membership_Entries (N : Node_Id) return RList is
6794 if No (Next (N)) then
6795 return Membership_Entry (N);
6797 return Membership_Entry (N) or Membership_Entries (Next (N));
6799 end Membership_Entries;
6801 ----------------------
6802 -- Membership_Entry --
6803 ----------------------
6805 function Membership_Entry (N : Node_Id) return RList is
6813 if Nkind (N) = N_Range then
6814 if not Is_Static_Expression (Low_Bound (N))
6816 not Is_Static_Expression (High_Bound (N))
6820 SLo := Expr_Value (Low_Bound (N));
6821 SHi := Expr_Value (High_Bound (N));
6822 return RList'(1 => REnt'(SLo, SHi));
6825 -- Static expression case
6827 elsif Is_Static_Expression (N) then
6828 Val := Expr_Value (N);
6829 return RList'(1 => REnt'(Val, Val));
6831 -- Identifier (other than static expression) case
6833 else pragma Assert (Nkind (N) = N_Identifier);
6837 if Is_Type (Entity (N)) then
6839 -- If type has predicates, process them
6841 if Has_Predicates (Entity (N)) then
6842 return Stat_Pred (Entity (N));
6844 -- For static subtype without predicates, get range
6846 elsif Is_Static_Subtype (Entity (N)) then
6847 SLo := Expr_Value (Type_Low_Bound (Entity (N)));
6848 SHi := Expr_Value (Type_High_Bound (Entity (N)));
6849 return RList'(1 => REnt'(SLo, SHi));
6851 -- Any other type makes us non-static
6857 -- Any other kind of identifier in predicate (e.g. a non-static
6858 -- expression value) means this is not a static predicate.
6864 end Membership_Entry;
6870 function Stat_Pred (Typ : Entity_Id) return RList is
6872 -- Not static if type does not have static predicates
6874 if not Has_Predicates (Typ) or else No (Static_Predicate (Typ)) then
6878 -- Otherwise we convert the predicate list to a range list
6881 Result : RList (1 .. List_Length (Static_Predicate (Typ)));
6885 P := First (Static_Predicate (Typ));
6886 for J in Result'Range loop
6887 Result (J) := REnt'(Lo_Val (P), Hi_Val (P));
6895 -- Start of processing for Build_Discrete_Static_Predicate
6898 -- Analyze the expression to see if it is a static predicate
6901 Ranges : constant RList := Get_RList (Expr);
6902 -- Range list from expression if it is static
6907 -- Convert range list into a form for the static predicate. In the
6908 -- Ranges array, we just have raw ranges, these must be converted
6909 -- to properly typed and analyzed static expressions or range nodes.
6911 -- Note: here we limit ranges to the ranges of the subtype, so that
6912 -- a predicate is always false for values outside the subtype. That
6913 -- seems fine, such values are invalid anyway, and considering them
6914 -- to fail the predicate seems allowed and friendly, and furthermore
6915 -- simplifies processing for case statements and loops.
6919 for J in Ranges'Range loop
6921 Lo : Uint := Ranges (J).Lo;
6922 Hi : Uint := Ranges (J).Hi;
6925 -- Ignore completely out of range entry
6927 if Hi < TLo or else Lo > THi then
6930 -- Otherwise process entry
6933 -- Adjust out of range value to subtype range
6943 -- Convert range into required form
6945 Append_To (Plist, Build_Range (Lo, Hi));
6950 -- Processing was successful and all entries were static, so now we
6951 -- can store the result as the predicate list.
6953 Set_Static_Predicate (Typ, Plist);
6955 -- The processing for static predicates put the expression into
6956 -- canonical form as a series of ranges. It also eliminated
6957 -- duplicates and collapsed and combined ranges. We might as well
6958 -- replace the alternatives list of the right operand of the
6959 -- membership test with the static predicate list, which will
6960 -- usually be more efficient.
6963 New_Alts : constant List_Id := New_List;
6968 Old_Node := First (Plist);
6969 while Present (Old_Node) loop
6970 New_Node := New_Copy (Old_Node);
6972 if Nkind (New_Node) = N_Range then
6973 Set_Low_Bound (New_Node, New_Copy (Low_Bound (Old_Node)));
6974 Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node)));
6977 Append_To (New_Alts, New_Node);
6981 -- If empty list, replace by False
6983 if Is_Empty_List (New_Alts) then
6984 Rewrite (Expr, New_Occurrence_Of (Standard_False, Loc));
6986 -- Else replace by set membership test
6991 Left_Opnd => Make_Identifier (Loc, Nam),
6992 Right_Opnd => Empty,
6993 Alternatives => New_Alts));
6995 -- Resolve new expression in function context
6997 Install_Formals (Predicate_Function (Typ));
6998 Push_Scope (Predicate_Function (Typ));
6999 Analyze_And_Resolve (Expr, Standard_Boolean);
7005 -- If non-static, return doing nothing
7010 end Build_Discrete_Static_Predicate;
7012 -------------------------------------------
7013 -- Build_Invariant_Procedure_Declaration --
7014 -------------------------------------------
7016 function Build_Invariant_Procedure_Declaration
7017 (Typ : Entity_Id) return Node_Id
7019 Loc : constant Source_Ptr := Sloc (Typ);
7020 Object_Entity : constant Entity_Id :=
7021 Make_Defining_Identifier (Loc, New_Internal_Name ('I'));
7026 Set_Etype (Object_Entity, Typ);
7028 -- Check for duplicate definiations.
7030 if Has_Invariants (Typ) and then Present (Invariant_Procedure (Typ)) then
7035 Make_Defining_Identifier (Loc,
7036 Chars => New_External_Name (Chars (Typ), "Invariant"));
7037 Set_Has_Invariants (Typ);
7038 Set_Ekind (SId, E_Procedure);
7039 Set_Is_Invariant_Procedure (SId);
7040 Set_Invariant_Procedure (Typ, SId);
7043 Make_Procedure_Specification (Loc,
7044 Defining_Unit_Name => SId,
7045 Parameter_Specifications => New_List (
7046 Make_Parameter_Specification (Loc,
7047 Defining_Identifier => Object_Entity,
7048 Parameter_Type => New_Occurrence_Of (Typ, Loc))));
7050 return Make_Subprogram_Declaration (Loc, Specification => Spec);
7051 end Build_Invariant_Procedure_Declaration;
7053 -------------------------------
7054 -- Build_Invariant_Procedure --
7055 -------------------------------
7057 -- The procedure that is constructed here has the form
7059 -- procedure typInvariant (Ixxx : typ) is
7061 -- pragma Check (Invariant, exp, "failed invariant from xxx");
7062 -- pragma Check (Invariant, exp, "failed invariant from xxx");
7064 -- pragma Check (Invariant, exp, "failed inherited invariant from xxx");
7066 -- end typInvariant;
7068 procedure Build_Invariant_Procedure (Typ : Entity_Id; N : Node_Id) is
7069 Loc : constant Source_Ptr := Sloc (Typ);
7077 -- Name for Check pragma, usually Invariant, but might be Type_Invariant
7078 -- if we come from a Type_Invariant aspect, we make sure to build the
7079 -- Check pragma with the right name, so that Check_Policy works right.
7081 Visible_Decls : constant List_Id := Visible_Declarations (N);
7082 Private_Decls : constant List_Id := Private_Declarations (N);
7084 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean);
7085 -- Appends statements to Stmts for any invariants in the rep item chain
7086 -- of the given type. If Inherit is False, then we only process entries
7087 -- on the chain for the type Typ. If Inherit is True, then we ignore any
7088 -- Invariant aspects, but we process all Invariant'Class aspects, adding
7089 -- "inherited" to the exception message and generating an informational
7090 -- message about the inheritance of an invariant.
7092 Object_Name : Name_Id;
7093 -- Name for argument of invariant procedure
7095 Object_Entity : Node_Id;
7096 -- The entity of the formal for the procedure
7098 --------------------
7099 -- Add_Invariants --
7100 --------------------
7102 procedure Add_Invariants (T : Entity_Id; Inherit : Boolean) is
7112 procedure Replace_Type_Reference (N : Node_Id);
7113 -- Replace a single occurrence N of the subtype name with a reference
7114 -- to the formal of the predicate function. N can be an identifier
7115 -- referencing the subtype, or a selected component, representing an
7116 -- appropriately qualified occurrence of the subtype name.
7118 procedure Replace_Type_References is
7119 new Replace_Type_References_Generic (Replace_Type_Reference);
7120 -- Traverse an expression replacing all occurrences of the subtype
7121 -- name with appropriate references to the object that is the formal
7122 -- parameter of the predicate function. Note that we must ensure
7123 -- that the type and entity information is properly set in the
7124 -- replacement node, since we will do a Preanalyze call of this
7125 -- expression without proper visibility of the procedure argument.
7127 ----------------------------
7128 -- Replace_Type_Reference --
7129 ----------------------------
7131 -- Note: See comments in Add_Predicates.Replace_Type_Reference
7132 -- regarding handling of Sloc and Comes_From_Source.
7134 procedure Replace_Type_Reference (N : Node_Id) is
7137 -- Add semantic information to node to be rewritten, for ASIS
7138 -- navigation needs.
7140 if Nkind (N) = N_Identifier then
7144 elsif Nkind (N) = N_Selected_Component then
7145 Analyze (Prefix (N));
7146 Set_Entity (Selector_Name (N), T);
7147 Set_Etype (Selector_Name (N), T);
7150 -- Invariant'Class, replace with T'Class (obj)
7152 if Class_Present (Ritem) then
7154 Make_Type_Conversion (Sloc (N),
7156 Make_Attribute_Reference (Sloc (N),
7157 Prefix => New_Occurrence_Of (T, Sloc (N)),
7158 Attribute_Name => Name_Class),
7159 Expression => Make_Identifier (Sloc (N), Object_Name)));
7161 Set_Entity (Expression (N), Object_Entity);
7162 Set_Etype (Expression (N), Typ);
7164 -- Invariant, replace with obj
7167 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
7168 Set_Entity (N, Object_Entity);
7172 Set_Comes_From_Source (N, True);
7173 end Replace_Type_Reference;
7175 -- Start of processing for Add_Invariants
7178 Ritem := First_Rep_Item (T);
7179 while Present (Ritem) loop
7180 if Nkind (Ritem) = N_Pragma
7181 and then Pragma_Name (Ritem) = Name_Invariant
7183 Arg1 := First (Pragma_Argument_Associations (Ritem));
7184 Arg2 := Next (Arg1);
7185 Arg3 := Next (Arg2);
7187 Arg1 := Get_Pragma_Arg (Arg1);
7188 Arg2 := Get_Pragma_Arg (Arg2);
7190 -- For Inherit case, ignore Invariant, process only Class case
7193 if not Class_Present (Ritem) then
7197 -- For Inherit false, process only item for right type
7200 if Entity (Arg1) /= Typ then
7206 Stmts := Empty_List;
7209 Exp := New_Copy_Tree (Arg2);
7211 -- Preserve sloc of original pragma Invariant
7213 Loc := Sloc (Ritem);
7215 -- We need to replace any occurrences of the name of the type
7216 -- with references to the object, converted to type'Class in
7217 -- the case of Invariant'Class aspects.
7219 Replace_Type_References (Exp, Chars (T));
7221 -- If this invariant comes from an aspect, find the aspect
7222 -- specification, and replace the saved expression because
7223 -- we need the subtype references replaced for the calls to
7224 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
7225 -- and Check_Aspect_At_End_Of_Declarations.
7227 if From_Aspect_Specification (Ritem) then
7232 -- Loop to find corresponding aspect, note that this
7233 -- must be present given the pragma is marked delayed.
7235 -- Note: in practice Next_Rep_Item (Ritem) is Empty so
7236 -- this loop does nothing. Furthermore, why isn't this
7237 -- simply Corresponding_Aspect ???
7239 Aitem := Next_Rep_Item (Ritem);
7240 while Present (Aitem) loop
7241 if Nkind (Aitem) = N_Aspect_Specification
7242 and then Aspect_Rep_Item (Aitem) = Ritem
7245 (Identifier (Aitem), New_Copy_Tree (Exp));
7249 Aitem := Next_Rep_Item (Aitem);
7254 -- Now we need to preanalyze the expression to properly capture
7255 -- the visibility in the visible part. The expression will not
7256 -- be analyzed for real until the body is analyzed, but that is
7257 -- at the end of the private part and has the wrong visibility.
7259 Set_Parent (Exp, N);
7260 Preanalyze_Assert_Expression (Exp, Standard_Boolean);
7262 -- In ASIS mode, even if assertions are not enabled, we must
7263 -- analyze the original expression in the aspect specification
7264 -- because it is part of the original tree.
7266 if ASIS_Mode and then From_Aspect_Specification (Ritem) then
7268 Inv : constant Node_Id :=
7269 Expression (Corresponding_Aspect (Ritem));
7271 Replace_Type_References (Inv, Chars (T));
7272 Preanalyze_Assert_Expression (Inv, Standard_Boolean);
7276 -- Get name to be used for Check pragma
7278 if not From_Aspect_Specification (Ritem) then
7279 Nam := Name_Invariant;
7281 Nam := Chars (Identifier (Corresponding_Aspect (Ritem)));
7284 -- Build first two arguments for Check pragma
7288 Make_Pragma_Argument_Association (Loc,
7289 Expression => Make_Identifier (Loc, Chars => Nam)),
7290 Make_Pragma_Argument_Association (Loc,
7291 Expression => Exp));
7293 -- Add message if present in Invariant pragma
7295 if Present (Arg3) then
7296 Str := Strval (Get_Pragma_Arg (Arg3));
7298 -- If inherited case, and message starts "failed invariant",
7299 -- change it to be "failed inherited invariant".
7302 String_To_Name_Buffer (Str);
7304 if Name_Buffer (1 .. 16) = "failed invariant" then
7305 Insert_Str_In_Name_Buffer ("inherited ", 8);
7306 Str := String_From_Name_Buffer;
7311 Make_Pragma_Argument_Association (Loc,
7312 Expression => Make_String_Literal (Loc, Str)));
7315 -- Add Check pragma to list of statements
7319 Pragma_Identifier =>
7320 Make_Identifier (Loc, Name_Check),
7321 Pragma_Argument_Associations => Assoc));
7323 -- If Inherited case and option enabled, output info msg. Note
7324 -- that we know this is a case of Invariant'Class.
7326 if Inherit and Opt.List_Inherited_Aspects then
7327 Error_Msg_Sloc := Sloc (Ritem);
7329 ("info: & inherits `Invariant''Class` aspect from #?L?",
7335 Next_Rep_Item (Ritem);
7339 -- Start of processing for Build_Invariant_Procedure
7347 -- If the aspect specification exists for some view of the type, the
7348 -- declaration for the procedure has been created.
7350 if Has_Invariants (Typ) then
7351 SId := Invariant_Procedure (Typ);
7354 if Present (SId) then
7355 PDecl := Unit_Declaration_Node (SId);
7357 PDecl := Build_Invariant_Procedure_Declaration (Typ);
7360 -- Recover formal of procedure, for use in the calls to invariant
7361 -- functions (including inherited ones).
7365 (First (Parameter_Specifications (Specification (PDecl))));
7366 Object_Name := Chars (Object_Entity);
7368 -- Add invariants for the current type
7370 Add_Invariants (Typ, Inherit => False);
7372 -- Add invariants for parent types
7375 Current_Typ : Entity_Id;
7376 Parent_Typ : Entity_Id;
7381 Parent_Typ := Etype (Current_Typ);
7383 if Is_Private_Type (Parent_Typ)
7384 and then Present (Full_View (Base_Type (Parent_Typ)))
7386 Parent_Typ := Full_View (Base_Type (Parent_Typ));
7389 exit when Parent_Typ = Current_Typ;
7391 Current_Typ := Parent_Typ;
7392 Add_Invariants (Current_Typ, Inherit => True);
7396 -- Build the procedure if we generated at least one Check pragma
7398 if Stmts /= No_List then
7399 Spec := Copy_Separate_Tree (Specification (PDecl));
7402 Make_Subprogram_Body (Loc,
7403 Specification => Spec,
7404 Declarations => Empty_List,
7405 Handled_Statement_Sequence =>
7406 Make_Handled_Sequence_Of_Statements (Loc,
7407 Statements => Stmts));
7409 -- Insert procedure declaration and spec at the appropriate points.
7410 -- If declaration is already analyzed, it was processed by the
7411 -- generated pragma.
7413 if Present (Private_Decls) then
7415 -- The spec goes at the end of visible declarations, but they have
7416 -- already been analyzed, so we need to explicitly do the analyze.
7418 if not Analyzed (PDecl) then
7419 Append_To (Visible_Decls, PDecl);
7423 -- The body goes at the end of the private declarations, which we
7424 -- have not analyzed yet, so we do not need to perform an explicit
7425 -- analyze call. We skip this if there are no private declarations
7426 -- (this is an error that will be caught elsewhere);
7428 Append_To (Private_Decls, PBody);
7430 -- If the invariant appears on the full view of a type, the
7431 -- analysis of the private part is complete, and we must
7432 -- analyze the new body explicitly.
7434 if In_Private_Part (Current_Scope) then
7438 -- If there are no private declarations this may be an error that
7439 -- will be diagnosed elsewhere. However, if this is a non-private
7440 -- type that inherits invariants, it needs no completion and there
7441 -- may be no private part. In this case insert invariant procedure
7442 -- at end of current declarative list, and analyze at once, given
7443 -- that the type is about to be frozen.
7445 elsif not Is_Private_Type (Typ) then
7446 Append_To (Visible_Decls, PDecl);
7447 Append_To (Visible_Decls, PBody);
7452 end Build_Invariant_Procedure;
7454 -------------------------------
7455 -- Build_Predicate_Functions --
7456 -------------------------------
7458 -- The procedures that are constructed here have the form:
7460 -- function typPredicate (Ixxx : typ) return Boolean is
7463 -- exp1 and then exp2 and then ...
7464 -- and then typ1Predicate (typ1 (Ixxx))
7465 -- and then typ2Predicate (typ2 (Ixxx))
7467 -- end typPredicate;
7469 -- Here exp1, and exp2 are expressions from Predicate pragmas. Note that
7470 -- this is the point at which these expressions get analyzed, providing the
7471 -- required delay, and typ1, typ2, are entities from which predicates are
7472 -- inherited. Note that we do NOT generate Check pragmas, that's because we
7473 -- use this function even if checks are off, e.g. for membership tests.
7475 -- If the expression has at least one Raise_Expression, then we also build
7476 -- the typPredicateM version of the function, in which any occurrence of a
7477 -- Raise_Expression is converted to "return False".
7479 procedure Build_Predicate_Functions (Typ : Entity_Id; N : Node_Id) is
7480 Loc : constant Source_Ptr := Sloc (Typ);
7483 -- This is the expression for the result of the function. It is
7484 -- is build by connecting the component predicates with AND THEN.
7487 -- This is the corresponding return expression for the Predicate_M
7488 -- function. It differs in that raise expressions are marked for
7489 -- special expansion (see Process_REs).
7491 Object_Name : constant Name_Id := New_Internal_Name ('I');
7492 -- Name for argument of Predicate procedure. Note that we use the same
7493 -- name for both predicate procedure. That way the reference within the
7494 -- predicate expression is the same in both functions.
7496 Object_Entity : constant Entity_Id :=
7497 Make_Defining_Identifier (Loc, Chars => Object_Name);
7498 -- Entity for argument of Predicate procedure
7500 Object_Entity_M : constant Entity_Id :=
7501 Make_Defining_Identifier (Loc, Chars => Object_Name);
7502 -- Entity for argument of Predicate_M procedure
7504 Raise_Expression_Present : Boolean := False;
7505 -- Set True if Expr has at least one Raise_Expression
7507 Static_Predic : Node_Id := Empty;
7508 -- Set to N_Pragma node for a static predicate if one is encountered
7510 procedure Add_Call (T : Entity_Id);
7511 -- Includes a call to the predicate function for type T in Expr if T
7512 -- has predicates and Predicate_Function (T) is non-empty.
7514 procedure Add_Predicates;
7515 -- Appends expressions for any Predicate pragmas in the rep item chain
7516 -- Typ to Expr. Note that we look only at items for this exact entity.
7517 -- Inheritance of predicates for the parent type is done by calling the
7518 -- Predicate_Function of the parent type, using Add_Call above.
7520 function Test_RE (N : Node_Id) return Traverse_Result;
7521 -- Used in Test_REs, tests one node for being a raise expression, and if
7522 -- so sets Raise_Expression_Present True.
7524 procedure Test_REs is new Traverse_Proc (Test_RE);
7525 -- Tests to see if Expr contains any raise expressions
7527 function Process_RE (N : Node_Id) return Traverse_Result;
7528 -- Used in Process REs, tests if node N is a raise expression, and if
7529 -- so, marks it to be converted to return False.
7531 procedure Process_REs is new Traverse_Proc (Process_RE);
7532 -- Marks any raise expressions in Expr_M to return False
7538 procedure Add_Call (T : Entity_Id) is
7542 if Present (T) and then Present (Predicate_Function (T)) then
7543 Set_Has_Predicates (Typ);
7545 -- Build the call to the predicate function of T
7549 (T, Convert_To (T, Make_Identifier (Loc, Object_Name)));
7551 -- Add call to evolving expression, using AND THEN if needed
7558 Left_Opnd => Relocate_Node (Expr),
7562 -- Output info message on inheritance if required. Note we do not
7563 -- give this information for generic actual types, since it is
7564 -- unwelcome noise in that case in instantiations. We also
7565 -- generally suppress the message in instantiations, and also
7566 -- if it involves internal names.
7568 if Opt.List_Inherited_Aspects
7569 and then not Is_Generic_Actual_Type (Typ)
7570 and then Instantiation_Depth (Sloc (Typ)) = 0
7571 and then not Is_Internal_Name (Chars (T))
7572 and then not Is_Internal_Name (Chars (Typ))
7574 Error_Msg_Sloc := Sloc (Predicate_Function (T));
7575 Error_Msg_Node_2 := T;
7576 Error_Msg_N ("info: & inherits predicate from & #?L?", Typ);
7581 --------------------
7582 -- Add_Predicates --
7583 --------------------
7585 procedure Add_Predicates is
7590 procedure Replace_Type_Reference (N : Node_Id);
7591 -- Replace a single occurrence N of the subtype name with a reference
7592 -- to the formal of the predicate function. N can be an identifier
7593 -- referencing the subtype, or a selected component, representing an
7594 -- appropriately qualified occurrence of the subtype name.
7596 procedure Replace_Type_References is
7597 new Replace_Type_References_Generic (Replace_Type_Reference);
7598 -- Traverse an expression changing every occurrence of an identifier
7599 -- whose name matches the name of the subtype with a reference to
7600 -- the formal parameter of the predicate function.
7602 ----------------------------
7603 -- Replace_Type_Reference --
7604 ----------------------------
7606 procedure Replace_Type_Reference (N : Node_Id) is
7608 Rewrite (N, Make_Identifier (Sloc (N), Object_Name));
7609 -- Use the Sloc of the usage name, not the defining name
7612 Set_Entity (N, Object_Entity);
7614 -- We want to treat the node as if it comes from source, so that
7615 -- ASIS will not ignore it
7617 Set_Comes_From_Source (N, True);
7618 end Replace_Type_Reference;
7620 -- Start of processing for Add_Predicates
7623 Ritem := First_Rep_Item (Typ);
7624 while Present (Ritem) loop
7625 if Nkind (Ritem) = N_Pragma
7626 and then Pragma_Name (Ritem) = Name_Predicate
7628 -- Save the static predicate of the type for diagnostics and
7629 -- error reporting purposes.
7631 if Present (Corresponding_Aspect (Ritem))
7632 and then Chars (Identifier (Corresponding_Aspect (Ritem))) =
7633 Name_Static_Predicate
7635 Static_Predic := Ritem;
7638 -- Acquire arguments
7640 Arg1 := First (Pragma_Argument_Associations (Ritem));
7641 Arg2 := Next (Arg1);
7643 Arg1 := Get_Pragma_Arg (Arg1);
7644 Arg2 := Get_Pragma_Arg (Arg2);
7646 -- See if this predicate pragma is for the current type or for
7647 -- its full view. A predicate on a private completion is placed
7648 -- on the partial view beause this is the visible entity that
7651 if Entity (Arg1) = Typ
7652 or else Full_View (Entity (Arg1)) = Typ
7654 -- We have a match, this entry is for our subtype
7656 -- We need to replace any occurrences of the name of the
7657 -- type with references to the object.
7659 Replace_Type_References (Arg2, Chars (Typ));
7661 -- If this predicate comes from an aspect, find the aspect
7662 -- specification, and replace the saved expression because
7663 -- we need the subtype references replaced for the calls to
7664 -- Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
7665 -- and Check_Aspect_At_End_Of_Declarations.
7667 if From_Aspect_Specification (Ritem) then
7672 -- Loop to find corresponding aspect, note that this
7673 -- must be present given the pragma is marked delayed.
7675 Aitem := Next_Rep_Item (Ritem);
7677 if Nkind (Aitem) = N_Aspect_Specification
7678 and then Aspect_Rep_Item (Aitem) = Ritem
7681 (Identifier (Aitem), New_Copy_Tree (Arg2));
7685 Aitem := Next_Rep_Item (Aitem);
7690 -- Now we can add the expression
7693 Expr := Relocate_Node (Arg2);
7695 -- There already was a predicate, so add to it
7700 Left_Opnd => Relocate_Node (Expr),
7701 Right_Opnd => Relocate_Node (Arg2));
7706 Next_Rep_Item (Ritem);
7714 function Process_RE (N : Node_Id) return Traverse_Result is
7716 if Nkind (N) = N_Raise_Expression then
7717 Set_Convert_To_Return_False (N);
7728 function Test_RE (N : Node_Id) return Traverse_Result is
7730 if Nkind (N) = N_Raise_Expression then
7731 Raise_Expression_Present := True;
7738 -- Start of processing for Build_Predicate_Functions
7741 -- Return if already built or if type does not have predicates
7743 if not Has_Predicates (Typ)
7744 or else Present (Predicate_Function (Typ))
7749 -- Prepare to construct predicate expression
7753 -- Add Predicates for the current type
7757 -- Add predicates for ancestor if present
7760 Atyp : constant Entity_Id := Nearest_Ancestor (Typ);
7762 if Present (Atyp) then
7767 -- Case where predicates are present
7769 if Present (Expr) then
7771 -- Test for raise expression present
7775 -- If raise expression is present, capture a copy of Expr for use
7776 -- in building the predicateM function version later on. For this
7777 -- copy we replace references to Object_Entity by Object_Entity_M.
7779 if Raise_Expression_Present then
7781 Map : constant Elist_Id := New_Elmt_List;
7783 Append_Elmt (Object_Entity, Map);
7784 Append_Elmt (Object_Entity_M, Map);
7785 Expr_M := New_Copy_Tree (Expr, Map => Map);
7789 -- Build the main predicate function
7792 SId : constant Entity_Id :=
7793 Make_Defining_Identifier (Loc,
7794 Chars => New_External_Name (Chars (Typ), "Predicate"));
7795 -- The entity for the the function spec
7797 SIdB : constant Entity_Id :=
7798 Make_Defining_Identifier (Loc,
7799 Chars => New_External_Name (Chars (Typ), "Predicate"));
7800 -- The entity for the function body
7807 -- Build function declaration
7809 Set_Ekind (SId, E_Function);
7810 Set_Is_Internal (SId);
7811 Set_Is_Predicate_Function (SId);
7812 Set_Predicate_Function (Typ, SId);
7814 -- The predicate function is shared between views of a type
7816 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
7817 Set_Predicate_Function (Full_View (Typ), SId);
7821 Make_Function_Specification (Loc,
7822 Defining_Unit_Name => SId,
7823 Parameter_Specifications => New_List (
7824 Make_Parameter_Specification (Loc,
7825 Defining_Identifier => Object_Entity,
7826 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
7827 Result_Definition =>
7828 New_Occurrence_Of (Standard_Boolean, Loc));
7831 Make_Subprogram_Declaration (Loc,
7832 Specification => Spec);
7834 -- Build function body
7837 Make_Function_Specification (Loc,
7838 Defining_Unit_Name => SIdB,
7839 Parameter_Specifications => New_List (
7840 Make_Parameter_Specification (Loc,
7841 Defining_Identifier =>
7842 Make_Defining_Identifier (Loc, Object_Name),
7844 New_Occurrence_Of (Typ, Loc))),
7845 Result_Definition =>
7846 New_Occurrence_Of (Standard_Boolean, Loc));
7849 Make_Subprogram_Body (Loc,
7850 Specification => Spec,
7851 Declarations => Empty_List,
7852 Handled_Statement_Sequence =>
7853 Make_Handled_Sequence_Of_Statements (Loc,
7854 Statements => New_List (
7855 Make_Simple_Return_Statement (Loc,
7856 Expression => Expr))));
7858 -- Insert declaration before freeze node and body after
7860 Insert_Before_And_Analyze (N, FDecl);
7861 Insert_After_And_Analyze (N, FBody);
7864 -- Test for raise expressions present and if so build M version
7866 if Raise_Expression_Present then
7868 SId : constant Entity_Id :=
7869 Make_Defining_Identifier (Loc,
7870 Chars => New_External_Name (Chars (Typ), "PredicateM"));
7871 -- The entity for the the function spec
7873 SIdB : constant Entity_Id :=
7874 Make_Defining_Identifier (Loc,
7875 Chars => New_External_Name (Chars (Typ), "PredicateM"));
7876 -- The entity for the function body
7884 -- Mark any raise expressions for special expansion
7886 Process_REs (Expr_M);
7888 -- Build function declaration
7890 Set_Ekind (SId, E_Function);
7891 Set_Is_Predicate_Function_M (SId);
7892 Set_Predicate_Function_M (Typ, SId);
7894 -- The predicate function is shared between views of a type
7896 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
7897 Set_Predicate_Function_M (Full_View (Typ), SId);
7901 Make_Function_Specification (Loc,
7902 Defining_Unit_Name => SId,
7903 Parameter_Specifications => New_List (
7904 Make_Parameter_Specification (Loc,
7905 Defining_Identifier => Object_Entity_M,
7906 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
7907 Result_Definition =>
7908 New_Occurrence_Of (Standard_Boolean, Loc));
7911 Make_Subprogram_Declaration (Loc,
7912 Specification => Spec);
7914 -- Build function body
7917 Make_Function_Specification (Loc,
7918 Defining_Unit_Name => SIdB,
7919 Parameter_Specifications => New_List (
7920 Make_Parameter_Specification (Loc,
7921 Defining_Identifier =>
7922 Make_Defining_Identifier (Loc, Object_Name),
7924 New_Occurrence_Of (Typ, Loc))),
7925 Result_Definition =>
7926 New_Occurrence_Of (Standard_Boolean, Loc));
7928 -- Build the body, we declare the boolean expression before
7929 -- doing the return, because we are not really confident of
7930 -- what happens if a return appears within a return.
7933 Make_Defining_Identifier (Loc,
7934 Chars => New_Internal_Name ('B'));
7937 Make_Subprogram_Body (Loc,
7938 Specification => Spec,
7940 Declarations => New_List (
7941 Make_Object_Declaration (Loc,
7942 Defining_Identifier => BTemp,
7943 Constant_Present => True,
7944 Object_Definition =>
7945 New_Occurrence_Of (Standard_Boolean, Loc),
7946 Expression => Expr_M)),
7948 Handled_Statement_Sequence =>
7949 Make_Handled_Sequence_Of_Statements (Loc,
7950 Statements => New_List (
7951 Make_Simple_Return_Statement (Loc,
7952 Expression => New_Occurrence_Of (BTemp, Loc)))));
7954 -- Insert declaration before freeze node and body after
7956 Insert_Before_And_Analyze (N, FDecl);
7957 Insert_After_And_Analyze (N, FBody);
7961 if Is_Discrete_Type (Typ) then
7963 -- Attempt to build a static predicate for a discrete subtype.
7964 -- This action may fail because the actual expression may not be
7965 -- static. Note that the presence of an inherited or explicitly
7966 -- declared dynamic predicate is orthogonal to this check because
7967 -- we are only interested in the static predicate.
7969 Build_Discrete_Static_Predicate (Typ, Expr, Object_Name);
7971 -- Emit an error when the predicate is categorized as static
7972 -- but its expression is dynamic.
7974 if Present (Static_Predic)
7975 and then No (Static_Predicate (Typ))
7978 ("expression does not have required form for "
7979 & "static predicate",
7980 Next (First (Pragma_Argument_Associations
7984 -- If a static predicate applies on other types, that's an error:
7985 -- either the type is scalar but non-static, or it's not even a
7986 -- scalar type. We do not issue an error on generated types, as
7987 -- these may be duplicates of the same error on a source type.
7989 elsif Present (Static_Predic) and then Comes_From_Source (Typ) then
7990 if Is_Real_Type (Typ) then
7992 ("static predicates not implemented for real type&",
7995 elsif Is_Scalar_Type (Typ) then
7997 ("static predicate not allowed for non-static type&",
8002 ("static predicate not allowed for non-scalar type&",
8007 end Build_Predicate_Functions;
8009 -----------------------------------------
8010 -- Check_Aspect_At_End_Of_Declarations --
8011 -----------------------------------------
8013 procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id) is
8014 Ent : constant Entity_Id := Entity (ASN);
8015 Ident : constant Node_Id := Identifier (ASN);
8016 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
8018 End_Decl_Expr : constant Node_Id := Entity (Ident);
8019 -- Expression to be analyzed at end of declarations
8021 Freeze_Expr : constant Node_Id := Expression (ASN);
8022 -- Expression from call to Check_Aspect_At_Freeze_Point
8024 T : constant Entity_Id := Etype (Freeze_Expr);
8025 -- Type required for preanalyze call
8028 -- Set False if error
8030 -- On entry to this procedure, Entity (Ident) contains a copy of the
8031 -- original expression from the aspect, saved for this purpose, and
8032 -- but Expression (Ident) is a preanalyzed copy of the expression,
8033 -- preanalyzed just after the freeze point.
8035 procedure Check_Overloaded_Name;
8036 -- For aspects whose expression is simply a name, this routine checks if
8037 -- the name is overloaded or not. If so, it verifies there is an
8038 -- interpretation that matches the entity obtained at the freeze point,
8039 -- otherwise the compiler complains.
8041 ---------------------------
8042 -- Check_Overloaded_Name --
8043 ---------------------------
8045 procedure Check_Overloaded_Name is
8047 if not Is_Overloaded (End_Decl_Expr) then
8048 Err := Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
8054 Index : Interp_Index;
8058 Get_First_Interp (End_Decl_Expr, Index, It);
8059 while Present (It.Typ) loop
8060 if It.Nam = Entity (Freeze_Expr) then
8065 Get_Next_Interp (Index, It);
8069 end Check_Overloaded_Name;
8071 -- Start of processing for Check_Aspect_At_End_Of_Declarations
8074 -- Case of aspects Dimension, Dimension_System and Synchronization
8076 if A_Id = Aspect_Synchronization then
8079 -- Case of stream attributes, just have to compare entities. However,
8080 -- the expression is just a name (possibly overloaded), and there may
8081 -- be stream operations declared for unrelated types, so we just need
8082 -- to verify that one of these interpretations is the one available at
8083 -- at the freeze point.
8085 elsif A_Id = Aspect_Input or else
8086 A_Id = Aspect_Output or else
8087 A_Id = Aspect_Read or else
8090 Analyze (End_Decl_Expr);
8091 Check_Overloaded_Name;
8093 elsif A_Id = Aspect_Variable_Indexing or else
8094 A_Id = Aspect_Constant_Indexing or else
8095 A_Id = Aspect_Default_Iterator or else
8096 A_Id = Aspect_Iterator_Element
8098 -- Make type unfrozen before analysis, to prevent spurious errors
8099 -- about late attributes.
8101 Set_Is_Frozen (Ent, False);
8102 Analyze (End_Decl_Expr);
8103 Set_Is_Frozen (Ent, True);
8105 -- If the end of declarations comes before any other freeze
8106 -- point, the Freeze_Expr is not analyzed: no check needed.
8108 if Analyzed (Freeze_Expr) and then not In_Instance then
8109 Check_Overloaded_Name;
8117 -- Indicate that the expression comes from an aspect specification,
8118 -- which is used in subsequent analysis even if expansion is off.
8120 Set_Parent (End_Decl_Expr, ASN);
8122 -- In a generic context the aspect expressions have not been
8123 -- preanalyzed, so do it now. There are no conformance checks
8124 -- to perform in this case.
8127 Check_Aspect_At_Freeze_Point (ASN);
8130 -- The default values attributes may be defined in the private part,
8131 -- and the analysis of the expression may take place when only the
8132 -- partial view is visible. The expression must be scalar, so use
8133 -- the full view to resolve.
8135 elsif (A_Id = Aspect_Default_Value
8137 A_Id = Aspect_Default_Component_Value)
8138 and then Is_Private_Type (T)
8140 Preanalyze_Spec_Expression (End_Decl_Expr, Full_View (T));
8143 Preanalyze_Spec_Expression (End_Decl_Expr, T);
8146 Err := not Fully_Conformant_Expressions (End_Decl_Expr, Freeze_Expr);
8149 -- Output error message if error. Force error on aspect specification
8150 -- even if there is an error on the expression itself.
8154 ("!visibility of aspect for& changes after freeze point",
8157 ("info: & is frozen here, aspects evaluated at this point??",
8158 Freeze_Node (Ent), Ent);
8160 end Check_Aspect_At_End_Of_Declarations;
8162 ----------------------------------
8163 -- Check_Aspect_At_Freeze_Point --
8164 ----------------------------------
8166 procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id) is
8167 Ident : constant Node_Id := Identifier (ASN);
8168 -- Identifier (use Entity field to save expression)
8170 A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
8172 T : Entity_Id := Empty;
8173 -- Type required for preanalyze call
8176 -- On entry to this procedure, Entity (Ident) contains a copy of the
8177 -- original expression from the aspect, saved for this purpose.
8179 -- On exit from this procedure Entity (Ident) is unchanged, still
8180 -- containing that copy, but Expression (Ident) is a preanalyzed copy
8181 -- of the expression, preanalyzed just after the freeze point.
8183 -- Make a copy of the expression to be preanalyzed
8185 Set_Expression (ASN, New_Copy_Tree (Entity (Ident)));
8187 -- Find type for preanalyze call
8191 -- No_Aspect should be impossible
8194 raise Program_Error;
8196 -- Aspects taking an optional boolean argument
8198 when Boolean_Aspects |
8199 Library_Unit_Aspects =>
8201 T := Standard_Boolean;
8203 -- Aspects corresponding to attribute definition clauses
8205 when Aspect_Address =>
8206 T := RTE (RE_Address);
8208 when Aspect_Attach_Handler =>
8209 T := RTE (RE_Interrupt_ID);
8211 when Aspect_Bit_Order | Aspect_Scalar_Storage_Order =>
8212 T := RTE (RE_Bit_Order);
8214 when Aspect_Convention =>
8218 T := RTE (RE_CPU_Range);
8220 -- Default_Component_Value is resolved with the component type
8222 when Aspect_Default_Component_Value =>
8223 T := Component_Type (Entity (ASN));
8225 -- Default_Value is resolved with the type entity in question
8227 when Aspect_Default_Value =>
8230 -- Depends is a delayed aspect because it mentiones names first
8231 -- introduced by aspect Global which is already delayed. There is
8232 -- no action to be taken with respect to the aspect itself as the
8233 -- analysis is done by the corresponding pragma.
8235 when Aspect_Depends =>
8238 when Aspect_Dispatching_Domain =>
8239 T := RTE (RE_Dispatching_Domain);
8241 when Aspect_External_Tag =>
8242 T := Standard_String;
8244 when Aspect_External_Name =>
8245 T := Standard_String;
8247 -- Global is a delayed aspect because it may reference names that
8248 -- have not been declared yet. There is no action to be taken with
8249 -- respect to the aspect itself as the reference checking is done
8250 -- on the corresponding pragma.
8252 when Aspect_Global =>
8255 when Aspect_Link_Name =>
8256 T := Standard_String;
8258 when Aspect_Priority | Aspect_Interrupt_Priority =>
8259 T := Standard_Integer;
8261 when Aspect_Relative_Deadline =>
8262 T := RTE (RE_Time_Span);
8264 when Aspect_Small =>
8265 T := Universal_Real;
8267 -- For a simple storage pool, we have to retrieve the type of the
8268 -- pool object associated with the aspect's corresponding attribute
8269 -- definition clause.
8271 when Aspect_Simple_Storage_Pool =>
8272 T := Etype (Expression (Aspect_Rep_Item (ASN)));
8274 when Aspect_Storage_Pool =>
8275 T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
8277 when Aspect_Alignment |
8278 Aspect_Component_Size |
8279 Aspect_Machine_Radix |
8280 Aspect_Object_Size |
8282 Aspect_Storage_Size |
8283 Aspect_Stream_Size |
8284 Aspect_Value_Size =>
8287 when Aspect_Linker_Section =>
8288 T := Standard_String;
8290 when Aspect_Synchronization =>
8293 -- Special case, the expression of these aspects is just an entity
8294 -- that does not need any resolution, so just analyze.
8303 Analyze (Expression (ASN));
8306 -- Same for Iterator aspects, where the expression is a function
8307 -- name. Legality rules are checked separately.
8309 when Aspect_Constant_Indexing |
8310 Aspect_Default_Iterator |
8311 Aspect_Iterator_Element |
8312 Aspect_Variable_Indexing =>
8313 Analyze (Expression (ASN));
8316 -- Ditto for Iterable, legality checks in Validate_Iterable_Aspect.
8318 when Aspect_Iterable =>
8322 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, T);
8327 if Cursor = Any_Type then
8331 Assoc := First (Component_Associations (Expression (ASN)));
8332 while Present (Assoc) loop
8333 Expr := Expression (Assoc);
8336 if not Error_Posted (Expr) then
8337 Resolve_Iterable_Operation
8338 (Expr, Cursor, T, Chars (First (Choices (Assoc))));
8347 -- Invariant/Predicate take boolean expressions
8349 when Aspect_Dynamic_Predicate |
8352 Aspect_Static_Predicate |
8353 Aspect_Type_Invariant =>
8354 T := Standard_Boolean;
8356 -- Here is the list of aspects that don't require delay analysis
8358 when Aspect_Abstract_State |
8360 Aspect_Contract_Cases |
8362 Aspect_Dimension_System |
8363 Aspect_Implicit_Dereference |
8364 Aspect_Initial_Condition |
8365 Aspect_Initializes |
8368 Aspect_Postcondition |
8370 Aspect_Precondition |
8371 Aspect_Refined_Depends |
8372 Aspect_Refined_Global |
8373 Aspect_Refined_Post |
8374 Aspect_Refined_State |
8377 raise Program_Error;
8381 -- Do the preanalyze call
8383 Preanalyze_Spec_Expression (Expression (ASN), T);
8384 end Check_Aspect_At_Freeze_Point;
8386 -----------------------------------
8387 -- Check_Constant_Address_Clause --
8388 -----------------------------------
8390 procedure Check_Constant_Address_Clause
8394 procedure Check_At_Constant_Address (Nod : Node_Id);
8395 -- Checks that the given node N represents a name whose 'Address is
8396 -- constant (in the same sense as OK_Constant_Address_Clause, i.e. the
8397 -- address value is the same at the point of declaration of U_Ent and at
8398 -- the time of elaboration of the address clause.
8400 procedure Check_Expr_Constants (Nod : Node_Id);
8401 -- Checks that Nod meets the requirements for a constant address clause
8402 -- in the sense of the enclosing procedure.
8404 procedure Check_List_Constants (Lst : List_Id);
8405 -- Check that all elements of list Lst meet the requirements for a
8406 -- constant address clause in the sense of the enclosing procedure.
8408 -------------------------------
8409 -- Check_At_Constant_Address --
8410 -------------------------------
8412 procedure Check_At_Constant_Address (Nod : Node_Id) is
8414 if Is_Entity_Name (Nod) then
8415 if Present (Address_Clause (Entity ((Nod)))) then
8417 ("invalid address clause for initialized object &!",
8420 ("address for& cannot" &
8421 " depend on another address clause! (RM 13.1(22))!",
8424 elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
8425 and then Sloc (U_Ent) < Sloc (Entity (Nod))
8428 ("invalid address clause for initialized object &!",
8430 Error_Msg_Node_2 := U_Ent;
8432 ("\& must be defined before & (RM 13.1(22))!",
8436 elsif Nkind (Nod) = N_Selected_Component then
8438 T : constant Entity_Id := Etype (Prefix (Nod));
8441 if (Is_Record_Type (T)
8442 and then Has_Discriminants (T))
8445 and then Is_Record_Type (Designated_Type (T))
8446 and then Has_Discriminants (Designated_Type (T)))
8449 ("invalid address clause for initialized object &!",
8452 ("\address cannot depend on component" &
8453 " of discriminated record (RM 13.1(22))!",
8456 Check_At_Constant_Address (Prefix (Nod));
8460 elsif Nkind (Nod) = N_Indexed_Component then
8461 Check_At_Constant_Address (Prefix (Nod));
8462 Check_List_Constants (Expressions (Nod));
8465 Check_Expr_Constants (Nod);
8467 end Check_At_Constant_Address;
8469 --------------------------
8470 -- Check_Expr_Constants --
8471 --------------------------
8473 procedure Check_Expr_Constants (Nod : Node_Id) is
8474 Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
8475 Ent : Entity_Id := Empty;
8478 if Nkind (Nod) in N_Has_Etype
8479 and then Etype (Nod) = Any_Type
8485 when N_Empty | N_Error =>
8488 when N_Identifier | N_Expanded_Name =>
8489 Ent := Entity (Nod);
8491 -- We need to look at the original node if it is different
8492 -- from the node, since we may have rewritten things and
8493 -- substituted an identifier representing the rewrite.
8495 if Original_Node (Nod) /= Nod then
8496 Check_Expr_Constants (Original_Node (Nod));
8498 -- If the node is an object declaration without initial
8499 -- value, some code has been expanded, and the expression
8500 -- is not constant, even if the constituents might be
8501 -- acceptable, as in A'Address + offset.
8503 if Ekind (Ent) = E_Variable
8505 Nkind (Declaration_Node (Ent)) = N_Object_Declaration
8507 No (Expression (Declaration_Node (Ent)))
8510 ("invalid address clause for initialized object &!",
8513 -- If entity is constant, it may be the result of expanding
8514 -- a check. We must verify that its declaration appears
8515 -- before the object in question, else we also reject the
8518 elsif Ekind (Ent) = E_Constant
8519 and then In_Same_Source_Unit (Ent, U_Ent)
8520 and then Sloc (Ent) > Loc_U_Ent
8523 ("invalid address clause for initialized object &!",
8530 -- Otherwise look at the identifier and see if it is OK
8532 if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
8533 or else Is_Type (Ent)
8538 Ekind (Ent) = E_Constant
8540 Ekind (Ent) = E_In_Parameter
8542 -- This is the case where we must have Ent defined before
8543 -- U_Ent. Clearly if they are in different units this
8544 -- requirement is met since the unit containing Ent is
8545 -- already processed.
8547 if not In_Same_Source_Unit (Ent, U_Ent) then
8550 -- Otherwise location of Ent must be before the location
8551 -- of U_Ent, that's what prior defined means.
8553 elsif Sloc (Ent) < Loc_U_Ent then
8558 ("invalid address clause for initialized object &!",
8560 Error_Msg_Node_2 := U_Ent;
8562 ("\& must be defined before & (RM 13.1(22))!",
8566 elsif Nkind (Original_Node (Nod)) = N_Function_Call then
8567 Check_Expr_Constants (Original_Node (Nod));
8571 ("invalid address clause for initialized object &!",
8574 if Comes_From_Source (Ent) then
8576 ("\reference to variable& not allowed"
8577 & " (RM 13.1(22))!", Nod, Ent);
8580 ("non-static expression not allowed"
8581 & " (RM 13.1(22))!", Nod);
8585 when N_Integer_Literal =>
8587 -- If this is a rewritten unchecked conversion, in a system
8588 -- where Address is an integer type, always use the base type
8589 -- for a literal value. This is user-friendly and prevents
8590 -- order-of-elaboration issues with instances of unchecked
8593 if Nkind (Original_Node (Nod)) = N_Function_Call then
8594 Set_Etype (Nod, Base_Type (Etype (Nod)));
8597 when N_Real_Literal |
8599 N_Character_Literal =>
8603 Check_Expr_Constants (Low_Bound (Nod));
8604 Check_Expr_Constants (High_Bound (Nod));
8606 when N_Explicit_Dereference =>
8607 Check_Expr_Constants (Prefix (Nod));
8609 when N_Indexed_Component =>
8610 Check_Expr_Constants (Prefix (Nod));
8611 Check_List_Constants (Expressions (Nod));
8614 Check_Expr_Constants (Prefix (Nod));
8615 Check_Expr_Constants (Discrete_Range (Nod));
8617 when N_Selected_Component =>
8618 Check_Expr_Constants (Prefix (Nod));
8620 when N_Attribute_Reference =>
8621 if Nam_In (Attribute_Name (Nod), Name_Address,
8623 Name_Unchecked_Access,
8624 Name_Unrestricted_Access)
8626 Check_At_Constant_Address (Prefix (Nod));
8629 Check_Expr_Constants (Prefix (Nod));
8630 Check_List_Constants (Expressions (Nod));
8634 Check_List_Constants (Component_Associations (Nod));
8635 Check_List_Constants (Expressions (Nod));
8637 when N_Component_Association =>
8638 Check_Expr_Constants (Expression (Nod));
8640 when N_Extension_Aggregate =>
8641 Check_Expr_Constants (Ancestor_Part (Nod));
8642 Check_List_Constants (Component_Associations (Nod));
8643 Check_List_Constants (Expressions (Nod));
8648 when N_Binary_Op | N_Short_Circuit | N_Membership_Test =>
8649 Check_Expr_Constants (Left_Opnd (Nod));
8650 Check_Expr_Constants (Right_Opnd (Nod));
8653 Check_Expr_Constants (Right_Opnd (Nod));
8655 when N_Type_Conversion |
8656 N_Qualified_Expression |
8658 N_Unchecked_Type_Conversion =>
8659 Check_Expr_Constants (Expression (Nod));
8661 when N_Function_Call =>
8662 if not Is_Pure (Entity (Name (Nod))) then
8664 ("invalid address clause for initialized object &!",
8668 ("\function & is not pure (RM 13.1(22))!",
8669 Nod, Entity (Name (Nod)));
8672 Check_List_Constants (Parameter_Associations (Nod));
8675 when N_Parameter_Association =>
8676 Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
8680 ("invalid address clause for initialized object &!",
8683 ("\must be constant defined before& (RM 13.1(22))!",
8686 end Check_Expr_Constants;
8688 --------------------------
8689 -- Check_List_Constants --
8690 --------------------------
8692 procedure Check_List_Constants (Lst : List_Id) is
8696 if Present (Lst) then
8697 Nod1 := First (Lst);
8698 while Present (Nod1) loop
8699 Check_Expr_Constants (Nod1);
8703 end Check_List_Constants;
8705 -- Start of processing for Check_Constant_Address_Clause
8708 -- If rep_clauses are to be ignored, no need for legality checks. In
8709 -- particular, no need to pester user about rep clauses that violate
8710 -- the rule on constant addresses, given that these clauses will be
8711 -- removed by Freeze before they reach the back end.
8713 if not Ignore_Rep_Clauses then
8714 Check_Expr_Constants (Expr);
8716 end Check_Constant_Address_Clause;
8718 ---------------------------
8719 -- Check_Pool_Size_Clash --
8720 ---------------------------
8722 procedure Check_Pool_Size_Clash (Ent : Entity_Id; SP, SS : Node_Id) is
8726 -- We need to find out which one came first. Note that in the case of
8727 -- aspects mixed with pragmas there are cases where the processing order
8728 -- is reversed, which is why we do the check here.
8730 if Sloc (SP) < Sloc (SS) then
8731 Error_Msg_Sloc := Sloc (SP);
8733 Error_Msg_NE ("Storage_Pool previously given for&#", Post, Ent);
8736 Error_Msg_Sloc := Sloc (SS);
8738 Error_Msg_NE ("Storage_Size previously given for&#", Post, Ent);
8742 ("\cannot have Storage_Size and Storage_Pool (RM 13.11(3))", Post);
8743 end Check_Pool_Size_Clash;
8745 ----------------------------------------
8746 -- Check_Record_Representation_Clause --
8747 ----------------------------------------
8749 procedure Check_Record_Representation_Clause (N : Node_Id) is
8750 Loc : constant Source_Ptr := Sloc (N);
8751 Ident : constant Node_Id := Identifier (N);
8752 Rectype : Entity_Id;
8757 Hbit : Uint := Uint_0;
8761 Max_Bit_So_Far : Uint;
8762 -- Records the maximum bit position so far. If all field positions
8763 -- are monotonically increasing, then we can skip the circuit for
8764 -- checking for overlap, since no overlap is possible.
8766 Tagged_Parent : Entity_Id := Empty;
8767 -- This is set in the case of a derived tagged type for which we have
8768 -- Is_Fully_Repped_Tagged_Type True (indicating that all components are
8769 -- positioned by record representation clauses). In this case we must
8770 -- check for overlap between components of this tagged type, and the
8771 -- components of its parent. Tagged_Parent will point to this parent
8772 -- type. For all other cases Tagged_Parent is left set to Empty.
8774 Parent_Last_Bit : Uint;
8775 -- Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
8776 -- last bit position for any field in the parent type. We only need to
8777 -- check overlap for fields starting below this point.
8779 Overlap_Check_Required : Boolean;
8780 -- Used to keep track of whether or not an overlap check is required
8782 Overlap_Detected : Boolean := False;
8783 -- Set True if an overlap is detected
8785 Ccount : Natural := 0;
8786 -- Number of component clauses in record rep clause
8788 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
8789 -- Given two entities for record components or discriminants, checks
8790 -- if they have overlapping component clauses and issues errors if so.
8792 procedure Find_Component;
8793 -- Finds component entity corresponding to current component clause (in
8794 -- CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
8795 -- start/stop bits for the field. If there is no matching component or
8796 -- if the matching component does not have a component clause, then
8797 -- that's an error and Comp is set to Empty, but no error message is
8798 -- issued, since the message was already given. Comp is also set to
8799 -- Empty if the current "component clause" is in fact a pragma.
8801 -----------------------------
8802 -- Check_Component_Overlap --
8803 -----------------------------
8805 procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
8806 CC1 : constant Node_Id := Component_Clause (C1_Ent);
8807 CC2 : constant Node_Id := Component_Clause (C2_Ent);
8810 if Present (CC1) and then Present (CC2) then
8812 -- Exclude odd case where we have two tag components in the same
8813 -- record, both at location zero. This seems a bit strange, but
8814 -- it seems to happen in some circumstances, perhaps on an error.
8816 if Nam_In (Chars (C1_Ent), Name_uTag, Name_uTag) then
8820 -- Here we check if the two fields overlap
8823 S1 : constant Uint := Component_Bit_Offset (C1_Ent);
8824 S2 : constant Uint := Component_Bit_Offset (C2_Ent);
8825 E1 : constant Uint := S1 + Esize (C1_Ent);
8826 E2 : constant Uint := S2 + Esize (C2_Ent);
8829 if E2 <= S1 or else E1 <= S2 then
8832 Error_Msg_Node_2 := Component_Name (CC2);
8833 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
8834 Error_Msg_Node_1 := Component_Name (CC1);
8836 ("component& overlaps & #", Component_Name (CC1));
8837 Overlap_Detected := True;
8841 end Check_Component_Overlap;
8843 --------------------
8844 -- Find_Component --
8845 --------------------
8847 procedure Find_Component is
8849 procedure Search_Component (R : Entity_Id);
8850 -- Search components of R for a match. If found, Comp is set
8852 ----------------------
8853 -- Search_Component --
8854 ----------------------
8856 procedure Search_Component (R : Entity_Id) is
8858 Comp := First_Component_Or_Discriminant (R);
8859 while Present (Comp) loop
8861 -- Ignore error of attribute name for component name (we
8862 -- already gave an error message for this, so no need to
8865 if Nkind (Component_Name (CC)) = N_Attribute_Reference then
8868 exit when Chars (Comp) = Chars (Component_Name (CC));
8871 Next_Component_Or_Discriminant (Comp);
8873 end Search_Component;
8875 -- Start of processing for Find_Component
8878 -- Return with Comp set to Empty if we have a pragma
8880 if Nkind (CC) = N_Pragma then
8885 -- Search current record for matching component
8887 Search_Component (Rectype);
8889 -- If not found, maybe component of base type discriminant that is
8890 -- absent from statically constrained first subtype.
8893 Search_Component (Base_Type (Rectype));
8896 -- If no component, or the component does not reference the component
8897 -- clause in question, then there was some previous error for which
8898 -- we already gave a message, so just return with Comp Empty.
8900 if No (Comp) or else Component_Clause (Comp) /= CC then
8901 Check_Error_Detected;
8904 -- Normal case where we have a component clause
8907 Fbit := Component_Bit_Offset (Comp);
8908 Lbit := Fbit + Esize (Comp) - 1;
8912 -- Start of processing for Check_Record_Representation_Clause
8916 Rectype := Entity (Ident);
8918 if Rectype = Any_Type then
8921 Rectype := Underlying_Type (Rectype);
8924 -- See if we have a fully repped derived tagged type
8927 PS : constant Entity_Id := Parent_Subtype (Rectype);
8930 if Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
8931 Tagged_Parent := PS;
8933 -- Find maximum bit of any component of the parent type
8935 Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
8936 Pcomp := First_Entity (Tagged_Parent);
8937 while Present (Pcomp) loop
8938 if Ekind_In (Pcomp, E_Discriminant, E_Component) then
8939 if Component_Bit_Offset (Pcomp) /= No_Uint
8940 and then Known_Static_Esize (Pcomp)
8945 Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
8948 Next_Entity (Pcomp);
8954 -- All done if no component clauses
8956 CC := First (Component_Clauses (N));
8962 -- If a tag is present, then create a component clause that places it
8963 -- at the start of the record (otherwise gigi may place it after other
8964 -- fields that have rep clauses).
8966 Fent := First_Entity (Rectype);
8968 if Nkind (Fent) = N_Defining_Identifier
8969 and then Chars (Fent) = Name_uTag
8971 Set_Component_Bit_Offset (Fent, Uint_0);
8972 Set_Normalized_Position (Fent, Uint_0);
8973 Set_Normalized_First_Bit (Fent, Uint_0);
8974 Set_Normalized_Position_Max (Fent, Uint_0);
8975 Init_Esize (Fent, System_Address_Size);
8977 Set_Component_Clause (Fent,
8978 Make_Component_Clause (Loc,
8979 Component_Name => Make_Identifier (Loc, Name_uTag),
8981 Position => Make_Integer_Literal (Loc, Uint_0),
8982 First_Bit => Make_Integer_Literal (Loc, Uint_0),
8984 Make_Integer_Literal (Loc,
8985 UI_From_Int (System_Address_Size))));
8987 Ccount := Ccount + 1;
8990 Max_Bit_So_Far := Uint_Minus_1;
8991 Overlap_Check_Required := False;
8993 -- Process the component clauses
8995 while Present (CC) loop
8998 if Present (Comp) then
8999 Ccount := Ccount + 1;
9001 -- We need a full overlap check if record positions non-monotonic
9003 if Fbit <= Max_Bit_So_Far then
9004 Overlap_Check_Required := True;
9007 Max_Bit_So_Far := Lbit;
9009 -- Check bit position out of range of specified size
9011 if Has_Size_Clause (Rectype)
9012 and then RM_Size (Rectype) <= Lbit
9015 ("bit number out of range of specified size",
9018 -- Check for overlap with tag component
9021 if Is_Tagged_Type (Rectype)
9022 and then Fbit < System_Address_Size
9025 ("component overlaps tag field of&",
9026 Component_Name (CC), Rectype);
9027 Overlap_Detected := True;
9035 -- Check parent overlap if component might overlap parent field
9037 if Present (Tagged_Parent) and then Fbit <= Parent_Last_Bit then
9038 Pcomp := First_Component_Or_Discriminant (Tagged_Parent);
9039 while Present (Pcomp) loop
9040 if not Is_Tag (Pcomp)
9041 and then Chars (Pcomp) /= Name_uParent
9043 Check_Component_Overlap (Comp, Pcomp);
9046 Next_Component_Or_Discriminant (Pcomp);
9054 -- Now that we have processed all the component clauses, check for
9055 -- overlap. We have to leave this till last, since the components can
9056 -- appear in any arbitrary order in the representation clause.
9058 -- We do not need this check if all specified ranges were monotonic,
9059 -- as recorded by Overlap_Check_Required being False at this stage.
9061 -- This first section checks if there are any overlapping entries at
9062 -- all. It does this by sorting all entries and then seeing if there are
9063 -- any overlaps. If there are none, then that is decisive, but if there
9064 -- are overlaps, they may still be OK (they may result from fields in
9065 -- different variants).
9067 if Overlap_Check_Required then
9068 Overlap_Check1 : declare
9070 OC_Fbit : array (0 .. Ccount) of Uint;
9071 -- First-bit values for component clauses, the value is the offset
9072 -- of the first bit of the field from start of record. The zero
9073 -- entry is for use in sorting.
9075 OC_Lbit : array (0 .. Ccount) of Uint;
9076 -- Last-bit values for component clauses, the value is the offset
9077 -- of the last bit of the field from start of record. The zero
9078 -- entry is for use in sorting.
9080 OC_Count : Natural := 0;
9081 -- Count of entries in OC_Fbit and OC_Lbit
9083 function OC_Lt (Op1, Op2 : Natural) return Boolean;
9084 -- Compare routine for Sort
9086 procedure OC_Move (From : Natural; To : Natural);
9087 -- Move routine for Sort
9089 package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
9095 function OC_Lt (Op1, Op2 : Natural) return Boolean is
9097 return OC_Fbit (Op1) < OC_Fbit (Op2);
9104 procedure OC_Move (From : Natural; To : Natural) is
9106 OC_Fbit (To) := OC_Fbit (From);
9107 OC_Lbit (To) := OC_Lbit (From);
9110 -- Start of processing for Overlap_Check
9113 CC := First (Component_Clauses (N));
9114 while Present (CC) loop
9116 -- Exclude component clause already marked in error
9118 if not Error_Posted (CC) then
9121 if Present (Comp) then
9122 OC_Count := OC_Count + 1;
9123 OC_Fbit (OC_Count) := Fbit;
9124 OC_Lbit (OC_Count) := Lbit;
9131 Sorting.Sort (OC_Count);
9133 Overlap_Check_Required := False;
9134 for J in 1 .. OC_Count - 1 loop
9135 if OC_Lbit (J) >= OC_Fbit (J + 1) then
9136 Overlap_Check_Required := True;
9143 -- If Overlap_Check_Required is still True, then we have to do the full
9144 -- scale overlap check, since we have at least two fields that do
9145 -- overlap, and we need to know if that is OK since they are in
9146 -- different variant, or whether we have a definite problem.
9148 if Overlap_Check_Required then
9149 Overlap_Check2 : declare
9150 C1_Ent, C2_Ent : Entity_Id;
9151 -- Entities of components being checked for overlap
9154 -- Component_List node whose Component_Items are being checked
9157 -- Component declaration for component being checked
9160 C1_Ent := First_Entity (Base_Type (Rectype));
9162 -- Loop through all components in record. For each component check
9163 -- for overlap with any of the preceding elements on the component
9164 -- list containing the component and also, if the component is in
9165 -- a variant, check against components outside the case structure.
9166 -- This latter test is repeated recursively up the variant tree.
9168 Main_Component_Loop : while Present (C1_Ent) loop
9169 if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
9170 goto Continue_Main_Component_Loop;
9173 -- Skip overlap check if entity has no declaration node. This
9174 -- happens with discriminants in constrained derived types.
9175 -- Possibly we are missing some checks as a result, but that
9176 -- does not seem terribly serious.
9178 if No (Declaration_Node (C1_Ent)) then
9179 goto Continue_Main_Component_Loop;
9182 Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
9184 -- Loop through component lists that need checking. Check the
9185 -- current component list and all lists in variants above us.
9187 Component_List_Loop : loop
9189 -- If derived type definition, go to full declaration
9190 -- If at outer level, check discriminants if there are any.
9192 if Nkind (Clist) = N_Derived_Type_Definition then
9193 Clist := Parent (Clist);
9196 -- Outer level of record definition, check discriminants
9198 if Nkind_In (Clist, N_Full_Type_Declaration,
9199 N_Private_Type_Declaration)
9201 if Has_Discriminants (Defining_Identifier (Clist)) then
9203 First_Discriminant (Defining_Identifier (Clist));
9204 while Present (C2_Ent) loop
9205 exit when C1_Ent = C2_Ent;
9206 Check_Component_Overlap (C1_Ent, C2_Ent);
9207 Next_Discriminant (C2_Ent);
9211 -- Record extension case
9213 elsif Nkind (Clist) = N_Derived_Type_Definition then
9216 -- Otherwise check one component list
9219 Citem := First (Component_Items (Clist));
9220 while Present (Citem) loop
9221 if Nkind (Citem) = N_Component_Declaration then
9222 C2_Ent := Defining_Identifier (Citem);
9223 exit when C1_Ent = C2_Ent;
9224 Check_Component_Overlap (C1_Ent, C2_Ent);
9231 -- Check for variants above us (the parent of the Clist can
9232 -- be a variant, in which case its parent is a variant part,
9233 -- and the parent of the variant part is a component list
9234 -- whose components must all be checked against the current
9235 -- component for overlap).
9237 if Nkind (Parent (Clist)) = N_Variant then
9238 Clist := Parent (Parent (Parent (Clist)));
9240 -- Check for possible discriminant part in record, this
9241 -- is treated essentially as another level in the
9242 -- recursion. For this case the parent of the component
9243 -- list is the record definition, and its parent is the
9244 -- full type declaration containing the discriminant
9247 elsif Nkind (Parent (Clist)) = N_Record_Definition then
9248 Clist := Parent (Parent ((Clist)));
9250 -- If neither of these two cases, we are at the top of
9254 exit Component_List_Loop;
9256 end loop Component_List_Loop;
9258 <<Continue_Main_Component_Loop>>
9259 Next_Entity (C1_Ent);
9261 end loop Main_Component_Loop;
9265 -- The following circuit deals with warning on record holes (gaps). We
9266 -- skip this check if overlap was detected, since it makes sense for the
9267 -- programmer to fix this illegality before worrying about warnings.
9269 if not Overlap_Detected and Warn_On_Record_Holes then
9270 Record_Hole_Check : declare
9271 Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
9272 -- Full declaration of record type
9274 procedure Check_Component_List
9278 -- Check component list CL for holes. The starting bit should be
9279 -- Sbit. which is zero for the main record component list and set
9280 -- appropriately for recursive calls for variants. DS is set to
9281 -- a list of discriminant specifications to be included in the
9282 -- consideration of components. It is No_List if none to consider.
9284 --------------------------
9285 -- Check_Component_List --
9286 --------------------------
9288 procedure Check_Component_List
9296 Compl := Integer (List_Length (Component_Items (CL)));
9298 if DS /= No_List then
9299 Compl := Compl + Integer (List_Length (DS));
9303 Comps : array (Natural range 0 .. Compl) of Entity_Id;
9304 -- Gather components (zero entry is for sort routine)
9306 Ncomps : Natural := 0;
9307 -- Number of entries stored in Comps (starting at Comps (1))
9310 -- One component item or discriminant specification
9313 -- Starting bit for next component
9321 function Lt (Op1, Op2 : Natural) return Boolean;
9322 -- Compare routine for Sort
9324 procedure Move (From : Natural; To : Natural);
9325 -- Move routine for Sort
9327 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
9333 function Lt (Op1, Op2 : Natural) return Boolean is
9335 return Component_Bit_Offset (Comps (Op1))
9337 Component_Bit_Offset (Comps (Op2));
9344 procedure Move (From : Natural; To : Natural) is
9346 Comps (To) := Comps (From);
9350 -- Gather discriminants into Comp
9352 if DS /= No_List then
9353 Citem := First (DS);
9354 while Present (Citem) loop
9355 if Nkind (Citem) = N_Discriminant_Specification then
9357 Ent : constant Entity_Id :=
9358 Defining_Identifier (Citem);
9360 if Ekind (Ent) = E_Discriminant then
9361 Ncomps := Ncomps + 1;
9362 Comps (Ncomps) := Ent;
9371 -- Gather component entities into Comp
9373 Citem := First (Component_Items (CL));
9374 while Present (Citem) loop
9375 if Nkind (Citem) = N_Component_Declaration then
9376 Ncomps := Ncomps + 1;
9377 Comps (Ncomps) := Defining_Identifier (Citem);
9383 -- Now sort the component entities based on the first bit.
9384 -- Note we already know there are no overlapping components.
9386 Sorting.Sort (Ncomps);
9388 -- Loop through entries checking for holes
9391 for J in 1 .. Ncomps loop
9393 Error_Msg_Uint_1 := Component_Bit_Offset (CEnt) - Nbit;
9395 if Error_Msg_Uint_1 > 0 then
9397 ("?H?^-bit gap before component&",
9398 Component_Name (Component_Clause (CEnt)), CEnt);
9401 Nbit := Component_Bit_Offset (CEnt) + Esize (CEnt);
9404 -- Process variant parts recursively if present
9406 if Present (Variant_Part (CL)) then
9407 Variant := First (Variants (Variant_Part (CL)));
9408 while Present (Variant) loop
9409 Check_Component_List
9410 (Component_List (Variant), Nbit, No_List);
9415 end Check_Component_List;
9417 -- Start of processing for Record_Hole_Check
9424 if Is_Tagged_Type (Rectype) then
9425 Sbit := UI_From_Int (System_Address_Size);
9430 if Nkind (Decl) = N_Full_Type_Declaration
9431 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
9433 Check_Component_List
9434 (Component_List (Type_Definition (Decl)),
9436 Discriminant_Specifications (Decl));
9439 end Record_Hole_Check;
9442 -- For records that have component clauses for all components, and whose
9443 -- size is less than or equal to 32, we need to know the size in the
9444 -- front end to activate possible packed array processing where the
9445 -- component type is a record.
9447 -- At this stage Hbit + 1 represents the first unused bit from all the
9448 -- component clauses processed, so if the component clauses are
9449 -- complete, then this is the length of the record.
9451 -- For records longer than System.Storage_Unit, and for those where not
9452 -- all components have component clauses, the back end determines the
9453 -- length (it may for example be appropriate to round up the size
9454 -- to some convenient boundary, based on alignment considerations, etc).
9456 if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then
9458 -- Nothing to do if at least one component has no component clause
9460 Comp := First_Component_Or_Discriminant (Rectype);
9461 while Present (Comp) loop
9462 exit when No (Component_Clause (Comp));
9463 Next_Component_Or_Discriminant (Comp);
9466 -- If we fall out of loop, all components have component clauses
9467 -- and so we can set the size to the maximum value.
9470 Set_RM_Size (Rectype, Hbit + 1);
9473 end Check_Record_Representation_Clause;
9479 procedure Check_Size
9483 Biased : out Boolean)
9485 UT : constant Entity_Id := Underlying_Type (T);
9491 -- Reject patently improper size values.
9493 if Is_Elementary_Type (T)
9494 and then Siz > UI_From_Int (Int'Last)
9496 Error_Msg_N ("Size value too large for elementary type", N);
9498 if Nkind (Original_Node (N)) = N_Op_Expon then
9500 ("\maybe '* was meant, rather than '*'*", Original_Node (N));
9504 -- Dismiss generic types
9506 if Is_Generic_Type (T)
9508 Is_Generic_Type (UT)
9510 Is_Generic_Type (Root_Type (UT))
9514 -- Guard against previous errors
9516 elsif No (UT) or else UT = Any_Type then
9517 Check_Error_Detected;
9520 -- Check case of bit packed array
9522 elsif Is_Array_Type (UT)
9523 and then Known_Static_Component_Size (UT)
9524 and then Is_Bit_Packed_Array (UT)
9532 Asiz := Component_Size (UT);
9533 Indx := First_Index (UT);
9535 Ityp := Etype (Indx);
9537 -- If non-static bound, then we are not in the business of
9538 -- trying to check the length, and indeed an error will be
9539 -- issued elsewhere, since sizes of non-static array types
9540 -- cannot be set implicitly or explicitly.
9542 if not Is_Static_Subtype (Ityp) then
9546 -- Otherwise accumulate next dimension
9548 Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
9549 Expr_Value (Type_Low_Bound (Ityp)) +
9553 exit when No (Indx);
9560 Error_Msg_Uint_1 := Asiz;
9562 ("size for& too small, minimum allowed is ^", N, T);
9563 Set_Esize (T, Asiz);
9564 Set_RM_Size (T, Asiz);
9568 -- All other composite types are ignored
9570 elsif Is_Composite_Type (UT) then
9573 -- For fixed-point types, don't check minimum if type is not frozen,
9574 -- since we don't know all the characteristics of the type that can
9575 -- affect the size (e.g. a specified small) till freeze time.
9577 elsif Is_Fixed_Point_Type (UT)
9578 and then not Is_Frozen (UT)
9582 -- Cases for which a minimum check is required
9585 -- Ignore if specified size is correct for the type
9587 if Known_Esize (UT) and then Siz = Esize (UT) then
9591 -- Otherwise get minimum size
9593 M := UI_From_Int (Minimum_Size (UT));
9597 -- Size is less than minimum size, but one possibility remains
9598 -- that we can manage with the new size if we bias the type.
9600 M := UI_From_Int (Minimum_Size (UT, Biased => True));
9603 Error_Msg_Uint_1 := M;
9605 ("size for& too small, minimum allowed is ^", N, T);
9615 --------------------------
9616 -- Freeze_Entity_Checks --
9617 --------------------------
9619 procedure Freeze_Entity_Checks (N : Node_Id) is
9620 E : constant Entity_Id := Entity (N);
9622 Non_Generic_Case : constant Boolean := Nkind (N) = N_Freeze_Entity;
9623 -- True in non-generic case. Some of the processing here is skipped
9624 -- for the generic case since it is not needed. Basically in the
9625 -- generic case, we only need to do stuff that might generate error
9626 -- messages or warnings.
9628 -- Remember that we are processing a freezing entity. Required to
9629 -- ensure correct decoration of internal entities associated with
9630 -- interfaces (see New_Overloaded_Entity).
9632 Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
9634 -- For tagged types covering interfaces add internal entities that link
9635 -- the primitives of the interfaces with the primitives that cover them.
9636 -- Note: These entities were originally generated only when generating
9637 -- code because their main purpose was to provide support to initialize
9638 -- the secondary dispatch tables. They are now generated also when
9639 -- compiling with no code generation to provide ASIS the relationship
9640 -- between interface primitives and tagged type primitives. They are
9641 -- also used to locate primitives covering interfaces when processing
9642 -- generics (see Derive_Subprograms).
9644 -- This is not needed in the generic case
9646 if Ada_Version >= Ada_2005
9647 and then Non_Generic_Case
9648 and then Ekind (E) = E_Record_Type
9649 and then Is_Tagged_Type (E)
9650 and then not Is_Interface (E)
9651 and then Has_Interfaces (E)
9653 -- This would be a good common place to call the routine that checks
9654 -- overriding of interface primitives (and thus factorize calls to
9655 -- Check_Abstract_Overriding located at different contexts in the
9656 -- compiler). However, this is not possible because it causes
9657 -- spurious errors in case of late overriding.
9659 Add_Internal_Interface_Entities (E);
9664 if Ekind (E) = E_Record_Type
9665 and then Is_CPP_Class (E)
9666 and then Is_Tagged_Type (E)
9667 and then Tagged_Type_Expansion
9669 if CPP_Num_Prims (E) = 0 then
9671 -- If the CPP type has user defined components then it must import
9672 -- primitives from C++. This is required because if the C++ class
9673 -- has no primitives then the C++ compiler does not added the _tag
9674 -- component to the type.
9676 if First_Entity (E) /= Last_Entity (E) then
9678 ("'C'P'P type must import at least one primitive from C++??",
9683 -- Check that all its primitives are abstract or imported from C++.
9684 -- Check also availability of the C++ constructor.
9687 Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
9689 Error_Reported : Boolean := False;
9693 Elmt := First_Elmt (Primitive_Operations (E));
9694 while Present (Elmt) loop
9695 Prim := Node (Elmt);
9697 if Comes_From_Source (Prim) then
9698 if Is_Abstract_Subprogram (Prim) then
9701 elsif not Is_Imported (Prim)
9702 or else Convention (Prim) /= Convention_CPP
9705 ("primitives of 'C'P'P types must be imported from C++ "
9706 & "or abstract??", Prim);
9708 elsif not Has_Constructors
9709 and then not Error_Reported
9711 Error_Msg_Name_1 := Chars (E);
9713 ("??'C'P'P constructor required for type %", Prim);
9714 Error_Reported := True;
9723 -- Check Ada derivation of CPP type
9725 if Expander_Active -- why? losing errors in -gnatc mode???
9726 and then Tagged_Type_Expansion
9727 and then Ekind (E) = E_Record_Type
9728 and then Etype (E) /= E
9729 and then Is_CPP_Class (Etype (E))
9730 and then CPP_Num_Prims (Etype (E)) > 0
9731 and then not Is_CPP_Class (E)
9732 and then not Has_CPP_Constructors (Etype (E))
9734 -- If the parent has C++ primitives but it has no constructor then
9735 -- check that all the primitives are overridden in this derivation;
9736 -- otherwise the constructor of the parent is needed to build the
9744 Elmt := First_Elmt (Primitive_Operations (E));
9745 while Present (Elmt) loop
9746 Prim := Node (Elmt);
9748 if not Is_Abstract_Subprogram (Prim)
9749 and then No (Interface_Alias (Prim))
9750 and then Find_Dispatching_Type (Ultimate_Alias (Prim)) /= E
9752 Error_Msg_Name_1 := Chars (Etype (E));
9754 ("'C'P'P constructor required for parent type %", E);
9763 Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
9765 -- If we have a type with predicates, build predicate function. This
9766 -- is not needed in the generic case, and is not needed within TSS
9767 -- subprograms and other predefined primitives.
9770 and then Is_Type (E)
9771 and then Has_Predicates (E)
9772 and then not Within_Internal_Subprogram
9774 Build_Predicate_Functions (E, N);
9777 -- If type has delayed aspects, this is where we do the preanalysis at
9778 -- the freeze point, as part of the consistent visibility check. Note
9779 -- that this must be done after calling Build_Predicate_Functions or
9780 -- Build_Invariant_Procedure since these subprograms fix occurrences of
9781 -- the subtype name in the saved expression so that they will not cause
9782 -- trouble in the preanalysis.
9784 -- This is also not needed in the generic case
9787 and then Has_Delayed_Aspects (E)
9788 and then Scope (E) = Current_Scope
9790 -- Retrieve the visibility to the discriminants in order to properly
9791 -- analyze the aspects.
9793 Push_Scope_And_Install_Discriminants (E);
9799 -- Look for aspect specification entries for this entity
9801 Ritem := First_Rep_Item (E);
9802 while Present (Ritem) loop
9803 if Nkind (Ritem) = N_Aspect_Specification
9804 and then Entity (Ritem) = E
9805 and then Is_Delayed_Aspect (Ritem)
9807 Check_Aspect_At_Freeze_Point (Ritem);
9810 Next_Rep_Item (Ritem);
9814 Uninstall_Discriminants_And_Pop_Scope (E);
9817 -- For a record type, deal with variant parts. This has to be delayed
9818 -- to this point, because of the issue of statically precicated
9819 -- subtypes, which we have to ensure are frozen before checking
9820 -- choices, since we need to have the static choice list set.
9822 if Is_Record_Type (E) then
9823 Check_Variant_Part : declare
9824 D : constant Node_Id := Declaration_Node (E);
9829 Others_Present : Boolean;
9830 pragma Warnings (Off, Others_Present);
9831 -- Indicates others present, not used in this case
9833 procedure Non_Static_Choice_Error (Choice : Node_Id);
9834 -- Error routine invoked by the generic instantiation below when
9835 -- the variant part has a non static choice.
9837 procedure Process_Declarations (Variant : Node_Id);
9838 -- Processes declarations associated with a variant. We analyzed
9839 -- the declarations earlier (in Sem_Ch3.Analyze_Variant_Part),
9840 -- but we still need the recursive call to Check_Choices for any
9841 -- nested variant to get its choices properly processed. This is
9842 -- also where we expand out the choices if expansion is active.
9844 package Variant_Choices_Processing is new
9845 Generic_Check_Choices
9846 (Process_Empty_Choice => No_OP,
9847 Process_Non_Static_Choice => Non_Static_Choice_Error,
9848 Process_Associated_Node => Process_Declarations);
9849 use Variant_Choices_Processing;
9851 -----------------------------
9852 -- Non_Static_Choice_Error --
9853 -----------------------------
9855 procedure Non_Static_Choice_Error (Choice : Node_Id) is
9857 Flag_Non_Static_Expr
9858 ("choice given in variant part is not static!", Choice);
9859 end Non_Static_Choice_Error;
9861 --------------------------
9862 -- Process_Declarations --
9863 --------------------------
9865 procedure Process_Declarations (Variant : Node_Id) is
9866 CL : constant Node_Id := Component_List (Variant);
9870 -- Check for static predicate present in this variant
9872 if Has_SP_Choice (Variant) then
9874 -- Here we expand. You might expect to find this call in
9875 -- Expand_N_Variant_Part, but that is called when we first
9876 -- see the variant part, and we cannot do this expansion
9877 -- earlier than the freeze point, since for statically
9878 -- predicated subtypes, the predicate is not known till
9879 -- the freeze point.
9881 -- Furthermore, we do this expansion even if the expander
9882 -- is not active, because other semantic processing, e.g.
9883 -- for aggregates, requires the expanded list of choices.
9885 -- If the expander is not active, then we can't just clobber
9886 -- the list since it would invalidate the ASIS -gnatct tree.
9887 -- So we have to rewrite the variant part with a Rewrite
9888 -- call that replaces it with a copy and clobber the copy.
9890 if not Expander_Active then
9892 NewV : constant Node_Id := New_Copy (Variant);
9894 Set_Discrete_Choices
9895 (NewV, New_Copy_List (Discrete_Choices (Variant)));
9896 Rewrite (Variant, NewV);
9900 Expand_Static_Predicates_In_Choices (Variant);
9903 -- We don't need to worry about the declarations in the variant
9904 -- (since they were analyzed by Analyze_Choices when we first
9905 -- encountered the variant), but we do need to take care of
9906 -- expansion of any nested variants.
9908 if not Null_Present (CL) then
9909 VP := Variant_Part (CL);
9911 if Present (VP) then
9913 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
9916 end Process_Declarations;
9918 -- Start of processing for Check_Variant_Part
9921 -- Find component list
9925 if Nkind (D) = N_Full_Type_Declaration then
9926 T := Type_Definition (D);
9928 if Nkind (T) = N_Record_Definition then
9929 C := Component_List (T);
9931 elsif Nkind (T) = N_Derived_Type_Definition
9932 and then Present (Record_Extension_Part (T))
9934 C := Component_List (Record_Extension_Part (T));
9938 -- Case of variant part present
9940 if Present (C) and then Present (Variant_Part (C)) then
9941 VP := Variant_Part (C);
9946 (VP, Variants (VP), Etype (Name (VP)), Others_Present);
9948 -- If the last variant does not contain the Others choice,
9949 -- replace it with an N_Others_Choice node since Gigi always
9950 -- wants an Others. Note that we do not bother to call Analyze
9951 -- on the modified variant part, since its only effect would be
9952 -- to compute the Others_Discrete_Choices node laboriously, and
9953 -- of course we already know the list of choices corresponding
9954 -- to the others choice (it's the list we're replacing).
9956 -- We only want to do this if the expander is active, since
9957 -- we do not want to clobber the ASIS tree.
9959 if Expander_Active then
9961 Last_Var : constant Node_Id :=
9962 Last_Non_Pragma (Variants (VP));
9964 Others_Node : Node_Id;
9967 if Nkind (First (Discrete_Choices (Last_Var))) /=
9970 Others_Node := Make_Others_Choice (Sloc (Last_Var));
9971 Set_Others_Discrete_Choices
9972 (Others_Node, Discrete_Choices (Last_Var));
9973 Set_Discrete_Choices
9974 (Last_Var, New_List (Others_Node));
9979 end Check_Variant_Part;
9981 end Freeze_Entity_Checks;
9983 -------------------------
9984 -- Get_Alignment_Value --
9985 -------------------------
9987 function Get_Alignment_Value (Expr : Node_Id) return Uint is
9988 Align : constant Uint := Static_Integer (Expr);
9991 if Align = No_Uint then
9994 elsif Align <= 0 then
9995 Error_Msg_N ("alignment value must be positive", Expr);
9999 for J in Int range 0 .. 64 loop
10001 M : constant Uint := Uint_2 ** J;
10004 exit when M = Align;
10008 ("alignment value must be power of 2", Expr);
10016 end Get_Alignment_Value;
10018 -------------------------------------
10019 -- Inherit_Aspects_At_Freeze_Point --
10020 -------------------------------------
10022 procedure Inherit_Aspects_At_Freeze_Point (Typ : Entity_Id) is
10023 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10024 (Rep_Item : Node_Id) return Boolean;
10025 -- This routine checks if Rep_Item is either a pragma or an aspect
10026 -- specification node whose correponding pragma (if any) is present in
10027 -- the Rep Item chain of the entity it has been specified to.
10029 --------------------------------------------------
10030 -- Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item --
10031 --------------------------------------------------
10033 function Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10034 (Rep_Item : Node_Id) return Boolean
10037 return Nkind (Rep_Item) = N_Pragma
10038 or else Present_In_Rep_Item
10039 (Entity (Rep_Item), Aspect_Rep_Item (Rep_Item));
10040 end Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item;
10042 -- Start of processing for Inherit_Aspects_At_Freeze_Point
10045 -- A representation item is either subtype-specific (Size and Alignment
10046 -- clauses) or type-related (all others). Subtype-specific aspects may
10047 -- differ for different subtypes of the same type (RM 13.1.8).
10049 -- A derived type inherits each type-related representation aspect of
10050 -- its parent type that was directly specified before the declaration of
10051 -- the derived type (RM 13.1.15).
10053 -- A derived subtype inherits each subtype-specific representation
10054 -- aspect of its parent subtype that was directly specified before the
10055 -- declaration of the derived type (RM 13.1.15).
10057 -- The general processing involves inheriting a representation aspect
10058 -- from a parent type whenever the first rep item (aspect specification,
10059 -- attribute definition clause, pragma) corresponding to the given
10060 -- representation aspect in the rep item chain of Typ, if any, isn't
10061 -- directly specified to Typ but to one of its parents.
10063 -- ??? Note that, for now, just a limited number of representation
10064 -- aspects have been inherited here so far. Many of them are
10065 -- still inherited in Sem_Ch3. This will be fixed soon. Here is
10066 -- a non- exhaustive list of aspects that likely also need to
10067 -- be moved to this routine: Alignment, Component_Alignment,
10068 -- Component_Size, Machine_Radix, Object_Size, Pack, Predicates,
10069 -- Preelaborable_Initialization, RM_Size and Small.
10071 if Nkind (Parent (Typ)) = N_Private_Extension_Declaration then
10077 if not Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005, False)
10078 and then Has_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005)
10079 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10080 (Get_Rep_Item (Typ, Name_Ada_05, Name_Ada_2005))
10082 Set_Is_Ada_2005_Only (Typ);
10087 if not Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012, False)
10088 and then Has_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012)
10089 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10090 (Get_Rep_Item (Typ, Name_Ada_12, Name_Ada_2012))
10092 Set_Is_Ada_2012_Only (Typ);
10097 if not Has_Rep_Item (Typ, Name_Atomic, Name_Shared, False)
10098 and then Has_Rep_Pragma (Typ, Name_Atomic, Name_Shared)
10099 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10100 (Get_Rep_Item (Typ, Name_Atomic, Name_Shared))
10102 Set_Is_Atomic (Typ);
10103 Set_Treat_As_Volatile (Typ);
10104 Set_Is_Volatile (Typ);
10107 -- Default_Component_Value
10109 if Is_Array_Type (Typ)
10110 and then Is_Base_Type (Typ)
10111 and then Has_Rep_Item (Typ, Name_Default_Component_Value, False)
10112 and then Has_Rep_Item (Typ, Name_Default_Component_Value)
10114 Set_Default_Aspect_Component_Value (Typ,
10115 Default_Aspect_Component_Value
10116 (Entity (Get_Rep_Item (Typ, Name_Default_Component_Value))));
10121 if Is_Scalar_Type (Typ)
10122 and then Is_Base_Type (Typ)
10123 and then Has_Rep_Item (Typ, Name_Default_Value, False)
10124 and then Has_Rep_Item (Typ, Name_Default_Value)
10126 Set_Default_Aspect_Value (Typ,
10127 Default_Aspect_Value
10128 (Entity (Get_Rep_Item (Typ, Name_Default_Value))));
10133 if not Has_Rep_Item (Typ, Name_Discard_Names, False)
10134 and then Has_Rep_Item (Typ, Name_Discard_Names)
10135 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10136 (Get_Rep_Item (Typ, Name_Discard_Names))
10138 Set_Discard_Names (Typ);
10143 if not Has_Rep_Item (Typ, Name_Invariant, False)
10144 and then Has_Rep_Item (Typ, Name_Invariant)
10145 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10146 (Get_Rep_Item (Typ, Name_Invariant))
10148 Set_Has_Invariants (Typ);
10150 if Class_Present (Get_Rep_Item (Typ, Name_Invariant)) then
10151 Set_Has_Inheritable_Invariants (Typ);
10157 if not Has_Rep_Item (Typ, Name_Volatile, False)
10158 and then Has_Rep_Item (Typ, Name_Volatile)
10159 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10160 (Get_Rep_Item (Typ, Name_Volatile))
10162 Set_Treat_As_Volatile (Typ);
10163 Set_Is_Volatile (Typ);
10166 -- Inheritance for derived types only
10168 if Is_Derived_Type (Typ) then
10170 Bas_Typ : constant Entity_Id := Base_Type (Typ);
10171 Imp_Bas_Typ : constant Entity_Id := Implementation_Base_Type (Typ);
10174 -- Atomic_Components
10176 if not Has_Rep_Item (Typ, Name_Atomic_Components, False)
10177 and then Has_Rep_Item (Typ, Name_Atomic_Components)
10178 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10179 (Get_Rep_Item (Typ, Name_Atomic_Components))
10181 Set_Has_Atomic_Components (Imp_Bas_Typ);
10184 -- Volatile_Components
10186 if not Has_Rep_Item (Typ, Name_Volatile_Components, False)
10187 and then Has_Rep_Item (Typ, Name_Volatile_Components)
10188 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10189 (Get_Rep_Item (Typ, Name_Volatile_Components))
10191 Set_Has_Volatile_Components (Imp_Bas_Typ);
10194 -- Finalize_Storage_Only.
10196 if not Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only, False)
10197 and then Has_Rep_Pragma (Typ, Name_Finalize_Storage_Only)
10199 Set_Finalize_Storage_Only (Bas_Typ);
10202 -- Universal_Aliasing
10204 if not Has_Rep_Item (Typ, Name_Universal_Aliasing, False)
10205 and then Has_Rep_Item (Typ, Name_Universal_Aliasing)
10206 and then Is_Pragma_Or_Corr_Pragma_Present_In_Rep_Item
10207 (Get_Rep_Item (Typ, Name_Universal_Aliasing))
10209 Set_Universal_Aliasing (Imp_Bas_Typ);
10212 -- Record type specific aspects
10214 if Is_Record_Type (Typ) then
10218 if not Has_Rep_Item (Typ, Name_Bit_Order, False)
10219 and then Has_Rep_Item (Typ, Name_Bit_Order)
10221 Set_Reverse_Bit_Order (Bas_Typ,
10222 Reverse_Bit_Order (Entity (Name
10223 (Get_Rep_Item (Typ, Name_Bit_Order)))));
10226 -- Scalar_Storage_Order
10228 if not Has_Rep_Item (Typ, Name_Scalar_Storage_Order, False)
10229 and then Has_Rep_Item (Typ, Name_Scalar_Storage_Order)
10231 Set_Reverse_Storage_Order (Bas_Typ,
10232 Reverse_Storage_Order (Entity (Name
10233 (Get_Rep_Item (Typ, Name_Scalar_Storage_Order)))));
10238 end Inherit_Aspects_At_Freeze_Point;
10244 procedure Initialize is
10246 Address_Clause_Checks.Init;
10247 Independence_Checks.Init;
10248 Unchecked_Conversions.Init;
10251 ---------------------------
10252 -- Install_Discriminants --
10253 ---------------------------
10255 procedure Install_Discriminants (E : Entity_Id) is
10259 Disc := First_Discriminant (E);
10260 while Present (Disc) loop
10261 Prev := Current_Entity (Disc);
10262 Set_Current_Entity (Disc);
10263 Set_Is_Immediately_Visible (Disc);
10264 Set_Homonym (Disc, Prev);
10265 Next_Discriminant (Disc);
10267 end Install_Discriminants;
10269 -------------------------
10270 -- Is_Operational_Item --
10271 -------------------------
10273 function Is_Operational_Item (N : Node_Id) return Boolean is
10275 if Nkind (N) /= N_Attribute_Definition_Clause then
10280 Id : constant Attribute_Id := Get_Attribute_Id (Chars (N));
10282 return Id = Attribute_Input
10283 or else Id = Attribute_Output
10284 or else Id = Attribute_Read
10285 or else Id = Attribute_Write
10286 or else Id = Attribute_External_Tag;
10289 end Is_Operational_Item;
10291 ---------------------
10292 -- Kill_Rep_Clause --
10293 ---------------------
10295 procedure Kill_Rep_Clause (N : Node_Id) is
10297 pragma Assert (Ignore_Rep_Clauses);
10299 -- Note: we use Replace rather than Rewrite, because we don't want
10300 -- ASIS to be able to use Original_Node to dig out the (undecorated)
10301 -- rep clause that is being replaced.
10303 Replace (N, Make_Null_Statement (Sloc (N)));
10305 -- The null statement must be marked as not coming from source. This is
10306 -- so that ASIS ignores if, and also the back end does not expect bogus
10307 -- "from source" null statements in weird places (e.g. in declarative
10308 -- regions where such null statements are not allowed).
10310 Set_Comes_From_Source (N, False);
10311 end Kill_Rep_Clause;
10317 function Minimum_Size
10319 Biased : Boolean := False) return Nat
10321 Lo : Uint := No_Uint;
10322 Hi : Uint := No_Uint;
10323 LoR : Ureal := No_Ureal;
10324 HiR : Ureal := No_Ureal;
10325 LoSet : Boolean := False;
10326 HiSet : Boolean := False;
10329 Ancest : Entity_Id;
10330 R_Typ : constant Entity_Id := Root_Type (T);
10333 -- If bad type, return 0
10335 if T = Any_Type then
10338 -- For generic types, just return zero. There cannot be any legitimate
10339 -- need to know such a size, but this routine may be called with a
10340 -- generic type as part of normal processing.
10342 elsif Is_Generic_Type (R_Typ)
10343 or else R_Typ = Any_Type
10347 -- Access types. Normally an access type cannot have a size smaller
10348 -- than the size of System.Address. The exception is on VMS, where
10349 -- we have short and long addresses, and it is possible for an access
10350 -- type to have a short address size (and thus be less than the size
10351 -- of System.Address itself). We simply skip the check for VMS, and
10352 -- leave it to the back end to do the check.
10354 elsif Is_Access_Type (T) then
10355 if OpenVMS_On_Target then
10358 return System_Address_Size;
10361 -- Floating-point types
10363 elsif Is_Floating_Point_Type (T) then
10364 return UI_To_Int (Esize (R_Typ));
10368 elsif Is_Discrete_Type (T) then
10370 -- The following loop is looking for the nearest compile time known
10371 -- bounds following the ancestor subtype chain. The idea is to find
10372 -- the most restrictive known bounds information.
10376 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
10381 if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
10382 Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
10389 if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
10390 Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
10396 Ancest := Ancestor_Subtype (Ancest);
10398 if No (Ancest) then
10399 Ancest := Base_Type (T);
10401 if Is_Generic_Type (Ancest) then
10407 -- Fixed-point types. We can't simply use Expr_Value to get the
10408 -- Corresponding_Integer_Value values of the bounds, since these do not
10409 -- get set till the type is frozen, and this routine can be called
10410 -- before the type is frozen. Similarly the test for bounds being static
10411 -- needs to include the case where we have unanalyzed real literals for
10412 -- the same reason.
10414 elsif Is_Fixed_Point_Type (T) then
10416 -- The following loop is looking for the nearest compile time known
10417 -- bounds following the ancestor subtype chain. The idea is to find
10418 -- the most restrictive known bounds information.
10422 if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
10426 -- Note: In the following two tests for LoSet and HiSet, it may
10427 -- seem redundant to test for N_Real_Literal here since normally
10428 -- one would assume that the test for the value being known at
10429 -- compile time includes this case. However, there is a glitch.
10430 -- If the real literal comes from folding a non-static expression,
10431 -- then we don't consider any non- static expression to be known
10432 -- at compile time if we are in configurable run time mode (needed
10433 -- in some cases to give a clearer definition of what is and what
10434 -- is not accepted). So the test is indeed needed. Without it, we
10435 -- would set neither Lo_Set nor Hi_Set and get an infinite loop.
10438 if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
10439 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
10441 LoR := Expr_Value_R (Type_Low_Bound (Ancest));
10448 if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
10449 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
10451 HiR := Expr_Value_R (Type_High_Bound (Ancest));
10457 Ancest := Ancestor_Subtype (Ancest);
10459 if No (Ancest) then
10460 Ancest := Base_Type (T);
10462 if Is_Generic_Type (Ancest) then
10468 Lo := UR_To_Uint (LoR / Small_Value (T));
10469 Hi := UR_To_Uint (HiR / Small_Value (T));
10471 -- No other types allowed
10474 raise Program_Error;
10477 -- Fall through with Hi and Lo set. Deal with biased case
10480 and then not Is_Fixed_Point_Type (T)
10481 and then not (Is_Enumeration_Type (T)
10482 and then Has_Non_Standard_Rep (T)))
10483 or else Has_Biased_Representation (T)
10489 -- Signed case. Note that we consider types like range 1 .. -1 to be
10490 -- signed for the purpose of computing the size, since the bounds have
10491 -- to be accommodated in the base type.
10493 if Lo < 0 or else Hi < 0 then
10497 -- S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
10498 -- Note that we accommodate the case where the bounds cross. This
10499 -- can happen either because of the way the bounds are declared
10500 -- or because of the algorithm in Freeze_Fixed_Point_Type.
10514 -- If both bounds are positive, make sure that both are represen-
10515 -- table in the case where the bounds are crossed. This can happen
10516 -- either because of the way the bounds are declared, or because of
10517 -- the algorithm in Freeze_Fixed_Point_Type.
10523 -- S = size, (can accommodate 0 .. (2**size - 1))
10526 while Hi >= Uint_2 ** S loop
10534 ---------------------------
10535 -- New_Stream_Subprogram --
10536 ---------------------------
10538 procedure New_Stream_Subprogram
10542 Nam : TSS_Name_Type)
10544 Loc : constant Source_Ptr := Sloc (N);
10545 Sname : constant Name_Id := Make_TSS_Name (Base_Type (Ent), Nam);
10546 Subp_Id : Entity_Id;
10547 Subp_Decl : Node_Id;
10551 Defer_Declaration : constant Boolean :=
10552 Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
10553 -- For a tagged type, there is a declaration for each stream attribute
10554 -- at the freeze point, and we must generate only a completion of this
10555 -- declaration. We do the same for private types, because the full view
10556 -- might be tagged. Otherwise we generate a declaration at the point of
10557 -- the attribute definition clause.
10559 function Build_Spec return Node_Id;
10560 -- Used for declaration and renaming declaration, so that this is
10561 -- treated as a renaming_as_body.
10567 function Build_Spec return Node_Id is
10568 Out_P : constant Boolean := (Nam = TSS_Stream_Read);
10571 T_Ref : constant Node_Id := New_Occurrence_Of (Etyp, Loc);
10574 Subp_Id := Make_Defining_Identifier (Loc, Sname);
10576 -- S : access Root_Stream_Type'Class
10578 Formals := New_List (
10579 Make_Parameter_Specification (Loc,
10580 Defining_Identifier =>
10581 Make_Defining_Identifier (Loc, Name_S),
10583 Make_Access_Definition (Loc,
10585 New_Occurrence_Of (
10586 Designated_Type (Etype (F)), Loc))));
10588 if Nam = TSS_Stream_Input then
10590 Make_Function_Specification (Loc,
10591 Defining_Unit_Name => Subp_Id,
10592 Parameter_Specifications => Formals,
10593 Result_Definition => T_Ref);
10597 Append_To (Formals,
10598 Make_Parameter_Specification (Loc,
10599 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
10600 Out_Present => Out_P,
10601 Parameter_Type => T_Ref));
10604 Make_Procedure_Specification (Loc,
10605 Defining_Unit_Name => Subp_Id,
10606 Parameter_Specifications => Formals);
10612 -- Start of processing for New_Stream_Subprogram
10615 F := First_Formal (Subp);
10617 if Ekind (Subp) = E_Procedure then
10618 Etyp := Etype (Next_Formal (F));
10620 Etyp := Etype (Subp);
10623 -- Prepare subprogram declaration and insert it as an action on the
10624 -- clause node. The visibility for this entity is used to test for
10625 -- visibility of the attribute definition clause (in the sense of
10626 -- 8.3(23) as amended by AI-195).
10628 if not Defer_Declaration then
10630 Make_Subprogram_Declaration (Loc,
10631 Specification => Build_Spec);
10633 -- For a tagged type, there is always a visible declaration for each
10634 -- stream TSS (it is a predefined primitive operation), and the
10635 -- completion of this declaration occurs at the freeze point, which is
10636 -- not always visible at places where the attribute definition clause is
10637 -- visible. So, we create a dummy entity here for the purpose of
10638 -- tracking the visibility of the attribute definition clause itself.
10642 Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
10644 Make_Object_Declaration (Loc,
10645 Defining_Identifier => Subp_Id,
10646 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
10649 Insert_Action (N, Subp_Decl);
10650 Set_Entity (N, Subp_Id);
10653 Make_Subprogram_Renaming_Declaration (Loc,
10654 Specification => Build_Spec,
10655 Name => New_Occurrence_Of (Subp, Loc));
10657 if Defer_Declaration then
10658 Set_TSS (Base_Type (Ent), Subp_Id);
10660 Insert_Action (N, Subp_Decl);
10661 Copy_TSS (Subp_Id, Base_Type (Ent));
10663 end New_Stream_Subprogram;
10665 ------------------------------------------
10666 -- Push_Scope_And_Install_Discriminants --
10667 ------------------------------------------
10669 procedure Push_Scope_And_Install_Discriminants (E : Entity_Id) is
10671 if Has_Discriminants (E) then
10674 -- Make discriminants visible for type declarations and protected
10675 -- type declarations, not for subtype declarations (RM 13.1.1 (12/3))
10677 if Nkind (Parent (E)) /= N_Subtype_Declaration then
10678 Install_Discriminants (E);
10681 end Push_Scope_And_Install_Discriminants;
10683 ------------------------
10684 -- Rep_Item_Too_Early --
10685 ------------------------
10687 function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
10689 -- Cannot apply non-operational rep items to generic types
10691 if Is_Operational_Item (N) then
10695 and then Is_Generic_Type (Root_Type (T))
10697 Error_Msg_N ("representation item not allowed for generic type", N);
10701 -- Otherwise check for incomplete type
10703 if Is_Incomplete_Or_Private_Type (T)
10704 and then No (Underlying_Type (T))
10706 (Nkind (N) /= N_Pragma
10707 or else Get_Pragma_Id (N) /= Pragma_Import)
10710 ("representation item must be after full type declaration", N);
10713 -- If the type has incomplete components, a representation clause is
10714 -- illegal but stream attributes and Convention pragmas are correct.
10716 elsif Has_Private_Component (T) then
10717 if Nkind (N) = N_Pragma then
10722 ("representation item must appear after type is fully defined",
10729 end Rep_Item_Too_Early;
10731 -----------------------
10732 -- Rep_Item_Too_Late --
10733 -----------------------
10735 function Rep_Item_Too_Late
10738 FOnly : Boolean := False) return Boolean
10741 Parent_Type : Entity_Id;
10743 procedure Too_Late;
10744 -- Output the too late message. Note that this is not considered a
10745 -- serious error, since the effect is simply that we ignore the
10746 -- representation clause in this case.
10752 procedure Too_Late is
10754 -- Other compilers seem more relaxed about rep items appearing too
10755 -- late. Since analysis tools typically don't care about rep items
10756 -- anyway, no reason to be too strict about this.
10758 if not Relaxed_RM_Semantics then
10759 Error_Msg_N ("|representation item appears too late!", N);
10763 -- Start of processing for Rep_Item_Too_Late
10766 -- First make sure entity is not frozen (RM 13.1(9))
10770 -- Exclude imported types, which may be frozen if they appear in a
10771 -- representation clause for a local type.
10773 and then not From_Limited_With (T)
10775 -- Exclude generated entities (not coming from source). The common
10776 -- case is when we generate a renaming which prematurely freezes the
10777 -- renamed internal entity, but we still want to be able to set copies
10778 -- of attribute values such as Size/Alignment.
10780 and then Comes_From_Source (T)
10783 S := First_Subtype (T);
10785 if Present (Freeze_Node (S)) then
10787 ("??no more representation items for }", Freeze_Node (S), S);
10792 -- Check for case of non-tagged derived type whose parent either has
10793 -- primitive operations, or is a by reference type (RM 13.1(10)).
10797 and then Is_Derived_Type (T)
10798 and then not Is_Tagged_Type (T)
10800 Parent_Type := Etype (Base_Type (T));
10802 if Has_Primitive_Operations (Parent_Type) then
10805 ("primitive operations already defined for&!", N, Parent_Type);
10808 elsif Is_By_Reference_Type (Parent_Type) then
10811 ("parent type & is a by reference type!", N, Parent_Type);
10816 -- No error, link item into head of chain of rep items for the entity,
10817 -- but avoid chaining if we have an overloadable entity, and the pragma
10818 -- is one that can apply to multiple overloaded entities.
10820 if Is_Overloadable (T) and then Nkind (N) = N_Pragma then
10822 Pname : constant Name_Id := Pragma_Name (N);
10824 if Nam_In (Pname, Name_Convention, Name_Import, Name_Export,
10825 Name_External, Name_Interface)
10832 Record_Rep_Item (T, N);
10834 end Rep_Item_Too_Late;
10836 -------------------------------------
10837 -- Replace_Type_References_Generic --
10838 -------------------------------------
10840 procedure Replace_Type_References_Generic (N : Node_Id; TName : Name_Id) is
10842 function Replace_Node (N : Node_Id) return Traverse_Result;
10843 -- Processes a single node in the traversal procedure below, checking
10844 -- if node N should be replaced, and if so, doing the replacement.
10846 procedure Replace_Type_Refs is new Traverse_Proc (Replace_Node);
10847 -- This instantiation provides the body of Replace_Type_References
10853 function Replace_Node (N : Node_Id) return Traverse_Result is
10858 -- Case of identifier
10860 if Nkind (N) = N_Identifier then
10862 -- If not the type name, all done with this node
10864 if Chars (N) /= TName then
10867 -- Otherwise do the replacement and we are done with this node
10870 Replace_Type_Reference (N);
10874 -- Case of selected component (which is what a qualification
10875 -- looks like in the unanalyzed tree, which is what we have.
10877 elsif Nkind (N) = N_Selected_Component then
10879 -- If selector name is not our type, keeping going (we might
10880 -- still have an occurrence of the type in the prefix).
10882 if Nkind (Selector_Name (N)) /= N_Identifier
10883 or else Chars (Selector_Name (N)) /= TName
10887 -- Selector name is our type, check qualification
10890 -- Loop through scopes and prefixes, doing comparison
10892 S := Current_Scope;
10895 -- Continue if no more scopes or scope with no name
10897 if No (S) or else Nkind (S) not in N_Has_Chars then
10901 -- Do replace if prefix is an identifier matching the
10902 -- scope that we are currently looking at.
10904 if Nkind (P) = N_Identifier
10905 and then Chars (P) = Chars (S)
10907 Replace_Type_Reference (N);
10911 -- Go check scope above us if prefix is itself of the
10912 -- form of a selected component, whose selector matches
10913 -- the scope we are currently looking at.
10915 if Nkind (P) = N_Selected_Component
10916 and then Nkind (Selector_Name (P)) = N_Identifier
10917 and then Chars (Selector_Name (P)) = Chars (S)
10922 -- For anything else, we don't have a match, so keep on
10923 -- going, there are still some weird cases where we may
10924 -- still have a replacement within the prefix.
10932 -- Continue for any other node kind
10940 Replace_Type_Refs (N);
10941 end Replace_Type_References_Generic;
10943 -------------------------
10944 -- Same_Representation --
10945 -------------------------
10947 function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
10948 T1 : constant Entity_Id := Underlying_Type (Typ1);
10949 T2 : constant Entity_Id := Underlying_Type (Typ2);
10952 -- A quick check, if base types are the same, then we definitely have
10953 -- the same representation, because the subtype specific representation
10954 -- attributes (Size and Alignment) do not affect representation from
10955 -- the point of view of this test.
10957 if Base_Type (T1) = Base_Type (T2) then
10960 elsif Is_Private_Type (Base_Type (T2))
10961 and then Base_Type (T1) = Full_View (Base_Type (T2))
10966 -- Tagged types never have differing representations
10968 if Is_Tagged_Type (T1) then
10972 -- Representations are definitely different if conventions differ
10974 if Convention (T1) /= Convention (T2) then
10978 -- Representations are different if component alignments or scalar
10979 -- storage orders differ.
10981 if (Is_Record_Type (T1) or else Is_Array_Type (T1))
10983 (Is_Record_Type (T2) or else Is_Array_Type (T2))
10985 (Component_Alignment (T1) /= Component_Alignment (T2)
10987 Reverse_Storage_Order (T1) /= Reverse_Storage_Order (T2))
10992 -- For arrays, the only real issue is component size. If we know the
10993 -- component size for both arrays, and it is the same, then that's
10994 -- good enough to know we don't have a change of representation.
10996 if Is_Array_Type (T1) then
10997 if Known_Component_Size (T1)
10998 and then Known_Component_Size (T2)
10999 and then Component_Size (T1) = Component_Size (T2)
11001 if VM_Target = No_VM then
11004 -- In VM targets the representation of arrays with aliased
11005 -- components differs from arrays with non-aliased components
11008 return Has_Aliased_Components (Base_Type (T1))
11010 Has_Aliased_Components (Base_Type (T2));
11015 -- Types definitely have same representation if neither has non-standard
11016 -- representation since default representations are always consistent.
11017 -- If only one has non-standard representation, and the other does not,
11018 -- then we consider that they do not have the same representation. They
11019 -- might, but there is no way of telling early enough.
11021 if Has_Non_Standard_Rep (T1) then
11022 if not Has_Non_Standard_Rep (T2) then
11026 return not Has_Non_Standard_Rep (T2);
11029 -- Here the two types both have non-standard representation, and we need
11030 -- to determine if they have the same non-standard representation.
11032 -- For arrays, we simply need to test if the component sizes are the
11033 -- same. Pragma Pack is reflected in modified component sizes, so this
11034 -- check also deals with pragma Pack.
11036 if Is_Array_Type (T1) then
11037 return Component_Size (T1) = Component_Size (T2);
11039 -- Tagged types always have the same representation, because it is not
11040 -- possible to specify different representations for common fields.
11042 elsif Is_Tagged_Type (T1) then
11045 -- Case of record types
11047 elsif Is_Record_Type (T1) then
11049 -- Packed status must conform
11051 if Is_Packed (T1) /= Is_Packed (T2) then
11054 -- Otherwise we must check components. Typ2 maybe a constrained
11055 -- subtype with fewer components, so we compare the components
11056 -- of the base types.
11059 Record_Case : declare
11060 CD1, CD2 : Entity_Id;
11062 function Same_Rep return Boolean;
11063 -- CD1 and CD2 are either components or discriminants. This
11064 -- function tests whether they have the same representation.
11070 function Same_Rep return Boolean is
11072 if No (Component_Clause (CD1)) then
11073 return No (Component_Clause (CD2));
11075 -- Note: at this point, component clauses have been
11076 -- normalized to the default bit order, so that the
11077 -- comparison of Component_Bit_Offsets is meaningful.
11080 Present (Component_Clause (CD2))
11082 Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
11084 Esize (CD1) = Esize (CD2);
11088 -- Start of processing for Record_Case
11091 if Has_Discriminants (T1) then
11093 -- The number of discriminants may be different if the
11094 -- derived type has fewer (constrained by values). The
11095 -- invisible discriminants retain the representation of
11096 -- the original, so the discrepancy does not per se
11097 -- indicate a different representation.
11099 CD1 := First_Discriminant (T1);
11100 CD2 := First_Discriminant (T2);
11101 while Present (CD1) and then Present (CD2) loop
11102 if not Same_Rep then
11105 Next_Discriminant (CD1);
11106 Next_Discriminant (CD2);
11111 CD1 := First_Component (Underlying_Type (Base_Type (T1)));
11112 CD2 := First_Component (Underlying_Type (Base_Type (T2)));
11113 while Present (CD1) loop
11114 if not Same_Rep then
11117 Next_Component (CD1);
11118 Next_Component (CD2);
11126 -- For enumeration types, we must check each literal to see if the
11127 -- representation is the same. Note that we do not permit enumeration
11128 -- representation clauses for Character and Wide_Character, so these
11129 -- cases were already dealt with.
11131 elsif Is_Enumeration_Type (T1) then
11132 Enumeration_Case : declare
11133 L1, L2 : Entity_Id;
11136 L1 := First_Literal (T1);
11137 L2 := First_Literal (T2);
11138 while Present (L1) loop
11139 if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
11148 end Enumeration_Case;
11150 -- Any other types have the same representation for these purposes
11155 end Same_Representation;
11157 --------------------------------
11158 -- Resolve_Iterable_Operation --
11159 --------------------------------
11161 procedure Resolve_Iterable_Operation
11163 Cursor : Entity_Id;
11172 if not Is_Overloaded (N) then
11173 if not Is_Entity_Name (N)
11174 or else Ekind (Entity (N)) /= E_Function
11175 or else Scope (Entity (N)) /= Scope (Typ)
11176 or else No (First_Formal (Entity (N)))
11177 or else Etype (First_Formal (Entity (N))) /= Typ
11179 Error_Msg_N ("iterable primitive must be local function name "
11180 & "whose first formal is an iterable type", N);
11185 F1 := First_Formal (Ent);
11186 if Nam = Name_First then
11188 -- First (Container) => Cursor
11190 if Etype (Ent) /= Cursor then
11191 Error_Msg_N ("primitive for First must yield a curosr", N);
11194 elsif Nam = Name_Next then
11196 -- Next (Container, Cursor) => Cursor
11198 F2 := Next_Formal (F1);
11200 if Etype (F2) /= Cursor
11201 or else Etype (Ent) /= Cursor
11202 or else Present (Next_Formal (F2))
11204 Error_Msg_N ("no match for Next iterable primitive", N);
11207 elsif Nam = Name_Has_Element then
11209 -- Has_Element (Container, Cursor) => Boolean
11211 F2 := Next_Formal (F1);
11212 if Etype (F2) /= Cursor
11213 or else Etype (Ent) /= Standard_Boolean
11214 or else Present (Next_Formal (F2))
11216 Error_Msg_N ("no match for Has_Element iterable primitive", N);
11219 elsif Nam = Name_Element then
11220 F2 := Next_Formal (F1);
11223 or else Etype (F2) /= Cursor
11224 or else Present (Next_Formal (F2))
11226 Error_Msg_N ("no match for Element iterable primitive", N);
11231 raise Program_Error;
11235 -- Overloaded case: find subprogram with proper signature.
11236 -- Caller will report error if no match is found.
11243 Get_First_Interp (N, I, It);
11244 while Present (It.Typ) loop
11245 if Ekind (It.Nam) = E_Function
11246 and then Scope (It.Nam) = Scope (Typ)
11247 and then Etype (First_Formal (It.Nam)) = Typ
11249 F1 := First_Formal (It.Nam);
11251 if Nam = Name_First then
11252 if Etype (It.Nam) = Cursor
11253 and then No (Next_Formal (F1))
11255 Set_Entity (N, It.Nam);
11259 elsif Nam = Name_Next then
11260 F2 := Next_Formal (F1);
11263 and then No (Next_Formal (F2))
11264 and then Etype (F2) = Cursor
11265 and then Etype (It.Nam) = Cursor
11267 Set_Entity (N, It.Nam);
11271 elsif Nam = Name_Has_Element then
11272 F2 := Next_Formal (F1);
11275 and then No (Next_Formal (F2))
11276 and then Etype (F2) = Cursor
11277 and then Etype (It.Nam) = Standard_Boolean
11279 Set_Entity (N, It.Nam);
11280 F2 := Next_Formal (F1);
11284 elsif Nam = Name_Element then
11285 F2 := Next_Formal (F1);
11288 and then No (Next_Formal (F2))
11289 and then Etype (F2) = Cursor
11291 Set_Entity (N, It.Nam);
11297 Get_Next_Interp (I, It);
11301 end Resolve_Iterable_Operation;
11307 procedure Set_Biased
11311 Biased : Boolean := True)
11315 Set_Has_Biased_Representation (E);
11317 if Warn_On_Biased_Representation then
11319 ("?B?" & Msg & " forces biased representation for&", N, E);
11324 --------------------
11325 -- Set_Enum_Esize --
11326 --------------------
11328 procedure Set_Enum_Esize (T : Entity_Id) is
11334 Init_Alignment (T);
11336 -- Find the minimum standard size (8,16,32,64) that fits
11338 Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
11339 Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
11342 if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
11343 Sz := Standard_Character_Size; -- May be > 8 on some targets
11345 elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
11348 elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
11351 else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
11356 if Hi < Uint_2**08 then
11357 Sz := Standard_Character_Size; -- May be > 8 on some targets
11359 elsif Hi < Uint_2**16 then
11362 elsif Hi < Uint_2**32 then
11365 else pragma Assert (Hi < Uint_2**63);
11370 -- That minimum is the proper size unless we have a foreign convention
11371 -- and the size required is 32 or less, in which case we bump the size
11372 -- up to 32. This is required for C and C++ and seems reasonable for
11373 -- all other foreign conventions.
11375 if Has_Foreign_Convention (T)
11376 and then Esize (T) < Standard_Integer_Size
11378 -- Don't do this if Short_Enums on target
11380 and then not Target_Short_Enums
11382 Init_Esize (T, Standard_Integer_Size);
11384 Init_Esize (T, Sz);
11386 end Set_Enum_Esize;
11388 -----------------------------
11389 -- Uninstall_Discriminants --
11390 -----------------------------
11392 procedure Uninstall_Discriminants (E : Entity_Id) is
11398 -- Discriminants have been made visible for type declarations and
11399 -- protected type declarations, not for subtype declarations.
11401 if Nkind (Parent (E)) /= N_Subtype_Declaration then
11402 Disc := First_Discriminant (E);
11403 while Present (Disc) loop
11404 if Disc /= Current_Entity (Disc) then
11405 Prev := Current_Entity (Disc);
11406 while Present (Prev)
11407 and then Present (Homonym (Prev))
11408 and then Homonym (Prev) /= Disc
11410 Prev := Homonym (Prev);
11416 Set_Is_Immediately_Visible (Disc, False);
11418 Outer := Homonym (Disc);
11419 while Present (Outer) and then Scope (Outer) = E loop
11420 Outer := Homonym (Outer);
11423 -- Reset homonym link of other entities, but do not modify link
11424 -- between entities in current scope, so that the back-end can
11425 -- have a proper count of local overloadings.
11428 Set_Name_Entity_Id (Chars (Disc), Outer);
11430 elsif Scope (Prev) /= Scope (Disc) then
11431 Set_Homonym (Prev, Outer);
11434 Next_Discriminant (Disc);
11437 end Uninstall_Discriminants;
11439 -------------------------------------------
11440 -- Uninstall_Discriminants_And_Pop_Scope --
11441 -------------------------------------------
11443 procedure Uninstall_Discriminants_And_Pop_Scope (E : Entity_Id) is
11445 if Has_Discriminants (E) then
11446 Uninstall_Discriminants (E);
11449 end Uninstall_Discriminants_And_Pop_Scope;
11451 ------------------------------
11452 -- Validate_Address_Clauses --
11453 ------------------------------
11455 procedure Validate_Address_Clauses is
11457 for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
11459 ACCR : Address_Clause_Check_Record
11460 renames Address_Clause_Checks.Table (J);
11464 X_Alignment : Uint;
11465 Y_Alignment : Uint;
11471 -- Skip processing of this entry if warning already posted
11473 if not Address_Warning_Posted (ACCR.N) then
11474 Expr := Original_Node (Expression (ACCR.N));
11478 X_Alignment := Alignment (ACCR.X);
11479 Y_Alignment := Alignment (ACCR.Y);
11481 -- Similarly obtain sizes
11483 X_Size := Esize (ACCR.X);
11484 Y_Size := Esize (ACCR.Y);
11486 -- Check for large object overlaying smaller one
11489 and then X_Size > Uint_0
11490 and then X_Size > Y_Size
11493 ("??& overlays smaller object", ACCR.N, ACCR.X);
11495 ("\??program execution may be erroneous", ACCR.N);
11496 Error_Msg_Uint_1 := X_Size;
11498 ("\??size of & is ^", ACCR.N, ACCR.X);
11499 Error_Msg_Uint_1 := Y_Size;
11501 ("\??size of & is ^", ACCR.N, ACCR.Y);
11503 -- Check for inadequate alignment, both of the base object
11504 -- and of the offset, if any.
11506 -- Note: we do not check the alignment if we gave a size
11507 -- warning, since it would likely be redundant.
11509 elsif Y_Alignment /= Uint_0
11510 and then (Y_Alignment < X_Alignment
11513 Nkind (Expr) = N_Attribute_Reference
11515 Attribute_Name (Expr) = Name_Address
11517 Has_Compatible_Alignment
11518 (ACCR.X, Prefix (Expr))
11519 /= Known_Compatible))
11522 ("??specified address for& may be inconsistent "
11523 & "with alignment", ACCR.N, ACCR.X);
11525 ("\??program execution may be erroneous (RM 13.3(27))",
11527 Error_Msg_Uint_1 := X_Alignment;
11529 ("\??alignment of & is ^", ACCR.N, ACCR.X);
11530 Error_Msg_Uint_1 := Y_Alignment;
11532 ("\??alignment of & is ^", ACCR.N, ACCR.Y);
11533 if Y_Alignment >= X_Alignment then
11535 ("\??but offset is not multiple of alignment", ACCR.N);
11541 end Validate_Address_Clauses;
11543 ---------------------------
11544 -- Validate_Independence --
11545 ---------------------------
11547 procedure Validate_Independence is
11548 SU : constant Uint := UI_From_Int (System_Storage_Unit);
11556 procedure Check_Array_Type (Atyp : Entity_Id);
11557 -- Checks if the array type Atyp has independent components, and
11558 -- if not, outputs an appropriate set of error messages.
11560 procedure No_Independence;
11561 -- Output message that independence cannot be guaranteed
11563 function OK_Component (C : Entity_Id) return Boolean;
11564 -- Checks one component to see if it is independently accessible, and
11565 -- if so yields True, otherwise yields False if independent access
11566 -- cannot be guaranteed. This is a conservative routine, it only
11567 -- returns True if it knows for sure, it returns False if it knows
11568 -- there is a problem, or it cannot be sure there is no problem.
11570 procedure Reason_Bad_Component (C : Entity_Id);
11571 -- Outputs continuation message if a reason can be determined for
11572 -- the component C being bad.
11574 ----------------------
11575 -- Check_Array_Type --
11576 ----------------------
11578 procedure Check_Array_Type (Atyp : Entity_Id) is
11579 Ctyp : constant Entity_Id := Component_Type (Atyp);
11582 -- OK if no alignment clause, no pack, and no component size
11584 if not Has_Component_Size_Clause (Atyp)
11585 and then not Has_Alignment_Clause (Atyp)
11586 and then not Is_Packed (Atyp)
11591 -- Check actual component size
11593 if not Known_Component_Size (Atyp)
11594 or else not (Addressable (Component_Size (Atyp))
11595 and then Component_Size (Atyp) < 64)
11596 or else Component_Size (Atyp) mod Esize (Ctyp) /= 0
11600 -- Bad component size, check reason
11602 if Has_Component_Size_Clause (Atyp) then
11603 P := Get_Attribute_Definition_Clause
11604 (Atyp, Attribute_Component_Size);
11606 if Present (P) then
11607 Error_Msg_Sloc := Sloc (P);
11608 Error_Msg_N ("\because of Component_Size clause#", N);
11613 if Is_Packed (Atyp) then
11614 P := Get_Rep_Pragma (Atyp, Name_Pack);
11616 if Present (P) then
11617 Error_Msg_Sloc := Sloc (P);
11618 Error_Msg_N ("\because of pragma Pack#", N);
11623 -- No reason found, just return
11628 -- Array type is OK independence-wise
11631 end Check_Array_Type;
11633 ---------------------
11634 -- No_Independence --
11635 ---------------------
11637 procedure No_Independence is
11639 if Pragma_Name (N) = Name_Independent then
11640 Error_Msg_NE ("independence cannot be guaranteed for&", N, E);
11643 ("independent components cannot be guaranteed for&", N, E);
11645 end No_Independence;
11651 function OK_Component (C : Entity_Id) return Boolean is
11652 Rec : constant Entity_Id := Scope (C);
11653 Ctyp : constant Entity_Id := Etype (C);
11656 -- OK if no component clause, no Pack, and no alignment clause
11658 if No (Component_Clause (C))
11659 and then not Is_Packed (Rec)
11660 and then not Has_Alignment_Clause (Rec)
11665 -- Here we look at the actual component layout. A component is
11666 -- addressable if its size is a multiple of the Esize of the
11667 -- component type, and its starting position in the record has
11668 -- appropriate alignment, and the record itself has appropriate
11669 -- alignment to guarantee the component alignment.
11671 -- Make sure sizes are static, always assume the worst for any
11672 -- cases where we cannot check static values.
11674 if not (Known_Static_Esize (C)
11676 Known_Static_Esize (Ctyp))
11681 -- Size of component must be addressable or greater than 64 bits
11682 -- and a multiple of bytes.
11684 if not Addressable (Esize (C)) and then Esize (C) < Uint_64 then
11688 -- Check size is proper multiple
11690 if Esize (C) mod Esize (Ctyp) /= 0 then
11694 -- Check alignment of component is OK
11696 if not Known_Component_Bit_Offset (C)
11697 or else Component_Bit_Offset (C) < Uint_0
11698 or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0
11703 -- Check alignment of record type is OK
11705 if not Known_Alignment (Rec)
11706 or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
11711 -- All tests passed, component is addressable
11716 --------------------------
11717 -- Reason_Bad_Component --
11718 --------------------------
11720 procedure Reason_Bad_Component (C : Entity_Id) is
11721 Rec : constant Entity_Id := Scope (C);
11722 Ctyp : constant Entity_Id := Etype (C);
11725 -- If component clause present assume that's the problem
11727 if Present (Component_Clause (C)) then
11728 Error_Msg_Sloc := Sloc (Component_Clause (C));
11729 Error_Msg_N ("\because of Component_Clause#", N);
11733 -- If pragma Pack clause present, assume that's the problem
11735 if Is_Packed (Rec) then
11736 P := Get_Rep_Pragma (Rec, Name_Pack);
11738 if Present (P) then
11739 Error_Msg_Sloc := Sloc (P);
11740 Error_Msg_N ("\because of pragma Pack#", N);
11745 -- See if record has bad alignment clause
11747 if Has_Alignment_Clause (Rec)
11748 and then Known_Alignment (Rec)
11749 and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
11751 P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment);
11753 if Present (P) then
11754 Error_Msg_Sloc := Sloc (P);
11755 Error_Msg_N ("\because of Alignment clause#", N);
11759 -- Couldn't find a reason, so return without a message
11762 end Reason_Bad_Component;
11764 -- Start of processing for Validate_Independence
11767 for J in Independence_Checks.First .. Independence_Checks.Last loop
11768 N := Independence_Checks.Table (J).N;
11769 E := Independence_Checks.Table (J).E;
11770 IC := Pragma_Name (N) = Name_Independent_Components;
11772 -- Deal with component case
11774 if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then
11775 if not OK_Component (E) then
11777 Reason_Bad_Component (E);
11782 -- Deal with record with Independent_Components
11784 if IC and then Is_Record_Type (E) then
11785 Comp := First_Component_Or_Discriminant (E);
11786 while Present (Comp) loop
11787 if not OK_Component (Comp) then
11789 Reason_Bad_Component (Comp);
11793 Next_Component_Or_Discriminant (Comp);
11797 -- Deal with address clause case
11799 if Is_Object (E) then
11800 Addr := Address_Clause (E);
11802 if Present (Addr) then
11804 Error_Msg_Sloc := Sloc (Addr);
11805 Error_Msg_N ("\because of Address clause#", N);
11810 -- Deal with independent components for array type
11812 if IC and then Is_Array_Type (E) then
11813 Check_Array_Type (E);
11816 -- Deal with independent components for array object
11818 if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then
11819 Check_Array_Type (Etype (E));
11824 end Validate_Independence;
11826 ------------------------------
11827 -- Validate_Iterable_Aspect --
11828 ------------------------------
11830 procedure Validate_Iterable_Aspect (Typ : Entity_Id; ASN : Node_Id) is
11835 Cursor : constant Entity_Id := Get_Cursor_Type (ASN, Typ);
11837 First_Id : Entity_Id;
11838 Next_Id : Entity_Id;
11839 Has_Element_Id : Entity_Id;
11840 Element_Id : Entity_Id;
11843 -- If previous error aspect is unusable
11845 if Cursor = Any_Type then
11851 Has_Element_Id := Empty;
11852 Element_Id := Empty;
11854 -- Each expression must resolve to a function with the proper signature
11856 Assoc := First (Component_Associations (Expression (ASN)));
11857 while Present (Assoc) loop
11858 Expr := Expression (Assoc);
11861 Prim := First (Choices (Assoc));
11863 if Nkind (Prim) /= N_Identifier
11864 or else Present (Next (Prim))
11866 Error_Msg_N ("illegal name in association", Prim);
11868 elsif Chars (Prim) = Name_First then
11869 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_First);
11870 First_Id := Entity (Expr);
11872 elsif Chars (Prim) = Name_Next then
11873 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Next);
11874 Next_Id := Entity (Expr);
11876 elsif Chars (Prim) = Name_Has_Element then
11877 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Has_Element);
11878 Has_Element_Id := Entity (Expr);
11880 elsif Chars (Prim) = Name_Element then
11881 Resolve_Iterable_Operation (Expr, Cursor, Typ, Name_Element);
11882 Element_Id := Entity (Expr);
11885 Error_Msg_N ("invalid name for iterable function", Prim);
11891 if No (First_Id) then
11892 Error_Msg_N ("match for First primitive not found", ASN);
11894 elsif No (Next_Id) then
11895 Error_Msg_N ("match for Next primitive not found", ASN);
11897 elsif No (Has_Element_Id) then
11898 Error_Msg_N ("match for Has_Element primitive not found", ASN);
11900 elsif No (Element_Id) then
11903 end Validate_Iterable_Aspect;
11905 -----------------------------------
11906 -- Validate_Unchecked_Conversion --
11907 -----------------------------------
11909 procedure Validate_Unchecked_Conversion
11911 Act_Unit : Entity_Id)
11913 Source : Entity_Id;
11914 Target : Entity_Id;
11918 -- Obtain source and target types. Note that we call Ancestor_Subtype
11919 -- here because the processing for generic instantiation always makes
11920 -- subtypes, and we want the original frozen actual types.
11922 -- If we are dealing with private types, then do the check on their
11923 -- fully declared counterparts if the full declarations have been
11924 -- encountered (they don't have to be visible, but they must exist).
11926 Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
11928 if Is_Private_Type (Source)
11929 and then Present (Underlying_Type (Source))
11931 Source := Underlying_Type (Source);
11934 Target := Ancestor_Subtype (Etype (Act_Unit));
11936 -- If either type is generic, the instantiation happens within a generic
11937 -- unit, and there is nothing to check. The proper check will happen
11938 -- when the enclosing generic is instantiated.
11940 if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
11944 if Is_Private_Type (Target)
11945 and then Present (Underlying_Type (Target))
11947 Target := Underlying_Type (Target);
11950 -- Source may be unconstrained array, but not target
11952 if Is_Array_Type (Target) and then not Is_Constrained (Target) then
11954 ("unchecked conversion to unconstrained array not allowed", N);
11958 -- Warn if conversion between two different convention pointers
11960 if Is_Access_Type (Target)
11961 and then Is_Access_Type (Source)
11962 and then Convention (Target) /= Convention (Source)
11963 and then Warn_On_Unchecked_Conversion
11965 -- Give warnings for subprogram pointers only on most targets. The
11966 -- exception is VMS, where data pointers can have different lengths
11967 -- depending on the pointer convention.
11969 if Is_Access_Subprogram_Type (Target)
11970 or else Is_Access_Subprogram_Type (Source)
11971 or else OpenVMS_On_Target
11974 ("?z?conversion between pointers with different conventions!",
11979 -- Warn if one of the operands is Ada.Calendar.Time. Do not emit a
11980 -- warning when compiling GNAT-related sources.
11982 if Warn_On_Unchecked_Conversion
11983 and then not In_Predefined_Unit (N)
11984 and then RTU_Loaded (Ada_Calendar)
11986 (Chars (Source) = Name_Time
11988 Chars (Target) = Name_Time)
11990 -- If Ada.Calendar is loaded and the name of one of the operands is
11991 -- Time, there is a good chance that this is Ada.Calendar.Time.
11994 Calendar_Time : constant Entity_Id :=
11995 Full_View (RTE (RO_CA_Time));
11997 pragma Assert (Present (Calendar_Time));
11999 if Source = Calendar_Time or else Target = Calendar_Time then
12001 ("?z?representation of 'Time values may change between " &
12002 "'G'N'A'T versions", N);
12007 -- Make entry in unchecked conversion table for later processing by
12008 -- Validate_Unchecked_Conversions, which will check sizes and alignments
12009 -- (using values set by the back-end where possible). This is only done
12010 -- if the appropriate warning is active.
12012 if Warn_On_Unchecked_Conversion then
12013 Unchecked_Conversions.Append
12014 (New_Val => UC_Entry'(Eloc => Sloc (N),
12017 Act_Unit => Act_Unit));
12019 -- If both sizes are known statically now, then back end annotation
12020 -- is not required to do a proper check but if either size is not
12021 -- known statically, then we need the annotation.
12023 if Known_Static_RM_Size (Source)
12025 Known_Static_RM_Size (Target)
12029 Back_Annotate_Rep_Info := True;
12033 -- If unchecked conversion to access type, and access type is declared
12034 -- in the same unit as the unchecked conversion, then set the flag
12035 -- No_Strict_Aliasing (no strict aliasing is implicit here)
12037 if Is_Access_Type (Target) and then
12038 In_Same_Source_Unit (Target, N)
12040 Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
12043 -- Generate N_Validate_Unchecked_Conversion node for back end in case
12044 -- the back end needs to perform special validation checks.
12046 -- Shouldn't this be in Exp_Ch13, since the check only gets done if we
12047 -- have full expansion and the back end is called ???
12050 Make_Validate_Unchecked_Conversion (Sloc (N));
12051 Set_Source_Type (Vnode, Source);
12052 Set_Target_Type (Vnode, Target);
12054 -- If the unchecked conversion node is in a list, just insert before it.
12055 -- If not we have some strange case, not worth bothering about.
12057 if Is_List_Member (N) then
12058 Insert_After (N, Vnode);
12060 end Validate_Unchecked_Conversion;
12062 ------------------------------------
12063 -- Validate_Unchecked_Conversions --
12064 ------------------------------------
12066 procedure Validate_Unchecked_Conversions is
12068 for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
12070 T : UC_Entry renames Unchecked_Conversions.Table (N);
12072 Eloc : constant Source_Ptr := T.Eloc;
12073 Source : constant Entity_Id := T.Source;
12074 Target : constant Entity_Id := T.Target;
12075 Act_Unit : constant Entity_Id := T.Act_Unit;
12081 -- Skip if function marked as warnings off
12083 if Warnings_Off (Act_Unit) then
12087 -- This validation check, which warns if we have unequal sizes for
12088 -- unchecked conversion, and thus potentially implementation
12089 -- dependent semantics, is one of the few occasions on which we
12090 -- use the official RM size instead of Esize. See description in
12091 -- Einfo "Handling of Type'Size Values" for details.
12093 if Serious_Errors_Detected = 0
12094 and then Known_Static_RM_Size (Source)
12095 and then Known_Static_RM_Size (Target)
12097 -- Don't do the check if warnings off for either type, note the
12098 -- deliberate use of OR here instead of OR ELSE to get the flag
12099 -- Warnings_Off_Used set for both types if appropriate.
12101 and then not (Has_Warnings_Off (Source)
12103 Has_Warnings_Off (Target))
12105 Source_Siz := RM_Size (Source);
12106 Target_Siz := RM_Size (Target);
12108 if Source_Siz /= Target_Siz then
12110 ("?z?types for unchecked conversion have different sizes!",
12113 if All_Errors_Mode then
12114 Error_Msg_Name_1 := Chars (Source);
12115 Error_Msg_Uint_1 := Source_Siz;
12116 Error_Msg_Name_2 := Chars (Target);
12117 Error_Msg_Uint_2 := Target_Siz;
12118 Error_Msg ("\size of % is ^, size of % is ^?z?", Eloc);
12120 Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
12122 if Is_Discrete_Type (Source)
12124 Is_Discrete_Type (Target)
12126 if Source_Siz > Target_Siz then
12128 ("\?z?^ high order bits of source will "
12129 & "be ignored!", Eloc);
12131 elsif Is_Unsigned_Type (Source) then
12133 ("\?z?source will be extended with ^ high order "
12134 & "zero bits!", Eloc);
12138 ("\?z?source will be extended with ^ high order "
12139 & "sign bits!", Eloc);
12142 elsif Source_Siz < Target_Siz then
12143 if Is_Discrete_Type (Target) then
12144 if Bytes_Big_Endian then
12146 ("\?z?target value will include ^ undefined "
12147 & "low order bits!", Eloc);
12150 ("\?z?target value will include ^ undefined "
12151 & "high order bits!", Eloc);
12156 ("\?z?^ trailing bits of target value will be "
12157 & "undefined!", Eloc);
12160 else pragma Assert (Source_Siz > Target_Siz);
12162 ("\?z?^ trailing bits of source will be ignored!",
12169 -- If both types are access types, we need to check the alignment.
12170 -- If the alignment of both is specified, we can do it here.
12172 if Serious_Errors_Detected = 0
12173 and then Is_Access_Type (Source)
12174 and then Is_Access_Type (Target)
12175 and then Target_Strict_Alignment
12176 and then Present (Designated_Type (Source))
12177 and then Present (Designated_Type (Target))
12180 D_Source : constant Entity_Id := Designated_Type (Source);
12181 D_Target : constant Entity_Id := Designated_Type (Target);
12184 if Known_Alignment (D_Source)
12186 Known_Alignment (D_Target)
12189 Source_Align : constant Uint := Alignment (D_Source);
12190 Target_Align : constant Uint := Alignment (D_Target);
12193 if Source_Align < Target_Align
12194 and then not Is_Tagged_Type (D_Source)
12196 -- Suppress warning if warnings suppressed on either
12197 -- type or either designated type. Note the use of
12198 -- OR here instead of OR ELSE. That is intentional,
12199 -- we would like to set flag Warnings_Off_Used in
12200 -- all types for which warnings are suppressed.
12202 and then not (Has_Warnings_Off (D_Source)
12204 Has_Warnings_Off (D_Target)
12206 Has_Warnings_Off (Source)
12208 Has_Warnings_Off (Target))
12210 Error_Msg_Uint_1 := Target_Align;
12211 Error_Msg_Uint_2 := Source_Align;
12212 Error_Msg_Node_1 := D_Target;
12213 Error_Msg_Node_2 := D_Source;
12215 ("?z?alignment of & (^) is stricter than "
12216 & "alignment of & (^)!", Eloc);
12218 ("\?z?resulting access value may have invalid "
12219 & "alignment!", Eloc);
12230 end Validate_Unchecked_Conversions;